BxBaseCmts.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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. function getCommentSearch($iCmtId, &$sAddon)
  227. {
  228. if(empty($sAddon))
  229. $sAddon = $this->getJsScript();
  230. $sComment = $this->getComment($iCmtId, array(), array(
  231. 'type' => BX_CMT_DISPLAY_FLAT,
  232. 'view_only' => true
  233. ));
  234. return BxDolTemplate::getInstance()->parseHtmlByName('comment_search.html', array(
  235. 'comment' => $sComment,
  236. 'view_image_popup' => $this->_getViewImagePopup(),
  237. ));
  238. }
  239. /**
  240. * get one comment for "Live Search"
  241. *
  242. * @param int $iCmtId - comment id
  243. * @return string
  244. */
  245. function getCommentLiveSearch($mixedCmt, $aParams = array())
  246. {
  247. $aCmt = !is_array($mixedCmt) ? $this->getCommentRow((int)$mixedCmt) : $mixedCmt;
  248. list($sAuthorName, $sAuthorLink, $sAuthorIcon) = $this->_getAuthorInfo($aCmt['cmt_author_id']);
  249. $bAuthorIcon = !empty($sAuthorIcon);
  250. $sViewLink = $this->getViewUrl($aCmt['cmt_id']);
  251. if(empty($sViewLink))
  252. $sViewLink = '#';
  253. return BxDolTemplate::getInstance()->parseHtmlByName('comment_live_search.html', array(
  254. 'bx_if:show_icon' => array(
  255. 'condition' => $bAuthorIcon,
  256. 'content' => array(
  257. 'author_icon' => $sAuthorIcon,
  258. 'view_link' => $sViewLink
  259. )
  260. ),
  261. 'bx_if:show_icon_empty' => array(
  262. 'condition' => !$bAuthorIcon,
  263. 'content' => array()
  264. ),
  265. 'view_link' => $sViewLink,
  266. 'text' => BxTemplFunctions::getInstance()->getStringWithLimitedLength(strip_tags($aCmt['cmt_text']), $this->_sSnippetLenthLiveSearch),
  267. 'sample' => isset($aParams['txt_sample_single']) ? _t($aParams['txt_sample_single']) : ''
  268. ));
  269. }
  270. function getFormBoxPost($aBp = array(), $aDp = array())
  271. {
  272. if(!$this->isPostReplyAllowed())
  273. return '';
  274. return $this->_getFormBox(BX_CMT_ACTION_POST, $aBp, $aDp);
  275. }
  276. function getFormBoxEdit($aBp = array(), $aDp = array())
  277. {
  278. return $this->_getFormBox(BX_CMT_ACTION_EDIT, $aBp, $aDp);
  279. }
  280. function getFormPost($iCmtParentId = 0, $aDp = array())
  281. {
  282. return $this->_getFormPost($iCmtParentId, $aDp);
  283. }
  284. function getFormEdit($iCmtId, $aDp = array())
  285. {
  286. return $this->_getFormEdit($iCmtId, $aDp);
  287. }
  288. function getNotification($iCountOld = 0, $iCountNew = 0)
  289. {
  290. $iCount = (int)$iCountNew - (int)$iCountOld;
  291. if($iCount < 0)
  292. return '';
  293. $aComments = $this->_oQuery->getCommentsBy(array('type' => 'latest', 'object_id' => $this->_iId, 'author' => $this->_getAuthorId(), 'others' => 1, 'start' => '0', 'per_page' => $iCount));
  294. if(empty($aComments) || !is_array($aComments))
  295. return '';
  296. $sJsObject = $this->getJsObjectName();
  297. $oTemplate = BxDolTemplate::getInstance();
  298. $aComments = array_reverse($aComments);
  299. $iComments = count($aComments);
  300. $aTmplVarsNotifs = array();
  301. foreach($aComments as $iIndex => $aComment) {
  302. $iCommentId = $aComment['cmt_id'];
  303. $sShowOnClick = "javascript:" . $sJsObject . ".goTo(this, '" . $this->_sSystem . $iCommentId . "', '" . $iCommentId . "');";
  304. $sReplyOnClick = "javascript:" . $sJsObject . ".goToAndReply(this, '" . $this->_sSystem . $iCommentId . "', '" . $iCommentId . "');";
  305. $aTmplVarsNotifs[] = array(
  306. 'bx_if:show_as_hidden' => array(
  307. 'condition' => $iIndex < ($iComments - 1),
  308. 'content' => array(),
  309. ),
  310. 'item' => $oTemplate->parseHtmlByName('comments_notification.html', array_merge(array(
  311. 'style_prefix' => $this->_sStylePrefix,
  312. 'onclick_show' => $sShowOnClick,
  313. 'onclick_reply' => $sReplyOnClick,
  314. ), $this->_getTmplVarsAuthor($aComment), $this->_getTmplVarsText($aComment))),
  315. 'bx_if:show_previous' => array(
  316. 'condition' => $iIndex > 0,
  317. 'content' => array(
  318. 'onclick_previous' => $sJsObject . '.previousLiveUpdate(this)'
  319. )
  320. ),
  321. 'onclick_close' => $sJsObject . '.hideLiveUpdate(this)'
  322. );
  323. }
  324. return $oTemplate->parseHtmlByName('popup_chain.html', array(
  325. 'html_id' => $this->getNotificationId(),
  326. 'bx_repeat:items' => $aTmplVarsNotifs
  327. ));
  328. }
  329. /**
  330. * private functions
  331. */
  332. protected function _getControlsBox()
  333. {
  334. $sDisplay = '';
  335. $bDisplay = (int)$this->_aSystem['is_display_switch'] == 1;
  336. if($bDisplay) {
  337. $aDisplayLinks = array(
  338. array('id' => $this->_sSystem . '-flat', 'name' => $this->_sSystem . '-flat', 'class' => '', 'title' => '_cmt_display_flat', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeDisplay(this, \'flat\');'),
  339. array('id' => $this->_sSystem . '-threaded', 'name' => $this->_sSystem . '-threaded', 'class' => '', 'title' => '_cmt_display_threaded', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeDisplay(this, \'threaded\');')
  340. );
  341. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-display', 'menu_items' => $aDisplayLinks));
  342. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sDisplayType);
  343. $sDisplay = $oMenu->getCode();
  344. }
  345. $sBrowseType = '';
  346. $bBrowseType = (int)$this->_aSystem['is_browse_switch'] == 1;
  347. if($bBrowseType) {
  348. $aBrowseLinks = array(
  349. array('id' => $this->_sSystem . '-tail', 'name' => $this->_sSystem . '-tail', 'class' => '', 'title' => '_cmt_browse_tail', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'tail\');'),
  350. array('id' => $this->_sSystem . '-head', 'name' => $this->_sSystem . '-head', 'class' => '', 'title' => '_cmt_browse_head', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'head\');'),
  351. array('id' => $this->_sSystem . '-popular', 'name' => $this->_sSystem . '-popular', 'class' => '', 'title' => '_cmt_browse_popular', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'popular\');'),
  352. );
  353. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-browse', 'menu_items' => $aBrowseLinks));
  354. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sBrowseType);
  355. $sBrowseType = $oMenu->getCode();
  356. }
  357. $sBrowseFilter = '';
  358. $bBrowseFilter = (int)$this->_aSystem['is_browse_filter'] == 1;
  359. if($bBrowseFilter) {
  360. $aFilterLinks = array(
  361. array('id' => $this->_sSystem . '-all', 'name' => $this->_sSystem . '-all', 'class' => '', 'title' => '_cmt_browse_all', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'all\');'),
  362. array('id' => $this->_sSystem . '-friends', 'name' => $this->_sSystem . '-friends', 'class' => '', 'title' => '_cmt_browse_friends', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'friends\');'),
  363. array('id' => $this->_sSystem . '-subscriptions', 'name' => $this->_sSystem . '-subscriptions', 'class' => '', 'title' => '_cmt_browse_subscriptions', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'subscriptions\');')
  364. );
  365. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-filter', 'menu_items' => $aFilterLinks));
  366. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sBrowseFilter);
  367. $sBrowseFilter = $oMenu->getCode();
  368. }
  369. return $this->_oTemplate->parseHtmlByName('comments_controls.html', array(
  370. 'display_switcher' => $bDisplay ? $sDisplay : '',
  371. 'bx_if:is_divider_1' => array(
  372. 'condition' => $bDisplay && $bBrowseType,
  373. 'content' => array(
  374. 'style_prefix' => $this->_sStylePrefix,
  375. )
  376. ),
  377. 'browse_switcher' => $bBrowseType ? $sBrowseType : '',
  378. 'bx_if:is_divider_2' => array(
  379. 'condition' => $bBrowseType && $bBrowseFilter,
  380. 'content' => array(
  381. 'style_prefix' => $this->_sStylePrefix,
  382. )
  383. ),
  384. 'filter_switcher' => $bBrowseFilter ? $sBrowseFilter : '',
  385. ));
  386. }
  387. protected function _getActionsBox(&$aCmt, $aDp = array())
  388. {
  389. $bViewOnly = isset($aDp['view_only']) && $aDp['view_only'] === true;
  390. $bDynamicMode = isset($aDp['dynamic_mode']) && $aDp['dynamic_mode'] === true;
  391. $sMenuActions = '';
  392. if(!$bViewOnly) {
  393. $oMenuActions = BxDolMenu::getObjectInstance($this->_sMenuObjActions);
  394. $oMenuActions->setCmtsData($this, $aCmt['cmt_id']);
  395. $oMenuActions->setDynamicMode($bDynamicMode);
  396. $sMenuActions = $oMenuActions->getCode();
  397. }
  398. return $this->_oTemplate->parseHtmlByName('comment_actions.html', array(
  399. 'id' => $aCmt['cmt_id'],
  400. 'js_object' => $this->_sJsObjName,
  401. 'style_prefix' => $this->_sStylePrefix,
  402. 'menu_actions' => $sMenuActions,
  403. ));
  404. }
  405. protected function _getFormBox($sType, $aBp, $aDp)
  406. {
  407. $iCmtParentId = isset($aBp['parent_id']) ? (int)$aBp['parent_id'] : 0;
  408. $sPosition = isset($aDp['position']) ? $aDp['position'] : '';
  409. $sPositionSystem = $this->_aSystem['post_form_position'];
  410. if(!empty($sPosition) && $sPositionSystem != $sPosition)
  411. return '';
  412. $sMethod = '_getForm' . ucfirst($sType);
  413. $aForm = $this->$sMethod($iCmtParentId, $aDp);
  414. return BxDolTemplate::getInstance()->parseHtmlByName('comment_reply_box.html', array(
  415. 'js_object' => $this->_sJsObjName,
  416. 'style_prefix' => $this->_sStylePrefix,
  417. 'bx_if:show_class' => array(
  418. 'condition' => !empty($sPosition),
  419. 'content' => array(
  420. 'class' => $this->_sStylePrefix . '-reply-' . $sPosition
  421. )
  422. ),
  423. 'form' => $aForm['form'],
  424. 'form_id' => $aForm['form_id'],
  425. ));
  426. }
  427. protected function _getFormPost($iCmtParentId = 0, $aDp = array())
  428. {
  429. $bDynamic = isset($aDp['dynamic_mode']) && (bool)$aDp['dynamic_mode'];
  430. $oForm = $this->_getForm(BX_CMT_ACTION_POST, $iCmtParentId);
  431. $oForm->aInputs['cmt_parent_id']['value'] = $iCmtParentId;
  432. $oForm->initChecker();
  433. if($oForm->isSubmittedAndValid()) {
  434. $iCmtAuthorId = $this->_getAuthorId();
  435. $iCmtParentId = $oForm->getCleanValue('cmt_parent_id');
  436. $sCmtText = $oForm->getCleanValue('cmt_text');
  437. $iLevel = 0;
  438. $iCmtVisualParentId = 0;
  439. if((int)$iCmtParentId > 0) {
  440. $aParent = $this->getCommentRow($iCmtParentId);
  441. $iLevel = (int)$aParent['cmt_level'] + 1;
  442. $iCmtVisualParentId = $iLevel > $this->getMaxLevel() ? $aParent['cmt_vparent_id'] : $iCmtParentId;
  443. }
  444. $iCmtId = (int)$oForm->insert(array('cmt_vparent_id' => $iCmtVisualParentId, 'cmt_object_id' => $this->_iId, 'cmt_author_id' => $iCmtAuthorId, 'cmt_level' => $iLevel, 'cmt_time' => time()));
  445. if($iCmtId != 0) {
  446. if($this->isAttachImageEnabled()) {
  447. $aImages = $oForm->getCleanValue('cmt_image');
  448. if(!empty($aImages) && is_array($aImages)) {
  449. $oStorage = BxDolStorage::getObjectInstance($this->getStorageObjectName());
  450. foreach($aImages as $iImageId)
  451. if($this->_oQuery->saveImages($this->_aSystem['system_id'], $iCmtId, $iImageId))
  452. $oStorage->afterUploadCleanup($iImageId, $iCmtAuthorId);
  453. }
  454. }
  455. if($iCmtParentId) {
  456. $this->_oQuery->updateRepliesCount($iCmtParentId, 1);
  457. if(!BxDolModuleQuery::getInstance()->isEnabledByName('bx_notifications'))
  458. $this->_sendNotificationEmail($iCmtId, $iCmtParentId);
  459. }
  460. $this->_triggerComment();
  461. $this->isPostReplyAllowed(true);
  462. if ($this->_sMetatagsObj) {
  463. $oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj);
  464. $oMetatags->metaAdd($this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId), $sCmtText);
  465. }
  466. $this->onPostAfter($iCmtId);
  467. return array('id' => $iCmtId, 'parent_id' => $iCmtParentId);
  468. }
  469. return array('msg' => _t('_cmt_err_cannot_perform_action'));
  470. }
  471. return array('form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id);
  472. }
  473. protected function _getFormEdit($iCmtId, $aDp = array())
  474. {
  475. $bDynamic = isset($aDp['dynamic_mode']) && (bool)$aDp['dynamic_mode'];
  476. $aCmt = $this->_oQuery->getCommentSimple ((int)$this->getId(), $iCmtId);
  477. if(!$aCmt)
  478. return array('msg' => _t('_No such comment'));
  479. $iCmtAuthorId = $this->_getAuthorId();
  480. if(!$this->isEditAllowed($aCmt))
  481. return array('msg' => $aCmt['cmt_author_id'] == $iCmtAuthorId ? strip_tags($this->msgErrEditAllowed()) : _t('_Access denied'));
  482. $oForm = $this->_getForm(BX_CMT_ACTION_EDIT, $aCmt['cmt_id']);
  483. $oForm->initChecker($aCmt);
  484. if($oForm->isSubmittedAndValid()) {
  485. $sCmtText = $oForm->getCleanValue('cmt_text');
  486. if($oForm->update($iCmtId) !== false) {
  487. $this->isEditAllowed($aCmt, true);
  488. if ($this->_sMetatagsObj) {
  489. $oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj);
  490. $oMetatags->metaAdd($this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId), $sCmtText);
  491. }
  492. $this->onEditAfter($iCmtId);
  493. return array('id' => $iCmtId, 'text' => $this->_prepareTextForOutput($sCmtText, $iCmtId));
  494. }
  495. return array('msg' => _t('_cmt_err_cannot_perform_action'));
  496. }
  497. return array('form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id);
  498. }
  499. protected function _getForm($sAction, $iId)
  500. {
  501. $oForm = $this->_getFormObject($sAction);
  502. $oForm->setId(sprintf($oForm->getAttributeMask('id'), $sAction, $this->_sSystem, $iId));
  503. $oForm->setName(sprintf($oForm->getAttributeMask('name'), $sAction, $this->_sSystem, $iId));
  504. $oForm->aParams['db']['table'] = $this->_aSystem['table'];
  505. $oForm->aInputs['sys']['value'] = $this->_sSystem;
  506. $oForm->aInputs['id']['value'] = $this->_iId;
  507. $oForm->aInputs['action']['value'] = 'Submit' . ucfirst($sAction) . 'Form';
  508. if(!$this->isAttachImageEnabled())
  509. unset($oForm->aInputs['cmt_image']);
  510. if(isset($oForm->aInputs['cmt_text'])) {
  511. $oForm->aInputs['cmt_text']['html'] = $this->_aSystem['html'];
  512. $oForm->aInputs['cmt_text']['db']['pass'] = $this->isHtml() ? 'XssHtml' : 'XssMultiline';
  513. $iCmtTextMin = (int)$this->_aSystem['chars_post_min'];
  514. $iCmtTextMax = (int)$this->_aSystem['chars_post_max'];
  515. $oForm->aInputs['cmt_text']['checker']['params'] = array($iCmtTextMin, $iCmtTextMax);
  516. $oForm->aInputs['cmt_text']['checker']['error'] = _t('_Please enter n1-n2 characters', $iCmtTextMin, $iCmtTextMax);
  517. }
  518. return $oForm;
  519. }
  520. protected function _getMoreLink($sCmts, $aBp = array(), $aDp = array())
  521. {
  522. $iStart = $iPerView = 0;
  523. switch($aBp['type']) {
  524. case BX_CMT_BROWSE_HEAD:
  525. case BX_CMT_BROWSE_POPULAR:
  526. case BX_CMT_BROWSE_CONNECTION:
  527. $iPerView = $aBp['per_view'];
  528. $iStart = $aBp['start'] + $iPerView;
  529. if($iStart >= $aBp['count'])
  530. return $sCmts;
  531. break;
  532. case BX_CMT_BROWSE_TAIL:
  533. $iPerView = $aBp['per_view'];
  534. $iStart = $aBp['start'] - $iPerView;
  535. if($iStart < 0) {
  536. $iPerView += $iStart;
  537. $iStart = 0;
  538. }
  539. if($iStart == 0 && $iPerView == 0)
  540. return $sCmts;
  541. break;
  542. }
  543. $bRoot = (int)$aBp['vparent_id'] <= 0;
  544. $sMore = BxDolTemplate::getInstance()->parseHtmlByName('comment_more.html', array(
  545. 'js_object' => $this->_sJsObjName,
  546. 'style_prefix' => $this->_sStylePrefix,
  547. 'bx_if:is_root' => array(
  548. 'condition' => $bRoot,
  549. 'content' => array()
  550. ),
  551. 'parent_id' => $aBp['vparent_id'],
  552. 'start' => $iStart,
  553. 'per_view' => $iPerView,
  554. 'title' => _t('_cmt_load_more_' . ($aBp['vparent_id'] == 0 ? 'comments' : 'replies') . '_' . $aBp['type'])
  555. ));
  556. switch($aBp['type']) {
  557. case BX_CMT_BROWSE_HEAD:
  558. case BX_CMT_BROWSE_POPULAR:
  559. case BX_CMT_BROWSE_CONNECTION:
  560. $sCmts .= $sMore;
  561. break;
  562. case BX_CMT_BROWSE_TAIL:
  563. $sCmts = $sMore . $sCmts;
  564. break;
  565. }
  566. return $sCmts;
  567. }
  568. protected function _getEmpty()
  569. {
  570. return BxDolTemplate::getInstance()->parseHtmlByName('comment_empty.html', array(
  571. 'style_prefix' => $this->_sStylePrefix,
  572. 'content' => MsgBox(_t('_Empty'))
  573. ));
  574. }
  575. protected function _getViewImagePopup()
  576. {
  577. $sViewImagePopupId = 'cmts-box-' . $this->_sSystem . '-' . $this->getId() . '-view-image-popup' ;
  578. $sViewImagePopupContent = BxDolTemplate::getInstance()->parseHtmlByName('popup_image.html', array(
  579. 'image_url' => ''
  580. ));
  581. return BxTemplFunctions::getInstance()->transBox($sViewImagePopupId, $sViewImagePopupContent, true);
  582. }
  583. protected function _getAttachments($aCmt)
  584. {
  585. $aTmplImages = array();
  586. if(!$this->isAttachImageEnabled())
  587. return '';
  588. $aFiles = $this->_oQuery->getFiles($this->_aSystem['system_id'], $aCmt['cmt_id']);
  589. if(!empty($aFiles) && is_array($aFiles)) {
  590. $oStorage = BxDolStorage::getObjectInstance($this->getStorageObjectName());
  591. $oTranscoder = BxDolTranscoderImage::getObjectInstance($this->getTranscoderPreviewName());
  592. foreach($aFiles as $aFile) {
  593. $bImage = $oTranscoder && $oTranscoder->isMimeTypeSupported($aFile['mime_type']);
  594. $sPreview = '';
  595. if($oTranscoder && $bImage)
  596. $sPreview = $oTranscoder->getFileUrl($aFile['image_id']);
  597. if(!$sPreview)
  598. $sPreview = $this->_oTemplate->getIconUrl($oStorage->getIconNameByFileName($aFile['file_name']));
  599. $aTmplVarsFile = array(
  600. 'js_object' => $this->_sJsObjName,
  601. 'preview' => $sPreview,
  602. 'file' => $oStorage->getFileUrlById($aFile['image_id']),
  603. 'file_name' => $aFile['file_name'],
  604. 'file_icon' => $oStorage->getFontIconNameByFileName($aFile['file_name']),
  605. 'file_size' => _t_format_size($aFile['size']),
  606. );
  607. $aTmplImages[] = array(
  608. 'style_prefix' => $this->_sStylePrefix,
  609. 'bx_if:show_image' => array(
  610. 'condition' => $bImage,
  611. 'content' => $aTmplVarsFile
  612. ),
  613. 'bx_if:show_file' => array(
  614. 'condition' => !$bImage,
  615. 'content' => $aTmplVarsFile
  616. ),
  617. );
  618. }
  619. }
  620. return $this->_oTemplate->parseHtmlByName('comment_attachments.html', array(
  621. 'bx_repeat:attached' => $aTmplImages
  622. ));
  623. }
  624. protected function _getTmplVarsAuthor($aCmt)
  625. {
  626. list($sAuthorName, $sAuthorLink, $sAuthorIcon, $sAuthorUnit) = $this->_getAuthorInfo($aCmt['cmt_author_id']);
  627. $bAuthorIcon = !empty($sAuthorIcon);
  628. return array(
  629. 'author_unit' => $sAuthorUnit,
  630. 'bx_if:show_icon' => array(
  631. 'condition' => $bAuthorIcon,
  632. 'content' => array(
  633. 'author_icon' => $sAuthorIcon
  634. )
  635. ),
  636. 'bx_if:show_icon_empty' => array(
  637. 'condition' => !$bAuthorIcon,
  638. 'content' => array()
  639. ),
  640. 'bx_if:show_author_link' => array(
  641. 'condition' => !empty($sAuthorLink),
  642. 'content' => array(
  643. 'author_link' => $sAuthorLink,
  644. 'author_title' => bx_html_attribute($sAuthorName),
  645. 'author_name' => $sAuthorName
  646. )
  647. ),
  648. 'bx_if:show_author_text' => array(
  649. 'condition' => empty($sAuthorLink),
  650. 'content' => array(
  651. 'author_name' => $sAuthorName
  652. )
  653. ),
  654. );
  655. }
  656. protected function _getTmplVarsText($aCmt)
  657. {
  658. $sText = $aCmt['cmt_text'];
  659. $sTextMore = '';
  660. if(!$this->isHtml()) {
  661. $iMaxLength = (int)$this->_aSystem['chars_display_max'];
  662. if(strlen($sText) > $iMaxLength) {
  663. $iLength = strpos($sText, ' ', $iMaxLength);
  664. $sTextMore = trim(substr($sText, $iLength));
  665. $sText = trim(substr($sText, 0, $iLength));
  666. }
  667. }
  668. $sText = $this->_prepareTextForOutput($sText, $aCmt['cmt_id']);
  669. $sTextMore = $this->_prepareTextForOutput($sTextMore, $aCmt['cmt_id']);
  670. return array(
  671. 'text' => $sText,
  672. 'bx_if:show_more' => array(
  673. 'condition' => !empty($sTextMore),
  674. 'content' => array(
  675. 'style_prefix' => $this->_sStylePrefix,
  676. 'js_object' => $this->_sJsObjName,
  677. 'text_more' => $sTextMore
  678. )
  679. ),
  680. );
  681. }
  682. }
  683. /** @} */