Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_set_tag.inc
1 <?php
17 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
18 require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
19 require_once SQ_SYSTEM_ROOT.'/core/attributes/parameter_map/parameter_map.inc';
20 
33 {
34 
35 
60  public static function execute($settings, &$state)
61  {
62  $result = FALSE;
63 
64  if (empty($state['asset'])) {
65  // grab the asset if assetid is given, but not the asset.
66  if (empty($state['assetid'])) {
67  return FALSE;
68  } else {
69  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
70  }
71  }
72 
73  // note that tagid can be an assetid or an array of ids
74  $tagid = 0;
75  // see if the tag id is set in the parameter map
76  $value_map = array_get_index($settings, 'value_map', serialize(Array()));
77  $param_map_attr = new Asset_Attribute_Parameter_Map(0, $value_map);
78  $param_map_value = $param_map_attr->getParameterValue('tag_id');
79  if (!empty($param_map_value)) {
80  // check if map value is array, otherwise explode by comma
81  if (is_array($param_map_value)) $tmp_ids = $param_map_value;
82  else $tmp_ids = explode(',', $param_map_value);
83  $tagid = Array();
84  foreach ($tmp_ids as $tmp_id) {
85  if ($GLOBALS['SQ_SYSTEM']->am->assetExists($tmp_id)) {
86  // only add valid tag id to the array
87  $tagid[] = $tmp_id;
88  } else {
89  trigger_localised_error('SYS0087', E_USER_WARNING, $tmp_id);
90  }
91  }
92  } else {
93  // if none is set in the param map,
94  // use the normal tag id chosen by the asset finder
95  if (!empty($settings['tagid']) && $GLOBALS['SQ_SYSTEM']->am->assetExists($settings['tagid'])) {
96  $tagid = $settings['tagid'];
97  } else {
98  // use the thesaurusid / term_name combo instead
99  if (!empty($settings['thesaurusid'])) {
100  $thesaurus = $GLOBALS['SQ_SYSTEM']->am->getAsset($settings['thesaurusid']);
101  if (!is_null($thesaurus)) {
102  $termid = $thesaurus->getTermIdByName($settings['term_name']);
103  if (!empty($termid)) {
104  $tagid = $thesaurus->id.':'.$termid;
105  }
106  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($thesaurus);
107  }
108  }
109  }
110  }
111  if (empty($tagid)) return FALSE;
112 
113  $action = array_get_index($settings, 'action', 'add');
114  if ($action == 'delete') {
115  $weighting = NULL;
116  } else {
117  $weighting = array_get_index($settings, 'weighting', NULL);
118  }
119 
120  $manual_cascade = array_get_index($settings, 'manual_cascade', FALSE);
121  if ($manual_cascade) {
122  // can't cascade and manually cascade at the same time
123  $cascade = FALSE;
124  } else {
125  $cascade = array_get_index($settings, 'cascade', FALSE);
126  }
127 
128 
129  $tag_manager = $GLOBALS['SQ_SYSTEM']->getTagManager();
130 
131  $tag_vars = Array();
132 
133  switch ($action) {
134  case 'add':
135  if ($cascade) {
136  $tag_vars[] = Array(
137  'tag_id' => $tagid,
138  'action' => $action,
139  'weight' => (!$weighting ? 1 : $weighting),
140  );
141  } else {
142  $result = $tag_manager->setTag($state['assetid'], $tagid, $weighting);
143  }
144  break;
145  case 'delete':
146  if ($cascade) {
147  $tag_vars[] = Array(
148  'tag_id' => $tagid,
149  'action' => $action,
150  'weight' => NULL,
151  );
152  } else {
153  $result = $tag_manager->deleteTag($state['assetid'], $tagid);
154  }
155  break;
156  }
157 
158  if ($manual_cascade) {
159  // we've already had the tags set on/removed from the asset, now
160  // manually propogate all tags on the asset to its children
161  $current_tag_links = $tag_manager->getTagLinks($state['assetid']);
162 
163  foreach ($current_tag_links as $link) {
164  $tag_vars[] = Array(
165  'tag_id' => $link['minorid'],
166  'action' => 'add',
167  'weight' => $link['value'],
168  );
169  }
170  }
171 
172  if (!empty($tag_vars)) {
173  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
174  $vars = Array(
175  'assets' => Array(
176  $state['asset']->id => Array(
177  'type_code' => $state['asset']->type(),
178  ),
179  ),
180  'tag_changes' => $tag_vars,
181  );
182  $errors = $hh->freestyleHIPO('hipo_job_edit_tags', $vars);
183  $result = empty($errors);
184  }
185 
186  if (!$result) {
187  return FALSE;
188  } else {
189  return Array(
190  'assetid' => $state['asset']->id,
191  'tagid' => $tagid,
192  'weighting' => $weighting,
193  'action' => $action,
194  );
195  }
196 
197  }//end execute()
198 
199 
210  public static function getInterface($settings, $prefix, $write_access=FALSE)
211  {
212  // print javascript to disable weighting checkbox, and to handle cascading
213  ob_start();
214  if ($write_access) {
215  ?><script>
216  function toggleWeighting(value) {
217  var prefix = '<?php echo $prefix; ?>';
218  var id = prefix + '[weighting]';
219  var element = document.getElementById(id);
220  if (!value) {
221  element.disabled = 'disabled';
222  } else {
223  element.disabled = '';
224  }
225  }
226 
227  function toggleExclusiveCheckboxes(check_clicked) {
228  var checkboxes = new Array();
229  checkboxes[0] = 'cascade';
230  checkboxes[1] = 'manual_cascade';
231 
232  var prefix = '<?php echo $prefix; ?>';
233  var id = '';
234  var ii = 0;
235 
236  for (ii = 0; ii < checkboxes.length; ii++) {
237  if (checkboxes[ii] != check_clicked) {
238  id = prefix + '[' + checkboxes[ii] + ']';
239  document.getElementById(id).checked = false;
240  }
241  }
242  }
243  </script><?php
244  }
245 
246  // check settings, set defaults if necessary
247  $tagid = array_get_index($settings, 'tagid', 0);
248  $action = array_get_index($settings, 'action', 'add');
249  $weighting = array_get_index($settings, 'weighting', ($action == 'delete' ? NULL : 1));
250 
251  $manual_cascade = array_get_index($settings, 'manual_cascade', FALSE);
252  if ($manual_cascade) {
253  // can't cascade and manually cascade at the same time
254  $cascade = FALSE;
255  } else {
256  $cascade = array_get_index($settings, 'cascade', FALSE);
257  }
258 
259 
260  // check if the selected asset is valid
261  if ($tagid && !$GLOBALS['SQ_SYSTEM']->am->assetExists($tagid)) {
262  $tagid = 0;
263  }
264 
265  ob_start();
266  if ($write_access) {
267  asset_finder($prefix.'[tag]', $tagid, Array('thesaurus_term' => 'D'));
268  echo '<br />';
269  } else {
270  if ($tagid) {
271  $tag = $GLOBALS['SQ_SYSTEM']->am->getAsset($tagid);
272  if (!is_null($tag)) {
273  echo '<b>'.$tag->name.' (#'.$tag->id.')</b>';
274  }
275  } else {
276  echo '<b>['.translate('trigger_set_tag_no_tag_selected').']</b>';
277  }
278  }
279  $tag_part = ob_get_contents();
280  ob_end_clean();
281 
282  ob_start();
283  if ($write_access) {
284  $disabled = ($action == 'add') ? '' : 'disabled="disabled"';
285  int_text_box($prefix.'[weighting]', $weighting, TRUE, 5, NULL, NULL, '', FALSE, FALSE, $disabled);
286  } else {
287  echo '<b>'.$weighting.'</b>';
288  }
289  $weighting_part = ob_get_contents();
290  ob_end_clean();
291 
292  ob_start();
293  $action_checked = ($action == 'add' ? FALSE : TRUE);
294  if ($write_access) {
295  check_box($prefix.'[action]', '1', $action_checked, 'toggleWeighting(!this.checked)');
296  } else {
297  ?><img src="<?php echo sq_web_path('lib').'/web/images/'.($action_checked ? 'tick' : 'cross').'.gif'; ?>" alt="<?php echo ($action_checked ? translate('yes') : translate('no')); ?>"><?php
298  }
299  label(translate('trigger_set_tag_label_remove_tag'), $prefix.'[action]');
300  $delete_part = ob_get_contents();
301  ob_end_clean();
302 
303  ob_start();
304  if ($write_access) {
305  check_box($prefix.'[cascade]', '1', $cascade, 'toggleExclusiveCheckboxes(\'cascade\')');
306  } else {
307  ?><img src="<?php echo sq_web_path('lib').'/web/images/'.($cascade ? 'tick' : 'cross').'.gif'; ?>" alt="<?php echo ($cascade ? translate('yes') : translate('no')); ?>"><?php
308  }
309  label(translate('cascade_tag_changes'), $prefix.'[cascade]');
310  $cascade_part = ob_get_contents();
311  ob_end_clean();
312 
313  ob_start();
314  if ($write_access) {
315  check_box($prefix.'[manual_cascade]', '1', $manual_cascade, 'toggleExclusiveCheckboxes(\'manual_cascade\')');
316  } else {
317  ?><img src="<?php echo sq_web_path('lib').'/web/images/'.($manual_cascade ? 'tick' : 'cross').'.gif'; ?>" alt="<?php echo ($manual_cascade ? translate('yes') : translate('no')); ?>"><?php
318  }
319  label(translate('manually_cascade_all_tags'), $prefix.'[manual_cascade]');
320  $manual_cascade_part = ob_get_contents();
321  ob_end_clean();
322 
323  echo translate('trigger_set_tag_select_tag').' '.$tag_part;
324  if ($write_access || $action == 'add') {
325  echo '<br />'.translate('trigger_set_tag_set_weighting').' '.$weighting_part;
326  }
327  echo '<br />'.$delete_part;
328  echo '<br /><br />'.$cascade_part;
329  echo '<br />'.$manual_cascade_part;
330 
331  // prints the parameter map to accept dynamic values
332  if ($write_access) {
333  // attribute friendly prefix
334  $new_prefix = str_replace(array('[',']'), '_', $prefix);
335  hidden_field($prefix.'[new_prefix]', $new_prefix);
336  $param_map_value = array_get_index($settings, 'value_map', serialize(Array()));
337  $param_map_attr = new Asset_Attribute_Parameter_Map(0, $param_map_value);
338  $param_map_attr->setParameter('tag_id', 'Tag ID');
339  echo $param_map_attr->paint($new_prefix.'_parameter_map');
340  }
341  $contents = ob_get_contents();
342  ob_end_clean();
343 
344  return $contents;
345 
346  }//end getInterface()
347 
348 
360  public static function processInterface(&$settings, $request_data)
361  {
362  // make sure the assetid isn't blank
363  if (!empty($request_data['tag']['assetid'])) {
364  $tagid = $request_data['tag']['assetid'];
365  } else {
366  return translate('no_tags_set');
367  }
368 
369  $settings['tagid'] = $tagid;
370  $settings['weighting'] = array_get_index($request_data, 'weighting', NULL);
371  $settings['action'] = (array_get_index($request_data, 'action') ? 'delete' : 'add');
372 
373  $settings['cascade'] = (bool) array_get_index($request_data, 'cascade', FALSE);
374  $settings['manual_cascade'] = (bool) array_get_index($request_data, 'manual_cascade', FALSE);
375 
376  // parameter map to accept dynamic value
377  $new_prefix = str_replace(array('[',']'), '_', array_get_index($request_data, 'new_prefix', ''));
378  if ($new_prefix) {
379  $param_map_attr = new Asset_Attribute_Parameter_Map();
380  $param_map_attr->process($new_prefix.'_parameter_map');
381  $settings['value_map'] = $param_map_attr->value;
382  }
383 
384  return FALSE;
385 
386  }//end processInterface()
387 
388 
398  public static function getLocks($settings, &$state)
399  {
400  return Array(
401  $state['assetid'] => Array('all'),
402  );
403 
404  }//end getLocks()
405 
406 
407 }//end class
408 ?>