Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_replace_file_asset.inc
1 <?php
17 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
18 require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
19 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_action/trigger_action.inc';
20 
33 {
34 
35 
53  public static function execute($settings, &$state)
54  {
55  // check required settings
56  if (empty($settings['file_path'])) return FALSE;
57  if (!is_readable($settings['file_path'])) {
58  trigger_localised_error('CORE0276', E_USER_WARNING, htmlentities($settings['file_path'], ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET));
59  return FALSE;
60  }
61 
62  if (empty($state['asset'])) {
63  // grab the asset if assetid is given but not the asset.
64  if (empty($state['assetid'])) {
65  return FALSE;
66  } else {
67  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
68  }
69  }
70 
71  if (is_null($state['asset'])) return FALSE;
72  if (!($state['asset'] instanceof File)) {
73  trigger_error('Unable to replace file content for a '.$state['asset']->type().' asset', E_USER_NOTICE);
74  return FALSE;
75  }
76 
77  $edit_fns = $state['asset']->getEditFns();
78  $lock_acquired = ($GLOBALS['SQ_SYSTEM']->am->acquireLock($state['asset']->id, 'attributes') == 1);
79 
80  $info = Array(
81  'name' => basename($settings['file_path']),
82  'tmp_name' => $settings['file_path'],
83  'non_uploaded_file' => TRUE,
84  );
85  $o = NULL; // as long as we don't try to redirect, this will be OK
86  $res = $edit_fns->processFileUpload($state['asset'], $o, '', $info, FALSE);
87  if ($lock_acquired) {
88  $GLOBALS['SQ_SYSTEM']->am->releaseLock($state['asset']->id, 'attributes');
89  }
90  return $res ? $info : FALSE;
91 
92  }//end execute()
93 
94 
105  public static function getInterface($settings, $prefix, $write_access=FALSE)
106  {
107  $file_path = array_get_index($settings, 'file_path', '');
108  ob_start();
109  if ($write_access) {
110  text_box($prefix.'[file_path]', $file_path, 40);
111  } else {
112  echo '<b>'.$file_path.'</b>';
113  }
114  $value = ob_get_clean();
115  $res = translate('trigger_replace_file', $value);
116  if ($write_access) {
117  $res .= '<p class="sq-backend-warning"><b>'.translate('trigger_replace_file_type_warning').'</b></p>';
118  }
119  return $res;
120 
121  }//end getInterface()
122 
123 
135  public static function processInterface(&$settings, $request_data)
136  {
137  if (!empty($request_data['file_path'])) {
138  $settings['file_path'] = $request_data['file_path'];
139  }
140  return FALSE;
141 
142  }//end processInterface()
143 
144 
145 }//end class
146 
147 ?>