BxBaseCmtsView.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. /**
  3. * @package Dolphin Core
  4. * @copyright Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  5. * @license CC-BY - http://creativecommons.org/licenses/by/3.0/
  6. */
  7. defined('BX_DOL') or die('hack attempt');
  8. bx_import('BxDolCmts');
  9. bx_import('BxDolProfile');
  10. bx_import('BxTemplPaginate');
  11. /**
  12. * @see BxDolCmts
  13. */
  14. class BxBaseCmtsView extends BxDolCmts {
  15. var $_sJsObjName;
  16. var $_sStylePrefix;
  17. function BxBaseCmtsView( $sSystem, $iId, $iInit = 1 ) {
  18. BxDolCmts::BxDolCmts( $sSystem, $iId, $iInit );
  19. if(empty($sSystem))
  20. return;
  21. $this->_sJsObjName = 'oCmts' . ucfirst($sSystem) . $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. * get full comments block with initializations
  27. */
  28. function getCommentsFirst() {
  29. $sCmts = $this->getComments(0);
  30. return BxDolTemplate::getInstance()->parseHtmlByName('comments_block.html', array(
  31. 'system' => $this->_sSystem,
  32. 'js_object' => $this->_sJsObjName,
  33. 'id' => $this->getId(),
  34. 'bx_if:show_empty' => array(
  35. 'condition' => $sCmts == '',
  36. 'content' => array(
  37. 'style_prefix' => $this->_sStylePrefix
  38. )
  39. ),
  40. 'comments' => $sCmts,
  41. 'reply' => $this->isPostReplyAllowed() ? $this->_getPostReplyBox() : '',
  42. 'script' => $this->getCmtsInit()
  43. ));
  44. }
  45. /**
  46. * get comments list for specified parent comment
  47. *
  48. * @param int $iCmtsParentId - parent comment to get child comments from
  49. */
  50. function getComments ($iCmtParentId = 0, $iStart = -1, $iPerView = -1)
  51. {
  52. list($iStart, $iPerView) = $this->_getBrowseParams($iCmtParentId, $iStart, $iPerView);
  53. $aCmts = $this->getCommentsArray($iCmtParentId, $this->_sOrder, $iStart, $iPerView);
  54. if(empty($aCmts) || !is_array($aCmts))
  55. return '';
  56. $sCmts = $iStart > 0 ? $this->_getMoreLink($iCmtParentId, $iStart, $iPerView) : '';
  57. foreach($aCmts as $k => $r)
  58. $sCmts .= $this->getComment($r);
  59. return $sCmts;
  60. }
  61. /**
  62. * get one just posted comment
  63. *
  64. * @param int $iCmtId - comment id
  65. * @return string
  66. */
  67. function getComment($mixedCmt)
  68. {
  69. $oTemplate = BxDolTemplate::getInstance();
  70. $iUserId = $this->_getAuthorId();
  71. $r = !is_array($mixedCmt) ? $this->getCommentRow((int)$mixedCmt) : $mixedCmt;
  72. $sClass = $sRet = '';
  73. if($r['cmt_rated'] == -1 || $r['cmt_rate'] < $this->_aSystem['viewing_threshold']) {
  74. $oTemplate->pareseHtmlByName('comment_hidden.html', array(
  75. 'js_object' => $this->_sJsObjName,
  76. 'id' => $r['cmt_id'],
  77. 'title' => bx_process_output(_t('_hidden_comment', $r['cmt_author_name'])),
  78. 'bx_if:show_replies' => array(
  79. 'condition' => $r['cmt_replies'] > 0,
  80. 'content' => array(
  81. 'replies' => _t('_Show N replies', $r['cmt_replies'])
  82. )
  83. )
  84. ));
  85. $sClass = ' cmt-hidden';
  86. }
  87. if($r['cmt_author_id'] == $iUserId)
  88. $sClass .= ' cmt-mine';
  89. list($sAuthorName, $sAuthorLink, $sAuthorIcon) = $this->_getAuthorInfo($r);
  90. $sActions = $this->_getActionsBox($r);
  91. $sText = bx_process_output($r['cmt_text'], BX_DATA_TEXT_MULTILINE);
  92. $sTextMore = '';
  93. $iMaxLength = (int)$this->_aSystem['chars_display_max'];
  94. if(strlen($sText) > $iMaxLength) {
  95. $iLength = strpos($sText, ' ', $iMaxLength);
  96. $sTextMore = substr($sText, $iLength);
  97. $sText = substr($sText, 0, $iLength);
  98. }
  99. $aTmplReplyTo = array();
  100. if((int)$r['cmt_parent_id'] != 0) {
  101. $aParent = $this->getCommentRow($r['cmt_parent_id']);
  102. list($sParAuthorName, $sParAuthorLink, $sParAuthorIcon) = $this->_getAuthorInfo($aParent);
  103. $aTmplReplyTo = array(
  104. 'par_cmt_link' => '#' . $r['cmt_parent_id'],
  105. 'par_cmt_author' => $sParAuthorName
  106. );
  107. }
  108. return $oTemplate->parseHtmlByName('comment.html', array(
  109. 'system' => $this->_sSystem,
  110. 'id' => $r['cmt_id'],
  111. 'style_prefix' => $this->_sStylePrefix,
  112. 'class' => $sClass,
  113. 'author_icon' => $sAuthorIcon,
  114. 'bx_if:show_author_link' => array(
  115. 'condition' => !empty($sAuthorLink),
  116. 'content' => array(
  117. 'author_link' => $sAuthorLink,
  118. 'author_name' => $sAuthorName
  119. )
  120. ),
  121. 'bx_if:show_author_text' => array(
  122. 'condition' => empty($sAuthorLink),
  123. 'content' => array(
  124. 'author_name' => $sAuthorName
  125. )
  126. ),
  127. 'ago' => $r['cmt_ago'],
  128. 'points' => _t($r['cmt_rate'] == 1 || $r['cmt_rate'] == -1 ? '_N_point' : '_N_points', $r['cmt_rate']),
  129. 'bx_if:show_reply_to' => array(
  130. 'condition' => !empty($aTmplReplyTo),
  131. 'content' => $aTmplReplyTo
  132. ),
  133. 'text' => $sText,
  134. 'bx_if:show_more' => array(
  135. 'condition' => !empty($sTextMore),
  136. 'content' => array(
  137. 'js_object' => $this->_sJsObjName,
  138. 'text_more' => $sTextMore
  139. )
  140. ),
  141. 'actions' => $sActions
  142. ));
  143. }
  144. function getFormBox($sType, $iParentId)
  145. {
  146. return $this->_getPostReplyBox($sType, $iParentId);
  147. }
  148. function getForm($iCmtParentId, $sText, $sFunction)
  149. {
  150. return $this->_getPostReplyForm($iCmtParentId, $sText, $sFunction);
  151. }
  152. /**
  153. * Get comments css file string
  154. *
  155. * @return string
  156. */
  157. function getExtraCss ()
  158. {
  159. BxDolTemplate::getInstance()->addCss('cmts.css');
  160. }
  161. /**
  162. * Get comments js file string
  163. *
  164. * @return string
  165. */
  166. function getExtraJs ()
  167. {
  168. BxDolTemplate::getInstance()->addJs('BxDolCmts.js');
  169. }
  170. /**
  171. * Get initialization section of comments box
  172. *
  173. * @return string
  174. */
  175. function getCmtsInit()
  176. {
  177. $sToggleAdd = '';
  178. $ret = '';
  179. $ret .= $sToggleAdd . "
  180. <script type=\"text/javascript\">
  181. var " . $this->_sJsObjName . " = new BxDolCmts({
  182. sObjName : '" . $this->_sJsObjName . "',
  183. sBaseUrl : '" . BX_DOL_URL_ROOT . "',
  184. sSystem : '" . $this->getSystemName() . "',
  185. sSystemTable: '" . $this->_aSystem['table_cmts'] . "',
  186. iAuthorId: '" . $this->_getAuthorId() . "',
  187. iObjId : '" . $this->getId () . "',
  188. sOrder : '" . $this->getOrder() . "',
  189. sDefaultErrMsg : '" . bx_js_string(_t('_Error occured')) . "',
  190. sConfirmMsg : '" . bx_js_string(_t('_Are you sure?')) . "',
  191. sTextAreaId: '" . $this->sTextAreaId . "'});
  192. " . $this->_sJsObjName . ".oCmtElements = {";
  193. for (reset($this->_aCmtElements); list($k,$r) = each ($this->_aCmtElements); ) {
  194. $ret .= "\n'$k' : { 'reg' : '{$r['reg']}', 'msg' : \"". bx_js_string(trim($r['msg'])) . "\" },";
  195. }
  196. $ret = substr($ret, 0, -1);
  197. $ret .= "\n};\n";
  198. $ret .= "</script>";
  199. $this->getExtraJs();
  200. $this->getExtraCss();
  201. return $ret;
  202. }
  203. /**
  204. * private functions
  205. */
  206. function _getMoreLink($iParentId, $iStart, $iPerView)
  207. {
  208. return BxDolTemplate::getInstance()->parseHtmlByName('comment_more.html', array(
  209. 'js_object' => $this->_sJsObjName,
  210. 'style_prefix' => $this->_sStylePrefix,
  211. 'parent_id' => $iParentId,
  212. 'start' => $iStart,
  213. 'per_view' => $iPerView
  214. ));
  215. }
  216. function _getActionsBox(&$a)
  217. {
  218. $iUserId = $this->_getAuthorId();
  219. $isEditAllowedPermanently = ($a['cmt_author_id'] == $iUserId && $this->isEditAllowed()) || $this->isEditAllowedAll();
  220. $isRemoveAllowedPermanently = ($a['cmt_author_id'] == $iUserId && $this->isRemoveAllowed()) || $this->isRemoveAllowedAll();
  221. return BxDolTemplate::getInstance()->parseHtmlByName('comment_actions.html', array(
  222. 'id' => $a['cmt_id'],
  223. 'style_prefix' => $this->_sStylePrefix,
  224. 'bx_if:show_edit' => array(
  225. 'condition' => $isEditAllowedPermanently,
  226. 'content' => array(
  227. 'js_object' => $this->_sJsObjName,
  228. 'style_prefix' => $this->_sStylePrefix,
  229. 'id' => $a['cmt_id']
  230. )
  231. ),
  232. 'bx_if:show_delete' => array(
  233. 'condition' => $isRemoveAllowedPermanently,
  234. 'content' => array(
  235. 'js_object' => $this->_sJsObjName,
  236. 'style_prefix' => $this->_sStylePrefix,
  237. 'id' => $a['cmt_id']
  238. )
  239. ),
  240. 'bx_if:show_rate' => array(
  241. 'condition' => $this->isRatable(),
  242. 'content' => array(
  243. 'js_object' => $this->_sJsObjName,
  244. 'style_prefix' => $this->_sStylePrefix,
  245. 'id' => $a['cmt_id']
  246. )
  247. ),
  248. 'bx_if:show_replies' => array(
  249. 'condition' => (int)$a['cmt_replies'] > 0,
  250. 'content' => array(
  251. 'js_object' => $this->_sJsObjName,
  252. 'style_prefix' => $this->_sStylePrefix,
  253. 'id' => $a['cmt_id'],
  254. 'text' => _t((isset($a['cmt_type']) && $a['cmt_type'] == 'comment' ? '_N_comments' : '_N_replies'), $a['cmt_replies'])
  255. )
  256. ),
  257. 'bx_if:show_reply' => array(
  258. 'condition' => $this->isPostReplyAllowed(),
  259. 'content' => array(
  260. 'js_object' => $this->_sJsObjName,
  261. 'style_prefix' => $this->_sStylePrefix,
  262. 'id' => $a['cmt_id'],
  263. 'text' => _t(isset($a['cmt_type']) && $a['cmt_type'] == 'comment' ? '_Comment_to_this_comment' : '_Reply_to_this_comment')
  264. )
  265. )
  266. ));
  267. }
  268. function _getPostReplyBox($sType = 'comment', $iCmtParentId = 0)
  269. {
  270. bx_import('BxDolProfile');
  271. $oProfile = null; //BxDolProfile::getInstanceAccountProfile($this->_getAuthorId());
  272. return BxDolTemplate::getInstance()->parseHtmlByName('comment_reply_box.html', array(
  273. 'style_prefix' => $this->_sStylePrefix,
  274. 'author_icon' => $oProfile ? $oProfile->getThumb() : 'icon',
  275. 'form' => $this->_getPostReplyForm($iCmtParentId)
  276. ));
  277. }
  278. function _getPostReplyForm($iCmtParentId = 0, $sText = "", $sFunction = "cmtSubmit(this)")
  279. {
  280. return BxDolTemplate::getInstance()->parseHtmlByName('comment_reply_form.html', array(
  281. 'js_object' => $this->_sJsObjName,
  282. 'js_function' => $sFunction,
  283. 'parent_id' => $iCmtParentId,
  284. 'text' => bx_process_output($sText)
  285. ));
  286. }
  287. function _getBrowseParams($iCmtParentId = 0, $iStart = -1, $iPerView = -1)
  288. {
  289. $iStart = $iStart != -1 ? $iStart : $this->_oQuery->getCommentsCount($this->_iId, $iCmtParentId);
  290. $iPerView = $iPerView != -1 ? $iPerView : $this->getPerView($iCmtParentId);
  291. switch($this->_aSystem['show']) {
  292. case BX_CMT_SHOW_HEAD:
  293. $iStart = 0;
  294. break;
  295. case BX_CMT_SHOW_TAIL:
  296. $iStart = $iStart - $iPerView;
  297. if($iStart < 0) {
  298. $iPerView += $iStart;
  299. $iStart = 0;
  300. }
  301. break;
  302. }
  303. return array($iStart, $iPerView);
  304. }
  305. function _getAuthorInfo($r)
  306. {
  307. $sAuthorName = _t('_Anonymous');
  308. $sAuthorLink = '';
  309. $sAuthorIcon = '';
  310. $bAuthor = false; //$r['cmt_author_id'] && $r['cmt_author_name'];
  311. if($bAuthor) {
  312. $oProfile = null; //BxDolProfile::getInstanceAccountProfile($r['cmt_author_id']);
  313. $sAuthorName = bx_process_output($r['cmt_author_name']);
  314. $sAuthorLink = $oProfile->getUrl();
  315. $sAuthorIcon = $oProfile->getThumb();
  316. }
  317. return array($sAuthorName, $sAuthorLink, $sAuthorIcon);
  318. }
  319. }