BxDolCmts.php 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689
  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 UnaCore UNA Core
  7. * @{
  8. */
  9. bx_import('BxDolAcl');
  10. define('BX_DOL_CMT_TYPE_COMMENT', 'comment');
  11. define('BX_DOL_CMT_TYPE_REVIEW', 'review');
  12. define('BX_CMT_OLD_VOTES', 365*86400); ///< comment votes older than this number of seconds will be deleted automatically
  13. define('BX_CMT_ACTION_POST', 'post');
  14. define('BX_CMT_ACTION_EDIT', 'edit');
  15. define('BX_CMT_DISPLAY_FLAT', 'flat');
  16. define('BX_CMT_DISPLAY_THREADED', 'threaded');
  17. define('BX_CMT_BROWSE_HEAD', 'head');
  18. define('BX_CMT_BROWSE_TAIL', 'tail');
  19. define('BX_CMT_BROWSE_POPULAR', 'popular');
  20. define('BX_CMT_BROWSE_CONNECTION', 'connection');
  21. define('BX_CMT_ORDER_BY_DATE', 'date');
  22. define('BX_CMT_ORDER_BY_POPULAR', 'popular');
  23. define('BX_CMT_FILTER_ALL', 'all');
  24. define('BX_CMT_FILTER_OTHERS', 'others');
  25. define('BX_CMT_FILTER_FRIENDS', 'friends');
  26. define('BX_CMT_FILTER_SUBSCRIPTIONS', 'subscriptions');
  27. define('BX_CMT_ORDER_WAY_ASC', 'asc');
  28. define('BX_CMT_ORDER_WAY_DESC', 'desc');
  29. define('BX_CMT_PFP_TOP', 'top');
  30. define('BX_CMT_PFP_BOTTOM', 'bottom');
  31. define('BX_CMT_RATE_VALUE_PLUS', 1);
  32. define('BX_CMT_RATE_VALUE_MINUS', -1);
  33. define('BX_CMT_USAGE_BLOCK', 'block');
  34. define('BX_CMT_USAGE_INLINE', 'inline');
  35. define('BX_CMT_USAGE_DEFAULT', BX_CMT_USAGE_BLOCK);
  36. /**
  37. * Comments for any content
  38. *
  39. * Related classes:
  40. * - BxDolCmtsQuery - comments database queries
  41. * - BxBaseCmts - comments base representation
  42. * - BxTemplCmts - custom template representation
  43. *
  44. * AJAX comments for any content.
  45. * Self moderated - users rate all comments, and if comment is
  46. * below viewing treshold it is hidden by default.
  47. *
  48. * To add comments section to your module you need to add a record to 'sys_objects_cmts' table:
  49. *
  50. * - ID - autoincremented id for internal usage
  51. * - ObjectName - your unique module name, with vendor prefix, lowercase and spaces are underscored
  52. * - TableCmts - table name where comments are stored
  53. * - Html - 0 - convert new lines to <br /> on saving, 1 - standard(default) visual editor, 2 - full visual editor, 3 - mini visual editor.
  54. * - PerView - number of comments on a page
  55. * - IsRatable - 0 or 1 allow to rate comments or not
  56. * - ViewingThreshold - comment viewing treshost, if comment is below this number it is hidden by default
  57. * - IsOn - is this comment object enabled
  58. * - RootStylePrefix - toot comments style prefix, if you need root comments look different\
  59. * - ObjectVote - Vote object name to process comments' votes. May be empty if Comment Vote is not needed.
  60. * - TriggerTable - table to be updated upon each comment
  61. * - TriggerFieldId - TriggerTable table field with unique record id of
  62. * - TriggerFieldComments - TriggerTable table field with comments count, it will be updated automatically upon eaech comment
  63. * - ClassName - your custom class name if you need to override default class, this class must have the same constructor arguments
  64. * - ClassFile - file where your ClassName is stored.
  65. *
  66. * You can refer to BoonEx modules for sample record in this table.
  67. *
  68. *
  69. *
  70. * @section example Example of usage:
  71. * After filling in the table you can show comments section in any place, using the following code:
  72. *
  73. * @code
  74. * $o = new BxTemplCmts('value of ObjectName field', $iYourEntryId);
  75. * if ($o->isEnabled())
  76. * echo $o->getCommentsFirst ();
  77. * @endcode
  78. *
  79. * Please note that you never need to use BxDolCmts class directly, use BxTemplCmts instead.
  80. * Also if you override comments class with your own then make it child of BxTemplCmts class.
  81. *
  82. *
  83. *
  84. * @section acl Memberships/ACL:
  85. * - comments post
  86. * - comments edit own
  87. * - comments remove own
  88. * - comments edit all
  89. *
  90. *
  91. *
  92. * @section alerts Alerts:
  93. * Alerts type/unit - every module has own type/unit, it equals to ObjectName.
  94. *
  95. * The following alerts are rised
  96. *
  97. * - commentPost - comment was posted
  98. * - $iObjectId - entry id
  99. * - $iSenderId - author of comment
  100. * - $aExtra['comment_id'] - just added comment id
  101. *
  102. * - commentRemoved - comments was removed
  103. * - $iObjectId - entry id
  104. * - $iSenderId - comment deleter id
  105. * - $aExtra['comment_id'] - removed comment id
  106. *
  107. * - commentUpdated - comments was updated
  108. * - $iObjectId - entry id
  109. * - $iSenderId - comment deleter id
  110. * - $aExtra['comment_id'] - updated comment id
  111. *
  112. * - commentRated - comments was rated
  113. * - $iObjectId - entry id
  114. * - $iSenderId - comment rater id
  115. * - $aExtra['comment_id'] - rated comment id
  116. * - $aExtra['rate'] - comment rate 1 or -1
  117. *
  118. */
  119. class BxDolCmts extends BxDolFactory implements iBxDolReplaceable, iBxDolContentInfoService
  120. {
  121. /**
  122. * System tables which are UNITED for all
  123. * comment systems and cannot be overwritten.
  124. */
  125. public static $sTableSystems = 'sys_objects_cmts';
  126. public static $sTableIds = 'sys_cmts_ids';
  127. /**
  128. * System tables which are used by default and
  129. * can be overwritten in comment systems.
  130. * @see BxDolCmts::getTableNameImages and BxDolCmts::getTableNameImages2Entries
  131. */
  132. protected $_sTableImages = 'sys_cmts_images';
  133. protected $_sTableImages2Entries = 'sys_cmts_images2entries';
  134. protected $_sType;
  135. protected $_oQuery = null;
  136. protected $_oTemplate = null;
  137. protected $_bPostQuote;
  138. protected $_bMinPostForm;
  139. protected $_sFormObject;
  140. protected $_sFormDisplayPost;
  141. protected $_sFormDisplayEdit;
  142. protected $_sConnObjFriends;
  143. protected $_sConnObjSubscriptions;
  144. protected $_sMenuObjManage;
  145. protected $_sMenuObjActions;
  146. protected $_sMenuObjMeta;
  147. protected $_sMetatagsObj;
  148. protected $_sViewUrl = '';
  149. protected $_sBaseUrl = '';
  150. protected $_sListAnchor = '';
  151. protected $_sItemAnchor = '';
  152. protected $_sSystem = 'profile'; ///< current comment system name
  153. protected $_aSystem = array (); ///< current comments system array
  154. protected $_iId = 0; ///< obect id to be commented
  155. protected $_aT = array (); ///< an array of lang keys
  156. protected $_aMarkers = array ();
  157. protected $_sDisplayType = '';
  158. protected $_sDpSessionKey = '';
  159. protected $_iDpMaxLevel = 0;
  160. protected $_sBrowseType = '';
  161. protected $_bBrowseFilter = false;
  162. protected $_sBrowseFilter = '';
  163. protected $_sBpSessionKeyType = '';
  164. protected $_sBpSessionKeyFilter = '';
  165. protected $_aOrder = array();
  166. protected $_sSnippetLenthLiveSearch = 50;
  167. protected $_iRememberTime = 2592000;
  168. /**
  169. * Constructor
  170. * $sSystem - comments system name
  171. * $iId - obect id to be commented
  172. */
  173. protected function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  174. {
  175. parent::__construct();
  176. $this->_sType = BX_DOL_CMT_TYPE_COMMENT;
  177. $this->_aSystems = $this->getSystems();
  178. if(!isset($this->_aSystems[$sSystem]))
  179. return;
  180. $this->_sSystem = $sSystem;
  181. $this->_aSystem = $this->_aSystems[$sSystem];
  182. $this->_aSystem['is_browse_filter'] = (int)$this->_bBrowseFilter;
  183. $this->_iDpMaxLevel = (int)$this->_aSystem['number_of_levels'];
  184. $this->_sDisplayType = $this->_iDpMaxLevel == 0 ? BX_CMT_DISPLAY_FLAT : BX_CMT_DISPLAY_THREADED;
  185. $this->_sDpSessionKey = 'bx_' . $this->_sSystem . '_dp_';
  186. $this->_sBrowseType = $this->_aSystem['browse_type'];
  187. $this->_sBrowseFilter = BX_CMT_FILTER_ALL;
  188. $this->_sBpSessionKeyType = 'bx_' . $this->_sSystem . '_bpt_';
  189. $this->_sBpSessionKeyFilter = 'bx_' . $this->_sSystem . '_bpf_';
  190. $this->_aOrder = array(
  191. 'by' => BX_CMT_ORDER_BY_DATE,
  192. 'way' => BX_CMT_ORDER_WAY_ASC
  193. );
  194. list($mixedUserDp, $mixedUserBpType, $mixedUserBpFilter) = $this->_getUserChoice();
  195. if(!empty($mixedUserDp))
  196. $this->_sDisplayType = $mixedUserDp;
  197. if(!empty($mixedUserBpType))
  198. $this->_sBrowseType = $mixedUserBpType;
  199. if(!empty($mixedUserBpFilter))
  200. $this->_sBrowseFilter = $mixedUserBpFilter;
  201. $this->_sViewUrl = BX_DOL_URL_ROOT . 'cmts.php';
  202. $this->_sBaseUrl = BxDolPermalinks::getInstance()->permalink($this->_aSystem['base_url']);
  203. if(get_mb_substr($this->_sBaseUrl, 0, 4) != 'http')
  204. $this->_sBaseUrl = BX_DOL_URL_ROOT . $this->_sBaseUrl;
  205. $this->_sListAnchor = "cmts-anchor-%s-%d";
  206. $this->_sItemAnchor = "cmt-anchor-%s-%d-%d";
  207. $this->_oQuery = new BxDolCmtsQuery($this);
  208. $this->_bPostQuote = false;
  209. $this->_bMinPostForm = true;
  210. $this->_sFormObject = 'sys_comment';
  211. $this->_sFormDisplayPost = 'sys_comment_post';
  212. $this->_sFormDisplayEdit = 'sys_comment_edit';
  213. $this->_sConnObjFriends = 'sys_profiles_friends';
  214. $this->_sConnObjSubscriptions = 'sys_profiles_subscriptions';
  215. $this->_sMenuObjManage = 'sys_cmts_item_manage';
  216. $this->_sMenuObjActions = 'sys_cmts_item_actions';
  217. $this->_sMenuObjMeta = 'sys_cmts_item_meta';
  218. $this->_sMetatagsObj = 'sys_cmts';
  219. $this->_aT = array(
  220. 'block_comments_title' => '_cmt_block_comments_title',
  221. 'txt_sample_single' => '_cmt_txt_sample_comment_single',
  222. 'txt_sample_vote_single' => '_cmt_txt_sample_vote_single',
  223. 'txt_sample_reaction_single' => '_cmt_txt_sample_reaction_single',
  224. 'txt_sample_score_up_single' => '_cmt_txt_sample_score_up_single',
  225. 'txt_sample_score_down_single' => '_cmt_txt_sample_score_down_single',
  226. 'txt_min_form_placeholder' => '_cmt_txt_min_form_placeholder'
  227. );
  228. if ($iInit)
  229. $this->init($iId);
  230. if ($oTemplate)
  231. $this->_oTemplate = $oTemplate;
  232. else
  233. $this->_oTemplate = BxDolTemplate::getInstance();
  234. }
  235. /**
  236. * get comments object instanse
  237. * @param $sSys comments object name
  238. * @param $iId associated content id, where comments are postred in
  239. * @param $iInit perform initialization
  240. * @return null on error, or ready to use class instance
  241. */
  242. public static function getObjectInstance($sSys, $iId, $iInit = true, $oTemplate = false)
  243. {
  244. if(isset($GLOBALS['bxDolClasses']['BxDolCmts!' . $sSys . $iId]))
  245. return $GLOBALS['bxDolClasses']['BxDolCmts!' . $sSys . $iId];
  246. $aSystems = self::getSystems();
  247. if (!isset($aSystems[$sSys]))
  248. return null;
  249. $sClassName = 'BxTemplCmts';
  250. if(!empty($aSystems[$sSys]['class_name'])) {
  251. $sClassName = $aSystems[$sSys]['class_name'];
  252. if(!empty($aSystems[$sSys]['class_file']))
  253. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSys]['class_file']);
  254. }
  255. $o = new $sClassName($sSys, $iId, $iInit, $oTemplate);
  256. return ($GLOBALS['bxDolClasses']['BxDolCmts!' . $sSys . $iId] = $o);
  257. }
  258. public static function &getSystems ()
  259. {
  260. $sKey = 'bx_dol_cache_memory_cmts_systems';
  261. if (!isset($GLOBALS[$sKey])) {
  262. $GLOBALS[$sKey] = BxDolDb::getInstance()->fromCache('sys_objects_cmts', 'getAllWithKey', '
  263. SELECT
  264. `ID` as `system_id`,
  265. `Name` AS `name`,
  266. `Module` AS `module`,
  267. `Table` AS `table`,
  268. `CharsPostMin` AS `chars_post_min`,
  269. `CharsPostMax` AS `chars_post_max`,
  270. `CharsDisplayMax` AS `chars_display_max`,
  271. `Html` AS `html`,
  272. `PerView` AS `per_view`,
  273. `PerViewReplies` AS `per_view_replies`,
  274. `BrowseType` AS `browse_type`,
  275. `IsBrowseSwitch` AS `is_browse_switch`,
  276. `PostFormPosition` AS `post_form_position`,
  277. `NumberOfLevels` AS `number_of_levels`,
  278. `IsDisplaySwitch` AS `is_display_switch`,
  279. `IsRatable` AS `is_ratable`,
  280. `ViewingThreshold` AS `viewing_threshold`,
  281. `IsOn` AS `is_on`,
  282. `RootStylePrefix` AS `root_style_prefix`,
  283. `BaseUrl` AS `base_url`,
  284. `ObjectVote` AS `object_vote`,
  285. `ObjectReaction` AS `object_reaction`,
  286. `ObjectScore` AS `object_score`,
  287. `ObjectReport` AS `object_report`,
  288. `TriggerTable` AS `trigger_table`,
  289. `TriggerFieldId` AS `trigger_field_id`,
  290. `TriggerFieldAuthor` AS `trigger_field_author`,
  291. `TriggerFieldTitle` AS `trigger_field_title`,
  292. `TriggerFieldComments` AS `trigger_field_comments`,
  293. `ClassName` AS `class_name`,
  294. `ClassFile` AS `class_file`
  295. FROM `' . self::$sTableSystems . '`', 'name');
  296. }
  297. return $GLOBALS[$sKey];
  298. }
  299. public static function getGlobalInfo ($iUniqueId)
  300. {
  301. return BxDolDb::getInstance()->getRow("SELECT `ti`.*, `ts`.`Name` AS `system_name` FROM `" . self::$sTableIds . "` AS `ti` LEFT JOIN `" . self::$sTableSystems . "` AS `ts` ON `ti`.`system_id`=`ts`.`ID` WHERE `ti`.`id`=:id LIMIT 1", array(
  302. 'id' => (int)$iUniqueId
  303. ));
  304. }
  305. public function init ($iId)
  306. {
  307. if (empty($this->iId) && $iId)
  308. $this->setId($iId);
  309. $this->addMarkers(array(
  310. 'object_id' => $this->getId(),
  311. 'user_id' => $this->_getAuthorId()
  312. ));
  313. }
  314. public function getId ()
  315. {
  316. return $this->_iId;
  317. }
  318. public function isEnabled ()
  319. {
  320. return isset($this->_aSystem['is_on']) && $this->_aSystem['is_on'];
  321. }
  322. public function getSystemId()
  323. {
  324. return $this->_aSystem['system_id'];
  325. }
  326. public function getSystemName()
  327. {
  328. return $this->_sSystem;
  329. }
  330. public function getSystemInfo()
  331. {
  332. return $this->_aSystem;
  333. }
  334. public function getStorageObjectName()
  335. {
  336. return $this->_getFormObject()->getStorageObjectName();
  337. }
  338. public function getTranscoderPreviewName()
  339. {
  340. return $this->_getFormObject()->getTranscoderPreviewName();
  341. }
  342. public function getTableNameImages()
  343. {
  344. return $this->_sTableImages;
  345. }
  346. public function getTableNameImages2Entries()
  347. {
  348. return $this->_sTableImages2Entries;
  349. }
  350. public function getLanguageKey($sIndex)
  351. {
  352. return isset($this->_aT[$sIndex]) ? $this->_aT[$sIndex] : '';
  353. }
  354. public function getMaxLevel()
  355. {
  356. return $this->_iDpMaxLevel;
  357. }
  358. public function getOrder ()
  359. {
  360. return $this->_sOrder;
  361. }
  362. public function getPerView ($iCmtParentId = 0)
  363. {
  364. return $iCmtParentId == 0 ? $this->_aSystem['per_view'] : $this->_aSystem['per_view_replies'];
  365. }
  366. public function getViewUrl($iCmtId)
  367. {
  368. if(empty($this->_aSystem['trigger_field_title']))
  369. return '';
  370. return bx_append_url_params($this->_sViewUrl, array(
  371. 'sys' => $this->_sSystem,
  372. 'id' => $this->_iId,
  373. 'cmt_id' => $iCmtId
  374. ));
  375. }
  376. public function getBaseUrl()
  377. {
  378. return $this->_replaceMarkers($this->_sBaseUrl);
  379. }
  380. public function getListUrl()
  381. {
  382. $sBaseUrl = $this->getBaseUrl();
  383. if(empty($sBaseUrl))
  384. return '';
  385. return $sBaseUrl . $this->getListAnchor(true);
  386. }
  387. public function getItemUrl($iItemId)
  388. {
  389. $sBaseUrl = $this->getBaseUrl();
  390. if(empty($sBaseUrl))
  391. return '';
  392. return $sBaseUrl . $this->getItemAnchor($iItemId, true);
  393. }
  394. public function getListAnchor($bWithHash = false)
  395. {
  396. return ($bWithHash ? '#' : '') . sprintf($this->_sListAnchor, str_replace('_', '-', $this->getSystemName()), $this->getId());
  397. }
  398. public function getItemAnchor($iItemId, $bWithHash = false)
  399. {
  400. return ($bWithHash ? '#' : '') . sprintf($this->_sItemAnchor, str_replace('_', '-', $this->getSystemName()), $this->getId(), $iItemId);
  401. }
  402. public function getConnectionObject($sType)
  403. {
  404. $sResult = '';
  405. switch($sType) {
  406. case BX_CMT_FILTER_FRIENDS:
  407. $sResult = $this->_sConnObjFriends;
  408. break;
  409. case BX_CMT_FILTER_SUBSCRIPTIONS:
  410. $sResult = $this->_sConnObjSubscriptions;
  411. break;
  412. }
  413. return $sResult;
  414. }
  415. public function getVoteObject($iId)
  416. {
  417. if(empty($this->_aSystem['object_vote']))
  418. $this->_aSystem['object_vote'] = 'sys_cmts';
  419. $oVote = BxDolVote::getObjectInstance($this->_aSystem['object_vote'], $iId, true, $this->_oTemplate);
  420. if(!$oVote || !$oVote->isEnabled())
  421. return false;
  422. return $oVote;
  423. }
  424. public function getReactionObject($iId)
  425. {
  426. if(empty($this->_aSystem['object_reaction']))
  427. $this->_aSystem['object_reaction'] = 'sys_cmts_reactions';
  428. $oReaction = BxDolVote::getObjectInstance($this->_aSystem['object_reaction'], $iId, true, $this->_oTemplate);
  429. if(!$oReaction || !$oReaction->isEnabled())
  430. return false;
  431. return $oReaction;
  432. }
  433. public function getScoreObject($iId)
  434. {
  435. if(empty($this->_aSystem['object_score']))
  436. $this->_aSystem['object_score'] = 'sys_cmts';
  437. $oScore = BxDolScore::getObjectInstance($this->_aSystem['object_score'], $iId, true, $this->_oTemplate);
  438. if(!$oScore || !$oScore->isEnabled())
  439. return false;
  440. return $oScore;
  441. }
  442. public function getReportObject($iId)
  443. {
  444. if(empty($this->_aSystem['object_report']))
  445. $this->_aSystem['object_report'] = 'sys_cmts';
  446. $oReport = BxDolReport::getObjectInstance($this->_aSystem['object_report'], $iId, true, $this->_oTemplate);
  447. if(!$oReport || !$oReport->isEnabled())
  448. return false;
  449. return $oReport;
  450. }
  451. public function getNotificationId()
  452. {
  453. return 'cmts-notification-' . $this->_sSystem . '-' . $this->_iId;
  454. }
  455. /**
  456. * @deprecated since version 10.0.0-B3 and can be removed in later versions.
  457. */
  458. public function setTableNameFiles($sTable)
  459. {
  460. $this->_sTableImages = $sTable;
  461. $this->_oQuery->setTableNameFiles($sTable);
  462. }
  463. /**
  464. * @deprecated since version 10.0.0-B3 and can be removed in later versions.
  465. */
  466. public function setTableNameFiles2Entries($sTable)
  467. {
  468. $this->_sTableImages2Entries = $sTable;
  469. $this->_oQuery->setTableNameFiles2Entries($sTable);
  470. }
  471. public function isHtml ()
  472. {
  473. return $this->_aSystem['html'] > 0;
  474. }
  475. public function isRatable ()
  476. {
  477. return $this->_aSystem['is_ratable'];
  478. }
  479. public function isAttachImageEnabled()
  480. {
  481. return true;
  482. }
  483. /**
  484. * set id to operate with votes
  485. */
  486. public function setId ($iId)
  487. {
  488. if ($iId == $this->getId()) return;
  489. $this->_iId = $iId;
  490. }
  491. /**
  492. * Add replace markers.
  493. * @param $a array of markers as key => value
  494. * @return true on success or false on error
  495. */
  496. public function addMarkers ($a)
  497. {
  498. if (empty($a) || !is_array($a))
  499. return false;
  500. $this->_aMarkers = array_merge ($this->_aMarkers, $a);
  501. return true;
  502. }
  503. /**
  504. * Database functions
  505. */
  506. public function getQueryObject ()
  507. {
  508. return $this->_oQuery;
  509. }
  510. public function getCommentsTableName ()
  511. {
  512. return $this->_oQuery->getTableName ();
  513. }
  514. public function getObjectAuthorId ($iObjectId = 0)
  515. {
  516. if(empty($this->_aSystem['trigger_field_author']))
  517. return 0;
  518. return $this->_oQuery->getObjectAuthorId ($iObjectId ? $iObjectId : $this->getId());
  519. }
  520. public function getObjectTitle ($iObjectId = 0)
  521. {
  522. if(empty($this->_aSystem['trigger_field_title']))
  523. return '';
  524. $sTitle = $this->_oQuery->getObjectTitle ($iObjectId ? $iObjectId : $this->getId());
  525. if(get_mb_substr($sTitle, 0, 1) == '_')
  526. $sTitle = _t($sTitle);
  527. return $sTitle;
  528. }
  529. public function getCommentsCountAll ($iObjectId = 0)
  530. {
  531. return $this->_oQuery->getCommentsCountAll ($iObjectId ? $iObjectId : $this->getId());
  532. }
  533. public function getCommentsCount ($iObjectId = 0, $iCmtVParentId = -1, $sFilter = '')
  534. {
  535. return $this->_oQuery->getCommentsCount ($iObjectId ? $iObjectId : $this->getId(), $iCmtVParentId, $this->_getAuthorId(), $sFilter);
  536. }
  537. public function getCommentsArray ($iVParentId, $sFilter, $aOrder, $iStart = 0, $iCount = -1)
  538. {
  539. return $this->_oQuery->getComments ($this->getId(), $iVParentId, $this->_getAuthorId(), $sFilter, $aOrder, $iStart, $iCount);
  540. }
  541. /**
  542. * Get comment's short info.
  543. */
  544. public function getCommentSimple ($iCmtId)
  545. {
  546. return $this->_oQuery->getCommentSimple ($this->getId(), $iCmtId);
  547. }
  548. /**
  549. * Get comment's full info.
  550. */
  551. public function getCommentRow ($iCmtId)
  552. {
  553. return $this->_oQuery->getComment ($this->getId(), $iCmtId);
  554. }
  555. public function onObjectDelete ($iObjectId = 0)
  556. {
  557. // delete comments
  558. $aFiles = $aCmtIds = array();
  559. $this->_oQuery->deleteObjectComments ($iObjectId ? $iObjectId : $this->getId(), $aFiles, $aCmtIds);
  560. // delete meta info
  561. $this->deleteMetaInfo($aCmtIds);
  562. // delete files
  563. if ($aFiles) {
  564. $oStorage = BxDolStorage::getObjectInstance($this->getStorageObjectName());
  565. if ($oStorage)
  566. $oStorage->queueFilesForDeletion($aFiles);
  567. }
  568. }
  569. public static function onAuthorDelete ($iAuthorId)
  570. {
  571. $aSystems = self::getSystems();
  572. foreach($aSystems as $sSystem => $aSystem) {
  573. $o = self::getObjectInstance($sSystem, 0);
  574. $oQuery = $o->getQueryObject();
  575. // delete comments
  576. $aFiles = $aCmtIds = array ();
  577. $oQuery->deleteAuthorComments($iAuthorId, $aFiles, $aCmtIds);
  578. // delete meta info
  579. $o->deleteMetaInfo($aCmtIds);
  580. // delete files
  581. $oStorage = BxDolStorage::getObjectInstance($o->getStorageObjectName());
  582. if ($oStorage)
  583. $oStorage->queueFilesForDeletion($aFiles);
  584. }
  585. return true;
  586. }
  587. public static function onModuleEnable ($sModuleName)
  588. {
  589. $aSystems = self::getSystems();
  590. foreach($aSystems as $sSystem => $aSystem) {
  591. if ($sModuleName !== $aSystem['module'])
  592. continue;
  593. $o = self::getObjectInstance($sSystem, 0);
  594. $o->registerTranscoders();
  595. }
  596. return true;
  597. }
  598. public static function onModuleDisable ($sModuleName)
  599. {
  600. $aSystems = self::getSystems();
  601. foreach($aSystems as $sSystem => $aSystem) {
  602. if ($sModuleName !== $aSystem['module'])
  603. continue;
  604. $o = self::getObjectInstance($sSystem, 0);
  605. $o->unregisterTranscoders();
  606. }
  607. return true;
  608. }
  609. public static function onModuleUninstall ($sModuleName, &$iFiles = null)
  610. {
  611. $aSystems = self::getSystems();
  612. foreach($aSystems as $sSystem => $aSystem) {
  613. if ($sModuleName !== $aSystem['module'])
  614. continue;
  615. $o = self::getObjectInstance($sSystem, 0);
  616. $oQuery = $o->getQueryObject();
  617. // delete comments
  618. $aFiles = $aCmtIds = array ();
  619. $oQuery->deleteAll($aSystem['system_id'], $aFiles, $aCmtIds);
  620. // delete meta info
  621. $o->deleteMetaInfo($aCmtIds);
  622. // delete files
  623. $oStorage = BxDolStorage::getObjectInstance($o->getStorageObjectName());
  624. if ($oStorage && $aFiles)
  625. $oStorage->queueFilesForDeletion($aFiles);
  626. if (null !== $iFiles)
  627. $iFiles += count($aFiles);
  628. }
  629. return true;
  630. }
  631. public function deleteMetaInfo ($mixedCmtId)
  632. {
  633. if(!is_array($mixedCmtId))
  634. $mixedCmtId = array($mixedCmtId);
  635. $oMetatags = false;
  636. if(!empty($this->_sMetatagsObj))
  637. $oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj);
  638. foreach($mixedCmtId as $iCmtId) {
  639. if($oMetatags)
  640. $oMetatags->onDeleteContent($this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId));
  641. $this->_oQuery->deleteCmtIds($this->_aSystem['system_id'], $iCmtId);
  642. }
  643. }
  644. /**
  645. * Permissions functions
  646. */
  647. public function checkAction ($sAction, $isPerformAction = false)
  648. {
  649. $iId = $this->_getAuthorId();
  650. $a = checkActionModule($iId, $sAction, 'system', $isPerformAction);
  651. return $a[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  652. }
  653. public function checkActionErrorMsg ($sAction)
  654. {
  655. $iId = $this->_getAuthorId();
  656. $a = checkActionModule($iId, $sAction, 'system');
  657. return $a[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $a[CHECK_ACTION_MESSAGE] : '';
  658. }
  659. public function isViewAllowed ($isPerformAction = false)
  660. {
  661. $mixedResult = BxDolService::call($this->_aSystem['module'], 'check_allowed_comments_view', array($this->getId(), $this->getSystemName()));
  662. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  663. return $mixedResult;
  664. return CHECK_ACTION_RESULT_ALLOWED;
  665. }
  666. public function isVoteAllowed ($aCmt, $isPerformAction = false)
  667. {
  668. if(!$this->isRatable())
  669. return false;
  670. $oVote = $this->getVoteObject($aCmt['cmt_id']);
  671. if($oVote === false)
  672. return false;
  673. $iUserId = (int)$this->_getAuthorId();
  674. if($iUserId == 0)
  675. return false;
  676. if(isAdmin())
  677. return true;
  678. return $oVote->isAllowedVote($isPerformAction);
  679. }
  680. public function isScoreAllowed ($aCmt, $isPerformAction = false)
  681. {
  682. if(!$this->isRatable())
  683. return false;
  684. $oScore = $this->getScoreObject($aCmt['cmt_id']);
  685. if($oScore === false)
  686. return false;
  687. $iUserId = (int)$this->_getAuthorId();
  688. if($iUserId == 0)
  689. return false;
  690. if(isAdmin())
  691. return true;
  692. return $oScore->isAllowedVote($isPerformAction);
  693. }
  694. public function isReportAllowed ($aCmt, $isPerformAction = false)
  695. {
  696. $oReport = $this->getReportObject($aCmt['cmt_id']);
  697. if($oReport === false)
  698. return false;
  699. $iUserId = (int)$this->_getAuthorId();
  700. if($iUserId == 0)
  701. return false;
  702. if(isAdmin())
  703. return true;
  704. return $oReport->isAllowedReport($isPerformAction);
  705. }
  706. public function isPostAllowed ($isPerformAction = false)
  707. {
  708. $mixedResult = BxDolService::call($this->_aSystem['module'], 'check_allowed_comments_post', array($this->getId(), $this->getSystemName()));
  709. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  710. return false;
  711. return $this->checkAction ('comments post', $isPerformAction);
  712. }
  713. public function msgErrPostAllowed ()
  714. {
  715. return $this->checkActionErrorMsg('comments post');
  716. }
  717. /**
  718. * Determines whether a 'reply' action allowed or not.
  719. * @param integer or array $mixedCmt - ID or an array which describes the comment to be replied
  720. * @param boolean $isPerformAction
  721. * @return boolean
  722. */
  723. public function isReplyAllowed ($mixedCmt, $isPerformAction = false)
  724. {
  725. return $this->isPostAllowed($isPerformAction);
  726. }
  727. public function msgErrReplyAllowed ()
  728. {
  729. return $this->msgErrPostAllowed ();
  730. }
  731. public function isQuoteAllowed ($mixedCmt, $isPerformAction = false)
  732. {
  733. return $this->_bPostQuote && $this->isReplyAllowed ($mixedCmt, $isPerformAction);
  734. }
  735. public function msgErrQuoteAllowed ()
  736. {
  737. return $this->msgErrReplyAllowed ();
  738. }
  739. public function isEditAllowed ($aCmt, $isPerformAction = false)
  740. {
  741. if(isAdmin())
  742. return true;
  743. if($this->isEditAllowedAll($isPerformAction))
  744. return true;
  745. $mixedResult = BxDolService::call($this->_aSystem['module'], 'check_allowed_comments_post', array($this->getId(), $this->getSystemName()));
  746. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  747. return false;
  748. return abs($aCmt['cmt_author_id']) == $this->_getAuthorId() && $this->checkAction ('comments edit own', $isPerformAction);
  749. }
  750. public function msgErrEditAllowed ()
  751. {
  752. return $this->checkActionErrorMsg ('comments edit own');
  753. }
  754. public function isEditAllowedAll ($isPerformAction = false)
  755. {
  756. return $this->checkAction('comments edit all', $isPerformAction);
  757. }
  758. public function isRemoveAllowed ($aCmt, $isPerformAction = false)
  759. {
  760. if(isAdmin())
  761. return true;
  762. if($this->isRemoveAllowedAll($isPerformAction))
  763. return true;
  764. return abs($aCmt['cmt_author_id']) == $this->_getAuthorId() && $this->checkAction ('comments remove own', $isPerformAction);
  765. }
  766. public function isRemoveAllowedAll ($isPerformAction = false)
  767. {
  768. return $this->checkAction ('comments remove all', $isPerformAction);
  769. }
  770. public function msgErrRemoveAllowed ()
  771. {
  772. return $this->checkActionErrorMsg('comments remove own');
  773. }
  774. public function isMoreAllowed ($aCmt, $isPerformAction = false)
  775. {
  776. $oMenuManage = BxDolMenu::getObjectInstance($this->_sMenuObjManage);
  777. $oMenuManage->setCmtsData($this, $aCmt['cmt_id']);
  778. return $oMenuManage->isVisible();
  779. }
  780. public function isModerator()
  781. {
  782. return $this->isEditAllowedAll() || $this->isRemoveAllowedAll();
  783. }
  784. /**
  785. * Actions functions
  786. */
  787. public function actionGetFormPost ()
  788. {
  789. if (!$this->isEnabled())
  790. return '';
  791. $iCmtParentId = bx_get('CmtParent');
  792. $iCmtParentId = $iCmtParentId !== false ? bx_process_input($iCmtParentId, BX_DATA_INT) : 0;
  793. $sCmtBrowse = bx_get('CmtBrowse');
  794. $sCmtBrowse = $sCmtBrowse !== false ? bx_process_input($sCmtBrowse, BX_DATA_TEXT) : '';
  795. $sCmtDisplay = bx_get('CmtDisplay');
  796. $sCmtDisplay = $sCmtDisplay !== false ? bx_process_input($sCmtDisplay, BX_DATA_TEXT) : '';
  797. $bQuote = bx_get('CmtQuote');
  798. $bQuote = $bQuote !== false ? bx_process_input($bQuote, BX_DATA_INT) == 1 : false;
  799. $bMinPostForm = bx_get('CmtMinPostForm');
  800. $bMinPostForm = $bMinPostForm !== false ? bx_process_input($bMinPostForm, BX_DATA_INT) == 1 : $this->_bMinPostForm;
  801. return $this->getFormBoxPost(array('parent_id' => $iCmtParentId, 'type' => $sCmtBrowse), array('type' => $sCmtDisplay, 'dynamic_mode' => true, 'quote' => $bQuote, 'min_post_form' => $bMinPostForm));
  802. }
  803. public function actionGetFormEdit ()
  804. {
  805. if (!$this->isEnabled())
  806. return echoJson(array());
  807. $iCmtId = bx_process_input(bx_get('Cmt'), BX_DATA_INT);
  808. echoJson($this->getFormEdit($iCmtId, array('dynamic_mode' => true)));
  809. }
  810. public function actionGetCmt ()
  811. {
  812. if (!$this->isEnabled())
  813. return '';
  814. if($this->isViewAllowed() !== CHECK_ACTION_RESULT_ALLOWED)
  815. return '';
  816. $iCmtId = bx_process_input($_REQUEST['Cmt'], BX_DATA_INT);
  817. $sCmtBrowse = isset($_REQUEST['CmtBrowse']) ? bx_process_input($_REQUEST['CmtBrowse'], BX_DATA_TEXT) : '';
  818. $sCmtDisplay = isset($_REQUEST['CmtDisplay']) ? bx_process_input($_REQUEST['CmtDisplay'], BX_DATA_TEXT) : '';
  819. $aCmt = $this->getCommentRow($iCmtId);
  820. echoJson(array(
  821. 'parent_id' => $aCmt['cmt_parent_id'],
  822. 'vparent_id' => $aCmt['cmt_vparent_id'],
  823. 'content' => $this->getComment($aCmt, array('type' => $sCmtBrowse), array('type' => $sCmtDisplay, 'dynamic_mode' => true))
  824. ));
  825. }
  826. public function actionGetCmts ()
  827. {
  828. if (!$this->isEnabled())
  829. return '';
  830. if($this->isViewAllowed() !== CHECK_ACTION_RESULT_ALLOWED)
  831. return '';
  832. $aBp = $aDp = array();
  833. $this->_getParams($aBp, $aDp);
  834. $aDp['dynamic_mode'] = true;
  835. return $this->getComments($aBp, $aDp);
  836. }
  837. public function actionSubmitPostForm()
  838. {
  839. if(!$this->isEnabled())
  840. return echoJson(array());
  841. $iCmtParentId = 0;
  842. if(bx_get('cmt_parent_id') !== false)
  843. $iCmtParentId = bx_process_input(bx_get('cmt_parent_id'), BX_DATA_INT);
  844. echoJson($this->getFormPost($iCmtParentId, array('dynamic_mode' => true)));
  845. }
  846. public function actionSubmitEditForm()
  847. {
  848. if (!$this->isEnabled()) {
  849. echoJson(array());
  850. return;
  851. };
  852. $iCmtId = 0;
  853. if(bx_get('cmt_id') !== false)
  854. $iCmtId = bx_process_input(bx_get('cmt_id'), BX_DATA_INT);
  855. echoJson($this->getFormEdit($iCmtId, array('dynamic_mode' => true)));
  856. }
  857. public function actionRemove()
  858. {
  859. if (!$this->isEnabled()) {
  860. echoJson(array());
  861. return;
  862. };
  863. $iCmtId = 0;
  864. if(bx_get('Cmt') !== false)
  865. $iCmtId = bx_process_input(bx_get('Cmt'), BX_DATA_INT);
  866. echoJson($this->remove($iCmtId));
  867. }
  868. public function remove($iCmtId)
  869. {
  870. $aCmt = $this->_oQuery->getCommentsBy(array('type' => 'id', 'id' => $iCmtId));
  871. if(!$aCmt)
  872. return array('msg' => _t('_No such comment'));
  873. $iObjId = $this->getId();
  874. if(!$iObjId){
  875. $this->setId($aCmt['cmt_object_id']);
  876. $iObjId = $aCmt['cmt_object_id'];
  877. }
  878. $iObjAthrId = $this->getObjectAuthorId($iObjId);
  879. if($aCmt['cmt_replies'] > 0) {
  880. if(!$this->isModerator())
  881. return array('msg' => _t('_Can not delete comments with replies'));
  882. $aReplies = $this->_oQuery->getCommentsBy(array('type' => 'parent_id', 'parent_id' => $iCmtId));
  883. foreach($aReplies as $aReply)
  884. $this->remove($aReply['cmt_id']);
  885. }
  886. $iPerformerId = $this->_getAuthorId();
  887. if(!$this->isRemoveAllowed($aCmt))
  888. return array('msg' => $aCmt['cmt_author_id'] == $iPerformerId ? strip_tags($this->msgErrRemoveAllowed()) : _t('_Access denied'));
  889. if($this->_oQuery->removeComment($iObjId, $iCmtId, $aCmt['cmt_parent_id'])) {
  890. $this->_triggerComment();
  891. $oStorage = BxDolStorage::getObjectInstance($this->getStorageObjectName());
  892. $aImages = $this->_oQuery->getFiles($this->_aSystem['system_id'], $iCmtId);
  893. foreach($aImages as $aImage)
  894. $oStorage->deleteFile($aImage['image_id']);
  895. $this->_oQuery->deleteImages($this->_aSystem['system_id'], $iCmtId);
  896. $this->isRemoveAllowed($aCmt, true);
  897. $this->deleteMetaInfo ($iCmtId);
  898. bx_alert($this->_sSystem, 'commentRemoved', $iObjId, $iPerformerId, array(
  899. 'object_author_id' => $iObjAthrId,
  900. 'comment_id' => $iCmtId,
  901. 'comment_author_id' => $aCmt['cmt_author_id']
  902. ));
  903. bx_alert('comment', 'deleted', $iCmtId, $iPerformerId, array(
  904. 'object_system' => $this->_sSystem,
  905. 'object_id' => $iObjId,
  906. 'object_author_id' => $iObjAthrId,
  907. 'comment_author_id' => $aCmt['cmt_author_id']
  908. ));
  909. if(!empty($aCmt['cmt_parent_id'])) {
  910. $iCmtPrntId = (int)$aCmt['cmt_parent_id'];
  911. $aCmtPrnt = $this->_oQuery->getCommentSimple($iObjId, $iCmtPrntId);
  912. if(!empty($aCmtPrnt) && is_array($aCmtPrnt)) {
  913. bx_alert($this->_sSystem, 'replyRemoved', $iCmtPrntId, $iPerformerId, array(
  914. 'object_id' => $iObjId,
  915. 'object_author_id' => $iObjAthrId,
  916. 'parent_author_id' => $aCmtPrnt['cmt_author_id'],
  917. 'comment_id' => $iCmtId,
  918. 'comment_author_id' => $aCmt['cmt_author_id'],
  919. ));
  920. bx_alert('reply', 'deleted', $iCmtId, $iPerformerId, array(
  921. 'object_system' => $this->_sSystem,
  922. 'object_id' => $iObjId,
  923. 'object_author_id' => $iObjAthrId,
  924. 'parent_id' => $iCmtPrntId,
  925. 'parent_author_id' => $aCmtPrnt['cmt_author_id'],
  926. 'comment_author_id' => $aCmt['cmt_author_id'],
  927. ));
  928. }
  929. }
  930. return array('id' => $iCmtId);
  931. }
  932. return array('msg' => _t('_cmt_err_cannot_perform_action'));
  933. }
  934. public function actionResumeLiveUpdate()
  935. {
  936. $sKey = $this->getNotificationId();
  937. bx_import('BxDolSession');
  938. BxDolSession::getInstance()->unsetValue($sKey);
  939. }
  940. public function actionPauseLiveUpdate()
  941. {
  942. $sKey = $this->getNotificationId();
  943. bx_import('BxDolSession');
  944. BxDolSession::getInstance()->setValue($sKey, 1);
  945. }
  946. public function onPostAfter($iCmtId)
  947. {
  948. $iObjId = (int)$this->getId();
  949. $iObjAthrId = $this->getObjectAuthorId($iObjId);
  950. $aCmt = $this->_oQuery->getCommentSimple($iObjId, $iCmtId);
  951. if(empty($aCmt) || !is_array($aCmt))
  952. return false;
  953. $iCmtPrntId = (int)$aCmt['cmt_parent_id'];
  954. $iPerformerId = (int)$aCmt['cmt_author_id'];
  955. bx_alert($this->_sSystem, 'commentPost', $iObjId, $iPerformerId, array(
  956. 'object_author_id' => $iObjAthrId,
  957. 'comment_id' => $iCmtId,
  958. 'comment_author_id' => $aCmt['cmt_author_id'],
  959. 'comment_text' => $aCmt['cmt_text']
  960. ));
  961. bx_alert('comment', 'added', $iCmtId, $iPerformerId, array(
  962. 'object_system' => $this->_sSystem,
  963. 'object_id' => $iObjId,
  964. 'object_author_id' => $iObjAthrId,
  965. 'comment_author_id' => $aCmt['cmt_author_id'],
  966. 'comment_text' => $aCmt['cmt_text']
  967. ));
  968. if(!empty($iCmtPrntId)) {
  969. $aCmtPrnt = $this->_oQuery->getCommentSimple($iObjId, $iCmtPrntId);
  970. if(!empty($aCmtPrnt) && is_array($aCmtPrnt)) {
  971. bx_alert($this->_sSystem, 'replyPost', $iCmtPrntId, $iPerformerId, array(
  972. 'object_id' => $iObjId,
  973. 'object_author_id' => $iObjAthrId,
  974. 'parent_author_id' => $aCmtPrnt['cmt_author_id'],
  975. 'comment_id' => $iCmtId,
  976. 'comment_author_id' => $aCmt['cmt_author_id'],
  977. 'comment_text' => $aCmt['cmt_text']
  978. ));
  979. bx_alert('comment', 'replied', $iCmtId, $iPerformerId, array(
  980. 'object_system' => $this->_sSystem,
  981. 'object_id' => $iObjId,
  982. 'object_author_id' => $iObjAthrId,
  983. 'parent_id' => $iCmtPrntId,
  984. 'parent_author_id' => $aCmtPrnt['cmt_author_id'],
  985. 'comment_author_id' => $aCmt['cmt_author_id'],
  986. 'comment_text' => $aCmt['cmt_text']
  987. ));
  988. }
  989. }
  990. return array('id' => $iCmtId, 'parent_id' => $iCmtPrntId);
  991. }
  992. public function onEditAfter($iCmtId)
  993. {
  994. $iObjId = (int)$this->getId();
  995. $iObjAthrId = $this->getObjectAuthorId($iObjId);
  996. $aCmt = $this->_oQuery->getCommentSimple($iObjId, $iCmtId);
  997. if(empty($aCmt) || !is_array($aCmt))
  998. return false;
  999. $iPerformerId = $this->_getAuthorId();
  1000. bx_alert($this->_sSystem, 'commentUpdated', $iObjId, $iPerformerId, array(
  1001. 'object_author_id' => $iObjAthrId,
  1002. 'comment_id' => $iCmtId,
  1003. 'comment_author_id' => $aCmt['cmt_author_id'],
  1004. 'comment_text' => $aCmt['cmt_text']
  1005. ));
  1006. bx_alert('comment', 'edited', $iCmtId, $iPerformerId, array(
  1007. 'object_system' => $this->_sSystem,
  1008. 'object_id' => $iObjId,
  1009. 'object_author_id' => $iObjAthrId,
  1010. 'comment_author_id' => $aCmt['cmt_author_id'],
  1011. 'comment_text' => $aCmt['cmt_text']
  1012. ));
  1013. return array('id' => $iCmtId, 'text' => $this->_prepareTextForOutput($aCmt['cmt_text'], $iCmtId));
  1014. }
  1015. public function serviceGetAuthor ($iContentId)
  1016. {
  1017. $aComment = $this->_oQuery->getCommentsBy(array('type' => 'id', 'id' => $iContentId));
  1018. $this->setId($aComment['cmt_object_id']);
  1019. return $aComment['cmt_author_id'];
  1020. }
  1021. public function serviceGetDateAdded ($iContentId)
  1022. {
  1023. $aComment = $this->_oQuery->getCommentsBy(array('type' => 'id', 'id' => $iContentId));
  1024. $this->setId($aComment['cmt_object_id']);
  1025. return $aComment['cmt_time'];
  1026. }
  1027. public function serviceGetDateChanged ($iContentId)
  1028. {
  1029. return 0;
  1030. }
  1031. public function serviceGetLink ($iContentId)
  1032. {
  1033. $aComment = $this->_oQuery->getCommentsBy(array('type' => 'id', 'id' => $iContentId));
  1034. $this->setId($aComment['cmt_object_id']);
  1035. return $this->getViewUrl($iContentId);
  1036. }
  1037. public function serviceGetTitle ($iContentId)
  1038. {
  1039. return '';
  1040. }
  1041. public function serviceGetText ($iContentId)
  1042. {
  1043. $aComment = $this->_oQuery->getCommentsBy(array('type' => 'id', 'id' => $iContentId));
  1044. $this->setId($aComment['cmt_object_id']);
  1045. return $aComment['cmt_text'];
  1046. }
  1047. public function serviceGetThumb ($iContentId)
  1048. {
  1049. return '';
  1050. }
  1051. public function serviceGetInfo ($iContentId, $bSearchableFieldsOnly = true)
  1052. {
  1053. $aComment = $this->_oQuery->getCommentsBy(array('type' => 'id', 'id' => $iContentId));
  1054. $this->setId($aComment['cmt_object_id']);
  1055. return BxDolContentInfo::formatFields($aComment);
  1056. }
  1057. public function serviceGetSearchResultUnit ($iContentId, $sUnitTemplate = '')
  1058. {
  1059. $aComment = $this->_oQuery->getCommentsBy(array('type' => 'id', 'id' => $iContentId));
  1060. if(empty($aComment) || !is_array($aComment))
  1061. return '';
  1062. $this->setId($aComment['cmt_object_id']);
  1063. return $this->getComment($aComment, array(), array('type' => BX_CMT_DISPLAY_FLAT, 'view_only' => true));
  1064. }
  1065. public function serviceGetAll ($aParams = array())
  1066. {
  1067. if(empty($aParams) || !is_array($aParams))
  1068. $aParams = array('type' => 'all');
  1069. return $this->_oQuery->getCommentsBy($aParams);
  1070. }
  1071. public function serviceGetSearchableFieldsExtended($aInputsAdd = array())
  1072. {
  1073. $oForm = BxDolForm::getObjectInstance('sys_comment', 'sys_comment_post', $this->_oTemplate);
  1074. if(!$oForm)
  1075. return array();
  1076. $aSrchNamesExcept = array();
  1077. $aSrchCaptionsSystem = array(
  1078. 'cmt_author_id' => '_sys_form_comment_input_caption_system_cmt_author_id',
  1079. 'cmt_text' => '_sys_form_comment_input_caption_system_cmt_text'
  1080. );
  1081. $aSrchCaptions = array(
  1082. 'cmt_author_id' => '_sys_form_comment_input_caption_cmt_author_id',
  1083. 'cmt_text' => '_sys_form_comment_input_caption_cmt_text'
  1084. );
  1085. $aResult = array(
  1086. 'cmt_author_id' => array(
  1087. 'type' => 'text_auto',
  1088. 'caption_system' => $aSrchCaptionsSystem['cmt_author_id'],
  1089. 'caption' => $aSrchCaptions['cmt_author_id'],
  1090. 'info' => '',
  1091. 'value' => '',
  1092. 'values' => '',
  1093. 'pass' => ''
  1094. )
  1095. );
  1096. $aInputs = $oForm->aInputs;
  1097. if(!empty($aInputsAdd) && is_array($aInputsAdd))
  1098. $aInputs = array_merge($aInputs, $aInputsAdd);
  1099. foreach($aInputs as $aInput)
  1100. if(in_array($aInput['type'], BxDolSearchExtended::$SEARCHABLE_TYPES) && !in_array($aInput['name'], $aSrchNamesExcept))
  1101. $aResult[$aInput['name']] = array(
  1102. 'type' => $aInput['type'],
  1103. 'caption_system' => !empty($aInput['caption_system_src']) ? $aInput['caption_system_src'] : '',
  1104. 'caption' => !empty($aInput['caption_src']) ? $aInput['caption_src'] : (!empty($aSrchCaptions[$aInput['name']]) ? $aSrchCaptions[$aInput['name']] : ''),
  1105. 'info' => !empty($aInput['info_src']) ? $aInput['info_src'] : '',
  1106. 'value' => !empty($aInput['value']) ? $aInput['value'] : '',
  1107. 'values' => !empty($aInput['values_src']) ? $aInput['values_src'] : '',
  1108. 'pass' => !empty($aInput['db']['pass']) ? $aInput['db']['pass'] : ''
  1109. );
  1110. return $aResult;
  1111. }
  1112. /**
  1113. * Overwrite this method and register transcoder(s) if comments object uses custom transcoder(s),
  1114. * which differs from default one 'sys_cmts_images_preview'
  1115. */
  1116. public function registerTranscoders()
  1117. {}
  1118. /**
  1119. * Overwrite this method and unregister transcoder(s) if comments object uses custom transcoder(s),
  1120. * which differs from default one 'sys_cmts_images_preview'
  1121. */
  1122. public function unregisterTranscoders()
  1123. {}
  1124. public function serviceGetSearchResultExtended($aParams, $iStart = 0, $iPerPage = 0)
  1125. {
  1126. if(empty($aParams) || !is_array($aParams))
  1127. return array();
  1128. return $this->_oQuery->getCommentsBy(array('type' => 'search_ids', 'search_params' => $aParams, 'start' => $iStart, 'per_page' => $iPerPage));
  1129. }
  1130. public function getAuthorInfo($iAuthorId = 0)
  1131. {
  1132. return $this->_getAuthorInfo($iAuthorId);
  1133. }
  1134. /**
  1135. * Internal functions
  1136. */
  1137. protected function _getAuthorId ()
  1138. {
  1139. return isMember() ? bx_get_logged_profile_id() : 0;
  1140. }
  1141. protected function _getAuthorPassword ()
  1142. {
  1143. return isMember() ? $_COOKIE['memberPassword'] : "";
  1144. }
  1145. protected function _getAuthorIp ()
  1146. {
  1147. return getVisitorIP();
  1148. }
  1149. protected function _getAuthorInfo($iAuthorId = 0)
  1150. {
  1151. $iUserId = $this->_getAuthorId();
  1152. $iAuthorId = (int)$iAuthorId;
  1153. $oProfile = $this->_getAuthorObject($iAuthorId);
  1154. return array(
  1155. $oProfile->getDisplayName(),
  1156. $oProfile->getUrl(),
  1157. $oProfile->getThumb(),
  1158. $oProfile->getUnit(0, array('template' => 'unit_wo_info'))
  1159. );
  1160. }
  1161. protected function _getAuthorObject($iAuthorId = 0)
  1162. {
  1163. return BxDolProfile::getInstanceMagic($iAuthorId);
  1164. }
  1165. protected function _getFormObject($sAction = BX_CMT_ACTION_POST)
  1166. {
  1167. $sDisplayName = '_sFormDisplay' . ucfirst($sAction);
  1168. return BxDolForm::getObjectInstance($this->_sFormObject, $this->$sDisplayName, false, $this->_sSystem);
  1169. }
  1170. protected function _getParams(&$aBp, &$aDp)
  1171. {
  1172. //--- Process 'Browse' params.
  1173. $aBp['parent_id'] = isset($aBp['parent_id']) ? (int)$aBp['parent_id'] : 0;
  1174. $aBp['vparent_id'] = isset($aBp['vparent_id']) ? (int)$aBp['vparent_id'] : 0;
  1175. if(bx_get('CmtParent') !== false)
  1176. $aBp['vparent_id'] = bx_process_input(bx_get('CmtParent'), BX_DATA_INT);
  1177. $aBp['type'] = isset($aBp['type']) ? $aBp['type'] : '';
  1178. if(bx_get('CmtBrowse') !== false)
  1179. $aBp['type'] = bx_process_input(bx_get('CmtBrowse'), BX_DATA_TEXT);
  1180. $aBp['filter'] = isset($aBp['filter']) ? $aBp['filter'] : '';
  1181. if(bx_get('CmtFilter') !== false)
  1182. $aBp['filter'] = bx_process_input(bx_get('CmtFilter'), BX_DATA_TEXT);
  1183. $aBp['start'] = isset($aBp['start']) ? (int)$aBp['start'] : -1;
  1184. if(bx_get('CmtStart') !== false)
  1185. $aBp['start'] = bx_process_input($_REQUEST['CmtStart'], BX_DATA_INT);
  1186. $aBp['per_view'] = isset($aBp['per_view']) ? (int)$aBp['per_view'] : -1;
  1187. if(bx_get('CmtPerView') !== false)
  1188. $aBp['per_view'] = bx_process_input($_REQUEST['CmtPerView'], BX_DATA_INT);
  1189. //--- Process 'Display' params.
  1190. $aDp['type'] = isset($aDp['type']) ? $aDp['type'] : '';
  1191. if(bx_get('CmtDisplay') !== false)
  1192. $aDp['type'] = bx_process_input($_REQUEST['CmtDisplay'], BX_DATA_TEXT);
  1193. $aDp['blink'] = isset($aDp['blink']) ? $aDp['blink'] : '';
  1194. if(bx_get('CmtBlink') !== false)
  1195. $aDp['blink'] = bx_process_input($_REQUEST['CmtBlink'], BX_DATA_TEXT);
  1196. $aDp['quote'] = isset($aDp['quote']) ? (int)$aDp['quote'] : 0;
  1197. if(bx_get('CmtQuote') !== false)
  1198. $aDp['quote'] = bx_process_input($_REQUEST['CmtQuote'], BX_DATA_INT);
  1199. $aDp['min_post_form'] = isset($aDp['min_post_form']) ? (bool)$aDp['min_post_form'] : $this->_bMinPostForm;
  1200. if(bx_get('CmtMinPostForm') !== false)
  1201. $aDp['min_post_form'] = bx_process_input(bx_get('CmtMinPostForm'), BX_DATA_INT) == 1;
  1202. $aDp['in_designbox'] = isset($aDp['in_designbox']) ? (bool)$aDp['in_designbox'] : true;
  1203. $aDp['dynamic_mode'] = isset($aDp['dynamic_mode']) ? (bool)$aDp['dynamic_mode'] : false;
  1204. $aDp['show_empty'] = isset($aDp['show_empty']) ? (bool)$aDp['show_empty'] : false;
  1205. }
  1206. protected function _prepareTextForOutput ($s, $iCmtId = 0)
  1207. {
  1208. $iDataAction = !$this->isHtml() ? BX_DATA_TEXT_MULTILINE : BX_DATA_HTML;
  1209. $s = bx_process_output($s, $iDataAction);
  1210. $s = bx_linkify_html($s, 'class="' . BX_DOL_LINK_CLASS . '"');
  1211. if($iCmtId && $this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  1212. $s = $oMetatags->metaParse($this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId), $s);
  1213. return $s;
  1214. }
  1215. protected function _prepareParams(&$aBp, &$aDp)
  1216. {
  1217. $aBp['type'] = !empty($aBp['type']) ? $aBp['type'] : $this->_sBrowseType;
  1218. $aBp['filter'] = !empty($aBp['filter']) ? $aBp['filter'] : $this->_sBrowseFilter;
  1219. $aBp['parent_id'] = isset($aBp['parent_id']) ? $aBp['parent_id'] : 0;
  1220. $aBp['start'] = isset($aBp['start']) ? $aBp['start'] : -1;
  1221. $aBp['per_view'] = isset($aBp['per_view']) ? $aBp['per_view'] : -1;
  1222. $aBp['order']['by'] = isset($aBp['order_by']) ? $aBp['order_by'] : $this->_aOrder['by'];
  1223. $aBp['order']['way'] = isset($aBp['order_way']) ? $aBp['order_way'] : $this->_aOrder['way'];
  1224. $aDp['type'] = !empty($aDp['type']) ? $aDp['type'] : $this->_sDisplayType;
  1225. $aDp['blink'] = !empty($aDp['blink']) ? $aDp['blink'] : array();
  1226. if(!is_array($aDp['blink']))
  1227. $aDp['blink'] = explode(',', $aDp['blink']);
  1228. $aDp['quote'] = !empty($aDp['quote']) ? (int)$aDp['quote'] : 0;
  1229. if(!isset($aDp['min_post_form']))
  1230. $aDp['min_post_form'] = $this->_bMinPostForm;
  1231. switch($aDp['type']) {
  1232. case BX_CMT_DISPLAY_FLAT:
  1233. $aBp['vparent_id'] = -1;
  1234. $aBp['per_view'] = $aBp['per_view'] != -1 ? $aBp['per_view'] : $this->getPerView(0);
  1235. break;
  1236. case BX_CMT_DISPLAY_THREADED:
  1237. $aBp['per_view'] = $aBp['per_view'] != -1 ? $aBp['per_view'] : $this->getPerView($aBp['vparent_id']);
  1238. break;
  1239. }
  1240. switch ($aBp['type']) {
  1241. case BX_CMT_BROWSE_POPULAR:
  1242. $aBp['order'] = array(
  1243. 'by' => BX_CMT_ORDER_BY_POPULAR,
  1244. 'way' => BX_CMT_ORDER_WAY_DESC
  1245. );
  1246. break;
  1247. }
  1248. $aBp['count'] = $this->getCommentsCount($this->_iId, $aBp['vparent_id'], $aBp['filter']);
  1249. if($aBp['start'] != -1)
  1250. return;
  1251. $aBp['start'] = 0;
  1252. if($aBp['type'] == BX_CMT_BROWSE_TAIL) {
  1253. $aBp['start'] = $aBp['count'] - $aBp['per_view'];
  1254. if($aBp['start'] < 0) {
  1255. $aBp['per_view'] += $aBp['start'];
  1256. $aBp['start'] = 0;
  1257. }
  1258. }
  1259. $this->_setUserChoice($aDp['type'], $aBp['type'], $aBp['filter']);
  1260. }
  1261. protected function _triggerComment()
  1262. {
  1263. if(!$this->_aSystem['trigger_table'])
  1264. return false;
  1265. $iId = $this->getId();
  1266. if(!$iId)
  1267. return false;
  1268. $iCount = $this->getCommentsCount($iId);
  1269. return $this->_oQuery->updateTriggerTable($iId, $iCount);
  1270. }
  1271. /**
  1272. * Replace provided markers in a string
  1273. * @param $mixed string or array to replace markers in
  1274. * @return string where all occured markers are replaced
  1275. */
  1276. protected function _replaceMarkers ($mixed)
  1277. {
  1278. return bx_replace_markers($mixed, $this->_aMarkers);
  1279. }
  1280. protected function _getUserChoice()
  1281. {
  1282. $mixedDp = $mixedBpType = $mixedBpFilter = false;
  1283. if(!isLogged())
  1284. return array($mixedDp, $mixedBpType, $mixedBpFilter);
  1285. $iUserId = $this->_getAuthorId();
  1286. $oSession = BxDolSession::getInstance();
  1287. $mixedDp = $oSession->getValue($this->_sDpSessionKey . $iUserId);
  1288. $mixedBpType = $oSession->getValue($this->_sBpSessionKeyType . $iUserId);
  1289. $mixedBpFilter = $oSession->getValue($this->_sBpSessionKeyFilter . $iUserId);
  1290. return array($mixedDp, $mixedBpType, $mixedBpFilter);
  1291. }
  1292. protected function _setUserChoice($sDp, $sBpType, $sBpFilter)
  1293. {
  1294. if(!isLogged())
  1295. return;
  1296. $iUserId = $this->_getAuthorId();
  1297. $oSession = BxDolSession::getInstance();
  1298. if(!empty($sDp))
  1299. $oSession->setValue($this->_sDpSessionKey . $iUserId, $sDp);
  1300. if(!empty($sBpType))
  1301. $oSession->setValue($this->_sBpSessionKeyType . $iUserId, $sBpType);
  1302. if(!empty($sBpFilter))
  1303. $oSession->setValue($this->_sBpSessionKeyFilter . $iUserId, $sBpFilter);
  1304. }
  1305. protected function _sendNotificationEmail($iCmtId, $iCmtParentId)
  1306. {
  1307. $aCmt = $this->getCommentRow($iCmtId);
  1308. $aCmtParent = $this->getCommentRow($iCmtParentId);
  1309. if(empty($aCmt) || !is_array($aCmt) || empty($aCmtParent) || !is_array($aCmtParent) || (int)$aCmt['cmt_author_id'] == (int)$aCmtParent['cmt_author_id'])
  1310. return;
  1311. $oProfile = $this->_getAuthorObject($aCmtParent['cmt_author_id']);
  1312. if($oProfile instanceof BxDolProfileUndefined)
  1313. return;
  1314. $iAccount = $oProfile->getAccountId();
  1315. $aAccount = BxDolAccount::getInstance($iAccount)->getInfo();
  1316. $aPlus = array();
  1317. $aPlus['sender_display_name'] = $oProfile->getDisplayName();
  1318. $aPlus['reply_text'] = $this->_prepareTextForOutput($aCmt['cmt_text'], $iCmtId);
  1319. $sPageUrl = $this->getBaseUrl();
  1320. if(empty($sPageUrl))
  1321. $sPageUrl = $this->getViewUrl($iCmtParentId);
  1322. else
  1323. $sPageUrl .= $this->getItemAnchor($iCmtParentId, true);
  1324. $aPlus['page_url'] = $sPageUrl;
  1325. $sPageTitle = $this->getObjectTitle();
  1326. if(empty($sPageTitle))
  1327. $sPageTitle = _t('_Content');
  1328. $aPlus['page_title'] = $sPageTitle;
  1329. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('t_CommentReplied', $aPlus);
  1330. return $aTemplate && sendMail($aAccount['email'], $aTemplate['Subject'], $aTemplate['Body']);
  1331. }
  1332. protected function _isShowDoComment($aParams, $isAllowedComment, $bCount)
  1333. {
  1334. $bShowDoComment = !isset($aParams['show_do_comment']) || $aParams['show_do_comment'] == true;
  1335. return $bShowDoComment && ($isAllowedComment || $bCount);
  1336. }
  1337. protected function _isShowCounter($aParams, $isAllowedComment, $bCount)
  1338. {
  1339. $bShowCounter = isset($aParams['show_counter']) && $aParams['show_counter'] === true;
  1340. return $bShowCounter && ($isAllowedComment || $bCount);
  1341. }
  1342. /**
  1343. * Note. By default image based controls aren't used.
  1344. * Therefore it can be overwritten in custom template.
  1345. */
  1346. protected function _getImageDo()
  1347. {
  1348. return '';
  1349. }
  1350. protected function _getIconDo()
  1351. {
  1352. return 'far comment';
  1353. }
  1354. protected function _getTitleDo()
  1355. {
  1356. return '_cmt_txt_do';
  1357. }
  1358. }
  1359. /** @} */