Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_create_link.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 
34 {
35 
36 
61  public static function execute($settings, &$state)
62  {
63  // check for required settings
64  if (!isset($settings['is_major']) || empty($settings['link_type'])) {
65  return FALSE;
66  }
67 
68  $parameter_map_value = array_get_index($settings, 'parameter_map_target_asset', serialize(Array()));
69  $atr_parameter_map = new Asset_Attribute_Parameter_Map(0, $parameter_map_value);
70  $map_assetid = $atr_parameter_map->getParameterValue('asset');
71 
72  if (empty($settings['assetid']) && empty($map_assetid)) {
73  return FALSE;
74  }
75 
76  if (!$GLOBALS['SQ_SYSTEM']->am->assetExists($settings['assetid'])) {
77  return FALSE;
78  }
79 
80  if (!is_null($map_assetid) && !$GLOBALS['SQ_SYSTEM']->am->assetExists($map_assetid)) {
81  return FALSE;
82  }
83 
84  // check the optional settings, assign defaults
85  $is_dependant = array_get_index($settings, 'is_dependant', 0);
86  $is_exclusive = array_get_index($settings, 'is_exclusive', 0);
87  $value = array_get_index($settings, 'value', '');
88  // don't remove any link if the new one is not significant
89  $remove_other_links = array_get_index($settings, 'link_exclusively', 0) && ($settings['link_type'] & SQ_SC_LINK_SIGNIFICANT);
90 
91  if (empty($state['asset'])) {
92  // if we've been given the assetid but not the asset, get the asset
93  if (empty($state['assetid'])) {
94  return FALSE;
95  } else {
96  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
97  }
98  }
99 
100  $am = $GLOBALS['SQ_SYSTEM']->am;
101 
102  if ($remove_other_links) {
103  // get a list of the existing links
104  $old_links = $am->getLinks($state['assetid'], SQ_SC_LINK_ALL, '', TRUE, 'minor');
105  }
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  // determine major/minor from settings
111  if ($settings['is_major']) {
112  $major = $state['asset'];
113  $minor = $am->getAsset($assetid);
114  } else {
115  $major = $am->getAsset($assetid);
116  $minor = $state['asset'];
117  }
118 
119  $side_of_link = ($settings['is_major']) ? 'major' : 'minor';
120  if (!empty($settings['value'])) {
121  $link_info = $am->getLinkByAsset($state['assetid'], $assetid, $settings['link_type'], $settings['value'], $side_of_link, TRUE);
122  } else {
123  $link_info = $am->getLinkByAsset($state['assetid'], $assetid, $settings['link_type'], NULL, $side_of_link, TRUE);
124  }
125 
126  $action_link = array_shift($link_info);
127 
128  // create a link
129  if (empty($action_link)) {
130  $linkid = $am->createAssetLink($major, $minor, $settings['link_type'], $value, NULL, $is_dependant, $is_exclusive);
131  if (!$linkid) return FALSE;
132  $action_link = $GLOBALS['SQ_SYSTEM']->am->getLinkById($linkid);
133  }
134 
135  if ($remove_other_links) {
136  foreach ($old_links as $old_link) {
137  // don't remove the link we have just created
138  if ($action_link['linkid'] != $old_link['linkid']) {
139  $am->deleteAssetLink($old_link['linkid']);
140  }
141  }
142  }
143 
144  // Whether to add auto remap
145  $rm = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('remap_manager');
146  $auto_add_remaps = $rm->attr('remap_upon_webpath_change');
147 
148  // update lookups for both assets
149  $major->updateLookups($auto_add_remaps);
150  $minor->updateLookups($auto_add_remaps);
151 
152  return Array(
153  'linkid' => $action_link['linkid'],
154  'majorid' => array_get_index($action_link, 'majorid', 0),
155  'minorid' => array_get_index($action_link, 'minorid', 0),
156  'link_type' => $action_link['link_type'],
157  'value' => $action_link['value'],
158  'sort_order' => $action_link['sort_order'],
159  'is_dependant' => $action_link['is_dependant'],
160  'is_exclusive' => $action_link['is_exclusive'],
161  );
162 
163  }//end execute()
164 
165 
176  public static function getInterface($settings, $prefix, $write_access=FALSE)
177  {
178  // check settings, set defaults if necessary
179  $selected_assetid = array_get_index($settings, 'assetid', 0);
180  $is_major = array_get_index($settings, 'is_major', 0);
181  $selected_link_type = array_get_index($settings, 'link_type', SQ_LINK_TYPE_1);
182  $value = array_get_index($settings, 'value', '');
183  $is_dependant = array_get_index($settings, 'is_dependant', 0);
184  $is_exclusive = array_get_index($settings, 'is_exclusive', 0);
185  $link_exclusively = array_get_index($settings, 'link_exclusively', 0);
186 
187  // check if the selected asset is valid
188  if ($selected_assetid && !$GLOBALS['SQ_SYSTEM']->am->assetExists($selected_assetid)) {
189  $selected_assetid = 0;
190  }
191 
192  $is_major_list = Array(0 => 'Child', 1 => 'Parent');
193 
194  $link_type_list = get_link_type_names();
195 
196  if (!$write_access) {
197  $form_element_extras = 'disabled="disabled"';
198  } else {
199  $form_element_extras = '';
200  }
201 
202  // begin buffering basic options
203 
204  ob_start();
205  combo_box($prefix.'[is_major]', $is_major_list, FALSE, $is_major, NULL, $form_element_extras);
206  $basic_part_1 = ob_get_contents();
207  ob_end_clean();
208 
209  ob_start();
210  if ($write_access) {
211  asset_finder($prefix.'[assetid]', $selected_assetid);
212  } else {
213  if ($selected_assetid) {
214  $selected_assetname = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($selected_assetid), Array(), TRUE, 'name');
215  text_box($prefix.'[assetid]', $selected_assetname[$selected_assetid].' (#'.$selected_assetid.')', '', '', FALSE, $form_element_extras);
216  } else {
217  text_box($prefix.'[assetid]', '', '', '', FALSE, $form_element_extras);
218  }
219  }
220 
221  $basic_part_2 = ob_get_contents();
222  ob_end_clean();
223 
224  ob_start();
225  combo_box($prefix.'[link_type]', $link_type_list, FALSE, $selected_link_type, NULL, $form_element_extras);
226  $basic_part_3 = ob_get_contents();
227  ob_end_clean();
228 
229 
230  // begin buffering advanced options
231 
232  // attribute friendly prefix
233  $new_prefix = str_replace(array('[',']'), '_', $prefix);
234  hidden_field($prefix.'[new_prefix]', $new_prefix);
235 
236  // parameter map options
237  ob_start();
238  $parameter_map_value = array_get_index($settings, 'parameter_map_target_asset', serialize(Array()));
239  $atr_parameter_map = new Asset_Attribute_Parameter_Map(0, $parameter_map_value);
240  $atr_parameter_map->setParameter('asset', 'Target Asset');
241  echo $atr_parameter_map->paint($new_prefix.'_parameter_map', !$write_access);
242  $basic_part_4 = ob_get_contents();
243  ob_end_clean();
244 
245  ob_start();
246  text_box($prefix.'[value]', $value, '', '', FALSE, $form_element_extras);
247  $advanced_part_1 = ob_get_contents();
248  ob_end_clean();
249 
250  ob_start();
251  $is_dependant_list = Array(
252  0 => translate('non_dependently'),
253  1 => translate('dependently'),
254  );
255  combo_box($prefix.'[is_dependant]', $is_dependant_list, FALSE, $is_dependant, NULL, $form_element_extras);
256  $advanced_part_2 = ob_get_contents();
257  ob_end_clean();
258 
259  ob_start();
260  $is_exclusive_list = Array(
261  0 => translate('non_exclusively'),
262  1 => translate('exclusively'),
263  );
264  combo_box($prefix.'[is_exclusive]', $is_exclusive_list, FALSE, $is_exclusive, NULL, $form_element_extras);
265  $advanced_part_3 = ob_get_contents();
266  ob_end_clean();
267 
268  ob_start();
269  if ($write_access) {
270  check_box($prefix.'[link_exclusively]', '1', $link_exclusively, '', 'id="'.$prefix.'_link_exclusively"');
271  label(translate('trigger_create_link_remove_others'), $prefix.'_link_exclusively');
272  } else {
273  echo '<img src="'.sq_web_path('lib').'/web/images/'.($link_exclusively ? 'tick' : 'cross').'.gif" />';
274  echo translate('trigger_create_link_remove_others');
275  }
276  $advanced_part_4 = ob_get_contents();
277  ob_end_clean();
278 
279  $contents = translate('trigger_create_link_basic', $basic_part_1, $basic_part_2, $basic_part_3).
280  '<br />'.$basic_part_4.'<br />'.
281  '<b>'.translate('triggers_advanced_user_options').'</b><br />'.
282  translate('trigger_create_link_advanced', $advanced_part_1, $advanced_part_2, $advanced_part_3).
283  '<br />'.$advanced_part_4;
284 
285  return $contents;
286 
287  }//end getInterface()
288 
289 
301  public static function processInterface(&$settings, $request_data)
302  {
303  $is_major_list = Array(0 => 'Child', 1 => 'Parent');
304 
305  $link_type_list = get_link_type_names();
306 
307  // make sure the assetid isn't blank
308  if (empty($request_data['assetid']['assetid'])) {
309  return 'Asset ID not specified';
310  }
311 
312  if (!isset($request_data['is_major']) || !isset($is_major_list[$request_data['is_major']])) {
313  return 'Parent or Child link not specified';
314  }
315 
316  if (empty($request_data['link_type']) || !isset($link_type_list[$request_data['link_type']])) {
317  return 'Link Type not specified';
318  }
319 
320  $new_prefix = str_replace(array('[',']'), '_', array_get_index($request_data, 'new_prefix', ''));
321  $atr_parameter_map = new Asset_Attribute_Parameter_Map();
322  $atr_parameter_map->process($new_prefix.'_parameter_map');
323  $settings['parameter_map_target_asset'] = $atr_parameter_map->value;
324 
325  $settings['assetid'] = $request_data['assetid']['assetid'];
326  $settings['is_major'] = $request_data['is_major'];
327  $settings['link_type'] = $request_data['link_type'];
328 
329  // optional fields
330  $settings['value'] = array_get_index($request_data, 'value', '');
331  $settings['is_dependant'] = array_get_index($request_data, 'is_dependant', 0);
332  $settings['is_exclusive'] = array_get_index($request_data, 'is_exclusive', 0);
333  $settings['link_exclusively'] = array_get_index($request_data, 'link_exclusively', 0);
334 
335  return FALSE;
336 
337  }//end processInterface()
338 
339 
340 }//end class
341 
342 ?>