BxBaseCmtsForm.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. class BxBaseCmtsForm extends BxTemplFormView
  10. {
  11. protected static $_sAttributeMaskId;
  12. protected static $_sAttributeMaskName;
  13. protected $_oObject;
  14. protected $_sGhostTemplateImage;
  15. public function __construct($aInfo, $oTemplate)
  16. {
  17. parent::__construct($aInfo, $oTemplate);
  18. if(empty(self::$_sAttributeMaskId))
  19. self::$_sAttributeMaskId = $this->aFormAttrs['id'];
  20. if(empty(self::$_sAttributeMaskName))
  21. self::$_sAttributeMaskName = $this->aFormAttrs['name'];
  22. $this->_oObject = null;
  23. $this->_sGhostTemplateImage = 'comments_uploader_nfw.html';
  24. if(isset($this->aInputs['cmt_image'])) {
  25. $this->aInputs['cmt_image']['storage_object'] = 'sys_cmts_images';
  26. $this->aInputs['cmt_image']['images_transcoder'] = 'sys_cmts_images_preview';
  27. $this->aInputs['cmt_image']['uploaders'] = !empty($this->aInputs['cmt_image']['value']) ? unserialize($this->aInputs['cmt_image']['value']) : array('sys_cmts_html5');
  28. $this->aInputs['cmt_image']['upload_buttons_titles'] = array('Simple' => 'camera', 'HTML5' => 'camera');
  29. $this->aInputs['cmt_image']['storage_private'] = 0;
  30. $this->aInputs['cmt_image']['multiple'] = true;
  31. $this->aInputs['cmt_image']['content_id'] = 0;
  32. $this->aInputs['cmt_image']['ghost_template'] = '';
  33. }
  34. }
  35. function getHtmlEditorQueryParams($aInput)
  36. {
  37. $aQueryParams = parent::getHtmlEditorQueryParams($aInput);
  38. if (isset($this->aInputs['id'])) {
  39. $aQueryParams['cid'] = $this->aInputs['id']['value'];
  40. }
  41. $oObject = $this->_getObject(bx_process_input($this->aInputs['sys']['value']), (int)$this->aInputs['id']['value']);
  42. if($oObject && ($aCmtInfo = $this->_oObject->getSystemInfo())) {
  43. $aQueryParams['cmod'] = $aCmtInfo['module'] ?? '';
  44. }
  45. $aQueryParams['m'] = 'sys_cmts';
  46. $aQueryParams['fi'] = '';
  47. /**
  48. * @hooks
  49. * @hookdef hook-system-editor_query_params 'system', 'editor_query_params' - hook to override http(s) request's query params, which is used in HTML editor
  50. * - $unit_name - equals `system`
  51. * - $action - equals `editor_query_params`
  52. * - $object_id - not used
  53. * - $sender_id - not used
  54. * - $extra_params - array of additional params with the following array keys:
  55. * - `form` - [object] an instance of form, @see BxDolForm
  56. * - `override_result` - [array] by ref, query string params, can be overridden in hook processing
  57. * @hook @ref hook-system-editor_query_params
  58. */
  59. bx_alert('system', 'editor_query_params', 0, 0, [
  60. 'form' => $this,
  61. 'override_result' => &$aQueryParams
  62. ]);
  63. return $aQueryParams;
  64. }
  65. public function getAttributeMask($sAttribute)
  66. {
  67. $sName = '_sAttributeMask' . bx_gen_method_name($sAttribute);
  68. return isset(self::$$sName) ? self::$$sName : '';
  69. }
  70. public function getStorageObjectName()
  71. {
  72. return isset($this->aInputs['cmt_image']['storage_object']) ? $this->aInputs['cmt_image']['storage_object'] : '';
  73. }
  74. public function getTranscoderPreviewName()
  75. {
  76. return isset($this->aInputs['cmt_image']['images_transcoder']) ? $this->aInputs['cmt_image']['images_transcoder'] : '';
  77. }
  78. function initChecker ($aValues = array (), $aSpecificValues = array())
  79. {
  80. if(isset($this->aInputs['cmt_image'])) {
  81. if(!empty($this->aInputs['sys']['value']) && !empty($this->aInputs['id']['value']) && !empty($aValues['cmt_id'])) {
  82. $oObject = $this->_getObject(bx_process_input($this->aInputs['sys']['value']), (int)$this->aInputs['id']['value']);
  83. if($oObject)
  84. $this->aInputs['cmt_image']['content_id'] = $oObject->getCommentUniqId($aValues['cmt_id']);
  85. }
  86. $this->aInputs['cmt_image']['ghost_template'] = $this->oTemplate->parseHtmlByName($this->_sGhostTemplateImage, $this->_getGhostTmplVarsImage());
  87. }
  88. if (isset($this->aInputs['cmt_anonymous']) && isset($aValues['cmt_author_id']))
  89. $this->aInputs['cmt_anonymous']['checked'] = $aValues['cmt_author_id'] < 0;
  90. parent::initChecker ($aValues, $aSpecificValues);
  91. }
  92. public function insert ($aValsToAdd = array(), $isIgnore = false)
  93. {
  94. $aValsToAdd['cmt_author_id'] *= isset($this->aInputs['cmt_anonymous']) && $this->getCleanValue('cmt_anonymous') ? -1 : 1;
  95. return parent::insert ($aValsToAdd, $isIgnore);
  96. }
  97. public function update ($iCmtId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  98. {
  99. if (isset($this->aInputs['cmt_anonymous'])) {
  100. $aCmt = $this->_getObject($this->getCleanValue('sys'), $this->getCleanValue('id'))->getCommentRow($iCmtId);
  101. $aValsToAdd['cmt_author_id'] = ($this->getCleanValue('cmt_anonymous') ? -1 : 1) * abs($aCmt['cmt_author_id']);
  102. }
  103. return parent::update ($iCmtId, $aValsToAdd, $aTrackTextFieldsChanges);
  104. }
  105. public function processImages ($oCmts, $sFieldName, $iCmtUniqId, $iCmtId, $iCmtAuthorId, $isAssociateWithContent = false)
  106. {
  107. if(!isset($this->aInputs[$sFieldName]))
  108. return true;
  109. $mixedFileIds = $this->getCleanValue($sFieldName);
  110. if(!$mixedFileIds)
  111. return true;
  112. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sFieldName]['storage_object']);
  113. if(!$oStorage)
  114. return false;
  115. $aGhostImages = $oStorage->getGhosts($iCmtAuthorId, $isAssociateWithContent ? 0 : $iCmtUniqId, true, $oCmts->isAdmin($iCmtAuthorId));
  116. if(!$aGhostImages)
  117. return true;
  118. foreach($aGhostImages as $aImage) {
  119. if(is_array($mixedFileIds) && !in_array($aImage['id'], $mixedFileIds))
  120. continue;
  121. if($aImage['private'])
  122. $oStorage->setFilePrivate($aImage['id'], 1);
  123. if($iCmtId)
  124. $this->_associalImageWithContent($oCmts, $sFieldName, $iCmtUniqId, $iCmtId, $iCmtAuthorId, $aImage['id']);
  125. }
  126. return true;
  127. }
  128. protected function genCustomRowCmtCf(&$aInput)
  129. {
  130. $aInput = BxDolContentFilter::getInstance()->getInputForComments($aInput);
  131. if($aInput['type'] == 'hidden') {
  132. $this->_sCodeAdd .= $this->genInput($aInput);
  133. return '';
  134. }
  135. return $this->genRowStandard($aInput);
  136. }
  137. protected function _getObject($sSystem, $iId)
  138. {
  139. if(empty($this->_oObject))
  140. $this->_oObject = BxDolCmts::getObjectInstance($sSystem, $iId);
  141. return $this->_oObject;
  142. }
  143. protected function _getGhostTmplVarsImage($aCmt = array())
  144. {
  145. return array (
  146. 'name' => $this->aInputs['cmt_image']['name'],
  147. 'content_id' => (int)$this->aInputs['cmt_image']['content_id'],
  148. 'editor_id' => '',
  149. );
  150. }
  151. protected function _associalImageWithContent($oCmts, $sFieldName, $iCmtUniqId, $iCmtId, $iCmtAuthorId, $iImageId)
  152. {
  153. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sFieldName]['storage_object']);
  154. if(!$oStorage)
  155. return false;
  156. $oStorage->updateGhostsContentId($iImageId, $iCmtAuthorId, $iCmtUniqId, $oCmts->isAdmin($iCmtAuthorId));
  157. $aSystem = $oCmts->getSystemInfo();
  158. $oCmts->getQueryObject()->saveImages($aSystem['system_id'], $iCmtId, $iImageId);
  159. return true;
  160. }
  161. }
  162. /** @} */