BxBaseCmts.php 31 KB

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