Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_create_asset.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 
33 {
34 
35 
58  public static function execute($settings, &$state)
59  {
60  // check required settings
61  if (empty($settings['type_code'])) return FALSE;
62  if (empty($settings['parentid'])) return FALSE;
63  if (empty($settings['link_type'])) return FALSE;
64 
65  if ($settings['parentid'] == 'current_asset') {
66  $settings['parentid'] = $state['assetid'];
67  }
68  $attributes = Array();
69  if (!empty($settings['attributes'])) {
70  $attributes = $settings['attributes'];
71  }
72  $parent = $GLOBALS['SQ_SYSTEM']->am->getAsset($settings['parentid']);
73  if (is_null($parent)) return FALSE;
74 
75  // check the optional settings, assign defaults
76  $is_dependant = array_get_index($settings, 'is_dependant', 0);
77  $is_exclusive = array_get_index($settings, 'is_exclusive', 0);
78  $value = array_get_index($settings, 'value', '');
79 
80  // the new link
81  $link = Array(
82  'asset' => &$parent,
83  'link_type' => $settings['link_type'],
84  'value' => $value,
85  'sort_order' => NULL,
86  'is_dependant' => (bool) $is_dependant,
87  'is_exclusive' => (bool) $is_exclusive,
88  );
89 
90  $type_code = $settings['type_code'];
91  $GLOBALS['SQ_SYSTEM']->am->includeAsset($type_code);
92  $new_asset = new $type_code();
93  // set the attributes for the newly created asset
94  if (!empty($attributes)) {
95  foreach ($attributes as $index => $val) {
96  $value = $val['value'];
97  //if the replace keywords checkbox is checked, do keyword replacement here before setting the value to attribute
98  if (array_get_index($settings, 'replace_keywords', 0)) {
99  // replace global keywords
100  $old_current_asset = $GLOBALS['SQ_SYSTEM']->getGlobalDefine('CURRENT_ASSET', NULL);
101  $GLOBALS['SQ_SYSTEM']->setGlobalDefine('CURRENT_ASSET', $state['asset']);
102 
103  replace_global_keywords($value);
104 
105  if (is_null($old_current_asset)) {
106  $GLOBALS['SQ_SYSTEM']->unsetGlobalDefine('CURRENT_ASSET');
107  } else {
108  $GLOBALS['SQ_SYSTEM']->setGlobalDefine('CURRENT_ASSET', $old_current_asset);
109  }
110 
111  // replace broadcasting asset keywords
112  $keywords = retrieve_keywords_replacements($value);
113  $replacements = Array();
114  foreach ($keywords as $keyword) {
115  $replacements[$keyword] = $state['asset']->getKeywordReplacement($keyword);
116  }
117  replace_keywords($value, $replacements);
118  }
119  $new_asset->setAttrValue($val['attribute'], $value);
120  }
121  }
122  $linkid = $new_asset->create($link);
123  if (!$linkid) return FALSE;
124 
125  $link = $GLOBALS['SQ_SYSTEM']->am->getLinkById($linkid);
126 
127  return Array(
128  'assetid' => $link['minorid'],
129  'parentid' => $link['majorid'],
130  'linkid' => $link['linkid'],
131  'link_type' => $link['link_type'],
132  'value' => $link['value'],
133  'sort_order' => $link['sort_order'],
134  'is_dependant' => $link['is_dependant'],
135  'is_exclusive' => $link['is_exclusive'],
136  );
137 
138  }//end execute()
139 
140 
151  public static function getInterface($settings, $prefix, $write_access=FALSE)
152  {
153  // check settings, set defaults if necessary
154  $parentid = array_get_index($settings, 'parentid', 0);
155  $type_code = array_get_index($settings, 'type_code', 0);
156  $link_type = array_get_index($settings, 'link_type', SQ_LINK_TYPE_1);
157  $value = array_get_index($settings, 'value', '');
158  $is_dependant = array_get_index($settings, 'is_dependant', 0);
159  $is_exclusive = array_get_index($settings, 'is_exclusive', 0);
160  $selected = array_get_index($settings, 'attributes', 0);
161 
162  $attrs = Array();
163  $attributes = Array();
164  if (!empty($type_code)) {
165  // get the attributes of this asset
166  $attrs = $GLOBALS['SQ_SYSTEM']->am->getAssetTypeAttributes($type_code);
167  foreach ($attrs as $attr => $type) {
168  $attributes[$attr] = ucwords(str_replace('_', ' ', $attr)).' - '.ucwords($type['type']);
169  }
170  $attributes[''] = '--SELECT ATTRIBUTE--';
171  }
172 
173  // check if the parent asset is valid
174  $parent_broadcasted = FALSE;
175  if ($parentid == 'current_asset') {
176  $parent_broadcasted = TRUE;
177  $parentid = 0;
178  } else {
179  if ($parentid && !$GLOBALS['SQ_SYSTEM']->am->assetExists($parentid)) {
180  $parentid = 0;
181  }
182  }
183 
184  // begin buffering basic options
185  ob_start();
186  if ($write_access) {
187  asset_type_chooser($prefix.'[type_code]', FALSE, Array($type_code), TRUE);
188  } else {
189  echo '<b>'.$type_code.'</b>';
190  }
191 
192  $basic_part_1 = ob_get_contents();
193  ob_end_clean();
194 
195 
196  ob_start();
197  if ($write_access) {
198  asset_finder($prefix.'[parentid]', $parentid, Array('asset' => 'D'));
199  echo '<br /><b>'.translate('trigger_create_under_broadcaster').'</b>';
200  check_box($prefix.'[parentid]', 'current_asset', $parent_broadcasted);
201  } else {
202  $parent = NULL;
203  if ($parentid > 0) {
204  $parent = $GLOBALS['SQ_SYSTEM']->am->getAsset($parentid);
205  }
206  if (is_null($parent)) {
207  echo '<b>['.translate('no_parent_selected').']</b>';
208  } else {
209  echo '<b>'.translate('asset_format', $parent->name, $parent->id).'</b>';
210  }
211  }
212 
213  $basic_part_2 = ob_get_contents();
214  ob_get_clean();
215 
216  $link_type_list = get_link_type_names();
217  unset($link_type_list[SQ_LINK_NOTICE]);
218 
219  ob_start();
220  if ($write_access) {
221  combo_box($prefix.'[link_type]', $link_type_list, FALSE, $link_type);
222  } else {
223  echo '<b>'.$link_type_list[$link_type].'</b>';
224  }
225 
226  $basic_part_3 = ob_get_contents();
227  ob_end_clean();
228 
229  // begin buffering advanced options
230  ob_start();
231  if ($write_access) {
232  text_box($prefix.'[value]', $value, '', '');
233  } else {
234  if (empty($value)) {
235  $value = '['.translate('no_value_entered').']';
236  }
237  echo '<b>'.$value.'</b>';
238  }
239  $advanced_part_1 = ob_get_contents();
240  ob_end_clean();
241 
242  ob_start();
243  $is_dependant_list = Array(
244  0 => translate('non_dependently'),
245  1 => translate('dependently'),
246  );
247  if ($write_access) {
248  combo_box($prefix.'[is_dependant]', $is_dependant_list, FALSE, $is_dependant);
249  } else {
250  echo '<b>'.$is_dependant_list[$is_dependant].'</b>';
251  }
252 
253  $advanced_part_2 = ob_get_contents();
254  ob_end_clean();
255 
256  ob_start();
257  $is_exclusive_list = Array(
258  0 => translate('non_exclusively'),
259  1 => translate('exclusively'),
260  );
261  if ($write_access) {
262  combo_box($prefix.'[is_exclusive]', $is_exclusive_list, FALSE, $is_exclusive);
263  } else {
264  echo '<b>'.$is_exclusive_list[$is_exclusive].'</b>';
265  }
266 
267  $advanced_part_3 = ob_get_contents();
268  ob_end_clean();
269 
270  ob_start();
271  if (!empty($attributes)) {
272  $count = 0;
273  if (!empty($selected)) {
274  $count = count($selected);
275  foreach ($selected as $index => $attr) {
276  if ($write_access) {
277  combo_box($prefix.'[attributes]['.$index.'][attribute]', $attributes, FALSE, $attr['attribute']);
278  text_box($prefix.'[attributes]['.$index.'][value]', $attr['value']);
279  check_box($prefix.'[attributes]['.$index.'][delete]','delete', FALSE);
280  echo translate('delete_question');
281  echo '<br />';
282  } else {
283  echo ucwords($attr['attribute']).': '.$attr['value'].'<br />';
284  }
285  }
286  }
287  if ($write_access) {
288  echo '<br />';
289  combo_box($prefix.'[attributes]['.$count.'][attribute]', $attributes, FALSE, '');
290  text_box($prefix.'[attributes]['.$count.'][value]', '');
291  }
292  }
293  $attributes = ob_get_contents();
294  ob_end_clean();
295 
296  // Replace keywords checkbox
297  ob_start();
298  check_box($prefix.'[replace_keywords]', '1', array_get_index($settings, 'replace_keywords', 0), NULL, ($write_access? '' : 'disabled="disabled"'));
299  label('Replace keywords in the attribute value', $prefix.'[replace_keywords]');
300  $replace_keywords = '<br />'.ob_get_clean();
301 
302  $attributes .= $replace_keywords;
303 
304  // now patch together the output
305  $contents = translate('trigger_new_asset_basic', $basic_part_1, $basic_part_2, $basic_part_3).
306  '<br /><br />'.
307  '<b>'.translate('triggers_advanced_user_options').'</b><br />'.
308  translate('trigger_new_asset_advanced', $advanced_part_1, $advanced_part_2, $advanced_part_3).
309  '<br /><br /><b>'.translate('set_attributes').'</b><br />'.$attributes.'<br /><br />'.
310  '<span class="sq-backend-warning"><b>'.translate('trigger_create_asset_required_attrs_warning').'</b></span>';
311 
312  return $contents;
313 
314  }//end getInterface()
315 
316 
328  public static function processInterface(&$settings, $request_data)
329  {
330  // make sure the parentid isn't blank
331  if (($request_data['parentid'] != 'current_asset') && empty($request_data['parentid']['assetid'])) {
332  return translate('parent_to_create_under_is_missing');
333  }
334 
335  $link_type_list = get_link_type_names();
336  if (empty($request_data['link_type']) || !isset($link_type_list[$request_data['link_type']])) {
337  return translate('link_type_missing');
338  }
339 
340  if (is_array($request_data['parentid'])) {
341  $settings['parentid'] = $request_data['parentid']['assetid'];
342  } else {
343  $settings['parentid'] = $request_data['parentid'];
344  }
345  $settings['link_type'] = $request_data['link_type'];
346 
347  $type_code = array_get_index($request_data, 'type_code', FALSE);
348  if (!$type_code) {
349  return translate('asset_type_not_specified');
350  }
351  $settings['type_code'] = $type_code;
352 
353  // optional fields
354  $settings['value'] = array_get_index($request_data, 'value', '');
355  $settings['is_dependant'] = array_get_index($request_data, 'is_dependant', 0);
356  $settings['is_exclusive'] = array_get_index($request_data, 'is_exclusive', 0);
357 
358  // if the user has chosen attributes and their default values
359  if (isset($request_data['attributes']) && !empty($request_data['attributes'])) {
360  $attribute = $request_data['attributes'];
361  $new_attr = Array();
362  foreach ($attribute as $index => $val) {
363  if (empty($val['attribute']) || empty($val['value'])) {
364  continue;
365  }
366  if (isset($val['delete'])) continue;
367  $new_attr[] = $val;
368  }
369  $settings['attributes'] = $new_attr;
370  }
371 
372  $settings['replace_keywords'] = array_get_index($request_data, 'replace_keywords', 0);
373 
374  return FALSE;
375 
376  }//end processInterface()
377 
378 
399  public static function addSettingAttributes($value, $settings)
400  {
401  foreach ($value['attribute'] as $attribute) {
402  $attr_name = $attribute['name'][0];
403  $attr_value = $attribute['value'][0];
404  $settings['attributes'][] = Array(
405  'attribute' => $attr_name,
406  'value' => $attr_value,
407  );
408  }
409 
410  return $settings;
411 
412  }//end addSettingAttributes()
413 
414 
415 }//end class
416 
417 ?>