BxBaseCmts.php 28 KB

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