BxBaseUploaderHTML5.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import ('BxDolUploader');
  10. /**
  11. * Upload files using AJAX uploader with multiple files selection support (without flash),
  12. * it works in Firefox and WebKit(Safari, Chrome) browsers only, but has fallback for other browsers (IE, Opera).
  13. * @see BxDolUploader
  14. */
  15. class BxBaseUploaderHTML5 extends BxDolUploader {
  16. protected $_sDivId; ///< div id where upload button will be placed
  17. function __construct ($aObject, $sStorageObject, $sUniqId) {
  18. parent::__construct($aObject, $sStorageObject, $sUniqId);
  19. $this->_sDivId = 'bx-form-input-files-' . $sUniqId . '-div-' . $this->_aObject['object'];
  20. $this->_sButtonTemplate = 'uploader_button_html5.html';
  21. }
  22. /**
  23. * Show uploader button.
  24. * @return HTML string
  25. */
  26. public function getUploaderButton($sGhostTemplate, $isMultiple = true, $aParams = array()) {
  27. $s = parent::getUploaderButton($sGhostTemplate, $isMultiple, $aParams);
  28. $this->_oTemplate->addJs('fileuploader.js');
  29. return $s;
  30. }
  31. /**
  32. * Get uploader button title
  33. */
  34. public function getUploaderButtonTitle($mixed = false) {
  35. if (is_string($mixed))
  36. return $mixed;
  37. elseif (is_array($mixed) && isset($mixed['HTML5']))
  38. return $mixed['HTML5'];
  39. else
  40. return _t('_sys_uploader_html5_button_name');
  41. }
  42. /**
  43. * Show uploader form.
  44. * @return HTML string
  45. */
  46. public function getUploaderForm($isMultiple = true, $iContentId = false) {
  47. parent::getUploaderForm($isMultiple, $iContentId);
  48. $sForm = $this->_oTemplate->parseHtmlByName('uploader_form_html5.html', array(
  49. 'form_container_id' => $this->_sFormContainerId,
  50. 'errors_container_id' => $this->_sErrorsContainerId,
  51. 'uploader_instance_name' => $this->getNameJsInstanceUploader(),
  52. 'restrictions_text' => $this->getRestrictionsText(),
  53. 'div_id' => $this->_sDivId,
  54. 'content_id' => $iContentId,
  55. ));
  56. bx_import('BxTemplFunctions');
  57. return BxTemplFunctions::getInstance()->transBox('', $sForm);
  58. }
  59. /**
  60. * Handle uploads here.
  61. * @param $mixedFiles as usual $_FILES['some_name'] array, but maybe some other params depending on the uploader
  62. * @return nothing, but if some files failed to upload, the actual error message can be determined by calling BxDolUploader::getUploadErrorMessages()
  63. */
  64. public function handleUploads ($iProfileId, $mixedFiles, $isMultiple = true, $iContentId = false) {
  65. bx_import('BxDolStorage');
  66. $oStorage = BxDolStorage::getObjectInstance($this->_sStorageObject);
  67. $iProfileId = bx_get_logged_profile_id();
  68. if (!$isMultiple)
  69. $this->cleanupGhostsForProfile($iProfileId, $iContentId);
  70. if (bx_get('qqfile'))
  71. $iId = $oStorage->storeFileFromXhr(bx_get('qqfile'), true, $iProfileId, $iContentId);
  72. else
  73. $iId = $oStorage->storeFileFromForm($_FILES['qqfile'], true, $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. require_once(BX_DIRECTORY_PATH_PLUGINS . 'Services_JSON.php');
  81. $oParser = new Services_JSON();
  82. echo htmlspecialchars($oParser->encode($aResponse), ENT_NOQUOTES);
  83. }
  84. }
  85. /** @} */