BxBaseCmts.php 73 KB

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