Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_condition_link_exists.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
19 
33 {
34 
35 
60  public static function evaluate($settings, &$state)
61  {
62  $am =& $GLOBALS['SQ_SYSTEM']->am;
63 
64  $assetid = $state['assetid']; // event broadcaster
65 
66  $link_type = array_get_index($settings, 'link_type');
67  if (empty($link_type)) $link_type = SQ_SC_LINK_ALL;
68  $is_parent = array_get_index($settings, 'is_major', FALSE);
69  $link_side = $is_parent ? 'major' : 'minor';
70  $value = array_get_index($settings, 'value');
71  $dependant = array_get_index($settings, 'is_dependant');
72  $exclusive = array_get_index($settings, 'is_exclusive');
73 
74  $selector_type = array_get_index($settings, 'selector_type', FALSE);
75 
76  if (!$selector_type) return FALSE;
77 
78  // second array_get_index is for backwards compatibility. it should be removed sometime in the future
79  $use_current_link = array_get_index($settings, 'use_current_link', array_get_index($settings, 'use_new_link', FALSE));
80 
81  // extract link data in case this is a link event
82  $current_link = array_get_index($state['event']['data'], 'link_info');
83 
84  // check if we only want to work with a newly created link
85  if ($use_current_link) {
86  if ($state['event']['name'] != 'trigger_event_link_created' && $state['event']['name'] != 'trigger_event_link_deleted' && is_null($current_link)) {
87  // incorrect event for this option
88  return FALSE;
89  }
90  }
91 
92  switch ($selector_type) {
93  case 'asset_type':
94  $selected_type = array_get_index($settings, 'asset_type');
95  if (empty($selected_type)) return FALSE;
96  $links = $am->getLinks($assetid, $link_type, $selected_type, TRUE, $link_side, $value, $dependant, $exclusive);
97 
98  break;
99 
100  case 'asset_id':
101  $selected_id = array_get_index($settings, 'asset_id');
102  if (!isset($selected_id)) return FALSE;
103 
104  if (empty($link_type)) $link_type = NULL;
105 
106  // do the request
107  $links = $am->getLinkByAsset($assetid, $selected_id, $link_type, $value, $link_side, TRUE, $dependant, $exclusive);
108 
109  break;
110 
111  case 'only_link_info':
112  // only link info (without parent/child asset data) can only be used for link events (where link data is already known)
113  if (!$use_current_link) {
114  return FALSE;
115  } else if (!is_null($value) && ($value != $current_link['value'])) {
116  return FALSE;
117  } else if (!is_null($dependant) && ($dependant != $current_link['is_dependant'])) {
118  return FALSE;
119  } else if (!is_null($exclusive) && ($exclusive != $current_link['is_exclusive'])) {
120  return FALSE;
121  } else if (!is_null($link_type) && !($link_type | $current_link['link_type'])) {
122  return FALSE;
123  } else if ($assetid != $current_link[$link_side.'id']) {
124  return FALSE;
125  } else {
126  return TRUE;
127  }
128  break;
129 
130  default:
131  return FALSE;
132  }//end switch $selector_type
133 
134 
135  // we rely on the fact that the links that we got already satisfy the criteria
136  if (empty($links)) return FALSE;
137  // now do the final test to see if we are looking for a particular link ID
138  if ($use_current_link) {
139  foreach ($links as $link) {
140  if ($current_link['linkid'] == $link['linkid']) {
141  return TRUE;
142  }
143  }
144  return FALSE;
145  }
146 
147  return TRUE;
148 
149  }//end evaluate()
150 
151 
162  public static function getInterface($settings, $prefix, $write_access=FALSE)
163  {
164  // check settings, set defaults if necessary
165  $is_major = array_get_index($settings, 'is_major', FALSE);
166  $selected_link_type = array_get_index($settings, 'link_type');
167  $value = array_get_index($settings, 'value');
168  $is_dependant = array_get_index($settings, 'is_dependant');
169  $is_exclusive = array_get_index($settings, 'is_exclusive');
170 
171  $selector_type = array_get_index($settings, 'selector_type', 'asset_type');
172  $asset_id = array_get_index($settings, 'asset_id');
173  $asset_type = array_get_index($settings, 'asset_type');
174 
175  // event-specific
176  // second array_get_index is for backwards compatibility. it should be removed sometime in the future
177  $use_current_link = array_get_index($settings, 'use_current_link', array_get_index($settings, 'use_new_link', FALSE));
178 
179  $is_major_list = Array(
180  0 => translate('parent'),
181  1 => translate('child'),
182  );
183 
184  $link_type_list = Array('' => '--- '.translate('any').' ---');
185  $link_type_list = $link_type_list + get_link_type_names();
186 
187  $dependant_options = Array(
188  '' => '--- '.translate('any').' ---',
189  '1' => translate('yes'),
190  '0' => translate('no'),
191  );
192 
193  $exclusive_options = Array(
194  '' => '--- '.translate('any').' ---',
195  '1' => translate('yes'),
196  '0' => translate('no'),
197  );
198 
199  if (!$write_access) {
200  $form_element_extras = 'disabled="disabled"';
201  } else {
202  $form_element_extras = '';
203  }
204 
205  // begin bufferning basic options
206  ob_start();
207  combo_box($prefix.'[is_major]', $is_major_list, FALSE, $is_major, NULL, $form_element_extras);
208  $major_selector = ob_get_clean();
209 
210  ob_start();
211  radio_button($prefix.'[selector_type]', 'asset_type', $selector_type == 'asset_type', NULL, $form_element_extras);
212  $asset_type_option = ob_get_clean();
213 
214  ob_start();
215  if ($write_access) {
216  asset_type_chooser($prefix.'[asset_type]', FALSE, $asset_type);
217  } else {
218  if ($asset_type) {
219  echo $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($asset_type, 'name');
220  }
221  }
222  $asset_type_selector = ob_get_clean();
223 
224  ob_start();
225  radio_button($prefix.'[selector_type]', 'asset_id', $selector_type == 'asset_id', NULL, $form_element_extras);
226  $asset_id_option = ob_get_clean();
227 
228  ob_start();
229  if ($write_access) {
230  asset_finder($prefix.'[asset_id]', $asset_id);
231  } else {
232  if ($asset_id) {
233  $asset_name = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($asset_id), Array(), TRUE, 'name');
234  text_box($prefix.'[assetid]', $asset_name[$asset_id].' (#'.$asset_id.')', '', '', FALSE, $form_element_extras);
235  } else {
236  text_box($prefix.'[assetid]', '', '', '', FALSE, $form_element_extras);
237  }
238  }
239  $asset_id_selector = ob_get_clean();
240 
241  ob_start();
242  radio_button($prefix.'[selector_type]', 'only_link_info', $selector_type == 'only_link_info', NULL, $form_element_extras);
243  $only_link_option = ob_get_clean();
244 
245  ob_start();
246  check_box($prefix.'[use_current_link]', '1', $use_current_link, NULL, $form_element_extras, NULL, $form_element_extras);
247  label(translate('match_current_link_only'), $prefix.'[use_current_link]');
248  $current_link_enabler = ob_get_clean();
249 
250 
251  // begin buffering advanced options
252  ob_start();
253  combo_box($prefix.'[link_type]', $link_type_list, FALSE, $selected_link_type, NULL, $form_element_extras);
254  $link_type_selector = ob_get_clean();
255 
256  ob_start();
257  combo_box($prefix.'[is_dependant]', $dependant_options, FALSE, $is_dependant, NULL, $form_element_extras);
258  $dependent_selector = ob_get_clean();
259 
260  ob_start();
261  combo_box($prefix.'[is_exclusive]', $exclusive_options, FALSE, $is_exclusive, NULL, $form_element_extras);
262  $exclusive_selector = ob_get_clean();
263 
264  ob_start();
265  text_box($prefix.'[value]', $value, 60, NULL, FALSE, $form_element_extras);
266  $link_value = ob_get_clean();
267 
268 
269  $contents = translate('trigger_link_exists_basic', $major_selector, $asset_type_option, $asset_type_selector, $asset_id_option, $asset_id_selector, $only_link_option, $current_link_enabler).
270  '<hr />'.
271  '<b>'.translate('triggers_advanced_user_options').'</b><br />'.
272  translate('trigger_link_exists_advanced', $link_type_selector, $dependent_selector, $exclusive_selector, $link_value);
273 
274  return $contents;
275 
276  }//end getInterface()
277 
278 
288  public static function processInterface(&$settings, $request_data)
289  {
290  $settings['is_major'] = !empty($request_data['is_major']);
291 
292  $settings['selector_type'] = array_get_index($request_data, 'selector_type');
293  switch ($settings['selector_type']) {
294  case 'asset_type':
295  $settings['asset_type'] = array_get_index($request_data, 'asset_type');
296  if (is_null($settings['asset_type']) || empty($settings['asset_type'])) {
297  return 'Invalid asset type selected. This condition will always fail.';
298  }
299  break;
300 
301  case 'asset_id':
302  $id_selector = array_get_index($request_data, 'asset_id');
303  $settings['asset_id'] = array_get_index($id_selector, 'assetid');
304 
305  if (empty($settings['asset_id'])) {
306  return 'Asset id is not specified.';
307  }
308  break;
309 
310  case 'only_link_info':
311  // nothing here at the moment
312  // we should check if any advanced data is supplied
313  // otherwise the condition is too generic and almost always return true
314  // because all assets have links
315  // give them a warning if condition is too broad
316  break;
317 
318  default:
319  return 'You have to choose the appropriate asset ID or asset type selector';
320  }
321 
322  $link_type = array_get_index($request_data, 'link_type');
323  $settings['link_type'] = is_null($link_type) ? SQ_SC_LINK_ALL : $link_type;
324  $value = trim(array_get_index($request_data, 'value'));
325  if (empty($value)) {
326  $settings['value'] = NULL;
327  } else {
328  $settings['value'] = $value;
329  }
330 
331  $settings['is_dependant'] = array_get_index($request_data, 'is_dependant');
332  $settings['is_exclusive'] = array_get_index($request_data, 'is_exclusive');
333  $settings['use_current_link'] = isset($request_data['use_current_link']);
334 
335  foreach ($settings as $key => $value) {
336  if ($value === '') $settings[$key] = NULL;
337  }
338  return FALSE;
339 
340  }//end processInterface()
341 
342 
353  public static function setHash(&$settings, &$hash)
354  {
355  if (isset($settings['selector_type']) && isset($settings['is_major'])) {
356  switch ($settings['selector_type']) {
357  case 'asset_type':
358  if (isset($settings['asset_type'])) {
359  $hash->setLinkedType($settings['asset_type'], $settings['is_major']);
360  }
361  break;
362 
363  case 'asset_id':
364  if (isset($settings['asset_id'])) {
365  $hash->setLinkedID($settings['asset_id'], $settings['is_major']);
366  }
367  break;
368  }
369  }
370 
371  return FALSE;
372 
373  }//end setHash()
374 
375 
383  public static function allowMultiple()
384  {
385  return TRUE;
386 
387  }//end allowMultiple()
388 
389 
390 }//end class
391 
392 ?>