/**
  * Find records from a certain table which have categories assigned
  *
  * @param array $categoryUids The uids of the categories
  * @param string $relationField Field relation in MM table
  * @param string $tableName Name of the table to search in
  * @return array
  * @throws Exception
  */
 protected function findByCategories($categoryUids, $relationField, $tableName = 'pages')
 {
     $result = array();
     foreach ($categoryUids as $categoryUid) {
         try {
             $collection = CategoryCollection::load($categoryUid, true, $tableName, $relationField);
             if ($collection->count() > 0) {
                 foreach ($collection as $record) {
                     $result[$record['uid']] = $record;
                 }
             }
         } catch (\RuntimeException $e) {
             throw new Exception($e->getMessage());
         }
     }
     return $result;
 }
예제 #2
0
 /**
  * Collects all pages for the selected categories, sorted according to configuration.
  *
  * @param string $selectedCategories Comma-separated list of system categories primary keys
  * @param array $configuration TypoScript configuration for the "special." keyword
  * @param \TYPO3\CMS\Frontend\ContentObject\Menu\AbstractMenuContentObject $parentObject Back-reference to the calling object
  * @return string List of selected pages
  */
 public function collectPages($selectedCategories, $configuration, $parentObject)
 {
     $selectedPages = array();
     $categoriesPerPage = array();
     // Determine the name of the relation field
     $relationField = '';
     if (isset($configuration['relation.'])) {
         $relationField = $parentObject->parent_cObj->stdWrap($configuration['relation'], $configuration['relation.']);
     } elseif (isset($configuration['relation'])) {
         $relationField = $configuration['relation'];
     }
     // Get the pages for each selected category
     $selectedCategories = GeneralUtility::intExplode(',', $selectedCategories, TRUE);
     foreach ($selectedCategories as $aCategory) {
         $collection = CategoryCollection::load($aCategory, TRUE, 'pages', $relationField);
         $categoryUid = 0;
         if ($collection instanceof AbstractRecordCollection) {
             $categoryUid = $collection->getUid();
         }
         // Loop on the results, overlay each page record found
         foreach ($collection as $pageItem) {
             $parentObject->getSysPage()->versionOL('pages', $pageItem, TRUE);
             if (is_array($pageItem)) {
                 $selectedPages[$pageItem['uid']] = $parentObject->getSysPage()->getPageOverlay($pageItem);
                 // Keep a list of the categories each page belongs to
                 if (!isset($categoriesPerPage[$pageItem['uid']])) {
                     $categoriesPerPage[$pageItem['uid']] = array();
                 }
                 $categoriesPerPage[$pageItem['uid']][] = $categoryUid;
             }
         }
     }
     // Loop on the selected pages to add the categories they belong to, as comma-separated list of category uid's)
     // (this makes them available for rendering, if needed)
     foreach ($selectedPages as $uid => $pageRecord) {
         $selectedPages[$uid]['_categories'] = implode(',', $categoriesPerPage[$uid]);
     }
     // Sort the pages according to the sorting property
     self::$sortingField = isset($configuration['sorting.']) ? $parentObject->getParentContentObject()->stdWrap($configuration['sorting'], $configuration['sorting.']) : $configuration['sorting'];
     $order = isset($configuration['order.']) ? $parentObject->getParentContentObject()->stdWrap($configuration['order'], $configuration['order.']) : $configuration['order'];
     $selectedPages = $this->sortPages($selectedPages, $order);
     return $selectedPages;
 }
예제 #3
0
 /**
  * Collects records for all selected tables and categories.
  *
  * @param string $selectedCategories Comma-separated list of categories
  * @param array $tables List of tables
  * @param string $relationField Name of the field containing the categories relation
  * @return void
  */
 protected function collectRecordsFromCategories($selectedCategories, array $tables, $relationField)
 {
     $selectedCategories = array_unique(GeneralUtility::intExplode(',', $selectedCategories, true));
     // Loop on all selected tables
     foreach ($tables as $table) {
         // Get the records for each selected category
         $tableRecords = array();
         $categoriesPerRecord = array();
         foreach ($selectedCategories as $aCategory) {
             try {
                 $collection = CategoryCollection::load($aCategory, true, $table, $relationField);
                 if ($collection->count() > 0) {
                     // Add items to the collection of records for the current table
                     foreach ($collection as $item) {
                         $tableRecords[$item['uid']] = $item;
                         // Keep track of all categories a given item belongs to
                         if (!isset($categoriesPerRecord[$item['uid']])) {
                             $categoriesPerRecord[$item['uid']] = array();
                         }
                         $categoriesPerRecord[$item['uid']][] = $aCategory;
                     }
                 }
             } catch (\Exception $e) {
                 $message = sprintf('Could not get records for category id %d. Error: %s (%d)', $aCategory, $e->getMessage(), $e->getCode());
                 $this->getTimeTracker()->setTSlogMessage($message, 2);
             }
         }
         // Store the resulting records into the itemArray and data results array
         if (!empty($tableRecords)) {
             $this->data[$table] = array();
             foreach ($tableRecords as $record) {
                 $this->itemArray[] = array('id' => $record['uid'], 'table' => $table);
                 // Add to the record the categories it belongs to
                 $record['_categories'] = implode(',', $categoriesPerRecord[$record['uid']]);
                 $this->data[$table][$record['uid']] = $record;
             }
         }
     }
 }