MetaModels\Helper\ToolboxFile::sortFiles PHP Method

sortFiles() public method

Allowed sort types are: name_asc - Sort by filename ascending. name_desc - Sort by filename descending date_asc - Sort by modification time ascending. date_desc - Sort by modification time descending. manual - Sort by passed id array, the array must contain the binary ids of the files. random - Shuffle all the files around.
public sortFiles ( string $sortType, array $sortIds = [] ) : array
$sortType string The sort condition to be applied.
$sortIds array The list of binary ids to sort by (sort type "manual" only).
return array The sorted file list.
    public function sortFiles($sortType, $sortIds = array())
    {
        switch ($sortType) {
            case 'name_desc':
                return $this->sortByName(false);
            case 'date_asc':
                return $this->sortByDate(true);
            case 'date_desc':
                return $this->sortByDate(false);
            case 'manual':
                return $this->sortByIdList($sortIds);
            case 'random':
                return $this->sortByRandom();
            default:
            case 'name_asc':
        }
        return $this->sortByName(true);
    }

Usage Example

 /**
  * {@inheritdoc}
  */
 protected function prepareTemplate(Template $objTemplate, $arrRowData, $objSettings)
 {
     parent::prepareTemplate($objTemplate, $arrRowData, $objSettings);
     $objToolbox = new ToolboxFile();
     $objToolbox->setBaseLanguage($this->getMetaModel()->getActiveLanguage());
     $objToolbox->setFallbackLanguage($this->getMetaModel()->getFallbackLanguage());
     $objToolbox->setLightboxId(sprintf('%s.%s.%s', $this->getMetaModel()->getTableName(), $objSettings->get('id'), $arrRowData['id']));
     if (strlen($this->get('file_validFileTypes'))) {
         $objToolbox->setAcceptedExtensions($this->get('file_validFileTypes'));
     }
     $objToolbox->setShowImages($objSettings->get('file_showImage'));
     if ($objSettings->get('file_imageSize')) {
         $objToolbox->setResizeImages($objSettings->get('file_imageSize'));
     }
     if ($arrRowData[$this->getColName()]) {
         if (isset($arrRowData[$this->getColName()]['value']['bin'])) {
             foreach ($arrRowData[$this->getColName()]['value']['bin'] as $strFile) {
                 $objToolbox->addPathById($strFile);
             }
         } elseif (is_array($arrRowData[$this->getColName()])) {
             // FIXME: should not happen anymore.
             foreach ($arrRowData[$this->getColName()] as $strFile) {
                 $objToolbox->addPathById($strFile);
             }
         } else {
             // FIXME: should not happen anymore.
             $objToolbox->addPathById($arrRowData[$this->getColName()]);
         }
     }
     $objToolbox->resolveFiles();
     $arrData = $objToolbox->sortFiles($objSettings->get('file_sortBy'));
     $objTemplate->files = $arrData['files'];
     $objTemplate->src = $arrData['source'];
 }