BxBaseCmts.php 58 KB

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