BxBaseCmts.php 75 KB

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