2018-04-11 19:49:35 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Grocy\Controllers;
|
|
|
|
|
2018-04-12 21:13:38 +02:00
|
|
|
use \Grocy\Services\DatabaseService;
|
|
|
|
use \Grocy\Services\ApplicationService;
|
2018-04-16 19:11:32 +02:00
|
|
|
use \Grocy\Services\LocalizationService;
|
2018-09-30 11:17:28 +02:00
|
|
|
use \Grocy\Services\UsersService;
|
2018-04-11 19:49:35 +02:00
|
|
|
|
|
|
|
class BaseController
|
|
|
|
{
|
2020-02-11 17:42:03 +01:00
|
|
|
public function __construct(\DI\Container $container) {
|
2018-04-11 19:49:35 +02:00
|
|
|
$databaseService = new DatabaseService();
|
|
|
|
$this->Database = $databaseService->GetDbConnection();
|
2018-07-14 18:23:41 +02:00
|
|
|
|
2018-07-24 19:41:35 +02:00
|
|
|
$localizationService = new LocalizationService(GROCY_CULTURE);
|
2018-07-14 18:23:41 +02:00
|
|
|
$this->LocalizationService = $localizationService;
|
2018-04-12 21:13:38 +02:00
|
|
|
|
2019-09-18 16:18:15 +02:00
|
|
|
$applicationService = new ApplicationService();
|
|
|
|
$versionInfo = $applicationService->GetInstalledVersion();
|
2018-09-25 08:55:25 +02:00
|
|
|
|
2020-02-11 17:42:03 +01:00
|
|
|
$view = $container->get('view');
|
|
|
|
$this->View = $view;
|
|
|
|
|
|
|
|
$view->set('version', $versionInfo->Version);
|
|
|
|
$view->set('releaseDate', $versionInfo->ReleaseDate);
|
|
|
|
$view->set('__t', function(string $text, ...$placeholderValues) use($localizationService)
|
2018-04-16 19:11:32 +02:00
|
|
|
{
|
2019-05-01 20:19:18 +02:00
|
|
|
return $localizationService->__t($text, $placeholderValues);
|
2018-04-16 19:11:32 +02:00
|
|
|
});
|
2020-02-11 17:42:03 +01:00
|
|
|
$view->set('__n', function($number, $singularForm, $pluralForm) use($localizationService)
|
2019-05-01 20:19:18 +02:00
|
|
|
{
|
|
|
|
return $localizationService->__n($number, $singularForm, $pluralForm);
|
|
|
|
});
|
2020-02-11 17:42:03 +01:00
|
|
|
$view->set('GettextPo', $localizationService->GetPoAsJsonString());
|
2019-05-01 20:19:18 +02:00
|
|
|
|
2020-02-11 17:42:03 +01:00
|
|
|
$view->set('U', function($relativePath, $isResource = false) use($container)
|
2018-04-18 19:03:39 +02:00
|
|
|
{
|
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-02-11 17:42:03 +01:00
|
|
|
$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-02-11 17:42:03 +01:00
|
|
|
$view->set('featureFlags', $constants);
|
2019-03-03 18:20:06 +01:00
|
|
|
|
2020-02-11 17:42:03 +01:00
|
|
|
$view->set('userentitiesForSidebar', $this->Database->userentities()->where('show_in_sidebar_menu = 1')->orderBy('name'));
|
2019-09-18 16:18:15 +02:00
|
|
|
|
2018-11-17 12:57:35 +01:00
|
|
|
try
|
|
|
|
{
|
2018-09-30 11:17:28 +02:00
|
|
|
$usersService = new UsersService();
|
2018-11-17 12:57:35 +01:00
|
|
|
if (defined('GROCY_USER_ID'))
|
|
|
|
{
|
2020-02-11 17:42:03 +01:00
|
|
|
$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-02-11 17:42:03 +01:00
|
|
|
$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...
|
|
|
|
}
|
|
|
|
|
2018-04-18 19:03:39 +02:00
|
|
|
$this->AppContainer = $container;
|
2018-04-11 19:49:35 +02:00
|
|
|
}
|
2020-02-11 19:36:15 +01:00
|
|
|
|
2018-04-11 19:49:35 +02:00
|
|
|
protected $AppContainer;
|
|
|
|
protected $Database;
|
2018-07-14 18:23:41 +02:00
|
|
|
protected $LocalizationService;
|
2020-02-11 17:42:03 +01:00
|
|
|
protected $View;
|
2018-04-11 19:49:35 +02:00
|
|
|
}
|