PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/ExamplePlugin/controllers/example/upload-example.php

https://github.com/TitanKing/todoplugins
PHP | 99 lines | 57 code | 15 blank | 27 comment | 6 complexity | a20fe9fc14ba0d382f51ecebf202b0b7 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * Controller Class: Upload example.
  4. * Like always we start our node with the controller.
  5. * @author Jason Schoeman
  6. * @return string
  7. */
  8. class uploadExample extends PHPDS_controller
  9. {
  10. /**
  11. * Execute Controller
  12. * @author Jason Schoeman
  13. */
  14. public function execute()
  15. {
  16. // Heading.
  17. $this->template->heading(_('Upload Example.'));
  18. $this->template->info(_('Shows how you can upload and list files.'));
  19. // Call filemanager.
  20. $filemanager = $this->factory('fileManager');
  21. // Deleting a file.
  22. if (! empty($this->security->get['df'])) {
  23. $deleted = $filemanager->deleteFiles($this->security->get['df']);
  24. if ($deleted) {
  25. // Note these messages gets logged automatically, you also have $this->template->ok, $this->template->warning, $this->template->notice, $this->template->critical, $this->template->message, $this->template->note.
  26. $this->template->ok(sprintf('Your file (%s) was deleted.', $deleted[$this->security->get['df']]['original_filename']));
  27. }
  28. }
  29. // Set some parameters, have a look at the API for more.
  30. $filemanager->allowedExt = 'default'; // Will load extentions as set in General Settings GUI
  31. $filemanager->alias = 'samplefiles'; // Group by this alias
  32. $filemanager->subId = '1'; // Can be set to an id accommodating a forms database id for instance so one could easily locate it when loading data.
  33. $filemanager->convertPdf = false; // This will convert PDF files to images, smart, I know.
  34. // Some exeption handling example.
  35. try {
  36. // Upload required files.
  37. if ($filename = $filemanager->autoUpload('file1')) {
  38. $this->template->ok(sprintf(_('File field 1 %s was uploaded'), $filename));
  39. }
  40. // Upload required files.
  41. if ($filename2 = $filemanager->autoUpload('file2')) {
  42. $this->template->ok(sprintf(_('File field 2 %s was uploaded'), $filename2));
  43. }
  44. // Upload required files.
  45. if ($filename3 = $filemanager->autoUpload('file3')) {
  46. $this->template->ok(sprintf(_('File field 3 %s was uploaded'), $filename3));
  47. }
  48. } catch (Exception $e) {
  49. // To make messages translatable, simply surround them with __('My message');
  50. $this->template->warning(sprintf(__('Oops, we had a slight problem uploading the file, %s in file %s on line %s'), $e->getMessage(), $e->getFile(), $e->getLine()));
  51. }
  52. // Lets load the files uploaded so far!
  53. // Use print_r to see all the data the filemanager returns :)
  54. $uploaded_files_array = $filemanager->loadFiles(false, 'samplefiles');
  55. $u_arr = array();
  56. $page_delete = $this->navigation->buildURL(false, 'df=');
  57. // Loop and assign files.
  58. if (! empty($uploaded_files_array)) {
  59. // Loop results of uploaded files.
  60. foreach ($uploaded_files_array as $files) {
  61. $u_arr[] = array(
  62. 'download_file' => $files['download_file'],
  63. 'file_id' => $files['file_id'],
  64. 'thumbnail' => $files['thumbnail'],
  65. 'resized' => $files['thumbnail'],
  66. 'extention_img' => $files['extention_img'],
  67. 'format_file_size' => $files['format_file_size'],
  68. 'original_filename' => $files['original_filename'],
  69. 'delete_file' => "<a href=\"{$page_delete}{$files['file_id']}\" {$this->core->confirmLink(sprintf(_('Are you sure you want to DELETE : %s'), $files['original_filename']))} class=\"button\">{$this->template->icon('broom--minus', _('Delete File'))}</a>"
  70. );
  71. }
  72. }
  73. // Call views plugin.
  74. $view = $this->factory('views');
  75. // Assign Variables.
  76. $view->set('self_url', $this->navigation->selfUrl());
  77. $view->set('u_arr', $u_arr);
  78. $view->set('developers_name', $this->configuration['user_display_name']);
  79. $view->set('absolute_url', $this->configuration['absolute_url'] . '/');
  80. // Show it.
  81. $view->show();
  82. }
  83. }
  84. return 'uploadExample';