BxBaseCmts.php 33 KB

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