BxBaseCmts.php 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  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 $_aElementDefaults;
  25. protected $_aAclId2Name;
  26. function __construct( $sSystem, $iId, $iInit = true, $oTemplate = false)
  27. {
  28. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  29. if (empty($sSystem))
  30. return;
  31. $this->_sTmplNameItem = 'comment.html';
  32. $this->_sTmplNameItemContent = 'comment_content.html';
  33. $this->_sJsObjClass = 'BxDolCmts';
  34. $this->_sJsObjName = 'oCmts' . bx_gen_method_name($sSystem, array('_' , '-')) . $iId;
  35. $this->_sStylePrefix = isset($this->_aSystem['root_style_prefix']) ? $this->_aSystem['root_style_prefix'] : 'cmt';
  36. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
  37. $this->_aHtmlIds = array(
  38. 'main' => 'bx-cmt-' . $sHtmlId,
  39. 'counter' => 'bx-cmt-counter-' . $sHtmlId
  40. );
  41. $this->_aElementDefaults = array(
  42. 'show_do_comment_as_button' => false,
  43. 'show_do_comment_as_button_small' => false,
  44. 'show_do_comment_image' => false,
  45. 'show_do_comment_icon' => true,
  46. 'show_do_comment_label' => false,
  47. 'show_counter' => true,
  48. 'show_counter_empty' => false
  49. );
  50. $this->_aAclId2Name = array();
  51. $aAclLevels = BxDolAcl::getInstance()->getMemberships(false, false, false);
  52. foreach($aAclLevels as $iAclId => $sAclName)
  53. $this->_aAclId2Name[$iAclId] = str_replace('_', '-', str_replace('_adm_prm_txt_level_', '', $sAclName));
  54. if(empty(self::$_sTmplContentElementBlock))
  55. self::$_sTmplContentElementBlock = $this->_oTemplate->getHtml('comment_element_block.html');
  56. if(empty(self::$_sTmplContentElementInline))
  57. self::$_sTmplContentElementInline = $this->_oTemplate->getHtml('comment_element_inline.html');
  58. if(empty(self::$_sTmplContentDoCommentLabel))
  59. self::$_sTmplContentDoCommentLabel = $this->_oTemplate->getHtml('comment_do_comment_label.html');
  60. if(empty(self::$_sTmplContentCounter))
  61. self::$_sTmplContentCounter = $this->_oTemplate->getHtml('comment_counter.html');
  62. $this->_oTemplate->addJsTranslation('_sys_txt_cmt_loading');
  63. }
  64. /**
  65. * Add comments CSS/JS
  66. */
  67. public function addCssJs ()
  68. {
  69. $oForm = BxDolForm::getObjectInstance($this->_sFormObject, $this->_sFormDisplayPost);
  70. $oForm->addCssJs();
  71. }
  72. public function getJsObjectName()
  73. {
  74. return $this->_sJsObjName;
  75. }
  76. /**
  77. * Get initialization section of comments box
  78. *
  79. * @return string
  80. */
  81. public function getJsScript($aBp = array(), $aDp = array())
  82. {
  83. $bMinPostForm = isset($aDp['min_post_form']) ? $aDp['min_post_form'] : $this->_bMinPostForm;
  84. $aParams = array(
  85. 'sObjName' => $this->_sJsObjName,
  86. 'sRootUrl' => BX_DOL_URL_ROOT,
  87. 'sSystem' => $this->getSystemName(),
  88. 'iAuthorId' => $this->_getAuthorId(),
  89. 'iObjId' => $this->getId(),
  90. 'sBaseUrl' => $this->getBaseUrl(),
  91. 'sPostFormPosition' => $this->_aSystem['post_form_position'],
  92. 'sBrowseType' => $this->_sBrowseType,
  93. 'sDisplayType' => $this->_sDisplayType,
  94. 'iDisplayStructure' => isset($aDp['structure']) && !empty($aDp['structure']) ? 1 : 0,
  95. 'iMinPostForm' => $bMinPostForm ? 1 : 0,
  96. 'sStylePrefix' => $this->_sStylePrefix,
  97. );
  98. $this->addCssJs();
  99. return $this->_oTemplate->_wrapInTagJsCode("if(window['" . $this->_sJsObjName . "'] == undefined) var " . $this->_sJsObjName . " = new " . $this->_sJsObjClass . "(" . json_encode($aParams) . "); " . $this->_sJsObjName . ".cmtInit();");
  100. }
  101. /**
  102. * get full comments block with initializations
  103. */
  104. function getCommentsBlock($aBp = array(), $aDp = array())
  105. {
  106. $mixedResult = $this->isViewAllowed();
  107. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  108. return $mixedResult;
  109. $this->_getParams($aBp, $aDp);
  110. //add live update
  111. $this->actionResumeLiveUpdate();
  112. $sServiceCall = BxDolService::getSerializedService('system', 'get_live_update', array($this->_sSystem, $this->_iId, $this->_getAuthorId(), '{count}'), 'TemplCmtsServices');
  113. BxDolLiveUpdates::getInstance()->add($this->_sSystem . '_live_updates_cmts_' . $this->_iId, 1, $sServiceCall);
  114. //add live update
  115. $sCaption = _t($this->_aT['block_comments_title'], $this->getCommentsCountAll());
  116. $sContent = $this->_oTemplate->parseHtmlByName('comments_block.html', array(
  117. 'system' => $this->_sSystem,
  118. 'list_anchor' => $this->getListAnchor(),
  119. 'id' => $this->getId(),
  120. 'comments' => $this->getComments($aBp, $aDp),
  121. 'post_form_top' => $this->getFormBoxPost($aBp, array_merge($aDp, array('type' => $this->_sDisplayType, 'position' => BX_CMT_PFP_TOP))),
  122. 'post_form_bottom' => $this->getFormBoxPost($aBp, array_merge($aDp, array('type' => $this->_sDisplayType, 'position' => BX_CMT_PFP_BOTTOM))),
  123. 'view_image_popup' => $this->_getViewImagePopup(),
  124. 'script' => $this->getJsScript($aBp, $aDp)
  125. ));
  126. return $aDp['in_designbox'] ? DesignBoxContent($sCaption, $sContent, BX_DB_DEF, $this->_getControlsBox()) : array(
  127. 'title' => $sCaption,
  128. 'content' => $sContent,
  129. 'menu' => $this->_getControlsBox(),
  130. );
  131. }
  132. /**
  133. * get comments list for specified parent comment
  134. *
  135. * @param array $aBp - browse params array
  136. * @param array $aDp - display params array
  137. *
  138. */
  139. function getComments($aBp = array(), $aDp = array())
  140. {
  141. $this->_prepareParams($aBp, $aDp);
  142. $aCmts = $this->getCommentsArray($aBp['vparent_id'], $aBp['filter'], $aBp['order'], $aBp['start'], $aBp['per_view']);
  143. if(empty($aCmts) || !is_array($aCmts)) {
  144. if((int)$aBp['parent_id'] == 0 && !isLogged()) {
  145. $oPermalink = BxDolPermalinks::getInstance();
  146. return MsgBox(_t('_cmt_msg_login_required', $oPermalink->permalink('page.php?i=login'), $oPermalink->permalink('page.php?i=create-account')));
  147. }
  148. return isset($aDp['show_empty']) && $aDp['show_empty'] === true ? $this->_getEmpty($aDp) : '';
  149. }
  150. $sCmts = '';
  151. foreach($aCmts as $k => $aCmt)
  152. $sCmts .= $this->getComment($aCmt, $aBp, $aDp);
  153. $sCmts = $this->_getMoreLink($sCmts, $aBp, $aDp);
  154. return $sCmts;
  155. }
  156. public function getCommentsByStructure($aBp = array(), $aDp = array())
  157. {
  158. if(empty($aDp['structure']))
  159. return isset($aDp['show_empty']) && $aDp['show_empty'] === true ? $this->_getEmpty($aDp) : '';
  160. $aBp['count'] = count($aDp['structure']);
  161. $this->_prepareParams($aBp, $aDp);
  162. $aDp['structure'] = array_slice($aDp['structure'], $aBp['start'], $aBp['per_view'], true);
  163. $sCmts = '';
  164. foreach($aDp['structure'] as $iCmtId => $aCmt)
  165. $sCmts .= $this->getComment($iCmtId, $aBp, $aDp);
  166. $sCmts = $this->_getMoreLink($sCmts, $aBp, $aDp);
  167. return $sCmts;
  168. }
  169. /**
  170. * get comment view block with initializations
  171. */
  172. function getCommentBlock($iCmtId = 0, $aBp = array(), $aDp = array())
  173. {
  174. $mixedResult = $this->isViewAllowed();
  175. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  176. return $mixedResult;
  177. $aBp = array_merge(array('type' => $this->_sBrowseType), $aBp);
  178. $aDp = array_merge(array('type' => BX_CMT_DISPLAY_THREADED), $aDp);
  179. //--- Beg: Using pregenerated structure
  180. $mixedStructure = $this->getCommentStructure((int)$iCmtId, $aBp, $aDp);
  181. if($mixedStructure !== false)
  182. $aDp['structure'] = $mixedStructure;
  183. //--- End: Using pregenerated structure
  184. $sComment = $this->getComment($iCmtId, $aBp, $aDp);
  185. if (!$sComment)
  186. return '';
  187. return $this->_oTemplate->parseHtmlByName('comment_block.html', array(
  188. 'system' => $this->_sSystem,
  189. 'id' => $this->getId(),
  190. 'comment' => $sComment,
  191. 'view_image_popup' => $this->_getViewImagePopup(),
  192. 'script' => $this->getJsScript($aBp, $aDp)
  193. ));
  194. }
  195. /**
  196. * get one just posted comment
  197. *
  198. * @param int $iCmtId - comment id
  199. * @return string
  200. */
  201. function getComment($mixedCmt, $aBp = array(), $aDp = array())
  202. {
  203. $iUserId = $this->_getAuthorId();
  204. $aCmt = !is_array($mixedCmt) ? $this->getCommentRow((int)$mixedCmt) : $mixedCmt;
  205. if (!$aCmt)
  206. return '';
  207. list($sAuthorName, $sAuthorLink, $sAuthorIcon) = $this->_getAuthorInfo($aCmt['cmt_author_id']);
  208. $sClass = $sClassCnt = '';
  209. if(isset($aCmt['vote_rate']) && (float)$aCmt['vote_rate'] < $this->_aSystem['viewing_threshold']) {
  210. $this->_oTemplate->pareseHtmlByName('comment_hidden.html', array(
  211. 'js_object' => $this->_sJsObjName,
  212. 'id' => $aCmt['cmt_id'],
  213. 'title' => bx_process_output(_t('_hidden_comment', $sAuthorName)),
  214. 'bx_if:show_replies' => array(
  215. 'condition' => $aCmt['cmt_replies'] > 0,
  216. 'content' => array(
  217. 'replies' => _t('_Show N replies', $aCmt['cmt_replies'])
  218. )
  219. )
  220. ));
  221. $sClass = ' ' . $this->_sStylePrefix . '-hidden';
  222. }
  223. if($aCmt['cmt_author_id'] == $iUserId)
  224. $sClass .= ' ' . $this->_sStylePrefix . '-mine';
  225. $aAuthorAcl = BxDolAcl::getInstance()->getMemberMembershipInfo($aCmt['cmt_author_id']);
  226. if(!empty($aAuthorAcl) && isset($this->_aAclId2Name[$aAuthorAcl['id']]))
  227. $sClass .= ' ' . $this->_sStylePrefix . '-aml-' . $this->_aAclId2Name[$aAuthorAcl['id']];
  228. if(!empty($aDp['blink']) && in_array($aCmt['cmt_id'], $aDp['blink']))
  229. $sClass .= ' ' . $this->_sStylePrefix . '-blink';
  230. if(!empty($aDp['class_comment']))
  231. $sClass .= ' ' . $aDp['class_comment'];
  232. if(!empty($aDp['class_comment_content']))
  233. $sClassCnt .= ' ' . $aDp['class_comment_content'];
  234. $sActions = $this->_getActionsBox($aCmt, $aDp);
  235. $aTmplReplyTo = array();
  236. if((int)$aCmt['cmt_parent_id'] != 0) {
  237. $aParent = $this->getCommentRow($aCmt['cmt_parent_id']);
  238. if(!empty($aParent) && is_array($aParent)) {
  239. $oProfile = $this->_getAuthorObject($aParent['cmt_author_id']);
  240. $sParAuthorName = $oProfile->getDisplayName();
  241. $sParAuthorUnit = $oProfile->getUnit(0, array('template' => array('name' => 'unit_wo_info_links', 'size' => 'icon')));
  242. $aTmplReplyTo = array(
  243. 'style_prefix' => $this->_sStylePrefix,
  244. 'par_cmt_link' => $this->getItemUrl($aCmt['cmt_parent_id']),
  245. 'par_cmt_title' => bx_html_attribute(_t('_in_reply_to_x', $sParAuthorName)),
  246. 'par_cmt_author' => $sParAuthorName,
  247. 'par_cmt_author_unit' => $sParAuthorUnit
  248. );
  249. }
  250. }
  251. $sReplies = '';
  252. if(!empty($aDp)) {
  253. $aDp['show_empty'] = false;
  254. if(!empty($aDp['structure'][$aCmt['cmt_id']]) && is_array($aDp['structure'][$aCmt['cmt_id']])) {
  255. if(!empty($aDp['structure'][$aCmt['cmt_id']]['items'])) {
  256. $aDp['structure'] = $aDp['structure'][$aCmt['cmt_id']]['items'];
  257. $sReplies = $this->getCommentsByStructure(array('parent_id' => $aCmt['cmt_id'], 'type' => $aBp['type']), $aDp);
  258. }
  259. }
  260. else if((int)$aCmt['cmt_replies'] > 0 && $aDp['type'] == BX_CMT_DISPLAY_THREADED)
  261. $sReplies = $this->getComments(array('parent_id' => $aCmt['cmt_id'], 'vparent_id' => $aCmt['cmt_id'], 'type' => $aBp['type']), $aDp);
  262. }
  263. $aTmplVarsMeta = array();
  264. if(!empty($this->_sMenuObjMeta)) {
  265. $oMenuMeta = BxDolMenu::getObjectInstance($this->_sMenuObjMeta, $this->_oTemplate);
  266. if($oMenuMeta) {
  267. $oMenuMeta->setCmtsData($this, $aCmt['cmt_id']);
  268. $aTmplVarsMeta = array(
  269. 'style_prefix' => $this->_sStylePrefix,
  270. 'meta' => $oMenuMeta->getCode()
  271. );
  272. }
  273. }
  274. $sContent = $this->_getContent($aCmt);
  275. $sResult = $this->_oTemplate->parseHtmlByName($this->_sTmplNameItem, array_merge(array(
  276. 'system' => $this->_sSystem,
  277. 'style_prefix' => $this->_sStylePrefix,
  278. 'js_object' => $this->_sJsObjName,
  279. 'id' => $aCmt['cmt_id'],
  280. 'anchor' => $this->getItemAnchor($aCmt['cmt_id']),
  281. 'class' => $sClass,
  282. 'class_cnt' => $sClassCnt,
  283. 'bx_if:show_reply_to' => array(
  284. 'condition' => !empty($aTmplReplyTo),
  285. 'content' => $aTmplReplyTo
  286. ),
  287. 'bx_if:meta' => array(
  288. 'condition' => !empty($aTmplVarsMeta),
  289. 'content' => $aTmplVarsMeta
  290. ),
  291. 'bx_if:show_pinned' => array(
  292. 'condition' => (int)$aCmt['cmt_pinned'] > 0,
  293. 'content' => array(
  294. 'style_prefix' => $this->_sStylePrefix,
  295. )
  296. ),
  297. 'content' => $sContent,
  298. 'actions' => $sActions,
  299. 'replies' => $sReplies,
  300. ), $this->_getTmplVarsAuthor($aCmt)));
  301. bx_alert('system', 'view_comment', $aCmt['cmt_id'], 0, array('comment' => $aCmt, 'system' => $this->_sSystem, 'override_result' => &$sResult));
  302. return $sResult;
  303. }
  304. public function getCommentStructure($iCmtId, $aBp = array(), $aDp = array())
  305. {
  306. $aRoot = $this->getCommentRow((int)$iCmtId);
  307. $aBps = $aBp;
  308. if(!empty($aRoot))
  309. $aBps['parent_id'] = $aRoot['cmt_id'];
  310. $this->_prepareStructureBp($aDp['type'], $aBps);
  311. $iLevel = 0;
  312. $aStructure = array();
  313. $this->_getStructure($aRoot, $aBps, $iLevel, $aStructure);
  314. return !empty($aStructure) && is_array($aStructure) ? $aStructure : false;
  315. }
  316. function getCommentSearch($iCmtId, &$sAddon)
  317. {
  318. $aBp = array();
  319. $aDp = array(
  320. 'type' => BX_CMT_DISPLAY_FLAT,
  321. 'view_only' => true
  322. );
  323. if(empty($sAddon))
  324. $sAddon = $this->getJsScript($aBp, $aDp);
  325. return $this->_oTemplate->parseHtmlByName('comment_search.html', array(
  326. 'comment' => $this->getComment($iCmtId, $aBp, $aDp),
  327. 'view_image_popup' => $this->_getViewImagePopup(),
  328. ));
  329. }
  330. /**
  331. * get one comment for "Live Search"
  332. *
  333. * @param int $iCmtId - comment id
  334. * @return string
  335. */
  336. function getCommentLiveSearch($mixedCmt, $aParams = array())
  337. {
  338. $aCmt = !is_array($mixedCmt) ? $this->getCommentRow((int)$mixedCmt) : $mixedCmt;
  339. list($sAuthorName, $sAuthorLink, $sAuthorIcon) = $this->_getAuthorInfo($aCmt['cmt_author_id']);
  340. $bAuthorIcon = !empty($sAuthorIcon);
  341. $sViewLink = $this->getViewUrl($aCmt['cmt_id']);
  342. if(empty($sViewLink))
  343. $sViewLink = '#';
  344. return $this->_oTemplate->parseHtmlByName('comment_live_search.html', array(
  345. 'bx_if:show_icon' => array(
  346. 'condition' => $bAuthorIcon,
  347. 'content' => array(
  348. 'author_icon' => $sAuthorIcon,
  349. 'view_link' => $sViewLink
  350. )
  351. ),
  352. 'bx_if:show_icon_empty' => array(
  353. 'condition' => !$bAuthorIcon,
  354. 'content' => array()
  355. ),
  356. 'view_link' => $sViewLink,
  357. 'text' => BxTemplFunctions::getInstance()->getStringWithLimitedLength(strip_tags($aCmt['cmt_text']), $this->_sSnippetLenthLiveSearch),
  358. 'sample' => isset($aParams['txt_sample_single']) ? _t($aParams['txt_sample_single']) : ''
  359. ));
  360. }
  361. function getFormBoxPost($aBp = array(), $aDp = array())
  362. {
  363. return $this->_getFormBox(BX_CMT_ACTION_POST, $aBp, $aDp);
  364. }
  365. function getFormBoxEdit($aBp = array(), $aDp = array())
  366. {
  367. return $this->_getFormBox(BX_CMT_ACTION_EDIT, $aBp, $aDp);
  368. }
  369. function getFormPost($iCmtParentId = 0, $aDp = array())
  370. {
  371. return $this->_getFormPost($iCmtParentId, $aDp);
  372. }
  373. function getFormEdit($iCmtId, $aDp = array())
  374. {
  375. return $this->_getFormEdit($iCmtId, $aDp);
  376. }
  377. function getLiveUpdate($iCountOld = 0, $iCountNew = 0)
  378. {
  379. $iCount = (int)$iCountNew - (int)$iCountOld;
  380. if($iCount < 0)
  381. return '';
  382. $aComments = $this->_oQuery->getCommentsBy(array('type' => 'latest', 'object_id' => $this->_iId, 'author' => $this->_getAuthorId(), 'others' => 1, 'start' => '0', 'per_page' => $iCount));
  383. if(empty($aComments) || !is_array($aComments))
  384. return '';
  385. $aComment = array_shift($aComments);
  386. if(empty($aComment) || !is_array($aComment))
  387. return '';
  388. $sJsObject = $this->getJsObjectName();
  389. return $this->_oTemplate->parseHtmlByName('comments_lu_button.html', array(
  390. 'style_prefix' => $this->_sStylePrefix,
  391. 'html_id' => $this->getNotificationId(),
  392. 'onclick_show' => "javascript:" . $sJsObject . ".goToBtn(this, '" . $this->getItemAnchor($aComment['cmt_id']) . "', '" . $aComment['cmt_id'] . "');",
  393. ));
  394. }
  395. function getLiveUpdates($iCountOld = 0, $iCountNew = 0)
  396. {
  397. $bShowAll = true;
  398. $bShowActions = false;
  399. $iCount = (int)$iCountNew - (int)$iCountOld;
  400. if($iCount < 0)
  401. return '';
  402. $aComments = $this->_oQuery->getCommentsBy(array('type' => 'latest', 'object_id' => $this->_iId, 'author' => $this->_getAuthorId(), 'others' => 1, 'start' => '0', 'per_page' => $iCount));
  403. if(empty($aComments) || !is_array($aComments))
  404. return '';
  405. $sJsObject = $this->getJsObjectName();
  406. $iUserId = $this->_getAuthorId();
  407. $bModerator = $this->isModerator();
  408. $aComments = array_reverse($aComments);
  409. $iComments = count($aComments);
  410. $aTmplVarsNotifs = array();
  411. foreach($aComments as $iIndex => $aComment) {
  412. $iCommentId = $aComment['cmt_id'];
  413. $sShowOnClick = "javascript:" . $sJsObject . ".goTo(this, '" . $this->getItemAnchor($iCommentId) . "', '" . $iCommentId . "');";
  414. $sReplyOnClick = "javascript:" . $sJsObject . ".goToAndReply(this, '" . $this->getItemAnchor($iCommentId) . "', '" . $iCommentId . "');";
  415. $iAuthorId = (int)$aComment['cmt_author_id'];
  416. if($iAuthorId < 0) {
  417. if(abs($iAuthorId) == $iUserId)
  418. continue;
  419. else if($bModerator)
  420. $iAuthorId *= -1;
  421. }
  422. $oAuthor = $this->_getAuthorObject($iAuthorId);
  423. $sAuthorName = $oAuthor->getDisplayName();
  424. $aTmplVarsNotifs[] = array(
  425. 'bx_if:show_as_hidden' => array(
  426. 'condition' => !$bShowAll && $iIndex < ($iComments - 1),
  427. 'content' => array(),
  428. ),
  429. 'item' => $this->_oTemplate->parseHtmlByName('comments_lu_notifications.html', array(
  430. 'style_prefix' => $this->_sStylePrefix,
  431. 'onclick_show' => $sShowOnClick,
  432. 'onclick_reply' => $sReplyOnClick,
  433. 'author_link' => $oAuthor->getUrl(),
  434. 'author_title' => bx_html_attribute($sAuthorName),
  435. 'author_name' => $sAuthorName,
  436. 'author_unit' => $oAuthor->getUnit(0, array('template' => 'unit_wo_info_links')),
  437. 'text' => _t('_cmt_txt_added_sample')
  438. )),
  439. 'bx_if:show_previous' => array(
  440. 'condition' => $bShowActions && $iIndex > 0,
  441. 'content' => array(
  442. 'onclick_previous' => $sJsObject . '.previousLiveUpdate(this)'
  443. )
  444. ),
  445. 'bx_if:show_close' => array(
  446. 'condition' => $bShowActions,
  447. 'content' => array(
  448. 'onclick_close' => $sJsObject . '.hideLiveUpdate(this)'
  449. )
  450. )
  451. );
  452. }
  453. return $this->_oTemplate->parseHtmlByName('popup_chain.html', array(
  454. 'html_id' => $this->getNotificationId(),
  455. 'bx_repeat:items' => $aTmplVarsNotifs
  456. ));
  457. }
  458. public function getElementBlock($aParams = array())
  459. {
  460. $aParams['usage'] = BX_CMT_USAGE_BLOCK;
  461. return $this->getElement($aParams);
  462. }
  463. public function getElementInline($aParams = array())
  464. {
  465. $aParams['usage'] = BX_CMT_USAGE_INLINE;
  466. return $this->getElement($aParams);
  467. }
  468. public function getElement($aParams = array())
  469. {
  470. $aParams = array_merge($this->_aElementDefaults, $aParams);
  471. $bShowDoCommentAsButtonSmall = isset($aParams['show_do_comment_as_button_small']) && $aParams['show_do_comment_as_button_small'] == true;
  472. $bShowDoCommentAsButton = !$bShowDoCommentAsButtonSmall && isset($aParams['show_do_comment_as_button']) && $aParams['show_do_comment_as_button'] == true;
  473. $bShowCounterEmpty = isset($aParams['show_counter_empty']) && $aParams['show_counter_empty'] == true;
  474. $iObjectId = $this->getId();
  475. $iAuthorId = $this->_getAuthorId();
  476. $iAuthorIp = $this->_getAuthorIp();
  477. $iCount = $this->getCommentsCountAll();
  478. $bCount = (int)$iCount != 0;
  479. $isAllowedComment = $this->isPostAllowed();
  480. //--- Do Comment
  481. $bTmplVarsDoComment = $this->_isShowDoComment($aParams, $isAllowedComment, $bCount);
  482. $aTmplVarsDoComment = array();
  483. if($bTmplVarsDoComment) {
  484. $sClass = '';
  485. if($bShowDoCommentAsButton)
  486. $sClass = 'bx-btn';
  487. else if ($bShowDoCommentAsButtonSmall)
  488. $sClass = 'bx-btn bx-btn-small';
  489. if(!$isAllowedComment)
  490. $sClass .= $bShowDoCommentAsButton || $bShowDoCommentAsButtonSmall ? ' bx-btn-disabled' : 'bx-cmts-disabled';
  491. $aTmplVarsDoComment = array(
  492. 'style_prefix' => $this->_sStylePrefix,
  493. 'do_comment' => $this->_oTemplate->parseLink($this->getListUrl(), $this->_getLabelDo($aParams), array(
  494. 'class' => $this->_sStylePrefix . '-do-comment ' . $this->_sStylePrefix . '-dc ' . $sClass,
  495. 'title' => _t($this->_getTitleDo())
  496. )),
  497. );
  498. }
  499. //--- Counter
  500. $bTmplVarsCounter = $this->_isShowCounter($aParams, $isAllowedComment, $bCount);
  501. $aTmplVarsCounter = array();
  502. if($bTmplVarsCounter)
  503. $aTmplVarsCounter = array(
  504. 'style_prefix' => $this->_sStylePrefix,
  505. 'bx_if:show_hidden' => array(
  506. 'condition' => !$bShowCounterEmpty && !$bCount,
  507. 'content' => array()
  508. ),
  509. 'counter' => $this->getCounter($aParams)
  510. );
  511. if(!$bTmplVarsDoComment && !$bTmplVarsCounter)
  512. return '';
  513. $sTmplName = $this->{'_getTmplElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_CMT_USAGE_DEFAULT)}();
  514. return $this->_oTemplate->parseHtmlByContent($sTmplName, array(
  515. 'style_prefix' => $this->_sStylePrefix,
  516. 'html_id' => $this->_aHtmlIds['main'],
  517. 'class' => $this->_sStylePrefix . ($bShowDoCommentAsButton ? '-button' : '') . ($bShowDoCommentAsButtonSmall ? '-button-small' : ''),
  518. 'count' => $iCount,
  519. 'bx_if:show_do_comment' => array(
  520. 'condition' => $bTmplVarsDoComment,
  521. 'content' => $aTmplVarsDoComment
  522. ),
  523. 'bx_if:show_counter' => array(
  524. 'condition' => $bTmplVarsCounter,
  525. 'content' => $aTmplVarsCounter
  526. ),
  527. 'script' => ''
  528. ));
  529. }
  530. public function getCounter($aParams = array())
  531. {
  532. $bShowEmpty = isset($aParams['show_counter_empty']) && $aParams['show_counter_empty'] == true;
  533. $bShowDoCommentAsButtonSmall = isset($aParams['show_do_comment_as_button_small']) && $aParams['show_do_comment_as_button_small'] == true;
  534. $bShowDoCommentAsButton = !$bShowDoCommentAsButtonSmall && isset($aParams['show_do_comment_as_button']) && $aParams['show_do_comment_as_button'] == true;
  535. $iCount = (int)$this->getCommentsCountAll();
  536. if($iCount == 0 && !$bShowEmpty)
  537. return '';
  538. $sClass = $this->_sStylePrefix . '-counter';
  539. if($bShowDoCommentAsButtonSmall)
  540. $sClass .= ' bx-btn-small-height';
  541. if($bShowDoCommentAsButton)
  542. $sClass .= ' bx-btn-height';
  543. $iCmtsLimit = 5;
  544. $aCmts = $this->_oQuery->getCommentsBy(array('type' => 'object_id', 'object_id' => $this->getId(), 'order_way' => 'desc', 'start' => 0, 'per_page' => $iCmtsLimit * 4));
  545. $aCmts = array_reverse($aCmts);
  546. $aTmplVarsProfiles = array();
  547. foreach($aCmts as $aCmt) {
  548. $iAuthor = (int)$aCmt['cmt_author_id'];
  549. if(array_key_exists($iAuthor, $aTmplVarsProfiles))
  550. continue;
  551. $oAuthor = BxDolProfile::getInstanceMagic($iAuthor);
  552. if(!$oAuthor)
  553. continue;
  554. $aTmplVarsProfiles[$iAuthor] = array(
  555. 'icon' => $oAuthor->getUnit(0, array('template' => array('name' => 'unit_wo_info_links', 'size' => 'icon')))
  556. );
  557. if(count($aTmplVarsProfiles) >= $iCmtsLimit)
  558. break;
  559. }
  560. $aTmplVarsProfiles = array_values($aTmplVarsProfiles);
  561. $sHref = !empty($aParams['overwrite_counter_link_href']) ? $aParams['overwrite_counter_link_href'] : $this->getListUrl();
  562. $sOnclick = '';
  563. if(!empty($aParams['overwrite_counter_link_onclick']))
  564. $sOnclick = $aParams['overwrite_counter_link_onclick'];
  565. return $this->_oTemplate->parseHtmlByContent($this->_getTmplCounter(), array(
  566. 'id' => $this->_aHtmlIds['counter'],
  567. 'class' => $sClass,
  568. 'href' => $sHref,
  569. 'bx_if:show_onclick' => array(
  570. 'condition' => !empty($sOnclick),
  571. 'content' => array(
  572. 'onclick' => $sOnclick
  573. )
  574. ),
  575. 'content' => $this->_getCounterLabel($iCount),
  576. 'bx_repeat:profiles' => $aTmplVarsProfiles,
  577. 'bx_if:show_icon' => array(
  578. 'condition' => $bShowEmpty || !empty($aTmplVarsProfiles),
  579. 'content' => array(
  580. 'sp' => $this->_sStylePrefix
  581. )
  582. )
  583. ));
  584. }
  585. protected function _getLabelDo($aParams = array())
  586. {
  587. return $this->_oTemplate->parseHtmlByContent($this->_getTmplLabelDo(), array(
  588. 'style_prefix' => $this->_sStylePrefix,
  589. 'bx_if:show_image' => array(
  590. 'condition' => isset($aParams['show_do_comment_image']) && $aParams['show_do_comment_image'] == true,
  591. 'content' => array(
  592. 'style_prefix' => $this->_sStylePrefix,
  593. 'src' => $this->_getImageDo()
  594. )
  595. ),
  596. 'bx_if:show_icon' => array(
  597. 'condition' => isset($aParams['show_do_comment_icon']) && $aParams['show_do_comment_icon'] == true,
  598. 'content' => array(
  599. 'style_prefix' => $this->_sStylePrefix,
  600. 'name' => $this->_getIconDo()
  601. )
  602. ),
  603. 'bx_if:show_text' => array(
  604. 'condition' => isset($aParams['show_do_comment_label']) && $aParams['show_do_comment_label'] == true,
  605. 'content' => array(
  606. 'style_prefix' => $this->_sStylePrefix,
  607. 'text' => _t($this->_getTitleDo())
  608. )
  609. )
  610. ));
  611. }
  612. protected function _getCounterLabel($iCount)
  613. {
  614. return (int)$iCount != 0 ? _t('_cmt_txt_counter', $iCount) : _t('_cmt_txt_counter_empty');
  615. }
  616. protected function _getTmplElementBlock()
  617. {
  618. return self::$_sTmplContentElementBlock;
  619. }
  620. protected function _getTmplElementInline()
  621. {
  622. return self::$_sTmplContentElementInline;
  623. }
  624. protected function _getTmplLabelDo()
  625. {
  626. return self::$_sTmplContentDoCommentLabel;
  627. }
  628. protected function _getTmplCounter()
  629. {
  630. return self::$_sTmplContentCounter;
  631. }
  632. /**
  633. * private functions
  634. */
  635. protected function _getControlsBox()
  636. {
  637. $sDisplay = '';
  638. $bDisplay = (int)$this->_aSystem['is_display_switch'] == 1;
  639. if($bDisplay) {
  640. $aDisplayLinks = array(
  641. array('id' => $this->_sSystem . '-flat', 'name' => $this->_sSystem . '-flat', 'class' => '', 'title' => '_cmt_display_flat', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeDisplay(this, \'flat\');'),
  642. array('id' => $this->_sSystem . '-threaded', 'name' => $this->_sSystem . '-threaded', 'class' => '', 'title' => '_cmt_display_threaded', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeDisplay(this, \'threaded\');')
  643. );
  644. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-display', 'menu_items' => $aDisplayLinks));
  645. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sDisplayType);
  646. $sDisplay = $oMenu->getCode();
  647. }
  648. $sBrowseType = '';
  649. $bBrowseType = (int)$this->_aSystem['is_browse_switch'] == 1;
  650. if($bBrowseType) {
  651. $aBrowseLinks = array(
  652. array('id' => $this->_sSystem . '-tail', 'name' => $this->_sSystem . '-tail', 'class' => '', 'title' => '_cmt_browse_tail', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'tail\');'),
  653. array('id' => $this->_sSystem . '-head', 'name' => $this->_sSystem . '-head', 'class' => '', 'title' => '_cmt_browse_head', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'head\');'),
  654. array('id' => $this->_sSystem . '-popular', 'name' => $this->_sSystem . '-popular', 'class' => '', 'title' => '_cmt_browse_popular', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'popular\');'),
  655. );
  656. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-browse', 'menu_items' => $aBrowseLinks));
  657. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sBrowseType);
  658. $sBrowseType = $oMenu->getCode();
  659. }
  660. $sBrowseFilter = '';
  661. $bBrowseFilter = (int)$this->_aSystem['is_browse_filter'] == 1;
  662. if($bBrowseFilter) {
  663. $aFilterLinks = array(
  664. array('id' => $this->_sSystem . '-all', 'name' => $this->_sSystem . '-all', 'class' => '', 'title' => '_cmt_browse_all', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'all\');'),
  665. array('id' => $this->_sSystem . '-friends', 'name' => $this->_sSystem . '-friends', 'class' => '', 'title' => '_cmt_browse_friends', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'friends\');'),
  666. array('id' => $this->_sSystem . '-subscriptions', 'name' => $this->_sSystem . '-subscriptions', 'class' => '', 'title' => '_cmt_browse_subscriptions', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'subscriptions\');')
  667. );
  668. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-filter', 'menu_items' => $aFilterLinks));
  669. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sBrowseFilter);
  670. $sBrowseFilter = $oMenu->getCode();
  671. }
  672. return $this->_oTemplate->parseHtmlByName('comments_controls.html', array(
  673. 'display_switcher' => $bDisplay ? $sDisplay : '',
  674. 'bx_if:is_divider_1' => array(
  675. 'condition' => $bDisplay && $bBrowseType,
  676. 'content' => array(
  677. 'style_prefix' => $this->_sStylePrefix,
  678. )
  679. ),
  680. 'browse_switcher' => $bBrowseType ? $sBrowseType : '',
  681. 'bx_if:is_divider_2' => array(
  682. 'condition' => $bBrowseType && $bBrowseFilter,
  683. 'content' => array(
  684. 'style_prefix' => $this->_sStylePrefix,
  685. )
  686. ),
  687. 'filter_switcher' => $bBrowseFilter ? $sBrowseFilter : '',
  688. ));
  689. }
  690. protected function _getActionsBox(&$aCmt, $aDp = array())
  691. {
  692. $bViewOnly = isset($aDp['view_only']) && $aDp['view_only'] === true;
  693. $bDynamicMode = isset($aDp['dynamic_mode']) && $aDp['dynamic_mode'] === true;
  694. $sMenuActions = '';
  695. if(!$bViewOnly) {
  696. $oMenuActions = BxDolMenu::getObjectInstance($this->_sMenuObjActions);
  697. $oMenuActions->setCmtsData($this, $aCmt['cmt_id']);
  698. $oMenuActions->setDynamicMode($bDynamicMode);
  699. $sMenuActions = $oMenuActions->getCode();
  700. }
  701. return $this->_oTemplate->parseHtmlByName('comment_actions.html', array(
  702. 'id' => $aCmt['cmt_id'],
  703. 'js_object' => $this->_sJsObjName,
  704. 'style_prefix' => $this->_sStylePrefix,
  705. 'menu_actions' => $sMenuActions,
  706. ));
  707. }
  708. protected function _getFormBox($sType, $aBp, $aDp)
  709. {
  710. $iCmtParentId = isset($aBp['parent_id']) ? (int)$aBp['parent_id'] : 0;
  711. $sPosition = isset($aDp['position']) ? $aDp['position'] : '';
  712. $bQuote = isset($aDp['quote']) && (bool)$aDp['quote'];
  713. $bFormMin = $iCmtParentId == 0 && isset($aDp['min_post_form']) && (bool)$aDp['min_post_form'];
  714. $sPositionSystem = $this->_aSystem['post_form_position'];
  715. if(!empty($sPosition) && $sPositionSystem != $sPosition)
  716. return '';
  717. $sClass = '';
  718. if(!empty($sPosition))
  719. $sClass .= ' ' . $this->_sStylePrefix . '-reply-' . $sPosition;
  720. if($bQuote)
  721. $sClass .= ' ' . $this->_sStylePrefix . '-reply-quote';
  722. if($bFormMin)
  723. $sClass .= ' ' . $this->_sStylePrefix . '-reply-min';
  724. if(!empty($aDp['class']))
  725. $sClass .= ' ' . $aDp['class'];
  726. $sClassBody = '';
  727. if(!empty($aDp['class_body']))
  728. $sClassBody .= ' ' . $aDp['class_body'];
  729. $aTmplVarsFormMin = array();
  730. if($bFormMin) {
  731. list($sAuthorName, $sAuthorLink, $sAuthorIcon, $sAuthorUnit) = $this->_getAuthorInfo($this->_getAuthorId());
  732. $oForm = new BxTemplFormView(array());
  733. $aInputPlaceholder = array(
  734. 'type' => 'text',
  735. 'name' => 'comment',
  736. 'caption' => '',
  737. 'attrs' => array(
  738. 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtShowForm(this)',
  739. 'placeholder' => _t($this->_aT['txt_min_form_placeholder'], $sAuthorName)
  740. ),
  741. 'value' => '',
  742. );
  743. $sClassBodyMin = '';
  744. if(!empty($aDp['class_body_min']))
  745. $sClassBodyMin .= ' ' . $aDp['class_body_min'];
  746. $aTmplVarsFormMin = array(
  747. 'js_object' => $this->_sJsObjName,
  748. 'class_body_min' => $sClassBodyMin,
  749. 'style_prefix' => $this->_sStylePrefix,
  750. 'author_unit' => $sAuthorUnit,
  751. 'placeholder' => $oForm->genRow($aInputPlaceholder)
  752. );
  753. }
  754. $aForm = $this->{'_getForm' . ucfirst($sType)}($iCmtParentId, $aDp);
  755. if(empty($aForm['form']))
  756. return !empty($aForm['msg']) && (isLogged() || $iCmtParentId != 0 || $this->getCommentsCount() > 0) ? MsgBox($aForm['msg']) : '';
  757. return $this->_oTemplate->parseHtmlByName('comment_reply_box.html', array(
  758. 'js_object' => $this->_sJsObjName,
  759. 'style_prefix' => $this->_sStylePrefix,
  760. 'class' => $sClass,
  761. 'class_body' => $sClassBody,
  762. 'bx_if:show_form_min' => array(
  763. 'condition' => $bFormMin,
  764. 'content' => $aTmplVarsFormMin
  765. ),
  766. 'form' => $aForm['form'],
  767. 'form_id' => $aForm['form_id'],
  768. ));
  769. }
  770. protected function _getFormAdd($aValues)
  771. {
  772. $iCmtAuthorId = isset($aValues['cmt_author_id']) ? (int)$aValues['cmt_author_id'] : $this->_getAuthorId();
  773. $iCmtParentId = isset($aValues['cmt_parent_id']) ? (int)$aValues['cmt_parent_id'] : 0;
  774. $oForm = $this->_getForm(BX_CMT_ACTION_POST, $iCmtParentId);
  775. $oForm->aFormAttrs['method'] = BX_DOL_FORM_METHOD_SPECIFIC;
  776. $oForm->aParams['csrf']['disable'] = true;
  777. $oForm->aInputs['cmt_parent_id']['value'] = $iCmtParentId;
  778. if(!empty($oForm->aParams['db']['submit_name'])) {
  779. $sSubmitName = false;
  780. if(is_array($oForm->aParams['db']['submit_name']))
  781. foreach($oForm->aParams['db']['submit_name'] as $sVal) {
  782. if(isset($oForm->aInputs[$sVal])) {
  783. $sSubmitName = $sVal;
  784. break;
  785. }
  786. }
  787. else
  788. $sSubmitName = $oForm->aParams['db']['submit_name'];
  789. if($sSubmitName && isset($oForm->aInputs[$sSubmitName]))
  790. $aValues[$sSubmitName] = $oForm->aInputs[$sSubmitName]['value'];
  791. }
  792. $oForm->initChecker(array(), $aValues);
  793. if(!$oForm->isSubmittedAndValid())
  794. return array('code' => 1, 'message' => '_sys_txt_error_occured');
  795. $iLevel = 0;
  796. $iCmtVisualParentId = 0;
  797. if((int)$iCmtParentId > 0) {
  798. $aParent = $this->getCommentRow($iCmtParentId);
  799. $iLevel = (int)$aParent['cmt_level'] + 1;
  800. $iCmtVisualParentId = $iLevel > $this->getMaxLevel() ? $aParent['cmt_vparent_id'] : $iCmtParentId;
  801. }
  802. $iCmtId = (int)$oForm->insert(array('cmt_vparent_id' => $iCmtVisualParentId, 'cmt_object_id' => $this->_iId, 'cmt_author_id' => $iCmtAuthorId, 'cmt_level' => $iLevel, 'cmt_time' => time()));
  803. if(!$iCmtId) {
  804. if(!$oForm->isValid())
  805. return array('code' => 1, 'message' => '_sys_txt_error_occured');
  806. else
  807. return array('code' => 2, 'message' => '_cmt_err_cannot_perform_action');
  808. }
  809. $iCmtUniqId = $this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId, $iCmtAuthorId);
  810. if($iCmtParentId) {
  811. $this->_oQuery->updateRepliesCount($iCmtParentId, 1);
  812. if(!BxDolModuleQuery::getInstance()->isEnabledByName('bx_notifications'))
  813. $this->_sendNotificationEmail($iCmtId, $iCmtParentId);
  814. }
  815. $this->_triggerComment();
  816. if($this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  817. $oMetatags->metaAdd($iCmtUniqId, $aValues['cmt_text']);
  818. $mixedResult = $this->onPostAfter($iCmtId);
  819. if($mixedResult === false)
  820. return array('code' => 2, 'message' => '_cmt_err_cannot_perform_action');
  821. return $mixedResult;
  822. }
  823. protected function _getFormPost($iCmtParentId = 0, $aDp = array())
  824. {
  825. $bCmtParentId = !empty($iCmtParentId);
  826. if(!$bCmtParentId && !$this->isPostAllowed())
  827. return array('msg' => $this->msgErrPostAllowed());
  828. if($bCmtParentId && !$this->isReplyAllowed($iCmtParentId))
  829. return array('msg' => $this->msgErrReplyAllowed());
  830. $bDynamic = isset($aDp['dynamic_mode']) && (bool)$aDp['dynamic_mode'];
  831. $bQuote = isset($aDp['quote']) && (bool)$aDp['quote'];
  832. $oForm = $this->_getForm(BX_CMT_ACTION_POST, $iCmtParentId);
  833. $oForm->aInputs['cmt_parent_id']['value'] = $iCmtParentId;
  834. if($bQuote) {
  835. $aCmtParent = $this->getCommentRow((int)$iCmtParentId);
  836. if(!empty($aCmtParent['cmt_text']))
  837. $oForm->aInputs['cmt_text']['value'] = $this->_oTemplate->parseHtmlByName('comment_quote.html', array(
  838. 'content' => $aCmtParent['cmt_text']
  839. ));
  840. }
  841. $oForm->initChecker();
  842. if($oForm->isSubmittedAndValid()) {
  843. $iCmtAuthorId = $this->_getAuthorId();
  844. $iCmtParentId = (int)$oForm->getCleanValue('cmt_parent_id');
  845. //--- Process Text ---//
  846. $sCmtText = $oForm->getCleanValue('cmt_text');
  847. $bCmtText = !empty($sCmtText);
  848. //--- Process Media ---//
  849. $aImageIds = $oForm->getCleanValue('cmt_image');
  850. $bImageIds = !empty($aImageIds) && is_array($aImageIds);
  851. if(!$bCmtText && !$bImageIds) {
  852. $oForm->aInputs['cmt_text']['error'] = _t('_Please enter characters');
  853. $oForm->setValid(false);
  854. return array('form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id);
  855. }
  856. $aParent = array();
  857. if($iCmtParentId > 0) {
  858. $aParent = $this->getCommentRow($iCmtParentId);
  859. if(empty($aParent) || !is_array($aParent)) {
  860. $iCmtParentId = 0;
  861. $oForm->setSubmittedValue('cmt_parent_id', $iCmtParentId, $oForm->aFormAttrs['method']);
  862. }
  863. }
  864. $iLevel = 0;
  865. $iCmtVisualParentId = 0;
  866. if($iCmtParentId > 0) {
  867. $iLevel = (int)$aParent['cmt_level'] + 1;
  868. $iCmtVisualParentId = $iLevel > $this->getMaxLevel() ? $aParent['cmt_vparent_id'] : $iCmtParentId;
  869. }
  870. $iCmtId = (int)$oForm->insert(array('cmt_vparent_id' => $iCmtVisualParentId, 'cmt_object_id' => $this->_iId, 'cmt_author_id' => $iCmtAuthorId, 'cmt_level' => $iLevel, 'cmt_time' => time()));
  871. if($iCmtId != 0) {
  872. $iCmtUniqId = $this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId, $iCmtAuthorId);
  873. if($this->isAttachImageEnabled())
  874. $oForm->processImages($this, 'cmt_image', $iCmtUniqId, $iCmtId, $iCmtAuthorId, true);
  875. if($iCmtParentId > 0) {
  876. $this->_oQuery->updateRepliesCount($iCmtParentId, 1);
  877. if(!BxDolModuleQuery::getInstance()->isEnabledByName('bx_notifications'))
  878. $this->_sendNotificationEmail($iCmtId, $iCmtParentId);
  879. }
  880. $this->_triggerComment();
  881. if($iCmtParentId > 0)
  882. $this->isReplyAllowed($iCmtParentId, true);
  883. else
  884. $this->isPostAllowed(true);
  885. if($this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  886. $oMetatags->metaAdd($iCmtUniqId, $sCmtText);
  887. if(($mixedResult = $this->onPostAfter($iCmtId)) !== false)
  888. return $mixedResult;
  889. }
  890. return array('msg' => _t('_cmt_err_cannot_perform_action'));
  891. }
  892. return array('form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id);
  893. }
  894. protected function _getFormEdit($iCmtId, $aDp = array())
  895. {
  896. $bDynamic = isset($aDp['dynamic_mode']) && (bool)$aDp['dynamic_mode'];
  897. $aCmt = $this->getCommentSimple($iCmtId);
  898. if(!$aCmt)
  899. return array('msg' => _t('_No such comment'));
  900. $iCmtAuthorId = $this->_getAuthorId();
  901. if(!$this->isEditAllowed($aCmt))
  902. return array('msg' => $aCmt['cmt_author_id'] == $iCmtAuthorId ? strip_tags($this->msgErrEditAllowed()) : _t('_Access denied'));
  903. $oForm = $this->_getForm(BX_CMT_ACTION_EDIT, $aCmt['cmt_id']);
  904. $oForm->initChecker($aCmt);
  905. if($oForm->isSubmittedAndValid()) {
  906. $sCmtText = $oForm->getCleanValue('cmt_text');
  907. if($oForm->update($iCmtId) !== false) {
  908. $iCmtUniqId = $this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId, (int)$aCmt['cmt_author_id']);
  909. if($this->isAttachImageEnabled())
  910. $oForm->processImages($this, 'cmt_image', $iCmtUniqId, $iCmtId, $iCmtAuthorId, false);
  911. $this->isEditAllowed($aCmt, true);
  912. if($this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  913. $oMetatags->metaAdd($iCmtUniqId, $sCmtText);
  914. if(($mixedResult = $this->onEditAfter($iCmtId)) !== false)
  915. return $mixedResult;
  916. }
  917. return array('msg' => _t('_cmt_err_cannot_perform_action'));
  918. }
  919. return array('form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id);
  920. }
  921. protected function _getForm($sAction, $iId)
  922. {
  923. $oForm = $this->_getFormObject($sAction);
  924. $oForm->setId(sprintf($oForm->getAttributeMask('id'), $sAction, $this->_sSystem, $iId));
  925. $oForm->setName(sprintf($oForm->getAttributeMask('name'), $sAction, $this->_sSystem, $iId));
  926. $oForm->aParams['db']['table'] = $this->_aSystem['table'];
  927. $oForm->aInputs['sys']['value'] = $this->_sSystem;
  928. $oForm->aInputs['id']['value'] = $this->_iId;
  929. $oForm->aInputs['action']['value'] = 'Submit' . ucfirst($sAction) . 'Form';
  930. if(!$this->isAttachImageEnabled())
  931. unset($oForm->aInputs['cmt_image']);
  932. if(isset($oForm->aInputs['cmt_text'])) {
  933. $oForm->aInputs['cmt_text']['html'] = $this->_aSystem['html'];
  934. $oForm->aInputs['cmt_text']['db']['pass'] = $this->isHtml() ? 'XssHtml' : 'XssMultiline';
  935. if(isset($oForm->aInputs['cmt_text']['checker']['func']) && strtolower($oForm->aInputs['cmt_text']['checker']['func']) == 'length') {
  936. $iCmtTextMin = (int)$this->_aSystem['chars_post_min'];
  937. $iCmtTextMax = (int)$this->_aSystem['chars_post_max'];
  938. $oForm->aInputs['cmt_text']['checker']['params'] = array($iCmtTextMin, $iCmtTextMax);
  939. $oForm->aInputs['cmt_text']['checker']['error'] = _t('_Please enter n1-n2 characters', $iCmtTextMin, $iCmtTextMax);
  940. }
  941. }
  942. if($sAction == BX_CMT_ACTION_EDIT && isset($oForm->aInputs['cmt_controls']))
  943. foreach($oForm->aInputs['cmt_controls'] as $mixedKey => $mixedValue) {
  944. if(!is_numeric($mixedKey) || empty($mixedValue['name']) || $mixedValue['name'] != 'cmt_cancel')
  945. continue;
  946. if(!isset($oForm->aInputs['cmt_controls'][$mixedKey]['attrs']))
  947. $oForm->aInputs['cmt_controls'][$mixedKey]['attrs'] = array();
  948. $oForm->aInputs['cmt_controls'][$mixedKey]['attrs']['onclick'] = $this->_sJsObjName . '.cmtEdit(this, ' . $iId . ', false)';
  949. }
  950. return $oForm;
  951. }
  952. protected function _getContent($aCmt)
  953. {
  954. $sAttachments = $this->_getAttachments($aCmt);
  955. $sReactions = '';
  956. if(($oReaction = $this->getReactionObject($aCmt['cmt_unique_id'])) !== false)
  957. $sReactions = $oReaction->getCounter(array(
  958. 'show_counter' => true
  959. ));
  960. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameItemContent, array_merge(array(
  961. 'style_prefix' => $this->_sStylePrefix,
  962. 'js_object' => $this->_sJsObjName,
  963. 'bx_if:show_attached' => array(
  964. 'condition' => !empty($sAttachments),
  965. 'content' => array(
  966. 'style_prefix' => $this->_sStylePrefix,
  967. 'attached' => $sAttachments
  968. )
  969. ),
  970. 'reactions' => $sReactions,
  971. ), $this->_getTmplVarsText($aCmt)));
  972. }
  973. protected function _getMoreLink($sCmts, $aBp = array(), $aDp = array())
  974. {
  975. $iStart = $iPerView = 0;
  976. switch($aBp['type']) {
  977. case BX_CMT_BROWSE_HEAD:
  978. case BX_CMT_BROWSE_POPULAR:
  979. case BX_CMT_BROWSE_CONNECTION:
  980. $iPerView = $aBp['per_view'];
  981. $iStart = $aBp['start'] + $iPerView;
  982. if($iStart >= $aBp['count'])
  983. return $sCmts;
  984. break;
  985. case BX_CMT_BROWSE_TAIL:
  986. $iPerView = $aBp['per_view'];
  987. $iStart = $aBp['start'] - $iPerView;
  988. if($iStart < 0) {
  989. $iPerView += $iStart;
  990. $iStart = 0;
  991. }
  992. if($iStart == 0 && $iPerView == 0)
  993. return $sCmts;
  994. break;
  995. }
  996. $sParentKey = isset($aBp['vparent_id']) ? 'vparent_id' : 'parent_id';
  997. $bRoot = (int)$aBp[$sParentKey] <= 0;
  998. $sMore = $this->_oTemplate->parseHtmlByName('comment_more.html', array(
  999. 'js_object' => $this->_sJsObjName,
  1000. 'style_prefix' => $this->_sStylePrefix,
  1001. 'bx_if:is_root' => array(
  1002. 'condition' => $bRoot,
  1003. 'content' => array()
  1004. ),
  1005. 'parent_id' => $aBp[$sParentKey],
  1006. 'start' => $iStart,
  1007. 'per_view' => $iPerView,
  1008. 'title' => _t('_cmt_load_more_' . ($aBp[$sParentKey] == 0 ? 'comments' : 'replies') . '_' . $aBp['type'])
  1009. ));
  1010. switch($aBp['type']) {
  1011. case BX_CMT_BROWSE_HEAD:
  1012. case BX_CMT_BROWSE_POPULAR:
  1013. case BX_CMT_BROWSE_CONNECTION:
  1014. $sCmts .= $sMore;
  1015. break;
  1016. case BX_CMT_BROWSE_TAIL:
  1017. $sCmts = $sMore . $sCmts;
  1018. break;
  1019. }
  1020. return $sCmts;
  1021. }
  1022. protected function _getEmpty($aDp = array())
  1023. {
  1024. $sClass = '';
  1025. if(!empty($aDp['class']))
  1026. $sClass .= ' ' . $aDp['class'];
  1027. return $this->_oTemplate->parseHtmlByName('comment_empty.html', array(
  1028. 'style_prefix' => $this->_sStylePrefix,
  1029. 'class' => $sClass,
  1030. 'content' => MsgBox(_t('_Empty'))
  1031. ));
  1032. }
  1033. protected function _getViewImagePopup()
  1034. {
  1035. $sViewImagePopupId = 'cmts-box-' . $this->_sSystem . '-' . $this->getId() . '-view-image-popup' ;
  1036. $sViewImagePopupContent = $this->_oTemplate->parseHtmlByName('popup_image.html', array(
  1037. 'image_url' => ''
  1038. ));
  1039. return BxTemplFunctions::getInstance()->transBox($sViewImagePopupId, $sViewImagePopupContent, true);
  1040. }
  1041. protected function _getAttachments($aCmt)
  1042. {
  1043. $aTmplImages = array();
  1044. if(!$this->isAttachImageEnabled())
  1045. return '';
  1046. $aFiles = $this->_oQuery->getFiles($this->_aSystem['system_id'], $aCmt['cmt_id']);
  1047. if(!empty($aFiles) && is_array($aFiles)) {
  1048. $oStorage = BxDolStorage::getObjectInstance($this->getStorageObjectName());
  1049. $oTranscoder = BxDolTranscoderImage::getObjectInstance($this->getTranscoderPreviewName());
  1050. foreach($aFiles as $aFile) {
  1051. $bImage = $oTranscoder && $oTranscoder->isMimeTypeSupported($aFile['mime_type']);
  1052. $sPreview = '';
  1053. if($oTranscoder && $bImage)
  1054. $sPreview = $oTranscoder->getFileUrl($aFile['image_id']);
  1055. if(!$sPreview)
  1056. $sPreview = $this->_oTemplate->getIconUrl($oStorage->getIconNameByFileName($aFile['file_name']));
  1057. $aTmplVarsFile = array(
  1058. 'js_object' => $this->_sJsObjName,
  1059. 'preview' => $sPreview,
  1060. 'file' => $oStorage->getFileUrlById($aFile['image_id']),
  1061. 'file_name' => $aFile['file_name'],
  1062. 'file_icon' => $oStorage->getFontIconNameByFileName($aFile['file_name']),
  1063. 'file_size' => _t_format_size($aFile['size']),
  1064. );
  1065. $aTmplImages[] = array(
  1066. 'style_prefix' => $this->_sStylePrefix,
  1067. 'bx_if:show_image' => array(
  1068. 'condition' => $bImage,
  1069. 'content' => $aTmplVarsFile
  1070. ),
  1071. 'bx_if:show_file' => array(
  1072. 'condition' => !$bImage,
  1073. 'content' => $aTmplVarsFile
  1074. ),
  1075. );
  1076. }
  1077. }
  1078. return $this->_oTemplate->parseHtmlByName('comment_attachments.html', array(
  1079. 'bx_repeat:attached' => $aTmplImages
  1080. ));
  1081. }
  1082. protected function _getTmplVarsAuthor($aCmt)
  1083. {
  1084. list($sAuthorName, $sAuthorLink, $sAuthorIcon, $sAuthorUnit) = $this->_getAuthorInfo($aCmt['cmt_author_id']);
  1085. $bAuthorIcon = !empty($sAuthorIcon);
  1086. return array(
  1087. 'author_unit' => $sAuthorUnit,
  1088. 'bx_if:show_icon' => array(
  1089. 'condition' => $bAuthorIcon,
  1090. 'content' => array(
  1091. 'author_icon' => $sAuthorIcon
  1092. )
  1093. ),
  1094. 'bx_if:show_icon_empty' => array(
  1095. 'condition' => !$bAuthorIcon,
  1096. 'content' => array()
  1097. ),
  1098. 'bx_if:show_author_link' => array(
  1099. 'condition' => !empty($sAuthorLink),
  1100. 'content' => array(
  1101. 'author_link' => $sAuthorLink,
  1102. 'author_title' => bx_html_attribute($sAuthorName),
  1103. 'author_name' => $sAuthorName
  1104. )
  1105. ),
  1106. 'bx_if:show_author_text' => array(
  1107. 'condition' => empty($sAuthorLink),
  1108. 'content' => array(
  1109. 'author_name' => $sAuthorName
  1110. )
  1111. ),
  1112. );
  1113. }
  1114. protected function _getTmplVarsText($aCmt)
  1115. {
  1116. $sText = $aCmt['cmt_text'];
  1117. $sTextMore = '';
  1118. if(!$this->isHtml()) {
  1119. $iMaxLength = (int)$this->_aSystem['chars_display_max'];
  1120. if(strlen($sText) > $iMaxLength) {
  1121. $iLength = strpos($sText, ' ', $iMaxLength);
  1122. $sTextMore = trim(substr($sText, $iLength));
  1123. $sText = trim(substr($sText, 0, $iLength));
  1124. }
  1125. }
  1126. $sText = $this->_prepareTextForOutput($sText, $aCmt['cmt_id']);
  1127. $sTextMore = $this->_prepareTextForOutput($sTextMore, $aCmt['cmt_id']);
  1128. return array(
  1129. 'text' => $sText,
  1130. 'bx_if:show_more' => array(
  1131. 'condition' => !empty($sTextMore),
  1132. 'content' => array(
  1133. 'style_prefix' => $this->_sStylePrefix,
  1134. 'js_object' => $this->_sJsObjName,
  1135. 'text_more' => $sTextMore
  1136. )
  1137. ),
  1138. );
  1139. }
  1140. }
  1141. /** @} */