BxBaseCmts.php 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809
  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 _getFormAdd($aValues)
  1029. {
  1030. $iCmtAuthorId = isset($aValues['cmt_author_id']) ? (int)$aValues['cmt_author_id'] : $this->_getAuthorId();
  1031. $iCmtParentId = isset($aValues['cmt_parent_id']) ? (int)$aValues['cmt_parent_id'] : 0;
  1032. $oForm = $this->_getForm(BX_CMT_ACTION_POST, $iCmtParentId);
  1033. $oForm->aFormAttrs['method'] = BX_DOL_FORM_METHOD_SPECIFIC;
  1034. $oForm->aParams['csrf']['disable'] = true;
  1035. $oForm->aInputs['cmt_parent_id']['value'] = $iCmtParentId;
  1036. if(!empty($oForm->aParams['db']['submit_name'])) {
  1037. $sSubmitName = false;
  1038. if(is_array($oForm->aParams['db']['submit_name']))
  1039. foreach($oForm->aParams['db']['submit_name'] as $sVal) {
  1040. if(isset($oForm->aInputs[$sVal])) {
  1041. $sSubmitName = $sVal;
  1042. break;
  1043. }
  1044. }
  1045. else
  1046. $sSubmitName = $oForm->aParams['db']['submit_name'];
  1047. if($sSubmitName) {
  1048. if(isset($oForm->aInputs[$sSubmitName]))
  1049. $aValues[$sSubmitName] = $oForm->aInputs[$sSubmitName]['value'];
  1050. else if(isset($oForm->aInputs['cmt_controls'])) {
  1051. foreach($oForm->aInputs['cmt_controls'] as $mKey => $mValue)
  1052. if(is_numeric($mKey) && is_array($mValue) && isset($mValue['name']) && $mValue['name'] == $sSubmitName) {
  1053. $aValues[$sSubmitName] = $mValue['value'];
  1054. break;
  1055. }
  1056. }
  1057. }
  1058. }
  1059. $oForm->initChecker(array(), $aValues);
  1060. if(!$oForm->isSubmittedAndValid())
  1061. return array('code' => 1, 'message' => '_sys_txt_error_occured');
  1062. $iLevel = 0;
  1063. $iCmtVisualParentId = 0;
  1064. if((int)$iCmtParentId > 0) {
  1065. $aParent = $this->getCommentRow($iCmtParentId);
  1066. $iLevel = (int)$aParent['cmt_level'] + 1;
  1067. $iCmtVisualParentId = $iLevel > $this->getMaxLevel() ? $aParent['cmt_vparent_id'] : $iCmtParentId;
  1068. }
  1069. $iCmtId = (int)$oForm->insert(array('cmt_vparent_id' => $iCmtVisualParentId, 'cmt_object_id' => $this->_iId, 'cmt_author_id' => $iCmtAuthorId, 'cmt_level' => $iLevel, 'cmt_time' => time()));
  1070. if(!$iCmtId) {
  1071. if(!$oForm->isValid())
  1072. return array('code' => 1, 'message' => '_sys_txt_error_occured');
  1073. else
  1074. return array('code' => 2, 'message' => '_cmt_err_cannot_perform_action');
  1075. }
  1076. $iCmtUniqId = $this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId, [
  1077. 'author_id' => $iCmtAuthorId,
  1078. 'status_admin' => $this->getStatusAdmin()
  1079. ]);
  1080. if($iCmtParentId) {
  1081. $this->_oQuery->updateRepliesCount($iCmtParentId, 1);
  1082. if(!BxDolModuleQuery::getInstance()->isEnabledByName('bx_notifications'))
  1083. $this->_sendNotificationEmail($iCmtId, $iCmtParentId);
  1084. }
  1085. $this->_triggerComment();
  1086. if($this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  1087. $oMetatags->metaAdd($iCmtUniqId, $aValues['cmt_text']);
  1088. $mixedResult = $this->onPostAfter($iCmtId);
  1089. if($mixedResult === false)
  1090. return array('code' => 2, 'message' => '_cmt_err_cannot_perform_action');
  1091. return $mixedResult;
  1092. }
  1093. protected function _getFormPost($iCmtParentId = 0, $aDp = [])
  1094. {
  1095. $bCmtParentId = !empty($iCmtParentId);
  1096. if(!$bCmtParentId && !$this->isPostAllowed()) {
  1097. $sMsg = '';
  1098. if(!isLogged()) {
  1099. $oPermalink = BxDolPermalinks::getInstance();
  1100. $sMsg = _t('_cmt_msg_login_required', $oPermalink->permalink('page.php?i=login'), $oPermalink->permalink('page.php?i=create-account'));
  1101. }
  1102. else
  1103. $sMsg = $this->msgErrPostAllowed();
  1104. return bx_is_api() ? bx_api_get_msg($sMsg) : ['msg' => $sMsg];
  1105. }
  1106. if($bCmtParentId && !$this->isReplyAllowed($iCmtParentId))
  1107. return bx_is_api() ? bx_api_get_msg($sMsg) : ['msg' => $this->msgErrReplyAllowed()];
  1108. $bDynamic = isset($aDp['dynamic_mode']) && (bool)$aDp['dynamic_mode'];
  1109. $bQuote = isset($aDp['quote']) && (bool)$aDp['quote'];
  1110. $oForm = $this->_getForm(BX_CMT_ACTION_POST, $iCmtParentId, $aDp);
  1111. $oForm->aInputs['cmt_parent_id']['value'] = $iCmtParentId;
  1112. if($bQuote) {
  1113. $aCmtParent = $this->getCommentRow((int)$iCmtParentId);
  1114. if(!empty($aCmtParent['cmt_text']))
  1115. $oForm->aInputs['cmt_text']['value'] = $this->_oTemplate->parseHtmlByName('comment_quote.html', [
  1116. 'content' => $aCmtParent['cmt_text']
  1117. ]);
  1118. }
  1119. $oForm->initChecker();
  1120. if($oForm->isSubmittedAndValid()) {
  1121. $iCmtAuthorId = $this->_getAuthorId();
  1122. $iCmtParentId = (int)$oForm->getCleanValue('cmt_parent_id');
  1123. //--- Process Text ---//
  1124. $sCmtText = $oForm->getCleanValue('cmt_text');
  1125. $bCmtText = !empty($sCmtText);
  1126. //--- Process Media ---//
  1127. $aImageIds = $oForm->getCleanValue('cmt_image');
  1128. $bImageIds = !empty($aImageIds) && is_array($aImageIds);
  1129. if(!$bCmtText && !$bImageIds) {
  1130. $oForm->aInputs['cmt_text']['error'] = _t('_Please enter characters');
  1131. $oForm->setValid(false);
  1132. return bx_is_api() ? ['form' => $oForm, 'res' => 0] : ['form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id];
  1133. }
  1134. $aParent = [];
  1135. if($iCmtParentId > 0) {
  1136. $aParent = $this->getCommentRow($iCmtParentId);
  1137. if(empty($aParent) || !is_array($aParent)) {
  1138. $iCmtParentId = 0;
  1139. $oForm->setSubmittedValue('cmt_parent_id', $iCmtParentId, $oForm->aFormAttrs['method']);
  1140. }
  1141. }
  1142. $iLevel = 0;
  1143. $iCmtVisualParentId = 0;
  1144. if($iCmtParentId > 0) {
  1145. $iLevel = (int)$aParent['cmt_level'] + 1;
  1146. $iCmtVisualParentId = $iLevel > $this->getMaxLevel() ? $aParent['cmt_vparent_id'] : $iCmtParentId;
  1147. }
  1148. $iCmtId = (int)$oForm->insert(['cmt_vparent_id' => $iCmtVisualParentId, 'cmt_object_id' => $this->_iId, 'cmt_author_id' => $iCmtAuthorId, 'cmt_level' => $iLevel, 'cmt_time' => time()]);
  1149. if($iCmtId != 0) {
  1150. $iCmtUniqId = $this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId, [
  1151. 'author_id' => $iCmtAuthorId,
  1152. 'status_admin' => $this->getStatusAdmin()
  1153. ]);
  1154. if($this->isAttachImageEnabled())
  1155. $oForm->processImages($this, 'cmt_image', $iCmtUniqId, $iCmtId, $iCmtAuthorId, true);
  1156. if($iCmtParentId > 0) {
  1157. $this->_oQuery->updateRepliesCount($iCmtParentId, 1);
  1158. if(!BxDolModuleQuery::getInstance()->isEnabledByName('bx_notifications'))
  1159. $this->_sendNotificationEmail($iCmtId, $iCmtParentId);
  1160. }
  1161. $this->_triggerComment();
  1162. if($iCmtParentId > 0)
  1163. $this->isReplyAllowed($iCmtParentId, true);
  1164. else
  1165. $this->isPostAllowed(true);
  1166. if($this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  1167. $oMetatags->metaAdd($iCmtUniqId, $sCmtText);
  1168. if(($mixedResult = $this->onPostAfter($iCmtId, $aDp)) !== false){
  1169. if (bx_is_api()){
  1170. $this->_unsetFormObject(BX_CMT_ACTION_POST);
  1171. return ['form' => $this->_getForm(BX_CMT_ACTION_POST, $iCmtParentId), 'res' => $iCmtId];
  1172. }
  1173. else{
  1174. return $mixedResult;
  1175. }
  1176. }
  1177. }
  1178. return bx_is_api() ? bx_api_get_msg('_cmt_err_cannot_perform_action') : ['msg' => _t('_cmt_err_cannot_perform_action')];
  1179. }
  1180. return bx_is_api() ? ['form' => $oForm, 'res' => 0] : ['form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id];
  1181. }
  1182. protected function _getFormEdit($iCmtId, $aDp = [])
  1183. {
  1184. $bDynamic = isset($aDp['dynamic_mode']) && (bool)$aDp['dynamic_mode'];
  1185. $aCmt = $this->getCommentSimple($iCmtId);
  1186. if(!$aCmt)
  1187. return bx_is_api() ? bx_api_get_msg(_t('_No such comment')) : array('msg' => _t('_No such comment'));
  1188. $iCmtAuthorId = $this->_getAuthorId();
  1189. if(!$this->isEditAllowed($aCmt)){
  1190. $sMsg = $aCmt['cmt_author_id'] == $iCmtAuthorId ? strip_tags($this->msgErrEditAllowed()) : _t('_Access denied');
  1191. return bx_is_api() ? bx_api_get_msg($sMsg) : array('msg' => $sMsg);
  1192. }
  1193. $oForm = $this->_getForm(BX_CMT_ACTION_EDIT, $aCmt['cmt_id'], $aDp);
  1194. $oForm->initChecker($aCmt);
  1195. if($oForm->isSubmittedAndValid()) {
  1196. $sCmtText = $oForm->getCleanValue('cmt_text');
  1197. if($oForm->update($iCmtId) !== false) {
  1198. $iCmtUniqId = $this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId, [
  1199. 'author_id' => (int)$aCmt['cmt_author_id']
  1200. ]);
  1201. $sStatusAdmin = $this->getStatusAdmin();
  1202. if($sStatusAdmin !== BX_CMT_STATUS_ACTIVE)
  1203. $this->_oQuery->updateUniqId(['status_admin' => $sStatusAdmin], ['id' => $iCmtUniqId]);
  1204. if($this->isAttachImageEnabled())
  1205. $oForm->processImages($this, 'cmt_image', $iCmtUniqId, $iCmtId, $iCmtAuthorId, false);
  1206. $this->_triggerComment();
  1207. $this->isEditAllowed($aCmt, true);
  1208. if($this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  1209. $oMetatags->metaAdd($iCmtUniqId, $sCmtText);
  1210. if(($mixedResult = $this->onEditAfter($iCmtId, $aDp)) !== false){
  1211. if (bx_is_api()){
  1212. $this->_unsetFormObject(BX_CMT_ACTION_POST);
  1213. return ['form' => $this->_getForm(BX_CMT_ACTION_POST, $aCmt['cmt_parent_id']), 'res' => $iCmtId];
  1214. }
  1215. else{
  1216. return $mixedResult;
  1217. }
  1218. }
  1219. }
  1220. return bx_is_api() ? bx_api_get_msg(_t('_cmt_err_cannot_perform_action')) : array('msg' => _t('_cmt_err_cannot_perform_action'));
  1221. }
  1222. return bx_is_api() ? ['form' => $oForm, 'res' => 0] : array('form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id);
  1223. }
  1224. protected function _getForm($sAction, $iId, $aDp = [])
  1225. {
  1226. $oForm = $this->_getFormObject($sAction);
  1227. $oForm->setId(sprintf($oForm->getAttributeMask('id'), $sAction, $this->_sSystem, $this->_iId, $iId));
  1228. $oForm->setName(sprintf($oForm->getAttributeMask('name'), $sAction, $this->_sSystem, $this->_iId, $iId));
  1229. $oForm->aParams['db']['table'] = $this->_aSystem['table'];
  1230. $oForm->aInputs['sys']['value'] = $this->_sSystem;
  1231. $oForm->aInputs['id']['value'] = $this->_iId;
  1232. $oForm->aInputs['action']['value'] = 'Submit' . ucfirst($sAction) . 'Form';
  1233. if($this->_aParams)
  1234. $oForm->aInputs = ['params' => [
  1235. 'name' => 'params',
  1236. 'type' => 'hidden',
  1237. 'value' => json_encode($this->_aParams)
  1238. ]] + $oForm->aInputs;
  1239. if(!$this->isAttachImageEnabled())
  1240. unset($oForm->aInputs['cmt_image']);
  1241. if(isset($oForm->aInputs['cmt_text'])) {
  1242. $oForm->aInputs['cmt_text']['html'] = $this->_aSystem['html'];
  1243. $oForm->aInputs['cmt_text']['db']['pass'] = $this->isHtml() ? 'XssHtml' : 'XssMultiline';
  1244. if(isset($oForm->aInputs['cmt_text']['checker']['func']) && strtolower($oForm->aInputs['cmt_text']['checker']['func']) == 'length') {
  1245. $iCmtTextMin = (int)$this->_aSystem['chars_post_min'];
  1246. $iCmtTextMax = (int)$this->_aSystem['chars_post_max'];
  1247. $oForm->aInputs['cmt_text']['checker']['params'] = array($iCmtTextMin, $iCmtTextMax);
  1248. $oForm->aInputs['cmt_text']['checker']['error'] = _t('_Please enter n1-n2 characters', $iCmtTextMin, $iCmtTextMax);
  1249. }
  1250. }
  1251. if($sAction == BX_CMT_ACTION_EDIT && isset($oForm->aInputs['cmt_controls']))
  1252. foreach($oForm->aInputs['cmt_controls'] as $mixedKey => $mixedValue) {
  1253. if(!is_numeric($mixedKey) || empty($mixedValue['name']) || $mixedValue['name'] != 'cmt_cancel')
  1254. continue;
  1255. if(!isset($oForm->aInputs['cmt_controls'][$mixedKey]['attrs']))
  1256. $oForm->aInputs['cmt_controls'][$mixedKey]['attrs'] = array();
  1257. $oForm->aInputs['cmt_controls'][$mixedKey]['attrs']['onclick'] = $this->_sJsObjName . '.cmtEdit(this, ' . $iId . ', false)';
  1258. }
  1259. return $oForm;
  1260. }
  1261. protected function _getContent($aCmt, $aBp = [], $aDp = [])
  1262. {
  1263. $sAttachments = $this->_getAttachments($aCmt);
  1264. $sCounters = $this->_getCountersBox($aCmt, $aBp, $aDp);
  1265. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameItemContent, array_merge(array(
  1266. 'style_prefix' => $this->_sStylePrefix,
  1267. 'js_object' => $this->_sJsObjName,
  1268. 'bx_if:show_attached' => [
  1269. 'condition' => !empty($sAttachments),
  1270. 'content' => [
  1271. 'style_prefix' => $this->_sStylePrefix,
  1272. 'attached' => $sAttachments
  1273. ]
  1274. ],
  1275. 'bx_if:show_counters' => [
  1276. 'condition' => !empty($sCounters),
  1277. 'content' => [
  1278. 'style_prefix' => $this->_sStylePrefix,
  1279. 'counters' => $sCounters,
  1280. ]
  1281. ]
  1282. ), $this->_getTmplVarsText($aCmt)));
  1283. }
  1284. protected function _getMoreLink($sCmts, $aBp = array(), $aDp = array())
  1285. {
  1286. $iStart = $iPerView = 0;
  1287. switch($aBp['type']) {
  1288. case BX_CMT_BROWSE_HEAD:
  1289. case BX_CMT_BROWSE_POPULAR:
  1290. case BX_CMT_BROWSE_CONNECTION:
  1291. $iPerView = $aBp['per_view'];
  1292. $iStart = $aBp['start'] + $iPerView;
  1293. if($iStart >= $aBp['count'])
  1294. return $sCmts;
  1295. break;
  1296. case BX_CMT_BROWSE_TAIL:
  1297. $iPerView = $aBp['per_view'];
  1298. $iStart = $aBp['start'] - $iPerView;
  1299. if($iStart < 0) {
  1300. $iPerView += $iStart;
  1301. $iStart = 0;
  1302. }
  1303. if($iStart == 0 && $iPerView == 0)
  1304. return $sCmts;
  1305. break;
  1306. }
  1307. $sParentKey = isset($aBp['vparent_id']) ? 'vparent_id' : 'parent_id';
  1308. $bRoot = (int)$aBp[$sParentKey] <= 0;
  1309. $sMore = $this->_oTemplate->parseHtmlByName('comment_more.html', array(
  1310. 'js_object' => $this->_sJsObjName,
  1311. 'style_prefix' => $this->_sStylePrefix,
  1312. 'bx_if:is_root' => array(
  1313. 'condition' => $bRoot,
  1314. 'content' => array()
  1315. ),
  1316. 'parent_id' => $aBp[$sParentKey],
  1317. 'start' => $iStart,
  1318. 'per_view' => $iPerView,
  1319. 'title' => _t('_cmt_load_more_' . ($aBp[$sParentKey] == 0 ? 'comments' : 'replies') . '_' . $aBp['type'])
  1320. ));
  1321. switch($aBp['type']) {
  1322. case BX_CMT_BROWSE_HEAD:
  1323. case BX_CMT_BROWSE_POPULAR:
  1324. case BX_CMT_BROWSE_CONNECTION:
  1325. $sCmts .= $sMore;
  1326. break;
  1327. case BX_CMT_BROWSE_TAIL:
  1328. $sCmts = $sMore . $sCmts;
  1329. break;
  1330. }
  1331. return $sCmts;
  1332. }
  1333. protected function _getEmpty($aDp = array())
  1334. {
  1335. $sClass = '';
  1336. if(!empty($aDp['class']))
  1337. $sClass .= ' ' . $aDp['class'];
  1338. return $this->_oTemplate->parseHtmlByName('comment_empty.html', array(
  1339. 'style_prefix' => $this->_sStylePrefix,
  1340. 'class' => $sClass,
  1341. 'content' => MsgBox(_t('_Empty'))
  1342. ));
  1343. }
  1344. protected function _getAttachments($aCmt)
  1345. {
  1346. $aTmplImages = array();
  1347. if(!$this->isAttachImageEnabled())
  1348. return '';
  1349. $aFiles = $this->_oQuery->getFiles($this->_aSystem['system_id'], $aCmt['cmt_id']);
  1350. if(!empty($aFiles) && is_array($aFiles)) {
  1351. $oStorage = BxDolStorage::getObjectInstance($this->getStorageObjectName());
  1352. $oTranscoder = BxDolTranscoderImage::getObjectInstance($this->getTranscoderPreviewName());
  1353. foreach($aFiles as $aFile) {
  1354. $sFile = $oStorage->getFileUrlById($aFile['image_id']);
  1355. $bImage = $oTranscoder && ($oTranscoder->isMimeTypeSupported($aFile['mime_type']) || $oTranscoder->isMimeTypeSvg($aFile['mime_type']));
  1356. $sPreview = '';
  1357. $iWidth = $iHeight = 0;
  1358. if($bImage) {
  1359. if(!$this->_bIsApi && $oTranscoder)
  1360. $sPreview = $oTranscoder->getFileUrl($aFile['image_id']);
  1361. if(empty($aFile['dimensions'])) {
  1362. $aFileInfo = $oStorage->getFile($aFile['image_id']);
  1363. if(!empty($aFileInfo['dimensions']))
  1364. $aFile['dimensions'] = $aFileInfo['dimensions'];
  1365. }
  1366. if(!empty($aFile['dimensions']))
  1367. list($iWidth, $iHeight) = explode('x', $aFile['dimensions']);
  1368. }
  1369. if(!$this->_bIsApi && !$sPreview)
  1370. $sPreview = $this->_oTemplate->getIconUrl($oStorage->getIconNameByFileName($aFile['file_name']));
  1371. $aTmplVarsFile = array(
  1372. 'js_object' => $this->_sJsObjName,
  1373. 'preview' => $sPreview,
  1374. 'file' => $sFile,
  1375. 'file_id' => $aFile['id'],
  1376. 'file_name' => $aFile['file_name'],
  1377. 'file_icon' => $oStorage->getFontIconNameByFileName($aFile['file_name']),
  1378. 'file_size' => _t_format_size($aFile['size']),
  1379. 'w' => $iWidth,
  1380. 'h' => $iHeight
  1381. );
  1382. if($this->_bIsApi) {
  1383. $aTmplImages[] = array(
  1384. 'is_image' => $bImage,
  1385. 'file' => $aTmplVarsFile['file'],
  1386. 'file_id' => $aTmplVarsFile['file_id'],
  1387. 'file_name' => $aTmplVarsFile['file_name'],
  1388. 'file_icon' => $aTmplVarsFile['file_icon'],
  1389. 'file_size' => $aTmplVarsFile['file_size'],
  1390. 'width' => $aTmplVarsFile['w'],
  1391. 'height' => $aTmplVarsFile['h'],
  1392. );
  1393. }
  1394. else {
  1395. $aTmplImages[] = array(
  1396. 'style_prefix' => $this->_sStylePrefix,
  1397. 'bx_if:show_image' => array(
  1398. 'condition' => $bImage,
  1399. 'content' => $aTmplVarsFile
  1400. ),
  1401. 'bx_if:show_file' => array(
  1402. 'condition' => !$bImage,
  1403. 'content' => $aTmplVarsFile
  1404. ),
  1405. );
  1406. }
  1407. }
  1408. }
  1409. if (bx_is_api()){
  1410. return $aTmplImages;
  1411. }
  1412. return $this->_oTemplate->parseHtmlByName('comment_attachments.html', array(
  1413. 'bx_repeat:attached' => $aTmplImages
  1414. ));
  1415. }
  1416. protected function _getTmplVarsNotes($aCmt)
  1417. {
  1418. $aTmplVars = [];
  1419. //--- Pending status related notes.
  1420. if(isset($aCmt['cmt_status_admin']) && $aCmt['cmt_status_admin'] == BX_CMT_STATUS_PENDING)
  1421. $aTmplVars[] = [
  1422. 'style_prefix' => $this->_sStylePrefix,
  1423. 'bx_if:show_note_color' => [
  1424. 'condition' => true,
  1425. 'content' => [
  1426. 'item_note_color' => 'red3'
  1427. ]
  1428. ],
  1429. 'item_note' => _t('_cmt_txt_note_approve_pending')
  1430. ];
  1431. return [
  1432. 'bx_if:show_notes' => [
  1433. 'condition' => !empty($aTmplVars),
  1434. 'content' => [
  1435. 'style_prefix' => $this->_sStylePrefix,
  1436. 'bx_repeat:notes' => $aTmplVars
  1437. ]
  1438. ],
  1439. ];
  1440. }
  1441. protected function _getTmplVarsAuthor($aCmt)
  1442. {
  1443. list($sAuthorName, $sAuthorLink, $sAuthorIcon, $sAuthorUnit) = $this->_getAuthorInfo($aCmt['cmt_author_id']);
  1444. $bAuthorIcon = !empty($sAuthorIcon);
  1445. return array(
  1446. 'author_unit' => $sAuthorUnit,
  1447. 'bx_if:show_icon' => array(
  1448. 'condition' => $bAuthorIcon,
  1449. 'content' => array(
  1450. 'author_icon' => $sAuthorIcon
  1451. )
  1452. ),
  1453. 'bx_if:show_icon_empty' => array(
  1454. 'condition' => !$bAuthorIcon,
  1455. 'content' => array()
  1456. ),
  1457. 'bx_if:show_author_link' => array(
  1458. 'condition' => !empty($sAuthorLink),
  1459. 'content' => array(
  1460. 'author_link' => $sAuthorLink,
  1461. 'author_title' => bx_html_attribute($sAuthorName),
  1462. 'author_name' => $sAuthorName
  1463. )
  1464. ),
  1465. 'bx_if:show_author_text' => array(
  1466. 'condition' => empty($sAuthorLink),
  1467. 'content' => array(
  1468. 'author_name' => $sAuthorName
  1469. )
  1470. ),
  1471. );
  1472. }
  1473. protected function _getTmplVarsText($aCmt)
  1474. {
  1475. return [
  1476. 'style_prefix' => $this->_sStylePrefix,
  1477. 'text' => $this->_prepareTextForOutput($aCmt['cmt_text'], $aCmt['cmt_id'])
  1478. ];
  1479. }
  1480. protected function _getDoComment($aParams = [], $isAllowedComment = true)
  1481. {
  1482. $bShowDoCommentAsButtonSmall = isset($aParams['show_do_comment_as_button_small']) && $aParams['show_do_comment_as_button_small'] == true;
  1483. $bShowDoCommentAsButton = !$bShowDoCommentAsButtonSmall && isset($aParams['show_do_comment_as_button']) && $aParams['show_do_comment_as_button'] == true;
  1484. $iCount = isset($aParams['count']) ? (int)$aParams['count'] : 0;
  1485. $bDisabled = !$isAllowedComment;
  1486. $sClass = '';
  1487. if($bShowDoCommentAsButton)
  1488. $sClass = 'bx-btn';
  1489. else if ($bShowDoCommentAsButtonSmall)
  1490. $sClass = 'bx-btn bx-btn-small';
  1491. if(!$isAllowedComment)
  1492. $sClass .= $bShowDoCommentAsButton || $bShowDoCommentAsButtonSmall ? ' bx-btn-disabled' : 'bx-cmts-disabled';
  1493. if($this->_bIsApi)
  1494. return [
  1495. 'is_disabled' => $bDisabled,
  1496. 'count' => $iCount,
  1497. 'link' => bx_api_get_relative_url($this->getListUrl()),
  1498. 'title' => _t($this->_getTitleDo(), $iCount)
  1499. ];
  1500. return $this->_oTemplate->parseLink($this->getListUrl(), $this->_getLabelDo($aParams), [
  1501. 'class' => $this->_sStylePrefix . '-do-comment ' . $this->_sStylePrefix . '-dc ' . $sClass,
  1502. 'title' => _t($this->_getTitleDo(), $iCount)
  1503. ]);
  1504. }
  1505. }
  1506. /** @} */