BxBaseCmtsView.php 27 KB

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