src/Rhyme/WMassArtsHub/Module/Submission/CombinedLister.php line 63

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (C) 2021 Rhyme Digital, LLC.
  4.  *
  5.  * @link       https://rhyme.digital
  6.  * @license    http://www.gnu.org/licenses/lgpl-3.0.html LGPL
  7.  */
  8. namespace Rhyme\WMassArtsHub\Module\Submission;
  9. use Contao\StringUtil;
  10. use Rhyme\WMassArtsHub\Model\Submission\CallsForArt;
  11. use Rhyme\WMassArtsHub\Model\Submission\Funding;
  12. use Rhyme\WMassArtsHub\Model\Submission\Profdev;
  13. use Rhyme\WMassArtsHub\Model\Submission\Space;
  14. use Contao\Date;
  15. use Contao\Input;
  16. use Contao\Config;
  17. use Contao\Module;
  18. use Contao\Environment;
  19. use Contao\Pagination;
  20. use Contao\Model\Collection;
  21. use Contao\BackendTemplate;
  22. use Contao\FrontendTemplate;
  23. use Contao\CoreBundle\Exception\PageNotFoundException;
  24. use Rhyme\WMassArtsHub\Model\Submission\SubmissionInterface;
  25. use Rhyme\WMassArtsHub\Model\Submission\Venue;
  26. /**
  27.  * Class CombinedLister
  28.  * @package Rhyme\WMassArtsHub\Module
  29.  */
  30. class CombinedLister extends Module
  31. {
  32.     /**
  33.      * Current date object
  34.      * @var Date
  35.      */
  36.     protected $Date;
  37.     /**
  38.      * Total items we have to work with after query
  39.      * @var integer
  40.      */
  41.     protected $totalItems;
  42.     /**
  43.      * Template
  44.      * @var string
  45.      */
  46.     protected $strTemplate 'mod_artshub_submission_combinedlister';
  47.     /**
  48.      * Display a wildcard in the back end
  49.      *
  50.      * @return string
  51.      */
  52.     public function generate()
  53.     {
  54.         if (TL_MODE == 'BE') {
  55.             $objTemplate = new BackendTemplate('be_wildcard');
  56.             $objTemplate->wildcard '### ARTSHUB COMBINED SUBMISSIONLISTER ###';
  57.             $objTemplate->title $this->headline;
  58.             $objTemplate->id $this->id;
  59.             $objTemplate->link $this->name;
  60.             $objTemplate->href 'contao/main.php?do=themes&amp;table=tl_module&amp;act=edit&amp;id=' $this->id;
  61.             return $objTemplate->parse();
  62.         }
  63.         $strBuffer parent::generate();
  64.         return empty($this->Template->items) ? '' $strBuffer;
  65.     }
  66.     /**
  67.      * Generate the module
  68.      */
  69.     protected function compile()
  70.     {
  71.         global $objPage;
  72.         // Set body class
  73.         $objPage->cssClass $objPage->cssClass . (stripos($objPage->cssClass'hasLister') !== false '' 'hasLister');
  74.         //Make sure we only grab ones we want
  75.         $arrElementTypes StringUtil::deserialize($this->elementTypes);
  76.         $callsforartCollection    in_array(CallsForArt::getTable(), $arrElementTypes) ? CallsForArt::findAllPublished() : null;
  77.         $spaceCollection          in_array(Space::getTable(), $arrElementTypes) ? Space::findAllPublished() : null;
  78.         $fundingCollection        in_array(Funding::getTable(), $arrElementTypes) ? Funding::findAllPublished() : null;
  79.         $profDevCollection        in_array(Profdev::getTable(), $arrElementTypes) ? Profdev::findAllPublished() : null;
  80.         $venueCollection          in_array(Venue::getTable(), $arrElementTypes) ? Venue::findAllPublished() : null;
  81.         //Set up arrays for already listed items if they have not been
  82.         if(!$GLOBALS['COMBINEDLISTERITEMS'][CallsForArt::getTable()]) $GLOBALS['COMBINEDLISTERITEMS'][CallsForArt::getTable()] = array();
  83.         if(!$GLOBALS['COMBINEDLISTERITEMS'][Space::getTable()]) $GLOBALS['COMBINEDLISTERITEMS'][Space::getTable()] = array();
  84.         if(!$GLOBALS['COMBINEDLISTERITEMS'][Funding::getTable()]) $GLOBALS['COMBINEDLISTERITEMS'][Funding::getTable()] = array();
  85.         if(!$GLOBALS['COMBINEDLISTERITEMS'][Profdev::getTable()]) $GLOBALS['COMBINEDLISTERITEMS'][Profdev::getTable()] = array();
  86.         if(!$GLOBALS['COMBINEDLISTERITEMS'][Venue::getTable()]) $GLOBALS['COMBINEDLISTERITEMS'][Venue::getTable()] = array();
  87.         $sortedData $this->sortModels($callsforartCollection$spaceCollection$fundingCollection$profDevCollection$venueCollection);
  88.         //Determine the number of events we need relative to everything else
  89.         $limit $this->totalItems;
  90.         $offset = (int) $this->skipFirst;
  91.         // Maximum number of items
  92.         if ($this->numberOfItems 0)
  93.         {
  94.             $limit min($this->numberOfItems$this->totalItems);
  95.         }
  96.         $total $this->totalItems $offset;
  97.         // Pagination
  98.         if ($this->perPage && (!isset($limit) || $this->numberOfItems $this->perPage))
  99.         {
  100.             // Adjust the overall limit
  101.             if (isset($limit))
  102.             {
  103.                 $total min($limit$total);
  104.             }
  105.             $id 'page_e' $this->id;
  106.             $page Input::get($id) ?? 1;
  107.             // Do not index or cache the page if the page number is outside the range
  108.             if ($page || $page max(ceil($total/$this->perPage), 1))
  109.             {
  110.                 throw new PageNotFoundException('Page not found: ' Environment::get('uri'));
  111.             }
  112.             // Set limit and offset
  113.             $limit $this->perPage;
  114.             $offset += (max($page1) - 1) * $this->perPage;
  115.             $skip = (int) $this->skipFirst;
  116.             // Overall limit
  117.             if ($offset $limit $total $skip)
  118.             {
  119.                 $limit $total $skip $offset;
  120.             }
  121.             $objPagination = new Pagination($total$this->perPageConfig::get('maxPaginationLinks'), $id);
  122.             $this->Template->pagination $objPagination->generate("\n  ");
  123.         }
  124.         $arrParsedItems = array();
  125.         // Parse items
  126.         for ($i=$offset$i<($limit $offset); $i++)
  127.         {
  128.             /** @var CallsForArt|Space|Funding|Profdev|null $model */
  129.             $model $sortedData[$i];
  130.             if(null !== $model) {
  131.                 $arrParsedItems[] = array('model' => $model'content' => $model->generate($this));
  132.                 $GLOBALS['COMBINEDLISTERITEMS'][$model::getTable()][] = $model->id;
  133.             }
  134.         }
  135.         $this->Template->items $arrParsedItems;
  136.         $this->Template->emptyMsg $GLOBALS['TL_LANG']['ARTSHUB_SUBMISSIONLISTER']['empty'];
  137.         // Add modal
  138.         $objModalTpl = new FrontendTemplate('modal_artshub_combinedlister');
  139.         $GLOBALS['TL_BODY']['modal_artshub_combinedlister'] = $objModalTpl->parse();
  140.         // Add scripts
  141.         $GLOBALS['TL_JAVASCRIPT']['combinedListerModal'] = 'web/bundles/rhymewmassartshub/assets/js/frontend/combinedLister/modal.js|static';
  142.     }
  143.     /**
  144.      * Sort out the model collection data into arrays
  145.      *
  146.      * @param  Collection|null      $callsforartCollection
  147.      * @param  Collection|null      $spaceCollection
  148.      * @param  Collection|null      $fundingCollection
  149.      * @param  Collection|null      $profDevCollection
  150.      * @param  Collection|null      $venueCollection
  151.      * @return array                $sortedModels
  152.      */
  153.     protected function sortModels($callsforartCollection$spaceCollection$fundingCollection$profDevCollection$venueCollection)
  154.     {
  155.         $arrCollections = array($callsforartCollection$spaceCollection$fundingCollection$profDevCollection$venueCollection);
  156.         if($this->artshub_sorting === 'title') {
  157.             return $this->sortByTitle($arrCollections);
  158.         }
  159.         return $this->sortByDateSubmitted($arrCollections);
  160.     }
  161.     /**
  162.      * Default sorting - by date submitted
  163.      *
  164.      * @param array                 $arrCollections
  165.      * @return array                $returnArray
  166.      */
  167.     protected function sortByDateSubmitted($arrCollections){
  168.         $sortedModels = array(
  169.             CallsForArt::getTable()       => array(),
  170.             Space::getTable()             => array(),
  171.             Funding::getTable()           => array(),
  172.             Profdev::getTable()           => array(),
  173.             Venue::getTable()           => array(),
  174.         );
  175.         $totalAllItems 0;
  176.         foreach($arrCollections as $index=>$collection) {
  177.             if (null !== $collection) {
  178.                 while ($collection->next()) {
  179.                     $t $collection->current()::getTable();
  180.                     if (!in_array($collection->current()->id$GLOBALS['COMBINEDLISTERITEMS'][$t])) {
  181.                         /** @var SubmissionInterface $collection ->current() */
  182.                         $sortedModels[$t][$collection->current()->getDateSubmitted()][] = $collection->current();
  183.                         $totalAllItems++;
  184.                     }
  185.                 }
  186.             }
  187.         }
  188.         //Sort items low to high
  189.         ksort$sortedModels[CallsForArt::getTable()]);
  190.         ksort$sortedModels[Space::getTable()]);
  191.         ksort$sortedModels[Funding::getTable()]);
  192.         ksort$sortedModels[Profdev::getTable()]);
  193.         ksort$sortedModels[Venue::getTable()]);
  194.         $sortedData = array();
  195.         //Get all items we want with dayStart as key (need to do this b/c of different date sorts)
  196.         foreach($sortedModels as $table => $dateGroup) {
  197.             foreach($dateGroup as $date => $modelArrays) {
  198.                 foreach($modelArrays as $model) {
  199.                     /** @var SubmissionInterface $model */
  200.                     $t $model::getTable();
  201.                     $day = new Date($date);
  202.                     $sortedData[$day->dayBegin][] = $model;
  203.                 }
  204.             }
  205.         }
  206.         //Reverse sort for chronological
  207.         krsort($sortedData);
  208.         $this->totalItems 0;
  209.         //Loop and return simple sorted array
  210.         $returnArray = array();
  211.         foreach($sortedData as $date => $modelGroup) {
  212.             foreach($modelGroup as $model) {
  213.                 $this->totalItems++;
  214.                 $returnArray[] = $model;
  215.             }
  216.         }
  217.         return $returnArray;
  218.     }
  219.     /**
  220.      * Default sorting - by date submitted
  221.      *
  222.      * @param array                 $arrCollections
  223.      * @return array                $returnArray
  224.      */
  225.     protected function sortByTitle($arrCollections){
  226.         $sortedModels = array(
  227.             CallsForArt::getTable()       => array(),
  228.             Space::getTable()             => array(),
  229.             Funding::getTable()           => array(),
  230.             Profdev::getTable()           => array(),
  231.             Venue::getTable()           => array(),
  232.         );
  233.         $totalAllItems 0;
  234.         foreach($arrCollections as $index=>$collection) {
  235.             if (null !== $collection) {
  236.                 while ($collection->next()) {
  237.                     $t $collection->current()::getTable();
  238.                     if (!in_array($collection->current()->id$GLOBALS['COMBINEDLISTERITEMS'][$t])) {
  239.                         /** @var SubmissionInterface $collection ->current() */
  240.                         $sortedModels[$t][$collection->current()->getTitle()][] = $collection->current();
  241.                         $totalAllItems++;
  242.                     }
  243.                 }
  244.             }
  245.         }
  246.         //Sort items low to high
  247.         ksort$sortedModels[CallsForArt::getTable()]);
  248.         ksort$sortedModels[Space::getTable()]);
  249.         ksort$sortedModels[Funding::getTable()]);
  250.         ksort$sortedModels[Profdev::getTable()]);
  251.         ksort$sortedModels[Venue::getTable()]);
  252.         $sortedData = array();
  253.         //I know that we are looping a couple times when we probably don't need to
  254.         //But trying to also account for if they want a limit on certain things, etc, like we did with Noho
  255.         foreach($sortedModels as $table => $dateGroup) {
  256.             foreach($dateGroup as $title => $modelArrays) {
  257.                 foreach($modelArrays as $model) {
  258.                     /** @var SubmissionInterface $model */
  259.                     $t $model::getTable();
  260.                     $sortedData[$model->getTitle()][] = $model;
  261.                 }
  262.             }
  263.         }
  264.         //Sort titles
  265.         ksort($sortedData);
  266.         $this->totalItems 0;
  267.         //Loop and return simple sorted array
  268.         $returnArray = array();
  269.         foreach($sortedData as $title => $modelGroup) {
  270.             foreach($modelGroup as $model) {
  271.                 $this->totalItems++;
  272.                 $returnArray[] = $model;
  273.             }
  274.         }
  275.         return $returnArray;
  276.     }
  277. }