src/Helpers/PageObjectRelationHelper.php line 95

Open in your IDE?
  1. <?php
  2. namespace App\Helpers;
  3. use App\Model\ProductCategory;
  4. use App\Model\Project;
  5. use Pimcore\Db;
  6. use Pimcore\Model\DataObject\Product;
  7. use Pimcore\Model\DataObject\Sector;
  8. use Pimcore\Model\Document;
  9. class PageObjectRelationHelper
  10. {
  11.     protected $locale;
  12.     /**
  13.      * @param Product $object
  14.      * @return false|Document|Document\Page
  15.      *
  16.      * Gets the related product page that has a relation to this Object for a specific language.
  17.      */
  18.     function getProductPageFromProductObject(Product $object)
  19.     {
  20.         return $this->getPageFromObject($object->getId(), "product""App\\\Controller\\\ProductController::singleAction");
  21.     }
  22.     /**
  23.      * @param ProductCategory $object
  24.      * @return false|Document|Document\Page
  25.      *
  26.      * Gets the related product page that has a relation to this Object for a specific language.
  27.      */
  28.     function getProductCategoryPageFromProductCategoryObject(ProductCategory $object)
  29.     {
  30.         return $this->getPageFromObject($object->getId(), "productCategory""App\\\Controller\\\ProductCategoryController::singleAction");
  31.     }
  32.     /**
  33.      * @param ProductCategory $object
  34.      * @return false|Document|Document\Page
  35.      *
  36.      * Gets the related product page that has a relation to this Object for a specific language.
  37.      */
  38.     function getSectorPageFromSectorObject(Sector $object)
  39.     {
  40.         return $this->getPageFromObject($object->getId(), "sector""App\\\Controller\\\SectorController::singleAction");
  41.     }
  42.     /**
  43.      * @param ProductCategory $object
  44.      * @return false|Document|Document\Page
  45.      *
  46.      * Gets the related project page that has a relation to this Object for a specific language.
  47.      */
  48.     function getProjectPageFromSectorObject(Project $object)
  49.     {
  50.         return $this->getPageFromObject($object->getId(), "project""App\\\Controller\\\ProjectController::singleAction");
  51.     }
  52.     private function getPageFromObject(int $objectIdstring $relationNamestring $controller)
  53.     {
  54.         // Get All pages(table documents)
  55.         // that have a relation named $relationName(from table documents_editables) and where in that relation the id of the $objectId is present
  56.         // and where the controller == $controller (from table documents_page)
  57.         //TODO use binding to prevent SQL injection:
  58.         $sql = <<< SQL
  59.             SELECT * FROM `documents_editables`
  60.             INNER JOIN `documents` ON `documents_editables`.`documentId` = `documents`.`id`
  61.             INNER JOIN `documents_page` ON `documents_editables`.`documentId` = `documents_page`.`id`
  62.             where `documents_editables`.`name` = '$relationName'
  63.             AND `documents_page`.`controller` = '$controller'
  64.             AND `documents_editables`.`data` LIKE '%"id";i:$objectId;%'
  65.             AND `documents`.`published` = 1
  66.             SQL;
  67.         foreach (Db::get()->fetchAll($sql) as $pageData) {
  68.             $page self::getPageObjectById($pageData);
  69.             if (!$page) {
  70.                 continue;
  71.             }
  72.             /** @var Document\Editable\Relation $pageRelationEditable */
  73.             if ($page->getProperty('language') == LocaleHelper::getCurrentLocale()) {
  74.                 return $page;
  75.             }
  76.         }
  77.         return null;
  78.     }
  79.     private static function getPageObjectById(array $pageArray)
  80.     {
  81.         if (isset($pageArray['id'])) {
  82.             return Document\Page::getById($pageArray['id']);
  83.         }
  84.         return null;
  85.     }
  86. }