BxBaseUploaderHTML5.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. $iResizeWidth = (int)getParam('client_image_resize_width');
  54. $iResizeHeight = (int)getParam('client_image_resize_height');
  55. $sAcceptedFiles = $this->getAcceptedFilesExtensions();
  56. return $this->_oTemplate->parseHtmlByName($this->_sUploaderFormTemplate, array(
  57. 'form_container_id' => $this->_sFormContainerId,
  58. 'errors_container_id' => $this->_sErrorsContainerId,
  59. 'uploader_instance_name' => $this->getNameJsInstanceUploader(),
  60. 'restrictions_text' => $this->getRestrictionsText(),
  61. 'div_id' => $this->_sDivId,
  62. 'content_id' => $iContentId,
  63. 'storage_private' => $isPrivate,
  64. 'max_file_size' => $this->getMaxUploadFileSize(),
  65. 'accepted_files' => null === $sAcceptedFiles ? 'null' : "'" . bx_js_string($sAcceptedFiles) . "'",
  66. 'resize_width' => $iResizeWidth ? $iResizeWidth : 'null',
  67. 'resize_height' => $iResizeHeight ? $iResizeHeight : 'null',
  68. 'resize_method' => $iResizeWidth && $iResizeHeight ? "'contain'" : 'null',
  69. ));
  70. }
  71. /**
  72. * Handle uploads here.
  73. * @param $mixedFiles as usual $_FILES['some_name'] array, but maybe some other params depending on the uploader
  74. * @return nothing, but if some files failed to upload, the actual error message can be determined by calling BxDolUploader::getUploadErrorMessages()
  75. */
  76. public function handleUploads ($iProfileId, $mixedFiles, $isMultiple = true, $iContentId = false, $bPrivate = true)
  77. {
  78. $oStorage = BxDolStorage::getObjectInstance($this->_sStorageObject);
  79. if (!$isMultiple)
  80. $this->deleteGhostsForProfile($iProfileId, $iContentId);
  81. if (bx_get('file')) {
  82. $iId = $oStorage->storeFileFromXhr(bx_get('file'), $bPrivate, $iProfileId, $iContentId);
  83. }
  84. else {
  85. $iId = $oStorage->storeFileFromForm($_FILES['file'], $bPrivate, $iProfileId, $iContentId);
  86. }
  87. if ($iId) {
  88. $aResponse = array ('success' => 1);
  89. } else {
  90. $this->appendUploadErrorMessage(_t('_sys_uploader_err_msg', isset($_FILES['file']['name']) ? $_FILES['file']['name'] : bx_get('file'), $oStorage->getErrorString()));
  91. $aResponse = array ('error' => $this->getUploadErrorMessages());
  92. }
  93. echo htmlspecialchars(json_encode($aResponse), ENT_NOQUOTES);
  94. }
  95. }
  96. /** @} */