* ADD Page browser (pi_base)
[TYPO3CMS/Extensions/contagged.git] / model / class.tx_contagged_model_terms.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2007 Jochen Rau <j.rau@web.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24 require_once (t3lib_extMgm::extPath('contagged') . 'model/class.tx_contagged_model_mapper.php');
25
26 /**
27 * The model of contagged.
28 *
29 * @author Jochen Rau <j.rau@web.de>
30 * @package TYPO3
31 * @subpackage tx_contagged_model_terms
32 */
33 class tx_contagged_model_terms {
34 var $conf; // the TypoScript configuration array
35 var $cObj;
36 var $controller;
37 var $tablesArray; // array of all tables in the database
38 var $terms;
39 var $configuredSources;
40
41 function tx_contagged_model_terms($controller) {
42 $this->controller = $controller;
43 $this->conf = $controller->conf;
44 $this->cObj = $controller->cObj;
45
46 $mapperClassName = t3lib_div::makeInstanceClassName('tx_contagged_model_mapper');
47 $this->mapper = new $mapperClassName($this->controller);
48
49 // build an array of tables in the database
50 $tablesArray = array();
51 $tablesResult = mysql_list_tables(TYPO3_db);
52 if (!mysql_error()) {
53 while($table = mysql_fetch_assoc($tablesResult)) {
54 $this->tablesArray[] = current($table);
55 }
56 }
57
58 foreach ($this->conf['dataSources.'] as $dataSource => $sourceConfiguration) {
59 $this->configuredSources[] = $sourceConfiguration['sourceName'];
60 }
61
62 $typesArray = $this->conf['types.'];
63 $this->terms = array();
64 $dataSourceArray = array();
65 foreach ($typesArray as $type=>$typeConfigArray) {
66 $storagePidsArray = $this->mapper->getStoragePidsArray($typeConfigArray);
67 $dataSource = $typeConfigArray['dataSource'] ? $typeConfigArray['dataSource'] : 'default';
68 foreach ($storagePidsArray as $pid) {
69 // if there is an entry for the data source: check for duplicates before adding the pid
70 // otherwise: create a new entry and add the pid
71 if ($dataSourceArray[$dataSource]) {
72 if ( !in_array($pid,$dataSourceArray[$dataSource]) ) {
73 $dataSourceArray[$dataSource][] = $pid;
74 }
75 } else {
76 $dataSourceArray[$dataSource][] = $pid;
77 }
78 }
79 }
80 // get an array of all data rows in the configured tables
81 foreach ($dataSourceArray as $dataSource=>$storagePidsArray ) {
82 $this->terms = array_merge($this->terms,$this->fetchAllTermsFromSource($dataSource,$storagePidsArray));
83 }
84
85 uasort($this->terms,array($this,'sortByTermAscending'));
86 }
87
88 function findAllTerms() {
89 return $this->terms;
90 }
91
92 function findAllTermsToBeListed($pid = NULL) {
93 $terms = array();
94 foreach ($this->terms as $key => $term) {
95 if ( $term['exclude']!=1 && $this->conf['types.'][$term['term_type'].'.']['dontListTerms']!=1 && (in_array($pid,$term['listPages']) || $pid === NULL) ) {
96 $terms[$key] = $term;
97 }
98 }
99 return $terms;
100 }
101
102 function findTermByUid($sourceName, $uid) {
103 $fetchedTerms = array();
104 foreach ($this->terms as $key => $term) {
105 if ($term['sourceName'] == $sourceName && $term['uid'] == $uid) {
106 return array($key => $term);
107 }
108 }
109
110 return NULL;
111 }
112
113 function sourceIsConfigured($sourceName) {
114 return in_array($sourceName, $this->configuredSources);
115 }
116
117 function sortByTermAscending($termArrayA,$termArrayB) {
118 // TODO: improve sorting (UTF8, configurable, localized->hook)
119 // strcasecmp() internally converts the two strings it is comparing to lowercase, based on the server locale settings. As such, it
120 // cannot be relied upon to be able to convert appropriate multibyte characters in UTF-8 to lowercase and, depending on the actual
121 // locale, may have internally corrupted the UTF-8 strings it is comparing, having falsely matched byte sequences. It won’t actually
122 // damage the UTF-8 string but the result of the comparison cannot be trusted. (Ref. http://www.phpwact.org/php/i18n/utf-8)
123 // TODO remove; just a hack
124 $sortFieldA = $this->getSortField($termArrayA);
125 $sortFieldB = $this->getSortField($termArrayB);
126 $termsArray = array($termArrayA[$sortFieldA],$termArrayB[$sortFieldB]);
127 // $GLOBALS['TSFE']->csConvObj->convArray($termsArray,'utf-8','iso-8859-1');
128 $termsArrayBefore = $termsArray;
129 sort($termsArray,SORT_LOCALE_STRING);
130 $termsArrayAfterwards = $termsArray;
131 // debug($termsArrayBefore,'before');debug($termsArrayAfterwards,'after');
132 if ($termsArrayBefore[0]==$termsArrayAfterwards[0]) {
133 $result = -1;
134 } else {
135 $result = 1;
136 }
137
138 return $result;
139 }
140
141 /**
142 * Build an array of the entries in the tables
143 *
144 * @param [type] $dataSource: ...
145 * @param [type] $storagePids: ...
146 * @return An array with the terms an their configuration
147 */
148 function fetchAllTermsFromSource($dataSource,$storagePidsArray=NULL) {
149 $dataArray = array();
150 $terms = array();
151 $storagePidsList = implode(',',$storagePidsArray);
152 $dataSourceConfigArray = $this->conf['dataSources.'][$dataSource.'.'];
153 $sourceName = $dataSourceConfigArray['sourceName'];
154
155 // check if the table exists in the database
156 if (t3lib_div::inArray($this->tablesArray,$sourceName) ) {
157 // Build WHERE-clause
158 $whereClause = '1=1';
159 $whereClause .= $storagePidsList ? ' AND pid IN ('.$storagePidsList.')' : '';
160 $whereClause .= $dataSourceConfigArray['hasSysLanguageUid'] ? ' AND (sys_language_uid='.intval($GLOBALS['TSFE']->sys_language_uid) . ' OR sys_language_uid=-1)' : '';
161 $whereClause .= tslib_cObj::enableFields($sourceName);
162
163 // execute SQL-query
164 $result = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
165 '*', // SELECT ...
166 $sourceName, // FROM ...
167 $whereClause // WHERE ..
168 );
169
170 // map the fields
171 $dataArray = $this->mapper->getDataArray($result,$dataSource);
172 }
173 $this->fetchRelatedTerms($dataArray);
174
175 // TODO piVars as a data source
176
177 return $dataArray;
178 }
179
180 function fetchRelatedTerms(&$dataArray) {
181 foreach ($dataArray as $key => $termArray) {
182 $result = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
183 'uid_foreign, tablenames', // SELECT ...
184 'tx_contagged_related_mm', // FROM ...
185 'uid_local=' . $termArray['uid'], // WHERE ..
186 'sorting'
187 );
188
189 if (!empty($result)) {
190 $termArray['related'] = array();
191 foreach ($result as $row) {
192 if ($this->sourceIsConfigured($row['tablenames'])) {
193 $termArray['related'][] = array('sourceName' => $row['tablenames'], 'uid' => $row['uid_foreign']);
194 }
195 }
196 } else {
197 $termArray['related'] = NULL;
198 }
199 $newDataArray[] = $termArray;
200 }
201 $dataArray = $newDataArray;
202 }
203
204
205
206 function getSortField($termArray) {
207 if ($this->conf['types.'][$termArray['term_type'].'.']['sortField']) {
208 $sortField = $this->conf['types.'][$termArray['term_type'].'.']['sortField'];
209 } elseif ($this->conf['sortField']) {
210 $sortField = $this->conf['sortField'];
211 } else {
212 $sortField = 'term';
213 }
214 return $sortField;
215 }
216
217 }
218
219 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/contagged/model/class.tx_contagged_model_terms.php']) {
220 include_once ($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/contagged/model/class.tx_contagged_model_terms.php']);
221 }
222 ?>