. */ namespace SP\Services\Upgrade; use Exception; use SP\Core\Acl\ActionsInterface; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\Services\CustomField\CustomFieldService; use SP\Services\Service; use SP\Storage\Database\Database; use SP\Storage\Database\QueryData; /** * Class UpgradeCustomFieldData * * @package SP\Services\Upgrade */ final class UpgradeCustomFieldData extends Service { /** * @var Database */ private $db; /** * upgrade_300_18072902 * * @throws Exception */ public function upgrade_300_18072902() { $this->eventDispatcher->notifyEvent('upgrade.customField.start', new Event($this, EventMessage::factory() ->addDescription(__u('Custom fields update')) ->addDescription(__FUNCTION__)) ); try { $this->transactionAware(function () { $customFieldService = $this->dic->get(CustomFieldService::class); foreach ($customFieldService->getAll() as $item) { $queryData = new QueryData(); $queryData->setQuery('UPDATE CustomFieldData SET moduleId = ? WHERE id = ? LIMIT 1'); $queryData->setParams([$this->moduleMapper($item->getModuleId()), $item->getId()]); $this->db->doQuery($queryData); $this->eventDispatcher->notifyEvent('upgrade.customField.process', new Event($this, EventMessage::factory() ->addDescription(__u('Field updated'))) ); } }); } catch (Exception $e) { processException($e); $this->eventDispatcher->notifyEvent('exception', new Event($e)); throw $e; } $this->eventDispatcher->notifyEvent('upgrade.customField.end', new Event($this, EventMessage::factory() ->addDescription(__u('Custom fields update')) ->addDescription(__FUNCTION__)) ); } /** * @param int $moduleId * * @return int */ private function moduleMapper(int $moduleId) { switch ($moduleId) { case 10: return ActionsInterface::ACCOUNT; case 61: return ActionsInterface::CATEGORY; case 62: return ActionsInterface::CLIENT; case 71: return ActionsInterface::USER; case 72: return ActionsInterface::GROUP; } return $moduleId; } protected function initialize() { $this->db = $this->dic->get(Database::class); } }