1
0

BxBaseUploaderHTML5.php 8.7 KB

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