BxBaseCmts.php 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859
  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 $_aAclId2Name;
  25. function __construct( $sSystem, $iId, $iInit = true, $oTemplate = false)
  26. {
  27. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  28. if (empty($sSystem))
  29. return;
  30. $this->_sTmplNameItem = 'comment.html';
  31. $this->_sTmplNameItemContent = 'comment_content.html';
  32. $this->_sStylePrefix = $this->_aSystem['root_style_prefix'] ?? 'cmt';
  33. $this->_sJsObjClass = 'BxDolCmts';
  34. $this->_sJsObjName = 'oCmts' . bx_gen_method_name($sSystem, ['_' , '-']) . '_' . ($iId ?? 0);
  35. $sHtmlId = str_replace(['_' , ' '], ['-', '-'], $sSystem) . '-' . ($iId ?? 0);
  36. $this->_aHtmlIds = [
  37. 'main' => 'bx-cmt-' . $sHtmlId,
  38. 'counter' => 'bx-cmt-counter-' . $sHtmlId
  39. ];
  40. $this->_aElementDefaults = [
  41. 'show_do_comment_as_button' => false,
  42. 'show_do_comment_as_button_small' => false,
  43. 'show_do_comment_icon' => true,
  44. 'show_do_comment_label' => false,
  45. 'show_counter' => true,
  46. 'show_counter_only' => true,
  47. 'show_counter_empty' => false,
  48. 'show_counter_reversed' => false,
  49. 'recalculate_counter' => false
  50. ];
  51. $this->_aElementDefaultsApi = array_merge($this->_aElementDefaults, [
  52. 'show_counter' => true,
  53. ]);
  54. $this->_aElementParamsApi = [];
  55. $this->_aAclId2Name = array();
  56. $aAclLevels = BxDolAcl::getInstance()->getMemberships(false, false, false);
  57. foreach($aAclLevels as $iAclId => $sAclName)
  58. $this->_aAclId2Name[$iAclId] = str_replace('_', '-', str_replace('_adm_prm_txt_level_', '', $sAclName));
  59. if(empty(self::$_sTmplContentElementBlock))
  60. self::$_sTmplContentElementBlock = $this->_oTemplate->getHtml('comment_element_block.html');
  61. if(empty(self::$_sTmplContentElementInline))
  62. self::$_sTmplContentElementInline = $this->_oTemplate->getHtml('comment_element_inline.html');
  63. if(empty(self::$_sTmplContentDoCommentLabel))
  64. self::$_sTmplContentDoCommentLabel = $this->_oTemplate->getHtml('comment_do_comment_label.html');
  65. if(empty(self::$_sTmplContentCounter))
  66. self::$_sTmplContentCounter = $this->_oTemplate->getHtml('comment_counter.html');
  67. $this->_oTemplate->addJsTranslation('_sys_txt_cmt_loading');
  68. }
  69. /**
  70. * Add comments CSS/JS
  71. */
  72. public function addCssJs($aBp = [], $aDp = [])
  73. {
  74. $bDynamicMode = isset($aDp['dynamic_mode']) && $aDp['dynamic_mode'] === true;
  75. $oForm = BxDolForm::getObjectInstance($this->_sFormObject, $this->_sFormDisplayPost);
  76. $oForm->addCssJs();
  77. $sResult = $this->_oTemplate->addCss([
  78. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'photo-swipe/|photoswipe.css',
  79. ], $bDynamicMode);
  80. $sResult .= $this->_oTemplate->addJs([
  81. 'photo-swipe/photoswipe.umd.min.js',
  82. 'photo-swipe/photoswipe-lightbox.umd.min.js',
  83. ], $bDynamicMode);
  84. return $sResult;
  85. }
  86. public function getJsObjectName()
  87. {
  88. return $this->_sJsObjName;
  89. }
  90. /**
  91. * Get initialization section of comments box
  92. *
  93. * @return string
  94. */
  95. public function getJsScript($aBp = [], $aDp = [])
  96. {
  97. $bDynamicMode = isset($aDp['dynamic_mode']) && $aDp['dynamic_mode'] === true;
  98. $bMinPostForm = isset($aDp['min_post_form']) ? $aDp['min_post_form'] : $this->_bMinPostForm;
  99. $aParams = [
  100. 'sObjName' => $this->_sJsObjName,
  101. 'sRootUrl' => BX_DOL_URL_ROOT,
  102. 'sSystem' => $this->getSystemName(),
  103. 'iAuthorId' => $this->_getAuthorId(),
  104. 'iObjId' => $this->getId(),
  105. 'sBaseUrl' => $this->getBaseUrl(),
  106. 'sSocket' => $this->getSocketName(),
  107. 'sPostFormPosition' => $this->_aSystem['post_form_position'],
  108. 'sBrowseType' => $this->_sBrowseType,
  109. 'sDisplayType' => $this->_sDisplayType,
  110. 'iDisplayStructure' => isset($aDp['structure']) && !empty($aDp['structure']) ? 1 : 0,
  111. 'iMinPostForm' => $bMinPostForm ? 1 : 0,
  112. 'sStylePrefix' => $this->_sStylePrefix,
  113. 'aHtmlIds' => $this->_aHtmlIds,
  114. ];
  115. if($this->_aParams)
  116. $aParams['aParams'] = $this->_aParams;
  117. $sIncludes = $this->addCssJs($aBp, $aDp);
  118. return ($bDynamicMode ? $sIncludes : '') . $this->_oTemplate->_wrapInTagJsCode("if(window['" . $this->_sJsObjName . "'] == undefined) var " . $this->_sJsObjName . " = new " . $this->_sJsObjClass . "(" . json_encode($aParams) . "); " . $this->_sJsObjName . ".cmtInit();");
  119. }
  120. function getCommentsBlockAPI($aParams, $aBp = [], $aDp = ['in_designbox' => false, 'show_empty' => false])
  121. {
  122. $mixedResult = $this->isViewAllowed();
  123. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  124. return $mixedResult; // TODO: error checking
  125. $aBp['type'] = 'head';
  126. $this->_getParams($aBp, $aDp);
  127. $this->_prepareParams($aBp, $aDp);
  128. $aBp['order']['way'] = 'desc';
  129. $aBp['order_way'] = 'desc';
  130. $aBp['start'] = 0 ;
  131. $aPp = $aBp['per_view'];
  132. $aBp['per_view'] = $aBp['per_view'] + 1;
  133. $aCmts = [];
  134. if (!isset($aParams['comment_id']))
  135. $aCmts = $this->getCommentsArray($aBp['vparent_id'], $aBp['filter'], $aBp['order'], $aBp['start'], $aBp[($aBp['init_view'] != -1 ? 'init' : 'per') . '_view']);
  136. else
  137. $aCmts = [['cmt_id' => $aParams['comment_id']]];
  138. $aParams['start_from'] = 0;
  139. if (count($aCmts) == $aBp['per_view']){
  140. $aBp['per_view'] = $aPp;
  141. $aCmts = array_slice($aCmts, 0, $aBp['per_view']);
  142. $aParams['start_from'] = $aBp['start'] + $aBp['per_view'];
  143. }
  144. $aCmtsRv = [];
  145. foreach ($aCmts as $aCmt) {
  146. $aCmtsRv[] = $this->getCommentStructure($aCmt['cmt_id'], $aBp, $aDp);
  147. }
  148. return [
  149. 'unit' => 'comments',
  150. 'start' => 0,
  151. 'order' => 'a',
  152. 'module' => $this->_sSystem,
  153. 'object_id' => $this->_iId,
  154. 'data' => $aCmtsRv,
  155. ];
  156. }
  157. function getCommentsBlockTitle()
  158. {
  159. return _t($this->_aT['block_comments_title'], $this->getCommentsCountAll(0, true));
  160. }
  161. /**
  162. * get full comments block with initializations
  163. */
  164. function getCommentsBlock($aBp = [], $aDp = [])
  165. {
  166. $mixedResult = $this->isViewAllowed();
  167. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  168. return $mixedResult;
  169. $this->_getParams($aBp, $aDp);
  170. $oSockets = BxDolSockets::getInstance();
  171. $bSockets = $oSockets->isEnabled();
  172. if($this->_bLiveUpdates && !$bSockets) {
  173. //add live update
  174. $this->actionResumeLiveUpdate();
  175. if(($oLiveUpdates = BxDolLiveUpdates::getInstance())!== false) {
  176. $sServiceCall = BxDolService::getSerializedService('system', 'get_live_update', [$this->_sSystem, $this->_iId, $this->_getAuthorId(), '{count}'], 'TemplCmtsServices');
  177. $oLiveUpdates->add($this->_sSystem . '_live_updates_cmts_' . $this->_iId, 1, $sServiceCall);
  178. }
  179. //add live update
  180. }
  181. $sComments = $this->getComments($aBp, $aDp);
  182. $sCommentsPinned = $this->getCommentsPinned(array_merge($aBp, ['pinned' => 1]), $aDp);
  183. $sContentBefore = $this->_getContentBefore($aBp, $aDp);
  184. $sContentAfter = $this->_getContentAfter($aBp, $aDp);
  185. if($this->_bLiveUpdates && $bSockets)
  186. $sContentAfter .= $this->getLiveUpdateButton();
  187. $sPostFormTop = $this->getFormBoxPost($aBp, array_merge($aDp, ['type' => $this->_sDisplayType, 'position' => BX_CMT_PFP_TOP]));
  188. $sPostFormBottom = $this->getFormBoxPost($aBp, array_merge($aDp, ['type' => $this->_sDisplayType, 'position' => BX_CMT_PFP_BOTTOM]));
  189. $sJsContent = $this->getJsScript($aBp, $aDp);
  190. $sBlockTitle = $this->getCommentsBlockTitle();
  191. $sBlockMenu = $this->_getControlsBox();
  192. /**
  193. * @hooks
  194. * @hookdef hook-system-view_comments 'system', 'view_comments' - hook to override comments block
  195. * - $unit_name - equals `system`
  196. * - $action - equals `view_comments`
  197. * - $object_id - not used
  198. * - $sender_id - not used
  199. * - $extra_params - array of additional params with the following array keys:
  200. * - `object` - [object] an instance of comments, @see BxDolCmts
  201. * - `system` - [string] comments object name
  202. * - `id` - [int] commented content id
  203. * - `params_browse` - [array] browse params array as key&value pairs
  204. * - `params_display` - [array] display params array as key&value pairs
  205. * - `post_form_top` - [string] by ref, post form code for top section, can be overridden in hook processing
  206. * - `content_before` - [string] by ref, a code to be displayed before comments list, can be overridden in hook processing
  207. * - `comments` - [string] by ref, a comments list code, can be overridden in hook processing
  208. * - `comments_pinned` - [string] by ref, a pinned comments list code, can be overridden in hook processing
  209. * - `content_after` - [string] by ref, a code to be displayed after comments list, can be overridden in hook processing
  210. * - `post_form_bottom` - [string] by ref, post form code for bottom section, can be overridden in hook processing
  211. * - `js_content` - [string] by ref, comments JavaScript code, can be overridden in hook processing
  212. * - `block_title` - [string] by ref, block title, can be overridden in hook processing
  213. * - `block_menu` - [string] by ref, block submenu, can be overridden in hook processing
  214. *
  215. * @hook @ref hook-system-view_comments
  216. */
  217. bx_alert('system', 'view_comments', 0, 0, [
  218. 'object' => $this,
  219. 'system' => $this->_sSystem,
  220. 'id' => $this->getId(),
  221. 'params_browse' => $aBp,
  222. 'params_display' => $aDp,
  223. 'post_form_top' => &$sPostFormTop,
  224. 'content_before' => &$sContentBefore,
  225. 'comments' => &$sComments,
  226. 'comments_pinned' => &$sCommentsPinned,
  227. 'content_after' => &$sContentAfter,
  228. 'post_form_bottom' => &$sPostFormBottom,
  229. 'js_content' => &$sJsContent,
  230. 'block_title' => &$sBlockTitle,
  231. 'block_menu' => &$sBlockMenu,
  232. 'post_form_top_ref' => &$sPostFormTop,
  233. 'content_before_ref' => &$sContentBefore,
  234. 'comments_ref' => &$sComments,
  235. 'comments_pinned_ref' => &$sCommentsPinned,
  236. 'content_after_ref' => &$sContentAfter,
  237. 'post_form_bottom_ref' => &$sPostFormBottom,
  238. 'js_content_ref' => &$sJsContent,
  239. 'block_title_ref' => &$sBlockTitle,
  240. 'block_menu_ref' => &$sBlockMenu,
  241. ]);
  242. $sContent = $this->_oTemplate->parseHtmlByName('comments_block.html', [
  243. 'style_prefix' => $this->_sStylePrefix,
  244. 'js_object' => $this->_sJsObjName,
  245. 'system' => $this->_sSystem,
  246. 'list_anchor' => $this->getListAnchor(),
  247. 'id' => $this->getId(),
  248. 'content_before' => $sContentBefore,
  249. 'comments' => $sComments,
  250. 'comments_pinned' => $sCommentsPinned,
  251. 'bx_if:show_divider_hidden' => [
  252. 'condition' => empty($sCommentsPinned),
  253. 'content' => []
  254. ],
  255. 'content_after' => $sContentAfter,
  256. 'post_form_top' => $sPostFormTop,
  257. 'post_form_bottom' => $sPostFormBottom,
  258. 'script' => $sJsContent
  259. ]);
  260. return $aDp['in_designbox'] ? DesignBoxContent($sBlockTitle, $sContent, BX_DB_DEF, $sBlockMenu) : [
  261. 'title' => $sBlockTitle,
  262. 'content' => $sContent,
  263. 'menu' => $sBlockMenu,
  264. ];
  265. }
  266. /**
  267. * get comments list for specified parent comment
  268. *
  269. * @param array $aBp - browse params array
  270. * @param array $aDp - display params array
  271. *
  272. */
  273. function getComments($aBp = [], $aDp = [])
  274. {
  275. $this->_prepareParams($aBp, $aDp);
  276. $aCmts = $this->getCommentsArray($aBp['vparent_id'], $aBp['filter'], $aBp['order'], $aBp['start'], $aBp[($aBp['init_view'] != -1 ? 'init' : 'per') . '_view']);
  277. if(empty($aCmts) || !is_array($aCmts))
  278. return isset($aDp['show_empty']) && $aDp['show_empty'] === true ? $this->_getEmpty($aDp) : '';
  279. $sCmts = '';
  280. foreach($aCmts as $k => $aCmt)
  281. $sCmts .= $this->getComment($aCmt, $aBp, $aDp);
  282. $sCmts = $this->_getMoreLink($sCmts, $aBp, $aDp);
  283. return $sCmts;
  284. }
  285. function getCommentsPinned($aBp = [], $aDp = [])
  286. {
  287. $this->_prepareParams($aBp, $aDp);
  288. $aCmts = $this->getCommentsArray($aBp['vparent_id'], BX_CMT_FILTER_PINNED, $aBp['order'], 0, -1);
  289. if(empty($aCmts) || !is_array($aCmts))
  290. return '';
  291. $sCmts = '';
  292. foreach($aCmts as $k => $aCmt)
  293. $sCmts .= $this->getComment($aCmt, $aBp, $aDp);
  294. return $sCmts;
  295. }
  296. public function getCommentsByStructure($aBp = array(), $aDp = array())
  297. {
  298. if(empty($aDp['structure']))
  299. return isset($aDp['show_empty']) && $aDp['show_empty'] === true ? $this->_getEmpty($aDp) : '';
  300. $aBp['count'] = count($aDp['structure']);
  301. $this->_prepareParams($aBp, $aDp);
  302. $aDp['structure'] = array_slice($aDp['structure'], $aBp['start'], $aBp[($aBp['init_view'] != -1 ? 'init' : 'per') . '_view'], true);
  303. $sCmts = '';
  304. foreach($aDp['structure'] as $iCmtId => $aCmt)
  305. $sCmts .= $this->getComment($iCmtId, $aBp, $aDp);
  306. $sCmts = $this->_getMoreLink($sCmts, $aBp, $aDp);
  307. return $sCmts;
  308. }
  309. /**
  310. * get comment view block with initializations
  311. */
  312. function getCommentBlock($iCmtId = 0, $aBp = array(), $aDp = array())
  313. {
  314. $mixedResult = $this->isViewAllowed();
  315. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  316. return $mixedResult;
  317. $aBp = array_merge(array('type' => $this->_sBrowseType), $aBp);
  318. $aDp = array_merge(array('type' => BX_CMT_DISPLAY_THREADED), $aDp);
  319. //--- Beg: Using pregenerated structure
  320. $mixedStructure = $this->getCommentStructure((int)$iCmtId, $aBp, $aDp);
  321. if($mixedStructure !== false)
  322. $aDp['structure'] = $mixedStructure;
  323. //--- End: Using pregenerated structure
  324. $sComment = $this->getComment($iCmtId, $aBp, $aDp);
  325. if (!$sComment)
  326. return '';
  327. return $this->_oTemplate->parseHtmlByName('comment_block.html', array(
  328. 'style_prefix' => $this->_sStylePrefix,
  329. 'js_object' => $this->_sJsObjName,
  330. 'system' => $this->_sSystem,
  331. 'id' => $this->getId(),
  332. 'comment' => $sComment,
  333. 'script' => $this->getJsScript($aBp, $aDp)
  334. ));
  335. }
  336. /**
  337. * get one just posted comment
  338. *
  339. * @param int $iCmtId - comment id
  340. * @return string
  341. */
  342. function getComment($mixedCmt, $aBp = array(), $aDp = array())
  343. {
  344. $iUserId = $this->_getAuthorId();
  345. $aCmt = !is_array($mixedCmt) ? $this->getCommentRow((int)$mixedCmt) : $mixedCmt;
  346. if (!$aCmt)
  347. return '';
  348. list($sAuthorName, $sAuthorLink, $sAuthorIcon) = $this->_getAuthorInfo($aCmt['cmt_author_id']);
  349. $sClass = $sClassCnt = '';
  350. if(isset($aCmt['vote_rate']) && (float)$aCmt['vote_rate'] < $this->_aSystem['viewing_threshold']) {
  351. $this->_oTemplate->pareseHtmlByName('comment_hidden.html', array(
  352. 'js_object' => $this->_sJsObjName,
  353. 'id' => $aCmt['cmt_id'],
  354. 'title' => bx_process_output(_t('_hidden_comment', $sAuthorName)),
  355. 'bx_if:show_replies' => array(
  356. 'condition' => $aCmt['cmt_replies'] > 0,
  357. 'content' => array(
  358. 'replies' => _t('_Show N replies', $aCmt['cmt_replies'])
  359. )
  360. )
  361. ));
  362. $sClass = ' ' . $this->_sStylePrefix . '-hidden';
  363. }
  364. if($aCmt['cmt_author_id'] == $iUserId)
  365. $sClass .= ' ' . $this->_sStylePrefix . '-mine';
  366. $aAuthorAcl = BxDolAcl::getInstance()->getMemberMembershipInfo($aCmt['cmt_author_id']);
  367. if(!empty($aAuthorAcl) && isset($this->_aAclId2Name[$aAuthorAcl['id']]))
  368. $sClass .= ' ' . $this->_sStylePrefix . '-aml-' . $this->_aAclId2Name[$aAuthorAcl['id']];
  369. if(!empty($aDp['blink']) && in_array($aCmt['cmt_id'], $aDp['blink']))
  370. $sClass .= ' ' . $this->_sStylePrefix . '-blink';
  371. if(!empty($aDp['class_comment']))
  372. $sClass .= ' ' . $aDp['class_comment'];
  373. if(!empty($aDp['class_comment_content']))
  374. $sClassCnt .= ' ' . $aDp['class_comment_content'];
  375. $sHeader = $this->_getHeaderBox($aCmt, $aBp, $aDp);
  376. $sActions = $this->_getActionsBox($aCmt, $aBp, $aDp);
  377. $sReplies = '';
  378. if(!(isset($aBp['pinned']) && (int)$aBp['pinned'] != 0 && (int)$aCmt['cmt_pinned'] != 0) && !empty($aDp)) {
  379. $aDp['show_empty'] = false;
  380. if(!empty($aDp['structure'][$aCmt['cmt_id']]) && is_array($aDp['structure'][$aCmt['cmt_id']])) {
  381. if(!empty($aDp['structure'][$aCmt['cmt_id']]['items'])) {
  382. $aDp['structure'] = $aDp['structure'][$aCmt['cmt_id']]['items'];
  383. $sReplies = $this->getCommentsByStructure(array('parent_id' => $aCmt['cmt_id'], 'type' => $aBp['type']), $aDp);
  384. }
  385. }
  386. else if((int)$aCmt['cmt_replies'] > 0 && $aDp['type'] == BX_CMT_DISPLAY_THREADED)
  387. $sReplies = $this->getComments(['parent_id' => $aCmt['cmt_id'], 'vparent_id' => $aCmt['cmt_id'], 'type' => $aBp['type'], 'order_way' => BX_CMT_ORDER_WAY_ASC], $aDp);
  388. }
  389. if($this->_isShowContent($aCmt)) {
  390. $sContent = $this->_getContent($aCmt, $aBp, $aDp);
  391. }
  392. else {
  393. $sClass .= ' cmt-author-not-active';
  394. $sContent = _t('_hidden_comment', BxDolProfileUndefined::getInstance()->getDisplayName());
  395. }
  396. $aVars = array_merge([
  397. 'system' => $this->_sSystem,
  398. 'style_prefix' => $this->_sStylePrefix,
  399. 'js_object' => $this->_sJsObjName,
  400. 'id' => $aCmt['cmt_id'],
  401. 'anchor' => $this->getItemAnchor($aCmt['cmt_id']),
  402. 'class' => $sClass,
  403. 'class_cnt' => $sClassCnt,
  404. 'header' => $sHeader,
  405. 'content' => $sContent,
  406. 'actions' => $sActions,
  407. 'replies' => $sReplies,
  408. ], $this->_getTmplVarsAuthor($aCmt));
  409. $sResult = $this->_oTemplate->parseHtmlByName($this->_sTmplNameItem, $aVars);
  410. /**
  411. * @hooks
  412. * @hookdef hook-system-view_comment 'system', 'view_comment' - hook on get comment
  413. * - $unit_name - equals `system`
  414. * - $action - equals `view_comment`
  415. * - $object_id - comment_id
  416. * - $sender_id - not used
  417. * - $extra_params - array of additional params with the following array keys:
  418. * - `comment` - [array] comment's data
  419. * - `system` - [string] comment's system name
  420. * - `tmpl_name` - [string] template file path
  421. * - `tmpl_vars` - [string] params for template file parsing
  422. * - `override_result` - [string] by ref, if account confirmed = true, otherwise false, can be overridden in hook processing
  423. * @hook @ref hook-system-view_comment
  424. */
  425. 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));
  426. return $sResult;
  427. }
  428. public function getCommentStructure($iCmtId, $aBp = array(), $aDp = array())
  429. {
  430. $aRoot = $this->getCommentRow((int)$iCmtId);
  431. $aBps = $aBp;
  432. if(!empty($aRoot))
  433. $aBps['parent_id'] = $aRoot['cmt_id'];
  434. $this->_prepareStructureBp($aDp['type'], $aBps);
  435. $iLevel = 0;
  436. $aStructure = array();
  437. $this->{'_getStructure' . ($this->_bIsApi ? 'API' : '')}($aRoot, $aBps, $iLevel, $aStructure);
  438. return !empty($aStructure) && is_array($aStructure) ? $aStructure : false;
  439. }
  440. function getCommentSearch($iCmtId, &$sAddon)
  441. {
  442. $aBp = array();
  443. $aDp = array(
  444. 'type' => BX_CMT_DISPLAY_FLAT,
  445. 'view_only' => true
  446. );
  447. if(empty($sAddon))
  448. $sAddon = $this->getJsScript($aBp, $aDp);
  449. return $this->_oTemplate->parseHtmlByName('comment_search.html', array(
  450. 'comment' => $this->getComment($iCmtId, $aBp, $aDp),
  451. ));
  452. }
  453. /**
  454. * get one comment for "Live Search"
  455. *
  456. * @param int $iCmtId - comment id
  457. * @return string
  458. */
  459. function getCommentLiveSearch($mixedCmt, $aParams = array())
  460. {
  461. $aCmt = !is_array($mixedCmt) ? $this->getCommentRow((int)$mixedCmt) : $mixedCmt;
  462. list($sAuthorName, $sAuthorLink, $sAuthorIcon) = $this->_getAuthorInfo($aCmt['cmt_author_id']);
  463. $bAuthorIcon = !empty($sAuthorIcon);
  464. $sViewLink = $this->getViewUrl($aCmt['cmt_id']);
  465. if(empty($sViewLink))
  466. $sViewLink = '#';
  467. return $this->_oTemplate->parseHtmlByName('comment_live_search.html', array(
  468. 'bx_if:show_icon' => array(
  469. 'condition' => $bAuthorIcon,
  470. 'content' => array(
  471. 'author_icon' => $sAuthorIcon,
  472. 'view_link' => $sViewLink
  473. )
  474. ),
  475. 'bx_if:show_icon_empty' => array(
  476. 'condition' => !$bAuthorIcon,
  477. 'content' => array()
  478. ),
  479. 'view_link' => $sViewLink,
  480. 'text' => BxTemplFunctions::getInstance()->getStringWithLimitedLength(strip_tags($aCmt['cmt_text']), $this->_sSnippetLenthLiveSearch),
  481. 'sample' => isset($aParams['txt_sample_single']) ? _t($aParams['txt_sample_single']) : ''
  482. ));
  483. }
  484. function getFormBoxPost($aBp = array(), $aDp = array())
  485. {
  486. return $this->_getFormBox(BX_CMT_ACTION_POST, $aBp, $aDp);
  487. }
  488. function getFormBoxEdit($aBp = array(), $aDp = array())
  489. {
  490. return $this->_getFormBox(BX_CMT_ACTION_EDIT, $aBp, $aDp);
  491. }
  492. function getFormPost($iCmtParentId = 0, $aDp = array())
  493. {
  494. return $this->_getFormPost($iCmtParentId, $aDp);
  495. }
  496. function getFormEdit($iCmtId, $aDp = array())
  497. {
  498. return $this->_getFormEdit($iCmtId, $aDp);
  499. }
  500. function getControlsBox()
  501. {
  502. return $this->_getControlsBox();
  503. }
  504. function getLiveUpdate($iCountOld = 0, $iCountNew = 0)
  505. {
  506. $iCount = (int)$iCountNew - (int)$iCountOld;
  507. if($iCount < 0)
  508. return '';
  509. $aComments = $this->_oQuery->getCommentsBy(array('type' => 'latest', 'object_id' => $this->_iId, 'author' => $this->_getAuthorId(), 'others' => 1, 'start' => '0', 'per_page' => $iCount));
  510. if(empty($aComments) || !is_array($aComments))
  511. return '';
  512. $aComment = array_shift($aComments);
  513. if(empty($aComment) || !is_array($aComment))
  514. return '';
  515. return $this->getLiveUpdateButton($aComment['cmt_id']);
  516. }
  517. function getLiveUpdateButton($iCommentId = 0)
  518. {
  519. $sJsObject = $this->getJsObjectName();
  520. $bComment = is_numeric($iCommentId) && (int)$iCommentId != 0;
  521. return $this->_oTemplate->parseHtmlByName('comments_lu_button.html', array(
  522. 'style_prefix' => $this->_sStylePrefix,
  523. 'class' => !$bComment ? 'hidden' : '',
  524. 'onclick_show' => "javascript:" . $sJsObject . ".goToBtn(this, '" . ($bComment ? $this->getItemAnchor($iCommentId) : '{cmt_anchor}') . "', '" . ($bComment ? $iCommentId : '{cmt_id}') . "');",
  525. ));
  526. }
  527. function getLiveUpdates($iCountOld = 0, $iCountNew = 0)
  528. {
  529. $bShowAll = true;
  530. $bShowActions = false;
  531. $iCount = (int)$iCountNew - (int)$iCountOld;
  532. if($iCount < 0)
  533. return '';
  534. $aComments = $this->_oQuery->getCommentsBy(array('type' => 'latest', 'object_id' => $this->_iId, 'author' => $this->_getAuthorId(), 'others' => 1, 'start' => '0', 'per_page' => $iCount));
  535. if(empty($aComments) || !is_array($aComments))
  536. return '';
  537. $sJsObject = $this->getJsObjectName();
  538. $iUserId = $this->_getAuthorId();
  539. $bModerator = $this->isModerator();
  540. $aComments = array_reverse($aComments);
  541. $iComments = count($aComments);
  542. $aTmplVarsNotifs = array();
  543. foreach($aComments as $iIndex => $aComment) {
  544. $iCommentId = $aComment['cmt_id'];
  545. $sShowOnClick = "javascript:" . $sJsObject . ".goTo(this, '" . $this->getItemAnchor($iCommentId) . "', '" . $iCommentId . "');";
  546. $sReplyOnClick = "javascript:" . $sJsObject . ".goToAndReply(this, '" . $this->getItemAnchor($iCommentId) . "', '" . $iCommentId . "');";
  547. $iAuthorId = (int)$aComment['cmt_author_id'];
  548. if($iAuthorId < 0) {
  549. if(abs($iAuthorId) == $iUserId)
  550. continue;
  551. else if($bModerator)
  552. $iAuthorId *= -1;
  553. }
  554. $oAuthor = $this->_getAuthorObject($iAuthorId);
  555. $sAuthorName = $oAuthor->getDisplayName();
  556. $aTmplVarsNotifs[] = array(
  557. 'bx_if:show_as_hidden' => array(
  558. 'condition' => !$bShowAll && $iIndex < ($iComments - 1),
  559. 'content' => array(),
  560. ),
  561. 'item' => $this->_oTemplate->parseHtmlByName('comments_lu_notifications.html', array(
  562. 'style_prefix' => $this->_sStylePrefix,
  563. 'onclick_show' => $sShowOnClick,
  564. 'onclick_reply' => $sReplyOnClick,
  565. 'author_link' => $oAuthor->getUrl(),
  566. 'author_title' => bx_html_attribute($sAuthorName),
  567. 'author_name' => $sAuthorName,
  568. 'author_unit' => $oAuthor->getUnit(0, array('template' => 'unit_wo_info_links')),
  569. 'text' => _t('_cmt_txt_added_sample')
  570. )),
  571. 'bx_if:show_previous' => array(
  572. 'condition' => $bShowActions && $iIndex > 0,
  573. 'content' => array(
  574. 'onclick_previous' => $sJsObject . '.previousLiveUpdate(this)'
  575. )
  576. ),
  577. 'bx_if:show_close' => array(
  578. 'condition' => $bShowActions,
  579. 'content' => array(
  580. 'onclick_close' => $sJsObject . '.hideLiveUpdate(this)'
  581. )
  582. )
  583. );
  584. }
  585. return $this->_oTemplate->parseHtmlByName('popup_chain.html', array(
  586. 'html_id' => $this->getNotificationId(),
  587. 'bx_repeat:items' => $aTmplVarsNotifs
  588. ));
  589. }
  590. public function getElementBlock($aParams = array())
  591. {
  592. $aParams['usage'] = BX_CMT_USAGE_BLOCK;
  593. return $this->getElement($aParams);
  594. }
  595. public function getElementInline($aParams = array())
  596. {
  597. $aParams['usage'] = BX_CMT_USAGE_INLINE;
  598. return $this->getElement($aParams);
  599. }
  600. public function getElement($aParams = array())
  601. {
  602. $aParams = array_merge($this->_aElementDefaults, $aParams);
  603. $bShowDoCommentAsButtonSmall = isset($aParams['show_do_comment_as_button_small']) && $aParams['show_do_comment_as_button_small'] == true;
  604. $bShowDoCommentAsButton = !$bShowDoCommentAsButtonSmall && isset($aParams['show_do_comment_as_button']) && $aParams['show_do_comment_as_button'] == true;
  605. $bShowCounterEmpty = isset($aParams['show_counter_empty']) && $aParams['show_counter_empty'] == true;
  606. $bRecalculateCounter = isset($aParams['recalculate_counter']) && $aParams['recalculate_counter'] == true;
  607. $iObjectId = $this->getId();
  608. $iAuthorId = $this->_getAuthorId();
  609. $iAuthorIp = $this->_getAuthorIp();
  610. $iCount = $this->getCommentsCountAll(0, $bRecalculateCounter);
  611. $bCount = (int)$iCount != 0;
  612. $isAllowedComment = $this->isPostAllowed();
  613. //--- Do Comment
  614. $bTmplVarsDoComment = $this->_isShowDoComment($aParams, $isAllowedComment, $bCount);
  615. $aTmplVarsDoComment = [];
  616. if($bTmplVarsDoComment)
  617. $aTmplVarsDoComment = [
  618. 'style_prefix' => $this->_sStylePrefix,
  619. 'do_comment' => $this->_getDoComment($aParams, $isAllowedComment),
  620. ];
  621. //--- Counter
  622. $bTmplVarsCounter = $this->_isShowCounter($aParams, $isAllowedComment, $bCount);
  623. $aTmplVarsCounter = array();
  624. if($bTmplVarsCounter)
  625. $aTmplVarsCounter = array(
  626. 'style_prefix' => $this->_sStylePrefix,
  627. 'bx_if:show_hidden' => array(
  628. 'condition' => !$bShowCounterEmpty && !$bCount,
  629. 'content' => array()
  630. ),
  631. 'counter' => $this->getCounter(array_merge($aParams, [
  632. 'show_counter_only' => false
  633. ]))
  634. );
  635. if(!$bTmplVarsDoComment && !$bTmplVarsCounter)
  636. return '';
  637. $sTmplName = $this->{'_getTmplElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_CMT_USAGE_DEFAULT)}();
  638. return $this->_oTemplate->parseHtmlByContent($sTmplName, array(
  639. 'style_prefix' => $this->_sStylePrefix,
  640. 'html_id' => $this->_aHtmlIds['main'],
  641. 'class' => $this->_sStylePrefix . ($bShowDoCommentAsButton ? '-button' : '') . ($bShowDoCommentAsButtonSmall ? '-button-small' : ''),
  642. 'count' => $iCount,
  643. 'bx_if:show_do_comment' => array(
  644. 'condition' => $bTmplVarsDoComment,
  645. 'content' => $aTmplVarsDoComment
  646. ),
  647. 'bx_if:show_counter' => array(
  648. 'condition' => $bTmplVarsCounter,
  649. 'content' => $aTmplVarsCounter
  650. ),
  651. 'script' => ''
  652. ));
  653. }
  654. public function getElementAPI($aParams = [])
  655. {
  656. if(!$this->_bIsApi)
  657. return;
  658. if(!$this->isEnabled())
  659. return bx_api_get_msg('_cmt_err_not_enabled');
  660. $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
  661. $bRecalculateCounter = isset($aParams['recalculate_counter']) && $aParams['recalculate_counter'] == true;
  662. $iObjectId = $this->getId();
  663. $iAuthorId = $this->_getAuthorId();
  664. $iCount = $this->getCommentsCountAll(0, $bRecalculateCounter);
  665. $bCount = (int)$iCount != 0;
  666. $isAllowedComment = $this->isPostAllowed();
  667. $aParams['count'] = $iCount;
  668. //--- Do Comment
  669. $bDoComment = $this->_isShowDoComment($aParams, $isAllowedComment, $bCount);
  670. $aDoComment = $bDoComment ? $this->_getDoComment($aParams, $isAllowedComment) : [];
  671. //--- Counter
  672. $bCounter = $this->_isShowCounter($aParams, $isAllowedComment, $bCount);
  673. $aCounter = $bCounter ? $this->getCounterAPI(array_merge($aParams, [
  674. 'show_counter_only' => false,
  675. ])) : [];
  676. if(!$bDoComment && !$bCounter)
  677. return bx_api_get_msg('');
  678. return [
  679. 'type' => $this->_sType,
  680. 'system' => $this->_sSystem,
  681. 'object_id' => $this->_iId,
  682. 'ts' => microtime(true),
  683. 'params' => array_intersect_key($aParams, array_flip($this->_aElementParamsApi)),
  684. 'action' => $aDoComment,
  685. 'counter' => $aCounter
  686. ];
  687. }
  688. public function getCounter($aParams = [])
  689. {
  690. $aParams = array_merge($this->_aElementDefaults, $aParams);
  691. $bShowDoCommentAsButtonSmall = isset($aParams['show_do_comment_as_button_small']) && $aParams['show_do_comment_as_button_small'] == true;
  692. $bShowDoCommentAsButton = !$bShowDoCommentAsButtonSmall && isset($aParams['show_do_comment_as_button']) && $aParams['show_do_comment_as_button'] == true;
  693. $bShowEmpty = isset($aParams['show_counter_empty']) && $aParams['show_counter_empty'] == true;
  694. $bShowReversed = isset($aParams['show_counter_reversed']) && $aParams['show_counter_reversed'] == true;
  695. $bShowScript = !isset($aParams['show_script']) || (bool)$aParams['show_script'] === true;
  696. $bRecalculateCounter = isset($aParams['recalculate_counter']) && $aParams['recalculate_counter'] == true;
  697. $sClass = 'sys-action-counter';
  698. if(isset($aParams['show_counter_only']) && (bool)$aParams['show_counter_only'] === true)
  699. $sClass .= ' sys-ac-only';
  700. $iCount = (int)$this->getCommentsCountAll(0, $bRecalculateCounter);
  701. if($iCount == 0 && !$bShowEmpty)
  702. $sClass .= ' sys-ac-hidden';
  703. $sClass .= ' ' . $this->_sStylePrefix . '-counter';
  704. if($bShowDoCommentAsButtonSmall)
  705. $sClass .= ' bx-btn-small-height';
  706. if($bShowDoCommentAsButton)
  707. $sClass .= ' bx-btn-height';
  708. $iCmtsLimit = 5;
  709. $aCmts = $this->_getCounterItems($iCmtsLimit);
  710. if(!$bShowReversed)
  711. $aCmts = array_reverse($aCmts);
  712. $aTmplVarsProfiles = [];
  713. foreach($aCmts as $aCmt) {
  714. $iAuthor = (int)$aCmt['cmt_author_id'];
  715. if(array_key_exists($iAuthor, $aTmplVarsProfiles))
  716. continue;
  717. $oAuthor = BxDolProfile::getInstanceMagic($iAuthor);
  718. if(!$oAuthor)
  719. continue;
  720. $aTmplVarsProfiles[$iAuthor] = [
  721. 'icon' => $oAuthor->getUnit(0, ['template' => ['name' => 'unit_wo_info_links', 'size' => 'icon']])
  722. ];
  723. if(count($aTmplVarsProfiles) >= $iCmtsLimit)
  724. break;
  725. }
  726. $aTmplVarsProfiles = array_values($aTmplVarsProfiles);
  727. $sHref = !empty($aParams['overwrite_counter_link_href']) ? $aParams['overwrite_counter_link_href'] : $this->getListUrl();
  728. $sOnclick = '';
  729. if(!empty($aParams['overwrite_counter_link_onclick']))
  730. $sOnclick = $aParams['overwrite_counter_link_onclick'];
  731. return $this->_oTemplate->parseHtmlByContent($this->_getTmplCounter(), [
  732. 'style_prefix' => $this->_sStylePrefix,
  733. 'id' => $this->_aHtmlIds['counter'],
  734. 'class' => $sClass,
  735. 'href' => $sHref,
  736. 'bx_if:show_onclick' => [
  737. 'condition' => !empty($sOnclick),
  738. 'content' => [
  739. 'onclick' => $sOnclick
  740. ]
  741. ],
  742. 'content' => $this->_getCounterLabel($iCount, $aParams),
  743. 'bx_repeat:profiles' => $aTmplVarsProfiles,
  744. 'bx_if:show_icon' => [
  745. 'condition' => ($bShowEmpty || !empty($aTmplVarsProfiles)) && (!isset($aParams['show_icon']) || $aParams['show_icon']),
  746. 'content' => [
  747. 'style_prefix' => $this->_sStylePrefix,
  748. 'icon' => !empty($aParams['custom_icon']) ? $aParams['custom_icon'] : $this->_oTemplate->getImageAuto('comment|' . $this->_sStylePrefix . '-counter-icon')
  749. ]
  750. ],
  751. 'script' => $this->getJsScript()
  752. ]);
  753. }
  754. public function getCounterApi($aParams = [])
  755. {
  756. $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
  757. $bShowReversed = isset($aParams['show_counter_reversed']) && $aParams['show_counter_reversed'] == true;
  758. $bRecalculateCounter = isset($aParams['recalculate_counter']) && $aParams['recalculate_counter'] == true;
  759. $iCount = (int)$this->getCommentsCountAll(0, $bRecalculateCounter);
  760. $iCmtsLimit = 5;
  761. $aCmts = $this->_getCounterItems($iCmtsLimit);
  762. if(!$bShowReversed)
  763. $aCmts = array_reverse($aCmts);
  764. $aCmtsAuthors = [];
  765. foreach($aCmts as $aCmt) {
  766. $iAuthor = (int)$aCmt['cmt_author_id'];
  767. if(array_key_exists($iAuthor, $aCmtsAuthors))
  768. continue;
  769. $aCmtsAuthors[$iAuthor] = BxDolProfile::getData($iAuthor);
  770. if(count($aCmtsAuthors) >= $iCmtsLimit)
  771. break;
  772. }
  773. $aCmtsAuthors = array_values($aCmtsAuthors);
  774. return [
  775. 'count' => $iCount,
  776. 'authors' => $aCmtsAuthors
  777. ];
  778. }
  779. protected function _getCounterItems($iCmtsLimit, $iCmtsStart = 0)
  780. {
  781. return $this->_oQuery->getCommentsBy(array('type' => 'object_id', 'object_id' => $this->getId(), 'order_way' => 'desc', 'start' => $iCmtsStart, 'per_page' => $iCmtsLimit * 4));
  782. }
  783. protected function _getLabelDo($aParams = [])
  784. {
  785. return $this->_oTemplate->parseHtmlByContent($this->_getTmplLabelDo(), [
  786. 'style_prefix' => $this->_sStylePrefix,
  787. 'bx_if:show_icon' => [
  788. 'condition' => isset($aParams['show_do_comment_icon']) && $aParams['show_do_comment_icon'] == true,
  789. 'content' => [
  790. 'style_prefix' => $this->_sStylePrefix,
  791. 'icon' => $this->_oTemplate->getImageAuto($this->_getIconDo())
  792. ]
  793. ],
  794. 'bx_if:show_text' => [
  795. 'condition' => isset($aParams['show_do_comment_label']) && $aParams['show_do_comment_label'] == true,
  796. 'content' => [
  797. 'style_prefix' => $this->_sStylePrefix,
  798. 'text' => _t($this->_getTitleDo())
  799. ]
  800. ]
  801. ]);
  802. }
  803. protected function _getCounterLabel($iCount, $aParams = [])
  804. {
  805. $aParams = array_merge($this->_aElementDefaults, $aParams);
  806. $sResult = '';
  807. if((int)$iCount != 0)
  808. $sResult = _t(isset($aParams['caption']) ? $aParams['caption'] : '_cmt_txt_counter', $iCount);
  809. else
  810. $sResult = _t(isset($aParams['caption_empty']) ? $aParams['caption_empty'] : '_cmt_txt_counter_empty');
  811. return $sResult;
  812. }
  813. protected function _getTmplElementBlock()
  814. {
  815. return self::$_sTmplContentElementBlock;
  816. }
  817. protected function _getTmplElementInline()
  818. {
  819. return self::$_sTmplContentElementInline;
  820. }
  821. protected function _getTmplLabelDo()
  822. {
  823. return self::$_sTmplContentDoCommentLabel;
  824. }
  825. protected function _getTmplCounter()
  826. {
  827. return self::$_sTmplContentCounter;
  828. }
  829. protected function _isShowContent($aCmt)
  830. {
  831. $oProfileAuthor = BxDolProfile::getInstance(abs($aCmt['cmt_author_id']));
  832. return (int)$aCmt['cmt_author_id'] == 0 || ($oProfileAuthor && $oProfileAuthor->isActive()) || isAdmin() || BxDolAcl::getInstance()->isMemberLevelInSet([MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR]);
  833. }
  834. protected function _getContentBefore($aBp = array(), $aDp = array())
  835. {
  836. return '';
  837. }
  838. protected function _getContentAfter($aBp = array(), $aDp = array())
  839. {
  840. return '';
  841. }
  842. protected function _getControlsBox()
  843. {
  844. $sDisplay = '';
  845. $bDisplay = (int)$this->_aSystem['is_display_switch'] == 1;
  846. if($bDisplay) {
  847. $aDisplayLinks = array(
  848. array('id' => $this->_sSystem . '-flat', 'name' => $this->_sSystem . '-flat', 'class' => '', 'title' => '_cmt_display_flat', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeDisplay(this, \'flat\');'),
  849. array('id' => $this->_sSystem . '-threaded', 'name' => $this->_sSystem . '-threaded', 'class' => '', 'title' => '_cmt_display_threaded', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeDisplay(this, \'threaded\');')
  850. );
  851. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-display', 'menu_items' => $aDisplayLinks));
  852. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sDisplayType);
  853. $sDisplay = $oMenu->getCode();
  854. }
  855. $sBrowseType = '';
  856. $bBrowseType = (int)$this->_aSystem['is_browse_switch'] == 1;
  857. if($bBrowseType) {
  858. $aBrowseLinks = array(
  859. array('id' => $this->_sSystem . '-tail', 'name' => $this->_sSystem . '-tail', 'class' => '', 'title' => '_cmt_browse_tail', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'tail\');'),
  860. array('id' => $this->_sSystem . '-head', 'name' => $this->_sSystem . '-head', 'class' => '', 'title' => '_cmt_browse_head', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'head\');'),
  861. array('id' => $this->_sSystem . '-popular', 'name' => $this->_sSystem . '-popular', 'class' => '', 'title' => '_cmt_browse_popular', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'popular\');'),
  862. );
  863. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-browse', 'menu_items' => $aBrowseLinks));
  864. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sBrowseType);
  865. $sBrowseType = $oMenu->getCode();
  866. }
  867. $sBrowseFilter = '';
  868. $bBrowseFilter = (int)$this->_aSystem['is_browse_filter'] == 1;
  869. if($bBrowseFilter) {
  870. $aFilterLinks = array(
  871. array('id' => $this->_sSystem . '-all', 'name' => $this->_sSystem . '-all', 'class' => '', 'title' => '_cmt_browse_all', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'all\');'),
  872. array('id' => $this->_sSystem . '-friends', 'name' => $this->_sSystem . '-friends', 'class' => '', 'title' => '_cmt_browse_friends', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'friends\');'),
  873. array('id' => $this->_sSystem . '-subscriptions', 'name' => $this->_sSystem . '-subscriptions', 'class' => '', 'title' => '_cmt_browse_subscriptions', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'subscriptions\');')
  874. );
  875. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-filter', 'menu_items' => $aFilterLinks));
  876. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sBrowseFilter);
  877. $sBrowseFilter = $oMenu->getCode();
  878. }
  879. return $this->_oTemplate->parseHtmlByName('comments_controls.html', array(
  880. 'display_switcher' => $bDisplay ? $sDisplay : '',
  881. 'bx_if:is_divider_1' => array(
  882. 'condition' => $bDisplay && $bBrowseType,
  883. 'content' => array(
  884. 'style_prefix' => $this->_sStylePrefix,
  885. )
  886. ),
  887. 'browse_switcher' => $bBrowseType ? $sBrowseType : '',
  888. 'bx_if:is_divider_2' => array(
  889. 'condition' => $bBrowseType && $bBrowseFilter,
  890. 'content' => array(
  891. 'style_prefix' => $this->_sStylePrefix,
  892. )
  893. ),
  894. 'filter_switcher' => $bBrowseFilter ? $sBrowseFilter : '',
  895. ));
  896. }
  897. protected function _getHeaderBox(&$aCmt, $aBp = [], $aDp = [])
  898. {
  899. $aTmplReplyTo = [];
  900. if((int)$aCmt['cmt_parent_id'] != 0) {
  901. $aParent = $this->getCommentRow($aCmt['cmt_parent_id']);
  902. if(!empty($aParent) && is_array($aParent)) {
  903. $oProfile = $this->_getAuthorObject($aParent['cmt_author_id']);
  904. $sParAuthorName = $oProfile->getDisplayName();
  905. $sParAuthorUnit = $oProfile->getUnit(0, ['template' => ['name' => 'unit_wo_info_links', 'size' => 'icon']]);
  906. $aTmplReplyTo = [
  907. 'style_prefix' => $this->_sStylePrefix,
  908. 'par_cmt_link' => $this->getItemUrl($aCmt['cmt_parent_id']),
  909. 'par_cmt_title' => bx_html_attribute(_t('_in_reply_to_x', $sParAuthorName)),
  910. 'par_cmt_author' => $sParAuthorName,
  911. 'par_cmt_author_unit' => $sParAuthorUnit
  912. ];
  913. }
  914. }
  915. $aTmplVarsMeta = [];
  916. if(!empty($this->_sMenuObjMeta) && ($oMenuMeta = BxDolMenu::getObjectInstance($this->_sMenuObjMeta, $this->_oTemplate)) !== false) {
  917. $oMenuMeta->setCmtsData($this, $aCmt['cmt_id']);
  918. $aTmplVarsMeta = [
  919. 'style_prefix' => $this->_sStylePrefix,
  920. 'meta' => $oMenuMeta->getCode()
  921. ];
  922. }
  923. return $this->_oTemplate->parseHtmlByName('comment_header.html', array_merge([
  924. 'style_prefix' => $this->_sStylePrefix,
  925. 'bx_if:show_reply_to' => [
  926. 'condition' => !empty($aTmplReplyTo),
  927. 'content' => $aTmplReplyTo
  928. ],
  929. 'bx_if:meta' => [
  930. 'condition' => !empty($aTmplVarsMeta),
  931. 'content' => $aTmplVarsMeta
  932. ],
  933. 'bx_if:show_pinned' => [
  934. 'condition' => (int)$aCmt['cmt_pinned'] > 0,
  935. 'content' => [
  936. 'style_prefix' => $this->_sStylePrefix,
  937. ]
  938. ],
  939. ], $this->_getTmplVarsNotes($aCmt)));
  940. }
  941. protected function _getActionsBox(&$aCmt, $aBp = [], $aDp = [])
  942. {
  943. $bViewOnly = isset($aDp['view_only']) && $aDp['view_only'] === true;
  944. $bDynamicMode = isset($aDp['dynamic_mode']) && $aDp['dynamic_mode'] === true;
  945. $sMenuActions = '';
  946. if(!$bViewOnly) {
  947. $oMenuActions = BxDolMenu::getObjectInstance($this->_sMenuObjActions);
  948. $oMenuActions->setCmtsData($this, $aCmt['cmt_id'], $aBp, $aDp);
  949. $oMenuActions->setDynamicMode($bDynamicMode);
  950. $sMenuActions = $oMenuActions->getCode();
  951. }
  952. return $this->_oTemplate->parseHtmlByName('comment_actions.html', array(
  953. 'id' => $aCmt['cmt_id'],
  954. 'js_object' => $this->_sJsObjName,
  955. 'style_prefix' => $this->_sStylePrefix,
  956. 'menu_actions' => $sMenuActions,
  957. ));
  958. }
  959. protected function _getCountersBox(&$aCmt, $aBp = [], $aDp = [])
  960. {
  961. $bDynamicMode = isset($aDp['dynamic_mode']) && $aDp['dynamic_mode'] === true;
  962. $oMenuCounters = BxDolMenu::getObjectInstance($this->_sMenuObjCounters);
  963. if(!$oMenuCounters)
  964. return '';
  965. $oMenuCounters->setCmtsData($this, $aCmt['cmt_id'], $aBp, $aDp);
  966. $oMenuCounters->setDynamicMode($bDynamicMode);
  967. return $oMenuCounters->getCode();
  968. }
  969. protected function _getFormBox($sType, $aBp, $aDp)
  970. {
  971. $iCmtParentId = isset($aBp['parent_id']) ? (int)$aBp['parent_id'] : 0;
  972. $sPosition = isset($aDp['position']) ? $aDp['position'] : '';
  973. $bQuote = isset($aDp['quote']) && (bool)$aDp['quote'];
  974. $bFormMin = $iCmtParentId == 0 && isset($aDp['min_post_form']) && (bool)$aDp['min_post_form'];
  975. $sPositionSystem = $this->_aSystem['post_form_position'];
  976. if(!empty($sPosition) && $sPositionSystem != $sPosition)
  977. return '';
  978. $sClass = '';
  979. if(!empty($sPosition))
  980. $sClass .= ' ' . $this->_sStylePrefix . '-reply-' . $sPosition;
  981. if($bQuote)
  982. $sClass .= ' ' . $this->_sStylePrefix . '-reply-quote';
  983. if($bFormMin)
  984. $sClass .= ' ' . $this->_sStylePrefix . '-reply-min';
  985. if(!empty($aDp['class']))
  986. $sClass .= ' ' . $aDp['class'];
  987. $sClassBody = '';
  988. if(!empty($aDp['class_body']))
  989. $sClassBody .= ' ' . $aDp['class_body'];
  990. $aTmplVarsFormMin = array();
  991. if($bFormMin) {
  992. list($sAuthorName, $sAuthorLink, $sAuthorIcon, $sAuthorUnit) = $this->_getAuthorInfo($this->_getAuthorId());
  993. $oForm = new BxTemplFormView(array());
  994. $aInputPlaceholder = array(
  995. 'type' => 'text',
  996. 'name' => 'comment',
  997. 'caption' => '',
  998. 'attrs' => array(
  999. 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtShowForm(this)',
  1000. 'placeholder' => _t($this->_aT['txt_min_form_placeholder'], $sAuthorName),
  1001. 'autocomplete' => 'off',
  1002. 'readonly' => 'readonly'
  1003. ),
  1004. 'value' => '',
  1005. );
  1006. $sClassBodyMin = '';
  1007. if(!empty($aDp['class_body_min']))
  1008. $sClassBodyMin .= ' ' . $aDp['class_body_min'];
  1009. $aTmplVarsFormMin = array(
  1010. 'js_object' => $this->_sJsObjName,
  1011. 'class_body_min' => $sClassBodyMin,
  1012. 'style_prefix' => $this->_sStylePrefix,
  1013. 'author_unit' => $sAuthorUnit,
  1014. 'placeholder' => $oForm->genRow($aInputPlaceholder)
  1015. );
  1016. }
  1017. $aForm = $this->{'_getForm' . ucfirst($sType)}($iCmtParentId, $aDp);
  1018. if(empty($aForm['form']))
  1019. return !empty($aForm['msg']) ? MsgBox($aForm['msg']) : '';
  1020. return $this->_oTemplate->parseHtmlByName('comment_reply_box.html', array(
  1021. 'js_object' => $this->_sJsObjName,
  1022. 'style_prefix' => $this->_sStylePrefix,
  1023. 'class' => $sClass,
  1024. 'class_body' => $sClassBody,
  1025. 'bx_if:show_form_min' => array(
  1026. 'condition' => $bFormMin,
  1027. 'content' => $aTmplVarsFormMin
  1028. ),
  1029. 'form' => $aForm['form'],
  1030. 'form_id' => $aForm['form_id'],
  1031. ));
  1032. }
  1033. protected function _getFormDirectEdit($aValues, $iCmtId)
  1034. {
  1035. return $this->_getFormManual($aValues, $iCmtId);
  1036. }
  1037. protected function _getFormDirectAdd($aValues)
  1038. {
  1039. return $this->_getFormManual($aValues);
  1040. }
  1041. protected function _getFormManual($aValues, $iEditedCmtId = 0)
  1042. {
  1043. $aCmt = [];
  1044. if ($iEditedCmtId) {
  1045. $aCmt = $this->getCommentSimple($iEditedCmtId);
  1046. if(!$aCmt)
  1047. return bx_is_api() ? bx_api_get_msg(_t('_No such comment')) : array('msg' => _t('_No such comment'));
  1048. }
  1049. $iCmtAuthorId = isset($aValues['cmt_author_id']) ? (int)$aValues['cmt_author_id'] : $this->_getAuthorId();
  1050. $iCmtParentId = isset($aValues['cmt_parent_id']) ? (int)$aValues['cmt_parent_id'] : 0;
  1051. $oForm = $this->_getForm($iEditedCmtId ? BX_CMT_ACTION_EDIT : BX_CMT_ACTION_POST, $iCmtParentId);
  1052. $oForm->aFormAttrs['method'] = BX_DOL_FORM_METHOD_SPECIFIC;
  1053. $oForm->aParams['csrf']['disable'] = true;
  1054. $oForm->aInputs['cmt_parent_id']['value'] = $iCmtParentId;
  1055. if(!empty($oForm->aParams['db']['submit_name'])) {
  1056. $sSubmitName = false;
  1057. if(is_array($oForm->aParams['db']['submit_name']))
  1058. foreach($oForm->aParams['db']['submit_name'] as $sVal) {
  1059. if(isset($oForm->aInputs[$sVal])) {
  1060. $sSubmitName = $sVal;
  1061. break;
  1062. }
  1063. }
  1064. else
  1065. $sSubmitName = $oForm->aParams['db']['submit_name'];
  1066. if($sSubmitName) {
  1067. if(isset($oForm->aInputs[$sSubmitName]))
  1068. $aValues[$sSubmitName] = $oForm->aInputs[$sSubmitName]['value'];
  1069. else if(isset($oForm->aInputs['cmt_controls'])) {
  1070. foreach($oForm->aInputs['cmt_controls'] as $mKey => $mValue)
  1071. if(is_numeric($mKey) && is_array($mValue) && isset($mValue['name']) && $mValue['name'] == $sSubmitName) {
  1072. $aValues[$sSubmitName] = $mValue['value'];
  1073. break;
  1074. }
  1075. }
  1076. }
  1077. }
  1078. $oForm->initChecker(array(), $aValues);
  1079. if(!$oForm->isSubmittedAndValid())
  1080. return array('code' => 1, 'message' => '_sys_txt_error_occured');
  1081. $iLevel = 0;
  1082. $iCmtVisualParentId = 0;
  1083. if((int)$iCmtParentId > 0) {
  1084. $aParent = $this->getCommentRow($iCmtParentId);
  1085. $iLevel = (int)($aParent['cmt_level'] ?? 0) + 1;
  1086. $iCmtVisualParentId = $iLevel > $this->getMaxLevel() ? $aParent['cmt_vparent_id'] : $iCmtParentId;
  1087. }
  1088. $iCmtId = 0;
  1089. if ($iEditedCmtId) {
  1090. $bRes = $oForm->update($iEditedCmtId);
  1091. if ($bRes)
  1092. $iCmtId = $iEditedCmtId;
  1093. } else {
  1094. $iCmtId = (int)$oForm->insert(array('cmt_vparent_id' => $iCmtVisualParentId, 'cmt_object_id' => $this->_iId, 'cmt_author_id' => $iCmtAuthorId, 'cmt_level' => $iLevel, 'cmt_time' => time()));
  1095. }
  1096. if(!$iCmtId) {
  1097. if(!$oForm->isValid())
  1098. return array('code' => 1, 'message' => '_sys_txt_error_occured');
  1099. else
  1100. return array('code' => 2, 'message' => '_cmt_err_cannot_perform_action');
  1101. }
  1102. $iCmtUniqId = 0;
  1103. if ($iEditedCmtId) {
  1104. $iCmtUniqId = $this->_oQuery->getUniqId($this->_aSystem['system_id'], $iEditedCmtId, [
  1105. 'author_id' => (int)$aCmt['cmt_author_id']
  1106. ]);
  1107. $sStatusAdmin = $this->getStatusAdmin();
  1108. if($sStatusAdmin !== BX_CMT_STATUS_ACTIVE)
  1109. $this->_oQuery->updateUniqId(['status_admin' => $sStatusAdmin], ['id' => $iCmtUniqId]);
  1110. }
  1111. else {
  1112. $iCmtUniqId = $this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId, [
  1113. 'author_id' => $iCmtAuthorId,
  1114. 'status_admin' => $this->getStatusAdmin()
  1115. ]);
  1116. }
  1117. if($iCmtParentId && !$iEditedCmtId) {
  1118. $this->_oQuery->updateRepliesCount($iCmtParentId, 1);
  1119. if(!BxDolModuleQuery::getInstance()->isEnabledByName('bx_notifications'))
  1120. $this->_sendNotificationEmail($iCmtId, $iCmtParentId);
  1121. }
  1122. $this->_triggerComment();
  1123. if($this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  1124. $oMetatags->metaAdd($iCmtUniqId, $aValues['cmt_text']);
  1125. $mixedResult = false;
  1126. if ($iEditedCmtId) {
  1127. $mixedResult = $this->onEditAfter($iCmtId);
  1128. }
  1129. else {
  1130. $mixedResult = $this->onPostAfter($iCmtId);
  1131. }
  1132. if($mixedResult === false)
  1133. return array('code' => 2, 'message' => '_cmt_err_cannot_perform_action');
  1134. return $mixedResult;
  1135. }
  1136. protected function _getFormPost($iCmtParentId = 0, $aDp = [])
  1137. {
  1138. $bCmtParentId = !empty($iCmtParentId);
  1139. if(!$bCmtParentId && !$this->isPostAllowed()) {
  1140. $sMsg = '';
  1141. if(!isLogged()) {
  1142. $oPermalink = BxDolPermalinks::getInstance();
  1143. $sMsg = _t('_cmt_msg_login_required', $oPermalink->permalink('page.php?i=login'), $oPermalink->permalink('page.php?i=create-account'));
  1144. }
  1145. else
  1146. $sMsg = $this->msgErrPostAllowed();
  1147. return bx_is_api() ? bx_api_get_msg($sMsg) : ['msg' => $sMsg];
  1148. }
  1149. if($bCmtParentId && !$this->isReplyAllowed($iCmtParentId))
  1150. return bx_is_api() ? bx_api_get_msg($sMsg) : ['msg' => $this->msgErrReplyAllowed()];
  1151. $bDynamic = isset($aDp['dynamic_mode']) && (bool)$aDp['dynamic_mode'];
  1152. $bQuote = isset($aDp['quote']) && (bool)$aDp['quote'];
  1153. $oForm = $this->_getForm(BX_CMT_ACTION_POST, $iCmtParentId, $aDp);
  1154. $oForm->aInputs['cmt_parent_id']['value'] = $iCmtParentId;
  1155. if($bQuote) {
  1156. $aCmtParent = $this->getCommentRow((int)$iCmtParentId);
  1157. if(!empty($aCmtParent['cmt_text']))
  1158. $oForm->aInputs['cmt_text']['value'] = $this->_oTemplate->parseHtmlByName('comment_quote.html', [
  1159. 'content' => $aCmtParent['cmt_text']
  1160. ]);
  1161. }
  1162. $oForm->initChecker();
  1163. if($oForm->isSubmittedAndValid()) {
  1164. $iCmtAuthorId = $this->_getAuthorId();
  1165. $iCmtParentId = (int)$oForm->getCleanValue('cmt_parent_id');
  1166. //--- Process Text ---//
  1167. $sCmtText = $oForm->getCleanValue('cmt_text');
  1168. $bCmtText = !empty($sCmtText);
  1169. //--- Process Media ---//
  1170. $aImageIds = $oForm->getCleanValue('cmt_image');
  1171. $bImageIds = !empty($aImageIds) && is_array($aImageIds);
  1172. if(!$bCmtText && !$bImageIds) {
  1173. $oForm->aInputs['cmt_text']['error'] = _t('_Please enter characters');
  1174. $oForm->setValid(false);
  1175. return bx_is_api() ? ['form' => $oForm, 'res' => 0] : ['form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id];
  1176. }
  1177. $aParent = [];
  1178. if($iCmtParentId > 0) {
  1179. $aParent = $this->getCommentRow($iCmtParentId);
  1180. if(empty($aParent) || !is_array($aParent)) {
  1181. $iCmtParentId = 0;
  1182. $oForm->setSubmittedValue('cmt_parent_id', $iCmtParentId, $oForm->aFormAttrs['method']);
  1183. }
  1184. }
  1185. $iLevel = 0;
  1186. $iCmtVisualParentId = 0;
  1187. if($iCmtParentId > 0) {
  1188. $iLevel = (int)$aParent['cmt_level'] + 1;
  1189. $iCmtVisualParentId = $iLevel > $this->getMaxLevel() ? $aParent['cmt_vparent_id'] : $iCmtParentId;
  1190. }
  1191. $iCmtId = (int)$oForm->insert(['cmt_vparent_id' => $iCmtVisualParentId, 'cmt_object_id' => $this->_iId, 'cmt_author_id' => $iCmtAuthorId, 'cmt_level' => $iLevel, 'cmt_time' => time()]);
  1192. if($iCmtId != 0) {
  1193. $iCmtUniqId = $this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId, [
  1194. 'author_id' => $iCmtAuthorId,
  1195. 'status_admin' => $this->getStatusAdmin()
  1196. ]);
  1197. if($this->isAttachImageEnabled())
  1198. $oForm->processImages($this, 'cmt_image', $iCmtUniqId, $iCmtId, $iCmtAuthorId, true);
  1199. if($iCmtParentId > 0) {
  1200. $this->_oQuery->updateRepliesCount($iCmtParentId, 1);
  1201. if(!BxDolModuleQuery::getInstance()->isEnabledByName('bx_notifications'))
  1202. $this->_sendNotificationEmail($iCmtId, $iCmtParentId);
  1203. }
  1204. $this->_triggerComment();
  1205. if($iCmtParentId > 0)
  1206. $this->isReplyAllowed($iCmtParentId, true);
  1207. else
  1208. $this->isPostAllowed(true);
  1209. if($this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  1210. $oMetatags->metaAdd($iCmtUniqId, $sCmtText);
  1211. if(($mixedResult = $this->onPostAfter($iCmtId, $aDp)) !== false){
  1212. if (bx_is_api()){
  1213. $this->_unsetFormObject(BX_CMT_ACTION_POST);
  1214. return ['form' => $this->_getForm(BX_CMT_ACTION_POST, $iCmtParentId), 'res' => $iCmtId];
  1215. }
  1216. else{
  1217. return $mixedResult;
  1218. }
  1219. }
  1220. }
  1221. return bx_is_api() ? bx_api_get_msg('_cmt_err_cannot_perform_action') : ['msg' => _t('_cmt_err_cannot_perform_action')];
  1222. }
  1223. return bx_is_api() ? ['form' => $oForm, 'res' => 0] : ['form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id];
  1224. }
  1225. protected function _getFormEdit($iCmtId, $aDp = [])
  1226. {
  1227. $bDynamic = isset($aDp['dynamic_mode']) && (bool)$aDp['dynamic_mode'];
  1228. $aCmt = $this->getCommentSimple($iCmtId);
  1229. if(!$aCmt)
  1230. return bx_is_api() ? bx_api_get_msg(_t('_No such comment')) : array('msg' => _t('_No such comment'));
  1231. $iCmtAuthorId = $this->_getAuthorId();
  1232. if(!$this->isEditAllowed($aCmt)){
  1233. $sMsg = $aCmt['cmt_author_id'] == $iCmtAuthorId ? strip_tags($this->msgErrEditAllowed()) : _t('_Access denied');
  1234. return bx_is_api() ? bx_api_get_msg($sMsg) : array('msg' => $sMsg);
  1235. }
  1236. $oForm = $this->_getForm(BX_CMT_ACTION_EDIT, $aCmt['cmt_id'], $aDp);
  1237. $oForm->initChecker($aCmt);
  1238. if($oForm->isSubmittedAndValid()) {
  1239. $sCmtText = $oForm->getCleanValue('cmt_text');
  1240. if($oForm->update($iCmtId) !== false) {
  1241. $iCmtUniqId = $this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId, [
  1242. 'author_id' => (int)$aCmt['cmt_author_id']
  1243. ]);
  1244. $sStatusAdmin = $this->getStatusAdmin();
  1245. if($sStatusAdmin !== BX_CMT_STATUS_ACTIVE)
  1246. $this->_oQuery->updateUniqId(['status_admin' => $sStatusAdmin], ['id' => $iCmtUniqId]);
  1247. if($this->isAttachImageEnabled())
  1248. $oForm->processImages($this, 'cmt_image', $iCmtUniqId, $iCmtId, $iCmtAuthorId, false);
  1249. $this->_triggerComment();
  1250. $this->isEditAllowed($aCmt, true);
  1251. if($this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  1252. $oMetatags->metaAdd($iCmtUniqId, $sCmtText);
  1253. if(($mixedResult = $this->onEditAfter($iCmtId, $aDp)) !== false){
  1254. if (bx_is_api()){
  1255. $this->_unsetFormObject(BX_CMT_ACTION_POST);
  1256. return ['form' => $this->_getForm(BX_CMT_ACTION_POST, $aCmt['cmt_parent_id']), 'res' => $iCmtId];
  1257. }
  1258. else{
  1259. return $mixedResult;
  1260. }
  1261. }
  1262. }
  1263. return bx_is_api() ? bx_api_get_msg(_t('_cmt_err_cannot_perform_action')) : array('msg' => _t('_cmt_err_cannot_perform_action'));
  1264. }
  1265. return bx_is_api() ? ['form' => $oForm, 'res' => 0] : array('form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id);
  1266. }
  1267. protected function _getForm($sAction, $iId, $aDp = [])
  1268. {
  1269. $oForm = $this->_getFormObject($sAction);
  1270. $oForm->setId(sprintf($oForm->getAttributeMask('id'), $sAction, $this->_sSystem, $this->_iId, $iId));
  1271. $oForm->setName(sprintf($oForm->getAttributeMask('name'), $sAction, $this->_sSystem, $this->_iId, $iId));
  1272. $oForm->aParams['db']['table'] = $this->_aSystem['table'];
  1273. $oForm->aInputs['sys']['value'] = $this->_sSystem;
  1274. $oForm->aInputs['id']['value'] = $this->_iId;
  1275. $oForm->aInputs['action']['value'] = 'Submit' . ucfirst($sAction) . 'Form';
  1276. if($this->_aParams)
  1277. $oForm->aInputs = ['params' => [
  1278. 'name' => 'params',
  1279. 'type' => 'hidden',
  1280. 'value' => json_encode($this->_aParams)
  1281. ]] + $oForm->aInputs;
  1282. if(!$this->isAttachImageEnabled())
  1283. unset($oForm->aInputs['cmt_image']);
  1284. if(isset($oForm->aInputs['cmt_text'])) {
  1285. $oForm->aInputs['cmt_text']['html'] = $this->_aSystem['html'];
  1286. $oForm->aInputs['cmt_text']['db']['pass'] = $this->isHtml() ? 'XssHtml' : 'XssMultiline';
  1287. if(isset($oForm->aInputs['cmt_text']['checker']['func']) && strtolower($oForm->aInputs['cmt_text']['checker']['func']) == 'length') {
  1288. $iCmtTextMin = (int)$this->_aSystem['chars_post_min'];
  1289. $iCmtTextMax = (int)$this->_aSystem['chars_post_max'];
  1290. $oForm->aInputs['cmt_text']['checker']['params'] = array($iCmtTextMin, $iCmtTextMax);
  1291. $oForm->aInputs['cmt_text']['checker']['error'] = _t('_Please enter n1-n2 characters', $iCmtTextMin, $iCmtTextMax);
  1292. }
  1293. }
  1294. if($sAction == BX_CMT_ACTION_EDIT && isset($oForm->aInputs['cmt_controls']))
  1295. foreach($oForm->aInputs['cmt_controls'] as $mixedKey => $mixedValue) {
  1296. if(!is_numeric($mixedKey) || empty($mixedValue['name']) || $mixedValue['name'] != 'cmt_cancel')
  1297. continue;
  1298. if(!isset($oForm->aInputs['cmt_controls'][$mixedKey]['attrs']))
  1299. $oForm->aInputs['cmt_controls'][$mixedKey]['attrs'] = array();
  1300. $oForm->aInputs['cmt_controls'][$mixedKey]['attrs']['onclick'] = $this->_sJsObjName . '.cmtEdit(this, ' . $iId . ', false)';
  1301. }
  1302. return $oForm;
  1303. }
  1304. protected function _getContent($aCmt, $aBp = [], $aDp = [])
  1305. {
  1306. $sAttachments = $this->_getAttachments($aCmt);
  1307. $sCounters = $this->_getCountersBox($aCmt, $aBp, $aDp);
  1308. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameItemContent, array_merge(array(
  1309. 'style_prefix' => $this->_sStylePrefix,
  1310. 'js_object' => $this->_sJsObjName,
  1311. 'bx_if:show_attached' => [
  1312. 'condition' => !empty($sAttachments),
  1313. 'content' => [
  1314. 'style_prefix' => $this->_sStylePrefix,
  1315. 'attached' => $sAttachments
  1316. ]
  1317. ],
  1318. 'bx_if:show_counters' => [
  1319. 'condition' => !empty($sCounters),
  1320. 'content' => [
  1321. 'style_prefix' => $this->_sStylePrefix,
  1322. 'counters' => $sCounters,
  1323. ]
  1324. ]
  1325. ), $this->_getTmplVarsText($aCmt)));
  1326. }
  1327. protected function _getMoreLink($sCmts, $aBp = array(), $aDp = array())
  1328. {
  1329. $iStart = $iPerView = 0;
  1330. switch($aBp['type']) {
  1331. case BX_CMT_BROWSE_HEAD:
  1332. case BX_CMT_BROWSE_POPULAR:
  1333. case BX_CMT_BROWSE_CONNECTION:
  1334. $iPerView = $aBp['per_view'];
  1335. $iStart = $aBp['start'] + $iPerView;
  1336. if($iStart >= $aBp['count'])
  1337. return $sCmts;
  1338. break;
  1339. case BX_CMT_BROWSE_TAIL:
  1340. $iPerView = $aBp['per_view'];
  1341. $iStart = $aBp['start'] - $iPerView;
  1342. if($iStart < 0) {
  1343. $iPerView += $iStart;
  1344. $iStart = 0;
  1345. }
  1346. if($iStart == 0 && $iPerView == 0)
  1347. return $sCmts;
  1348. break;
  1349. }
  1350. $sParentKey = isset($aBp['vparent_id']) ? 'vparent_id' : 'parent_id';
  1351. $bRoot = (int)$aBp[$sParentKey] <= 0;
  1352. $sMore = $this->_oTemplate->parseHtmlByName('comment_more.html', array(
  1353. 'js_object' => $this->_sJsObjName,
  1354. 'style_prefix' => $this->_sStylePrefix,
  1355. 'bx_if:is_root' => array(
  1356. 'condition' => $bRoot,
  1357. 'content' => array()
  1358. ),
  1359. 'parent_id' => $aBp[$sParentKey],
  1360. 'start' => $iStart,
  1361. 'per_view' => $iPerView,
  1362. 'title' => _t('_cmt_load_more_' . ($aBp[$sParentKey] == 0 ? 'comments' : 'replies') . '_' . $aBp['type'])
  1363. ));
  1364. switch($aBp['type']) {
  1365. case BX_CMT_BROWSE_HEAD:
  1366. case BX_CMT_BROWSE_POPULAR:
  1367. case BX_CMT_BROWSE_CONNECTION:
  1368. $sCmts .= $sMore;
  1369. break;
  1370. case BX_CMT_BROWSE_TAIL:
  1371. $sCmts = $sMore . $sCmts;
  1372. break;
  1373. }
  1374. return $sCmts;
  1375. }
  1376. protected function _getEmpty($aDp = array())
  1377. {
  1378. $sClass = '';
  1379. if(!empty($aDp['class']))
  1380. $sClass .= ' ' . $aDp['class'];
  1381. return $this->_oTemplate->parseHtmlByName('comment_empty.html', array(
  1382. 'style_prefix' => $this->_sStylePrefix,
  1383. 'class' => $sClass,
  1384. 'content' => MsgBox(_t('_Empty'))
  1385. ));
  1386. }
  1387. protected function _getAttachments($aCmt)
  1388. {
  1389. if(!$this->isAttachImageEnabled())
  1390. return $this->_bIsApi ? [] : '';
  1391. $aTmplImages = [];
  1392. if(($aFiles = $this->_oQuery->getFiles($this->_aSystem['system_id'], $aCmt['cmt_id'])) && is_array($aFiles)) {
  1393. $oStorage = BxDolStorage::getObjectInstance($this->getStorageObjectName());
  1394. $oTranscoder = BxDolTranscoderImage::getObjectInstance($this->getTranscoderPreviewName());
  1395. foreach($aFiles as $aFile) {
  1396. $sFile = $oStorage->getFileUrlById($aFile['image_id']);
  1397. if(!$sFile)
  1398. continue;
  1399. $bImage = $oTranscoder && ($oTranscoder->isMimeTypeSupported($aFile['mime_type']) || $oTranscoder->isMimeTypeSvg($aFile['mime_type']));
  1400. $sPreview = '';
  1401. $iWidth = $iHeight = 0;
  1402. if($bImage) {
  1403. if(!$this->_bIsApi && $oTranscoder)
  1404. $sPreview = $oTranscoder->getFileUrl($aFile['image_id']);
  1405. if(empty($aFile['dimensions'])) {
  1406. $aFileInfo = $oStorage->getFile($aFile['image_id']);
  1407. if(!empty($aFileInfo['dimensions']))
  1408. $aFile['dimensions'] = $aFileInfo['dimensions'];
  1409. }
  1410. if(!empty($aFile['dimensions']))
  1411. list($iWidth, $iHeight) = explode('x', $aFile['dimensions']);
  1412. }
  1413. if(!$this->_bIsApi && !$sPreview)
  1414. $sPreview = $this->_oTemplate->getIconUrl($oStorage->getIconNameByFileName($aFile['file_name']));
  1415. $aTmplVarsFile = array(
  1416. 'js_object' => $this->_sJsObjName,
  1417. 'preview' => $sPreview,
  1418. 'file' => $sFile,
  1419. 'file_id' => $aFile['id'],
  1420. 'file_name' => $aFile['file_name'],
  1421. 'file_icon' => $oStorage->getFontIconNameByFileName($aFile['file_name']),
  1422. 'file_size' => _t_format_size($aFile['size']),
  1423. 'w' => $iWidth,
  1424. 'h' => $iHeight
  1425. );
  1426. if($this->_bIsApi) {
  1427. $aTmplImages[] = array(
  1428. 'is_image' => $bImage,
  1429. 'file' => $aTmplVarsFile['file'],
  1430. 'file_id' => $aTmplVarsFile['file_id'],
  1431. 'file_name' => $aTmplVarsFile['file_name'],
  1432. 'file_icon' => $aTmplVarsFile['file_icon'],
  1433. 'file_size' => $aTmplVarsFile['file_size'],
  1434. 'width' => $aTmplVarsFile['w'],
  1435. 'height' => $aTmplVarsFile['h'],
  1436. );
  1437. }
  1438. else {
  1439. $aTmplImages[] = array(
  1440. 'style_prefix' => $this->_sStylePrefix,
  1441. 'bx_if:show_image' => array(
  1442. 'condition' => $bImage,
  1443. 'content' => $aTmplVarsFile
  1444. ),
  1445. 'bx_if:show_file' => array(
  1446. 'condition' => !$bImage,
  1447. 'content' => $aTmplVarsFile
  1448. ),
  1449. );
  1450. }
  1451. }
  1452. }
  1453. if($this->_bIsApi)
  1454. return $aTmplImages;
  1455. return $this->_oTemplate->parseHtmlByName('comment_attachments.html', array(
  1456. 'bx_repeat:attached' => $aTmplImages
  1457. ));
  1458. }
  1459. protected function _getTmplVarsNotes($aCmt)
  1460. {
  1461. $aTmplVars = [];
  1462. //--- Pending status related notes.
  1463. if(isset($aCmt['cmt_status_admin']) && $aCmt['cmt_status_admin'] == BX_CMT_STATUS_PENDING)
  1464. $aTmplVars[] = [
  1465. 'style_prefix' => $this->_sStylePrefix,
  1466. 'bx_if:show_note_color' => [
  1467. 'condition' => true,
  1468. 'content' => [
  1469. 'item_note_color' => 'red3'
  1470. ]
  1471. ],
  1472. 'item_note' => _t('_cmt_txt_note_approve_pending')
  1473. ];
  1474. return [
  1475. 'bx_if:show_notes' => [
  1476. 'condition' => !empty($aTmplVars),
  1477. 'content' => [
  1478. 'style_prefix' => $this->_sStylePrefix,
  1479. 'bx_repeat:notes' => $aTmplVars
  1480. ]
  1481. ],
  1482. ];
  1483. }
  1484. protected function _getTmplVarsAuthor($aCmt)
  1485. {
  1486. list($sAuthorName, $sAuthorLink, $sAuthorIcon, $sAuthorUnit) = $this->_getAuthorInfo($aCmt['cmt_author_id']);
  1487. $bAuthorIcon = !empty($sAuthorIcon);
  1488. return array(
  1489. 'author_unit' => $sAuthorUnit,
  1490. 'bx_if:show_icon' => array(
  1491. 'condition' => $bAuthorIcon,
  1492. 'content' => array(
  1493. 'author_icon' => $sAuthorIcon
  1494. )
  1495. ),
  1496. 'bx_if:show_icon_empty' => array(
  1497. 'condition' => !$bAuthorIcon,
  1498. 'content' => array()
  1499. ),
  1500. 'bx_if:show_author_link' => array(
  1501. 'condition' => !empty($sAuthorLink),
  1502. 'content' => array(
  1503. 'author_link' => $sAuthorLink,
  1504. 'author_title' => bx_html_attribute($sAuthorName),
  1505. 'author_name' => $sAuthorName
  1506. )
  1507. ),
  1508. 'bx_if:show_author_text' => array(
  1509. 'condition' => empty($sAuthorLink),
  1510. 'content' => array(
  1511. 'author_name' => $sAuthorName
  1512. )
  1513. ),
  1514. );
  1515. }
  1516. protected function _getTmplVarsText($aCmt)
  1517. {
  1518. return [
  1519. 'style_prefix' => $this->_sStylePrefix,
  1520. 'text' => $this->_prepareTextForOutput($aCmt['cmt_text'], $aCmt['cmt_id'])
  1521. ];
  1522. }
  1523. protected function _getDoComment($aParams = [], $isAllowedComment = true)
  1524. {
  1525. $bShowDoCommentAsButtonSmall = isset($aParams['show_do_comment_as_button_small']) && $aParams['show_do_comment_as_button_small'] == true;
  1526. $bShowDoCommentAsButton = !$bShowDoCommentAsButtonSmall && isset($aParams['show_do_comment_as_button']) && $aParams['show_do_comment_as_button'] == true;
  1527. $iCount = isset($aParams['count']) ? (int)$aParams['count'] : 0;
  1528. $bDisabled = !$isAllowedComment;
  1529. $sClass = '';
  1530. if($bShowDoCommentAsButton)
  1531. $sClass = 'bx-btn';
  1532. else if ($bShowDoCommentAsButtonSmall)
  1533. $sClass = 'bx-btn bx-btn-small';
  1534. if(!$isAllowedComment)
  1535. $sClass .= $bShowDoCommentAsButton || $bShowDoCommentAsButtonSmall ? ' bx-btn-disabled' : 'bx-cmts-disabled';
  1536. if($this->_bIsApi)
  1537. return [
  1538. 'is_disabled' => $bDisabled,
  1539. 'count' => $iCount,
  1540. 'link' => bx_api_get_relative_url($this->getListUrl()),
  1541. 'title' => _t($this->_getTitleDo(), $iCount)
  1542. ];
  1543. return $this->_oTemplate->parseLink($this->getListUrl(), $this->_getLabelDo($aParams), [
  1544. 'class' => $this->_sStylePrefix . '-do-comment ' . $this->_sStylePrefix . '-dc ' . $sClass,
  1545. 'title' => _t($this->_getTitleDo(), $iCount)
  1546. ]);
  1547. }
  1548. }
  1549. /** @} */