BxBaseCmts.php 73 KB

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