|
TYPO3 API
SVNRelease
|
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 2007-2011 Andreas Wolf <andreas.wolf@ikt-werk.de> 00006 * All rights reserved 00007 * 00008 * This script is part of the TYPO3 project. The TYPO3 project is 00009 * free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * The GNU General Public License can be found at 00015 * http://www.gnu.org/copyleft/gpl.html. 00016 * A copy is found in the textfile GPL.txt and important notices to the license 00017 * from the author is found in LICENSE.txt distributed with these scripts. 00018 * 00019 * 00020 * This script is distributed in the hope that it will be useful, 00021 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00023 * GNU General Public License for more details. 00024 * 00025 * This copyright notice MUST APPEAR in all copies of the script! 00026 ***************************************************************/ 00027 /** 00028 * TCEforms wizard for rendering an AJAX selector for records 00029 * 00030 * $Id: class.t3lib_tceforms_suggest.php 10121 2011-01-18 20:15:30Z ohader $ 00031 * 00032 * @author Andreas Wolf <andreas.wolf@ikt-werk.de> 00033 * @author Benjamin Mack <benni@typo3.org> 00034 */ 00035 00036 class t3lib_TCEforms_Suggest { 00037 // count the number of ajax selectors used 00038 public $suggestCount = 0; 00039 public $cssClass = 'typo3-TCEforms-suggest'; 00040 public $TCEformsObj; // reference to t3lib_tceforms 00041 00042 00043 /** 00044 * Initialize an instance of t3lib_TCEforms_suggest 00045 * 00046 * @param t3lib_TCEforms $tceForms Reference to an TCEforms instance 00047 * @return void 00048 */ 00049 public function init(&$tceForms) { 00050 $this->TCEformsObj =& $tceForms; 00051 } 00052 00053 /** 00054 * Renders an ajax-enabled text field. Also adds required JS 00055 * 00056 * @param string $fieldname The fieldname in the form 00057 * @param string $table The table we render this selector for 00058 * @param string $field The field we render this selector for 00059 * @param array $row The row which is currently edited 00060 * @param array $config The TSconfig of the field 00061 * @return string The HTML code for the selector 00062 */ 00063 public function renderSuggestSelector($fieldname, $table, $field, array $row, array $config) { 00064 $this->suggestCount++; 00065 00066 $containerCssClass = $this->cssClass . ' ' . $this->cssClass . '-position-right'; 00067 $suggestId = 'suggest-' . $table . '-' . $field . '-' . $row['uid']; 00068 00069 $selector = ' 00070 <div class="' . $containerCssClass . '" id="' . $suggestId . '"> 00071 <input type="text" id="' . $fieldname . 'Suggest" value="' . 00072 $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.findRecord') . '" class="' . $this->cssClass . '-search" /> 00073 <div class="' . $this->cssClass . '-indicator" style="display: none;" id="' . $fieldname . 'SuggestIndicator"> 00074 <img src="' . $GLOBALS['BACK_PATH'] . 'gfx/spinner.gif" alt="' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:alttext.suggestSearching') . '" /> 00075 </div> 00076 <div class="' . $this->cssClass . '-choices" style="display: none;" id="' . $fieldname . 'SuggestChoices"></div> 00077 00078 </div>'; 00079 00080 // get minimumCharacters from TCA 00081 if (isset($config['fieldConf']['config']['wizards']['suggest']['default']['minimumCharacters'])) { 00082 $minChars = intval($config['fieldConf']['config']['wizards']['suggest']['default']['minimumCharacters']); 00083 } 00084 // overwrite it with minimumCharacters from TSConfig (TCEFORM) if given 00085 if (isset($config['fieldTSConfig']['suggest.']['default.']['minimumCharacters'])) { 00086 $minChars = intval($config['fieldTSConfig']['suggest.']['default.']['minimumCharacters']); 00087 } 00088 $minChars = ($minChars > 0 ? $minChars : 2); 00089 00090 // replace "-" with ucwords for the JS object name 00091 $jsObj = str_replace(' ', '', ucwords(str_replace('-', ' ', t3lib_div::strtolower($suggestId)))); 00092 $this->TCEformsObj->additionalJS_post[] = ' 00093 var ' . $jsObj . ' = new TCEForms.Suggest("' . $fieldname . '", "' . $table . '", "' . $field . 00094 '", "' . $row['uid'] . '", ' . $row['pid'] . ', ' . $minChars . '); 00095 ' . $jsObj . '.defaultValue = "' . t3lib_div::slashJS($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.findRecord')) . '"; 00096 '; 00097 00098 return $selector; 00099 } 00100 00101 /** 00102 * Ajax handler for the "suggest" feature in TCEforms. 00103 * 00104 * @param array $params The parameters from the AJAX call 00105 * @param TYPO3AJAX $ajaxObj The AJAX object representing the AJAX call 00106 * @return void 00107 */ 00108 public function processAjaxRequest($params, &$ajaxObj) { 00109 00110 // get parameters from $_GET/$_POST 00111 $search = t3lib_div::_GP('value'); 00112 $table = t3lib_div::_GP('table'); 00113 $field = t3lib_div::_GP('field'); 00114 $uid = t3lib_div::_GP('uid'); 00115 $pageId = t3lib_div::_GP('pid'); 00116 00117 t3lib_div::loadTCA($table); 00118 00119 // If the $uid is numeric, we have an already existing element, so get the 00120 // TSconfig of the page itself or the element container (for non-page elements) 00121 // otherwise it's a new element, so use given id of parent page (i.e., don't modify it here) 00122 if (is_numeric($uid)) { 00123 if ($table == 'pages') { 00124 $pageId = $uid; 00125 } else { 00126 $row = t3lib_BEfunc::getRecord($table, $uid); 00127 $pageId = $row['pid']; 00128 } 00129 } 00130 00131 $TSconfig = t3lib_BEfunc::getPagesTSconfig($pageId); 00132 $queryTables = array(); 00133 $foreign_table_where = ''; 00134 $wizardConfig = $GLOBALS['TCA'][$table]['columns'][$field]['config']['wizards']['suggest']; 00135 if (isset($GLOBALS['TCA'][$table]['columns'][$field]['config']['allowed'])) { 00136 $queryTables = t3lib_div::trimExplode(',', $GLOBALS['TCA'][$table]['columns'][$field]['config']['allowed']); 00137 } elseif (isset($GLOBALS['TCA'][$table]['columns'][$field]['config']['foreign_table'])) { 00138 $queryTables = array($GLOBALS['TCA'][$table]['columns'][$field]['config']['foreign_table']); 00139 $foreign_table_where = $GLOBALS['TCA'][$table]['columns'][$field]['config']['foreign_table_where']; 00140 // strip ORDER BY clause 00141 $foreign_table_where = trim(preg_replace('/ORDER[[:space:]]+BY.*/i', '', $foreign_table_where)); 00142 } 00143 $resultRows = array(); 00144 00145 // fetch the records for each query table. A query table is a table from which records are allowed to 00146 // be added to the TCEForm selector, originally fetched from the "allowed" config option in the TCA 00147 foreach ($queryTables as $queryTable) { 00148 t3lib_div::loadTCA($queryTable); 00149 00150 // if the table does not exist, skip it 00151 if (!is_array($GLOBALS['TCA'][$queryTable]) || !count($GLOBALS['TCA'][$queryTable])) { 00152 continue; 00153 } 00154 $config = (array) $wizardConfig['default']; 00155 00156 if (is_array($wizardConfig[$queryTable])) { 00157 $config = t3lib_div::array_merge_recursive_overrule($config, $wizardConfig[$queryTable]); 00158 } 00159 00160 00161 // merge the configurations of different "levels" to get the working configuration for this table and 00162 // field (i.e., go from the most general to the most special configuration) 00163 if (is_array($TSconfig['TCEFORM.']['suggest.']['default.'])) { 00164 $config = t3lib_div::array_merge_recursive_overrule($config, $TSconfig['TCEFORM.']['suggest.']['default.']); 00165 } 00166 00167 if (is_array($TSconfig['TCEFORM.']['suggest.'][$queryTable . '.'])) { 00168 $config = t3lib_div::array_merge_recursive_overrule($config, $TSconfig['TCEFORM.']['suggest.'][$queryTable . '.']); 00169 } 00170 00171 // use $table instead of $queryTable here because we overlay a config 00172 // for the input-field here, not for the queried table 00173 if (is_array($TSconfig['TCEFORM.'][$table . '.'][$field . '.']['suggest.']['default.'])) { 00174 $config = t3lib_div::array_merge_recursive_overrule($config, $TSconfig['TCEFORM.'][$table . '.'][$field . '.']['suggest.']['default.']); 00175 } 00176 if (is_array($TSconfig['TCEFORM.'][$table . '.'][$field . '.']['suggest.'][$queryTable . '.'])) { 00177 $config = t3lib_div::array_merge_recursive_overrule($config, $TSconfig['TCEFORM.'][$table . '.'][$field . '.']['suggest.'][$queryTable . '.']); 00178 } 00179 00180 //process addWhere 00181 if (!isset($config['addWhere']) && $foreign_table_where) { 00182 $config['addWhere'] = $foreign_table_where; 00183 } 00184 if (isset($config['addWhere'])) { 00185 $config['addWhere'] = strtr(' ' . $config['addWhere'], array( 00186 '###THIS_UID###' => intval($uid), 00187 '###CURRENT_PID###' => intval($pageId), 00188 )); 00189 } 00190 // instantiate the class that should fetch the records for this $queryTable 00191 $receiverClassName = $config['receiverClass']; 00192 if (!class_exists($receiverClassName)) { 00193 $receiverClassName = 't3lib_TCEforms_Suggest_DefaultReceiver'; 00194 } 00195 $receiverObj = t3lib_div::makeInstance($receiverClassName, $queryTable, $config); 00196 00197 $params = array('value' => $search); 00198 $rows = $receiverObj->queryTable($params); 00199 00200 if (empty($rows)) { 00201 continue; 00202 } 00203 $resultRows = t3lib_div::array_merge($resultRows, $rows); 00204 unset($rows); 00205 } 00206 00207 $listItems = array(); 00208 if (count($resultRows) > 0) { 00209 // traverse all found records and sort them 00210 $rowsSort = array(); 00211 foreach ($resultRows as $key => $row) { 00212 $rowsSort[$key] = $row['text']; 00213 } 00214 asort($rowsSort); 00215 $rowsSort = array_keys($rowsSort); 00216 00217 // Limit the number of items in the result list 00218 $maxItems = $config['maxItemsInResultList'] ? $config['maxItemsInResultList'] : 10; 00219 $maxItems = min(count($resultRows), $maxItems); 00220 00221 // put together the selector entry 00222 for ($i = 0; $i < $maxItems; $i++) { 00223 $row = $resultRows[$rowsSort[$i]]; 00224 $rowId = $row['table'] . '-' . $row['uid'] . '-' . $table . '-' . $uid . '-' . $field; 00225 $listItems[] = '<li' . ($row['class'] != '' ? ' class="' . $row['class'] . '"' : '') . 00226 ' id="' . $rowId . '" style="' . $row['style'] . '">' . $row['text'] . '</li>'; 00227 } 00228 } 00229 00230 if (count($listItems) > 0) { 00231 $list = implode('', $listItems); 00232 } else { 00233 $list = '<li class="suggest-noresults"><i>' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.noRecordFound') . '</i></li>'; 00234 } 00235 00236 $list = '<ul class="' . $this->cssClass . '-resultlist">' . $list . '</ul>'; 00237 $ajaxObj->addContent(0, $list); 00238 } 00239 } 00240 00241 00242 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['classes/t3lib/tceforms/class.t3lib_tceforms_suggest.php'])) { 00243 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['classes/t3lib/tceforms/class.t3lib_tceforms_suggest.php']); 00244 } 00245 00246 ?>
1.8.0