BxBaseCmts.php 73 KB

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