BxBaseCmts.php 72 KB

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