[TASK] Add Category Based FileCollection 75/24575/6
authorFrans Saris <franssaris@gmail.com>
Fri, 11 Oct 2013 14:19:19 +0000 (16:19 +0200)
committerSteffen Ritter <info@rs-websystems.de>
Tue, 15 Oct 2013 11:13:20 +0000 (13:13 +0200)
In order to ease migration from DAM we need to
be able to get File Objects based on its selected
categories.

This patch extends the file collection type to
create collection based on sys_category.

Resolves: #51580
Releases: 6.2
Change-Id: Ic8dfe39bae8977924b6cf90fb8d6284dd60d84c8
Reviewed-on: https://review.typo3.org/24575
Reviewed-by: Francois Suter
Tested-by: Francois Suter
Reviewed-by: Steffen Ritter
Tested-by: Steffen Ritter
typo3/sysext/core/Classes/Resource/Collection/CategoryBasedFileCollection.php [new file with mode: 0644]
typo3/sysext/core/Classes/Resource/ResourceFactory.php
typo3/sysext/core/Configuration/TCA/sys_file_collection.php
typo3/sysext/core/ext_tables.sql
typo3/sysext/lang/locallang_tca.xlf

diff --git a/typo3/sysext/core/Classes/Resource/Collection/CategoryBasedFileCollection.php b/typo3/sysext/core/Classes/Resource/Collection/CategoryBasedFileCollection.php
new file mode 100644 (file)
index 0000000..0585699
--- /dev/null
@@ -0,0 +1,90 @@
+<?php
+namespace TYPO3\CMS\Core\Resource\Collection;
+
+/***************************************************************
+ * Copyright notice
+ *
+ * (c) 2013 Frans Saris <franssaris (at) gmail.com>
+ * All rights reserved
+ *
+ * This script is part of the TYPO3 project. The TYPO3 project is
+ * free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * The GNU General Public License can be found at
+ * http://www.gnu.org/copyleft/gpl.html.
+ * A copy is found in the textfile GPL.txt and important notices to the license
+ * from the author is found in LICENSE.txt distributed with these scripts.
+ *
+ *
+ * This script is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+
+/**
+ * A collection containing a set files belonging to certain categories.
+ * This collection is persisted to the database with the accordant category identifiers.
+ */
+class CategoryBasedFileCollection extends \TYPO3\CMS\Core\Resource\Collection\AbstractFileCollection {
+
+       /**
+        * @var string
+        */
+       static protected $storageTableName = 'sys_file_collection';
+
+       /**
+        * @var string
+        */
+       static protected $type = 'categories';
+
+       /**
+        * @var string
+        */
+       static protected $itemsCriteriaField = 'category';
+
+       /**
+        * @var string
+        */
+       protected $itemTableName = 'sys_category';
+
+       /**
+        * Populates the content-entries of the collection
+        *
+        * @return void
+        */
+       public function loadContents() {
+
+               $resource = $this->getDatabase()->exec_SELECT_mm_query(
+                       'sys_file_metadata.file',
+                       'sys_category',
+                       'sys_category_record_mm',
+                       'sys_file_metadata',
+                       'AND sys_category.uid=' . intval($this->getItemsCriteria()) .
+                       ' AND sys_category_record_mm.tablenames = \'sys_file_metadata\''
+               );
+
+               $resourceFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
+               if ($resource) {
+                       while (($record = $this->getDatabase()->sql_fetch_assoc($resource)) !== FALSE) {
+                               $this->add($resourceFactory->getFileObject(intval($record['file'])));
+                       }
+                       $this->getDatabase()->sql_free_result($resource);
+               }
+       }
+
+       /**
+        * Gets the database object.
+        *
+        * @return \TYPO3\CMS\Core\Database\DatabaseConnection
+        */
+       protected function getDatabase() {
+               return $GLOBALS['TYPO3_DB'];
+       }
+
+}
index a0c9bb5..74d6e2c 100644 (file)
@@ -280,6 +280,9 @@ class ResourceFactory implements \TYPO3\CMS\Core\SingletonInterface {
                        case 'folder':
                                $collection = Collection\FolderBasedFileCollection::create($collectionData);
                                break;
+                       case 'category':
+                               $collection = Collection\CategoryBasedFileCollection::create($collectionData);
+                               break;
                        default:
                                $collection = NULL;
                }
index 5e953b5..1295bb2 100644 (file)
@@ -113,7 +113,8 @@ return array(
                                'type' => 'select',
                                'items' => array(
                                        array('Static selection of files', 'static'),
-                                       array('Folder from Storage', 'folder')
+                                       array('Folder from Storage', 'folder'),
+                                       array('Select by category', 'category')
                                )
                        )
                ),
@@ -154,12 +155,32 @@ return array(
                                'items' => array(),
                                'itemsProcFunc' => 'typo3/sysext/core/Classes/Resource/Service/UserFileMountService.php:TYPO3\CMS\Core\Resource\Service\UserFileMountService->renderTceformsSelectDropdown',
                        )
+               ),
+               'category' => array(
+                       'exclude' => 0,
+                       'label' => 'LLL:EXT:lang/locallang_tca.xlf:sys_file_collection.category',
+                       'config' => array(
+                               'minitems' => 0,
+                               'maxitems' => 1,
+                               'type' => 'select',
+                               'renderMode' => 'tree',
+                               'foreign_table' => 'sys_category',
+                               'foreign_table_where' => ' AND sys_category.sys_language_uid IN (-1,0) ORDER BY sys_category.sorting ASC',
+                               'treeConfig' => array(
+                                       'parentField' => 'parent',
+                                       'appearance' => array(
+                                               'expandAll' => TRUE,
+                                               'showHeader' => TRUE,
+                                       )
+                               )
+                       )
                )
        ),
        'types' => array(
                '0' => array('showitem' => 'sys_language_uid;;;;1-1-1, l10n_parent, l10n_diffsource, title;;1, type, files'),
                'static' => array('showitem' => 'sys_language_uid;;;;1-1-1, l10n_parent, l10n_diffsource, title;;1, type, files'),
-               'folder' => array('showitem' => 'sys_language_uid;;;;1-1-1, l10n_parent, l10n_diffsource, title;;1, type, storage, folder')
+               'folder' => array('showitem' => 'sys_language_uid;;;;1-1-1, l10n_parent, l10n_diffsource, title;;1, type, storage, folder'),
+               'category' => array('showitem' => 'sys_language_uid;;;;1-1-1, l10n_parent, l10n_diffsource, title;;1, type, category')
        ),
        'palettes' => array(
                '1' => array('showitem' => 'hidden, starttime, endtime')
index 433054d..83ac34f 100644 (file)
@@ -447,7 +447,7 @@ CREATE TABLE sys_file_collection (
        # Actual fields
        title tinytext,
        description text,
-       type varchar(6) DEFAULT 'static' NOT NULL,
+       type varchar(30) DEFAULT 'static' NOT NULL,
 
        # for type=static
        files int(11) DEFAULT '0' NOT NULL,
@@ -456,6 +456,9 @@ CREATE TABLE sys_file_collection (
        storage int(11) DEFAULT '0' NOT NULL,
        folder text NOT NULL,
 
+       # for type=category:
+       category int(11) DEFAULT '0' NOT NULL,
+
        PRIMARY KEY (uid),
        KEY parent (pid,deleted),
        KEY t3ver_oid (t3ver_oid,t3ver_wsid)
index 52bb689..e6b4df5 100644 (file)
                        <trans-unit id="sys_file_collection.folder" xml:space="preserve">
                                <source>Folder</source>
                        </trans-unit>
+                       <trans-unit id="sys_file_collection.category" xml:space="preserve">
+                               <source>Category</source>
+                       </trans-unit>
                        <trans-unit id="sys_language" xml:space="preserve">
                                <source>Website Language</source>
                        </trans-unit>