container = $container; } protected function getContainer() { return $this->container; } protected function getEntityManager() { return $this->getContainer()->get('entityManager'); } protected function getConfig() { return $this->getContainer()->get('config'); } protected function getLanaguage() { return $this->getContainer()->get('language'); } public function getAll() { $notifications = []; //is cron configured if (!$this->isCronConfigured()) { $notifications[] = array( 'id' => 'cronNotConfigured', 'data' => array( 'id' => 'cronNotConfigured', 'message' => $this->getLanaguage()->translate('cronNotConfigured', 'messages', 'Admin'), ), ); } //is need upgrade instance $neededUpgrade = $this->getInstanceNeededUpgrade(); if (!empty($neededUpgrade)) { $message = $this->getLanaguage()->translate('upgradeInstance', 'messages', 'Admin'); $notifications[] = array( 'id' => 'upgradeInstance', 'data' => array( 'id' => 'upgradeInstance', 'message' => $this->prepareMessage($message, $neededUpgrade), ), ); } //are extensions needed upgrade $neededUpgrade = $this->getExtensionsNeededUpgrade(); if (!empty($neededUpgrade)) { foreach ($neededUpgrade as $extensionName => $extensionDetails) { $message = $this->getLanaguage()->translate('upgradeExtension', 'messages', 'Admin'); $notifications[] = array( 'id' => 'upgradeExtension' . Util::toCamelCase($extensionName, ' ', true), 'data' => array( 'id' => 'upgradeExtension', 'message' => $this->prepareMessage($message, $extensionDetails), ), ); } } return $notifications; } protected function isCronConfigured() { return $this->getContainer()->get('scheduledJob')->isCronConfigured(); } protected function getInstanceNeededUpgrade() { $config = $this->getConfig(); $latestVersion = $config->get('latestRelease'); if (isset($latestVersion)) { $currentVersion = $config->get('version'); if (version_compare($latestVersion, $currentVersion, '>')) { return array( 'currentVersion' => $currentVersion, 'latestVersion' => $latestVersion, ); } } } protected function getExtensionsNeededUpgrade() { $config = $this->getConfig(); $extensions = []; $latestExtensionReleases = $config->get('latestExtensionReleases'); if (!empty($latestExtensionReleases) && is_array($latestExtensionReleases)) { foreach ($latestExtensionReleases as $extensionName => $extensionLatestVersion) { $currentVersion = $this->getExtensionLatestVersion($extensionName); if (isset($currentVersion) && version_compare($extensionLatestVersion, $currentVersion, '>')) { $extensions[$extensionName] = array( 'currentVersion' => $currentVersion, 'latestVersion' => $extensionLatestVersion, 'extensionName' => $extensionName, ); } } } return $extensions; } protected function getExtensionLatestVersion($extensionName) { $pdo = $this->getEntityManager()->getPDO(); $query = " SELECT version FROM extension WHERE name='". $extensionName ."' AND deleted=0 AND is_installed = 1 ORDER BY created_at DESC "; $sth = $pdo->prepare($query); $sth->execute(); $row = $sth->fetch(\PDO::FETCH_ASSOC); if (isset($row['version'])) { return $row['version']; } } /** * Set EspoCRM latest release * * @param string $version */ public function setRelease($version) { $config = $this->getConfig(); $config->set('latestRelease', $version); $this->isConfigChanged = true; } /** * Set latest release of an extension * * @param string $extensionName * @param string $version */ public function setExtensionRelease($extensionName, $version) { $config = $this->getConfig(); $latestExtensionReleases = $config->get('latestExtensionReleases', []); $latestExtensionReleases[$extensionName] = $version; $config->set('latestExtensionReleases', $latestExtensionReleases); $this->isConfigChanged = true; } /** * Save releases after setRelease() and setExtensionRelease() * * @return boolean */ public function saveReleases() { if ($this->isConfigChanged) { $this->isConfigChanged = false; return $this->getConfig()->save(); } } /** * Create EspoCRM notification for a user * * @param string $message * @param string $userId * * @return void */ public function createNotification($message, $userId = '1') { $notification = $this->getEntityManager()->getEntity('Notification'); $notification->set(array( 'type' => 'message', 'data' => array( 'userId' => $this->getUser()->id, 'userName' => $this->getUser()->get('name') ), 'userId' => $user->id, 'message' => $actionData['messageTemplate'] )); $this->getEntityManager()->saveEntity($notification); } /** * Replance variable with values * * @param string $message * @param array $data * * @return string */ protected function prepareMessage($message, array $data = array()) { foreach ($data as $name => $value) { $message = str_replace('{'.$name.'}', $value, $message); } return $message; } }