Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_create_clone.inc
1 <?php
17 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
18 require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
19 
33 {
34 
35 
59  public static function execute($settings, &$state)
60  {
61  $clone_broadcaster = array_get_index($settings, 'clone_broadcaster', 1);
62  $link_to_broadcaster = array_get_index($settings, 'link_to_broadcaster', 0);
63 
64  if (($clone_broadcaster == 0) && (empty($settings['clone_assetid']) || !$GLOBALS['SQ_SYSTEM']->am->assetExists($settings['clone_assetid']))) {
65  return FALSE;
66  }
67 
68  if (($link_to_broadcaster == 0) && (empty($settings['assetid']) || !$GLOBALS['SQ_SYSTEM']->am->assetExists($settings['assetid']))) {
69  return FALSE;
70  }
71 
72  if (empty($settings['link_type'])) {
73  return FALSE;
74  }
75 
76  // check the optional settings, assign defaults
77  $is_dependant = array_get_index($settings, 'is_dependant', 0);
78  $is_exclusive = array_get_index($settings, 'is_exclusive', 0);
79  $disallow_duplicates = array_get_index($settings, 'disallow_duplicates', 0);
80 
81  if (empty($state['asset'])) {
82  // grab the asset if assetid is given, but not the asset.
83  if (empty($state['assetid'])) {
84  return FALSE;
85  } else {
86  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
87  }
88  }
89 
90  $am = $GLOBALS['SQ_SYSTEM']->am;
91 
92  $major = NULL;
93  if ($link_to_broadcaster == 1) {
94  $major = $state['asset'];
95  } else {
96  $major = $am->getAsset($settings['assetid']);
97  }
98 
99  $minor = NULL;
100  if ($clone_broadcaster == 1) {
101  $minor = $state['asset'];
102  } else {
103  $minor = $am->getAsset($settings['clone_assetid']);
104  }
105 
106  if ($disallow_duplicates) {
107  // get all the children of the target asset, check whether an asset
108  // with the same name, short_name and type_code already exists
109  $children = $am->getChildren($major->id);
110  foreach ($children as $childid => $child_type) {
111  $name = $am->getAssetInfo(Array($childid), Array(), TRUE, 'name');
112  $short_name = $am->getAssetInfo(Array($childid), Array(), TRUE, 'short_name');
113  $type_code = $am->getAssetInfo(Array($childid), Array(), TRUE, 'type_code');
114  if ($name[$childid] == $minor->name && $short_name[$childid] == $minor->short_name && $type_code[$childid] == $minor->type()) {
115  // a copy of the clone already exists
116  return Array(
117  'assetid' => $childid,
118  'parentid' => $major->id,
119  'link_type' => $settings['link_type'],
120  'value' => $settings['value'],
121  'is_dependant' => $is_dependant,
122  'is_exclusive' => $is_exclusive,
123  );
124  }
125  }
126  }
127 
128  // the new link
129  $link = Array(
130  'asset' => &$major,
131  'link_type' => $settings['link_type'],
132  'value' => $settings['value'],
133  'sort_order' => NULL,
134  'is_dependant' => $is_dependant,
135  'is_exclusive' => $is_exclusive,
136  );
137 
138  // old -> new asset id mapping
139  $clone_map = Array();
140 
141  // Clone only the following atrributes. (tagging will be cloned by cloneLinks)
142  $components = Array(
143  'attributes',
144  'metadata_schemas',
145  'metadata',
146  'workflow',
147  'permissions',
148  'roles',
149  );
150 
151  $clone = $am->cloneAsset($minor, $link, $clone_map, $components);
152 
153  if (is_null($clone)) {
154  trigger_localised_error('CORE0138', E_USER_WARNING);
155  return FALSE;
156  }
157 
158  $valid_paths = make_valid_web_paths($minor->getWebPaths());
159  $good_paths = $GLOBALS['SQ_SYSTEM']->am->webPathsInUse($link['asset'], $valid_paths, $clone->id, TRUE);
160 
161  $clone->saveWebPaths($good_paths);
162  $clone->remapAssetids($clone_map);
163 
164  return Array(
165  'assetid' => $clone->id,
166  'parentid' => $major->id,
167  'link_type' => $settings['link_type'],
168  'value' => $settings['value'],
169  'is_dependant' => $is_dependant,
170  'is_exclusive' => $is_exclusive,
171  );
172 
173  }//end execute()
174 
175 
186  public static function getInterface($settings, $prefix, $write_access=FALSE)
187  {
188  // check settings, set defaults if necessary
189  $clone_assetid = array_get_index($settings, 'clone_assetid', 0);
190  $selected_assetid = array_get_index($settings, 'assetid', 0);
191  $selected_link_type = array_get_index($settings, 'link_type', SQ_LINK_TYPE_1);
192  $value = array_get_index($settings, 'value', '');
193  $is_dependant = array_get_index($settings, 'is_dependant', 0);
194  $is_exclusive = array_get_index($settings, 'is_exclusive', 0);
195  $disallow_duplicates = array_get_index($settings, 'disallow_duplicates', 0);
196  $clone_broadcaster = array_get_index($settings, 'clone_broadcaster', 1);
197  $link_to_broadcaster = array_get_index($settings, 'link_to_broadcaster', 0);
198 
199  // check if the cloned asset is valid
200  if ($clone_assetid && !$GLOBALS['SQ_SYSTEM']->am->assetExists($clone_assetid)) {
201  $clone_assetid = 0;
202  }
203 
204  // check if the selected asset is valid
205  if ($selected_assetid && !$GLOBALS['SQ_SYSTEM']->am->assetExists($selected_assetid)) {
206  $selected_assetid = 0;
207  }
208 
209  $link_type_list = get_link_type_names();
210  unset($link_type_list[SQ_LINK_NOTICE]);
211 
212  if (!$write_access) {
213  $form_element_extras = 'disabled="disabled"';
214  } else {
215  $form_element_extras = '';
216  }
217 
218  // begin buffering basic options
219  ob_start();
220  if ($write_access) {
221  asset_finder($prefix.'[clone_assetid]', $clone_assetid);
222  } else {
223  if ($clone_assetid) {
224  $clone_assetname = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($clone_assetid), Array(), TRUE, 'name');
225  text_box($prefix.'[clone_assetid]', $clone_assetname[$clone_assetid].' (#'.$clone_assetid.')', '', '', FALSE, $form_element_extras);
226  } else {
227  text_box($prefix.'[clone_assetid]', '', '', '', FALSE, $form_element_extras);
228  }
229  }
230 
231  $basic_part_1 = ob_get_contents();
232  ob_end_clean();
233 
234  ob_start();
235  if ($write_access) {
236  asset_finder($prefix.'[assetid]', $selected_assetid);
237  } else {
238  if ($selected_assetid) {
239  $selected_assetname = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($selected_assetid), Array(), TRUE, 'name');
240  text_box($prefix.'[assetid]', $selected_assetname[$selected_assetid].' (#'.$selected_assetid.')', '', '', FALSE, $form_element_extras);
241  } else {
242  text_box($prefix.'[assetid]', '', '', '', FALSE, $form_element_extras);
243  }
244  }
245 
246  $basic_part_2 = ob_get_contents();
247  ob_end_clean();
248 
249  ob_start();
250  combo_box($prefix.'[link_type]', $link_type_list, FALSE, $selected_link_type, NULL, $form_element_extras);
251  $basic_part_3 = ob_get_contents();
252  ob_end_clean();
253 
254  // begin buffering advanced options
255  ob_start();
256  text_box($prefix.'[value]', $value, '', '', FALSE, $form_element_extras);
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  combo_box($prefix.'[is_dependant]', $is_dependant_list, FALSE, $is_dependant, NULL, $form_element_extras);
266  $advanced_part_2 = ob_get_contents();
267  ob_end_clean();
268 
269  ob_start();
270  $is_exclusive_list = Array(
271  0 => translate('non_exclusively'),
272  1 => translate('exclusivly'),
273  );
274  combo_box($prefix.'[is_exclusive]', $is_exclusive_list, FALSE, $is_exclusive, NULL, $form_element_extras);
275  $advanced_part_3 = ob_get_contents();
276  ob_end_clean();
277 
278  ob_start();
279  check_box($prefix.'[disallow_duplicates]', '1', $disallow_duplicates, NULL, $form_element_extras);
280  label(translate('do_not_create_clone_if_same_name_exists'), $prefix.'[disallow_duplicates]');
281  $advanced_part_4 = ob_get_contents();
282  ob_end_clean();
283 
284  ob_start();
285  check_box($prefix.'[clone_broadcaster]', '1', $clone_broadcaster, NULL, $form_element_extras);
286  label(translate('clone_broadcasting_asset'), $prefix.'[clone_broadcaster]');
287  $advanced_part_5 = ob_get_contents();
288  ob_end_clean();
289 
290  ob_start();
291  check_box($prefix.'[link_to_broadcaster]', '1', $link_to_broadcaster, NULL, $form_element_extras);
292  label(translate('link_to_broadcasting_asset'), $prefix.'[link_to_broadcaster]');
293  $advanced_part_6 = ob_get_contents();
294  ob_end_clean();
295 
296 
297  // now patch together the output
298  $contents = translate('trigger_create_clone_basic', $basic_part_1, $basic_part_2, $basic_part_3).
299  '<br /><br />'.
300  '<b>'.translate('triggers_advanced_user_options').'</b><br />'.
301  translate('trigger_create_clone_advanced', $advanced_part_1, $advanced_part_2, $advanced_part_3, $advanced_part_4, $advanced_part_5, $advanced_part_6);
302 
303  return $contents;
304 
305  }//end getInterface()
306 
307 
319  public static function processInterface(&$settings, $request_data)
320  {
321  $link_type_list = get_link_type_names();
322 
323  $clone_broadcaster = array_get_index($request_data, 'clone_broadcaster', 0);
324  $link_to_broadcaster = array_get_index($request_data, 'link_to_broadcaster', 0);
325 
326  // make sure the clone assetid isn't blank if "Clone broadcasting asset" is not selected
327  if (($clone_broadcaster == 0) && empty($request_data['clone_assetid']['assetid'])) {
328  return translate('trigger_clone_asset_missing');
329  }
330 
331  // make sure the assetid isn't blank if "Link to broadcasting asset" is not selected
332  if (($link_to_broadcaster == 0) && empty($request_data['assetid']['assetid'])) {
333  return translate('trigger_clone_parent_missing');
334  }
335 
336  if (empty($request_data['link_type']) || !isset($link_type_list[$request_data['link_type']])) {
337  return translate('trigger_link_type_missing');
338  }
339 
340  $settings['clone_assetid'] = $request_data['clone_assetid']['assetid'];
341  $settings['assetid'] = $request_data['assetid']['assetid'];
342  $settings['link_type'] = $request_data['link_type'];
343  $settings['value'] = array_get_index($request_data, 'value', '');
344  $settings['is_dependant'] = array_get_index($request_data, 'is_dependant', 0);
345  $settings['is_exclusive'] = array_get_index($request_data, 'is_exclusive', 0);
346  $settings['disallow_duplicates'] = array_get_index($request_data, 'disallow_duplicates', 0);
347  $settings['clone_broadcaster'] = $clone_broadcaster;
348  $settings['link_to_broadcaster'] = $link_to_broadcaster;
349 
350  return FALSE;
351 
352  }//end processInterface()
353 
354 
364  public static function getLocks($settings, &$state)
365  {
366  return Array(
367  $state['assetid'] => Array('all'),
368  );
369 
370  }//end getLocks()
371 
372 
373 }//end class
374 
375 ?>