BxBaseCmtsView.php 28 KB

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