BxBaseCmts.php 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  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. return $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. }
  288. public function getCommentStructure($iCmtId, $aBp = array(), $aDp = array())
  289. {
  290. $aRoot = $this->getCommentRow((int)$iCmtId);
  291. $aBps = $aBp;
  292. if(!empty($aRoot))
  293. $aBps['parent_id'] = $aRoot['cmt_id'];
  294. $this->_prepareStructureBp($aDp['type'], $aBps);
  295. $iLevel = 0;
  296. $aStructure = array();
  297. $this->_getStructure($aRoot, $aBps, $iLevel, $aStructure);
  298. return !empty($aStructure) && is_array($aStructure) ? $aStructure : false;
  299. }
  300. function getCommentSearch($iCmtId, &$sAddon)
  301. {
  302. $aBp = array();
  303. $aDp = array(
  304. 'type' => BX_CMT_DISPLAY_FLAT,
  305. 'view_only' => true
  306. );
  307. if(empty($sAddon))
  308. $sAddon = $this->getJsScript($aBp, $aDp);
  309. return $this->_oTemplate->parseHtmlByName('comment_search.html', array(
  310. 'comment' => $this->getComment($iCmtId, $aBp, $aDp),
  311. 'view_image_popup' => $this->_getViewImagePopup(),
  312. ));
  313. }
  314. /**
  315. * get one comment for "Live Search"
  316. *
  317. * @param int $iCmtId - comment id
  318. * @return string
  319. */
  320. function getCommentLiveSearch($mixedCmt, $aParams = array())
  321. {
  322. $aCmt = !is_array($mixedCmt) ? $this->getCommentRow((int)$mixedCmt) : $mixedCmt;
  323. list($sAuthorName, $sAuthorLink, $sAuthorIcon) = $this->_getAuthorInfo($aCmt['cmt_author_id']);
  324. $bAuthorIcon = !empty($sAuthorIcon);
  325. $sViewLink = $this->getViewUrl($aCmt['cmt_id']);
  326. if(empty($sViewLink))
  327. $sViewLink = '#';
  328. return $this->_oTemplate->parseHtmlByName('comment_live_search.html', array(
  329. 'bx_if:show_icon' => array(
  330. 'condition' => $bAuthorIcon,
  331. 'content' => array(
  332. 'author_icon' => $sAuthorIcon,
  333. 'view_link' => $sViewLink
  334. )
  335. ),
  336. 'bx_if:show_icon_empty' => array(
  337. 'condition' => !$bAuthorIcon,
  338. 'content' => array()
  339. ),
  340. 'view_link' => $sViewLink,
  341. 'text' => BxTemplFunctions::getInstance()->getStringWithLimitedLength(strip_tags($aCmt['cmt_text']), $this->_sSnippetLenthLiveSearch),
  342. 'sample' => isset($aParams['txt_sample_single']) ? _t($aParams['txt_sample_single']) : ''
  343. ));
  344. }
  345. function getFormBoxPost($aBp = array(), $aDp = array())
  346. {
  347. return $this->_getFormBox(BX_CMT_ACTION_POST, $aBp, $aDp);
  348. }
  349. function getFormBoxEdit($aBp = array(), $aDp = array())
  350. {
  351. return $this->_getFormBox(BX_CMT_ACTION_EDIT, $aBp, $aDp);
  352. }
  353. function getFormPost($iCmtParentId = 0, $aDp = array())
  354. {
  355. return $this->_getFormPost($iCmtParentId, $aDp);
  356. }
  357. function getFormEdit($iCmtId, $aDp = array())
  358. {
  359. return $this->_getFormEdit($iCmtId, $aDp);
  360. }
  361. function getLiveUpdate($iCountOld = 0, $iCountNew = 0)
  362. {
  363. $iCount = (int)$iCountNew - (int)$iCountOld;
  364. if($iCount < 0)
  365. return '';
  366. $aComments = $this->_oQuery->getCommentsBy(array('type' => 'latest', 'object_id' => $this->_iId, 'author' => $this->_getAuthorId(), 'others' => 1, 'start' => '0', 'per_page' => $iCount));
  367. if(empty($aComments) || !is_array($aComments))
  368. return '';
  369. $aComment = array_shift($aComments);
  370. if(empty($aComment) || !is_array($aComment))
  371. return '';
  372. $sJsObject = $this->getJsObjectName();
  373. return $this->_oTemplate->parseHtmlByName('comments_lu_button.html', array(
  374. 'style_prefix' => $this->_sStylePrefix,
  375. 'html_id' => $this->getNotificationId(),
  376. 'onclick_show' => "javascript:" . $sJsObject . ".goToBtn(this, '" . $this->getItemAnchor($aComment['cmt_id']) . "', '" . $aComment['cmt_id'] . "');",
  377. ));
  378. }
  379. function getLiveUpdates($iCountOld = 0, $iCountNew = 0)
  380. {
  381. $bShowAll = true;
  382. $bShowActions = false;
  383. $iCount = (int)$iCountNew - (int)$iCountOld;
  384. if($iCount < 0)
  385. return '';
  386. $aComments = $this->_oQuery->getCommentsBy(array('type' => 'latest', 'object_id' => $this->_iId, 'author' => $this->_getAuthorId(), 'others' => 1, 'start' => '0', 'per_page' => $iCount));
  387. if(empty($aComments) || !is_array($aComments))
  388. return '';
  389. $sJsObject = $this->getJsObjectName();
  390. $iUserId = $this->_getAuthorId();
  391. $bModerator = $this->isModerator();
  392. $aComments = array_reverse($aComments);
  393. $iComments = count($aComments);
  394. $aTmplVarsNotifs = array();
  395. foreach($aComments as $iIndex => $aComment) {
  396. $iCommentId = $aComment['cmt_id'];
  397. $sShowOnClick = "javascript:" . $sJsObject . ".goTo(this, '" . $this->getItemAnchor($iCommentId) . "', '" . $iCommentId . "');";
  398. $sReplyOnClick = "javascript:" . $sJsObject . ".goToAndReply(this, '" . $this->getItemAnchor($iCommentId) . "', '" . $iCommentId . "');";
  399. $iAuthorId = (int)$aComment['cmt_author_id'];
  400. if($iAuthorId < 0) {
  401. if(abs($iAuthorId) == $iUserId)
  402. continue;
  403. else if($bModerator)
  404. $iAuthorId *= -1;
  405. }
  406. $oAuthor = $this->_getAuthorObject($iAuthorId);
  407. $sAuthorName = $oAuthor->getDisplayName();
  408. $aTmplVarsNotifs[] = array(
  409. 'bx_if:show_as_hidden' => array(
  410. 'condition' => !$bShowAll && $iIndex < ($iComments - 1),
  411. 'content' => array(),
  412. ),
  413. 'item' => $this->_oTemplate->parseHtmlByName('comments_lu_notifications.html', array(
  414. 'style_prefix' => $this->_sStylePrefix,
  415. 'onclick_show' => $sShowOnClick,
  416. 'onclick_reply' => $sReplyOnClick,
  417. 'author_link' => $oAuthor->getUrl(),
  418. 'author_title' => bx_html_attribute($sAuthorName),
  419. 'author_name' => $sAuthorName,
  420. 'author_unit' => $oAuthor->getUnit(0, array('template' => 'unit_wo_info_links')),
  421. 'text' => _t('_cmt_txt_added_sample')
  422. )),
  423. 'bx_if:show_previous' => array(
  424. 'condition' => $bShowActions && $iIndex > 0,
  425. 'content' => array(
  426. 'onclick_previous' => $sJsObject . '.previousLiveUpdate(this)'
  427. )
  428. ),
  429. 'bx_if:show_close' => array(
  430. 'condition' => $bShowActions,
  431. 'content' => array(
  432. 'onclick_close' => $sJsObject . '.hideLiveUpdate(this)'
  433. )
  434. )
  435. );
  436. }
  437. return $this->_oTemplate->parseHtmlByName('popup_chain.html', array(
  438. 'html_id' => $this->getNotificationId(),
  439. 'bx_repeat:items' => $aTmplVarsNotifs
  440. ));
  441. }
  442. public function getElementBlock($aParams = array())
  443. {
  444. $aParams['usage'] = BX_CMT_USAGE_BLOCK;
  445. return $this->getElement($aParams);
  446. }
  447. public function getElementInline($aParams = array())
  448. {
  449. $aParams['usage'] = BX_CMT_USAGE_INLINE;
  450. return $this->getElement($aParams);
  451. }
  452. public function getElement($aParams = array())
  453. {
  454. $aParams = array_merge($this->_aElementDefaults, $aParams);
  455. $bShowDoCommentAsButtonSmall = isset($aParams['show_do_comment_as_button_small']) && $aParams['show_do_comment_as_button_small'] == true;
  456. $bShowDoCommentAsButton = !$bShowDoCommentAsButtonSmall && isset($aParams['show_do_comment_as_button']) && $aParams['show_do_comment_as_button'] == true;
  457. $bShowCounterEmpty = isset($aParams['show_counter_empty']) && $aParams['show_counter_empty'] == true;
  458. $iObjectId = $this->getId();
  459. $iAuthorId = $this->_getAuthorId();
  460. $iAuthorIp = $this->_getAuthorIp();
  461. $iCount = $this->getCommentsCountAll();
  462. $bCount = (int)$iCount != 0;
  463. $isAllowedComment = $this->isPostAllowed();
  464. //--- Do Comment
  465. $bTmplVarsDoComment = $this->_isShowDoComment($aParams, $isAllowedComment, $bCount);
  466. $aTmplVarsDoComment = array();
  467. if($bTmplVarsDoComment) {
  468. $sClass = '';
  469. if($bShowDoCommentAsButton)
  470. $sClass = 'bx-btn';
  471. else if ($bShowDoCommentAsButtonSmall)
  472. $sClass = 'bx-btn bx-btn-small';
  473. if(!$isAllowedComment)
  474. $sClass .= $bShowDoCommentAsButton || $bShowDoCommentAsButtonSmall ? ' bx-btn-disabled' : 'bx-cmts-disabled';
  475. $aTmplVarsDoComment = array(
  476. 'style_prefix' => $this->_sStylePrefix,
  477. 'do_comment' => $this->_oTemplate->parseLink($this->getListUrl(), $this->_getLabelDo($aParams), array(
  478. 'class' => $this->_sStylePrefix . '-do-comment ' . $this->_sStylePrefix . '-dc ' . $sClass,
  479. 'title' => _t($this->_getTitleDo())
  480. )),
  481. );
  482. }
  483. //--- Counter
  484. $bTmplVarsCounter = $this->_isShowCounter($aParams, $isAllowedComment, $bCount);
  485. $aTmplVarsCounter = array();
  486. if($bTmplVarsCounter)
  487. $aTmplVarsCounter = array(
  488. 'style_prefix' => $this->_sStylePrefix,
  489. 'bx_if:show_hidden' => array(
  490. 'condition' => !$bShowCounterEmpty && !$bCount,
  491. 'content' => array()
  492. ),
  493. 'counter' => $this->getCounter($aParams)
  494. );
  495. if(!$bTmplVarsDoComment && !$bTmplVarsCounter)
  496. return '';
  497. $sTmplName = $this->{'_getTmplElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_SCORE_USAGE_DEFAULT)}();
  498. return $this->_oTemplate->parseHtmlByContent($sTmplName, array(
  499. 'style_prefix' => $this->_sStylePrefix,
  500. 'html_id' => $this->_aHtmlIds['main'],
  501. 'class' => $this->_sStylePrefix . ($bShowDoCommentAsButton ? '-button' : '') . ($bShowDoCommentAsButtonSmall ? '-button-small' : ''),
  502. 'count' => $iCount,
  503. 'bx_if:show_do_comment' => array(
  504. 'condition' => $bTmplVarsDoComment,
  505. 'content' => $aTmplVarsDoComment
  506. ),
  507. 'bx_if:show_counter' => array(
  508. 'condition' => $bTmplVarsCounter,
  509. 'content' => $aTmplVarsCounter
  510. ),
  511. 'script' => ''
  512. ));
  513. }
  514. public function getCounter($aParams = array())
  515. {
  516. $bShowEmpty = isset($aParams['show_counter_empty']) && $aParams['show_counter_empty'] == true;
  517. $bShowDoCommentAsButtonSmall = isset($aParams['show_do_comment_as_button_small']) && $aParams['show_do_comment_as_button_small'] == true;
  518. $bShowDoCommentAsButton = !$bShowDoCommentAsButtonSmall && isset($aParams['show_do_comment_as_button']) && $aParams['show_do_comment_as_button'] == true;
  519. $iCount = (int)$this->getCommentsCountAll();
  520. $sClass = $this->_sStylePrefix . '-counter';
  521. if($bShowDoCommentAsButtonSmall)
  522. $sClass .= ' bx-btn-small-height';
  523. if($bShowDoCommentAsButton)
  524. $sClass .= ' bx-btn-height';
  525. $iCmtsLimit = 5;
  526. $aCmts = $this->_oQuery->getCommentsBy(array('type' => 'object_id', 'object_id' => $this->getId(), 'order_way' => 'desc', 'start' => 0, 'per_page' => $iCmtsLimit * 4));
  527. $aCmts = array_reverse($aCmts);
  528. $aTmplVarsProfiles = array();
  529. foreach($aCmts as $aCmt) {
  530. $iAuthor = (int)$aCmt['cmt_author_id'];
  531. if(array_key_exists($iAuthor, $aTmplVarsProfiles))
  532. continue;
  533. $oAuthor = BxDolProfile::getInstanceMagic($iAuthor);
  534. if(!$oAuthor)
  535. continue;
  536. $aTmplVarsProfiles[$iAuthor] = array(
  537. 'icon' => $oAuthor->getUnit(0, array('template' => array('name' => 'unit_wo_info_links', 'size' => 'icon')))
  538. );
  539. if(count($aTmplVarsProfiles) >= $iCmtsLimit)
  540. break;
  541. }
  542. $aTmplVarsProfiles = array_values($aTmplVarsProfiles);
  543. $sHref = !empty($aParams['overwrite_counter_link_href']) ? $aParams['overwrite_counter_link_href'] : $this->getListUrl();
  544. $sOnclick = '';
  545. if(!empty($aParams['overwrite_counter_link_onclick']))
  546. $sOnclick = $aParams['overwrite_counter_link_onclick'];
  547. return $this->_oTemplate->parseHtmlByContent($this->_getTmplCounter(), array(
  548. 'id' => $this->_aHtmlIds['counter'],
  549. 'class' => $sClass,
  550. 'href' => $sHref,
  551. 'bx_if:show_onclick' => array(
  552. 'condition' => !empty($sOnclick),
  553. 'content' => array(
  554. 'onclick' => $sOnclick
  555. )
  556. ),
  557. 'content' => $iCount != 0 || $bShowEmpty ? $this->_getCounterLabel($iCount) : '',
  558. 'bx_repeat:profiles' => $aTmplVarsProfiles,
  559. 'bx_if:show_icon' => array(
  560. 'condition' => $bShowEmpty || !empty($aTmplVarsProfiles),
  561. 'content' => array(
  562. 'sp' => $this->_sStylePrefix
  563. )
  564. )
  565. ));
  566. }
  567. protected function _getLabelDo($aParams = array())
  568. {
  569. return $this->_oTemplate->parseHtmlByContent($this->_getTmplLabelDo(), array(
  570. 'style_prefix' => $this->_sStylePrefix,
  571. 'bx_if:show_image' => array(
  572. 'condition' => isset($aParams['show_do_comment_image']) && $aParams['show_do_comment_image'] == true,
  573. 'content' => array(
  574. 'style_prefix' => $this->_sStylePrefix,
  575. 'src' => $this->_getImageDo()
  576. )
  577. ),
  578. 'bx_if:show_icon' => array(
  579. 'condition' => isset($aParams['show_do_comment_icon']) && $aParams['show_do_comment_icon'] == true,
  580. 'content' => array(
  581. 'style_prefix' => $this->_sStylePrefix,
  582. 'name' => $this->_getIconDo()
  583. )
  584. ),
  585. 'bx_if:show_text' => array(
  586. 'condition' => isset($aParams['show_do_comment_label']) && $aParams['show_do_comment_label'] == true,
  587. 'content' => array(
  588. 'style_prefix' => $this->_sStylePrefix,
  589. 'text' => _t($this->_getTitleDo())
  590. )
  591. )
  592. ));
  593. }
  594. protected function _getCounterLabel($iCount)
  595. {
  596. return (int)$iCount != 0 ? _t('_cmt_txt_counter', $iCount) : _t('_cmt_txt_counter_empty');
  597. }
  598. protected function _getTmplElementBlock()
  599. {
  600. return self::$_sTmplContentElementBlock;
  601. }
  602. protected function _getTmplElementInline()
  603. {
  604. return self::$_sTmplContentElementInline;
  605. }
  606. protected function _getTmplLabelDo()
  607. {
  608. return self::$_sTmplContentDoCommentLabel;
  609. }
  610. protected function _getTmplCounter()
  611. {
  612. return self::$_sTmplContentCounter;
  613. }
  614. /**
  615. * private functions
  616. */
  617. protected function _getControlsBox()
  618. {
  619. $sDisplay = '';
  620. $bDisplay = (int)$this->_aSystem['is_display_switch'] == 1;
  621. if($bDisplay) {
  622. $aDisplayLinks = array(
  623. array('id' => $this->_sSystem . '-flat', 'name' => $this->_sSystem . '-flat', 'class' => '', 'title' => '_cmt_display_flat', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeDisplay(this, \'flat\');'),
  624. array('id' => $this->_sSystem . '-threaded', 'name' => $this->_sSystem . '-threaded', 'class' => '', 'title' => '_cmt_display_threaded', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeDisplay(this, \'threaded\');')
  625. );
  626. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-display', 'menu_items' => $aDisplayLinks));
  627. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sDisplayType);
  628. $sDisplay = $oMenu->getCode();
  629. }
  630. $sBrowseType = '';
  631. $bBrowseType = (int)$this->_aSystem['is_browse_switch'] == 1;
  632. if($bBrowseType) {
  633. $aBrowseLinks = array(
  634. array('id' => $this->_sSystem . '-tail', 'name' => $this->_sSystem . '-tail', 'class' => '', 'title' => '_cmt_browse_tail', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'tail\');'),
  635. array('id' => $this->_sSystem . '-head', 'name' => $this->_sSystem . '-head', 'class' => '', 'title' => '_cmt_browse_head', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'head\');'),
  636. array('id' => $this->_sSystem . '-popular', 'name' => $this->_sSystem . '-popular', 'class' => '', 'title' => '_cmt_browse_popular', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'popular\');'),
  637. );
  638. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-browse', 'menu_items' => $aBrowseLinks));
  639. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sBrowseType);
  640. $sBrowseType = $oMenu->getCode();
  641. }
  642. $sBrowseFilter = '';
  643. $bBrowseFilter = (int)$this->_aSystem['is_browse_filter'] == 1;
  644. if($bBrowseFilter) {
  645. $aFilterLinks = array(
  646. array('id' => $this->_sSystem . '-all', 'name' => $this->_sSystem . '-all', 'class' => '', 'title' => '_cmt_browse_all', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'all\');'),
  647. array('id' => $this->_sSystem . '-friends', 'name' => $this->_sSystem . '-friends', 'class' => '', 'title' => '_cmt_browse_friends', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'friends\');'),
  648. array('id' => $this->_sSystem . '-subscriptions', 'name' => $this->_sSystem . '-subscriptions', 'class' => '', 'title' => '_cmt_browse_subscriptions', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'subscriptions\');')
  649. );
  650. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-filter', 'menu_items' => $aFilterLinks));
  651. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sBrowseFilter);
  652. $sBrowseFilter = $oMenu->getCode();
  653. }
  654. return $this->_oTemplate->parseHtmlByName('comments_controls.html', array(
  655. 'display_switcher' => $bDisplay ? $sDisplay : '',
  656. 'bx_if:is_divider_1' => array(
  657. 'condition' => $bDisplay && $bBrowseType,
  658. 'content' => array(
  659. 'style_prefix' => $this->_sStylePrefix,
  660. )
  661. ),
  662. 'browse_switcher' => $bBrowseType ? $sBrowseType : '',
  663. 'bx_if:is_divider_2' => array(
  664. 'condition' => $bBrowseType && $bBrowseFilter,
  665. 'content' => array(
  666. 'style_prefix' => $this->_sStylePrefix,
  667. )
  668. ),
  669. 'filter_switcher' => $bBrowseFilter ? $sBrowseFilter : '',
  670. ));
  671. }
  672. protected function _getActionsBox(&$aCmt, $aDp = array())
  673. {
  674. $bViewOnly = isset($aDp['view_only']) && $aDp['view_only'] === true;
  675. $bDynamicMode = isset($aDp['dynamic_mode']) && $aDp['dynamic_mode'] === true;
  676. $sMenuActions = '';
  677. if(!$bViewOnly) {
  678. $oMenuActions = BxDolMenu::getObjectInstance($this->_sMenuObjActions);
  679. $oMenuActions->setCmtsData($this, $aCmt['cmt_id']);
  680. $oMenuActions->setDynamicMode($bDynamicMode);
  681. $sMenuActions = $oMenuActions->getCode();
  682. }
  683. return $this->_oTemplate->parseHtmlByName('comment_actions.html', array(
  684. 'id' => $aCmt['cmt_id'],
  685. 'js_object' => $this->_sJsObjName,
  686. 'style_prefix' => $this->_sStylePrefix,
  687. 'menu_actions' => $sMenuActions,
  688. ));
  689. }
  690. protected function _getFormBox($sType, $aBp, $aDp)
  691. {
  692. $iCmtParentId = isset($aBp['parent_id']) ? (int)$aBp['parent_id'] : 0;
  693. $sPosition = isset($aDp['position']) ? $aDp['position'] : '';
  694. $bQuote = isset($aDp['quote']) && (bool)$aDp['quote'];
  695. $bFormMin = $iCmtParentId == 0 && isset($aDp['min_post_form']) && (bool)$aDp['min_post_form'];
  696. $sPositionSystem = $this->_aSystem['post_form_position'];
  697. if(!empty($sPosition) && $sPositionSystem != $sPosition)
  698. return '';
  699. $sClass = '';
  700. if(!empty($sPosition))
  701. $sClass .= ' ' . $this->_sStylePrefix . '-reply-' . $sPosition;
  702. if($bQuote)
  703. $sClass .= ' ' . $this->_sStylePrefix . '-reply-quote';
  704. if($bFormMin)
  705. $sClass .= ' ' . $this->_sStylePrefix . '-reply-min';
  706. if(!empty($aDp['class']))
  707. $sClass .= ' ' . $aDp['class'];
  708. $sClassBody = '';
  709. if(!empty($aDp['class_body']))
  710. $sClassBody .= ' ' . $aDp['class_body'];
  711. $aTmplVarsFormMin = array();
  712. if($bFormMin) {
  713. list($sAuthorName, $sAuthorLink, $sAuthorIcon, $sAuthorUnit) = $this->_getAuthorInfo($this->_getAuthorId());
  714. $oForm = new BxTemplFormView(array());
  715. $aInputPlaceholder = array(
  716. 'type' => 'text',
  717. 'name' => 'comment',
  718. 'caption' => '',
  719. 'attrs' => array(
  720. 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtShowForm(this)',
  721. 'placeholder' => _t($this->_aT['txt_min_form_placeholder'], $sAuthorName)
  722. ),
  723. 'value' => '',
  724. );
  725. $sClassBodyMin = '';
  726. if(!empty($aDp['class_body_min']))
  727. $sClassBodyMin .= ' ' . $aDp['class_body_min'];
  728. $aTmplVarsFormMin = array(
  729. 'js_object' => $this->_sJsObjName,
  730. 'class_body_min' => $sClassBodyMin,
  731. 'style_prefix' => $this->_sStylePrefix,
  732. 'author_unit' => $sAuthorUnit,
  733. 'placeholder' => $oForm->genRow($aInputPlaceholder)
  734. );
  735. }
  736. $aForm = $this->{'_getForm' . ucfirst($sType)}($iCmtParentId, $aDp);
  737. if(empty($aForm['form']))
  738. return !empty($aForm['msg']) && (isLogged() || $iCmtParentId != 0 || $this->getCommentsCount() > 0) ? MsgBox($aForm['msg']) : '';
  739. return $this->_oTemplate->parseHtmlByName('comment_reply_box.html', array(
  740. 'js_object' => $this->_sJsObjName,
  741. 'style_prefix' => $this->_sStylePrefix,
  742. 'class' => $sClass,
  743. 'class_body' => $sClassBody,
  744. 'bx_if:show_form_min' => array(
  745. 'condition' => $bFormMin,
  746. 'content' => $aTmplVarsFormMin
  747. ),
  748. 'form' => $aForm['form'],
  749. 'form_id' => $aForm['form_id'],
  750. ));
  751. }
  752. protected function _getFormAdd($aValues)
  753. {
  754. $iCmtAuthorId = isset($aValues['cmt_author_id']) ? (int)$aValues['cmt_author_id'] : $this->_getAuthorId();
  755. $iCmtParentId = isset($aValues['cmt_parent_id']) ? (int)$aValues['cmt_parent_id'] : 0;
  756. $oForm = $this->_getForm(BX_CMT_ACTION_POST, $iCmtParentId);
  757. $oForm->aFormAttrs['method'] = BX_DOL_FORM_METHOD_SPECIFIC;
  758. $oForm->aParams['csrf']['disable'] = true;
  759. $oForm->aInputs['cmt_parent_id']['value'] = $iCmtParentId;
  760. if(!empty($oForm->aParams['db']['submit_name'])) {
  761. $sSubmitName = false;
  762. if(is_array($oForm->aParams['db']['submit_name']))
  763. foreach($oForm->aParams['db']['submit_name'] as $sVal) {
  764. if(isset($oForm->aInputs[$sVal])) {
  765. $sSubmitName = $sVal;
  766. break;
  767. }
  768. }
  769. else
  770. $sSubmitName = $oForm->aParams['db']['submit_name'];
  771. if($sSubmitName && isset($oForm->aInputs[$sSubmitName]))
  772. $aValues[$sSubmitName] = $oForm->aInputs[$sSubmitName]['value'];
  773. }
  774. $oForm->initChecker(array(), $aValues);
  775. if(!$oForm->isSubmittedAndValid())
  776. return array('code' => 1, 'message' => '_sys_txt_error_occured');
  777. $iLevel = 0;
  778. $iCmtVisualParentId = 0;
  779. if((int)$iCmtParentId > 0) {
  780. $aParent = $this->getCommentRow($iCmtParentId);
  781. $iLevel = (int)$aParent['cmt_level'] + 1;
  782. $iCmtVisualParentId = $iLevel > $this->getMaxLevel() ? $aParent['cmt_vparent_id'] : $iCmtParentId;
  783. }
  784. $iCmtId = (int)$oForm->insert(array('cmt_vparent_id' => $iCmtVisualParentId, 'cmt_object_id' => $this->_iId, 'cmt_author_id' => $iCmtAuthorId, 'cmt_level' => $iLevel, 'cmt_time' => time()));
  785. if(!$iCmtId) {
  786. if(!$oForm->isValid())
  787. return array('code' => 1, 'message' => '_sys_txt_error_occured');
  788. else
  789. return array('code' => 2, 'message' => '_cmt_err_cannot_perform_action');
  790. }
  791. $iCmtUniqId = $this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId, $iCmtAuthorId);
  792. if($iCmtParentId) {
  793. $this->_oQuery->updateRepliesCount($iCmtParentId, 1);
  794. if(!BxDolModuleQuery::getInstance()->isEnabledByName('bx_notifications'))
  795. $this->_sendNotificationEmail($iCmtId, $iCmtParentId);
  796. }
  797. $this->_triggerComment();
  798. if($this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  799. $oMetatags->metaAdd($iCmtUniqId, $aValues['cmt_text']);
  800. $mixedResult = $this->onPostAfter($iCmtId);
  801. if($mixedResult === false)
  802. return array('code' => 2, 'message' => '_cmt_err_cannot_perform_action');
  803. return $mixedResult;
  804. }
  805. protected function _getFormPost($iCmtParentId = 0, $aDp = array())
  806. {
  807. $bCmtParentId = !empty($iCmtParentId);
  808. if(!$bCmtParentId && !$this->isPostAllowed())
  809. return array('msg' => $this->msgErrPostAllowed());
  810. if($bCmtParentId && !$this->isReplyAllowed($iCmtParentId))
  811. return array('msg' => $this->msgErrReplyAllowed());
  812. $bDynamic = isset($aDp['dynamic_mode']) && (bool)$aDp['dynamic_mode'];
  813. $bQuote = isset($aDp['quote']) && (bool)$aDp['quote'];
  814. $oForm = $this->_getForm(BX_CMT_ACTION_POST, $iCmtParentId);
  815. $oForm->aInputs['cmt_parent_id']['value'] = $iCmtParentId;
  816. if($bQuote) {
  817. $aCmtParent = $this->getCommentRow((int)$iCmtParentId);
  818. if(!empty($aCmtParent['cmt_text']))
  819. $oForm->aInputs['cmt_text']['value'] = $this->_oTemplate->parseHtmlByName('comment_quote.html', array(
  820. 'content' => $aCmtParent['cmt_text']
  821. ));
  822. }
  823. $oForm->initChecker();
  824. if($oForm->isSubmittedAndValid()) {
  825. $iCmtAuthorId = $this->_getAuthorId();
  826. $iCmtParentId = (int)$oForm->getCleanValue('cmt_parent_id');
  827. $sCmtText = $oForm->getCleanValue('cmt_text');
  828. $aParent = array();
  829. if($iCmtParentId > 0) {
  830. $aParent = $this->getCommentRow($iCmtParentId);
  831. if(empty($aParent) || !is_array($aParent)) {
  832. $iCmtParentId = 0;
  833. $oForm->setSubmittedValue('cmt_parent_id', $iCmtParentId, $oForm->aFormAttrs['method']);
  834. }
  835. }
  836. $iLevel = 0;
  837. $iCmtVisualParentId = 0;
  838. if($iCmtParentId > 0) {
  839. $iLevel = (int)$aParent['cmt_level'] + 1;
  840. $iCmtVisualParentId = $iLevel > $this->getMaxLevel() ? $aParent['cmt_vparent_id'] : $iCmtParentId;
  841. }
  842. $iCmtId = (int)$oForm->insert(array('cmt_vparent_id' => $iCmtVisualParentId, 'cmt_object_id' => $this->_iId, 'cmt_author_id' => $iCmtAuthorId, 'cmt_level' => $iLevel, 'cmt_time' => time()));
  843. if($iCmtId != 0) {
  844. $iCmtUniqId = $this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId, $iCmtAuthorId);
  845. if($this->isAttachImageEnabled())
  846. $oForm->processImages($this, 'cmt_image', $iCmtUniqId, $iCmtId, $iCmtAuthorId, true);
  847. if($iCmtParentId > 0) {
  848. $this->_oQuery->updateRepliesCount($iCmtParentId, 1);
  849. if(!BxDolModuleQuery::getInstance()->isEnabledByName('bx_notifications'))
  850. $this->_sendNotificationEmail($iCmtId, $iCmtParentId);
  851. }
  852. $this->_triggerComment();
  853. if($iCmtParentId > 0)
  854. $this->isReplyAllowed($iCmtParentId, true);
  855. else
  856. $this->isPostAllowed(true);
  857. if($this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  858. $oMetatags->metaAdd($iCmtUniqId, $sCmtText);
  859. if(($mixedResult = $this->onPostAfter($iCmtId)) !== false)
  860. return $mixedResult;
  861. }
  862. return array('msg' => _t('_cmt_err_cannot_perform_action'));
  863. }
  864. return array('form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id);
  865. }
  866. protected function _getFormEdit($iCmtId, $aDp = array())
  867. {
  868. $bDynamic = isset($aDp['dynamic_mode']) && (bool)$aDp['dynamic_mode'];
  869. $aCmt = $this->getCommentSimple($iCmtId);
  870. if(!$aCmt)
  871. return array('msg' => _t('_No such comment'));
  872. $iCmtAuthorId = $this->_getAuthorId();
  873. if(!$this->isEditAllowed($aCmt))
  874. return array('msg' => $aCmt['cmt_author_id'] == $iCmtAuthorId ? strip_tags($this->msgErrEditAllowed()) : _t('_Access denied'));
  875. $oForm = $this->_getForm(BX_CMT_ACTION_EDIT, $aCmt['cmt_id']);
  876. $oForm->initChecker($aCmt);
  877. if($oForm->isSubmittedAndValid()) {
  878. $sCmtText = $oForm->getCleanValue('cmt_text');
  879. if($oForm->update($iCmtId) !== false) {
  880. $iCmtUniqId = $this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId, (int)$aCmt['cmt_author_id']);
  881. if($this->isAttachImageEnabled())
  882. $oForm->processImages($this, 'cmt_image', $iCmtUniqId, $iCmtId, $iCmtAuthorId, false);
  883. $this->isEditAllowed($aCmt, true);
  884. if($this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  885. $oMetatags->metaAdd($iCmtUniqId, $sCmtText);
  886. if(($mixedResult = $this->onEditAfter($iCmtId)) !== false)
  887. return $mixedResult;
  888. }
  889. return array('msg' => _t('_cmt_err_cannot_perform_action'));
  890. }
  891. return array('form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id);
  892. }
  893. protected function _getForm($sAction, $iId)
  894. {
  895. $oForm = $this->_getFormObject($sAction);
  896. $oForm->setId(sprintf($oForm->getAttributeMask('id'), $sAction, $this->_sSystem, $iId));
  897. $oForm->setName(sprintf($oForm->getAttributeMask('name'), $sAction, $this->_sSystem, $iId));
  898. $oForm->aParams['db']['table'] = $this->_aSystem['table'];
  899. $oForm->aInputs['sys']['value'] = $this->_sSystem;
  900. $oForm->aInputs['id']['value'] = $this->_iId;
  901. $oForm->aInputs['action']['value'] = 'Submit' . ucfirst($sAction) . 'Form';
  902. if(!$this->isAttachImageEnabled())
  903. unset($oForm->aInputs['cmt_image']);
  904. if(isset($oForm->aInputs['cmt_text'])) {
  905. $oForm->aInputs['cmt_text']['html'] = $this->_aSystem['html'];
  906. $oForm->aInputs['cmt_text']['db']['pass'] = $this->isHtml() ? 'XssHtml' : 'XssMultiline';
  907. $iCmtTextMin = (int)$this->_aSystem['chars_post_min'];
  908. $iCmtTextMax = (int)$this->_aSystem['chars_post_max'];
  909. $oForm->aInputs['cmt_text']['checker']['params'] = array($iCmtTextMin, $iCmtTextMax);
  910. $oForm->aInputs['cmt_text']['checker']['error'] = _t('_Please enter n1-n2 characters', $iCmtTextMin, $iCmtTextMax);
  911. }
  912. if($sAction == BX_CMT_ACTION_EDIT && isset($oForm->aInputs['cmt_controls']))
  913. foreach($oForm->aInputs['cmt_controls'] as $mixedKey => $mixedValue) {
  914. if(!is_numeric($mixedKey) || empty($mixedValue['name']) || $mixedValue['name'] != 'cmt_cancel')
  915. continue;
  916. if(!isset($oForm->aInputs['cmt_controls'][$mixedKey]['attrs']))
  917. $oForm->aInputs['cmt_controls'][$mixedKey]['attrs'] = array();
  918. $oForm->aInputs['cmt_controls'][$mixedKey]['attrs']['onclick'] = $this->_sJsObjName . '.cmtEdit(this, ' . $iId . ', false)';
  919. }
  920. return $oForm;
  921. }
  922. protected function _getContent($aCmt)
  923. {
  924. $sAttachments = $this->_getAttachments($aCmt);
  925. $sReactions = '';
  926. if(($oReaction = $this->getReactionObject($aCmt['cmt_unique_id'])) !== false)
  927. $sReactions = $oReaction->getCounter(array(
  928. 'show_counter' => true
  929. ));
  930. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameItemContent, array_merge(array(
  931. 'style_prefix' => $this->_sStylePrefix,
  932. 'js_object' => $this->_sJsObjName,
  933. 'bx_if:show_attached' => array(
  934. 'condition' => !empty($sAttachments),
  935. 'content' => array(
  936. 'style_prefix' => $this->_sStylePrefix,
  937. 'attached' => $sAttachments
  938. )
  939. ),
  940. 'reactions' => $sReactions,
  941. ), $this->_getTmplVarsText($aCmt)));
  942. }
  943. protected function _getMoreLink($sCmts, $aBp = array(), $aDp = array())
  944. {
  945. $iStart = $iPerView = 0;
  946. switch($aBp['type']) {
  947. case BX_CMT_BROWSE_HEAD:
  948. case BX_CMT_BROWSE_POPULAR:
  949. case BX_CMT_BROWSE_CONNECTION:
  950. $iPerView = $aBp['per_view'];
  951. $iStart = $aBp['start'] + $iPerView;
  952. if($iStart >= $aBp['count'])
  953. return $sCmts;
  954. break;
  955. case BX_CMT_BROWSE_TAIL:
  956. $iPerView = $aBp['per_view'];
  957. $iStart = $aBp['start'] - $iPerView;
  958. if($iStart < 0) {
  959. $iPerView += $iStart;
  960. $iStart = 0;
  961. }
  962. if($iStart == 0 && $iPerView == 0)
  963. return $sCmts;
  964. break;
  965. }
  966. $sParentKey = isset($aBp['vparent_id']) ? 'vparent_id' : 'parent_id';
  967. $bRoot = (int)$aBp[$sParentKey] <= 0;
  968. $sMore = $this->_oTemplate->parseHtmlByName('comment_more.html', array(
  969. 'js_object' => $this->_sJsObjName,
  970. 'style_prefix' => $this->_sStylePrefix,
  971. 'bx_if:is_root' => array(
  972. 'condition' => $bRoot,
  973. 'content' => array()
  974. ),
  975. 'parent_id' => $aBp[$sParentKey],
  976. 'start' => $iStart,
  977. 'per_view' => $iPerView,
  978. 'title' => _t('_cmt_load_more_' . ($aBp[$sParentKey] == 0 ? 'comments' : 'replies') . '_' . $aBp['type'])
  979. ));
  980. switch($aBp['type']) {
  981. case BX_CMT_BROWSE_HEAD:
  982. case BX_CMT_BROWSE_POPULAR:
  983. case BX_CMT_BROWSE_CONNECTION:
  984. $sCmts .= $sMore;
  985. break;
  986. case BX_CMT_BROWSE_TAIL:
  987. $sCmts = $sMore . $sCmts;
  988. break;
  989. }
  990. return $sCmts;
  991. }
  992. protected function _getEmpty($aDp = array())
  993. {
  994. $sClass = '';
  995. if(!empty($aDp['class']))
  996. $sClass .= ' ' . $aDp['class'];
  997. return $this->_oTemplate->parseHtmlByName('comment_empty.html', array(
  998. 'style_prefix' => $this->_sStylePrefix,
  999. 'class' => $sClass,
  1000. 'content' => MsgBox(_t('_Empty'))
  1001. ));
  1002. }
  1003. protected function _getViewImagePopup()
  1004. {
  1005. $sViewImagePopupId = 'cmts-box-' . $this->_sSystem . '-' . $this->getId() . '-view-image-popup' ;
  1006. $sViewImagePopupContent = $this->_oTemplate->parseHtmlByName('popup_image.html', array(
  1007. 'image_url' => ''
  1008. ));
  1009. return BxTemplFunctions::getInstance()->transBox($sViewImagePopupId, $sViewImagePopupContent, true);
  1010. }
  1011. protected function _getAttachments($aCmt)
  1012. {
  1013. $aTmplImages = array();
  1014. if(!$this->isAttachImageEnabled())
  1015. return '';
  1016. $aFiles = $this->_oQuery->getFiles($this->_aSystem['system_id'], $aCmt['cmt_id']);
  1017. if(!empty($aFiles) && is_array($aFiles)) {
  1018. $oStorage = BxDolStorage::getObjectInstance($this->getStorageObjectName());
  1019. $oTranscoder = BxDolTranscoderImage::getObjectInstance($this->getTranscoderPreviewName());
  1020. foreach($aFiles as $aFile) {
  1021. $bImage = $oTranscoder && $oTranscoder->isMimeTypeSupported($aFile['mime_type']);
  1022. $sPreview = '';
  1023. if($oTranscoder && $bImage)
  1024. $sPreview = $oTranscoder->getFileUrl($aFile['image_id']);
  1025. if(!$sPreview)
  1026. $sPreview = $this->_oTemplate->getIconUrl($oStorage->getIconNameByFileName($aFile['file_name']));
  1027. $aTmplVarsFile = array(
  1028. 'js_object' => $this->_sJsObjName,
  1029. 'preview' => $sPreview,
  1030. 'file' => $oStorage->getFileUrlById($aFile['image_id']),
  1031. 'file_name' => $aFile['file_name'],
  1032. 'file_icon' => $oStorage->getFontIconNameByFileName($aFile['file_name']),
  1033. 'file_size' => _t_format_size($aFile['size']),
  1034. );
  1035. $aTmplImages[] = array(
  1036. 'style_prefix' => $this->_sStylePrefix,
  1037. 'bx_if:show_image' => array(
  1038. 'condition' => $bImage,
  1039. 'content' => $aTmplVarsFile
  1040. ),
  1041. 'bx_if:show_file' => array(
  1042. 'condition' => !$bImage,
  1043. 'content' => $aTmplVarsFile
  1044. ),
  1045. );
  1046. }
  1047. }
  1048. return $this->_oTemplate->parseHtmlByName('comment_attachments.html', array(
  1049. 'bx_repeat:attached' => $aTmplImages
  1050. ));
  1051. }
  1052. protected function _getTmplVarsAuthor($aCmt)
  1053. {
  1054. list($sAuthorName, $sAuthorLink, $sAuthorIcon, $sAuthorUnit) = $this->_getAuthorInfo($aCmt['cmt_author_id']);
  1055. $bAuthorIcon = !empty($sAuthorIcon);
  1056. return array(
  1057. 'author_unit' => $sAuthorUnit,
  1058. 'bx_if:show_icon' => array(
  1059. 'condition' => $bAuthorIcon,
  1060. 'content' => array(
  1061. 'author_icon' => $sAuthorIcon
  1062. )
  1063. ),
  1064. 'bx_if:show_icon_empty' => array(
  1065. 'condition' => !$bAuthorIcon,
  1066. 'content' => array()
  1067. ),
  1068. 'bx_if:show_author_link' => array(
  1069. 'condition' => !empty($sAuthorLink),
  1070. 'content' => array(
  1071. 'author_link' => $sAuthorLink,
  1072. 'author_title' => bx_html_attribute($sAuthorName),
  1073. 'author_name' => $sAuthorName
  1074. )
  1075. ),
  1076. 'bx_if:show_author_text' => array(
  1077. 'condition' => empty($sAuthorLink),
  1078. 'content' => array(
  1079. 'author_name' => $sAuthorName
  1080. )
  1081. ),
  1082. );
  1083. }
  1084. protected function _getTmplVarsText($aCmt)
  1085. {
  1086. $sText = $aCmt['cmt_text'];
  1087. $sTextMore = '';
  1088. if(!$this->isHtml()) {
  1089. $iMaxLength = (int)$this->_aSystem['chars_display_max'];
  1090. if(strlen($sText) > $iMaxLength) {
  1091. $iLength = strpos($sText, ' ', $iMaxLength);
  1092. $sTextMore = trim(substr($sText, $iLength));
  1093. $sText = trim(substr($sText, 0, $iLength));
  1094. }
  1095. }
  1096. $sText = $this->_prepareTextForOutput($sText, $aCmt['cmt_id']);
  1097. $sTextMore = $this->_prepareTextForOutput($sTextMore, $aCmt['cmt_id']);
  1098. return array(
  1099. 'text' => $sText,
  1100. 'bx_if:show_more' => array(
  1101. 'condition' => !empty($sTextMore),
  1102. 'content' => array(
  1103. 'style_prefix' => $this->_sStylePrefix,
  1104. 'js_object' => $this->_sJsObjName,
  1105. 'text_more' => $sTextMore
  1106. )
  1107. ),
  1108. );
  1109. }
  1110. }
  1111. /** @} */