Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_create_file_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 
60  public static function execute($settings, &$state)
61  {
62  // check required settings
63  if (empty($settings['type_code'])) return FALSE;
64  if (empty($settings['parentid'])) return FALSE;
65  if (empty($settings['link_type'])) return FALSE;
66  if (empty($settings['file_path'])) return FALSE;
67  if (!is_readable($settings['file_path'])) {
68  trigger_localised_error('CORE0276', E_USER_WARNING, htmlentities($settings['file_path'], ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET));
69  return FALSE;
70  }
71 
72  if ($settings['parentid'] == 'current_asset') {
73  $settings['parentid'] = $state['assetid'];
74  }
75  $attributes = Array();
76  if (!empty($settings['attributes'])) {
77  $attributes = $settings['attributes'];
78  }
79  $parent = $GLOBALS['SQ_SYSTEM']->am->getAsset($settings['parentid']);
80  if (is_null($parent)) return FALSE;
81 
82  // check the optional settings, assign defaults
83  $is_dependant = array_get_index($settings, 'is_dependant', 0);
84  $is_exclusive = array_get_index($settings, 'is_exclusive', 0);
85  $value = array_get_index($settings, 'value', '');
86 
87  // the new link
88  $link = Array(
89  'asset' => &$parent,
90  'link_type' => $settings['link_type'],
91  'value' => $value,
92  'sort_order' => NULL,
93  'is_dependant' => (bool) $is_dependant,
94  'is_exclusive' => (bool) $is_exclusive,
95  );
96 
97  if (isset($settings['file_name']) === false) {
98  $settings['file_name'] = basename($settings['file_path']);
99  }
100 
101  $info = Array(
102  'name' => $settings['file_name'],
103  'tmp_name' => $settings['file_path'],
104  'non_uploaded_file' => TRUE,
105  );
106 
107  $type_code = $settings['type_code'];
108  $GLOBALS['SQ_SYSTEM']->am->includeAsset($type_code);
109  $new_asset = new $type_code();
110  $new_asset->_tmp['uploading_file'] = TRUE;
111 
112  // set the attributes for the newly created asset
113  if (!empty($attributes)) {
114  foreach ($attributes as $index => $val) {
115  $new_asset->setAttrValue($val['attribute'], $val['value']);
116  }
117  }
118 
119  $linkid = $new_asset->create($link, $info);
120  if (!$linkid) return FALSE;
121 
122  // If we're running at a low runlevel, we need to run the _createAdditional function here
123  if (!$GLOBALS['SQ_SYSTEM']->runLevelEnables(SQ_SECURITY_INTEGRITY)) {
124  if (!$new_asset->createAdditional($link)) {
125  return FALSE;
126  }
127 
128  }
129 
130  $link = $GLOBALS['SQ_SYSTEM']->am->getLinkById($linkid);
131 
132  return Array(
133  'assetid' => $link['minorid'],
134  'parentid' => $link['majorid'],
135  'linkid' => $link['linkid'],
136  'link_type' => $link['link_type'],
137  'value' => $link['value'],
138  'sort_order' => $link['sort_order'],
139  'is_dependant' => $link['is_dependant'],
140  'is_exclusive' => $link['is_exclusive'],
141  );
142 
143  }//end execute()
144 
145 
156  public static function getInterface($settings, $prefix, $write_access=FALSE)
157  {
158  // check settings, set defaults if necessary
159  $parentid = array_get_index($settings, 'parentid', 0);
160  $type_code = array_get_index($settings, 'type_code', 0);
161  $link_type = array_get_index($settings, 'link_type', SQ_LINK_TYPE_1);
162  $value = array_get_index($settings, 'value', '');
163  $is_dependant = array_get_index($settings, 'is_dependant', 0);
164  $is_exclusive = array_get_index($settings, 'is_exclusive', 0);
165  $selected = array_get_index($settings, 'attributes', 0);
166 
167  $attrs = Array();
168  $attributes = Array();
169  if (!empty($type_code)) {
170  // get the attributes of this asset
171  $attrs = $GLOBALS['SQ_SYSTEM']->am->getAssetTypeAttributes($type_code);
172  foreach ($attrs as $attr => $type) {
173  $attributes[$attr] = ucwords(str_replace('_', ' ', $attr)).' - '.ucwords($type['type']);
174  }
175  $attributes[''] = '--SELECT ATTRIBUTE--';
176  }
177 
178  // check if the parent asset is valid
179  $parent_broadcasted = FALSE;
180  if ($parentid == 'current_asset') {
181  $parent_broadcasted = TRUE;
182  $parentid = 0;
183  } else {
184  if ($parentid && !$GLOBALS['SQ_SYSTEM']->am->assetExists($parentid)) {
185  $parentid = 0;
186  }
187  }
188 
189  // begin buffering basic options
190  ob_start();
191  if ($write_access) {
192  asset_type_chooser($prefix.'[type_code]', FALSE, Array($type_code), TRUE, FALSE, FALSE, Array('file'));
193  } else {
194  echo '<b>'.$type_code.'</b>';
195  }
196 
197  $basic_part_1 = ob_get_contents();
198  ob_end_clean();
199 
200 
201  ob_start();
202  if ($write_access) {
203  radio_button($prefix.'[parent_type]', 'explicit', !$parent_broadcasted, '', 'id="parent_type_explicit"');
204  label(translate('under'), 'parent_type_explicit');
205  echo ' ';
206  asset_finder($prefix.'[parentid]', $parentid, Array('asset' => 'D'));
207  echo '<br />';
208  radio_button($prefix.'[parent_type]', 'broadcaster', $parent_broadcasted, '', 'id="parent_type_broadcaster"');
209  label(translate('trigger_create_under_broadcaster'), 'parent_type_broadcaster');
210  } else {
211  if ($parent_broadcasted) {
212  echo translate('trigger_create_under_broadcaster');
213  } else {
214  echo translate('under').' ';
215  if (!empty($parentid)) {
216  echo '<b>'.get_asset_tag_line($parentid).'</b>';
217  } else {
218  echo '<b>['.translate('no_parent_selected').']</b>';
219  }
220  }
221  }
222 
223  $basic_part_2 = ob_get_contents();
224  ob_get_clean();
225 
226  ob_start();
227  $link_type_list = get_link_type_names();
228  unset($link_type_list[SQ_LINK_NOTICE]);
229  if ($write_access) {
230  combo_box($prefix.'[link_type]', $link_type_list, FALSE, $link_type);
231  } else {
232  echo '<b>'.$link_type_list[$link_type].'</b>';
233  }
234 
235  $basic_part_3 = ob_get_contents();
236  ob_end_clean();
237 
238  ob_start();
239  if ($write_access) {
240  text_box($prefix.'[file_path]', array_get_index($settings, 'file_path', ''), 40);
241  } else {
242  echo array_get_index($settings, 'file_path', '');
243  }
244  $basic_part_4 = ob_get_contents();
245  ob_end_clean();
246 
247  // begin buffering advanced options
248  ob_start();
249  if ($write_access) {
250  text_box($prefix.'[value]', $value, '', '');
251  } else {
252  if (empty($value)) {
253  $value = '['.translate('no_value_entered').']';
254  }
255  echo '<b>'.$value.'</b>';
256  }
257  $advanced_part_1 = ob_get_contents();
258  ob_end_clean();
259 
260  ob_start();
261  $is_dependant_list = Array(
262  0 => translate('non_dependently'),
263  1 => translate('dependently'),
264  );
265  if ($write_access) {
266  combo_box($prefix.'[is_dependant]', $is_dependant_list, FALSE, $is_dependant);
267  } else {
268  echo '<b>'.$is_dependant_list[$is_dependant].'</b>';
269  }
270 
271  $advanced_part_2 = ob_get_contents();
272  ob_end_clean();
273 
274  ob_start();
275  $is_exclusive_list = Array(
276  0 => translate('non_exclusively'),
277  1 => translate('exclusively'),
278  );
279  if ($write_access) {
280  combo_box($prefix.'[is_exclusive]', $is_exclusive_list, FALSE, $is_exclusive);
281  } else {
282  echo '<b>'.$is_exclusive_list[$is_exclusive].'</b>';
283  }
284 
285  $advanced_part_3 = ob_get_contents();
286  ob_end_clean();
287 
288  ob_start();
289  if (!empty($attributes)) {
290  $count = 0;
291  if (!empty($selected)) {
292  $count = count($selected);
293  foreach ($selected as $index => $attr) {
294  if ($write_access) {
295  combo_box($prefix.'[attributes]['.$index.'][attribute]', $attributes, FALSE, $attr['attribute']);
296  text_box($prefix.'[attributes]['.$index.'][value]', $attr['value']);
297  check_box($prefix.'[attributes]['.$index.'][delete]','delete', FALSE);
298  echo translate('delete_question');
299  echo '<br />';
300  } else {
301  echo ucwords($attr['attribute']).': '.$attr['value'].'<br />';
302  }
303  }
304  } else {
305  if (!$write_access) {
306  echo '<i>('.translate('none').')</i>';
307  }
308  }
309  if ($write_access) {
310  combo_box($prefix.'[attributes]['.$count.'][attribute]', $attributes, FALSE, '');
311  text_box($prefix.'[attributes]['.$count.'][value]', '');
312  echo '<br />';
313  }
314  }
315  $attributes = ob_get_contents();
316  ob_end_clean();
317 
318  // now patch together the output
319  $contents = translate('trigger_new_asset_basic', $basic_part_1, $basic_part_2, $basic_part_3).
320  '<br />and populate it with the file located on the server at '.$basic_part_4.
321  '<br /><br />'.
322  '<b>'.translate('triggers_advanced_user_options').'</b><br />'.
323  translate('trigger_new_asset_advanced', $advanced_part_1, $advanced_part_2, $advanced_part_3).'<br />'.
324  translate('set_attributes').':<br />'.$attributes;
325  if ($write_access) {
326  $contents .= '<p class="sq-backend-warning"><b>'.translate('trigger_create_file_required_attrs_warning').'</b></p>';
327  }
328 
329  return $contents;
330 
331  }//end getInterface()
332 
333 
345  public static function processInterface(&$settings, $request_data)
346  {
347  // make sure the parentid isn't blank
348  if (($request_data['parent_type'] != 'broadcaster') && empty($request_data['parentid']['assetid'])) {
349  return translate('parent_to_create_under_is_missing');
350  }
351 
352  if (!isset($request_data['file_path']) || empty($request_data['file_path'])) {
353  return translate('trigger_action_create_file_asset_file_path_not_set');
354  }
355 
356  $link_type_list = get_link_type_names();
357  if (empty($request_data['link_type']) || !isset($link_type_list[$request_data['link_type']])) {
358  return translate('link_type_missing');
359  }
360 
361  if (($request_data['parent_type'] == 'explicit')) {
362  $settings['parentid'] = $request_data['parentid']['assetid'];
363  } else {
364  $settings['parentid'] = 'current_asset';
365  }
366  $settings['link_type'] = $request_data['link_type'];
367 
368  $type_code = array_get_index($request_data, 'type_code', FALSE);
369  if (!$type_code) {
370  return translate('asset_type_not_specified');
371  }
372 
373  $settings['type_code'] = $type_code;
374  $settings['file_path'] = $request_data['file_path'];
375 
376  // optional fields
377  $settings['value'] = array_get_index($request_data, 'value', '');
378  $settings['is_dependant'] = array_get_index($request_data, 'is_dependant', 0);
379  $settings['is_exclusive'] = array_get_index($request_data, 'is_exclusive', 0);
380 
381  // if the user has chosen attributes and their default values
382  if (isset($request_data['attributes']) && !empty($request_data['attributes'])) {
383  $attribute = $request_data['attributes'];
384  $new_attr = Array();
385  foreach ($attribute as $index => $val) {
386  if (empty($val['attribute']) || empty($val['value'])) {
387  continue;
388  }
389  if (isset($val['delete'])) continue;
390  $new_attr[] = $val;
391  }
392  $settings['attributes'] = $new_attr;
393  }
394  return FALSE;
395 
396  }//end processInterface()
397 
398 
399 }//end class
400 
401 ?>