BxBaseCmts.php 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  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. 'bx_if:show_pinned' => array(
  284. 'condition' => (int)$aCmt['cmt_pinned'] > 0,
  285. 'content' => array(
  286. 'style_prefix' => $this->_sStylePrefix,
  287. )
  288. ),
  289. 'content' => $sContent,
  290. 'actions' => $sActions,
  291. 'replies' => $sReplies,
  292. ), $this->_getTmplVarsAuthor($aCmt)));
  293. bx_alert('system', 'view_comment', $aCmt['cmt_id'], 0, array('comment' => $aCmt, 'system' => $this->_sSystem, 'override_result' => &$sResult));
  294. return $sResult;
  295. }
  296. public function getCommentStructure($iCmtId, $aBp = array(), $aDp = array())
  297. {
  298. $aRoot = $this->getCommentRow((int)$iCmtId);
  299. $aBps = $aBp;
  300. if(!empty($aRoot))
  301. $aBps['parent_id'] = $aRoot['cmt_id'];
  302. $this->_prepareStructureBp($aDp['type'], $aBps);
  303. $iLevel = 0;
  304. $aStructure = array();
  305. $this->_getStructure($aRoot, $aBps, $iLevel, $aStructure);
  306. return !empty($aStructure) && is_array($aStructure) ? $aStructure : false;
  307. }
  308. function getCommentSearch($iCmtId, &$sAddon)
  309. {
  310. $aBp = array();
  311. $aDp = array(
  312. 'type' => BX_CMT_DISPLAY_FLAT,
  313. 'view_only' => true
  314. );
  315. if(empty($sAddon))
  316. $sAddon = $this->getJsScript($aBp, $aDp);
  317. return $this->_oTemplate->parseHtmlByName('comment_search.html', array(
  318. 'comment' => $this->getComment($iCmtId, $aBp, $aDp),
  319. 'view_image_popup' => $this->_getViewImagePopup(),
  320. ));
  321. }
  322. /**
  323. * get one comment for "Live Search"
  324. *
  325. * @param int $iCmtId - comment id
  326. * @return string
  327. */
  328. function getCommentLiveSearch($mixedCmt, $aParams = array())
  329. {
  330. $aCmt = !is_array($mixedCmt) ? $this->getCommentRow((int)$mixedCmt) : $mixedCmt;
  331. list($sAuthorName, $sAuthorLink, $sAuthorIcon) = $this->_getAuthorInfo($aCmt['cmt_author_id']);
  332. $bAuthorIcon = !empty($sAuthorIcon);
  333. $sViewLink = $this->getViewUrl($aCmt['cmt_id']);
  334. if(empty($sViewLink))
  335. $sViewLink = '#';
  336. return $this->_oTemplate->parseHtmlByName('comment_live_search.html', array(
  337. 'bx_if:show_icon' => array(
  338. 'condition' => $bAuthorIcon,
  339. 'content' => array(
  340. 'author_icon' => $sAuthorIcon,
  341. 'view_link' => $sViewLink
  342. )
  343. ),
  344. 'bx_if:show_icon_empty' => array(
  345. 'condition' => !$bAuthorIcon,
  346. 'content' => array()
  347. ),
  348. 'view_link' => $sViewLink,
  349. 'text' => BxTemplFunctions::getInstance()->getStringWithLimitedLength(strip_tags($aCmt['cmt_text']), $this->_sSnippetLenthLiveSearch),
  350. 'sample' => isset($aParams['txt_sample_single']) ? _t($aParams['txt_sample_single']) : ''
  351. ));
  352. }
  353. function getFormBoxPost($aBp = array(), $aDp = array())
  354. {
  355. return $this->_getFormBox(BX_CMT_ACTION_POST, $aBp, $aDp);
  356. }
  357. function getFormBoxEdit($aBp = array(), $aDp = array())
  358. {
  359. return $this->_getFormBox(BX_CMT_ACTION_EDIT, $aBp, $aDp);
  360. }
  361. function getFormPost($iCmtParentId = 0, $aDp = array())
  362. {
  363. return $this->_getFormPost($iCmtParentId, $aDp);
  364. }
  365. function getFormEdit($iCmtId, $aDp = array())
  366. {
  367. return $this->_getFormEdit($iCmtId, $aDp);
  368. }
  369. function getLiveUpdate($iCountOld = 0, $iCountNew = 0)
  370. {
  371. $iCount = (int)$iCountNew - (int)$iCountOld;
  372. if($iCount < 0)
  373. return '';
  374. $aComments = $this->_oQuery->getCommentsBy(array('type' => 'latest', 'object_id' => $this->_iId, 'author' => $this->_getAuthorId(), 'others' => 1, 'start' => '0', 'per_page' => $iCount));
  375. if(empty($aComments) || !is_array($aComments))
  376. return '';
  377. $aComment = array_shift($aComments);
  378. if(empty($aComment) || !is_array($aComment))
  379. return '';
  380. $sJsObject = $this->getJsObjectName();
  381. return $this->_oTemplate->parseHtmlByName('comments_lu_button.html', array(
  382. 'style_prefix' => $this->_sStylePrefix,
  383. 'html_id' => $this->getNotificationId(),
  384. 'onclick_show' => "javascript:" . $sJsObject . ".goToBtn(this, '" . $this->getItemAnchor($aComment['cmt_id']) . "', '" . $aComment['cmt_id'] . "');",
  385. ));
  386. }
  387. function getLiveUpdates($iCountOld = 0, $iCountNew = 0)
  388. {
  389. $bShowAll = true;
  390. $bShowActions = false;
  391. $iCount = (int)$iCountNew - (int)$iCountOld;
  392. if($iCount < 0)
  393. return '';
  394. $aComments = $this->_oQuery->getCommentsBy(array('type' => 'latest', 'object_id' => $this->_iId, 'author' => $this->_getAuthorId(), 'others' => 1, 'start' => '0', 'per_page' => $iCount));
  395. if(empty($aComments) || !is_array($aComments))
  396. return '';
  397. $sJsObject = $this->getJsObjectName();
  398. $iUserId = $this->_getAuthorId();
  399. $bModerator = $this->isModerator();
  400. $aComments = array_reverse($aComments);
  401. $iComments = count($aComments);
  402. $aTmplVarsNotifs = array();
  403. foreach($aComments as $iIndex => $aComment) {
  404. $iCommentId = $aComment['cmt_id'];
  405. $sShowOnClick = "javascript:" . $sJsObject . ".goTo(this, '" . $this->getItemAnchor($iCommentId) . "', '" . $iCommentId . "');";
  406. $sReplyOnClick = "javascript:" . $sJsObject . ".goToAndReply(this, '" . $this->getItemAnchor($iCommentId) . "', '" . $iCommentId . "');";
  407. $iAuthorId = (int)$aComment['cmt_author_id'];
  408. if($iAuthorId < 0) {
  409. if(abs($iAuthorId) == $iUserId)
  410. continue;
  411. else if($bModerator)
  412. $iAuthorId *= -1;
  413. }
  414. $oAuthor = $this->_getAuthorObject($iAuthorId);
  415. $sAuthorName = $oAuthor->getDisplayName();
  416. $aTmplVarsNotifs[] = array(
  417. 'bx_if:show_as_hidden' => array(
  418. 'condition' => !$bShowAll && $iIndex < ($iComments - 1),
  419. 'content' => array(),
  420. ),
  421. 'item' => $this->_oTemplate->parseHtmlByName('comments_lu_notifications.html', array(
  422. 'style_prefix' => $this->_sStylePrefix,
  423. 'onclick_show' => $sShowOnClick,
  424. 'onclick_reply' => $sReplyOnClick,
  425. 'author_link' => $oAuthor->getUrl(),
  426. 'author_title' => bx_html_attribute($sAuthorName),
  427. 'author_name' => $sAuthorName,
  428. 'author_unit' => $oAuthor->getUnit(0, array('template' => 'unit_wo_info_links')),
  429. 'text' => _t('_cmt_txt_added_sample')
  430. )),
  431. 'bx_if:show_previous' => array(
  432. 'condition' => $bShowActions && $iIndex > 0,
  433. 'content' => array(
  434. 'onclick_previous' => $sJsObject . '.previousLiveUpdate(this)'
  435. )
  436. ),
  437. 'bx_if:show_close' => array(
  438. 'condition' => $bShowActions,
  439. 'content' => array(
  440. 'onclick_close' => $sJsObject . '.hideLiveUpdate(this)'
  441. )
  442. )
  443. );
  444. }
  445. return $this->_oTemplate->parseHtmlByName('popup_chain.html', array(
  446. 'html_id' => $this->getNotificationId(),
  447. 'bx_repeat:items' => $aTmplVarsNotifs
  448. ));
  449. }
  450. public function getElementBlock($aParams = array())
  451. {
  452. $aParams['usage'] = BX_CMT_USAGE_BLOCK;
  453. return $this->getElement($aParams);
  454. }
  455. public function getElementInline($aParams = array())
  456. {
  457. $aParams['usage'] = BX_CMT_USAGE_INLINE;
  458. return $this->getElement($aParams);
  459. }
  460. public function getElement($aParams = array())
  461. {
  462. $aParams = array_merge($this->_aElementDefaults, $aParams);
  463. $bShowDoCommentAsButtonSmall = isset($aParams['show_do_comment_as_button_small']) && $aParams['show_do_comment_as_button_small'] == true;
  464. $bShowDoCommentAsButton = !$bShowDoCommentAsButtonSmall && isset($aParams['show_do_comment_as_button']) && $aParams['show_do_comment_as_button'] == true;
  465. $bShowCounterEmpty = isset($aParams['show_counter_empty']) && $aParams['show_counter_empty'] == true;
  466. $iObjectId = $this->getId();
  467. $iAuthorId = $this->_getAuthorId();
  468. $iAuthorIp = $this->_getAuthorIp();
  469. $iCount = $this->getCommentsCountAll();
  470. $bCount = (int)$iCount != 0;
  471. $isAllowedComment = $this->isPostAllowed();
  472. //--- Do Comment
  473. $bTmplVarsDoComment = $this->_isShowDoComment($aParams, $isAllowedComment, $bCount);
  474. $aTmplVarsDoComment = array();
  475. if($bTmplVarsDoComment) {
  476. $sClass = '';
  477. if($bShowDoCommentAsButton)
  478. $sClass = 'bx-btn';
  479. else if ($bShowDoCommentAsButtonSmall)
  480. $sClass = 'bx-btn bx-btn-small';
  481. if(!$isAllowedComment)
  482. $sClass .= $bShowDoCommentAsButton || $bShowDoCommentAsButtonSmall ? ' bx-btn-disabled' : 'bx-cmts-disabled';
  483. $aTmplVarsDoComment = array(
  484. 'style_prefix' => $this->_sStylePrefix,
  485. 'do_comment' => $this->_oTemplate->parseLink($this->getListUrl(), $this->_getLabelDo($aParams), array(
  486. 'class' => $this->_sStylePrefix . '-do-comment ' . $this->_sStylePrefix . '-dc ' . $sClass,
  487. 'title' => _t($this->_getTitleDo())
  488. )),
  489. );
  490. }
  491. //--- Counter
  492. $bTmplVarsCounter = $this->_isShowCounter($aParams, $isAllowedComment, $bCount);
  493. $aTmplVarsCounter = array();
  494. if($bTmplVarsCounter)
  495. $aTmplVarsCounter = array(
  496. 'style_prefix' => $this->_sStylePrefix,
  497. 'bx_if:show_hidden' => array(
  498. 'condition' => !$bShowCounterEmpty && !$bCount,
  499. 'content' => array()
  500. ),
  501. 'counter' => $this->getCounter($aParams)
  502. );
  503. if(!$bTmplVarsDoComment && !$bTmplVarsCounter)
  504. return '';
  505. $sTmplName = $this->{'_getTmplElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_CMT_USAGE_DEFAULT)}();
  506. return $this->_oTemplate->parseHtmlByContent($sTmplName, array(
  507. 'style_prefix' => $this->_sStylePrefix,
  508. 'html_id' => $this->_aHtmlIds['main'],
  509. 'class' => $this->_sStylePrefix . ($bShowDoCommentAsButton ? '-button' : '') . ($bShowDoCommentAsButtonSmall ? '-button-small' : ''),
  510. 'count' => $iCount,
  511. 'bx_if:show_do_comment' => array(
  512. 'condition' => $bTmplVarsDoComment,
  513. 'content' => $aTmplVarsDoComment
  514. ),
  515. 'bx_if:show_counter' => array(
  516. 'condition' => $bTmplVarsCounter,
  517. 'content' => $aTmplVarsCounter
  518. ),
  519. 'script' => ''
  520. ));
  521. }
  522. public function getCounter($aParams = array())
  523. {
  524. $bShowEmpty = isset($aParams['show_counter_empty']) && $aParams['show_counter_empty'] == true;
  525. $bShowDoCommentAsButtonSmall = isset($aParams['show_do_comment_as_button_small']) && $aParams['show_do_comment_as_button_small'] == true;
  526. $bShowDoCommentAsButton = !$bShowDoCommentAsButtonSmall && isset($aParams['show_do_comment_as_button']) && $aParams['show_do_comment_as_button'] == true;
  527. $iCount = (int)$this->getCommentsCountAll();
  528. if($iCount == 0 && !$bShowEmpty)
  529. return '';
  530. $sClass = $this->_sStylePrefix . '-counter';
  531. if($bShowDoCommentAsButtonSmall)
  532. $sClass .= ' bx-btn-small-height';
  533. if($bShowDoCommentAsButton)
  534. $sClass .= ' bx-btn-height';
  535. $iCmtsLimit = 5;
  536. $aCmts = $this->_oQuery->getCommentsBy(array('type' => 'object_id', 'object_id' => $this->getId(), 'order_way' => 'desc', 'start' => 0, 'per_page' => $iCmtsLimit * 4));
  537. $aCmts = array_reverse($aCmts);
  538. $aTmplVarsProfiles = array();
  539. foreach($aCmts as $aCmt) {
  540. $iAuthor = (int)$aCmt['cmt_author_id'];
  541. if(array_key_exists($iAuthor, $aTmplVarsProfiles))
  542. continue;
  543. $oAuthor = BxDolProfile::getInstanceMagic($iAuthor);
  544. if(!$oAuthor)
  545. continue;
  546. $aTmplVarsProfiles[$iAuthor] = array(
  547. 'icon' => $oAuthor->getUnit(0, array('template' => array('name' => 'unit_wo_info_links', 'size' => 'icon')))
  548. );
  549. if(count($aTmplVarsProfiles) >= $iCmtsLimit)
  550. break;
  551. }
  552. $aTmplVarsProfiles = array_values($aTmplVarsProfiles);
  553. $sHref = !empty($aParams['overwrite_counter_link_href']) ? $aParams['overwrite_counter_link_href'] : $this->getListUrl();
  554. $sOnclick = '';
  555. if(!empty($aParams['overwrite_counter_link_onclick']))
  556. $sOnclick = $aParams['overwrite_counter_link_onclick'];
  557. return $this->_oTemplate->parseHtmlByContent($this->_getTmplCounter(), array(
  558. 'id' => $this->_aHtmlIds['counter'],
  559. 'class' => $sClass,
  560. 'href' => $sHref,
  561. 'bx_if:show_onclick' => array(
  562. 'condition' => !empty($sOnclick),
  563. 'content' => array(
  564. 'onclick' => $sOnclick
  565. )
  566. ),
  567. 'content' => $this->_getCounterLabel($iCount),
  568. 'bx_repeat:profiles' => $aTmplVarsProfiles,
  569. 'bx_if:show_icon' => array(
  570. 'condition' => $bShowEmpty || !empty($aTmplVarsProfiles),
  571. 'content' => array(
  572. 'sp' => $this->_sStylePrefix
  573. )
  574. )
  575. ));
  576. }
  577. protected function _getLabelDo($aParams = array())
  578. {
  579. return $this->_oTemplate->parseHtmlByContent($this->_getTmplLabelDo(), array(
  580. 'style_prefix' => $this->_sStylePrefix,
  581. 'bx_if:show_image' => array(
  582. 'condition' => isset($aParams['show_do_comment_image']) && $aParams['show_do_comment_image'] == true,
  583. 'content' => array(
  584. 'style_prefix' => $this->_sStylePrefix,
  585. 'src' => $this->_getImageDo()
  586. )
  587. ),
  588. 'bx_if:show_icon' => array(
  589. 'condition' => isset($aParams['show_do_comment_icon']) && $aParams['show_do_comment_icon'] == true,
  590. 'content' => array(
  591. 'style_prefix' => $this->_sStylePrefix,
  592. 'name' => $this->_getIconDo()
  593. )
  594. ),
  595. 'bx_if:show_text' => array(
  596. 'condition' => isset($aParams['show_do_comment_label']) && $aParams['show_do_comment_label'] == true,
  597. 'content' => array(
  598. 'style_prefix' => $this->_sStylePrefix,
  599. 'text' => _t($this->_getTitleDo())
  600. )
  601. )
  602. ));
  603. }
  604. protected function _getCounterLabel($iCount)
  605. {
  606. return (int)$iCount != 0 ? _t('_cmt_txt_counter', $iCount) : _t('_cmt_txt_counter_empty');
  607. }
  608. protected function _getTmplElementBlock()
  609. {
  610. return self::$_sTmplContentElementBlock;
  611. }
  612. protected function _getTmplElementInline()
  613. {
  614. return self::$_sTmplContentElementInline;
  615. }
  616. protected function _getTmplLabelDo()
  617. {
  618. return self::$_sTmplContentDoCommentLabel;
  619. }
  620. protected function _getTmplCounter()
  621. {
  622. return self::$_sTmplContentCounter;
  623. }
  624. /**
  625. * private functions
  626. */
  627. protected function _getControlsBox()
  628. {
  629. $sDisplay = '';
  630. $bDisplay = (int)$this->_aSystem['is_display_switch'] == 1;
  631. if($bDisplay) {
  632. $aDisplayLinks = array(
  633. array('id' => $this->_sSystem . '-flat', 'name' => $this->_sSystem . '-flat', 'class' => '', 'title' => '_cmt_display_flat', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeDisplay(this, \'flat\');'),
  634. array('id' => $this->_sSystem . '-threaded', 'name' => $this->_sSystem . '-threaded', 'class' => '', 'title' => '_cmt_display_threaded', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeDisplay(this, \'threaded\');')
  635. );
  636. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-display', 'menu_items' => $aDisplayLinks));
  637. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sDisplayType);
  638. $sDisplay = $oMenu->getCode();
  639. }
  640. $sBrowseType = '';
  641. $bBrowseType = (int)$this->_aSystem['is_browse_switch'] == 1;
  642. if($bBrowseType) {
  643. $aBrowseLinks = array(
  644. array('id' => $this->_sSystem . '-tail', 'name' => $this->_sSystem . '-tail', 'class' => '', 'title' => '_cmt_browse_tail', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'tail\');'),
  645. array('id' => $this->_sSystem . '-head', 'name' => $this->_sSystem . '-head', 'class' => '', 'title' => '_cmt_browse_head', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'head\');'),
  646. array('id' => $this->_sSystem . '-popular', 'name' => $this->_sSystem . '-popular', 'class' => '', 'title' => '_cmt_browse_popular', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeBrowse(this, \'popular\');'),
  647. );
  648. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-browse', 'menu_items' => $aBrowseLinks));
  649. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sBrowseType);
  650. $sBrowseType = $oMenu->getCode();
  651. }
  652. $sBrowseFilter = '';
  653. $bBrowseFilter = (int)$this->_aSystem['is_browse_filter'] == 1;
  654. if($bBrowseFilter) {
  655. $aFilterLinks = array(
  656. array('id' => $this->_sSystem . '-all', 'name' => $this->_sSystem . '-all', 'class' => '', 'title' => '_cmt_browse_all', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'all\');'),
  657. array('id' => $this->_sSystem . '-friends', 'name' => $this->_sSystem . '-friends', 'class' => '', 'title' => '_cmt_browse_friends', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'friends\');'),
  658. array('id' => $this->_sSystem . '-subscriptions', 'name' => $this->_sSystem . '-subscriptions', 'class' => '', 'title' => '_cmt_browse_subscriptions', 'target' => '_self', 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtChangeFilter(this, \'subscriptions\');')
  659. );
  660. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sSystem . '-filter', 'menu_items' => $aFilterLinks));
  661. $oMenu->setSelected('', $this->_sSystem . '-' . $this->_sBrowseFilter);
  662. $sBrowseFilter = $oMenu->getCode();
  663. }
  664. return $this->_oTemplate->parseHtmlByName('comments_controls.html', array(
  665. 'display_switcher' => $bDisplay ? $sDisplay : '',
  666. 'bx_if:is_divider_1' => array(
  667. 'condition' => $bDisplay && $bBrowseType,
  668. 'content' => array(
  669. 'style_prefix' => $this->_sStylePrefix,
  670. )
  671. ),
  672. 'browse_switcher' => $bBrowseType ? $sBrowseType : '',
  673. 'bx_if:is_divider_2' => array(
  674. 'condition' => $bBrowseType && $bBrowseFilter,
  675. 'content' => array(
  676. 'style_prefix' => $this->_sStylePrefix,
  677. )
  678. ),
  679. 'filter_switcher' => $bBrowseFilter ? $sBrowseFilter : '',
  680. ));
  681. }
  682. protected function _getActionsBox(&$aCmt, $aDp = array())
  683. {
  684. $bViewOnly = isset($aDp['view_only']) && $aDp['view_only'] === true;
  685. $bDynamicMode = isset($aDp['dynamic_mode']) && $aDp['dynamic_mode'] === true;
  686. $sMenuActions = '';
  687. if(!$bViewOnly) {
  688. $oMenuActions = BxDolMenu::getObjectInstance($this->_sMenuObjActions);
  689. $oMenuActions->setCmtsData($this, $aCmt['cmt_id']);
  690. $oMenuActions->setDynamicMode($bDynamicMode);
  691. $sMenuActions = $oMenuActions->getCode();
  692. }
  693. return $this->_oTemplate->parseHtmlByName('comment_actions.html', array(
  694. 'id' => $aCmt['cmt_id'],
  695. 'js_object' => $this->_sJsObjName,
  696. 'style_prefix' => $this->_sStylePrefix,
  697. 'menu_actions' => $sMenuActions,
  698. ));
  699. }
  700. protected function _getFormBox($sType, $aBp, $aDp)
  701. {
  702. $iCmtParentId = isset($aBp['parent_id']) ? (int)$aBp['parent_id'] : 0;
  703. $sPosition = isset($aDp['position']) ? $aDp['position'] : '';
  704. $bQuote = isset($aDp['quote']) && (bool)$aDp['quote'];
  705. $bFormMin = $iCmtParentId == 0 && isset($aDp['min_post_form']) && (bool)$aDp['min_post_form'];
  706. $sPositionSystem = $this->_aSystem['post_form_position'];
  707. if(!empty($sPosition) && $sPositionSystem != $sPosition)
  708. return '';
  709. $sClass = '';
  710. if(!empty($sPosition))
  711. $sClass .= ' ' . $this->_sStylePrefix . '-reply-' . $sPosition;
  712. if($bQuote)
  713. $sClass .= ' ' . $this->_sStylePrefix . '-reply-quote';
  714. if($bFormMin)
  715. $sClass .= ' ' . $this->_sStylePrefix . '-reply-min';
  716. if(!empty($aDp['class']))
  717. $sClass .= ' ' . $aDp['class'];
  718. $sClassBody = '';
  719. if(!empty($aDp['class_body']))
  720. $sClassBody .= ' ' . $aDp['class_body'];
  721. $aTmplVarsFormMin = array();
  722. if($bFormMin) {
  723. list($sAuthorName, $sAuthorLink, $sAuthorIcon, $sAuthorUnit) = $this->_getAuthorInfo($this->_getAuthorId());
  724. $oForm = new BxTemplFormView(array());
  725. $aInputPlaceholder = array(
  726. 'type' => 'text',
  727. 'name' => 'comment',
  728. 'caption' => '',
  729. 'attrs' => array(
  730. 'onclick' => 'javascript:' . $this->_sJsObjName . '.cmtShowForm(this)',
  731. 'placeholder' => _t($this->_aT['txt_min_form_placeholder'], $sAuthorName)
  732. ),
  733. 'value' => '',
  734. );
  735. $sClassBodyMin = '';
  736. if(!empty($aDp['class_body_min']))
  737. $sClassBodyMin .= ' ' . $aDp['class_body_min'];
  738. $aTmplVarsFormMin = array(
  739. 'js_object' => $this->_sJsObjName,
  740. 'class_body_min' => $sClassBodyMin,
  741. 'style_prefix' => $this->_sStylePrefix,
  742. 'author_unit' => $sAuthorUnit,
  743. 'placeholder' => $oForm->genRow($aInputPlaceholder)
  744. );
  745. }
  746. $aForm = $this->{'_getForm' . ucfirst($sType)}($iCmtParentId, $aDp);
  747. if(empty($aForm['form']))
  748. return !empty($aForm['msg']) && (isLogged() || $iCmtParentId != 0 || $this->getCommentsCount() > 0) ? MsgBox($aForm['msg']) : '';
  749. return $this->_oTemplate->parseHtmlByName('comment_reply_box.html', array(
  750. 'js_object' => $this->_sJsObjName,
  751. 'style_prefix' => $this->_sStylePrefix,
  752. 'class' => $sClass,
  753. 'class_body' => $sClassBody,
  754. 'bx_if:show_form_min' => array(
  755. 'condition' => $bFormMin,
  756. 'content' => $aTmplVarsFormMin
  757. ),
  758. 'form' => $aForm['form'],
  759. 'form_id' => $aForm['form_id'],
  760. ));
  761. }
  762. protected function _getFormAdd($aValues)
  763. {
  764. $iCmtAuthorId = isset($aValues['cmt_author_id']) ? (int)$aValues['cmt_author_id'] : $this->_getAuthorId();
  765. $iCmtParentId = isset($aValues['cmt_parent_id']) ? (int)$aValues['cmt_parent_id'] : 0;
  766. $oForm = $this->_getForm(BX_CMT_ACTION_POST, $iCmtParentId);
  767. $oForm->aFormAttrs['method'] = BX_DOL_FORM_METHOD_SPECIFIC;
  768. $oForm->aParams['csrf']['disable'] = true;
  769. $oForm->aInputs['cmt_parent_id']['value'] = $iCmtParentId;
  770. if(!empty($oForm->aParams['db']['submit_name'])) {
  771. $sSubmitName = false;
  772. if(is_array($oForm->aParams['db']['submit_name']))
  773. foreach($oForm->aParams['db']['submit_name'] as $sVal) {
  774. if(isset($oForm->aInputs[$sVal])) {
  775. $sSubmitName = $sVal;
  776. break;
  777. }
  778. }
  779. else
  780. $sSubmitName = $oForm->aParams['db']['submit_name'];
  781. if($sSubmitName && isset($oForm->aInputs[$sSubmitName]))
  782. $aValues[$sSubmitName] = $oForm->aInputs[$sSubmitName]['value'];
  783. }
  784. $oForm->initChecker(array(), $aValues);
  785. if(!$oForm->isSubmittedAndValid())
  786. return array('code' => 1, 'message' => '_sys_txt_error_occured');
  787. $iLevel = 0;
  788. $iCmtVisualParentId = 0;
  789. if((int)$iCmtParentId > 0) {
  790. $aParent = $this->getCommentRow($iCmtParentId);
  791. $iLevel = (int)$aParent['cmt_level'] + 1;
  792. $iCmtVisualParentId = $iLevel > $this->getMaxLevel() ? $aParent['cmt_vparent_id'] : $iCmtParentId;
  793. }
  794. $iCmtId = (int)$oForm->insert(array('cmt_vparent_id' => $iCmtVisualParentId, 'cmt_object_id' => $this->_iId, 'cmt_author_id' => $iCmtAuthorId, 'cmt_level' => $iLevel, 'cmt_time' => time()));
  795. if(!$iCmtId) {
  796. if(!$oForm->isValid())
  797. return array('code' => 1, 'message' => '_sys_txt_error_occured');
  798. else
  799. return array('code' => 2, 'message' => '_cmt_err_cannot_perform_action');
  800. }
  801. $iCmtUniqId = $this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId, $iCmtAuthorId);
  802. if($iCmtParentId) {
  803. $this->_oQuery->updateRepliesCount($iCmtParentId, 1);
  804. if(!BxDolModuleQuery::getInstance()->isEnabledByName('bx_notifications'))
  805. $this->_sendNotificationEmail($iCmtId, $iCmtParentId);
  806. }
  807. $this->_triggerComment();
  808. if($this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  809. $oMetatags->metaAdd($iCmtUniqId, $aValues['cmt_text']);
  810. $mixedResult = $this->onPostAfter($iCmtId);
  811. if($mixedResult === false)
  812. return array('code' => 2, 'message' => '_cmt_err_cannot_perform_action');
  813. return $mixedResult;
  814. }
  815. protected function _getFormPost($iCmtParentId = 0, $aDp = array())
  816. {
  817. $bCmtParentId = !empty($iCmtParentId);
  818. if(!$bCmtParentId && !$this->isPostAllowed())
  819. return array('msg' => $this->msgErrPostAllowed());
  820. if($bCmtParentId && !$this->isReplyAllowed($iCmtParentId))
  821. return array('msg' => $this->msgErrReplyAllowed());
  822. $bDynamic = isset($aDp['dynamic_mode']) && (bool)$aDp['dynamic_mode'];
  823. $bQuote = isset($aDp['quote']) && (bool)$aDp['quote'];
  824. $oForm = $this->_getForm(BX_CMT_ACTION_POST, $iCmtParentId);
  825. $oForm->aInputs['cmt_parent_id']['value'] = $iCmtParentId;
  826. if($bQuote) {
  827. $aCmtParent = $this->getCommentRow((int)$iCmtParentId);
  828. if(!empty($aCmtParent['cmt_text']))
  829. $oForm->aInputs['cmt_text']['value'] = $this->_oTemplate->parseHtmlByName('comment_quote.html', array(
  830. 'content' => $aCmtParent['cmt_text']
  831. ));
  832. }
  833. $oForm->initChecker();
  834. if($oForm->isSubmittedAndValid()) {
  835. $iCmtAuthorId = $this->_getAuthorId();
  836. $iCmtParentId = (int)$oForm->getCleanValue('cmt_parent_id');
  837. //--- Process Text ---//
  838. $sCmtText = $oForm->getCleanValue('cmt_text');
  839. $bCmtText = !empty($sCmtText);
  840. //--- Process Media ---//
  841. $aImageIds = $oForm->getCleanValue('cmt_image');
  842. $bImageIds = !empty($aImageIds) && is_array($aImageIds);
  843. if(!$bCmtText && !$bImageIds) {
  844. $oForm->aInputs['cmt_text']['error'] = _t('_Please enter characters');
  845. $oForm->setValid(false);
  846. return array('form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id);
  847. }
  848. $aParent = array();
  849. if($iCmtParentId > 0) {
  850. $aParent = $this->getCommentRow($iCmtParentId);
  851. if(empty($aParent) || !is_array($aParent)) {
  852. $iCmtParentId = 0;
  853. $oForm->setSubmittedValue('cmt_parent_id', $iCmtParentId, $oForm->aFormAttrs['method']);
  854. }
  855. }
  856. $iLevel = 0;
  857. $iCmtVisualParentId = 0;
  858. if($iCmtParentId > 0) {
  859. $iLevel = (int)$aParent['cmt_level'] + 1;
  860. $iCmtVisualParentId = $iLevel > $this->getMaxLevel() ? $aParent['cmt_vparent_id'] : $iCmtParentId;
  861. }
  862. $iCmtId = (int)$oForm->insert(array('cmt_vparent_id' => $iCmtVisualParentId, 'cmt_object_id' => $this->_iId, 'cmt_author_id' => $iCmtAuthorId, 'cmt_level' => $iLevel, 'cmt_time' => time()));
  863. if($iCmtId != 0) {
  864. $iCmtUniqId = $this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId, $iCmtAuthorId);
  865. if($this->isAttachImageEnabled())
  866. $oForm->processImages($this, 'cmt_image', $iCmtUniqId, $iCmtId, $iCmtAuthorId, true);
  867. if($iCmtParentId > 0) {
  868. $this->_oQuery->updateRepliesCount($iCmtParentId, 1);
  869. if(!BxDolModuleQuery::getInstance()->isEnabledByName('bx_notifications'))
  870. $this->_sendNotificationEmail($iCmtId, $iCmtParentId);
  871. }
  872. $this->_triggerComment();
  873. if($iCmtParentId > 0)
  874. $this->isReplyAllowed($iCmtParentId, true);
  875. else
  876. $this->isPostAllowed(true);
  877. if($this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  878. $oMetatags->metaAdd($iCmtUniqId, $sCmtText);
  879. if(($mixedResult = $this->onPostAfter($iCmtId)) !== false)
  880. return $mixedResult;
  881. }
  882. return array('msg' => _t('_cmt_err_cannot_perform_action'));
  883. }
  884. return array('form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id);
  885. }
  886. protected function _getFormEdit($iCmtId, $aDp = array())
  887. {
  888. $bDynamic = isset($aDp['dynamic_mode']) && (bool)$aDp['dynamic_mode'];
  889. $aCmt = $this->getCommentSimple($iCmtId);
  890. if(!$aCmt)
  891. return array('msg' => _t('_No such comment'));
  892. $iCmtAuthorId = $this->_getAuthorId();
  893. if(!$this->isEditAllowed($aCmt))
  894. return array('msg' => $aCmt['cmt_author_id'] == $iCmtAuthorId ? strip_tags($this->msgErrEditAllowed()) : _t('_Access denied'));
  895. $oForm = $this->_getForm(BX_CMT_ACTION_EDIT, $aCmt['cmt_id']);
  896. $oForm->initChecker($aCmt);
  897. if($oForm->isSubmittedAndValid()) {
  898. $sCmtText = $oForm->getCleanValue('cmt_text');
  899. if($oForm->update($iCmtId) !== false) {
  900. $iCmtUniqId = $this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId, (int)$aCmt['cmt_author_id']);
  901. if($this->isAttachImageEnabled())
  902. $oForm->processImages($this, 'cmt_image', $iCmtUniqId, $iCmtId, $iCmtAuthorId, false);
  903. $this->isEditAllowed($aCmt, true);
  904. if($this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  905. $oMetatags->metaAdd($iCmtUniqId, $sCmtText);
  906. if(($mixedResult = $this->onEditAfter($iCmtId)) !== false)
  907. return $mixedResult;
  908. }
  909. return array('msg' => _t('_cmt_err_cannot_perform_action'));
  910. }
  911. return array('form' => $oForm->getCode($bDynamic), 'form_id' => $oForm->id);
  912. }
  913. protected function _getForm($sAction, $iId)
  914. {
  915. $oForm = $this->_getFormObject($sAction);
  916. $oForm->setId(sprintf($oForm->getAttributeMask('id'), $sAction, $this->_sSystem, $iId));
  917. $oForm->setName(sprintf($oForm->getAttributeMask('name'), $sAction, $this->_sSystem, $iId));
  918. $oForm->aParams['db']['table'] = $this->_aSystem['table'];
  919. $oForm->aInputs['sys']['value'] = $this->_sSystem;
  920. $oForm->aInputs['id']['value'] = $this->_iId;
  921. $oForm->aInputs['action']['value'] = 'Submit' . ucfirst($sAction) . 'Form';
  922. if(!$this->isAttachImageEnabled())
  923. unset($oForm->aInputs['cmt_image']);
  924. if(isset($oForm->aInputs['cmt_text'])) {
  925. $oForm->aInputs['cmt_text']['html'] = $this->_aSystem['html'];
  926. $oForm->aInputs['cmt_text']['db']['pass'] = $this->isHtml() ? 'XssHtml' : 'XssMultiline';
  927. if(isset($oForm->aInputs['cmt_text']['checker']['func']) && strtolower($oForm->aInputs['cmt_text']['checker']['func']) == 'length') {
  928. $iCmtTextMin = (int)$this->_aSystem['chars_post_min'];
  929. $iCmtTextMax = (int)$this->_aSystem['chars_post_max'];
  930. $oForm->aInputs['cmt_text']['checker']['params'] = array($iCmtTextMin, $iCmtTextMax);
  931. $oForm->aInputs['cmt_text']['checker']['error'] = _t('_Please enter n1-n2 characters', $iCmtTextMin, $iCmtTextMax);
  932. }
  933. }
  934. if($sAction == BX_CMT_ACTION_EDIT && isset($oForm->aInputs['cmt_controls']))
  935. foreach($oForm->aInputs['cmt_controls'] as $mixedKey => $mixedValue) {
  936. if(!is_numeric($mixedKey) || empty($mixedValue['name']) || $mixedValue['name'] != 'cmt_cancel')
  937. continue;
  938. if(!isset($oForm->aInputs['cmt_controls'][$mixedKey]['attrs']))
  939. $oForm->aInputs['cmt_controls'][$mixedKey]['attrs'] = array();
  940. $oForm->aInputs['cmt_controls'][$mixedKey]['attrs']['onclick'] = $this->_sJsObjName . '.cmtEdit(this, ' . $iId . ', false)';
  941. }
  942. return $oForm;
  943. }
  944. protected function _getContent($aCmt)
  945. {
  946. $sAttachments = $this->_getAttachments($aCmt);
  947. $sReactions = '';
  948. if(($oReaction = $this->getReactionObject($aCmt['cmt_unique_id'])) !== false)
  949. $sReactions = $oReaction->getCounter(array(
  950. 'show_counter' => true
  951. ));
  952. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameItemContent, array_merge(array(
  953. 'style_prefix' => $this->_sStylePrefix,
  954. 'js_object' => $this->_sJsObjName,
  955. 'bx_if:show_attached' => array(
  956. 'condition' => !empty($sAttachments),
  957. 'content' => array(
  958. 'style_prefix' => $this->_sStylePrefix,
  959. 'attached' => $sAttachments
  960. )
  961. ),
  962. 'reactions' => $sReactions,
  963. ), $this->_getTmplVarsText($aCmt)));
  964. }
  965. protected function _getMoreLink($sCmts, $aBp = array(), $aDp = array())
  966. {
  967. $iStart = $iPerView = 0;
  968. switch($aBp['type']) {
  969. case BX_CMT_BROWSE_HEAD:
  970. case BX_CMT_BROWSE_POPULAR:
  971. case BX_CMT_BROWSE_CONNECTION:
  972. $iPerView = $aBp['per_view'];
  973. $iStart = $aBp['start'] + $iPerView;
  974. if($iStart >= $aBp['count'])
  975. return $sCmts;
  976. break;
  977. case BX_CMT_BROWSE_TAIL:
  978. $iPerView = $aBp['per_view'];
  979. $iStart = $aBp['start'] - $iPerView;
  980. if($iStart < 0) {
  981. $iPerView += $iStart;
  982. $iStart = 0;
  983. }
  984. if($iStart == 0 && $iPerView == 0)
  985. return $sCmts;
  986. break;
  987. }
  988. $sParentKey = isset($aBp['vparent_id']) ? 'vparent_id' : 'parent_id';
  989. $bRoot = (int)$aBp[$sParentKey] <= 0;
  990. $sMore = $this->_oTemplate->parseHtmlByName('comment_more.html', array(
  991. 'js_object' => $this->_sJsObjName,
  992. 'style_prefix' => $this->_sStylePrefix,
  993. 'bx_if:is_root' => array(
  994. 'condition' => $bRoot,
  995. 'content' => array()
  996. ),
  997. 'parent_id' => $aBp[$sParentKey],
  998. 'start' => $iStart,
  999. 'per_view' => $iPerView,
  1000. 'title' => _t('_cmt_load_more_' . ($aBp[$sParentKey] == 0 ? 'comments' : 'replies') . '_' . $aBp['type'])
  1001. ));
  1002. switch($aBp['type']) {
  1003. case BX_CMT_BROWSE_HEAD:
  1004. case BX_CMT_BROWSE_POPULAR:
  1005. case BX_CMT_BROWSE_CONNECTION:
  1006. $sCmts .= $sMore;
  1007. break;
  1008. case BX_CMT_BROWSE_TAIL:
  1009. $sCmts = $sMore . $sCmts;
  1010. break;
  1011. }
  1012. return $sCmts;
  1013. }
  1014. protected function _getEmpty($aDp = array())
  1015. {
  1016. $sClass = '';
  1017. if(!empty($aDp['class']))
  1018. $sClass .= ' ' . $aDp['class'];
  1019. return $this->_oTemplate->parseHtmlByName('comment_empty.html', array(
  1020. 'style_prefix' => $this->_sStylePrefix,
  1021. 'class' => $sClass,
  1022. 'content' => MsgBox(_t('_Empty'))
  1023. ));
  1024. }
  1025. protected function _getViewImagePopup()
  1026. {
  1027. $sViewImagePopupId = 'cmts-box-' . $this->_sSystem . '-' . $this->getId() . '-view-image-popup' ;
  1028. $sViewImagePopupContent = $this->_oTemplate->parseHtmlByName('popup_image.html', array(
  1029. 'image_url' => ''
  1030. ));
  1031. return BxTemplFunctions::getInstance()->transBox($sViewImagePopupId, $sViewImagePopupContent, true);
  1032. }
  1033. protected function _getAttachments($aCmt)
  1034. {
  1035. $aTmplImages = array();
  1036. if(!$this->isAttachImageEnabled())
  1037. return '';
  1038. $aFiles = $this->_oQuery->getFiles($this->_aSystem['system_id'], $aCmt['cmt_id']);
  1039. if(!empty($aFiles) && is_array($aFiles)) {
  1040. $oStorage = BxDolStorage::getObjectInstance($this->getStorageObjectName());
  1041. $oTranscoder = BxDolTranscoderImage::getObjectInstance($this->getTranscoderPreviewName());
  1042. foreach($aFiles as $aFile) {
  1043. $bImage = $oTranscoder && $oTranscoder->isMimeTypeSupported($aFile['mime_type']);
  1044. $sPreview = '';
  1045. if($oTranscoder && $bImage)
  1046. $sPreview = $oTranscoder->getFileUrl($aFile['image_id']);
  1047. if(!$sPreview)
  1048. $sPreview = $this->_oTemplate->getIconUrl($oStorage->getIconNameByFileName($aFile['file_name']));
  1049. $aTmplVarsFile = array(
  1050. 'js_object' => $this->_sJsObjName,
  1051. 'preview' => $sPreview,
  1052. 'file' => $oStorage->getFileUrlById($aFile['image_id']),
  1053. 'file_name' => $aFile['file_name'],
  1054. 'file_icon' => $oStorage->getFontIconNameByFileName($aFile['file_name']),
  1055. 'file_size' => _t_format_size($aFile['size']),
  1056. );
  1057. $aTmplImages[] = array(
  1058. 'style_prefix' => $this->_sStylePrefix,
  1059. 'bx_if:show_image' => array(
  1060. 'condition' => $bImage,
  1061. 'content' => $aTmplVarsFile
  1062. ),
  1063. 'bx_if:show_file' => array(
  1064. 'condition' => !$bImage,
  1065. 'content' => $aTmplVarsFile
  1066. ),
  1067. );
  1068. }
  1069. }
  1070. return $this->_oTemplate->parseHtmlByName('comment_attachments.html', array(
  1071. 'bx_repeat:attached' => $aTmplImages
  1072. ));
  1073. }
  1074. protected function _getTmplVarsAuthor($aCmt)
  1075. {
  1076. list($sAuthorName, $sAuthorLink, $sAuthorIcon, $sAuthorUnit) = $this->_getAuthorInfo($aCmt['cmt_author_id']);
  1077. $bAuthorIcon = !empty($sAuthorIcon);
  1078. return array(
  1079. 'author_unit' => $sAuthorUnit,
  1080. 'bx_if:show_icon' => array(
  1081. 'condition' => $bAuthorIcon,
  1082. 'content' => array(
  1083. 'author_icon' => $sAuthorIcon
  1084. )
  1085. ),
  1086. 'bx_if:show_icon_empty' => array(
  1087. 'condition' => !$bAuthorIcon,
  1088. 'content' => array()
  1089. ),
  1090. 'bx_if:show_author_link' => array(
  1091. 'condition' => !empty($sAuthorLink),
  1092. 'content' => array(
  1093. 'author_link' => $sAuthorLink,
  1094. 'author_title' => bx_html_attribute($sAuthorName),
  1095. 'author_name' => $sAuthorName
  1096. )
  1097. ),
  1098. 'bx_if:show_author_text' => array(
  1099. 'condition' => empty($sAuthorLink),
  1100. 'content' => array(
  1101. 'author_name' => $sAuthorName
  1102. )
  1103. ),
  1104. );
  1105. }
  1106. protected function _getTmplVarsText($aCmt)
  1107. {
  1108. $sText = $aCmt['cmt_text'];
  1109. $sTextMore = '';
  1110. if(!$this->isHtml()) {
  1111. $iMaxLength = (int)$this->_aSystem['chars_display_max'];
  1112. if(strlen($sText) > $iMaxLength) {
  1113. $iLength = strpos($sText, ' ', $iMaxLength);
  1114. $sTextMore = trim(substr($sText, $iLength));
  1115. $sText = trim(substr($sText, 0, $iLength));
  1116. }
  1117. }
  1118. $sText = $this->_prepareTextForOutput($sText, $aCmt['cmt_id']);
  1119. $sTextMore = $this->_prepareTextForOutput($sTextMore, $aCmt['cmt_id']);
  1120. return array(
  1121. 'text' => $sText,
  1122. 'bx_if:show_more' => array(
  1123. 'condition' => !empty($sTextMore),
  1124. 'content' => array(
  1125. 'style_prefix' => $this->_sStylePrefix,
  1126. 'js_object' => $this->_sJsObjName,
  1127. 'text_more' => $sTextMore
  1128. )
  1129. ),
  1130. );
  1131. }
  1132. }
  1133. /** @} */