vendor/memo_development/contao-foundation-bundle/src/Model/FoundationModel.php line 140

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * @package   Memo\MemoFoundationBundle
  5.  * @author    Media Motion AG
  6.  * @license   LGPL-3.0+
  7.  * @copyright Media Motion AG
  8.  */
  9. namespace Memo\FoundationBundle\Model;
  10. use Contao\PageModel;
  11. use Memo\TeamBundle\Model\TeamModel;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. use Terminal42\ChangeLanguage\PageFinder;
  14. /**
  15.  * Class FoundationModel
  16.  *
  17.  * Enable ease Translation of all fields in DCA
  18.  */
  19. class FoundationModel extends \Haste\Model\Model
  20. {
  21.     /**
  22.      * Returns a item, based on the alias, regardless of the language
  23.      * @param $strAlias
  24.      * @param $bolIgnorePublished (only show published by default)
  25.      * @return $objItem
  26.      */
  27.     public static function getItemByTranslatedAlias($strAlias$bolIgnorePublished=false$strCurrentLanguage=false$strField="alias")
  28.     {
  29.         // Presets
  30.         $strTable = static::$strTable;
  31.         \Controller::loadDataContainer($strTable);
  32.         $strCurrentLanguage = ($strCurrentLanguage) ? $strCurrentLanguage $GLOBALS['TL_LANGUAGE'];
  33.         // Get Default Language
  34.         $strDefaultLanguage \System::getContainer()->get('memo.foundation.language')->getDefaultLanguage();
  35.         // Get Items
  36.         if($strCurrentLanguage != $strDefaultLanguage && $GLOBALS['TL_DCA'][$strTable]['fields'][$strField]['translate'] === true)
  37.         {
  38.             $strFieldname $strField.'_' $strCurrentLanguage;
  39.             if($bolIgnorePublished){
  40.                 $colItem = static::findBy(
  41.                     array("$strTable.$strFieldname=?"),
  42.                     array($strAlias)
  43.                 );
  44.             } else {
  45.                 $colItem = static::findBy(
  46.                     array("$strTable.$strFieldname=?""$strTable.published=?""($strTable.start='' OR $strTable.start<?)""($strTable.stop='' OR $strTable.stop>?)"),
  47.                     array($strAlias1time(), time())
  48.                 );
  49.             }
  50.             if(!$colItem)
  51.             {
  52.                 if($bolIgnorePublished){
  53.                     $colItem = static::findBy(
  54.                         array("$strTable.$strField=?"),
  55.                         array($strAlias)
  56.                     );
  57.                 } else {
  58.                     $colItem = static::findBy(
  59.                         array("$strTable.$strField=?""$strTable.published=?""($strTable.start='' OR $strTable.start<?)""($strTable.stop='' OR $strTable.stop>?)"),
  60.                         array($strAlias1time(), time())
  61.                     );
  62.                 }
  63.             }
  64.         } else {
  65.             if($bolIgnorePublished){
  66.                 $colItem = static::findBy(
  67.                     array("$strTable.$strField=?"),
  68.                     array($strAlias)
  69.                 );
  70.             } else {
  71.                 $colItem = static::findBy(
  72.                     array("$strTable.$strField=?""$strTable.published=?""($strTable.start='' OR $strTable.start<?)""($strTable.stop='' OR $strTable.stop>?)"),
  73.                     array($strAlias1time(), time())
  74.                 );
  75.             }
  76.         }
  77.         // Reset Data that was modified (for Default-Language)
  78.         if($colItem){
  79.             foreach($colItem as $objItem)
  80.             {
  81.                 if(is_array($objItem->arrModified) && count($objItem->arrModified) > 0)
  82.                 {
  83.                     foreach($objItem->arrModified as $keyField => $strValue){
  84.                         $objItem->$keyField $strValue;
  85.                     }
  86.                 }
  87.             }
  88.         }
  89.         return $colItem;
  90.     }
  91.     /**
  92.      * Returns an array of all Page-IDs, on which auto_item Elements could be used on
  93.      * @param bool $bolTranslatedPages
  94.      * @return array
  95.      */
  96.     public static function getDetailPages($bolTranslatedPages=false$bolOnlyActiveLanguages=false)
  97.     {
  98.         // Presets
  99.         $objLanguageService \System::getContainer()->get('memo.foundation.language');
  100.         $arrLanguages $objLanguageService->getAllLanguages(true$bolOnlyActiveLanguages);
  101.         $arrPages = array();
  102.         // Only continue, if the archive-table exists
  103.         $objDatabase \Database::getInstance();
  104.         if ($objDatabase->tableExists(static::getTable()) === false){
  105.             return $arrPages;
  106.         }
  107.         // Get all archives
  108.         $objArchives = static::findAll();
  109.         // Look for jumpTo Pages in the Archives
  110.         if ($objArchives)
  111.         {
  112.             foreach($objArchives as $objArchive)
  113.             {
  114.                 if($objArchive->jumpTo)
  115.                 {
  116.                     $arrPages[$objArchive->jumpTo] = $objArchive->jumpTo;
  117.                 }
  118.                 if($bolTranslatedPages){
  119.                     foreach($arrLanguages as $strLanguage){
  120.                         $strFieldname "jumpTo" $objLanguageService->getLanguagePostfix($strLanguage);
  121.                         if($objArchive->$strFieldname)
  122.                         {
  123.                             $arrPages[$objArchive->$strFieldname] = $objArchive->$strFieldname;
  124.                         }
  125.                     }
  126.                 } else {
  127.                     if($objArchive->jumpTo)
  128.                     {
  129.                         $arrPages[$objArchive->jumpTo] = $objArchive->jumpTo;
  130.                     }
  131.                 }
  132.             }
  133.         }
  134.         $colModules \ModuleModel::findBy(array('tl_module.jumpTo!=?''foundation_item_template!=?'), array(''''));
  135.         if($colModules)
  136.         {
  137.             foreach($colModules as $objModule){
  138.                 $arrPages[$objModule->jumpTo] = $objModule->jumpTo;
  139.             }
  140.         }
  141.         // Get the pages in all languages
  142.         if($bolTranslatedPages && is_array($arrPages)){
  143.             $arrOrigPages $arrPages;
  144.             $objPageFinder = new PageFinder();
  145.             foreach($arrLanguages as $strLanguage)
  146.             {
  147.                 foreach($arrOrigPages as $intPage)
  148.                 {
  149.                     try {
  150.                         $objTranslatedPage $objPageFinder->findAssociatedForLanguage(\PageModel::findByPk($intPage), $strLanguage);
  151.                     } catch (\InvalidArgumentException $e) {
  152.                         // Do nothing - this can happen, if there are multiple domains in the same installation
  153.                     }
  154.                     if($objTranslatedPage)
  155.                     {
  156.                         $arrPages[$objTranslatedPage->id] = $objTranslatedPage->id;
  157.                     }
  158.                 }
  159.             }
  160.         }
  161.         return $arrPages;
  162.     }
  163.     /**
  164.      * Returns the object, with all translated (or default) values
  165.      * @param $strLanguage
  166.      * @return $this
  167.      */
  168.     public function getTranslatedModel($strLanguage)
  169.     {
  170.         // Get Language Postfix
  171.         $strPostfix \System::getContainer()->get('memo.foundation.language')->getLanguagePostfix($strLanguage);
  172.         // Only run, if there is a Postfix
  173.         if($strPostfix != '')
  174.         {
  175.             // Loop all dca-fields
  176.             foreach($this->arrData as $key => $value)
  177.             {
  178.                 // Define the field-name for the wanted translation
  179.                 $strLanguageField $key $strPostfix;
  180.                 // Check if the field exists and if there is a value set (to override the default-value)
  181.                 if (
  182.                     array_key_exists($strLanguageField$this->arrData) &&
  183.                     $this->$strLanguageField != '' &&
  184.                     $this->$strLanguageField != null)
  185.                 {
  186.                     // Set new value (only for this call)
  187.                     $this->$key $this->$strLanguageField;
  188.                 }
  189.             }
  190.         }
  191.         // Return the translated object
  192.         return $this;
  193.     }
  194.     /**
  195.      * @param $arrPids
  196.      * @param array $arrOptions
  197.      * @return |null
  198.      */
  199.     public static function findPublishedByPids($arrPids, array $arrOptions=array(), array $arrColumns=array(), $arrValues=array())
  200.     {
  201.         // Return null, if no data found
  202.         if (empty($arrPids) || !\is_array($arrPids))
  203.         {
  204.             return null;
  205.         }
  206.         // Presets
  207.         $strTable = static::$strTable;
  208.         // Set Filter for Archives
  209.         $arrColumns[] = "$strTable.pid IN(" implode(','array_map('\intval'$arrPids)) . ")";
  210.         // Set Filter for Published
  211.         $arrColumns[] = "$strTable.published='1'";
  212.         // Set Filter for stop/start
  213.         $arrColumns[] = "($strTable.start='' OR $strTable.start<".time().")";
  214.         $arrColumns[] = "($strTable.stop='' OR $strTable.stop>".time().")";
  215.         // Get the Items
  216.         $colItems = static::findBy($arrColumns$arrValues$arrOptions);
  217.         // Return the Items
  218.         return $colItems;
  219.     }
  220.     /**
  221.      * @param $arrIds
  222.      * @param array $arrOptions
  223.      * @param array $arrColumns
  224.      * @return \Contao\Model|\Contao\Model[]|\Contao\Model\Collection|FoundationModel|null
  225.      */
  226.     public static function findPublishedByIds($arrIds, array $arrOptions=array(), array $arrColumns=array())
  227.     {
  228.         // Return null, if no data found
  229.         if (empty($arrIds) || !\is_array($arrIds))
  230.         {
  231.             return null;
  232.         }
  233.         // Presets
  234.         $strTable = static::$strTable;
  235.         // Set Filter for Archives
  236.         $arrColumns[] = "$strTable.id IN(" implode(','array_map('\intval'$arrIds)) . ")";
  237.         // Set Filter for Published
  238.         $arrColumns[] = "$strTable.published='1'";
  239.         // Set Filter for stop/start
  240.         $arrColumns[] = "($strTable.start='' OR $strTable.start<".time().")";
  241.         $arrColumns[] = "($strTable.stop='' OR $strTable.stop>".time().")";
  242.         // Get the Items
  243.         $colItems = static::findBy($arrColumnsnull$arrOptions);
  244.         // Return the Items
  245.         return $colItems;
  246.     }
  247.     public static function findAllPublished(array $arrOptions=array())
  248.     {
  249.         if(empty($arrOptions)){
  250.             $arrOptions = array('order' => 'title');
  251.         }
  252.         // Presets
  253.         $strTable = static::$strTable;
  254.         // Set Filter for Published
  255.         $arrColumns[] = "$strTable.published='1'";
  256.         // Set Filter for stop/start
  257.         $arrColumns[] = "($strTable.start='' OR $strTable.start<".time().")";
  258.         $arrColumns[] = "($strTable.stop='' OR $strTable.stop>".time().")";
  259.         // Get the Items
  260.         $colItems = static::findBy($arrColumnsnull$arrOptions);
  261.         // Return the Items
  262.         return $colItems;
  263.     }
  264.     public function getModified()
  265.     {
  266.         return $this->arrModified;
  267.     }
  268.     public function getURL($strLanguage=false$strType=UrlGeneratorInterface::ABSOLUTE_URL)
  269.     {
  270.         // Get the language
  271.         if(!$strLanguage)
  272.         {
  273.             $strLanguage $GLOBALS['TL_LANGUAGE'];
  274.         }
  275.         // Get the default language
  276.         $strDefaultLanguage \System::getContainer()->get('memo.foundation.language')->getDefaultLanguage();
  277.         // Get Tag in correct language
  278.         if ($strLanguage != $strDefaultLanguage) {
  279.             $this->getTranslatedModel($strLanguage);
  280.         }
  281.         // Static URL set?
  282.         if($this->url != ''){
  283.             return $this->url;
  284.         }
  285.         // Get the archive and translate it
  286.         $objArchive $this->getRelated('pid');
  287.         if ($strLanguage != $strDefaultLanguage) {
  288.             $objArchive->getTranslatedModel($strLanguage);
  289.         }
  290.         if(!$objArchive) {
  291.             return false;
  292.         }
  293.         if(!$objArchive->jumpTo){
  294.             return false;
  295.         }
  296.         // Get the page
  297.         $objDetailPage $objArchive->getRelated('jumpTo');
  298.         if($strLanguage != $strDefaultLanguage){
  299.             $objPageFinder = new \Terminal42\ChangeLanguage\PageFinder();
  300.             // Check if the page exists
  301.             if($objDetailPage && $objPageFinder){
  302.                 // Get the page in the correct language
  303.                 $objDetailPage $objPageFinder->findAssociatedForLanguage($objDetailPage$strLanguage);
  304.             } else {
  305.                 // Write to contao log
  306.                 \System::log('No page found for the detail page of the archive with the id ' $objArchive->id ' in ' $strLanguage__METHOD__TL_ERROR);
  307.             }
  308.         }
  309.         if(!$objDetailPage){
  310.             return false;
  311.         }
  312.         if(!$this->alias){
  313.             return false;
  314.         }
  315.         if(method_exists($objDetailPage'getAbsoluteUrl')){
  316.             // Generate absolute url
  317.             $strLink $objDetailPage->getAbsoluteUrl('/'.$this->alias);
  318.         } else {
  319.             // Get the url generator
  320.             $urlGenerator \System::getContainer()->get('contao.routing.url_generator');
  321.             // Defined the parameters array
  322.             $arrParams = array(
  323.                 '_locale' => $strLanguage,
  324.                 'alias' => $this->alias,
  325.                 'auto_item' => 'alias'
  326.             );
  327.             $strLink =  $urlGenerator->generate($objDetailPage->alias '/{alias}'$arrParams$strType);
  328.         }
  329.         return $strLink;
  330.     }
  331.     public static function countByFormData($strLanguage='de'$intOffset=0$intLimit=0$arrFormData=array())
  332.     {
  333.         // Get Tables
  334.         $strTable = static::$strTable;
  335.         // Define the default filter values
  336.         $arrColumns = array("$strTable.published=?""($strTable.start='' OR $strTable.start<?)""($strTable.stop='' OR $strTable.stop>?)");
  337.         $arrValues = array(1time(), time());
  338.         // Add defaut count
  339.         $intProducts 0;
  340.         // Add the form filters
  341.         if(class_exists('\Memo\ProductBundle\Model\ProductAttributeModel')){
  342.             $arrFormFilters \Memo\ProductBundle\Model\ProductAttributeModel::resolveFilterForm($arrFormData);
  343.             $arrColumns array_merge($arrColumns$arrFormFilters[0]);
  344.             $arrValues array_merge($arrValues$arrFormFilters[1]);
  345.             // Get the product count
  346.             $intProducts self::countBy($arrColumns$arrValues);
  347.         }
  348.         // Return the product count
  349.         return $intProducts;
  350.     }
  351.     public static function findByFormData($strLanguage=false$intOffset=0$intLimit=0$arrFormData=array(), $arrOptions=array())
  352.     {
  353.         // Get the language
  354.         if(!$strLanguage)
  355.         {
  356.             $strLanguage $GLOBALS['TL_LANGUAGE'];
  357.         }
  358.         // Get the default language
  359.         $strDefaultLanguage \System::getContainer()->get('memo.foundation.language')->getDefaultLanguage();
  360.         // Get Tables
  361.         $strTable = static::$strTable;
  362.         // Define the default filter values
  363.         $arrColumns = array("$strTable.published=?""($strTable.start='' OR $strTable.start<?)""($strTable.stop='' OR $strTable.stop>?)");
  364.         $arrValues = array(1time(), time());
  365.         // Add the form filters
  366.         if(class_exists('\Memo\ProductBundle\Model\ProductAttributeModel')) {
  367.             $arrFormFilters \Memo\ProductBundle\Model\ProductAttributeModel::resolveFilterForm($arrFormData);
  368.             $arrColumns array_merge($arrColumns$arrFormFilters[0]);
  369.             $arrValues array_merge($arrValues$arrFormFilters[1]);
  370.         }
  371.         // Add the offset and limit
  372.         $arrOptions array_merge(array('offset'=>$intOffset'limit'=>$intLimit), $arrOptions);
  373.         // Get the items
  374.         $colItems self::findBy($arrColumns$arrValues$arrOptions);
  375.         $arrItems= array();
  376.         if(!$colItems){
  377.             return $arrItems;
  378.         }
  379.         foreach($colItems as $objItem)
  380.         {
  381.             if($strLanguage != $strDefaultLanguage){
  382.                 $objItem->getTranslatedModel($strLanguage);
  383.             }
  384.             $arrItems[] = $objItem;
  385.         }
  386.         // Return the items
  387.         return $arrItems;
  388.     }
  389. }