TYPO3 CMS  TYPO3_6-2
ElementConditionMatcher.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Backend\Form;
3 
18 
25 
29  protected $flexformValueKey = '';
30 
34  protected $record = array();
35 
49  public function match($displayCondition, array $record = array(), $flexformValueKey = '', $recursionLevel = 0) {
50  if ($recursionLevel > 99) {
51  // This should not happen, treat as misconfiguration
52  return TRUE;
53  }
54  if (!is_array($displayCondition)) {
55  // DisplayCondition is not an array - just get its value
56  $result = $this->matchSingle($displayCondition, $record, $flexformValueKey);
57  } else {
58  // Multiple conditions given as array ('AND|OR' => condition array)
59  $conditionEvaluations = array(
60  'AND' => array(),
61  'OR' => array(),
62  );
63  foreach ($displayCondition as $logicalOperator => $groupedDisplayConditions) {
64  $logicalOperator = strtoupper($logicalOperator);
65  if (($logicalOperator !== 'AND' && $logicalOperator !== 'OR') || !is_array($groupedDisplayConditions)) {
66  // Invalid line. Skip it.
67  continue;
68  } else {
69  foreach ($groupedDisplayConditions as $key => $singleDisplayCondition) {
70  $key = strtoupper($key);
71  if (($key === 'AND' || $key === 'OR') && is_array($singleDisplayCondition)) {
72  // Recursion statement: condition is 'AND' or 'OR' and is pointing to an array (should be conditions again)
73  $conditionEvaluations[$logicalOperator][] = $this->match(
74  array($key => $singleDisplayCondition),
75  $record,
77  $recursionLevel + 1
78  );
79  } else {
80  // Condition statement: collect evaluation of this single condition.
81  $conditionEvaluations[$logicalOperator][] = $this->matchSingle(
82  $singleDisplayCondition,
83  $record,
85  );
86  }
87  }
88  }
89  }
90  if (count($conditionEvaluations['OR']) > 0 && in_array(TRUE, $conditionEvaluations['OR'], TRUE)) {
91  // There are OR conditions and at least one of them is TRUE
92  $result = TRUE;
93  } elseif (count($conditionEvaluations['AND']) > 0 && !in_array(FALSE, $conditionEvaluations['AND'], TRUE)) {
94  // There are AND conditions and none of them is FALSE
95  $result = TRUE;
96  } elseif (count($conditionEvaluations['OR']) > 0 || count($conditionEvaluations['AND']) > 0) {
97  // There are some conditions. But no OR was TRUE and at least one AND was FALSE
98  $result = FALSE;
99  } else {
100  // There are no proper conditions - misconfiguration. Return TRUE.
101  $result = TRUE;
102  }
103  }
104  return $result;
105  }
106 
120  protected function matchSingle($displayCondition, array $record = array(), $flexformValueKey = '') {
121  $this->record = $record;
122  $this->flexformValueKey = $flexformValueKey;
123  $result = FALSE;
124  list($matchType, $condition) = explode(':', $displayCondition, 2);
125  switch ($matchType) {
126  case 'EXT':
127  $result = $this->matchExtensionCondition($condition);
128  break;
129  case 'FIELD':
130  $result = $this->matchFieldCondition($condition);
131  break;
132  case 'HIDE_FOR_NON_ADMINS':
134  break;
135  case 'HIDE_L10N_SIBLINGS':
136  $result = $this->matchHideL10nSiblingsCondition($condition);
137  break;
138  case 'REC':
139  $result = $this->matchRecordCondition($condition);
140  break;
141  case 'VERSION':
142  $result = $this->matchVersionCondition($condition);
143  break;
144  }
145  return $result;
146  }
147 
157  protected function matchExtensionCondition($condition) {
158  $result = FALSE;
159  list($extensionKey, $operator, $operand) = explode(':', $condition, 3);
160  if ($operator === 'LOADED') {
161  if (strtoupper($operand) === 'TRUE') {
163  } elseif (strtoupper($operand) === 'FALSE') {
165  }
166  }
167  return $result;
168  }
169 
180  protected function matchFieldCondition($condition) {
181  list($fieldName, $operator, $operand) = explode(':', $condition, 3);
182  if ($this->flexformValueKey) {
183  if (strpos($fieldName, 'parentRec.') !== FALSE) {
184  $fieldNameParts = explode('.', $fieldName, 2);
185  $fieldValue = $this->record['parentRec'][$fieldNameParts[1]];
186  } else {
187  $fieldValue = $this->record[$fieldName][$this->flexformValueKey];
188  }
189  } else {
190  $fieldValue = $this->record[$fieldName];
191  }
192 
193  $result = FALSE;
194  switch ($operator) {
195  case 'REQ':
196  if (strtoupper($operand) === 'TRUE') {
197  $result = (bool) $fieldValue;
198  } else {
199  $result = !$fieldValue;
200  }
201  break;
202  case '>':
203  $result = $fieldValue > $operand;
204  break;
205  case '<':
206  $result = $fieldValue < $operand;
207  break;
208  case '>=':
209  $result = $fieldValue >= $operand;
210  break;
211  case '<=':
212  $result = $fieldValue <= $operand;
213  break;
214  case '-':
215  case '!-':
216  list($minimum, $maximum) = explode('-', $operand);
217  $result = $fieldValue >= $minimum && $fieldValue <= $maximum;
218  if ($operator[0] === '!') {
219  $result = !$result;
220  }
221  break;
222  case 'IN':
223  case '!IN':
224  case '=':
225  case '!=':
227  if ($operator[0] === '!') {
228  $result = !$result;
229  }
230  break;
231  case 'BIT':
232  case '!BIT':
233  $result = ((int)$fieldValue & $operand) ? TRUE : FALSE;
234  if ($operator[0] === '!') {
235  $result = !$result;
236  }
237  break;
238  }
239  return $result;
240  }
241 
247  protected function matchHideForNonAdminsCondition() {
248  return (bool) $this->getBackendUser()->isAdmin();
249  }
250 
258  protected function matchHideL10nSiblingsCondition($condition) {
259  $result = FALSE;
260  if ($this->flexformValueKey === 'vDEF') {
261  $result = TRUE;
262  } elseif ($condition === 'except_admin' && $this->getBackendUser()->isAdmin()) {
263  $result = TRUE;
264  }
265  return $result;
266  }
267 
278  protected function matchRecordCondition($condition) {
279  $result = FALSE;
280  list($operator, $operand) = explode(':', $condition, 2);
281  if ($operator === 'NEW') {
282  if (strtoupper($operand) === 'TRUE') {
283  $result = !((int)$this->record['uid'] > 0);
284  } elseif (strtoupper($operand) === 'FALSE') {
285  $result = ((int)$this->record['uid'] > 0);
286  }
287  }
288  return $result;
289  }
290 
298  protected function matchVersionCondition($condition) {
299  $result = FALSE;
300  list($operator, $operand) = explode(':', $condition, 2);
301  if ($operator === 'IS') {
302  $isNewRecord = !((int)$this->record['uid'] > 0);
303  // Detection of version can be done be detecting the workspace of the user
304  $isUserInWorkspace = $this->getBackendUser()->workspace > 0;
305  if ((int)$this->record['pid'] === -1 || (int)$this->record['_ORIG_pid'] === -1) {
306  $isRecordDetectedAsVersion = TRUE;
307  } else {
308  $isRecordDetectedAsVersion = FALSE;
309  }
310  // New records in a workspace are not handled as a version record
311  // if it's no new version, we detect versions like this:
312  // -- if user is in workspace: always TRUE
313  // -- if editor is in live ws: only TRUE if pid == -1
314  $isVersion = ($isUserInWorkspace || $isRecordDetectedAsVersion) && !$isNewRecord;
315  if (strtoupper($operand) === 'TRUE') {
316  $result = $isVersion;
317  } elseif (strtoupper($operand) === 'FALSE') {
318  $result = !$isVersion;
319  }
320  }
321  return $result;
322  }
323 
329  protected function getBackendUser() {
330  return $GLOBALS['BE_USER'];
331  }
332 }
match($displayCondition, array $record=array(), $flexformValueKey='', $recursionLevel=0)
matchSingle($displayCondition, array $record=array(), $flexformValueKey='')
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]