Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_remove_links.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_CORE_PACKAGE_PATH.'/system/triggers/trigger_action/trigger_action.inc';
20 require_once SQ_SYSTEM_ROOT.'/core/attributes/parameter_map/parameter_map.inc';
21 
39 {
40 
41 
67  public static function execute($settings, &$state)
68  {
69  // check for required settings
70  if (!isset($settings['is_major']) || empty($settings['link_type'])) {
71  return FALSE;
72  }
73 
74  $parameter_map_value = array_get_index($settings, 'parameter_map_target_asset', serialize(Array()));
75  $attr_parameter_map = new Asset_Attribute_Parameter_Map(0, $parameter_map_value);
76  $map_assetid = $attr_parameter_map->getParameterValue('asset');
77  $direct_links_only = $attr_parameter_map->getParameterValue('direct_links_only');
78 
79  if (empty($settings['assetid']) && empty($map_assetid)) {
80  return FALSE;
81  }
82 
83  if (!$GLOBALS['SQ_SYSTEM']->am->assetExists($settings['assetid'])) {
84  return FALSE;
85  }
86 
87  if (!is_null($map_assetid) && !$GLOBALS['SQ_SYSTEM']->am->assetExists($map_assetid)) {
88  return FALSE;
89  }
90 
91  // check the optional settings, assign defaults
92  $is_dependant = array_get_index($settings, 'is_dependant', 0);
93  $is_exclusive = array_get_index($settings, 'is_exclusive', 0);
94  $link_value = array_get_index($settings, 'link_value', '');
95 
96  if (empty($state['asset'])) {
97  // if we've been given the assetid but not the asset, get the asset
98  if (empty($state['assetid'])) {
99  return FALSE;
100  } else {
101  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
102  }
103  }
104 
105  $am = $GLOBALS['SQ_SYSTEM']->am;
106 
107  // whether we use the parameter map value or the settings value for the target asset
108  $assetid = is_null($map_assetid) ? $settings['assetid'] : $map_assetid;
109 
110  $major = $am->getAsset($assetid);
111  $minor = $state['asset'];
112 
113  $link_value_sql = (!empty($settings['link_value'])) ? 'l.value = '.MatrixDAL::quote($settings['link_value']).' AND ' : '';
114 
115  // If the Link Value is set to blank and we haven't selected "Any Link Value", then we want empty link values only
116  if (empty($link_value_sql) && (!$settings['any_link_value'])) {
117  $link_value_sql = 'l.value = '.MatrixDAL::quote('').' AND ';
118  }
119 
120  $link_type_sql = ($settings['link_type'] != 'ANY') ? 'l.link_type = '.MatrixDAL::quote($settings['link_type']).' AND ' : '';
121  $link_asset_type_codes_sql = '';
122 
123  if (!empty($settings['asset_types'])) {
124  $link_asset_type_codes_sql = 'AND a.type_code IN (';
125  foreach ($settings['asset_types'] as $asset_type) {
126  $link_asset_type_codes_sql .= MatrixDAL::quote($asset_type).',';
127  }
128  // Remove last comma and terminate the IN clause
129  $link_asset_type_codes_sql = substr($link_asset_type_codes_sql, 0, strlen($link_asset_type_codes_sql)-1).')';
130  }
131 
132  $major_or_minor_link = 'minorid';
133 
134  $broadcasting_asset_id = $state['assetid'];
135  $root_node_asset_id = $assetid;
136 
137  // Compile a list of candidate links which reside DIRECTLY under our Root Node (default operation)
138  $assets = 'SELECT DISTINCT
139  a.assetid
140  FROM
141  sq_ast a, sq_ast_lnk l
142  WHERE
143  l.majorid = '.MatrixDAL::quote($root_node_asset_id).' AND
144  a.assetid = l.minorid AND
145  a.assetid != '.MatrixDAL::quote($broadcasting_asset_id).' '.
146  $link_asset_type_codes_sql;
147 
148  // Find out whether the Parameter Map was used to populate the "Direct Links Only" option - if so, populate our retrieved value
149  $direct_links_only = is_null($direct_links_only) ? $settings['direct_links_only'] : ($direct_links_only == 1);
150 
151  // If we are looking in the entire tree (ie; not "Direct Links Only"), generate a query to compile a list of candidate links which reside anywhere under our Root Node
152  if (!$direct_links_only) {
153  // Select candidate links from under the Root Node
154  // Get the Tree ID of our Root Node
155  $tree_id_sql = 'SELECT
156  t.treeid
157  FROM
158  sq_ast_lnk l, sq_ast_lnk_tree t
159  WHERE
160  t.linkid = l.linkid AND
161  l.minorid = '.MatrixDAL::quote($root_node_asset_id);
162 
163  $root_node_tree_id = MatrixDAL::executeSqlOne($tree_id_sql);
164 
165  // Ok, any matching assets should be linked under the Broadcasting Asset back to underneath our Root Node (ie; share a common Tree ID)
166  // Let's climb the tree...
167  $assets = 'SELECT DISTINCT
168  a.assetid
169  FROM
170  sq_ast a, sq_ast_lnk l, sq_ast_lnk_tree t
171  WHERE
172  t.linkid = l.linkid AND
173  t.treeid LIKE '.MatrixDAL::quote($root_node_tree_id.'%').' AND
174  t.treeid != '.MatrixDAL::quote($root_node_tree_id).' AND
175  a.assetid = l.minorid AND
176  a.assetid != '.MatrixDAL::quote($broadcasting_asset_id).' '.
177  $link_asset_type_codes_sql;
178  }
179 
180  // Abort if we're about to remove the last significant link!
181  if ($settings['link_type'] & SQ_SC_LINK_SIGNIFICANT) {
182  $our_links = $am->getLinks($broadcasting_asset_id, SQ_SC_LINK_SIGNIFICANT, '', TRUE, 'minor');
183  $num_links = count($our_links);
184 
185  // Abort... abort... abort...
186  if ($num_links < 2) return Array();
187  }
188 
189  // Match candidate links with links associated with (ie; immediately above) the Broadcasting Asset
190  $sql = 'SELECT
191  l.linkid
192  FROM
193  sq_ast_lnk l
194  WHERE
195  l.minorid = '.MatrixDAL::quote($broadcasting_asset_id).' AND '.
196  $link_type_sql.
197  $link_value_sql.
198  'l.majorid IN ('.
199  $assets.
200  ')';
201 
202  // Our links to remove
203  $old_links = MatrixDAL::executeSqlAssoc($sql);
204 
205  // Remove the candidate links
206  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
207  $db = MatrixDAL::getDb();
208  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
209 
210  try {
211  foreach ($old_links as $old_link) {
212  $am->deleteAssetLink($old_link['linkid']);
213  }
214 
215  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
216 
217  } catch (Exception $e) {
218  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
219  throw new Exception('Unable to delete links for asset: '.addslashes($assetid).' due to database error: '.$e->getMessage());
220  }
221 
222  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
223 
224  // update lookups for both assets
225  $major->updateLookups();
226  $minor->updateLookups();
227 
228  return Array();
229 
230  }//end execute()
231 
232 
243  public static function getInterface($settings, $prefix, $write_access=FALSE)
244  {
245  // check settings, set defaults if necessary
246  $selected_assetid = array_get_index($settings, 'assetid', 0);
247  $is_major = array_get_index($settings, 'is_major', 1);
248  $selected_link_type = array_get_index($settings, 'link_type', 'ANY');
249  $link_value = array_get_index($settings, 'link_value', '');
250  $is_exclusive = array_get_index($settings, 'is_exclusive', 0);
251  $any_link_value = array_get_index($settings, 'any_link_value', TRUE);
252  $direct_links_only = array_get_index($settings, 'direct_links_only', TRUE);
253 
254  // check if the selected asset is valid
255  if ($selected_assetid && !$GLOBALS['SQ_SYSTEM']->am->assetExists($selected_assetid)) {
256  $selected_assetid = 0;
257  }
258 
259  $is_major_list = Array(0 => 'Child', 1 => 'Parent');
260 
261  $link_type_list = get_link_type_names();
262 
263  if (!$write_access) {
264  $form_element_extras = 'disabled="disabled"';
265  } else {
266  $form_element_extras = '';
267  }
268 
269  // Matching Asset Types
270  $asset_types = $GLOBALS['SQ_SYSTEM']->am->getTypeList();
271  foreach ($asset_types as $type_code) {
272  $info = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($type_code);
273  if ($info['allowed_access'] != 'system' && $info['instantiable']) {
274  $asset_types_options[$type_code] = $info['name'];
275  }
276  }
277  asort($asset_types_options);
278  $asset_types_options = array_reverse($asset_types_options, TRUE);
279  $asset_types_options[''] = '-- '.translate('all_asset_types').' --';
280  $asset_types_options = array_reverse($asset_types_options, TRUE);
281 
282  // initialise settings var
283  $settings['asset_types'] = isset($settings['asset_types']) ? $settings['asset_types'] : Array();
284  if (empty($settings['asset_types'])) {
285  $settings['asset_types'] = Array(0 => '');
286  }
287 
288  ob_start();
289  ?><table><tr><?php
290 
291  if ($write_access) {
292  ?>
293  <td valign="top"><b><?php echo translate('trigger_remove_link_matching_asset_types'); ?></b></td>
294  <td valign="top" style="padding-bottom: 10px;"><?php echo combo_box($prefix.'[asset_types]', $asset_types_options, TRUE, $settings['asset_types'], 8); ?></td>
295  <?php
296  } else {
297  ?>
298  <td valign="top"><b><?php echo translate('trigger_remove_link_matching_asset_types'); ?></b></td>
299  <td valign="top" style="padding-bottom: 10px;"><?php
300  if (!empty($settings['asset_types'][0])) {
301  ?><ul><?php
302  foreach ($settings['asset_types'] as $type_code) {
303  ?><li><?php
304  echo get_asset_type_icon($type_code);
305  echo $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($type_code, 'name');
306  ?></li><?php
307  }
308  ?></ul><?php
309  } else {
310  echo '&nbsp; &nbsp;'.translate('all_asset_types');
311  }
312  ?></td><?php
313  }
314 
315  ?></tr></table><?php
316 
317  $asset_types_combo_box = ob_get_contents();
318  ob_end_clean();
319 
320  ob_start();
321  if ($write_access) {
322  asset_finder($prefix.'[assetid]', $selected_assetid);
323  } else {
324  if ($selected_assetid) {
325  $selected_assetname = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($selected_assetid), Array(), TRUE, 'name');
326  text_box($prefix.'[assetid]', $selected_assetname[$selected_assetid].' (#'.$selected_assetid.')', '', '', FALSE, $form_element_extras);
327  } else {
328  text_box($prefix.'[assetid]', '', '', '', FALSE, $form_element_extras);
329  }
330  }
331 
332  $basic_part_2 = ob_get_contents();
333  ob_end_clean();
334 
335  ob_start();
336  $link_type_list['ANY'] = 'ANY TYPE';
337 
338  combo_box($prefix.'[link_type]', $link_type_list, FALSE, $selected_link_type, NULL, $form_element_extras);
339  $basic_part_3 = ob_get_contents();
340  ob_end_clean();
341 
342 
343  // attribute friendly prefix
344  $new_prefix = str_replace(array('[', ']'), '_', $prefix);
345  hidden_field($prefix.'[new_prefix]', $new_prefix);
346 
347  // parameter map options
348  ob_start();
349  $parameter_map_value = array_get_index($settings, 'parameter_map_target_asset', serialize(Array()));
350  $attr_parameter_map = new Asset_Attribute_Parameter_Map(0, $parameter_map_value);
351  $attr_parameter_map->setParameter('asset', 'Candidate Assets');
352  $attr_parameter_map->setParameter('direct_links_only', 'Where Candidate Assets are Directly Linked to the Root Node (1 = TRUE)');
353  echo $attr_parameter_map->paint($new_prefix.'_parameter_map', !$write_access);
354  $basic_part_4 = ob_get_contents();
355  ob_end_clean();
356 
357  ob_start();
358  $link_value_onkeypress = '';
359  $any_link_value_onclick = '';
360 
361  if ($write_access) {
362  $link_value_onkeypress = 'onkeypress = "document.getElementById(\'any_link_value\').checked = false;"';
363  $any_link_value_onclick = 'onclick = "document.getElementById(\'link_value\').value = \'\';"';
364  }
365 
366  text_box($prefix.'[link_value]', $link_value, '', '', FALSE, $form_element_extras.' '.$link_value_onkeypress.' id="link_value"');
367  check_box($prefix.'[any_link_value]', '1', $any_link_value, '', $form_element_extras.' '.$any_link_value_onclick.' id="any_link_value"');
368  echo translate('trigger_remove_link_any_link_value');
369 
370  echo '<p>'.translate('trigger_remove_link_direct_links_only');
371  check_box($prefix.'[direct_links_only]', '1', $direct_links_only, '', $form_element_extras);
372  echo '</p>';
373 
374  $advanced_part_1 = ob_get_contents();
375  ob_end_clean();
376 
377  $contents = translate('trigger_remove_link_basic', $basic_part_2).'<br />'.
378  translate('trigger_remove_link_advanced', $basic_part_3, $advanced_part_1).'<br /><br />'.
379  $asset_types_combo_box.
380  $basic_part_4;
381 
382  return $contents;
383 
384  }//end getInterface()
385 
386 
398  public static function processInterface(&$settings, $request_data)
399  {
400  $is_major_list = Array(0 => 'Child', 1 => 'Parent');
401 
402  $link_type_list = get_link_type_names();
403  $link_type_list['ANY'] = 'ANY TYPE';
404 
405  // make sure the assetid isn't blank
406  if (empty($request_data['assetid']['assetid'])) {
407  return 'Asset ID not specified';
408  }
409 
410  $request_data['is_major'] = TRUE;
411  if (!isset($request_data['is_major']) || !isset($is_major_list[$request_data['is_major']])) {
412  return 'Parent or Child link not specified';
413  }
414 
415  if (empty($request_data['link_type']) || !isset($link_type_list[$request_data['link_type']])) {
416  return 'Link Type not specified';
417  }
418 
419  $new_prefix = str_replace(array('[', ']'), '_', array_get_index($request_data, 'new_prefix', ''));
420  $attr_parameter_map = new Asset_Attribute_Parameter_Map();
421  $attr_parameter_map->process($new_prefix.'_parameter_map');
422  $settings['parameter_map_target_asset'] = $attr_parameter_map->value;
423 
424  $settings['assetid'] = $request_data['assetid']['assetid'];
425  $settings['link_id'] = $request_data['assetid']['linkid'];
426  $settings['is_major'] = $request_data['is_major'];
427  $settings['link_type'] = $request_data['link_type'];
428 
429  // selected asset types
430  $settings['asset_types'] = Array();
431  if (isset($request_data['asset_types']) && !empty($request_data['asset_types'][0])) {
432  $settings['asset_types'] = $request_data['asset_types'];
433  }
434 
435  // optional fields
436  $settings['link_value'] = array_get_index($request_data, 'link_value', '');
437  $settings['any_link_value'] = array_get_index($request_data, 'any_link_value', FALSE);
438  $settings['direct_links_only'] = array_get_index($request_data, 'direct_links_only', FALSE);
439 
440  return FALSE;
441 
442  }//end processInterface()
443 
444 
445 }//end class
446 
447 ?>