BxBaseCmts.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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. $sAgo = bx_time_js($aCmt['cmt_time']);
  216. $bObjectTitle = !empty($this->_aSystem['trigger_field_title']);
  217. return $this->_oTemplate->parseHtmlByName('comment.html', array_merge(array(
  218. 'system' => $this->_sSystem,
  219. 'style_prefix' => $this->_sStylePrefix,
  220. 'js_object' => $this->_sJsObjName,
  221. 'id' => $aCmt['cmt_id'],
  222. 'anchor' => $this->getItemAnchor($aCmt['cmt_id']),
  223. 'class' => $sClass,
  224. 'class_cnt' => $sClassCnt,
  225. 'bx_if:show_reply_to' => array(
  226. 'condition' => !empty($aTmplReplyTo),
  227. 'content' => $aTmplReplyTo
  228. ),
  229. 'bx_if:show_ago_link' => array(
  230. 'condition' => $bObjectTitle,
  231. 'content' => array(
  232. 'style_prefix' => $this->_sStylePrefix,
  233. 'view_link' => $this->getViewUrl($aCmt['cmt_id']),
  234. 'ago' => $sAgo
  235. )
  236. ),
  237. 'bx_if:show_ago_text' => array(
  238. 'condition' => !$bObjectTitle,
  239. 'content' => array(
  240. 'ago' => $sAgo
  241. )
  242. ),
  243. 'bx_if:show_attached' => array(
  244. 'condition' => !empty($sAttachments),
  245. 'content' => array(
  246. 'style_prefix' => $this->_sStylePrefix,
  247. 'attached' => $sAttachments
  248. )
  249. ),
  250. 'actions' => $sActions,
  251. 'replies' => $sReplies
  252. ), $this->_getTmplVarsAuthor($aCmt), $this->_getTmplVarsText($aCmt)));
  253. }
  254. function getCommentSearch($iCmtId, &$sAddon)
  255. {
  256. if(empty($sAddon))
  257. $sAddon = $this->getJsScript();
  258. $sComment = $this->getComment($iCmtId, array(), array(
  259. 'type' => BX_CMT_DISPLAY_FLAT,
  260. 'view_only' => true
  261. ));
  262. return BxDolTemplate::getInstance()->parseHtmlByName('comment_search.html', array(
  263. 'comment' => $sComment,
  264. 'view_image_popup' => $this->_getViewImagePopup(),
  265. ));
  266. }
  267. /**
  268. * get one comment for "Live Search"
  269. *
  270. * @param int $iCmtId - comment id
  271. * @return string
  272. */
  273. function getCommentLiveSearch($mixedCmt, $aParams = array())
  274. {
  275. $aCmt = !is_array($mixedCmt) ? $this->getCommentRow((int)$mixedCmt) : $mixedCmt;
  276. list($sAuthorName, $sAuthorLink, $sAuthorIcon) = $this->_getAuthorInfo($aCmt['cmt_author_id']);
  277. $bAuthorIcon = !empty($sAuthorIcon);
  278. $sViewLink = $this->getViewUrl($aCmt['cmt_id']);
  279. if(empty($sViewLink))
  280. $sViewLink = '#';
  281. return BxDolTemplate::getInstance()->parseHtmlByName('comment_live_search.html', array(
  282. 'bx_if:show_icon' => array(
  283. 'condition' => $bAuthorIcon,
  284. 'content' => array(
  285. 'author_icon' => $sAuthorIcon,
  286. 'view_link' => $sViewLink
  287. )
  288. ),
  289. 'bx_if:show_icon_empty' => array(
  290. 'condition' => !$bAuthorIcon,
  291. 'content' => array()
  292. ),
  293. 'view_link' => $sViewLink,
  294. 'text' => BxTemplFunctions::getInstance()->getStringWithLimitedLength(strip_tags($aCmt['cmt_text']), $this->_sSnippetLenthLiveSearch),
  295. 'sample' => isset($aParams['txt_sample_single']) ? _t($aParams['txt_sample_single']) : ''
  296. ));
  297. }
  298. function getFormBoxPost($aBp = array(), $aDp = array())
  299. {
  300. if(!$this->isPostReplyAllowed())
  301. return '';
  302. return $this->_getFormBox(BX_CMT_ACTION_POST, $aBp, $aDp);
  303. }
  304. function getFormBoxEdit($aBp = array(), $aDp = array())
  305. {
  306. return $this->_getFormBox(BX_CMT_ACTION_EDIT, $aBp, $aDp);
  307. }
  308. function getFormPost($iCmtParentId = 0, $aDp = array())
  309. {
  310. return $this->_getFormPost($iCmtParentId, $aDp);
  311. }
  312. function getFormEdit($iCmtId, $aDp = array())
  313. {
  314. return $this->_getFormEdit($iCmtId, $aDp);
  315. }
  316. function getNotification($iCountOld = 0, $iCountNew = 0)
  317. {
  318. $iCount = (int)$iCountNew - (int)$iCountOld;
  319. if($iCount < 0)
  320. return '';
  321. $aComments = $this->_oQuery->getCommentsBy(array('type' => 'latest', 'object_id' => $this->_iId, 'author' => $this->_getAuthorId(), 'others' => 1, 'start' => '0', 'per_page' => $iCount));
  322. if(empty($aComments) || !is_array($aComments))
  323. return '';
  324. $sJsObject = $this->getJsObjectName();
  325. $oTemplate = BxDolTemplate::getInstance();
  326. $aComments = array_reverse($aComments);
  327. $iComments = count($aComments);
  328. $aTmplVarsNotifs = array();
  329. foreach($aComments as $iIndex => $aComment) {
  330. $iCommentId = $aComment['cmt_id'];
  331. $sShowOnClick = "javascript:" . $sJsObject . ".goTo(this, '" . $this->_sSystem . $iCommentId . "', '" . $iCommentId . "');";
  332. $sReplyOnClick = "javascript:" . $sJsObject . ".goToAndReply(this, '" . $this->_sSystem . $iCommentId . "', '" . $iCommentId . "');";
  333. $aTmplVarsNotifs[] = array(
  334. 'bx_if:show_as_hidden' => array(
  335. 'condition' => $iIndex < ($iComments - 1),
  336. 'content' => array(),
  337. ),
  338. 'item' => $oTemplate->parseHtmlByName('comments_notification.html', array_merge(array(
  339. 'style_prefix' => $this->_sStylePrefix,
  340. 'onclick_show' => $sShowOnClick,
  341. 'onclick_reply' => $sReplyOnClick,
  342. ), $this->_getTmplVarsAuthor($aComment), $this->_getTmplVarsText($aComment))),
  343. 'bx_if:show_previous' => array(
  344. 'condition' => $iIndex > 0,
  345. 'content' => array(
  346. 'onclick_previous' => $sJsObject . '.previousLiveUpdate(this)'
  347. )
  348. ),
  349. 'onclick_close' => $sJsObject . '.hideLiveUpdate(this)'
  350. );
  351. }
  352. return $oTemplate->parseHtmlByName('popup_chain.html', array(
  353. 'html_id' => $this->getNotificationId(),
  354. 'bx_repeat:items' => $aTmplVarsNotifs
  355. ));
  356. }
  357. public function getElementBlock($aParams = array())
  358. {
  359. $aParams['usage'] = BX_CMT_USAGE_BLOCK;
  360. return $this->getElement($aParams);
  361. }
  362. public function getElementInline($aParams = array())
  363. {
  364. $aParams['usage'] = BX_CMT_USAGE_INLINE;
  365. return $this->getElement($aParams);
  366. }
  367. public function getElement($aParams = array())
  368. {
  369. $aParams = array_merge($this->_aElementDefaults, $aParams);
  370. $bShowDoCommentAsButtonSmall = isset($aParams['show_do_comment_as_button_small']) && $aParams['show_do_comment_as_button_small'] == true;
  371. $bShowDoCommentAsButton = !$bShowDoCommentAsButtonSmall && isset($aParams['show_do_comment_as_button']) && $aParams['show_do_comment_as_button'] == true;
  372. $bShowCounterEmpty = isset($aParams['show_counter_empty']) && $aParams['show_counter_empty'] == true;
  373. $iObjectId = $this->getId();
  374. $iAuthorId = $this->_getAuthorId();
  375. $iAuthorIp = $this->_getAuthorIp();
  376. $iCount = $this->getCommentsCountAll();
  377. $bCount = (int)$iCount != 0;
  378. $isAllowedComment = $this->isPostReplyAllowed();
  379. //--- Do Comment
  380. $bTmplVarsDoComment = $this->_isShowDoComment($aParams, $isAllowedComment, $bCount);
  381. $aTmplVarsDoComment = array();
  382. if($bTmplVarsDoComment) {
  383. $sClass = '';
  384. if($bShowDoCommentAsButton)
  385. $sClass = 'bx-btn';
  386. else if ($bShowDoCommentAsButtonSmall)
  387. $sClass = 'bx-btn bx-btn-small';
  388. if(!$isAllowedComment)
  389. $sClass .= $bShowDoCommentAsButton || $bShowDoCommentAsButtonSmall ? ' bx-btn-disabled' : 'bx-cmts-disabled';
  390. $aTmplVarsDoComment = array(
  391. 'style_prefix' => $this->_sStylePrefix,
  392. 'do_comment' => $this->_oTemplate->parseLink($this->getListUrl(), $this->_getLabelDo($aParams), array(
  393. 'class' => $this->_sStylePrefix . '-do-comment ' . $this->_sStylePrefix . '-dc ' . $sClass,
  394. 'title' => _t($this->_getTitleDo())
  395. )),
  396. );
  397. }
  398. //--- Counter
  399. $bTmplVarsCounter = $this->_isShowCounter($aParams, $isAllowedComment, $bCount);
  400. $aTmplVarsCounter = array();
  401. if($bTmplVarsCounter)
  402. $aTmplVarsCounter = array(
  403. 'style_prefix' => $this->_sStylePrefix,
  404. 'bx_if:show_hidden' => array(
  405. 'condition' => !$bShowCounterEmpty && !$bCount,
  406. 'content' => array()
  407. ),
  408. 'counter' => $this->getCounter($aParams)
  409. );
  410. if(!$bTmplVarsDoComment && !$bTmplVarsCounter)
  411. return '';
  412. $sTmplName = $this->{'_getTmplElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_SCORE_USAGE_DEFAULT)}();
  413. return $this->_oTemplate->parseHtmlByContent($sTmplName, array(
  414. 'style_prefix' => $this->_sStylePrefix,
  415. 'html_id' => $this->_aHtmlIds['main'],
  416. 'class' => $this->_sStylePrefix . ($bShowDoCommentAsButton ? '-button' : '') . ($bShowDoCommentAsButtonSmall ? '-button-small' : ''),
  417. 'count' => $iCount,
  418. 'bx_if:show_do_comment' => array(
  419. 'condition' => $bTmplVarsDoComment,
  420. 'content' => $aTmplVarsDoComment
  421. ),
  422. 'bx_if:show_counter' => array(
  423. 'condition' => $bTmplVarsCounter,
  424. 'content' => $aTmplVarsCounter
  425. ),
  426. 'script' => ''
  427. ));
  428. }
  429. public function getCounter($aParams = array())
  430. {
  431. $bShowEmpty = isset($aParams['show_counter_empty']) && $aParams['show_counter_empty'] == true;
  432. $bShowDoCommentAsButtonSmall = isset($aParams['show_do_comment_as_button_small']) && $aParams['show_do_comment_as_button_small'] == true;
  433. $bShowDoCommentAsButton = !$bShowDoCommentAsButtonSmall && isset($aParams['show_do_comment_as_button']) && $aParams['show_do_comment_as_button'] == true;
  434. $iCount = (int)$this->getCommentsCountAll();
  435. $sClass = $this->_sStylePrefix . '-counter';
  436. if($bShowDoCommentAsButtonSmall)
  437. $sClass .= ' bx-btn-small-height';
  438. if($bShowDoCommentAsButton)
  439. $sClass .= ' bx-btn-height';
  440. return $this->_oTemplate->parseHtmlByContent($this->_getTmplCounter(), array(
  441. 'id' => $this->_aHtmlIds['counter'],
  442. 'class' => $sClass,
  443. 'content' => $iCount != 0 || $bShowEmpty ? $this->_getLabelCounter($iCount) : ''
  444. ));
  445. }
  446. protected function _getLabelDo($aParams = array())
  447. {
  448. return $this->_oTemplate->parseHtmlByContent($this->_getTmplLabelDo(), array(
  449. 'bx_if:show_image' => array(
  450. 'condition' => isset($aParams['show_do_comment_image']) && $aParams['show_do_comment_image'] == true,
  451. 'content' => array(
  452. 'src' => $this->_getImageDo()
  453. )
  454. ),
  455. 'bx_if:show_icon' => array(
  456. 'condition' => isset($aParams['show_do_comment_icon']) && $aParams['show_do_comment_icon'] == true,
  457. 'content' => array(
  458. 'name' => $this->_getIconDo()
  459. )
  460. ),
  461. 'bx_if:show_text' => array(
  462. 'condition' => isset($aParams['show_do_comment_label']) && $aParams['show_do_comment_label'] == true,
  463. 'content' => array(
  464. 'text' => _t($this->_getTitleDo())
  465. )
  466. )
  467. ));
  468. }
  469. protected function _getLabelCounter($iCount)
  470. {
  471. return (int)$iCount != 0 ? _t('_cmt_txt_counter', $iCount) : _t('_cmt_txt_counter_empty');
  472. }
  473. protected function _getTmplElementBlock()
  474. {
  475. return self::$_sTmplContentElementBlock;
  476. }
  477. protected function _getTmplElementInline()
  478. {
  479. return self::$_sTmplContentElementInline;
  480. }
  481. protected function _getTmplLabelDo()
  482. {
  483. return self::$_sTmplContentDoCommentLabel;
  484. }
  485. protected function _getTmplCounter()
  486. {
  487. return self::$_sTmplContentCounter;
  488. }
  489. /**
  490. * private functions
  491. */
  492. protected function _getControlsBox()
  493. {
  494. $sDisplay = '';
  495. $bDisplay = (int)$this->_aSystem['is_display_switch'] == 1;
  496. if($bDisplay) {
  497. $aDisplayLinks = array(
  498. array('id' => $this->_sSystem . '-flat', 'name' => $this->_sSystem . '-flat', 'class' => '', 'title' => '_cmt_display_flat', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeDisplay(this, \'flat\');'),
  499. array('id' => $this->_sSystem . '-threaded', 'name' => $this->_sSystem . '-threaded', 'class' => '', 'title' => '_cmt_display_threaded', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeDisplay(this, \'threaded\');')
  500. );
  501. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-display', 'menu_items' => $aDisplayLinks));
  502. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sDisplayType);
  503. $sDisplay = $oMenu->getCode();
  504. }
  505. $sBrowseType = '';
  506. $bBrowseType = (int)$this->_aSystem['is_browse_switch'] == 1;
  507. if($bBrowseType) {
  508. $aBrowseLinks = array(
  509. array('id' => $this->_sSystem . '-tail', 'name' => $this->_sSystem . '-tail', 'class' => '', 'title' => '_cmt_browse_tail', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'tail\');'),
  510. array('id' => $this->_sSystem . '-head', 'name' => $this->_sSystem . '-head', 'class' => '', 'title' => '_cmt_browse_head', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'head\');'),
  511. array('id' => $this->_sSystem . '-popular', 'name' => $this->_sSystem . '-popular', 'class' => '', 'title' => '_cmt_browse_popular', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'popular\');'),
  512. );
  513. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-browse', 'menu_items' => $aBrowseLinks));
  514. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sBrowseType);
  515. $sBrowseType = $oMenu->getCode();
  516. }
  517. $sBrowseFilter = '';
  518. $bBrowseFilter = (int)$this->_aSystem['is_browse_filter'] == 1;
  519. if($bBrowseFilter) {
  520. $aFilterLinks = array(
  521. array('id' => $this->_sSystem . '-all', 'name' => $this->_sSystem . '-all', 'class' => '', 'title' => '_cmt_browse_all', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'all\');'),
  522. array('id' => $this->_sSystem . '-friends', 'name' => $this->_sSystem . '-friends', 'class' => '', 'title' => '_cmt_browse_friends', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'friends\');'),
  523. array('id' => $this->_sSystem . '-subscriptions', 'name' => $this->_sSystem . '-subscriptions', 'class' => '', 'title' => '_cmt_browse_subscriptions', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'subscriptions\');')
  524. );
  525. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-filter', 'menu_items' => $aFilterLinks));
  526. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sBrowseFilter);
  527. $sBrowseFilter = $oMenu->getCode();
  528. }
  529. return $this->_oTemplate->parseHtmlByName('comments_controls.html', array(
  530. 'display_switcher' => $bDisplay ? $sDisplay : '',
  531. 'bx_if:is_divider_1' => array(
  532. 'condition' => $bDisplay && $bBrowseType,
  533. 'content' => array(
  534. 'style_prefix' => $this->_sStylePrefix,
  535. )
  536. ),
  537. 'browse_switcher' => $bBrowseType ? $sBrowseType : '',
  538. 'bx_if:is_divider_2' => array(
  539. 'condition' => $bBrowseType && $bBrowseFilter,
  540. 'content' => array(
  541. 'style_prefix' => $this->_sStylePrefix,
  542. )
  543. ),
  544. 'filter_switcher' => $bBrowseFilter ? $sBrowseFilter : '',
  545. ));
  546. }
  547. protected function _getActionsBox(&$aCmt, $aDp = array())
  548. {
  549. $bViewOnly = isset($aDp['view_only']) && $aDp['view_only'] === true;
  550. $bDynamicMode = isset($aDp['dynamic_mode']) && $aDp['dynamic_mode'] === true;
  551. $sMenuActions = '';
  552. if(!$bViewOnly) {
  553. $oMenuActions = BxDolMenu::getObjectInstance($this->_sMenuObjActions);
  554. $oMenuActions->setCmtsData($this, $aCmt['cmt_id']);
  555. $oMenuActions->setDynamicMode($bDynamicMode);
  556. $sMenuActions = $oMenuActions->getCode();
  557. }
  558. return $this->_oTemplate->parseHtmlByName('comment_actions.html', array(
  559. 'id' => $aCmt['cmt_id'],
  560. 'js_object' => $this->_sJsObjName,
  561. 'style_prefix' => $this->_sStylePrefix,
  562. 'menu_actions' => $sMenuActions,
  563. ));
  564. }
  565. protected function _getFormBox($sType, $aBp, $aDp)
  566. {
  567. $iCmtParentId = isset($aBp['parent_id']) ? (int)$aBp['parent_id'] : 0;
  568. $sPosition = isset($aDp['position']) ? $aDp['position'] : '';
  569. $sPositionSystem = $this->_aSystem['post_form_position'];
  570. if(!empty($sPosition) && $sPositionSystem != $sPosition)
  571. return '';
  572. $sMethod = '_getForm' . ucfirst($sType);
  573. $aForm = $this->$sMethod($iCmtParentId, $aDp);
  574. return BxDolTemplate::getInstance()->parseHtmlByName('comment_reply_box.html', array(
  575. 'js_object' => $this->_sJsObjName,
  576. 'style_prefix' => $this->_sStylePrefix,
  577. 'bx_if:show_class' => array(
  578. 'condition' => !empty($sPosition),
  579. 'content' => array(
  580. 'class' => $this->_sStylePrefix . '-reply-' . $sPosition
  581. )
  582. ),
  583. 'form' => $aForm['form'],
  584. 'form_id' => $aForm['form_id'],
  585. ));
  586. }
  587. protected function _getFormPost($iCmtParentId = 0, $aDp = array())
  588. {
  589. $bDynamic = isset($aDp['dynamic_mode']) && (bool)$aDp['dynamic_mode'];
  590. $oForm = $this->_getForm(BX_CMT_ACTION_POST, $iCmtParentId);
  591. $oForm->aInputs['cmt_parent_id']['value'] = $iCmtParentId;
  592. $oForm->initChecker();
  593. if($oForm->isSubmittedAndValid()) {
  594. $iCmtAuthorId = $this->_getAuthorId();
  595. $iCmtParentId = $oForm->getCleanValue('cmt_parent_id');
  596. $sCmtText = $oForm->getCleanValue('cmt_text');
  597. $iLevel = 0;
  598. $iCmtVisualParentId = 0;
  599. if((int)$iCmtParentId > 0) {
  600. $aParent = $this->getCommentRow($iCmtParentId);
  601. $iLevel = (int)$aParent['cmt_level'] + 1;
  602. $iCmtVisualParentId = $iLevel > $this->getMaxLevel() ? $aParent['cmt_vparent_id'] : $iCmtParentId;
  603. }
  604. $iCmtId = (int)$oForm->insert(array('cmt_vparent_id' => $iCmtVisualParentId, 'cmt_object_id' => $this->_iId, 'cmt_author_id' => $iCmtAuthorId, 'cmt_level' => $iLevel, 'cmt_time' => time()));
  605. if($iCmtId != 0) {
  606. if($this->isAttachImageEnabled()) {
  607. $aImages = $oForm->getCleanValue('cmt_image');
  608. if(!empty($aImages) && is_array($aImages)) {
  609. $oStorage = BxDolStorage::getObjectInstance($this->getStorageObjectName());
  610. foreach($aImages as $iImageId)
  611. if($this->_oQuery->saveImages($this->_aSystem['system_id'], $iCmtId, $iImageId))
  612. $oStorage->afterUploadCleanup($iImageId, $iCmtAuthorId);
  613. }
  614. }
  615. if($iCmtParentId) {
  616. $this->_oQuery->updateRepliesCount($iCmtParentId, 1);
  617. if(!BxDolModuleQuery::getInstance()->isEnabledByName('bx_notifications'))
  618. $this->_sendNotificationEmail($iCmtId, $iCmtParentId);
  619. }
  620. $this->_triggerComment();
  621. $this->isPostReplyAllowed(true);
  622. if ($this->_sMetatagsObj) {
  623. $oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj);
  624. $oMetatags->metaAdd($this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId), $sCmtText);
  625. }
  626. $this->onPostAfter($iCmtId);
  627. return array('id' => $iCmtId, 'parent_id' => $iCmtParentId);
  628. }
  629. return array('msg' => _t('_cmt_err_cannot_perform_action'));
  630. }
  631. return array('form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id);
  632. }
  633. protected function _getFormEdit($iCmtId, $aDp = array())
  634. {
  635. $bDynamic = isset($aDp['dynamic_mode']) && (bool)$aDp['dynamic_mode'];
  636. $aCmt = $this->_oQuery->getCommentSimple ((int)$this->getId(), $iCmtId);
  637. if(!$aCmt)
  638. return array('msg' => _t('_No such comment'));
  639. $iCmtAuthorId = $this->_getAuthorId();
  640. if(!$this->isEditAllowed($aCmt))
  641. return array('msg' => $aCmt['cmt_author_id'] == $iCmtAuthorId ? strip_tags($this->msgErrEditAllowed()) : _t('_Access denied'));
  642. $oForm = $this->_getForm(BX_CMT_ACTION_EDIT, $aCmt['cmt_id']);
  643. $oForm->initChecker($aCmt);
  644. if($oForm->isSubmittedAndValid()) {
  645. $sCmtText = $oForm->getCleanValue('cmt_text');
  646. if($oForm->update($iCmtId) !== false) {
  647. $this->isEditAllowed($aCmt, true);
  648. if ($this->_sMetatagsObj) {
  649. $oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj);
  650. $oMetatags->metaAdd($this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId), $sCmtText);
  651. }
  652. $this->onEditAfter($iCmtId);
  653. return array('id' => $iCmtId, 'text' => $this->_prepareTextForOutput($sCmtText, $iCmtId));
  654. }
  655. return array('msg' => _t('_cmt_err_cannot_perform_action'));
  656. }
  657. return array('form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id);
  658. }
  659. protected function _getForm($sAction, $iId)
  660. {
  661. $oForm = $this->_getFormObject($sAction);
  662. $oForm->setId(sprintf($oForm->getAttributeMask('id'), $sAction, $this->_sSystem, $iId));
  663. $oForm->setName(sprintf($oForm->getAttributeMask('name'), $sAction, $this->_sSystem, $iId));
  664. $oForm->aParams['db']['table'] = $this->_aSystem['table'];
  665. $oForm->aInputs['sys']['value'] = $this->_sSystem;
  666. $oForm->aInputs['id']['value'] = $this->_iId;
  667. $oForm->aInputs['action']['value'] = 'Submit' . ucfirst($sAction) . 'Form';
  668. if(!$this->isAttachImageEnabled())
  669. unset($oForm->aInputs['cmt_image']);
  670. if(isset($oForm->aInputs['cmt_text'])) {
  671. $oForm->aInputs['cmt_text']['html'] = $this->_aSystem['html'];
  672. $oForm->aInputs['cmt_text']['db']['pass'] = $this->isHtml() ? 'XssHtml' : 'XssMultiline';
  673. $iCmtTextMin = (int)$this->_aSystem['chars_post_min'];
  674. $iCmtTextMax = (int)$this->_aSystem['chars_post_max'];
  675. $oForm->aInputs['cmt_text']['checker']['params'] = array($iCmtTextMin, $iCmtTextMax);
  676. $oForm->aInputs['cmt_text']['checker']['error'] = _t('_Please enter n1-n2 characters', $iCmtTextMin, $iCmtTextMax);
  677. }
  678. return $oForm;
  679. }
  680. protected function _getMoreLink($sCmts, $aBp = array(), $aDp = array())
  681. {
  682. $iStart = $iPerView = 0;
  683. switch($aBp['type']) {
  684. case BX_CMT_BROWSE_HEAD:
  685. case BX_CMT_BROWSE_POPULAR:
  686. case BX_CMT_BROWSE_CONNECTION:
  687. $iPerView = $aBp['per_view'];
  688. $iStart = $aBp['start'] + $iPerView;
  689. if($iStart >= $aBp['count'])
  690. return $sCmts;
  691. break;
  692. case BX_CMT_BROWSE_TAIL:
  693. $iPerView = $aBp['per_view'];
  694. $iStart = $aBp['start'] - $iPerView;
  695. if($iStart < 0) {
  696. $iPerView += $iStart;
  697. $iStart = 0;
  698. }
  699. if($iStart == 0 && $iPerView == 0)
  700. return $sCmts;
  701. break;
  702. }
  703. $bRoot = (int)$aBp['vparent_id'] <= 0;
  704. $sMore = BxDolTemplate::getInstance()->parseHtmlByName('comment_more.html', array(
  705. 'js_object' => $this->_sJsObjName,
  706. 'style_prefix' => $this->_sStylePrefix,
  707. 'bx_if:is_root' => array(
  708. 'condition' => $bRoot,
  709. 'content' => array()
  710. ),
  711. 'parent_id' => $aBp['vparent_id'],
  712. 'start' => $iStart,
  713. 'per_view' => $iPerView,
  714. 'title' => _t('_cmt_load_more_' . ($aBp['vparent_id'] == 0 ? 'comments' : 'replies') . '_' . $aBp['type'])
  715. ));
  716. switch($aBp['type']) {
  717. case BX_CMT_BROWSE_HEAD:
  718. case BX_CMT_BROWSE_POPULAR:
  719. case BX_CMT_BROWSE_CONNECTION:
  720. $sCmts .= $sMore;
  721. break;
  722. case BX_CMT_BROWSE_TAIL:
  723. $sCmts = $sMore . $sCmts;
  724. break;
  725. }
  726. return $sCmts;
  727. }
  728. protected function _getEmpty()
  729. {
  730. return BxDolTemplate::getInstance()->parseHtmlByName('comment_empty.html', array(
  731. 'style_prefix' => $this->_sStylePrefix,
  732. 'content' => MsgBox(_t('_Empty'))
  733. ));
  734. }
  735. protected function _getViewImagePopup()
  736. {
  737. $sViewImagePopupId = 'cmts-box-' . $this->_sSystem . '-' . $this->getId() . '-view-image-popup' ;
  738. $sViewImagePopupContent = BxDolTemplate::getInstance()->parseHtmlByName('popup_image.html', array(
  739. 'image_url' => ''
  740. ));
  741. return BxTemplFunctions::getInstance()->transBox($sViewImagePopupId, $sViewImagePopupContent, true);
  742. }
  743. protected function _getAttachments($aCmt)
  744. {
  745. $aTmplImages = array();
  746. if(!$this->isAttachImageEnabled())
  747. return '';
  748. $aFiles = $this->_oQuery->getFiles($this->_aSystem['system_id'], $aCmt['cmt_id']);
  749. if(!empty($aFiles) && is_array($aFiles)) {
  750. $oStorage = BxDolStorage::getObjectInstance($this->getStorageObjectName());
  751. $oTranscoder = BxDolTranscoderImage::getObjectInstance($this->getTranscoderPreviewName());
  752. foreach($aFiles as $aFile) {
  753. $bImage = $oTranscoder && $oTranscoder->isMimeTypeSupported($aFile['mime_type']);
  754. $sPreview = '';
  755. if($oTranscoder && $bImage)
  756. $sPreview = $oTranscoder->getFileUrl($aFile['image_id']);
  757. if(!$sPreview)
  758. $sPreview = $this->_oTemplate->getIconUrl($oStorage->getIconNameByFileName($aFile['file_name']));
  759. $aTmplVarsFile = array(
  760. 'js_object' => $this->_sJsObjName,
  761. 'preview' => $sPreview,
  762. 'file' => $oStorage->getFileUrlById($aFile['image_id']),
  763. 'file_name' => $aFile['file_name'],
  764. 'file_icon' => $oStorage->getFontIconNameByFileName($aFile['file_name']),
  765. 'file_size' => _t_format_size($aFile['size']),
  766. );
  767. $aTmplImages[] = array(
  768. 'style_prefix' => $this->_sStylePrefix,
  769. 'bx_if:show_image' => array(
  770. 'condition' => $bImage,
  771. 'content' => $aTmplVarsFile
  772. ),
  773. 'bx_if:show_file' => array(
  774. 'condition' => !$bImage,
  775. 'content' => $aTmplVarsFile
  776. ),
  777. );
  778. }
  779. }
  780. return $this->_oTemplate->parseHtmlByName('comment_attachments.html', array(
  781. 'bx_repeat:attached' => $aTmplImages
  782. ));
  783. }
  784. protected function _getTmplVarsAuthor($aCmt)
  785. {
  786. list($sAuthorName, $sAuthorLink, $sAuthorIcon, $sAuthorUnit) = $this->_getAuthorInfo($aCmt['cmt_author_id']);
  787. $bAuthorIcon = !empty($sAuthorIcon);
  788. return array(
  789. 'author_unit' => $sAuthorUnit,
  790. 'bx_if:show_icon' => array(
  791. 'condition' => $bAuthorIcon,
  792. 'content' => array(
  793. 'author_icon' => $sAuthorIcon
  794. )
  795. ),
  796. 'bx_if:show_icon_empty' => array(
  797. 'condition' => !$bAuthorIcon,
  798. 'content' => array()
  799. ),
  800. 'bx_if:show_author_link' => array(
  801. 'condition' => !empty($sAuthorLink),
  802. 'content' => array(
  803. 'author_link' => $sAuthorLink,
  804. 'author_title' => bx_html_attribute($sAuthorName),
  805. 'author_name' => $sAuthorName
  806. )
  807. ),
  808. 'bx_if:show_author_text' => array(
  809. 'condition' => empty($sAuthorLink),
  810. 'content' => array(
  811. 'author_name' => $sAuthorName
  812. )
  813. ),
  814. );
  815. }
  816. protected function _getTmplVarsText($aCmt)
  817. {
  818. $sText = $aCmt['cmt_text'];
  819. $sTextMore = '';
  820. if(!$this->isHtml()) {
  821. $iMaxLength = (int)$this->_aSystem['chars_display_max'];
  822. if(strlen($sText) > $iMaxLength) {
  823. $iLength = strpos($sText, ' ', $iMaxLength);
  824. $sTextMore = trim(substr($sText, $iLength));
  825. $sText = trim(substr($sText, 0, $iLength));
  826. }
  827. }
  828. $sText = $this->_prepareTextForOutput($sText, $aCmt['cmt_id']);
  829. $sTextMore = $this->_prepareTextForOutput($sTextMore, $aCmt['cmt_id']);
  830. return array(
  831. 'text' => $sText,
  832. 'bx_if:show_more' => array(
  833. 'condition' => !empty($sTextMore),
  834. 'content' => array(
  835. 'style_prefix' => $this->_sStylePrefix,
  836. 'js_object' => $this->_sJsObjName,
  837. 'text_more' => $sTextMore
  838. )
  839. ),
  840. );
  841. }
  842. }
  843. /** @} */