Files
grocy/controllers/BaseController.php
T

118 lines
3.2 KiB
PHP
Raw Normal View History

2018-04-11 19:49:35 +02:00
<?php
namespace Grocy\Controllers;
2020-08-29 12:05:32 +02:00
use Grocy\Controllers\Users\User;
2020-08-31 20:40:31 +02:00
use Grocy\Services\ApplicationService;
use Grocy\Services\DatabaseService;
use Grocy\Services\LocalizationService;
use Grocy\Services\UsersService;
2023-07-29 14:02:56 +02:00
use DI\Container;
2018-04-11 19:49:35 +02:00
class BaseController
{
2023-07-29 14:02:56 +02:00
public function __construct(Container $container)
2020-08-31 20:40:31 +02:00
{
$this->AppContainer = $container;
$this->View = $container->get('view');
2026-04-20 22:46:47 +02:00
$this->DB = DatabaseService::GetInstance()->GetDbConnection();
}
2021-07-16 17:32:08 +02:00
protected $AppContainer;
protected $View;
2026-04-20 22:46:47 +02:00
protected $DB;
2023-08-01 21:23:59 +02:00
2026-04-20 22:46:47 +02:00
protected function Render($response, $viewName, $data = [])
{
$container = $this->AppContainer;
2026-04-20 22:46:47 +02:00
$versionInfo = ApplicationService::GetInstance()->GetInstalledVersion();
$this->View->set('version', $versionInfo->Version);
2020-02-11 17:42:03 +01:00
2026-04-20 22:46:47 +02:00
$localizationService = LocalizationService::GetInstance();
$this->View->set('__t', function (string $text, ...$placeholderValues) use ($localizationService)
{
return $localizationService->__t($text, $placeholderValues);
2018-04-16 19:11:32 +02:00
});
$this->View->set('__n', function ($number, $singularForm, $pluralForm, $isQu = false) use ($localizationService)
{
return $localizationService->__n($number, $singularForm, $pluralForm, $isQu);
});
2021-07-12 21:20:39 +02:00
$this->View->set('LocalizationStrings', $localizationService->GetPoAsJsonString());
$this->View->set('LocalizationStringsQu', $localizationService->GetPoAsJsonStringQu());
2020-12-11 18:06:32 +01:00
// TODO: Better handle this generically based on the current language (header in .po file?)
$dir = 'ltr';
if (GROCY_LOCALE == 'he_IL')
{
$dir = 'rtl';
}
$this->View->set('dir', $dir);
$this->View->set('U', function ($relativePath, $isResource = false) use ($container)
{
2020-02-11 17:42:03 +01:00
return $container->get('UrlManager')->ConstructUrl($relativePath, $isResource);
});
$embedded = false;
2020-02-11 17:42:03 +01:00
if (isset($_GET['embedded']))
{
$embedded = true;
}
$this->View->set('embedded', $embedded);
$constants = get_defined_constants();
foreach ($constants as $constant => $value)
{
if (substr($constant, 0, 19) !== 'GROCY_FEATURE_FLAG_')
{
unset($constants[$constant]);
}
}
$this->View->set('featureFlags', $constants);
2023-05-20 18:20:30 +02:00
2020-08-29 12:05:32 +02:00
if (GROCY_AUTHENTICATED)
{
$this->View->set('permissions', User::PermissionList());
2026-04-20 22:46:47 +02:00
$decimalPlacesAmounts = UsersService::GetInstance()->GetUserSetting(GROCY_USER_ID, 'stock_decimal_places_amounts');
2021-01-12 10:40:14 +01:00
if ($decimalPlacesAmounts <= 0)
{
$defaultMinAmount = 1;
}
else
{
$defaultMinAmount = '0.' . str_repeat('0', $decimalPlacesAmounts - 1) . '1';
}
$this->View->set('DEFAULT_MIN_AMOUNT', $defaultMinAmount);
2020-12-23 19:56:37 +01:00
}
2023-05-20 18:20:30 +02:00
$this->View->set('viewName', $viewName);
2026-04-20 22:46:47 +02:00
return $this->View->Render($response, $viewName, $data);
}
2026-04-20 22:46:47 +02:00
protected function RenderPage($response, $viewName, $data = [])
{
2026-04-20 22:46:47 +02:00
$this->View->set('userentitiesForSidebar', $this->DB->userentities()->where('show_in_sidebar_menu = 1')->orderBy('name'));
try
{
2026-04-20 22:46:47 +02:00
$usersService = UsersService::GetInstance();
if (defined('GROCY_USER_ID'))
{
$this->View->set('userSettings', $usersService->GetUserSettings(GROCY_USER_ID));
2018-10-20 03:07:05 -04:00
}
else
{
$this->View->set('userSettings', null);
}
}
catch (\Exception $ex)
{
// Happens when database is not initialised or migrated...
}
2026-04-20 22:46:47 +02:00
return $this->Render($response, $viewName, $data);
}
2018-04-11 19:49:35 +02:00
}