vendor/pimcore/pimcore/models/Site/Dao.php line 63

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\Site;
  15. use Pimcore\Model;
  16. use Pimcore\Model\Exception\NotFoundException;
  17. /**
  18.  * @internal
  19.  *
  20.  * @property \Pimcore\Model\Site $model
  21.  */
  22. class Dao extends Model\Dao\AbstractDao
  23. {
  24.     /**
  25.      * @param int $id
  26.      *
  27.      * @throws NotFoundException
  28.      */
  29.     public function getById($id)
  30.     {
  31.         $data $this->db->fetchAssociative('SELECT * FROM sites WHERE id = ?', [$id]);
  32.         if (empty($data['id'])) {
  33.             throw new NotFoundException(sprintf('Unable to load site with ID `%s`'$id));
  34.         }
  35.         $this->assignVariablesToModel($data);
  36.     }
  37.     /**
  38.      * @param int $id
  39.      *
  40.      * @throws NotFoundException
  41.      */
  42.     public function getByRootId($id)
  43.     {
  44.         $data $this->db->fetchAssociative('SELECT * FROM sites WHERE rootId = ?', [$id]);
  45.         if (empty($data['id'])) {
  46.             throw new NotFoundException(sprintf('Unable to load site with ID `%s`'$id));
  47.         }
  48.         $this->assignVariablesToModel($data);
  49.     }
  50.     /**
  51.      * @param string $domain
  52.      *
  53.      * @throws NotFoundException
  54.      */
  55.     public function getByDomain($domain)
  56.     {
  57.         $data $this->db->fetchAssociative('SELECT * FROM sites WHERE mainDomain = ? OR domains LIKE ?', [$domain'%"' $domain '"%']);
  58.         if (empty($data['id'])) {
  59.             // check for wildcards
  60.             // @TODO: refactor this to be more clear
  61.             $sitesRaw $this->db->fetchAllAssociative('SELECT id,domains FROM sites');
  62.             $wildcardDomains = [];
  63.             foreach ($sitesRaw as $site) {
  64.                 if (!empty($site['domains']) && strpos($site['domains'], '*')) {
  65.                     $siteDomains unserialize($site['domains']);
  66.                     if (is_array($siteDomains) && count($siteDomains) > 0) {
  67.                         foreach ($siteDomains as $siteDomain) {
  68.                             if (strpos($siteDomain'*') !== false) {
  69.                                 $siteDomain str_replace('.*''*'$siteDomain); // backward compatibility
  70.                                 $wildcardDomains[$siteDomain] = $site['id'];
  71.                             }
  72.                         }
  73.                     }
  74.                 }
  75.             }
  76.             foreach ($wildcardDomains as $wildcardDomain => $siteId) {
  77.                 $wildcardDomain preg_quote($wildcardDomain'#');
  78.                 $wildcardDomain str_replace('\\*''.*'$wildcardDomain);
  79.                 if (preg_match('#^' $wildcardDomain '$#'$domain)) {
  80.                     $data $this->db->fetchAssociative('SELECT * FROM sites WHERE id = ?', [$siteId]);
  81.                 }
  82.             }
  83.             if (empty($data['id'])) {
  84.                 throw new NotFoundException('there is no site for the requested domain: `' $domain 'ยด');
  85.             }
  86.         }
  87.         $this->assignVariablesToModel($data);
  88.     }
  89.     /**
  90.      * Save object to database
  91.      */
  92.     public function save()
  93.     {
  94.         if (!$this->model->getId()) {
  95.             $this->create();
  96.         }
  97.         $this->update();
  98.     }
  99.     /**
  100.      * Create a new record for the object in database
  101.      */
  102.     public function create()
  103.     {
  104.         $ts time();
  105.         $this->model->setCreationDate($ts);
  106.         $this->model->setModificationDate($ts);
  107.         $this->db->insert('sites', ['rootId' => $this->model->getRootId()]);
  108.         $this->model->setId((int) $this->db->lastInsertId());
  109.     }
  110.     /**
  111.      * Save changes to database, it's a good idea to use save() instead
  112.      */
  113.     public function update()
  114.     {
  115.         $ts time();
  116.         $this->model->setModificationDate($ts);
  117.         $data = [];
  118.         $site $this->model->getObjectVars();
  119.         foreach ($site as $key => $value) {
  120.             if (in_array($key$this->getValidTableColumns('sites'))) {
  121.                 if (is_array($value) || is_object($value)) {
  122.                     $value \Pimcore\Tool\Serialize::serialize($value);
  123.                 }
  124.                 if (is_bool($value)) {
  125.                     $value = (int) $value;
  126.                 }
  127.                 $data[$key] = $value;
  128.             }
  129.         }
  130.         $this->db->update('sites'$data, ['id' => $this->model->getId()]);
  131.         $this->model->clearDependentCache();
  132.     }
  133.     /**
  134.      * Deletes site from database
  135.      */
  136.     public function delete()
  137.     {
  138.         $this->db->delete('sites', ['id' => $this->model->getId()]);
  139.         //clean slug table
  140.         Model\DataObject\Data\UrlSlug::handleSiteDeleted($this->model->getId());
  141.         $this->model->clearDependentCache();
  142.     }
  143. }