BxBaseCmts.php 29 KB

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