BxBaseUploaderHTML5.php 4.9 KB

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