From 3a5ff74d71880a8240114a7265f23ce550239073 Mon Sep 17 00:00:00 2001 From: Leonardo Zanotti Date: Mon, 15 Jul 2024 14:52:31 -0300 Subject: [PATCH 1/2] =?UTF-8?q?FIX=20-=20dropdown=20de=20Tipos=20de=20espa?= =?UTF-8?q?=C3=A7o=20agora=20aparecem=20em=20ordem=20alfab=C3=A9tica?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/App.php | 1214 +++++++------ src/core/Entity.php | 535 +++--- src/core/Theme.php | 372 ++-- .../assets/js/components-base/API.js | 758 ++++---- .../assets/js/components-base/Entity.js | 1587 +++++++++-------- .../components/create-space/script.js | 191 +- .../components/create-space/template.php | 47 +- .../components/entity-field/script.js | 467 ++--- .../components/entity-field/template.php | 38 +- 9 files changed, 2790 insertions(+), 2419 deletions(-) diff --git a/src/core/App.php b/src/core/App.php index a945563e9b..43c2c42694 100644 --- a/src/core/App.php +++ b/src/core/App.php @@ -1,4 +1,5 @@ startTime = microtime(true); $this->id = $id; @@ -342,18 +346,19 @@ protected function __construct(string $id) { * @param array $config * @return array */ - function parseConfig(array $config): array { - if(empty($config['base.url'])){ - $config['base.url'] = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'https://' : 'http://') . - (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost') . '/'; + function parseConfig(array $config): array + { + if (empty($config['base.url'])) { + $config['base.url'] = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'https://' : 'http://') . + (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost') . '/'; } - if(empty($config['base.assetUrl'])){ + if (empty($config['base.assetUrl'])) { $config['base.assetUrl'] = $config['base.url'] . 'assets/'; } - if(!is_array($config['app.verifiedSealsIds'])) { - if (is_numeric($config['app.verifiedSealsIds'])) { + if (!is_array($config['app.verifiedSealsIds'])) { + if (is_numeric($config['app.verifiedSealsIds'])) { $config['app.verifiedSealsIds'] = [(int) $config['app.verifiedSealsIds']]; } else { $config['app.verifiedSealsIds'] = []; @@ -375,30 +380,31 @@ function parseConfig(array $config): array { * @throws LogicException * @throws Exception */ - function init(array $config) { + function init(array $config) + { $config = $this->parseConfig($config); - + $this->_config = &$config; $this->config = &$config; - foreach($this->config['middlewares'] as $middleware) { + foreach ($this->config['middlewares'] as $middleware) { $this->slim->add($middleware); } // necessário para obter o endereço ip da origem do request $this->slim->add(new \RKA\Middleware\IpAddress); - if($config['app.mode'] == APPMODE_DEVELOPMENT){ + if ($config['app.mode'] == APPMODE_DEVELOPMENT) { error_reporting(E_ALL ^ E_STRICT); } session_save_path(SESSIONS_SAVE_PATH); session_start(); - if($config['app.offline']){ + if ($config['app.offline']) { $bypass_callable = $config['app.offlineBypassFunction']; - - if (php_sapi_name()!=="cli" && (!is_callable($bypass_callable) || !$bypass_callable())) { + + if (php_sapi_name() !== "cli" && (!is_callable($bypass_callable) || !$bypass_callable())) { http_response_code(307); header('Location: ' . $config['app.offlineUrl']); die; @@ -415,7 +421,7 @@ function init(array $config) { $this->_initAutoloader(); $this->_initCache(); $this->_initDoctrine(); - + $this->_initSubsite(); $this->_initRouteManager(); @@ -438,7 +444,7 @@ function init(array $config) { $this->_initStorage(); - if(defined('DB_UPDATES_FILE') && file_exists(DB_UPDATES_FILE)) + if (defined('DB_UPDATES_FILE') && file_exists(DB_UPDATES_FILE)) $this->_dbUpdates(); $this->applyHookBoundTo($this, 'app.init:after'); @@ -461,7 +467,8 @@ function init(array $config) { * @throws TransactionRequiredException * @throws WorkflowRequest */ - public function run() { + public function run() + { $this->applyHookBoundTo($this, 'mapasculturais.run:before'); $this->slim->run(); $this->persistPCachePendingQueue(); @@ -474,45 +481,43 @@ public function run() { * * @return void */ - protected function _initLogger() { + protected function _initLogger() + { $processors = $this->config['monolog.processors']; - + $handlers = []; if (is_string($this->config['monolog.handlers'])) { $handlers_config = explode(',', $this->config['monolog.handlers']); - - foreach($handlers_config as $handler_config) { + + foreach ($handlers_config as $handler_config) { $handler_config = explode(':', $handler_config); - + $type = $handler_config[0]; $level = $handler_config[1] ?? $this->config['monolog.defaultLevel']; - + if ($type == 'file') { $handlers[] = new Handler\StreamHandler($this->config['monolog.logsDir'] . 'app.log', 'DEBUG'); - } else if ($type == 'error_log') { $formatter = new LineFormatter("%message%"); $handler = new Handler\ErrorLogHandler(level: $level); $handler->setFormatter($formatter); $handlers[] = $handler; - } else if ($type == 'browser') { $formatter = new LineFormatter("%message%"); $handler = new Handler\BrowserConsoleHandler(level: $level); $handler->setFormatter($formatter); $handlers[] = $handler; - } else if ($type == 'telegram') { $api_key = (string) $this->config['monolog.telegram.apiKey'] ?? false; $channel_id = (string) $this->config['monolog.telegram.channelId'] ?? false; - - if($api_key && $channel_id) { - $handler = new Handler\TelegramBotHandler($api_key, $channel_id, $level, parseMode:'Markdown'); - + + if ($api_key && $channel_id) { + $handler = new Handler\TelegramBotHandler($api_key, $channel_id, $level, parseMode: 'Markdown'); + $formatter = new LineFormatter("%message%", includeStacktraces: true); - + $handler->setFormatter($formatter); - + $handlers[] = $handler; } } @@ -524,7 +529,7 @@ protected function _initLogger() { $this->log = new Logger('', $handlers, $processors); if ($this->config['app.log.query']) { - $this->hook('app.init:after', function() use($handlers) { + $this->hook('app.init:after', function () use ($handlers) { $query_logger = new QueryLogger; $this->em->getConnection()->getConfiguration()->setSQLLogger($query_logger); }); @@ -536,11 +541,12 @@ protected function _initLogger() { * * @return void */ - protected function _initAutoloader() { + protected function _initAutoloader() + { $config = $this->config; // list of modules - if($handle = opendir(MODULES_PATH)){ + if ($handle = opendir(MODULES_PATH)) { while (false !== ($file = readdir($handle))) { $dir = MODULES_PATH . $file . '/'; if ($file != "." && $file != ".." && is_dir($dir) && file_exists("$dir/Module.php")) { @@ -549,51 +555,51 @@ protected function _initAutoloader() { } closedir($handle); } - + // list of themes foreach (scandir(THEMES_PATH) as $ff) { - if ($ff != '.' && $ff != '..' ) { + if ($ff != '.' && $ff != '..') { $theme_folder = THEMES_PATH . $ff; if (is_dir($theme_folder) && file_exists($theme_folder . '/Theme.php')) { $content = file_get_contents($theme_folder . '/Theme.php'); - if(preg_match('#namespace +([a-z0-9\\\]+) *;#i', $content, $matches)) { + if (preg_match('#namespace +([a-z0-9\\\]+) *;#i', $content, $matches)) { $namespace = $matches[1]; - if ( !array_key_exists($namespace, $config['namespaces']) ) + if (!array_key_exists($namespace, $config['namespaces'])) $config['namespaces'][$namespace] = $theme_folder; } } } } - spl_autoload_register(function($class) use ($config){ + spl_autoload_register(function ($class) use ($config) { $namespaces = $config['namespaces']; $namespaces['MapasCulturais\\DoctrineProxies'] = DOCTRINE_PROXIES_PATH; - $subfolders = ['Controllers','Entities','Repositories','Jobs']; + $subfolders = ['Controllers', 'Entities', 'Repositories', 'Jobs']; - foreach($config['plugins'] as $plugin){ - if(is_string($plugin)) { + foreach ($config['plugins'] as $plugin) { + if (is_string($plugin)) { $plugin = ['namespace' => $plugin]; } $namespace = $plugin['namespace']; $dir = $plugin['path'] ?? PLUGINS_PATH . $namespace; - if(!isset($namespaces[$namespace])){ + if (!isset($namespaces[$namespace])) { $namespaces[$namespace] = $dir; } - foreach($subfolders as $subfolder) { - if(!isset($namespaces[$namespace . '\\' . $subfolder])){ + foreach ($subfolders as $subfolder) { + if (!isset($namespaces[$namespace . '\\' . $subfolder])) { $namespaces[$namespace . '\\' . $subfolder] = $dir . '/' . $subfolder; - } + } } } - foreach($namespaces as $namespace => $base_dir){ - if(strpos($class, $namespace) === 0){ - $path = str_replace('\\', '/', str_replace($namespace, $base_dir, $class) . '.php' ); + foreach ($namespaces as $namespace => $base_dir) { + if (strpos($class, $namespace) === 0) { + $path = str_replace('\\', '/', str_replace($namespace, $base_dir, $class) . '.php'); - if(\file_exists($path)){ + if (\file_exists($path)) { require_once $path; return true; } @@ -610,7 +616,8 @@ protected function _initAutoloader() { * * @return void */ - protected function _initCache() { + protected function _initCache() + { $this->config['app.registeredAutoloadCache.lifetime'] = (int) $this->config['app.registeredAutoloadCache.lifetime']; $this->config['app.assetsUrlCache.lifetime'] = (int) $this->config['app.assetsUrlCache.lifetime']; $this->config['app.fileUrlCache.lifetime'] = (int) $this->config['app.fileUrlCache.lifetime']; @@ -624,7 +631,7 @@ protected function _initCache() { $this->cache = new Cache($this->config['app.cache']); $this->mscache = new Cache($this->config['app.mscache']); $this->mscache->setNamespace('MS'); - + $rcache_adapter = new \Symfony\Component\Cache\Adapter\ArrayAdapter(10000, false, 100000, 10000); $this->rcache = new Cache($rcache_adapter); } @@ -638,7 +645,8 @@ protected function _initCache() { * @throws LogicException * @throws Exception */ - protected function _initDoctrine() { + protected function _initDoctrine() + { // annotation driver $doctrine_config = ORMSetup::createAnnotationMetadataConfiguration( paths: [__DIR__ . '/Entities/'], @@ -646,7 +654,7 @@ protected function _initDoctrine() { cache: $this->cache->adapter ); - + // tells the doctrine to ignore hook annotation. AnnotationReader::addGlobalIgnoredName('hook'); @@ -680,7 +688,7 @@ protected function _initDoctrine() { $doctrine_config->addCustomStringFunction('recurring_event_occurrence_for', 'MapasCulturais\DoctrineMappings\Functions\RecurringEventOcurrenceFor'); $doctrine_config->addCustomNumericFunction('CAST', DoctrineMappings\Functions\Cast::class); $doctrine_config->addCustomStringFunction('CAST', DoctrineMappings\Functions\Cast::class); - + $doctrine_config->addCustomNumericFunction('st_dwithin', 'MapasCulturais\DoctrineMappings\Functions\STDWithin'); $doctrine_config->addCustomStringFunction('st_envelope', 'MapasCulturais\DoctrineMappings\Functions\STEnvelope'); $doctrine_config->addCustomNumericFunction('st_within', 'MapasCulturais\DoctrineMappings\Functions\STWithin'); @@ -692,7 +700,7 @@ protected function _initDoctrine() { $doctrine_config->setResultCache($this->mscache->adapter); $doctrine_config->setAutoGenerateProxyClasses(\Doctrine\Common\Proxy\AbstractProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS); - + // obtaining the entity manager $connection = DriverManager::getConnection([ 'driver' => 'pdo_pgsql', @@ -702,8 +710,8 @@ protected function _initDoctrine() { 'host' => $this->config['db.host'], 'wrapperClass' => Connection::class ], $doctrine_config); - - + + // obtaining the entity manager $this->em = new EntityManager($connection, $doctrine_config); @@ -732,29 +740,31 @@ protected function _initDoctrine() { * * @return void */ - protected function _initSubsite() { + protected function _initSubsite() + { $domain = $_SERVER['HTTP_HOST'] ?? 'localhost'; - if(($pos = strpos($domain, ':')) !== false){ + if (($pos = strpos($domain, ':')) !== false) { $domain = substr($domain, 0, $pos); } // para permitir o db update rodar para criar a tabela do subsite - if(($pos = strpos($domain, ':')) !== false){ + if (($pos = strpos($domain, ':')) !== false) { $domain = substr($domain, 0, $pos); } - try{ + try { $subsite = $this->repo('Subsite')->findOneBy(['url' => $domain, 'status' => 1]) ?: - $this->repo('Subsite')->findOneBy(['aliasUrl' => $domain, 'status' => 1]); + $this->repo('Subsite')->findOneBy(['aliasUrl' => $domain, 'status' => 1]); - if ($subsite){ + if ($subsite) { $this->subsite = $subsite; } - } catch ( \Exception $e) { } + } catch (\Exception $e) { + } $this->hook('app.init:after', function () { - if($this->subsite){ + if ($this->subsite) { $this->subsite->applyApiFilters(); $this->subsite->applyConfigurations(); } @@ -765,19 +775,20 @@ protected function _initSubsite() { * Inicializa o provedor de autenticação * @return void */ - protected function _initAuthProvider() { + protected function _initAuthProvider() + { // register auth providers $this->registerAuthProvider('OpenID'); $this->registerAuthProvider('logincidadao'); $this->registerAuthProvider('authentik'); - - $auth_class_name = strpos($this->config['auth.provider'], '\\') !== false ? - $this->config['auth.provider'] : + + $auth_class_name = strpos($this->config['auth.provider'], '\\') !== false ? + $this->config['auth.provider'] : 'MapasCulturais\AuthProviders\\' . $this->config['auth.provider']; $auth = new $auth_class_name($this->config['auth.config']); - + $auth->setCookies(); - + $this->auth = $auth; } @@ -785,9 +796,10 @@ protected function _initAuthProvider() { * Inicializa a instância do tema * @return void */ - protected function _initTheme() { + protected function _initTheme() + { - if($this->subsite){ + if ($this->subsite) { $this->cache->setNamespace($this->config['app.cache.namespace'] . ':' . $this->subsite->id); $theme_class = $this->subsite->namespace . "\Theme"; @@ -822,12 +834,13 @@ protected function _initTheme() { * * @return void */ - protected function _initModules() { + protected function _initModules() + { $available_modules = []; - if($handle = opendir(MODULES_PATH)){ + if ($handle = opendir(MODULES_PATH)) { while (false !== ($file = readdir($handle))) { $dir = MODULES_PATH . $file . '/'; - if ($file != "." && $file != ".." && is_dir($dir) && file_exists($dir."/Module.php")) { + if ($file != "." && $file != ".." && is_dir($dir) && file_exists($dir . "/Module.php")) { $available_modules[] = $file; } } @@ -837,11 +850,11 @@ protected function _initModules() { sort($available_modules); $this->applyHookBoundTo($this, 'app.modules.init:before', [&$available_modules]); - foreach ($available_modules as $module){ + foreach ($available_modules as $module) { $module_class_name = "$module\Module"; - $module_config = isset($config["module.$module"]) ? - $config["module.$module"] : []; - + $module_config = isset($config["module.$module"]) ? + $config["module.$module"] : []; + $this->applyHookBoundTo($this, "app.module({$module}).init:before", [&$module_config]); $this->modules[$module] = new $module_class_name($module_config); $this->applyHookBoundTo($this, "app.module({$module}).init:after"); @@ -857,14 +870,15 @@ protected function _initModules() { * * @return void */ - protected function _initPlugins() { + protected function _initPlugins() + { // esta constante é usada no script que executa os db-updates, // para que na primeira rodada do db-update não sejam incluídos os plugins - if(!env('DISABLE_PLUGINS')) { + if (!env('DISABLE_PLUGINS')) { $this->applyHookBoundTo($this, 'app.plugins.preInit:before'); $plugins = []; - foreach($this->config['plugins'] as $slug => $plugin){ + foreach ($this->config['plugins'] as $slug => $plugin) { if (is_numeric($slug) && is_string($plugin)) { $_namespace = $plugin; $slug = $plugin; @@ -875,7 +889,7 @@ protected function _initPlugins() { $plugin_class_name = "$_namespace\\$_class"; } - if(class_exists($plugin_class_name)){ + if (class_exists($plugin_class_name)) { $plugin_config = isset($plugin['config']) && is_array($plugin['config']) ? $plugin['config'] : []; $slug = is_numeric($slug) ? $_namespace : $slug; @@ -892,7 +906,7 @@ protected function _initPlugins() { $this->applyHookBoundTo($this, 'app.plugins.preInit:after', ['plugins' => &$plugins]); - $this->hook('app.modules.init:after', function() use ($plugins) { + $this->hook('app.modules.init:after', function () use ($plugins) { $this->applyHookBoundTo($this, 'app.plugins.init:before'); foreach ($plugins as $plugin) { $slug = $plugin['slug']; @@ -904,18 +918,19 @@ protected function _initPlugins() { $this->applyHookBoundTo($this, 'app.plugins.init:after'); }); } - } + } /** * Inicializa o gerenciador de rotas * @return void */ - protected function _initStorage() { + protected function _initStorage() + { $storage_class = $this->config['storage.driver'] ?? ''; - if($storage_class && class_exists($storage_class) && is_subclass_of($storage_class, Storage::class)){ + if ($storage_class && class_exists($storage_class) && is_subclass_of($storage_class, Storage::class)) { $storage_config = $this->config['storage.config'] ?? null; $this->storage = $storage_class::i($storage_config); - }else{ + } else { $this->storage = Storage\FileSystem::i(); } } @@ -924,7 +939,8 @@ protected function _initStorage() { * Inicializa o gerenciador de rotas * @return void */ - protected function _initRouteManager() { + protected function _initRouteManager() + { $this->_routesManager = new RoutesManager($this->config['routes'] ?? []); } @@ -942,13 +958,14 @@ protected function _initRouteManager() { * @throws NotSupported * @throws GlobalException */ - public function setCurrentSubsiteId(int $subsite_id = null) { - if(is_null($subsite_id)) { + public function setCurrentSubsiteId(int $subsite_id = null) + { + if (is_null($subsite_id)) { $this->subsite = null; } else { $subsite = $this->repo('Subsite')->find($subsite_id); - if(!$subsite) { + if (!$subsite) { throw new \Exception('Subsite not found'); } @@ -966,7 +983,8 @@ public function setCurrentSubsiteId(int $subsite_id = null) { * * @return string */ - public static function getHookPrefix() { + public static function getHookPrefix() + { return 'App'; } @@ -975,10 +993,11 @@ public static function getHookPrefix() { * * @return string */ - public function getVersion(){ + public function getVersion() + { $version_filename = PROTECTED_PATH . 'version.txt'; $version = trim(file_get_contents($version_filename)); - if(is_numeric($version[0])) { + if (is_numeric($version[0])) { return sprintf('v%s', $version); } else { return $version; @@ -992,7 +1011,8 @@ public function getVersion(){ * * @return string */ - public function getSiteName(): string { + public function getSiteName(): string + { return $this->config['app.siteName']; } @@ -1003,15 +1023,17 @@ public function getSiteName(): string { * * @return string */ - public function getSiteDescription(): string { + public function getSiteDescription(): string + { return $this->config['app.siteDescription']; } - + /** * Returns the RoutesManager * @return RoutesManager */ - public function getRoutesManager(): RoutesManager{ + public function getRoutesManager(): RoutesManager + { return $this->_routesManager; } @@ -1019,7 +1041,8 @@ public function getRoutesManager(): RoutesManager{ * Returns the base url of the project * @return string the base url */ - public function getBaseUrl(){ + public function getBaseUrl() + { return $this->config['base.url']; } @@ -1027,7 +1050,8 @@ public function getBaseUrl(){ * Returns the asset url of the project * @return string the asset url */ - public function getAssetUrl(){ + public function getAssetUrl() + { return isset($this->config['base.assetUrl']) ? $this->config['base.assetUrl'] : $this->getBaseUrl() . 'assets/'; } @@ -1035,7 +1059,8 @@ public function getAssetUrl(){ * Returns the logged in user * @return UserInterface */ - public function getUser(): UserInterface { + public function getUser(): UserInterface + { return $this->auth->getAuthenticatedUser(); } @@ -1044,7 +1069,8 @@ public function getUser(): UserInterface { * * @return Entities\Subsite|null */ - public function getCurrentSubsite(): Entities\Subsite|null { + public function getCurrentSubsite(): Entities\Subsite|null + { return $this->subsite; } @@ -1053,10 +1079,11 @@ public function getCurrentSubsite(): Entities\Subsite|null { * * @return int|null ID of the current site or Null for main site */ - public function getCurrentSubsiteId(): int|null { + public function getCurrentSubsiteId(): int|null + { // @TODO: alterar isto quando for implementada a possibilidade de termos // instalações de subsite com o tema diferente do Subsite - if($this->subsite){ + if ($this->subsite) { return $this->subsite->id; } @@ -1069,23 +1096,24 @@ public function getCurrentSubsiteId(): int|null { * @param bool $useSuffix * @return mixed */ - public function getMaxUploadSize(bool $useSuffix = true): string|float|int { + public function getMaxUploadSize(bool $useSuffix = true): string|float|int + { $MB = 1024; $GB = $MB * 1024; - $convertToKB = function($size) use($MB, $GB){ - switch(strtolower(substr($size, -1))){ + $convertToKB = function ($size) use ($MB, $GB) { + switch (strtolower(substr($size, -1))) { case 'k'; $size = intval($size); - break; + break; case 'm': $size = intval($size) * $MB; - break; + break; case 'g': $size = intval($size) * $GB; - break; + break; } return $size; @@ -1097,17 +1125,17 @@ public function getMaxUploadSize(bool $useSuffix = true): string|float|int { $result = min($max_upload, $max_post, $memory_limit); - if(!$useSuffix) + if (!$useSuffix) return $result; - if($result < $MB){ + if ($result < $MB) { $result .= ' KB'; - }else if($result < $GB){ + } else if ($result < $GB) { $result = number_format($result / $MB, 0) . ' MB'; - }else{ + } else { $result = $result / $GB; $formated = number_format($result, 1); - if( $formated == (int) $result ) + if ($formated == (int) $result) $result = intval($result) . ' GB'; else $result = $formated . ' GB'; @@ -1121,16 +1149,17 @@ public function getMaxUploadSize(bool $useSuffix = true): string|float|int { * * @return array */ - function getRegisteredGeoDivisions(): array { + function getRegisteredGeoDivisions(): array + { $result = []; - foreach($this->config['app.geoDivisionsHierarchy'] as $key => $division) { + foreach ($this->config['app.geoDivisionsHierarchy'] as $key => $division) { $display = true; if (substr($key, 0, 1) == '_') { $display = false; $key = substr($key, 1); } - + if (!is_array($division)) { // for backward compability version < 4.0, $division is string not a array. $d = new \stdClass(); $d->key = $key; @@ -1156,7 +1185,8 @@ function getRegisteredGeoDivisions(): array { * * @return string */ - static function getCurrentLCode(): string{ + static function getCurrentLCode(): string + { return i::get_locale(); } @@ -1164,7 +1194,8 @@ static function getCurrentLCode(): string{ * Retorna o nome do grupo de agente relacionado das inscrições * @return string */ - public function getOpportunityRegistrationAgentRelationGroupName(): string { + public function getOpportunityRegistrationAgentRelationGroupName(): string + { return 'registration'; } @@ -1175,13 +1206,13 @@ public function getOpportunityRegistrationAgentRelationGroupName(): string { * * @return mixed */ - public function getConfig(string $key = null) { + public function getConfig(string $key = null) + { if (is_null($key)) { return $this->config; } else { - return key_exists ($key, $this->config) ? $this->config[$key] : null; + return key_exists($key, $this->config) ? $this->config[$key] : null; } - } /** @@ -1190,7 +1221,8 @@ public function getConfig(string $key = null) { * @param int $length * @return string */ - static function getToken(int $length):string { + static function getToken(int $length): string + { /** * http://stackoverflow.com/questions/1846202/php-how-to-generate-a-random-unique-alphanumeric-string/13733588#13733588 */ @@ -1211,13 +1243,13 @@ static function getToken(int $length):string { $token = ""; $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - $codeAlphabet.= "abcdefghijklmnopqrstuvwxyz"; - $codeAlphabet.= "0123456789"; + $codeAlphabet .= "abcdefghijklmnopqrstuvwxyz"; + $codeAlphabet .= "0123456789"; $max = strlen($codeAlphabet) - 1; for ($i = 0; $i < $length; $i++) { $token .= $codeAlphabet[$crypto_rand_secure(0, $max)]; } - + return $token; } @@ -1230,7 +1262,8 @@ static function getToken(int $length):string { * @param mixed $slug * @return string|null */ - function getReadableName(string $slug): string|null { + function getReadableName(string $slug): string|null + { if (array_key_exists($slug, $this->config['routes']['readableNames'])) { return $this->config['routes']['readableNames'][$slug]; } @@ -1248,7 +1281,8 @@ function getReadableName(string $slug): string|null { * @param string $entity * @return bool */ - function isEnabled(string $entity){ + function isEnabled(string $entity) + { $entities = [ 'agent' => 'agents', @@ -1264,17 +1298,18 @@ function isEnabled(string $entity){ $entity = $entities[$entity] ?? $entity; $enabled = (bool) $this->config['app.enabled.' . $entity] ?? false; - + $this->applyHookBoundTo($this, "app.isEnabled({$entity})", [&$enabled]); return $enabled; } - /** - * Habilita o controle de acesso - * @return void - */ - function enableAccessControl(){ + /** + * Habilita o controle de acesso + * @return void + */ + function enableAccessControl() + { if ($this->_disableAccessControlCount > 0) { $this->_disableAccessControlCount--; } @@ -1284,7 +1319,8 @@ function enableAccessControl(){ * Desabilita o controle de acesso * @return void */ - function disableAccessControl(){ + function disableAccessControl() + { $this->_disableAccessControlCount++; } @@ -1292,7 +1328,8 @@ function disableAccessControl(){ * Indica se o controle de acesso está habilitado * @return bool */ - function isAccessControlEnabled(){ + function isAccessControlEnabled() + { return $this->_disableAccessControlCount === 0; } @@ -1300,7 +1337,8 @@ function isAccessControlEnabled(){ * Habilita a criação de Requests * @return void */ - function enableWorkflow(){ + function enableWorkflow() + { if ($this->_disableWorkflowCount > 0) { $this->_disableWorkflowCount--; } @@ -1310,7 +1348,8 @@ function enableWorkflow(){ * Desabilita a criação de Requests * @return void */ - function disableWorkflow(){ + function disableWorkflow() + { $this->_disableWorkflowCount++; } @@ -1318,7 +1357,8 @@ function disableWorkflow(){ * Indica se a criação de Requests está habilitada * @return bool */ - function isWorkflowEnabled(){ + function isWorkflowEnabled() + { return $this->_disableWorkflowCount === 0; } @@ -1327,67 +1367,70 @@ function isWorkflowEnabled(){ *******************************/ - /** - * Bloqueia a execução do código posterior ao chamamento da função, - * para o mesmo $name, enquanto não for chamado o unlock para o $name. - * - * Caso o segundo parâmetro tenha sido informado, espera o tempo informado - * pelo unlock. - * - * @param string $name - * @param float|int $wait_for_unlock - * @param float|int $expire_in - * @return void - * @throws GlobalException - */ - function lock(string $name, float $wait_for_unlock = 0, float $expire_in = 10) { - $name = $this->slugify($name); - - $filename = sys_get_temp_dir() . "/lock-{$name}.lock"; - - if ($expire_in && file_exists($filename) && (microtime (true) - filectime($filename) > $expire_in)) { - unlink($filename); - } - - if ($wait_for_unlock) { - $count = 0; - while (file_exists($filename) && $count < $wait_for_unlock) { - if ($expire_in && (microtime (true) - filectime($filename) > $expire_in)) { - unlink($filename); - } else { - $count += 0.1; - usleep(100000); - } - } - } - - if (file_exists($filename)) { - throw new \Exception("{$name} is locked"); - } - - file_put_contents($filename, "1"); - } - - /** - * Desbloqueia a execução do código posterior ao chamamento do lock($name) - - * @param string $name - * @return void - */ - function unlock(string $name) { + /** + * Bloqueia a execução do código posterior ao chamamento da função, + * para o mesmo $name, enquanto não for chamado o unlock para o $name. + * + * Caso o segundo parâmetro tenha sido informado, espera o tempo informado + * pelo unlock. + * + * @param string $name + * @param float|int $wait_for_unlock + * @param float|int $expire_in + * @return void + * @throws GlobalException + */ + function lock(string $name, float $wait_for_unlock = 0, float $expire_in = 10) + { + $name = $this->slugify($name); + + $filename = sys_get_temp_dir() . "/lock-{$name}.lock"; + + if ($expire_in && file_exists($filename) && (microtime(true) - filectime($filename) > $expire_in)) { + unlink($filename); + } + + if ($wait_for_unlock) { + $count = 0; + while (file_exists($filename) && $count < $wait_for_unlock) { + if ($expire_in && (microtime(true) - filectime($filename) > $expire_in)) { + unlink($filename); + } else { + $count += 0.1; + usleep(100000); + } + } + } + + if (file_exists($filename)) { + throw new \Exception("{$name} is locked"); + } + + file_put_contents($filename, "1"); + } + + /** + * Desbloqueia a execução do código posterior ao chamamento do lock($name) + + * @param string $name + * @return void + */ + function unlock(string $name) + { $name = $this->slugify($name); - - $filename = sys_get_temp_dir()."/lock-{$name}.lock"; + + $filename = sys_get_temp_dir() . "/lock-{$name}.lock"; unlink($filename); - } - - /** - * Transforma o texto num slug - * @param string $text - * @return string - */ - function slugify(string $text): string { + } + + /** + * Transforma o texto num slug + * @param string $text + * @return string + */ + function slugify(string $text): string + { return Utils::slugify($text); } @@ -1397,7 +1440,8 @@ function slugify(string $text): string { * @param string $string * @return string */ - function removeAccents(string $string): string { + function removeAccents(string $string): string + { return Utils::removeAccents($string); } @@ -1412,7 +1456,8 @@ function removeAccents(string $string): string { * * @return string the URL to action */ - public function createUrl(string $controller_id, string $action_name = '', array $data = []): string { + public function createUrl(string $controller_id, string $action_name = '', array $data = []): string + { return $this->_routesManager->createUrl($controller_id, $action_name, $data); } @@ -1424,11 +1469,12 @@ public function createUrl(string $controller_id, string $action_name = '', array * @param string $entity_name Repository Class Name * @return Repository the Entity Repository */ - public function repo(string $entity_name): Repository { + public function repo(string $entity_name): Repository + { // add MapasCulturais\Entities namespace if no namespace in repo name - if(strpos($entity_name, '\\') === false) - $entity_name = "\MapasCulturais\Entities\\{$entity_name}"; + if (strpos($entity_name, '\\') === false) + $entity_name = "\MapasCulturais\Entities\\{$entity_name}"; return $this->em->getRepository($entity_name); } @@ -1441,8 +1487,9 @@ public function repo(string $entity_name): Repository { * @return string * @throws GlobalException */ - function renderMustacheTemplate(string $template_name, array|object $template_data): string { - if(!is_array($template_data) && !is_object($template_data)) { + function renderMustacheTemplate(string $template_name, array|object $template_data): string + { + if (!is_array($template_data) && !is_object($template_data)) { throw new \Exception('Template data not object or array'); } @@ -1451,29 +1498,28 @@ function renderMustacheTemplate(string $template_name, array|object $template_da if ($this->view->version >= 2) { $template_data->siteName = $this->siteName; $template_data->siteDescription = $this->siteDescription; - } else { $template_data->siteName = $this->view->dict('site: name', false); $template_data->siteDescription = $this->view->dict('site: description', false); $template_data->siteOwner = $this->view->dict('site: owner', false); } - + $template_data->baseUrl = $this->getBaseUrl(); - if(!($footer_name = $this->view->resolveFileName('templates/' . i::get_locale(), '_footer.html'))) { - if(!($footer_name = $this->view->resolveFileName('templates/pt_BR', '_footer.html'))) { + if (!($footer_name = $this->view->resolveFileName('templates/' . i::get_locale(), '_footer.html'))) { + if (!($footer_name = $this->view->resolveFileName('templates/pt_BR', '_footer.html'))) { throw new \Exception('Email footer template not found'); } } - if(!($header_name = $this->view->resolveFileName('templates/' . i::get_locale(), '_header.html'))) { - if(!($header_name = $this->view->resolveFileName('templates/pt_BR', '_header.html'))) { + if (!($header_name = $this->view->resolveFileName('templates/' . i::get_locale(), '_header.html'))) { + if (!($header_name = $this->view->resolveFileName('templates/pt_BR', '_header.html'))) { throw new \Exception('Email header template not found'); } } - if(!($file_name = $this->view->resolveFileName('templates/' . i::get_locale(), $template_name))) { - if(!($file_name = $this->view->resolveFileName('templates/pt_BR', $template_name))) { + if (!($file_name = $this->view->resolveFileName('templates/' . i::get_locale(), $template_name))) { + if (!($file_name = $this->view->resolveFileName('templates/pt_BR', $template_name))) { throw new \Exception('Email Template undefined'); } } @@ -1483,26 +1529,26 @@ function renderMustacheTemplate(string $template_name, array|object $template_da $content = file_get_contents($file_name); $matches = []; - if(preg_match_all('#\{\{asset:([^\}]+)\}\}#', $header, $matches)){ - foreach($matches[0] as $i => $tag){ + if (preg_match_all('#\{\{asset:([^\}]+)\}\}#', $header, $matches)) { + foreach ($matches[0] as $i => $tag) { $header = str_replace($tag, $this->view->asset($matches[1][$i], false), $header); } } $matches = []; - if(preg_match_all('#\{\{asset:([^\}]+)\}\}#', $footer, $matches)){ - foreach($matches[0] as $i => $tag){ + if (preg_match_all('#\{\{asset:([^\}]+)\}\}#', $footer, $matches)) { + foreach ($matches[0] as $i => $tag) { $footer = str_replace($tag, $this->view->asset($matches[1][$i], false), $footer); } } $matches = []; - if(preg_match_all('#\{\{asset:([^\}]+)\}\}#', $content, $matches)){ - foreach($matches[0] as $i => $tag){ + if (preg_match_all('#\{\{asset:([^\}]+)\}\}#', $content, $matches)) { + foreach ($matches[0] as $i => $tag) { $content = str_replace($tag, $this->view->asset($matches[1][$i], false), $content); } } - + $mustache = new \Mustache_Engine(); $headerData = $template_data; @@ -1530,11 +1576,13 @@ function renderMustacheTemplate(string $template_name, array|object $template_da * @return never * @throws NotFound */ - function pass() { + function pass() + { throw new Exceptions\NotFound; } - function redirect(string $destination, int $status_code = 302) { + function redirect(string $destination, int $status_code = 302) + { $this->response = $this->response->withHeader('Location', $destination); $this->halt($status_code); } @@ -1549,7 +1597,8 @@ function redirect(string $destination, int $status_code = 302) { * @throws RuntimeException * @throws InvalidArgumentException */ - function halt(int $status_code, string $message = '') { + function halt(int $status_code, string $message = '') + { $this->response = $this->response->withStatus($status_code); if ($message) { @@ -1576,32 +1625,32 @@ function halt(int $status_code, string $message = '') { * * @return Entities\File|Entities\File[] */ - public function handleUpload($key, $file_class_name){ - if(is_array($_FILES) && key_exists($key, $_FILES)){ + public function handleUpload($key, $file_class_name) + { + if (is_array($_FILES) && key_exists($key, $_FILES)) { - if(is_array($_FILES[$key]['name'])){ + if (is_array($_FILES[$key]['name'])) { $result = []; - foreach(array_keys($_FILES[$key]['name']) as $i){ + foreach (array_keys($_FILES[$key]['name']) as $i) { $tmp_file = []; - foreach(array_keys($_FILES[$key]) as $k){ + foreach (array_keys($_FILES[$key]) as $k) { $tmp_file[$k] = $k == 'name' ? $this->sanitizeFilename($_FILES[$key][$k][$i]) : $_FILES[$key][$k][$i]; } $result[] = new $file_class_name($tmp_file); } - }else{ + } else { - if($_FILES[$key]['error']){ + if ($_FILES[$key]['error']) { throw new Exceptions\FileUploadError($key, $_FILES[$key]['error']); } $mime = mime_content_type($_FILES[$key]['tmp_name']); $_FILES[$key]['name'] = $this->sanitizeFilename($_FILES[$key]['name'], $mime); $result = new $file_class_name($_FILES[$key]); - } return $result; - }else{ + } else { return null; } } @@ -1615,9 +1664,10 @@ public function handleUpload($key, $file_class_name){ * * @return string The sanitized filename. */ - function sanitizeFilename($filename, $mimetype = false){ - $filename = str_replace(' ','_', strtolower($filename)); - if(is_callable($this->config['app.sanitize_filename_function'])){ + function sanitizeFilename($filename, $mimetype = false) + { + $filename = str_replace(' ', '_', strtolower($filename)); + if (is_callable($this->config['app.sanitize_filename_function'])) { $cb = $this->config['app.sanitize_filename_function']; $filename = $cb($filename); } @@ -1637,7 +1687,6 @@ function sanitizeFilename($filename, $mimetype = false){ if (array_key_exists($mimetype, $imagetypes)) $filename .= '.' . $imagetypes[$mimetype]; - } return $filename; @@ -1645,7 +1694,7 @@ function sanitizeFilename($filename, $mimetype = false){ /********************************************** * Hooks System - **********************************************/ + **********************************************/ /** * Clear hook listeners @@ -1656,7 +1705,8 @@ function sanitizeFilename($filename, $mimetype = false){ * * @param string $name A hook name (Optional) */ - public function clearHooks(string $name = null) { + public function clearHooks(string $name = null) + { $this->hooks->clear($name); } @@ -1671,7 +1721,8 @@ public function clearHooks(string $name = null) { * @param string $name A hook name (Optional) * @return array|null */ - public function getHooks(string $name = null) { + public function getHooks(string $name = null) + { return $this->hooks->get($name); } @@ -1681,7 +1732,8 @@ public function getHooks(string $name = null) { * @param callable $callable A callable object * @param int $priority The hook priority; 0 = high, 10 = low */ - function hook(string $name, callable $callable, int $priority = 10) { + function hook(string $name, callable $callable, int $priority = 10) + { $this->hooks->hook($name, $callable, $priority); } @@ -1692,7 +1744,8 @@ function hook(string $name, callable $callable, int $priority = 10) { * * @return callable[] */ - function applyHook(string $name, array $hookArg = []): array { + function applyHook(string $name, array $hookArg = []): array + { return $this->hooks->apply($name, $hookArg); } @@ -1705,7 +1758,8 @@ function applyHook(string $name, array $hookArg = []): array { * * @return callable[] */ - function applyHookBoundTo(object $target_object, string $name, array $hookArg = []) { + function applyHookBoundTo(object $target_object, string $name, array $hookArg = []) + { return $this->hooks->applyBoundTo($target_object, $name, $hookArg); } @@ -1726,11 +1780,12 @@ function applyHookBoundTo(object $target_object, string $name, array $hookArg = * @return Job * @throws Exception */ - public function enqueueOrReplaceJob(string $type_slug, array $data, string $start_string = 'now', string $interval_string = '', int $iterations = 1) { + public function enqueueOrReplaceJob(string $type_slug, array $data, string $start_string = 'now', string $interval_string = '', int $iterations = 1) + { return $this->enqueueJob($type_slug, $data, $start_string, $interval_string, $iterations, true); } - + /** * Enfileira um job * @@ -1743,13 +1798,14 @@ public function enqueueOrReplaceJob(string $type_slug, array $data, string $star * @return Job * @throws Exception */ - public function enqueueJob(string $type_slug, array $data, string $start_string = 'now', string $interval_string = '', int $iterations = 1, $replace = false) { - if($this->config['app.log.jobs']) { + public function enqueueJob(string $type_slug, array $data, string $start_string = 'now', string $interval_string = '', int $iterations = 1, $replace = false) + { + if ($this->config['app.log.jobs']) { $this->log->debug("ENQUEUED JOB: $type_slug"); } $type = $this->getRegisteredJobType($type_slug); - + if (!$type) { throw new \Exception("invalid job type: {$type_slug}"); } @@ -1798,13 +1854,14 @@ public function enqueueJob(string $type_slug, array $data, string $start_string * @throws ORMException * @throws OptimisticLockException */ - public function unqueueJob(string $type_slug, array $data, string $start_string = 'now', string $interval_string = '', int $iterations = 1) { - if($this->config['app.log.jobs']) { + public function unqueueJob(string $type_slug, array $data, string $start_string = 'now', string $interval_string = '', int $iterations = 1) + { + if ($this->config['app.log.jobs']) { $this->log->debug("UNQUEUED JOB: $type_slug"); } $type = $this->getRegisteredJobType($type_slug); - + if (!$type) { throw new \Exception("invalid job type: {$type_slug}"); } @@ -1822,7 +1879,8 @@ public function unqueueJob(string $type_slug, array $data, string $start_string * @return int|false * @throws Exception */ - public function executeJob(): int|false { + public function executeJob(): int|false + { $conn = $this->em->getConnection(); $now = date('Y-m-d H:i:s'); $job_id = $conn->fetchScalar(" @@ -1839,7 +1897,7 @@ public function executeJob(): int|false { /** @var Job $job */ $conn->executeQuery("UPDATE job SET status = 1 WHERE id = '{$job_id}'"); $job = $this->repo('Job')->find($job_id); - + $this->disableAccessControl(); $this->applyHookBoundTo($this, "app.executeJob:before"); $job->execute(); @@ -1865,10 +1923,11 @@ public function executeJob(): int|false { * * @return void */ - public function enqueueEntityToPCacheRecreation(Entity $entity, User $user = null) { + public function enqueueEntityToPCacheRecreation(Entity $entity, User $user = null) + { if (!$entity->__skipQueuingPCacheRecreation) { - $entity_key = $entity->id ? "{$entity}" : "{$entity}:".spl_object_id($entity); - if($user) { + $entity_key = $entity->id ? "{$entity}" : "{$entity}:" . spl_object_id($entity); + if ($user) { $entity_key = "{$entity_key}:{$user->id}"; } $this->_permissionCachePendingQueue[$entity_key] = [$entity, $user]; @@ -1883,9 +1942,10 @@ public function enqueueEntityToPCacheRecreation(Entity $entity, User $user = nul * * @return bool */ - public function isEntityEnqueuedToPCacheRecreation(Entity $entity, User $user = null) { - $entity_key = $entity->id ? "{$entity}" : "{$entity}:".spl_object_id($entity); - if($user) { + public function isEntityEnqueuedToPCacheRecreation(Entity $entity, User $user = null) + { + $entity_key = $entity->id ? "{$entity}" : "{$entity}:" . spl_object_id($entity); + if ($user) { $entity_key = "{$entity_key}:{$user->id}"; } @@ -1895,17 +1955,18 @@ public function isEntityEnqueuedToPCacheRecreation(Entity $entity, User $user = /** * Persiste a fila de entidades para reprocessamento de cache de permissão */ - public function persistPCachePendingQueue() { + public function persistPCachePendingQueue() + { $created = false; - foreach($this->_permissionCachePendingQueue as $config) { + foreach ($this->_permissionCachePendingQueue as $config) { $entity = $config[0]; $user = $config[1]; if (is_int($entity->id) && !$this->repo('PermissionCachePending')->findBy([ - 'objectId' => $entity->id, - 'objectType' => $entity->getClassName(), - 'status' => 0, - 'user' => $user - ])) { + 'objectId' => $entity->id, + 'objectType' => $entity->getClassName(), + 'status' => 0, + 'user' => $user + ])) { $pendingCache = new \MapasCulturais\Entities\PermissionCachePending(); $pendingCache->objectId = $entity->id; $pendingCache->objectType = $entity->getClassName(); @@ -1929,7 +1990,8 @@ public function persistPCachePendingQueue() { * @param Entity $entity * @return void */ - public function setEntityPermissionCacheAsRecreated(Entity $entity) { + public function setEntityPermissionCacheAsRecreated(Entity $entity) + { $this->_recreatedPermissionCacheList["$entity"] = $entity; } @@ -1940,7 +2002,8 @@ public function setEntityPermissionCacheAsRecreated(Entity $entity) { * * @return bool */ - public function isEntityPermissionCacheRecreated(Entity $entity) { + public function isEntityPermissionCacheRecreated(Entity $entity) + { return isset($this->_recreatedPermissionCacheList["$entity"]); } @@ -1959,7 +2022,8 @@ public function isEntityPermissionCacheRecreated(Entity $entity) { * @throws WorkflowRequest * @throws GlobalException */ - public function recreatePermissionsCache(){ + public function recreatePermissionsCache() + { $conn = $this->em->getConnection(); $id = $conn->fetchOne(' @@ -1973,7 +2037,7 @@ public function recreatePermissionsCache(){ status > 0 )'); - if(!$id) { + if (!$id) { return; } $item = $this->repo('PermissionCachePending')->find($id); @@ -1993,20 +2057,20 @@ public function recreatePermissionsCache(){ $item = $this->repo('PermissionCachePending')->find($item->id); $this->em->remove($item); $this->em->flush(); - } catch (\Exception $e ){ + } catch (\Exception $e) { $this->disableAccessControl(); $item = $this->repo('PermissionCachePending')->find($item->id); $item->status = 2; // ERROR $item->save(true); $this->enableAccessControl(); - if(php_sapi_name()==="cli"){ + if (php_sapi_name() === "cli") { echo "\n\t - ERROR - {$e->getMessage()}"; } throw $e; } - if($this->config['app.log.pcache']){ + if ($this->config['app.log.pcache']) { $end_time = microtime(true); $total_time = number_format($end_time - $start_time, 1); @@ -2028,7 +2092,8 @@ public function recreatePermissionsCache(){ * @throws ExceptionInvalidArgumentException * @throws UnsupportedSchemeException */ - function getMailerTransport(): TransportInterface { + function getMailerTransport(): TransportInterface + { $transport = Transport::fromDsn($this->config['mailer.transport']); $this->applyHook('mailer.transport', [&$transport]); @@ -2044,7 +2109,8 @@ function getMailerTransport(): TransportInterface { * @throws ExceptionInvalidArgumentException * @throws UnsupportedSchemeException */ - function getMailer(): Mailer { + function getMailer(): Mailer + { $mailer = new Mailer($this->getMailerTransport()); return $mailer; @@ -2055,34 +2121,35 @@ function getMailer(): Mailer { * Cria uma mensagem de email * */ - function createMailMessage(array $args = []): Email { + function createMailMessage(array $args = []): Email + { $message = new Email(); - if($this->config['mailer.from']){ + if ($this->config['mailer.from']) { $message->from($this->config['mailer.from']); } - if($this->config['mailer.alwaysTo']){ + if ($this->config['mailer.alwaysTo']) { $message->to($this->config['mailer.alwaysTo']); } - if($this->config['mailer.bcc']){ - $bcc = is_array($this->config['mailer.bcc']) ? - $this->config['mailer.bcc']: + if ($this->config['mailer.bcc']) { + $bcc = is_array($this->config['mailer.bcc']) ? + $this->config['mailer.bcc'] : explode(',', $this->config['mailer.bcc']); - + $message->bcc(...$bcc); } - if($this->config['mailer.replyTo']){ + if ($this->config['mailer.replyTo']) { $message->replyTo($this->config['mailer.replyTo']); } $original = []; - foreach($args as $method_name => $value){ - if(in_array($method_name, ['to', 'cc', 'bcc'])) { - if($method_name == 'bcc' && isset($bcc)) { + foreach ($args as $method_name => $value) { + if (in_array($method_name, ['to', 'cc', 'bcc'])) { + if ($method_name == 'bcc' && isset($bcc)) { $value = [...$bcc, ...(is_array($value) ? $value : explode(',', $value))]; } if ($this->config['mailer.alwaysTo']) { @@ -2092,19 +2159,19 @@ function createMailMessage(array $args = []): Email { $message->$method_name(...$value); } } else { - if($method_name == 'body') { + if ($method_name == 'body') { $method_name = 'html'; } - - if(method_exists($message, $method_name)){ + + if (method_exists($message, $method_name)) { $message->$method_name($value); } } } - if($this->config['mailer.alwaysTo']){ - foreach($original as $key => $val){ - if(is_array($val)){ + if ($this->config['mailer.alwaysTo']) { + foreach ($original as $key => $val) { + if (is_array($val)) { $val = implode(', ', $val); } $current_body = $message->getHtmlBody(); @@ -2121,7 +2188,8 @@ function createMailMessage(array $args = []): Email { * @param Email $message * @return bool */ - function sendMailMessage(Email $message): bool { + function sendMailMessage(Email $message): bool + { $mailer = $this->getMailer(); if (!is_object($mailer)) @@ -2130,7 +2198,7 @@ function sendMailMessage(Email $message): bool { try { $mailer->send($message); return true; - } catch(TransportExceptionInterface $exception) { + } catch (TransportExceptionInterface $exception) { $this->log->error('Mailer error: ' . $exception->getMessage()); return false; } @@ -2148,11 +2216,12 @@ function sendMailMessage(Email $message): bool { * @throws Throwable * @throws ExceptionLogicException */ - function createAndSendMailMessage(array $args = []){ + function createAndSendMailMessage(array $args = []) + { $message = $this->createMailMessage($args); return $this->sendMailMessage($message); - } - + } + /** * Renderiza um template de email * @@ -2164,8 +2233,9 @@ function createAndSendMailMessage(array $args = []){ * @throws GlobalException * @throws MailTemplateNotFound */ - function renderMailerTemplate(string $template_name, array|object $template_data = []): array { - if($message = $this->config['mailer.templates'][$template_name] ?? null) { + function renderMailerTemplate(string $template_name, array|object $template_data = []): array + { + if ($message = $this->config['mailer.templates'][$template_name] ?? null) { $message['body'] = $this->renderMustacheTemplate($message['template'], $template_data); } else { throw new Exceptions\MailTemplateNotFound($template_name); @@ -2206,8 +2276,9 @@ function renderMailerTemplate(string $template_name, array|object $template_data * @throws ReflectionException * @throws MappingException */ - protected function register(){ - if($this->_registered) + protected function register() + { + if ($this->_registered) return; $this->_registered = true; @@ -2219,7 +2290,7 @@ protected function register(){ $this->registerController('site', 'MapasCulturais\Controllers\Site'); $this->registerController('auth', 'MapasCulturais\Controllers\Auth'); - if(($this->view) instanceof Themes\BaseV1\Theme ) { + if (($this->view) instanceof Themes\BaseV1\Theme) { $this->registerController('panel', 'MapasCulturais\Controllers\Panel'); } @@ -2242,9 +2313,9 @@ protected function register(){ $this->registerController('term', 'MapasCulturais\Controllers\Term'); $this->registerController('file', 'MapasCulturais\Controllers\File'); $this->registerController('metalist', 'MapasCulturais\Controllers\MetaList'); - $this->registerController('eventOccurrence','MapasCulturais\Controllers\EventOccurrence'); + $this->registerController('eventOccurrence', 'MapasCulturais\Controllers\EventOccurrence'); - $this->registerController('eventAttendance','MapasCulturais\Controllers\EventAttendance'); + $this->registerController('eventAttendance', 'MapasCulturais\Controllers\EventAttendance'); //workflow controllers $this->registerController('notification', 'MapasCulturais\Controllers\Notification'); @@ -2269,7 +2340,7 @@ protected function register(){ 'plural' => i::__('Super Administradores do SaaS'), 'another_roles' => ['saasAdmin', 'superAdmin', 'admin'], 'subsite' => false, - 'can_user_manage_role' => function(UserInterface $user, $subsite_id) { + 'can_user_manage_role' => function (UserInterface $user, $subsite_id) { return $user->is('saasSuperAdmin'); } ], @@ -2278,7 +2349,7 @@ protected function register(){ 'plural' => i::__('Administradores do SaaS'), 'another_roles' => ['superAdmin', 'admin'], 'subsite' => false, - 'can_user_manage_role' => function(UserInterface $user, $subsite_id) { + 'can_user_manage_role' => function (UserInterface $user, $subsite_id) { return $user->is('saasSuperAdmin'); } ], @@ -2287,7 +2358,7 @@ protected function register(){ 'plural' => i::__('Super Administradores'), 'another_roles' => ['admin'], 'subsite' => true, - 'can_user_manage_role' => function(UserInterface $user, $subsite_id) { + 'can_user_manage_role' => function (UserInterface $user, $subsite_id) { return $user->is('superAdmin', $subsite_id); } ], @@ -2296,7 +2367,7 @@ protected function register(){ 'plural' => i::__('Administradores'), 'another_roles' => [], 'subsite' => true, - 'can_user_manage_role' => function(UserInterface $user, $subsite_id) { + 'can_user_manage_role' => function (UserInterface $user, $subsite_id) { return $user->is('superAdmin', $subsite_id); } ], @@ -2320,12 +2391,12 @@ protected function register(){ 'gallery' => new Definitions\FileGroup('gallery', ['^image/(jpeg|png)$'], i::__('O arquivo enviado não é uma imagem válida.'), false), 'registrationFileConfiguration' => new Definitions\FileGroup('registrationFileTemplate', ['^application/.*'], i::__('O arquivo enviado não é um documento válido.'), true), 'rules' => new Definitions\FileGroup('rules', ['^application/.*'], i::__('O arquivo enviado não é um documento válido.'), true), - 'logo' => new Definitions\FileGroup('logo',['^image/(jpeg|png)$'], i::__('O arquivo enviado não é uma imagem válida.'), true), - 'background' => new Definitions\FileGroup('background',['^image/(jpeg|png)$'], i::__('O arquivo enviado não é uma imagem válida.'),true), - 'share' => new Definitions\FileGroup('share',['^image/(jpeg|png)$'], i::__('O arquivo enviado não é uma imagem válida.'),true), - 'institute' => new Definitions\FileGroup('institute',['^image/(jpeg|png)$'], i::__('O arquivo enviado não é uma imagem válida.'), true), - 'favicon' => new Definitions\FileGroup('favicon',['^image/(jpeg|png|x-icon|vnd.microsoft.icon)$'], i::__('O arquivo enviado não é uma imagem válida.'), true), - 'zipArchive' => new Definitions\FileGroup('zipArchive',['^application/zip$'], i::__('O arquivo não é um ZIP.'), true, null, true), + 'logo' => new Definitions\FileGroup('logo', ['^image/(jpeg|png)$'], i::__('O arquivo enviado não é uma imagem válida.'), true), + 'background' => new Definitions\FileGroup('background', ['^image/(jpeg|png)$'], i::__('O arquivo enviado não é uma imagem válida.'), true), + 'share' => new Definitions\FileGroup('share', ['^image/(jpeg|png)$'], i::__('O arquivo enviado não é uma imagem válida.'), true), + 'institute' => new Definitions\FileGroup('institute', ['^image/(jpeg|png)$'], i::__('O arquivo enviado não é uma imagem válida.'), true), + 'favicon' => new Definitions\FileGroup('favicon', ['^image/(jpeg|png|x-icon|vnd.microsoft.icon)$'], i::__('O arquivo enviado não é uma imagem válida.'), true), + 'zipArchive' => new Definitions\FileGroup('zipArchive', ['^application/zip$'], i::__('O arquivo não é um ZIP.'), true, null, true), ]; // register file groups @@ -2363,28 +2434,29 @@ protected function register(){ $this->registerFileGroup('registrationFileConfiguration', $file_groups['registrationFileConfiguration']); $this->registerFileGroup('registration', $file_groups['zipArchive']); - $this->registerFileGroup('subsite',$file_groups['header']); - $this->registerFileGroup('subsite',$file_groups['avatar']); - $this->registerFileGroup('subsite',$file_groups['logo']); - $this->registerFileGroup('subsite',$file_groups['background']); - $this->registerFileGroup('subsite',$file_groups['share']); - $this->registerFileGroup('subsite',$file_groups['institute']); - $this->registerFileGroup('subsite',$file_groups['favicon']); - $this->registerFileGroup('subsite',$file_groups['downloads']); + $this->registerFileGroup('subsite', $file_groups['header']); + $this->registerFileGroup('subsite', $file_groups['avatar']); + $this->registerFileGroup('subsite', $file_groups['logo']); + $this->registerFileGroup('subsite', $file_groups['background']); + $this->registerFileGroup('subsite', $file_groups['share']); + $this->registerFileGroup('subsite', $file_groups['institute']); + $this->registerFileGroup('subsite', $file_groups['favicon']); + $this->registerFileGroup('subsite', $file_groups['downloads']); - if ($theme_image_transformations = $this->view->resolveFilename('','image-transformations.php')) { + if ($theme_image_transformations = $this->view->resolveFilename('', 'image-transformations.php')) { $image_transformations = include $theme_image_transformations; } else { - $image_transformations = include APPLICATION_PATH.'/conf/image-transformations.php'; + $image_transformations = include APPLICATION_PATH . '/conf/image-transformations.php'; } - foreach($image_transformations as $name => $transformation) + foreach ($image_transformations as $name => $transformation) $this->registerImageTransformation($name, $transformation); // all metalist groups $metalist_groups = [ - 'links' => new Definitions\MetaListGroup('links', + 'links' => new Definitions\MetaListGroup( + 'links', [ 'title' => [ 'label' => 'Nome' @@ -2399,7 +2471,8 @@ protected function register(){ i::__('O arquivo enviado não é uma imagem válida.'), true ), - 'videos' => new Definitions\MetaListGroup('videos', + 'videos' => new Definitions\MetaListGroup( + 'videos', [ 'title' => [ 'label' => 'Nome' @@ -2440,67 +2513,67 @@ protected function register(){ // get types and metadata configurations - if ($theme_space_types = $this->view->resolveFilename('','space-types.php')) { + if ($theme_space_types = $this->view->resolveFilename('', 'space-types.php')) { $space_types = include $theme_space_types; } else { - $space_types = include APPLICATION_PATH.'/conf/space-types.php'; + $space_types = include APPLICATION_PATH . '/conf/space-types.php'; } $space_meta = key_exists('metadata', $space_types) && is_array($space_types['metadata']) ? $space_types['metadata'] : []; - if ($theme_agent_types = $this->view->resolveFilename('','agent-types.php')) { + if ($theme_agent_types = $this->view->resolveFilename('', 'agent-types.php')) { $agent_types = include $theme_agent_types; } else { - $agent_types = include APPLICATION_PATH.'/conf/agent-types.php'; + $agent_types = include APPLICATION_PATH . '/conf/agent-types.php'; } $agents_meta = key_exists('metadata', $agent_types) && is_array($agent_types['metadata']) ? $agent_types['metadata'] : []; - if ($theme_event_types = $this->view->resolveFilename('','event-types.php')) { + if ($theme_event_types = $this->view->resolveFilename('', 'event-types.php')) { $event_types = include $theme_event_types; } else { - $event_types = include APPLICATION_PATH.'/conf/event-types.php'; + $event_types = include APPLICATION_PATH . '/conf/event-types.php'; } $event_meta = key_exists('metadata', $event_types) && is_array($event_types['metadata']) ? $event_types['metadata'] : []; - if ($theme_project_types = $this->view->resolveFilename('','project-types.php')) { + if ($theme_project_types = $this->view->resolveFilename('', 'project-types.php')) { $project_types = include $theme_project_types; } else { - $project_types = include APPLICATION_PATH.'/conf/project-types.php'; + $project_types = include APPLICATION_PATH . '/conf/project-types.php'; } $projects_meta = key_exists('metadata', $project_types) && is_array($project_types['metadata']) ? $project_types['metadata'] : []; - if ($theme_opportunity_types = $this->view->resolveFilename('','opportunity-types.php')) { + if ($theme_opportunity_types = $this->view->resolveFilename('', 'opportunity-types.php')) { $opportunity_types = include $theme_opportunity_types; } else { - $opportunity_types = include APPLICATION_PATH.'/conf/opportunity-types.php'; + $opportunity_types = include APPLICATION_PATH . '/conf/opportunity-types.php'; } $opportunities_meta = key_exists('metadata', $opportunity_types) && is_array($opportunity_types['metadata']) ? $opportunity_types['metadata'] : []; // get types and metadata configurations - if ($theme_subsite_types = $this->view->resolveFilename('','subsite-types.php')) { + if ($theme_subsite_types = $this->view->resolveFilename('', 'subsite-types.php')) { $subsite_types = include $theme_subsite_types; } else { - $subsite_types = include APPLICATION_PATH.'/conf/subsite-types.php'; + $subsite_types = include APPLICATION_PATH . '/conf/subsite-types.php'; } $subsite_meta = key_exists('metadata', $subsite_types) && is_array($subsite_types['metadata']) ? $subsite_types['metadata'] : []; - if ($theme_seal_types = $this->view->resolveFilename('','seal-types.php')) { + if ($theme_seal_types = $this->view->resolveFilename('', 'seal-types.php')) { $seal_types = include $theme_seal_types; } else { - $seal_types = include APPLICATION_PATH.'/conf/seal-types.php'; + $seal_types = include APPLICATION_PATH . '/conf/seal-types.php'; } $seals_meta = key_exists('metadata', $seal_types) && is_array($seal_types['metadata']) ? $seal_types['metadata'] : []; - if ($theme_notification_types = $this->view->resolveFilename('','notification-types.php')) { + if ($theme_notification_types = $this->view->resolveFilename('', 'notification-types.php')) { $notification_types = include $theme_notification_types; } else { - $notification_types = include APPLICATION_PATH.'/conf/notification-types.php'; + $notification_types = include APPLICATION_PATH . '/conf/notification-types.php'; } $notification_meta = key_exists('metadata', $notification_types) && is_array($notification_types['metadata']) ? $notification_types['metadata'] : []; // registration agent relations - foreach($this->config['registration.agentRelations'] as $config){ + foreach ($this->config['registration.agentRelations'] as $config) { $def = new Definitions\RegistrationAgentRelation($config); $opportunities_meta[$def->metadataName] = $def->getMetadataConfiguration(); @@ -2509,14 +2582,14 @@ protected function register(){ // register space types and spaces metadata - foreach($space_types['items'] as $group_name => $group_config){ + foreach ($space_types['items'] as $group_name => $group_config) { $entity_class = 'MapasCulturais\Entities\Space'; $group = new Definitions\EntityTypeGroup($entity_class, $group_name, $group_config['range'][0], $group_config['range'][1]); $this->registerEntityTypeGroup($group); $group_meta = key_exists('metadata', $group_config) ? $group_config['metadata'] : []; - foreach ($group_config['items'] as $type_id => $type_config){ + foreach ($group_config['items'] as $type_id => $type_config) { $type = new Definitions\EntityType($entity_class, $type_id, $type_config['name']); $group->registerType($type); $this->registerEntityType($type); @@ -2525,19 +2598,19 @@ protected function register(){ $type_config['metadata'] = $type_meta; // add group metadata to space type - if(key_exists('metadata', $group_config)) - foreach($group_meta as $meta_key => $meta_config) - if(!key_exists($meta_key, $type_meta) || key_exists($meta_key, $type_meta) && is_null($type_config['metadata'][$meta_key])) - $type_config['metadata'][$meta_key] = $meta_config; + if (key_exists('metadata', $group_config)) + foreach ($group_meta as $meta_key => $meta_config) + if (!key_exists($meta_key, $type_meta) || key_exists($meta_key, $type_meta) && is_null($type_config['metadata'][$meta_key])) + $type_config['metadata'][$meta_key] = $meta_config; // add space metadata to space type - foreach($space_meta as $meta_key => $meta_config) - if(!key_exists($meta_key, $type_meta) || key_exists($meta_key, $type_meta) && is_null($type_config['metadata'][$meta_key])) - $type_config['metadata'][$meta_key] = $meta_config; + foreach ($space_meta as $meta_key => $meta_config) + if (!key_exists($meta_key, $type_meta) || key_exists($meta_key, $type_meta) && is_null($type_config['metadata'][$meta_key])) + $type_config['metadata'][$meta_key] = $meta_config; - foreach($type_config['metadata'] as $meta_key => $meta_config){ - $metadata = new Definitions\Metadata($meta_key, $meta_config); - $this->registerMetadata($metadata, $entity_class, $type_id); + foreach ($type_config['metadata'] as $meta_key => $meta_config) { + $metadata = new Definitions\Metadata($meta_key, $meta_config); + $this->registerMetadata($metadata, $entity_class, $type_id); } } } @@ -2545,7 +2618,7 @@ protected function register(){ // register agent types and agent metadata $entity_class = 'MapasCulturais\Entities\Agent'; - foreach($agent_types['items'] as $type_id => $type_config){ + foreach ($agent_types['items'] as $type_id => $type_config) { $type = new Definitions\EntityType($entity_class, $type_id, $type_config['name']); $this->registerEntityType($type); @@ -2554,11 +2627,11 @@ protected function register(){ $type_config['metadata'] = $type_meta; // add agents metadata definition to agent type - foreach($agents_meta as $meta_key => $meta_config) - if(!key_exists($meta_key, $type_meta) || key_exists($meta_key, $type_meta) && is_null($type_config['metadata'][$meta_key])) + foreach ($agents_meta as $meta_key => $meta_config) + if (!key_exists($meta_key, $type_meta) || key_exists($meta_key, $type_meta) && is_null($type_config['metadata'][$meta_key])) $type_config['metadata'][$meta_key] = $meta_config; - foreach($type_config['metadata'] as $meta_key => $meta_config){ + foreach ($type_config['metadata'] as $meta_key => $meta_config) { $metadata = new Definitions\Metadata($meta_key, $meta_config); $this->registerMetadata($metadata, $entity_class, $type_id); @@ -2568,20 +2641,20 @@ protected function register(){ // register event types and event metadata $entity_class = 'MapasCulturais\Entities\Event'; - foreach($event_types['items'] as $type_id => $type_config){ + foreach ($event_types['items'] as $type_id => $type_config) { $type = new Definitions\EntityType($entity_class, $type_id, $type_config['name']); $this->registerEntityType($type); $type_meta = key_exists('metadata', $type_config) && is_array($type_config['metadata']) ? $type_config['metadata'] : []; $type_config['metadata'] = $type_meta; - + // add events metadata definition to event type - foreach($event_meta as $meta_key => $meta_config) - if(!key_exists($meta_key, $type_meta) || key_exists($meta_key, $type_meta) && is_null($type_config['metadata'][$meta_key])) + foreach ($event_meta as $meta_key => $meta_config) + if (!key_exists($meta_key, $type_meta) || key_exists($meta_key, $type_meta) && is_null($type_config['metadata'][$meta_key])) $type_config['metadata'][$meta_key] = $meta_config; - foreach($type_config['metadata'] as $meta_key => $meta_config){ + foreach ($type_config['metadata'] as $meta_key => $meta_config) { $metadata = new Definitions\Metadata($meta_key, $meta_config); $this->registerMetadata($metadata, $entity_class, $type_id); } @@ -2590,7 +2663,7 @@ protected function register(){ // register project types and project metadata $entity_class = 'MapasCulturais\Entities\Project'; - foreach($project_types['items'] as $type_id => $type_config){ + foreach ($project_types['items'] as $type_id => $type_config) { $type = new Definitions\EntityType($entity_class, $type_id, $type_config['name']); $this->registerEntityType($type); @@ -2598,11 +2671,11 @@ protected function register(){ $type_config['metadata'] = $type_meta; // add projects metadata definition to project type - foreach($projects_meta as $meta_key => $meta_config) - if(!key_exists($meta_key, $type_meta) || key_exists($meta_key, $type_meta) && is_null($type_config['metadata'][$meta_key])) + foreach ($projects_meta as $meta_key => $meta_config) + if (!key_exists($meta_key, $type_meta) || key_exists($meta_key, $type_meta) && is_null($type_config['metadata'][$meta_key])) $type_config['metadata'][$meta_key] = $meta_config; - foreach($type_config['metadata'] as $meta_key => $meta_config){ + foreach ($type_config['metadata'] as $meta_key => $meta_config) { $metadata = new Definitions\Metadata($meta_key, $meta_config); $this->registerMetadata($metadata, $entity_class, $type_id); } @@ -2611,7 +2684,7 @@ protected function register(){ // register opportunity types and opportunity metadata $entity_class = 'MapasCulturais\Entities\Opportunity'; - foreach($opportunity_types['items'] as $type_id => $type_config){ + foreach ($opportunity_types['items'] as $type_id => $type_config) { $type = new Definitions\EntityType($entity_class, $type_id, $type_config['name']); $this->registerEntityType($type); @@ -2619,11 +2692,11 @@ protected function register(){ $type_config['metadata'] = $type_meta; // add opportunities metadata definition to opportunity type - foreach($opportunities_meta as $meta_key => $meta_config) - if(!key_exists($meta_key, $type_meta) || key_exists($meta_key, $type_meta) && is_null($type_config['metadata'][$meta_key])) + foreach ($opportunities_meta as $meta_key => $meta_config) + if (!key_exists($meta_key, $type_meta) || key_exists($meta_key, $type_meta) && is_null($type_config['metadata'][$meta_key])) $type_config['metadata'][$meta_key] = $meta_config; - foreach($type_config['metadata'] as $meta_key => $meta_config){ + foreach ($type_config['metadata'] as $meta_key => $meta_config) { $metadata = new Definitions\Metadata($meta_key, $meta_config); $this->registerMetadata($metadata, $entity_class, $type_id); } @@ -2633,27 +2706,27 @@ protected function register(){ $entity_class = 'MapasCulturais\Entities\Subsite'; // add subsite metadata definition to event type - foreach($subsite_meta as $meta_key => $meta_config){ + foreach ($subsite_meta as $meta_key => $meta_config) { $metadata = new Definitions\Metadata($meta_key, $meta_config); $this->registerMetadata($metadata, $entity_class); } // register seal time unit types - $entity_class = 'MapasCulturais\Entities\Seal'; + $entity_class = 'MapasCulturais\Entities\Seal'; - foreach($seal_types['items'] as $type_id => $type_config){ - $type = new Definitions\EntityType($entity_class, $type_id, $type_config['name']); - $this->registerEntityType($type); + foreach ($seal_types['items'] as $type_id => $type_config) { + $type = new Definitions\EntityType($entity_class, $type_id, $type_config['name']); + $this->registerEntityType($type); $type_meta = key_exists('metadata', $type_config) && is_array($type_config['metadata']) ? $type_config['metadata'] : []; $type_config['metadata'] = $type_meta; - - // add projects metadata definition to project type - foreach($seals_meta as $meta_key => $meta_config) - if(!key_exists($meta_key, $type_meta) || key_exists($meta_key, $type_meta) && is_null($type_config['metadata'][$meta_key])) + + // add projects metadata definition to project type + foreach ($seals_meta as $meta_key => $meta_config) + if (!key_exists($meta_key, $type_meta) || key_exists($meta_key, $type_meta) && is_null($type_config['metadata'][$meta_key])) $type_config['metadata'][$meta_key] = $meta_config; - foreach($type_config['metadata'] as $meta_key => $meta_config){ + foreach ($type_config['metadata'] as $meta_key => $meta_config) { $metadata = new Definitions\Metadata($meta_key, $meta_config); $this->registerMetadata($metadata, $entity_class, $type_id); } @@ -2663,19 +2736,19 @@ protected function register(){ $entity_class = 'MapasCulturais\Entities\Notification'; // add notification metadata definition - foreach($notification_meta as $meta_key => $meta_config){ + foreach ($notification_meta as $meta_key => $meta_config) { $metadata = new Definitions\Metadata($meta_key, $meta_config); $this->registerMetadata($metadata, $entity_class); } // register taxonomies - if ($theme_taxonomies = $this->view->resolveFilename('','taxonomies.php')) { + if ($theme_taxonomies = $this->view->resolveFilename('', 'taxonomies.php')) { $taxonomies = include $theme_taxonomies; } else { $taxonomies = include APPLICATION_PATH . '/conf/taxonomies.php'; } - foreach($taxonomies as $taxonomy_id => $taxonomy_definition){ + foreach ($taxonomies as $taxonomy_id => $taxonomy_definition) { $taxonomy_slug = $taxonomy_definition['slug']; $taxonomy_required = key_exists('required', $taxonomy_definition) ? $taxonomy_definition['required'] : false; $taxonomy_description = key_exists('description', $taxonomy_definition) ? $taxonomy_definition['description'] : ''; @@ -2685,22 +2758,22 @@ protected function register(){ $definition->name = $taxonomy_definition['name'] ?? ''; $entity_classes = $taxonomy_definition['entities']; - foreach($entity_classes as $entity_class){ + foreach ($entity_classes as $entity_class) { $this->registerTaxonomy($entity_class, $definition); } } $this->view->register(); - foreach($this->modules as $module){ + foreach ($this->modules as $module) { $module->register(); } - foreach($this->plugins as $plugin){ + foreach ($this->plugins as $plugin) { $plugin->register(); } - $this->applyHookBoundTo($this, 'app.register',[&$this->_register]); + $this->applyHookBoundTo($this, 'app.register', [&$this->_register]); $this->applyHookBoundTo($this, 'app.register:after'); } @@ -2715,8 +2788,9 @@ protected function register(){ * @return void * @throws GlobalException */ - public function registerJobType(Definitions\JobType $definition) { - if(key_exists($definition->slug, $this->_register['job_types'])){ + public function registerJobType(Definitions\JobType $definition) + { + if (key_exists($definition->slug, $this->_register['job_types'])) { throw new \Exception("Job type {$definition->slug} already registered"); } $this->_register['job_types'][$definition->slug] = $definition; @@ -2727,7 +2801,8 @@ public function registerJobType(Definitions\JobType $definition) { * * @return Definitions\JobType[] */ - public function getRegisteredJobTypes(): array { + public function getRegisteredJobTypes(): array + { return $this->_register['job_types']; } @@ -2736,7 +2811,8 @@ public function getRegisteredJobTypes(): array { * * @return Definitions\JobType */ - public function getRegisteredJobType(string $slug): Definitions\JobType|null { + public function getRegisteredJobType(string $slug): Definitions\JobType|null + { return $this->_register['job_types'][$slug] ?? null; } @@ -2751,7 +2827,8 @@ public function getRegisteredJobType(string $slug): Definitions\JobType|null { * @param Definitions\Role $role the role definition * @return void */ - public function registerRole(Definitions\Role $role) { + public function registerRole(Definitions\Role $role) + { $this->_register['roles'][$role->getRole()] = $role; } @@ -2760,7 +2837,8 @@ public function registerRole(Definitions\Role $role) { * * @return Definitions\Role[] */ - public function getRoles(): array { + public function getRoles(): array + { return $this->_register['roles']; } @@ -2770,7 +2848,8 @@ public function getRoles(): array { * @param string $role_slug * @return Definitions\Role|null */ - public function getRoleDefinition(string $role_slug): Definitions\Role|null { + public function getRoleDefinition(string $role_slug): Definitions\Role|null + { return $this->_register['roles'][$role_slug] ?? null; } @@ -2780,7 +2859,8 @@ public function getRoleDefinition(string $role_slug): Definitions\Role|null { * @param string $role_slug * @return string|null */ - public function getRoleName(string $role_slug): string|null { + public function getRoleName(string $role_slug): string|null + { $def = $this->_register['roles'][$role_slug] ?? null; return $def ? $def->name : $role_slug; } @@ -2797,9 +2877,10 @@ public function getRoleName(string $role_slug): string|null { * @return void * @throws GlobalException */ - function registerRegistrationAgentRelation(Definitions\RegistrationAgentRelation $def) { + function registerRegistrationAgentRelation(Definitions\RegistrationAgentRelation $def) + { $group_name = $def->agentRelationGroupName; - if($this->_register['registration_agent_relations'][$group_name] ?? false){ + if ($this->_register['registration_agent_relations'][$group_name] ?? false) { throw new \Exception('There is already a RegistrationAgentRelation with agent relation group name "' . $def->agentRelationGroupName . '"'); } $this->_register['registration_agent_relations'][$group_name] = $def; @@ -2810,7 +2891,8 @@ function registerRegistrationAgentRelation(Definitions\RegistrationAgentRelation * * @return Definitions\RegistrationAgentRelation[] */ - function getRegisteredRegistrationAgentRelations(): array { + function getRegisteredRegistrationAgentRelations(): array + { return $this->_register['registration_agent_relations']; } @@ -2819,7 +2901,8 @@ function getRegisteredRegistrationAgentRelations(): array { * * @return Definitions\RegistrationAgentRelation */ - function getRegistrationOwnerDefinition(): Definitions\RegistrationAgentRelation{ + function getRegistrationOwnerDefinition(): Definitions\RegistrationAgentRelation + { $config = $this->getConfig('registration.ownerDefinition'); $definition = new Definitions\RegistrationAgentRelation($config); return $definition; @@ -2830,9 +2913,10 @@ function getRegistrationOwnerDefinition(): Definitions\RegistrationAgentRelation * * @return Definitions\RegistrationAgentRelation[] */ - function getRegistrationAgentsDefinitions(): array { + function getRegistrationAgentsDefinitions(): array + { $definitions = ['owner' => $this->getRegistrationOwnerDefinition()]; - foreach ($this->getRegisteredRegistrationAgentRelations() as $groupName => $def){ + foreach ($this->getRegisteredRegistrationAgentRelations() as $groupName => $def) { $definitions[$groupName] = $def; } return $definitions; @@ -2843,7 +2927,8 @@ function getRegistrationAgentsDefinitions(): array { * @param string $group_name * @return RegistrationAgentRelation|null */ - function getRegisteredRegistrationAgentRelationByAgentRelationGroupName(string $group_name): Definitions\RegistrationAgentRelation|null { + function getRegisteredRegistrationAgentRelationByAgentRelationGroupName(string $group_name): Definitions\RegistrationAgentRelation|null + { return $this->_register['registration_agent_relations'][$group_name] ?? null; } @@ -2852,14 +2937,15 @@ function getRegisteredRegistrationAgentRelationByAgentRelationGroupName(string $ * ============ AGENTES RELACIONADOS DAS INSCRIÇÕES ============ */ - /** - * Registra um tipo de thread de chat - * - * @param ChatThreadType $definition - * @return void - * @throws GlobalException - */ - function registerChatThreadType(Definitions\ChatThreadType $definition) { + /** + * Registra um tipo de thread de chat + * + * @param ChatThreadType $definition + * @return void + * @throws GlobalException + */ + function registerChatThreadType(Definitions\ChatThreadType $definition) + { if (isset($this->_register['chat_thread_types'][$definition->slug])) { throw new \Exception("Attempting to re-register {$definition->slug}."); } @@ -2871,7 +2957,8 @@ function registerChatThreadType(Definitions\ChatThreadType $definition) { * * @return array */ - function getRegisteredChatThreadTypes(): array { + function getRegisteredChatThreadTypes(): array + { return $this->_register['chat_thread_types']; } @@ -2881,7 +2968,8 @@ function getRegisteredChatThreadTypes(): array { * @param mixed $slug * @return ChatThreadType|null */ - function getRegisteredChatThreadType($slug): Definitions\ChatThreadType|null { + function getRegisteredChatThreadType($slug): Definitions\ChatThreadType|null + { return ($this->_register['chat_thread_types'][$slug] ?? null); } @@ -2897,8 +2985,9 @@ function getRegisteredChatThreadType($slug): Definitions\ChatThreadType|null * @param string $api_output_id the api_output id * */ - public function registerApiOutput($api_output_class_name, $api_output_id = null){ - if(is_null($api_output_id)) + public function registerApiOutput($api_output_class_name, $api_output_id = null) + { + if (is_null($api_output_id)) $api_output_id = strtolower(str_replace('\\', '.', str_replace('MapasCulturais\ApiOutputs\\', '', $api_output_class_name))); $this->_register['api_outputs'][$api_output_id] = $api_output_class_name; @@ -2914,8 +3003,9 @@ public function registerApiOutput($api_output_class_name, $api_output_id = null) * * @return ApiOutput|null the API Output */ - public function getRegisteredApiOutputByClassName($api_output_class_name): ApiOutput|null { - if(in_array($api_output_class_name, $this->_register['api_outputs']) && class_exists($api_output_class_name) && is_subclass_of($api_output_class_name, '\MapasCulturais\ApiOutput')) { + public function getRegisteredApiOutputByClassName($api_output_class_name): ApiOutput|null + { + if (in_array($api_output_class_name, $this->_register['api_outputs']) && class_exists($api_output_class_name) && is_subclass_of($api_output_class_name, '\MapasCulturais\ApiOutput')) { return $api_output_class_name::i(); } else { return null; @@ -2931,15 +3021,15 @@ public function getRegisteredApiOutputByClassName($api_output_class_name): ApiOu * * @return ApiOutput|null The API Output */ - public function getRegisteredApiOutputById(string $api_output_id): ApiOutput|null { + public function getRegisteredApiOutputById(string $api_output_id): ApiOutput|null + { $api_output_id = strtolower($api_output_id); - if(key_exists($api_output_id, $this->_register['api_outputs']) && class_exists($this->_register['api_outputs'][$api_output_id]) && is_subclass_of($this->_register['api_outputs'][$api_output_id], '\MapasCulturais\ApiOutput')){ + if (key_exists($api_output_id, $this->_register['api_outputs']) && class_exists($this->_register['api_outputs'][$api_output_id]) && is_subclass_of($this->_register['api_outputs'][$api_output_id], '\MapasCulturais\ApiOutput')) { $api_output_class_name = $this->_register['api_outputs'][$api_output_id]; return $api_output_class_name::i(); - }else{ + } else { return null; } - } /** @@ -2951,7 +3041,8 @@ public function getRegisteredApiOutputById(string $api_output_id): ApiOutput|nul * * @return string|null the API Output id */ - public function getRegisteredApiOutputId($api_output): string|null { + public function getRegisteredApiOutputId($api_output): string|null + { if (is_object($api_output)) { $api_output = get_class($api_output); } @@ -2972,7 +3063,8 @@ public function getRegisteredApiOutputId($api_output): string|null { * @param string $name * @return void */ - public function registerAuthProvider(string $name) { + public function registerAuthProvider(string $name) + { $nextId = count($this->_register['auth_providers']) + 1; $this->_register['auth_providers'][$nextId] = strtolower($name); } @@ -2982,7 +3074,8 @@ public function registerAuthProvider(string $name) { * @param mixed $name * @return int|string|false */ - public function getRegisteredAuthProviderId($name){ + public function getRegisteredAuthProviderId($name) + { return array_search(strtolower($name), $this->_register['auth_providers']); } @@ -3001,10 +3094,11 @@ public function getRegisteredAuthProviderId($name){ * * @throws \Exception */ - public function registerController(string $id, string $controller_class_name, string $default_action = 'index', $view_dir = null) { + public function registerController(string $id, string $controller_class_name, string $default_action = 'index', $view_dir = null) + { $id = strtolower($id); - if(key_exists($id, $this->_register['controllers'])) + if (key_exists($id, $this->_register['controllers'])) throw new \Exception('Controller Id already in use'); $this->_register['controllers-by-class'][$controller_class_name] = $id; @@ -3021,10 +3115,11 @@ public function registerController(string $id, string $controller_class_name, st * * @return array */ - public function getRegisteredControllers(bool $return_controller_object = false): array { + public function getRegisteredControllers(bool $return_controller_object = false): array + { $controllers = $this->_register['controllers']; - if($return_controller_object){ - foreach($controllers as $id => $class){ + if ($return_controller_object) { + foreach ($controllers as $id => $class) { $controllers[$id] = $class::i(); } } @@ -3043,12 +3138,13 @@ public function getRegisteredControllers(bool $return_controller_object = false) * * @return Controller|null */ - public function getController(string $controller_id): Controller|null { + public function getController(string $controller_id): Controller|null + { $controller_id = strtolower($controller_id); - if(key_exists($controller_id, $this->_register['controllers']) && class_exists($this->_register['controllers'][$controller_id])){ + if (key_exists($controller_id, $this->_register['controllers']) && class_exists($this->_register['controllers'][$controller_id])) { $class = $this->_register['controllers'][$controller_id]; return $class::i($controller_id); - }else{ + } else { return null; } } @@ -3062,7 +3158,8 @@ public function getController(string $controller_id): Controller|null { * * @return Controller|null */ - public function controller(string $controller_id): Controller|null { + public function controller(string $controller_id): Controller|null + { return $this->getController($controller_id); } @@ -3076,10 +3173,11 @@ public function controller(string $controller_id): Controller|null { * * @return Controller|null The controller */ - public function getControllerByClass(string $controller_class): Controller|null { - if(key_exists($controller_class, $this->_register['controllers-by-class']) && class_exists($controller_class)){ + public function getControllerByClass(string $controller_class): Controller|null + { + if (key_exists($controller_class, $this->_register['controllers-by-class']) && class_exists($controller_class)) { return $controller_class::i($this->_register['controllers-by-class'][$controller_class]); - }else{ + } else { return null; } } @@ -3097,10 +3195,11 @@ public function getControllerByClass(string $controller_class): Controller|null * * @return Controllers\EntityController|null The controller */ - public function getControllerByEntity(Entity|string $entity): Controller|null { - if(is_object($entity)) + public function getControllerByEntity(Entity|string $entity): Controller|null + { + if (is_object($entity)) $entity = $entity->getClassName(); - + $controller_class = $entity::getControllerClassName(); return $this->getControllerByClass($controller_class); } @@ -3116,8 +3215,9 @@ public function getControllerByEntity(Entity|string $entity): Controller|null { * * @return Controller|null The controller */ - public function getControllerIdByEntity(Entity|string $entity): string|null { - if(is_object($entity)) + public function getControllerIdByEntity(Entity|string $entity): string|null + { + if (is_object($entity)) $entity = $entity->getClassName(); $controller_class = $entity::getControllerClassName(); @@ -3132,8 +3232,9 @@ public function getControllerIdByEntity(Entity|string $entity): string|null { * * @return string|null */ - public function getControllerId(Controller|string $controller): string|null { - if(is_object($controller)) + public function getControllerId(Controller|string $controller): string|null + { + if (is_object($controller)) $controller = get_class($controller); return array_search($controller, $this->_register['controllers']) ?: null; @@ -3148,7 +3249,8 @@ public function getControllerId(Controller|string $controller): string|null { * * @return string|null */ - public function controllerId(Controller|string $controller): string|null { + public function controllerId(Controller|string $controller): string|null + { return $this->getControllerId($controller); } @@ -3160,11 +3262,12 @@ public function controllerId(Controller|string $controller): string|null { * * @return string|null */ - public function getControllerDefaultAction(string $controller_id): string|null { + public function getControllerDefaultAction(string $controller_id): string|null + { $controller_id = strtolower($controller_id); - if(key_exists($controller_id, $this->_register['controllers_default_actions'])){ + if (key_exists($controller_id, $this->_register['controllers_default_actions'])) { return $this->_register['controllers_default_actions'][$controller_id]; - }else{ + } else { return null; } } @@ -3179,7 +3282,8 @@ public function getControllerDefaultAction(string $controller_id): string|null { * * @return string|null */ - public function controllerDefaultAction(string $controller_id): string|null { + public function controllerDefaultAction(string $controller_id): string|null + { return $this->getControllerDefaultAction($controller_id); } @@ -3193,9 +3297,10 @@ public function controllerDefaultAction(string $controller_id): string|null { * * @param Definitions\EntityTypeGroup $group The Entity Type Group to register. */ - function registerEntityTypeGroup(Definitions\EntityTypeGroup $group){ - if(!key_exists($group->entity_class, $this->_register['entity_type_groups'])) - $this->_register['entity_type_groups'][$group->entity_class] = []; + function registerEntityTypeGroup(Definitions\EntityTypeGroup $group) + { + if (!key_exists($group->entity_class, $this->_register['entity_type_groups'])) + $this->_register['entity_type_groups'][$group->entity_class] = []; $this->_register['entity_type_groups'][$group->entity_class][] = $group; } @@ -3208,17 +3313,18 @@ function registerEntityTypeGroup(Definitions\EntityTypeGroup $group){ * * @return Definitions\EntityTypeGroup|null */ - function getRegisteredEntityTypeGroupByTypeId(Entity|string $entity, int $type_id): Definitions\EntityTypeGroup|null { - if(is_object($entity)) + function getRegisteredEntityTypeGroupByTypeId(Entity|string $entity, int $type_id): Definitions\EntityTypeGroup|null + { + if (is_object($entity)) $entity = $entity->getClassName(); - if(key_exists($entity, $this->_register['entity_type_groups'])){ - foreach($this->_register['entity_type_groups'][$entity] as $group){ - if($group->min_id >= $type_id && $group->max_id <= $type_id) + if (key_exists($entity, $this->_register['entity_type_groups'])) { + foreach ($this->_register['entity_type_groups'][$entity] as $group) { + if ($group->min_id >= $type_id && $group->max_id <= $type_id) return $group; } return null; - }else{ + } else { return null; } } @@ -3230,13 +3336,14 @@ function getRegisteredEntityTypeGroupByTypeId(Entity|string $entity, int $type_i * * @return Definitions\EntityTypeGroup[] */ - function getRegisteredEntityTypeGroupsByEntity(Entity|string $entity): array { - if(is_object($entity)) + function getRegisteredEntityTypeGroupsByEntity(Entity|string $entity): array + { + if (is_object($entity)) $entity = $entity->getClassName(); - if(key_exists($entity, $this->_register['entity_type_groups'])){ + if (key_exists($entity, $this->_register['entity_type_groups'])) { return $this->_register['entity_type_groups'][$entity]; - }else{ + } else { return []; } } @@ -3247,8 +3354,9 @@ function getRegisteredEntityTypeGroupsByEntity(Entity|string $entity): array { * @param Definitions\EntityType $type * @return void */ - function registerEntityType(Definitions\EntityType $type){ - if (!key_exists($type->entity_class, $this->_register['entity_types'])){ + function registerEntityType(Definitions\EntityType $type) + { + if (!key_exists($type->entity_class, $this->_register['entity_types'])) { $this->_register['entity_types'][$type->entity_class] = []; } @@ -3265,7 +3373,8 @@ function registerEntityType(Definitions\EntityType $type){ * @throws ReflectionException * @throws MappingException */ - function getRegisteredEntityTypeById(Entity|string $entity, int|string|null $type_id): Definitions\EntityType|null { + function getRegisteredEntityTypeById(Entity|string $entity, int|string|null $type_id): Definitions\EntityType|null + { if (is_object($entity)) { $entity = $entity->getClassName(); } @@ -3281,7 +3390,8 @@ function getRegisteredEntityTypeById(Entity|string $entity, int|string|null $typ * * @return boolean */ - function entityTypeExists(Entity|string $entity, int|string $type_id): bool { + function entityTypeExists(Entity|string $entity, int|string $type_id): bool + { return !!$this->getRegisteredEntityTypeById($entity, $type_id); } @@ -3292,7 +3402,8 @@ function entityTypeExists(Entity|string $entity, int|string $type_id): bool { * * @return Definitions\EntityType */ - function getRegisteredEntityType(Entity $entity): Definitions\EntityType|null { + function getRegisteredEntityType(Entity $entity): Definitions\EntityType|null + { return $this->_register['entity_types'][$entity->getClassName()][(string)$entity->type] ?? null; } @@ -3306,8 +3417,9 @@ function getRegisteredEntityType(Entity $entity): Definitions\EntityType|null { * @throws ReflectionException * @throws MappingException */ - function getRegisteredEntityTypeByTypeName(Entity|string $entity, string $type_name): Definitions\EntityType|null { - foreach($this->getRegisteredEntityTypes($entity) as $type) { + function getRegisteredEntityTypeByTypeName(Entity|string $entity, string $type_name): Definitions\EntityType|null + { + foreach ($this->getRegisteredEntityTypes($entity) as $type) { if (strtolower($type->name) == trim(strtolower($type_name))) { return $type; } @@ -3323,10 +3435,15 @@ function getRegisteredEntityTypeByTypeName(Entity|string $entity, string $type_n * * @return Definitions\EntityType[] */ - function getRegisteredEntityTypes(Entity|string $entity): array { - if(is_object($entity)) + function getRegisteredEntityTypes(Entity|string $entity): array + { + if (is_object($entity)) $entity = $entity->getClassName(); + usort($this->_register['entity_types'][$entity], function ($a, $b) { + return strcmp($a->name, $b->name); + }); + return $this->_register['entity_types'][$entity] ?? []; } @@ -3342,7 +3459,8 @@ function getRegisteredEntityTypes(Entity|string $entity): array { * @param RegistrationFieldType $registration_field * @return void */ - function registerRegistrationFieldType(Definitions\RegistrationFieldType $registration_field) { + function registerRegistrationFieldType(Definitions\RegistrationFieldType $registration_field) + { $this->_register['registration_fields'][$registration_field->slug] = $registration_field; } @@ -3351,7 +3469,8 @@ function registerRegistrationFieldType(Definitions\RegistrationFieldType $regist * * @return Definitions\RegistrationFieldType[] */ - function getRegisteredRegistrationFieldTypes(): array { + function getRegisteredRegistrationFieldTypes(): array + { return $this->_register['registration_fields']; } @@ -3361,7 +3480,8 @@ function getRegisteredRegistrationFieldTypes(): array { * @param string $slug * @return RegistrationFieldType|null */ - function getRegisteredRegistrationFieldTypeBySlug(string $slug): Definitions\RegistrationFieldType|null { + function getRegisteredRegistrationFieldTypeBySlug(string $slug): Definitions\RegistrationFieldType|null + { return $this->_register['registration_fields'][$slug] ?? null; } @@ -3378,24 +3498,25 @@ function getRegisteredRegistrationFieldTypeBySlug(string $slug): Definitions\Reg * @param string $entity_class * @param int|string|null $entity_type_id */ - function registerMetadata(Definitions\Metadata $metadata, string $entity_class, int|string $entity_type_id = null) { - if($entity_class::usesTypes() && is_null($entity_type_id)){ - foreach($this->getRegisteredEntityTypes($entity_class) as $type){ - if($type){ - $this->registerMetadata($metadata, $entity_class, $type->id); + function registerMetadata(Definitions\Metadata $metadata, string $entity_class, int|string $entity_type_id = null) + { + if ($entity_class::usesTypes() && is_null($entity_type_id)) { + foreach ($this->getRegisteredEntityTypes($entity_class) as $type) { + if ($type) { + $this->registerMetadata($metadata, $entity_class, $type->id); } } return; } $key = is_null($entity_type_id) ? $entity_class : $entity_class . ':' . $entity_type_id; - if(!key_exists($key, $this->_register['entity_metadata_definitions'])) + if (!key_exists($key, $this->_register['entity_metadata_definitions'])) $this->_register['entity_metadata_definitions'][$key] = []; $this->_register['entity_metadata_definitions'][$key][$metadata->key] = $metadata; - if($entity_type_id){ - if(!key_exists($entity_class, $this->_register['entity_metadata_definitions'])) + if ($entity_type_id) { + if (!key_exists($entity_class, $this->_register['entity_metadata_definitions'])) $this->_register['entity_metadata_definitions'][$entity_class] = []; $this->_register['entity_metadata_definitions'][$entity_class][$metadata->key] = $metadata; @@ -3412,17 +3533,17 @@ function registerMetadata(Definitions\Metadata $metadata, string $entity_class, * * @return void */ - function unregisterEntityMetadata(string $entity_class, string $key = null) { - foreach($this->_register['entity_metadata_definitions'] as $class => $metadata){ - if($class === $entity_class || strpos($class . ':', $entity_class) === 0){ - if($key){ + function unregisterEntityMetadata(string $entity_class, string $key = null) + { + foreach ($this->_register['entity_metadata_definitions'] as $class => $metadata) { + if ($class === $entity_class || strpos($class . ':', $entity_class) === 0) { + if ($key) { unset($this->_register['entity_metadata_definitions'][$class][$key]); } else { $this->_register['entity_metadata_definitions'][$class] = []; } } } - } /** @@ -3434,14 +3555,15 @@ function unregisterEntityMetadata(string $entity_class, string $key = null) { * * @return Definitions\Metadata[] */ - function getRegisteredMetadata(Entity|string $entity, int|Definitions\EntityType $type = null){ + function getRegisteredMetadata(Entity|string $entity, int|Definitions\EntityType $type = null) + { if (is_object($entity)) { $entity = $entity->getClassName(); } $key = $entity::usesTypes() && $type ? "{$entity}:{$type}" : $entity; - return key_exists($key, $this->_register['entity_metadata_definitions']) ? + return key_exists($key, $this->_register['entity_metadata_definitions']) ? $this->_register['entity_metadata_definitions'][$key] : []; } @@ -3453,11 +3575,12 @@ function getRegisteredMetadata(Entity|string $entity, int|Definitions\EntityType * @param int $type * @return Definitions\Metadata|null */ - function getRegisteredMetadataByMetakey(string $metakey, Entity|string $entity, int $type = null): Definitions\Metadata|null { + function getRegisteredMetadataByMetakey(string $metakey, Entity|string $entity, int $type = null): Definitions\Metadata|null + { if (is_object($entity)) { $entity = $entity->getClassName(); } - + $metas = $this->getRegisteredMetadata($entity, $type); return $metas[$metakey] ?? null; @@ -3475,10 +3598,11 @@ function getRegisteredMetadataByMetakey(string $metakey, Entity|string $entity, * @param string $controller_id The id of the controller. * @param Definitions\FileGroup $group The group to register */ - function registerFileGroup(string $controller_id, Definitions\FileGroup $group){ + function registerFileGroup(string $controller_id, Definitions\FileGroup $group) + { $controller_id = strtolower($controller_id); - if(!key_exists($controller_id, $this->_register['file_groups'])){ + if (!key_exists($controller_id, $this->_register['file_groups'])) { $this->_register['file_groups'][$controller_id] = []; } @@ -3495,7 +3619,8 @@ function registerFileGroup(string $controller_id, Definitions\FileGroup $group){ * * @return Definitions\FileGroup|null The File Group Definition */ - function getRegisteredFileGroup(string $controller_id, string $group_name): Definitions\FileGroup|null { + function getRegisteredFileGroup(string $controller_id, string $group_name): Definitions\FileGroup|null + { return $this->_register['file_groups'][$controller_id][$group_name] ?? null; } @@ -3508,7 +3633,8 @@ function getRegisteredFileGroup(string $controller_id, string $group_name): Defi * @throws ReflectionException * @throws MappingException */ - function getRegisteredFileGroupsByEntity(Entity|string $entity): array { + function getRegisteredFileGroupsByEntity(Entity|string $entity): array + { if (is_object($entity)) { $entity = $entity->getClassName(); } @@ -3532,7 +3658,8 @@ function getRegisteredFileGroupsByEntity(Entity|string $entity): array { * @param string $name * @param string $transformation */ - function registerImageTransformation(string $name, string $transformation) { + function registerImageTransformation(string $name, string $transformation) + { $this->_register['image_transformations'][$name] = trim($transformation); } @@ -3543,7 +3670,8 @@ function registerImageTransformation(string $name, string $transformation) { * * @return string The Transformation Expression */ - function getRegisteredImageTransformation(string $name): string { + function getRegisteredImageTransformation(string $name): string + { return $this->_register['image_transformations'][$name] ?? null; } @@ -3559,8 +3687,9 @@ function getRegisteredImageTransformation(string $name): string { * @param string $controller_id The id of the controller. * @param Definitions\MetaListGroup $group The group to register */ - function registerMetaListGroup(string $controller_id, Definitions\MetaListGroup $group) { - if(!key_exists($controller_id, $this->_register['metalist_groups'])) + function registerMetaListGroup(string $controller_id, Definitions\MetaListGroup $group) + { + if (!key_exists($controller_id, $this->_register['metalist_groups'])) $this->_register['metalist_groups'][$controller_id] = []; $this->_register['metalist_groups'][$controller_id][$group->name] = $group; @@ -3576,7 +3705,8 @@ function registerMetaListGroup(string $controller_id, Definitions\MetaListGroup * * @return Definitions\MetaListGroup|null The MetaList Group Definition */ - function getRegisteredMetaListGroup(string $controller_id, string $group_name): Definitions\MetaListGroup|null { + function getRegisteredMetaListGroup(string $controller_id, string $group_name): Definitions\MetaListGroup|null + { return $this->_register['metalist_groups'][$controller_id][$group_name] ?? null; } @@ -3590,8 +3720,9 @@ function getRegisteredMetaListGroup(string $controller_id, string $group_name): * @throws ReflectionException * @throws MappingException */ - function getRegisteredMetaListGroupsByEntity(Entity|string $entity): array { - if(is_object($entity)) + function getRegisteredMetaListGroupsByEntity(Entity|string $entity): array + { + if (is_object($entity)) $entity = $entity->getClassName(); $controller_id = $this->getControllerIdByEntity($entity); @@ -3605,7 +3736,8 @@ function getRegisteredMetaListGroupsByEntity(Entity|string $entity): array { * @param string $entity_class The entity class name to register. * @param Definitions\Taxonomy $definition */ - function registerTaxonomy($entity_class, Definitions\Taxonomy $definition) { + function registerTaxonomy($entity_class, Definitions\Taxonomy $definition) + { if (!key_exists($entity_class, $this->_register['taxonomies']['by-entity'])) { $this->_register['taxonomies']['by-entity'][$entity_class] = []; } @@ -3623,7 +3755,8 @@ function registerTaxonomy($entity_class, Definitions\Taxonomy $definition) { * * @return Definitions\Taxonomy The Taxonomy Definition */ - function getRegisteredTaxonomyById($taxonomy_id): Definitions\Taxonomy|null { + function getRegisteredTaxonomyById($taxonomy_id): Definitions\Taxonomy|null + { return $this->_register['taxonomies']['by-id'][$taxonomy_id] ?? null; } @@ -3634,7 +3767,8 @@ function getRegisteredTaxonomyById($taxonomy_id): Definitions\Taxonomy|null { * * @return Definitions\Taxonomy The Taxonomy Definition */ - function getRegisteredTaxonomyBySlug(string $taxonomy_slug): Definitions\Taxonomy|null { + function getRegisteredTaxonomyBySlug(string $taxonomy_slug): Definitions\Taxonomy|null + { return $this->_register['taxonomies']['by-slug'][$taxonomy_slug] ?? null; } @@ -3647,14 +3781,15 @@ function getRegisteredTaxonomyBySlug(string $taxonomy_slug): Definitions\Taxonom * * @return Definitions\Taxonomy[] The Taxonomy Definitions objects or an empty array */ - function getRegisteredTaxonomies(Entity|string $entity = null): array { + function getRegisteredTaxonomies(Entity|string $entity = null): array + { if (is_object($entity)) { $entity = $entity->getClassName(); } - if(is_null($entity)){ + if (is_null($entity)) { return $this->_register['taxonomies']['by-slug']; - }else{ + } else { return $this->_register['taxonomies']['by-entity'][$entity] ?? []; } } @@ -3669,7 +3804,8 @@ function getRegisteredTaxonomies(Entity|string $entity = null): array { * * @return Definitions\Taxonomy|null The Taxonomy Definition. */ - function getRegisteredTaxonomy(Entity|string $entity, string $taxonomy_slug): Definitions\Taxonomy|null { + function getRegisteredTaxonomy(Entity|string $entity, string $taxonomy_slug): Definitions\Taxonomy|null + { if (is_object($entity)) { $entity = $entity->getClassName(); } @@ -3688,7 +3824,8 @@ function getRegisteredTaxonomy(Entity|string $entity, string $taxonomy_slug): De * * @param Definitions\EvaluationMethod $def */ - function registerEvaluationMethod(Definitions\EvaluationMethod $def) { + function registerEvaluationMethod(Definitions\EvaluationMethod $def) + { $this->_register['evaluation_method'][$def->slug] = $def; } @@ -3700,9 +3837,10 @@ function registerEvaluationMethod(Definitions\EvaluationMethod $def) { * * @return Definitions\EvaluationMethod[]; */ - function getRegisteredEvaluationMethods(bool $return_internal = false): array { - return array_filter($this->_register['evaluation_method'], function(Definitions\EvaluationMethod $em) use ($return_internal) { - if($return_internal || !$em->internal) { + function getRegisteredEvaluationMethods(bool $return_internal = false): array + { + return array_filter($this->_register['evaluation_method'], function (Definitions\EvaluationMethod $em) use ($return_internal) { + if ($return_internal || !$em->internal) { return $em; } }); @@ -3713,7 +3851,8 @@ function getRegisteredEvaluationMethods(bool $return_internal = false): array { * * @param Definitions\EvaluationMethod $def */ - function unregisterEvaluationMethod(string $slug){ + function unregisterEvaluationMethod(string $slug) + { unset($this->_register['evaluation_method'][$slug]); } @@ -3724,7 +3863,8 @@ function unregisterEvaluationMethod(string $slug){ * * @return Definitions\EvaluationMethod; */ - function getRegisteredEvaluationMethodBySlug(string $slug){ + function getRegisteredEvaluationMethodBySlug(string $slug) + { return $this->_register['evaluation_method'][$slug] ?? null; } @@ -3736,49 +3876,49 @@ function getRegisteredEvaluationMethodBySlug(string $slug){ /** * Aplica os db-uptades */ - function _dbUpdates(){ + function _dbUpdates() + { $this->disableAccessControl(); $executed_updates = []; - foreach($this->repo('DbUpdate')->findAll() as $update) + foreach ($this->repo('DbUpdate')->findAll() as $update) $executed_updates[] = $update->name; $updates = include DB_UPDATES_FILE; - foreach($this->view->path as $path){ + foreach ($this->view->path as $path) { $db_update_file = $path . 'db-updates.php'; - if(file_exists($db_update_file)){ + if (file_exists($db_update_file)) { $updates += include $db_update_file; } } $new_updates = false; - foreach($updates as $name => $function){ - if(!in_array($name, $executed_updates)){ + foreach ($updates as $name => $function) { + if (!in_array($name, $executed_updates)) { $new_updates = true; echo "\nApplying db update \"$name\":"; echo "\n-------------------------------------------------------------------------------------------------------\n"; - try{ - if($function() !== false){ + try { + if ($function() !== false) { $update = new Entities\DbUpdate(); $update->name = $name; $update->save(); } - }catch(\Exception $e){ + } catch (\Exception $e) { echo "\nERROR " . $e . "\n"; } echo "\n-------------------------------------------------------------------------------------------------------\n\n"; } } - if($new_updates){ + if ($new_updates) { $this->em->flush(); $this->cache->deleteAll(); } $this->enableAccessControl(); } - } diff --git a/src/core/Entity.php b/src/core/Entity.php index 813036b152..061b12aa82 100644 --- a/src/core/Entity.php +++ b/src/core/Entity.php @@ -1,4 +1,5 @@ >).new** - Executed when the __construct method of the $entity_class is called. */ - public function __construct() { + public function __construct() + { $app = App::i(); - foreach($app->em->getClassMetadata($this->getClassName())->associationMappings as $field => $conf){ - if($conf['type'] === 4){ + foreach ($app->em->getClassMetadata($this->getClassName())->associationMappings as $field => $conf) { + if ($conf['type'] === 4) { $this->$field = new \Doctrine\Common\Collections\ArrayCollection; } } - foreach($app->em->getClassMetadata($this->getClassName())->fieldMappings as $field => $conf){ + foreach ($app->em->getClassMetadata($this->getClassName())->fieldMappings as $field => $conf) { - if($conf['type'] == 'point'){ - $this->$field = new Types\GeoPoint(0,0); + if ($conf['type'] == 'point') { + $this->$field = new Types\GeoPoint(0, 0); } } - if(property_exists($this, 'createTimestamp')) { + if (property_exists($this, 'createTimestamp')) { $this->createTimestamp = new \DateTime; } - if(property_exists($this, 'updateTimestamp')) { + if (property_exists($this, 'updateTimestamp')) { $this->updateTimestamp = new \DateTime; } - if($this->usesOwnerAgent() && !$app->user->is('guest')){ + if ($this->usesOwnerAgent() && !$app->user->is('guest')) { $this->setOwner($app->user->profile); } - } - function __toString() { + function __toString() + { return $this->getClassName() . ':' . $this->id; } - static function isPrivateEntity(){ + static function isPrivateEntity() + { return false; } - function refresh(){ + function refresh() + { App::i()->em->refresh($this); } @@ -133,56 +138,61 @@ function refresh(){ * * @return self */ - function refreshed() { + function refreshed() + { return $this->repo()->find($this->id); } - function equals($entity){ + function equals($entity) + { return is_object($entity) && $entity instanceof Entity && $entity->getClassName() === $this->getClassName() && $entity->id === $this->id; } - function isNew(){ + function isNew() + { return App::i()->em->getUnitOfWork()->getEntityState($this) === \Doctrine\ORM\UnitOfWork::STATE_NEW; } - function isArchived(){ + function isArchived() + { return $this->status === self::STATUS_ARCHIVED; } - function simplify($properties = 'id,name'){ + function simplify($properties = 'id,name') + { $e = new \stdClass; $e->{'@entityType'} = $this->getControllerId(); - $properties = is_string($properties) ? explode(',',$properties) : $properties; - if(is_array($properties)){ - foreach($properties as $prop){ - switch ($prop){ + $properties = is_string($properties) ? explode(',', $properties) : $properties; + if (is_array($properties)) { + foreach ($properties as $prop) { + switch ($prop) { case 'className': $e->className = $this->getClassName(); - break; + break; case 'avatar': - if($this->usesAvatar()){ + if ($this->usesAvatar()) { $e->avatar = []; - if($avatar = $this->avatar){ - foreach($avatar->files as $transformation => $f){ + if ($avatar = $this->avatar) { + foreach ($avatar->files as $transformation => $f) { $e->avatar[$transformation] = $f->simplify('id,url'); } $e->files = $e->files ?? []; $e->files['avatar'] = $this->avatar->simplify('id,name,description,url,files'); } } - break; + break; case 'terms': - if($this->usesTaxonomies()) + if ($this->usesTaxonomies()) $e->terms = $this->getTerms(); - break; + break; default: $e->$prop = $this->__get($prop); - break; + break; } } } @@ -190,13 +200,15 @@ function simplify($properties = 'id,name'){ return $e; } - function dump(){ + function dump() + { echo '
';
         \Doctrine\Common\Util\Debug::dump($this);
         echo '
'; } - static function getClassName(){ + static function getClassName() + { return App::i()->em->getClassMetadata(get_called_class())->name; } @@ -205,20 +217,21 @@ static function getClassName(){ * * @return \MapasCulturais\Entities\User */ - function getOwnerUser(){ + function getOwnerUser() + { $app = App::i(); - if(isset($this->user)){ + if (isset($this->user)) { return $this->user; } $owner = $this->owner; - if(!$owner){ + if (!$owner) { return $app->user; } - if(!($owner instanceof Entity)){ + if (!($owner instanceof Entity)) { return $app->user; } @@ -233,12 +246,13 @@ function getOwnerUser(){ * @return array */ - static function getStatusesNames() { + static function getStatusesNames() + { $app = App::i(); $class = get_called_class(); - + $statuses = $class::_getStatusesNames(); - + // hook: entity(EntityName).statusesNames $hook_prefix = $class::getHookPrefix(); $app->applyHook("{$hook_prefix}.statusesNames", [&$statuses]); @@ -251,7 +265,8 @@ static function getStatusesNames() { * * @return array */ - protected static function _getStatusesNames() { + protected static function _getStatusesNames() + { return [ self::STATUS_ARCHIVED => i::__('Arquivado'), self::STATUS_DISABLED => i::__('Desabilitado'), @@ -266,7 +281,8 @@ protected static function _getStatusesNames() { * * @return string|null */ - static function getStatusNameById($status) { + static function getStatusNameById($status) + { $class = get_called_class(); $statuses = $class::getStatusesNames(); @@ -278,22 +294,23 @@ static function getStatusNameById($status) { * * @param int $status * @return void - */ - function setStatus(int $status){ + */ + function setStatus(int $status) + { $app = App::i(); - - if($status != $this->status){ - - switch($status){ + + if ($status != $this->status) { + + switch ($status) { case self::STATUS_ARCHIVED: - if($this->usesArchive()){ + if ($this->usesArchive()) { $this->checkPermission('archive'); } break; - + case self::STATUS_TRASH: - if($this->usesSoftDelete()){ + if ($this->usesSoftDelete()) { $this->checkPermission('remove'); } break; @@ -319,36 +336,38 @@ function setStatus(int $status){ $app->applyHookBoundTo($this, "{$hook_prefix}.setStatus({$status})", [&$status]); - if($this->usesPermissionCache() && !$this->__skipQueuingPCacheRecreation) { + if ($this->usesPermissionCache() && !$this->__skipQueuingPCacheRecreation) { $this->enqueueToPCacheRecreation(); } $this->status = $status; } - protected function fetchByStatus($collection, $status, $order = null){ - if(!is_object($collection) || !method_exists($collection, 'matching')) - return []; + protected function fetchByStatus($collection, $status, $order = null) + { + if (!is_object($collection) || !method_exists($collection, 'matching')) + return []; $criteria = Criteria::create()->where(Criteria::expr()->eq("status", $status)); - if(is_array($order)){ + if (is_array($order)) { $criteria = $criteria->orderBy($order); } return $collection->matching($criteria); } - protected function genericPermissionVerification($user){ - - if($user->is('guest')) + protected function genericPermissionVerification($user) + { + + if ($user->is('guest')) return false; - if($this->isUserAdmin($user)){ + if ($this->isUserAdmin($user)) { return true; } - if($this->getOwnerUser()->id == $user->id) + if ($this->getOwnerUser()->id == $user->id) return true; - if($this->usesAgentRelation() && $this->userHasControl($user)) + if ($this->usesAgentRelation() && $this->userHasControl($user)) return true; @@ -356,34 +375,36 @@ protected function genericPermissionVerification($user){ $permission = end($class_parts); $entity_user = $this->getOwnerUser(); - if($user->isAttorney("manage{$permission}", $entity_user)){ + if ($user->isAttorney("manage{$permission}", $entity_user)) { return true; } return false; } - protected function canUserView($user){ - if($this->status > 0){ + protected function canUserView($user) + { + if ($this->status > 0) { return true; - }else{ + } else { return $this->canUser('@control', $user); } } - protected function canUserCreate($user){ + protected function canUserCreate($user) + { $result = $this->genericPermissionVerification($user); - if($result && $this->usesOwnerAgent()){ + if ($result && $this->usesOwnerAgent()) { $owner = $this->getOwner(); - if(!$owner || $owner->status < 1){ + if (!$owner || $owner->status < 1) { $result = false; } } - if($result && $this->usesNested()){ + if ($result && $this->usesNested()) { $parent = $this->getParent(); - if($parent && $parent->status < 1){ + if ($parent && $parent->status < 1) { $result = false; } } @@ -392,35 +413,37 @@ protected function canUserCreate($user){ } - protected function canUserModify($user) { + protected function canUserModify($user) + { return $this->genericPermissionVerification($user); } - protected function canUserRemove($user){ - if($user->is('guest')) + protected function canUserRemove($user) + { + if ($user->is('guest')) return false; - if($this->isUserAdmin($user) || $this->getOwnerUser()->id == $user->id) { + if ($this->isUserAdmin($user) || $this->getOwnerUser()->id == $user->id) { return true; - } return false; } - protected function canUser_control($user) { - if ($this->usesAgentRelation() && $this->userHasControl($user)){ + protected function canUser_control($user) + { + if ($this->usesAgentRelation() && $this->userHasControl($user)) { return true; } - + if ($this->isUserAdmin($user)) { return true; - } - + } + if ($this->usesNested() && $this->parent && $this->parent->canUser('@control')) { return true; - } - + } + if (isset($this->owner) && $this->owner->canUser('@control')) { return true; } @@ -428,15 +451,16 @@ protected function canUser_control($user) { return false; } - public function canUser($action, $userOrAgent = null){ + public function canUser($action, $userOrAgent = null) + { $app = App::i(); - if(!$app->isAccessControlEnabled()){ + if (!$app->isAccessControlEnabled()) { return true; } - if(is_null($userOrAgent)){ + if (is_null($userOrAgent)) { $user = $app->user; - } else if($userOrAgent instanceof UserInterface) { + } else if ($userOrAgent instanceof UserInterface) { $user = $userOrAgent; } else { $user = $userOrAgent->getOwnerUser(); @@ -449,7 +473,7 @@ public function canUser($action, $userOrAgent = null){ $permission = end($class_parts); $entity_user = $this->getOwnerUser(); - if($user->isAttorney("{$action}{$permission}", $entity_user) || $user->isAttorney("manage{$permission}", $entity_user)){ + if ($user->isAttorney("{$action}{$permission}", $entity_user) || $user->isAttorney("manage{$permission}", $entity_user)) { $result = true; } else { if (strtolower($action) === '@control') { @@ -464,12 +488,11 @@ public function canUser($action, $userOrAgent = null){ $app->applyHookBoundTo($this, 'can(' . $this->getHookClassPath() . '.' . $action . ')', ['user' => $user, 'result' => &$result]); $app->applyHookBoundTo($this, $this->getHookPrefix() . '.canUser(' . $action . ')', ['user' => $user, 'result' => &$result]); - } return $result; } - + /** * Wether a user can access the private files owned by this entity * @@ -477,22 +500,24 @@ public function canUser($action, $userOrAgent = null){ * * Other entities can extend this method and change the verification * - */ - protected function canUserViewPrivateFiles($user) { - if($this->isPrivateEntity()) { + */ + protected function canUserViewPrivateFiles($user) + { + if ($this->isPrivateEntity()) { return $this->canUser('view', $user); - }else { + } else { return $this->canUser('@control', $user); } } - public function isUserAdmin(UserInterface $user, $role = 'admin'){ + public function isUserAdmin(UserInterface $user, $role = 'admin') + { $result = false; - if($this->usesOriginSubsite()){ - if($user->is($role, $this->_subsiteId)){ + if ($this->usesOriginSubsite()) { + if ($user->is($role, $this->_subsiteId)) { $result = true; } - } else if($user->is($role)) { + } else if ($user->is($role)) { $result = true; } @@ -504,15 +529,17 @@ public function isUserAdmin(UserInterface $user, $role = 'admin'){ return $result; } - public function checkPermission($action){ - if(!$this->canUser($action)) + public function checkPermission($action) + { + if (!$this->canUser($action)) throw new Exceptions\PermissionDenied(App::i()->user, $this, $action); } - public static function getPropertiesLabels(){ + public static function getPropertiesLabels() + { $result = []; - foreach(self::getPropertiesMetadata() as $key => $metadata){ - if(isset($metadata['@select'])){ + foreach (self::getPropertiesMetadata() as $key => $metadata) { + if (isset($metadata['@select'])) { $key = $metadata['@select']; } $result[$key] = $metadata['label']; @@ -520,22 +547,24 @@ public static function getPropertiesLabels(){ return $result; } - public static function getPropertyLabel($property_name){ + public static function getPropertyLabel($property_name) + { $labels = self::getPropertiesLabels(); return isset($labels[$property_name]) ? $labels[$property_name] : ''; } - public static function _getConfiguredPropertyLabel($property_name){ + public static function _getConfiguredPropertyLabel($property_name) + { $app = App::i(); $label = ''; $prop_labels = $app->config['app.entityPropertiesLabels']; - if(isset($prop_labels [self::getClassName()][$property_name])){ + if (isset($prop_labels[self::getClassName()][$property_name])) { $label = $prop_labels[self::getClassName()][$property_name]; - }elseif(isset($prop_labels ['@default'][$property_name])){ - $label = $prop_labels ['@default'][$property_name]; + } elseif (isset($prop_labels['@default'][$property_name])) { + $label = $prop_labels['@default'][$property_name]; } return $label; @@ -543,7 +572,8 @@ public static function _getConfiguredPropertyLabel($property_name){ private static $__permissions = []; - static function getPermissionsList() { + static function getPermissionsList() + { $class_name = self::getClassName(); if (!isset(self::$__permissions[$class_name])) { $permissions = ['@control']; @@ -559,7 +589,7 @@ static function getPermissionsList() { self::$__permissions[$class_name] = $permissions; } - + return self::$__permissions[$class_name]; } @@ -588,7 +618,8 @@ static function getPermissionsList() { * * @return array the metadata of this entity properties. */ - public static function getPropertiesMetadata($include_column_name = false){ + public static function getPropertiesMetadata($include_column_name = false) + { $app = App::i(); $__class = get_called_class(); @@ -601,7 +632,7 @@ public static function getPropertiesMetadata($include_column_name = false){ $validations = $__class::getValidations(); - foreach ($class_metadata as $key => $value){ + foreach ($class_metadata as $key => $value) { $metadata = [ 'isMetadata' => false, 'isEntityRelation' => false, @@ -613,23 +644,23 @@ public static function getPropertiesMetadata($include_column_name = false){ ]; $metadata['isPK'] = $value['id'] ?? false; - + if ($include_column_name && isset($value['columnName'])) { $metadata['columnName'] = $value['columnName']; } - if($key[0] == '_'){ + if ($key[0] == '_') { $prop = substr($key, 1); - if(method_exists($class, 'get' . $prop)){ - $metadata['@select'] = $prop; - }else{ + if (method_exists($class, 'get' . $prop)) { + $metadata['@select'] = $prop; + } else { continue; } } if ($key == 'status') { $options = [ - 'draft' => self::STATUS_DRAFT, + 'draft' => self::STATUS_DRAFT, 'enabled' => self::STATUS_ENABLED, ]; if ($class::usesSoftDelete()) { @@ -644,25 +675,25 @@ public static function getPropertiesMetadata($include_column_name = false){ $result[$key] = $metadata; } - foreach ($class_relations as $key => $value){ + foreach ($class_relations as $key => $value) { $result[$key] = [ 'isMetadata' => false, 'isEntityRelation' => true, - 'targetEntity' => str_replace('MapasCulturais\Entities\\','',$value['targetEntity']), + 'targetEntity' => str_replace('MapasCulturais\Entities\\', '', $value['targetEntity']), 'isOwningSide' => $value['isOwningSide'], 'label' => $class::_getConfiguredPropertyLabel($key) ]; } - if($class::usesMetadata()){ + if ($class::usesMetadata()) { $result = $result + $class::getMetadataMetadata(); } - - if($class::usesTypes()){ + + if ($class::usesTypes()) { $types = []; $types_order = []; - foreach($app->getRegisteredEntityTypes($class) as $type) { + foreach ($app->getRegisteredEntityTypes($class) as $type) { $types[$type->id] = $type->name; $types_order[] = $type->id; } @@ -675,8 +706,10 @@ public static function getPropertiesMetadata($include_column_name = false){ ] + $result['_type']; } - if(isset($result['location']) && isset($result['publicLocation'])){ - $result['location']['private'] = function(){ return (bool) ! $this->publicLocation; }; + if (isset($result['location']) && isset($result['publicLocation'])) { + $result['location']['private'] = function () { + return (bool) !$this->publicLocation; + }; } $kook_prefix = $class::getHookPrefix(); @@ -685,11 +718,13 @@ public static function getPropertiesMetadata($include_column_name = false){ return $result; } - static public function getValidations(){ + static public function getValidations() + { return []; } - public function isPropertyRequired($entity,$property) { + public function isPropertyRequired($entity, $property) + { $app = App::i(); $return = false; @@ -697,12 +732,12 @@ public function isPropertyRequired($entity,$property) { $class = $__class::getClassName(); $metadata = $class::getPropertiesMetadata(); - if(array_key_exists($property,$metadata) && array_key_exists('required',$metadata[$property])) { + if (array_key_exists($property, $metadata) && array_key_exists('required', $metadata[$property])) { $return = $metadata[$property]['required']; } $v = $class::getValidations(); - if(!$return && array_key_exists($property,$v) && array_key_exists('required',$v[$property])) { + if (!$return && array_key_exists($property, $v) && array_key_exists('required', $v[$property])) { $return = true; } @@ -714,31 +749,36 @@ public function isPropertyRequired($entity,$property) { * * @return \MapasCulturais\Entity */ - public function getEntity(){ + public function getEntity() + { $data = []; - foreach ($this as $key => $value){ - if($key[0] == '_') + foreach ($this as $key => $value) { + if ($key[0] == '_') continue; $data[$key] = $value; } return $data; } - public function getSingleUrl(){ + public function getSingleUrl() + { return App::i()->createUrl($this->controllerId, 'single', [$this->id]); } - public function getEditUrl(){ + public function getEditUrl() + { return App::i()->createUrl($this->controllerId, 'edit', [$this->id]); } - public function getDeleteUrl(){ + public function getDeleteUrl() + { return App::i()->createUrl($this->controllerId, 'delete', [$this->id]); } - static function getControllerClassName() { + static function getControllerClassName() + { $class = get_called_class(); - + return preg_replace('#\\\Entities\\\([^\\\]+)$#', '\\Controllers\\\$1', $class::getClassName()); } @@ -747,7 +787,8 @@ static function getControllerClassName() { * * @return \MapasCulturais\Controller The controller */ - public function getController(){ + public function getController() + { return App::i()->getControllerByEntity($this); } @@ -756,7 +797,8 @@ public function getController(){ * * @return string */ - public static function getControllerId() { + public static function getControllerId() + { $called_class = get_called_class(); $class = $called_class::getClassName(); return App::i()->getControllerIdByEntity($class) ?? ''; @@ -773,28 +815,33 @@ public static function getControllerId() { * * @return string */ - public static function getHookClassPath($class = null){ - if(!$class){ + public static function getHookClassPath($class = null) + { + if (!$class) { $called_class = get_called_class(); $class = $called_class::getClassName(); } - return preg_replace('#^MapasCulturais\.Entities\.#','',str_replace('\\','.',$class)); + return preg_replace('#^MapasCulturais\.Entities\.#', '', str_replace('\\', '.', $class)); } - public static function getHookPrefix($class = null) { + public static function getHookPrefix($class = null) + { $hook_class_path = self::getHookClassPath($class); return "entity({$hook_class_path})"; } - public function getEntityType(){ - return str_replace('MapasCulturais\Entities\\','',$this->getClassName()); + public function getEntityType() + { + return str_replace('MapasCulturais\Entities\\', '', $this->getClassName()); } - public static function getEntityTypeLabel($plural = false): string { + public static function getEntityTypeLabel($plural = false): string + { return ''; } - function getEntityState() { + function getEntityState() + { return App::i()->em->getUnitOfWork()->getEntityState($this); } @@ -803,7 +850,8 @@ function getEntityState() { * * @param boolean $flush Flushes to the Database */ - public function save($flush = false){ + public function save($flush = false) + { $app = App::i(); $requests = []; @@ -816,9 +864,9 @@ public function save($flush = false){ } catch (Exceptions\WorkflowRequestTransport $e) { $requests[] = $e->request; } - + if (method_exists($this, '_saveNested')) { - try { + try { $this->_saveNested(); } catch (Exceptions\WorkflowRequestTransport $e) { $requests[] = $e->request; @@ -827,64 +875,61 @@ public function save($flush = false){ if (method_exists($this, '_saveOwnerAgent')) { try { - $this->_saveOwnerAgent(); + $this->_saveOwnerAgent(); } catch (Exceptions\WorkflowRequestTransport $e) { $requests[] = $e->request; } } - try{ - if($this->isNew()){ + try { + if ($this->isNew()) { $this->checkPermission('create'); $is_new = true; - if($this->usesOriginSubsite() && $app->getCurrentSubsiteId()){ + if ($this->usesOriginSubsite() && $app->getCurrentSubsiteId()) { $subsite = $app->repo('Subsite')->find($app->getCurrentSubsiteId()); $this->setSubsite($subsite); } - - }else{ + } else { $this->checkPermission('modify'); $is_new = false; - } $app->applyHookBoundTo($this, "{$hook_prefix}.save:before"); $app->em->persist($this); $app->applyHookBoundTo($this, "{$hook_prefix}.save:after"); - if($flush){ + if ($flush) { $app->em->flush(); } - if($this->usesMetadata()){ + if ($this->usesMetadata()) { $this->saveMetadata(); - if($flush){ + if ($flush) { $app->em->flush(); } } - if($this->usesTaxonomies()){ + if ($this->usesTaxonomies()) { $this->saveTerms(); - if($flush){ + if ($flush) { $app->em->flush(); } } - if($this->usesRevision()) { - if($is_new){ + if ($this->usesRevision()) { + if ($is_new) { $this->_newCreatedRevision(); } else { $this->_newModifiedRevision(); } } - - }catch(Exceptions\PermissionDenied $e){ - if(!$requests) + } catch (Exceptions\PermissionDenied $e) { + if (!$requests) throw $e; } - - if($requests){ - foreach($requests as $request) + + if ($requests) { + foreach ($requests as $request) $request->save($flush); $e = new Exceptions\WorkflowRequest($requests); throw $e; @@ -897,7 +942,6 @@ public function save($flush = false){ } $app->applyHookBoundTo($this, "{$hook_prefix}.save:finish", [$flush]); - } /** @@ -905,28 +949,31 @@ public function save($flush = false){ * * @param boolean $flush Flushes to the database */ - public function delete($flush = false){ + public function delete($flush = false) + { $this->checkPermission('remove'); App::i()->em->remove($this); - if($flush) + if ($flush) App::i()->em->flush(); } - private function _isPropertySerializable($val, array $allowed_classes){ - if(is_array($val)){ + private function _isPropertySerializable($val, array $allowed_classes) + { + if (is_array($val)) { $nval = []; - foreach($val as $k => $v){ - try{ + foreach ($val as $k => $v) { + try { $nval[$k] = $this->_isPropertySerializable($v, $allowed_classes); - } catch (\Exception $e) {} + } catch (\Exception $e) { + } } $val = $nval; - }elseif(is_object($val) && !is_subclass_of($val, __CLASS__) && !in_array(\get_class($val), $allowed_classes)){ + } elseif (is_object($val) && !is_subclass_of($val, __CLASS__) && !in_array(\get_class($val), $allowed_classes)) { throw new \Exception(); - }elseif(is_object($val)){ + } elseif (is_object($val)) { - if(in_array($val, Entity::$_jsonSerializeNestedObjects)) + if (in_array($val, Entity::$_jsonSerializeNestedObjects)) throw new \Exception(); } @@ -938,9 +985,10 @@ private function _isPropertySerializable($val, array $allowed_classes){ * * @return array */ - public function jsonSerialize(): array { + public function jsonSerialize(): array + { $app = App::i(); - + $result = [ '@entityType' => $this->getControllerId() ]; @@ -953,23 +1001,24 @@ public function jsonSerialize(): array { Entity::$_jsonSerializeNestedObjects[$_uid] = $this; - foreach($this as $prop => $val){ - if($prop[0] == '_') + foreach ($this as $prop => $val) { + if ($prop[0] == '_') continue; - if($prop[0] == '_' && method_exists($this, 'get' . substr($prop, 1))) - $prop = substr ($prop, 1); + if ($prop[0] == '_' && method_exists($this, 'get' . substr($prop, 1))) + $prop = substr($prop, 1); - try{ + try { $val = $this->__get($prop); $val = $this->_isPropertySerializable($val, $allowed_classes); $result[$prop] = $val; - } catch (\Exception $e){} + } catch (\Exception $e) { + } } - if($this->usesMetadata()){ + if ($this->usesMetadata()) { $registered_metadata = $this->getRegisteredMetadata(); - foreach($registered_metadata as $def){ + foreach ($registered_metadata as $def) { $meta_key = $def->key; $meta_value = $this->$meta_key; if ($meta_value instanceof Entity) { @@ -988,17 +1037,17 @@ public function jsonSerialize(): array { $result['terms'] = $this->terms; } - if($controller_id = $this->getControllerId()){ + if ($controller_id = $this->getControllerId()) { $result['controllerId'] = $controller_id; $result['deleteUrl'] = $this->getDeleteUrl(); $result['editUrl'] = $this->getEditUrl(); $result['singleUrl'] = $this->getSingleUrl(); } - if($this->usesSealRelation()) { + if ($this->usesSealRelation()) { $result['lockedFields'] = $this->lockedFields; } - + unset(Entity::$_jsonSerializeNestedObjects[$_uid]); // adiciona as permissões do usuário sobre a entidade: @@ -1019,11 +1068,12 @@ public function jsonSerialize(): array { * * @return boolean */ - protected function validateUniquePropertyValue($property_name){ + protected function validateUniquePropertyValue($property_name) + { $class = get_called_class(); $dql = "SELECT COUNT(e.$property_name) FROM $class e WHERE e.$property_name = :val"; $params = ['val' => $this->$property_name]; - if($this->id){ + if ($this->id) { $dql .= ' AND e.id != :id'; $params['id'] = $this->id; } @@ -1052,27 +1102,28 @@ protected function validateUniquePropertyValue($property_name){ * * @return array */ - public function getValidationErrors(){ + public function getValidationErrors() + { $app = App::i(); $errors = $this->_validationErrors; $class = get_called_class(); - if(!method_exists($class, 'getValidations')) { + if (!method_exists($class, 'getValidations')) { return $errors; } $properties_validations = $class::getValidations(); - $tags = ['style','script','embed','object','iframe','img','link']; - $validation = implode(',', array_map(function($tag){ + $tags = ['style', 'script', 'embed', 'object', 'iframe', 'img', 'link']; + $validation = implode(',', array_map(function ($tag) { return "v::contains('<{$tag}')"; }, $tags)); $htmltags_validation = "v::noneOf($validation)"; - foreach($this->getPropertiesMetadata() as $prop => $metadata){ - if(!$metadata['isEntityRelation']){ - if(!isset($properties_validations[$prop])){ + foreach ($this->getPropertiesMetadata() as $prop => $metadata) { + if (!$metadata['isEntityRelation']) { + if (!isset($properties_validations[$prop])) { $properties_validations[$prop] = []; } @@ -1081,69 +1132,66 @@ public function getValidationErrors(){ } $hook_prefix = $this->getHookPrefix(); - + $app->applyHookBoundTo($this, "{$hook_prefix}.validations", [&$properties_validations]); - foreach($properties_validations as $property => $validations){ + foreach ($properties_validations as $property => $validations) { - if(!$this->$property && !key_exists('required', $validations)) + if (!$this->$property && !key_exists('required', $validations)) continue; - foreach($validations as $validation => $error_message){ + foreach ($validations as $validation => $error_message) { $validation = trim($validation); $ok = true; - if($validation == 'htmlentities'){ - if(!is_string($this->$property)){ + if ($validation == 'htmlentities') { + if (!is_string($this->$property)) { continue; } - + $validation = $htmltags_validation; } - if($validation == 'required'){ + if ($validation == 'required') { if (is_string($this->$property)) { $ok = trim($this->$property) !== ''; } else { $ok = (bool) $this->$property || $this->$property === 0; } - - }elseif($validation == 'unique'){ + } elseif ($validation == 'unique') { $ok = $this->validateUniquePropertyValue($property); - - }elseif(strpos($validation,'v::') === 0){ + } elseif (strpos($validation, 'v::') === 0) { $validation = str_replace('v::', 'Respect\Validation\Validator::', $validation); eval('$ok = ' . $validation . '->validate($this->' . $property . ');'); - }else{ + } else { $value = $this->$property; eval('$ok = ' . $validation . ';'); } - if(!$ok){ + if (!$ok) { if (!key_exists($property, $errors)) $errors[$property] = []; $errors[$property][] = $error_message; - } } } - if($this->usesTypes() && !$this->_type) + if ($this->usesTypes() && !$this->_type) $errors['type'] = [\MapasCulturais\i::__('O Tipo é obrigatório')]; - elseif($this->usesTypes() && !$this->validateType()) + elseif ($this->usesTypes() && !$this->validateType()) $errors['type'] = [\MapasCulturais\i::__('Tipo inválido')]; - if($this->usesMetadata()) + if ($this->usesMetadata()) $errors = $errors + $this->getMetadataValidationErrors(); - if($this->usesTaxonomies()) + if ($this->usesTaxonomies()) $errors = $errors + $this->getTaxonomiesValidationErrors(); $app->applyHookBoundTo($this, "{$this->hookPrefix}.validationErrors", [&$errors]); - + return $errors; } @@ -1153,7 +1201,8 @@ public function getValidationErrors(){ * * @return \Doctrine\ORM\EntityRepository */ - public function repo(){ + public function repo() + { return App::i()->repo($this->getClassName()); } @@ -1183,9 +1232,10 @@ public function computeChangeSets() * @hook **entity.save:before** * @hook **entity({$entity_class}).save:before** */ - public function prePersist($args = null){ + public function prePersist($args = null) + { $app = App::i(); - + $this->computeChangeSets(); $hook_prefix = $this->getHookPrefix(); @@ -1212,9 +1262,10 @@ public function prePersist($args = null){ * @hook **entity.save:after** * @hook **entity({$entity_class}).save:after** */ - public function postPersist($args = null){ + public function postPersist($args = null) + { $app = App::i(); - + $hook_prefix = $this->getHookPrefix(); $app->applyHookBoundTo($this, "{$hook_prefix}.insert:after"); @@ -1234,9 +1285,10 @@ public function postPersist($args = null){ * @hook **entity.remove:before** * @hook **entity({$entity_class}).remove:before** */ - public function preRemove($args = null){ + public function preRemove($args = null) + { $app = App::i(); - + $hook_prefix = $this->getHookPrefix(); $app->applyHookBoundTo($this, "{$hook_prefix}.remove:before"); @@ -1251,22 +1303,23 @@ public function preRemove($args = null){ * @hook **entity.remove:after** * @hook **entity({$entity_class}).remove:after** */ - public function postRemove($args = null){ + public function postRemove($args = null) + { $app = App::i(); - + $hook_prefix = $this->getHookPrefix(); $app->applyHookBoundTo($this, "{$hook_prefix}.remove:after"); - if($this->usesRevision()) { + if ($this->usesRevision()) { //$this->_newDeletedRevision(); } - if($this->usesPermissionCache()){ + if ($this->usesPermissionCache()) { $this->deletePermissionsCache(); } - if($this->usesRevision()) { + if ($this->usesRevision()) { //$this->_newDeletedRevision(); } } @@ -1282,14 +1335,15 @@ public function postRemove($args = null){ * @hook **entity.save:before** * @hook **entity({$entity_class}).save:before** */ - public function preUpdate($args = null){ + public function preUpdate($args = null) + { $app = App::i(); $this->computeChangeSets(); $hook_prefix = $this->getHookPrefix(); $app->applyHookBoundTo($this, "{$hook_prefix}.update:before"); $this->computeChangeSets(); - + if (property_exists($this, 'updateTimestamp')) { $this->updateTimestamp = new \DateTime; } @@ -1314,7 +1368,8 @@ public function preUpdate($args = null){ * * @ORM\PostUpdate */ - public function postUpdate($args = null){ + public function postUpdate($args = null) + { $app = App::i(); $hook_prefix = $this->getHookPrefix(); diff --git a/src/core/Theme.php b/src/core/Theme.php index 140898ad94..954eac4a55 100644 --- a/src/core/Theme.php +++ b/src/core/Theme.php @@ -1,4 +1,5 @@ _assetManager = $asset_manager; $app = App::i(); - + $this->documentMeta = new \ArrayObject; $this->bodyClasses = new \ArrayObject; $this->bodyProperties = new \ArrayObject; @@ -111,7 +114,7 @@ public function __construct(AssetManager $asset_manager) { $this->jsObject = new \ArrayObject; $this->jsObject['baseURL'] = $app->baseUrl; $this->jsObject['assetURL'] = $app->assetUrl; - $this->jsObject['maxUploadSize'] = $app->getMaxUploadSize($useSuffix=false); + $this->jsObject['maxUploadSize'] = $app->getMaxUploadSize($useSuffix = false); $this->jsObject['maxUploadSizeFormatted'] = $app->getMaxUploadSize(); $this->jsObject['EntitiesDescription'] = []; $this->jsObject['config'] = [ @@ -120,20 +123,20 @@ public function __construct(AssetManager $asset_manager) { 'currency' => $app->config['app.currency'] ]; $this->jsObject['routes'] = $app->config['routes']; - - $app->hook('app.init:after', function(){ + + $app->hook('app.init:after', function () { $this->view->jsObject['userId'] = $this->user->is('guest') ? null : $this->user->id; $this->view->jsObject['user'] = $this->user; }); - $app->hook('app.register', function() use($app){ + $app->hook('app.register', function () use ($app) { $def = new Definitions\Metadata('sentNotification', ['label' => 'Notificação enviada', 'type' => 'boolean']); $app->registerMetadata($def, 'MapasCulturais\Entities\Agent'); $app->registerMetadata($def, 'MapasCulturais\Entities\Space'); }); - - $app->hook('mapas.printJsObject:before', function () use($app) { + + $app->hook('mapas.printJsObject:before', function () use ($app) { if ($app->view->version >= 2) { $this->jsObject['request'] = [ 'controller' => $app->view->controller->id, @@ -143,7 +146,7 @@ public function __construct(AssetManager $asset_manager) { $this->jsObject['request']['id'] = $app->view->controller->data['id'] ?? null; } - + $this->jsObject['EntitiesDescription'] = [ "user" => Entities\User::getPropertiesMetadata(), "agent" => Entities\Agent::getPropertiesMetadata(), @@ -159,12 +162,12 @@ public function __construct(AssetManager $asset_manager) { ]; $taxonomies = []; - foreach($app->getRegisteredTaxonomies() as $slug => $definition) { + foreach ($app->getRegisteredTaxonomies() as $slug => $definition) { $taxonomy = $definition->jsonSerialize(); $taxonomy['terms'] = array_values($taxonomy['restrictedTerms']); unset($taxonomy['id'], $taxonomy['slug'], $taxonomy['restrictedTerms']); - + $taxonomies[$slug] = $taxonomy; } $this->jsObject['Taxonomies'] = $taxonomies; @@ -176,12 +179,12 @@ public function __construct(AssetManager $asset_manager) { $self = $this; $class = get_called_class(); - $app->hook('app.modules.init:after', function() use($class, $self){ + $app->hook('app.modules.init:after', function () use ($class, $self) { $reflaction = new \ReflectionClass($class); - - while($reflaction->getName() != __CLASS__){ + + while ($reflaction->getName() != __CLASS__) { $dir = dirname($reflaction->getFileName()); - if($dir != __DIR__) { + if ($dir != __DIR__) { $self->addPath($dir); } $reflaction = $reflaction->getParentClass(); @@ -189,19 +192,19 @@ public function __construct(AssetManager $asset_manager) { }, 100); - $app->hook('app.init:after', function () use($app) { - if(!$app->user->is('guest') && $app->user->profile->status < 1){ - if($app->view->version < 2) { - $app->hook('view.partial(nav-main-user).params', function($params, &$name){ + $app->hook('app.init:after', function () use ($app) { + if (!$app->user->is('guest') && $app->user->profile->status < 1) { + if ($app->view->version < 2) { + $app->hook('view.partial(nav-main-user).params', function ($params, &$name) { $name = 'header-profile-link'; }); } - + // redireciona o usuário para a edição do perfil se este não estiver publicado - $app->hook('GET(panel.<<*>>):before, GET(<<*>>.<>):before', function() use($app){ - if($entity = $this->requestedEntity) { + $app->hook('GET(panel.<<*>>):before, GET(<<*>>.<>):before', function () use ($app) { + if ($entity = $this->requestedEntity) { /** @var \MapasCulturais\Entity $entity */ - if(!$entity->equals($app->user->profile)){ + if (!$entity->equals($app->user->profile)) { $app->redirect($app->user->profile->editUrl); } } else { @@ -213,24 +216,25 @@ public function __construct(AssetManager $asset_manager) { $reflaction = new \ReflectionClass(get_class($this)); - - while($reflaction->getName() != __CLASS__){ + + while ($reflaction->getName() != __CLASS__) { $dir = dirname($reflaction->getFileName()); - if($dir != __DIR__) { + if ($dir != __DIR__) { i::addReplacements($dir . '/translations/replacements'); } $reflaction = $reflaction->getParentClass(); } } - function init(){ + function init() + { $app = App::i(); $app->applyHookBoundTo($this, 'theme.init:before'); $this->_init(); $app->applyHookBoundTo($this, 'theme.init:after'); } - + /** * Nome do último arquivo que teve o log de texto impresso. * * @var string @@ -274,15 +278,16 @@ function init(){ * * @return string */ - function text(string $name, string $default_localized_text) { + function text(string $name, string $default_localized_text) + { $app = App::i(); - $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,1); + $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1); $caller_filename = $bt[0]['file']; if ($conf = $app->_config['app.log.texts']) { $filename = str_replace(APPLICATION_PATH, '', $caller_filename); - if($filename != $this->__previousLoggedFilename) { + if ($filename != $this->__previousLoggedFilename) { $this->__previousLoggedFilename = $filename; $app->log->debug("text > \033[37m{$filename}\033[0m"); } @@ -294,7 +299,7 @@ function text(string $name, string $default_localized_text) { $action = $this->controller->action; // TEMPLATE PART - if(preg_match("#layouts/parts/(.*?)\.php$#", $caller_filename, $matches)){ + if (preg_match("#layouts/parts/(.*?)\.php$#", $caller_filename, $matches)) { $match = $matches[1]; $keys = [ "text:{$controller_id}.{$action}.part($match).title", @@ -303,8 +308,8 @@ function text(string $name, string $default_localized_text) { "text:part($match).title", ]; - // LAYOUT - }elseif(preg_match("#layouts/([^/]*?)\.php$#", $caller_filename, $matches)) { + // LAYOUT + } elseif (preg_match("#layouts/([^/]*?)\.php$#", $caller_filename, $matches)) { $match = $matches[1]; $keys = [ "text:{$controller_id}.{$action}.layout({$match}).{$name}", @@ -313,8 +318,8 @@ function text(string $name, string $default_localized_text) { "text:layout({$match}).{$name}", ]; - // VIEWS - }elseif(preg_match("#views/([^/]*?)\.php$#", $caller_filename, $matches)) { + // VIEWS + } elseif (preg_match("#views/([^/]*?)\.php$#", $caller_filename, $matches)) { $match = $matches[1]; $keys = [ "text:{$controller_id}.{$action}.view({$match}).{$name}", @@ -323,7 +328,7 @@ function text(string $name, string $default_localized_text) { "text:view({$match}).{$name}", ]; - // COMPONENTS + // COMPONENTS } else if (preg_match("#components/([^/]+)/[^/]+.php#", $caller_filename, $matches)) { $match = $matches[1]; $keys = [ @@ -334,14 +339,14 @@ function text(string $name, string $default_localized_text) { ]; } - foreach($keys as $key) { + foreach ($keys as $key) { if ($conf = $app->_config['app.log.texts']) { - if(is_bool($conf) || preg_match('#' . str_replace('*', '.*', $conf) . '#i', $key)){ + if (is_bool($conf) || preg_match('#' . str_replace('*', '.*', $conf) . '#i', $key)) { $app->log->debug("text >> \033[33m{$key}\033[0m"); } } - if($text = $app->_config[$key] ?? false) { + if ($text = $app->_config[$key] ?? false) { return $text; } } @@ -356,7 +361,8 @@ function text(string $name, string $default_localized_text) { * * @param bool $val */ - public function setPartial($val){ + public function setPartial($val) + { $this->_partial = $val; } @@ -364,7 +370,8 @@ public function setPartial($val){ * Sets the layout property. * @param string $name */ - public function setLayout($name){ + public function setLayout($name) + { $this->controller->layout = $name; } @@ -373,7 +380,8 @@ public function setLayout($name){ * * @param \MapasCulturais\Controller $controller the controller. */ - public function setController(\MapasCulturais\Controller $controller){ + public function setController(\MapasCulturais\Controller $controller) + { $this->controller = $controller; } @@ -387,7 +395,8 @@ public function setController(\MapasCulturais\Controller $controller){ * @param string $template the template name. * @return string The rendered template */ - public function render($template, array $data = []){ + public function render($template, array $data = []) + { $app = App::i(); $this->template = $template; @@ -395,9 +404,9 @@ public function render($template, array $data = []){ $this->data = $data; if ($this->_partial) { - $output = $this->partialRender ($template, $data); + $output = $this->partialRender($template, $data); } else { - $output = $this->fullRender ($template, $data); + $output = $this->fullRender($template, $data); } $app->response->getBody()->write($output); @@ -420,31 +429,32 @@ public function render($template, array $data = []){ * * @return string The rendered template. */ - public function fullRender($__template, $data = null){ + public function fullRender($__template, $data = null) + { $app = App::i(); $__template_filename = strtolower(substr($__template, -4)) === '.php' ? $__template : $__template . '.php'; $render_data = []; - foreach($data as $k => $val){ + foreach ($data as $k => $val) { $render_data[$k] = $val; $$k = $val; } $controller = $this->controller; - + $this->bodyClasses[] = "controller-{$controller->id}"; $this->bodyClasses[] = "action-{$controller->action}"; $this->bodyClasses[] = "layout-{$controller->layout}"; - - if(isset($entity)){ + + if (isset($entity)) { $this->bodyClasses[] = 'entity'; } // render the template $__templatePath = $this->resolveFilename('views', $__template_filename); - if(!$__templatePath){ + if (!$__templatePath) { throw new \Exception("Template $__template_filename not found"); } @@ -459,15 +469,15 @@ public function fullRender($__template, $data = null){ // render the layout with template $__layoutPath = $this->resolveFilename('layouts', $__layout_filename); - if(strtolower(substr($__layoutPath, -4)) !== '.php') - $__layoutPath .= '.php'; + if (strtolower(substr($__layoutPath, -4)) !== '.php') + $__layoutPath .= '.php'; - ob_start(function($output){ + ob_start(function ($output) { return $output; }); $app->applyHookBoundTo($this, 'view.renderLayout(' . $controller->layout . '):before', ['template' => $__template_name]); - + include $__layoutPath; $app->applyHookBoundTo($this, 'view.renderLayout(' . $controller->layout . '):after', ['template' => $__template_name]); @@ -496,54 +506,52 @@ public function fullRender($__template, $data = null){ * * @return string The rendered template. */ - public function partialRender($__template, $__data = [], $_is_part = false){ + public function partialRender($__template, $__data = [], $_is_part = false) + { $app = App::i(); - - if($__data instanceof \Slim\Helper\Set){ + + if ($__data instanceof \Slim\Helper\Set) { $_data = $__data; $__data = []; - - foreach($_data->keys() as $k){ + + foreach ($_data->keys() as $k) { $__data[$k] = $_data->get($k); } - } - + $app->applyHookBoundTo($this, 'view.partial(' . $__template . ').params', [&$__data, &$__template]); - if(strtolower(substr($__template, -4)) === '.php'){ + if (strtolower(substr($__template, -4)) === '.php') { $__template_filename = $__template; $__template = substr($__template, 0, -4); } else { $__template_filename = $__template . '.php'; } - - if(is_array($__data)){ + + if (is_array($__data)) { extract($__data); } // render the template - if($_is_part){ + if ($_is_part) { $__templatePath = $this->resolveFilename('layouts', 'parts/' . $__template_filename); - }else{ + } else { $__templatePath = $this->resolveFilename('views', $__template_filename); - } - - if(!$__templatePath){ - throw new \Exception("Template $__template_filename not found"); + if (!$__templatePath) { + throw new \Exception("Template $__template_filename not found"); } - $__template_name = substr(preg_replace('#^'.$this->templatesDirectory.'/?#', '', $__templatePath),0,-4); + $__template_name = substr(preg_replace('#^' . $this->templatesDirectory . '/?#', '', $__templatePath), 0, -4); $app->applyHookBoundTo($this, 'view.partial(' . $__template . '):before', ['template' => $__template]); - ob_start(function($output){ + ob_start(function ($output) { return $output; }); - + if ($app->mode == APPMODE_DEVELOPMENT) { $template_debug = str_replace(THEMES_PATH, '', $__template_name); $template_debug = str_replace(MODULES_PATH, 'modules/', $template_debug); @@ -552,7 +560,7 @@ public function partialRender($__template, $__data = [], $_is_part = false){ } include $__templatePath; - + if ($app->mode == APPMODE_DEVELOPMENT) { echo '"; } @@ -577,21 +585,23 @@ public function partialRender($__template, $__data = [], $_is_part = false){ * @param string $template * @param array $data Data to be passed to template part. */ - public function part($template, $data = []){ + public function part($template, $data = []) + { echo $this->partialRender($template, $data, true); } - function getTitle($entity = null){ + function getTitle($entity = null) + { $app = App::i(); $title = ''; - if($entity){ + if ($entity) { $title = $entity->name . ' - ' . $app->siteName; - }elseif($this->controller->id == 'site' && $this->controller->action === 'index'){ + } elseif ($this->controller->id == 'site' && $this->controller->action === 'index') { $title = $app->siteName; - }elseif($this->controller->id == 'panel' && $this->controller->action === 'index'){ + } elseif ($this->controller->id == 'panel' && $this->controller->action === 'index') { $title = $app->getReadableName('panel'); - }else{ - $title =$app->getReadableName($this->controller->action); + } else { + $title = $app->getReadableName($this->controller->action); } $app->applyHookBoundTo($this, 'mapasculturais.getTitle', [&$title]); @@ -599,8 +609,9 @@ function getTitle($entity = null){ return $title; } - function addPath($path){ - if(substr($path,-1) !== '/') $path .= '/'; + function addPath($path) + { + if (substr($path, -1) !== '/') $path .= '/'; $this->path[] = (string) $path; } @@ -609,28 +620,31 @@ function addPath($path){ * * @return \MapasCulturais\AssetManager */ - function getAssetManager(){ + function getAssetManager() + { return $this->_assetManager; } - function enqueueScript($group, $script_name, $script_filename, array $dependences = []){ + function enqueueScript($group, $script_name, $script_filename, array $dependences = []) + { $app = App::i(); - if($app->config['app.log.assets']){ + if ($app->config['app.log.assets']) { $dep = implode(', ', $dependences); $app->log->debug("enqueueScript ({$group}) {$script_name} : {$script_filename} ({$dep})"); } $this->_assetManager->enqueueScript($group, $script_name, $script_filename, $dependences); } - function enqueueStyle($group, $style_name, $style_filename, array $dependences = [], $media = 'all'){ + function enqueueStyle($group, $style_name, $style_filename, array $dependences = [], $media = 'all') + { $app = App::i(); - if($app->config['app.log.assets']){ + if ($app->config['app.log.assets']) { $dep = implode(', ', $dependences); $app->log->debug("enqueueScript ({$group}) {$style_name} : {$style_filename} ({$dep})"); } $this->_assetManager->enqueueStyle($group, $style_name, $style_filename, $dependences, $media); } - + /** * Add localization strings to a javascript object * @@ -647,26 +661,27 @@ function enqueueStyle($group, $style_name, $style_filename, array $dependences = * @param string $group All strings will be grouped in this property. Make this unique to avoid conflict with other scripts * @param array $vars Array with translated strgins with key beeing the variable name anda value beeing the translated string */ - public function localizeScript($group, $vars) { - + public function localizeScript($group, $vars) + { + if (!is_string($group) || empty($group)) throw new \Exception('localizeScript expects $group to be a string'); - + if (!is_array($vars)) throw new \Exception('localizeScript expects $vars to be an array'); - + if (!isset($this->jsObject['gettext'])) $this->jsObject['gettext'] = []; - - if ( isset($this->jsObject['gettext'][$group]) && is_array($this->jsObject['gettext'][$group]) ) { + + if (isset($this->jsObject['gettext'][$group]) && is_array($this->jsObject['gettext'][$group])) { $this->jsObject['gettext'][$group] = array_merge($vars, $this->jsObject['gettext'][$group]); } else { $this->jsObject['gettext'][$group] = $vars; } - } - function printJsObject (string $var_name = 'Mapas', bool $print_script_tag = true) { + function printJsObject(string $var_name = 'Mapas', bool $print_script_tag = true) + { $app = App::i(); $app->applyHookBoundTo($this, 'mapas.printJsObject:before'); @@ -687,19 +702,22 @@ function printJsObject (string $var_name = 'Mapas', bool $print_script_tag = tru $app->applyHookBoundTo($this, 'mapas.printJsObject:after'); } - function printScripts($group){ + function printScripts($group) + { $this->_assetManager->printScripts($group); } - function printStyles($group){ + function printStyles($group) + { $this->_assetManager->printStyles($group); } - function printDocumentMeta(){ - - foreach($this->documentMeta as $metacfg){ + function printDocumentMeta() + { + + foreach ($this->documentMeta as $metacfg) { $meta = "\n $val){ + foreach ($metacfg as $prop => $val) { $val = htmlentities((string) $val); $meta .= " {$prop}=\"{$val}\""; } @@ -708,13 +726,14 @@ function printDocumentMeta(){ } } - function resolveFilename($folder, $file){ - if(!substr($folder, -1) !== '/') $folder .= '/'; + function resolveFilename($folder, $file) + { + if (!substr($folder, -1) !== '/') $folder .= '/'; $path = $this->path->getArrayCopy(); - foreach($path as $dir){ - if(file_exists($dir . $folder . $file)){ + foreach ($path as $dir) { + if (file_exists($dir . $folder . $file)) { return $dir . $folder . $file; } } @@ -722,49 +741,52 @@ function resolveFilename($folder, $file){ return null; } - function getAssetFilename($file){ + function getAssetFilename($file) + { $filename = $this->resolveFilename('assets', $file); - if(!$filename) throw new \Exception('Asset not found: ' . $file); + if (!$filename) throw new \Exception('Asset not found: ' . $file); return $filename; } - function asset($file, $print = true, $include_hash_in_filename = true){ + function asset($file, $print = true, $include_hash_in_filename = true) + { $app = App::i(); $app->applyHook('asset(' . $file . ')', [&$file]); $url = $this->getAssetManager()->assetUrl($file, $include_hash_in_filename); $app->applyHook('asset(' . $file . '):url', [&$url]); - if($print){ + if ($print) { echo $url; } return $url; } - function renderMarkdown($markdown){ + function renderMarkdown($markdown) + { $app = App::i(); $matches = []; - if(preg_match_all('#\{\{asset:([^\}]+)\}\}#', $markdown, $matches)){ - foreach($matches[0] as $i => $tag){ + if (preg_match_all('#\{\{asset:([^\}]+)\}\}#', $markdown, $matches)) { + foreach ($matches[0] as $i => $tag) { $markdown = str_replace($tag, $this->asset($matches[1][$i], false), $markdown); } } - if(method_exists($this, 'dict') && preg_match_all('#\{\{dict:([^\}]+)\}\}#', $markdown, $matches)){ - foreach($matches[0] as $i => $tag){ + if (method_exists($this, 'dict') && preg_match_all('#\{\{dict:([^\}]+)\}\}#', $markdown, $matches)) { + foreach ($matches[0] as $i => $tag) { $markdown = str_replace($tag, $this->dict(trim($matches[1][$i]), false), $markdown); } } - if(preg_match_all('#\{\{downloads:([^\}]+)\}\}#', $markdown, $matches)){ + if (preg_match_all('#\{\{downloads:([^\}]+)\}\}#', $markdown, $matches)) { $subsite = $app->getCurrentSubsite(); $files = $subsite->getFiles('downloads'); - if($subsite) { - foreach($matches[0] as $i => $tag){ - foreach($files as $file) { - if($file->description == $matches[1][$i]) { + if ($subsite) { + foreach ($matches[0] as $i => $tag) { + foreach ($files as $file) { + if ($file->description == $matches[1][$i]) { $markdown = str_replace($tag, $file->url, $markdown); break; } @@ -777,33 +799,38 @@ function renderMarkdown($markdown){ return \Michelf\MarkdownExtra::defaultTransform($markdown); } - function isEditable(){ - $result = $this->controller->action == 'edit' || $this->controller->action == 'create'|| $this->editable ?? false; + function isEditable() + { + $result = $this->controller->action == 'edit' || $this->controller->action == 'create' || $this->editable ?? false; App::i()->applyHookBoundTo($this, 'mapasculturais.isEditable', [&$result]); return $result; } - function isSearch(){ + function isSearch() + { return (bool) $this->controller->id === 'site' && $this->action === 'search'; } public $insideBody = false; - function bodyBegin(){ + function bodyBegin() + { $this->insideBody = true; App::i()->applyHook('mapasculturais.body:before'); - $this->applyTemplateHook('body','begin'); + $this->applyTemplateHook('body', 'begin'); } - function bodyEnd(){ - $this->applyTemplateHook('body','end'); + function bodyEnd() + { + $this->applyTemplateHook('body', 'end'); App::i()->applyHook('mapasculturais.body:after'); $this->insideBody = false; } - function bodyProperties(){ + function bodyProperties() + { $body_properties = []; foreach ($this->bodyProperties as $key => $val) @@ -814,20 +841,21 @@ function bodyProperties(){ echo implode(' ', $body_properties); } - function head(){ + function head() + { $app = App::i(); $app->applyHook('mapasculturais.head'); $this->printDocumentMeta(); - } - - function applyTemplateHook($name, $sufix = '', $args = []){ + + function applyTemplateHook($name, $sufix = '', $args = []) + { $app = App::i(); $hook = "template({$this->controller->id}.{$this->controller->action}.$name)"; - if($sufix){ + if ($sufix) { $hook .= ':' . $sufix; } @@ -836,7 +864,7 @@ function applyTemplateHook($name, $sufix = '', $args = []){ } $app->applyHookBoundTo($this, $hook, $args); } - + /** * Replace links in text with html links * @@ -846,45 +874,46 @@ function applyTemplateHook($name, $sufix = '', $args = []){ * @param bool $force By default will check for isEditable and only add links if not on edit mode. Set force to true to force replace * @return string */ - function autoLinkString($text, $force = false) { - + function autoLinkString($text, $force = false) + { + if ($this->isEditable() && true !== $force) return $text; - + return preg_replace('@(http)?(s)?(://)?(([-\w]+\.)+([^\s]+)+[^,.\s])@', '$1$2$3$4', $text); - - } + } - function addRequestedEntityToJs(string $entity_class_name = null, int $entity_id = null) { + function addRequestedEntityToJs(string $entity_class_name = null, int $entity_id = null) + { $entity_class_name = $entity_class_name ?: $this->controller->entityClassName ?? null; $entity_id = $entity_id ?: $this->controller->data['id'] ?? null; - + $_entity = $entity_class_name::getHookClassPath(); if ($entity_class_name && $entity_id) { $app = App::i(); $query_params = [ - '@select' => '*', - 'id' => "EQ({$entity_id})", - '@permissions'=>'view', + '@select' => '*', + 'id' => "EQ({$entity_id})", + '@permissions' => 'view', ]; - if($entity_class_name == EvaluationMethodConfiguration::class) { + if ($entity_class_name == EvaluationMethodConfiguration::class) { unset($query_params['@permissions']); } - if(property_exists ($entity_class_name, 'status')) { - $query_params['status'] = 'GTE(-10)'; + if (property_exists($entity_class_name, 'status')) { + $query_params['status'] = 'GTE(-10)'; } - if(property_exists ($entity_class_name, 'project')) { + if (property_exists($entity_class_name, 'project')) { $query_params['@select'] .= ',project.{name,type,files.avatar,terms,seals}'; } - if(property_exists ($entity_class_name, 'evaluationMethodConfiguration')) { + if (property_exists($entity_class_name, 'evaluationMethodConfiguration')) { $query_params['@select'] .= ',evaluationMethodConfiguration.*'; } - + if ($entity_class_name::usesAgentRelation()) { $query_params['@select'] .= ',agentRelations'; } @@ -904,7 +933,7 @@ function addRequestedEntityToJs(string $entity_class_name = null, int $entity_id $e = $query->findOne(); - if(property_exists ($entity_class_name, 'opportunity')) { + if (property_exists($entity_class_name, 'opportunity')) { $query = $app->em->createQuery(" SELECT o FROM MapasCulturais\\Entities\\Opportunity o @@ -913,11 +942,11 @@ function addRequestedEntityToJs(string $entity_class_name = null, int $entity_id $query->setParameter('id', $e['id']); $opportunity = $query->getSingleResult(); $e['opportunity'] = $opportunity->simplify('id,name,type,files,terms,seals'); - if($opportunity->parent){ + if ($opportunity->parent) { $e['opportunity']->parent = $opportunity->parent->simplify('id,name,type,files,terms,seals'); } } - + if ($entity_class_name == Entities\Agent::class) { $owner_prop = 'parent'; @@ -936,35 +965,35 @@ function addRequestedEntityToJs(string $entity_class_name = null, int $entity_id } else { $owner_prop = 'owner'; } - + if ($owner_id = $e[$owner_prop] ?? false) { $owner_query_params = [ - '@select' => 'name, terms, files.avatar, singleUrl, shortDescription', - 'id' => "EQ({$owner_id})", + '@select' => 'name, terms, files.avatar, singleUrl, shortDescription', + 'id' => "EQ({$owner_id})", 'status' => 'GTE(-10)', - '@permissions'=>'view', + '@permissions' => 'view', ]; - $app->applyHookBoundTo($this,"view.requestedEntity($_entity).owner.params", [&$owner_query_params, $entity_class_name, $entity_id]); + $app->applyHookBoundTo($this, "view.requestedEntity($_entity).owner.params", [&$owner_query_params, $entity_class_name, $entity_id]); $query = new ApiQuery(Entities\Agent::class, $owner_query_params); $query->__useDQLCache = false; $owner = $query->findOne(); $e[$owner_prop] = $owner; } - if($owner_prop != 'parent' && $entity_class_name::usesNested() && !empty($e['parent'])) { + if ($owner_prop != 'parent' && $entity_class_name::usesNested() && !empty($e['parent'])) { $parent_query_params = [ - '@select' => 'name, terms, files.avatar, singleUrl, shortDescription', - 'id' => "EQ({$e['parent']})", + '@select' => 'name, terms, files.avatar, singleUrl, shortDescription', + 'id' => "EQ({$e['parent']})", 'status' => 'GTE(-10)', - '@permissions'=>'view', + '@permissions' => 'view', ]; - $app->applyHookBoundTo($this,"view.requestedEntity($_entity).parent.params", [&$parent_query_params, $entity_class_name, $entity_id]); + $app->applyHookBoundTo($this, "view.requestedEntity($_entity).parent.params", [&$parent_query_params, $entity_class_name, $entity_id]); $query = new ApiQuery($entity_class_name, $parent_query_params); $query->__useDQLCache = false; $parent = $query->findOne(); $e['parent'] = $parent; } - + $e['controllerId'] = $app->getControllerIdByEntity($entity_class_name); // adiciona as permissões do usuário sobre a entidade: @@ -972,7 +1001,7 @@ function addRequestedEntityToJs(string $entity_class_name = null, int $entity_id $entity = $app->repo($entity_class_name)->find($entity_id); $permissions_list = $entity_class_name::getPermissionsList(); $permissions = []; - foreach($permissions_list as $action) { + foreach ($permissions_list as $action) { $permissions[$action] = $entity->canUser($action); } @@ -983,7 +1012,7 @@ function addRequestedEntityToJs(string $entity_class_name = null, int $entity_id $entity = $app->repo(Agent::class)->find($profile_id); $permissions_list = Agent::getPermissionsList(); $permissions = []; - foreach($permissions_list as $action) { + foreach ($permissions_list as $action) { $permissions[$action] = $entity->canUser($action); } @@ -991,9 +1020,8 @@ function addRequestedEntityToJs(string $entity_class_name = null, int $entity_id } $app->applyHookBoundTo($this, "view.requestedEntity($_entity).result", [&$e, $entity_class_name, $entity_id]); - + $this->jsObject['requestedEntity'] = $e; } } - } diff --git a/src/modules/Components/assets/js/components-base/API.js b/src/modules/Components/assets/js/components-base/API.js index 6d7d742b28..fd8e10ba75 100644 --- a/src/modules/Components/assets/js/components-base/API.js +++ b/src/modules/Components/assets/js/components-base/API.js @@ -1,379 +1,407 @@ globalThis.originalFetch = globalThis.fetch; globalThis.fetch = async (...args) => { - let [resource, config ] = args; + let [resource, config] = args; - const before = new CustomEvent('beforeFetch', { detail: {resource, config} }); - globalThis.dispatchEvent(before); + const before = new CustomEvent("beforeFetch", { + detail: { resource, config }, + }); + globalThis.dispatchEvent(before); - const response = await globalThis.originalFetch(resource, config); - - const after = new CustomEvent('afterFetch', { detail: response }); - globalThis.dispatchEvent(after); - - return response; + const response = await globalThis.originalFetch(resource, config); + + const after = new CustomEvent("afterFetch", { detail: response }); + globalThis.dispatchEvent(after); + + return response; }; -globalThis.useEntitiesCache = Pinia.defineStore('entitiesCache', { - state: () => { - return { - default: {} - } - }, - - actions: { - store(entity, scope) { - scope = scope || 'default'; - this[scope] = this[scope] || {}; - this[scope][entity.cacheId] = entity; - }, - - remove(entity, scope) { - scope = scope || 'default'; - this[scope] = this[scope] || {}; - delete this[scope][entity.cacheId]; - }, - - fetch(cacheId, scope) { - scope = scope || 'default'; - this[scope] = this[scope] || {}; - return this[scope][cacheId]; - } - } +globalThis.useEntitiesCache = Pinia.defineStore("entitiesCache", { + state: () => { + return { + default: {}, + }; + }, + + actions: { + store(entity, scope) { + scope = scope || "default"; + this[scope] = this[scope] || {}; + this[scope][entity.cacheId] = entity; + }, + + remove(entity, scope) { + scope = scope || "default"; + this[scope] = this[scope] || {}; + delete this[scope][entity.cacheId]; + }, + + fetch(cacheId, scope) { + scope = scope || "default"; + this[scope] = this[scope] || {}; + return this[scope][cacheId]; + }, + }, }); -globalThis.useEntitiesLists = Pinia.defineStore('entitiesLists', { - state: () => { - return { - default: {} - } - }, - - actions: { - store(name, list, scope) { - scope = scope || 'default'; - list.__name = name; - this[scope] = this[scope] || {}; - this[scope][name] = list; - }, - - remove(name, scope) { - scope = scope || 'default'; - this[scope] = this[scope] || {}; - delete this[scope][name]; - }, - - fetch(name, scope) { - scope = scope || 'default'; - this[scope] = this[scope] || {}; - return this[scope][name]; - }, - - fetchAll(scope) { - scope = scope || 'default'; - this[scope] = this[scope] || {}; - return this[scope]; - }, - - fetchEntityLists(entity, scope) { - scope = scope || 'default'; - this[scope] = this[scope] || {}; - this[scope].ENTITY_LISTS = this[scope].ENTITY_LISTS || {}; - - const objectId = entity.__objectId; - this[scope].ENTITY_LISTS[objectId] = this[scope].ENTITY_LISTS[objectId] || []; - return this[scope].ENTITY_LISTS[objectId]; - } - } +globalThis.useEntitiesLists = Pinia.defineStore("entitiesLists", { + state: () => { + return { + default: {}, + }; + }, + + actions: { + store(name, list, scope) { + scope = scope || "default"; + list.__name = name; + this[scope] = this[scope] || {}; + this[scope][name] = list; + }, + + remove(name, scope) { + scope = scope || "default"; + this[scope] = this[scope] || {}; + delete this[scope][name]; + }, + + fetch(name, scope) { + scope = scope || "default"; + this[scope] = this[scope] || {}; + return this[scope][name]; + }, + + fetchAll(scope) { + scope = scope || "default"; + this[scope] = this[scope] || {}; + return this[scope]; + }, + + fetchEntityLists(entity, scope) { + scope = scope || "default"; + this[scope] = this[scope] || {}; + this[scope].ENTITY_LISTS = this[scope].ENTITY_LISTS || {}; + + const objectId = entity.__objectId; + this[scope].ENTITY_LISTS[objectId] = + this[scope].ENTITY_LISTS[objectId] || []; + return this[scope].ENTITY_LISTS[objectId]; + }, + }, }); globalThis.apiInstances = {}; class API { - constructor(objectType, scope, fetchOptions) { - const instanceId = `${objectType}:${scope}`; - if (apiInstances[instanceId]) { - return apiInstances[instanceId]; - } else { - this.scope = scope; - this.cache = useEntitiesCache(); - this.lists = useEntitiesLists(); - this.objectType = objectType; - this.options = { - cacheMode: 'force-cache', - ...fetchOptions - }; - - apiInstances[instanceId] = this; - } - } - - get $PK() { - const __properties = this.getEntityDescription('!relations'); - let pk; - for (let prop in __properties) { - if(__properties[prop].isPK) { - pk = prop; - break; - } - } - - return pk || 'id'; - } - - getHeaders(data) { - if (data instanceof FormData) { - return {}; - } else { - return {'Content-Type': 'application/json'}; - } - } - - parseData(data) { - if (data instanceof FormData) { - return data; - } else { - return JSON.stringify(data); - } - } - - parseUrl(url) { - let _url = url.toString(); - if(_url.indexOf('/') === 0 || _url.indexOf('http://') === 0 || _url.indexOf('https://') === 0) { - return url; - } else { - return this.createUrl(url); - } - } - - async GET(url, data, init) { - url = this.parseUrl(url); - const requestInit = { - cache: this.options.cacheMode, - ...init - } - - return fetch(url, requestInit); - } - - async PUT(url, data) { - url = this.parseUrl(url); - return fetch(url, { - method: 'PUT', - headers: this.getHeaders(data), - body: this.parseData(data) - }).catch((e) => { - return new Response(null, {status: 0, statusText: 'erro inesperado'}); - }); - } - - async PATCH(url, data) { - url = this.parseUrl(url); - return fetch(url, { - method: 'PATCH', - headers: this.getHeaders(data), - body: this.parseData(data) - }).catch((e) => { - return new Response(null, {status: 0, statusText: 'erro inesperado'}); - }); - } - - async POST(url, data) { - url = this.parseUrl(url); - return fetch(url, { - method: 'POST', - headers: this.getHeaders(data), - body: this.parseData(data) - }).catch((e) => { - return new Response(null, {status: 0, statusText: 'erro inesperado'}); - }); - } - - async DELETE(url, data) { - url = this.parseUrl(url); - return fetch(url, { - method: 'DELETE', - headers: this.getHeaders(data), - body: this.parseData(data) - }).catch((e) => { - return new Response(null, {status: 0, statusText: 'erro inesperado'}); - }); - } - - async persistEntity(entity) { - if (!entity[this.$PK]) { - let url = Utils.createUrl(this.objectType, 'index'); - return this.POST(url, entity.data()) - - } else { - return this.PATCH(entity.singleUrl, entity.data(true)) - } - } - - async deleteEntity(entity) { - if (entity[this.$PK]) { - return this.DELETE(entity.singleUrl); - } - } - - async undeleteEntity(entity) { - if (entity[this.$PK]) { - return this.POST(entity.getUrl('undelete')); - } - } - - async destroyEntity(entity) { - if (entity[this.$PK]) { - return this.DELETE(entity.getUrl('destroy')); - } - } - - async publishEntity(entity) { - if (entity[this.$PK]) { - return this.POST(entity.getUrl('publish')); - } - } - - async archiveEntity(entity) { - if (entity[this.$PK]) { - return this.POST(entity.getUrl('archive')); - } - } - - async unpublishEntity(entity) { - if (entity[this.$PK]) { - return this.POST(entity.getUrl('unpublish')); - } - } - - async findOne(id, select) { - let url = this.createApiUrl('findOne', {id: `EQ(${id})`, '@select': select || '*'}); - return this.GET(url).then(response => response.json().then(obj => { - let entity = this.getEntityInstance(id); - return entity.populate(obj); - })); - } - - async find(query, list, rawProcessor) { - const raw = !!rawProcessor; - rawProcessor = (typeof rawProcessor == 'function') ? rawProcessor : undefined; - - return this.fetch('find', query, {list, raw, rawProcessor}); - } - - async fetch(endpoint, query, {list, raw, rawProcessor, refresh}) { - let url = this.createApiUrl(endpoint, query); - return this.GET(url).then(response => response.json().then(objs => { - let result; - if(raw) { - rawProcessor = rawProcessor || Utils.entityRawProcessor; - - result = objs.map(rawProcessor); - - if(list) { - objs.forEach(element => { - list.push(element); - }); - } - } else { - result = list || []; - - objs.forEach(element => { - const api = new API(element['@entityType'], this.scope); - const entity = api.getEntityInstance(element[api.$PK]); - entity.populate(element, !refresh); - result.push(entity); - entity.$LISTS.push(result); - }); - } - - result.metadata = JSON.parse(response.headers.get('API-Metadata')); - - return result; - })); - } - - createUrl(route, query) { - const url = Utils.createUrl(this.objectType, route, query); - return url; - } - - createApiUrl(route, query) { - const url = Utils.createUrl(this.objectType, `api/${route}`); - for (var key in query) { - url.searchParams.append(key, query[key]); - } - - return url; - } - - createCacheId(objectId) { - return this.objectType + ':' + objectId; - } - - getEntityInstance(objectId) { - const cacheId = this.createCacheId(objectId); - let entity = this.cache.fetch(cacheId, this.scope); - if (entity) { - return entity; - } else { - entity = new Entity(this.objectType, objectId, this.scope); - this.cache.store(entity, this.scope); - return entity; - } - } - - getEntityDescription(filter) { - const description = $DESCRIPTIONS[this.objectType]; - - let result = {}; - - function filteredBy(f) { - let filters = filter.split(','); - return filters.indexOf(f) > -1; - } - - for (var key in description) { - if(key.substr(0,2) === '__') { - continue; - } - - let desc = description[key]; - let ok = true - - if (filter) { - if (filteredBy('private') && desc.private !== true) { - ok = false; - } - - if (filteredBy('public') && desc.private) { - ok = false - } - - if (filteredBy('metadata') && !desc.isMetadata) { - ok = false - } else if(filteredBy('!metadata') && desc.isMetadata) { - ok = false - } - - if (filteredBy('relations') && !desc.isEntityRelation) { - ok = false - } else if(filteredBy('!relations') && desc.isEntityRelation) { - ok = false - } - } - - if (ok) { - key = desc['@select'] || key; - - if (desc.isEntityRelation && key[0] == '_'){ - key = key.substr(1); - } - - result[key] = desc; - } - } - - return result; - } -} \ No newline at end of file + constructor(objectType, scope, fetchOptions) { + const instanceId = `${objectType}:${scope}`; + if (apiInstances[instanceId]) { + return apiInstances[instanceId]; + } else { + this.scope = scope; + this.cache = useEntitiesCache(); + this.lists = useEntitiesLists(); + this.objectType = objectType; + this.options = { + cacheMode: "force-cache", + ...fetchOptions, + }; + + apiInstances[instanceId] = this; + } + } + + get $PK() { + const __properties = this.getEntityDescription("!relations"); + let pk; + for (let prop in __properties) { + if (__properties[prop].isPK) { + pk = prop; + break; + } + } + + return pk || "id"; + } + + getHeaders(data) { + if (data instanceof FormData) { + return {}; + } else { + return { "Content-Type": "application/json" }; + } + } + + parseData(data) { + if (data instanceof FormData) { + return data; + } else { + return JSON.stringify(data); + } + } + + parseUrl(url) { + let _url = url.toString(); + if ( + _url.indexOf("/") === 0 || + _url.indexOf("http://") === 0 || + _url.indexOf("https://") === 0 + ) { + return url; + } else { + return this.createUrl(url); + } + } + + async GET(url, data, init) { + url = this.parseUrl(url); + const requestInit = { + cache: this.options.cacheMode, + ...init, + }; + + return fetch(url, requestInit); + } + + async PUT(url, data) { + url = this.parseUrl(url); + return fetch(url, { + method: "PUT", + headers: this.getHeaders(data), + body: this.parseData(data), + }).catch((e) => { + return new Response(null, { + status: 0, + statusText: "erro inesperado", + }); + }); + } + + async PATCH(url, data) { + url = this.parseUrl(url); + return fetch(url, { + method: "PATCH", + headers: this.getHeaders(data), + body: this.parseData(data), + }).catch((e) => { + return new Response(null, { + status: 0, + statusText: "erro inesperado", + }); + }); + } + + async POST(url, data) { + url = this.parseUrl(url); + return fetch(url, { + method: "POST", + headers: this.getHeaders(data), + body: this.parseData(data), + }).catch((e) => { + return new Response(null, { + status: 0, + statusText: "erro inesperado", + }); + }); + } + + async DELETE(url, data) { + url = this.parseUrl(url); + return fetch(url, { + method: "DELETE", + headers: this.getHeaders(data), + body: this.parseData(data), + }).catch((e) => { + return new Response(null, { + status: 0, + statusText: "erro inesperado", + }); + }); + } + + async persistEntity(entity) { + if (!entity[this.$PK]) { + let url = Utils.createUrl(this.objectType, "index"); + return this.POST(url, entity.data()); + } else { + return this.PATCH(entity.singleUrl, entity.data(true)); + } + } + + async deleteEntity(entity) { + if (entity[this.$PK]) { + return this.DELETE(entity.singleUrl); + } + } + + async undeleteEntity(entity) { + if (entity[this.$PK]) { + return this.POST(entity.getUrl("undelete")); + } + } + + async destroyEntity(entity) { + if (entity[this.$PK]) { + return this.DELETE(entity.getUrl("destroy")); + } + } + + async publishEntity(entity) { + if (entity[this.$PK]) { + return this.POST(entity.getUrl("publish")); + } + } + + async archiveEntity(entity) { + if (entity[this.$PK]) { + return this.POST(entity.getUrl("archive")); + } + } + + async unpublishEntity(entity) { + if (entity[this.$PK]) { + return this.POST(entity.getUrl("unpublish")); + } + } + + async findOne(id, select) { + let url = this.createApiUrl("findOne", { + id: `EQ(${id})`, + "@select": select || "*", + }); + return this.GET(url).then((response) => + response.json().then((obj) => { + let entity = this.getEntityInstance(id); + return entity.populate(obj); + }) + ); + } + + async find(query, list, rawProcessor) { + const raw = !!rawProcessor; + rawProcessor = + typeof rawProcessor == "function" ? rawProcessor : undefined; + + return this.fetch("find", query, { list, raw, rawProcessor }); + } + + async fetch(endpoint, query, { list, raw, rawProcessor, refresh }) { + let url = this.createApiUrl(endpoint, query); + return this.GET(url).then((response) => + response.json().then((objs) => { + let result; + if (raw) { + rawProcessor = rawProcessor || Utils.entityRawProcessor; + + result = objs.map(rawProcessor); + + if (list) { + objs.forEach((element) => { + list.push(element); + }); + } + } else { + result = list || []; + + objs.forEach((element) => { + const api = new API(element["@entityType"], this.scope); + const entity = api.getEntityInstance(element[api.$PK]); + entity.populate(element, !refresh); + result.push(entity); + entity.$LISTS.push(result); + }); + } + + result.metadata = JSON.parse( + response.headers.get("API-Metadata") + ); + + return result; + }) + ); + } + + createUrl(route, query) { + const url = Utils.createUrl(this.objectType, route, query); + return url; + } + + createApiUrl(route, query) { + const url = Utils.createUrl(this.objectType, `api/${route}`); + for (var key in query) { + url.searchParams.append(key, query[key]); + } + + return url; + } + + createCacheId(objectId) { + return this.objectType + ":" + objectId; + } + + getEntityInstance(objectId) { + const cacheId = this.createCacheId(objectId); + let entity = this.cache.fetch(cacheId, this.scope); + if (entity) { + return entity; + } else { + entity = new Entity(this.objectType, objectId, this.scope); + this.cache.store(entity, this.scope); + return entity; + } + } + + getEntityDescription(filter) { + const description = $DESCRIPTIONS[this.objectType]; + + let result = {}; + + function filteredBy(f) { + let filters = filter.split(","); + return filters.indexOf(f) > -1; + } + + for (var key in description) { + if (key.substr(0, 2) === "__") { + continue; + } + + let desc = description[key]; + let ok = true; + + if (filter) { + if (filteredBy("private") && desc.private !== true) { + ok = false; + } + + if (filteredBy("public") && desc.private) { + ok = false; + } + + if (filteredBy("metadata") && !desc.isMetadata) { + ok = false; + } else if (filteredBy("!metadata") && desc.isMetadata) { + ok = false; + } + + if (filteredBy("relations") && !desc.isEntityRelation) { + ok = false; + } else if (filteredBy("!relations") && desc.isEntityRelation) { + ok = false; + } + } + + if (ok) { + key = desc["@select"] || key; + + if (desc.isEntityRelation && key[0] == "_") { + key = key.substr(1); + } + + result[key] = desc; + } + } + + return result; + } +} diff --git a/src/modules/Components/assets/js/components-base/Entity.js b/src/modules/Components/assets/js/components-base/Entity.js index 2b2282edbe..9dd99dff65 100644 --- a/src/modules/Components/assets/js/components-base/Entity.js +++ b/src/modules/Components/assets/js/components-base/Entity.js @@ -1,764 +1,829 @@ class Entity { - constructor(objectType, id, scope) { - this.__objectType = objectType; - this.id = id; - this.__scope = (scope || 'default'); - this.__validationErrors = {}; - - this.__messagesEnabled = true; - this.__processing = false; - - this.__originalValues = {}; - - // as traduções estão no arquivo texts.php do componente - this.text = Utils.getTexts('mc-entity'); - } - - populate(obj, preserveValues = true) { - const __properties = this.$PROPERTIES; - const __relations = this.$RELATIONS; - const defaultProperties = [ - 'terms', 'seals', , 'currentUserPermissions', - 'relatedAgents', 'agentRelations', - 'relatedSpaces', 'spaceRelations', - ]; - - this.populateId(obj); - - for (const prop of defaultProperties) { - if (this[prop] && !obj[prop]) { - continue; - } - let _default = prop == 'terms' ? [] : {}; - this[prop] = obj[prop] || _default; - } - - for (let prop in __properties) { - if(prop == this.$PK) { - continue; - } - let definition = __properties[prop]; - let val = obj[prop]; - - if(val === undefined && preserveValues) { - val = this[prop]; - } - - if ((definition.type == 'datetime' || definition.type == 'date' ) && val && !(val instanceof McDate)) { - if (typeof val == 'string') { - val = new McDate(val); - } else { - val = new McDate(val.date); - } - } - - if (prop == 'location' && val) { - if(val?.latitude && val?.longitude) { - val = {lat: parseFloat(val?.latitude), lng: parseFloat(val?.longitude)}; - } - val.lat = val.lat ?? 0; - val.lng = val.lng ?? 0; - } - - if(prop == 'type' && (typeof val == 'number')) { - val = { - id: val, - name: __properties['type']?.options?.[val] - }; - } - if(JSON.stringify(this[prop]) != JSON.stringify(val)) { - if(!preserveValues || this[prop] == undefined) { - this[prop] = val; - } - } - } - - for (let key in __relations) { - let prop = obj[key]; - let value; - if (prop instanceof Array) { - for (let i in prop) { - value = value || []; - value[i] = this.parseRelation(prop[i], key); - } - } else { - value = this.parseRelation(prop, key); - } - - if (value) { - this[key] = value; - } - } - - this.populateFiles(obj.files); - this.populateMetalists(obj.metalists); - - this.cleanErrors(); - - this.__originalValues = this.data(); - return this; - } - - parseRelation(prop, key) { - const type = prop?.['@entityType'] || this.$RELATIONS[key]?.targetEntity?.toLocaleLowerCase(); - const id = typeof prop == 'number' ? prop : prop?.id - - if (type && id) { - const propAPI = new API(type, this.__scope); - const instance = propAPI.getEntityInstance(id); - if(typeof prop != 'number') { - instance.populate(prop, true); - } - return instance; - } else { - return prop; - } - } - - populateId(obj) { - this.id = obj[this.$PK]; - } - - populateFiles(files) { - this.files = this.files || {}; - for (let groupName in files) { - const group = files[groupName]; - if (group instanceof Array) { - this.files[groupName] = this.files[groupName] || []; - this.files[groupName] = group.map((data) => new EntityFile(this, groupName, data)); - } else { - this.files[groupName] = new EntityFile(this, groupName, group); - } - } - } - - populateMetalists(metalists) { - this.metalists = this.metalists || {}; - for (let groupName in metalists) { - const group = metalists[groupName]; - this.metalists[groupName] = group.map((data) => new EntityMetalist(this, groupName, data)); - } - } - - cleanErrors() { - this.__validationErrors = {}; - } - - catchErrors(res, data) { - const message = data.data.message; - - if (res.status >= 500 && res.status <= 599) { - this.sendMessage(message || this.text('erro inesperado'), 'error'); - } else if(res.status == 400) { - if (data.error) { - this.__validationErrors = data.data; - this.sendMessage(message || this.text('erro de validacao'), 'error'); - } - } else if(res.status == 403) { - this.sendMessage(message || this.text('permissao negada'), 'error'); - } - } - - data(onlyModifiedFields) { - const skipProperties = ['id', 'createTimestamp', 'updateTimestamp', 'lastLoginTimestamp']; - const skipRelations = ['user', 'subsite']; - - const result = {}; - - const $PROPERTIES = this.$PROPERTIES; - - for (let prop in $PROPERTIES) { - if (skipProperties.indexOf(prop) > -1) { - continue; - } - - const definition = $PROPERTIES[prop]; - - let val = this[prop]; - - if(val instanceof McDate) { - if (definition.type == 'date') { - val = val.sql('date'); - } else if (definition.type == 'time') { - val = val.time(); - } else if (definition.type == 'datetime') { - val = val.sql('date') + ' ' + val.time(); - } - } - - if (val && (typeof val == 'object')) { - if (prop == 'type') { - val = val.id; - } else { - result[prop] = JSON.parse(JSON.stringify(val)); - } - } else { - result[prop] = val; - } - } - - for (let prop in this.$RELATIONS) { - const relation = this.$RELATIONS[prop]; - - if (skipRelations.indexOf(prop) > -1 || !relation.isOwningSide) { - continue; - } - - // para a criação de oportunidades - if(prop == 'ownerEntity' && this[prop]) { - result[prop] = this[prop]?.id; - result['objectType'] = this[prop]?.__objectType; - } else if(prop == 'parent' && this[prop]) { - if (this[prop]?.id != this.id) { - result[prop] = this[prop]?.id; - } - } else { - result[prop] = this[prop]?.id; - } - } - - if(this.terms) { - result.terms = JSON.parse(JSON.stringify(this.terms)); - } - - if(onlyModifiedFields) { - for(let key in result) { - if(JSON.stringify(result[key]) == JSON.stringify(this.__originalValues[key])){ - delete result[key]; - } - } - } - - for(let key in result) { - if(typeof result[key] == 'object' && !result[key] instanceof Entity) { - if(result[key] instanceof Array) { - result[key] = [...result[key]]; - } else { - result[key] = {...result[key]}; - } - } - } - return result; - - } - - get API () { - return new API(this.__objectType, this.__scope || 'default'); - } - - get $PROPERTIES() { - return this.API.getEntityDescription('!relations'); - } - - get $PK() { - const __properties = this.$PROPERTIES; - let pk; - for (let prop in __properties) { - if(__properties[prop].isPK) { - pk = prop; - break; - } - } - - return pk; - } - - get id() { - let pk = this.$PK; - if (pk == 'id') { - pk = '_id'; - } - return this[pk]; - } - - set id (value) { - let pk = this.$PK; - if (pk == 'id') { - pk = '_id'; - } - this[pk] = value; - } - - get __objectId() { - return `${this.__scope}-${this.__objectType}-${this.id}`; - } - - get $RELATIONS() { - const result = this.API.getEntityDescription('relations'); - if (this.__objectType == 'opportunity') { - result.ownerEntity = { - isEntityRelation: true, - isMetadata: false, - isOwningSide: true, - label: "", - targetEntity: null, - }; - } - return result; - } - - get $LISTS() { - const lists = useEntitiesLists(); - return lists.fetchEntityLists(this); - } - - get singleUrl() { - return this.getUrl('single'); - } - - get editUrl() { - return this.getUrl('edit'); - } - - get cacheId() { - return this.API.createCacheId(this.id); - } - - sendMessage(message, type) { - type = type || 'success'; - if(this.__messagesEnabled) { - const messages = useMessages(); - messages[type](message); - } - } - - disableMessages() { - this.__messagesEnabled = false; - } - - enableMessages() { - this.__messagesEnabled = true; - } - - getUrl(action, params) { - if (params) { - params = {0: this.id, ...params}; - } else { - params = [this.id]; - } - return this.API.createUrl(action, params); - } - - removeFromLists(skipList) { - skipList = skipList || []; - this.$LISTS.forEach((list) => { - if (skipList.indexOf(list.__name) >= 0) { - return; - } - let index = list.indexOf(this); - if (index >= 0){ - list.splice(index,1); - } - }); - } - - async doPromise(res, cb) { - let data = await res.json(); - let result; - - if (res.ok) { // status 20x - data = cb(data) || data; - this.cleanErrors(); - result = Promise.resolve(data); - } else { - this.catchErrors(res, data); - data.status = res.status; - result = Promise.reject(data); - } - - this.__processing = false; - return result; - } - - async POST(action, {callback, data}) { - const res = await this.API.POST(this.getUrl(action), data); - callback = callback || (() => {}); - - try { - return this.doPromise(res, callback); - } catch(error) { - return this.doCatch(error); - } - } - - async doCatch(error) { - this.__processing = false; - this.sendMessage(this.text('erro inesperado'), 'error'); - return Promise.reject({error: true, status:0, data: this.text('erro inesperado'), exception: error}); - } - - async save(delay = 300, preserveValues = true) { - this.__processing = this.text('salvando'); - if(!this.id) { - preserveValues = false; - } - - clearTimeout(this.__saveTimeout); - - this.resolvers = this.resolvers || []; - this.rejecters = this.rejecters || []; - - return new Promise((resolve, reject) => { - this.resolvers.push(resolve); - this.rejecters.push(reject); - - this.__saveTimeout = setTimeout(async () => { - try { - const data = this.data(true); - if(JSON.stringify(data) == '{}') { - const response = this.data(); - for(let resolve of this.resolvers) { - resolve(response); - } - - this.__processing = false; - return; - } - - const res = await this.API.persistEntity(this); - this.doPromise(res, (entity) => { - if (this.id) { - this.sendMessage(this.text('modificacoes salvas')); - } else { - this.sendMessage(this.text('entidade salva')); - } - this.populate(entity, preserveValues); - - }).then((response) => { - for(let resolve of this.resolvers) { - resolve(response); - } - }).catch((error) => { - for(let reject of this.rejecters) { - reject(error); - } - }) - - } catch (error) { - this.doCatch(error).then((response) => { - for(let reject of this.rejecters) { - reject(response); - } - }); - } - - }, delay); - }); - } - - async delete(removeFromLists) { - this.__processing = this.text('excluindo'); - - try { - const res = await this.API.deleteEntity(this); - return this.doPromise(res, (entity) => { - this.sendMessage(this.text('entidade removida')); - - if(removeFromLists) { - this.removeFromLists(); - } - - this.populate(entity); - }); - - } catch (error) { - return this.doCatch(error) - } - } - - async undelete(removeFromLists) { - this.__processing = this.text('recuperando'); - - try { - const res = await this.API.undeleteEntity(this); - return this.doPromise(res, (entity) => { - this.sendMessage(this.text('entidade recuperada')); - this.populate(entity); - if(removeFromLists) { - this.removeFromLists(); - } - }); - } catch (error) { - return this.doCatch(error); - } - } - - async destroy() { - this.__processing = this.text('excluindo definitivamente'); - - try { - const res = await this.API.destroyEntity(this); - return this.doPromise(res, () => { - this.sendMessage(this.text('entidade removida definitivamente')); - this.removeFromLists() - }); - } catch (error) { - return this.doCatch(error) - } - } - - async publish(removeFromLists) { - this.__processing = this.text('publicando'); - try { - // se há modificações não salvas, primeiro salva as alterações, só depois publica a entidade - if(Object.keys(this.data(true)).length > 0) { - await this.save(); - } - const res = await this.API.publishEntity(this); - return this.doPromise(res, (entity) => { - this.sendMessage(this.text('entidade publicada')); - this.populate(entity); - if(removeFromLists) { - this.removeFromLists(); - } - }); - } catch (error) { - return this.doCatch(error); - } - } - - async archive(removeFromLists) { - this.__processing = this.text('arquivando'); - - try { - const res = await this.API.archiveEntity(this); - return this.doPromise(res, (entity) => { - this.sendMessage(this.text('entidade arquivada')); - this.populate(entity); - if(removeFromLists) { - this.removeFromLists(); - } - }); - } catch (error) { - return this.doCatch(error); - } - } - - async unpublish(removeFromLists) { - this.__processing = this.text('tornando rascunho'); - - try { - const res = await this.API.unpublishEntity(this); - return this.doPromise(res, (entity) => { - this.sendMessage(this.text('entidade foi tornada rascunho')); - this.populate(entity); - if(removeFromLists) { - this.removeFromLists(); - } - }); - } catch (error) { - return this.doCatch(error); - } - } - - async upload(file, {group, description}) { - this.__processing = this.text('subindo arquivo'); - - const data = new FormData(); - data.append(group, file); - if (description) { - data.append(`description[${group}]`, description); - } - try{ - const res = await fetch(this.getUrl('upload'), {method: 'POST', body: data}); - return this.doPromise(res, (f) => { - let file; - if(f[group] instanceof Array) { - file = new EntityFile(this, group, f[group][0]); - this.files[group] = this.files[group] || []; - this.files[group].push(file); - } else { - file = new EntityFile(this, group, f[group]); - this.files[group] = file; - } - return file; - }); - } catch (error) { - this.doCatch(error); - } - } - - async createMetalist(group, {title, description, value} ) { - this.__processing = this.text('criando'); - try{ - const res = await this.API.POST(this.getUrl('metalist'), {group, title, description, value}); - - this.metalists[group] = this.metalists[group] || []; - - this.doPromise(res, (data) => { - const _data = { - id: data.id, - title, description, value - }; - const metalist = new EntityMetalist(this, group, _data); - this.metalists[group] = this.metalists[group] || []; - this.metalists[group].push(metalist); - }); - } catch (error) { - this.doCatch(error); - } - } - - async createAgentRelation(group, agent, hasControl, metadata) { - try{ - const res = await this.API.POST(this.getUrl('createAgentRelation'), {group, agentId: agent.id, has_control: hasControl}); - - this.doPromise(res, (agentRelation) => { - delete agentRelation.owner; - delete agentRelation.agentUserId; - delete agentRelation.objectId; - delete agentRelation.owner; - delete agentRelation.ownerUserId; - - agentRelation.agent = agent; - - this.agentRelations[group] = this.agentRelations[group] || []; - this.agentRelations[group].push(agentRelation); - - this.relatedAgents[group] = this.relatedAgents[group] || []; - this.relatedAgents[group].push(agent); - - }).then(response => { - const messages = useMessages(); - - if (response.status == -5) { - messages.success(this.text('solicitação enviada com sucesso')); - } - }); - } catch (error) { - this.doCatch(error); - } - } - - async addRelatedAgent(group, agent, metadata) { - this.__processing = this.text('adicionando agente relacionado'); - - return this.createAgentRelation(group, agent); - } - - async addAdmin(agent) { - this.__processing = this.text('adicionando administrador'); - - return this.createAgentRelation('group-admin', agent, true); - } - - async removeAgentRelation(group, agent) { - this.__processing = this.text('removendo agente relacionado'); - - try { - const res = await this.API.POST(this.getUrl('removeAgentRelation'), {group, agentId: agent.id}); - this.doPromise(res, (data) => { - let index; - - index = this.agentRelations[group].indexOf(agent); - this.agentRelations[group].splice(index,1); - - index = this.relatedAgents[group].indexOf(agent); - this.relatedAgents[group].splice(index,1); - - }); - } catch (error) { - return this.doCatch(error); - } - } - - - async removeAgentRelationGroup(group) { - this.__processing = this.text('removendo grupo de agentes relacionados'); - - try { - const res = await this.API.POST(this.getUrl('removeAgentRelationGroup'), {group}); - this.doPromise(res, (data) => { - delete this.agentRelations[group]; - delete this.relatedAgents[group]; - }); - } catch (error) { - return this.doCatch(error); - } - } - - - async renameAgentRelationGroup(oldName, newName) { - this.__processing = this.text('renomeando grupo de agentes relacionados'); - try { - const res = await this.API.POST(this.getUrl('renameAgentRelationGroup'), {oldName, newName}); - this.doPromise(res, (data) => { - this.agentRelations[newName] = this.agentRelations[oldName]; - this.relatedAgents[newName] = this.relatedAgents[oldName]; - delete this.agentRelations[oldName]; - delete this.relatedAgents[oldName]; - - }); - } catch (error) { - return this.doCatch(error); - } - } - - async createSealRelation(seal) - { - this.__processing = this.text('relacionando selo à entidade'); - - try{ - const res = await this.API.POST(this.getUrl('createSealRelation'), {sealId: seal.id}); - - this.doPromise(res, (r) => { - - const seal = { - sealId: r.seal.id, - sealRelationId: r.id, - singleUrl: Utils.createUrl('sealRelation', 'single', [r.id]), - name: r.seal.name, - createTimestamp: r.createTimestamp, - files: r.seal.files, - }; - - this.seals = this.seals || []; - this.seals.push(seal); - }); - } catch (error) { - this.doCatch(error); - } - } - - async removeSealRelation(seal) - { - this.__processing = this.text('removendo selo da entidade'); - - try { - const res = await this.API.POST(this.getUrl('removeSealRelation'), {sealId: seal.sealId}); - this.doPromise(res, (data) => { - let index; - - index = this.seals.indexOf(seal); - this.seals.splice(index,1); - }); - } catch (error) { - return this.doCatch(error); - } - } - - async changeOwner(ownerId) { - const global = useGlobalState(); - - this.__processing = this.text('alterando propriedade da entidade'); - ownerId = ownerId || global.auth.user?.profile?.id; - - if (!ownerId) { - return Promise.reject('ownerId indefinido'); - } - - try { - const res = await this.API.POST(this.getUrl('changeOwner'), {ownerId}); - this.doPromise(res, (data) => { - - console.log('NÃO IMPLEMENTADO', data); - - /* let index; + constructor(objectType, id, scope) { + this.__objectType = objectType; + this.id = id; + this.__scope = scope || "default"; + this.__validationErrors = {}; + + this.__messagesEnabled = true; + this.__processing = false; + + this.__originalValues = {}; + + // as traduções estão no arquivo texts.php do componente + this.text = Utils.getTexts("mc-entity"); + } + + populate(obj, preserveValues = true) { + const __properties = this.$PROPERTIES; + const __relations = this.$RELATIONS; + const defaultProperties = [ + "terms", + "seals", + , + "currentUserPermissions", + "relatedAgents", + "agentRelations", + "relatedSpaces", + "spaceRelations", + ]; + + this.populateId(obj); + + for (const prop of defaultProperties) { + if (this[prop] && !obj[prop]) { + continue; + } + let _default = prop == "terms" ? [] : {}; + this[prop] = obj[prop] || _default; + } + + for (let prop in __properties) { + if (prop == this.$PK) { + continue; + } + let definition = __properties[prop]; + let val = obj[prop]; + + if (val === undefined && preserveValues) { + val = this[prop]; + } + + if ( + (definition.type == "datetime" || definition.type == "date") && + val && + !(val instanceof McDate) + ) { + if (typeof val == "string") { + val = new McDate(val); + } else { + val = new McDate(val.date); + } + } + + if (prop == "location" && val) { + if (val?.latitude && val?.longitude) { + val = { + lat: parseFloat(val?.latitude), + lng: parseFloat(val?.longitude), + }; + } + val.lat = val.lat ?? 0; + val.lng = val.lng ?? 0; + } + + if (prop == "type" && typeof val == "number") { + val = { + id: val, + name: __properties["type"]?.options?.[val], + }; + } + if (JSON.stringify(this[prop]) != JSON.stringify(val)) { + if (!preserveValues || this[prop] == undefined) { + this[prop] = val; + } + } + } + + for (let key in __relations) { + let prop = obj[key]; + let value; + if (prop instanceof Array) { + for (let i in prop) { + value = value || []; + value[i] = this.parseRelation(prop[i], key); + } + } else { + value = this.parseRelation(prop, key); + } + + if (value) { + this[key] = value; + } + } + + this.populateFiles(obj.files); + this.populateMetalists(obj.metalists); + + this.cleanErrors(); + + this.__originalValues = this.data(); + + return this; + } + + parseRelation(prop, key) { + const type = + prop?.["@entityType"] || + this.$RELATIONS[key]?.targetEntity?.toLocaleLowerCase(); + const id = typeof prop == "number" ? prop : prop?.id; + + if (type && id) { + const propAPI = new API(type, this.__scope); + const instance = propAPI.getEntityInstance(id); + if (typeof prop != "number") { + instance.populate(prop, true); + } + return instance; + } else { + return prop; + } + } + + populateId(obj) { + this.id = obj[this.$PK]; + } + + populateFiles(files) { + this.files = this.files || {}; + for (let groupName in files) { + const group = files[groupName]; + if (group instanceof Array) { + this.files[groupName] = this.files[groupName] || []; + this.files[groupName] = group.map( + (data) => new EntityFile(this, groupName, data) + ); + } else { + this.files[groupName] = new EntityFile(this, groupName, group); + } + } + } + + populateMetalists(metalists) { + this.metalists = this.metalists || {}; + for (let groupName in metalists) { + const group = metalists[groupName]; + this.metalists[groupName] = group.map( + (data) => new EntityMetalist(this, groupName, data) + ); + } + } + + cleanErrors() { + this.__validationErrors = {}; + } + + catchErrors(res, data) { + const message = data.data.message; + + if (res.status >= 500 && res.status <= 599) { + this.sendMessage(message || this.text("erro inesperado"), "error"); + } else if (res.status == 400) { + if (data.error) { + this.__validationErrors = data.data; + this.sendMessage( + message || this.text("erro de validacao"), + "error" + ); + } + } else if (res.status == 403) { + this.sendMessage(message || this.text("permissao negada"), "error"); + } + } + + data(onlyModifiedFields) { + const skipProperties = [ + "id", + "createTimestamp", + "updateTimestamp", + "lastLoginTimestamp", + ]; + const skipRelations = ["user", "subsite"]; + + const result = {}; + + const $PROPERTIES = this.$PROPERTIES; + + for (let prop in $PROPERTIES) { + if (skipProperties.indexOf(prop) > -1) { + continue; + } + + const definition = $PROPERTIES[prop]; + + let val = this[prop]; + + if (val instanceof McDate) { + if (definition.type == "date") { + val = val.sql("date"); + } else if (definition.type == "time") { + val = val.time(); + } else if (definition.type == "datetime") { + val = val.sql("date") + " " + val.time(); + } + } + + if (val && typeof val == "object") { + if (prop == "type") { + val = val.id; + } else { + result[prop] = JSON.parse(JSON.stringify(val)); + } + } else { + result[prop] = val; + } + } + + for (let prop in this.$RELATIONS) { + const relation = this.$RELATIONS[prop]; + + if (skipRelations.indexOf(prop) > -1 || !relation.isOwningSide) { + continue; + } + + // para a criação de oportunidades + if (prop == "ownerEntity" && this[prop]) { + result[prop] = this[prop]?.id; + result["objectType"] = this[prop]?.__objectType; + } else if (prop == "parent" && this[prop]) { + if (this[prop]?.id != this.id) { + result[prop] = this[prop]?.id; + } + } else { + result[prop] = this[prop]?.id; + } + } + + if (this.terms) { + result.terms = JSON.parse(JSON.stringify(this.terms)); + } + + if (onlyModifiedFields) { + for (let key in result) { + if ( + JSON.stringify(result[key]) == + JSON.stringify(this.__originalValues[key]) + ) { + delete result[key]; + } + } + } + + for (let key in result) { + if ( + typeof result[key] == "object" && + !result[key] instanceof Entity + ) { + if (result[key] instanceof Array) { + result[key] = [...result[key]]; + } else { + result[key] = { ...result[key] }; + } + } + } + return result; + } + + get API() { + return new API(this.__objectType, this.__scope || "default"); + } + + get $PROPERTIES() { + return this.API.getEntityDescription("!relations"); + } + + get $PK() { + const __properties = this.$PROPERTIES; + let pk; + for (let prop in __properties) { + if (__properties[prop].isPK) { + pk = prop; + break; + } + } + + return pk; + } + + get id() { + let pk = this.$PK; + if (pk == "id") { + pk = "_id"; + } + return this[pk]; + } + + set id(value) { + let pk = this.$PK; + if (pk == "id") { + pk = "_id"; + } + this[pk] = value; + } + + get __objectId() { + return `${this.__scope}-${this.__objectType}-${this.id}`; + } + + get $RELATIONS() { + const result = this.API.getEntityDescription("relations"); + if (this.__objectType == "opportunity") { + result.ownerEntity = { + isEntityRelation: true, + isMetadata: false, + isOwningSide: true, + label: "", + targetEntity: null, + }; + } + return result; + } + + get $LISTS() { + const lists = useEntitiesLists(); + return lists.fetchEntityLists(this); + } + + get singleUrl() { + return this.getUrl("single"); + } + + get editUrl() { + return this.getUrl("edit"); + } + + get cacheId() { + return this.API.createCacheId(this.id); + } + + sendMessage(message, type) { + type = type || "success"; + if (this.__messagesEnabled) { + const messages = useMessages(); + messages[type](message); + } + } + + disableMessages() { + this.__messagesEnabled = false; + } + + enableMessages() { + this.__messagesEnabled = true; + } + + getUrl(action, params) { + if (params) { + params = { 0: this.id, ...params }; + } else { + params = [this.id]; + } + return this.API.createUrl(action, params); + } + + removeFromLists(skipList) { + skipList = skipList || []; + this.$LISTS.forEach((list) => { + if (skipList.indexOf(list.__name) >= 0) { + return; + } + let index = list.indexOf(this); + if (index >= 0) { + list.splice(index, 1); + } + }); + } + + async doPromise(res, cb) { + let data = await res.json(); + let result; + + if (res.ok) { + // status 20x + data = cb(data) || data; + this.cleanErrors(); + result = Promise.resolve(data); + } else { + this.catchErrors(res, data); + data.status = res.status; + result = Promise.reject(data); + } + + this.__processing = false; + return result; + } + + async POST(action, { callback, data }) { + const res = await this.API.POST(this.getUrl(action), data); + callback = callback || (() => {}); + + try { + return this.doPromise(res, callback); + } catch (error) { + return this.doCatch(error); + } + } + + async doCatch(error) { + this.__processing = false; + this.sendMessage(this.text("erro inesperado"), "error"); + return Promise.reject({ + error: true, + status: 0, + data: this.text("erro inesperado"), + exception: error, + }); + } + + async save(delay = 300, preserveValues = true) { + this.__processing = this.text("salvando"); + if (!this.id) { + preserveValues = false; + } + + clearTimeout(this.__saveTimeout); + + this.resolvers = this.resolvers || []; + this.rejecters = this.rejecters || []; + + return new Promise((resolve, reject) => { + this.resolvers.push(resolve); + this.rejecters.push(reject); + + this.__saveTimeout = setTimeout(async () => { + try { + const data = this.data(true); + if (JSON.stringify(data) == "{}") { + const response = this.data(); + for (let resolve of this.resolvers) { + resolve(response); + } + + this.__processing = false; + return; + } + + const res = await this.API.persistEntity(this); + this.doPromise(res, (entity) => { + if (this.id) { + this.sendMessage(this.text("modificacoes salvas")); + } else { + this.sendMessage(this.text("entidade salva")); + } + this.populate(entity, preserveValues); + }) + .then((response) => { + for (let resolve of this.resolvers) { + resolve(response); + } + }) + .catch((error) => { + for (let reject of this.rejecters) { + reject(error); + } + }); + } catch (error) { + this.doCatch(error).then((response) => { + for (let reject of this.rejecters) { + reject(response); + } + }); + } + }, delay); + }); + } + + async delete(removeFromLists) { + this.__processing = this.text("excluindo"); + + try { + const res = await this.API.deleteEntity(this); + return this.doPromise(res, (entity) => { + this.sendMessage(this.text("entidade removida")); + + if (removeFromLists) { + this.removeFromLists(); + } + + this.populate(entity); + }); + } catch (error) { + return this.doCatch(error); + } + } + + async undelete(removeFromLists) { + this.__processing = this.text("recuperando"); + + try { + const res = await this.API.undeleteEntity(this); + return this.doPromise(res, (entity) => { + this.sendMessage(this.text("entidade recuperada")); + this.populate(entity); + if (removeFromLists) { + this.removeFromLists(); + } + }); + } catch (error) { + return this.doCatch(error); + } + } + + async destroy() { + this.__processing = this.text("excluindo definitivamente"); + + try { + const res = await this.API.destroyEntity(this); + return this.doPromise(res, () => { + this.sendMessage( + this.text("entidade removida definitivamente") + ); + this.removeFromLists(); + }); + } catch (error) { + return this.doCatch(error); + } + } + + async publish(removeFromLists) { + this.__processing = this.text("publicando"); + try { + // se há modificações não salvas, primeiro salva as alterações, só depois publica a entidade + if (Object.keys(this.data(true)).length > 0) { + await this.save(); + } + const res = await this.API.publishEntity(this); + return this.doPromise(res, (entity) => { + this.sendMessage(this.text("entidade publicada")); + this.populate(entity); + if (removeFromLists) { + this.removeFromLists(); + } + }); + } catch (error) { + return this.doCatch(error); + } + } + + async archive(removeFromLists) { + this.__processing = this.text("arquivando"); + + try { + const res = await this.API.archiveEntity(this); + return this.doPromise(res, (entity) => { + this.sendMessage(this.text("entidade arquivada")); + this.populate(entity); + if (removeFromLists) { + this.removeFromLists(); + } + }); + } catch (error) { + return this.doCatch(error); + } + } + + async unpublish(removeFromLists) { + this.__processing = this.text("tornando rascunho"); + + try { + const res = await this.API.unpublishEntity(this); + return this.doPromise(res, (entity) => { + this.sendMessage(this.text("entidade foi tornada rascunho")); + this.populate(entity); + if (removeFromLists) { + this.removeFromLists(); + } + }); + } catch (error) { + return this.doCatch(error); + } + } + + async upload(file, { group, description }) { + this.__processing = this.text("subindo arquivo"); + + const data = new FormData(); + data.append(group, file); + if (description) { + data.append(`description[${group}]`, description); + } + try { + const res = await fetch(this.getUrl("upload"), { + method: "POST", + body: data, + }); + return this.doPromise(res, (f) => { + let file; + if (f[group] instanceof Array) { + file = new EntityFile(this, group, f[group][0]); + this.files[group] = this.files[group] || []; + this.files[group].push(file); + } else { + file = new EntityFile(this, group, f[group]); + this.files[group] = file; + } + return file; + }); + } catch (error) { + this.doCatch(error); + } + } + + async createMetalist(group, { title, description, value }) { + this.__processing = this.text("criando"); + try { + const res = await this.API.POST(this.getUrl("metalist"), { + group, + title, + description, + value, + }); + + this.metalists[group] = this.metalists[group] || []; + + this.doPromise(res, (data) => { + const _data = { + id: data.id, + title, + description, + value, + }; + const metalist = new EntityMetalist(this, group, _data); + this.metalists[group] = this.metalists[group] || []; + this.metalists[group].push(metalist); + }); + } catch (error) { + this.doCatch(error); + } + } + + async createAgentRelation(group, agent, hasControl, metadata) { + try { + const res = await this.API.POST( + this.getUrl("createAgentRelation"), + { group, agentId: agent.id, has_control: hasControl } + ); + + this.doPromise(res, (agentRelation) => { + delete agentRelation.owner; + delete agentRelation.agentUserId; + delete agentRelation.objectId; + delete agentRelation.owner; + delete agentRelation.ownerUserId; + + agentRelation.agent = agent; + + this.agentRelations[group] = this.agentRelations[group] || []; + this.agentRelations[group].push(agentRelation); + + this.relatedAgents[group] = this.relatedAgents[group] || []; + this.relatedAgents[group].push(agent); + }).then((response) => { + const messages = useMessages(); + + if (response.status == -5) { + messages.success( + this.text("solicitação enviada com sucesso") + ); + } + }); + } catch (error) { + this.doCatch(error); + } + } + + async addRelatedAgent(group, agent, metadata) { + this.__processing = this.text("adicionando agente relacionado"); + + return this.createAgentRelation(group, agent); + } + + async addAdmin(agent) { + this.__processing = this.text("adicionando administrador"); + + return this.createAgentRelation("group-admin", agent, true); + } + + async removeAgentRelation(group, agent) { + this.__processing = this.text("removendo agente relacionado"); + + try { + const res = await this.API.POST( + this.getUrl("removeAgentRelation"), + { group, agentId: agent.id } + ); + this.doPromise(res, (data) => { + let index; + + index = this.agentRelations[group].indexOf(agent); + this.agentRelations[group].splice(index, 1); + + index = this.relatedAgents[group].indexOf(agent); + this.relatedAgents[group].splice(index, 1); + }); + } catch (error) { + return this.doCatch(error); + } + } + + async removeAgentRelationGroup(group) { + this.__processing = this.text( + "removendo grupo de agentes relacionados" + ); + + try { + const res = await this.API.POST( + this.getUrl("removeAgentRelationGroup"), + { group } + ); + this.doPromise(res, (data) => { + delete this.agentRelations[group]; + delete this.relatedAgents[group]; + }); + } catch (error) { + return this.doCatch(error); + } + } + + async renameAgentRelationGroup(oldName, newName) { + this.__processing = this.text( + "renomeando grupo de agentes relacionados" + ); + try { + const res = await this.API.POST( + this.getUrl("renameAgentRelationGroup"), + { oldName, newName } + ); + this.doPromise(res, (data) => { + this.agentRelations[newName] = this.agentRelations[oldName]; + this.relatedAgents[newName] = this.relatedAgents[oldName]; + delete this.agentRelations[oldName]; + delete this.relatedAgents[oldName]; + }); + } catch (error) { + return this.doCatch(error); + } + } + + async createSealRelation(seal) { + this.__processing = this.text("relacionando selo à entidade"); + + try { + const res = await this.API.POST(this.getUrl("createSealRelation"), { + sealId: seal.id, + }); + + this.doPromise(res, (r) => { + const seal = { + sealId: r.seal.id, + sealRelationId: r.id, + singleUrl: Utils.createUrl("sealRelation", "single", [ + r.id, + ]), + name: r.seal.name, + createTimestamp: r.createTimestamp, + files: r.seal.files, + }; + + this.seals = this.seals || []; + this.seals.push(seal); + }); + } catch (error) { + this.doCatch(error); + } + } + + async removeSealRelation(seal) { + this.__processing = this.text("removendo selo da entidade"); + + try { + const res = await this.API.POST(this.getUrl("removeSealRelation"), { + sealId: seal.sealId, + }); + this.doPromise(res, (data) => { + let index; + + index = this.seals.indexOf(seal); + this.seals.splice(index, 1); + }); + } catch (error) { + return this.doCatch(error); + } + } + + async changeOwner(ownerId) { + const global = useGlobalState(); + + this.__processing = this.text("alterando propriedade da entidade"); + ownerId = ownerId || global.auth.user?.profile?.id; + + if (!ownerId) { + return Promise.reject("ownerId indefinido"); + } + + try { + const res = await this.API.POST(this.getUrl("changeOwner"), { + ownerId, + }); + this.doPromise(res, (data) => { + console.log("NÃO IMPLEMENTADO", data); + + /* let index; index = this.seals.indexOf(seal); this.seals.splice(index,1); */ - }); - } catch (error) { - return this.doCatch(error); - } - } -} \ No newline at end of file + }); + } catch (error) { + return this.doCatch(error); + } + } +} diff --git a/src/modules/Entities/components/create-space/script.js b/src/modules/Entities/components/create-space/script.js index 472e913b17..dc9f7bee26 100644 --- a/src/modules/Entities/components/create-space/script.js +++ b/src/modules/Entities/components/create-space/script.js @@ -1,102 +1,105 @@ -app.component('create-space', { - template: $TEMPLATES['create-space'], - emits: ['create'], +app.component("create-space", { + template: $TEMPLATES["create-space"], + emits: ["create"], - setup() { - // os textos estão localizados no arquivo texts.php deste componente - const text = Utils.getTexts('create-space') - return { text } - }, + setup() { + // os textos estão localizados no arquivo texts.php deste componente + const text = Utils.getTexts("create-space"); + return { text }; + }, - created() { - this.iterationFields(); - var stat = 'publish'; - }, + created() { + this.iterationFields(); + var stat = "publish"; + }, - data() { - return { - entity: null, - fields: [], - } - }, + data() { + return { + entity: null, + fields: [], + }; + }, - props: { - editable: { - type: Boolean, - default: true - }, - }, + props: { + editable: { + type: Boolean, + default: true, + }, + }, - computed: { - areaErrors() { - return this.entity.__validationErrors['term-area']; - }, - areaClasses() { - return this.areaErrors ? 'field error' : 'field'; - }, - modalTitle() { - if (this.entity?.id) { - if (this.entity.status == 1) { - return __('espaçoCriado', 'create-space'); - } else { - return __('criarRascunho', 'create-space'); - } - } else { - return __('criarEspaço', 'create-space'); + computed: { + areaErrors() { + return this.entity.__validationErrors["term-area"]; + }, + areaClasses() { + return this.areaErrors ? "field error" : "field"; + }, + modalTitle() { + if (this.entity?.id) { + if (this.entity.status == 1) { + return __("espaçoCriado", "create-space"); + } else { + return __("criarRascunho", "create-space"); + } + } else { + return __("criarEspaço", "create-space"); + } + }, + }, - } - }, - }, + methods: { + iterationFields() { + let skip = [ + "createTimestamp", + "id", + "location", + "name", + "shortDescription", + "status", + "type", + "_type", + "userId", + ]; + Object.keys($DESCRIPTIONS.space).forEach((item) => { + if ( + !skip.includes(item) && + $DESCRIPTIONS.space[item].required + ) { + this.fields.push(item); + } + }); + }, + createEntity() { + this.entity = Vue.ref(new Entity("space")); + this.entity.type = 1; + this.entity.terms = { area: [] }; + }, + createDraft(modal) { + this.entity.status = 0; + this.save(modal); + }, + createPublic(modal) { + //lançar dois eventos + this.entity.status = 1; + this.save(modal); + }, + save(modal) { + modal.loading(true); + this.entity + .save() + .then((response) => { + this.$emit("create", response); + modal.loading(false); + Utils.pushEntityToList(this.entity); + }) + .catch((e) => { + modal.loading(false); + }); + }, - methods: { - iterationFields() { - let skip = [ - 'createTimestamp', - 'id', - 'location', - 'name', - 'shortDescription', - 'status', - 'type', - '_type', - 'userId', - ]; - Object.keys($DESCRIPTIONS.space).forEach((item) => { - if (!skip.includes(item) && $DESCRIPTIONS.space[item].required) { - this.fields.push(item); - } - }) - }, - createEntity() { - this.entity = Vue.ref(new Entity('space')); - this.entity.type = 1; - this.entity.terms = { area: [] } - - }, - createDraft(modal) { - this.entity.status = 0; - this.save(modal); - }, - createPublic(modal) { - //lançar dois eventos - this.entity.status = 1; - this.save(modal); - }, - save(modal) { - modal.loading(true); - this.entity.save().then((response) => { - this.$emit('create', response) - modal.loading(false); - Utils.pushEntityToList(this.entity); - - }).catch((e) => { - modal.loading(false); - }); - }, - - destroyEntity() { - // para o conteúdo da modal não sumir antes dela fechar - setTimeout(() => this.entity = null, 200); - } - }, + destroyEntity() { + // para o conteúdo da modal não sumir antes dela fechar + setTimeout(() => (this.entity = null), 200); + }, + }, }); diff --git a/src/modules/Entities/components/create-space/template.php b/src/modules/Entities/components/create-space/template.php index 8e6d030a21..416e9439b7 100644 --- a/src/modules/Entities/components/create-space/template.php +++ b/src/modules/Entities/components/create-space/template.php @@ -1,4 +1,5 @@ - - + - + - + - + \ No newline at end of file diff --git a/src/modules/Entities/components/entity-field/script.js b/src/modules/Entities/components/entity-field/script.js index eea0210c7d..8d008a05e2 100644 --- a/src/modules/Entities/components/entity-field/script.js +++ b/src/modules/Entities/components/entity-field/script.js @@ -1,209 +1,258 @@ -app.component('entity-field', { - template: $TEMPLATES['entity-field'], - emits: ['change', 'save'], - - setup(props, { slots }) { - const hasSlot = name => !!slots[name] - return { hasSlot } - }, - - data() { - let uid = Math.random().toString(36).slice(2); - let description, - value = this.entity[this.prop]; - - description = this.entity.$PROPERTIES[this.prop] || {}; - - if (description.type == 'array' && !(value instanceof Array)) { - if (!value) { - value = []; - } else { - value = [value]; - } - } - - let isAdmin = function() { - let result = false; - $MAPAS.currentUserRoles.forEach(function(item){ - if(item.toLowerCase().match('admin')){ - result = true; - return; - } - }) - - return result; - } - - if(this.entity.__objectType === "agent" && this.prop === "type" && !isAdmin()){ - - var typeOptions = {}; - var optionsOrder = []; - Object.keys(description.options).forEach(function(item, index){ - if(description.options[item] != "Individual"){ - typeOptions[index] = description.options[item]; - optionsOrder.push(parseInt(index)); - } - }); - description.options = typeOptions; - description.optionsOrder = optionsOrder; - } - - let fieldType = this.type || description.field_type || description.type; - - if(this.type == 'textarea' || (description.type == 'text' && description.field_type === undefined)) { - fieldType = 'textarea'; - } - - return { - __timeout: null, - description: description, - propId: `${this.entity.__objectId}--${this.prop}--${uid}`, - fieldType, - currencyValue: this.entity[this.prop], - } - }, - - props: { - entity: { - type: Entity, - required: true - }, - prop: { - type: String, - required: true - }, - label: { - type: String, - default: null - }, - placeholder: { - type: String - }, - type: { - type: String, - default: null - }, - hideLabel: { - type: Boolean, - default: false - }, - hideDescription: { - type: Boolean, - default: false - }, - hideRequired: { - type: Boolean, - default: false - }, - debounce: { - type: Number, - default: 0 - }, - classes: { - type: [String, Array, Object], - required: false - }, - min: { - type: [ Number, String, Date ], - default: 0 || null - }, - max: { - type: [ Number, String, Date ], - default: 0 || null - }, - fieldDescription: { - type: String, - default: null - }, - autosave: { - type: Number, - }, - disabled: { - type: Boolean, - default: false - }, - mask: { - type: String, - default: null, - }, - }, - - computed: { - - charRemaining() { - return 400 - this.value.length; - }, - hasErrors() { - let errors = this.entity.__validationErrors[this.prop] || []; - if(errors.length > 0){ - return true; - } else { - return false; - } - }, - errors() { - return this.entity.__validationErrors[this.prop]; - }, - value() { - return this.entity[this.prop]?.id ?? this.entity[this.prop]; - } - }, - - methods: { - propExists(){ - return !! this.entity.$PROPERTIES[this.prop]; - }, - - change(event, now) { - clearTimeout(this.__timeout); - let oldValue = this.entity[this.prop] ? JSON.parse(JSON.stringify(this.entity[this.prop])) : null; - - this.__timeout = setTimeout(() => { - if(this.is('date') || this.is('datetime') || this.is('time')) { - if(event) { - this.entity[this.prop] = new McDate(event); - } else { - this.entity[this.prop] = ''; - } - - this.$emit('change', {entity: this.entity, prop: this.prop, oldValue: oldValue, newValue: event}); - } else if(this.is('currency')) { - this.entity[this.prop] = this.currencyValue; - this.$emit('change', {entity: this.entity, prop: this.prop, oldValue: oldValue, newValue: event.target.checked}); - } else if(this.is('checkbox')) { - this.entity[this.prop] = event.target.checked; - this.$emit('change', {entity: this.entity, prop: this.prop, oldValue: oldValue, newValue: event.target.checked}); - } else if (this.is('multiselect')) { - if (this.entity[this.prop] === '' || !this.entity[this.prop]) { - this.entity[this.prop] = [] - } else if (typeof this.entity[this.prop] !== 'object') { - this.entity[this.prop] = this.entity[this.prop].split(";"); - } - - let index = this.entity[this.prop].indexOf(event.target.value); - if (index >= 0) { - this.entity[this.prop].splice(index, 1); - } else { - this.entity[this.prop].push(event.target.value) - } - - this.$emit('change', {entity: this.entity, prop: this.prop, oldValue: oldValue, newValue: event.target.value}); - } else { - this.entity[this.prop] = event.target.value; - this.$emit('change', {entity: this.entity, prop: this.prop, oldValue: oldValue, newValue: event.target.value}); - } - - if (this.autosave && (now || JSON.stringify(this.entity[this.prop]) != JSON.stringify(oldValue))) { - this.entity.save(now ? 0 : this.autosave).then(() => { - this.$emit('save', this.entity); - }); - } - - }, now ? 0 : this.debounce); - }, - - is(type) { - return this.fieldType == type; - } - }, -}); \ No newline at end of file +app.component("entity-field", { + template: $TEMPLATES["entity-field"], + emits: ["change", "save"], + + setup(props, { slots }) { + const hasSlot = (name) => !!slots[name]; + return { hasSlot }; + }, + + data() { + let uid = Math.random().toString(36).slice(2); + let description, + value = this.entity[this.prop]; + + description = this.entity.$PROPERTIES[this.prop] || {}; + + if (description.type == "array" && !(value instanceof Array)) { + if (!value) { + value = []; + } else { + value = [value]; + } + } + + let isAdmin = function () { + let result = false; + $MAPAS.currentUserRoles.forEach(function (item) { + if (item.toLowerCase().match("admin")) { + result = true; + return; + } + }); + + return result; + }; + + if ( + this.entity.__objectType === "agent" && + this.prop === "type" && + !isAdmin() + ) { + var typeOptions = {}; + var optionsOrder = []; + Object.keys(description.options).forEach(function (item, index) { + if (description.options[item] != "Individual") { + typeOptions[index] = description.options[item]; + optionsOrder.push(parseInt(index)); + } + }); + description.options = typeOptions; + description.optionsOrder = optionsOrder; + } + + let fieldType = this.type || description.field_type || description.type; + + if ( + this.type == "textarea" || + (description.type == "text" && description.field_type === undefined) + ) { + fieldType = "textarea"; + } + + return { + __timeout: null, + description: description, + propId: `${this.entity.__objectId}--${this.prop}--${uid}`, + fieldType, + currencyValue: this.entity[this.prop], + }; + }, + + props: { + entity: { + type: Entity, + required: true, + }, + prop: { + type: String, + required: true, + }, + label: { + type: String, + default: null, + }, + placeholder: { + type: String, + }, + type: { + type: String, + default: null, + }, + hideLabel: { + type: Boolean, + default: false, + }, + hideDescription: { + type: Boolean, + default: false, + }, + hideRequired: { + type: Boolean, + default: false, + }, + debounce: { + type: Number, + default: 0, + }, + classes: { + type: [String, Array, Object], + required: false, + }, + min: { + type: [Number, String, Date], + default: 0 || null, + }, + max: { + type: [Number, String, Date], + default: 0 || null, + }, + fieldDescription: { + type: String, + default: null, + }, + autosave: { + type: Number, + }, + disabled: { + type: Boolean, + default: false, + }, + mask: { + type: String, + default: null, + }, + }, + + computed: { + charRemaining() { + return 400 - this.value.length; + }, + hasErrors() { + let errors = this.entity.__validationErrors[this.prop] || []; + if (errors.length > 0) { + return true; + } else { + return false; + } + }, + errors() { + return this.entity.__validationErrors[this.prop]; + }, + value() { + return this.entity[this.prop]?.id ?? this.entity[this.prop]; + }, + }, + + methods: { + propExists() { + return !!this.entity.$PROPERTIES[this.prop]; + }, + + change(event, now) { + clearTimeout(this.__timeout); + let oldValue = this.entity[this.prop] + ? JSON.parse(JSON.stringify(this.entity[this.prop])) + : null; + + this.__timeout = setTimeout( + () => { + if ( + this.is("date") || + this.is("datetime") || + this.is("time") + ) { + if (event) { + this.entity[this.prop] = new McDate(event); + } else { + this.entity[this.prop] = ""; + } + + this.$emit("change", { + entity: this.entity, + prop: this.prop, + oldValue: oldValue, + newValue: event, + }); + } else if (this.is("currency")) { + this.entity[this.prop] = this.currencyValue; + this.$emit("change", { + entity: this.entity, + prop: this.prop, + oldValue: oldValue, + newValue: event.target.checked, + }); + } else if (this.is("checkbox")) { + this.entity[this.prop] = event.target.checked; + this.$emit("change", { + entity: this.entity, + prop: this.prop, + oldValue: oldValue, + newValue: event.target.checked, + }); + } else if (this.is("multiselect")) { + if ( + this.entity[this.prop] === "" || + !this.entity[this.prop] + ) { + this.entity[this.prop] = []; + } else if (typeof this.entity[this.prop] !== "object") { + this.entity[this.prop] = + this.entity[this.prop].split(";"); + } + + let index = this.entity[this.prop].indexOf( + event.target.value + ); + if (index >= 0) { + this.entity[this.prop].splice(index, 1); + } else { + this.entity[this.prop].push(event.target.value); + } + + this.$emit("change", { + entity: this.entity, + prop: this.prop, + oldValue: oldValue, + newValue: event.target.value, + }); + } else { + this.entity[this.prop] = event.target.value; + this.$emit("change", { + entity: this.entity, + prop: this.prop, + oldValue: oldValue, + newValue: event.target.value, + }); + } + + if ( + this.autosave && + (now || + JSON.stringify(this.entity[this.prop]) != + JSON.stringify(oldValue)) + ) { + this.entity.save(now ? 0 : this.autosave).then(() => { + this.$emit("save", this.entity); + }); + } + }, + now ? 0 : this.debounce + ); + }, + + is(type) { + return this.fieldType == type; + }, + }, +}); diff --git a/src/modules/Entities/components/entity-field/template.php b/src/modules/Entities/components/entity-field/template.php index d7cdfaf9bc..af5051cb06 100644 --- a/src/modules/Entities/components/entity-field/template.php +++ b/src/modules/Entities/components/entity-field/template.php @@ -1,4 +1,5 @@ -{{label || description.label}} * - - + + @@ -28,11 +30,11 @@ -
+
-

+

{{ value ? value?.length : '0' }}/400 -

+

@@ -43,23 +45,23 @@ - + - + - + - + @@ -75,18 +77,18 @@ - + {{ fieldDescription || description.description}} - + {{errors.join('; ')}}
\ No newline at end of file From 5ba6efe62b1c69fe4f0283f4a0a23e20d882ed6d Mon Sep 17 00:00:00 2001 From: Leonardo Zanotti Date: Thu, 15 Aug 2024 19:08:16 -0300 Subject: [PATCH 2/2] =?UTF-8?q?FIX=20-=20removido=20altera=C3=A7=C3=B5es?= =?UTF-8?q?=20desnecess=C3=A1rias=20no=20c=C3=B3digo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/App.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/core/App.php b/src/core/App.php index a945563e9b..3c35556c5c 100644 --- a/src/core/App.php +++ b/src/core/App.php @@ -3324,9 +3324,13 @@ function getRegisteredEntityTypeByTypeName(Entity|string $entity, string $type_n * @return Definitions\EntityType[] */ function getRegisteredEntityTypes(Entity|string $entity): array { - if(is_object($entity)) + if (is_object($entity)) $entity = $entity->getClassName(); + usort($this->_register['entity_types'][$entity], function ($a, $b) { + return strcmp($a->name, $b->name); + }); + return $this->_register['entity_types'][$entity] ?? []; }