1
0

BxBaseUploaderHTML5.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 $_sFocusDivId;
  18. protected $_sProgressDivId;
  19. public function __construct ($aObject, $sStorageObject, $sUniqId, $oTemplate)
  20. {
  21. parent::__construct($aObject, $sStorageObject, $sUniqId, $oTemplate);
  22. $this->_sDivId = 'bx-form-input-files-' . $sUniqId . '-div-' . $this->_aObject['object'];
  23. $this->_sFocusDivId = 'bx-form-input-files-' . $sUniqId . '-focus-' . $this->_aObject['object'];
  24. $this->_sProgressDivId = 'bx-form-input-files-' . $sUniqId . '-progress';
  25. $this->_sButtonTemplate = 'uploader_button_html5.html';
  26. $this->_sJsTemplate = 'uploader_button_html5_js.html';
  27. $this->_sUploaderFormTemplate = 'uploader_form_html5.html';
  28. $aUploaderLangs = array('ar-ar' => 1, 'cs-cz' => 1, 'da-dk' => 1, 'de-de' => 1, 'el-el' => 1, 'en-en' => 1, 'es-es' => 1, 'fa-ir' => 1, 'fi-fi' => 1, 'fr-fr' => 1, 'he-he' => 1, 'hr-hr' => 1, 'hu-hu' => 1, 'id-id' => 1, 'it-it' => 1, 'ja-ja' => 1, 'lt-lt' => 1, 'nl-nl' => 1, 'no-nb' => 1, 'pl-pl' => 1, 'pt-br' => 1, 'ro-ro' => 1, 'ru-ru' => 1, 'sk-sk' => 1, 'sv-se' => 1, 'tr-tr' => 1, 'uk-ua' => 1, 'vi-vi' => 1, 'zh-cn' => 1, 'zh-tw' => 1);
  29. $sUploaderLang = BxDolLanguages::getInstance()->getCurrentLanguage() . '-' . BxDolLanguages::getInstance()->getLangFlag();
  30. if (!isset($aUploaderLangs[$sUploaderLang]))
  31. $sUploaderLang = 'en-en';
  32. $this->addJs([
  33. 'filepond/locale/' . $sUploaderLang . '.js',
  34. 'filepond/filepond.min.js',
  35. 'filepond/filepond-plugin-image-preview.min.js',
  36. 'filepond/filepond-plugin-image-transform.min.js',
  37. 'filepond/filepond-plugin-image-crop.min.js',
  38. 'filepond/filepond-plugin-image-resize.min.js',
  39. 'filepond/filepond-plugin-file-validate-size.min.js',
  40. 'filepond/filepond-plugin-file-validate-type.js',
  41. ]);
  42. $this->addCss([
  43. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'filepond/|filepond.min.css',
  44. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'filepond/|filepond-plugin-image-preview.min.css',
  45. ]);
  46. }
  47. /**
  48. * Get uploader button title
  49. */
  50. public function getUploaderButtonTitle($mixed = false)
  51. {
  52. if (is_string($mixed))
  53. return $mixed;
  54. elseif (is_array($mixed) && isset($mixed['HTML5']))
  55. return $mixed['HTML5'];
  56. else
  57. return _t('_sys_uploader_html5_button_name');
  58. }
  59. /**
  60. * Show uploader button.
  61. * @return HTML string
  62. */
  63. public function getUploaderButton($aParams = array())
  64. {
  65. $iResizeWidth = (int)getParam('client_image_resize_width');
  66. $iResizeHeight = (int)getParam('client_image_resize_height');
  67. $sAcceptedFiles = $this->getAcceptedFilesExtensions();
  68. $iContentId = $aParams['content_id'];
  69. $isPrivate = $aParams['storage_private'];
  70. $aParams = array_merge(
  71. $aParams,
  72. [
  73. 'form_container_id' => $this->_sFormContainerId,
  74. 'errors_container_id' => $this->_sErrorsContainerId,
  75. 'uploader_instance_name' => $this->getNameJsInstanceUploader(),
  76. 'restrictions_text' => $this->getRestrictionsText(),
  77. 'div_id' => $this->_sDivId,
  78. 'focus_div_id' => $this->_sFocusDivId,
  79. 'progress_div_id' => $this->_sProgressDivId,
  80. 'content_id' => $iContentId,
  81. 'storage_private' => $isPrivate,
  82. 'max_file_size' => $this->getMaxUploadFileSize(),
  83. 'accepted_files' => null === $sAcceptedFiles ? 'null' : "'" . bx_js_string($sAcceptedFiles) . "'",
  84. 'resize_width' => $iResizeWidth ? $iResizeWidth : 'null',
  85. 'resize_height' => $iResizeHeight ? $iResizeHeight : 'null',
  86. 'resize_method' => $iResizeWidth && $iResizeHeight ? "'contain'" : 'null',
  87. ]
  88. );
  89. return $this->_oTemplate->parseHtmlByName(isset($aParams['button_template']) ? $aParams['button_template'] : $this->_sButtonTemplate, $aParams);
  90. }
  91. public function getUploaderJsParams(){
  92. $iResizeWidth = (int)getParam('client_image_resize_width');
  93. $iResizeHeight = (int)getParam('client_image_resize_height');
  94. $sAcceptedFiles = $this->getAcceptedFilesExtensions();
  95. return [
  96. 'max_file_size' => $this->getMaxUploadFileSize(),
  97. 'accepted_files' => null === $sAcceptedFiles ? 'null' : "'" . bx_js_string($sAcceptedFiles) . "'",
  98. 'resize_width' => $iResizeWidth ? $iResizeWidth : 'null',
  99. 'resize_height' => $iResizeHeight ? $iResizeHeight : 'null',
  100. 'resize_method' => $iResizeWidth && $iResizeHeight ? "'contain'" : 'null',
  101. ];
  102. }
  103. /**
  104. * Show uploader form.
  105. * @return HTML string
  106. */
  107. public function getUploaderForm($isMultiple = true, $iContentId = false, $isPrivate = true)
  108. {
  109. $iResizeWidth = (int)getParam('client_image_resize_width');
  110. $iResizeHeight = (int)getParam('client_image_resize_height');
  111. $sAcceptedFiles = $this->getAcceptedFilesExtensions();
  112. return $this->_oTemplate->parseHtmlByName($this->_sUploaderFormTemplate, array(
  113. 'form_container_id' => $this->_sFormContainerId,
  114. 'errors_container_id' => $this->_sErrorsContainerId,
  115. 'uploader_instance_name' => $this->getNameJsInstanceUploader(),
  116. 'restrictions_text' => $this->getRestrictionsText(),
  117. 'div_id' => $this->_sDivId,
  118. 'focus_div_id' => $this->_sFocusDivId,
  119. 'content_id' => $iContentId,
  120. 'storage_private' => $isPrivate,
  121. 'max_file_size' => $this->getMaxUploadFileSize(),
  122. 'accepted_files' => null === $sAcceptedFiles ? 'null' : "'" . bx_js_string($sAcceptedFiles) . "'",
  123. 'resize_width' => $iResizeWidth ? $iResizeWidth : 'null',
  124. 'resize_height' => $iResizeHeight ? $iResizeHeight : 'null',
  125. 'resize_method' => $iResizeWidth && $iResizeHeight ? "'contain'" : 'null',
  126. ));
  127. }
  128. /**
  129. * Handle uploads here.
  130. * @param $mixedFiles as usual $_FILES['some_name'] array, but maybe some other params depending on the uploader
  131. * @return nothing, but if some files failed to upload, the actual error message can be determined by calling BxDolUploader::getUploadErrorMessages()
  132. */
  133. public function handleUploads ($iProfileId, $mixedFiles, $isMultiple = true, $iContentId = false, $bPrivate = true)
  134. {
  135. $oStorage = BxDolStorage::getObjectInstance($this->_sStorageObject);
  136. if (!$isMultiple)
  137. $this->deleteGhostsForProfile($iProfileId, $iContentId);
  138. if (bx_get('file')) {
  139. $iId = $oStorage->storeFileFromXhr(bx_get('file'), $bPrivate, $iProfileId, $iContentId);
  140. }
  141. else {
  142. $iId = $oStorage->storeFileFromForm($_FILES['file'], $bPrivate, $iProfileId, $iContentId);
  143. }
  144. if ($iId) {
  145. $aResponse = array ('success' => 1, 'id' => $iId);
  146. } else {
  147. $this->appendUploadErrorMessage(_t('_sys_uploader_err_msg', isset($_FILES['file']['name']) ? $_FILES['file']['name'] : bx_get('file'), $oStorage->getErrorString()));
  148. $aResponse = array ('error' => $this->getUploadErrorMessages());
  149. }
  150. if (bx_is_api())
  151. return $aResponse;
  152. else
  153. echo htmlspecialchars(json_encode($aResponse), ENT_NOQUOTES);
  154. }
  155. }
  156. /** @} */