Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
tool_structured_file_import.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 require_once SQ_DATA_PATH.'/private/conf/import_tools_manager.inc';
23 
39 {
40 
41 
48  function Tool_Structured_File_Import($assetid=0)
49  {
50  $this->Tool($assetid);
51 
52  }//end constructor
53 
54 
64  public static function paintTool(&$o, $type_code)
65  {
66  $am =& $GLOBALS['SQ_SYSTEM']->am;
67  $itm = $am->getSystemAsset('import_tools_manager');
68 
69  $import_dir = STRUCTURED_FILE_IMPORT_DIR;
70  if (empty($import_dir)) {
71 
72  $o->openSection('Warning');
73  $o->openField(translate('import_tools_not_ready'));
74  echo translate('import_tools_not_ready_msg');
75  $o->closeField();
76  $o->closeSection();
77 
78  } else {
79 
80  // Some variable initialisation
81  $tool_info = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($type_code);
82  $o->openSection($tool_info['name']);
83 
84  $compatible_types = $itm->attr('valid_file_types');
85  $o->openField(translate('import_tools_import_dir'));
86  echo $import_dir;
87  $o->closeField();
88  $o->openField(translate('import_tools_files'));
89  // This will get us a list of all the files in the import directory
90  $files_array = list_files($import_dir, TRUE);
91  // Filter these to include only compatible files.
92  foreach ($files_array as $id => $file_name) {
93  $file_type = get_file_type($file_name);
94  if (!in_array($file_type, $compatible_types)) {
95  unset($files_array[$id]);
96  }
97  }
98  ?>
99  <script type="text/javascript">
100  <?php
101  $tmp = 'var file_list =[';
102  for (reset($files_array); $key = key($files_array); next($files_array)) {
103  $strrpos_files_array = strrpos($files_array[$key], '/') + 1;
104  $stripped_fname = substr($files_array[$key], $strrpos_files_array);
105  $tmp .= '"'.str_replace(Array('.',' '), '_', $stripped_fname).'",';
106  }
107  echo substr($tmp, 0, -1).'];';
108  ?>
109  function uncheck_import_file() {
110  var form = document.getElementById("main_form");
111  for (counter = 0; counter < form.structured_file_import_filename.length; counter++) {
112  if (form.structured_file_import_filename[counter].value == '') {
113  form.structured_file_import_filename[counter].checked = true;
114  } else {
115  form.structured_file_import_filename[counter].checked = false;
116  }
117  }
118  }
119  function can_select() {
120  for (counter = 0; counter < file_list.length; counter++ ) {
121  var delete_chkbox = document.getElementById("structured_file_remove_" + file_list[counter]);
122  if (delete_chkbox.checked == true) {
123  uncheck_import_file();
124  alert("<?php echo translate("import_tools_javascript_warning"); ?>");
125  break;
126  }
127  }
128  }
129  </script>
130  <table class="sq-backend-table" style="width: 440px;">
131  <tr>
132  <th class="sq-backend-table-header" style="width: 240px;"><?php echo translate('import_tools_select_file'); ?></th>
133  <th class="sq-backend-table-header" style="width: 100px;"><?php echo translate('import_tools_select_for_import'); ?></th>
134  <th class="sq-backend-table-header" style="width: 100px; text-align: center;"><?php echo translate('import_tools_select_for_removal'); ?></th>
135  </tr>
136  <tr>
137  <td class="sq-backend-table-cell"><?php echo translate('none'); ?></td>
138  <td class="sq-backend-table-cell" style="text-align: center;"><?php radio_button('structured_file_import_filename', '', TRUE); ?></td>
139  <td class="sq-backend-table-cell" style="text-align: center;"></td>
140  </tr>
141  <?php
142  foreach ($files_array as $file_name) {
143  $strrpos_file_name = strrpos($file_name, '/') + 1;
144  $stripped_fname = substr($file_name, $strrpos_file_name);
145  $replaced_fname = str_replace(Array('.',' '), '_', $stripped_fname);
146  ?>
147  <tr>
148  <td class="sq-backend-table-cell"><?php echo $stripped_fname; ?></td>
149  <td class="sq-backend-table-cell" style="text-align: center;"><?php radio_button('structured_file_import_filename', $file_name, FALSE, 'can_select();'); ?></td>
150  <td class="sq-backend-table-cell" style="text-align: center;"><?php check_box('structured_file_remove_'.$replaced_fname, '1', FALSE, 'uncheck_import_file();'); ?></td>
151  </tr>
152  <?php
153  }
154  ?>
155  </table>
156  <?php
157  $o->note(translate('import_tools_select_file'));
158  $o->closeField();
159 
160  $o->openField(translate('import_tools_asset_to_import'));
161  asset_finder($type_code.'_root_assetid', '');
162  $o->closeField();
163  $o->openField(translate('import_tools_new_site'));
164  ?>
165  <table class="sq-backend-table" style="width: 300px;">
166  <tr>
167  <td>
168  <?php echo translate('import_tools_create_new_site'); ?>
169  </td>
170  <td>
171  <?php
172  check_box($type_code.'_create_new_site', '1');
173  ?>
174  </td>
175  </tr>
176  <tr>
177  <td>
178  <?php echo translate('import_tools_create_new_site_name'); ?>
179  </td>
180  <td>
181  <?php
182  text_box($type_code.'_new_site_name', '', '30', '255');
183  ?>
184  </td>
185  </tr>
186  <tr>
187  <td>
188  <?php echo 'Create Site Map for New Site:'; ?>
189  </td>
190  <td>
191  <?php
192  check_box($type_code.'_create_new_map', '1');
193  ?>
194  </td>
195  </tr>
196  </table>
197  <?php
198  $o->note(translate('import_tools_create_new_site_note'));
199  $o->closeField();
200  $o->openField('Headings to Split On');
201  check_box($type_code.'_no_headings', '1', FALSE, 'document.getElementById(\''.$type_code.'_headings_table\').style.visibility = (this.checked == true) ? \'hidden\' : \'\';');
202  label('Import as a Single Page (Headings will be ignored)', $type_code.'_no_headings');
203  $heading_defaults = $itm->attr('heading_defaults');
204  ?>
205  <table id="<?php echo $type_code.'_headings_table'; ?>" class="sq-backend-table" style="width: 300px;">
206  <tr>
207  <th class="sq-backend-table-header"><?php echo translate('import_tools_heading_style'); ?></th>
208  <th class="sq-backend-table-header" style="width: 120px;"><?php echo translate('import_tools_use_for_structure'); ?></th>
209  </tr>
210 
211  <?php
212  for ($i = 1; $i <= 6; $i++) {
213  $h_string = 'h'.strval($i);
214  ?>
215  <tr>
216  <td class="sq-backend-table-cell"><?php echo strtoupper($h_string); ?></td>
217  <td class="sq-backend-table-cell" style="text-align: center;">
218  <?php
219  check_box('structured_file_import_headings['.$h_string.']', '1', in_array($h_string, $heading_defaults));
220  ?>
221  </td>
222  </tr>
223  <?php
224  }
225  ?>
226  </table>
227  <?php
228  $o->closeField();
229  $o->openField(translate('import_tools_create_css'));
230  check_box($type_code.'_create_css', '1', $itm->attr('create_css_file'));
231  $o->note(translate('import_tools_create_css_note'));
232  $o->closeField();
233  $o->openField(translate('import_tools_leading_text'));
234  check_box($type_code.'_leading_text', '1');
235  $o->note(translate('import_tools_leading_text_note'));
236  $o->closeField();
237  $o->openField(translate('import_tools_leading_page_name'));
238  text_box($type_code.'_leading_name', 'Preface');
239  $o->note(translate('import_tools_leading_page_name_note'));
240  $o->closeField();
241  $o->closeSection();
242 
243  $o->openSection(translate('import_tools_tidying_options'));
244  $o->openField(translate('import_tools_word_tags'));
245  check_box($type_code.'_remove_word', '1');
246  $o->closeField();
247  $o->openField(translate('import_tools_html_tidy'));
248  check_box($type_code.'_run_tidy');
249  $o->note(translate('import_tools_tidy_note'));
250  $o->closeField();
251  $o->openField(translate('import_tools_footnotes'));
252  check_box($type_code.'_footnotes');
253  $o->closeField();
254  $o->closeSection();
255 
256  }//end else
257 
258 
259  }//end paintTool()
260 
261 
271  public static function processTool(&$o, $type_code)
272  {
273  $am =& $GLOBALS['SQ_SYSTEM']->am;
274  $itm = $am->getSystemAsset('import_tools_manager');
275  $vars = Array();
276  $vars['import_file'] = array_get_index($_POST, 'structured_file_import_filename', '');
277  $vars['remove_file'] = array_get_index($_POST, 'structured_file_remove_filename', '');
278  $vars['create_new_site'] = FALSE;
279  if (isset($_POST[$type_code.'_create_new_site'])) {
280  // Create New Site Checked
281  $vars['create_new_site'] = TRUE;
282  $vars['new_site_name'] = $_POST[$type_code.'_new_site_name'];
283  }
284  $vars['root_assetid'] = $_POST[$type_code.'_root_assetid']['assetid'];
285  $vars['use_headings'] = isset($_POST[$type_code.'_no_headings']) ? FALSE : TRUE;
286  $vars['headings'] = array_keys($_POST['structured_file_import_headings']);
287  $vars['import_dir'] = STRUCTURED_FILE_IMPORT_DIR;
288  $vars['create_css'] = isset($_POST[$type_code.'_create_css_file']);
289  $vars['create_site_map'] = isset($_POST[$type_code.'_create_new_map']);
290  $vars['leading_create'] = isset($_POST[$type_code.'_leading_text']);
291  $vars['leading_text'] = $_POST[$type_code.'_leading_name'];
292 
293  $vars['remove_word'] = isset($_POST[$type_code.'_remove_word']);
294  $vars['run_tidy'] = isset($_POST[$type_code.'_run_tidy']);
295  $vars['fix_anchors'] = isset($_POST[$type_code.'_footnotes']);
296 
297  if ($vars['run_tidy']) {
298  $vars['tidy_path'] = SQ_TOOL_HTML_TIDY_PATH;
299  }
300 
301  // Any deletion?
302  $import_dir = STRUCTURED_FILE_IMPORT_DIR;
303  $files_array = list_files($import_dir, TRUE);
304  $remove_list = Array();
305  foreach ($files_array as $id => $file_name) {
306  $strrpos_files_name = strrpos($file_name, '/') + 1;
307  $stripped_fname = substr($file_name, $strrpos_files_name);
308  $replaced_fname = str_replace(Array('.',' '), '_', $stripped_fname);
309  if (isset($_POST['structured_file_remove_'.$replaced_fname])) {
310  $remove_list[] = $stripped_fname;
311  }
312  }
313  if (empty($remove_list)) {
314  // Importing section
315  if (($vars['import_file'] == '') || (empty($vars['headings']) && ($vars['use_headings'])) || ($vars['root_assetid'] == '' && $vars['create_new_site'] == FALSE)) {
316  return FALSE;
317  }
318 
319  // We have all the required data, so queue up our HIPO and go to town
320  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
321  $hh->queueHipo('hipo_job_structured_file_import', $vars, '', SQ_PACKAGES_PATH.'/import_tools/hipo_jobs/');
322  $url = $hh->runQueuedJobs();
323  if (!empty($url)) $o->setRedirect($url);
324  return TRUE;
325 
326  } else {
327  // Delete the imported files
328  foreach ($remove_list as $file_name) {
329  $tmp = explode('.', $file_name);
330  $fname_no_ext = $tmp[0];
331  if (file_exists($import_dir.'/'.$file_name)) {
332  $success = unlink($import_dir.'/'.$file_name);
333  }
334  if ($success && file_exists($import_dir.'/'.$fname_no_ext.'.doc')) {
335  $success = unlink($import_dir.'/'.$fname_no_ext.'.doc');
336  }
337  if ($success && file_exists($import_dir.'/'.$fname_no_ext.'.dot')) {
338  $success = unlink($import_dir.'/'.$fname_no_ext.'.dot');
339  }
340  if ($success && file_exists($import_dir.'/'.$fname_no_ext.'_files')) {
341  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
342  $success = delete_directory($import_dir.'/'.$fname_no_ext.'_files');
343  }
344  if (!$success) {
345  // error report
346  trigger_error('Failed to delete the imported files.');
347  break;
348  }
349  }
350  return FALSE;
351  }
352 
353 
354  }//end processTool()
355 
356 
357 }//end class
358 
359 
360 ?>