BxBaseCmts.php 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  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('_' , '-')) . $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. public function getCounter($aParams = array())
  577. {
  578. $bShowEmpty = isset($aParams['show_counter_empty']) && $aParams['show_counter_empty'] == true;
  579. $bShowDoCommentAsButtonSmall = isset($aParams['show_do_comment_as_button_small']) && $aParams['show_do_comment_as_button_small'] == true;
  580. $bShowDoCommentAsButton = !$bShowDoCommentAsButtonSmall && isset($aParams['show_do_comment_as_button']) && $aParams['show_do_comment_as_button'] == true;
  581. $iCount = (int)$this->getCommentsCountAll();
  582. if($iCount == 0 && !$bShowEmpty)
  583. return '';
  584. $sClass = $this->_sStylePrefix . '-counter';
  585. if($bShowDoCommentAsButtonSmall)
  586. $sClass .= ' bx-btn-small-height';
  587. if($bShowDoCommentAsButton)
  588. $sClass .= ' bx-btn-height';
  589. $iCmtsLimit = 5;
  590. $aCmts = $this->_oQuery->getCommentsBy(array('type' => 'object_id', 'object_id' => $this->getId(), 'order_way' => 'desc', 'start' => 0, 'per_page' => $iCmtsLimit * 4));
  591. $aCmts = array_reverse($aCmts);
  592. $aTmplVarsProfiles = array();
  593. foreach($aCmts as $aCmt) {
  594. $iAuthor = (int)$aCmt['cmt_author_id'];
  595. if(array_key_exists($iAuthor, $aTmplVarsProfiles))
  596. continue;
  597. $oAuthor = BxDolProfile::getInstanceMagic($iAuthor);
  598. if(!$oAuthor)
  599. continue;
  600. $aTmplVarsProfiles[$iAuthor] = array(
  601. 'icon' => $oAuthor->getUnit(0, array('template' => array('name' => 'unit_wo_info_links', 'size' => 'icon')))
  602. );
  603. if(count($aTmplVarsProfiles) >= $iCmtsLimit)
  604. break;
  605. }
  606. $aTmplVarsProfiles = array_values($aTmplVarsProfiles);
  607. $sHref = !empty($aParams['overwrite_counter_link_href']) ? $aParams['overwrite_counter_link_href'] : $this->getListUrl();
  608. $sOnclick = '';
  609. if(!empty($aParams['overwrite_counter_link_onclick']))
  610. $sOnclick = $aParams['overwrite_counter_link_onclick'];
  611. return $this->_oTemplate->parseHtmlByContent($this->_getTmplCounter(), array(
  612. 'id' => $this->_aHtmlIds['counter'],
  613. 'class' => $sClass,
  614. 'href' => $sHref,
  615. 'bx_if:show_onclick' => array(
  616. 'condition' => !empty($sOnclick),
  617. 'content' => array(
  618. 'onclick' => $sOnclick
  619. )
  620. ),
  621. 'content' => $this->_getCounterLabel($iCount),
  622. 'bx_repeat:profiles' => $aTmplVarsProfiles,
  623. 'bx_if:show_icon' => array(
  624. 'condition' => $bShowEmpty || !empty($aTmplVarsProfiles),
  625. 'content' => array(
  626. 'sp' => $this->_sStylePrefix
  627. )
  628. )
  629. ));
  630. }
  631. protected function _getLabelDo($aParams = array())
  632. {
  633. return $this->_oTemplate->parseHtmlByContent($this->_getTmplLabelDo(), array(
  634. 'style_prefix' => $this->_sStylePrefix,
  635. 'bx_if:show_image' => array(
  636. 'condition' => isset($aParams['show_do_comment_image']) && $aParams['show_do_comment_image'] == true,
  637. 'content' => array(
  638. 'style_prefix' => $this->_sStylePrefix,
  639. 'src' => $this->_getImageDo()
  640. )
  641. ),
  642. 'bx_if:show_icon' => array(
  643. 'condition' => isset($aParams['show_do_comment_icon']) && $aParams['show_do_comment_icon'] == true,
  644. 'content' => array(
  645. 'style_prefix' => $this->_sStylePrefix,
  646. 'name' => $this->_getIconDo()
  647. )
  648. ),
  649. 'bx_if:show_text' => array(
  650. 'condition' => isset($aParams['show_do_comment_label']) && $aParams['show_do_comment_label'] == true,
  651. 'content' => array(
  652. 'style_prefix' => $this->_sStylePrefix,
  653. 'text' => _t($this->_getTitleDo())
  654. )
  655. )
  656. ));
  657. }
  658. protected function _getCounterLabel($iCount)
  659. {
  660. return (int)$iCount != 0 ? _t('_cmt_txt_counter', $iCount) : _t('_cmt_txt_counter_empty');
  661. }
  662. protected function _getTmplElementBlock()
  663. {
  664. return self::$_sTmplContentElementBlock;
  665. }
  666. protected function _getTmplElementInline()
  667. {
  668. return self::$_sTmplContentElementInline;
  669. }
  670. protected function _getTmplLabelDo()
  671. {
  672. return self::$_sTmplContentDoCommentLabel;
  673. }
  674. protected function _getTmplCounter()
  675. {
  676. return self::$_sTmplContentCounter;
  677. }
  678. /**
  679. * private functions
  680. */
  681. protected function _getContentBefore($aBp = array(), $aDp = array())
  682. {
  683. return '';
  684. }
  685. protected function _getContentAfter($aBp = array(), $aDp = array())
  686. {
  687. return '';
  688. }
  689. protected function _getControlsBox()
  690. {
  691. $sDisplay = '';
  692. $bDisplay = (int)$this->_aSystem['is_display_switch'] == 1;
  693. if($bDisplay) {
  694. $aDisplayLinks = array(
  695. array('id' => $this->_sSystem . '-flat', 'name' => $this->_sSystem . '-flat', 'class' => '', 'title' => '_cmt_display_flat', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeDisplay(this, \'flat\');'),
  696. array('id' => $this->_sSystem . '-threaded', 'name' => $this->_sSystem . '-threaded', 'class' => '', 'title' => '_cmt_display_threaded', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeDisplay(this, \'threaded\');')
  697. );
  698. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-display', 'menu_items' => $aDisplayLinks));
  699. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sDisplayType);
  700. $sDisplay = $oMenu->getCode();
  701. }
  702. $sBrowseType = '';
  703. $bBrowseType = (int)$this->_aSystem['is_browse_switch'] == 1;
  704. if($bBrowseType) {
  705. $aBrowseLinks = array(
  706. array('id' => $this->_sSystem . '-tail', 'name' => $this->_sSystem . '-tail', 'class' => '', 'title' => '_cmt_browse_tail', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'tail\');'),
  707. array('id' => $this->_sSystem . '-head', 'name' => $this->_sSystem . '-head', 'class' => '', 'title' => '_cmt_browse_head', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'head\');'),
  708. array('id' => $this->_sSystem . '-popular', 'name' => $this->_sSystem . '-popular', 'class' => '', 'title' => '_cmt_browse_popular', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'popular\');'),
  709. );
  710. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-browse', 'menu_items' => $aBrowseLinks));
  711. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sBrowseType);
  712. $sBrowseType = $oMenu->getCode();
  713. }
  714. $sBrowseFilter = '';
  715. $bBrowseFilter = (int)$this->_aSystem['is_browse_filter'] == 1;
  716. if($bBrowseFilter) {
  717. $aFilterLinks = array(
  718. array('id' => $this->_sSystem . '-all', 'name' => $this->_sSystem . '-all', 'class' => '', 'title' => '_cmt_browse_all', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'all\');'),
  719. array('id' => $this->_sSystem . '-friends', 'name' => $this->_sSystem . '-friends', 'class' => '', 'title' => '_cmt_browse_friends', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'friends\');'),
  720. array('id' => $this->_sSystem . '-subscriptions', 'name' => $this->_sSystem . '-subscriptions', 'class' => '', 'title' => '_cmt_browse_subscriptions', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'subscriptions\');')
  721. );
  722. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-filter', 'menu_items' => $aFilterLinks));
  723. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sBrowseFilter);
  724. $sBrowseFilter = $oMenu->getCode();
  725. }
  726. return $this->_oTemplate->parseHtmlByName('comments_controls.html', array(
  727. 'display_switcher' => $bDisplay ? $sDisplay : '',
  728. 'bx_if:is_divider_1' => array(
  729. 'condition' => $bDisplay && $bBrowseType,
  730. 'content' => array(
  731. 'style_prefix' => $this->_sStylePrefix,
  732. )
  733. ),
  734. 'browse_switcher' => $bBrowseType ? $sBrowseType : '',
  735. 'bx_if:is_divider_2' => array(
  736. 'condition' => $bBrowseType && $bBrowseFilter,
  737. 'content' => array(
  738. 'style_prefix' => $this->_sStylePrefix,
  739. )
  740. ),
  741. 'filter_switcher' => $bBrowseFilter ? $sBrowseFilter : '',
  742. ));
  743. }
  744. protected function _getActionsBox(&$aCmt, $aDp = array())
  745. {
  746. $bViewOnly = isset($aDp['view_only']) && $aDp['view_only'] === true;
  747. $bDynamicMode = isset($aDp['dynamic_mode']) && $aDp['dynamic_mode'] === true;
  748. $sMenuActions = '';
  749. if(!$bViewOnly) {
  750. $oMenuActions = BxDolMenu::getObjectInstance($this->_sMenuObjActions);
  751. $oMenuActions->setCmtsData($this, $aCmt['cmt_id']);
  752. $oMenuActions->setDynamicMode($bDynamicMode);
  753. $sMenuActions = $oMenuActions->getCode();
  754. }
  755. return $this->_oTemplate->parseHtmlByName('comment_actions.html', array(
  756. 'id' => $aCmt['cmt_id'],
  757. 'js_object' => $this->_sJsObjName,
  758. 'style_prefix' => $this->_sStylePrefix,
  759. 'menu_actions' => $sMenuActions,
  760. ));
  761. }
  762. protected function _getFormBox($sType, $aBp, $aDp)
  763. {
  764. $iCmtParentId = isset($aBp['parent_id']) ? (int)$aBp['parent_id'] : 0;
  765. $sPosition = isset($aDp['position']) ? $aDp['position'] : '';
  766. $bQuote = isset($aDp['quote']) && (bool)$aDp['quote'];
  767. $bFormMin = $iCmtParentId == 0 && isset($aDp['min_post_form']) && (bool)$aDp['min_post_form'];
  768. $sPositionSystem = $this->_aSystem['post_form_position'];
  769. if(!empty($sPosition) && $sPositionSystem != $sPosition)
  770. return '';
  771. $sClass = '';
  772. if(!empty($sPosition))
  773. $sClass .= ' ' . $this->_sStylePrefix . '-reply-' . $sPosition;
  774. if($bQuote)
  775. $sClass .= ' ' . $this->_sStylePrefix . '-reply-quote';
  776. if($bFormMin)
  777. $sClass .= ' ' . $this->_sStylePrefix . '-reply-min';
  778. if(!empty($aDp['class']))
  779. $sClass .= ' ' . $aDp['class'];
  780. $sClassBody = '';
  781. if(!empty($aDp['class_body']))
  782. $sClassBody .= ' ' . $aDp['class_body'];
  783. $aTmplVarsFormMin = array();
  784. if($bFormMin) {
  785. list($sAuthorName, $sAuthorLink, $sAuthorIcon, $sAuthorUnit) = $this->_getAuthorInfo($this->_getAuthorId());
  786. $oForm = new BxTemplFormView(array());
  787. $aInputPlaceholder = array(
  788. 'type' => 'text',
  789. 'name' => 'comment',
  790. 'caption' => '',
  791. 'attrs' => array(
  792. 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtShowForm(this)',
  793. 'placeholder' => _t($this->_aT['txt_min_form_placeholder'], $sAuthorName),
  794. 'autocomplete' => 'off',
  795. 'readonly' => 'readonly'
  796. ),
  797. 'value' => '',
  798. );
  799. $sClassBodyMin = '';
  800. if(!empty($aDp['class_body_min']))
  801. $sClassBodyMin .= ' ' . $aDp['class_body_min'];
  802. $aTmplVarsFormMin = array(
  803. 'js_object' => $this->_sJsObjName,
  804. 'class_body_min' => $sClassBodyMin,
  805. 'style_prefix' => $this->_sStylePrefix,
  806. 'author_unit' => $sAuthorUnit,
  807. 'placeholder' => $oForm->genRow($aInputPlaceholder)
  808. );
  809. }
  810. $aForm = $this->{'_getForm' . ucfirst($sType)}($iCmtParentId, $aDp);
  811. if(empty($aForm['form']))
  812. return !empty($aForm['msg']) && (isLogged() || $iCmtParentId != 0 || $this->getCommentsCount() > 0) ? MsgBox($aForm['msg']) : '';
  813. return $this->_oTemplate->parseHtmlByName('comment_reply_box.html', array(
  814. 'js_object' => $this->_sJsObjName,
  815. 'style_prefix' => $this->_sStylePrefix,
  816. 'class' => $sClass,
  817. 'class_body' => $sClassBody,
  818. 'bx_if:show_form_min' => array(
  819. 'condition' => $bFormMin,
  820. 'content' => $aTmplVarsFormMin
  821. ),
  822. 'form' => $aForm['form'],
  823. 'form_id' => $aForm['form_id'],
  824. ));
  825. }
  826. protected function _getFormAdd($aValues)
  827. {
  828. $iCmtAuthorId = isset($aValues['cmt_author_id']) ? (int)$aValues['cmt_author_id'] : $this->_getAuthorId();
  829. $iCmtParentId = isset($aValues['cmt_parent_id']) ? (int)$aValues['cmt_parent_id'] : 0;
  830. $oForm = $this->_getForm(BX_CMT_ACTION_POST, $iCmtParentId);
  831. $oForm->aFormAttrs['method'] = BX_DOL_FORM_METHOD_SPECIFIC;
  832. $oForm->aParams['csrf']['disable'] = true;
  833. $oForm->aInputs['cmt_parent_id']['value'] = $iCmtParentId;
  834. if(!empty($oForm->aParams['db']['submit_name'])) {
  835. $sSubmitName = false;
  836. if(is_array($oForm->aParams['db']['submit_name']))
  837. foreach($oForm->aParams['db']['submit_name'] as $sVal) {
  838. if(isset($oForm->aInputs[$sVal])) {
  839. $sSubmitName = $sVal;
  840. break;
  841. }
  842. }
  843. else
  844. $sSubmitName = $oForm->aParams['db']['submit_name'];
  845. if($sSubmitName && isset($oForm->aInputs[$sSubmitName]))
  846. $aValues[$sSubmitName] = $oForm->aInputs[$sSubmitName]['value'];
  847. }
  848. $oForm->initChecker(array(), $aValues);
  849. if(!$oForm->isSubmittedAndValid())
  850. return array('code' => 1, 'message' => '_sys_txt_error_occured');
  851. $iLevel = 0;
  852. $iCmtVisualParentId = 0;
  853. if((int)$iCmtParentId > 0) {
  854. $aParent = $this->getCommentRow($iCmtParentId);
  855. $iLevel = (int)$aParent['cmt_level'] + 1;
  856. $iCmtVisualParentId = $iLevel > $this->getMaxLevel() ? $aParent['cmt_vparent_id'] : $iCmtParentId;
  857. }
  858. $iCmtId = (int)$oForm->insert(array('cmt_vparent_id' => $iCmtVisualParentId, 'cmt_object_id' => $this->_iId, 'cmt_author_id' => $iCmtAuthorId, 'cmt_level' => $iLevel, 'cmt_time' => time()));
  859. if(!$iCmtId) {
  860. if(!$oForm->isValid())
  861. return array('code' => 1, 'message' => '_sys_txt_error_occured');
  862. else
  863. return array('code' => 2, 'message' => '_cmt_err_cannot_perform_action');
  864. }
  865. $iCmtUniqId = $this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId, $iCmtAuthorId);
  866. if($iCmtParentId) {
  867. $this->_oQuery->updateRepliesCount($iCmtParentId, 1);
  868. if(!BxDolModuleQuery::getInstance()->isEnabledByName('bx_notifications'))
  869. $this->_sendNotificationEmail($iCmtId, $iCmtParentId);
  870. }
  871. $this->_triggerComment();
  872. if($this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  873. $oMetatags->metaAdd($iCmtUniqId, $aValues['cmt_text']);
  874. $mixedResult = $this->onPostAfter($iCmtId);
  875. if($mixedResult === false)
  876. return array('code' => 2, 'message' => '_cmt_err_cannot_perform_action');
  877. return $mixedResult;
  878. }
  879. protected function _getFormPost($iCmtParentId = 0, $aDp = array())
  880. {
  881. $bCmtParentId = !empty($iCmtParentId);
  882. if(!$bCmtParentId && !$this->isPostAllowed())
  883. return array('msg' => $this->msgErrPostAllowed());
  884. if($bCmtParentId && !$this->isReplyAllowed($iCmtParentId))
  885. return array('msg' => $this->msgErrReplyAllowed());
  886. $bDynamic = isset($aDp['dynamic_mode']) && (bool)$aDp['dynamic_mode'];
  887. $bQuote = isset($aDp['quote']) && (bool)$aDp['quote'];
  888. $oForm = $this->_getForm(BX_CMT_ACTION_POST, $iCmtParentId);
  889. $oForm->aInputs['cmt_parent_id']['value'] = $iCmtParentId;
  890. if($bQuote) {
  891. $aCmtParent = $this->getCommentRow((int)$iCmtParentId);
  892. if(!empty($aCmtParent['cmt_text']))
  893. $oForm->aInputs['cmt_text']['value'] = $this->_oTemplate->parseHtmlByName('comment_quote.html', array(
  894. 'content' => $aCmtParent['cmt_text']
  895. ));
  896. }
  897. $oForm->initChecker();
  898. if($oForm->isSubmittedAndValid()) {
  899. $iCmtAuthorId = $this->_getAuthorId();
  900. $iCmtParentId = (int)$oForm->getCleanValue('cmt_parent_id');
  901. //--- Process Text ---//
  902. $sCmtText = $oForm->getCleanValue('cmt_text');
  903. $bCmtText = !empty($sCmtText);
  904. //--- Process Media ---//
  905. $aImageIds = $oForm->getCleanValue('cmt_image');
  906. $bImageIds = !empty($aImageIds) && is_array($aImageIds);
  907. if(!$bCmtText && !$bImageIds) {
  908. $oForm->aInputs['cmt_text']['error'] = _t('_Please enter characters');
  909. $oForm->setValid(false);
  910. return array('form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id);
  911. }
  912. $aParent = array();
  913. if($iCmtParentId > 0) {
  914. $aParent = $this->getCommentRow($iCmtParentId);
  915. if(empty($aParent) || !is_array($aParent)) {
  916. $iCmtParentId = 0;
  917. $oForm->setSubmittedValue('cmt_parent_id', $iCmtParentId, $oForm->aFormAttrs['method']);
  918. }
  919. }
  920. $iLevel = 0;
  921. $iCmtVisualParentId = 0;
  922. if($iCmtParentId > 0) {
  923. $iLevel = (int)$aParent['cmt_level'] + 1;
  924. $iCmtVisualParentId = $iLevel > $this->getMaxLevel() ? $aParent['cmt_vparent_id'] : $iCmtParentId;
  925. }
  926. $iCmtId = (int)$oForm->insert(array('cmt_vparent_id' => $iCmtVisualParentId, 'cmt_object_id' => $this->_iId, 'cmt_author_id' => $iCmtAuthorId, 'cmt_level' => $iLevel, 'cmt_time' => time()));
  927. if($iCmtId != 0) {
  928. $iCmtUniqId = $this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId, $iCmtAuthorId);
  929. if($this->isAttachImageEnabled())
  930. $oForm->processImages($this, 'cmt_image', $iCmtUniqId, $iCmtId, $iCmtAuthorId, true);
  931. if($iCmtParentId > 0) {
  932. $this->_oQuery->updateRepliesCount($iCmtParentId, 1);
  933. if(!BxDolModuleQuery::getInstance()->isEnabledByName('bx_notifications'))
  934. $this->_sendNotificationEmail($iCmtId, $iCmtParentId);
  935. }
  936. $this->_triggerComment();
  937. if($iCmtParentId > 0)
  938. $this->isReplyAllowed($iCmtParentId, true);
  939. else
  940. $this->isPostAllowed(true);
  941. if($this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  942. $oMetatags->metaAdd($iCmtUniqId, $sCmtText);
  943. if(($mixedResult = $this->onPostAfter($iCmtId)) !== false)
  944. return $mixedResult;
  945. }
  946. return array('msg' => _t('_cmt_err_cannot_perform_action'));
  947. }
  948. return array('form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id);
  949. }
  950. protected function _getFormEdit($iCmtId, $aDp = array())
  951. {
  952. $bDynamic = isset($aDp['dynamic_mode']) && (bool)$aDp['dynamic_mode'];
  953. $aCmt = $this->getCommentSimple($iCmtId);
  954. if(!$aCmt)
  955. return array('msg' => _t('_No such comment'));
  956. $iCmtAuthorId = $this->_getAuthorId();
  957. if(!$this->isEditAllowed($aCmt))
  958. return array('msg' => $aCmt['cmt_author_id'] == $iCmtAuthorId ? strip_tags($this->msgErrEditAllowed()) : _t('_Access denied'));
  959. $oForm = $this->_getForm(BX_CMT_ACTION_EDIT, $aCmt['cmt_id']);
  960. $oForm->initChecker($aCmt);
  961. if($oForm->isSubmittedAndValid()) {
  962. $sCmtText = $oForm->getCleanValue('cmt_text');
  963. if($oForm->update($iCmtId) !== false) {
  964. $iCmtUniqId = $this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId, (int)$aCmt['cmt_author_id']);
  965. if($this->isAttachImageEnabled())
  966. $oForm->processImages($this, 'cmt_image', $iCmtUniqId, $iCmtId, $iCmtAuthorId, false);
  967. $this->isEditAllowed($aCmt, true);
  968. if($this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  969. $oMetatags->metaAdd($iCmtUniqId, $sCmtText);
  970. if(($mixedResult = $this->onEditAfter($iCmtId)) !== false)
  971. return $mixedResult;
  972. }
  973. return array('msg' => _t('_cmt_err_cannot_perform_action'));
  974. }
  975. return array('form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id);
  976. }
  977. protected function _getForm($sAction, $iId)
  978. {
  979. $oForm = $this->_getFormObject($sAction);
  980. $oForm->setId(sprintf($oForm->getAttributeMask('id'), $sAction, $this->_sSystem, $iId));
  981. $oForm->setName(sprintf($oForm->getAttributeMask('name'), $sAction, $this->_sSystem, $iId));
  982. $oForm->aParams['db']['table'] = $this->_aSystem['table'];
  983. $oForm->aInputs['sys']['value'] = $this->_sSystem;
  984. $oForm->aInputs['id']['value'] = $this->_iId;
  985. $oForm->aInputs['action']['value'] = 'Submit' . ucfirst($sAction) . 'Form';
  986. if(!$this->isAttachImageEnabled())
  987. unset($oForm->aInputs['cmt_image']);
  988. if(isset($oForm->aInputs['cmt_text'])) {
  989. $oForm->aInputs['cmt_text']['html'] = $this->_aSystem['html'];
  990. $oForm->aInputs['cmt_text']['db']['pass'] = $this->isHtml() ? 'XssHtml' : 'XssMultiline';
  991. if(isset($oForm->aInputs['cmt_text']['checker']['func']) && strtolower($oForm->aInputs['cmt_text']['checker']['func']) == 'length') {
  992. $iCmtTextMin = (int)$this->_aSystem['chars_post_min'];
  993. $iCmtTextMax = (int)$this->_aSystem['chars_post_max'];
  994. $oForm->aInputs['cmt_text']['checker']['params'] = array($iCmtTextMin, $iCmtTextMax);
  995. $oForm->aInputs['cmt_text']['checker']['error'] = _t('_Please enter n1-n2 characters', $iCmtTextMin, $iCmtTextMax);
  996. }
  997. }
  998. if($sAction == BX_CMT_ACTION_EDIT && isset($oForm->aInputs['cmt_controls']))
  999. foreach($oForm->aInputs['cmt_controls'] as $mixedKey => $mixedValue) {
  1000. if(!is_numeric($mixedKey) || empty($mixedValue['name']) || $mixedValue['name'] != 'cmt_cancel')
  1001. continue;
  1002. if(!isset($oForm->aInputs['cmt_controls'][$mixedKey]['attrs']))
  1003. $oForm->aInputs['cmt_controls'][$mixedKey]['attrs'] = array();
  1004. $oForm->aInputs['cmt_controls'][$mixedKey]['attrs']['onclick'] = $this->_sJsObjName . '.cmtEdit(this, ' . $iId . ', false)';
  1005. }
  1006. return $oForm;
  1007. }
  1008. protected function _getContent($aCmt)
  1009. {
  1010. $sAttachments = $this->_getAttachments($aCmt);
  1011. $sReactions = '';
  1012. if(($oReaction = $this->getReactionObject($aCmt['cmt_unique_id'])) !== false)
  1013. $sReactions = $oReaction->getCounter(array(
  1014. 'show_counter' => true
  1015. ));
  1016. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameItemContent, array_merge(array(
  1017. 'style_prefix' => $this->_sStylePrefix,
  1018. 'js_object' => $this->_sJsObjName,
  1019. 'bx_if:show_attached' => array(
  1020. 'condition' => !empty($sAttachments),
  1021. 'content' => array(
  1022. 'style_prefix' => $this->_sStylePrefix,
  1023. 'attached' => $sAttachments
  1024. )
  1025. ),
  1026. 'reactions' => $sReactions,
  1027. ), $this->_getTmplVarsText($aCmt)));
  1028. }
  1029. protected function _getMoreLink($sCmts, $aBp = array(), $aDp = array())
  1030. {
  1031. $iStart = $iPerView = 0;
  1032. switch($aBp['type']) {
  1033. case BX_CMT_BROWSE_HEAD:
  1034. case BX_CMT_BROWSE_POPULAR:
  1035. case BX_CMT_BROWSE_CONNECTION:
  1036. $iPerView = $aBp['per_view'];
  1037. $iStart = $aBp['start'] + $iPerView;
  1038. if($iStart >= $aBp['count'])
  1039. return $sCmts;
  1040. break;
  1041. case BX_CMT_BROWSE_TAIL:
  1042. $iPerView = $aBp['per_view'];
  1043. $iStart = $aBp['start'] - $iPerView;
  1044. if($iStart < 0) {
  1045. $iPerView += $iStart;
  1046. $iStart = 0;
  1047. }
  1048. if($iStart == 0 && $iPerView == 0)
  1049. return $sCmts;
  1050. break;
  1051. }
  1052. $sParentKey = isset($aBp['vparent_id']) ? 'vparent_id' : 'parent_id';
  1053. $bRoot = (int)$aBp[$sParentKey] <= 0;
  1054. $sMore = $this->_oTemplate->parseHtmlByName('comment_more.html', array(
  1055. 'js_object' => $this->_sJsObjName,
  1056. 'style_prefix' => $this->_sStylePrefix,
  1057. 'bx_if:is_root' => array(
  1058. 'condition' => $bRoot,
  1059. 'content' => array()
  1060. ),
  1061. 'parent_id' => $aBp[$sParentKey],
  1062. 'start' => $iStart,
  1063. 'per_view' => $iPerView,
  1064. 'title' => _t('_cmt_load_more_' . ($aBp[$sParentKey] == 0 ? 'comments' : 'replies') . '_' . $aBp['type'])
  1065. ));
  1066. switch($aBp['type']) {
  1067. case BX_CMT_BROWSE_HEAD:
  1068. case BX_CMT_BROWSE_POPULAR:
  1069. case BX_CMT_BROWSE_CONNECTION:
  1070. $sCmts .= $sMore;
  1071. break;
  1072. case BX_CMT_BROWSE_TAIL:
  1073. $sCmts = $sMore . $sCmts;
  1074. break;
  1075. }
  1076. return $sCmts;
  1077. }
  1078. protected function _getEmpty($aDp = array())
  1079. {
  1080. $sClass = '';
  1081. if(!empty($aDp['class']))
  1082. $sClass .= ' ' . $aDp['class'];
  1083. return $this->_oTemplate->parseHtmlByName('comment_empty.html', array(
  1084. 'style_prefix' => $this->_sStylePrefix,
  1085. 'class' => $sClass,
  1086. 'content' => MsgBox(_t('_Empty'))
  1087. ));
  1088. }
  1089. protected function _getViewImagePopup()
  1090. {
  1091. $sViewImagePopupId = 'cmts-box-' . $this->_sSystem . '-' . $this->getId() . '-view-image-popup' ;
  1092. $sViewImagePopupContent = $this->_oTemplate->parseHtmlByName('popup_image.html', array(
  1093. 'image_url' => ''
  1094. ));
  1095. return BxTemplFunctions::getInstance()->transBox($sViewImagePopupId, $sViewImagePopupContent, true);
  1096. }
  1097. protected function _getAttachments($aCmt)
  1098. {
  1099. $aTmplImages = array();
  1100. if(!$this->isAttachImageEnabled())
  1101. return '';
  1102. $aFiles = $this->_oQuery->getFiles($this->_aSystem['system_id'], $aCmt['cmt_id']);
  1103. if(!empty($aFiles) && is_array($aFiles)) {
  1104. $oStorage = BxDolStorage::getObjectInstance($this->getStorageObjectName());
  1105. $oTranscoder = BxDolTranscoderImage::getObjectInstance($this->getTranscoderPreviewName());
  1106. foreach($aFiles as $aFile) {
  1107. $bImage = $oTranscoder && $oTranscoder->isMimeTypeSupported($aFile['mime_type']);
  1108. $sPreview = '';
  1109. if($oTranscoder && $bImage)
  1110. $sPreview = $oTranscoder->getFileUrl($aFile['image_id']);
  1111. if(!$sPreview)
  1112. $sPreview = $this->_oTemplate->getIconUrl($oStorage->getIconNameByFileName($aFile['file_name']));
  1113. $aTmplVarsFile = array(
  1114. 'js_object' => $this->_sJsObjName,
  1115. 'preview' => $sPreview,
  1116. 'file' => $oStorage->getFileUrlById($aFile['image_id']),
  1117. 'file_name' => $aFile['file_name'],
  1118. 'file_icon' => $oStorage->getFontIconNameByFileName($aFile['file_name']),
  1119. 'file_size' => _t_format_size($aFile['size']),
  1120. );
  1121. $aTmplImages[] = array(
  1122. 'style_prefix' => $this->_sStylePrefix,
  1123. 'bx_if:show_image' => array(
  1124. 'condition' => $bImage,
  1125. 'content' => $aTmplVarsFile
  1126. ),
  1127. 'bx_if:show_file' => array(
  1128. 'condition' => !$bImage,
  1129. 'content' => $aTmplVarsFile
  1130. ),
  1131. );
  1132. }
  1133. }
  1134. return $this->_oTemplate->parseHtmlByName('comment_attachments.html', array(
  1135. 'bx_repeat:attached' => $aTmplImages
  1136. ));
  1137. }
  1138. protected function _getTmplVarsAuthor($aCmt)
  1139. {
  1140. list($sAuthorName, $sAuthorLink, $sAuthorIcon, $sAuthorUnit) = $this->_getAuthorInfo($aCmt['cmt_author_id']);
  1141. $bAuthorIcon = !empty($sAuthorIcon);
  1142. return array(
  1143. 'author_unit' => $sAuthorUnit,
  1144. 'bx_if:show_icon' => array(
  1145. 'condition' => $bAuthorIcon,
  1146. 'content' => array(
  1147. 'author_icon' => $sAuthorIcon
  1148. )
  1149. ),
  1150. 'bx_if:show_icon_empty' => array(
  1151. 'condition' => !$bAuthorIcon,
  1152. 'content' => array()
  1153. ),
  1154. 'bx_if:show_author_link' => array(
  1155. 'condition' => !empty($sAuthorLink),
  1156. 'content' => array(
  1157. 'author_link' => $sAuthorLink,
  1158. 'author_title' => bx_html_attribute($sAuthorName),
  1159. 'author_name' => $sAuthorName
  1160. )
  1161. ),
  1162. 'bx_if:show_author_text' => array(
  1163. 'condition' => empty($sAuthorLink),
  1164. 'content' => array(
  1165. 'author_name' => $sAuthorName
  1166. )
  1167. ),
  1168. );
  1169. }
  1170. protected function _getTmplVarsText($aCmt)
  1171. {
  1172. $sText = $aCmt['cmt_text'];
  1173. $sTextMore = '';
  1174. if(!$this->isHtml()) {
  1175. $iMaxLength = (int)$this->_aSystem['chars_display_max'];
  1176. if(strlen($sText) > $iMaxLength) {
  1177. $iLength = strpos($sText, ' ', $iMaxLength);
  1178. $sTextMore = trim(substr($sText, $iLength));
  1179. $sText = trim(substr($sText, 0, $iLength));
  1180. }
  1181. }
  1182. $sText = $this->_prepareTextForOutput($sText, $aCmt['cmt_id']);
  1183. $sTextMore = $this->_prepareTextForOutput($sTextMore, $aCmt['cmt_id']);
  1184. return array(
  1185. 'text' => $sText,
  1186. 'bx_if:show_more' => array(
  1187. 'condition' => !empty($sTextMore),
  1188. 'content' => array(
  1189. 'style_prefix' => $this->_sStylePrefix,
  1190. 'js_object' => $this->_sJsObjName,
  1191. 'text_more' => $sTextMore
  1192. )
  1193. ),
  1194. );
  1195. }
  1196. }
  1197. /** @} */