BxBaseCmts.php 75 KB

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