1
0

BxBaseUploaderHTML5.php 4.3 KB

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