Working Copy
[TYPO3CMS/Extensions/powermailCond.git] / lib / class.tx_powermailcond_ajaxFieldList_eid.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 Alexander Kellner <alexander.kellner@in2code.de>, in2code.
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 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 require_once(PATH_t3lib . 'class.t3lib_befunc.php');
26 require_once(PATH_t3lib . 'stddb/tables.php');
27 require_once(t3lib_extMgm::extPath('cms', 'ext_tables.php'));
28 require_once(PATH_tslib . 'class.tslib_pibase.php');
29 require_once(PATH_tslib . 'class.tslib_fe.php');
30 require_once(PATH_t3lib . 'class.t3lib_page.php');
31 require_once(t3lib_extMgm::extPath('powermail_cond') . 'lib/class.tx_powermailcond_div.php'); // load div class
32
33 /**
34 * This class tells jQuery which field are allowed in which are not allowed
35 *
36 * @author Alex Kellner <alexander.kellner@in2code.de>, in2code.
37 * @package TYPO3
38 * @subpackage tx_powermailcond_ajaxWriteInSession_eid
39 */
40 class tx_powermailcond_ajaxFieldList_eid extends tslib_pibase {
41
42 public $extKey = 'powermail_cond'; // Extension key
43 public $prefixId = 'tx_powermailcond_pi1';
44 public $piVars;
45 private $content = '';
46
47 /**
48 * Return list with fields which are not allowed (should be hidden)
49 *
50 * @return string $content: commaseparated field list (1,2,3)
51 * complex list could be:
52 * filter:12:option1;option2,12,13,fieldset:2:18;19
53 * explanation - filter select.uid12, hide uid12, hide uid3, hide fieldset uid2 and clear uid18 and uid19
54 */
55 public function main() {
56 $this->cObj = $this->getCObj(); // enable TSFE globals
57 $conditions = $this->getConditionsFromThisPage(); // get conditions from database
58 $targetFields = $this->div->getStartFields($conditions); // get all startfields in an array
59 if ($this->piVars['uid'] > 0 && !in_array($this->piVars['uid'], $targetFields)) { // if current field uid given and this
60 return 'nochange';
61 }
62
63 foreach ((array) $conditions as $targetUid => $conf) { // one loop for every single target field
64 $content .= $this->checkRules($conf) . ','; // add list to content object
65 }
66
67 $content = t3lib_div::rm_endcomma($content); // remove last ,
68 $content = t3lib_div::uniqueList($content); // remove double values
69
70 return $content;
71 }
72
73 /**
74 * Preflight function checks the rules if there should be an action (show/hide) or not
75 *
76 * @param array $conf: Configuration of current field
77 * @return boolean true:hide false:show(nothing)
78 */
79 public function checkRules($conf) {
80 $content = '';
81 $do = 0; // start with 0
82 if ($conf[0]['conjunction'] == 'AND') {
83 $do = 1; // start with 1
84 }
85
86 foreach ((array) $conf as $key => $subconf) { // one loop for every rule of current target field
87 $ttcontentUid = $this->div->getTtcontentUid($conf[$key]['startField']);
88
89 // special case: hide a field from the beginning
90 if ($conf[$key]['actions'] == 1) { // show
91 $content .= $this->getFieldsFromFieldset($conf[$key]['targetField'], $ttcontentUid) . ',';
92 }
93
94 // operations
95 $act = 0;
96 $startFieldSession = $GLOBALS['TSFE']->fe_user->sesData['powermail_' . $ttcontentUid]['uid' . $conf[$key]['startField']]; // get startfield value from session
97 if (is_array($startFieldSession)) { // if second level
98 $startFieldSession = implode(',', $startFieldSession); // get all values in a commaseparated list
99 }
100 switch ($conf[$key]['ops']) {
101 case 0: // "not empty"
102 if ($startFieldSession != '') { // if start field value in session is not empty
103 $act = 1;
104 $tmp_conf = $conf[$key];
105 }
106 break;
107
108 case 1: // "empty"
109 if ($startFieldSession == '') { // if start field value in session is empty
110 $act = 1;
111 $tmp_conf = $conf[$key];
112 }
113 break;
114
115 case 2: // "contains"
116 if (stristr($startFieldSession, $conf[$key]['condstring'])) { // if start field value in session contains condstring
117 $act = 1;
118 $tmp_conf = $conf[$key];
119 }
120 break;
121
122 case 3: // "not contains"
123 if (!stristr($startFieldSession, $conf[$key]['condstring'])) { // if start field value in session contains not condstring
124 $act = 1;
125 $tmp_conf = $conf[$key];
126 }
127 break;
128
129 case 4: // "is"
130 if ($startFieldSession === $conf[$key]['condstring']) { // if start field value in session === condstring
131 $act = 1;
132 $tmp_conf = $conf[$key];
133 }
134 break;
135
136 case 5: // "is not"
137 if ($startFieldSession !== $conf[$key]['condstring']) { // if start field value in session is not condstring
138 $act = 1;
139 $tmp_conf = $conf[$key];
140 }
141 break;
142
143 case 6: // "is greater than"
144 if (intval($startFieldSession) > intval($conf[$key]['condstring'])) { // if start field value in session is greater than condstring
145 $act = 1;
146 $tmp_conf = $conf[$key];
147 }
148 break;
149
150 case 7: // "is less than"
151 if (intval($startFieldSession) < intval($conf[$key]['condstring'])) { // if start field value in session is greater than condstring
152 $act = 1;
153 $tmp_conf = $conf[$key];
154 }
155 break;
156
157 case 8: // "contains value from field"
158 $comparisonFieldSession = $GLOBALS['TSFE']->fe_user->sesData['powermail_' . $ttcontentUid]['uid' . $conf[$key]['equalField']]; // get comparisonfield value from session
159 if (stristr($comparisonFieldSession, $startFieldSession)) {
160 $act = 1;
161 $tmp_conf = $conf[$key];
162 }
163 break;
164
165 case 9: // "contains not value from field"
166 $comparisonFieldSession = $GLOBALS['TSFE']->fe_user->sesData['powermail_' . $ttcontentUid]['uid' . $conf[$key]['equalField']]; // get comparisonfield value from session
167 if (!stristr($comparisonFieldSession, $startFieldSession)) {
168 $act = 1;
169 $tmp_conf = $conf[$key];
170 }
171 break;
172 }
173
174 $do = $this->setDo(($act ? 1 : 0), $do, $conf[$key]['conjunction']); // $do = 1;
175 }
176
177 $content = $this->doAction($do, $content, $ttcontentUid, $tmp_conf); // add new field if needed
178 return t3lib_div::rm_endcomma($content); // return commaseparated list
179 }
180
181 /**
182 * This function return current field uid if this field should be hidden (and removes value from session)
183 *
184 * @param boolean $do: If something should be done or not
185 * @param string $content: List with all fields which should be hidden
186 * @param integer $ttcontentUid: UID of this tt_content
187 * @param array $conf: Configuration of current field
188 * @return string list
189 */
190 public function doAction($do, $content, $ttcontentUid, $conf) {
191 if (!$do) {
192 return $content;
193 }
194
195 switch ($conf['actions']) {
196 case 0: // hide
197 $this->div->saveInSession(array('uid' . $conf['targetField'] => ''), 'powermail_' . $ttcontentUid); // remove value from session of this field
198 $content .= $this->getFieldsFromFieldset($conf['targetField'], $ttcontentUid, 1) . ','; // hide this field
199 break;
200
201 case 1: // show
202 $content = t3lib_div::rmFromList($this->getFieldsFromFieldset($conf['targetField'], $ttcontentUid), $content); // remove from hidelist (show this field)
203 break;
204
205 case 2: // filter from selectbox
206 $content .= 'filter:' . $conf['targetField'] . ':' . str_replace(',', ';', $conf['filterSelectField']);
207 break;
208 }
209
210 return $content;
211 }
212
213 /**
214 * Set a value to 0 or 1 depending on previous value and on conjunction (AND/OR)
215 *
216 * @param boolean $newStatus: New status
217 * @param boolean $oldStatus: Old status
218 * @param string $conjunction: AND or OR
219 * @return boolean
220 */
221 private function setDo($newStatus, $oldStatus = 0, $conjunction = 'OR') {
222 //t3lib_div::debug(array($newStatus, $oldStatus), $conjunction);
223 if ($conjunction == 'OR') {
224 if ($newStatus || $oldStatus) {
225 return 1;
226 } else {
227 return 0;
228 }
229 } elseif ($conjunction == 'AND') {
230 if ($newStatus && $oldStatus) {
231 return 1;
232 } else {
233 return 0;
234 }
235 }
236 }
237
238 /**
239 * get condition as array from current page
240 *
241 * @return array $arr: Array with all conditions of the current page
242 */
243 private function getConditionsFromThisPage() {
244 $arr = array();
245 $pid = t3lib_div::_GET('id');
246 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery ( // Get all rules to current field
247 'tx_powermailcond_conditions.targetField, tx_powermailcond_conditions.actions, tx_powermailcond_conditions.conjunction, tx_powermailcond_conditions.filterSelectField, tx_powermailcond_rules.startField, tx_powermailcond_rules.ops, tx_powermailcond_rules.condstring, tx_powermailcond_rules.equalField',
248 'tx_powermailcond_conditions LEFT JOIN tx_powermailcond_rules ON tx_powermailcond_conditions.uid = tx_powermailcond_rules.conditions',
249 $where_clause = (intval($pid) ? 'tx_powermailcond_conditions.pid = ' . intval($pid) : '1') . $this->cObj->enableFields('tx_powermailcond_conditions') . $this->cObj->enableFields('tx_powermailcond_rules'),
250 $groupBy = 'tx_powermailcond_rules.uid',
251 $orderBy = '',
252 $limit = '1000'
253 );
254 if ($res) { // If there is a result
255 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) { // One loop for every rule on current page
256 $arr[$row['targetField']][] = $row;
257 }
258
259 return $arr;
260 }
261 }
262
263 /**
264 * Get all fields in a commaseparated list from a fieldset uid
265 *
266 * @param integer $uid: Fieldset UID
267 * @param integer $ttcontentUid: UID of this tt_content
268 * @param boolean $clearSession: Clear cache of each of this fields
269 * @return string $list: Commaseparated List with field uids
270 */
271 private function getFieldsFromFieldset($uid, $ttcontentUid, $clearSession = 0) {
272 if (is_numeric($uid)) { // if this uid don't contains fs (for fs123)
273 return $uid;
274 }
275
276 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery (
277 'tx_powermail_fields.uid',
278 'tx_powermail_fieldsets LEFT JOIN tx_powermail_fields ON tx_powermail_fieldsets.uid = tx_powermail_fields.fieldset',
279 $where = 'tx_powermail_fieldsets.uid = ' . intval(str_replace('fieldset:', '', $uid)),
280 $groupBy = '',
281 $orderBy = '',
282 $limit = '1000'
283 );
284 if ($res) { // If there is a result
285 $uids = '';
286 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) { // One loop for every field
287 $uids .= $row['uid'] . ';';
288 $this->div->saveInSession(array('uid' . $row['uid'] => ''), 'powermail_' . $ttcontentUid); // remove value from session of this field
289 }
290 }
291
292 if (!isset($uids)) {
293 return $uid;
294 }
295
296 return $uid . ':' . substr($uids, 0, -1); // return without last ;
297 }
298
299 /**
300 * Initialize cObj and TSFE Globals
301 *
302 * @return object cObj
303 */
304 private function getCObj() {
305 $this->piVars = t3lib_div::_GET($this->prefixId);
306 $this->div = t3lib_div::makeInstance('tx_powermailcond_div'); // Create new instance for div class
307 $userObj = tslib_eidtools::initFeUser();
308 $temp_TSFEclassName = t3lib_div::makeInstance('tslib_fe');
309 $GLOBALS['TSFE'] = new $temp_TSFEclassName($TYPO3_CONF_VARS, 0, 0, true);
310 $GLOBALS['TSFE']->connectToDB();
311 $GLOBALS['TSFE']->fe_user = $userObj;
312 $GLOBALS['TSFE']->id = t3lib_div::_GET('id');
313 $GLOBALS['TSFE']->determineId();
314 $GLOBALS['TSFE']->getCompressedTCarray();
315 $GLOBALS['TSFE']->initTemplate();
316 $GLOBALS['TSFE']->getConfigArray();
317 $GLOBALS['TSFE']->includeTCA();
318
319 return t3lib_div::makeInstance('tslib_cObj');
320 }
321
322 }
323
324 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/powermail_cond/lib/class.tx_powermailcond_ajaxFieldList_eid.php']) {
325 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/powermail_cond/lib/class.tx_powermailcond_ajaxFieldList_eid.php']);
326 }
327
328 $SOBE = t3lib_div::makeInstance('tx_powermailcond_ajaxFieldList_eid'); // make instance
329 echo $SOBE->main(); // print content
330 ?>