BxBaseCmts.php 31 KB

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