[TASK] Replace deprecated methods, remove require_once, use namespaced core classes...
[TYPO3CMS/Extensions/contagged.git] / model / class.tx_contagged_model_terms.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 * (c) 2007 Jochen Rau <j.rau@web.de>
5 * All rights reserved
6 * This script is part of the TYPO3 project. The TYPO3 project is
7 * free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 * The GNU General Public License can be found at
12 * http://www.gnu.org/copyleft/gpl.html.
13 * This script is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 * This copyright notice MUST APPEAR in all copies of the script!
18 ***************************************************************/
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * The model of contagged.
23 *
24 * @author Jochen Rau <j.rau@web.de>
25 * @package TYPO3
26 * @subpackage tx_contagged_model_terms
27 */
28 class tx_contagged_model_terms implements \TYPO3\CMS\Core\SingletonInterface {
29
30 var $conf; // the TypoScript configuration array
31 var $controller;
32
33 var $tablesArray = array(); // array of all tables in the database
34 var $dataSourceArray = array();
35
36 var $terms = array();
37
38 var $configuredSources = array();
39
40 var $listPagesCache = array();
41
42 function __construct($controller) {
43 $this->controller = $controller;
44 $this->conf = $controller->conf;
45 if (!is_object($this->cObj)) {
46 $this->cObj = GeneralUtility::makeInstance('TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer');
47 }
48
49 $this->mapper = GeneralUtility::makeInstance('tx_contagged_model_mapper', $this->controller);
50
51 // build an array of tables in the database
52 $this->tablesArray = $GLOBALS['TYPO3_DB']->admin_get_tables(TYPO3_db);
53
54 if (is_array($this->conf['dataSources.'])) {
55 foreach ($this->conf['dataSources.'] as $dataSource => $sourceConfiguration) {
56 $this->configuredSources[$sourceConfiguration['sourceName']] = substr($dataSource, 0, -1);
57 }
58 } else {
59 throw new RuntimeException('No configuration. Please include the static template.');
60 }
61
62 $typesArray = $this->conf['types.'];
63 foreach ($typesArray as $type => $typeConfigArray) {
64 $storagePidsArray = $this->getStoragePidsArray($typeConfigArray);
65 $dataSource = $typeConfigArray['dataSource'] ? $typeConfigArray['dataSource'] : 'default';
66 foreach ($storagePidsArray as $pid) {
67 // if there is an entry for the data source: check for duplicates before adding the pid
68 // otherwise: create a new entry and add the pid
69 if ($this->dataSourceArray[$dataSource]) {
70 if (!in_array($pid, $this->dataSourceArray[$dataSource])) {
71 $this->dataSourceArray[$dataSource][] = intval($pid);
72 }
73 } else {
74 $this->dataSourceArray[$dataSource][] = intval($pid);
75 }
76 }
77 }
78 }
79
80 function findAllTerms($additionalWhereClause = '') {
81 if (empty($this->terms)) {
82 foreach ($this->dataSourceArray as $dataSource => $storagePidsArray) {
83 $this->terms = array_merge($this->terms, $this->fetchTermsFromSource($dataSource, $storagePidsArray));
84 }
85 }
86 return $this->terms;
87 }
88
89 function findAllTermsToListOnPage($pid = NULL) {
90 $terms = $this->findAllTerms(' AND exclude=0');
91 if ($pid === NULL) {
92 $pid = $GLOBALS['TSFE']->id;
93 }
94 $filteredTerms = array();
95 foreach ($terms as $key => $term) {
96 $typeConfigurationArray = $this->conf['types.'][$term['term_type'] . '.'];
97 $listPidsArray = $this->getListPidsArray($term['term_type']);
98 if (($typeConfigurationArray['dontListTerms'] == 0) && (in_array($pid, $listPidsArray) || is_array($GLOBALS['T3_VAR']['ext']['contagged']['index'][$pid][$key]))) {
99 $filteredTerms[$key] = $term;
100 }
101 }
102 uasort($filteredTerms, array($this, 'sortByTermAscending'));
103 return $filteredTerms;
104 }
105
106 function sortByTermAscending($termArrayA, $termArrayB) {
107 return strnatcasecmp($termArrayA['term'], $termArrayB['term']);
108 }
109
110 function findTermByUid($dataSource, $uid) {
111 $additionalWhereClause = ' AND uid=' . intval($uid);
112 $terms = $this->fetchTermsFromSource($dataSource, $storagePidsArray, $additionalWhereClause);
113 if ($this->conf["fetchRelatedTerms"] == 1) {
114 $this->fetchRelatedTerms($terms);
115 }
116 if (is_array($terms) && count($terms) > 0) {
117 return array_shift($terms);
118 } else {
119 return NULL;
120 }
121 }
122
123 /**
124 * Build an array of the entries in the tables
125 *
126 * @param string $dataSource: The identifier of the data source
127 * @param array $storagePids: An array of storage page IDs
128 * @return array An array with the terms an their configuration
129 */
130 function fetchTermsFromSource($dataSource, $storagePidsArray = array(), $additionalWhereClause = '') {
131 $dataArray = array();
132 $dataSourceConfigArray = $this->conf['dataSources.'][$dataSource . '.'];
133 $tableName = $dataSourceConfigArray['sourceName'];
134 // check if the table exists in the database
135 if (array_key_exists($tableName, $this->tablesArray)) {
136 // Build WHERE-clause
137 $whereClause = '1=1';
138 $whereClause .= count($storagePidsArray) > 0 ? ' AND pid IN (' . implode(',', $storagePidsArray) . ')' : '';
139 $whereClause .= $dataSourceConfigArray['hasSysLanguageUid'] ? ' AND (sys_language_uid=' . intval($GLOBALS['TSFE']->sys_language_uid) . ' OR sys_language_uid=-1)' : '';
140 $whereClause .= $this->cObj->enableFields($tableName);
141 $whereClause .= $additionalWhereClause;
142
143 // execute SQL-query
144 $result = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
145 '*', // SELECT ...
146 $tableName, // FROM ...
147 $whereClause // WHERE ..
148 );
149 // map the fields
150 $mappedResult = $this->mapper->getDataArray($result, $dataSource);
151 }
152 if (is_array($mappedResult)) {
153 foreach ($mappedResult as $result) {
154 $dataArray[$result['source'] . '_' . $result['uid']] = $result;
155 }
156 }
157 // TODO piVars as a data source
158 return $dataArray;
159 }
160
161 function fetchRelatedTerms(&$dataArray) {
162 $newDataArray = array();
163 foreach ($dataArray as $key => $termArray) {
164 $result = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
165 'uid_foreign, tablenames', // SELECT ...
166 'tx_contagged_related_mm', // FROM ...
167 'uid_local=' . $termArray['uid'], // WHERE ..
168 'sorting'
169 );
170
171 if (!empty($result)) {
172 $termArray['related'] = array();
173 foreach ($result as $row) {
174 $dataSource = $this->configuredSources[$row['tablenames']];
175 if ($dataSource !== NULL) {
176 $termArray['related'][] = array('source' => $dataSource, 'uid' => $row['uid_foreign']);
177 }
178 }
179 } else {
180 $termArray['related'] = NULL;
181 }
182 $newDataArray[] = $termArray;
183 }
184 $dataArray = $newDataArray;
185 }
186
187 /**
188 * get the storage pids; cascade: type > dataSource > globalConfig
189 *
190 * @param string $typeConfigArray
191 * @return array An array containing the storage PIDs of the type given by
192 * @author Jochen Rau
193 */
194 function getStoragePidsArray($typeConfigArray) {
195 $storagePidsArray = array();
196 $dataSource = $typeConfigArray['dataSource'] ? $typeConfigArray['dataSource'] : 'default';
197 if (!empty($typeConfigArray['storagePids'])) {
198 $storagePidsArray = GeneralUtility::intExplode(',', $typeConfigArray['storagePids']);
199 } elseif (!empty($this->conf['dataSources.'][$dataSource . '.']['storagePids'])) {
200 $storagePidsArray = GeneralUtility::intExplode(',', $this->conf['dataSources.'][$dataSource . '.']['storagePids']);
201 } elseif (!empty($this->conf['storagePids'])) {
202 $storagePidsArray = GeneralUtility::intExplode(',', $this->conf['storagePids']);
203 }
204 return $storagePidsArray;
205 }
206
207 /**
208 * get the list page IDs; cascade: type > globalConfig
209 *
210 * @param string $typeConfigArray
211 * @return array An array containing the list PIDs of the type given by
212 * @author Jochen Rau
213 */
214 function getListPidsArray($termType) {
215 if (!isset($this->listPagesCache[$termType])) {
216 $listPidsArray = array();
217 if (!empty($this->conf['types.'][$termArray['term_type'] . '.']['listPages'])) {
218 $this->listPagesCache[$termType] = GeneralUtility::intExplode(',', $this->conf['types.'][$termArray['term_type'] . '.']['listPages']);
219 } elseif (!empty($this->conf['listPages'])) {
220 $this->listPagesCache[$termType] = GeneralUtility::intExplode(',', $this->conf['listPages']);
221 }
222 }
223 return $this->listPagesCache[$termType];
224 }
225 }
226
227 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/contagged/model/class.tx_contagged_model_terms.php']) {
228 include_once ($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/contagged/model/class.tx_contagged_model_terms.php']);
229 }
230 ?>