BxBaseCmts.php 59 KB

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