vendor/pimcore/pimcore/models/Translation/Dao.php line 116

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Model\Translation;
  15. use Pimcore\Db\Helper;
  16. use Pimcore\Logger;
  17. use Pimcore\Model;
  18. use Pimcore\Model\User;
  19. use Symfony\Component\Translation\Exception\NotFoundResourceException;
  20. /**
  21.  * @internal
  22.  *
  23.  * @property \Pimcore\Model\Translation $model
  24.  */
  25. class Dao extends Model\Dao\AbstractDao
  26. {
  27.     /**
  28.      * @var string
  29.      */
  30.     const TABLE_PREFIX 'translations_';
  31.     /**
  32.      * @return string
  33.      */
  34.     public function getDatabaseTableName(): string
  35.     {
  36.         return self::TABLE_PREFIX $this->model->getDomain();
  37.     }
  38.     /**
  39.      * @param string $key
  40.      * @param array|null $languages
  41.      *
  42.      * @throws NotFoundResourceException
  43.      * @throws \Doctrine\DBAL\Exception
  44.      */
  45.     public function getByKey($key$languages null)
  46.     {
  47.         if (is_array($languages)) {
  48.             $sql 'SELECT * FROM ' $this->getDatabaseTableName() . ' WHERE `key` = :key
  49.             AND `language` IN (:languages) ORDER BY `creationDate` ';
  50.         } else {
  51.             $sql ='SELECT * FROM ' $this->getDatabaseTableName() . ' WHERE `key` = :key ORDER BY `creationDate` ';
  52.         }
  53.         $data $this->db->fetchAllAssociative($sql,
  54.             ['key' => $key'languages' => $languages],
  55.             ['languages' => \Doctrine\DBAL\Connection::PARAM_STR_ARRAY]
  56.         );
  57.         if (!empty($data)) {
  58.             foreach ($data as $d) {
  59.                 $this->model->addTranslation($d['language'], $d['text']);
  60.                 $this->model->setKey($d['key']);
  61.                 $this->model->setCreationDate($d['creationDate']);
  62.                 $this->model->setModificationDate($d['modificationDate']);
  63.                 $this->model->setType($d['type']);
  64.                 $this->model->setUserOwner($d['userOwner']);
  65.                 $this->model->setUserModification($d['userModification']);
  66.             }
  67.         } else {
  68.             throw new NotFoundResourceException("Translation-Key -->'" $key "'<-- not found");
  69.         }
  70.     }
  71.     /**
  72.      * Save object to database
  73.      */
  74.     public function save()
  75.     {
  76.         //Create Domain table if doesn't exist
  77.         $this->createOrUpdateTable();
  78.         $this->updateModificationInfos();
  79.         $editableLanguages = [];
  80.         if ($this->model->getDomain() != Model\Translation::DOMAIN_ADMIN) {
  81.             if ($user User::getById($this->model->getUserModification())) {
  82.                 $editableLanguages $user->getAllowedLanguagesForEditingWebsiteTranslations();
  83.             }
  84.         }
  85.         if ($this->model->getKey() !== '') {
  86.             if (is_array($this->model->getTranslations())) {
  87.                 foreach ($this->model->getTranslations() as $language => $text) {
  88.                     if (count($editableLanguages) && !in_array($language$editableLanguages)) {
  89.                         Logger::warning(sprintf('User %s not allowed to edit %s translation'$user->getUsername(), $language)); // @phpstan-ignore-line
  90.                         continue;
  91.                     }
  92.                     $data = [
  93.                         'key' => $this->model->getKey(),
  94.                         'type' => $this->model->getType(),
  95.                         'language' => $language,
  96.                         'text' => $text,
  97.                         'modificationDate' => $this->model->getModificationDate(),
  98.                         'creationDate' => $this->model->getCreationDate(),
  99.                         'userOwner' => $this->model->getUserOwner(),
  100.                         'userModification' => $this->model->getUserModification(),
  101.                     ];
  102.                     Helper::insertOrUpdate($this->db$this->getDatabaseTableName(), $data);
  103.                 }
  104.             }
  105.         }
  106.     }
  107.     /**
  108.      * Deletes object from database
  109.      */
  110.     public function delete()
  111.     {
  112.         $this->db->delete($this->getDatabaseTableName(), [$this->db->quoteIdentifier('key') => $this->model->getKey()]);
  113.     }
  114.     /**
  115.      * Returns a array containing all available languages
  116.      *
  117.      * @return array
  118.      */
  119.     public function getAvailableLanguages()
  120.     {
  121.         $l $this->db->fetchAllAssociative('SELECT * FROM ' $this->getDatabaseTableName()  . '  GROUP BY `language`;');
  122.         $languages = [];
  123.         foreach ($l as $values) {
  124.             $languages[] = $values['language'];
  125.         }
  126.         return $languages;
  127.     }
  128.     /**
  129.      * Returns a array containing all available domains
  130.      *
  131.      * @return array
  132.      */
  133.     public function getAvailableDomains()
  134.     {
  135.         $domainTables $this->db->fetchAllAssociative("SHOW TABLES LIKE 'translations_%'");
  136.         $domains = [];
  137.         foreach ($domainTables as $domainTable) {
  138.             $domains[] = str_replace('translations_'''$domainTable[array_key_first($domainTable)]);
  139.         }
  140.         return $domains;
  141.     }
  142.     /**
  143.      * Returns boolean, if the domain table exists
  144.      *
  145.      * @param string $domain
  146.      *
  147.      * @return bool
  148.      */
  149.     public function isAValidDomain(string $domain): bool
  150.     {
  151.         try {
  152.             $this->db->fetchOne(sprintf('SELECT * FROM translations_%s LIMIT 1;'$domain));
  153.             return true;
  154.         } catch (\Exception $e) {
  155.             return false;
  156.         }
  157.     }
  158.     public function createOrUpdateTable()
  159.     {
  160.         $table $this->getDatabaseTableName();
  161.         if ($table == self::TABLE_PREFIX) {
  162.             throw new \Exception('Domain is missing to create new translation domain');
  163.         }
  164.         $this->db->executeQuery('CREATE TABLE IF NOT EXISTS `' $table "` (
  165.                           `key` varchar(190) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT '',
  166.                           `type` varchar(10) DEFAULT NULL,
  167.                           `language` varchar(10) NOT NULL DEFAULT '',
  168.                           `text` text DEFAULT NULL,
  169.                           `creationDate` int(11) unsigned DEFAULT NULL,
  170.                           `modificationDate` int(11) unsigned DEFAULT NULL,
  171.                           `userOwner` int(11) unsigned DEFAULT NULL,
  172.                           `userModification` int(11) unsigned DEFAULT NULL,
  173.                           PRIMARY KEY (`key`,`language`),
  174.                           KEY `language` (`language`)
  175.                         ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");
  176.     }
  177.     protected function updateModificationInfos(): void
  178.     {
  179.         $updateTime time();
  180.         $this->model->setModificationDate($updateTime);
  181.         if (!$this->model->getCreationDate()) {
  182.             $this->model->setCreationDate($updateTime);
  183.         }
  184.         // auto assign user if possible, if no user present, use ID=0 which represents the "system" user
  185.         $userId \Pimcore\Tool\Admin::getCurrentUser()?->getId() ?? 0;
  186.         $this->model->setUserModification($userId);
  187.         if ($this->model->getUserOwner() === null) {
  188.             $this->model->setUserOwner($userId);
  189.         }
  190.     }
  191. }