1
0

BxBaseUploaderHTML5.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * Upload files using AJAX uploader with multiple files selection support (without flash),
  11. * it works in Firefox and WebKit(Safari, Chrome) browsers only, but has fallback for other browsers (IE, Opera).
  12. * @see BxDolUploader
  13. */
  14. class BxBaseUploaderHTML5 extends BxDolUploader
  15. {
  16. protected $_sDivId; ///< div id where upload button will be placed
  17. protected $_sUploaderFormTemplate = 'uploader_form_html5.html';
  18. function __construct ($aObject, $sStorageObject, $sUniqId, $oTemplate)
  19. {
  20. parent::__construct($aObject, $sStorageObject, $sUniqId, $oTemplate);
  21. $this->_sDivId = 'bx-form-input-files-' . $sUniqId . '-div-' . $this->_aObject['object'];
  22. $this->_sButtonTemplate = 'uploader_button_html5.html';
  23. }
  24. /**
  25. * add necessary js, css files and js translations
  26. */
  27. public function addCssJs ()
  28. {
  29. parent::addCssJs ();
  30. $this->_oTemplate->addJs('dropzone/dropzone.min.js');
  31. $this->_oTemplate->addCss(array(
  32. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'dropzone/|dropzone.min.css',
  33. ));
  34. }
  35. /**
  36. * Get uploader button title
  37. */
  38. public function getUploaderButtonTitle($mixed = false)
  39. {
  40. if (is_string($mixed))
  41. return $mixed;
  42. elseif (is_array($mixed) && isset($mixed['HTML5']))
  43. return $mixed['HTML5'];
  44. else
  45. return _t('_sys_uploader_html5_button_name');
  46. }
  47. /**
  48. * Show uploader form.
  49. * @return HTML string
  50. */
  51. public function getUploaderForm($isMultiple = true, $iContentId = false, $isPrivate = true)
  52. {
  53. return $this->_oTemplate->parseHtmlByName($this->_sUploaderFormTemplate, array(
  54. 'form_container_id' => $this->_sFormContainerId,
  55. 'errors_container_id' => $this->_sErrorsContainerId,
  56. 'uploader_instance_name' => $this->getNameJsInstanceUploader(),
  57. 'restrictions_text' => $this->getRestrictionsText(),
  58. 'div_id' => $this->_sDivId,
  59. 'content_id' => $iContentId,
  60. 'storage_private' => $isPrivate,
  61. 'max_file_size' => $this->getMaxUploadFileSize(),
  62. ));
  63. }
  64. /**
  65. * Handle uploads here.
  66. * @param $mixedFiles as usual $_FILES['some_name'] array, but maybe some other params depending on the uploader
  67. * @return nothing, but if some files failed to upload, the actual error message can be determined by calling BxDolUploader::getUploadErrorMessages()
  68. */
  69. public function handleUploads ($iProfileId, $mixedFiles, $isMultiple = true, $iContentId = false, $bPrivate = true)
  70. {
  71. $oStorage = BxDolStorage::getObjectInstance($this->_sStorageObject);
  72. if (!$isMultiple)
  73. $this->deleteGhostsForProfile($iProfileId, $iContentId);
  74. if (bx_get('file')) {
  75. $iId = $oStorage->storeFileFromXhr(bx_get('file'), $bPrivate, $iProfileId, $iContentId);
  76. }
  77. else {
  78. $iId = $oStorage->storeFileFromForm($_FILES['file'], $bPrivate, $iProfileId, $iContentId);
  79. }
  80. if ($iId) {
  81. $aResponse = array ('success' => 1);
  82. } else {
  83. $this->appendUploadErrorMessage(_t('_sys_uploader_err_msg', isset($_FILES['file']['name']) ? $_FILES['file']['name'] : bx_get('file'), $oStorage->getErrorString()));
  84. $aResponse = array ('error' => $this->getUploadErrorMessages());
  85. }
  86. echo htmlspecialchars(json_encode($aResponse), ENT_NOQUOTES);
  87. }
  88. }
  89. /** @} */