BxBaseCmts.php 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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. * @see BxDolCmts
  11. */
  12. class BxBaseCmts extends BxDolCmts
  13. {
  14. protected static $_sTmplContentElementBlock;
  15. protected static $_sTmplContentElementInline;
  16. protected static $_sTmplContentDoCommentLabel;
  17. protected static $_sTmplContentCounter;
  18. protected $_sJsObjName;
  19. protected $_sStylePrefix;
  20. protected $_aHtmlIds;
  21. protected $_aElementDefaults;
  22. function __construct( $sSystem, $iId, $iInit = true, $oTemplate = false)
  23. {
  24. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  25. if (empty($sSystem))
  26. return;
  27. $this->_sJsObjName = 'oCmts' . bx_gen_method_name($sSystem, array('_' , '-')) . $iId;
  28. $this->_sStylePrefix = isset($this->_aSystem['root_style_prefix']) ? $this->_aSystem['root_style_prefix'] : 'cmt';
  29. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
  30. $this->_aHtmlIds = array(
  31. 'main' => 'bx-cmt-' . $sHtmlId,
  32. 'counter' => 'bx-cmt-counter-' . $sHtmlId
  33. );
  34. $this->_aElementDefaults = array(
  35. 'show_do_comment_as_button' => false,
  36. 'show_do_comment_as_button_small' => false,
  37. 'show_do_comment_image' => false,
  38. 'show_do_comment_icon' => true,
  39. 'show_do_comment_label' => false,
  40. 'show_counter' => true,
  41. 'show_counter_empty' => false
  42. );
  43. if(empty(self::$_sTmplContentElementBlock))
  44. self::$_sTmplContentElementBlock = $this->_oTemplate->getHtml('comment_element_block.html');
  45. if(empty(self::$_sTmplContentElementInline))
  46. self::$_sTmplContentElementInline = $this->_oTemplate->getHtml('comment_element_inline.html');
  47. if(empty(self::$_sTmplContentDoCommentLabel))
  48. self::$_sTmplContentDoCommentLabel = $this->_oTemplate->getHtml('comment_do_comment_label.html');
  49. if(empty(self::$_sTmplContentCounter))
  50. self::$_sTmplContentCounter = $this->_oTemplate->getHtml('comment_counter.html');
  51. BxDolTemplate::getInstance()->addJsTranslation('_sys_txt_cmt_loading');
  52. }
  53. /**
  54. * Add comments CSS/JS
  55. */
  56. public function addCssJs ()
  57. {
  58. $oForm = BxDolForm::getObjectInstance($this->_sFormObject, $this->_sFormDisplayPost);
  59. $oForm->addCssJs();
  60. }
  61. public function getJsObjectName()
  62. {
  63. return $this->_sJsObjName;
  64. }
  65. /**
  66. * Get initialization section of comments box
  67. *
  68. * @return string
  69. */
  70. public function getJsScript()
  71. {
  72. $aParams = array(
  73. 'sObjName' => $this->_sJsObjName,
  74. 'sRootUrl' => BX_DOL_URL_ROOT,
  75. 'sSystem' => $this->getSystemName(),
  76. 'iAuthorId' => $this->_getAuthorId(),
  77. 'iObjId' => $this->getId(),
  78. 'sBaseUrl' => $this->getBaseUrl(),
  79. 'sPostFormPosition' => $this->_aSystem['post_form_position'],
  80. 'sBrowseType' => $this->_sBrowseType,
  81. 'sDisplayType' => $this->_sDisplayType
  82. );
  83. $this->addCssJs();
  84. return BxDolTemplate::getInstance()->_wrapInTagJsCode("var " . $this->_sJsObjName . " = new BxDolCmts(" . json_encode($aParams) . ");");
  85. }
  86. /**
  87. * get full comments block with initializations
  88. */
  89. function getCommentsBlock($aBp = array(), $aDp = array())
  90. {
  91. $this->_getParams($aBp, $aDp);
  92. //add live update
  93. $this->actionResumeLiveUpdate();
  94. $sServiceCall = BxDolService::getSerializedService('system', 'get_live_updates_comments', array($this->_sSystem, $this->_iId, $this->_getAuthorId(), '{count}'), 'TemplCmtsServices');
  95. BxDolLiveUpdates::getInstance()->add($this->_sSystem . '_live_updates_cmts_' . $this->_iId, 1, $sServiceCall);
  96. //add live update
  97. $sCaption = _t($this->_aT['block_comments_title'], $this->getCommentsCountAll());
  98. $sContent = BxDolTemplate::getInstance()->parseHtmlByName('comments_block.html', array(
  99. 'system' => $this->_sSystem,
  100. 'list_anchor' => $this->getListAnchor(),
  101. 'id' => $this->getId(),
  102. 'comments' => $this->getComments($aBp, $aDp),
  103. 'post_form_top' => $this->getFormBoxPost($aBp, array_merge($aDp, array('type' => $this->_sDisplayType, 'position' => BX_CMT_PFP_TOP))),
  104. 'post_form_bottom' => $this->getFormBoxPost($aBp, array_merge($aDp, array('type' => $this->_sDisplayType, 'position' => BX_CMT_PFP_BOTTOM))),
  105. 'view_image_popup' => $this->_getViewImagePopup(),
  106. 'script' => $this->getJsScript()
  107. ));
  108. return $aDp['in_designbox'] ? DesignBoxContent($sCaption, $sContent, BX_DB_DEF, $this->_getControlsBox()) : array(
  109. 'title' => $sCaption,
  110. 'content' => $sContent,
  111. 'menu' => $this->_getControlsBox(),
  112. );
  113. }
  114. /**
  115. * get comments list for specified parent comment
  116. *
  117. * @param array $aBp - browse params array
  118. * @param array $aDp - display params array
  119. *
  120. */
  121. function getComments($aBp = array(), $aDp = array())
  122. {
  123. $this->_prepareParams($aBp, $aDp);
  124. $aCmts = $this->getCommentsArray($aBp['vparent_id'], $aBp['filter'], $aBp['order'], $aBp['start'], $aBp['per_view']);
  125. if(empty($aCmts) || !is_array($aCmts)) {
  126. if((int)$aBp['parent_id'] == 0 && !isLogged()) {
  127. $oPermalink = BxDolPermalinks::getInstance();
  128. return MsgBox(_t('_cmt_msg_login_required', $oPermalink->permalink('page.php?i=login'), $oPermalink->permalink('page.php?i=create-account')));
  129. }
  130. return isset($aDp['show_empty']) && $aDp['show_empty'] === true ? $this->_getEmpty() : '';
  131. }
  132. $sCmts = '';
  133. foreach($aCmts as $k => $aCmt)
  134. $sCmts .= $this->getComment($aCmt, $aBp, $aDp);
  135. $sCmts = $this->_getMoreLink($sCmts, $aBp, $aDp);
  136. return $sCmts;
  137. }
  138. /**
  139. * get comment view block with initializations
  140. */
  141. function getCommentBlock($iCmtId = 0, $aBp = array(), $aDp = array())
  142. {
  143. $mixedResult = $this->isViewAllowed();
  144. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  145. return $mixedResult;
  146. $sComment = $this->getComment(
  147. $iCmtId,
  148. array_merge(array('type' => $this->_sBrowseType), $aBp),
  149. array_merge(array('type' => BX_CMT_DISPLAY_THREADED), $aDp)
  150. );
  151. if (!$sComment)
  152. return '';
  153. return BxDolTemplate::getInstance()->parseHtmlByName('comment_block.html', array(
  154. 'system' => $this->_sSystem,
  155. 'id' => $this->getId(),
  156. 'comment' => $sComment,
  157. 'view_image_popup' => $this->_getViewImagePopup(),
  158. 'script' => $this->getJsScript()
  159. ));
  160. }
  161. /**
  162. * get one just posted comment
  163. *
  164. * @param int $iCmtId - comment id
  165. * @return string
  166. */
  167. function getComment($mixedCmt, $aBp = array(), $aDp = array())
  168. {
  169. $iUserId = $this->_getAuthorId();
  170. $aCmt = !is_array($mixedCmt) ? $this->getCommentRow((int)$mixedCmt) : $mixedCmt;
  171. if (!$aCmt)
  172. return '';
  173. list($sAuthorName, $sAuthorLink, $sAuthorIcon) = $this->_getAuthorInfo($aCmt['cmt_author_id']);
  174. $sClass = $sClassCnt = '';
  175. if(isset($aCmt['vote_rate']) && (float)$aCmt['vote_rate'] < $this->_aSystem['viewing_threshold']) {
  176. $this->_oTemplate->pareseHtmlByName('comment_hidden.html', array(
  177. 'js_object' => $this->_sJsObjName,
  178. 'id' => $aCmt['cmt_id'],
  179. 'title' => bx_process_output(_t('_hidden_comment', $sAuthorName)),
  180. 'bx_if:show_replies' => array(
  181. 'condition' => $aCmt['cmt_replies'] > 0,
  182. 'content' => array(
  183. 'replies' => _t('_Show N replies', $aCmt['cmt_replies'])
  184. )
  185. )
  186. ));
  187. $sClass = ' ' . $this->_sStylePrefix . '-hidden';
  188. }
  189. if($aCmt['cmt_author_id'] == $iUserId)
  190. $sClass .= ' ' . $this->_sStylePrefix . '-mine';
  191. if(!empty($aDp['blink']) && in_array($aCmt['cmt_id'], $aDp['blink']))
  192. $sClass .= ' ' . $this->_sStylePrefix . '-blink';
  193. if(!empty($aDp['class_comment']))
  194. $sClass .= ' ' . $aDp['class_comment'];
  195. if(!empty($aDp['class_comment_content']))
  196. $sClassCnt .= ' ' . $aDp['class_comment_content'];
  197. $sActions = $this->_getActionsBox($aCmt, $aDp);
  198. $aTmplReplyTo = array();
  199. if((int)$aCmt['cmt_parent_id'] != 0) {
  200. $aParent = $this->getCommentRow($aCmt['cmt_parent_id']);
  201. list($sParAuthorName, $sParAuthorLink, $sParAuthorIcon) = $this->_getAuthorInfo($aParent['cmt_author_id']);
  202. $aTmplReplyTo = array(
  203. 'style_prefix' => $this->_sStylePrefix,
  204. 'par_cmt_link' => $this->getItemUrl($aCmt['cmt_parent_id']),
  205. 'par_cmt_title' => bx_html_attribute(_t('_in_reply_to', $sParAuthorName)),
  206. 'par_cmt_author' => $sParAuthorName
  207. );
  208. }
  209. $sAttachments = $this->_getAttachments($aCmt);
  210. $sReplies = '';
  211. if((int)$aCmt['cmt_replies'] > 0 && !empty($aDp) && $aDp['type'] == BX_CMT_DISPLAY_THREADED) {
  212. $aDp['show_empty'] = false;
  213. $sReplies = $this->getComments(array('parent_id' => $aCmt['cmt_id'], 'vparent_id' => $aCmt['cmt_id'], 'type' => $aBp['type']), $aDp);
  214. }
  215. $aTmplVarsMeta = array();
  216. if(!empty($this->_sMenuObjMeta)) {
  217. $oMenuMeta = BxDolMenu::getObjectInstance($this->_sMenuObjMeta, $this->_oTemplate);
  218. if($oMenuMeta) {
  219. $oMenuMeta->setCmtsData($this, $aCmt['cmt_id']);
  220. $aTmplVarsMeta = array(
  221. 'style_prefix' => $this->_sStylePrefix,
  222. 'meta' => $oMenuMeta->getCode()
  223. );
  224. }
  225. }
  226. return $this->_oTemplate->parseHtmlByName('comment.html', array_merge(array(
  227. 'system' => $this->_sSystem,
  228. 'style_prefix' => $this->_sStylePrefix,
  229. 'js_object' => $this->_sJsObjName,
  230. 'id' => $aCmt['cmt_id'],
  231. 'anchor' => $this->getItemAnchor($aCmt['cmt_id']),
  232. 'class' => $sClass,
  233. 'class_cnt' => $sClassCnt,
  234. 'bx_if:show_reply_to' => array(
  235. 'condition' => !empty($aTmplReplyTo),
  236. 'content' => $aTmplReplyTo
  237. ),
  238. 'bx_if:meta' => array(
  239. 'condition' => !empty($aTmplVarsMeta),
  240. 'content' => $aTmplVarsMeta
  241. ),
  242. 'bx_if:show_attached' => array(
  243. 'condition' => !empty($sAttachments),
  244. 'content' => array(
  245. 'style_prefix' => $this->_sStylePrefix,
  246. 'attached' => $sAttachments
  247. )
  248. ),
  249. 'actions' => $sActions,
  250. 'replies' => $sReplies
  251. ), $this->_getTmplVarsAuthor($aCmt), $this->_getTmplVarsText($aCmt)));
  252. }
  253. function getCommentSearch($iCmtId, &$sAddon)
  254. {
  255. if(empty($sAddon))
  256. $sAddon = $this->getJsScript();
  257. $sComment = $this->getComment($iCmtId, array(), array(
  258. 'type' => BX_CMT_DISPLAY_FLAT,
  259. 'view_only' => true
  260. ));
  261. return BxDolTemplate::getInstance()->parseHtmlByName('comment_search.html', array(
  262. 'comment' => $sComment,
  263. 'view_image_popup' => $this->_getViewImagePopup(),
  264. ));
  265. }
  266. /**
  267. * get one comment for "Live Search"
  268. *
  269. * @param int $iCmtId - comment id
  270. * @return string
  271. */
  272. function getCommentLiveSearch($mixedCmt, $aParams = array())
  273. {
  274. $aCmt = !is_array($mixedCmt) ? $this->getCommentRow((int)$mixedCmt) : $mixedCmt;
  275. list($sAuthorName, $sAuthorLink, $sAuthorIcon) = $this->_getAuthorInfo($aCmt['cmt_author_id']);
  276. $bAuthorIcon = !empty($sAuthorIcon);
  277. $sViewLink = $this->getViewUrl($aCmt['cmt_id']);
  278. if(empty($sViewLink))
  279. $sViewLink = '#';
  280. return BxDolTemplate::getInstance()->parseHtmlByName('comment_live_search.html', array(
  281. 'bx_if:show_icon' => array(
  282. 'condition' => $bAuthorIcon,
  283. 'content' => array(
  284. 'author_icon' => $sAuthorIcon,
  285. 'view_link' => $sViewLink
  286. )
  287. ),
  288. 'bx_if:show_icon_empty' => array(
  289. 'condition' => !$bAuthorIcon,
  290. 'content' => array()
  291. ),
  292. 'view_link' => $sViewLink,
  293. 'text' => BxTemplFunctions::getInstance()->getStringWithLimitedLength(strip_tags($aCmt['cmt_text']), $this->_sSnippetLenthLiveSearch),
  294. 'sample' => isset($aParams['txt_sample_single']) ? _t($aParams['txt_sample_single']) : ''
  295. ));
  296. }
  297. function getFormBoxPost($aBp = array(), $aDp = array())
  298. {
  299. if(!$this->isPostReplyAllowed())
  300. return '';
  301. return $this->_getFormBox(BX_CMT_ACTION_POST, $aBp, $aDp);
  302. }
  303. function getFormBoxEdit($aBp = array(), $aDp = array())
  304. {
  305. return $this->_getFormBox(BX_CMT_ACTION_EDIT, $aBp, $aDp);
  306. }
  307. function getFormPost($iCmtParentId = 0, $aDp = array())
  308. {
  309. return $this->_getFormPost($iCmtParentId, $aDp);
  310. }
  311. function getFormEdit($iCmtId, $aDp = array())
  312. {
  313. return $this->_getFormEdit($iCmtId, $aDp);
  314. }
  315. function getNotification($iCountOld = 0, $iCountNew = 0)
  316. {
  317. $iCount = (int)$iCountNew - (int)$iCountOld;
  318. if($iCount < 0)
  319. return '';
  320. $aComments = $this->_oQuery->getCommentsBy(array('type' => 'latest', 'object_id' => $this->_iId, 'author' => $this->_getAuthorId(), 'others' => 1, 'start' => '0', 'per_page' => $iCount));
  321. if(empty($aComments) || !is_array($aComments))
  322. return '';
  323. $sJsObject = $this->getJsObjectName();
  324. $oTemplate = BxDolTemplate::getInstance();
  325. $aComments = array_reverse($aComments);
  326. $iComments = count($aComments);
  327. $aTmplVarsNotifs = array();
  328. foreach($aComments as $iIndex => $aComment) {
  329. $iCommentId = $aComment['cmt_id'];
  330. $sShowOnClick = "javascript:" . $sJsObject . ".goTo(this, '" . $this->_sSystem . $iCommentId . "', '" . $iCommentId . "');";
  331. $sReplyOnClick = "javascript:" . $sJsObject . ".goToAndReply(this, '" . $this->_sSystem . $iCommentId . "', '" . $iCommentId . "');";
  332. $aTmplVarsNotifs[] = array(
  333. 'bx_if:show_as_hidden' => array(
  334. 'condition' => $iIndex < ($iComments - 1),
  335. 'content' => array(),
  336. ),
  337. 'item' => $oTemplate->parseHtmlByName('comments_notification.html', array_merge(array(
  338. 'style_prefix' => $this->_sStylePrefix,
  339. 'onclick_show' => $sShowOnClick,
  340. 'onclick_reply' => $sReplyOnClick,
  341. ), $this->_getTmplVarsAuthor($aComment), $this->_getTmplVarsText($aComment))),
  342. 'bx_if:show_previous' => array(
  343. 'condition' => $iIndex > 0,
  344. 'content' => array(
  345. 'onclick_previous' => $sJsObject . '.previousLiveUpdate(this)'
  346. )
  347. ),
  348. 'onclick_close' => $sJsObject . '.hideLiveUpdate(this)'
  349. );
  350. }
  351. return $oTemplate->parseHtmlByName('popup_chain.html', array(
  352. 'html_id' => $this->getNotificationId(),
  353. 'bx_repeat:items' => $aTmplVarsNotifs
  354. ));
  355. }
  356. public function getElementBlock($aParams = array())
  357. {
  358. $aParams['usage'] = BX_CMT_USAGE_BLOCK;
  359. return $this->getElement($aParams);
  360. }
  361. public function getElementInline($aParams = array())
  362. {
  363. $aParams['usage'] = BX_CMT_USAGE_INLINE;
  364. return $this->getElement($aParams);
  365. }
  366. public function getElement($aParams = array())
  367. {
  368. $aParams = array_merge($this->_aElementDefaults, $aParams);
  369. $bShowDoCommentAsButtonSmall = isset($aParams['show_do_comment_as_button_small']) && $aParams['show_do_comment_as_button_small'] == true;
  370. $bShowDoCommentAsButton = !$bShowDoCommentAsButtonSmall && isset($aParams['show_do_comment_as_button']) && $aParams['show_do_comment_as_button'] == true;
  371. $bShowCounterEmpty = isset($aParams['show_counter_empty']) && $aParams['show_counter_empty'] == true;
  372. $iObjectId = $this->getId();
  373. $iAuthorId = $this->_getAuthorId();
  374. $iAuthorIp = $this->_getAuthorIp();
  375. $iCount = $this->getCommentsCountAll();
  376. $bCount = (int)$iCount != 0;
  377. $isAllowedComment = $this->isPostReplyAllowed();
  378. //--- Do Comment
  379. $bTmplVarsDoComment = $this->_isShowDoComment($aParams, $isAllowedComment, $bCount);
  380. $aTmplVarsDoComment = array();
  381. if($bTmplVarsDoComment) {
  382. $sClass = '';
  383. if($bShowDoCommentAsButton)
  384. $sClass = 'bx-btn';
  385. else if ($bShowDoCommentAsButtonSmall)
  386. $sClass = 'bx-btn bx-btn-small';
  387. if(!$isAllowedComment)
  388. $sClass .= $bShowDoCommentAsButton || $bShowDoCommentAsButtonSmall ? ' bx-btn-disabled' : 'bx-cmts-disabled';
  389. $aTmplVarsDoComment = array(
  390. 'style_prefix' => $this->_sStylePrefix,
  391. 'do_comment' => $this->_oTemplate->parseLink($this->getListUrl(), $this->_getLabelDo($aParams), array(
  392. 'class' => $this->_sStylePrefix . '-do-comment ' . $this->_sStylePrefix . '-dc ' . $sClass,
  393. 'title' => _t($this->_getTitleDo())
  394. )),
  395. );
  396. }
  397. //--- Counter
  398. $bTmplVarsCounter = $this->_isShowCounter($aParams, $isAllowedComment, $bCount);
  399. $aTmplVarsCounter = array();
  400. if($bTmplVarsCounter)
  401. $aTmplVarsCounter = array(
  402. 'style_prefix' => $this->_sStylePrefix,
  403. 'bx_if:show_hidden' => array(
  404. 'condition' => !$bShowCounterEmpty && !$bCount,
  405. 'content' => array()
  406. ),
  407. 'counter' => $this->getCounter($aParams)
  408. );
  409. if(!$bTmplVarsDoComment && !$bTmplVarsCounter)
  410. return '';
  411. $sTmplName = $this->{'_getTmplElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_SCORE_USAGE_DEFAULT)}();
  412. return $this->_oTemplate->parseHtmlByContent($sTmplName, array(
  413. 'style_prefix' => $this->_sStylePrefix,
  414. 'html_id' => $this->_aHtmlIds['main'],
  415. 'class' => $this->_sStylePrefix . ($bShowDoCommentAsButton ? '-button' : '') . ($bShowDoCommentAsButtonSmall ? '-button-small' : ''),
  416. 'count' => $iCount,
  417. 'bx_if:show_do_comment' => array(
  418. 'condition' => $bTmplVarsDoComment,
  419. 'content' => $aTmplVarsDoComment
  420. ),
  421. 'bx_if:show_counter' => array(
  422. 'condition' => $bTmplVarsCounter,
  423. 'content' => $aTmplVarsCounter
  424. ),
  425. 'script' => ''
  426. ));
  427. }
  428. public function getCounter($aParams = array())
  429. {
  430. $bShowEmpty = isset($aParams['show_counter_empty']) && $aParams['show_counter_empty'] == true;
  431. $bShowDoCommentAsButtonSmall = isset($aParams['show_do_comment_as_button_small']) && $aParams['show_do_comment_as_button_small'] == true;
  432. $bShowDoCommentAsButton = !$bShowDoCommentAsButtonSmall && isset($aParams['show_do_comment_as_button']) && $aParams['show_do_comment_as_button'] == true;
  433. $iCount = (int)$this->getCommentsCountAll();
  434. $sClass = $this->_sStylePrefix . '-counter';
  435. if($bShowDoCommentAsButtonSmall)
  436. $sClass .= ' bx-btn-small-height';
  437. if($bShowDoCommentAsButton)
  438. $sClass .= ' bx-btn-height';
  439. return $this->_oTemplate->parseHtmlByContent($this->_getTmplCounter(), array(
  440. 'id' => $this->_aHtmlIds['counter'],
  441. 'class' => $sClass,
  442. 'content' => $iCount != 0 || $bShowEmpty ? $this->_getLabelCounter($iCount) : ''
  443. ));
  444. }
  445. protected function _getLabelDo($aParams = array())
  446. {
  447. return $this->_oTemplate->parseHtmlByContent($this->_getTmplLabelDo(), array(
  448. 'bx_if:show_image' => array(
  449. 'condition' => isset($aParams['show_do_comment_image']) && $aParams['show_do_comment_image'] == true,
  450. 'content' => array(
  451. 'src' => $this->_getImageDo()
  452. )
  453. ),
  454. 'bx_if:show_icon' => array(
  455. 'condition' => isset($aParams['show_do_comment_icon']) && $aParams['show_do_comment_icon'] == true,
  456. 'content' => array(
  457. 'name' => $this->_getIconDo()
  458. )
  459. ),
  460. 'bx_if:show_text' => array(
  461. 'condition' => isset($aParams['show_do_comment_label']) && $aParams['show_do_comment_label'] == true,
  462. 'content' => array(
  463. 'text' => _t($this->_getTitleDo())
  464. )
  465. )
  466. ));
  467. }
  468. protected function _getLabelCounter($iCount)
  469. {
  470. return (int)$iCount != 0 ? _t('_cmt_txt_counter', $iCount) : _t('_cmt_txt_counter_empty');
  471. }
  472. protected function _getTmplElementBlock()
  473. {
  474. return self::$_sTmplContentElementBlock;
  475. }
  476. protected function _getTmplElementInline()
  477. {
  478. return self::$_sTmplContentElementInline;
  479. }
  480. protected function _getTmplLabelDo()
  481. {
  482. return self::$_sTmplContentDoCommentLabel;
  483. }
  484. protected function _getTmplCounter()
  485. {
  486. return self::$_sTmplContentCounter;
  487. }
  488. /**
  489. * private functions
  490. */
  491. protected function _getControlsBox()
  492. {
  493. $sDisplay = '';
  494. $bDisplay = (int)$this->_aSystem['is_display_switch'] == 1;
  495. if($bDisplay) {
  496. $aDisplayLinks = array(
  497. array('id' => $this->_sSystem . '-flat', 'name' => $this->_sSystem . '-flat', 'class' => '', 'title' => '_cmt_display_flat', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeDisplay(this, \'flat\');'),
  498. array('id' => $this->_sSystem . '-threaded', 'name' => $this->_sSystem . '-threaded', 'class' => '', 'title' => '_cmt_display_threaded', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeDisplay(this, \'threaded\');')
  499. );
  500. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-display', 'menu_items' => $aDisplayLinks));
  501. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sDisplayType);
  502. $sDisplay = $oMenu->getCode();
  503. }
  504. $sBrowseType = '';
  505. $bBrowseType = (int)$this->_aSystem['is_browse_switch'] == 1;
  506. if($bBrowseType) {
  507. $aBrowseLinks = array(
  508. array('id' => $this->_sSystem . '-tail', 'name' => $this->_sSystem . '-tail', 'class' => '', 'title' => '_cmt_browse_tail', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'tail\');'),
  509. array('id' => $this->_sSystem . '-head', 'name' => $this->_sSystem . '-head', 'class' => '', 'title' => '_cmt_browse_head', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'head\');'),
  510. array('id' => $this->_sSystem . '-popular', 'name' => $this->_sSystem . '-popular', 'class' => '', 'title' => '_cmt_browse_popular', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'popular\');'),
  511. );
  512. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-browse', 'menu_items' => $aBrowseLinks));
  513. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sBrowseType);
  514. $sBrowseType = $oMenu->getCode();
  515. }
  516. $sBrowseFilter = '';
  517. $bBrowseFilter = (int)$this->_aSystem['is_browse_filter'] == 1;
  518. if($bBrowseFilter) {
  519. $aFilterLinks = array(
  520. array('id' => $this->_sSystem . '-all', 'name' => $this->_sSystem . '-all', 'class' => '', 'title' => '_cmt_browse_all', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'all\');'),
  521. array('id' => $this->_sSystem . '-friends', 'name' => $this->_sSystem . '-friends', 'class' => '', 'title' => '_cmt_browse_friends', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'friends\');'),
  522. array('id' => $this->_sSystem . '-subscriptions', 'name' => $this->_sSystem . '-subscriptions', 'class' => '', 'title' => '_cmt_browse_subscriptions', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'subscriptions\');')
  523. );
  524. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-filter', 'menu_items' => $aFilterLinks));
  525. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sBrowseFilter);
  526. $sBrowseFilter = $oMenu->getCode();
  527. }
  528. return $this->_oTemplate->parseHtmlByName('comments_controls.html', array(
  529. 'display_switcher' => $bDisplay ? $sDisplay : '',
  530. 'bx_if:is_divider_1' => array(
  531. 'condition' => $bDisplay && $bBrowseType,
  532. 'content' => array(
  533. 'style_prefix' => $this->_sStylePrefix,
  534. )
  535. ),
  536. 'browse_switcher' => $bBrowseType ? $sBrowseType : '',
  537. 'bx_if:is_divider_2' => array(
  538. 'condition' => $bBrowseType && $bBrowseFilter,
  539. 'content' => array(
  540. 'style_prefix' => $this->_sStylePrefix,
  541. )
  542. ),
  543. 'filter_switcher' => $bBrowseFilter ? $sBrowseFilter : '',
  544. ));
  545. }
  546. protected function _getActionsBox(&$aCmt, $aDp = array())
  547. {
  548. $bViewOnly = isset($aDp['view_only']) && $aDp['view_only'] === true;
  549. $bDynamicMode = isset($aDp['dynamic_mode']) && $aDp['dynamic_mode'] === true;
  550. $sMenuActions = '';
  551. if(!$bViewOnly) {
  552. $oMenuActions = BxDolMenu::getObjectInstance($this->_sMenuObjActions);
  553. $oMenuActions->setCmtsData($this, $aCmt['cmt_id']);
  554. $oMenuActions->setDynamicMode($bDynamicMode);
  555. $sMenuActions = $oMenuActions->getCode();
  556. }
  557. return $this->_oTemplate->parseHtmlByName('comment_actions.html', array(
  558. 'id' => $aCmt['cmt_id'],
  559. 'js_object' => $this->_sJsObjName,
  560. 'style_prefix' => $this->_sStylePrefix,
  561. 'menu_actions' => $sMenuActions,
  562. ));
  563. }
  564. protected function _getFormBox($sType, $aBp, $aDp)
  565. {
  566. $iCmtParentId = isset($aBp['parent_id']) ? (int)$aBp['parent_id'] : 0;
  567. $sPosition = isset($aDp['position']) ? $aDp['position'] : '';
  568. $sPositionSystem = $this->_aSystem['post_form_position'];
  569. if(!empty($sPosition) && $sPositionSystem != $sPosition)
  570. return '';
  571. $sMethod = '_getForm' . ucfirst($sType);
  572. $aForm = $this->$sMethod($iCmtParentId, $aDp);
  573. return BxDolTemplate::getInstance()->parseHtmlByName('comment_reply_box.html', array(
  574. 'js_object' => $this->_sJsObjName,
  575. 'style_prefix' => $this->_sStylePrefix,
  576. 'bx_if:show_class' => array(
  577. 'condition' => !empty($sPosition),
  578. 'content' => array(
  579. 'class' => $this->_sStylePrefix . '-reply-' . $sPosition
  580. )
  581. ),
  582. 'form' => $aForm['form'],
  583. 'form_id' => $aForm['form_id'],
  584. ));
  585. }
  586. protected function _getFormPost($iCmtParentId = 0, $aDp = array())
  587. {
  588. $bDynamic = isset($aDp['dynamic_mode']) && (bool)$aDp['dynamic_mode'];
  589. $oForm = $this->_getForm(BX_CMT_ACTION_POST, $iCmtParentId);
  590. $oForm->aInputs['cmt_parent_id']['value'] = $iCmtParentId;
  591. $oForm->initChecker();
  592. if($oForm->isSubmittedAndValid()) {
  593. $iCmtAuthorId = $this->_getAuthorId();
  594. $iCmtParentId = $oForm->getCleanValue('cmt_parent_id');
  595. $sCmtText = $oForm->getCleanValue('cmt_text');
  596. $iLevel = 0;
  597. $iCmtVisualParentId = 0;
  598. if((int)$iCmtParentId > 0) {
  599. $aParent = $this->getCommentRow($iCmtParentId);
  600. $iLevel = (int)$aParent['cmt_level'] + 1;
  601. $iCmtVisualParentId = $iLevel > $this->getMaxLevel() ? $aParent['cmt_vparent_id'] : $iCmtParentId;
  602. }
  603. $iCmtId = (int)$oForm->insert(array('cmt_vparent_id' => $iCmtVisualParentId, 'cmt_object_id' => $this->_iId, 'cmt_author_id' => $iCmtAuthorId, 'cmt_level' => $iLevel, 'cmt_time' => time()));
  604. if($iCmtId != 0) {
  605. if($this->isAttachImageEnabled()) {
  606. $aImages = $oForm->getCleanValue('cmt_image');
  607. if(!empty($aImages) && is_array($aImages)) {
  608. $oStorage = BxDolStorage::getObjectInstance($this->getStorageObjectName());
  609. foreach($aImages as $iImageId)
  610. if($this->_oQuery->saveImages($this->_aSystem['system_id'], $iCmtId, $iImageId))
  611. $oStorage->afterUploadCleanup($iImageId, $iCmtAuthorId);
  612. }
  613. }
  614. if($iCmtParentId) {
  615. $this->_oQuery->updateRepliesCount($iCmtParentId, 1);
  616. if(!BxDolModuleQuery::getInstance()->isEnabledByName('bx_notifications'))
  617. $this->_sendNotificationEmail($iCmtId, $iCmtParentId);
  618. }
  619. $this->_triggerComment();
  620. $this->isPostReplyAllowed(true);
  621. if ($this->_sMetatagsObj) {
  622. $oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj);
  623. $oMetatags->metaAdd($this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId), $sCmtText);
  624. }
  625. $this->onPostAfter($iCmtId);
  626. return array('id' => $iCmtId, 'parent_id' => $iCmtParentId);
  627. }
  628. return array('msg' => _t('_cmt_err_cannot_perform_action'));
  629. }
  630. return array('form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id);
  631. }
  632. protected function _getFormEdit($iCmtId, $aDp = array())
  633. {
  634. $bDynamic = isset($aDp['dynamic_mode']) && (bool)$aDp['dynamic_mode'];
  635. $aCmt = $this->_oQuery->getCommentSimple ((int)$this->getId(), $iCmtId);
  636. if(!$aCmt)
  637. return array('msg' => _t('_No such comment'));
  638. $iCmtAuthorId = $this->_getAuthorId();
  639. if(!$this->isEditAllowed($aCmt))
  640. return array('msg' => $aCmt['cmt_author_id'] == $iCmtAuthorId ? strip_tags($this->msgErrEditAllowed()) : _t('_Access denied'));
  641. $oForm = $this->_getForm(BX_CMT_ACTION_EDIT, $aCmt['cmt_id']);
  642. $oForm->initChecker($aCmt);
  643. if($oForm->isSubmittedAndValid()) {
  644. $sCmtText = $oForm->getCleanValue('cmt_text');
  645. if($oForm->update($iCmtId) !== false) {
  646. $this->isEditAllowed($aCmt, true);
  647. if ($this->_sMetatagsObj) {
  648. $oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj);
  649. $oMetatags->metaAdd($this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId), $sCmtText);
  650. }
  651. $this->onEditAfter($iCmtId);
  652. return array('id' => $iCmtId, 'text' => $this->_prepareTextForOutput($sCmtText, $iCmtId));
  653. }
  654. return array('msg' => _t('_cmt_err_cannot_perform_action'));
  655. }
  656. return array('form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id);
  657. }
  658. protected function _getForm($sAction, $iId)
  659. {
  660. $oForm = $this->_getFormObject($sAction);
  661. $oForm->setId(sprintf($oForm->getAttributeMask('id'), $sAction, $this->_sSystem, $iId));
  662. $oForm->setName(sprintf($oForm->getAttributeMask('name'), $sAction, $this->_sSystem, $iId));
  663. $oForm->aParams['db']['table'] = $this->_aSystem['table'];
  664. $oForm->aInputs['sys']['value'] = $this->_sSystem;
  665. $oForm->aInputs['id']['value'] = $this->_iId;
  666. $oForm->aInputs['action']['value'] = 'Submit' . ucfirst($sAction) . 'Form';
  667. if(!$this->isAttachImageEnabled())
  668. unset($oForm->aInputs['cmt_image']);
  669. if(isset($oForm->aInputs['cmt_text'])) {
  670. $oForm->aInputs['cmt_text']['html'] = $this->_aSystem['html'];
  671. $oForm->aInputs['cmt_text']['db']['pass'] = $this->isHtml() ? 'XssHtml' : 'XssMultiline';
  672. $iCmtTextMin = (int)$this->_aSystem['chars_post_min'];
  673. $iCmtTextMax = (int)$this->_aSystem['chars_post_max'];
  674. $oForm->aInputs['cmt_text']['checker']['params'] = array($iCmtTextMin, $iCmtTextMax);
  675. $oForm->aInputs['cmt_text']['checker']['error'] = _t('_Please enter n1-n2 characters', $iCmtTextMin, $iCmtTextMax);
  676. }
  677. return $oForm;
  678. }
  679. protected function _getMoreLink($sCmts, $aBp = array(), $aDp = array())
  680. {
  681. $iStart = $iPerView = 0;
  682. switch($aBp['type']) {
  683. case BX_CMT_BROWSE_HEAD:
  684. case BX_CMT_BROWSE_POPULAR:
  685. case BX_CMT_BROWSE_CONNECTION:
  686. $iPerView = $aBp['per_view'];
  687. $iStart = $aBp['start'] + $iPerView;
  688. if($iStart >= $aBp['count'])
  689. return $sCmts;
  690. break;
  691. case BX_CMT_BROWSE_TAIL:
  692. $iPerView = $aBp['per_view'];
  693. $iStart = $aBp['start'] - $iPerView;
  694. if($iStart < 0) {
  695. $iPerView += $iStart;
  696. $iStart = 0;
  697. }
  698. if($iStart == 0 && $iPerView == 0)
  699. return $sCmts;
  700. break;
  701. }
  702. $bRoot = (int)$aBp['vparent_id'] <= 0;
  703. $sMore = BxDolTemplate::getInstance()->parseHtmlByName('comment_more.html', array(
  704. 'js_object' => $this->_sJsObjName,
  705. 'style_prefix' => $this->_sStylePrefix,
  706. 'bx_if:is_root' => array(
  707. 'condition' => $bRoot,
  708. 'content' => array()
  709. ),
  710. 'parent_id' => $aBp['vparent_id'],
  711. 'start' => $iStart,
  712. 'per_view' => $iPerView,
  713. 'title' => _t('_cmt_load_more_' . ($aBp['vparent_id'] == 0 ? 'comments' : 'replies') . '_' . $aBp['type'])
  714. ));
  715. switch($aBp['type']) {
  716. case BX_CMT_BROWSE_HEAD:
  717. case BX_CMT_BROWSE_POPULAR:
  718. case BX_CMT_BROWSE_CONNECTION:
  719. $sCmts .= $sMore;
  720. break;
  721. case BX_CMT_BROWSE_TAIL:
  722. $sCmts = $sMore . $sCmts;
  723. break;
  724. }
  725. return $sCmts;
  726. }
  727. protected function _getEmpty()
  728. {
  729. return BxDolTemplate::getInstance()->parseHtmlByName('comment_empty.html', array(
  730. 'style_prefix' => $this->_sStylePrefix,
  731. 'content' => MsgBox(_t('_Empty'))
  732. ));
  733. }
  734. protected function _getViewImagePopup()
  735. {
  736. $sViewImagePopupId = 'cmts-box-' . $this->_sSystem . '-' . $this->getId() . '-view-image-popup' ;
  737. $sViewImagePopupContent = BxDolTemplate::getInstance()->parseHtmlByName('popup_image.html', array(
  738. 'image_url' => ''
  739. ));
  740. return BxTemplFunctions::getInstance()->transBox($sViewImagePopupId, $sViewImagePopupContent, true);
  741. }
  742. protected function _getAttachments($aCmt)
  743. {
  744. $aTmplImages = array();
  745. if(!$this->isAttachImageEnabled())
  746. return '';
  747. $aFiles = $this->_oQuery->getFiles($this->_aSystem['system_id'], $aCmt['cmt_id']);
  748. if(!empty($aFiles) && is_array($aFiles)) {
  749. $oStorage = BxDolStorage::getObjectInstance($this->getStorageObjectName());
  750. $oTranscoder = BxDolTranscoderImage::getObjectInstance($this->getTranscoderPreviewName());
  751. foreach($aFiles as $aFile) {
  752. $bImage = $oTranscoder && $oTranscoder->isMimeTypeSupported($aFile['mime_type']);
  753. $sPreview = '';
  754. if($oTranscoder && $bImage)
  755. $sPreview = $oTranscoder->getFileUrl($aFile['image_id']);
  756. if(!$sPreview)
  757. $sPreview = $this->_oTemplate->getIconUrl($oStorage->getIconNameByFileName($aFile['file_name']));
  758. $aTmplVarsFile = array(
  759. 'js_object' => $this->_sJsObjName,
  760. 'preview' => $sPreview,
  761. 'file' => $oStorage->getFileUrlById($aFile['image_id']),
  762. 'file_name' => $aFile['file_name'],
  763. 'file_icon' => $oStorage->getFontIconNameByFileName($aFile['file_name']),
  764. 'file_size' => _t_format_size($aFile['size']),
  765. );
  766. $aTmplImages[] = array(
  767. 'style_prefix' => $this->_sStylePrefix,
  768. 'bx_if:show_image' => array(
  769. 'condition' => $bImage,
  770. 'content' => $aTmplVarsFile
  771. ),
  772. 'bx_if:show_file' => array(
  773. 'condition' => !$bImage,
  774. 'content' => $aTmplVarsFile
  775. ),
  776. );
  777. }
  778. }
  779. return $this->_oTemplate->parseHtmlByName('comment_attachments.html', array(
  780. 'bx_repeat:attached' => $aTmplImages
  781. ));
  782. }
  783. protected function _getTmplVarsAuthor($aCmt)
  784. {
  785. list($sAuthorName, $sAuthorLink, $sAuthorIcon, $sAuthorUnit) = $this->_getAuthorInfo($aCmt['cmt_author_id']);
  786. $bAuthorIcon = !empty($sAuthorIcon);
  787. return array(
  788. 'author_unit' => $sAuthorUnit,
  789. 'bx_if:show_icon' => array(
  790. 'condition' => $bAuthorIcon,
  791. 'content' => array(
  792. 'author_icon' => $sAuthorIcon
  793. )
  794. ),
  795. 'bx_if:show_icon_empty' => array(
  796. 'condition' => !$bAuthorIcon,
  797. 'content' => array()
  798. ),
  799. 'bx_if:show_author_link' => array(
  800. 'condition' => !empty($sAuthorLink),
  801. 'content' => array(
  802. 'author_link' => $sAuthorLink,
  803. 'author_title' => bx_html_attribute($sAuthorName),
  804. 'author_name' => $sAuthorName
  805. )
  806. ),
  807. 'bx_if:show_author_text' => array(
  808. 'condition' => empty($sAuthorLink),
  809. 'content' => array(
  810. 'author_name' => $sAuthorName
  811. )
  812. ),
  813. );
  814. }
  815. protected function _getTmplVarsText($aCmt)
  816. {
  817. $sText = $aCmt['cmt_text'];
  818. $sTextMore = '';
  819. if(!$this->isHtml()) {
  820. $iMaxLength = (int)$this->_aSystem['chars_display_max'];
  821. if(strlen($sText) > $iMaxLength) {
  822. $iLength = strpos($sText, ' ', $iMaxLength);
  823. $sTextMore = trim(substr($sText, $iLength));
  824. $sText = trim(substr($sText, 0, $iLength));
  825. }
  826. }
  827. $sText = $this->_prepareTextForOutput($sText, $aCmt['cmt_id']);
  828. $sTextMore = $this->_prepareTextForOutput($sTextMore, $aCmt['cmt_id']);
  829. return array(
  830. 'text' => $sText,
  831. 'bx_if:show_more' => array(
  832. 'condition' => !empty($sTextMore),
  833. 'content' => array(
  834. 'style_prefix' => $this->_sStylePrefix,
  835. 'js_object' => $this->_sJsObjName,
  836. 'text_more' => $sTextMore
  837. )
  838. ),
  839. );
  840. }
  841. }
  842. /** @} */