Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
js_api.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/page/page.inc';
18 
29 class JS_Api extends Page
30 {
31 
32  /*
33  * maping array for the function
34  * and the attribute to enable them
35  */
36  private $_attribute_mapping = Array(
37  'getAssetTypes' => 'get_asset_types',
38  'trashAsset' => 'trash_asset',
39  'getChildCount' => 'get_general',
40  'updateMultipleLinks' => 'update_link',
41  'removeMultipleLinks' => 'remove_link',
42  'importAssetsFromXML' => 'import_assets',
43  'getRoles' => 'get_roles',
44  'executeHTMLTidy' => 'execute_html_tidy',
45  'getGeneral' => 'get_general',
46  'getAttributes' => 'get_attributes',
47  'setMultipleAttributes' => 'set_attribute',
48  'setAttribute' => 'set_attribute',
49  'getMetadata' => 'get_metadata',
50  'setMetadataAllFields' => 'set_metadata',
51  'setMetadata' => 'set_metadata',
52  'getChildren' => 'get_children',
53  'getParents' => 'get_parents',
54  'getPermissions' => 'get_permissions',
55  'createAsset' => 'create_asset',
56  'getLocksInfo' => 'get_locks_info',
57  'acquireLock' => 'acquire_lock',
58  'releaseLock' => 'release_lock',
59  'createLink' => 'create_link',
60  'removeLink' => 'remove_link',
61  'moveLink' => 'move_link',
62  'updateLink' => 'update_link',
63  'getLinkId' => 'get_link_id',
64  'getAssetTree' => 'get_asset_tree',
65  'getKeywordsReplacements' => 'get_keywords_replacements',
66  'setAssetStatus' => 'set_asset_status',
67  'getWebPath' => 'get_web_path',
68  'setWebPath' => 'set_web_path',
69  'getWorkflowSchema' => 'get_workflow_schema',
70  'createFileAsset' => 'create_asset',
71  'setContentOfEditableFileAsset' => 'set_file_content',
72  'getLineage' => 'get_lineage',
73  'getLineageFromUrl' => 'get_lineage_from_url',
74  'getUrlFromLineage' => 'get_url_from_lineage',
75  'batchRequest' => 'batch_requests',
76  'cloneAsset' => 'clone_asset',
77  'showDifference' => 'show_diff',
78  'setContext' => 'set_context',
79  'restoreContext' => 'set_context',
80  'getAlternateContext' => 'get_context',
81  'getCurrentContext' => 'get_context',
82  'getAllContexts' => 'get_context',
83  'getMetadataSchema' => 'get_metadata_schema',
84  );
85 
86  /*
87  * array mapping the function call
88  * the permission needed to access them
89  */
90  private $_permission_mapping = Array(
91  'getGeneral' => Array('read_access'),
92  'getAttributes' => Array('read_access'),
93  'getMetadata' => Array('read_access'),
94  'getChildren' => Array('read_access'),
95  'getParents' => Array('read_access'),
96  'getPermissions' => Array('read_access'),
97  'getLocksInfo' => Array('read_access'),
98  'getLinkId' => Array('read_access'),
99  'getAssetTree' => Array('read_access'),
100  'getKeywordsReplacements' => Array('read_access'),
101  'getWebPath' => Array('read_access'),
102  'getWorkflowSchema' => Array('read_access'),
103  'showDifference' => Array('read_access'),
104  'setMultipleAttributes' => Array('write_access'),
105  'setAttribute' => Array('write_access'),
106  'setMetadataAllFields' => Array('write_access'),
107  'setMetadata' => Array('write_access'),
108  'createAsset' => Array('write_access', 'ignore_perm'),
109  'cloneAsset' => Array('write_access', 'ignore_perm'),
110  'createFileAsset' => Array('write_access', 'ignore_perm'),
111  'acquireLock' => Array('write_access'),
112  'releaseLock' => Array('write_access'),
113  'createLink' => Array('write_access'),
114  'removeLink' => Array('write_access'),
115  'moveLink' => Array('write_access'),
116  'updateLink' => Array('write_access'),
117  'setAssetStatus' => Array('write_access'),
118  'setWebPath' => Array('write_access'),
119  'setContentOfEditableFileAsset' => Array('write_access'),
120  'getMetadataSchema' => Array('write_access'),
121  'getAssetTypes' => Array('no_check'),
122  'trashAsset' => Array('no_check'),
123  'getChildCount' => Array('no_check'),
124  'updateMultipleLinks' => Array('no_check'),
125  'removeMultipleLinks' => Array('no_check'),
126  'importAssetsFromXML' => Array('no_check'),
127  'getRoles' => Array('no_check'),
128  'executeHTMLTidy' => Array('no_check'),
129  'getLineage' => Array('no_check'),
130  'getLineageFromUrl' => Array('no_check'),
131  'getUrlFromLineage' => Array('no_check'),
132  'batchRequest' => Array('no_check'),
133  'getAlternateContext' => Array('no_check'),
134  'getCurrentContext' => Array('no_check'),
135  'getAllContexts' => Array('no_check'),
136  'setContext' => Array('no_check'),
137  'restoreContext' => Array('no_check'),
138  );
139 
140 
147  function __construct($assetid=0)
148  {
149  $this->_ser_attrs = TRUE;
150  parent::__construct($assetid);
151 
152  }//end constructor
153 
154 
165  protected function _createAdditional(Array &$link)
166  {
167  if (!parent::_createAdditional($link)) return FALSE;
168 
169  // Create a random key
170  $GLOBALS['SQ_SYSTEM']->am->acquireLock($this->id, 'attributes');
171  $key = rand(1000000000, 9999999999);
172  $this->setAttrValue('api_key', $key);
173  $this->saveAttributes();
174  $GLOBALS['SQ_SYSTEM']->am->releaseLock($this->id, 'attributes');
175 
176  return $this->makeAndSaveInitialWebPath(strtolower($this->attr('name').'.js'), $link);
177 
178  }//end _createAdditional()
179 
180 
188  public function _getAllowedLinks()
189  {
190  $links = parent::_getAllowedLinks();
191  $links[SQ_LINK_NOTICE] = Array('asset' => Array('card' => 'M', 'exclusive' => FALSE));
192  return $links;
193 
194  }//end _getAllowedLinks()
195 
196 
203  function printFrontend()
204  {
205  // Make sure the user can log in first
206  if (!$this->readAccess()) {
207  $GLOBALS['SQ_SYSTEM']->paintLogin(translate('login'), translate('cannot_access_asset', $this->name));
208  return;
209  }
210 
211  // Should we print our JS file, or send back some JSON?
212  if (!isset($_REQUEST['key']) && !isset($_REQUEST['id']) && !isset($_REQUEST['type'])) {
213  header('Content-Type: text/javascript');
214  header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
215  if ($this->attr('use_enhanced_js')) {
216  require_once 'js_api_enhanced_function_list.inc';
218  } else {
219  require_once 'js_api_standard_function_list.inc';
221  }
222  return;
223  }
224 
225  // Overriding the matrix error handler so HTML doesn't get printed to the screen
226  require_once dirname(__FILE__).'/js_api_error_handler.inc';
227  $old_error_handler = set_error_handler('js_api_error_handler');
228 
229  // Get our JSON data that was sent, then encode it into an array
230  $api_key = array_get_index($_REQUEST, 'key', '');
231  $id = array_get_index($_REQUEST, 'id', '');
232  // if id is still empty try getting 'assetid'
233  if (empty($id)) $id = array_get_index($_REQUEST, 'assetid', '');
234  $function_type = array_get_index($_REQUEST, 'type', '');
235 
236  // nonce token check
237  $nonce_token = get_unique_token();
238  if (!isset($_POST['nonce_token']) || ($_POST['nonce_token'] !== $nonce_token)) {
239  $this->returnError('nonce token is invalid');
240  restore_error_handler();
241  return;
242  }
243 
244  // Check to see if we have a function type
245  if (empty($function_type)) {
246  $this->returnError('You must set a function type');
247  restore_error_handler();
248  return;
249  }
250 
251  if ($this->attr('sync_context') && isset($_SESSION[SQ_SESSION_SANDBOX_INDEX]['SQ_BACKEND_ALTERNATE_CONTEXT_ID'])) {
252  $current_contextid = $_SESSION[SQ_SESSION_SANDBOX_INDEX]['SQ_BACKEND_ALTERNATE_CONTEXT_ID'];
253  if (!isset($_SESSION[SQ_SESSION_SANDBOX_INDEX]['SQ_ALTERNATE_CONTEXT_ID']) ||
254  $_SESSION[SQ_SESSION_SANDBOX_INDEX]['SQ_ALTERNATE_CONTEXT_ID'] != $current_contextid) {
255  $_SESSION[SQ_SESSION_SANDBOX_INDEX]['SQ_ALTERNATE_CONTEXT_ID'] = $current_contextid;
256  $GLOBALS['SQ_SYSTEM']->changeContext($current_contextid);
257  }
258  }
259 
260  if ($function_type == 'batchRequest') {
261  // check to see if batching is allowed else complain
262  if (!$this->_attribute_mapping[$function_type]) {
263  $this->returnError('The '.$type.' function must be activated');
264  restore_error_handler();
265  return;
266  }
267  if (!function_exists('json_decode')) {
268  require_once 'Services/JSON.php';
269  $json = new Services_JSON();
270  $functions_info = Array();
271  $request_data = $json->decode($_REQUEST['functions']);
272  $functions_info = $this->object2Array($request_data);
273  } else {
274  $functions_info = json_decode($_REQUEST['functions'], TRUE);
275  }//end else
276 
277  foreach ($functions_info as $index => $info) {
278  $function_calls[$index] = $info['function'];
279  }
280  $batching_request = TRUE;
281  } else {
282  $function_calls = Array($function_type);
283  $batching_request = FALSE;
284  }
285 
286  // Set our API Key
287  $check_key = $this->attr('api_key');
288 
289  // Check to see if we even have an API Key
290  if (!isset($check_key) || $check_key == '' || $api_key == '') {
291  $this->returnError('You must enter an API key in order to use this asset');
292  restore_error_handler();
293  return;
294  }
295 
296  // Check to see if the API key attribute matches the JSON key
297  if ($check_key != $api_key) {
298  $this->returnError('The API key does not match, please enter a valid key');
299  restore_error_handler();
300  return;
301  }
302 
303  // #4372 Feature Request : JS API ability to specify _admin/_edit access
304  // Lets populate our $_REQUEST var so that we can use it later :)
305  $_REQUEST['force_simple_edit'] = $this->attr('force_simple_edit');
306  $temp_server_vars = $_SERVER['PHP_SELF'];
307 
308  if (isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER'])) {
309  $referer_url = str_replace(current_protocol().'://', '', $_SERVER['HTTP_REFERER']);
310 
311  $suffix_to_check = Array(
312  SQ_CONF_BACKEND_SUFFIX,
313  SQ_CONF_LIMBO_SUFFIX,
314  SQ_CONF_NOCACHE_SUFFIX,
315  SQ_CONF_RECACHE_SUFFIX,
316  SQ_CONF_PERFORMANCE_SUFFIX
317  );
318  // if we are in Backend or limbo remove everything after /_edit or /_admin
319  // or else getAssetFromURL will get confused and wont return us correct asset
320  foreach ($suffix_to_check as $suffix) {
321  if (strpos($_SERVER['HTTP_REFERER'], $suffix) !== FALSE) {
322  $referer_url = preg_replace('|/'.$suffix.'(.)*|', '', $referer_url);
323  }
324  }
325 
326  $url_asset_object = $GLOBALS['SQ_SYSTEM']->am->getAssetFromURL(current_protocol(), $referer_url, TRUE, TRUE);
327  if (!is_null($url_asset_object) ) {
328  $_SERVER['PHP_SELF'] = str_replace(current_protocol().'://'.$_SERVER['HTTP_HOST'], '', $_SERVER['HTTP_REFERER']);
329  }
330 
331  if (strpos($_SERVER['HTTP_REFERER'], SQ_CONF_LIMBO_SUFFIX) !== FALSE || $this->attr('force_simple_edit')) {
332  $_SERVER['PHP_SELF'] = preg_replace('|/'.SQ_CONF_LIMBO_SUFFIX.'(.)*|', '', $_SERVER['PHP_SELF']);
333  $_SERVER['PHP_SELF'] = preg_replace('|/'.SQ_CONF_BACKEND_SUFFIX.'(.)*|', '', $_SERVER['PHP_SELF']);
334  $_SERVER['PHP_SELF'] = $_SERVER['PHP_SELF'].'/';
335  $_REQUEST['force_simple_edit'] = TRUE;
336  }
337  }
338 
339  // keep are results array empty to start with
340  $results = Array();
341 
342  // if we are batching the request lets keep the
343  // track of the number of functions we are executing
344  $track_func = 0;
345  $replace_these = Array ('%26' , '%23' , '%3F' , '%2B');
346  $replace_with = Array ('&' , '#' , '?' , '+');
347 
348  foreach ($function_calls as $type) {
349  // lets decide on what arguments we need to pass through for execution
350  $blocking = FALSE;
351  if ($batching_request) {
352  $args = $functions_info[$track_func]['args'];
353  $blocking = (isset($functions_info[$track_func]['blocking']) && $functions_info[$track_func]['blocking']) ? TRUE : FALSE;
354  // let the user use 'id' and 'asset_id' to pass in the asset id to work on in the argmunets
355  // bug fix #5945 Inconsistency witht he parameter needs to be passed to (advanced) JS API functions
356  // few of the functions in the advanced API passes the assetid as a different index name. Make sure
357  // to look for them and change it here.
358  if (!isset($args['id'])) {
359  if (isset($args['asset_id'])) {
360  $args['id'] = $args['asset_id'];
361  } else if (isset($args['child_id'])) {
362  $args['id'] = $args['child_id'];
363  } else if (isset($args['parentID'])) {
364  $args['id'] = $args['parentID'];
365  } else if (isset($args['parent_id'])) {
366  $args['id'] = $args['parent_id'];
367  }
368  }
369 
370  // so we are batching requests
371  // in this case we get the assetid needed
372  // to start with from the first function call
373  // and then every consecutive time get it from the output of previously
374  // executed function is the format there is %results_0_*% type
375  // else it might be a raw input and we use that
376  if ($track_func === 0 || strpos($args['id'], '%results_') === FALSE) {
377  $id = $args['id'];
378  } else {
379  // okie so we arent the first called function in batched request
380  // and we are to use the assetid from the previous function exec
381  $keyword = $args['id'];
382  $instructions = explode('_', trim($keyword, '%'));
383  // if we know where to take the new id from, do it or
384  // else just continue with the previous one we have been using
385  $string_to_eval = '';
386  if (count($instructions) >= 3) {
387  for ($i = 1; $i < count($instructions); $i++) {
388  $string_to_eval .= '[$'."instructions[$i]]";
389  }
390  eval('$id = $results'.$string_to_eval.';');
391  }
392  }
393  } else {
394  // old API way
395  $args = $_REQUEST;
396  }
397 
398  // fix our args elements
399  foreach ($args as $index => $arg) {
400  $args[$index] = str_replace($replace_these, $replace_with, $args[$index]);
401  }
402 
403  // first thing check if the function that is being tried to executed is enabled
404  if ($this->attr($this->_attribute_mapping[$type])) {
405  $asset = NULL;
406  if ($this->_permission_mapping[$type] != Array('no_check')) {
407  // Check to see if we supplied with valid assetid
408  if ((preg_match('/^[0-9]*:/i', $id) != FALSE) && !$GLOBALS['SQ_SYSTEM']->am->assetExists($id)) {
409  $results[$track_func]['error'] = 'Assetid supplied doesnt exist on the system';
410  $track_func++;
411  break;
412  }
413  $asset = $this->setAsset($id);
414 
415  // Check to see if passed ID is under our root node, or, if we are using a root node
416  if (!$this->checkRoot($asset)) {
417  $this->returnError('You do not have permissions to access this asset');
418  restore_error_handler();
419  return;
420  }
421 
422  if ($this->_permission_mapping[$type] == Array('read_access')) {
423  $ra = $asset->readAccess();
424  $wa = FALSE;
425  } else {
426  // check if there is any runing workflow schema on it
427  $wfm = $GLOBALS['SQ_SYSTEM']->getWorkflowManager();
428  $running_wfs = $wfm->getSchemas($asset->id, TRUE, TRUE);
429  if(!empty($running_wfs)) {
430  $wa = $asset->writeAccess();
431  } else {
432  $wa = $asset->writeAccess('', Array(), FALSE);
433  }
434  $ra = FALSE;
435  }
436  }
437 
438  $func_name = '_'.$type;
439  try {
440  $results[$track_func] = $this->$func_name($args, $asset, $ra, $wa);
441  } catch (Exception $e) {
442  // If any exception was thrown when executing the function, catch it
443  $results[$track_func]['error'] = 'Exception occured when executing JS API function "'.$type.'()": '.$e->getMessage();
444  }
445 
446  // if batching request and one of the function is set to blocking
447  // and of the function execution returned error bail out
448  if (isset($results[$track_func]['error']) && $blocking) {
449  $results['error'] = 'Function '.$type.' set to blocking returned error';
450  break;
451  }
452 
453  } else {
454  $this->returnError('The '.$type.' function must be activated');
455  restore_error_handler();
456  return;
457  }
458  $track_func++;
459  }
460 
461  // if we are batching request, send back all the data we have
462  // if not, then pick the 0th index of the results because thats our baby
463  if ($function_type == 'batchRequest') {
464  $data = $results;
465  } else {
466  $data = $results[$track_func - 1];
467  }
468 
469  // Send our data as JSON
470  if (isset($data)) {
471  $this->returnJSON($data, $type);
472  }//end if
473 
474  // lets restor PHP_SELF just incase if Matrix is going to check if $_SERVER var is
475  // exactly the same when it went in THIS function and when it goes out
476  $_SERVER['PHP_SELF'] = $temp_server_vars;
477 
478  // Restore error handler
479  restore_error_handler();
480 
481  }//end printFrontend()
482 
483 
492  function setAsset($id)
493  {
494  // Set some shortcuts
495  $am = $GLOBALS['SQ_SYSTEM']->am;
496 
497  // Check to see if the asset id is real
498  if ($id == '') {
499  $this->returnError('You must enter a valid asset id or URL');
500  return FALSE;
501  }
502 
503  // Check to see if we are using an id or URL
504  if (is_numeric($id) || (preg_match('/^[0-9]*:/i', $id) != 0)) {
505 
506  // Check to see if the asset id is real
507  try {
508  $valid_assetid = $am->assetExists($id);
509  } catch (Exception $e) {
510  $this->returnError($id.' is not a valid asset id');
511  return FALSE;
512  }
513  if (!$valid_assetid) {
514  $this->returnError($id.' is not a valid asset id');
515  return FALSE;
516  }
517  // Set our asset reference
518  return $am->getAsset($id);
519 
520  } else {
521  // We need to make sure this is a URL
522  if (strpos($id, 'http://') !== FALSE || strpos($id, 'https://') !== FALSE) {
523  // figure out if we are on dealing with a http or a https url here
524  // results will be wrong here further if we let Matrix assume if the
525  // protocol is always http
526  $url_parts = parse_url($id);
527  $protocol = (isset($url_parts['scheme']) && !empty($url_parts['scheme'])) ? $url_parts['scheme'] : 'http';
528 
529  // Set our asset reference
530  $asset = $am->getAssetFromURL($protocol, strip_url($id, TRUE), TRUE, TRUE);
531  if (is_null($asset)) $this->returnError($id.' is not a valid URL');
532  return $asset;
533  } else {
534  $this->returnError($id.' is not a valid URL');
535  return FALSE;
536  }//end else
537 
538  }//end else
539 
540  }//end setAsset
541 
542 
556  private function _getAssetTypes($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
557  {
558  $data = $GLOBALS['SQ_SYSTEM']->am->getAssetTypes(TRUE, TRUE);
559  // Sort in A-Z order
560  ksort($data);
561 
562  return $data;
563 
564  }// end _getAssetTypes
565 
566 
580  private function _trashAsset($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
581  {
582  if (isset($args['assetid']) && !empty($args['assetid'])) {
583  $assetids = explode('\\,', $args['assetid']);
584  $data = $this->trashAsset($assetids);
585  } else {
586  $data['error'] = 'Please Enter Asset Id(s) to move to Trash';
587  }
588 
589  return $data;
590 
591  }// end _trashAsset
592 
593 
608  private function _getChildCount($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
609  {
610  if (isset($args['asset_id']) && !empty($args['asset_id'])) {
611  $assetid = $args['asset_id'];
612  // Check to see how deep we should go
613  if ($args['levels'] == '0') {
614  $level = NULL;
615  } else {
616  $level = $args['levels'];
617  }
618  $children = $GLOBALS['SQ_SYSTEM']->am->getChildren($assetid, '', TRUE, NULL, NULL, NULL, TRUE, NULL, $level);
619  $data['child_count'] = count($children);
620  } else {
621  $data['error'] = 'Please Enter Asset Id to get the child count';
622  }
623 
624  return $data;
625 
626  }// end _getChildCount
627 
628 
642  private function _updateMultipleLinks($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
643  {
644  $links = $data = Array();
645  $links['parents'] = explode('\\,', $args['parent_id']);
646  $links['children'] = explode('\\,', $args['child_id']);
647  $links['existing_link_type'] = explode('\\,', $args['existing_link_type']);
648  $links['existing_link_value'] = explode('\\,', $args['existing_link_value']);
649  $links['link_type'] = explode('\\,', $args['link_type']);
650  $links['link_value'] = explode('\\,', $args['link_value']);
651  $links['sort_order'] = explode('\\,', $args['sort_order']);
652  $links['lock'] = explode('\\,', $args['locked']);
653 
654  foreach ($links as $elements) {
655  if (count($elements) != count($links['parents'])) {
656  $data['error'] = 'Please provide correct number of link elements';
657  return $data;
658  }
659  }
660 
661  //looks like correct number of elements are passed
662  for ($i = 0; $i < count($links['parents']); $i++) {
663  // Update our link
664  if($links['children'][$i] == 'Undefined' || $links['children'][$i] == '' ) {
665  $data[$i]['error'] = 'Child Id whose link is to be update was not provided';
666  continue;
667  } else if ($links['parents'][$i] == 'Undefined' || $links['parents'][$i] == ''){
668  $data[$i]['error'] = 'Parent Id whose link is to be update was not provided';
669  continue;
670  }
671 
672  $child_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($links['children'][$i]);
673  if(is_null($child_asset)) {
674  $data[$i]['error'] = 'Asset #'.$links['children'][$i].' does not exist';
675  continue;
676  }
677 
678  $parent_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($links['parents'][$i]);
679  if(is_null($parent_asset)) {
680  $data[$i]['error'] = 'Asset #'.$links['parents'][$i].' does not exist';
681  continue;
682  }
683 
684  if ($child_asset->writeAccess()) {
685 
686  $ext_link_type = defined($links['existing_link_type'][$i]) ? constant($links['existing_link_type'][$i]) : SQ_LINK_TYPE_1;
687  $ext_link_value = $links['existing_link_value'][$i];
688  $link_type = defined($links['link_type'][$i]) ? constant($links['link_type'][$i]) : SQ_LINK_TYPE_1;
689  $link_value = $links['link_value'][$i];
690  $data[$i] = $this->updateLink(
691  $links['parents'][$i] ,
692  $links['children'][$i] ,
693  $ext_link_type,
694  $ext_link_value,
695  $link_type,
696  $link_value ,
697  ($links['sort_order'][$i] != '' && $links['sort_order'][$i] != 'undefined') ? $links['sort_order'][$i] : NULL ,
698  ($links['lock'][$i] != '' && $links['lock'][$i] != 'undefined') ? $links['lock'][$i] : TRUE
699  );
700  } else {
701  $data[$i] = 'Not enough permission to update link between Asset "'.$child_asset->name.'" (#'.$child_asset->id.') and for Asset "'.$parent_asset->name.'" (#'.$parent_asset->id.')';
702  }
703  }
704 
705  return $data;
706 
707  }// end _updateMultipleLinks
708 
709 
723  private function _removeMultipleLinks($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
724  {
725  $links = $data = Array();
726  $links['parents'] = explode('\\,', $args['parent_id']);
727  $links['children'] = explode('\\,', $args['child_id']);
728  $links['link_type'] = explode('\\,', $args['link_type']);
729  $links['link_value'] = explode('\\,', $args['link_value']);
730 
731  foreach ($links as $elements) {
732  if (count($elements) != count($links['parents'])) {
733  $data['error'] = 'Please provide correct number of link elements';
734  return $data;
735  }
736  }
737 
738  //looks like correct number of elements are passed
739  for ($i = 0; $i < count($links['parents']); $i++) {
740  // Update our link
741  if($links['children'][$i] == 'Undefined' || $links['children'][$i] == '' || $links['children'][$i] == NULL) {
742  $data[$i]['error'] = 'Child Id whose link is to be update was not provided';
743  continue;
744  } else if ($links['parents'][$i] == 'Undefined' || $links['parents'][$i] == ''|| $links['children'][$i] == NULL){
745  $data[$i]['error'] = 'Parent Id whose link is to be update was not provided';
746  continue;
747  }
748 
749  $child_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($links['children'][$i]);
750  if(is_null($child_asset)) {
751  $data[$i]['error'] = 'Asset #'.$links['children'][$i].' does not exist';
752  continue;
753  }
754 
755  $parent_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($links['parents'][$i]);
756  if(is_null($parent_asset)) {
757  $data[$i]['error'] = 'Asset #'.$links['parents'][$i].' does not exist';
758  continue;
759  }
760 
761  if ($child_asset->writeAccess()) {
762 
763  $link_value = $links['link_value'][$i];
764  $link_type = defined($links['link_type'][$i]) ? constant($links['link_type'][$i]) : SQ_LINK_TYPE_1;
765 
766  $data[$i] = $this->removeLink($parent_asset->id, $child_asset->id, $link_type, $link_value);
767  } else {
768  $data[$i] = 'Not enough permission to remove link between Asset "'.$child_asset->name.'" (#'.$child_asset->id.') and for Asset "'.$parent_asset->name.'" (#'.$parent_asset->id.')';
769  }
770  }
771 
772  return $data;
773 
774  }// end _removeMultipleLinks
775 
776 
790  private function _importAssetsFromXML($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
791  {
792  $data =Array();
793  $filePath = $args['filePath'];
794  $import_under = $args['id'];
795 
796  //check if the file exists
797  if (file_exists($filePath) || $filePath == 'no_file_provided') {
798  //check 2 make sure it is a XML file
799  if (strtolower(substr($filePath, -3)) == 'xml') {
800  //looks all good...makes sure the asset id provided is valid
801  if($GLOBALS['SQ_SYSTEM']->am->assetExists($import_under)) {
802  // set HIPO running_vars
803  $vars['file_info'] = $filePath;
804  $vars['create_under_assetid'] = $import_under;
805  $vars['delete_after_import'] = FALSE;
806 
807  // run HIPO job
808  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
809  $errors = $hh->freestyleHipo('hipo_job_import_assets_from_xml', $vars, SQ_PACKAGES_PATH.'/import_tools/hipo_jobs');
810  if (empty($errors)) {
811  $data['success'] = 'Assets sucessfully imported under Asset #'.$import_under;
812  } else {
813  $data['error'] = 'Error occured while importing assets under Asset #'.$import_under;
814  }
815 
816  } else {
817  $data['error'] = 'Assetid provided to import assets under is not valid';
818  }
819  } else {
820  $data['error'] = 'File provided is not XML';
821  }
822  } else {
823  $data['error'] = 'No File path provided OR File does not exist for the provided path';
824  }
825 
826  return $data;
827 
828  }// end _importAssetsFromXML
829 
830 
844  private function _getRoles($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
845  {
846  $assetid = isset($args['id']) && $args['id'] != '' ? $args['id'] : NULL;
847  $roleid = isset($args['roleid']) && $args['roleid'] != '' ? $args['roleid'] : NULL;
848  $userid = isset($args['userid']) && $args['userid'] != '' ? $args['userid'] : NULL;
849  $include_assetid = isset($args['include_assetid']) && $args['include_assetid'] != '' ? $args['include_assetid'] : FALSE;
850  $include_globals = isset($args['include_globals']) && $args['include_globals'] != '' ? $args['include_globals'] : FALSE;
851  $expand_groups = isset($args['expand_groups']) && $args['expand_groups'] != '' ? $args['expand_groups'] : FALSE;
852  $inc_dependants = isset($args['inc_dependants']) && $args['inc_dependants'] != '' ? $args['inc_dependants'] : TRUE;
853  $include_parents = isset($args['include_parents']) && $args['include_parents'] != '' ? $args['include_parents'] : FALSE;
854  $type_codes = (isset($args['type_codes']) && $args['type_codes'] != '') ? explode('\\,', $args['type_codes']) : Array();
855  $strict_type_code = isset($args['strict_type_code']) && $args['strict_type_code'] != '' ? $args['strict_type_code'] : TRUE;
856 
857  $data = $GLOBALS['SQ_SYSTEM']->am->getRole($assetid, $roleid, $userid, $include_assetid, $include_globals, $expand_groups, $inc_dependants, $include_parents, $type_codes,$strict_type_code);
858 
859  if(empty($data)) $data[] = 'No roles data is found';
860  return $data;
861 
862  }// end _getRoles
863 
864 
878  private function _executeHTMLTidy($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
879  {
880  $str_to_process = isset($args['content']) && $args['content'] != '' ? $args['content'] : NULL;
881 
882  if (!is_null($str_to_process)) {
883 
884  global $ROOT_PATH;
885  $ROOT_PATH = SQ_FUDGE_PATH.'/wysiwyg/';
886  require_once SQ_FUDGE_PATH.'/wysiwyg/plugins/html_tidy/html_tidy.inc';
887 
888  $tidy = new HTML_Tidy();
889  $tidy->process($str_to_process);
890 
891  if ($tidy->htmltidy_status == 'pass') {
892  $data[] = $str_to_process;
893 
894  } else if ($tidy->htmltidy_status == 'fail') {
895  $data['error'] = 'HTML Tidy failed to process the given content';
896 
897  } else if ($tidy->htmltidy_status == 'wait') {
898  $data['error'] = 'HTML Tidy wasn\'t ready to process content. Try again';
899 
900  } else if ($tidy->htmltidy_status == 'disabled') {
901  $data['error'] = 'HTML Tidy is disabled on the System';
902  }
903 
904  } else {
905  $data['error'] = 'No string passes to process HTMLTidy on';
906  }
907 
908  return $data;
909 
910  }// end _executeHTMLTidy
911 
912 
926  private function _getGeneral($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
927  {
928  if ($read_access) {
929  $get_attributes = isset($args['get_attributes']) ? $args['get_attributes'] : FALSE;
930  $data = $this->getGeneralInfo($asset, $get_attributes);
931  } else {
932  $data['error'] = 'You do not have permissions to access this asset';
933  }//end else
934 
935  return $data;
936 
937  }// end _getGeneral
938 
939 
953  private function _getAttributes($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
954  {
955  if ($read_access) {
956  // Get available attributes
957  $attributes = $GLOBALS['SQ_SYSTEM']->am->getAssetTypeAttributes($asset->type());
958  foreach ($attributes as $key => $val) {
959  if ($val['type'] == 'password') continue;
960  if ($key == 'api_key') {
961  // Added security to make sure no one can get API Keys from other API Assets
962  $data[$key] = '';
963  } else {
964  $data[$key] = $asset->attr($key);
965  }
966  }//end foreach
967  } else {
968  $data['error'] = 'You do not have permissions to access this asset';
969  }//end else
970 
971  return $data;
972 
973  }// end _getAttributes
974 
975 
990  private function _setMultipleAttributes($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
991  {
992  return $this->_setAttribute($args, $asset, $read_access, $write_access);
993 
994  }// end _setMultipleAttributes
995 
996 
1010  private function _setAttribute($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
1011  {
1012  if ($write_access) {
1013  if (isset($args['attr_name']) && isset($args['attr_val'])) {
1014  $attr_names = explode('\\,', $args['attr_name']);
1015  $attr_vals = explode('\\,', $args['attr_val']);
1016 
1017  // Set our new values
1018  $success = $this->setAttributeValue($asset->id, $attr_names, $attr_vals);;
1019  if($success) {
1020  $data = $success;
1021  } else {
1022  $data = 'Attribute for Asset #'.$asset->id.' were not set. Check Error Log for more information';
1023  }
1024  } else {
1025  $data['error'] = 'Please enter both the attribute name and value';
1026  }//end else
1027  } else {
1028  $data['error'] = 'You do not have permissions to access this asset';
1029  }//end else
1030 
1031  return $data;
1032 
1033  }// end _setAttribute
1034 
1035 
1049  private function _getMetadata($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
1050  {
1051  if ($read_access) {
1052  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
1053  $schema = $mm->getSchemas($asset->id, TRUE);
1054  if (!empty($schema)) {
1055  $data = $mm->getMetadataFieldValues($asset->id);
1056  } else {
1057  $data['error'] = 'No Metadata Schema applied to Asset "'.$asset->name.'(#'.$asset->id.')"';
1058  }
1059  } else {
1060  $data['error'] = 'You do not have permissions to access this asset';
1061  }//end else
1062 
1063  return $data;
1064 
1065  }//end _getMetadata
1066 
1067 
1082  private function _setMetadataAllFields($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
1083  {
1084  return $this->_setMetadata($args, $asset, $read_access, $write_access);
1085 
1086  }// end _setMetadataAllFields
1087 
1088 
1102  private function _setMetadata($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
1103  {
1104  if ($write_access) {
1105  if (isset($args['field_id']) && isset($args['field_val'])) {
1106  require_once SQ_CORE_PACKAGE_PATH.'/metadata/metadata_field/metadata_field.inc';
1107 
1108  $field_ids = explode('\\,', $args['field_id']);
1109  $field_vals = explode('\\,', $args['field_val']);
1110 
1111  foreach($field_vals as $index => $fieldVal) {
1112  $field_vals[$index] = $fieldVal;
1113  $field_vals[$index] = Metadata_Field::encodeValueString($field_vals[$index], Array());
1114  }
1115 
1116  $result = $this->setAssetMetadata($asset->id, $field_ids, $field_vals);
1117  if($result) {
1118  $i = 0;
1119  foreach($field_vals as $index => $fieldVal) {
1120  $value = $fieldVal;
1121  $to_decode = Array('\=', '\;', '\\\\');
1122  $decoded_str = Array('=', ';', '\\');
1123  $value = str_replace($to_decode, $decoded_str, $value);
1124  $data['success'][] = 'Metadata field #'.$field_ids[$i].' has been successfully set to "'.$value.'" for Asset "'.$asset->name.'" (#'.$asset->id.')';
1125  $i++;
1126  }
1127  }
1128  } else {
1129  $data['error'] = 'Please enter both the field id and value';
1130  }//end else
1131  } else {
1132  $data['error'] = 'You do not have permissions to access this asset';
1133  }//end else
1134 
1135  return $data;
1136 
1137  }// end _setMetadata
1138 
1139 
1154  private function _getChildren($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
1155  {
1156 
1157  if ($read_access) {
1158  if (empty($args['levels']) && $args['levels'] != '0') {
1159  $data['error'] = 'Please set a depth(levels) limit';
1160  return $data;
1161  }
1162  // Check to see how deep we should go
1163  if ($args['levels'] == '0') {
1164  $level = NULL;
1165  } else {
1166  $level = $args['levels'];
1167  }
1168 
1169  // lets check for the type_codes, link_types, and link_values
1170  $type_codes = (isset($args['type_codes']) && $args['type_codes'] != '') ? explode('\\,', $args['type_codes']) : Array();
1171  $link_types = (isset($args['link_type']) && $args['link_type'] != '') ? explode('\\,', $args['link_type']) : Array();
1172  $link_values = (isset($args['link_value']) && $args['link_value'] != '') ? explode('\\,', $args['link_value']) : Array() ;
1173  $get_attributes = isset($args['get_attributes']) ? $args['get_attributes'] : FALSE;
1174 
1175  $data = $this->getChildren($asset->id, $level, $type_codes, $link_types, $link_values, $get_attributes);
1176 
1177  if (empty($data)) {
1178  $data['error'] = 'Asset #'.$asset->id.' has no children';
1179  }
1180  } else {
1181  $data['error'] = 'You do not have permissions to access this asset';
1182  }//end else
1183 
1184  return $data;
1185 
1186  }// end _getChildren
1187 
1188 
1204  private function _getParents($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
1205  {
1206  if ($read_access) {
1207  if ($asset->id == 1) {
1208  $data['error'] = 'The root asset does not have any parents';
1209  return $data;
1210  }
1211  if (empty($args['levels']) && $args['levels'] != '0') {
1212  $data['error'] = 'Please set a depth(levels) limit';
1213  return $data;
1214  }
1215  // Check to see how deep we should go
1216  if ($args['levels'] == '0') {
1217  $level = NULL;
1218  } else {
1219  $level = $args['levels'];
1220  }
1221 
1222  // lets check for the type_codes, link_types, and link_values
1223  $type_codes = (isset($args['type_codes']) && $args['type_codes'] != '') ? explode('\\,', $args['type_codes']) : Array();
1224  $link_types = (isset($args['link_type']) && $args['link_type'] != '') ? explode('\\,', $args['link_type']) : Array();
1225  $link_values = (isset($args['link_value']) && $args['link_value'] != '') ? explode('\\,', $args['link_value']) : Array();
1226  $get_attributes = isset($args['get_attributes']) ? $args['get_attributes'] : FALSE;
1227 
1228  $data = $this->getParents($asset->id, $level, $type_codes, $link_types, $link_values, $get_attributes);
1229  } else {
1230  $data['error'] = 'You do not have permissions to access this asset';
1231  }//end else
1232 
1233  return $data;
1234 
1235  }// end _getParents
1236 
1237 
1251  private function _getPermissions($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
1252  {
1253  if ($read_access) {
1254  $level = $args['level'];
1255  if (empty($level)) {
1256  $data['error'] = 'Please set a permissions level';
1257  return $data;
1258  }
1259  if (!is_numeric($level)) {
1260  $data['error'] = 'Permissions level must be numeric';
1261  return $data;
1262  }
1263  if ($level > 3) {
1264  $data['error'] = $level.' is not a valid permissions level';
1265  return $data;
1266  }
1267 
1268  $data = $this->getPermission($asset->id, $level);
1269  if (!$data) return FALSE;
1270 
1271  return $data;
1272  } else {
1273  $data['error'] = 'You do not have permissions to access this asset';
1274  }//end else
1275 
1276  return $data;
1277 
1278  }// end _getPermissions
1279 
1280 
1294  private function _createAsset($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
1295  {
1296  if ($write_access || $this->attr('ignore_permissions')) {
1297  // These must be set, so we don't need to check these
1298  if (isset($args['type_code']) && isset($args['asset_name'])) {
1299  $type_code = isset($args['type_code']) ? $args['type_code'] : '';
1300  $asset_name = isset($args['asset_name']) ? $args['asset_name'] : '';
1301  $link_type = isset($args['link_type']) ? $args['link_type'] : 1;
1302  $link_value = isset($args['link_value']) ? $args['link_value'] : '';
1303  $sort_order = isset($args['sort_order']) ? $args['sort_order'] : '';
1304  $is_dependant = isset($args['is_dependant']) ? $args['is_dependant'] : 0;
1305  $is_exclusive = isset($args['is_exclusive']) ? $args['is_exclusive'] : 0;
1306  $extra_attributes = (isset($args['extra_attributes']) && $args['extra_attributes'] == '1') ? TRUE : FALSE;
1307 
1308  // Create our asset
1309  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
1310  $data = $this->createAsset($asset->id, $type_code, $asset_name, $link_type, $link_value, $sort_order, $is_dependant, $is_exclusive, $extra_attributes, $args);
1311  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
1312  } else {
1313  $data['error'] = 'You must set the parent id, type code and asset name to create an asset';
1314  }
1315  } else {
1316  $data['error'] = 'You do not have permissions to create an asset';
1317  }//end else
1318 
1319  return $data;
1320 
1321  }// end _createAsset
1322 
1323 
1337  private function _getLocksInfo($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
1338  {
1339  if ($read_access) {
1340  $data = Array();
1341  $screen = $args['screen'];
1342  $response = $this->getLocksInfo($asset->id, $screen);
1343  if (empty($response)) {
1344  $data[] = 'No lock are held by anyone on Asset "'.$asset->name.'" (#'.$asset->id.')';
1345  } else {
1346  require_once SQ_FUDGE_PATH.'/general/datetime.inc';
1347  foreach ($response as $screen_lock => $lock_info) {
1348  if ($screen_lock == '0') $screen_lock = $screen;
1349  $user = $GLOBALS['SQ_SYSTEM']->am->getAsset($lock_info['userid']);
1350  $expires_in = easy_time_total(($lock_info['expires'] - time()), TRUE);
1351  if (!$expires_in) $expires_in = '1 second';
1352  $data[] = '"'.$screen_lock.'" locks are held by User "'.$user->name.'" (#'.$lock_info['userid'].') for Asset "'.$asset->name.'" (#'.$asset->id.'). This is due to expire in '.$expires_in;
1353  }
1354  }// end if
1355  } else {
1356  $data['error'] = 'You do not have permission to access this asset';
1357  }//end else
1358 
1359  return $data;
1360 
1361  }// end _getLocksInfo
1362 
1363 
1379  private function _acquireLock($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
1380  {
1381  if ($write_access) {
1382  $screen = $args['screen'];
1383  $dependants_only = isset($args['dependants_only']) ? $args['dependants_only'] : TRUE ;
1384  $force_acquire = isset($args['force_acquire']) ? $args['force_acquire'] : FALSE;
1385 
1386  $response = $this->acquireLocks($asset, $screen, $dependants_only, $force_acquire);
1387  if (empty($response)) {
1388  $data[] = '"'.$screen.'" locks are now acquired for Asset "'.$asset->name.'" (#'.$asset->id.')';
1389  } else {
1390  $data = $response;
1391  }// end if
1392  } else {
1393  $data['error'] = 'You do not have permission to access this asset';
1394  }//end else
1395 
1396  return $data;
1397 
1398  }// end _acquireLock
1399 
1400 
1414  private function _releaseLock($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
1415  {
1416  $screen = $args['screen'];
1417  $response = $this->getLocksInfo($asset->id, $screen);
1418 
1419  // we might not have write access but this might be caused because
1420  // the status of the asset has changed (say from UC to pending approval)
1421  // but still we should be able to release locks if we hold any
1422  if ($write_access || (!empty($response) && $asset->writeAccess('', Array(), FALSE))) {
1423  if (empty($response)) {
1424  $data[] = 'No lock are held by anyone on Asset "'.$asset->name.'" (#'.$asset->id.')';
1425  } else {
1426  $response = $GLOBALS['SQ_SYSTEM']->am->releaseLock($asset->id, $screen);
1427  if ($response) {
1428  $data[] ='"'.$screen.'" locks are now released for Asset "'.$asset->name.'" (#'.$asset->id.')';
1429  } else {
1430  $data['error'] = 'You cannot release locks on this asset';
1431  }// end if
1432  }//end else
1433  } else {
1434  $data['error'] = 'You do not have permission to access this asset';
1435  }//end else
1436 
1437  return $data;
1438 
1439  }// end _releaseLock
1440 
1441 
1455  private function _createLink($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
1456  {
1457  if ($write_access) {
1458  if (isset($args['parent_id'])) {
1459  // Check to see if we are using an id or a URL
1460  $parent = $this->setAsset($args['parent_id']);
1461  if (!$parent) return FALSE;
1462  } else {
1463  $data['error'] = 'A parent asset is required';
1464  return FALSE;
1465  }
1466 
1467  $link_type = isset($args['link_type']) ? $args['link_type'] : 1;
1468  $link_value = isset($args['link_value']) ? $args['link_value'] : '';
1469  $sort_order = isset($args['sort_order']) ? $args['sort_order'] : '';
1470  $is_dependant = isset($args['is_dependant']) ? $args['is_dependant'] : 0;
1471  $is_exclusive = isset($args['is_exclusive']) ? $args['is_exclusive'] : 0;
1472 
1473  $data = $this->createLink($parent->id, $asset->id, $link_type, $link_value, $sort_order, $is_dependant, $is_exclusive);
1474  } else {
1475  $data['error'] = 'You do not have permission to access this asset';
1476  }//end else
1477 
1478  return $data;
1479 
1480  }// end _createLink
1481 
1482 
1497  private function _removeLink($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
1498  {
1499  if ($write_access) {
1500  if (isset($args['parent_id'])) {
1501  // Check to see if we are using an id or a URL
1502  $parent = $this->setAsset($args['parent_id']);
1503  if (!$parent) return FALSE;
1504  } else {
1505  $data['error'] = 'A parent asset is required';
1506  return $data;
1507  }
1508  $link_type = defined($args['link_type']) ? constant($args['link_type']) : SQ_LINK_TYPE_1;
1509  $link_value = $args['link_value'];
1510 
1511  // Remove our link
1512  $data = $this->removeLink($parent->id, $asset->id, $link_type, $link_value);
1513  if (!$data) return FALSE;
1514  } else {
1515  $data['error'] = 'You do not have permission to access this asset';
1516  }//end else
1517 
1518  return $data;
1519 
1520  }// end _removeLink
1521 
1522 
1536  private function _moveLink($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
1537  {
1538  if ($write_access) {
1539  if (!empty($args['old_parent_id']) && !empty($args['new_parent_id'])) {
1540  // Check to see if we are using an id or a URL
1541  $old_parent = $this->setAsset($args['old_parent_id']);
1542  $new_parent = $this->setAsset($args['new_parent_id']);
1543  if (is_null($old_parent) || is_null($new_parent)) {
1544  $data['error'] = 'Either Old Parent or the New Parent was not a valid assetid.';
1545  return $data;
1546  }
1547  } else {
1548  $data['error'] = 'A parent asset is required';
1549  return $data;
1550  }//end else
1551 
1552  $old_link_type = defined($args['old_link_type']) ? constant($args['old_link_type']) : SQ_LINK_TYPE_1 ;
1553  $old_link_value = $args['old_link_value'];
1554  $new_link_type = defined($args['new_link_type']) ? constant($args['new_link_type']) : SQ_LINK_TYPE_1 ;
1555  $new_link_value = $args['new_link_value'];
1556  $new_position = isset($args['new_position']) ? $args['new_position'] : -1;
1557 
1558  // Remove our link
1559  $data = $this->moveLink($old_parent->id, $asset->id, $old_link_type, $old_link_value, $new_parent->id, $new_link_type, $new_link_value, $new_position);
1560 
1561  if (!$data) return FALSE;
1562  } else {
1563  $data['error'] = 'You do not have permission to access this asset';
1564  }//end else
1565 
1566  return $data;
1567 
1568  }// end _moveLink
1569 
1570 
1584  private function _updateLink($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
1585  {
1586  if ($write_access) {
1587  if (isset($args['parent_id'])) {
1588  // Check to see if we are using an id or a URL
1589  $parent = $this->setAsset($args['parent_id']);
1590  if (!$parent) return FALSE;
1591  } else {
1592  $data['error'] = 'A parent asset is required';
1593  return $data;
1594  }
1595 
1596  $old_link_type = defined($args['existing_link_type']) ? constant($args['existing_link_type']) : SQ_LINK_TYPE_1 ;
1597  $old_link_value = $args['existing_link_value'];
1598  $new_link_type = defined($args['link_type']) ? constant($args['link_type']) : SQ_LINK_TYPE_1 ;
1599  $new_link_value = $args['link_value'];
1600  $sort_order = (isset($args['sort_order']) && $args['sort_order'] != '' ) ?$args['sort_order'] : NULL;
1601  $locked = (isset($args['locked']) && $args['locked'] != '' ) ? $args['locked'] : TRUE;
1602 
1603  // Remove our link
1604  $data = $this->updateLink($parent->id, $asset->id, $old_link_type, $old_link_value, $new_link_type, $new_link_value, $sort_order, $locked);
1605 
1606  if (!$data) return FALSE;
1607  } else {
1608  $data['error'] = 'You do not have permission to access this asset';
1609  }//end else
1610 
1611  return $data;
1612 
1613  }// end _updateLink
1614 
1615 
1629  private function _getLinkId($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
1630  {
1631  if ($read_access) {
1632  if (isset($args['parent_id'])) {
1633  // Check to see if we are using an id or a URL
1634  $parent = $this->setAsset($args['parent_id']);
1635  if (!$parent) return FALSE;
1636  } else {
1637  $data['error'] = 'A parent asset is required';
1638  return $data;
1639  }
1640  $link_type = defined($args['link_type']) ? constant($args['link_type']) : SQ_LINK_TYPE_1 ;
1641  $link_value = $args['link_value'];
1642  $all_info = (boolean)$args['all_info'];
1643 
1644  // Get our link id
1645  $data = $this->getLinkId($parent->id, $asset->id, $link_type, $link_value, $all_info);
1646 
1647  if (!$data) {
1648  $data['error'] = 'Parent Asset #'.$parent->id.' and Child Asset #'.$asset->id.' do not have a valid link for given Link Type and Link Value';
1649  }
1650  } else {
1651  $data['error'] = 'You do not have permissions to access this asset';
1652  }//end if
1653 
1654  return $data;
1655 
1656  }// end _getLinkId
1657 
1658 
1672  private function _getAssetTree($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
1673  {
1674  if ($read_access) {
1675  // Check to see how deep we should go
1676  if (empty($args['levels'])) {
1677  $levels = NULL;
1678  } else {
1679  $levels = $args['levels'];
1680  }//end if
1681 
1682  $data = $GLOBALS['SQ_SYSTEM']->am->getAssetTree($asset->id, $levels);
1683 
1684  if (empty($data)) $data['error'] = 'Asset #'.$asset->id.' has no children';
1685  } else {
1686  $data['error'] = 'You do not have permissions to access this asset';
1687  }//end else
1688 
1689  return $data;
1690 
1691  }// end _getAssetTree
1692 
1693 
1707  private function _getKeywordsReplacements($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
1708  {
1709  if (!isset($arg['keywords']) && isset($args['keywords_array'])) {
1710  // if this IF statement is true that means the batchRequest() is used to call
1711  // getKeywordReplacement(). Make sure the keywords index expected is always populated.
1712  $args['keywords'] = $args['keywords_array'];
1713  }
1714 
1715  if ($read_access) {
1716  if (isset($args['keywords']) && !empty ($args['keywords']) ){
1717  $keywords = is_array($args['keywords']) ? $args['keywords'] : explode('\\,', $args['keywords']);
1718  $data = $this->getKeywordsReplacements($asset, $keywords);
1719  } else {
1720  $data['error'] = 'Please provide keywords to get replacement for';
1721  }
1722  } else {
1723  $data['error'] = 'You do not have permissions to access this asset';
1724  }//end else
1725 
1726  return $data;
1727 
1728  }// end _getKeywordsReplacements
1729 
1730 
1744  private function _setAssetStatus($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
1745  {
1746  if ($write_access) {
1747  $status = (int) $args['status'];
1748  $cascade = isset($args['cascade']) ? $args['cascade'] : FALSE;
1749  $workflow_stream = (isset($args['workflow_stream']) && ($args['workflow_stream'] != '') )? $args['workflow_stream'] : 'SQ_USE_DEFAULT';
1750 
1751  //let check if the status supplied is valid one
1752  $desc = get_status_description($status);
1753  $current_desc = ($status != $asset->status) ? get_status_description($asset->status) : $desc;
1754 
1755  $response = $this->setAssetStatus($asset, $status, $cascade, $current_desc, $desc, $workflow_stream);
1756  if (empty($response)) {
1757  if (!$cascade) {
1758  $data[] = 'Status for Asset "'.$asset->name.'" (#'.$asset->id.') has been changed successfully to '.$desc;
1759  } else {
1760  $data[] = 'Status for Asset "'.$asset->name.'" (#'.$asset->id.') and its non-dependant children has been changed successfully to '.$desc;
1761  }
1762  } else {
1763  $data = $response;
1764  }
1765  } else {
1766  $data['error'] = 'You do not have permissions to access this change status for this asset';
1767  }//end else
1768 
1769  return $data;
1770 
1771  }// end _setAssetStatus
1772 
1773 
1787  private function _getWebPath($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
1788  {
1789  if($read_access) {
1790  $webpaths = $asset->getWebPaths();
1791  if(!empty($webpaths)) {
1792  $i = 0;
1793  foreach($webpaths as $webpath) {
1794  $data['webpath'][$i] = $webpath;
1795  $i++;
1796  }
1797  } else {
1798  $data ['webpath'] = Array();
1799  }
1800  } else {
1801  $data['error'] = 'You do not have permission to access this asset';
1802  }
1803 
1804  return $data;
1805 
1806  }// end _getWebPath
1807 
1808 
1823  private function _setWebPath($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
1824  {
1825  if($write_access) {
1826  $webpaths = explode('\\,', $args['webpath']);
1827  if (!empty($webpaths)) {
1828  $webpaths = explode('\\,', $args['webpath']);
1829  $auto_remap = (isset($args['auto_remap']) && $args['auto_remap'] != '') ? $args['auto_remap'] : TRUE;
1830  $response = $asset->saveWebpaths($webpaths, $auto_remap);
1831 
1832  if($response) {
1833  $this->updateLookupsforAsset($asset->id);
1834  $data[] = 'Webpath for Asset "'.$asset->name.'" (# '.$asset->id.') had been updated sucessfully';
1835  } else {
1836  $data[] = 'Could not update webpath for Asset "'.$asset->name.'" (# '.$asset->id.') ';
1837  }
1838  } else {
1839  $data['error'] = 'No webpaths provided';
1840  }
1841  } else {
1842  $data['error'] = 'You do not have permission to access this asset';
1843  }
1844 
1845  return $data;
1846 
1847  }// end _setWebPath
1848 
1849 
1863  private function _getWorkflowSchema($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
1864  {
1865  if($read_access) {
1866  $wfm = $GLOBALS['SQ_SYSTEM']->getWorkflowManager();
1867  $granted = isset($args['granted']) && $args['granted'] != '' ? $args['granted'] : NULL;
1868  $running = isset($args['running']) && $args['running'] != '' ? $args['running'] : FALSE;
1869  $schemas = $wfm->getSchemas($asset->id, $granted, $running);
1870 
1871  if(empty($schemas)) {
1872  $message = 'No Workflow Schema ';
1873  if ($running) {
1874  $message .= 'currently running for ';
1875  } else {
1876  if (is_null($granted)) {
1877  $message .= 'currently applied to';
1878  } else {
1879  $message .= $granted ? 'granted on ' : 'denied on ';
1880  }
1881  }
1882  $data[] = $message.'Asset "'.$asset->name.'" (# '.$asset->id.')';
1883  } else {
1884  foreach ($schemas as $index => $info) {
1885  if ($running && $granted) {
1886  $data['running'][] = $info;
1887  } else if (is_null($granted) && $running) {
1888  $data['running'][] = $index;
1889  } else {
1890  if (is_null($granted)) {
1891  $status = $info ? 'granted' : 'denied';
1892  $data[$status][] = $index;
1893  } else {
1894  $status = $granted ? 'granted' : 'denied';
1895  $data[$status][] = $info;
1896  }
1897  }
1898  }
1899  }
1900  } else {
1901  $data['error'] = 'You do not have permission to access this asset';
1902  }
1903 
1904  return $data;
1905 
1906  }// end _getWorkflowSchema
1907 
1908 
1924  private function _createFileAsset($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
1925  {
1926  if ($write_access || $this->attr('ignore_permissions')) {
1927 
1928  if ((isset($args['friendly_name']) && $args['friendly_name'] != '' )) {
1929  $filename = $args['friendly_name'];
1930  $type_code = $args['type_code'];
1931  $link_type = defined($args['link_type']) ? constant($args['link_type']) : SQ_LINK_TYPE_1;
1932  $link_value = $args['link_value'];
1933  $valid_type_code= $GLOBALS['SQ_SYSTEM']->am->getTypeDescendants('file', TRUE);
1934 
1935  if(!in_array(Array($type_code), $valid_type_code)) {
1936 
1937  $GLOBALS['SQ_SYSTEM']->am->includeAsset($type_code);
1938  $new_asset = new $type_code();
1939 
1940  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
1941  $file_ext = get_file_type($filename);
1942 
1943  // bug fix #5749 JS API createFileAsset warns of file extension on generic file asset
1944  // if we are creaed generic File asset the allowed_extensions property is empty for
1945  // this get all the allowed file extensions by matrix and let the user go nuts !!!
1946  if (strtolower($type_code) == 'file' || strtolower($type_code) == 'text_file') {
1947  // get our standard list of MIME type allowed
1948  include SQ_FUDGE_PATH.'/standards_lists/mime_types.inc';
1949  $allowed_extensions = array_keys($standards_lists_mime_types);
1950  } else {
1951  $allowed_extensions = $new_asset->allowed_extensions;
1952  }
1953 
1954  if (!in_array($file_ext, $allowed_extensions)) {
1955  return Array('error' => 'The supplied file extension "'.$file_ext.'" is not allowed for file type "'.$type_code.'"');
1956  }
1957  //We are going to write to the Matrix Data tmp dir
1958  $tmp_file_name = $this->getRandomFilename($filename);
1959  $destination_file = SQ_DATA_PATH.'/temp/'.$tmp_file_name.'.'.$file_ext;
1960 
1961  while (file_exists($destination_file)) {
1962  $tmp_file_name = $this->getRandomFilename($filename);
1963  $destination_file = SQ_DATA_PATH.'/temp/'.$tmp_file_name.'.'.$file_ext;
1964  }//end while
1965 
1966  //create an file with empty content, coz we are getting rid of it anyways
1967  if ($type_code != 'image' && $type_code != 'thumbnail') {
1968  file_put_contents($destination_file, '');
1969  } else {
1970  // is a image do it differently
1971  $img = imagecreate('10','10');
1972  $background_color = imagecolorallocate($img, 0, 0, 0);
1973  imagepng($img, $destination_file);
1974  }
1975 
1976  // construct the link array
1977  $link = Array (
1978  'asset' => $asset,
1979  'link_type' => $link_type,
1980  'value' => $link_value,
1981  'sort_order'=> -1,
1982  );
1983 
1984  $file_info = Array (
1985  'name' => $filename,
1986  'tmp_name' => $destination_file,
1987  'type' => $file_ext,
1988  'error' => 0,
1989  'size' => filesize($destination_file),
1990  'filename' => $filename,
1991  'non_uploaded_file' => TRUE,
1992  );
1993 
1994  //fake it
1995  $new_asset->_tmp['uploading_file'] = TRUE;
1996  if ($type_code == 'data_source_graph') $new_asset->setAttrValue('title', $filename);
1997 
1998  $success = $new_asset->create($link, $file_info);
1999 
2000  if (!$success) {
2001  $data['error'] = 'Error occured while creating new asset of type \''.$type_code.'\'';
2002  } else {
2003  // done creating the asset ? let remove the temp file we had uploaded and make way for new one
2004  $fv = $GLOBALS['SQ_SYSTEM']->getFileVersioning();
2005  $existing = $new_asset->getExistingFile();
2006 
2007  $new_asset_edit_fns = $new_asset->getEditFns();
2008  $new_asset_edit_fns->removeOldFile($new_asset, $existing, $fv);
2009  $data[$new_asset->id] = 'New File Asset (#'.$new_asset->id.') \''.$new_asset->name.'\'of type_code \''.$type_code.'\' created successfully';
2010  }
2011  } else {
2012  $data['error'] = 'Passed type code is not File Type asset or its Descendant Type';
2013  }
2014  } else {
2015  $data['error'] = 'You must set the Name of the Asset and provide a Base 64 encoded file to upload';
2016  }
2017  } else {
2018  $data['error'] = 'You do not have permissions to create an asset';
2019  }
2020 
2021  return $data;
2022 
2023  }// end _createFileAsset
2024 
2025 
2039  private function _setContentOfEditableFileAsset($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
2040  {
2041  if ($write_access) {
2042  $valid_file_type = Array('xml_file', 'xsl_file', 'css_file', 'text_file', 'js_file');
2043  if(in_array($asset->type(), $valid_file_type)) {
2044  if($asset->writeAccess('attributes')) {
2045  $content = urldecode($args['content']);
2046  if($content == 'no_value_provided') {
2047  $data[] = 'No file content provided';
2048  } else {
2049  require_once(SQ_INCLUDE_PATH.'/backend_outputter.inc');
2050  $o = new Backend_Outputter();
2051  $prefix = $asset->getPrefix();
2052  $edit_fns = $asset->getEditFns();
2053  $_POST[$prefix.'_new_file'] = urldecode($content);
2054 
2055  $success = $edit_fns->processEditFile($asset, $o, $prefix);
2056  if ($success) {
2057  $data['success'] = 'Content of Asset '.$asset->short_name.'(#'.$asset->id.') successfully updated';
2058  } else {
2059  $data['error'] = 'Error occured while updating Content of Asset '.$asset->short_name.'(#'.$asset->id.')';
2060  }
2061  }
2062  } else {
2063  $data['error'] = 'Locks are not acquired on Asset '.$asset->short_name.'(#'.$asset->id.'), content cannot be updated';
2064  }
2065  } else {
2066  $data['error'] = 'Content for Asset of type code \''.$asset->type().'\' cannot be updated';
2067  }
2068  } else {
2069  $data['error'] = 'You do not have permissions to Edit this asset';
2070  }
2071 
2072  return $data;
2073 
2074  }// end _setContentOfEditableFileAsset
2075 
2076 
2090  private function _getLineage($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
2091  {
2092  // we have the url in the args, lets get it and get the asset are dealing with
2093  $url = trim(array_get_index($args, 'asset_url', ''));
2094  if (empty($url)) {
2095  return Array(
2096  'error' => 'No URL supplied'
2097  );
2098  }
2099  $significant_link_only = array_get_index($args, 'significant_link_only', TRUE);
2100 
2101  // cant get lineage of root_folder, DUHH !
2102  if ($url == '1') return Array();
2103 
2104  // now if we have url we have a ready made function in asset_manager.inc we can use
2105  // although we manipulate the results as it not the elements/format we want
2106  $url_bits = parse_url($url);
2107  if (!empty($url_bits['scheme']) && !empty($url_bits['host'])) {
2108  $asset = $GLOBALS['SQ_SYSTEM']->am->getAssetFromURL($url_bits['scheme'], $url_bits['host'].$url_bits['path']);
2109  if (is_null($asset)) {
2110  return Array(
2111  'error' => 'URL supplied does not point to asset in Matrix'
2112  );
2113  }
2114  } else if (preg_match('|^[0-9]+(?:\:.+)?|', $url)) {
2115  if (!$GLOBALS['SQ_SYSTEM']->am->getAssetInfo($url)) {
2116  return Array(
2117  'error' => 'Supplied assetid #'.$url.' does not exist'
2118  );
2119  }
2120  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($url);
2121  } else {
2122  return Array(
2123  'error' => 'URL supplied is invalid'
2124  );
2125  }
2126 
2127  if(!$asset->readAccess()) return Array('error' => 'You do not have permissions to access this asset');
2128 
2129  $raw_lineages = $GLOBALS['SQ_SYSTEM']->am->getLinkLineages($asset->id, 0, NULL, Array('name', 'type_code'), $significant_link_only);
2130  $root_folder = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_folder');
2131 
2132  $result_lineages = Array();
2133  $linkid_done = Array();
2134  $link_types = $significant_link_only ? SQ_SC_LINK_SIGNIFICANT : SQ_SC_LINK_ALL;
2135 
2136  foreach ($raw_lineages as $index => $val) {
2137  $data = Array();
2138  $minorid = $asset->id;
2139  $parent_lineage_assetids = array_merge(array_reverse(array_keys($val['lineage'])), Array($root_folder->id));
2140  foreach($parent_lineage_assetids as $majorid) {
2141  $major_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($majorid);
2142  $minor_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($minorid);
2143 
2144  $links = $GLOBALS['SQ_SYSTEM']->am->getLinkByAsset($major_asset->id, $minor_asset->id, $link_types, NULL, 'major', TRUE);
2145  foreach($links as $link) {
2146  if (!in_array($link['linkid'], $linkid_done)) {
2147  if ($link['link_type'] & SQ_SC_LINK_SIGNIFICANT) {
2148  $treeid_data = $GLOBALS['SQ_SYSTEM']->am->getLinkTreeid($link['linkid']);
2149  } else {
2150  $treeid_data = Array($link['linkid'] => Array(Array(FALSE)));
2151  }
2152  foreach($treeid_data[$link['linkid']] as $treeid) {
2153  $info = Array();
2154  $info['tree_id'] = $treeid[0];
2155  $info['assetid'] = $minor_asset->id;
2156  $info['name'] = $minor_asset->name;
2157  $info['type_code'] = $minor_asset->type();
2158  $info['link_type'] = $link['link_type'];
2159  $info['link_value'] = $link['value'];
2160  $info['linkid'] = $link['linkid'];
2161  $info['parent_assetid'] = $major_asset->id;
2162  $info['parent_type_code'] = $major_asset->type();
2163 
2164  $data[] = $info;
2165  }
2166  $linkid_done[] = $link['linkid'];
2167  }
2168  }//end foreach
2169 
2170  // Get the link info for the next lineage, one level up, if any
2171  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($minorid);
2172  $minorid = $majorid;
2173  }//end foreach
2174 
2175  $result_lineages = array_merge($result_lineages, array_reverse($data));
2176  }//end foreach
2177 
2178  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($root_folder);
2179 
2180  return $result_lineages;
2181 
2182  }// end _getLineage()
2183 
2184 
2198  private function _getLineageFromUrl($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
2199  {
2200  // we have the url in the args, lets get it and get the asset are dealing with
2201  $url = trim(array_get_index($args, 'asset_url', ''));
2202  if (empty($url)) return Array('error' => 'No URL supplied');
2203 
2204  $url_bits = parse_url($url);
2205 
2206  $asset = $GLOBALS['SQ_SYSTEM']->am->getAssetFromURL($url_bits['scheme'], $url_bits['host'].$url_bits['path']);
2207 
2208  if (is_null($asset)) return Array('error' => 'URL supplied does not point to asset in Matrix');
2209 
2210  if(!$asset->readAccess()) return Array('error' => 'You do not have permissions to access this asset');
2211 
2212 
2213  $result_lineage = $GLOBALS['SQ_SYSTEM']->am->getLineageFromURL($url_bits['scheme'], $url_bits['host'].$url_bits['path']);
2214  if(empty($result_lineage)) return Array('error' => 'No lineage found');
2215 
2216  return $result_lineage;
2217 
2218  }// end _getLineageFromUrl()
2219 
2220 
2234  private function _getUrlFromLineage($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
2235  {
2236 
2237  $lineage = trim(array_get_index($args, 'lineage', ''));
2238  $root_url = trim(array_get_index($args, 'root_url', NULL));
2239  $protocol = trim(array_get_index($args, 'protocol', NULL));
2240 
2241  if(empty($root_url)) $root_url = NULL;
2242  if (empty($lineage)) return Array('error' => 'No lineage supplied');
2243 
2244  $lineage = explode('\\,', $lineage);
2245 
2246  $current_asset_id = trim(array_pop($lineage));
2247  $current_asset = $this->setAsset($current_asset_id);
2248  If(!$current_asset) return FALSE;
2249 
2250  if(!$current_asset->readAccess()) return Array('error' => 'You do not have permissions to access this asset');
2251 
2252  $candidate_urls = $GLOBALS['SQ_SYSTEM']->am->getURLs($current_asset_id);
2253  if(empty($candidate_urls)) return Array('error' => 'No URL found for current asset');
2254 
2255  $lineage[] = $current_asset_id;
2256 
2257  $result = $GLOBALS['SQ_SYSTEM']->am->getUrlFromLineage($lineage, $root_url, $protocol);
2258 
2259  if (empty($result)) return Array('error' => 'No URL found for supplied lineage');
2260 
2261  return $result;
2262 
2263  }// end _getUrlFromLineage()
2264 
2265 
2279  private function _cloneAsset($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
2280  {
2281  if ($write_access || $this->attr('ignore_permissions')) {
2282  $data = Array();
2283  $new_parent = array_get_index($args, 'new_parent', '');
2284 
2285  // check if the parent asset is provided
2286  if (empty($new_parent)) {
2287  $data['error'] = 'No location to clone the asset was provided';
2288  return $data;
2289  }
2290 
2291  // check to see if we have write permission to the new parent
2292  if (!($new_parent_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($new_parent, '', TRUE))) {
2293  $data['error'] = 'Assetid "'.$new_parent.'" provided is invalid or doesn\'t exist on system';
2294  return $data;
2295  }
2296 
2297  if (!$new_parent_asset->writeAccess() && !$this->attr('ignore_permissions')) {
2298  $data['error'] = 'You do not have write access to the Asset under which you are tying to clone';
2299  return $data;
2300  }
2301 
2302  $clone_num = array_get_index($args, 'clone_num', 1);
2303  $position = array_get_index($args, 'new_position', -1);
2304  $value = array_get_index($args, 'link_value', '');
2305  $link_type = defined($args['link_type']) ? constant($args['link_type']) : SQ_LINK_TYPE_1;
2306 
2307  if (!is_null($asset)) {
2308  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
2309  $running_vars = Array (
2310  'assets' => Array (
2311  $asset->id => Array (
2312  'number_of_clones' => 1,
2313  ),
2314  ),
2315  'to_parent_assetid' => $new_parent,
2316  'to_parent_pos' => $position,
2317  'link_type' => $link_type,
2318  'value' => $value,
2319  );
2320 
2321  $all_errors = Array();
2322  $cloned_assets = Array();
2323  for ($i = 0; $i < $clone_num; $i++) {
2324  // Need to reset the running vars again as it is passed by reference to the hipo job
2325  $running_vars_copy = $running_vars;
2326  $errors = $hh->freestyleHipo('hipo_job_clone_assets', $running_vars_copy);
2327  if(isset($running_vars_copy['cloned_asset'])) {
2328  $cloned_asset_id = array_pop(array_keys($running_vars_copy['cloned_asset']));
2329  $cloned_asset_id = '#'.$cloned_asset_id;
2330  $cloned_assets[] = $cloned_asset_id;
2331  }
2332  $all_errors += $errors;
2333  }//end for
2334  if (empty($all_errors)) {
2335  $cloned_assets_string = implode($cloned_assets, ', ');
2336  $data['success'] = $asset->name.'(#'.$asset->id.') has been successfully cloned to '.$cloned_assets_string;
2337  } else {
2338  $data['error'] = $all_errors;
2339  }
2340  } else {
2341  $data['error'] = 'Asset to clone was not provided';
2342  }
2343  } else {
2344  $data['error'] = 'You do not have permissions to create an asset';
2345  }//end else
2346 
2347  return $data;
2348 
2349  }// end _cloneAsset
2350 
2351 
2366  private function _showDifference($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
2367  {
2368  $asset_2_id = array_get_index($args, 'assetid_2', '');
2369  if (empty($asset_2_id)) {
2370  $data['error'] = 'Only one asset was provided. Need 2 assets to check the difference';
2371  return $data;
2372  }
2373 
2374  if (!($asset_2 = $GLOBALS['SQ_SYSTEM']->am->getAsset($asset_2_id, '', TRUE))) {
2375  $data['error'] = 'Assetid "'.$asset_2_id.'", provided is invalid or doesn\'t exist on system';
2376  return $data;
2377  }
2378 
2379  if (($read_access) && $asset_2->readAccess()) {
2380  $data = Array();
2381  $paint_layout_1 = array_get_index($args, 'paint_layout_1', '');
2382  $paint_layout_2 = array_get_index($args, 'paint_layout_2', '');
2383 
2384  if (empty($paint_layout_1) || empty($paint_layout_2)) {
2385  // we dont care about the paint layout
2386  // get the content without them
2387  ob_start();
2388  $asset->printBody();
2389  $content1 = ob_get_contents();
2390  ob_end_clean();
2391 
2392  ob_start();
2393  $asset_2->printBody();
2394  $content2 = ob_get_contents();
2395  ob_end_clean();
2396 
2397  } else {
2398  // we are considering the paint layout lets see if we are passed the layouts
2399  // to use or just using the defined layouts
2400  // now the values for $paint_layout_1 and $paint_layout_2 are either assetids
2401  // or empty string, both are valid argument we can pass into printBodyWithPaintLayout
2402  foreach (Array($paint_layout_1, $paint_layout_2) as $paint_layout_assetid) {
2403  if (!$paint_layout = $GLOBALS['SQ_SYSTEM']->am->getAsset($paint_layout_assetid, 'paint_layout_page', 'TRUE')) {
2404  $data['error'] = 'Invalid assetid (#'.$paint_layout_assetid.') provided for Paint Layout to use';
2405  }
2406  $paint_layout = NULL;
2407  if (isset($data['error'])) return $data;
2408  }
2409 
2410  ob_start();
2411  $asset->printBodyWithPaintLayout($paint_layout_1);
2412  $content1 = ob_get_contents();
2413  ob_end_clean();
2414 
2415  ob_start();
2416  $asset_2->printBodyWithPaintLayout($paint_layout_2);
2417  $content2 = ob_get_contents();
2418  ob_end_clean();
2419  }
2420 
2421  // Show the differences
2422  require_once SQ_CORE_PACKAGE_PATH.'/designs/design_areas/design_area_body/matrix_diff_highlighter.inc';
2423  $highlighter = new Matrix_Diff_Highlighter();
2424  $contents = Array(&$content1, &$content2);
2425  foreach ($contents as $index => $content) {
2426  $pattern = '/(<div)([^>]*)( id="content_div_[0-9]*")([^>]*)(>)/';
2427  $replacement = '$1$2$4$5';
2428  // we need to remove the div id's as they are the assetid
2429  // of the bodycopy containers ..this will always be different while comparing 2 assets
2430  // and this will cause the output to be malformed even if the content was same
2431  $contents[$index] = preg_replace($pattern, $replacement, $contents[$index]);
2432  // replace &nbsp; with proper space. this is to prevent html_entity_decode
2433  // in matrix_diff_highlighter.inc to convert it to a "\xa0"
2434  // which is break the putput when were are json_encode'ing the results
2435  // to be sent back to API
2436  $contents[$index] = str_replace('&nbsp;', ' ', $contents[$index]);
2437  }
2438  $difference = $highlighter->process($content1, $content2, FALSE);
2439  $data['success'] = $difference;
2440  } else {
2441  $data['error'] = 'You do not have permissions to read content of one or either of the assets passed in';
2442  }//end else
2443 
2444  return $data;
2445 
2446  }// end _showDifference()
2447 
2448 
2463  private function _getAlternateContext($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
2464  {
2465  $all_info = array_get_index($args, 'all_info', FALSE);
2466 
2467  $system_contexts = $GLOBALS['SQ_SYSTEM']->getAllContexts();
2468  $current_contextid = $GLOBALS['SQ_SYSTEM']->getAlternateContext();
2469  $context_info = Array();
2470 
2471  $currnet_context = $system_contexts[$current_contextid];
2472  if ($all_info) {
2473  $context_info[$current_contextid] = $currnet_context;
2474  } else {
2475  $context_info[$current_contextid] = $currnet_context['name'];
2476  }
2477 
2478  $data['success'] = $context_info;
2479 
2480  return $data;
2481 
2482  }// end _getAlternateContext()
2483 
2484 
2498  private function _getCurrentContext($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
2499  {
2500  $all_info = array_get_index($args, 'all_info', FALSE);
2501 
2502  // check to se if we can read the contextid from the session first
2503  if ($this->attr('sync_context') && isset($_SESSION[SQ_SESSION_SANDBOX_INDEX]['SQ_BACKEND_ALTERNATE_CONTEXT_ID'])) {
2504  $current_contextid = $_SESSION[SQ_SESSION_SANDBOX_INDEX]['SQ_BACKEND_ALTERNATE_CONTEXT_ID'];
2505  if (!isset($_SESSION[SQ_SESSION_SANDBOX_INDEX]['SQ_ALTERNATE_CONTEXT_ID']) ||
2506  $_SESSION[SQ_SESSION_SANDBOX_INDEX]['SQ_ALTERNATE_CONTEXT_ID'] != $current_contextid) {
2507  $_SESSION[SQ_SESSION_SANDBOX_INDEX]['SQ_ALTERNATE_CONTEXT_ID'] = $current_contextid;
2508  }
2509  } else if (isset($_SESSION[SQ_SESSION_SANDBOX_INDEX]['SQ_ALTERNATE_CONTEXT_ID'])) {
2510  $current_contextid = $_SESSION[SQ_SESSION_SANDBOX_INDEX]['SQ_ALTERNATE_CONTEXT_ID'];
2511  } else {
2512  $current_contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
2513  }
2514 
2515  $system_contexts = $GLOBALS['SQ_SYSTEM']->getAllContexts();
2516  $context_info = Array();
2517 
2518  $currnet_context = $system_contexts[$current_contextid];
2519  if ($all_info) {
2520  $context_info[$current_contextid] = $currnet_context;
2521  } else {
2522  $context_info[$current_contextid] = $currnet_context['name'];
2523  }
2524 
2525  $data['success'] = $context_info;
2526 
2527  return $data;
2528 
2529  }// end _getCurrentContext()
2530 
2531 
2545  private function _getAllContexts($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
2546  {
2547  $all_info = array_get_index($args, 'all_info', FALSE);
2548 
2549  $system_contexts = $GLOBALS['SQ_SYSTEM']->getAllContexts();
2550  $context_info = Array();
2551 
2552  foreach ($system_contexts as $context_id => $context_data) {
2553  if ($all_info) {
2554  $context_info[$context_id] = $context_data;
2555  } else {
2556  $context_info[$context_id] = $context_data['name'];
2557  }
2558  }//end foreach
2559 
2560  $data['success'] = $context_info;
2561 
2562  return $data;
2563 
2564  }// end _getAllContexts()
2565 
2566 
2580  private function _setContext($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
2581  {
2582  $context_id = array_get_index($args, 'context_id', NULL);
2583 
2584  if (!is_null($context_id)) {
2585  $all_contexts = $GLOBALS['SQ_SYSTEM']->getAllContexts();
2586  if (isset($all_contexts[$context_id])) {
2587  // valid context id is provided
2588  $GLOBALS['SQ_SYSTEM']->changeContext($context_id);
2589  $_SESSION[SQ_SESSION_SANDBOX_INDEX]['SQ_ALTERNATE_CONTEXT_ID'] = $context_id;
2590  if ($this->attr('sync_context')) {
2591  $_SESSION[SQ_SESSION_SANDBOX_INDEX]['SQ_BACKEND_ALTERNATE_CONTEXT_ID'] = $context_id;
2592  }
2593  $data['success'] = "Context successfully set to '$context_id'.";
2594  } else {
2595  $data['error'] = 'Context Id Provided doesn\'t exist on the system.';
2596  }
2597  } else {
2598  $data['error'] = 'No Context Id Provided.';
2599  }
2600 
2601  return $data;
2602 
2603  }// end _setContext()
2604 
2605 
2619  private function _restoreContext($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
2620  {
2621  // valid context id is provided
2622  $restored_context = $GLOBALS['SQ_SYSTEM']->restoreContext(TRUE);
2623  $_SESSION[SQ_SESSION_SANDBOX_INDEX]['SQ_ALTERNATE_CONTEXT_ID'] = $restored_context;
2624  if ($this->attr('sync_context')) {
2625  $_SESSION[SQ_SESSION_SANDBOX_INDEX]['SQ_BACKEND_ALTERNATE_CONTEXT_ID'] = $restored_context;
2626  }
2627 
2628  $data['success'] = "Context successfully restored back to '$restored_context'.";
2629 
2630  return $data;
2631 
2632  }// end _restoreContext()
2633 
2634 
2648  private function _getMetadataSchema($args=Array(), $asset=NULL, $read_access=FALSE, $write_access=FALSE)
2649  {
2650  if ($write_access) {
2651  if (!is_null($asset)) {
2652  $granted = array_get_index($args, 'granted', TRUE);
2653  $cascades = array_get_index($args, 'cascades', NULL);
2654 
2655  $data = $this->getMetadataSchema($asset, $granted, $cascades);
2656  } else {
2657  $data['error'] = 'Asset to get the Metadata Schema applied to it was not provided';
2658  }
2659  } else {
2660  $data['error'] = 'You do not have permissions to access this asset';
2661  }//end else
2662 
2663  return $data;
2664 
2665  }// end _getMetadataSchema()
2666 
2667 
2677  public function getGeneralInfo(Asset $asset, $add_more_attributes=FALSE)
2678  {
2679  if (is_null($asset)) return Array();
2680 
2681  //root folder?
2682  if ($asset->id == '1') {
2683  $data['name'] = 'Root Folder';
2684  return $data;
2685  }
2686 
2687  // Set some shortcuts
2688  $am = $GLOBALS['SQ_SYSTEM']->am;
2689 
2690  $user_to_check = Array(
2691  'created_user' => Array(
2692  'id' => 'created_userid',
2693  'default' => 'Undefined',
2694  ),
2695  'updated_user' => Array(
2696  'id' => 'updated_userid',
2697  'default' => 'Never Updated',
2698  ),
2699  'published_user' => Array(
2700  'id' => 'published_userid',
2701  'default' => 'Never Published',
2702  ),
2703  'status_user' => Array(
2704  'id' => 'status_changed_userid',
2705  'default' => 'Never Changed',
2706  ),
2707  );
2708 
2709  $created_user = $updated_user = $published_user = $status_user = NULL;
2710  foreach ($user_to_check as $user => $user_info) {
2711  $user_id = $user_info['id'];
2712  if (isset($asset->$user_id) && $asset->$user_id != '' && (int)$asset->$user_id != 0 ) {
2713  if(!(${$user} = $am->getAsset($asset->$user_id, '', TRUE))) {
2714  ${$user} = 'Unknown User';
2715  }
2716  } else if (isset($asset->$user_id) && (int)$asset->$user_id == 0) {
2717  ${$user} = 'Root User';
2718  } else {
2719  ${$user} = $user_info['default'];
2720  }
2721  }
2722 
2723  $user = $am->getAsset($GLOBALS['SQ_SYSTEM']->currentUserId());
2724  $array_to_check_in = array_merge(Array($user->id), $user->getUserGroups());
2725  $effective_permission = '';
2726 
2727  if($user instanceof Root_User || $user instanceof System_User) {
2728  $effective_permission = 'Admin Access';
2729  }
2730 
2731  //if we havent got max permissions yet try the user id and all the user groups it belongs in
2732  if($effective_permission === '' ) {
2733  foreach($array_to_check_in as $user_group_id) {
2734  if ($asset->adminAccess('', Array($user_group_id))) {
2735  $effective_permission = 'Admin Access';
2736  // if admin permission is granted
2737  // no need to check on other groups
2738  break;
2739 
2740  } else if ($effective_permission != 'Write Access' && $asset->writeAccess('', Array($user_group_id), FALSE)) {
2741  $effective_permission = 'Write Access';
2742  // do not break out here as other usergroup
2743  // might have admin perm
2744 
2745  // go in the following block only if the effective permission isn't determined
2746  // if the write permission is already found no need to change the perm to read access
2747  } else if ($effective_permission == '' && $asset->readAccess(Array($user_group_id))) {
2748  $effective_permission = 'Read Access';
2749  // do not break out here as other usergroup
2750  // might have admin/write perm
2751  }
2752  }
2753  }
2754 
2755  // Get general info about asset
2756  $data = Array(
2757  'name' => $asset->name,
2758  'short_name' => $asset->short_name,
2759  'id' => $asset->id,
2760  'type_code' => $asset->type(),
2761  'type' => str_replace('_', ' ', get_class($asset)),
2762  'icon_path' => $am->getAssetIconURL($asset->type()),
2763  'data_path' => $asset->data_path,
2764  'web_path' => $asset->getURL(),
2765  'status' => $asset->getStatusDescription(),
2766  'created' => $asset->created,
2767  'created_userid' => $asset->created_userid,
2768  'created_username' => ($created_user === 'Root User' || $created_user === 'Undefined' || $created_user === 'Unknown User') ? $created_user : $created_user->name,
2769  'updated' => ($updated_user === 'Never Updated' || $updated_user === 'Unknown User') ? $updated_user : $asset->updated,
2770  'updated_userid' => ($updated_user === 'Never Updated' || $updated_user === 'Unknown User') ? $updated_user : $updated_user->id,
2771  'updated_username' => ($updated_user === 'Never Updated' || $updated_user === 'Unknown User') ? $updated_user : $updated_user->name,
2772  'published' => ($published_user === 'Never Published' || $published_user === 'Unknown User') ? $published_user : $asset->published,
2773  'published_userid' => ($published_user === 'Never Published' || $published_user === 'Unknown User') ? $published_user : $published_user->id,
2774  'published_username' => ($published_user === 'Never Published' || $published_user === 'Unknown User') ? $published_user : $published_user->name,
2775  'status_changed' => ($status_user === 'Never Changed' || $status_user === 'Unknown User') ? $status_user : $asset->status_changed,
2776  'status_changed_userid' => ($status_user === 'Never Changed' || $status_user === 'Unknown User') ? $status_user : $status_user->id,
2777  'status_changed_username' => ($status_user === 'Never Changed' || $status_user === 'Unknown User') ? $status_user : $status_user->name,
2778  'maximum_perm_on_asset' => $effective_permission,
2779  );
2780 
2781  foreach($am->getAssetTypeAttributes($asset->type()) as $name => $type) {
2782  if(($add_more_attributes || isset($data[$name])) && $name != 'api_key' && $type['type'] != 'password') {
2783  $value = $asset->attr($name);
2784  $data[$name] = $value;
2785  }
2786  }
2787 
2788  return $data;
2789 
2790  }//end getGeneralInfo()
2791 
2792 
2807  function createLink($parent, $child, $link_type, $link_value, $sort_order, $is_dependant, $is_exclusive)
2808  {
2809  if (!empty($parent) && !empty($child)) {
2810  // Set some shortcuts
2811  $am = $GLOBALS['SQ_SYSTEM']->am;
2812 
2813  $parent = $am->getAsset($parent);
2814  $child = $am->getAsset($child);
2815  $new_id = $am->createAssetLink($parent, $child, $link_type, $link_value, $sort_order, $is_dependant, $is_exclusive);
2816  //done creating the link, lets update lookup
2817  $result = $this->updateLookupsforAsset($child->id);
2818  if (!is_null($result)) return Array('error' => 'Unable to Update Lookups on the Asset #'.$child->id.' and its Childrens');
2819 
2820  if ($new_id == 0) {
2821  return Array('error' => 'Unable to create link');
2822  }//end if
2823  return Array (
2824  'link_id' => $new_id,
2825  );
2826  } else {
2827  $this->returnError('Missing information to create link. Make sure parent id and child are provided');
2828  }//end else
2829 
2830  }//end CreateAssetLink()
2831 
2832 
2844  function removeLink($parent, $child, $link_type=NULL, $link_value=NULL)
2845  {
2846  $am = $GLOBALS['SQ_SYSTEM']->am;
2847 
2848  // Set our link id
2849  $linkid = $this->getLinkId($parent, $child, $link_type, $link_value, TRUE);
2850 
2851  if (!$linkid) {
2852  $data['error'] = 'Parent Asset #'.$parent.' and Child Asset #'.$child.' do not have a valid link for given Link Type and Link Value';
2853  return $data;
2854  }
2855 
2856  $parent_asset = $am->getAsset($parent);
2857  $child_asset = $am->getAsset($child);
2858 
2859  if ($linkid['locked']) {
2860  $data['error'] = 'Link #"'.$linkid['link_id'].'" between Asset "'.$child_asset->name.'" (#'.$child_asset->id.') and Asset "'.$parent_asset->name.'" (#'.$parent_asset->id.') " cannot be deleted, link is locked';
2861  return $data;
2862  }
2863 
2864  // Set some shortcuts
2865  $am = $GLOBALS['SQ_SYSTEM']->am;
2866 
2867  if ($linkid['link_id'] != '') {
2868  $link = $am->getLinkById($linkid['link_id']);
2869  // Make sure our array contains some items
2870  if (!empty($link)) {
2871  // Get all our locks
2872  $am->acquireLock($link['majorid'], 'all');
2873  $am->acquireLock($link['minorid'], 'all');
2874  $parents = $am->getLinks($link['minorid'], SQ_SC_LINK_SIGNIFICANT, '', TRUE, 'minor');
2875 
2876  // See if we should delete a link or move the asset to the trash
2877  // but do this only if we are deleting a SIGNIFICANT LINK and not NOTICE link
2878  if (count($parents) > 1 || !($link_type & SQ_SC_LINK_SIGNIFICANT)) {
2879  // More than one parent link, we can delete the link
2880  $result = $am->deleteAssetLink($linkid['link_id']);
2881  } else {
2882  // Only one link holly cow, lets trash it instead, otherwise we'l have an orphan asset in the system
2883  $result = $am->trashAsset($link['minorid'], TRUE);
2884  }//end if
2885  //no matter if link is deleted or the whole asset is moved to trash, we will update lookups :)
2886  $result_from_update = $this->updateLookupsforAsset($child);
2887  if (!is_null($result_from_update)) $this->returnError('Unable to Update Lookups on the Asset "'.$child_asset->name.'" (#'.$child.') and its Childrens');
2888 
2889  // Release all our locks
2890  $am->releaseLock($link['minorid'], 'all');
2891  $am->releaseLock($link['majorid'], 'all');
2892 
2893  if ($result) {
2894  $data['success'] = 'Link #"'.$linkid['link_id'].'" between Asset "'.$child_asset->name.'" (#'.$child_asset->id.') and Asset "'.$parent_asset->name.'" (#'.$parent_asset->id.') " has been sucessfully removed';
2895  return $data;
2896  } else {
2897  $data['error'] = 'Unable to delete link';
2898  return $data;
2899  }//end else
2900 
2901  } else {
2902  $data['error'] = 'The link id that was provided is invalid';
2903  return $data;
2904  }//end else
2905 
2906  } else {
2907  $data['error'] = 'The link id provided is empty';
2908  return $data;
2909  }//end else
2910 
2911  return FALSE;
2912 
2913  }//end removeLink
2914 
2915 
2931  function moveLink($old_parent_id, $asset_id_to_move, $old_link_type=SQ_LINK_TYPE_1, $old_link_value='', $new_parent_id, $new_link_type=SQ_LINK_TYPE_1, $new_link_value='', $new_position)
2932  {
2933  // lets make sure the link type passed in for both, old link and new link are significant
2934  if (!($new_link_type & SQ_SC_LINK_SIGNIFICANT)) $new_link_type = SQ_LINK_TYPE_1;
2935  if (!($old_link_type & SQ_SC_LINK_SIGNIFICANT)) $old_link_type = SQ_LINK_TYPE_1;
2936 
2937  if (!empty($old_parent_id) && !empty($asset_id_to_move) && !empty($new_parent_id)) {
2938  // Get our link id!!
2939  $link_id = $this->getLinkId($old_parent_id, $asset_id_to_move, $old_link_type, $old_link_value, TRUE);
2940 
2941  if (!$link_id) {
2942  $data['error'] = 'Parent Asset #'.$old_parent_id.' and Child Asset #'.$asset_id_to_move.' do not have a valid link for given Link Type and Link Value';
2943  return $data;
2944  }
2945 
2946  if ($link_id['locked']) {
2947  $this->returnError('Link "'.$link_id['link_id'].'" cannot be moved, link is locked');
2948  return FALSE;
2949  }
2950  $new_link_id = $GLOBALS['SQ_SYSTEM']->am->moveLink($link_id['link_id'], $new_parent_id, $new_link_type, $new_position, $new_link_value);
2951 
2952  if (!empty($new_link_id)) {
2953  //wow we moved the asset successfully, now lets try and update lookups
2954  $result = $this->updateLookupsforAsset($asset_id_to_move);
2955  if (!is_null($result)) $this->returnError('Unable to Update Lookups on the Asset #'.$asset_id_to_move.' and its Childrens');
2956  return Array (
2957  'link_id' => $new_link_id,
2958  );
2959  } else {
2960  $this->returnError('Unable to move link');
2961  }//end else
2962  } else {
2963  $this->returnError('Please provide a valid current parent id, asset id to move, and new parent id');
2964  }//end else
2965 
2966  return FALSE;
2967 
2968  }//end moveLink
2969 
2970 
2984  function updateLink($parent, $child, $existing_link_type=SQ_LINK_TYPE_1, $existing_link_value= '', $link_type=SQ_LINK_TYPE_1, $link_value='', $sort_order, $locked=TRUE)
2985  {
2986  $am = $GLOBALS['SQ_SYSTEM']->am;
2987 
2988  if (!empty($parent) && !empty($child)) {
2989 
2990  $link_id = $this->getLinkId($parent, $child, $existing_link_type, $existing_link_value, TRUE);
2991  $parent_asset = $am->getAsset($parent);
2992  $child_asset = $am->getAsset($child);
2993 
2994  if (!$link_id) {
2995  $data['error'] = 'Parent Asset #'.$parent.' and Child Asset #'.$child.' do not have a valid link for given Link Type and Link Value';
2996  return $data;
2997  }
2998 
2999  // we cannot change a link from being a significant link to a non-significant link or vice versa
3000  // so if we are try to do that, do what a backend does, delete the old one and then create a new
3001  // one with all same properties except for changed linked value
3002  $current_is_sig = (bool) ((int) $link_id['link_type'] & SQ_SC_LINK_SIGNIFICANT);
3003  $new_is_sig = (bool) ($link_type & SQ_SC_LINK_SIGNIFICANT);
3004  if ($current_is_sig !== $new_is_sig) {
3005  if ($am->deleteAssetLink($link_id['link_id'], FALSE)) {
3006  // irrespective of what is passed value of link lock, if the link isnt siginificant, then dont bother locking it
3007  if (!$new_is_sig) {
3008  $lock = 0;
3009  } else {
3010  $lock = $locked;
3011  }
3012  $new_link_id = $am->createAssetLink($parent_asset, $child_asset, $link_type, $link_value, $sort_order, $link_id['is_dependant'], $link_id['is_exclusive'], FALSE, $lock);
3013  }
3014 
3015  } else {
3016  $new_link_id = $am->updateLink($link_id['link_id'], $link_type, $link_value, $sort_order, $locked);
3017  }
3018 
3019  if (!empty($new_link_id)) {
3020  $response['success'] = 'Link #'.$link_id['link_id'].' between Asset "'.$child_asset->name.'" (#'.$child_asset->id.') and Asset "'.$parent_asset->name.'" (#'.$parent_asset->id.') has been updated';
3021  return $response;
3022  } else {
3023  $response['error'] = 'Unable to update Link #'.$link_id['link_id'].' between Asset "'.$child_asset->name.'" (#'.$child_asset->id.') and Asset "'.$parent_asset->name.'" (#'.$parent_asset->id.')';
3024  return $response;
3025  }//end else
3026  } else {
3027  $this->returnError('Please enter a valid parent id and child id');
3028  return FALSE;
3029  }//end else
3030 
3031  }//end updateLink
3032 
3033 
3043  function getPermission($id, $level)
3044  {
3045  if (!empty($id) && !empty($level)) {
3046  // Shortcuts
3047  $am = $GLOBALS['SQ_SYSTEM']->am;
3048 
3049  // Set our level
3050  if ($level == 1) {
3051  $level = SQ_PERMISSION_READ;
3052  $level_name = 'read';
3053  } else if ($level == 2) {
3054  $level = SQ_PERMISSION_WRITE;
3055  $level_name = 'write';
3056  } else if ($level == 3) {
3057  $level = SQ_PERMISSION_ADMIN;
3058  $level_name = 'admin';
3059  }//end else if
3060 
3061  $permissions = $am->getPermission($id, $level, NULL, FALSE, FALSE, TRUE);
3062  if (empty($permissions)) {
3063  $this->returnError('There are no '.$level_name.' permissions set for id #'.$id);
3064  return FALSE;
3065  }
3066 
3067  foreach ($permissions as $key => $access) {
3068 
3069  $granted = ($access == 0) ? 'denied' : 'granted';
3070  $asset = $am->getAsset($key);
3071 
3072  $data[$granted][$key]['id'] = $key;
3073  $data[$granted][$key]['name'] = $asset->name;
3074  if (isset($asset->vars['username'])) {
3075  $data[$granted][$key]['user_name'] = $asset->attr('username');
3076  }
3077  $data[$granted][$key]['type'] = $asset->type();
3078 
3079  // Expand our user groups
3080  if ($asset->type() == 'user_group') {
3081 
3082  $data[$granted][$key]['users'] = $this->getGroupChildren($key);
3083 
3084  }//end if
3085 
3086  }//end foreach
3087 
3088  return $data;
3089 
3090  } else {
3091  $this->returnError('Asset ID or Access Level is not valid. Please provide a valid AssetID and Access Level');
3092  return FALSE;
3093  }//end else
3094 
3095  }//end getPermission
3096 
3097 
3106  function getGroupChildren($key)
3107  {
3108  // Shortcuts
3109  $am = $GLOBALS['SQ_SYSTEM']->am;
3110 
3111  $group_children = $am->getChildren($key, '', TRUE, NULL, NULL, NULL, TRUE, NULL, 1);
3112  foreach ($group_children as $children => $child_id) {
3113  $child = $am->getAsset($children);
3114  $children_arr[$child->id] = Array(
3115  'id' => $child->id,
3116  'name' => $child->name,
3117  'type' => $child->type(),
3118  'username' => $child->attr('username'),
3119  );
3120  }//end foreach
3121 
3122  return $children_arr;
3123 
3124  }//end getGroupChildren
3125 
3126 
3142  function getParents($id, $level, $type_codes=Array(), $link_types=Array(), $link_values=Array(), $also_attributes=FALSE)
3143  {
3144  // Shortcuts
3145  $am = $GLOBALS['SQ_SYSTEM']->am;
3146  $parent_arr = Array();
3147 
3148  $parents = $am->getParents($id, '', TRUE, NULL, NULL, TRUE, NULL, $level);
3149  foreach ($parents as $parent => $parent_type_code) {
3150  // not in the type_code we want, remove it
3151  if(!empty($type_codes) && !in_array($parent_type_code, $type_codes)) {
3152  unset($childrens[$children]);
3153  continue;
3154  }
3155  $parent_asset = $am->getAsset($parent);
3156 
3157  // Levels greater than 1 won't have a link id with the parent
3158  if (!$this->hasLink($parent, $id)) {
3159  $show['link_id'] = NULL;
3160  $show['link_type'] = NULL;
3161  $show['is_dependant'] = NULL;
3162  $show['is_exclusive'] = NULL;
3163  $show['sort_order'] = NULL;
3164  $show['link_value'] = NULL;
3165  $show['locked'] = NULL;
3166  } else {
3167  $show_link = $this->getLinkId($parent, $id, NULL, NULL, TRUE);
3168  if (!$show_link) {
3169  $data['error'] = 'Parent Asset #'.$parent.' and Child Asset #'.$id.' do not have a valid link';
3170  return $data;
3171  }
3172 
3173  // abra-ka-dabra all except for the link_values we are interested in
3174  if (!empty($link_values) && !in_array($show_link['value'], $link_values)) {
3175  unset($childrens[$children]);
3176  continue;
3177  }
3178 
3179  if(!empty($link_types)) {
3180  $found = FALSE;
3181  foreach($link_types as $link_type) {
3182  if(defined($link_type) && (constant($link_type) == $show_link['link_type'])) {
3183  $found = TRUE;
3184  } else if (!defined($link_type)) {
3185  $found = TRUE;
3186  }
3187  }
3188  if(!$found) {
3189  unset($childrens[$children]);
3190  continue;
3191  }
3192  }
3193 
3194  $show['link_id'] = $show_link['link_id'];
3195  $show['link_type'] = $show_link['link_type'];
3196  $show['is_dependant'] = $show_link['is_dependant'];
3197  $show['is_exclusive'] = $show_link['is_exclusive'];
3198  $show['sort_order'] = $show_link['sort_order'];
3199  $show['link_value'] = $show_link['value'];
3200  $show['locked'] = $show_link['locked'];
3201  }
3202  $parent_arr[] = array_merge($this->getGeneralInfo($parent_asset, $also_attributes), $show);
3203 
3204  }//end foreach
3205 
3206  return $parent_arr;
3207 
3208  }//end getParents
3209 
3210 
3226  function getChildren($id, $level, $type_codes=Array(), $link_types=Array(), $link_values=Array(), $also_attributes=FALSE)
3227  {
3228  // Shortcuts
3229  $am = $GLOBALS['SQ_SYSTEM']->am;
3230  $children_arr =Array();
3231  if (empty($type_codes)) $type_codes = '';
3232  foreach ($link_types as $index => $link_type) {
3233  if(defined($link_type)) $link_types[$index] = constant($link_type);
3234  }
3235 
3236  $children = $am->getChildren($id, $type_codes, TRUE, NULL, NULL, NULL, TRUE, NULL, $level, TRUE, NULL, $link_types);
3237 
3238  foreach ($children as $child_id => $child_info) {
3239 
3240  // Levels lower than 1 won't have a link id with the parent
3241  if (!$this->hasLink($id, $child_id)) {
3242  $show['link_id'] = NULL;
3243  $show['link_type'] = NULL;
3244  $show['is_dependant'] = NULL;
3245  $show['is_exclusive'] = NULL;
3246  $show['sort_order'] = NULL;
3247  $show['link_value'] = NULL;
3248  $show['locked'] = NULL;
3249  } else {
3250  $show_link = $this->getLinkId($id, $child_id, NULL, NULL, TRUE);
3251 
3252  // avada kedavra all except fo the link_values we are interested in
3253  if (!empty($link_values) && !in_array($show_link['value'], $link_values)) {
3254  unset($children[$child_id]);
3255  continue;
3256  }
3257 
3258  $show['link_id'] = $show_link['link_id'];
3259  $show['link_type'] = $show_link['link_type'];
3260  $show['is_dependant'] = $show_link['is_dependant'];
3261  $show['is_exclusive'] = $show_link['is_exclusive'];
3262  $show['sort_order'] = $show_link['sort_order'];
3263  $show['link_value'] = $show_link['value'];
3264  $show['locked'] = $show_link['locked'];
3265  }
3266 
3267  $sub_children = $am->getLinks($child_id, SQ_SC_LINK_ALL);
3268  $sub_dependent_count = $sub_non_dependent_count = $sub_shadow_child_count = 0;
3269 
3270  foreach ($sub_children as $idx => $sub_child) {
3271 
3272  if ($sub_child['link_type'] == SQ_LINK_NOTICE) $sub_shadow_child_count++;
3273  else if ($sub_child['is_dependant']) $sub_dependent_count++;
3274  else if ($sub_child['is_dependant'] == 0) $sub_non_dependent_count++;
3275  }
3276 
3277  $show['direct_dependant_children'] = $sub_dependent_count;
3278  $show['direct_non_dependant_children'] = $sub_non_dependent_count;
3279  $show['direct_notice_linked_children'] = $sub_shadow_child_count;
3280 
3281  $child = $am->getAsset($child_id);
3282  $children_arr[] = array_merge($this->getGeneralInfo($child, $also_attributes), $show);
3283 
3284  }//end foreach
3285 
3286  return $children_arr;
3287 
3288  }//end getChildren
3289 
3290 
3300  function trashAsset($ids)
3301  {
3302  // Set some shortcuts
3303  $am = $GLOBALS['SQ_SYSTEM']->am;
3304  $response = Array();
3305 
3306  if (!empty($ids)) {
3307  foreach($ids as $id) {
3308  $already_in_trash = $am->assetInTrash($id, TRUE);
3309  $asset = $am->getAsset($id);
3310  //need to make sure that links are not locked
3311  $parents = $this->getParents($id, 1);
3312  $locked = FALSE;
3313  //loop through each parents and see if any of the links are locked
3314  foreach ($parents as $parentid => $parent_link_info) {
3315  if($parent_link_info['locked'] == '1') $locked = TRUE;
3316  }
3317 
3318  if ($already_in_trash) {
3319  $response[] = 'Asset "'.$asset->name.'" (#'.$asset->id.') is already in the trash';
3320  } else if ($locked && $asset->writeAccess()){
3321  // few or all the links are locked so lets go through each one of them and try to delete the links
3322  foreach ($parents as $parentid => $parent_link_info) {
3323  $parent_asset = $am->getAsset($parentid);
3324  if ($parent_link_info['locked'] == '1') {
3325  // found the link locked....
3326  $response[] = 'Link between Asset "'.$asset->name.'" (#'.$asset->id.') and Asset "'.$parent_asset->name.'" (#'.$parent_asset->id.') is locked';
3327  } else {
3328  //link not locked....lets get it done with
3329  if($am->deleteAssetLink($parent_link_info['link_id'])) {
3330  $response[] = 'Successfully unlinked link between Asset "'.$asset->name.'" (#'.$asset->id.') and Asset "'.$parent_asset->name.'" (#'.$parent_asset->id.')';
3331  }
3332  }
3333  }
3334  } else if ($asset->writeAccess()) {
3335  // asset isnt in trash and none of the links are locked, Hooorrrrray !!!!
3336  $am->acquireLock($id, 'all');
3337  $result = $am->trashAsset($id, TRUE);
3338  if (!$result) {
3339  $response[] = 'Could not move Asset "'.$asset->name.'" (#'.$asset->id.') to Trash';
3340  } else {
3341  //done trashing the asset? update lookups then
3342  $result_from_update = $this->updateLookupsforAsset($id);
3343  if (!is_null($result_from_update)) {
3344  $response[] = 'Asset deleted but unable to Update Lookups on the Asset "'.$asset->name.'" (#'.$asset->id.')';
3345  } else {
3346  //all good? let the user know what we did
3347  $response[] = 'Asset "'.$asset->name.'" (#'.$asset->id.') successfully moved to Trash';
3348  }
3349  }
3350  $am->releaseLock($id, 'all');
3351  } else {
3352  $response[] = 'Not enough permision to delete Asset "'.$asset->name.'" (#'.$asset->id.')';
3353  }
3354 
3355  }
3356  } else {
3357  $this->returnError('Please provide a valid Asset ID');
3358  return FALSE;
3359  }//end else
3360 
3361  return $response;
3362 
3363  }//end trashAsset()
3364 
3365 
3376  function setAttributeValue($assetid, $attr_name, $attr_val)
3377  {
3378  if (!empty($assetid) && !empty($attr_name) && !empty($attr_val)) {
3379  if (count($attr_name) != count($attr_val) ) $this->returnError('Please provide Attributes and corresponding values correctly');
3380  // Shortcuts
3381  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
3382  $am = $GLOBALS['SQ_SYSTEM']->am;
3383 
3384  // Change our values
3385  $am->acquireLock($assetid, 'attributes');
3386  for ($i = 0; $i < count($attr_name); $i++ ) {
3387  $attributes = $am->getAssetTypeAttributes($asset->type());
3388  if ($attr_name[$i] != '' && array_key_exists($attr_name[$i], $attributes)) {
3389  //set our new attribute
3390  $attr_set = $asset->setAttrValue($attr_name[$i], $attr_val[$i]);
3391  if(!$attr_set) {
3392  $attr_type = $attributes[$attr_name[$i]]['type'];
3393  $data[] = 'Attribute "'.$attr_name[$i].'"(of type "'.$attr_type.'") could not be set to "'.$attr_val[$i].'" for Asset "'.$asset->name.'" (#'.$assetid.')';
3394  continue;
3395  }
3396  } else if (!array_key_exists($attr_name[$i], $attributes)){
3397  $data[] = 'Attribute "'.$attr_name[$i].'" does not exist for Asset "'.$asset->name.'" (#'.$assetid.')';
3398  continue;
3399  }
3400  if (!$asset->saveAttributes()) {
3401  $data[] = 'Attribute "'.$attr_name[$i].'" could not be set to "'.$attr_val[$i].'" for Asset "'.$asset->name.'" (#'.$assetid.')';
3402  } else {
3403  $data[] = 'Attribute "'.$attr_name[$i].'" has been successfully set to "'.$attr_val[$i].'" for Asset "'.$asset->name.'" (#'.$assetid.')';
3404  }
3405  }//end for
3406  $am->releaseLock($assetid, 'attributes');
3407  $am->forgetAsset($asset);
3408 
3409  return $data;
3410  } else {
3411  $this->returnError('Please make sure to provide an Asset ID, Attribute Name and Attribute Value');
3412  }//end else
3413 
3414  }//end setAttributeValue()
3415 
3416 
3427  function setAssetMetadata($assetid, $fieldid, $new_value)
3428  {
3429  if (!empty($assetid) && !empty($fieldid)) {
3430  if (count($fieldid) != count($new_value) ) $this->returnError('Please provide Metadata fieldids and corresponding values correctly');
3431  $s_result = TRUE;
3432  $g_result = TRUE;
3433 
3434  // Shortucts
3435  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
3436  $am = $GLOBALS['SQ_SYSTEM']->am;
3437 
3438  $am->acquireLock($assetid, 'metadata');
3439  // All context metadata files need to be regenerated if there is a non-contextable field changed
3440  $regen_all_contexts = FALSE;
3441  for ($i = 0; $i < count($fieldid); $i++ ) {
3442 
3443  $field = $am->getAsset($fieldid[$i]);
3444  if (!($field instanceof Metadata_Field)) {
3445  $this->returnError('The Field ID provided does not belong to a metadata field');
3446  }//end if
3447 
3448  if (!$regen_all_contexts && (!$field->attr('is_contextable') || $field instanceof Metadata_Field_Select)) {
3449  $regen_all_contexts = TRUE;
3450  }
3451 
3452  $field_name = $field->attr('name');
3453  $metadata_info = Array (
3454  $fieldid[$i] => Array (
3455  Array (
3456  'name' => $field_name,
3457  'value' => $new_value[$i],
3458  ),
3459  ),
3460  );
3461  // Set metadata
3462  if (!$mm->setMetadata($assetid, $metadata_info)) $s_result = FALSE;
3463 
3464  }//end for
3465  $am->releaseLock($assetid, 'metadata');
3466 
3467  // if the Metadata wasnt set correctly, dont even worry about regenerating the metadata file
3468  // just return from here telling the user it was a big time fail ;)
3469  if(!$s_result) {
3470  $this->returnError('Unable to set Matadata for Asset #'.$assetid);
3471  return FALSE;
3472  }
3473 
3474  // instead for doing it for every field, lets regen content file just once here
3475  if ($regen_all_contexts) {
3476  $all_contexts = $GLOBALS['SQ_SYSTEM']->getAllContexts();
3477  foreach ($all_contexts as $contextid => $context_data) {
3478  if (!$mm->generateContentFile($assetid, FALSE, $contextid)) {
3479  $g_result = FALSE;
3480  break;
3481  }
3482  }
3483  } else {
3484  if (!$mm->generateContentFile($assetid)) $g_result = FALSE;
3485  }
3486 
3487  if ($s_result && $g_result) {
3488  return TRUE;
3489  } else {
3490  $this->returnError('Unable To Regenerate Metadata For Asset #'.$assetid);
3491  return FALSE;
3492  }//end else
3493  } else {
3494  $this->returnError('Please provide both AssetID and FieldID of the metadata field');
3495  return FALSE;
3496  }//end else
3497 
3498  }//end setAssetMetadata()
3499 
3500 
3513  function getLinkId($parent, $child, $link_type=SQ_LINK_TYPE_1, $link_value=NULL, $all_info = FALSE)
3514  {
3515  if (!empty($parent) && !empty($child)) {
3516  $link = $GLOBALS['SQ_SYSTEM']->am->getLinkByAsset($parent, $child, $link_type, $link_value, 'major', TRUE);
3517  if (empty($link)) {
3518  return FALSE;
3519  } else if (count($link) > 1) {
3520  $this->returnError('More than one link found between Parent Asset #'.$parent.' and Child Asset #'.$child.', please filter out with help of Link value and Link Type');
3521  return FALSE;
3522  }
3523 
3524  $link = $link[0];
3525  if (!$all_info) {
3526  $data_array['link_id'] = array_get_index($link, 'linkid', 0);
3527  return $data_array;
3528  } else {
3529  // we are returning more information
3530  $data_array = Array (
3531  'link_id' => isset($link['linkid']) ? $link['linkid'] : '',
3532  'minorid' => isset($link['minorid']) ? $link['minorid'] : '',
3533  'majorid' => $parent,
3534  'value' => isset($link['value']) ? $link['value'] : '',
3535  'link_type' => isset($link['link_type']) ? $link['link_type'] : '',
3536  'sort_order' => isset($link['sort_order']) ? $link['sort_order'] : '',
3537  'is_dependant' => isset($link['is_dependant']) ? $link['is_dependant'] : '',
3538  'is_exclusive' => isset($link['is_exclusive']) ? $link['is_exclusive'] : '',
3539  'locked' => isset($link['locked']) ? $link['locked'] : '',
3540  );
3541  return $data_array;
3542  }
3543  } else {
3544  $this->returnError('Please provide a parent id and child id');
3545  return FALSE;
3546  }//end else
3547 
3548  }//end getLinkId
3549 
3550 
3560  function hasLink($parent, $child)
3561  {
3562  if (!empty($parent) && !empty($child)) {
3563  $link = $GLOBALS['SQ_SYSTEM']->am->getLinkByAsset($parent, $child);
3564  $linkid = array_get_index($link, 'linkid', 0);
3565  if (!empty($linkid)) {
3566  // Found a link
3567  return TRUE;
3568  }//end if
3569  }//end if
3570 
3571  return FALSE;
3572 
3573  }//end hasLink
3574 
3575 
3593  function createAsset($id, $type_code, $asset_name, $link_type=1, $link_value, $sort_order, $is_dependant=0, $is_exclusive=0, $extra_attributes=FALSE, $attribute_values)
3594  {
3595  // Shortucts
3596  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
3597  $am = $GLOBALS['SQ_SYSTEM']->am;
3598  // Get our parent asset
3599  $pa = $am->getAsset($id);
3600  // Set our asset type to create
3601  $type_code = strtolower($type_code);
3602  if (!$am->installed($type_code)) {
3603  return Array('error' => 'Asset type requested to create not installed on the system');
3604  }//end if
3605 
3606  // Are we allowed to create this?
3607  $types = $this->attr('types');
3608  if (!empty($types)) {
3609  if (!isset($types[$type_code])) {
3610  return Array('error' => 'Asset type requested to create is not allowed via API');
3611  }
3612  }
3613 
3614  // Are we allowed to create this asset under this parent type?
3615  $restrict = $this->attr('types_restriction');
3616  if (!empty($restrict)) {
3617  if (!$this->checkParentType($pa)) {
3618  return Array('error' => 'Parent asset passed does not match the restricted Parents type configured');
3619  }
3620  }
3621 
3622  $am->includeAsset($type_code);
3623 
3624  $object_name = str_replace(' ', '_', ucwords(str_replace('_', ' ', $type_code)));
3625  $asset = new $object_name();
3626  $asset->setAttrValue('name', $asset_name);
3627  $request_info['asset'] = $pa;
3628  // Set our asset info
3629  $link_info = Array (
3630  'asset' => $pa,
3631  'name' => $asset_name,
3632  'link_type' => $link_type,
3633  'value' => $link_value,
3634  'sort_order' => $sort_order,
3635  'is_dependant' => $is_dependant,
3636  'is_exclusive' => $is_exclusive,
3637  );
3638 
3639  // Should we add some attributes to our new asset?
3640  if ($extra_attributes && $this->attr('create_attributes')) {
3641  // Check our post values for any correct attibutes
3642  $attributes = $am->getAssetTypeAttributes($type_code);
3643  foreach($attribute_values as $name => $value) {
3644  if (array_key_exists($name, $attributes) && $name !== $link_info[$name]) {
3645  // Set our new values
3646  $asset->setAttrValue($name, $value);
3647  }//end if
3648  }//end foreach
3649  }//end if
3650 
3651  $link_id = $asset->create($link_info);
3652 
3653  if ($link_id) {
3654  // Save and quit
3655  $am->acquireLock($asset->id, 'attributes');
3656  $asset->saveAttributes();
3657  $am->releaseLock($asset->id, 'attributes');
3658  return Array (
3659  'name' => $asset_name,
3660  'id' => $asset->id,
3661  'link_id' => $link_id,
3662  );
3663  }//end if
3664 
3665  return Array('error' => 'Error occured while asset creation');
3666 
3667  }//end createAsset
3668 
3669 
3678  function checkParentType($parent)
3679  {
3680  // Are we allowed to create this asset under this parent type?
3681  if (array_key_exists($parent->type(), $this->attr('types_restriction'))) {
3682  return TRUE;
3683  }
3684 
3685  }//end checkParentType
3686 
3687 
3696  function checkRoot(&$asset)
3697  {
3698  // This is the root node, will allow it
3699  $root_nodes = $this->getRootNodes();
3700 
3701  // check to see if we ARE the root node OR
3702  // one of the root node is ROOT_FOLDER
3703  // that way we can cut down the queries
3704  if (in_array($asset->id, $root_nodes) || in_array('1', $root_nodes)) return TRUE;
3705 
3706  // If a shadow link test the bridge instead
3707  if (strpos($asset->id, ':') !== FALSE) {
3708  $matches = explode(':', $asset->id);
3709  $test_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($matches[0]);
3710  } else {
3711  $test_asset = $asset;
3712  }//end if
3713 
3714  $is_under = $GLOBALS['SQ_SYSTEM']->am->isUnderRootNodes($test_asset->id, $root_nodes);
3715 
3716  return $is_under;
3717 
3718  }//end checkRoot
3719 
3720 
3727  public function getRootNodes()
3728  {
3729  $root_nodes = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_NOTICE, '', TRUE, 'major', 'root');
3730  $nodes = Array();
3731  foreach ($root_nodes as $node) {
3732  $node_id = array_get_index($node, 'minorid', 0);
3733  if (!empty($node_id)) $nodes[] = $node_id;
3734  }//end foreach
3735 
3736  return $nodes;
3737 
3738  }//end getRootNodes()
3739 
3740 
3747  public function updateLookupsforAsset($assetid)
3748  {
3749  require_once SQ_SYSTEM_ROOT.'/core/hipo/jobs/hipo_job_update_lookups.inc';
3750  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
3751 
3752  $vars = Array('assetids' => Array($assetid));
3753  $hh->freestyleHipo('hipo_job_update_lookups', $vars);
3754 
3755  }//end updateLookupsforAsset()
3756 
3757 
3767  public function getKeywordsReplacements($asset, $keywords)
3768  {
3769  require_once SQ_INCLUDE_PATH.'/general.inc';
3770  $return_array = Array();
3771  $se_level_keywords = Array();
3772  //lets split asset/global level keywords as opposed to simple edit layout keywords
3773  foreach ($keywords as $index => $keyword) {
3774  if(!(strpos($keyword, '%asset_') === 0 || strpos($keyword, '%globals_') === 0) ) {
3775  //seems like its a simple edit screen keywords
3776  $se_level_keywords[] = $keyword;
3777  unset($keywords[$index]);
3778  }
3779  }
3780 
3781  foreach ($keywords as $keyword) {
3782  $trimmed_keyword = trim($keyword,'%');
3783  if (strpos($keyword, '%globals_') === 0) {
3784  $return_array[$trimmed_keyword] = replace_global_keywords($keyword);
3785  } else if (strpos($keyword, '%asset_') === 0 ) {
3786  $return_array[$trimmed_keyword] = $asset->getKeywordReplacement($trimmed_keyword);
3787  }
3788  if (empty($return_array[$trimmed_keyword]) || $return_array[$trimmed_keyword] == '%'.$trimmed_keyword.'%') {
3789  $return_array[$trimmed_keyword] = 'Could not get replacement for keyword : '.$trimmed_keyword ;
3790  }
3791  }
3792 
3793  if(!empty($se_level_keywords)) {
3794  // create layout on the fly
3795  $GLOBALS['SQ_SYSTEM']->am->includeAsset('layout');
3796  $layout = new Layout();
3797  // create limbo outputter on fly
3798  require_once SQ_INCLUDE_PATH.'/limbo_outputter.inc';
3799  $limbo_outputter = new Limbo_Outputter();
3800  // get asset_edit_interface
3801  require_once SQ_SYSTEM_ROOT.'/core/include/asset_edit_interface.inc';
3802  $ei = new Asset_Edit_Interface($asset->type());
3803 
3804  // determine and set the preview url
3805  // we don't care about the returned value, it's not used here
3806  $ei->_determinePreviewUrl($asset, $limbo_outputter);
3807 
3808  foreach ($se_level_keywords as $index => $se_keyword) {
3809  $se_keyword = trim($se_keyword, '%');
3810  $layout->layout_keywords = $layout->getLayoutKeywordsArray(Array ($se_keyword));
3811  //sort order isnt really important here, so we always use like follows
3812  $screen_sort_order = Array ('0' => 'custom', '1' => 'screen');
3813  $return_array[$se_keyword] = $ei->getKeywordsReplacementsForPaint($asset, $limbo_outputter, $layout, $screen_sort_order, FALSE, TRUE);
3814  // lets replace % with &#37; so that while rendering on frontend %asset_name% (keywords)
3815  // are not replaced and the raw keyword is spat out. but also make sure to do this only to keywords
3816  $pattern = '/(.)(%)([asset_|globals_]?[a-zA-Z0-9_:\.\^]*)(%)(.)/';
3817  $replacement = '$1&#37;$3&#37;$5';
3818  $return_array[$se_keyword] = preg_replace($pattern, $replacement ,$return_array[$se_keyword]);
3819  $pattern_2 = '/([\"|\']{0}%)([asset_|globals_]?[a-zA-Z0-9_:\.\^]*)(%[\"|\']{0})/';
3820  $replacement_2 = '&#37;$2&#37;';
3821  $return_array[$se_keyword] = preg_replace($pattern_2, $replacement_2 ,$return_array[$se_keyword]);
3822 
3823  if (empty($return_array[$se_keyword])) $return_array[$se_keyword] = 'Could not get replacement for keyword : '.$se_keyword ;
3824  }
3825  }
3826 
3827  return $return_array;
3828 
3829  }//end getKeywordsReplacements()
3830 
3831 
3844  public function acquireLocks(Asset $asset, $screen, $dependants_only , $force_acquire)
3845  {
3846  $return_array = Array();
3847 
3848  //if the we arent trying to get all locks and if the locks name specified doesnt exist for this asset then start complaining
3849  if(!in_array($screen, $GLOBALS['SQ_SYSTEM']->am->getLockTypes($asset->id, 'all')) && $screen != 'all') {
3850  $return_array[] = '"'.$screen.'" type lock doesnt exist for Asset "'.$asset->name.'" (#'.$asset->id.').';
3851  return $return_array;
3852  }
3853 
3854  //lets prepare a hipo running vars
3855  $res = NULL;
3856  $locks = $this->getLocksInfo($asset->id, $screen);
3857 
3858  if (empty($locks) || (!empty($locks) && $force_acquire) || (!empty($locks) && $screen == 'all')) {
3859  $job_dir = SQ_SYSTEM_ROOT.'/core/hipo/jobs';
3860  require_once $job_dir.'/hipo_job_acquire_locks.inc';
3861  $init_hipo = new Hipo_Job_Acquire_Locks();
3862  $running_vars = Array(
3863  'assetids' => Array($asset->id),
3864  'lock_type' => $screen,
3865  'forceably_acquire' => $force_acquire,
3866  'dependants_only' => $dependants_only,
3867  );
3868  $init_hipo->setRunningVars($running_vars);
3869  $init_hipo->_steps = $init_hipo->getInitialStepData();
3870  if (!$init_hipo->prepare()) {
3871  $return_array['error'] = 'Could not initialise HIPO job';
3872  return $return_array;
3873  }
3874  set_error_handler(Array(&$init_hipo, '_errorHandler'));
3875  $init_hipo->freestyle();
3876  restore_error_handler();
3877  $return_array = $init_hipo->getErrors();
3878  } else {
3879  // the locks are already acquired on the asset
3880  // if we are asked again to do it mean we have
3881  // to update the locks
3882  if (!$GLOBALS['SQ_SYSTEM']->am->updateLock($asset->id, $screen)) {
3883  $return_array['error'] = 'Unable to update locks for Asset '.$asset->name.'('.$asset->id.')';
3884  } else {
3885  $return_array[] = 'Locks for \''.$screen.'\' screen for Asset '.$asset->name.'('.$asset->id.') successfully updated';
3886  }
3887  }
3888 
3889  return array_unique($return_array);
3890 
3891  }//end acquireLocks()
3892 
3893 
3904  public function getLocksInfo($assetid, $lock_types)
3905  {
3906  $return_array = $GLOBALS['SQ_SYSTEM']->am->getLockInfo($assetid, $lock_types);
3907  foreach ($return_array as $screen => $lock_info){
3908  if (empty($lock_info)){
3909  unset($return_array[$screen]);
3910  }
3911  }
3912  if($lock_types != 'all' && !empty($return_array) && !(array_key_exists('attributes', $return_array) || array_key_exists('links', $return_array))) $return_array = Array ($return_array);
3913 
3914  return $return_array;
3915 
3916  }//end getLocksInfo()
3917 
3918 
3931  public function setAssetStatus(Asset $asset, $status, $cascade, $current_status_desc, $to_be_status_desc, $workflow_stream)
3932  {
3933 
3934  $return_array = Array();
3935  //if status is being set to something we are not allowed to set then start complaining
3936  if(!(in_array($status, array_keys($asset->getAvailableStatii()))) && !($status == $asset->status && $cascade) ) {
3937  $return_array[] = '"'.$to_be_status_desc.'" is not a valid option for the "'.$asset->name.'" asset (#'.$asset->id.') in its current "'.$current_status_desc.'" state';
3938  return $return_array;
3939  }
3940 
3941  //lets prepare a hipo running vars
3942  $job_dir = SQ_SYSTEM_ROOT.'/core/hipo/jobs';
3943  require_once $job_dir.'/hipo_job_edit_status.inc';
3944  $init_hipo = new HIPO_Job_Edit_Status();
3945  $running_vars = Array(
3946  'assetid' => $asset->id,
3947  'new_status' => $status,
3948  'dependants_only' => !$cascade,
3949  );
3950  if ($workflow_stream !== 'SQ_USE_DEFAULT') {
3951  $running_vars['workflow_stream'] = $workflow_stream;
3952  }
3953 
3954  $init_hipo->setRunningVars($running_vars);
3955  $init_hipo->_steps = $init_hipo->getInitialStepData();
3956  if (!$init_hipo->prepare()) {
3957  $return_array[] = 'Could not initialise HIPO job';
3958  return $return_array;
3959  }
3960  set_error_handler(Array(&$init_hipo, '_errorHandler'));
3961  $init_hipo->freestyle();
3962  restore_error_handler();
3963  $return_array = $init_hipo->getErrors();
3964 
3965  return $return_array;
3966 
3967  }//end setAssetStatus()
3968 
3969 
3980  function getMetadataSchema($asset, $granted, $cascades)
3981  {
3982  $return_array = Array();
3983 
3984  if (is_null($granted)) {
3985  $granted = TRUE;
3986  }
3987 
3988  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
3989  $schemas = $mm->getSchemas($asset->id, $granted, $cascades);
3990  if (!empty($schemas)) {
3991  foreach($schemas as $schema_id) {
3992  $return_array[$schema_id] = Array();
3993  $field_children = $GLOBALS['SQ_SYSTEM']->am->getChildren($schema_id, 'metadata_field', FALSE);
3994  foreach($field_children as $field_id => $field_type) {
3995  $return_array[$schema_id][$field_id] = $GLOBALS['SQ_SYSTEM']->am->getAllAttributeValues($field_id, $field_type[0]['type_code']);
3996  }//end foreach
3997  }//end foreach
3998  } else {
3999  $return_array['error'] = 'Asset #'.$asset->id.' does not has any Metadata Schema '.($granted ? 'applied' : 'denied') .' to it';
4000  }
4001 
4002  return $return_array;
4003 
4004  }//end getMetadataSchema()
4005 
4006 
4014  function returnError($error)
4015  {
4016  // Set our error
4017  trigger_error($error, E_USER_WARNING);
4018 
4019  // Send error as JSON so they are not confused why the function quit
4020  $data['error'] = $error;
4021 
4022  // Send our data as JSON
4023  if (isset($data)) {
4024  $this->returnJSON($data);
4025  }//end if
4026 
4027  }//end returnError()
4028 
4029 
4037  function returnJSON($data, $type='')
4038  {
4039  // Are we allowed to send JSON? OR return JSON when the function is a 'get' OR on error which will be empty type
4040  if ($this->attr('allow_json') || (!empty($type) && (strpos($type, 'get') === 0))) {
4041  // Send our data as JSON
4042  if (!empty($data)) {
4043  if (!function_exists('json_encode')) {
4044  require_once 'Services/JSON.php';
4045  $json = new Services_JSON();
4046  $output = $json->encode($data);
4047  if (Services_JSON::isError($output)) {
4048  $this->returnError($output);
4049  }
4050  } else {
4051  // all ISO characters have to be converted to UTF8 encoding
4052  if(SQ_CONF_DEFAULT_CHARACTER_SET == 'iso-8859-1')
4053  $data = $this->array_utf8_encode_recursive($data);
4054  $output = json_encode($data);
4055  if (function_exists('json_last_error') && (json_last_error() != JSON_ERROR_NONE)) {
4056  switch (json_last_error()) {
4057  case JSON_ERROR_DEPTH:
4058  $error = 'Maximum stack depth exceeded';
4059  break;
4060  case JSON_ERROR_STATE_MISMATCH:
4061  $error = 'Underflow or the modes mismatch';
4062  break;
4063  case JSON_ERROR_CTRL_CHAR:
4064  $error = 'Unexpected control character found';
4065  break;
4066  case JSON_ERROR_SYNTAX:
4067  $error = 'Syntax error, malformed JSON';
4068  break;
4069  case JSON_ERROR_UTF8:
4070  $error = 'Malformed UTF-8 characters, possibly incorrectly encoded';
4071  break;
4072  default:
4073  $error = 'Unknown error';
4074  break;
4075  }
4076  $this->returnError($error);
4077  }
4078  }//end else
4079 
4080  // Send our JSON
4081  $output = preg_replace('/(%){1}(globals_[snippet|asset][_A-Za-z0-9\-:]*)(%){1}/','&#37;$2&#37;', $output);
4082  $GLOBALS['SQ_SYSTEM']->setGlobalDefine('SQ_REPLACE_MYSOURCE_LEVEL_KEYWORDS', FALSE);
4083 
4084  echo $output;
4085 
4086  }//end if
4087 
4088  }//end if
4089 
4090  }//end returnJSON()
4091 
4092 
4101  function getRandomFilename($file_name)
4102  {
4103  $hash_file_name = hash('md5', $file_name.time());
4104  $rand_position = rand(0, strlen($hash_file_name)-6);
4105  $tmp_file_name = substr($hash_file_name, $rand_position, 6);
4106 
4107  return $tmp_file_name;
4108 
4109  }//end getRandomFileName()
4110 
4111 
4120  function object2Array($obj)
4121  {
4122  $array = Array();
4123  if (is_object($obj)) {
4124  foreach ($obj as $key => $value) {
4125  if (is_object($value)) {
4126  $array[$key] = $this->object2Array($value);
4127  } else {
4128  $array[$key] = $value;
4129  }
4130  }
4131  } else {
4132  $array = $obj;
4133  }
4134 
4135  return $array;
4136 
4137  }// end object2Array
4138 
4139 
4148  function array_utf8_encode_recursive($dat)
4149  {
4150  if (is_string($dat)) {
4151  return utf8_encode($dat);
4152  }
4153 
4154  if (is_object($dat)) {
4155  $ovs= get_object_vars($dat);
4156  $new=$dat;
4157  foreach ($ovs as $k =>$v) {
4158  $new->$k= $this->array_utf8_encode_recursive($new->$k);
4159  }
4160  return $new;
4161  }
4162 
4163  if (!is_array($dat)) return $dat;
4164  $ret = array();
4165  foreach($dat as $i=>$d) {
4166  $ret[$i] = $this->array_utf8_encode_recursive($d);
4167  }
4168 
4169  return $ret;
4170 
4171  }//end array_utf8_encode_recursive
4172 
4173 
4174 }//end class
4175 
4176 
4177 ?>