vendor/pimcore/pimcore/models/Document/Service/Dao.php line 86

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\Document\Service;
  15. use Pimcore\Db\Helper;
  16. use Pimcore\Model;
  17. use Pimcore\Model\Document;
  18. use Pimcore\Model\Site;
  19. /**
  20.  * @internal
  21.  *
  22.  * @property \Pimcore\Model\Document\Service $model
  23.  */
  24. class Dao extends Model\Dao\AbstractDao
  25. {
  26.     /**
  27.      * @param Site $site
  28.      * @param string $path
  29.      *
  30.      * @return int
  31.      */
  32.     public function getDocumentIdByPrettyUrlInSite(Site $site$path)
  33.     {
  34.         return (int) $this->db->fetchOne(
  35.             'SELECT documents.id FROM documents
  36.             LEFT JOIN documents_page ON documents.id = documents_page.id
  37.             WHERE documents.path LIKE ? AND documents_page.prettyUrl = ?',
  38.             [Helper::escapeLike($site->getRootPath()) . '/%'rtrim($path'/')]
  39.         );
  40.     }
  41.     /**
  42.      * @param Document $document
  43.      *
  44.      * @return int
  45.      */
  46.     public function getTranslationSourceId(Document $document)
  47.     {
  48.         $sourceId $this->db->fetchOne('SELECT sourceId FROM documents_translations WHERE id = ?', [$document->getId()]);
  49.         if (!$sourceId) {
  50.             $sourceId $document->getId();
  51.         }
  52.         return $sourceId;
  53.     }
  54.     /**
  55.      * @param Document $document
  56.      * @param string $task
  57.      *
  58.      * @return int[]
  59.      */
  60.     public function getTranslations(Document $document$task 'open')
  61.     {
  62.         $sourceId $this->getTranslationSourceId($document);
  63.         $data $this->db->fetchAllAssociative('SELECT id,language FROM documents_translations WHERE sourceId IN(?, ?) UNION SELECT sourceId as id,"source" FROM documents_translations WHERE id = ?', [$sourceId$document->getId(), $document->getId()]);
  64.         if ($task == 'open') {
  65.             $linkedData = [];
  66.             foreach ($data as $value) {
  67.                 $linkedData $this->db->fetchAllAssociative('SELECT id,language FROM documents_translations WHERE sourceId = ? UNION SELECT sourceId as id,"source" FROM documents_translations WHERE id = ?', [$value['id'], $value['id']]);
  68.             }
  69.             if (count($linkedData) > 0) {
  70.                 $data array_merge($data$linkedData);
  71.             }
  72.         }
  73.         $translations = [];
  74.         foreach ($data as $translation) {
  75.             if ($translation['language'] == 'source') {
  76.                 $sourceDocument Document::getById((int) $translation['id']);
  77.                 $translations[$sourceDocument->getProperty('language')] = $sourceDocument->getId();
  78.             } else {
  79.                 $translations[$translation['language']] = (int) $translation['id'];
  80.             }
  81.         }
  82.         // add language from source document
  83.         if (!empty($translations)) {
  84.             $sourceDocument Document::getById($sourceId);
  85.             $translations[$sourceDocument->getProperty('language')] = $sourceDocument->getId();
  86.         }
  87.         return $translations;
  88.     }
  89.     /**
  90.      * @param Document $document
  91.      * @param Document $translation
  92.      * @param string|null $language
  93.      */
  94.     public function addTranslation(Document $documentDocument $translation$language null)
  95.     {
  96.         $sourceId $this->getTranslationSourceId($document);
  97.         if (!$language) {
  98.             $language $translation->getProperty('language');
  99.         }
  100.         Helper::insertOrUpdate($this->db'documents_translations', [
  101.             'id' => $translation->getId(),
  102.             'sourceId' => $sourceId,
  103.             'language' => $language,
  104.         ]);
  105.     }
  106.     /**
  107.      * @param Document $document
  108.      */
  109.     public function removeTranslation(Document $document)
  110.     {
  111.         // if $document is a source-document, we need to move them over to a new document
  112.         $newSourceId $this->db->fetchOne('SELECT id FROM documents_translations WHERE sourceId = ?', [$document->getId()]);
  113.         if ($newSourceId) {
  114.             $this->db->update('documents_translations', ['sourceId' => $newSourceId], ['sourceId' => $document->getId()]);
  115.             $this->db->delete('documents_translations', ['id' => $newSourceId]);
  116.         }
  117.     }
  118.     /**
  119.      * @param Document $document
  120.      * @param Document $targetDocument
  121.      */
  122.     public function removeTranslationLink(Document $documentDocument $targetDocument)
  123.     {
  124.         $sourceId $this->getTranslationSourceId($document);
  125.         if ($targetDocument->getId() == $sourceId) {
  126.             $sourceId $document->getId();
  127.         }
  128.         $newSourceId $this->db->fetchOne('SELECT id FROM documents_translations WHERE id = ? AND sourceId = ?', [$targetDocument->getId(), $sourceId]);
  129.         if (empty($newSourceId)) {
  130.             $sourceId $document->getId();
  131.         }
  132.         // Remove in both way
  133.         $this->db->delete('documents_translations', ['id' => $targetDocument->getId(), 'sourceId' => $sourceId]);
  134.         $this->db->delete('documents_translations', ['id' => $sourceId'sourceId' => $targetDocument->getId()]);
  135.     }
  136. }