vendor/pimcore/pimcore/models/DataObject/Listing/Concrete/Dao.php line 51

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\DataObject\Listing\Concrete;
  15. use Doctrine\DBAL\Query\QueryBuilder as DoctrineQueryBuilder;
  16. use Pimcore\Localization\LocaleServiceInterface;
  17. use Pimcore\Model;
  18. use Pimcore\Model\DataObject;
  19. use Pimcore\Tool;
  20. /**
  21.  * @internal
  22.  *
  23.  * @property \Pimcore\Model\DataObject\Listing\Concrete $model
  24.  */
  25. class Dao extends Model\DataObject\Listing\Dao
  26. {
  27.     /**
  28.      * @var bool
  29.      */
  30.     protected $firstException true;
  31.     /**
  32.      * @var string
  33.      */
  34.     private $tableName null;
  35.     /**
  36.      * @var int
  37.      */
  38.     protected $totalCount 0;
  39.     /**
  40.      * @return int[]
  41.      *
  42.      * @throws \Exception
  43.      */
  44.     public function loadIdList()
  45.     {
  46.         try {
  47.             return parent::loadIdList();
  48.         } catch (\Exception $e) {
  49.             return $this->exceptionHandler($e);
  50.         }
  51.     }
  52.     /**
  53.      * @param \Exception $e
  54.      *
  55.      * @return int[]
  56.      *
  57.      * @throws \Exception
  58.      */
  59.     protected function exceptionHandler($e)
  60.     {
  61.         // create view if it doesn't exist already // HACK
  62.         $pdoMySQL preg_match('/Base table or view not found/'$e->getMessage());
  63.         $Mysqli preg_match("/Table (.*) doesn't exist/"$e->getMessage());
  64.         if (($Mysqli || $pdoMySQL) && $this->firstException) {
  65.             $this->firstException false;
  66.             $localizedFields = new DataObject\Localizedfield();
  67.             $localizedFields->setClass(DataObject\ClassDefinition::getById($this->model->getClassId()));
  68.             $localizedFields->createUpdateTable();
  69.             return $this->loadIdList();
  70.         }
  71.         throw $e;
  72.     }
  73.     /**
  74.      * @return string
  75.      *
  76.      * @throws \Exception
  77.      */
  78.     public function getLocalizedBrickLanguage()
  79.     {
  80.         $language null;
  81.         // check for a localized field and if they should be used for this list
  82.         if ($this->model->getLocale()) {
  83.             if (Tool::isValidLanguage((string)$this->model->getLocale())) {
  84.                 $language = (string)$this->model->getLocale();
  85.             }
  86.         }
  87.         if (!$language) {
  88.             $locale \Pimcore::getContainer()->get(LocaleServiceInterface::class)->findLocale();
  89.             if (Tool::isValidLanguage((string)$locale)) {
  90.                 $language = (string)$locale;
  91.             }
  92.         }
  93.         if (!$language) {
  94.             $language Tool::getDefaultLanguage();
  95.         }
  96.         return $language;
  97.     }
  98.     /**
  99.      * @return string
  100.      *
  101.      * @throws \Exception
  102.      */
  103.     public function getTableName()
  104.     {
  105.         if (empty($this->tableName)) {
  106.             // default
  107.             $this->tableName 'object_' $this->model->getClassId();
  108.             if (!$this->model->getIgnoreLocalizedFields()) {
  109.                 $language null;
  110.                 // check for a localized field and if they should be used for this list
  111.                 if (property_exists('\\Pimcore\\Model\\DataObject\\' ucfirst($this->model->getClassName()), 'localizedfields')) {
  112.                     if ($this->model->getLocale()) {
  113.                         if (Tool::isValidLanguage((string)$this->model->getLocale())) {
  114.                             $language = (string)$this->model->getLocale();
  115.                         }
  116.                         if (!$language && DataObject\Localizedfield::isStrictMode()) {
  117.                             throw new \Exception('could not resolve locale: ' $this->model->getLocale());
  118.                         }
  119.                     }
  120.                     if (!$language) {
  121.                         $locale \Pimcore::getContainer()->get(LocaleServiceInterface::class)->findLocale();
  122.                         if (Tool::isValidLanguage((string)$locale)) {
  123.                             $language = (string)$locale;
  124.                         }
  125.                     }
  126.                     if (!$language) {
  127.                         $language Tool::getDefaultLanguage();
  128.                     }
  129.                     if (!$language) {
  130.                         throw new \Exception('No valid language/locale set. Use $list->setLocale() to add a language to the listing, or register a global locale');
  131.                     }
  132.                     $this->tableName 'object_localized_' $this->model->getClassId() . '_' $language;
  133.                 }
  134.             }
  135.         }
  136.         return $this->tableName;
  137.     }
  138.     /**
  139.      * @param DoctrineQueryBuilder $queryBuilder
  140.      *
  141.      * @return $this
  142.      *
  143.      * @throws \Exception
  144.      */
  145.     protected function applyJoins(DoctrineQueryBuilder $queryBuilder)
  146.     {
  147.         // add fielcollection's
  148.         $fieldCollections $this->model->getFieldCollections();
  149.         if (!empty($fieldCollections)) {
  150.             foreach ($fieldCollections as $fc) {
  151.                 // join info
  152.                 $table 'object_collection_' $fc['type'] . '_' $this->model->getClassId();
  153.                 $name $fc['type'];
  154.                 if (!empty($fc['fieldname'])) {
  155.                     $name .= '~' $fc['fieldname'];
  156.                 }
  157.                 // set join condition
  158.                 $condition = <<<CONDITION
  159. 1
  160.  AND {$this->db->quoteIdentifier($name)}.o_id = {$this->db->quoteIdentifier($this->getTableName())}.o_id
  161. CONDITION;
  162.                 if (!empty($fc['fieldname'])) {
  163.                     $condition .= <<<CONDITION
  164.  AND {$this->db->quoteIdentifier($name)}.fieldname = "{$fc['fieldname']}"
  165. CONDITION;
  166.                 }
  167.                 // add join
  168.                 $queryBuilder->leftJoin($this->getTableName(), $table$this->db->quoteIdentifier($name), $condition);
  169.             }
  170.         }
  171.         // add brick's
  172.         $objectbricks $this->model->getObjectbricks();
  173.         if (!empty($objectbricks)) {
  174.             foreach ($objectbricks as $ob) {
  175.                 $brickDefinition DataObject\Objectbrick\Definition::getByKey($ob);
  176.                 if (!$brickDefinition instanceof DataObject\Objectbrick\Definition) {
  177.                     continue;
  178.                 }
  179.                 // join info
  180.                 $table 'object_brick_query_' $ob '_' $this->model->getClassId();
  181.                 $name $ob;
  182.                 // add join
  183.                 $queryBuilder->leftJoin($this->getTableName(), $table$this->db->quoteIdentifier($name),
  184.                     <<<CONDITION
  185. 1
  186. AND {$this->db->quoteIdentifier($name)}.o_id = {$this->db->quoteIdentifier($this->getTableName())}.o_id
  187. CONDITION
  188.                 );
  189.                 if ($brickDefinition->getFieldDefinition('localizedfields')) {
  190.                     $langugage $this->getLocalizedBrickLanguage();
  191.                     //TODO wrong pattern
  192.                     $localizedTable 'object_brick_localized_query_' $ob '_' $this->model->getClassId() . '_' $langugage;
  193.                     $name $ob '_localized';
  194.                     // add join
  195.                     $queryBuilder->leftJoin($this->getTableName(), $localizedTable$this->db->quoteIdentifier($name),
  196.                         <<<CONDITION
  197. 1
  198. AND {$this->db->quoteIdentifier($name)}.ooo_id = {$this->db->quoteIdentifier($this->getTableName())}.o_id
  199. CONDITION
  200.                     );
  201.                 }
  202.             }
  203.         }
  204.         return $this;
  205.     }
  206. }