BxBaseCmts.php 56 KB

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