Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_condition_tree_location.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_condition/trigger_condition.inc';
18 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
19 
34 {
35 
36 
55  public static function evaluate($settings, &$state)
56  {
57  if (!empty($state['assetid'])) {
58  $assetid = $state['assetid'];
59  } else {
60  if (!empty($state['asset'])) {
61  $assetid = $state['asset']->id;
62  } else {
63  // can't retrieve, fail
64  return FALSE;
65  }
66  }
67 
68  if (empty($settings['treeids'])) {
69  if (empty($settings['assetids'])) {
70  return FALSE;
71  } else {
72  $settings['treeids'] = Array();
73  foreach ($settings['assetids'] as $tree_asset_id) {
74  if ($GLOBALS['SQ_SYSTEM']->am->assetExists($tree_asset_id)) {
75  $settings['treeids'][$tree_asset_id] = $GLOBALS['SQ_SYSTEM']->am->getAssetTreeids($tree_asset_id);
76  }
77  }
78  }
79  }
80 
81  if (empty($settings['immed_child'])) return FALSE;
82 
83  // grab the data we need to check the condition, if it's not already cached
84  // note that new state is modified and new data is available to other conditions
85  if (empty($state['treeid'])) {
86  // not stored, need to retrieve
87  $state['treeid'] = $GLOBALS['SQ_SYSTEM']->am->getAssetTreeids($assetid);
88  if (empty($state['treeid'])) trigger_localised_error('TRIG0006', E_USER_WARNING);
89  }
90 
91  if (!is_array($state['treeid'])) {
92  $state['treeid'] = Array($state['treeid']);
93  }
94 
95  // evaluate the condition
96  foreach ($state['treeid'] as $state_tree) {
97  foreach ($settings['treeids'] as $tree_asset_id => $treeids) {
98  foreach ($treeids as $settings_tree) {
99  // if the asset in question is under the tree specified in the settings...
100  if ($settings_tree === $state_tree) {
101  break;
102  } else {
103  if ($settings['immed_child'][$tree_asset_id]) {
104  if (strlen($settings_tree) == strlen($state_tree) - SQ_CONF_ASSET_TREE_SIZE &&
105  substr($state_tree, 0, strlen($settings_tree)) == $settings_tree) {
106  return TRUE;
107  }
108  } else {
109  if (substr($state_tree, 0, strlen($settings_tree)) == $settings_tree) {
110  return TRUE;
111  }
112  }
113  }//end if
114  }
115  }
116  }
117 
118  return FALSE;
119 
120  }//end evaluate()
121 
122 
133  public static function getInterface($settings, $prefix, $write_access=FALSE)
134  {
135  // set to default
136  $selected_assetids = array_get_index($settings, 'assetids', Array());
137 
138  // check if the selected assets are valid
139  foreach ($selected_assetids as $selected_assetid){
140  if ($selected_assetid && !$GLOBALS['SQ_SYSTEM']->am->assetExists($selected_assetid)) {
141  unset($selected_assetids[$selected_assetid]);
142  }
143  }
144 
145  $immed_child = array_get_index($settings, 'immed_child', Array());
146 
147  // begin buffering basic options
148  ob_start();
149  // create an asset_finder, and pre-select the asset that is saved in settings (if any)
150  if ($write_access) {
151  ?><script type="text/javascript" src="<?php echo sq_web_path('lib')?>/html_form/html_form.js"></script><?php
152  echo '<div>';
153  $name = $prefix.'[assetids]';
154  foreach (array_values($selected_assetids) as $index => $selected_assetid){
155  asset_finder($name.'['.$index.']', $selected_assetid, Array(), 'sq_sidenav', FALSE, 'null', Array('clear'));
156  echo '<span style="margin-left:10px;">';
157  echo translate('immediate_child_only');
158  check_box($prefix.'[immed_child]['.$index.']', '1', (!empty($immed_child[$selected_assetid])), '', 'style="margin-left:10px;"');
159  echo '</span>';
160  echo '<br />';
161  }
162 
163  asset_finder($name.'['.count($selected_assetids).']', 0, Array(), 'sq_sidenav', FALSE, 'null', Array('clear'));
164  echo '<span id="trigger_condition_tree_location_check_box" style="margin-left:10px;">';
165  echo translate('immediate_child_only');
166  check_box($prefix.'[immed_child]['.count($selected_assetids).']', '1', FALSE, '', 'style="margin-left:10px;"');
167  echo '</span>';
168 
169  $map_frame_exp = trim('parent.frames["sq_sidenav"]', '.');
170  $safe_name = 'sq_asset_finder_'.preg_replace('/[^A-Za-z0-9]/', '_', $name);
171 
172  ?>
173  <script type="text/javascript">
174  // insert target parent check box when clicking More button
175  var immed_child_index = <?php echo count($selected_assetids)+ 1; ?>;
176  function addParentNode(node) {
177  var new_span = document.getElementById('trigger_condition_tree_location_check_box').cloneNode(true);
178  new_span.removeAttribute('id');
179  new_span.lastChild.setAttribute('id', '<?php echo $prefix; ?>' + '[immed_child][' + immed_child_index + ']');
180  new_span.lastChild.setAttribute('name', '<?php echo $prefix; ?>' + '[immed_child][' + immed_child_index + ']');
181  node.parentNode.insertBefore(new_span, node);
182  immed_child_index = immed_child_index + 1;
183  }
184  </script>
185  <?php
186 
187  echo '<input type="button" name="'.htmlspecialchars($safe_name.'_more_btn').'" id="'.htmlspecialchars($safe_name.'_more_btn').'" value="'.translate('more').'..." onclick="addNewAssetFinder(this, \''.$name.'\', \''.$safe_name.'\', \'\', \''.htmlentities($map_frame_exp, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET).'\', \'null\', true); addParentNode(this);" style="margin-left:10px;"/>';
188  echo '<br />';
189  echo '</div>';
190  } else {
191  if ($selected_assetids) {
192  foreach ($selected_assetids as $selected_assetid){
193  echo get_asset_tag_line($selected_assetid);
194  if (!empty($immed_child[$selected_assetid])) {
195  echo '<b> '.translate('as_an_immediate_child_only').'</b>';
196  }
197  echo '<br />';
198  }
199  } else {
200  echo '<b>['.translate('no_asset_selected').']</b>';
201  }
202  }
203 
204  $basic_part_1 = ob_get_contents();
205  ob_end_clean();
206 
207  return translate('trigger_tree_location_basic', $basic_part_1);
208 
209  }//end getInterface()
210 
211 
221  public static function processInterface(&$settings, $request_data)
222  {
223  $settings['assetids'] = $settings['propagate'] = $settings['immed_child'] = Array();
224  $immed_child = array_get_index($request_data, 'immed_child', Array());
225  $assetids = array_get_index($request_data, 'assetids', Array());
226  foreach ($assetids as $index => $tree_location){
227  if (!empty($tree_location['assetid'])) {
228  if ($GLOBALS['SQ_SYSTEM']->am->assetExists($tree_location['assetid'])) {
229  $settings['assetids'][] = $tree_location['assetid'];
230  if (empty($immed_child[$index])) {
231  $settings['propagate'][$tree_location['assetid']] = SQ_TRIG_TREE_PROPAGATE;
232  $settings['immed_child'][$tree_location['assetid']] = '0';
233  } else {
234  $settings['propagate'][$tree_location['assetid']] = SQ_TRIG_TREE_NO_PROPAGATE;
235  $settings['immed_child'][$tree_location['assetid']] = '1';
236  }
237  } else {
238  trigger_error(translate('selected_asset_does_not_exist'), E_USER_NOTICE);
239  }
240  }
241  }
242 
243  if (empty($settings['assetids'])) return translate('triger_no_tree_location');
244 
245  return FALSE;
246 
247  }//end processInterface()
248 
249 
260  public static function setHash(&$settings, &$hash)
261  {
262  if (isset($settings['assetids']) && isset($settings['propagate'])) {
263  // only use the raw tree value in the hash, generate from assetid dynamically
264  foreach ($settings['assetids'] as $assetid){
265  $hash->setLinkTree($GLOBALS['SQ_SYSTEM']->am->getAssetTreeids($assetid), $settings['propagate'][$assetid]);
266  }
267  }
268 
269  return FALSE;
270 
271  }//end setHash()
272 
273 
281  public static function allowMultiple()
282  {
283  // default to to disallow
284  return TRUE;
285 
286  }//end allowMultiple()
287 
288 
289 }//end class
290 
291 ?>