BxBaseCmtsView.php 29 KB

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