BxBaseCmtsView.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. <?php
  2. /**
  3. * @package Dolphin Core
  4. * @copyright Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  5. * @license CC-BY - http://creativecommons.org/licenses/by/3.0/
  6. */
  7. defined('BX_DOL') or die('hack attempt');
  8. bx_import('BxDolCmts');
  9. bx_import('BxDolProfile');
  10. bx_import('BxTemplPaginate');
  11. /**
  12. * @see BxDolCmts
  13. */
  14. class BxBaseCmtsView extends BxDolCmts {
  15. var $_sJsObjName;
  16. var $_sStylePrefix;
  17. function BxBaseCmtsView( $sSystem, $iId, $iInit = 1 ) {
  18. BxDolCmts::BxDolCmts( $sSystem, $iId, $iInit );
  19. if(empty($sSystem))
  20. return;
  21. $this->_sJsObjName = 'oCmts' . ucfirst($sSystem) . $iId;
  22. $this->_sStylePrefix = isset($this->_aSystem['root_style_prefix']) ? $this->_aSystem['root_style_prefix'] : 'cmt';
  23. BxDolTemplate::getInstance()->addJsTranslation('_sys_txt_cmt_loading');
  24. }
  25. /**
  26. * Get comments css file string
  27. *
  28. * @return string
  29. */
  30. function getExtraCss ()
  31. {
  32. BxDolTemplate::getInstance()->addCss(array('cmts.css'));
  33. }
  34. /**
  35. * Get comments js file string
  36. *
  37. * @return string
  38. */
  39. function getExtraJs ()
  40. {
  41. BxDolTemplate::getInstance()->addJs(array('common_anim.js', 'jquery.form.js', 'BxDolCmts.js'));
  42. }
  43. /**
  44. * Get initialization section of comments box
  45. *
  46. * @return string
  47. */
  48. function getScript()
  49. {
  50. $sToggleAdd = '';
  51. $ret = '';
  52. $ret .= $sToggleAdd . "
  53. <script type=\"text/javascript\">
  54. var " . $this->_sJsObjName . " = new BxDolCmts({
  55. sObjName: '" . $this->_sJsObjName . "',
  56. sRootUrl: '" . BX_DOL_URL_ROOT . "',
  57. sSystem: '" . $this->getSystemName() . "',
  58. sSystemTable: '" . $this->_aSystem['table_cmts'] . "',
  59. iAuthorId: '" . $this->_getAuthorId() . "',
  60. iObjId: '" . $this->getId () . "',
  61. sPostFormPosition: '" . $this->_aSystem['post_form_position'] . "',
  62. sBrowseType: '" . $this->_sBrowseType . "',
  63. sDisplayType: '" . $this->_sDisplayType . "'});\n";
  64. $ret .= "</script>";
  65. $this->getExtraJs();
  66. $this->getExtraCss();
  67. BxDolTemplate::getInstance()->addJsTranslation(array(
  68. '_Error occured',
  69. '_Are you sure?'
  70. ));
  71. return $ret;
  72. }
  73. /**
  74. * get full comments block with initializations
  75. */
  76. function getCommentsBlock($iParentId = 0, $iVParentId = 0, $bInDesignbox = true) {
  77. $aBp = array('parent_id' => $iParentId, 'vparent_id' => $iVParentId);
  78. $sCmts = $this->getComments($aBp);
  79. $sCaption = _t('_cmt_block_comments_title', $this->_oQuery->getCommentsCount($this->_iId));
  80. $sContent = BxDolTemplate::getInstance()->parseHtmlByName('comments_block.html', array(
  81. 'system' => $this->_sSystem,
  82. 'id' => $this->getId(),
  83. 'bx_if:show_empty' => array(
  84. 'condition' => $sCmts == '',
  85. 'content' => array(
  86. 'style_prefix' => $this->_sStylePrefix
  87. )
  88. ),
  89. 'comments' => $sCmts,
  90. 'post_form_top' => $this->getFormBoxPost($aBp, array('type' => $this->_sDisplayType, 'position' => BX_CMT_PFP_TOP)),
  91. 'post_form_bottom' => $this->getFormBoxPost($aBp, array('type' => $this->_sDisplayType, 'position' => BX_CMT_PFP_BOTTOM)),
  92. 'script' => $this->getScript()
  93. ));
  94. return $bInDesignbox ? DesignBoxContent($sCaption, $sContent, BX_DB_PADDING_DEF, $this->_getControlsBox()) : $sContent;
  95. }
  96. /**
  97. * get comments list for specified parent comment
  98. *
  99. * @param array $aBp - browse params array
  100. * @param array $aDp - display params array
  101. *
  102. */
  103. function getComments($aBp = array(), $aDp = array())
  104. {
  105. $this->_prepareParams($aBp, $aDp);
  106. $aCmts = $this->getCommentsArray($aBp['vparent_id'], $aBp['order'], $aBp['start'], $aBp['per_view']);
  107. if(empty($aCmts) || !is_array($aCmts))
  108. return '';
  109. $sCmts = '';
  110. foreach($aCmts as $k => $aCmt)
  111. $sCmts .= $this->getComment($aCmt, $aBp, $aDp);
  112. $sCmts = $this->_getMoreLink($sCmts, $aBp, $aDp);
  113. return $sCmts;
  114. }
  115. /**
  116. * get comment view block with initializations
  117. */
  118. function getCommentBlock($iCmtId = 0)
  119. {
  120. return BxDolTemplate::getInstance()->parseHtmlByName('comment_block.html', array(
  121. 'system' => $this->_sSystem,
  122. 'id' => $this->getId(),
  123. 'comment' => $this->getComment($iCmtId, array('type' => $this->_sBrowseType), array('type' => BX_CMT_DISPLAY_THREADED)),
  124. 'script' => $this->getScript()
  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 = $sRet = '';
  140. if($aCmt['cmt_rated'] == -1 || $aCmt['cmt_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. $aImages = $this->_oQuery->getImages($this->_aSystem['system_id'], $aCmt['cmt_id']);
  179. if(!empty($aImages) && is_array($aImages)) {
  180. bx_import('BxDolImageTranscoder');
  181. $oTranscoder = BxDolImageTranscoder::getObjectInstance($this->_sTranscoderPreview);
  182. foreach($aImages as $aImage)
  183. $aTmplImages[] = array(
  184. 'style_prefix' => $this->_sStylePrefix,
  185. 'js_object' => $this->_sJsObjName,
  186. 'id' => $aImage['image_id'],
  187. 'image' => $oTranscoder->getImageUrl($aImage['image_id'])
  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. 'view_link' => bx_append_url_params($this->_sViewUrl, array(
  228. 'sys' => $this->_sSystem,
  229. 'id' => $this->_iId,
  230. 'cmt_id' => $aCmt['cmt_id']
  231. )),
  232. 'ago' => bx_time_js($aCmt['cmt_time']),
  233. 'bx_if:hide_rate_count' => array(
  234. 'condition' => (int)$aCmt['cmt_rate'] <= 0,
  235. 'content' => array()
  236. ),
  237. 'points' => _t(in_array($aCmt['cmt_rate'], array(-1, 0, 1)) ? '_N_point' : '_N_points', $aCmt['cmt_rate']),
  238. 'text' => $sText,
  239. 'bx_if:show_more' => array(
  240. 'condition' => !empty($sTextMore),
  241. 'content' => array(
  242. 'style_prefix' => $this->_sStylePrefix,
  243. 'js_object' => $this->_sJsObjName,
  244. 'text_more' => $sTextMore
  245. )
  246. ),
  247. 'bx_if:show_attached' => array(
  248. 'condition' => !empty($aTmplImages),
  249. 'content' => array(
  250. 'style_prefix' => $this->_sStylePrefix,
  251. 'bx_repeat:attached' => $aTmplImages
  252. )
  253. ),
  254. 'actions' => $sActions,
  255. 'replies' => $sReplies
  256. ));
  257. }
  258. function getFormBoxPost($aBp = array(), $aDp = array())
  259. {
  260. return $this->_getFormBox(BX_CMT_ACTION_POST, $aBp, $aDp);
  261. }
  262. function getFormBoxEdit($aBp = array(), $aDp = array())
  263. {
  264. return $this->_getFormBox(BX_CMT_ACTION_EDIT, $aBp, $aDp);
  265. }
  266. function getFormPost($iCmtParentId = 0)
  267. {
  268. return $this->_getFormPost($iCmtParentId);
  269. }
  270. function getFormEdit($aCmt)
  271. {
  272. return $this->_getFormEdit($aCmt);
  273. }
  274. function getPlusedBy($iCmtId)
  275. {
  276. $oTemplate = BxDolTemplate::getInstance();
  277. $aTmplUsers = array();
  278. $aUserIds = $this->_oQuery->getRatedBy($this->_aSystem['system_id'], $iCmtId);
  279. foreach($aUserIds as $iUserId) {
  280. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($iUserId);
  281. $aTmplUsers[] = array(
  282. 'style_prefix' => $this->_sStylePrefix,
  283. 'user_unit' => $sUserUnit
  284. );
  285. }
  286. bx_import('BxTemplStudioFunctions');
  287. $sContent = BxTemplStudioFunctions::getInstance()->transBox($oTemplate->parseHtmlByName('comment_pb_list.html', array(
  288. 'style_prefix' => $this->_sStylePrefix,
  289. 'bx_repeat:list' => $aTmplUsers
  290. )));
  291. return $oTemplate->parseHtmlByName('comment_plused_by.html', array(
  292. 'style_prefix' => $this->_sStylePrefix,
  293. 'id' => $this->_sSystem . '-plused-by',
  294. 'content' => $sContent
  295. ));
  296. }
  297. function getImage($iImgId)
  298. {
  299. $oTemplate = BxDolTemplate::getInstance();
  300. bx_import('BxDolStorage');
  301. $oStorage = BxDolStorage::getObjectInstance($this->_sStorageObject);
  302. bx_import('BxTemplStudioFunctions');
  303. $sContent = BxTemplStudioFunctions::getInstance()->transBox($oTemplate->parseHtmlByName('bx_img.html', array(
  304. 'src' => $oStorage->getFileUrlById($iImgId),
  305. 'bx_repeat:attrs' => array(
  306. array('key' => 'alt', 'value' => bx_html_attribute(_t('_cmt_view_attached_image'))),
  307. array('key' => 'title', 'value' => bx_html_attribute(_t('_cmt_close_attached_image'))),
  308. array('key' => 'onclick', 'value' => $this->_sJsObjName . '.hideImage(this);')
  309. )
  310. )));
  311. return $oTemplate->parseHtmlByName('comment_image.html', array(
  312. 'style_prefix' => $this->_sStylePrefix,
  313. 'id' => $this->_sSystem . '-attached-image',
  314. 'content' => $sContent
  315. ));
  316. }
  317. /**
  318. * private functions
  319. */
  320. protected function _getControlsBox()
  321. {
  322. $oTemplate = BxDolTemplate::getInstance();
  323. $sDisplay = '';
  324. $bDisplay = (int)$this->_aSystem['is_display_switch'] == 1;
  325. if($bDisplay) {
  326. $aDisplayLinks = array(
  327. array('id' => $this->_sSystem . '-flat', 'name' => $this->_sSystem . '-flat', 'class' => '', 'title' => '_cmt_display_flat', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeDisplay(this, \'flat\');'),
  328. array('id' => $this->_sSystem . '-threaded', 'name' => $this->_sSystem . '-threaded', 'class' => '', 'title' => '_cmt_display_threaded', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeDisplay(this, \'threaded\');')
  329. );
  330. bx_import('BxTemplMenuInteractive');
  331. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-display', 'menu_items' => $aDisplayLinks));
  332. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sDisplayType);
  333. $sDisplay = $oMenu->getCode();
  334. }
  335. $sBrowse = '';
  336. $bBrowse = (int)$this->_aSystem['is_browse_switch'] == 1;
  337. if($bBrowse) {
  338. $aBrowseLinks = array(
  339. array('id' => $this->_sSystem . '-tail', 'name' => $this->_sSystem . '-tail', 'class' => '', 'title' => '_cmt_browse_tail', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'tail\');'),
  340. array('id' => $this->_sSystem . '-head', 'name' => $this->_sSystem . '-head', 'class' => '', 'title' => '_cmt_browse_head', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'head\');'),
  341. array('id' => $this->_sSystem . '-popular', 'name' => $this->_sSystem . '-popular', 'class' => '', 'title' => '_cmt_browse_popular', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'popular\');'),
  342. array('id' => $this->_sSystem . '-connection', 'name' => $this->_sSystem . '-connection', 'class' => '', 'title' => '_cmt_browse_connection', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'connection\');')
  343. );
  344. bx_import('BxTemplMenuInteractive');
  345. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-browse', 'menu_items' => $aBrowseLinks));
  346. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sBrowseType);
  347. $sBrowse = $oMenu->getCode();
  348. }
  349. return $oTemplate->parseHtmlByName('comments_controls.html', array(
  350. 'display_switcher' => $bDisplay ? $sDisplay : '',
  351. 'bx_if:is_divider' => array(
  352. 'condition' => $bDisplay && $bBrowse,
  353. 'content' => array(
  354. 'style_prefix' => $this->_sStylePrefix,
  355. )
  356. ),
  357. 'browse_switcher' => $bBrowse ? $sBrowse : '',
  358. ));
  359. }
  360. protected function _getActionsBox(&$aCmt, $aDp = array())
  361. {
  362. $oTemplate = BxDolTemplate::getInstance();
  363. $iUserId = $this->_getAuthorId();
  364. $isEditAllowedPermanently = ($aCmt['cmt_author_id'] == $iUserId && $this->isEditAllowed()) || $this->isEditAllowedAll();
  365. $isRemoveAllowedPermanently = ($aCmt['cmt_author_id'] == $iUserId && $this->isRemoveAllowed()) || $this->isRemoveAllowedAll();
  366. $sManagePopupId = $sManagePopupText = '';
  367. if($isEditAllowedPermanently || $isRemoveAllowedPermanently) {
  368. $aMenu = array(
  369. array('name' => 'cmt-edit', 'icon' => 'pencil', 'onclick' => $this->_sJsObjName . ".cmtEdit(this, " . $aCmt['cmt_id'] . ")", 'title' => _t('_Edit')),
  370. array('name' => 'cmt-delete', 'icon' => 'remove', 'onclick' => $this->_sJsObjName . ".cmtRemove(this, " . $aCmt['cmt_id'] . ")", 'title' => _t('_Delete')),
  371. );
  372. bx_import('BxTemplStudioMenu');
  373. $oMenu = new BxTemplStudioMenu(array('template' => 'menu_vertical.html', 'menu_items' => $aMenu));
  374. bx_import('BxTemplStudioFunctions');
  375. $sManagePopupText = BxTemplStudioFunctions::getInstance()->transBox($oTemplate->parseHtmlByName('comment_manage.html', array(
  376. 'style_prefix' => $this->_sStylePrefix,
  377. 'content' => $oMenu->getCode()
  378. )));
  379. }
  380. $bRated = (int)$aCmt['cmt_rated'] > 0;
  381. return $oTemplate->parseHtmlByName('comment_actions.html', array(
  382. 'id' => $aCmt['cmt_id'],
  383. 'style_prefix' => $this->_sStylePrefix,
  384. 'view_link' => bx_append_url_params($this->_sViewUrl, array(
  385. 'sys' => $this->_sSystem,
  386. 'id' => $this->_iId,
  387. 'cmt_id' => $aCmt['cmt_id']
  388. )),
  389. 'points' => _t($aCmt['cmt_rate'] == 1 || $aCmt['cmt_rate'] == -1 ? '_N_point' : '_N_points', $aCmt['cmt_rate']),
  390. 'bx_if:show_reply' => array(
  391. 'condition' => $this->isPostReplyAllowed(),
  392. 'content' => array(
  393. 'js_object' => $this->_sJsObjName,
  394. 'style_prefix' => $this->_sStylePrefix,
  395. 'id' => $aCmt['cmt_id'],
  396. 'title_reply' => bx_html_attribute(_t(isset($aCmt['cmt_type']) && $aCmt['cmt_type'] == 'comment' ? '_Comment_to_this_comment' : '_Reply_to_this_comment'))
  397. )
  398. ),
  399. 'bx_if:show_rate' => array(
  400. 'condition' => $this->isRatable() && $this->isRateAllowed(),
  401. 'content' => array(
  402. 'js_object' => $this->_sJsObjName,
  403. 'style_prefix' => $this->_sStylePrefix,
  404. 'id' => $aCmt['cmt_id'],
  405. 'bx_if:hide_rate_plus' => array(
  406. 'condition' => $bRated,
  407. 'content' => array()
  408. ),
  409. 'bx_if:hide_rate_minus' => array(
  410. 'condition' => !$bRated,
  411. 'content' => array()
  412. )
  413. )
  414. ),
  415. 'bx_if:show_manage' => array(
  416. 'condition' => $isEditAllowedPermanently || $isRemoveAllowedPermanently,
  417. 'content' => array(
  418. 'js_object' => $this->_sJsObjName,
  419. 'style_prefix' => $this->_sStylePrefix,
  420. 'id' => $aCmt['cmt_id'],
  421. 'popup_id' => $this->_sSystem . '-manage' . $aCmt['cmt_id'],
  422. 'popup_text' => $sManagePopupText
  423. )
  424. )
  425. ));
  426. }
  427. protected function _getFormBox($sType, $aBp, $aDp)
  428. {
  429. $iCmtParentId = isset($aBp['parent_id']) ? (int)$aBp['parent_id'] : 0;
  430. $sPosition = isset($aDp['position']) ? $aDp['position'] : '';
  431. if(!$this->isPostReplyAllowed())
  432. return '';
  433. list($sAuthorName, $sAuthorLink, $sAuthorIcon) = $this->_getAuthorInfo();
  434. $sPositionSystem = $this->_aSystem['post_form_position'];
  435. if(!empty($sPosition) && $sPositionSystem != $sPosition)
  436. return '';
  437. $sMethod = '_getForm' . ucfirst($sType);
  438. $aForm = $this->$sMethod($iCmtParentId);
  439. $bAuthorIcon = !empty($sAuthorIcon);
  440. return BxDolTemplate::getInstance()->parseHtmlByName('comment_reply_box.html', array(
  441. 'js_object' => $this->_sJsObjName,
  442. 'style_prefix' => $this->_sStylePrefix,
  443. 'bx_if:show_class' => array(
  444. 'condition' => !empty($sPosition),
  445. 'content' => array(
  446. 'class' => $this->_sStylePrefix . '-reply-' . $sPosition
  447. )
  448. ),
  449. 'bx_if:show_icon' => array(
  450. 'condition' => $bAuthorIcon,
  451. 'content' => array(
  452. 'author_icon' => $sAuthorIcon
  453. )
  454. ),
  455. 'bx_if:show_icon_empty' => array(
  456. 'condition' => !$bAuthorIcon,
  457. 'content' => array()
  458. ),
  459. 'bx_if:show_author_link' => array(
  460. 'condition' => !empty($sAuthorLink),
  461. 'content' => array(
  462. 'author_link' => $sAuthorLink,
  463. 'author_name' => $sAuthorName
  464. )
  465. ),
  466. 'bx_if:show_author_text' => array(
  467. 'condition' => empty($sAuthorLink),
  468. 'content' => array(
  469. 'author_name' => $sAuthorName
  470. )
  471. ),
  472. 'form' => $aForm['form'],
  473. 'form_id' => $aForm['form_id'],
  474. ));
  475. }
  476. protected function _getFormPost($iCmtParentId = 0)
  477. {
  478. $oForm = $this->_getFormObject(BX_CMT_ACTION_POST, $iCmtParentId);
  479. $oForm->aInputs['cmt_parent_id']['value'] = $iCmtParentId;
  480. $oForm->initChecker();
  481. if($oForm->isSubmittedAndValid()) {
  482. $iCmtAuthorId = $this->_getAuthorId();
  483. $iCmtParentId = $oForm->getCleanValue('cmt_parent_id');
  484. $sCmtText = $oForm->getCleanValue('cmt_text');
  485. if($this->_isSpam($sCmtText))
  486. return array('msg' => _t('_sys_spam_detected', BX_DOL_URL_ROOT . 'contact.php'));
  487. $sCmtText = $this->_prepareTextForSave ($sCmtText);
  488. $oForm->setSubmittedValue('cmt_text', $sCmtText, $oForm->aFormAttrs['method']);
  489. $iLevel = 0;
  490. $iCmtVisualParentId = 0;
  491. if((int)$iCmtParentId > 0) {
  492. $aParent = $this->getCommentRow($iCmtParentId);
  493. $iLevel = (int)$aParent['cmt_level'] + 1;
  494. $iCmtVisualParentId = $iLevel > $this->getMaxLevel() ? $aParent['cmt_vparent_id'] : $iCmtParentId;
  495. }
  496. $iCmtId = (int)$oForm->insert(array('cmt_vparent_id' => $iCmtVisualParentId, 'cmt_object_id' => $this->_iId, 'cmt_author_id' => $iCmtAuthorId, 'cmt_level' => $iLevel, 'cmt_time' => time()));
  497. if($iCmtId != 0) {
  498. $aImages = $oForm->getCleanValue('cmt_image');
  499. if(!empty($aImages) || is_array($aImages)) {
  500. bx_import('BxDolStorage');
  501. $oStorage = BxDolStorage::getObjectInstance($this->_sStorageObject);
  502. foreach($aImages as $iImageId)
  503. if($this->_oQuery->saveImages($this->_aSystem['system_id'], $iCmtId, $iImageId))
  504. $oStorage->afterUploadCleanup($iImageId, $iCmtAuthorId);
  505. }
  506. if($iCmtParentId)
  507. $this->_oQuery->updateRepliesCount($iCmtParentId, 1);
  508. $this->_triggerComment();
  509. $this->isPostReplyAllowed(true);
  510. bx_import('BxDolAlerts');
  511. $oZ = new BxDolAlerts($this->_sSystem, 'commentPost', $this->getId(), $iCmtAuthorId, array('comment_id' => $iCmtId, 'comment_author_id' => $iCmtAuthorId));
  512. $oZ->alert();
  513. return array('id' => $iCmtId, 'parent_id' => $iCmtParentId);
  514. }
  515. return array('msg' => _t('_cmt_err_cannot_perform_action'));
  516. }
  517. return array('form' => $oForm->getCode(), 'form_id' => $oForm->id);
  518. }
  519. protected function _getFormEdit($iCmtId)
  520. {
  521. $aCmt = $this->_oQuery->getCommentSimple ($this->getId(), $iCmtId);
  522. if(!$aCmt)
  523. return array('msg' => _t('_No such comment'));
  524. $iCmtAuthorId = $this->_getAuthorId();
  525. if(!$this->isEditAllowedAll() && ($aCmt['cmt_author_id'] != $iCmtAuthorId || !$this->isEditAllowed()))
  526. return array('msg' => $aCmt['cmt_author_id'] == $iCmtAuthorId && !$this->isEditAllowed() ? strip_tags($this->msgErrEditAllowed()) : _t('_Access denied'));
  527. $oForm = $this->_getFormObject(BX_CMT_ACTION_EDIT, $aCmt['cmt_id']);
  528. $oForm->initChecker($aCmt);
  529. if($oForm->isSubmittedAndValid()) {
  530. $sCmtText = $oForm->getCleanValue('cmt_text');
  531. if($this->_isSpam($sCmtText))
  532. return array('msg' => _t('_sys_spam_detected', BX_DOL_URL_ROOT . 'contact.php'));
  533. $sCmtText = $this->_prepareTextForSave ($sCmtText);
  534. $oForm->setSubmittedValue('cmt_text', $sCmtText, $oForm->aFormAttrs['method']);
  535. if($oForm->update($iCmtId)) {
  536. if ($aCmt['cmt_author_id'] == $iCmtAuthorId)
  537. $this->isEditAllowed(true);
  538. bx_import('BxDolAlerts');
  539. $oZ = new BxDolAlerts($this->_sSystem, 'commentUpdated', $this->getId(), $iCmtAuthorId, array('comment_id' => $aCmt['cmt_id'], 'comment_author_id' => $aCmt['cmt_author_id']));
  540. $oZ->alert();
  541. return array('id' => $iCmtId, 'text' => $sCmtText);
  542. }
  543. return array('msg' => _t('_cmt_err_cannot_perform_action'));
  544. }
  545. return array('form' => $oForm->getCode(), 'form_id' => $oForm->id);
  546. }
  547. protected function _getFormObject($sAction, $iId)
  548. {
  549. $sActionCap = ucfirst($sAction);
  550. $sDisplayName = '_sFormDisplay' . $sActionCap;
  551. bx_import('BxDolForm');
  552. $oForm = BxDolForm::getObjectInstance($this->_sFormObject, $this->$sDisplayName);
  553. $oForm->setId(sprintf($oForm->aFormAttrs['id'], $sAction, $this->_sSystem, $iId));
  554. $oForm->setName(sprintf($oForm->aFormAttrs['name'], $sAction, $this->_sSystem, $iId));
  555. $oForm->aParams['db']['table'] = $this->_aSystem['table_cmts'];
  556. $oForm->aInputs['sys']['value'] = $this->_sSystem;
  557. $oForm->aInputs['id']['value'] = $this->_iId;
  558. $oForm->aInputs['action']['value'] = 'Submit' . $sActionCap . 'Form';
  559. if(isset($oForm->aInputs['cmt_image'])) {
  560. $aFormNested = array(
  561. 'params' =>array(
  562. 'nested_form_template' => 'comments_uploader_nfw.html'
  563. ),
  564. 'inputs' => array(),
  565. );
  566. bx_import('BxDolFormNested');
  567. $oFormNested = new BxDolFormNested('cmt_image', $aFormNested, 'cmt_submit');
  568. $oForm->aInputs['cmt_image']['storage_object'] = $this->_sStorageObject;
  569. $oForm->aInputs['cmt_image']['images_transcoder'] = $this->_sTranscoderPreview;
  570. $oForm->aInputs['cmt_image']['uploaders'] = $this->_aImageUploaders;
  571. $oForm->aInputs['cmt_image']['upload_buttons_titles'] = array('Simple' => 'camera');
  572. $oForm->aInputs['cmt_image']['multiple'] = true;
  573. $oForm->aInputs['cmt_image']['ghost_template'] = $oFormNested;
  574. }
  575. if(isset($oForm->aInputs['cmt_text'])) {
  576. $iCmtTextMin = (int)$this->_aSystem['chars_post_min'];
  577. $iCmtTextMax = (int)$this->_aSystem['chars_post_max'];
  578. $oForm->aInputs['cmt_text']['checker']['params'] = array($iCmtTextMin, $iCmtTextMax);
  579. $oForm->aInputs['cmt_text']['checker']['error'] = _t('_Please enter n1-n2 characters', $iCmtTextMin, $iCmtTextMax);
  580. }
  581. return $oForm;
  582. }
  583. protected function _getMoreLink($sCmts, $aBp = array(), $aDp = array())
  584. {
  585. $iStart = $iPerView = 0;
  586. switch($aBp['type']) {
  587. case BX_CMT_BROWSE_HEAD:
  588. case BX_CMT_BROWSE_POPULAR:
  589. case BX_CMT_BROWSE_CONNECTION:
  590. $iPerView = $aBp['per_view'];
  591. $iStart = $aBp['start'] + $iPerView;
  592. if($iStart >= $aBp['count'])
  593. return $sCmts;
  594. break;
  595. case BX_CMT_BROWSE_TAIL:
  596. $iPerView = $aBp['per_view'];
  597. $iStart = $aBp['start'] - $iPerView;
  598. if($iStart < 0) {
  599. $iPerView += $iStart;
  600. $iStart = 0;
  601. }
  602. if($iStart == 0 && $iPerView == 0)
  603. return $sCmts;
  604. break;
  605. }
  606. $bRoot = (int)$aBp['vparent_id'] <= 0;
  607. $sMore = BxDolTemplate::getInstance()->parseHtmlByName('comment_more.html', array(
  608. 'js_object' => $this->_sJsObjName,
  609. 'style_prefix' => $this->_sStylePrefix,
  610. 'bx_if:is_root' => array(
  611. 'condition' => $bRoot,
  612. 'content' => array()
  613. ),
  614. 'bx_if:is_button' => array(
  615. 'condition' => $bRoot,
  616. 'content' => array()
  617. ),
  618. 'parent_id' => $aBp['vparent_id'],
  619. 'start' => $iStart,
  620. 'per_view' => $iPerView,
  621. 'title' => _t('_load_more_' . ($aBp['vparent_id'] == 0 ? 'comments' : 'replies') . '_' . $aBp['type'])
  622. ));
  623. switch($aBp['type']) {
  624. case BX_CMT_BROWSE_HEAD:
  625. case BX_CMT_BROWSE_POPULAR:
  626. case BX_CMT_BROWSE_CONNECTION:
  627. $sCmts .= $sMore;
  628. break;
  629. case BX_CMT_BROWSE_TAIL:
  630. $sCmts = $sMore . $sCmts;
  631. break;
  632. }
  633. return $sCmts;
  634. }
  635. protected function _echoResultJson($a, $isAutoWrapForFormFileSubmit = false) {
  636. header('Content-type: text/html; charset=utf-8');
  637. require_once(BX_DIRECTORY_PATH_PLUGINS . 'Services_JSON.php');
  638. $oParser = new Services_JSON();
  639. $s = $oParser->encode($a);
  640. if ($isAutoWrapForFormFileSubmit && !empty($_FILES))
  641. $s = '<textarea>' . $s . '</textarea>'; // http://jquery.malsup.com/form/#file-upload
  642. echo $s;
  643. }
  644. }