1cb471a1cdc5e97f4b12ffbea2b668270ac82c8e
[TYPO3CMS/Extensions/powermailCond.git] / Classes / UserFunc / GetPowermailFields.php
1 <?php
2 namespace In2code\PowermailCond\UserFunc;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2015 in2code.de
8 * Alex Kellner <alexander.kellner@in2code.de>,
9 * Oliver Eglseder <oliver.eglseder@in2code.de>
10 *
11 * All rights reserved
12 *
13 * This script is part of the TYPO3 project. The TYPO3 project is
14 * free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * The GNU General Public License can be found at
20 * http://www.gnu.org/copyleft/gpl.html.
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 use In2code\PowermailCond\Utility\ArrayUtility;
31 use TYPO3\CMS\Backend\Form\FormEngine;
32 use TYPO3\CMS\Core\Utility\GeneralUtility;
33
34 /**
35 * List powermail fields in Backend for powermail_cond rules
36 *
37 * @package powermail_cond
38 * @license http://www.gnu.org/licenses/lgpl.html
39 * GNU Lesser General Public License, version 3 or later
40 */
41 class GetPowermailFields
42 {
43
44 /**
45 * @var \TYPO3\CMS\Core\Database\DatabaseConnection
46 */
47 protected $databaseConnection = null;
48
49 /**
50 * @var array
51 */
52 protected $params = [];
53
54 /**
55 * @var int
56 */
57 protected $formUid = 0;
58
59 /**
60 * @var array
61 */
62 protected $defaultFieldTypes = [
63 'input',
64 'textarea',
65 'select',
66 'radio',
67 'check'
68 ];
69
70 /**
71 * show all fields in the backend
72 *
73 * @param array $params
74 * @return void
75 */
76 public function getFields(array &$params)
77 {
78 $this->initialize($params);
79 $this->addFieldsToParams();
80 $this->addFieldsetsToParams();
81 }
82
83 /**
84 * Add fields to params array
85 *
86 * @return void
87 */
88 protected function addFieldsToParams()
89 {
90 $fields = $this->getFieldsFromForm();
91 $this->params['items'][] = [
92 'powermail Fields',
93 '--div--'
94 ];
95 foreach ($fields as $properties) {
96 $this->params['items'][] = [
97 $this->getLabelFromFieldProperties($properties),
98 $properties['uid']
99 ];
100 }
101 }
102
103 /**
104 * Add fieldsets to Params
105 *
106 * @return void
107 */
108 protected function addFieldsetsToParams()
109 {
110 if (!empty($this->params['config']['itemsProcFunc_addFieldsets'])) {
111 $fieldsets = $this->getFieldsetsFromForm();
112 $this->params['items'][] = [
113 'powermail Fieldsets',
114 '--div--'
115 ];
116 foreach ($fieldsets as $properties) {
117 $this->params['items'][] = [
118 $properties['title'] . ' (' . $properties['uid'] . ')',
119 'fieldset:' . $properties['uid']
120 ];
121 }
122 }
123 }
124
125 /**
126 * Get fields
127 *
128 * @return array
129 */
130 protected function getFieldsFromForm()
131 {
132 $fields = [];
133 $select = 'f.uid, f.title, f.marker';
134 $from = 'tx_powermail_domain_model_fields f ' .
135 'left join tx_powermail_domain_model_pages p on f.pages = p.uid ' .
136 'left join tx_powermail_domain_model_forms fo on p.forms = fo.uid';
137 $where = 'f.hidden = 0 and f.deleted = 0 and f.type in (' . $this->getDefaultFieldTypesForQuery() . ')';
138 if ($this->getFormUid() > 0) {
139 $where .= ' and fo.uid = ' . $this->getFormUid();
140 }
141 $groupBy = '';
142 $orderBy = 'f.sorting';
143 $limit = 10000;
144 $res = $this->databaseConnection->exec_SELECTquery($select, $from, $where, $groupBy, $orderBy, $limit);
145 if ($res) {
146 while (($row = $this->databaseConnection->sql_fetch_assoc($res))) {
147 $fields[] = $row;
148 }
149 }
150 return $fields;
151 }
152
153 /**
154 * give me all fieldsets in an array
155 *
156 * @return array
157 */
158 protected function getFieldsetsFromForm()
159 {
160 $fieldsets = [];
161 $select = 'uid, title';
162 $from = 'tx_powermail_domain_model_pages';
163 $where = 'forms = ' . $this->getFormUid() . ' AND hidden = 0 AND deleted = 0';
164 $groupBy = '';
165 $orderBy = 'sorting';
166 $limit = '';
167 $res = $this->databaseConnection->exec_SELECTquery($select, $from, $where, $groupBy, $orderBy, $limit);
168 if ($res) {
169 while (($row = $this->databaseConnection->sql_fetch_assoc($res))) {
170 $fieldsets[] = $row;
171 }
172 }
173 return $fieldsets;
174 }
175
176 /**
177 * Get Form Uid from Rule
178 *
179 * @param int $conditionUid
180 * @return int formUid
181 */
182 protected function getFormUidFromCondition($conditionUid)
183 {
184 $select = 'cc.form';
185 $from = 'tx_powermailcond_domain_model_conditioncontainer cc ' .
186 'left join tx_powermailcond_domain_model_condition c on cc.uid = c.conditioncontainer';
187 $where = 'c.uid = ' . (int) $conditionUid . ' AND c.hidden = 0 AND c.deleted = 0';
188 $groupBy = '';
189 $orderBy = '';
190 $limit = 1;
191 $res = $this->databaseConnection->exec_SELECTquery($select, $from, $where, $groupBy, $orderBy, $limit);
192 if ($res) {
193 $row = $this->databaseConnection->sql_fetch_assoc($res);
194 return (int) $row['form'];
195 }
196 return 0;
197 }
198
199 /**
200 * Get Form Uid from Condition Container
201 *
202 * @param int $conditionContainerUid
203 * @return int formUid
204 */
205 protected function getFormUidFromConditionContainer($conditionContainerUid)
206 {
207 $select = 'cc.form';
208 $from = 'tx_powermailcond_domain_model_conditioncontainer cc';
209 $where = 'cc.uid = ' . (int) $conditionContainerUid . ' AND cc.deleted = 0';
210 $groupBy = '';
211 $orderBy = '';
212 $limit = 1;
213 $res = $this->databaseConnection->exec_SELECTquery($select, $from, $where, $groupBy, $orderBy, $limit);
214 if ($res) {
215 $row = $this->databaseConnection->sql_fetch_assoc($res);
216 return (int) $row['form'];
217 }
218 return 0;
219 }
220
221 /**
222 * Build a label
223 *
224 * @param array $properties
225 * @return string
226 */
227 protected function getLabelFromFieldProperties(array $properties)
228 {
229 return $properties['title'] . ', {' . $properties['marker'] . '}, uid' . $properties['uid'];
230 }
231
232 /**
233 * @param array $params
234 * @return void
235 */
236 protected function initialize(array &$params)
237 {
238 $this->databaseConnection = $GLOBALS['TYPO3_DB'];
239 $this->params = &$params;
240 $this->setFormUid()->setDefaultFieldTypes();
241 }
242
243 /**
244 * @return GetPowermailFields
245 */
246 public function setFormUid()
247 {
248 $formUid = (int) $this->params['row']['form'];
249 if ($formUid === 0) {
250 $formUid = $this->getFormUidFromConditionContainer((int) $this->params['row']['conditioncontainer']);
251 }
252 if (!empty($this->params['row']['conditions'])) {
253 $formUid = $this->getFormUidFromCondition($this->params['row']['conditions']);
254 }
255 $this->formUid = $formUid;
256 return $this;
257 }
258
259 /**
260 * @return int
261 */
262 public function getFormUid()
263 {
264 return $this->formUid;
265 }
266
267 /**
268 * @return GetPowermailFields
269 */
270 public function setDefaultFieldTypes()
271 {
272 if (!empty($this->params['config']['itemsProcFuncValue'])) {
273 $this->defaultFieldTypes =
274 GeneralUtility::trimExplode(',', $this->params['config']['itemsProcFuncValue'], true);
275 }
276 return $this;
277 }
278
279 /**
280 * @return array
281 */
282 public function getDefaultFieldTypes()
283 {
284 return $this->defaultFieldTypes;
285 }
286
287 /**
288 * @return string
289 */
290 public function getDefaultFieldTypesForQuery()
291 {
292 $fieldTypes = $this->getDefaultFieldTypes();
293 return ArrayUtility::getQuotedList($fieldTypes);
294 }
295 }