1
0

BxBaseCmts.php 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * @see BxDolCmts
  11. */
  12. class BxBaseCmts extends BxDolCmts
  13. {
  14. protected static $_sTmplContentElementBlock;
  15. protected static $_sTmplContentElementInline;
  16. protected static $_sTmplContentDoCommentLabel;
  17. protected static $_sTmplContentCounter;
  18. protected $_sTmplNameItem;
  19. protected $_sTmplNameItemContent;
  20. protected $_sJsObjClass;
  21. protected $_sJsObjName;
  22. protected $_sStylePrefix;
  23. protected $_aHtmlIds;
  24. protected $_aElementDefaults;
  25. function __construct( $sSystem, $iId, $iInit = true, $oTemplate = false)
  26. {
  27. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  28. if (empty($sSystem))
  29. return;
  30. $this->_sTmplNameItem = 'comment.html';
  31. $this->_sTmplNameItemContent = 'comment_content.html';
  32. $this->_sJsObjClass = 'BxDolCmts';
  33. $this->_sJsObjName = 'oCmts' . bx_gen_method_name($sSystem, array('_' , '-')) . $iId;
  34. $this->_sStylePrefix = isset($this->_aSystem['root_style_prefix']) ? $this->_aSystem['root_style_prefix'] : 'cmt';
  35. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
  36. $this->_aHtmlIds = array(
  37. 'main' => 'bx-cmt-' . $sHtmlId,
  38. 'counter' => 'bx-cmt-counter-' . $sHtmlId
  39. );
  40. $this->_aElementDefaults = array(
  41. 'show_do_comment_as_button' => false,
  42. 'show_do_comment_as_button_small' => false,
  43. 'show_do_comment_image' => false,
  44. 'show_do_comment_icon' => true,
  45. 'show_do_comment_label' => false,
  46. 'show_counter' => true,
  47. 'show_counter_empty' => false
  48. );
  49. if(empty(self::$_sTmplContentElementBlock))
  50. self::$_sTmplContentElementBlock = $this->_oTemplate->getHtml('comment_element_block.html');
  51. if(empty(self::$_sTmplContentElementInline))
  52. self::$_sTmplContentElementInline = $this->_oTemplate->getHtml('comment_element_inline.html');
  53. if(empty(self::$_sTmplContentDoCommentLabel))
  54. self::$_sTmplContentDoCommentLabel = $this->_oTemplate->getHtml('comment_do_comment_label.html');
  55. if(empty(self::$_sTmplContentCounter))
  56. self::$_sTmplContentCounter = $this->_oTemplate->getHtml('comment_counter.html');
  57. $this->_oTemplate->addJsTranslation('_sys_txt_cmt_loading');
  58. }
  59. /**
  60. * Add comments CSS/JS
  61. */
  62. public function addCssJs ()
  63. {
  64. $oForm = BxDolForm::getObjectInstance($this->_sFormObject, $this->_sFormDisplayPost);
  65. $oForm->addCssJs();
  66. }
  67. public function getJsObjectName()
  68. {
  69. return $this->_sJsObjName;
  70. }
  71. /**
  72. * Get initialization section of comments box
  73. *
  74. * @return string
  75. */
  76. public function getJsScript($aBp = array(), $aDp = array())
  77. {
  78. $bMinPostForm = isset($aDp['min_post_form']) ? $aDp['min_post_form'] : $this->_bMinPostForm;
  79. $aParams = array(
  80. 'sObjName' => $this->_sJsObjName,
  81. 'sRootUrl' => BX_DOL_URL_ROOT,
  82. 'sSystem' => $this->getSystemName(),
  83. 'iAuthorId' => $this->_getAuthorId(),
  84. 'iObjId' => $this->getId(),
  85. 'sBaseUrl' => $this->getBaseUrl(),
  86. 'sPostFormPosition' => $this->_aSystem['post_form_position'],
  87. 'sBrowseType' => $this->_sBrowseType,
  88. 'sDisplayType' => $this->_sDisplayType,
  89. 'iDisplayStructure' => isset($aDp['structure']) && !empty($aDp['structure']) ? 1 : 0,
  90. 'iMinPostForm' => $bMinPostForm ? 1 : 0,
  91. 'sStylePrefix' => $this->_sStylePrefix,
  92. );
  93. $this->addCssJs();
  94. return $this->_oTemplate->_wrapInTagJsCode("if(window['" . $this->_sJsObjName . "'] == undefined) var " . $this->_sJsObjName . " = new " . $this->_sJsObjClass . "(" . json_encode($aParams) . "); " . $this->_sJsObjName . ".cmtInit();");
  95. }
  96. /**
  97. * get full comments block with initializations
  98. */
  99. function getCommentsBlock($aBp = array(), $aDp = array())
  100. {
  101. $mixedResult = $this->isViewAllowed();
  102. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  103. return $mixedResult;
  104. $this->_getParams($aBp, $aDp);
  105. //add live update
  106. $this->actionResumeLiveUpdate();
  107. $sServiceCall = BxDolService::getSerializedService('system', 'get_live_update', array($this->_sSystem, $this->_iId, $this->_getAuthorId(), '{count}'), 'TemplCmtsServices');
  108. BxDolLiveUpdates::getInstance()->add($this->_sSystem . '_live_updates_cmts_' . $this->_iId, 1, $sServiceCall);
  109. //add live update
  110. $sCaption = _t($this->_aT['block_comments_title'], $this->getCommentsCountAll());
  111. $sContent = $this->_oTemplate->parseHtmlByName('comments_block.html', array(
  112. 'system' => $this->_sSystem,
  113. 'list_anchor' => $this->getListAnchor(),
  114. 'id' => $this->getId(),
  115. 'comments' => $this->getComments($aBp, $aDp),
  116. 'post_form_top' => $this->getFormBoxPost($aBp, array_merge($aDp, array('type' => $this->_sDisplayType, 'position' => BX_CMT_PFP_TOP))),
  117. 'post_form_bottom' => $this->getFormBoxPost($aBp, array_merge($aDp, array('type' => $this->_sDisplayType, 'position' => BX_CMT_PFP_BOTTOM))),
  118. 'view_image_popup' => $this->_getViewImagePopup(),
  119. 'script' => $this->getJsScript($aBp, $aDp)
  120. ));
  121. return $aDp['in_designbox'] ? DesignBoxContent($sCaption, $sContent, BX_DB_DEF, $this->_getControlsBox()) : array(
  122. 'title' => $sCaption,
  123. 'content' => $sContent,
  124. 'menu' => $this->_getControlsBox(),
  125. );
  126. }
  127. /**
  128. * get comments list for specified parent comment
  129. *
  130. * @param array $aBp - browse params array
  131. * @param array $aDp - display params array
  132. *
  133. */
  134. function getComments($aBp = array(), $aDp = array())
  135. {
  136. $this->_prepareParams($aBp, $aDp);
  137. $aCmts = $this->getCommentsArray($aBp['vparent_id'], $aBp['filter'], $aBp['order'], $aBp['start'], $aBp['per_view']);
  138. if(empty($aCmts) || !is_array($aCmts)) {
  139. if((int)$aBp['parent_id'] == 0 && !isLogged()) {
  140. $oPermalink = BxDolPermalinks::getInstance();
  141. return MsgBox(_t('_cmt_msg_login_required', $oPermalink->permalink('page.php?i=login'), $oPermalink->permalink('page.php?i=create-account')));
  142. }
  143. return isset($aDp['show_empty']) && $aDp['show_empty'] === true ? $this->_getEmpty($aDp) : '';
  144. }
  145. $sCmts = '';
  146. foreach($aCmts as $k => $aCmt)
  147. $sCmts .= $this->getComment($aCmt, $aBp, $aDp);
  148. $sCmts = $this->_getMoreLink($sCmts, $aBp, $aDp);
  149. return $sCmts;
  150. }
  151. public function getCommentsByStructure($aBp = array(), $aDp = array())
  152. {
  153. if(empty($aDp['structure']))
  154. return isset($aDp['show_empty']) && $aDp['show_empty'] === true ? $this->_getEmpty($aDp) : '';
  155. $aBp['count'] = count($aDp['structure']);
  156. $this->_prepareParams($aBp, $aDp);
  157. $aDp['structure'] = array_slice($aDp['structure'], $aBp['start'], $aBp['per_view'], true);
  158. $sCmts = '';
  159. foreach($aDp['structure'] as $iCmtId => $aCmt)
  160. $sCmts .= $this->getComment($iCmtId, $aBp, $aDp);
  161. $sCmts = $this->_getMoreLink($sCmts, $aBp, $aDp);
  162. return $sCmts;
  163. }
  164. /**
  165. * get comment view block with initializations
  166. */
  167. function getCommentBlock($iCmtId = 0, $aBp = array(), $aDp = array())
  168. {
  169. $mixedResult = $this->isViewAllowed();
  170. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  171. return $mixedResult;
  172. $aBp = array_merge(array('type' => $this->_sBrowseType), $aBp);
  173. $aDp = array_merge(array('type' => BX_CMT_DISPLAY_THREADED), $aDp);
  174. //--- Beg: Using pregenerated structure
  175. $mixedStructure = $this->getCommentStructure((int)$iCmtId, $aBp, $aDp);
  176. if($mixedStructure !== false)
  177. $aDp['structure'] = $mixedStructure;
  178. //--- End: Using pregenerated structure
  179. $sComment = $this->getComment($iCmtId, $aBp, $aDp);
  180. if (!$sComment)
  181. return '';
  182. return $this->_oTemplate->parseHtmlByName('comment_block.html', array(
  183. 'system' => $this->_sSystem,
  184. 'id' => $this->getId(),
  185. 'comment' => $sComment,
  186. 'view_image_popup' => $this->_getViewImagePopup(),
  187. 'script' => $this->getJsScript($aBp, $aDp)
  188. ));
  189. }
  190. /**
  191. * get one just posted comment
  192. *
  193. * @param int $iCmtId - comment id
  194. * @return string
  195. */
  196. function getComment($mixedCmt, $aBp = array(), $aDp = array())
  197. {
  198. $iUserId = $this->_getAuthorId();
  199. $aCmt = !is_array($mixedCmt) ? $this->getCommentRow((int)$mixedCmt) : $mixedCmt;
  200. if (!$aCmt)
  201. return '';
  202. list($sAuthorName, $sAuthorLink, $sAuthorIcon) = $this->_getAuthorInfo($aCmt['cmt_author_id']);
  203. $sClass = $sClassCnt = '';
  204. if(isset($aCmt['vote_rate']) && (float)$aCmt['vote_rate'] < $this->_aSystem['viewing_threshold']) {
  205. $this->_oTemplate->pareseHtmlByName('comment_hidden.html', array(
  206. 'js_object' => $this->_sJsObjName,
  207. 'id' => $aCmt['cmt_id'],
  208. 'title' => bx_process_output(_t('_hidden_comment', $sAuthorName)),
  209. 'bx_if:show_replies' => array(
  210. 'condition' => $aCmt['cmt_replies'] > 0,
  211. 'content' => array(
  212. 'replies' => _t('_Show N replies', $aCmt['cmt_replies'])
  213. )
  214. )
  215. ));
  216. $sClass = ' ' . $this->_sStylePrefix . '-hidden';
  217. }
  218. if($aCmt['cmt_author_id'] == $iUserId)
  219. $sClass .= ' ' . $this->_sStylePrefix . '-mine';
  220. if(!empty($aDp['blink']) && in_array($aCmt['cmt_id'], $aDp['blink']))
  221. $sClass .= ' ' . $this->_sStylePrefix . '-blink';
  222. if(!empty($aDp['class_comment']))
  223. $sClass .= ' ' . $aDp['class_comment'];
  224. if(!empty($aDp['class_comment_content']))
  225. $sClassCnt .= ' ' . $aDp['class_comment_content'];
  226. $sActions = $this->_getActionsBox($aCmt, $aDp);
  227. $aTmplReplyTo = array();
  228. if((int)$aCmt['cmt_parent_id'] != 0) {
  229. $aParent = $this->getCommentRow($aCmt['cmt_parent_id']);
  230. if(!empty($aParent) && is_array($aParent)) {
  231. $oProfile = $this->_getAuthorObject($aParent['cmt_author_id']);
  232. $sParAuthorName = $oProfile->getDisplayName();
  233. $sParAuthorUnit = $oProfile->getUnit(0, array('template' => array('name' => 'unit_wo_info_links', 'size' => 'icon')));
  234. $aTmplReplyTo = array(
  235. 'style_prefix' => $this->_sStylePrefix,
  236. 'par_cmt_link' => $this->getItemUrl($aCmt['cmt_parent_id']),
  237. 'par_cmt_title' => bx_html_attribute(_t('_in_reply_to_x', $sParAuthorName)),
  238. 'par_cmt_author' => $sParAuthorName,
  239. 'par_cmt_author_unit' => $sParAuthorUnit
  240. );
  241. }
  242. }
  243. $sReplies = '';
  244. if(!empty($aDp)) {
  245. $aDp['show_empty'] = false;
  246. if(!empty($aDp['structure'][$aCmt['cmt_id']]) && is_array($aDp['structure'][$aCmt['cmt_id']])) {
  247. if(!empty($aDp['structure'][$aCmt['cmt_id']]['items'])) {
  248. $aDp['structure'] = $aDp['structure'][$aCmt['cmt_id']]['items'];
  249. $sReplies = $this->getCommentsByStructure(array('parent_id' => $aCmt['cmt_id'], 'type' => $aBp['type']), $aDp);
  250. }
  251. }
  252. else if((int)$aCmt['cmt_replies'] > 0 && $aDp['type'] == BX_CMT_DISPLAY_THREADED)
  253. $sReplies = $this->getComments(array('parent_id' => $aCmt['cmt_id'], 'vparent_id' => $aCmt['cmt_id'], 'type' => $aBp['type']), $aDp);
  254. }
  255. $aTmplVarsMeta = array();
  256. if(!empty($this->_sMenuObjMeta)) {
  257. $oMenuMeta = BxDolMenu::getObjectInstance($this->_sMenuObjMeta, $this->_oTemplate);
  258. if($oMenuMeta) {
  259. $oMenuMeta->setCmtsData($this, $aCmt['cmt_id']);
  260. $aTmplVarsMeta = array(
  261. 'style_prefix' => $this->_sStylePrefix,
  262. 'meta' => $oMenuMeta->getCode()
  263. );
  264. }
  265. }
  266. $sContent = $this->_getContent($aCmt);
  267. $sResult = $this->_oTemplate->parseHtmlByName($this->_sTmplNameItem, array_merge(array(
  268. 'system' => $this->_sSystem,
  269. 'style_prefix' => $this->_sStylePrefix,
  270. 'js_object' => $this->_sJsObjName,
  271. 'id' => $aCmt['cmt_id'],
  272. 'anchor' => $this->getItemAnchor($aCmt['cmt_id']),
  273. 'class' => $sClass,
  274. 'class_cnt' => $sClassCnt,
  275. 'bx_if:show_reply_to' => array(
  276. 'condition' => !empty($aTmplReplyTo),
  277. 'content' => $aTmplReplyTo
  278. ),
  279. 'bx_if:meta' => array(
  280. 'condition' => !empty($aTmplVarsMeta),
  281. 'content' => $aTmplVarsMeta
  282. ),
  283. 'content' => $sContent,
  284. 'actions' => $sActions,
  285. 'replies' => $sReplies,
  286. ), $this->_getTmplVarsAuthor($aCmt)));
  287. bx_alert('system', 'view_comment', $aCmt['cmt_id'], 0, array('comment' => $aCmt, 'system' => $this->_sSystem, 'override_result' => &$sResult));
  288. return $sResult;
  289. }
  290. public function getCommentStructure($iCmtId, $aBp = array(), $aDp = array())
  291. {
  292. $aRoot = $this->getCommentRow((int)$iCmtId);
  293. $aBps = $aBp;
  294. if(!empty($aRoot))
  295. $aBps['parent_id'] = $aRoot['cmt_id'];
  296. $this->_prepareStructureBp($aDp['type'], $aBps);
  297. $iLevel = 0;
  298. $aStructure = array();
  299. $this->_getStructure($aRoot, $aBps, $iLevel, $aStructure);
  300. return !empty($aStructure) && is_array($aStructure) ? $aStructure : false;
  301. }
  302. function getCommentSearch($iCmtId, &$sAddon)
  303. {
  304. $aBp = array();
  305. $aDp = array(
  306. 'type' => BX_CMT_DISPLAY_FLAT,
  307. 'view_only' => true
  308. );
  309. if(empty($sAddon))
  310. $sAddon = $this->getJsScript($aBp, $aDp);
  311. return $this->_oTemplate->parseHtmlByName('comment_search.html', array(
  312. 'comment' => $this->getComment($iCmtId, $aBp, $aDp),
  313. 'view_image_popup' => $this->_getViewImagePopup(),
  314. ));
  315. }
  316. /**
  317. * get one comment for "Live Search"
  318. *
  319. * @param int $iCmtId - comment id
  320. * @return string
  321. */
  322. function getCommentLiveSearch($mixedCmt, $aParams = array())
  323. {
  324. $aCmt = !is_array($mixedCmt) ? $this->getCommentRow((int)$mixedCmt) : $mixedCmt;
  325. list($sAuthorName, $sAuthorLink, $sAuthorIcon) = $this->_getAuthorInfo($aCmt['cmt_author_id']);
  326. $bAuthorIcon = !empty($sAuthorIcon);
  327. $sViewLink = $this->getViewUrl($aCmt['cmt_id']);
  328. if(empty($sViewLink))
  329. $sViewLink = '#';
  330. return $this->_oTemplate->parseHtmlByName('comment_live_search.html', array(
  331. 'bx_if:show_icon' => array(
  332. 'condition' => $bAuthorIcon,
  333. 'content' => array(
  334. 'author_icon' => $sAuthorIcon,
  335. 'view_link' => $sViewLink
  336. )
  337. ),
  338. 'bx_if:show_icon_empty' => array(
  339. 'condition' => !$bAuthorIcon,
  340. 'content' => array()
  341. ),
  342. 'view_link' => $sViewLink,
  343. 'text' => BxTemplFunctions::getInstance()->getStringWithLimitedLength(strip_tags($aCmt['cmt_text']), $this->_sSnippetLenthLiveSearch),
  344. 'sample' => isset($aParams['txt_sample_single']) ? _t($aParams['txt_sample_single']) : ''
  345. ));
  346. }
  347. function getFormBoxPost($aBp = array(), $aDp = array())
  348. {
  349. return $this->_getFormBox(BX_CMT_ACTION_POST, $aBp, $aDp);
  350. }
  351. function getFormBoxEdit($aBp = array(), $aDp = array())
  352. {
  353. return $this->_getFormBox(BX_CMT_ACTION_EDIT, $aBp, $aDp);
  354. }
  355. function getFormPost($iCmtParentId = 0, $aDp = array())
  356. {
  357. return $this->_getFormPost($iCmtParentId, $aDp);
  358. }
  359. function getFormEdit($iCmtId, $aDp = array())
  360. {
  361. return $this->_getFormEdit($iCmtId, $aDp);
  362. }
  363. function getLiveUpdate($iCountOld = 0, $iCountNew = 0)
  364. {
  365. $iCount = (int)$iCountNew - (int)$iCountOld;
  366. if($iCount < 0)
  367. return '';
  368. $aComments = $this->_oQuery->getCommentsBy(array('type' => 'latest', 'object_id' => $this->_iId, 'author' => $this->_getAuthorId(), 'others' => 1, 'start' => '0', 'per_page' => $iCount));
  369. if(empty($aComments) || !is_array($aComments))
  370. return '';
  371. $aComment = array_shift($aComments);
  372. if(empty($aComment) || !is_array($aComment))
  373. return '';
  374. $sJsObject = $this->getJsObjectName();
  375. return $this->_oTemplate->parseHtmlByName('comments_lu_button.html', array(
  376. 'style_prefix' => $this->_sStylePrefix,
  377. 'html_id' => $this->getNotificationId(),
  378. 'onclick_show' => "javascript:" . $sJsObject . ".goToBtn(this, '" . $this->getItemAnchor($aComment['cmt_id']) . "', '" . $aComment['cmt_id'] . "');",
  379. ));
  380. }
  381. function getLiveUpdates($iCountOld = 0, $iCountNew = 0)
  382. {
  383. $bShowAll = true;
  384. $bShowActions = false;
  385. $iCount = (int)$iCountNew - (int)$iCountOld;
  386. if($iCount < 0)
  387. return '';
  388. $aComments = $this->_oQuery->getCommentsBy(array('type' => 'latest', 'object_id' => $this->_iId, 'author' => $this->_getAuthorId(), 'others' => 1, 'start' => '0', 'per_page' => $iCount));
  389. if(empty($aComments) || !is_array($aComments))
  390. return '';
  391. $sJsObject = $this->getJsObjectName();
  392. $iUserId = $this->_getAuthorId();
  393. $bModerator = $this->isModerator();
  394. $aComments = array_reverse($aComments);
  395. $iComments = count($aComments);
  396. $aTmplVarsNotifs = array();
  397. foreach($aComments as $iIndex => $aComment) {
  398. $iCommentId = $aComment['cmt_id'];
  399. $sShowOnClick = "javascript:" . $sJsObject . ".goTo(this, '" . $this->getItemAnchor($iCommentId) . "', '" . $iCommentId . "');";
  400. $sReplyOnClick = "javascript:" . $sJsObject . ".goToAndReply(this, '" . $this->getItemAnchor($iCommentId) . "', '" . $iCommentId . "');";
  401. $iAuthorId = (int)$aComment['cmt_author_id'];
  402. if($iAuthorId < 0) {
  403. if(abs($iAuthorId) == $iUserId)
  404. continue;
  405. else if($bModerator)
  406. $iAuthorId *= -1;
  407. }
  408. $oAuthor = $this->_getAuthorObject($iAuthorId);
  409. $sAuthorName = $oAuthor->getDisplayName();
  410. $aTmplVarsNotifs[] = array(
  411. 'bx_if:show_as_hidden' => array(
  412. 'condition' => !$bShowAll && $iIndex < ($iComments - 1),
  413. 'content' => array(),
  414. ),
  415. 'item' => $this->_oTemplate->parseHtmlByName('comments_lu_notifications.html', array(
  416. 'style_prefix' => $this->_sStylePrefix,
  417. 'onclick_show' => $sShowOnClick,
  418. 'onclick_reply' => $sReplyOnClick,
  419. 'author_link' => $oAuthor->getUrl(),
  420. 'author_title' => bx_html_attribute($sAuthorName),
  421. 'author_name' => $sAuthorName,
  422. 'author_unit' => $oAuthor->getUnit(0, array('template' => 'unit_wo_info_links')),
  423. 'text' => _t('_cmt_txt_added_sample')
  424. )),
  425. 'bx_if:show_previous' => array(
  426. 'condition' => $bShowActions && $iIndex > 0,
  427. 'content' => array(
  428. 'onclick_previous' => $sJsObject . '.previousLiveUpdate(this)'
  429. )
  430. ),
  431. 'bx_if:show_close' => array(
  432. 'condition' => $bShowActions,
  433. 'content' => array(
  434. 'onclick_close' => $sJsObject . '.hideLiveUpdate(this)'
  435. )
  436. )
  437. );
  438. }
  439. return $this->_oTemplate->parseHtmlByName('popup_chain.html', array(
  440. 'html_id' => $this->getNotificationId(),
  441. 'bx_repeat:items' => $aTmplVarsNotifs
  442. ));
  443. }
  444. public function getElementBlock($aParams = array())
  445. {
  446. $aParams['usage'] = BX_CMT_USAGE_BLOCK;
  447. return $this->getElement($aParams);
  448. }
  449. public function getElementInline($aParams = array())
  450. {
  451. $aParams['usage'] = BX_CMT_USAGE_INLINE;
  452. return $this->getElement($aParams);
  453. }
  454. public function getElement($aParams = array())
  455. {
  456. $aParams = array_merge($this->_aElementDefaults, $aParams);
  457. $bShowDoCommentAsButtonSmall = isset($aParams['show_do_comment_as_button_small']) && $aParams['show_do_comment_as_button_small'] == true;
  458. $bShowDoCommentAsButton = !$bShowDoCommentAsButtonSmall && isset($aParams['show_do_comment_as_button']) && $aParams['show_do_comment_as_button'] == true;
  459. $bShowCounterEmpty = isset($aParams['show_counter_empty']) && $aParams['show_counter_empty'] == true;
  460. $iObjectId = $this->getId();
  461. $iAuthorId = $this->_getAuthorId();
  462. $iAuthorIp = $this->_getAuthorIp();
  463. $iCount = $this->getCommentsCountAll();
  464. $bCount = (int)$iCount != 0;
  465. $isAllowedComment = $this->isPostAllowed();
  466. //--- Do Comment
  467. $bTmplVarsDoComment = $this->_isShowDoComment($aParams, $isAllowedComment, $bCount);
  468. $aTmplVarsDoComment = array();
  469. if($bTmplVarsDoComment) {
  470. $sClass = '';
  471. if($bShowDoCommentAsButton)
  472. $sClass = 'bx-btn';
  473. else if ($bShowDoCommentAsButtonSmall)
  474. $sClass = 'bx-btn bx-btn-small';
  475. if(!$isAllowedComment)
  476. $sClass .= $bShowDoCommentAsButton || $bShowDoCommentAsButtonSmall ? ' bx-btn-disabled' : 'bx-cmts-disabled';
  477. $aTmplVarsDoComment = array(
  478. 'style_prefix' => $this->_sStylePrefix,
  479. 'do_comment' => $this->_oTemplate->parseLink($this->getListUrl(), $this->_getLabelDo($aParams), array(
  480. 'class' => $this->_sStylePrefix . '-do-comment ' . $this->_sStylePrefix . '-dc ' . $sClass,
  481. 'title' => _t($this->_getTitleDo())
  482. )),
  483. );
  484. }
  485. //--- Counter
  486. $bTmplVarsCounter = $this->_isShowCounter($aParams, $isAllowedComment, $bCount);
  487. $aTmplVarsCounter = array();
  488. if($bTmplVarsCounter)
  489. $aTmplVarsCounter = array(
  490. 'style_prefix' => $this->_sStylePrefix,
  491. 'bx_if:show_hidden' => array(
  492. 'condition' => !$bShowCounterEmpty && !$bCount,
  493. 'content' => array()
  494. ),
  495. 'counter' => $this->getCounter($aParams)
  496. );
  497. if(!$bTmplVarsDoComment && !$bTmplVarsCounter)
  498. return '';
  499. $sTmplName = $this->{'_getTmplElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_SCORE_USAGE_DEFAULT)}();
  500. return $this->_oTemplate->parseHtmlByContent($sTmplName, array(
  501. 'style_prefix' => $this->_sStylePrefix,
  502. 'html_id' => $this->_aHtmlIds['main'],
  503. 'class' => $this->_sStylePrefix . ($bShowDoCommentAsButton ? '-button' : '') . ($bShowDoCommentAsButtonSmall ? '-button-small' : ''),
  504. 'count' => $iCount,
  505. 'bx_if:show_do_comment' => array(
  506. 'condition' => $bTmplVarsDoComment,
  507. 'content' => $aTmplVarsDoComment
  508. ),
  509. 'bx_if:show_counter' => array(
  510. 'condition' => $bTmplVarsCounter,
  511. 'content' => $aTmplVarsCounter
  512. ),
  513. 'script' => ''
  514. ));
  515. }
  516. public function getCounter($aParams = array())
  517. {
  518. $bShowEmpty = isset($aParams['show_counter_empty']) && $aParams['show_counter_empty'] == true;
  519. $bShowDoCommentAsButtonSmall = isset($aParams['show_do_comment_as_button_small']) && $aParams['show_do_comment_as_button_small'] == true;
  520. $bShowDoCommentAsButton = !$bShowDoCommentAsButtonSmall && isset($aParams['show_do_comment_as_button']) && $aParams['show_do_comment_as_button'] == true;
  521. $iCount = (int)$this->getCommentsCountAll();
  522. if($iCount == 0 && !$bShowEmpty)
  523. return '';
  524. $sClass = $this->_sStylePrefix . '-counter';
  525. if($bShowDoCommentAsButtonSmall)
  526. $sClass .= ' bx-btn-small-height';
  527. if($bShowDoCommentAsButton)
  528. $sClass .= ' bx-btn-height';
  529. $iCmtsLimit = 5;
  530. $aCmts = $this->_oQuery->getCommentsBy(array('type' => 'object_id', 'object_id' => $this->getId(), 'order_way' => 'desc', 'start' => 0, 'per_page' => $iCmtsLimit * 4));
  531. $aCmts = array_reverse($aCmts);
  532. $aTmplVarsProfiles = array();
  533. foreach($aCmts as $aCmt) {
  534. $iAuthor = (int)$aCmt['cmt_author_id'];
  535. if(array_key_exists($iAuthor, $aTmplVarsProfiles))
  536. continue;
  537. $oAuthor = BxDolProfile::getInstanceMagic($iAuthor);
  538. if(!$oAuthor)
  539. continue;
  540. $aTmplVarsProfiles[$iAuthor] = array(
  541. 'icon' => $oAuthor->getUnit(0, array('template' => array('name' => 'unit_wo_info_links', 'size' => 'icon')))
  542. );
  543. if(count($aTmplVarsProfiles) >= $iCmtsLimit)
  544. break;
  545. }
  546. $aTmplVarsProfiles = array_values($aTmplVarsProfiles);
  547. $sHref = !empty($aParams['overwrite_counter_link_href']) ? $aParams['overwrite_counter_link_href'] : $this->getListUrl();
  548. $sOnclick = '';
  549. if(!empty($aParams['overwrite_counter_link_onclick']))
  550. $sOnclick = $aParams['overwrite_counter_link_onclick'];
  551. return $this->_oTemplate->parseHtmlByContent($this->_getTmplCounter(), array(
  552. 'id' => $this->_aHtmlIds['counter'],
  553. 'class' => $sClass,
  554. 'href' => $sHref,
  555. 'bx_if:show_onclick' => array(
  556. 'condition' => !empty($sOnclick),
  557. 'content' => array(
  558. 'onclick' => $sOnclick
  559. )
  560. ),
  561. 'content' => $this->_getCounterLabel($iCount),
  562. 'bx_repeat:profiles' => $aTmplVarsProfiles,
  563. 'bx_if:show_icon' => array(
  564. 'condition' => $bShowEmpty || !empty($aTmplVarsProfiles),
  565. 'content' => array(
  566. 'sp' => $this->_sStylePrefix
  567. )
  568. )
  569. ));
  570. }
  571. protected function _getLabelDo($aParams = array())
  572. {
  573. return $this->_oTemplate->parseHtmlByContent($this->_getTmplLabelDo(), array(
  574. 'style_prefix' => $this->_sStylePrefix,
  575. 'bx_if:show_image' => array(
  576. 'condition' => isset($aParams['show_do_comment_image']) && $aParams['show_do_comment_image'] == true,
  577. 'content' => array(
  578. 'style_prefix' => $this->_sStylePrefix,
  579. 'src' => $this->_getImageDo()
  580. )
  581. ),
  582. 'bx_if:show_icon' => array(
  583. 'condition' => isset($aParams['show_do_comment_icon']) && $aParams['show_do_comment_icon'] == true,
  584. 'content' => array(
  585. 'style_prefix' => $this->_sStylePrefix,
  586. 'name' => $this->_getIconDo()
  587. )
  588. ),
  589. 'bx_if:show_text' => array(
  590. 'condition' => isset($aParams['show_do_comment_label']) && $aParams['show_do_comment_label'] == true,
  591. 'content' => array(
  592. 'style_prefix' => $this->_sStylePrefix,
  593. 'text' => _t($this->_getTitleDo())
  594. )
  595. )
  596. ));
  597. }
  598. protected function _getCounterLabel($iCount)
  599. {
  600. return (int)$iCount != 0 ? _t('_cmt_txt_counter', $iCount) : _t('_cmt_txt_counter_empty');
  601. }
  602. protected function _getTmplElementBlock()
  603. {
  604. return self::$_sTmplContentElementBlock;
  605. }
  606. protected function _getTmplElementInline()
  607. {
  608. return self::$_sTmplContentElementInline;
  609. }
  610. protected function _getTmplLabelDo()
  611. {
  612. return self::$_sTmplContentDoCommentLabel;
  613. }
  614. protected function _getTmplCounter()
  615. {
  616. return self::$_sTmplContentCounter;
  617. }
  618. /**
  619. * private functions
  620. */
  621. protected function _getControlsBox()
  622. {
  623. $sDisplay = '';
  624. $bDisplay = (int)$this->_aSystem['is_display_switch'] == 1;
  625. if($bDisplay) {
  626. $aDisplayLinks = array(
  627. array('id' => $this->_sSystem . '-flat', 'name' => $this->_sSystem . '-flat', 'class' => '', 'title' => '_cmt_display_flat', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeDisplay(this, \'flat\');'),
  628. array('id' => $this->_sSystem . '-threaded', 'name' => $this->_sSystem . '-threaded', 'class' => '', 'title' => '_cmt_display_threaded', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeDisplay(this, \'threaded\');')
  629. );
  630. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-display', 'menu_items' => $aDisplayLinks));
  631. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sDisplayType);
  632. $sDisplay = $oMenu->getCode();
  633. }
  634. $sBrowseType = '';
  635. $bBrowseType = (int)$this->_aSystem['is_browse_switch'] == 1;
  636. if($bBrowseType) {
  637. $aBrowseLinks = array(
  638. array('id' => $this->_sSystem . '-tail', 'name' => $this->_sSystem . '-tail', 'class' => '', 'title' => '_cmt_browse_tail', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'tail\');'),
  639. array('id' => $this->_sSystem . '-head', 'name' => $this->_sSystem . '-head', 'class' => '', 'title' => '_cmt_browse_head', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'head\');'),
  640. array('id' => $this->_sSystem . '-popular', 'name' => $this->_sSystem . '-popular', 'class' => '', 'title' => '_cmt_browse_popular', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'popular\');'),
  641. );
  642. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-browse', 'menu_items' => $aBrowseLinks));
  643. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sBrowseType);
  644. $sBrowseType = $oMenu->getCode();
  645. }
  646. $sBrowseFilter = '';
  647. $bBrowseFilter = (int)$this->_aSystem['is_browse_filter'] == 1;
  648. if($bBrowseFilter) {
  649. $aFilterLinks = array(
  650. array('id' => $this->_sSystem . '-all', 'name' => $this->_sSystem . '-all', 'class' => '', 'title' => '_cmt_browse_all', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'all\');'),
  651. array('id' => $this->_sSystem . '-friends', 'name' => $this->_sSystem . '-friends', 'class' => '', 'title' => '_cmt_browse_friends', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'friends\');'),
  652. array('id' => $this->_sSystem . '-subscriptions', 'name' => $this->_sSystem . '-subscriptions', 'class' => '', 'title' => '_cmt_browse_subscriptions', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'subscriptions\');')
  653. );
  654. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-filter', 'menu_items' => $aFilterLinks));
  655. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sBrowseFilter);
  656. $sBrowseFilter = $oMenu->getCode();
  657. }
  658. return $this->_oTemplate->parseHtmlByName('comments_controls.html', array(
  659. 'display_switcher' => $bDisplay ? $sDisplay : '',
  660. 'bx_if:is_divider_1' => array(
  661. 'condition' => $bDisplay && $bBrowseType,
  662. 'content' => array(
  663. 'style_prefix' => $this->_sStylePrefix,
  664. )
  665. ),
  666. 'browse_switcher' => $bBrowseType ? $sBrowseType : '',
  667. 'bx_if:is_divider_2' => array(
  668. 'condition' => $bBrowseType && $bBrowseFilter,
  669. 'content' => array(
  670. 'style_prefix' => $this->_sStylePrefix,
  671. )
  672. ),
  673. 'filter_switcher' => $bBrowseFilter ? $sBrowseFilter : '',
  674. ));
  675. }
  676. protected function _getActionsBox(&$aCmt, $aDp = array())
  677. {
  678. $bViewOnly = isset($aDp['view_only']) && $aDp['view_only'] === true;
  679. $bDynamicMode = isset($aDp['dynamic_mode']) && $aDp['dynamic_mode'] === true;
  680. $sMenuActions = '';
  681. if(!$bViewOnly) {
  682. $oMenuActions = BxDolMenu::getObjectInstance($this->_sMenuObjActions);
  683. $oMenuActions->setCmtsData($this, $aCmt['cmt_id']);
  684. $oMenuActions->setDynamicMode($bDynamicMode);
  685. $sMenuActions = $oMenuActions->getCode();
  686. }
  687. return $this->_oTemplate->parseHtmlByName('comment_actions.html', array(
  688. 'id' => $aCmt['cmt_id'],
  689. 'js_object' => $this->_sJsObjName,
  690. 'style_prefix' => $this->_sStylePrefix,
  691. 'menu_actions' => $sMenuActions,
  692. ));
  693. }
  694. protected function _getFormBox($sType, $aBp, $aDp)
  695. {
  696. $iCmtParentId = isset($aBp['parent_id']) ? (int)$aBp['parent_id'] : 0;
  697. $sPosition = isset($aDp['position']) ? $aDp['position'] : '';
  698. $bQuote = isset($aDp['quote']) && (bool)$aDp['quote'];
  699. $bFormMin = $iCmtParentId == 0 && isset($aDp['min_post_form']) && (bool)$aDp['min_post_form'];
  700. $sPositionSystem = $this->_aSystem['post_form_position'];
  701. if(!empty($sPosition) && $sPositionSystem != $sPosition)
  702. return '';
  703. $sClass = '';
  704. if(!empty($sPosition))
  705. $sClass .= ' ' . $this->_sStylePrefix . '-reply-' . $sPosition;
  706. if($bQuote)
  707. $sClass .= ' ' . $this->_sStylePrefix . '-reply-quote';
  708. if($bFormMin)
  709. $sClass .= ' ' . $this->_sStylePrefix . '-reply-min';
  710. if(!empty($aDp['class']))
  711. $sClass .= ' ' . $aDp['class'];
  712. $sClassBody = '';
  713. if(!empty($aDp['class_body']))
  714. $sClassBody .= ' ' . $aDp['class_body'];
  715. $aTmplVarsFormMin = array();
  716. if($bFormMin) {
  717. list($sAuthorName, $sAuthorLink, $sAuthorIcon, $sAuthorUnit) = $this->_getAuthorInfo($this->_getAuthorId());
  718. $oForm = new BxTemplFormView(array());
  719. $aInputPlaceholder = array(
  720. 'type' => 'text',
  721. 'name' => 'comment',
  722. 'caption' => '',
  723. 'attrs' => array(
  724. 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtShowForm(this)',
  725. 'placeholder' => _t($this->_aT['txt_min_form_placeholder'], $sAuthorName)
  726. ),
  727. 'value' => '',
  728. );
  729. $sClassBodyMin = '';
  730. if(!empty($aDp['class_body_min']))
  731. $sClassBodyMin .= ' ' . $aDp['class_body_min'];
  732. $aTmplVarsFormMin = array(
  733. 'js_object' => $this->_sJsObjName,
  734. 'class_body_min' => $sClassBodyMin,
  735. 'style_prefix' => $this->_sStylePrefix,
  736. 'author_unit' => $sAuthorUnit,
  737. 'placeholder' => $oForm->genRow($aInputPlaceholder)
  738. );
  739. }
  740. $aForm = $this->{'_getForm' . ucfirst($sType)}($iCmtParentId, $aDp);
  741. if(empty($aForm['form']))
  742. return !empty($aForm['msg']) && (isLogged() || $iCmtParentId != 0 || $this->getCommentsCount() > 0) ? MsgBox($aForm['msg']) : '';
  743. return $this->_oTemplate->parseHtmlByName('comment_reply_box.html', array(
  744. 'js_object' => $this->_sJsObjName,
  745. 'style_prefix' => $this->_sStylePrefix,
  746. 'class' => $sClass,
  747. 'class_body' => $sClassBody,
  748. 'bx_if:show_form_min' => array(
  749. 'condition' => $bFormMin,
  750. 'content' => $aTmplVarsFormMin
  751. ),
  752. 'form' => $aForm['form'],
  753. 'form_id' => $aForm['form_id'],
  754. ));
  755. }
  756. protected function _getFormAdd($aValues)
  757. {
  758. $iCmtAuthorId = isset($aValues['cmt_author_id']) ? (int)$aValues['cmt_author_id'] : $this->_getAuthorId();
  759. $iCmtParentId = isset($aValues['cmt_parent_id']) ? (int)$aValues['cmt_parent_id'] : 0;
  760. $oForm = $this->_getForm(BX_CMT_ACTION_POST, $iCmtParentId);
  761. $oForm->aFormAttrs['method'] = BX_DOL_FORM_METHOD_SPECIFIC;
  762. $oForm->aParams['csrf']['disable'] = true;
  763. $oForm->aInputs['cmt_parent_id']['value'] = $iCmtParentId;
  764. if(!empty($oForm->aParams['db']['submit_name'])) {
  765. $sSubmitName = false;
  766. if(is_array($oForm->aParams['db']['submit_name']))
  767. foreach($oForm->aParams['db']['submit_name'] as $sVal) {
  768. if(isset($oForm->aInputs[$sVal])) {
  769. $sSubmitName = $sVal;
  770. break;
  771. }
  772. }
  773. else
  774. $sSubmitName = $oForm->aParams['db']['submit_name'];
  775. if($sSubmitName && isset($oForm->aInputs[$sSubmitName]))
  776. $aValues[$sSubmitName] = $oForm->aInputs[$sSubmitName]['value'];
  777. }
  778. $oForm->initChecker(array(), $aValues);
  779. if(!$oForm->isSubmittedAndValid())
  780. return array('code' => 1, 'message' => '_sys_txt_error_occured');
  781. $iLevel = 0;
  782. $iCmtVisualParentId = 0;
  783. if((int)$iCmtParentId > 0) {
  784. $aParent = $this->getCommentRow($iCmtParentId);
  785. $iLevel = (int)$aParent['cmt_level'] + 1;
  786. $iCmtVisualParentId = $iLevel > $this->getMaxLevel() ? $aParent['cmt_vparent_id'] : $iCmtParentId;
  787. }
  788. $iCmtId = (int)$oForm->insert(array('cmt_vparent_id' => $iCmtVisualParentId, 'cmt_object_id' => $this->_iId, 'cmt_author_id' => $iCmtAuthorId, 'cmt_level' => $iLevel, 'cmt_time' => time()));
  789. if(!$iCmtId) {
  790. if(!$oForm->isValid())
  791. return array('code' => 1, 'message' => '_sys_txt_error_occured');
  792. else
  793. return array('code' => 2, 'message' => '_cmt_err_cannot_perform_action');
  794. }
  795. $iCmtUniqId = $this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId, $iCmtAuthorId);
  796. if($iCmtParentId) {
  797. $this->_oQuery->updateRepliesCount($iCmtParentId, 1);
  798. if(!BxDolModuleQuery::getInstance()->isEnabledByName('bx_notifications'))
  799. $this->_sendNotificationEmail($iCmtId, $iCmtParentId);
  800. }
  801. $this->_triggerComment();
  802. if($this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  803. $oMetatags->metaAdd($iCmtUniqId, $aValues['cmt_text']);
  804. $mixedResult = $this->onPostAfter($iCmtId);
  805. if($mixedResult === false)
  806. return array('code' => 2, 'message' => '_cmt_err_cannot_perform_action');
  807. return $mixedResult;
  808. }
  809. protected function _getFormPost($iCmtParentId = 0, $aDp = array())
  810. {
  811. $bCmtParentId = !empty($iCmtParentId);
  812. if(!$bCmtParentId && !$this->isPostAllowed())
  813. return array('msg' => $this->msgErrPostAllowed());
  814. if($bCmtParentId && !$this->isReplyAllowed($iCmtParentId))
  815. return array('msg' => $this->msgErrReplyAllowed());
  816. $bDynamic = isset($aDp['dynamic_mode']) && (bool)$aDp['dynamic_mode'];
  817. $bQuote = isset($aDp['quote']) && (bool)$aDp['quote'];
  818. $oForm = $this->_getForm(BX_CMT_ACTION_POST, $iCmtParentId);
  819. $oForm->aInputs['cmt_parent_id']['value'] = $iCmtParentId;
  820. if($bQuote) {
  821. $aCmtParent = $this->getCommentRow((int)$iCmtParentId);
  822. if(!empty($aCmtParent['cmt_text']))
  823. $oForm->aInputs['cmt_text']['value'] = $this->_oTemplate->parseHtmlByName('comment_quote.html', array(
  824. 'content' => $aCmtParent['cmt_text']
  825. ));
  826. }
  827. $oForm->initChecker();
  828. if($oForm->isSubmittedAndValid()) {
  829. $iCmtAuthorId = $this->_getAuthorId();
  830. $iCmtParentId = (int)$oForm->getCleanValue('cmt_parent_id');
  831. $sCmtText = $oForm->getCleanValue('cmt_text');
  832. $aParent = array();
  833. if($iCmtParentId > 0) {
  834. $aParent = $this->getCommentRow($iCmtParentId);
  835. if(empty($aParent) || !is_array($aParent)) {
  836. $iCmtParentId = 0;
  837. $oForm->setSubmittedValue('cmt_parent_id', $iCmtParentId, $oForm->aFormAttrs['method']);
  838. }
  839. }
  840. $iLevel = 0;
  841. $iCmtVisualParentId = 0;
  842. if($iCmtParentId > 0) {
  843. $iLevel = (int)$aParent['cmt_level'] + 1;
  844. $iCmtVisualParentId = $iLevel > $this->getMaxLevel() ? $aParent['cmt_vparent_id'] : $iCmtParentId;
  845. }
  846. $iCmtId = (int)$oForm->insert(array('cmt_vparent_id' => $iCmtVisualParentId, 'cmt_object_id' => $this->_iId, 'cmt_author_id' => $iCmtAuthorId, 'cmt_level' => $iLevel, 'cmt_time' => time()));
  847. if($iCmtId != 0) {
  848. $iCmtUniqId = $this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId, $iCmtAuthorId);
  849. if($this->isAttachImageEnabled())
  850. $oForm->processImages($this, 'cmt_image', $iCmtUniqId, $iCmtId, $iCmtAuthorId, true);
  851. if($iCmtParentId > 0) {
  852. $this->_oQuery->updateRepliesCount($iCmtParentId, 1);
  853. if(!BxDolModuleQuery::getInstance()->isEnabledByName('bx_notifications'))
  854. $this->_sendNotificationEmail($iCmtId, $iCmtParentId);
  855. }
  856. $this->_triggerComment();
  857. if($iCmtParentId > 0)
  858. $this->isReplyAllowed($iCmtParentId, true);
  859. else
  860. $this->isPostAllowed(true);
  861. if($this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  862. $oMetatags->metaAdd($iCmtUniqId, $sCmtText);
  863. if(($mixedResult = $this->onPostAfter($iCmtId)) !== false)
  864. return $mixedResult;
  865. }
  866. return array('msg' => _t('_cmt_err_cannot_perform_action'));
  867. }
  868. return array('form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id);
  869. }
  870. protected function _getFormEdit($iCmtId, $aDp = array())
  871. {
  872. $bDynamic = isset($aDp['dynamic_mode']) && (bool)$aDp['dynamic_mode'];
  873. $aCmt = $this->getCommentSimple($iCmtId);
  874. if(!$aCmt)
  875. return array('msg' => _t('_No such comment'));
  876. $iCmtAuthorId = $this->_getAuthorId();
  877. if(!$this->isEditAllowed($aCmt))
  878. return array('msg' => $aCmt['cmt_author_id'] == $iCmtAuthorId ? strip_tags($this->msgErrEditAllowed()) : _t('_Access denied'));
  879. $oForm = $this->_getForm(BX_CMT_ACTION_EDIT, $aCmt['cmt_id']);
  880. $oForm->initChecker($aCmt);
  881. if($oForm->isSubmittedAndValid()) {
  882. $sCmtText = $oForm->getCleanValue('cmt_text');
  883. if($oForm->update($iCmtId) !== false) {
  884. $iCmtUniqId = $this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId, (int)$aCmt['cmt_author_id']);
  885. if($this->isAttachImageEnabled())
  886. $oForm->processImages($this, 'cmt_image', $iCmtUniqId, $iCmtId, $iCmtAuthorId, false);
  887. $this->isEditAllowed($aCmt, true);
  888. if($this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  889. $oMetatags->metaAdd($iCmtUniqId, $sCmtText);
  890. if(($mixedResult = $this->onEditAfter($iCmtId)) !== false)
  891. return $mixedResult;
  892. }
  893. return array('msg' => _t('_cmt_err_cannot_perform_action'));
  894. }
  895. return array('form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id);
  896. }
  897. protected function _getForm($sAction, $iId)
  898. {
  899. $oForm = $this->_getFormObject($sAction);
  900. $oForm->setId(sprintf($oForm->getAttributeMask('id'), $sAction, $this->_sSystem, $iId));
  901. $oForm->setName(sprintf($oForm->getAttributeMask('name'), $sAction, $this->_sSystem, $iId));
  902. $oForm->aParams['db']['table'] = $this->_aSystem['table'];
  903. $oForm->aInputs['sys']['value'] = $this->_sSystem;
  904. $oForm->aInputs['id']['value'] = $this->_iId;
  905. $oForm->aInputs['action']['value'] = 'Submit' . ucfirst($sAction) . 'Form';
  906. if(!$this->isAttachImageEnabled())
  907. unset($oForm->aInputs['cmt_image']);
  908. if(isset($oForm->aInputs['cmt_text'])) {
  909. $oForm->aInputs['cmt_text']['html'] = $this->_aSystem['html'];
  910. $oForm->aInputs['cmt_text']['db']['pass'] = $this->isHtml() ? 'XssHtml' : 'XssMultiline';
  911. $iCmtTextMin = (int)$this->_aSystem['chars_post_min'];
  912. $iCmtTextMax = (int)$this->_aSystem['chars_post_max'];
  913. $oForm->aInputs['cmt_text']['checker']['params'] = array($iCmtTextMin, $iCmtTextMax);
  914. $oForm->aInputs['cmt_text']['checker']['error'] = _t('_Please enter n1-n2 characters', $iCmtTextMin, $iCmtTextMax);
  915. }
  916. if($sAction == BX_CMT_ACTION_EDIT && isset($oForm->aInputs['cmt_controls']))
  917. foreach($oForm->aInputs['cmt_controls'] as $mixedKey => $mixedValue) {
  918. if(!is_numeric($mixedKey) || empty($mixedValue['name']) || $mixedValue['name'] != 'cmt_cancel')
  919. continue;
  920. if(!isset($oForm->aInputs['cmt_controls'][$mixedKey]['attrs']))
  921. $oForm->aInputs['cmt_controls'][$mixedKey]['attrs'] = array();
  922. $oForm->aInputs['cmt_controls'][$mixedKey]['attrs']['onclick'] = $this->_sJsObjName . '.cmtEdit(this, ' . $iId . ', false)';
  923. }
  924. return $oForm;
  925. }
  926. protected function _getContent($aCmt)
  927. {
  928. $sAttachments = $this->_getAttachments($aCmt);
  929. $sReactions = '';
  930. if(($oReaction = $this->getReactionObject($aCmt['cmt_unique_id'])) !== false)
  931. $sReactions = $oReaction->getCounter(array(
  932. 'show_counter' => true
  933. ));
  934. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameItemContent, array_merge(array(
  935. 'style_prefix' => $this->_sStylePrefix,
  936. 'js_object' => $this->_sJsObjName,
  937. 'bx_if:show_attached' => array(
  938. 'condition' => !empty($sAttachments),
  939. 'content' => array(
  940. 'style_prefix' => $this->_sStylePrefix,
  941. 'attached' => $sAttachments
  942. )
  943. ),
  944. 'reactions' => $sReactions,
  945. ), $this->_getTmplVarsText($aCmt)));
  946. }
  947. protected function _getMoreLink($sCmts, $aBp = array(), $aDp = array())
  948. {
  949. $iStart = $iPerView = 0;
  950. switch($aBp['type']) {
  951. case BX_CMT_BROWSE_HEAD:
  952. case BX_CMT_BROWSE_POPULAR:
  953. case BX_CMT_BROWSE_CONNECTION:
  954. $iPerView = $aBp['per_view'];
  955. $iStart = $aBp['start'] + $iPerView;
  956. if($iStart >= $aBp['count'])
  957. return $sCmts;
  958. break;
  959. case BX_CMT_BROWSE_TAIL:
  960. $iPerView = $aBp['per_view'];
  961. $iStart = $aBp['start'] - $iPerView;
  962. if($iStart < 0) {
  963. $iPerView += $iStart;
  964. $iStart = 0;
  965. }
  966. if($iStart == 0 && $iPerView == 0)
  967. return $sCmts;
  968. break;
  969. }
  970. $sParentKey = isset($aBp['vparent_id']) ? 'vparent_id' : 'parent_id';
  971. $bRoot = (int)$aBp[$sParentKey] <= 0;
  972. $sMore = $this->_oTemplate->parseHtmlByName('comment_more.html', array(
  973. 'js_object' => $this->_sJsObjName,
  974. 'style_prefix' => $this->_sStylePrefix,
  975. 'bx_if:is_root' => array(
  976. 'condition' => $bRoot,
  977. 'content' => array()
  978. ),
  979. 'parent_id' => $aBp[$sParentKey],
  980. 'start' => $iStart,
  981. 'per_view' => $iPerView,
  982. 'title' => _t('_cmt_load_more_' . ($aBp[$sParentKey] == 0 ? 'comments' : 'replies') . '_' . $aBp['type'])
  983. ));
  984. switch($aBp['type']) {
  985. case BX_CMT_BROWSE_HEAD:
  986. case BX_CMT_BROWSE_POPULAR:
  987. case BX_CMT_BROWSE_CONNECTION:
  988. $sCmts .= $sMore;
  989. break;
  990. case BX_CMT_BROWSE_TAIL:
  991. $sCmts = $sMore . $sCmts;
  992. break;
  993. }
  994. return $sCmts;
  995. }
  996. protected function _getEmpty($aDp = array())
  997. {
  998. $sClass = '';
  999. if(!empty($aDp['class']))
  1000. $sClass .= ' ' . $aDp['class'];
  1001. return $this->_oTemplate->parseHtmlByName('comment_empty.html', array(
  1002. 'style_prefix' => $this->_sStylePrefix,
  1003. 'class' => $sClass,
  1004. 'content' => MsgBox(_t('_Empty'))
  1005. ));
  1006. }
  1007. protected function _getViewImagePopup()
  1008. {
  1009. $sViewImagePopupId = 'cmts-box-' . $this->_sSystem . '-' . $this->getId() . '-view-image-popup' ;
  1010. $sViewImagePopupContent = $this->_oTemplate->parseHtmlByName('popup_image.html', array(
  1011. 'image_url' => ''
  1012. ));
  1013. return BxTemplFunctions::getInstance()->transBox($sViewImagePopupId, $sViewImagePopupContent, true);
  1014. }
  1015. protected function _getAttachments($aCmt)
  1016. {
  1017. $aTmplImages = array();
  1018. if(!$this->isAttachImageEnabled())
  1019. return '';
  1020. $aFiles = $this->_oQuery->getFiles($this->_aSystem['system_id'], $aCmt['cmt_id']);
  1021. if(!empty($aFiles) && is_array($aFiles)) {
  1022. $oStorage = BxDolStorage::getObjectInstance($this->getStorageObjectName());
  1023. $oTranscoder = BxDolTranscoderImage::getObjectInstance($this->getTranscoderPreviewName());
  1024. foreach($aFiles as $aFile) {
  1025. $bImage = $oTranscoder && $oTranscoder->isMimeTypeSupported($aFile['mime_type']);
  1026. $sPreview = '';
  1027. if($oTranscoder && $bImage)
  1028. $sPreview = $oTranscoder->getFileUrl($aFile['image_id']);
  1029. if(!$sPreview)
  1030. $sPreview = $this->_oTemplate->getIconUrl($oStorage->getIconNameByFileName($aFile['file_name']));
  1031. $aTmplVarsFile = array(
  1032. 'js_object' => $this->_sJsObjName,
  1033. 'preview' => $sPreview,
  1034. 'file' => $oStorage->getFileUrlById($aFile['image_id']),
  1035. 'file_name' => $aFile['file_name'],
  1036. 'file_icon' => $oStorage->getFontIconNameByFileName($aFile['file_name']),
  1037. 'file_size' => _t_format_size($aFile['size']),
  1038. );
  1039. $aTmplImages[] = array(
  1040. 'style_prefix' => $this->_sStylePrefix,
  1041. 'bx_if:show_image' => array(
  1042. 'condition' => $bImage,
  1043. 'content' => $aTmplVarsFile
  1044. ),
  1045. 'bx_if:show_file' => array(
  1046. 'condition' => !$bImage,
  1047. 'content' => $aTmplVarsFile
  1048. ),
  1049. );
  1050. }
  1051. }
  1052. return $this->_oTemplate->parseHtmlByName('comment_attachments.html', array(
  1053. 'bx_repeat:attached' => $aTmplImages
  1054. ));
  1055. }
  1056. protected function _getTmplVarsAuthor($aCmt)
  1057. {
  1058. list($sAuthorName, $sAuthorLink, $sAuthorIcon, $sAuthorUnit) = $this->_getAuthorInfo($aCmt['cmt_author_id']);
  1059. $bAuthorIcon = !empty($sAuthorIcon);
  1060. return array(
  1061. 'author_unit' => $sAuthorUnit,
  1062. 'bx_if:show_icon' => array(
  1063. 'condition' => $bAuthorIcon,
  1064. 'content' => array(
  1065. 'author_icon' => $sAuthorIcon
  1066. )
  1067. ),
  1068. 'bx_if:show_icon_empty' => array(
  1069. 'condition' => !$bAuthorIcon,
  1070. 'content' => array()
  1071. ),
  1072. 'bx_if:show_author_link' => array(
  1073. 'condition' => !empty($sAuthorLink),
  1074. 'content' => array(
  1075. 'author_link' => $sAuthorLink,
  1076. 'author_title' => bx_html_attribute($sAuthorName),
  1077. 'author_name' => $sAuthorName
  1078. )
  1079. ),
  1080. 'bx_if:show_author_text' => array(
  1081. 'condition' => empty($sAuthorLink),
  1082. 'content' => array(
  1083. 'author_name' => $sAuthorName
  1084. )
  1085. ),
  1086. );
  1087. }
  1088. protected function _getTmplVarsText($aCmt)
  1089. {
  1090. $sText = $aCmt['cmt_text'];
  1091. $sTextMore = '';
  1092. if(!$this->isHtml()) {
  1093. $iMaxLength = (int)$this->_aSystem['chars_display_max'];
  1094. if(strlen($sText) > $iMaxLength) {
  1095. $iLength = strpos($sText, ' ', $iMaxLength);
  1096. $sTextMore = trim(substr($sText, $iLength));
  1097. $sText = trim(substr($sText, 0, $iLength));
  1098. }
  1099. }
  1100. $sText = $this->_prepareTextForOutput($sText, $aCmt['cmt_id']);
  1101. $sTextMore = $this->_prepareTextForOutput($sTextMore, $aCmt['cmt_id']);
  1102. return array(
  1103. 'text' => $sText,
  1104. 'bx_if:show_more' => array(
  1105. 'condition' => !empty($sTextMore),
  1106. 'content' => array(
  1107. 'style_prefix' => $this->_sStylePrefix,
  1108. 'js_object' => $this->_sJsObjName,
  1109. 'text_more' => $sTextMore
  1110. )
  1111. ),
  1112. );
  1113. }
  1114. }
  1115. /** @} */