56f697bcf571c6a7d88bde946f1d6fc1f754785a
[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
40 function tx_contagged_model_terms($controller) {
41 $this->controller = $controller;
42 $this->conf = $controller->conf;
43 $this->cObj = $controller->cObj;
44
45 $mapperClassName = t3lib_div::makeInstanceClassName('tx_contagged_model_mapper');
46 $this->mapper = new $mapperClassName($this->controller);
47
48 // build an array of tables in the database
49 $tablesArray = array();
50 $tablesResult = mysql_list_tables(TYPO3_db);
51 if (!mysql_error()) {
52 while($table = mysql_fetch_assoc($tablesResult)) {
53 $this->tablesArray[] = current($table);
54 }
55 }
56
57 $typesArray = $this->conf['types.'];
58 $this->terms = array();
59 $dataSourceArray = array();
60 foreach ($typesArray as $type=>$typeConfigArray) {
61 $storagePidsArray = $this->mapper->getStoragePidsArray($typeConfigArray);
62 $dataSource = $typeConfigArray['dataSource'] ? $typeConfigArray['dataSource'] : 'default';
63 foreach ($storagePidsArray as $pid) {
64 // if there is an entry for the data source: check for duplicates before adding the pid
65 // otherwise: create a new entry and add the pid
66 if ($dataSourceArray[$dataSource]) {
67 if ( !in_array($pid,$dataSourceArray[$dataSource]) ) {
68 $dataSourceArray[$dataSource][] = $pid;
69 }
70 } else {
71 $dataSourceArray[$dataSource][] = $pid;
72 }
73 }
74 }
75 // get an array of all data rows in the configured tables
76 foreach ($dataSourceArray as $dataSource=>$storagePidsArray ) {
77 $this->terms = array_merge($this->terms,$this->fetchAllTermsFromSource($dataSource,$storagePidsArray));
78 }
79
80 uasort($this->terms,array($this,'sortByTermAscending'));
81 }
82
83 /**
84 * [Describe function...]
85 *
86 * @return [type] ...
87 */
88 function findAllTerms() {
89 return $this->terms;
90 }
91
92 function findTermByUid($sourceName, $uid) {
93 $fetchedTerms = array();
94 foreach ($this->terms as $key => $term) {
95 if ($term['sourceName'] == $sourceName && $term['uid'] == $uid) {
96 return array($key => $term);
97 }
98 }
99
100 return NULL;
101 }
102
103 /**
104 * [Describe function...]
105 *
106 * @param [type] $termArrayA: ...
107 * @param [type] $termArrayB: ...
108 * @return [type] ...
109 */
110 function sortByTermAscending($termArrayA,$termArrayB) {
111 // TODO: improve sorting (UTF8, configurable, localized->hook)
112 // strcasecmp() internally converts the two strings it is comparing to lowercase, based on the server locale settings. As such, it
113 // cannot be relied upon to be able to convert appropriate multibyte characters in UTF-8 to lowercase and, depending on the actual
114 // locale, may have internally corrupted the UTF-8 strings it is comparing, having falsely matched byte sequences. It won’t actually
115 // damage the UTF-8 string but the result of the comparison cannot be trusted. (Ref. http://www.phpwact.org/php/i18n/utf-8)
116 // TODO remove; just a hack
117 $sortFieldA = $this->getSortField($termArrayA);
118 $sortFieldB = $this->getSortField($termArrayB);
119 $termsArray = array($termArrayA[$sortFieldA],$termArrayB[$sortFieldB]);
120 // $GLOBALS['TSFE']->csConvObj->convArray($termsArray,'utf-8','iso-8859-1');
121 $termsArrayBefore = $termsArray;
122 sort($termsArray,SORT_LOCALE_STRING);
123 $termsArrayAfterwards = $termsArray;
124 // debug($termsArrayBefore,'before');debug($termsArrayAfterwards,'after');
125 if ($termsArrayBefore[0]==$termsArrayAfterwards[0]) {
126 $result = -1;
127 } else {
128 $result = 1;
129 }
130
131 return $result;
132 }
133
134 /**
135 * Build an array of the entries in the tables
136 *
137 * @param [type] $dataSource: ...
138 * @param [type] $storagePids: ...
139 * @return An array with the terms an their configuration
140 */
141 function fetchAllTermsFromSource($dataSource,$storagePidsArray=NULL) {
142 $dataArray = array();
143 $terms = array();
144 $storagePidsList = implode(',',$storagePidsArray);
145 $dataSourceConfigArray = $this->conf['dataSources.'][$dataSource.'.'];
146 $sourceName = $dataSourceConfigArray['sourceName'];
147
148 // check if the table exists in the database
149 if (t3lib_div::inArray($this->tablesArray,$sourceName) ) {
150 // Build WHERE-clause
151 $whereClause = '1=1';
152 $whereClause .= $storagePidsList ? ' AND pid IN ('.$storagePidsList.')' : '';
153 $whereClause .= $dataSourceConfigArray['hasSysLanguageUid'] ? ' AND (sys_language_uid='.intval($GLOBALS['TSFE']->sys_language_uid) . ' OR sys_language_uid=-1)' : '';
154 $whereClause .= tslib_cObj::enableFields($sourceName);
155
156 // execute SQL-query
157 $result = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
158 '*', // SELECT ...
159 $sourceName, // FROM ...
160 $whereClause // WHERE ..
161 );
162
163 // map the fields
164 $dataArray = $this->mapper->getDataArray($result,$dataSource);
165 }
166 $this->fetchRelatedTerms($dataArray);
167
168 // TODO piVars as a data source
169
170 return $dataArray;
171 }
172
173 function fetchRelatedTerms(&$dataArray) {
174 foreach ($dataArray as $key => $termArray) {
175 $result = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
176 'uid_foreign, tablenames', // SELECT ...
177 'tx_contagged_related_mm', // FROM ...
178 'uid_local=' . $termArray['uid'] // WHERE ..
179 );
180
181 if (!empty($result)) {
182 $termArray['related'] = array();
183 foreach ($result as $row) {
184 $termArray['related'][] = array('sourceName' => $row['tablenames'], 'uid' => $row['uid_foreign']);
185 }
186 } else {
187 $termArray['related'] = NULL;
188 }
189 $newDataArray[] = $termArray;
190 }
191 $dataArray = $newDataArray;
192 }
193
194
195
196 function getSortField($termArray) {
197 if ($this->conf['types.'][$termArray['term_type'].'.']['sortField']) {
198 $sortField = $this->conf['types.'][$termArray['term_type'].'.']['sortField'];
199 } elseif ($this->conf['sortField']) {
200 $sortField = $this->conf['sortField'];
201 } else {
202 $sortField = 'term';
203 }
204 return $sortField;
205 }
206
207 }
208
209 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/contagged/model/class.tx_contagged_model_terms.php']) {
210 include_once ($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/contagged/model/class.tx_contagged_model_terms.php']);
211 }
212 ?>