BxBaseUploaderHTML5.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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->_sFocusDivId = 'bx-form-input-files-' . $sUniqId . '-focus-' . $this->_aObject['object'];
  23. $this->_sButtonTemplate = 'uploader_button_html5.html';
  24. }
  25. /**
  26. * add necessary js, css files and js translations
  27. */
  28. public function addCssJs ($bDynamic = false)
  29. {
  30. $s = parent::addCssJs ($bDynamic);
  31. $s .= $this->_oTemplate->addJs('dropzone/dropzone.min.js', $bDynamic);
  32. $s .= $this->_oTemplate->addCss(array(
  33. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'dropzone/|dropzone.min.css',
  34. ), $bDynamic);
  35. return $bDynamic ? $s : '';
  36. }
  37. /**
  38. * Get uploader button title
  39. */
  40. public function getUploaderButtonTitle($mixed = false)
  41. {
  42. if (is_string($mixed))
  43. return $mixed;
  44. elseif (is_array($mixed) && isset($mixed['HTML5']))
  45. return $mixed['HTML5'];
  46. else
  47. return _t('_sys_uploader_html5_button_name');
  48. }
  49. /**
  50. * Show uploader form.
  51. * @return HTML string
  52. */
  53. public function getUploaderForm($isMultiple = true, $iContentId = false, $isPrivate = true)
  54. {
  55. $iResizeWidth = (int)getParam('client_image_resize_width');
  56. $iResizeHeight = (int)getParam('client_image_resize_height');
  57. $sAcceptedFiles = $this->getAcceptedFilesExtensions();
  58. return $this->_oTemplate->parseHtmlByName($this->_sUploaderFormTemplate, array(
  59. 'form_container_id' => $this->_sFormContainerId,
  60. 'errors_container_id' => $this->_sErrorsContainerId,
  61. 'uploader_instance_name' => $this->getNameJsInstanceUploader(),
  62. 'restrictions_text' => $this->getRestrictionsText(),
  63. 'div_id' => $this->_sDivId,
  64. 'focus_div_id' => $this->_sFocusDivId,
  65. 'content_id' => $iContentId,
  66. 'storage_private' => $isPrivate,
  67. 'max_file_size' => $this->getMaxUploadFileSize(),
  68. 'accepted_files' => null === $sAcceptedFiles ? 'null' : "'" . bx_js_string($sAcceptedFiles) . "'",
  69. 'resize_width' => $iResizeWidth ? $iResizeWidth : 'null',
  70. 'resize_height' => $iResizeHeight ? $iResizeHeight : 'null',
  71. 'resize_method' => $iResizeWidth && $iResizeHeight ? "'contain'" : 'null',
  72. ));
  73. }
  74. /**
  75. * Handle uploads here.
  76. * @param $mixedFiles as usual $_FILES['some_name'] array, but maybe some other params depending on the uploader
  77. * @return nothing, but if some files failed to upload, the actual error message can be determined by calling BxDolUploader::getUploadErrorMessages()
  78. */
  79. public function handleUploads ($iProfileId, $mixedFiles, $isMultiple = true, $iContentId = false, $bPrivate = true)
  80. {
  81. $oStorage = BxDolStorage::getObjectInstance($this->_sStorageObject);
  82. if (!$isMultiple)
  83. $this->deleteGhostsForProfile($iProfileId, $iContentId);
  84. if (bx_get('file')) {
  85. $iId = $oStorage->storeFileFromXhr(bx_get('file'), $bPrivate, $iProfileId, $iContentId);
  86. }
  87. else {
  88. $iId = $oStorage->storeFileFromForm($_FILES['file'], $bPrivate, $iProfileId, $iContentId);
  89. }
  90. if ($iId) {
  91. $aResponse = array ('success' => 1);
  92. } else {
  93. $this->appendUploadErrorMessage(_t('_sys_uploader_err_msg', isset($_FILES['file']['name']) ? $_FILES['file']['name'] : bx_get('file'), $oStorage->getErrorString()));
  94. $aResponse = array ('error' => $this->getUploadErrorMessages());
  95. }
  96. echo htmlspecialchars(json_encode($aResponse), ENT_NOQUOTES);
  97. }
  98. }
  99. /** @} */