Fixed bug #11920: Follow-up to feature #6872: TCEforms suggest gives no results with...
[Packages/TYPO3.CMS.git] / t3lib / tceforms / class.t3lib_tceforms_suggest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2007-2009 Andreas Wolf <andreas.wolf@ikt-werk.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 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * TCEforms wizard for rendering an AJAX selector for records
29 *
30 * $Id:$
31 *
32 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
33 * @author Benjamin Mack <benni@typo3.org>
34 */
35
36 class t3lib_TCEforms_Suggest {
37 // count the number of ajax selectors used
38 public $suggestCount = 0;
39 public $cssClass = 'typo3-TCEforms-suggest';
40 public $TCEformsObj; // reference to t3lib_tceforms
41
42
43 /**
44 * Initialize an instance of t3lib_TCEforms_suggest
45 *
46 * @param t3lib_TCEforms $tceForms Reference to an TCEforms instance
47 * @return void
48 */
49 public function init(&$tceForms) {
50 $this->TCEformsObj =& $tceForms;
51 }
52
53 /**
54 * Renders an ajax-enabled text field. Also adds required JS
55 *
56 * @param string $fieldname The fieldname in the form
57 * @param string $table The table we render this selector for
58 * @param string $field The field we render this selector for
59 * @param array $row The row which is currently edited
60 * @param array $config The TSconfig of the field
61 * @return string The HTML code for the selector
62 */
63 public function renderSuggestSelector($fieldname, $table, $field, array $row, array $config) {
64 $this->suggestCount++;
65
66 $containerCssClass = $this->cssClass . ' ' . $this->cssClass . '-position-right';
67 $suggestId = 'suggest-' . $table . '-' . $field . '-' . $row['uid'];
68
69 $selector = '
70 <div class="' . $containerCssClass . '" id="' . $suggestId . '">
71 <input type="text" id="' . $fieldname . 'Suggest" value="' .
72 $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.findRecord') . '" class="' . $this->cssClass . '-search" />
73 <div class="' . $this->cssClass . '-choices" style="display: none;" id="' . $fieldname . 'SuggestChoices"></div>
74
75 </div>';
76
77 // get minimumCharacters from TCA
78 if (isset($config['fieldConf']['config']['wizards']['suggest']['default']['minimumCharacters'])) {
79 $minChars = intval($config['fieldConf']['config']['wizards']['suggest']['default']['minimumCharacters']);
80 }
81 // overwrite it with minimumCharacters from TSConfig (TCEFORM) if given
82 if (isset($config['fieldTSConfig']['suggest.']['default.']['minimumCharacters'])) {
83 $minChars = intval($config['fieldTSConfig']['suggest.']['default.']['minimumCharacters']);
84 }
85 $minChars = ($minChars > 0 ? $minChars : 2);
86
87 // replace "-" with ucwords for the JS object name
88 $jsObj = str_replace(' ', '', ucwords(str_replace('-', ' ', t3lib_div::strtolower($suggestId))));
89 $this->TCEformsObj->additionalJS_post[] = '
90 var ' . $jsObj . ' = new TCEForms.Suggest("' . $fieldname . '", "' . $table . '", "' . $field .
91 '", "' . $row['uid'] . '", ' . $row['pid'] . ', ' . $minChars . ');
92 ' . $jsObj . '.defaultValue = "' . t3lib_div::slashJS($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.findRecord')) . '";
93 ';
94
95 return $selector;
96 }
97
98 /**
99 * Ajax handler for the "suggest" feature in TCEforms
100 * compiles together the different results from
101 *
102 * @param string $search The string to search for in the database
103 * @param string $table The table which is currently edited
104 * @param string $field The field which is currently edited
105 * @param integer $uid The uid of the record which is currently edited (may be NEWxxxxxx)
106 * @param integer $pid The pid of the record which is currently edited (always numeric
107 * @param string $formfield The field which is edited
108 * @return void
109 */
110 public function processAjaxRequest($params, &$ajaxObj) {
111
112 // get parameters from $_GET/$_POST
113 $search = t3lib_div::_GP('value');
114 $table = t3lib_div::_GP('table');
115 $field = t3lib_div::_GP('field');
116 $uid = t3lib_div::_GP('uid');
117 $pageId = t3lib_div::_GP('pid');
118
119 t3lib_div::loadTCA($table);
120
121 // If the $uid is numeric, we have an already existing element, so get the
122 // TSconfig of the page itself or the element container (for non-page elements)
123 // otherwise it's a new element, so use given id of parent page (i.e., don't modify it here)
124 if (is_numeric($uid)) {
125 if ($table == 'pages') {
126 $pageId = $uid;
127 } else {
128 $row = t3lib_BEfunc::getRecord($table, $uid);
129 $pageId = $row['pid'];
130 }
131 }
132
133 $TSconfig = t3lib_BEfunc::getPagesTSconfig($pageId);
134
135 $wizardConfig = $GLOBALS['TCA'][$table]['columns'][$field]['config']['wizards']['suggest'];
136 $queryTables = t3lib_div::trimExplode(',', $GLOBALS['TCA'][$table]['columns'][$field]['config']['allowed']);
137 $resultRows = array();
138
139 // fetch the records for each query table. A query table is a table from which records are allowed to
140 // be added to the TCEForm selector, originally fetched from the "allowed" config option in the TCA
141 foreach ($queryTables as $queryTable) {
142 t3lib_div::loadTCA($queryTable);
143
144 // if the table does not exist, skip it
145 if (!is_array($GLOBALS['TCA'][$queryTable]) || !count($GLOBALS['TCA'][$queryTable])) {
146 continue;
147 }
148 $config = (array)$wizardConfig['default'];
149
150 if (is_array($wizardConfig[$queryTable])) {
151 $config = t3lib_div::array_merge_recursive_overrule($config, $wizardConfig[$queryTable]);
152 }
153
154
155 // merge the configurations of different "levels" to get the working configuration for this table and
156 // field (i.e., go from the most general to the most special configuration)
157 if (is_array($TSconfig['TCEFORM.']['suggest.']['default.'])) {
158 $config = t3lib_div::array_merge_recursive_overrule($config, $TSconfig['TCEFORM.']['suggest.']['default.']);
159 }
160
161 if (is_array($TSconfig['TCEFORM.']['suggest.'][$queryTable.'.'])) {
162 $config = t3lib_div::array_merge_recursive_overrule($config, $TSconfig['TCEFORM.']['suggest.'][$queryTable.'.']);
163 }
164
165 // use $table instead of $queryTable here because we overlay a config
166 // for the input-field here, not for the queried table
167 if (is_array($TSconfig['TCEFORM.'][$table.'.'][$field.'.']['suggest.']['default.'])) {
168 $config = t3lib_div::array_merge_recursive_overrule($config, $TSconfig['TCEFORM.'][$table.'.'][$field.'.']['suggest.']['default.']);
169 }
170 if (is_array($TSconfig['TCEFORM.'][$table.'.'][$field.'.']['suggest.'][$queryTable.'.'])) {
171 $config = t3lib_div::array_merge_recursive_overrule($config, $TSconfig['TCEFORM.'][$table.'.'][$field.'.']['suggest.'][$queryTable.'.']);
172 }
173 // instantiate the class that should fetch the records for this $queryTable
174 $receiverClassName = $config['receiverClass'];
175 if (!class_exists($receiverClassName)) {
176 $receiverClassName = 't3lib_TCEforms_Suggest_DefaultReceiver';
177 }
178 $receiverObj = t3lib_div::makeInstance($receiverClassName, $queryTable, $config);
179
180 $params = array('value' => $search);
181 $rows = $receiverObj->queryTable($params);
182
183 if (!$rows) {
184 continue;
185 }
186 $resultRows = t3lib_div::array_merge($resultRows, $rows);
187 unset($rows);
188 }
189
190 $listItems = array();
191 if (count($resultRows) > 0) {
192 // traverse all found records and sort them
193 $rowsSort = array();
194 foreach ($resultRows as $key => $row) {
195 $rowsSort[$key] = $row['text'];
196 }
197 asort($rowsSort);
198 $rowsSort = array_keys($rowsSort);
199
200 // Limit the number of items in the result list
201 $maxItems = $config['maxItemsInResultList'] ? $config['maxItemsInResultList'] : 10;
202 $maxItems = min(count($resultRows), $maxItems);
203
204 // put together the selector entry
205 for ($i = 0; $i < $maxItems; $i++) {
206 $row = $resultRows[$rowsSort[$i]];
207 $rowId = $row['table'] . '-' . $row['uid'] . '-' . $table . '-' . $uid . '-' . $field;
208 $listItems[] = '<li' . ($row['class'] != '' ? ' class="' . $row['class'] . '"' : '') .
209 ' id="' . $rowId . '" style="' . $row['style'] . '">' . $row['text'] . '</li>';
210 }
211 }
212
213 if (count($listItems) > 0) {
214 $list = implode('', $listItems);
215 } else {
216 $list = '<li class="suggest-noresults"><i>' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.noRecordFound') . '</i></li>';
217 }
218
219 $list = '<ul class="' . $this->cssClass . '-resultlist">' . $list . '</ul>';
220 $ajaxObj->addContent(0, $list);
221 }
222 }
223
224
225 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['classes/t3lib/tceforms/class.t3lib_tceforms_suggest.php']) {
226 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['classes/t3lib/tceforms/class.t3lib_tceforms_suggest.php']);
227 }