BxBaseCmts.php 54 KB

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