Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
tool_import_assets_from_xml.inc
1 <?php
18 require_once SQ_SYSTEM_ROOT.'/core/assets/system/tool/tool.inc';
19 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
20 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
21 require_once SQ_DATA_PATH.'/private/conf/tools.inc';
22 
38 {
39 
40 
47  function Tool_Import_Assets_From_Xml($assetid=0)
48  {
49  $this->Tool($assetid);
50 
51  }//end constructor
52 
53 
63  public static function paintTool(&$o, $type_code)
64  {
65 
66  $tool_info = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($type_code);
67  $o->openSection($tool_info['name']);
68 
69  $o->openField(translate('tool_import_assets_from_xml_file_upload'));
70  file_upload($type_code.'_file_upload');
71  $o->note(translate('tool_import_assets_from_xml_file_upload_note', sq_web_path('data').'/asset_types/tool_import_assets_from_xml/files/example.xml'));
72  $o->closeField();
73 
74  $o->openField(translate('tool_import_assets_from_xml_create_under'));
75  asset_finder($type_code.'_create_under_assetid', 0, Array('asset' => 'D'));
76  $o->note(translate('tool_import_assets_from_xml_create_under_note'));
77  $o->closeField();
78 
79  $o->closeSection();
80 
81  }//end paintTool()
82 
83 
93  public static function processTool(&$o, $type_code)
94  {
95 
96  // error: xml file not specified
97  $file_info = get_file_upload_info($type_code.'_file_upload');
98  if (empty($file_info)) {
99  trigger_localised_error('IMP0002', E_USER_NOTICE);
100  return FALSE;
101  } else {
102 
103  // make import directory, move xml files there
104  $import_dir = SQ_DATA_PATH.'/private/import_assets'.time();
105 
106  if (!move_file($file_info['tmp_name'], $import_dir.'/'.$file_info['name'])) {
107  trigger_error('Could not move uploaded file from tmp directory to data dir');
108  return FALSE;
109  }
110 
111  // set right permission
112  $om = umask(0000);
113  chmod($import_dir.'/'.$file_info['name'], 0774);
114  umask($om);
115 
116  // untar tarball if required
117  if (substr($file_info['name'], -7) == '.tar.gz' || substr($file_info['name'], -4) == '.tgz') {
118  require_once 'Archive/Tar.php';
119  $tar_ball = new Archive_Tar($import_dir.'/'.$file_info['name']);
120  $tar_ball->extract($import_dir);
121  }
122  // get the main xml export file, it has to be one
123  $temp_filename = array_shift(glob($import_dir.'/*.xml'));
124  }
125 
126  $root_node_id = $_POST[$type_code.'_create_under_assetid']['assetid'];
127  // if value of root node is 0 then we are good. It just means we are not selecting root node
128  // but if we have a value, then make sure it is a proper asset
129  if($root_node_id != 0) {
130  $root_node = $GLOBALS['SQ_SYSTEM']->am->getAsset($root_node_id);
131  if (is_null($root_node)) {
132  trigger_localised_error('IMP0003', E_USER_NOTICE);
133  return FALSE;
134  }
135  }
136 
137  if (empty($temp_filename)) {
138  trigger_error('There is not xml file found');
139  return FALSE;
140  }
141 
142  // set HIPO running_vars
143  $vars['file_info'] = $temp_filename;
144  $vars['create_under_assetid'] = $root_node_id;
145  $vars['delete_after_import'] = TRUE;
146 
147  // run HIPO job
148  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
149  $hh->queueHipo('hipo_job_import_assets_from_xml', $vars, '', SQ_PACKAGES_PATH.'/import_tools/hipo_jobs');
150  $url = $hh->runQueuedJobs();
151 
152 
153  if (!empty($url)) $o->setRedirect($url);
154 
155  return TRUE;
156 
157 
158  }//end processTool()
159 
160 
161 }//end class
162 
163 
164 ?>