BxBaseCmts.php 28 KB

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