BxBaseCmtsForm.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 = array_merge(parent::getHtmlEditorQueryParams($aInput), $this->getMentionsQueryParams($aInput), [
  38. 'fi' => ''
  39. ]);
  40. /**
  41. * @hooks
  42. * @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
  43. * - $unit_name - equals `system`
  44. * - $action - equals `editor_query_params`
  45. * - $object_id - not used
  46. * - $sender_id - not used
  47. * - $extra_params - array of additional params with the following array keys:
  48. * - `form` - [object] an instance of form, @see BxDolForm
  49. * - `override_result` - [array] by ref, query string params, can be overridden in hook processing
  50. * @hook @ref hook-system-editor_query_params
  51. */
  52. bx_alert('system', 'editor_query_params', 0, 0, [
  53. 'form' => $this,
  54. 'override_result' => &$aQueryParams
  55. ]);
  56. return $aQueryParams;
  57. }
  58. function getMentionsQueryParams($aInput)
  59. {
  60. $aParams = array_merge(parent::getMentionsQueryParams($aInput), [
  61. 'ct' => 'comment',
  62. 'cm' => '',
  63. 'ci' => 0,
  64. 'cp' => ''
  65. ]);
  66. if(($sSystem = $this->aInputs['sys']['value'] ?? false) && ($iObjectId = $this->aInputs['id']['value'] ?? false)) {
  67. $aParams['ci'] = (int)$iObjectId;
  68. $oObject = $this->_getObject(bx_process_input($sSystem), (int)$iObjectId);
  69. if($oObject && ($aCmtInfo = $this->_oObject->getSystemInfo()))
  70. $aParams['cm'] = $aCmtInfo['module'] ?? '';
  71. }
  72. return $aParams;
  73. }
  74. public function getAttributeMask($sAttribute)
  75. {
  76. $sName = '_sAttributeMask' . bx_gen_method_name($sAttribute);
  77. return isset(self::$$sName) ? self::$$sName : '';
  78. }
  79. public function getStorageObjectName()
  80. {
  81. return isset($this->aInputs['cmt_image']['storage_object']) ? $this->aInputs['cmt_image']['storage_object'] : '';
  82. }
  83. public function getTranscoderPreviewName()
  84. {
  85. return isset($this->aInputs['cmt_image']['images_transcoder']) ? $this->aInputs['cmt_image']['images_transcoder'] : '';
  86. }
  87. function initChecker ($aValues = array (), $aSpecificValues = array())
  88. {
  89. if(isset($this->aInputs['cmt_image'])) {
  90. if(!empty($this->aInputs['sys']['value']) && !empty($this->aInputs['id']['value']) && !empty($aValues['cmt_id'])) {
  91. $oObject = $this->_getObject(bx_process_input($this->aInputs['sys']['value']), (int)$this->aInputs['id']['value']);
  92. if($oObject)
  93. $this->aInputs['cmt_image']['content_id'] = $oObject->getCommentUniqId($aValues['cmt_id']);
  94. }
  95. $this->aInputs['cmt_image']['ghost_template'] = $this->oTemplate->parseHtmlByName($this->_sGhostTemplateImage, $this->_getGhostTmplVarsImage());
  96. }
  97. if (isset($this->aInputs['cmt_anonymous']) && isset($aValues['cmt_author_id']))
  98. $this->aInputs['cmt_anonymous']['checked'] = $aValues['cmt_author_id'] < 0;
  99. parent::initChecker ($aValues, $aSpecificValues);
  100. }
  101. public function insert ($aValsToAdd = array(), $isIgnore = false)
  102. {
  103. $aValsToAdd['cmt_author_id'] *= isset($this->aInputs['cmt_anonymous']) && $this->getCleanValue('cmt_anonymous') ? -1 : 1;
  104. return parent::insert ($aValsToAdd, $isIgnore);
  105. }
  106. public function update ($iCmtId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  107. {
  108. if (isset($this->aInputs['cmt_anonymous'])) {
  109. $aCmt = $this->_getObject($this->getCleanValue('sys'), $this->getCleanValue('id'))->getCommentRow($iCmtId);
  110. $aValsToAdd['cmt_author_id'] = ($this->getCleanValue('cmt_anonymous') ? -1 : 1) * abs($aCmt['cmt_author_id']);
  111. }
  112. return parent::update ($iCmtId, $aValsToAdd, $aTrackTextFieldsChanges);
  113. }
  114. public function processImages ($oCmts, $sFieldName, $iCmtUniqId, $iCmtId, $iCmtAuthorId, $isAssociateWithContent = false)
  115. {
  116. if(!isset($this->aInputs[$sFieldName]))
  117. return true;
  118. $mixedFileIds = $this->getCleanValue($sFieldName);
  119. if(!$mixedFileIds)
  120. return true;
  121. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sFieldName]['storage_object']);
  122. if(!$oStorage)
  123. return false;
  124. $aGhostImages = $oStorage->getGhosts($iCmtAuthorId, $isAssociateWithContent ? 0 : $iCmtUniqId, true, $oCmts->isAdmin($iCmtAuthorId));
  125. if(!$aGhostImages)
  126. return true;
  127. foreach($aGhostImages as $aImage) {
  128. if(is_array($mixedFileIds) && !in_array($aImage['id'], $mixedFileIds))
  129. continue;
  130. if($aImage['private'])
  131. $oStorage->setFilePrivate($aImage['id'], 1);
  132. if($iCmtId)
  133. $this->_associalImageWithContent($oCmts, $sFieldName, $iCmtUniqId, $iCmtId, $iCmtAuthorId, $aImage['id']);
  134. }
  135. return true;
  136. }
  137. protected function genCustomRowCmtCf(&$aInput)
  138. {
  139. $aInput = BxDolContentFilter::getInstance()->getInputForComments($aInput);
  140. if($aInput['type'] == 'hidden') {
  141. $this->_sCodeAdd .= $this->genInput($aInput);
  142. return '';
  143. }
  144. return $this->genRowStandard($aInput);
  145. }
  146. protected function _getObject($sSystem, $iId)
  147. {
  148. if(empty($this->_oObject))
  149. $this->_oObject = BxDolCmts::getObjectInstance($sSystem, $iId);
  150. return $this->_oObject;
  151. }
  152. protected function _getGhostTmplVarsImage($aCmt = array())
  153. {
  154. return array (
  155. 'name' => $this->aInputs['cmt_image']['name'],
  156. 'content_id' => (int)$this->aInputs['cmt_image']['content_id'],
  157. 'editor_id' => '',
  158. );
  159. }
  160. protected function _associalImageWithContent($oCmts, $sFieldName, $iCmtUniqId, $iCmtId, $iCmtAuthorId, $iImageId)
  161. {
  162. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sFieldName]['storage_object']);
  163. if(!$oStorage)
  164. return false;
  165. $oStorage->updateGhostsContentId($iImageId, $iCmtAuthorId, $iCmtUniqId, $oCmts->isAdmin($iCmtAuthorId));
  166. $aSystem = $oCmts->getSystemInfo();
  167. $oCmts->getQueryObject()->saveImages($aSystem['system_id'], $iCmtId, $iImageId);
  168. return true;
  169. }
  170. }
  171. /** @} */