[FEATURE] Adapt to new TCA and Form Engine API of TYPO3 CMS 7
[TYPO3CMS/Extensions/static_info_tables.git] / Classes / Hook / Backend / Form / Wizard / SuggestReceiver.php
1 <?php
2 namespace SJBR\StaticInfoTables\Hook\Backend\Form\Wizard;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2007-2011 Andreas Wolf <andreas.wolf@ikt-werk.de>
8 * (c) 2013-2015 Stanislas Rolland <typo3(arobas)sjbr.ca>
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30
31 use SJBR\StaticInfoTables\Utility\LocalizationUtility;
32
33 /**
34 * Default implementation of a handler class for an ajax record selector.
35 *
36 * Normally other implementations should be inherited from this one.
37 * queryTable() should not be overwritten under normal circumstances.
38 *
39 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
40 * @author Benjamin Mack <benni@typo3.org>
41 * @author Stanislas Rolland <typo3(arobas)sjbr.ca>
42 */
43 class SuggestReceiver extends \TYPO3\CMS\Backend\Form\Wizard\SuggestWizardDefaultReceiver {
44
45 /**
46 * Prepare the statement for selecting the records which will be returned to the selector. May also return some
47 * other records (e.g. from a mm-table) which will be used later on to select the real records
48 *
49 * @return void
50 */
51 protected function prepareSelectStatement() {
52 $searchWholePhrase = $this->config['searchWholePhrase'];
53 $searchString = $this->params['value'];
54 $searchUid = intval($searchString);
55 if (strlen($searchString)) {
56 $searchString = $GLOBALS['TYPO3_DB']->quoteStr($searchString, $this->table);
57 $likeCondition = ' LIKE \'' . ($searchWholePhrase ? '%' : '') . $GLOBALS['TYPO3_DB']->escapeStrForLike($searchString, $this->table) . '%\'';
58 // Get the label field for the current language, if any is available
59 $lang = LocalizationUtility::getCurrentLanguage();
60 $lang = LocalizationUtility::getIsoLanguageKey($lang);
61 $labelFields = LocalizationUtility::getLabelFields($this->table, $lang);
62 $selectFieldsList = $labelFields[0] . ',' . $this->config['additionalSearchFields'];
63 $selectFields = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $selectFieldsList, TRUE);
64 $selectFields = array_unique($selectFields);
65 $selectParts = array();
66 foreach ($selectFields as $field) {
67 $selectParts[] = $field . $likeCondition;
68 }
69 $this->selectClause = '(' . implode(' OR ', $selectParts) . ')';
70 if ($searchUid > 0 && $searchUid == $searchString) {
71 $this->selectClause = '(' . $this->selectClause . ' OR uid = ' . $searchUid . ')';
72 }
73 }
74 if (isset($GLOBALS['TCA'][$this->table]['ctrl']['delete'])) {
75 $this->selectClause .= ' AND ' . $GLOBALS['TCA'][$this->table]['ctrl']['delete'] . ' = 0';
76 }
77 if (count($this->allowedPages)) {
78 $pidList = $GLOBALS['TYPO3_DB']->cleanIntArray($this->allowedPages);
79 if (count($pidList)) {
80 $this->selectClause .= ' AND pid IN (' . implode(', ', $pidList) . ') ';
81 }
82 }
83 // add an additional search condition comment
84 if (isset($this->config['searchCondition']) && strlen($this->config['searchCondition']) > 0) {
85 $this->selectClause .= ' AND ' . $this->config['searchCondition'];
86 }
87 // add the global clauses to the where-statement
88 $this->selectClause .= $this->addWhere;
89 }
90
91 /**
92 * Prepares the clause by which the result elements are sorted. See description of ORDER BY in
93 * SQL standard for reference.
94 *
95 * @return void
96 */
97 protected function prepareOrderByStatement() {
98 if ($GLOBALS['TCA'][$this->table]['ctrl']['label']) {
99 $this->orderByStatement = $GLOBALS['TCA'][$this->table]['ctrl']['label'];
100 }
101 // Get the label field for the current language, if any is available
102 $lang = LocalizationUtility::getCurrentLanguage();
103 $lang = LocalizationUtility::getIsoLanguageKey($lang);
104 $labelFields = LocalizationUtility::getLabelFields($this->table, $lang);
105 $this->orderByStatement = implode(',' , $labelFields);
106 }
107
108 /**
109 * Manipulate a record before using it to render the selector; may be used to replace a MM-relation etc.
110 *
111 * @param array $row
112 */
113 protected function manipulateRecord(&$row) {
114 // Localize the record
115 $row[$GLOBALS['TCA'][$this->table]['ctrl']['label']] = LocalizationUtility::translate(array('uid' => $row['uid']), $this->table);
116 }
117 }