Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hipo_job_import_assets_from_xml.inc
1 <?php
17 require_once SQ_SYSTEM_ROOT.'/core/hipo/hipo_job.inc';
18 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
19 require_once SQ_LIB_PATH.'/import_export/import.inc';
20 
21 
37 {
38 
39 
40  public $import_action_outputs = Array();
41 
47  function HIPO_Job_Import_Assets_From_Xml($code_name='')
48  {
49  $this->uses_trans = FALSE;
50  $this->HIPO_Job($code_name);
51 
52  }//end constructor
53 
54 
61  function getCodeName()
62  {
63  return 'HIPO_Job_Import_Assets_From_Xml-'.md5($this->_running_vars['create_under_assetid'].$this->_running_vars['file_info']);
64 
65  }//end getCodeName()
66 
67 
74  function getHipoName()
75  {
76  return translate('tool_import_assets_from_xml_hipo_name');
77 
78  }//end getHipoName()
79 
80 
88  function getInitialStepData()
89  {
90  return Array(
91  Array(
92  'name' => translate('tool_import_assets_from_xml_hipo_name'),
93  'function_call' => Array(
94  'process_function' => 'processImportAsset',
95  ),
96  'running_mode' => 'server',
97  'auto_step' => TRUE,
98  'allow_cancel' => TRUE,
99  'percent_done' => 0,
100  'complete' => FALSE,
101  'message' => '',
102  ),
103  );
104 
105  }//end getInitialStepData()
106 
107 
114  function freestyle()
115  {
116  $step_data['complete'] = FALSE;
117  while(!$step_data['complete']) {
118  if(!$this->processImportAsset($step_data, '')) {
119  return FALSE;
120  }
121  }
122 
123  return TRUE;
124 
125  }//end freestyle()
126 
127 
134  function prepare()
135  {
136  $this->_running_vars['import_action_outputs'] = Array();
137  $this->_running_vars['nest_content_to_fix'] = Array();
138  $file_info = $this->_running_vars['file_info'];
139  $import_actions = get_import_actions($file_info);
140 
141  // if we want to explictily target the root node under which the structure is imported , do it
142  if(isset($this->_running_vars['create_under_assetid']) && $this->_running_vars['create_under_assetid'] != 0) {
143  $import_actions['actions'][0]['action'][0]['parentid'][0] = $this->_running_vars['create_under_assetid'];
144  }
145 
146  // fix the file import path, it should be relative to the export dir
147  foreach ($import_actions['actions'][0]['action'] as $id => $action) {
148  if($action['action_type'][0] === 'create_file_asset') {
149  $action['file_path'][0] = dirname($file_info).'/'.$action['file_path'][0];
150  $import_actions['actions'][0]['action'][$id] = $action;
151  }
152  // remember nest content to fix
153  if($action['action_type'][0] === 'create_asset' && $action['type_code'][0] === 'Content_Type_Nest_Content') {
154  $this->_running_vars['nest_content_to_fix'] [] = $action['action_id'][0];
155  }
156 
157  // check if there is hard coded assetid reference which doesn't exist in the target system
158  if(!$this->checkAssetExists($action, 'parentid') || !$this->checkAssetExists($action, 'assetid') || !$this->checkAssetExists($action, 'asset')) {
159  $this->_addError('Action ID "'.$action['action_id'][0].'" contains non-exist assetid reference. Action skipped.', E_USER_WARNING);
160  unset($import_actions['actions'][0]['action'][$id]);
161  }
162  }
163 
164  $this->_running_vars['import_actions'] = $import_actions;
165  $this->_running_vars['total_count'] = count($import_actions['actions'][0]['action']);
166 
167 
168  if (empty($file_info)) {
169  trigger_localised_error('HIPO0095', E_USER_NOTICE);
170  return FALSE;
171  }
172 
173  $root_node_id = $this->_running_vars['create_under_assetid'];
174  // if value of root node is 0 then we are good. It just means we are not selecting root node
175  // but if we have a value, then make sure it is a proper asset
176  if($root_node_id != 0) {
177  $root_node = $GLOBALS['SQ_SYSTEM']->am->getAsset($root_node_id);
178  if (is_null($root_node)) {
179  trigger_localised_error('HIPO0096', E_USER_NOTICE);
180  return FALSE;
181  }
182  }
183 
184  return parent::prepare();
185 
186  }//end prepare()
187 
188 
198  function processImportAsset(&$step_data, $prefix)
199  {
200  $import_actions = $this->_running_vars['import_actions'];
201 
202 
203  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
204  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_OPEN);
205  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
206 
207  $action = array_shift($import_actions['actions'][0]['action']);
208  // Loop through the Actions from the XML File
209  if(!empty($action)) {
210 
211  // Execute the action
212  if (!execute_import_action($action, $this->_running_vars['import_action_outputs'])) {
213  trigger_error('Action ID "'.$action['action_id'][0].'" could not be executed', E_USER_WARNING);
214  }
215  }
216 
217  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
218  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
219  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
220 
221  $this->_running_vars['import_actions'] = $import_actions;
222 
223  // update progress
224  if (count($import_actions['actions'][0]['action']) == 0) {
225  $step_data['percent_done'] = 100;
226  $step_data['complete'] = TRUE;
227 
228  // fix nest content type, regenerate the bodycopy
229  foreach ($this->_running_vars['nest_content_to_fix'] as $actionid) {
230  if(isset($this->_running_vars['import_action_outputs'][$actionid])) {
231  $nest_content_id = $this->_running_vars['import_action_outputs'][$actionid]['assetid'];
232  $nest_content = $GLOBALS['SQ_SYSTEM']->am->getAsset($nest_content_id);
233  $nest_content->_tmp['edit_fns'] = NULL;
234  $nest_content->linksUpdated();
235  }
236  }
237 
238  // done with everything ? delete the file
239  if ($this->_running_vars['delete_after_import']) {
240  // unlink the import dir
241  $command = "rm -rf ".dirname($this->_running_vars['file_info']);
242  system($command);
243  }
244  }
245  else {
246  $step_data['percent_done'] = (($this->_running_vars['total_count'] - count($import_actions['actions'][0]['action'])) / $this->_running_vars['total_count']) * 100;
247  $step_data['complete'] = FALSE;
248  }
249 
250  return TRUE;
251 
252  }//end processImport()
253 
254 
264  function checkAssetExists($action, $type='asset')
265  {
266  if(isset($action[$type][0]) && preg_match('/^[0-9]+$/', $action[$type][0])){
267  return ($GLOBALS['SQ_SYSTEM']->am->assetExists ($action[$type][0]));
268  }
269  return TRUE;
270  }
271 
272 }//end class
273 
274 ?>