BxBaseCmtsView.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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 comments css file string
  27. *
  28. * @return string
  29. */
  30. function getExtraCss ()
  31. {
  32. BxDolTemplate::getInstance()->addCss(array('cmts.css'));
  33. }
  34. /**
  35. * Get comments js file string
  36. *
  37. * @return string
  38. */
  39. function getExtraJs ()
  40. {
  41. BxDolTemplate::getInstance()->addJs(array('common_anim.js', 'jquery.form.js', 'BxDolCmts.js'));
  42. }
  43. /**
  44. * Get initialization section of comments box
  45. *
  46. * @return string
  47. */
  48. function getScript()
  49. {
  50. $sToggleAdd = '';
  51. $ret = '';
  52. $ret .= $sToggleAdd . "
  53. <script type=\"text/javascript\">
  54. var " . $this->_sJsObjName . " = new BxDolCmts({
  55. sObjName: '" . $this->_sJsObjName . "',
  56. sRootUrl: '" . BX_DOL_URL_ROOT . "',
  57. sSystem: '" . $this->getSystemName() . "',
  58. sSystemTable: '" . $this->_aSystem['table_cmts'] . "',
  59. iAuthorId: '" . $this->_getAuthorId() . "',
  60. iObjId: '" . $this->getId () . "',
  61. sPostFormPosition: '" . $this->_aSystem['post_form_position'] . "',
  62. sBrowseType: '" . $this->_sBrowseType . "',
  63. sDisplayType: '" . $this->_sDisplayType . "'});\n";
  64. $ret .= "</script>";
  65. $this->getExtraJs();
  66. $this->getExtraCss();
  67. BxDolTemplate::getInstance()->addJsTranslation(array(
  68. '_Error occured',
  69. '_Are you sure?'
  70. ));
  71. return $ret;
  72. }
  73. /**
  74. * get full comments block with initializations
  75. */
  76. function getCommentsBlock($iParentId = 0, $iVParentId = 0, $bInDesignbox = true) {
  77. $aBp = array('parent_id' => $iParentId, 'vparent_id' => $iVParentId);
  78. $sCmts = $this->getComments($aBp);
  79. $sCaption = _t('_cmt_block_comments_title', $this->_oQuery->getCommentsCount($this->_iId));
  80. $sContent = BxDolTemplate::getInstance()->parseHtmlByName('comments_block.html', array(
  81. 'system' => $this->_sSystem,
  82. 'id' => $this->getId(),
  83. 'bx_if:show_empty' => array(
  84. 'condition' => $sCmts == '',
  85. 'content' => array(
  86. 'style_prefix' => $this->_sStylePrefix
  87. )
  88. ),
  89. 'comments' => $sCmts,
  90. 'post_form_top' => $this->getFormBoxPost($aBp, array('type' => $this->_sDisplayType, 'position' => BX_CMT_PFP_TOP)),
  91. 'post_form_bottom' => $this->getFormBoxPost($aBp, array('type' => $this->_sDisplayType, 'position' => BX_CMT_PFP_BOTTOM)),
  92. 'script' => $this->getScript()
  93. ));
  94. return $bInDesignbox ? DesignBoxContent($sCaption, $sContent, BX_DB_PADDING_DEF, $this->_getControlsBox()) : $sContent;
  95. }
  96. /**
  97. * get comments list for specified parent comment
  98. *
  99. * @param array $aBp - browse params array
  100. * @param array $aDp - display params array
  101. *
  102. */
  103. function getComments($aBp = array(), $aDp = array())
  104. {
  105. $this->_prepareParams($aBp, $aDp);
  106. $aCmts = $this->getCommentsArray($aBp['vparent_id'], $aBp['order'], $aBp['start'], $aBp['per_view']);
  107. if(empty($aCmts) || !is_array($aCmts))
  108. return '';
  109. $sCmts = '';
  110. foreach($aCmts as $k => $r)
  111. $sCmts .= $this->getComment($r, $aBp, $aDp);
  112. $sCmts = $this->_getMoreLink($sCmts, $aBp, $aDp);
  113. return $sCmts;
  114. }
  115. /**
  116. * get comment view block with initializations
  117. */
  118. function getCommentBlock($iCmtId = 0)
  119. {
  120. return BxDolTemplate::getInstance()->parseHtmlByName('comment_block.html', array(
  121. 'system' => $this->_sSystem,
  122. 'id' => $this->getId(),
  123. 'comment' => $this->getComment($iCmtId, array('type' => $this->_sBrowseType), array('type' => BX_CMT_DISPLAY_THREADED)),
  124. 'script' => $this->getScript()
  125. ));
  126. }
  127. /**
  128. * get one just posted comment
  129. *
  130. * @param int $iCmtId - comment id
  131. * @return string
  132. */
  133. function getComment($mixedCmt, $aBp = array(), $aDp = array())
  134. {
  135. $oTemplate = BxDolTemplate::getInstance();
  136. $iUserId = $this->_getAuthorId();
  137. $r = !is_array($mixedCmt) ? $this->getCommentRow((int)$mixedCmt) : $mixedCmt;
  138. list($sAuthorName, $sAuthorLink, $sAuthorIcon) = $this->_getAuthorInfo($r['cmt_author_id']);
  139. $sClass = $sRet = '';
  140. if($r['cmt_rated'] == -1 || $r['cmt_rate'] < $this->_aSystem['viewing_threshold']) {
  141. $oTemplate->pareseHtmlByName('comment_hidden.html', array(
  142. 'js_object' => $this->_sJsObjName,
  143. 'id' => $r['cmt_id'],
  144. 'title' => bx_process_output(_t('_hidden_comment', $sAuthorName)),
  145. 'bx_if:show_replies' => array(
  146. 'condition' => $r['cmt_replies'] > 0,
  147. 'content' => array(
  148. 'replies' => _t('_Show N replies', $r['cmt_replies'])
  149. )
  150. )
  151. ));
  152. $sClass = ' cmt-hidden';
  153. }
  154. if($r['cmt_author_id'] == $iUserId)
  155. $sClass .= ' cmt-mine';
  156. $sActions = $this->_getActionsBox($r, $aDp);
  157. $sText = $r['cmt_text'];
  158. $sTextMore = '';
  159. $iMaxLength = (int)$this->_aSystem['chars_display_max'];
  160. if(strlen($sText) > $iMaxLength) {
  161. $iLength = strpos($sText, ' ', $iMaxLength);
  162. $sTextMore = trim(substr($sText, $iLength));
  163. $sText = trim(substr($sText, 0, $iLength));
  164. }
  165. $sText = $this->_prepareTextForOutput($sText);
  166. $sTextMore = $this->_prepareTextForOutput($sTextMore);
  167. $aTmplReplyTo = array();
  168. if((int)$r['cmt_parent_id'] != 0) {
  169. $aParent = $this->getCommentRow($r['cmt_parent_id']);
  170. list($sParAuthorName, $sParAuthorLink, $sParAuthorIcon) = $this->_getAuthorInfo($aParent['cmt_author_id']);
  171. $aTmplReplyTo = array(
  172. 'style_prefix' => $this->_sStylePrefix,
  173. 'par_cmt_link' => $this->getBaseUrl() . '#' . $this->_sSystem . $r['cmt_parent_id'],
  174. 'par_cmt_author' => $sParAuthorName
  175. );
  176. }
  177. $aTmplImages = array();
  178. $aImages = $this->_oQuery->getImages($this->_aSystem['system_id'], $r['cmt_id']);
  179. if(!empty($aImages) && is_array($aImages)) {
  180. bx_import('BxDolImageTranscoder');
  181. $oTranscoder = BxDolImageTranscoder::getObjectInstance($this->_sTranscoderPreview);
  182. foreach($aImages as $aImage)
  183. $aTmplImages[] = array(
  184. 'style_prefix' => $this->_sStylePrefix,
  185. 'image' => $oTranscoder->getImageUrl($aImage['image_id'])
  186. );
  187. }
  188. $sReplies = '';
  189. if((int)$r['cmt_replies'] > 0 && !empty($aDp) && $aDp['type'] == BX_CMT_DISPLAY_THREADED)
  190. $sReplies = $this->getComments(array('parent_id' => $r['cmt_id'], 'vparent_id' => $r['cmt_id'], 'type' => $aBp['type']), $aDp);
  191. $bAuthorIcon = !empty($sAuthorIcon);
  192. return $oTemplate->parseHtmlByName('comment.html', array(
  193. 'system' => $this->_sSystem,
  194. 'style_prefix' => $this->_sStylePrefix,
  195. 'js_object' => $this->_sJsObjName,
  196. 'id' => $r['cmt_id'],
  197. 'class' => $sClass,
  198. 'bx_if:show_icon' => array(
  199. 'condition' => $bAuthorIcon,
  200. 'content' => array(
  201. 'author_icon' => $sAuthorIcon
  202. )
  203. ),
  204. 'bx_if:show_icon_empty' => array(
  205. 'condition' => !$bAuthorIcon,
  206. 'content' => array()
  207. ),
  208. 'bx_if:show_author_link' => array(
  209. 'condition' => !empty($sAuthorLink),
  210. 'content' => array(
  211. 'author_link' => $sAuthorLink,
  212. 'author_name' => $sAuthorName
  213. )
  214. ),
  215. 'bx_if:show_author_text' => array(
  216. 'condition' => empty($sAuthorLink),
  217. 'content' => array(
  218. 'author_name' => $sAuthorName
  219. )
  220. ),
  221. 'bx_if:show_reply_to' => array(
  222. 'condition' => !empty($aTmplReplyTo),
  223. 'content' => $aTmplReplyTo
  224. ),
  225. 'view_link' => bx_append_url_params($this->_sViewUrl, array(
  226. 'sys' => $this->_sSystem,
  227. 'id' => $this->_iId,
  228. 'cmt_id' => $r['cmt_id']
  229. )),
  230. 'ago' => $r['cmt_ago'],
  231. 'bx_if:hide_rate_count' => array(
  232. 'condition' => (int)$r['cmt_rate'] <= 0,
  233. 'content' => array()
  234. ),
  235. 'points' => _t(in_array($r['cmt_rate'], array(-1, 0, 1)) ? '_N_point' : '_N_points', $r['cmt_rate']),
  236. 'text' => $sText,
  237. 'bx_if:show_more' => array(
  238. 'condition' => !empty($sTextMore),
  239. 'content' => array(
  240. 'style_prefix' => $this->_sStylePrefix,
  241. 'js_object' => $this->_sJsObjName,
  242. 'text_more' => $sTextMore
  243. )
  244. ),
  245. 'bx_if:show_attached' => array(
  246. 'condition' => !empty($aTmplImages),
  247. 'content' => array(
  248. 'style_prefix' => $this->_sStylePrefix,
  249. 'bx_repeat:attached' => $aTmplImages
  250. )
  251. ),
  252. 'actions' => $sActions,
  253. 'replies' => $sReplies
  254. ));
  255. }
  256. function getFormBoxPost($aBp = array(), $aDp = array())
  257. {
  258. return $this->_getFormBox(BX_CMT_ACTION_POST, $aBp, $aDp);
  259. }
  260. function getFormBoxEdit($aBp = array(), $aDp = array())
  261. {
  262. return $this->_getFormBox(BX_CMT_ACTION_EDIT, $aBp, $aDp);
  263. }
  264. function getFormPost($iCmtParentId = 0)
  265. {
  266. return $this->_getFormPost($iCmtParentId);
  267. }
  268. function getFormEdit($aCmt)
  269. {
  270. return $this->_getFormEdit($aCmt);
  271. }
  272. function getPlusedBy($iCmtId)
  273. {
  274. $oTemplate = BxDolTemplate::getInstance();
  275. $aTmplUsers = array();
  276. $aUserIds = $this->_oQuery->getRatedBy($this->_aSystem['system_id'], $iCmtId);
  277. foreach($aUserIds as $iUserId) {
  278. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($iUserId);
  279. $aTmplUsers[] = array(
  280. 'style_prefix' => $this->_sStylePrefix,
  281. 'user_unit' => $sUserUnit
  282. );
  283. }
  284. bx_import('BxTemplStudioFunctions');
  285. $sContent = BxTemplStudioFunctions::getInstance()->transBox($oTemplate->parseHtmlByName('comment_pb_list.html', array(
  286. 'style_prefix' => $this->_sStylePrefix,
  287. 'bx_repeat:list' => $aTmplUsers
  288. )));
  289. return $oTemplate->parseHtmlByName('comment_plused_by.html', array(
  290. 'style_prefix' => $this->_sStylePrefix,
  291. 'id' => $this->_sSystem . '-plused-by' . $iCmtId,
  292. 'content' => $sContent
  293. ));
  294. }
  295. /**
  296. * private functions
  297. */
  298. protected function _getControlsBox()
  299. {
  300. $oTemplate = BxDolTemplate::getInstance();
  301. $sDisplay = '';
  302. $bDisplay = (int)$this->_aSystem['is_display_switch'] == 1;
  303. if($bDisplay) {
  304. $aDisplayLinks = array(
  305. array('id' => $this->_sSystem . '-flat', 'name' => $this->_sSystem . '-flat', 'class' => '', 'title' => '_cmt_display_flat', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeDisplay(this, \'flat\');'),
  306. array('id' => $this->_sSystem . '-threaded', 'name' => $this->_sSystem . '-threaded', 'class' => '', 'title' => '_cmt_display_threaded', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeDisplay(this, \'threaded\');')
  307. );
  308. bx_import('BxTemplMenuInteractive');
  309. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-display', 'menu_items' => $aDisplayLinks));
  310. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sDisplayType);
  311. $sDisplay = $oMenu->getCode();
  312. }
  313. $sBrowse = '';
  314. $bBrowse = (int)$this->_aSystem['is_browse_switch'] == 1;
  315. if($bBrowse) {
  316. $aBrowseLinks = array(
  317. array('id' => $this->_sSystem . '-tail', 'name' => $this->_sSystem . '-tail', 'class' => '', 'title' => '_cmt_browse_tail', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'tail\');'),
  318. array('id' => $this->_sSystem . '-head', 'name' => $this->_sSystem . '-head', 'class' => '', 'title' => '_cmt_browse_head', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'head\');'),
  319. array('id' => $this->_sSystem . '-popular', 'name' => $this->_sSystem . '-popular', 'class' => '', 'title' => '_cmt_browse_popular', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'popular\');'),
  320. array('id' => $this->_sSystem . '-connection', 'name' => $this->_sSystem . '-connection', 'class' => '', 'title' => '_cmt_browse_connection', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'connection\');')
  321. );
  322. bx_import('BxTemplMenuInteractive');
  323. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-browse', 'menu_items' => $aBrowseLinks));
  324. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sBrowseType);
  325. $sBrowse = $oMenu->getCode();
  326. }
  327. return $oTemplate->parseHtmlByName('comments_controls.html', array(
  328. 'display_switcher' => $bDisplay ? $sDisplay : '',
  329. 'bx_if:is_divider' => array(
  330. 'condition' => $bDisplay && $bBrowse,
  331. 'content' => array(
  332. 'style_prefix' => $this->_sStylePrefix,
  333. )
  334. ),
  335. 'browse_switcher' => $bBrowse ? $sBrowse : '',
  336. ));
  337. }
  338. protected function _getActionsBox(&$a, $aDp = array())
  339. {
  340. $oTemplate = BxDolTemplate::getInstance();
  341. $iUserId = $this->_getAuthorId();
  342. $isEditAllowedPermanently = ($a['cmt_author_id'] == $iUserId && $this->isEditAllowed()) || $this->isEditAllowedAll();
  343. $isRemoveAllowedPermanently = ($a['cmt_author_id'] == $iUserId && $this->isRemoveAllowed()) || $this->isRemoveAllowedAll();
  344. $sManagePopupId = $sManagePopupText = '';
  345. if($isEditAllowedPermanently || $isRemoveAllowedPermanently) {
  346. $aMenu = array(
  347. array('name' => 'cmt-edit', 'icon' => 'pencil', 'onclick' => $this->_sJsObjName . ".cmtEdit(this, " . $a['cmt_id'] . ")", 'title' => _t('_Edit')),
  348. array('name' => 'cmt-delete', 'icon' => 'remove', 'onclick' => $this->_sJsObjName . ".cmtRemove(this, " . $a['cmt_id'] . ")", 'title' => _t('_Delete')),
  349. );
  350. bx_import('BxTemplStudioMenu');
  351. $oMenu = new BxTemplStudioMenu(array('template' => 'menu_vertical.html', 'menu_items' => $aMenu));
  352. bx_import('BxTemplStudioFunctions');
  353. $sManagePopupText = BxTemplStudioFunctions::getInstance()->transBox($oTemplate->parseHtmlByName('comment_manage.html', array(
  354. 'style_prefix' => $this->_sStylePrefix,
  355. 'content' => $oMenu->getCode()
  356. )));
  357. }
  358. $bRated = (int)$a['cmt_rated'] > 0;
  359. return $oTemplate->parseHtmlByName('comment_actions.html', array(
  360. 'id' => $a['cmt_id'],
  361. 'style_prefix' => $this->_sStylePrefix,
  362. 'view_link' => bx_append_url_params($this->_sViewUrl, array(
  363. 'sys' => $this->_sSystem,
  364. 'id' => $this->_iId,
  365. 'cmt_id' => $a['cmt_id']
  366. )),
  367. 'ago' => $a['cmt_ago'],
  368. 'points' => _t($a['cmt_rate'] == 1 || $a['cmt_rate'] == -1 ? '_N_point' : '_N_points', $a['cmt_rate']),
  369. 'bx_if:show_reply' => array(
  370. 'condition' => $this->isPostReplyAllowed(),
  371. 'content' => array(
  372. 'js_object' => $this->_sJsObjName,
  373. 'style_prefix' => $this->_sStylePrefix,
  374. 'id' => $a['cmt_id'],
  375. 'text' => _t(isset($a['cmt_type']) && $a['cmt_type'] == 'comment' ? '_Comment_to_this_comment' : '_Reply_to_this_comment')
  376. )
  377. ),
  378. 'bx_if:show_rate' => array(
  379. 'condition' => $this->isRatable() && $this->isRateAllowed(),
  380. 'content' => array(
  381. 'js_object' => $this->_sJsObjName,
  382. 'style_prefix' => $this->_sStylePrefix,
  383. 'id' => $a['cmt_id'],
  384. 'bx_if:hide_rate_plus' => array(
  385. 'condition' => $bRated,
  386. 'content' => array()
  387. ),
  388. 'bx_if:hide_rate_minus' => array(
  389. 'condition' => !$bRated,
  390. 'content' => array()
  391. )
  392. )
  393. ),
  394. 'bx_if:show_manage' => array(
  395. 'condition' => $isEditAllowedPermanently || $isRemoveAllowedPermanently,
  396. 'content' => array(
  397. 'js_object' => $this->_sJsObjName,
  398. 'style_prefix' => $this->_sStylePrefix,
  399. 'id' => $a['cmt_id'],
  400. 'popup_id' => $this->_sSystem . '-manage' . $a['cmt_id'],
  401. 'popup_text' => $sManagePopupText
  402. )
  403. )
  404. ));
  405. }
  406. protected function _getFormBox($sType, $aBp, $aDp)
  407. {
  408. $iCmtParentId = isset($aBp['parent_id']) ? (int)$aBp['parent_id'] : 0;
  409. $sPosition = isset($aDp['position']) ? $aDp['position'] : '';
  410. if(!$this->isPostReplyAllowed())
  411. return '';
  412. list($sAuthorName, $sAuthorLink, $sAuthorIcon) = $this->_getAuthorInfo();
  413. $sPositionSystem = $this->_aSystem['post_form_position'];
  414. if(!empty($sPosition) && $sPositionSystem != BX_CMT_PFP_BOTH && $sPositionSystem != $sPosition)
  415. return '';
  416. $sMethod = '_getForm' . ucfirst($sType);
  417. $aForm = $this->$sMethod($iCmtParentId);
  418. $bAuthorIcon = !empty($sAuthorIcon);
  419. return BxDolTemplate::getInstance()->parseHtmlByName('comment_reply_box.html', array(
  420. 'js_object' => $this->_sJsObjName,
  421. 'style_prefix' => $this->_sStylePrefix,
  422. 'bx_if:show_class' => array(
  423. 'condition' => !empty($sPosition),
  424. 'content' => array(
  425. 'class' => $this->_sStylePrefix . '-reply-' . $sPosition
  426. )
  427. ),
  428. 'bx_if:show_icon' => array(
  429. 'condition' => $bAuthorIcon,
  430. 'content' => array(
  431. 'author_icon' => $sAuthorIcon
  432. )
  433. ),
  434. 'bx_if:show_icon_empty' => array(
  435. 'condition' => !$bAuthorIcon,
  436. 'content' => array()
  437. ),
  438. 'bx_if:show_author_link' => array(
  439. 'condition' => !empty($sAuthorLink),
  440. 'content' => array(
  441. 'author_link' => $sAuthorLink,
  442. 'author_name' => $sAuthorName
  443. )
  444. ),
  445. 'bx_if:show_author_text' => array(
  446. 'condition' => empty($sAuthorLink),
  447. 'content' => array(
  448. 'author_name' => $sAuthorName
  449. )
  450. ),
  451. 'form' => $aForm['form'],
  452. 'form_id' => $aForm['form_id'],
  453. ));
  454. }
  455. protected function _getFormPost($iCmtParentId = 0)
  456. {
  457. $oForm = $this->_getFormObject(BX_CMT_ACTION_POST, $iCmtParentId);
  458. $oForm->aInputs['cmt_parent_id']['value'] = $iCmtParentId;
  459. $oForm->initChecker();
  460. if($oForm->isSubmittedAndValid()) {
  461. $iCmtAuthorId = $this->_getAuthorId();
  462. $iCmtParentId = $oForm->getCleanValue('cmt_parent_id');
  463. $sCmtText = $oForm->getCleanValue('cmt_text');
  464. if($this->_isSpam($sCmtText))
  465. return array('msg' => _t('_sys_spam_detected', BX_DOL_URL_ROOT . 'contact.php'));
  466. $sCmtText = $this->_prepareTextForSave ($sCmtText);
  467. $oForm->setSubmittedValue('cmt_text', $sCmtText, $oForm->aFormAttrs['method']);
  468. $iLevel = 0;
  469. $iCmtVisualParentId = 0;
  470. if((int)$iCmtParentId > 0) {
  471. $aParent = $this->getCommentRow($iCmtParentId);
  472. $iLevel = (int)$aParent['cmt_level'] + 1;
  473. $iCmtVisualParentId = $iLevel > $this->getMaxLevel() ? $aParent['cmt_vparent_id'] : $iCmtParentId;
  474. }
  475. $iCmtId = (int)$oForm->insert(array('cmt_vparent_id' => $iCmtVisualParentId, 'cmt_object_id' => $this->_iId, 'cmt_author_id' => $iCmtAuthorId, 'cmt_level' => $iLevel, 'cmt_time' => mktime()));
  476. if($iCmtId != 0) {
  477. $aImages = $oForm->getCleanValue('cmt_image');
  478. if(!empty($aImages) || is_array($aImages)) {
  479. bx_import('BxDolStorage');
  480. $oStorage = BxDolStorage::getObjectInstance($this->_sStorageObject);
  481. foreach($aImages as $iImageId)
  482. if($this->_oQuery->saveImages($this->_aSystem['system_id'], $iCmtId, $iImageId))
  483. $oStorage->afterUploadCleanup($iImageId, $iCmtAuthorId);
  484. }
  485. if($iCmtParentId)
  486. $this->_oQuery->updateRepliesCount($iCmtParentId, 1);
  487. $this->_triggerComment();
  488. $this->isPostReplyAllowed(true);
  489. bx_import('BxDolAlerts');
  490. $oZ = new BxDolAlerts($this->_sSystem, 'commentPost', $this->getId(), $iCmtAuthorId, array('comment_id' => $iCmtId, 'comment_author_id' => $iCmtAuthorId));
  491. $oZ->alert();
  492. return array('id' => $iCmtId, 'parent_id' => $iCmtParentId);
  493. }
  494. return array('msg' => _t('_cmt_err_cannot_perform_action'));
  495. }
  496. return array('form' => $oForm->getCode(), 'form_id' => $oForm->id);
  497. }
  498. protected function _getFormEdit($iCmtId)
  499. {
  500. $aCmt = $this->_oQuery->getCommentSimple ($this->getId(), $iCmtId);
  501. if(!$aCmt)
  502. return array('msg' => _t('_No such comment'));
  503. $iCmtAuthorId = $this->_getAuthorId();
  504. if(!$this->isEditAllowedAll() && ($aCmt['cmt_author_id'] != $iCmtAuthorId || !$this->isEditAllowed()))
  505. return array('msg' => $aCmt['cmt_author_id'] == $iCmtAuthorId && !$this->isEditAllowed() ? strip_tags($this->msgErrEditAllowed()) : _t('_Access denied'));
  506. $oForm = $this->_getFormObject(BX_CMT_ACTION_EDIT, $aCmt['cmt_id']);
  507. $oForm->initChecker($aCmt);
  508. if($oForm->isSubmittedAndValid()) {
  509. $sCmtText = $oForm->getCleanValue('cmt_text');
  510. if($this->_isSpam($sCmtText))
  511. return array('msg' => _t('_sys_spam_detected', BX_DOL_URL_ROOT . 'contact.php'));
  512. $sCmtText = $this->_prepareTextForSave ($sCmtText);
  513. $oForm->setSubmittedValue('cmt_text', $sCmtText, $oForm->aFormAttrs['method']);
  514. if($oForm->update($iCmtId)) {
  515. if ($aCmt['cmt_author_id'] == $iCmtAuthorId)
  516. $this->isEditAllowed(true);
  517. bx_import('BxDolAlerts');
  518. $oZ = new BxDolAlerts($this->_sSystem, 'commentUpdated', $this->getId(), $iCmtAuthorId, array('comment_id' => $aCmt['cmt_id'], 'comment_author_id' => $aCmt['cmt_author_id']));
  519. $oZ->alert();
  520. return array('id' => $iCmtId, 'text' => $sCmtText);
  521. }
  522. return array('msg' => _t('_cmt_err_cannot_perform_action'));
  523. }
  524. return array('form' => $oForm->getCode(), 'form_id' => $oForm->id);
  525. }
  526. protected function _getFormObject($sAction, $iId)
  527. {
  528. $sActionCap = ucfirst($sAction);
  529. $sDisplayName = '_sFormDisplay' . $sActionCap;
  530. bx_import('BxDolForm');
  531. $oForm = BxDolForm::getObjectInstance($this->_sFormObject, $this->$sDisplayName);
  532. $oForm->setId(sprintf($oForm->aFormAttrs['id'], $sAction, $this->_sSystem, $iId));
  533. $oForm->setName(sprintf($oForm->aFormAttrs['name'], $sAction, $this->_sSystem, $iId));
  534. $oForm->aParams['db']['table'] = $this->_aSystem['table_cmts'];
  535. $oForm->aInputs['sys']['value'] = $this->_sSystem;
  536. $oForm->aInputs['id']['value'] = $this->_iId;
  537. $oForm->aInputs['action']['value'] = 'Submit' . $sActionCap . 'Form';
  538. if(isset($oForm->aInputs['cmt_image'])) {
  539. $aFormNested = array(
  540. 'params' =>array(
  541. 'nested_form_template' => 'comments_uploader_nfw.html'
  542. ),
  543. 'inputs' => array(),
  544. );
  545. bx_import('BxDolFormNested');
  546. $oFormNested = new BxDolFormNested('cmt_image', $aFormNested, 'cmt_submit');
  547. $oForm->aInputs['cmt_image']['storage_object'] = $this->_sStorageObject;
  548. $oForm->aInputs['cmt_image']['images_transcoder'] = $this->_sTranscoderPreview;
  549. $oForm->aInputs['cmt_image']['uploaders'] = array('sys_simple_cmts');
  550. $oForm->aInputs['cmt_image']['upload_buttons_titles'] = array('Simple' => 'camera');
  551. $oForm->aInputs['cmt_image']['multiple'] = true;
  552. $oForm->aInputs['cmt_image']['ghost_template'] = $oFormNested;
  553. }
  554. if(isset($oForm->aInputs['cmt_text'])) {
  555. $iCmtTextMin = (int)$this->_aSystem['chars_post_min'];
  556. $iCmtTextMax = (int)$this->_aSystem['chars_post_max'];
  557. $oForm->aInputs['cmt_text']['checker']['params'] = array($iCmtTextMin, $iCmtTextMax);
  558. $oForm->aInputs['cmt_text']['checker']['error'] = _t('_Please enter n1-n2 characters', $iCmtTextMin, $iCmtTextMax);
  559. }
  560. return $oForm;
  561. }
  562. protected function _getMoreLink($sCmts, $aBp = array(), $aDp = array())
  563. {
  564. $iStart = $iPerView = 0;
  565. switch($aBp['type']) {
  566. case BX_CMT_BROWSE_HEAD:
  567. case BX_CMT_BROWSE_POPULAR:
  568. case BX_CMT_BROWSE_CONNECTION:
  569. $iPerView = $aBp['per_view'];
  570. $iStart = $aBp['start'] + $iPerView;
  571. if($iStart >= $aBp['count'])
  572. return $sCmts;
  573. break;
  574. case BX_CMT_BROWSE_TAIL:
  575. $iPerView = $aBp['per_view'];
  576. $iStart = $aBp['start'] - $iPerView;
  577. if($iStart < 0) {
  578. $iPerView += $iStart;
  579. $iStart = 0;
  580. }
  581. if($iStart == 0 && $iPerView == 0)
  582. return $sCmts;
  583. break;
  584. }
  585. $bRoot = (int)$aBp['vparent_id'] <= 0;
  586. $sMore = BxDolTemplate::getInstance()->parseHtmlByName('comment_more.html', array(
  587. 'js_object' => $this->_sJsObjName,
  588. 'style_prefix' => $this->_sStylePrefix,
  589. 'bx_if:is_root' => array(
  590. 'condition' => $bRoot,
  591. 'content' => array()
  592. ),
  593. 'bx_if:is_button' => array(
  594. 'condition' => $bRoot,
  595. 'content' => array()
  596. ),
  597. 'parent_id' => $aBp['vparent_id'],
  598. 'start' => $iStart,
  599. 'per_view' => $iPerView,
  600. 'title' => _t('_load_more_' . ($aBp['vparent_id'] == 0 ? 'comments' : 'replies') . '_' . $aBp['type'])
  601. ));
  602. switch($aBp['type']) {
  603. case BX_CMT_BROWSE_HEAD:
  604. case BX_CMT_BROWSE_POPULAR:
  605. case BX_CMT_BROWSE_CONNECTION:
  606. $sCmts .= $sMore;
  607. break;
  608. case BX_CMT_BROWSE_TAIL:
  609. $sCmts = $sMore . $sCmts;
  610. break;
  611. }
  612. return $sCmts;
  613. }
  614. protected function _echoResultJson($a, $isAutoWrapForFormFileSubmit = false) {
  615. header('Content-type: text/html; charset=utf-8');
  616. require_once(BX_DIRECTORY_PATH_PLUGINS . 'Services_JSON.php');
  617. $oParser = new Services_JSON();
  618. $s = $oParser->encode($a);
  619. if ($isAutoWrapForFormFileSubmit && !empty($_FILES))
  620. $s = '<textarea>' . $s . '</textarea>'; // http://jquery.malsup.com/form/#file-upload
  621. echo $s;
  622. }
  623. }