2018-04-11 19:49:35 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Grocy\Controllers;
|
|
|
|
|
|
2026-04-24 19:59:42 +02:00
|
|
|
use DI\Container;
|
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;
|
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
|
|
|
{
|
2020-03-01 23:47:47 +07:00
|
|
|
$this->AppContainer = $container;
|
|
|
|
|
$this->View = $container->get('view');
|
2026-04-20 22:46:47 +02:00
|
|
|
$this->DB = DatabaseService::GetInstance()->GetDbConnection();
|
2020-03-01 23:47:47 +07:00
|
|
|
}
|
|
|
|
|
|
2021-07-16 17:32:08 +02:00
|
|
|
protected $AppContainer;
|
2025-01-12 13:58:47 +01:00
|
|
|
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 = [])
|
2020-03-01 23:47:47 +07:00
|
|
|
{
|
|
|
|
|
$container = $this->AppContainer;
|
2018-09-25 08:55:25 +02:00
|
|
|
|
2026-04-20 22:46:47 +02:00
|
|
|
$versionInfo = ApplicationService::GetInstance()->GetInstalledVersion();
|
2020-03-01 23:47:47 +07:00
|
|
|
$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();
|
2023-08-02 18:44:30 +02:00
|
|
|
$this->View->set('__t', function (string $text, ...$placeholderValues) use ($localizationService)
|
|
|
|
|
{
|
2019-05-01 20:19:18 +02:00
|
|
|
return $localizationService->__t($text, $placeholderValues);
|
2018-04-16 19:11:32 +02:00
|
|
|
});
|
2023-08-02 18:44:30 +02:00
|
|
|
$this->View->set('__n', function ($number, $singularForm, $pluralForm, $isQu = false) use ($localizationService)
|
|
|
|
|
{
|
2022-02-06 21:09:34 +01:00
|
|
|
return $localizationService->__n($number, $singularForm, $pluralForm, $isQu);
|
2019-05-01 20:19:18 +02:00
|
|
|
});
|
2021-07-12 21:20:39 +02:00
|
|
|
$this->View->set('LocalizationStrings', $localizationService->GetPoAsJsonString());
|
2022-02-06 21:09:34 +01:00
|
|
|
$this->View->set('LocalizationStringsQu', $localizationService->GetPoAsJsonStringQu());
|
2019-05-01 20:19:18 +02:00
|
|
|
|
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);
|
|
|
|
|
|
2023-08-02 18:44:30 +02:00
|
|
|
$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);
|
2018-04-18 19:03:39 +02:00
|
|
|
});
|
|
|
|
|
|
2018-11-17 12:57:35 +01:00
|
|
|
$embedded = false;
|
2020-02-11 17:42:03 +01:00
|
|
|
if (isset($_GET['embedded']))
|
2018-11-17 12:57:35 +01:00
|
|
|
{
|
|
|
|
|
$embedded = true;
|
|
|
|
|
}
|
2020-03-01 23:47:47 +07:00
|
|
|
$this->View->set('embedded', $embedded);
|
2018-11-17 12:57:35 +01:00
|
|
|
|
2019-03-03 18:20:06 +01:00
|
|
|
$constants = get_defined_constants();
|
|
|
|
|
foreach ($constants as $constant => $value)
|
|
|
|
|
{
|
|
|
|
|
if (substr($constant, 0, 19) !== 'GROCY_FEATURE_FLAG_')
|
|
|
|
|
{
|
|
|
|
|
unset($constants[$constant]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-01 23:47:47 +07:00
|
|
|
$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);
|
2020-03-01 23:47:47 +07:00
|
|
|
}
|
2019-09-18 16:18:15 +02:00
|
|
|
|
2026-04-20 22:46:47 +02:00
|
|
|
protected function RenderPage($response, $viewName, $data = [])
|
2020-03-01 23:47:47 +07:00
|
|
|
{
|
2026-04-20 22:46:47 +02:00
|
|
|
$this->View->set('userentitiesForSidebar', $this->DB->userentities()->where('show_in_sidebar_menu = 1')->orderBy('name'));
|
2018-11-17 12:57:35 +01:00
|
|
|
try
|
|
|
|
|
{
|
2026-04-20 22:46:47 +02:00
|
|
|
$usersService = UsersService::GetInstance();
|
2018-11-17 12:57:35 +01:00
|
|
|
if (defined('GROCY_USER_ID'))
|
|
|
|
|
{
|
2020-03-01 23:47:47 +07:00
|
|
|
$this->View->set('userSettings', $usersService->GetUserSettings(GROCY_USER_ID));
|
2018-10-20 03:07:05 -04:00
|
|
|
}
|
2019-06-08 15:54:56 +02:00
|
|
|
else
|
|
|
|
|
{
|
2020-03-01 23:47:47 +07:00
|
|
|
$this->View->set('userSettings', null);
|
2019-06-08 15:54:56 +02:00
|
|
|
}
|
2018-09-30 11:17:28 +02:00
|
|
|
}
|
|
|
|
|
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);
|
2020-03-01 23:47:47 +07:00
|
|
|
}
|
2018-04-11 19:49:35 +02:00
|
|
|
}
|