BxDolCmts.php 51 KB

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