BxBaseCmts.php 26 KB

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