BxBaseCmtsView.php 25 KB

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