Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
tool_bulk_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 
38 {
39 
40 
47  function Tool_Bulk_File_Import($assetid=0)
48  {
49  $this->Tool($assetid);
50 
51  }//end constructor
52 
53 
63  public static function paintTool(&$o, $type_code)
64  {
65  $am =& $GLOBALS['SQ_SYSTEM']->am;
66  $itm = $am->getSystemAsset('import_tools_manager');
67  $import_option = $itm->attr('allowed_import_type');
68  $import_dir = BULK_FILE_IMPORT_DIR;
69  $process_local = ($import_option != 'Server Only');
70  $process_server = ($import_option != 'Local Only');
71 
72  $o->openSection(translate('file_import_tools_global_options'));
73  $o->openField(translate('file_import_tools_allow_unrestricted'));
74  combo_box($type_code.'_allow_unrestricted', Array(1 => 'Yes', 0 => 'No'));
75  echo '<br />'.translate('file_import_tools_allow_unrestricted_note');
76  $o->closeField();
77 
78  $o->openField('Link Type');
79  combo_box($type_code.'_link_type', Array(SQ_LINK_TYPE_1 => 'TYPE_1', SQ_LINK_TYPE_2 => 'TYPE_2'));
80  echo '<br />'.translate('file_import_tools_file_link_type_msg');
81  $o->closeField();
82  $o->closeSection();
83 
84  $o->openSection(translate('file_import_tools_file_dir_selection'));
85  if (empty($import_dir)) {
86  $o->openField(translate('file_import_tools_warning'));
87  echo translate('file_import_tools_not_ready_msg');
88  $o->closeSection();
89  } else {
90 
91  if ($process_server) {
92 
93  $o->openField(translate('file_import_tools_server_import'));
94 
95  $compatible_types = $itm->attr('valid_file_types');
96 
97  // This will get us a list of all the files in the import directory
98  $dirs_array = list_dirs($import_dir, TRUE);
99  ?>
100  <table class="sq-backend-table" style="width: 600px;">
101  <tr>
102  <th class="sq-backend-table-header" style="width: 130px;"><?php echo 'Directory'; ?></th>
103  <th class="sq-backend-table-header" style="width: 80px; text-align: center;"><?php echo 'Select for Import'; ?></th>
104  <th class="sq-backend-table-header" style="width: 390px; text-align: center;"><?php echo 'Import Under'; ?></th>
105  </tr>
106  <?php
107  $files = Tool_Bulk_File_Import::_getFileList($import_dir);
108  if (!empty($files)) {
109  $num_of_files = count($files);
110  ?>
111  <tr>
112  <td class="sq-backend-table-cell" style="border-bottom: 0px;"><?php echo $num_of_files; ?> file<?php echo ($num_of_files > 1) ? 's' : ''; ?>.</td>
113  <td class="sq-backend-table-cell" style="border-bottom: 0px; text-align: center;"><?php check_box('chunk_file_import', '1', FALSE); ?></td>
114  <td class="sq-backend-table-cell" style="border-bottom: 0px; text-align: center;"><?php asset_finder('chunk_file_import_root_assetid', ''); ?></td>
115  </tr>
116  <tr>
117  <td class="sq-backend-table-cell" style="border-top: 0px;"colspan="3"><?php echo translate('file_import_tools_file_chunk', '<b>'.$import_dir.'</b>') ?></td>
118  </tr>
119  <?php
120  }
121  ?>
122  <?php
123  foreach ($dirs_array as $dir_name) {
124  $files = Tool_Bulk_File_Import::_getFileList($dir_name);
125  $num_of_files = count($files);
126  $stripped_dname = substr($dir_name, strrpos($dir_name, '/') + 1);
127 
128  // Local import temp directory, Do not display
129  if (strpos($stripped_dname, '__tmp_file_upload__') !== FALSE) {
130  continue;
131  }
132 
133  ?>
134  <tr>
135  <td class="sq-backend-table-cell"><?php echo $stripped_dname; ?>&nbsp;(<?php echo $num_of_files; ?> file<?php echo ($num_of_files > 1) ? 's' : ''; ?>)</td>
136  <td class="sq-backend-table-cell" style="text-align: center;"><?php check_box('file_import_dirname_'.str_replace(' ', '_', $stripped_dname), $dir_name, FALSE); ?></td>
137  <td class="sq-backend-table-cell" style="text-align: center;"><?php asset_finder(str_replace(' ', '_', $stripped_dname).'_root_assetid', ''); ?></td>
138  </tr>
139  <?php
140  }
141  ?>
142  <tr>
143  <td class="sq-backend-table-cell" style="border-top: 0px;"colspan="3">
144  <?php
145  echo "Sort the files alphanumerically on import? ";
146  check_box('sort_file_import', '1', FALSE);
147  ?>
148  </td>
149  </tr>
150  </table>
151  <?php
152  $o->note(translate('file_import_tools_select_file'));
153  $o->closeField();
154 
155  }//end if Server Import
156 
157  if ($process_local) {
158 
159  $o->openField(translate('file_import_tools_local_import'));
160 
161  if (!is_writable($import_dir)) {
162  $o->note(translate('file_import_tools_local_import_permission_warning', $import_dir));
163  } else {
164  // Max File Size for local file upload
166  ?>
167  <table class="sq-backend-table" style="width: 600px;">
168  <tr>
169  <th class="sq-backend-table-header" colspan="2"><?php
170  echo translate('file_import_tools_local_file_import_root');
171  ?></th>
172  </tr>
173  <tr>
174  <td class="sq-backend-table-header" style="width: 100px;"><?php
175  echo translate('file_import_tools_root_asset');
176  ?></td>
177  <td class="sq-backend-table-header" style="width: 500px;"><?php
178  asset_finder('bulk_file_import_local_upload_root_asset', '');
179  ?></td>
180  </tr>
181  </table>
182 
183  <div style='display: none;`' id='bulk_file_import_local_file_input_container'></div>
184  <div id='bulk_file_import_table_container'></div>
185 
186  <span style="font-size: 11px;">&nbsp;<?php echo translate('core_cannot_upload_file_larger_than', '<b>'.strtoupper(easy_filesize($max_size)).'</b>'); ?></span>
187  <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_size; ?>" />
188 
189  <script type="text/javascript">
190  //<![CDATA[
191 
192  function local_file_import_table()
193  {
194  this.tbody;
195  this.table = document.createElement("table");
196  this.id_count = 1;
197  this.file_names = Array();
198 
199  // Browser Compatibility
200  this.is_gecko = (navigator.product == 'Gecko');
201  this.is_ie = (
202  (navigator.userAgent.toLowerCase().indexOf("msie") != -1)
203  );
204 
205  // Allowed extension for each file type
206  this.image_exts = Array('jpg', 'jpeg', 'gif', 'png');
207  this.doc_exts = Array('doc', 'dot', 'docx');
208  this.pdf_exts = Array('pdf');
209  this.xls_exts = Array('xls', 'xlt', 'xlsx', 'xlsm', 'xltx', 'xltm');
210  this.rtf_exts = Array('rtf');
211  this.txt_exts = Array('txt');
212  this.js_exts = Array('js');
213  this.ppt_exts = Array('ppt', 'pps', 'pot', 'pptx', 'ppsx', 'potx', 'pptm', 'potm', 'ppsm');
214  this.css_exts = Array('css');
215  this.mp3_exts = Array('mp3');
216  this.video_exts = Array('mov', 'avi', 'wmv', 'asf', 'flv', 'mp4', 'm4v', 'mpg', 'mpeg');
217 
218  // Create the initial table for local imiport
219  this.init = function()
220  {
221  var container = document.getElementById('bulk_file_import_table_container');
222 
223  this.table.className = 'sq-backend-table';
224  this.table.style.width = '600px';
225 
226  // Create THead
227  var thead = document.createElement('thead');
228  var header_row = document.createElement('tr');
229  var th = document.createElement('th');
230  th.colSpan = '3';
231  th.className = 'sq-backend-table-header';
232  th.style.width = '100px';
233  th.innerHTML = 'Files to Import';
234  header_row.appendChild(th);
235  thead.appendChild(header_row);
236 
237  // Create TBody
238  var tbody = document.createElement('tbody');
239  var body_row = document.createElement('tr');
240  var row_id = 'bulk_file_upload_new_row_0';
241  body_row.id = row_id;
242  var td_1 = document.createElement('td');
243  var td_2 = document.createElement('td');
244  var td_3 = document.createElement('td');
245 
246  td_1.className = 'sq-backend-table-cell';
247  td_2.className = 'sq-backend-table-cell';
248  td_3.className = 'sq-backend-table-cell';
249 
250  td_1.style.borderTop = '1px solid #1A1A1A';
251  td_2.style.borderTop = '1px solid #1A1A1A';
252  td_3.style.borderTop = '1px solid #1A1A1A';
253 
254  td_1.style.width = '100px';
255  td_2.style.width = '470px';
256  td_3.style.width = '30px';
257 
258  td_1.innerHTML = '<b>Choose File</b>';
259  td_2.innerHTML = '<input type="file" size="50" name="bulk_file_upload_file_chooser_0" id="bulk_file_upload_file_chooser_0" class="sq-form-field" onchange="local_file_table.fileSelected(this, \'0\');" />';
260  td_3.innerHTML = '<img title="Delete News" src="<?php echo sq_web_path('data'); ?>/asset_types/bodycopy/images/icons/delete.png" onclick="local_file_table.removeRow(\'0\', true);">';
261 
262  body_row.appendChild(td_1);
263  body_row.appendChild(td_2);
264  body_row.appendChild(td_3);
265  tbody.appendChild(body_row);
266  this.tbody = tbody;
267 
268  // Create TFoot
269  var tfoot = document.createElement('tfoot');
270  var footer_row = document.createElement('tr');
271  var td = document.createElement('td');
272  td.className = 'sq-backend-table-cell';
273  td.colSpan = '3';
274  td.align = 'right';
275  td.style.borderTop = '1px solid #000000';
276  td.innerHTML = '<div id="insert_file_link" onclick="local_file_table.addRow();"><b>[Click Here to Add]</b></div>';
277  footer_row.appendChild(td);
278  tfoot.appendChild(footer_row);
279 
280  // Append all
281  this.table.appendChild(thead);
282  this.table.appendChild(tbody);
283  this.table.appendChild(tfoot);
284  container.appendChild(this.table);
285 
286  }//end init()
287 
288 
289  this.addRow = function()
290  {
291  children = this.tbody.childNodes;
292  this.tbody.appendChild(this._newRow(this.id_count));
293  this.id_count++;
294 
295  }//end addRow()
296 
297  this._newRow = function(id)
298  {
299  var new_tr = document.createElement('tr');
300  var row_id = 'bulk_file_upload_new_row_' + id;
301  new_tr.id = row_id;
302  var td_1 = document.createElement('td');
303  var td_2 = document.createElement('td');
304  var td_3 = document.createElement('td');
305 
306  td_1.className = 'sq-backend-table-cell';
307  td_2.className = 'sq-backend-table-cell';
308  td_3.className = 'sq-backend-table-cell';
309  td_1.style.borderTop = '1px solid #19212A';
310  td_2.style.borderTop = '1px solid #19212A';
311  td_3.style.borderTop = '1px solid #19212A';
312  td_1.innerHTML = '<b>Choose File</b>';
313  td_2.innerHTML = '<input type="file" size="50" name="bulk_file_upload_file_chooser_' + id + '" id="bulk_file_upload_file_chooser_' + id + '" class="sq-form-field" onchange="local_file_table.fileSelected(this, \'' + id + '\');" />';
314  td_3.innerHTML = '<img title="Delete News" src="<?php echo sq_web_path('data'); ?>/asset_types/bodycopy/images/icons/delete.png" onclick="local_file_table.removeRow(\'' + id + '\', true);">';
315 
316  new_tr.appendChild(td_1);
317  new_tr.appendChild(td_2);
318  new_tr.appendChild(td_3);
319 
320  return new_tr;
321 
322  }//end _newRow()
323 
324  this.removeRow = function(id, removeFromList)
325  {
326  elt = document.getElementById('bulk_file_upload_file_chooser_' + id);
327  if (elt.tagName.toLowerCase() == 'span') {
328  file_name = document.getElementById('bulk_file_upload_file_chooser_' + id + '_').value;
329  } else if (elt.tagName == 'input') {
330  file_name = '';
331  }
332 
333  children = this.tbody.childNodes;
334  for (var i = 0; i < children.length; i++) {
335  if (children[i].id == 'bulk_file_upload_new_row_' + id) {
336  type = this._currentFileType(id);
337  if (type == 'image') {
338  this.tbody.removeChild(children[i + 4]);
339  this.tbody.removeChild(children[i + 3]);
340  this.tbody.removeChild(children[i + 2]);
341  this.tbody.removeChild(children[i + 1]);
342  } else {
343  if (children[i + 1] != null) {
344  if (children[i + 1].id == 'bulk_file_upload_new_row_file_type_' + id) {
345  this.tbody.removeChild(children[i + 2]);
346  this.tbody.removeChild(children[i + 1]);
347  }
348  }
349  }
350  this.tbody.removeChild(children[i]);
351  break;
352  }
353  }
354 
355  // Remove the file name from the list
356  if (removeFromList) {
357  new_file_names = Array();
358  for (i = 0; i < this.file_names.length; i++) {
359  if (file_name != this.file_names[i]) {
360  new_file_names.push(this.file_names[i]);
361  }
362  }
363  this.file_names = new_file_names;
364  }
365 
366  }//end removeRow()
367 
368  // Set the type of the file as one of the specialised types, or file if none match
369  this.fileSelected = function(elt, id)
370  {
371  // Replace input tag with span so that people can double select the file
372  span = document.createElement('span');
373  span.innerHTML = elt.value;
374  span.id = elt.id;
375  p_node = elt.parentNode;
376  p_node.insertBefore(span, elt);
377  elt.style.display = 'none';
378  elt.id = elt.id + '_';
379 
380  // Duplication Checks.
381  file_name = elt.value;
382  for (i = 0; i < this.file_names.length; i++) {
383  if (file_name == this.file_names[i]) {
384  alert('The file, ' + file_name + ' already has been added.');
385  this.removeRow(id, false);
386  return FALSE;
387  }
388  }
389  this.file_names.push(file_name);
390 
391  row_id = 'bulk_file_upload_new_row_' + id;
392  children = this.tbody.childNodes;
393  for (var i = 0; i < children.length; i++) {
394  if (children[i].id == row_id) {
395  extension = this._getFileType(file_name);
396  if (i == (children.length - 1)) {
397  // Last element, appendChild
398  this.tbody.appendChild(this._fileTypeRow(id, extension));
399  this.tbody.appendChild(this._newImageRow(id, 'Title'));
400  if (this._currentFileType(id) == 'image') {
401  this.tbody.appendChild(this._newImageRow(id, 'Alt'));
402  this.tbody.appendChild(this._newImageRow(id, 'Caption'));
403  }
404  } else {
405  // Somewhere in the middle, insertBefore
406  this.tbody.insertBefore(this._fileTypeRow(id, extension), children[i + 1]);
407  this.tbody.insertBefore(this._newImageRow(id, 'Title'), children[i + 2]);
408  if (this._currentFileType(id) == 'image') {
409  this.tbody.insertBefore(this._newImageRow(id, 'Alt'), children[i + 3]);
410  this.tbody.insertBefore(this._newImageRow(id, 'Caption'), children[i + 4]);
411  }
412  }
413  break;
414  }
415  }
416 
417  }//end fileSelected()
418 
419  this._fileTypeRow = function(id, ext)
420  {
421  new_tr = document.createElement('tr');
422  new_tr.id = 'bulk_file_upload_new_row_file_type_' + id;
423  td_1 = document.createElement('td');
424  td_2 = document.createElement('td');
425 
426 
427  td_1.className = 'sq-backend-table-cell';
428  td_2.className = 'sq-backend-table-cell';
429  td_1.innerHTML = 'File Type';
430 
431  // Detect the initial file type from the file extension
432  if (this._isFileTypeIn(this.image_exts, ext)) {
433  td_2.innerHTML = 'Image<input type="hidden" value="1" name="bulk_file_upload_row_' + id + '_file_type_image" id="bulk_file_upload_row_' + id + '_file_type_image" >';
434  } else if (this._isFileTypeIn(this.doc_exts, ext)) {
435  td_2.innerHTML = 'MS Word<input type="hidden" value="1" name="bulk_file_upload_row_' + id + '_file_type_word" id="bulk_file_upload_row_' + id + '_file_type_word" >';
436  } else if (this._isFileTypeIn(this.pdf_exts, ext)) {
437  td_2.innerHTML = 'PDF<input type="hidden" value="1" name="bulk_file_upload_row_' + id + '_file_type_pdf" id="bulk_file_upload_row_' + id + '_file_type_pdf" >';
438  } else if (this._isFileTypeIn(this.ppt_exts, ext)) {
439  td_2.innerHTML = 'MS Powerpoint<input type="hidden" value="1" name="bulk_file_upload_row_' + id + '_file_type_powerpoint" id="bulk_file_upload_row_' + id + '_file_type_powerpoint" >';
440  } else if (this._isFileTypeIn(this.xls_exts, ext)) {
441  td_2.innerHTML = 'MS Excel<input type="hidden" value="1" name="bulk_file_upload_row_' + id + '_file_type_excel" id="bulk_file_upload_row_' + id + '_file_type_excel" >';
442  } else if (this._isFileTypeIn(this.rtf_exts, ext)) {
443  td_2.innerHTML = 'RTF<input type="hidden" value="1" name="bulk_file_upload_row_' + id + '_file_type_rtf" id="bulk_file_upload_row_' + id + '_file_type_rtf" >';
444  } else if (this._isFileTypeIn(this.txt_exts, ext)) {
445  td_2.innerHTML = 'Text<input type="hidden" value="1" name="bulk_file_upload_row_' + id + '_file_type_text" id="bulk_file_upload_row_' + id + '_file_type_textf" >';
446  } else if (this._isFileTypeIn(this.css_exts, ext)) {
447  td_2.innerHTML = 'CSS<input type="hidden" value="1" name="bulk_file_upload_row_' + id + '_file_type_css" id="bulk_file_upload_row_' + id + '_file_type_css" >';
448  } else if (this._isFileTypeIn(this.js_exts, ext)) {
449  td_2.innerHTML = 'JS<input type="hidden" value="1" name="bulk_file_upload_row_' + id + '_file_type_js" id="bulk_file_upload_row_' + id + '_file_type_js" >';
450  } else if (this._isFileTypeIn(this.mp3_exts, ext)) {
451  td_2.innerHTML = 'MP3<input type="hidden" value="1" name="bulk_file_upload_row_' + id + '_file_type_mp3" id="bulk_file_upload_row_' + id + '_file_type_mp3" >';
452  } else if (this._isFileTypeIn(this.video_exts, ext)) {
453  td_2.innerHTML = 'Video<input type="hidden" value="1" name="bulk_file_upload_row_' + id + '_file_type_video" id="bulk_file_upload_row_' + id + '_file_type_video" >';
454  } else {
455  td_2.innerHTML = 'Others<input type="hidden" value="1" name="bulk_file_upload_row_' + id + '_file_type_others" id="bulk_file_upload_row_' + id + '_file_type_others" >';
456  }
457 
458  td_2.colSpan = '2';
459  new_tr.appendChild(td_1);
460  new_tr.appendChild(td_2);
461 
462  return new_tr;
463 
464  }//end _fileTypeRow()
465 
466  this._isFileTypeIn = function(list, findme)
467  {
468  for (var i = 0; i < list.length; i++) {
469  if (findme.toLowerCase() == list[i]) {
470  return true;
471  }
472  }
473  return false;
474 
475  }//end _isFileTypeIn()
476 
477  this._getFileType = function(name)
478  {
479  if (name.indexOf('.') == -1) {
480  return '';
481  }
482  var arr = name.split('.');
483  extension = arr[arr.length - 1];
484  return extension.toLowerCase();
485 
486  }//end _isImage()
487 
488  // Return the currently chosen file type name in String
489  // (one of the specialised types, or 'others' for other file types)
490  // It also returns 'none' if the file has been chosen
491  this._currentFileType = function(id)
492  {
493  types = Array('image', 'pdf', 'word', 'powerpoint', 'excel', 'rtf', 'text', 'js' , 'css', 'mp3', 'video', 'others');
494  for (i = 0; i < types.length; i++) {
495  radio_button = document.getElementById('bulk_file_upload_row_' + id + '_file_type_' + types[i]);
496  if (radio_button != null) {
497  if (radio_button.value == "1") {
498  return types[i];
499  }
500  }
501  }
502 
503  }//end _currentFileType()
504 
505  this._newImageRow = function(id, type)
506  {
507  var new_tr = document.createElement('tr');
508  new_tr.id = 'bulk_file_upload_new_row_' + type.toLowerCase() + '_' + id;
509  var td_1 = document.createElement('td');
510  var td_2 = document.createElement('td');
511 
512  td_1.className = 'sq-backend-table-cell';
513  td_2.className = 'sq-backend-table-cell';
514 
515  td_1.innerHTML = type;
516  if (type == 'Caption') {
517  td_2.innerHTML = '<textarea cols="60" rows="2" name="bulk_file_upload_new_row_' + type.toLowerCase() + '_msg_' + id + '" size="30" maxlength="255"></textarea>';
518  } else {
519  td_2.innerHTML = '<input type="text" name="bulk_file_upload_new_row_' + type.toLowerCase() + '_msg_' + id + '" size="30" maxlength="255" />';
520  }
521  td_2.colSpan = '2';
522 
523  new_tr.appendChild(td_1);
524  new_tr.appendChild(td_2);
525 
526  return new_tr;
527 
528  }//end _newCaptionRow()
529 
530  }//end class local_file_import_table()
531 
532  var local_file_table = new local_file_import_table();
533  local_file_table.init();
534  //]]>
535  </script>
536  <?php
537  }//end else
538 
539  $o->closeField();
540 
541  }//end if Local Upload
542 
543  }//end else
544 
545  $o->closeSection();
546 
547  // Divider
548  $o->openSection(' ');
549  $o->closeSection();
550 
551  }//end paintTool()
552 
553 
563  public static function processTool(&$o, $type_code)
564  {
565  $am =& $GLOBALS['SQ_SYSTEM']->am;
566  $vars = Array();
567  $itm = $am->getSystemAsset('import_tools_manager');
568  $import_dir = BULK_FILE_IMPORT_DIR;
569  $import_option = $itm->attr('allowed_import_type');
570  $process_local = ($import_option != 'Server Only');
571  $process_server = ($import_option != 'Local Only');
572 
573  // Process Server Upload
574  if ($process_server) {
575  $sorting = array_get_index($_POST, 'sort_file_import', FALSE);
576  $dirs_array = list_dirs($import_dir, TRUE);
577 
578  foreach ($dirs_array as $dir_name) {
579  $files = Tool_Bulk_File_Import::_getFileList($dir_name);
580  $num_of_files = count($files);
581 
582  $stripped_dname = substr($dir_name, strrpos($dir_name, '/') + 1);
583  $replaced_dname = str_replace(' ', '_', $stripped_dname);
584  $tmp = array_get_index($_POST, 'file_import_dirname_'.$replaced_dname, '');
585  if (!empty($tmp)) {
586 
587  $tmp = array_get_index($_POST, $replaced_dname.'_root_assetid', '');
588  if (!empty($tmp['assetid'])) {
589  if (!isset($vars['total_count'])) {
590  $vars['total_count'] = $num_of_files;
591  } else {
592  $vars['total_count'] = $vars['total_count'] + $num_of_files;
593  }
594  $vars['server_file_import'][$dir_name]['file_list'] = $files;
595  $vars['server_file_import'][$dir_name]['num_of_files'] = $num_of_files;
596  $vars['server_file_import'][$dir_name]['root_id'] = $tmp['assetid'];
597  }
598  }
599  }//end foreach
600 
601  $tmp = array_get_index($_POST, 'chunk_file_import', '');
602 
603  if (!empty($tmp)) {
604 
605  $tmp = array_get_index($_POST, 'chunk_file_import_root_assetid', '');
606  if (!empty($tmp['assetid'])) {
607  $files = Tool_Bulk_File_Import::_getFileList($import_dir);
608  if (!isset($vars['total_count'])) {
609  $vars['total_count'] = count($files);
610  } else {
611  $vars['total_count'] = $vars['total_count'] + count($files);
612  }
613  $vars['server_file_import'][$import_dir]['file_list'] = $files;
614  $vars['server_file_import'][$import_dir]['num_of_files'] = count($files);
615  $vars['server_file_import'][$import_dir]['root_id'] = $tmp['assetid'];
616  }
617  }
618 
619  if ($sorting && isset($vars['server_file_import'])) {
620  foreach ($vars['server_file_import'] as $import_dir_name => $import_file_info) {
621  if (isset($vars['server_file_import'][$import_dir_name]['file_list']))
622  sort($vars['server_file_import'][$import_dir_name]['file_list']);
623  }
624  }
625  }//end if
626 
627  // Process Local Upload
628  if ($process_local) {
629 
630  if (isset($_POST['bulk_file_import_local_upload_root_asset'])) {
631  $local_import_rootid = $_POST['bulk_file_import_local_upload_root_asset']['assetid'];
632  } else {
633  if (!empty($_FILES)) {
634  // the user didn't pick the root asset, show some warning.
635  trigger_error(translate('file_import_tools_local_file_no_root_asset'), E_USER_WARNING);
636  }
637  }
638 
639  if (!empty($local_import_rootid) && !empty($_FILES)) {
640 
641  $error = FALSE;
642  $file_types = Array('image', 'pdf', 'word','powerpoint', 'excel', 'rtf', 'text', 'js' , 'css', 'mp3', 'video', 'others');
643  $uploaded_files = Array();
644  $tmp_dir_name = $type_code.'__tmp_file_upload__';
645 
646  foreach ($_FILES as $id => $data) {
647  $file_info = get_file_upload_info($id);
648 
649  if (!empty($file_info)) {
650 
651  // File Upload
652  if (!file_exists($import_dir.'/'.$tmp_dir_name)) {
653  if (!mkdir($import_dir.'/'.$tmp_dir_name)) {
654  trigger_error(translate('file_import_tools_cannot_create_temp_dir'), E_USER_WARNING);
655  return FALSE;
656  }
657  }
658 
659  $result = commit_file_upload($id, $import_dir.'/'.$tmp_dir_name, TRUE);
660  if ($result === FALSE) {
661  trigger_error(translate('file_import_tools_local_file_upload_error', $file_info['name']), E_USER_WARNING);
662  $error = TRUE;
663  } else if ($result === '') {
664  trigger_error(translate('file_import_tools_local_file_cannot_upload', $file_info['name']), E_USER_WARNING);
665  $error = TRUE;
666  } else if ($result == $file_info['name']) {
667 
668  $text_length = strlen('bulk_file_upload_file_chooser_');
669  $file_id = substr($id, $text_length);
670 
671  // get file type
672  foreach ($file_types as $file_type) {
673  $value = array_get_index($_POST, 'bulk_file_upload_row_'.$file_id.'_file_type_'.$file_type, '');
674  if (!empty($value)) {
675  $file_info['file_type'] = $file_type;
676  break;
677  }
678  }
679 
680  // if it's image, get title, alt and caption
681  if ($file_info['file_type'] == 'image') {
682  foreach (Array('title', 'alt', 'caption') as $name) {
683  $file_info[$name] = array_get_index($_POST, 'bulk_file_upload_new_row_'.$name.'_msg_'.$file_id, '');
684  }
685  } else {
686  $file_info['title'] = array_get_index($_POST, 'bulk_file_upload_new_row_title_msg_'.$file_id, '');
687  }
688 
689  $uploaded_files[] = $file_info;
690  }
691 
692  // We may want to delete the uploaded files here, if it is required.
693  if ($error) return FALSE;
694 
695  }//end if
696 
697  }//end foreach $_FILES
698 
699  if (!empty($uploaded_files)) {
700  $vars['local_file_import']['rootid'] = $local_import_rootid;
701  $vars['local_file_import']['location'] = $import_dir.'/'.$tmp_dir_name;
702  $vars['local_file_import']['files'] = $uploaded_files;
703  }
704 
705  }//end if !empty($_FILES)
706 
707 
708  }//end if Process Local
709 
710  if (!empty($vars['server_file_import']) || !empty($vars['local_file_import'])) {
711 
712  $vars['allow_unrestricted'] = array_get_index($_POST, $type_code.'_allow_unrestricted', TRUE);
713  $vars['link_type'] = array_get_index($_POST, $type_code.'_link_type', SQ_LINK_TYPE_1);
714  // We have all the required data, so queue up our HIPO and go to town
715  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
716  $result = $hh->queueHipo('hipo_job_bulk_file_import', $vars, '', SQ_PACKAGES_PATH.'/import_tools/hipo_jobs/');
717  $url = $hh->runQueuedJobs();
718  if (!empty($url)) $o->setRedirect($url);
719  return TRUE;
720  }
721 
722  return FALSE;
723 
724  }//end processTool()
725 
726 
735  protected static function _getFileList($dir)
736  {
737  $files = list_files($dir);
738  foreach ($files as $key => $file_name) {
739  if (substr($file_name, 0, 1) == '.') {
740  unset($files[$key]);
741  }
742  }
743 
744  return $files;
745 
746  }//end _getFileList()
747 
748 
755  protected static function _getMaxFileSize()
756  {
757  $user_prefs_max_file_size = $GLOBALS['SQ_SYSTEM']->getUserPrefs('file', 'SQ_FILE_MAX_SIZE');
758  $max_size = strtolower($user_prefs_max_file_size);
759  if ($max_size) {
760  if (substr($max_size, -1) == 'k') {
761  $max_size = $max_size * 1024;
762  } else if (substr($max_size, -1) == 'm') {
763  $max_size = $max_size * 1024 * 1024;
764  }
765  } else {
766  // work out the max file size that PHP is allowing
767  $ini_size = strtolower(ini_get('upload_max_filesize'));
768  if (substr($ini_size, -1) == 'k') {
769  $ini_size = $ini_size * 1024;
770  } else if (substr($ini_size, -1) == 'm') {
771  $ini_size = $ini_size * 1024 * 1024;
772  }
773 
774  // work out the max post size that PHP is allowing
775  $post_size = strtolower(ini_get('post_max_size'));
776  if (substr($post_size, -1) == 'k') {
777  $post_size = $post_size * 1024;
778  } else if (substr($post_size, -1) == 'm') {
779  $post_size = $post_size * 1024 * 1024;
780  }
781 
782  $mem_limit_size = strtolower(ini_get('memory_limit'));
783  if (substr($mem_limit_size, -1) == 'k') {
784  $mem_limit_size = $mem_limit_size * 1024;
785  } else if (substr($mem_limit_size, -1) == 'm') {
786  $mem_limit_size = $mem_limit_size * 1024 * 1024;
787  }
788  $max_size = min($ini_size, $post_size, $mem_limit_size);
789  }
790  return $max_size;
791 
792  }//end _getMaxFileSize()
793 
794 
795 }//end class
796 
797 
798 ?>