[FEATURE] Support for Powermail > 3.0
[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\Powermail\Domain\Model\Field;
31 use In2code\Powermail\Domain\Model\Form;
32 use In2code\Powermail\Domain\Model\Page;
33 use In2code\PowermailCond\Utility\ArrayUtility;
34 use TYPO3\CMS\Backend\Form\FormEngine;
35 use TYPO3\CMS\Core\Utility\GeneralUtility;
36
37 /**
38 * List powermail fields in Backend for powermail_cond rules
39 *
40 * @package powermail_cond
41 * @license http://www.gnu.org/licenses/lgpl.html
42 * GNU Lesser General Public License, version 3 or later
43 */
44 class GetPowermailFields
45 {
46
47 /**
48 * @var \TYPO3\CMS\Core\Database\DatabaseConnection
49 */
50 protected $databaseConnection = null;
51
52 /**
53 * @var array
54 */
55 protected $params = [];
56
57 /**
58 * @var int
59 */
60 protected $formUid = 0;
61
62 /**
63 * @var array
64 */
65 protected $defaultFieldTypes = [
66 'input',
67 'textarea',
68 'select',
69 'radio',
70 'check'
71 ];
72
73 /**
74 * show all fields in the backend
75 *
76 * @param array $params
77 * @return void
78 */
79 public function getFields(array &$params)
80 {
81 $this->initialize($params);
82 $this->addFieldsToParams();
83 $this->addFieldsetsToParams();
84 }
85
86 /**
87 * Add fields to params array
88 *
89 * @return void
90 */
91 protected function addFieldsToParams()
92 {
93 $fields = $this->getFieldsFromForm();
94 $this->params['items'][] = [
95 'powermail Fields',
96 '--div--'
97 ];
98 foreach ($fields as $properties) {
99 $this->params['items'][] = [
100 $this->getLabelFromFieldProperties($properties),
101 $properties['uid']
102 ];
103 }
104 }
105
106 /**
107 * Add fieldsets to Params
108 *
109 * @return void
110 */
111 protected function addFieldsetsToParams()
112 {
113 if (!empty($this->params['config']['itemsProcFunc_addFieldsets'])) {
114 $fieldsets = $this->getFieldsetsFromForm();
115 $this->params['items'][] = [
116 'powermail Fieldsets',
117 '--div--'
118 ];
119 foreach ($fieldsets as $properties) {
120 $this->params['items'][] = [
121 $properties['title'] . ' (' . $properties['uid'] . ')',
122 'fieldset:' . $properties['uid']
123 ];
124 }
125 }
126 }
127
128 /**
129 * Get fields
130 *
131 * @return array
132 */
133 protected function getFieldsFromForm()
134 {
135 $fields = [];
136 $select = 'f.uid, f.title, f.marker';
137 $from = Field::TABLE_NAME . ' f ' .
138 'left join ' . Page::TABLE_NAME . ' p on f.pages = p.uid ' .
139 'left join ' . Form::TABLE_NAME . ' fo on p.forms = fo.uid';
140 $where = 'f.hidden = 0 and f.deleted = 0 and f.type in (' . $this->getDefaultFieldTypesForQuery() . ')';
141 if ($this->getFormUid() > 0) {
142 $where .= ' and fo.uid = ' . $this->getFormUid();
143 }
144 $groupBy = '';
145 $orderBy = 'f.sorting';
146 $limit = 10000;
147 $res = $this->databaseConnection->exec_SELECTquery($select, $from, $where, $groupBy, $orderBy, $limit);
148 if ($res) {
149 while (($row = $this->databaseConnection->sql_fetch_assoc($res))) {
150 $fields[] = $row;
151 }
152 }
153 return $fields;
154 }
155
156 /**
157 * give me all fieldsets in an array
158 *
159 * @return array
160 */
161 protected function getFieldsetsFromForm()
162 {
163 $fieldsets = [];
164 $select = 'uid, title';
165 $from = Page::TABLE_NAME;
166 $where = 'forms = ' . $this->getFormUid() . ' AND hidden = 0 AND deleted = 0';
167 $groupBy = '';
168 $orderBy = 'sorting';
169 $limit = '';
170 $res = $this->databaseConnection->exec_SELECTquery($select, $from, $where, $groupBy, $orderBy, $limit);
171 if ($res) {
172 while (($row = $this->databaseConnection->sql_fetch_assoc($res))) {
173 $fieldsets[] = $row;
174 }
175 }
176 return $fieldsets;
177 }
178
179 /**
180 * Get Form Uid from Rule
181 *
182 * @param int $conditionUid
183 * @return int formUid
184 */
185 protected function getFormUidFromCondition($conditionUid)
186 {
187 $select = 'cc.form';
188 $from = 'tx_powermailcond_domain_model_conditioncontainer cc ' .
189 'left join tx_powermailcond_domain_model_condition c on cc.uid = c.conditioncontainer';
190 $where = 'c.uid = ' . (int) $conditionUid . ' AND c.hidden = 0 AND c.deleted = 0';
191 $groupBy = '';
192 $orderBy = '';
193 $limit = 1;
194 $res = $this->databaseConnection->exec_SELECTquery($select, $from, $where, $groupBy, $orderBy, $limit);
195 if ($res) {
196 $row = $this->databaseConnection->sql_fetch_assoc($res);
197 return (int) $row['form'];
198 }
199 return 0;
200 }
201
202 /**
203 * Get Form Uid from Condition Container
204 *
205 * @param int $conditionContainerUid
206 * @return int formUid
207 */
208 protected function getFormUidFromConditionContainer($conditionContainerUid)
209 {
210 $select = 'cc.form';
211 $from = 'tx_powermailcond_domain_model_conditioncontainer cc';
212 $where = 'cc.uid = ' . (int) $conditionContainerUid . ' AND cc.deleted = 0';
213 $groupBy = '';
214 $orderBy = '';
215 $limit = 1;
216 $res = $this->databaseConnection->exec_SELECTquery($select, $from, $where, $groupBy, $orderBy, $limit);
217 if ($res) {
218 $row = $this->databaseConnection->sql_fetch_assoc($res);
219 return (int) $row['form'];
220 }
221 return 0;
222 }
223
224 /**
225 * Build a label
226 *
227 * @param array $properties
228 * @return string
229 */
230 protected function getLabelFromFieldProperties(array $properties)
231 {
232 return $properties['title'] . ', {' . $properties['marker'] . '}, uid' . $properties['uid'];
233 }
234
235 /**
236 * @param array $params
237 * @return void
238 */
239 protected function initialize(array &$params)
240 {
241 $this->databaseConnection = $GLOBALS['TYPO3_DB'];
242 $this->params = &$params;
243 $this->setFormUid()->setDefaultFieldTypes();
244 }
245
246 /**
247 * @return GetPowermailFields
248 */
249 public function setFormUid()
250 {
251 $formUid = (int) $this->params['row']['form'];
252 if ($formUid === 0) {
253 $formUid = $this->getFormUidFromConditionContainer((int) $this->params['row']['conditioncontainer']);
254 }
255 if (!empty($this->params['row']['conditions'])) {
256 $formUid = $this->getFormUidFromCondition($this->params['row']['conditions']);
257 }
258 $this->formUid = $formUid;
259 return $this;
260 }
261
262 /**
263 * @return int
264 */
265 public function getFormUid()
266 {
267 return $this->formUid;
268 }
269
270 /**
271 * @return GetPowermailFields
272 */
273 public function setDefaultFieldTypes()
274 {
275 if (!empty($this->params['config']['itemsProcFuncValue'])) {
276 $this->defaultFieldTypes =
277 GeneralUtility::trimExplode(',', $this->params['config']['itemsProcFuncValue'], true);
278 }
279 return $this;
280 }
281
282 /**
283 * @return array
284 */
285 public function getDefaultFieldTypes()
286 {
287 return $this->defaultFieldTypes;
288 }
289
290 /**
291 * @return string
292 */
293 public function getDefaultFieldTypesForQuery()
294 {
295 $fieldTypes = $this->getDefaultFieldTypes();
296 return ArrayUtility::getQuotedList($fieldTypes);
297 }
298 }