BxBaseUploaderHTML5.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 UnaCore UNA Core
  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->_sButtonTemplate = 'uploader_button_html5.html';
  23. }
  24. /**
  25. * add necessary js, css files and js translations
  26. */
  27. public function addCssJs ()
  28. {
  29. parent::addCssJs ();
  30. $this->_oTemplate->addJs('fileuploader.js');
  31. }
  32. /**
  33. * Get uploader button title
  34. */
  35. public function getUploaderButtonTitle($mixed = false)
  36. {
  37. if (is_string($mixed))
  38. return $mixed;
  39. elseif (is_array($mixed) && isset($mixed['HTML5']))
  40. return $mixed['HTML5'];
  41. else
  42. return _t('_sys_uploader_html5_button_name');
  43. }
  44. /**
  45. * Show uploader form.
  46. * @return HTML string
  47. */
  48. public function getUploaderForm($isMultiple = true, $iContentId = false, $isPrivate = true)
  49. {
  50. return $this->_oTemplate->parseHtmlByName($this->_sUploaderFormTemplate, array(
  51. 'form_container_id' => $this->_sFormContainerId,
  52. 'errors_container_id' => $this->_sErrorsContainerId,
  53. 'uploader_instance_name' => $this->getNameJsInstanceUploader(),
  54. 'restrictions_text' => $this->getRestrictionsText(),
  55. 'div_id' => $this->_sDivId,
  56. 'content_id' => $iContentId,
  57. 'storage_private' => $isPrivate,
  58. ));
  59. }
  60. /**
  61. * Handle uploads here.
  62. * @param $mixedFiles as usual $_FILES['some_name'] array, but maybe some other params depending on the uploader
  63. * @return nothing, but if some files failed to upload, the actual error message can be determined by calling BxDolUploader::getUploadErrorMessages()
  64. */
  65. public function handleUploads ($iProfileId, $mixedFiles, $isMultiple = true, $iContentId = false, $bPrivate = true)
  66. {
  67. $oStorage = BxDolStorage::getObjectInstance($this->_sStorageObject);
  68. if (!$isMultiple)
  69. $this->deleteGhostsForProfile($iProfileId, $iContentId);
  70. if (bx_get('qqfile'))
  71. $iId = $oStorage->storeFileFromXhr(bx_get('qqfile'), $bPrivate, $iProfileId, $iContentId);
  72. else
  73. $iId = $oStorage->storeFileFromForm($_FILES['qqfile'], $bPrivate, $iProfileId, $iContentId);
  74. if ($iId) {
  75. $aResponse = array ('success' => 1);
  76. } else {
  77. $this->appendUploadErrorMessage(_t('_sys_uploader_err_msg', isset($_FILES['qqfile']['name']) ? $_FILES['qqfile']['name'] : bx_get('qqfile'), $oStorage->getErrorString()));
  78. $aResponse = array ('error' => $this->getUploadErrorMessages());
  79. }
  80. echo htmlspecialchars(json_encode($aResponse), ENT_NOQUOTES);
  81. }
  82. }
  83. /** @} */