BxBaseUploaderHTML5.php 8.8 KB

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