Resolves #45809: Refactor code and introduce namespacing
[TYPO3CMS/Extensions/static_info_tables.git] / Classes / Utility / EntityLabelUtility.php
1 <?php
2 namespace SJBR\StaticInfoTables\Utility;
3 /***************************************************************
4 * Copyright notice
5 *
6 * (c) 2004-2010 René Fritz (r.fritz@colorcube.de)
7 * (c) 2013 Stanislas Rolland <typo3(arobas)sjbr.ca>
8 * All rights reserved
9 *
10 * This script is part of the Typo3 project. The Typo3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * Functions to get appropriate labels for entities
28 */
29 class EntityLabelUtility {
30
31 /**
32 * Replaces any dynamic markers in a SQL statement.
33 *
34 * @param string The SQL statement with dynamic markers.
35 * @param string Name of the table.
36 * @param array row from table.
37 * @return string SQL query with dynamic markers subsituted.
38 */
39 protected function replaceMarkersInSQL ($sql, $table, $row) {
40
41 $TSconfig = \TYPO3\CMS\Backend\Utility\BackendUtility::getTCEFORM_TSconfig($table, $row);
42
43 /* Replace references to specific fields with value of that field */
44 if (strstr($sql,'###REC_FIELD_')) {
45 $sql_parts = explode('###REC_FIELD_',$sql);
46 while(list($kk,$vv)=each($sql_parts)) {
47 if ($kk) {
48 $sql_subpart = explode('###',$vv,2);
49 $sql_parts[$kk]=$TSconfig['_THIS_ROW'][$sql_subpart[0]].$sql_subpart[1];
50 }
51 }
52 $sql = implode('',$sql_parts);
53 }
54
55 /* Replace markers with TSConfig values */
56 $sql = str_replace('###THIS_UID###',intval($TSconfig['_THIS_UID']),$sql);
57 $sql = str_replace('###THIS_CID###',intval($TSconfig['_THIS_CID']),$sql);
58 $sql = str_replace('###SITEROOT###',intval($TSconfig['_SITEROOT']),$sql);
59 $sql = str_replace('###PAGE_TSCONFIG_ID###',intval($TSconfig[$field]['PAGE_TSCONFIG_ID']),$sql);
60 $sql = str_replace('###PAGE_TSCONFIG_IDLIST###',$GLOBALS['TYPO3_DB']->cleanIntList($TSconfig[$field]['PAGE_TSCONFIG_IDLIST']),$sql);
61 $sql = str_replace('###PAGE_TSCONFIG_STR###',$GLOBALS['TYPO3_DB']->quoteStr($TSconfig[$field]['PAGE_TSCONFIG_STR'], $table),$sql);
62
63 return $sql;
64 }
65
66
67 /**
68 * Function to use in own TCA definitions
69 * Adds additional select items
70 *
71 * items reference to the array of items (label,value,icon)
72 * config The config array for the field.
73 * TSconfig The "itemsProcFunc." from fieldTSconfig of the field.
74 * table Table name
75 * row Record row
76 * field Field name
77 *
78 * @param array itemsProcFunc data array:
79 * @return void The $items array may have been modified
80 */
81 public function selectItemsTCA ($params) {
82 global $TCA;
83
84 $where = '';
85 $config = &$params['config'];
86 $table = $config['itemsProcFunc_config']['table'];
87 $tcaWhere = $config['itemsProcFunc_config']['where'];
88 if ($tcaWhere) {
89 $where = self::replaceMarkersInSQL($tcaWhere, $params['table'], $params['row']);
90 }
91
92 if ($table) {
93 $indexField = $config['itemsProcFunc_config']['indexField'];
94 $indexField = $indexField ? $indexField : 'uid';
95
96 $lang = \SJBR\StaticInfoTables\Utility\LocalizationUtility::getCurrentLanguage();
97 $lang = strtolower(\SJBR\StaticInfoTables\Utility\LocalizationUtility::getIsoLanguageKey($lang));
98 $titleFields = \SJBR\StaticInfoTables\Utility\LocalizationUtility::getLabelFields($table, $lang);
99 $prefixedTitleFields = array();
100 foreach ($titleFields as $titleField) {
101 $prefixedTitleFields[] = $table.'.'.$titleField;
102 }
103 $fields = $table.'.'.$indexField.','.implode(',', $prefixedTitleFields);
104
105 if ($config['itemsProcFunc_config']['prependHotlist']) {
106
107 $limit = $config['itemsProcFunc_config']['hotlistLimit'];
108 $limit = $limit ? $limit : '8';
109 $app = $config['itemsProcFunc_config']['hotlistApp'];
110 $app = $app ? $app : TYPO3_MODE;
111
112 $res = $GLOBALS['TYPO3_DB']->exec_SELECT_mm_query(
113 $fields,
114 $table,
115 'tx_staticinfotables_hotlist',
116 '', // $foreign_table
117 'AND tx_staticinfotables_hotlist.tablenames='.$GLOBALS['TYPO3_DB']->fullQuoteStr($table,'tx_staticinfotables_hotlist').' AND tx_staticinfotables_hotlist.application='.$GLOBALS['TYPO3_DB']->fullQuoteStr($app,'tx_staticinfotables_hotlist'),
118 '',
119 'tx_staticinfotables_hotlist.sorting DESC', // $orderBy
120 $limit
121 );
122
123 $cnt = 0;
124 $rows = array();
125 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
126
127 foreach ($titleFields as $titleField) {
128 if ($row[$titleField]) {
129 $rows[$row[$indexField]] = $row[$titleField];
130 break;
131 }
132 }
133 $cnt++;
134 }
135 $GLOBALS['TYPO3_DB']->sql_free_result($res);
136
137 if (!isset($config['itemsProcFunc_config']['hotlistSort']) || $config['itemsProcFunc_config']['hotlistSort']) {
138 asort ($rows);
139 }
140
141 foreach ($rows as $index => $title) {
142 $params['items'][] = array($title, $index, '');
143 $cnt++;
144 }
145 if($cnt && !$config['itemsProcFunc_config']['hotlistOnly']) {
146 $params['items'][] = array('--------------', '', '');
147 }
148 }
149
150 // Set ORDER BY:
151 $orderBy = $titleFields[0];
152
153 if(!$config['itemsProcFunc_config']['hotlistOnly']) {
154 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($fields, $table, '1=1'.$where . \SJBR\StaticInfoTables\Utility\TcaUtility::getEnableFields($table), '', $orderBy);
155 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
156 foreach ($titleFields as $titleField) {
157 if ($row[$titleField]) {
158 $params['items'][] = array($row[$titleField], $row[$indexField], '');
159 break;
160 }
161 }
162 }
163 $GLOBALS['TYPO3_DB']->sql_free_result($res);
164 }
165 }
166 }
167
168
169 /**
170 * Updates the hotlist table.
171 * This means that a hotlist entry will be created or the counter of an existing entry will be increased
172 *
173 * @param string table name: static_countries, ...
174 * @param string value of the following index field
175 * @param string the field which holds the value and is an index field: uid (default) or one of the iso code fields which are also unique
176 * @param string This indicates a counter group. Default is TYPO3_MOD (BE or FE). If you want a unique hotlist for your application you can provide here a name (e.g. extension key)
177 * @return void
178 */
179 public function updateHotlist ($table, $indexValue, $indexField='', $app='') {
180
181 if ($table && $indexValue) {
182 $indexField = $indexField ? $indexField : 'uid';
183 $app = $app ? $app : TYPO3_MODE;
184
185 if ($indexField=='uid') {
186 $uid = $indexValue;
187
188 } else {
189 // fetch original record
190 $fields = array();
191 $fields[$indexField] = $indexField;
192 $fields['uid'] = 'uid';
193
194 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(implode(',',$fields), $table, $indexField.'='.$GLOBALS['TYPO3_DB']->fullQuoteStr($indexValue,$table) . \SJBR\StaticInfoTables\Utility\TcaUtility::getEnableFields($table));
195 if ($res !== FALSE) {
196 if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
197 $uid = $row['uid'];
198 }
199 $GLOBALS['TYPO3_DB']->sql_free_result($res);
200 }
201 }
202
203 if ($uid) {
204 // update record from hotlist table
205 $newRow = array('sorting' => 'sorting+1');
206 // the dumb update function does not allow to use sorting+1 - that's why this trick is necessary
207
208 $GLOBALS['TYPO3_DB']->sql_query(str_replace('"sorting+1"', 'sorting+1', $GLOBALS['TYPO3_DB']->UPDATEquery(
209 'tx_staticinfotables_hotlist',
210 'uid_local='.$uid.
211 ' AND application='.$GLOBALS['TYPO3_DB']->fullQuoteStr($app,'tx_staticinfotables_hotlist').
212 ' AND tablenames='.$GLOBALS['TYPO3_DB']->fullQuoteStr($table,'tx_staticinfotables_hotlist').
213 \SJBR\StaticInfoTables\Utility\TcaUtility::getEnableFields('tx_staticinfotables_hotlist'),
214 $newRow)));
215
216 if (!$GLOBALS['TYPO3_DB']->sql_affected_rows()) {
217 // insert new hotlist entry
218 $row = array(
219 'uid_local' => $uid,
220 'tablenames' => $table,
221 'application' => $app,
222 'sorting' => 1,
223 );
224 $res = $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_staticinfotables_hotlist', $row);
225 }
226 }
227 }
228 }
229 }
230 ?>