BxDolCmts.php 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320
  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. bx_import('BxDolPrivacy');
  11. define('BX_DOL_CMT_TYPE_COMMENTS', 'comments');
  12. define('BX_DOL_CMT_TYPE_REVIEWS', 'reviews');
  13. define('BX_CMT_OLD_VOTES', 365*86400); ///< comment votes older than this number of seconds will be deleted automatically
  14. define('BX_CMT_ACTION_POST', 'post');
  15. define('BX_CMT_ACTION_EDIT', 'edit');
  16. define('BX_CMT_DISPLAY_FLAT', 'flat');
  17. define('BX_CMT_DISPLAY_THREADED', 'threaded');
  18. define('BX_CMT_BROWSE_HEAD', 'head');
  19. define('BX_CMT_BROWSE_TAIL', 'tail');
  20. define('BX_CMT_BROWSE_POPULAR', 'popular');
  21. define('BX_CMT_BROWSE_CONNECTION', 'connection');
  22. define('BX_CMT_ORDER_BY_DATE', 'date');
  23. define('BX_CMT_ORDER_BY_POPULAR', 'popular');
  24. define('BX_CMT_FILTER_ALL', 'all');
  25. define('BX_CMT_FILTER_OTHERS', 'others');
  26. define('BX_CMT_FILTER_FRIENDS', 'friends');
  27. define('BX_CMT_FILTER_SUBSCRIPTIONS', 'subscriptions');
  28. define('BX_CMT_FILTER_PINNED', 'pinned');
  29. define('BX_CMT_ORDER_WAY_ASC', 'asc');
  30. define('BX_CMT_ORDER_WAY_DESC', 'desc');
  31. define('BX_CMT_PFP_TOP', 'top');
  32. define('BX_CMT_PFP_BOTTOM', 'bottom');
  33. define('BX_CMT_RATE_VALUE_PLUS', 1);
  34. define('BX_CMT_RATE_VALUE_MINUS', -1);
  35. define('BX_CMT_USAGE_BLOCK', 'block');
  36. define('BX_CMT_USAGE_INLINE', 'inline');
  37. define('BX_CMT_USAGE_DEFAULT', BX_CMT_USAGE_BLOCK);
  38. define('BX_CMT_STATUS_ACTIVE', 'active');
  39. define('BX_CMT_STATUS_HIDDEN', 'hidden');
  40. define('BX_CMT_STATUS_PENDING', 'pending');
  41. /**
  42. * Comments for any content
  43. *
  44. * Related classes:
  45. * - BxDolCmtsQuery - comments database queries
  46. * - BxBaseCmts - comments base representation
  47. * - BxTemplCmts - custom template representation
  48. *
  49. * AJAX comments for any content.
  50. * Self moderated - users rate all comments, and if comment is
  51. * below viewing treshold it is hidden by default.
  52. *
  53. * To add comments section to your module you need to add a record to 'sys_objects_cmts' table:
  54. *
  55. * - ID - autoincremented id for internal usage
  56. * - ObjectName - your unique module name, with vendor prefix, lowercase and spaces are underscored
  57. * - TableCmts - table name where comments are stored
  58. * - Html - 0 - convert new lines to <br /> on saving, 1 - standard(default) visual editor, 2 - full visual editor, 3 - mini visual editor.
  59. * - PerView - number of comments on a page
  60. * - IsRatable - 0 or 1 allow to rate comments or not
  61. * - ViewingThreshold - comment viewing treshost, if comment is below this number it is hidden by default
  62. * - IsOn - is this comment object enabled
  63. * - RootStylePrefix - toot comments style prefix, if you need root comments look different\
  64. * - ObjectVote - Vote object name to process comments' votes. May be empty if Comment Vote is not needed.
  65. * - TriggerTable - table to be updated upon each comment
  66. * - TriggerFieldId - TriggerTable table field with unique record id of
  67. * - TriggerFieldComments - TriggerTable table field with comments count, it will be updated automatically upon eaech comment
  68. * - ClassName - your custom class name if you need to override default class, this class must have the same constructor arguments
  69. * - ClassFile - file where your ClassName is stored.
  70. *
  71. * You can refer to BoonEx modules for sample record in this table.
  72. *
  73. *
  74. *
  75. * @section example Example of usage:
  76. * After filling in the table you can show comments section in any place, using the following code:
  77. *
  78. * @code
  79. * $o = new BxTemplCmts('value of ObjectName field', $iYourEntryId);
  80. * if ($o->isEnabled())
  81. * echo $o->getCommentsBlock ();
  82. * @endcode
  83. *
  84. * Please note that you never need to use BxDolCmts class directly, use BxTemplCmts instead.
  85. * Also if you override comments class with your own then make it child of BxTemplCmts class.
  86. *
  87. *
  88. *
  89. * @section acl Memberships/ACL:
  90. * - comments post
  91. * - comments edit own
  92. * - comments remove own
  93. * - comments edit all
  94. *
  95. *
  96. *
  97. * @section alerts Alerts:
  98. * Alerts type/unit - every module has own type/unit, it equals to ObjectName.
  99. *
  100. * The following alerts are rised
  101. *
  102. * - commentPost - comment was posted
  103. * - $iObjectId - entry id
  104. * - $iSenderId - author of comment
  105. * - $aExtra['comment_id'] - just added comment id
  106. *
  107. * - commentRemoved - comments was removed
  108. * - $iObjectId - entry id
  109. * - $iSenderId - comment deleter id
  110. * - $aExtra['comment_id'] - removed comment id
  111. *
  112. * - commentUpdated - comments was updated
  113. * - $iObjectId - entry id
  114. * - $iSenderId - comment deleter id
  115. * - $aExtra['comment_id'] - updated comment id
  116. *
  117. * - commentRated - comments was rated
  118. * - $iObjectId - entry id
  119. * - $iSenderId - comment rater id
  120. * - $aExtra['comment_id'] - rated comment id
  121. * - $aExtra['rate'] - comment rate 1 or -1
  122. *
  123. */
  124. class BxDolCmts extends BxDolFactory implements iBxDolReplaceable, iBxDolContentInfoService
  125. {
  126. /**
  127. * System tables which are UNITED for all
  128. * comment systems and cannot be overwritten.
  129. */
  130. public static $sTableSystems = 'sys_objects_cmts';
  131. public static $sTableIds = 'sys_cmts_ids';
  132. /**
  133. * System tables which are used by default and
  134. * can be overwritten in comment systems.
  135. * @see BxDolCmts::getTableNameImages and BxDolCmts::getTableNameImages2Entries
  136. */
  137. protected $_sTableImages = 'sys_cmts_images';
  138. protected $_sTableImages2Entries = 'sys_cmts_images2entries';
  139. protected $_aElementDefaults;
  140. protected $_aElementDefaultsApi;
  141. protected $_aElementParamsApi; //--- Params from DefaultsApi array to be passed to Api
  142. protected $_bIsApi;
  143. protected $_sType;
  144. protected $_oQuery = null;
  145. protected $_oTemplate = null;
  146. protected $_bPostQuote;
  147. protected $_bMinPostForm;
  148. protected $_sFormObject;
  149. protected $_sFormDisplayPost;
  150. protected $_sFormDisplayEdit;
  151. protected $_sConnObjFriends;
  152. protected $_sConnObjSubscriptions;
  153. protected $_sMenuObjManage;
  154. protected $_sMenuObjActions;
  155. protected $_sMenuObjCounters;
  156. protected $_sMenuObjMeta;
  157. protected $_sMetatagsObj;
  158. protected $_sViewUrl = '';
  159. protected $_sBaseUrl = '';
  160. protected $_sListAnchor = '';
  161. protected $_sItemAnchor = '';
  162. protected $_sSystem = 'profile'; ///< current comment system name
  163. protected $_aSystem = array (); ///< current comments system array
  164. protected $_iId = 0; ///< obect id to be commented
  165. protected $_iAuthorId = 0; ///< currently logged in user who browse, post, etc.
  166. protected $_aT = array (); ///< an array of lang keys
  167. protected $_aMarkers = array ();
  168. protected $_sDisplayType = '';
  169. protected $_sDpSessionKey = '';
  170. protected $_iDpMaxLevel = 0;
  171. protected $_sBrowseType = '';
  172. protected $_bBrowseFilter = false;
  173. protected $_sBrowseFilter = '';
  174. protected $_sBpSessionKeyType = '';
  175. protected $_sBpSessionKeyFilter = '';
  176. protected $_aOrder = array();
  177. protected $_sSnippetLenthLiveSearch = 50;
  178. protected $_iRememberTime = 2592000;
  179. /**
  180. * Constructor
  181. * $sSystem - comments system name
  182. * $iId - obect id to be commented
  183. */
  184. protected function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  185. {
  186. parent::__construct();
  187. $this->_bIsApi = bx_is_api();
  188. $this->_sType = BX_DOL_CMT_TYPE_COMMENTS;
  189. $this->_aSystems = $this->getSystems();
  190. if(!isset($this->_aSystems[$sSystem]))
  191. return;
  192. $this->_sSystem = $sSystem;
  193. $this->_aSystem = $this->_aSystems[$sSystem];
  194. $this->_aSystem['is_browse_filter'] = (int)$this->_bBrowseFilter;
  195. $this->_iAuthorId = $this->_getAuthorId();
  196. $this->_iDpMaxLevel = (int)$this->_aSystem['number_of_levels'];
  197. $this->_sDisplayType = $this->_iDpMaxLevel == 0 ? BX_CMT_DISPLAY_FLAT : BX_CMT_DISPLAY_THREADED;
  198. $this->_sDpSessionKey = 'bx_' . $this->_sSystem . '_dp_';
  199. $this->_sBrowseType = $this->_aSystem['browse_type'];
  200. $this->_sBrowseFilter = BX_CMT_FILTER_ALL;
  201. $this->_sBpSessionKeyType = 'bx_' . $this->_sSystem . '_bpt_';
  202. $this->_sBpSessionKeyFilter = 'bx_' . $this->_sSystem . '_bpf_';
  203. $this->_aOrder = array(
  204. 'by' => BX_CMT_ORDER_BY_DATE,
  205. 'way' => BX_CMT_ORDER_WAY_ASC
  206. );
  207. list($mixedUserDp, $mixedUserBpType, $mixedUserBpFilter) = $this->_getUserChoice();
  208. if(!empty($mixedUserDp))
  209. $this->_sDisplayType = $mixedUserDp;
  210. if(!empty($mixedUserBpType))
  211. $this->_sBrowseType = $mixedUserBpType;
  212. if(!empty($mixedUserBpFilter))
  213. $this->_sBrowseFilter = $mixedUserBpFilter;
  214. $this->_sViewUrl = 'page.php?i=cmts-view';
  215. $this->_sBaseUrl = $this->_aSystem['base_url'];
  216. $this->_sListAnchor = "cmts-anchor-%s-%d";
  217. $this->_sItemAnchor = "cmt-anchor-%s-%d-%d";
  218. $this->_oQuery = new BxDolCmtsQuery($this);
  219. $this->_bPostQuote = false;
  220. $this->_bMinPostForm = true;
  221. $this->_sFormObject = 'sys_comment';
  222. $this->_sFormDisplayPost = 'sys_comment_post';
  223. $this->_sFormDisplayEdit = 'sys_comment_edit';
  224. $this->_sConnObjFriends = 'sys_profiles_friends';
  225. $this->_sConnObjSubscriptions = 'sys_profiles_subscriptions';
  226. $this->_sMenuObjManage = 'sys_cmts_item_manage';
  227. $this->_sMenuObjActions = 'sys_cmts_item_actions';
  228. $this->_sMenuObjCounters = 'sys_cmts_item_counters';
  229. $this->_sMenuObjMeta = 'sys_cmts_item_meta';
  230. $this->_sMetatagsObj = 'sys_cmts';
  231. $this->_aT = array(
  232. 'block_comments_title' => '_cmt_block_comments_title',
  233. 'txt_sample_single' => '_cmt_txt_sample_comment_single',
  234. 'txt_sample_vote_single' => '_cmt_txt_sample_vote_single',
  235. 'txt_sample_reaction_single' => '_cmt_txt_sample_reaction_single',
  236. 'txt_sample_score_up_single' => '_cmt_txt_sample_score_up_single',
  237. 'txt_sample_score_down_single' => '_cmt_txt_sample_score_down_single',
  238. 'txt_min_form_placeholder' => '_cmt_txt_min_form_placeholder'
  239. );
  240. if ($iInit)
  241. $this->init($iId);
  242. if ($oTemplate)
  243. $this->_oTemplate = $oTemplate;
  244. else
  245. $this->_oTemplate = BxDolTemplate::getInstance();
  246. }
  247. /**
  248. * get comments object instanse
  249. * @param $sSys comments object name
  250. * @param $iId associated content id, where comments are postred in
  251. * @param $iInit perform initialization
  252. * @return null on error, or ready to use class instance
  253. */
  254. public static function getObjectInstance($sSys, $iId, $iInit = true, $oTemplate = false)
  255. {
  256. if(isset($GLOBALS['bxDolClasses']['BxDolCmts!' . $sSys . $iId]))
  257. return $GLOBALS['bxDolClasses']['BxDolCmts!' . $sSys . $iId];
  258. $aSystems = self::getSystems();
  259. if (!isset($aSystems[$sSys]))
  260. return null;
  261. $sClassName = 'BxTemplCmts';
  262. if(!empty($aSystems[$sSys]['class_name'])) {
  263. $sClassName = $aSystems[$sSys]['class_name'];
  264. if(!empty($aSystems[$sSys]['class_file']))
  265. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSys]['class_file']);
  266. }
  267. $o = new $sClassName($sSys, $iId, $iInit, $oTemplate);
  268. return ($GLOBALS['bxDolClasses']['BxDolCmts!' . $sSys . $iId] = $o);
  269. }
  270. /**
  271. * get comments object instanse
  272. * @param $iUniqId unique comment id
  273. * @return null on error, or ready to use class instance
  274. */
  275. public static function getObjectInstanceByUniqId($iUniqId, $iInit = true, $oTemplate = false)
  276. {
  277. $aData = BxDolCmtsQuery::getInfoByUniqId($iUniqId);
  278. if(empty($aData) || !is_array($aData))
  279. return null;
  280. return self::getObjectInstance($aData['system_name'], $aData['cmt_object_id']);
  281. }
  282. public static function &getSystems ()
  283. {
  284. $sKey = 'bx_dol_cache_memory_cmts_systems';
  285. if (!isset($GLOBALS[$sKey])) {
  286. $GLOBALS[$sKey] = BxDolDb::getInstance()->fromCache('sys_objects_cmts', 'getAllWithKey', '
  287. SELECT
  288. `ID` as `system_id`,
  289. `Name` AS `name`,
  290. `Module` AS `module`,
  291. `Table` AS `table`,
  292. `CharsPostMin` AS `chars_post_min`,
  293. `CharsPostMax` AS `chars_post_max`,
  294. `CharsDisplayMax` AS `chars_display_max`,
  295. `Html` AS `html`,
  296. `PerView` AS `per_view`,
  297. `PerViewReplies` AS `per_view_replies`,
  298. `BrowseType` AS `browse_type`,
  299. `IsBrowseSwitch` AS `is_browse_switch`,
  300. `PostFormPosition` AS `post_form_position`,
  301. `NumberOfLevels` AS `number_of_levels`,
  302. `IsDisplaySwitch` AS `is_display_switch`,
  303. `IsRatable` AS `is_ratable`,
  304. `ViewingThreshold` AS `viewing_threshold`,
  305. `IsOn` AS `is_on`,
  306. `RootStylePrefix` AS `root_style_prefix`,
  307. `BaseUrl` AS `base_url`,
  308. `ObjectVote` AS `object_vote`,
  309. `ObjectReaction` AS `object_reaction`,
  310. `ObjectScore` AS `object_score`,
  311. `ObjectReport` AS `object_report`,
  312. `TriggerTable` AS `trigger_table`,
  313. `TriggerFieldId` AS `trigger_field_id`,
  314. `TriggerFieldAuthor` AS `trigger_field_author`,
  315. `TriggerFieldTitle` AS `trigger_field_title`,
  316. `TriggerFieldComments` AS `trigger_field_comments`,
  317. `ClassName` AS `class_name`,
  318. `ClassFile` AS `class_file`
  319. FROM `' . self::$sTableSystems . '`', 'name');
  320. }
  321. return $GLOBALS[$sKey];
  322. }
  323. public static function getGlobalInfo ($iUniqueId)
  324. {
  325. 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(
  326. 'id' => (int)$iUniqueId
  327. ));
  328. }
  329. public static function getGlobalNumByParams($aParams = [])
  330. {
  331. $sQuery = "SELECT COUNT(*) FROM `" . self::$sTableIds . "` WHERE 1";
  332. foreach($aParams as $aValue)
  333. $sQuery .= " AND `" . $aValue['key'] ."` " . $aValue['operator'] . " '" . $aValue['value'] . "'";
  334. return BxDolDb::getInstance()->getOne($sQuery);
  335. }
  336. public function init ($iId)
  337. {
  338. if (empty($this->iId) && $iId)
  339. $this->setId($iId);
  340. $this->addMarkers(array(
  341. 'object_id' => $this->getId(),
  342. 'user_id' => $this->_getAuthorId()
  343. ));
  344. }
  345. public function getId ()
  346. {
  347. return $this->_iId;
  348. }
  349. public function isEnabled ()
  350. {
  351. return isset($this->_aSystem['is_on']) && $this->_aSystem['is_on'];
  352. }
  353. public function getSystemId()
  354. {
  355. return $this->_aSystem['system_id'];
  356. }
  357. public function getSystemName()
  358. {
  359. return $this->_sSystem;
  360. }
  361. public function getSystemInfo()
  362. {
  363. return $this->_aSystem;
  364. }
  365. public function getStorageObjectName()
  366. {
  367. return $this->_getFormObject()->getStorageObjectName();
  368. }
  369. public function getTranscoderPreviewName()
  370. {
  371. return $this->_getFormObject()->getTranscoderPreviewName();
  372. }
  373. public function getFormObject()
  374. {
  375. return $this->_getFormObject();
  376. }
  377. public function getTableNameImages()
  378. {
  379. return $this->_sTableImages;
  380. }
  381. public function getTableNameImages2Entries()
  382. {
  383. return $this->_sTableImages2Entries;
  384. }
  385. public function getLanguageKey($sIndex)
  386. {
  387. return isset($this->_aT[$sIndex]) ? $this->_aT[$sIndex] : '';
  388. }
  389. public function getMaxLevel()
  390. {
  391. return $this->_iDpMaxLevel;
  392. }
  393. public function getOrder ()
  394. {
  395. return $this->_sOrder;
  396. }
  397. public function getPerView ($iCmtParentId = 0)
  398. {
  399. return $iCmtParentId == 0 ? $this->_aSystem['per_view'] : $this->_aSystem['per_view_replies'];
  400. }
  401. public function getStatusAdmin()
  402. {
  403. return $this->isEditAllowedAll() || $this->isRemoveAllowedAll() || $this->isAutoApprove() ? BX_CMT_STATUS_ACTIVE : BX_CMT_STATUS_PENDING;
  404. }
  405. public function getViewUrl($iCmtId, $bAbsolute = true)
  406. {
  407. if(empty($this->_aSystem['trigger_field_title']))
  408. return '';
  409. $s = BxDolPermalinks::getInstance()->permalink($this->_sViewUrl, [
  410. 'sys' => $this->_sSystem,
  411. 'id' => $this->_iId,
  412. 'cmt_id' => $iCmtId
  413. ]);
  414. return $bAbsolute ? bx_absolute_url($s) : $s;
  415. }
  416. public function getViewText($mixedItem)
  417. {
  418. if(!is_array($mixedItem))
  419. $mixedItem = $this->getCommentSimple((int)$mixedItem);
  420. if(empty($mixedItem) || !is_array($mixedItem))
  421. return '';
  422. return $this->_prepareTextForOutput($mixedItem['cmt_text'], (int)$mixedItem['cmt_id']);
  423. }
  424. public function getBaseUrl()
  425. {
  426. $sUrl = $this->_replaceMarkers($this->_sBaseUrl);
  427. $sUrl = BxDolPermalinks::getInstance()->permalink($sUrl);
  428. if(get_mb_substr($sUrl, 0, 6) != 'http:/' && get_mb_substr($sUrl, 0, 7) != 'https:/')
  429. $sUrl = BX_DOL_URL_ROOT . $sUrl;
  430. return $sUrl;
  431. }
  432. public function getListUrl()
  433. {
  434. $sBaseUrl = $this->getBaseUrl();
  435. if(empty($sBaseUrl))
  436. return '';
  437. return $sBaseUrl . $this->getListAnchor(true);
  438. }
  439. public function getItemUrl($iItemId)
  440. {
  441. $sBaseUrl = $this->getBaseUrl();
  442. if(empty($sBaseUrl))
  443. return '';
  444. return $sBaseUrl . $this->getItemAnchor($iItemId, true);
  445. }
  446. public function getListAnchor($bWithHash = false)
  447. {
  448. return ($bWithHash ? '#' : '') . sprintf($this->_sListAnchor, str_replace('_', '-', $this->getSystemName()), $this->getId());
  449. }
  450. public function getItemAnchor($iItemId, $bWithHash = false)
  451. {
  452. return ($bWithHash ? '#' : '') . sprintf($this->_sItemAnchor, str_replace('_', '-', $this->getSystemName()), $this->getId(), $iItemId);
  453. }
  454. public function getAttachments($iCmtId)
  455. {
  456. $aResult = array();
  457. if(!$this->isAttachImageEnabled())
  458. return $aResult;
  459. $aFiles = $this->_oQuery->getFiles($this->_aSystem['system_id'], $iCmtId);
  460. if(empty($aFiles) || !is_array($aFiles))
  461. return $aResult;
  462. $oStorage = BxDolStorage::getObjectInstance($this->getStorageObjectName());
  463. $oTranscoder = BxDolTranscoderImage::getObjectInstance($this->getTranscoderPreviewName());
  464. foreach($aFiles as $aFile) {
  465. $bImage = $oTranscoder && $oTranscoder->isMimeTypeSupported($aFile['mime_type']);
  466. $sPreview = '';
  467. if($bImage)
  468. $sPreview = $oTranscoder->getFileUrl($aFile['image_id']);
  469. if(!$sPreview)
  470. $sPreview = $this->_oTemplate->getIconUrl($oStorage->getIconNameByFileName($aFile['file_name']));
  471. $sUrl = $oStorage->getFileUrlById($aFile['image_id']);
  472. $aResult[] = array(
  473. 'id' => $aFile['image_id'],
  474. 'src' => $sPreview,
  475. 'src_orig' => $bImage ? $sUrl : '',
  476. 'url' => !$bImage ? $sUrl : ''
  477. );
  478. }
  479. return $aResult;
  480. }
  481. public function getConnectionObject($sType)
  482. {
  483. $sResult = '';
  484. switch($sType) {
  485. case BX_CMT_FILTER_FRIENDS:
  486. $sResult = $this->_sConnObjFriends;
  487. break;
  488. case BX_CMT_FILTER_SUBSCRIPTIONS:
  489. $sResult = $this->_sConnObjSubscriptions;
  490. break;
  491. }
  492. return $sResult;
  493. }
  494. public function getVoteObject($iEniqId)
  495. {
  496. if(empty($this->_aSystem['object_vote']))
  497. $this->_aSystem['object_vote'] = 'sys_cmts';
  498. $oVote = BxDolVote::getObjectInstance($this->_aSystem['object_vote'], $iEniqId, true, $this->_oTemplate);
  499. if(!$oVote || !$oVote->isEnabled())
  500. return false;
  501. return $oVote;
  502. }
  503. public function getReactionObject($iEniqId)
  504. {
  505. if(empty($this->_aSystem['object_reaction']))
  506. $this->_aSystem['object_reaction'] = 'sys_cmts_reactions';
  507. $oReaction = BxDolVote::getObjectInstance($this->_aSystem['object_reaction'], $iEniqId, true, $this->_oTemplate);
  508. if(!$oReaction || !$oReaction->isEnabled())
  509. return false;
  510. return $oReaction;
  511. }
  512. public function getScoreObject($iEniqId)
  513. {
  514. if(empty($this->_aSystem['object_score']))
  515. $this->_aSystem['object_score'] = 'sys_cmts';
  516. $oScore = BxDolScore::getObjectInstance($this->_aSystem['object_score'], $iEniqId, true, $this->_oTemplate);
  517. if(!$oScore || !$oScore->isEnabled())
  518. return false;
  519. return $oScore;
  520. }
  521. public function getReportObject($iEniqId)
  522. {
  523. if(empty($this->_aSystem['object_report']))
  524. $this->_aSystem['object_report'] = 'sys_cmts';
  525. $oReport = BxDolReport::getObjectInstance($this->_aSystem['object_report'], $iEniqId, true, $this->_oTemplate);
  526. if(!$oReport || !$oReport->isEnabled())
  527. return false;
  528. return $oReport;
  529. }
  530. /**
  531. * Is used as:
  532. * 1. Live Update's session key.
  533. * 2. HTML ID for Live Update's 'New Content' button.
  534. * @return string with ID.
  535. */
  536. public function getNotificationId()
  537. {
  538. return 'cmts-notification-' . $this->_sSystem . '-' . $this->_iId;
  539. }
  540. public function getSocketName()
  541. {
  542. return 'cmts_' . $this->_sSystem;
  543. }
  544. /**
  545. * @deprecated since version 10.0.0-B3 and can be removed in later versions.
  546. */
  547. public function setTableNameFiles($sTable)
  548. {
  549. $this->_sTableImages = $sTable;
  550. $this->_oQuery->setTableNameFiles($sTable);
  551. }
  552. /**
  553. * @deprecated since version 10.0.0-B3 and can be removed in later versions.
  554. */
  555. public function setTableNameFiles2Entries($sTable)
  556. {
  557. $this->_sTableImages2Entries = $sTable;
  558. $this->_oQuery->setTableNameFiles2Entries($sTable);
  559. }
  560. public function isHtml ()
  561. {
  562. return $this->_aSystem['html'] > 0;
  563. }
  564. public function isRatable ()
  565. {
  566. return $this->_aSystem['is_ratable'];
  567. }
  568. public function isAttachImageEnabled()
  569. {
  570. return true;
  571. }
  572. public function isAutoApprove()
  573. {
  574. return getParam('sys_cmts_enable_auto_approve') == 'on';
  575. }
  576. /**
  577. * set id to operate with votes
  578. */
  579. public function setId ($iId)
  580. {
  581. if ($iId == $this->getId()) return;
  582. $this->_iId = $iId;
  583. }
  584. /**
  585. * Add replace markers.
  586. * @param $a array of markers as key => value
  587. * @return true on success or false on error
  588. */
  589. public function addMarkers ($a)
  590. {
  591. if (empty($a) || !is_array($a))
  592. return false;
  593. $this->_aMarkers = array_merge ($this->_aMarkers, $a);
  594. return true;
  595. }
  596. /**
  597. * Database functions
  598. */
  599. public function getQueryObject ()
  600. {
  601. return $this->_oQuery;
  602. }
  603. public function getCommentsTableName ()
  604. {
  605. return $this->_oQuery->getTableName ();
  606. }
  607. public function getObjectAuthorId ($iObjectId = 0)
  608. {
  609. if(empty($this->_aSystem['trigger_field_author']))
  610. return 0;
  611. return $this->_oQuery->getObjectAuthorId ($iObjectId ? $iObjectId : $this->getId());
  612. }
  613. public function getObjectTitle ($iObjectId = 0)
  614. {
  615. if(empty($this->_aSystem['trigger_field_title']))
  616. return '';
  617. $sTitle = $this->_oQuery->getObjectTitle ($iObjectId ? $iObjectId : $this->getId());
  618. if(get_mb_substr($sTitle, 0, 1) == '_')
  619. $sTitle = _t($sTitle);
  620. return $sTitle;
  621. }
  622. public function getObjectPrivacyView ($iObjectId = 0)
  623. {
  624. if(empty($iObjectId))
  625. $iObjectId = $this->getId();
  626. $sFieldPrivacyView = '';
  627. if(!empty($this->_aSystem['trigger_field_privacy_view']))
  628. $sFieldPrivacyView = $this->_aSystem['trigger_field_privacy_view'];
  629. if(($iPrivacyView = $this->_oQuery->getObjectPrivacyView($iObjectId, $sFieldPrivacyView)) !== false)
  630. return $iPrivacyView;
  631. return BX_DOL_PG_ALL;
  632. }
  633. /*
  634. * Gets Content Filter object.
  635. */
  636. public function getObjectContentFilter ()
  637. {
  638. $oCf = BxDolContentFilter::getInstance();
  639. if(!$oCf->isEnabledForComments())
  640. return false;
  641. return $oCf;
  642. }
  643. public function getCommentsCountAll ($iObjectId = 0, $bForceCalculate = false)
  644. {
  645. return $this->_oQuery->getCommentsCountAll ($iObjectId ? $iObjectId : $this->getId(), $this->_getAuthorId(), $bForceCalculate);
  646. }
  647. public function getCommentsCount ($iObjectId = 0, $iCmtVParentId = -1, $sFilter = '')
  648. {
  649. return $this->_oQuery->getCommentsCount ($iObjectId ? $iObjectId : $this->getId(), $iCmtVParentId, $this->_getAuthorId(), $sFilter);
  650. }
  651. public function getCommentsArray ($iVParentId, $sFilter, $aOrder, $iStart = 0, $iCount = -1)
  652. {
  653. return $this->_oQuery->getComments ($this->getId(), $iVParentId, $this->_getAuthorId(), $sFilter, $aOrder, $iStart, $iCount);
  654. }
  655. public function getCommentsBy ($aParams = [])
  656. {
  657. return $this->_oQuery->getCommentsBy($aParams);
  658. }
  659. /**
  660. * Get comment's unique id.
  661. */
  662. public function getCommentUniqId($iCmtId, $iAuthorId = 0)
  663. {
  664. return $this->_oQuery->getUniqId($this->getSystemId(), $iCmtId, ['author_id' => $iAuthorId]);
  665. }
  666. /**
  667. * Get comment's short info.
  668. */
  669. public function getCommentSimple ($iCmtId)
  670. {
  671. return $this->_oQuery->getCommentSimple ($this->getId(), $iCmtId);
  672. }
  673. /**
  674. * Get comment's full info.
  675. */
  676. public function getCommentRow ($iCmtId)
  677. {
  678. return $this->_oQuery->getComment ($this->getId(), $iCmtId);
  679. }
  680. public function onObjectDelete ($iObjectId = 0)
  681. {
  682. // delete comments
  683. $aFiles = $aCmtIds = [];
  684. $this->_oQuery->deleteObjectComments ($iObjectId ? $iObjectId : $this->getId(), $aFiles, $aCmtIds);
  685. // delete votes
  686. $this->deleteVotes($aCmtIds);
  687. // delete reactions
  688. $this->deleteReactions($aCmtIds);
  689. // delete scores
  690. $this->deleteScores($aCmtIds);
  691. // delete reports
  692. $this->deleteReports($aCmtIds);
  693. // delete meta info
  694. $this->deleteMetaInfo($aCmtIds);
  695. // delete files
  696. if ($aFiles) {
  697. $oStorage = BxDolStorage::getObjectInstance($this->getStorageObjectName());
  698. if ($oStorage)
  699. $oStorage->queueFilesForDeletion($aFiles);
  700. }
  701. // delete unique IDs
  702. $this->deleteUniqueIds($aCmtIds);
  703. }
  704. public static function onAuthorDelete ($iAuthorId)
  705. {
  706. $aSystems = self::getSystems();
  707. foreach($aSystems as $sSystem => $aSystem) {
  708. $o = self::getObjectInstance($sSystem, 0);
  709. $oQuery = $o->getQueryObject();
  710. // delete comments
  711. $aFiles = $aCmtIds = array ();
  712. $oQuery->deleteAuthorComments($iAuthorId, $aFiles, $aCmtIds);
  713. // delete votes
  714. $o->deleteVotes($aCmtIds);
  715. // delete reactions
  716. $o->deleteReactions($aCmtIds);
  717. // delete scores
  718. $o->deleteScores($aCmtIds);
  719. // delete reports
  720. $o->deleteReports($aCmtIds);
  721. // delete meta info
  722. $o->deleteMetaInfo($aCmtIds);
  723. // delete files
  724. $oStorage = BxDolStorage::getObjectInstance($o->getStorageObjectName());
  725. if ($oStorage)
  726. $oStorage->queueFilesForDeletion($aFiles);
  727. // delete unique IDs
  728. $o->deleteUniqueIds($aCmtIds);
  729. }
  730. return true;
  731. }
  732. public static function onModuleEnable ($sModuleName)
  733. {
  734. $aSystems = self::getSystems();
  735. foreach($aSystems as $sSystem => $aSystem) {
  736. if ($sModuleName !== $aSystem['module'])
  737. continue;
  738. $o = self::getObjectInstance($sSystem, 0);
  739. $o->registerTranscoders();
  740. }
  741. return true;
  742. }
  743. public static function onModuleDisable ($sModuleName)
  744. {
  745. $aSystems = self::getSystems();
  746. foreach($aSystems as $sSystem => $aSystem) {
  747. if ($sModuleName !== $aSystem['module'])
  748. continue;
  749. $o = self::getObjectInstance($sSystem, 0);
  750. $o->unregisterTranscoders();
  751. }
  752. return true;
  753. }
  754. public static function onModuleUninstall ($sModuleName, &$iFiles = null)
  755. {
  756. $aSystems = self::getSystems();
  757. foreach($aSystems as $sSystem => $aSystem) {
  758. if ($sModuleName !== $aSystem['module'])
  759. continue;
  760. $o = self::getObjectInstance($sSystem, 0);
  761. $oQuery = $o->getQueryObject();
  762. // delete comments
  763. $aFiles = $aCmtIds = array ();
  764. $oQuery->deleteAll($aSystem['system_id'], $aFiles, $aCmtIds);
  765. // delete votes
  766. $o->deleteVotes($aCmtIds);
  767. // delete reactions
  768. $o->deleteReactions($aCmtIds);
  769. // delete scores
  770. $o->deleteScores($aCmtIds);
  771. // delete reports
  772. $o->deleteReports($aCmtIds);
  773. // delete meta info
  774. $o->deleteMetaInfo($aCmtIds);
  775. // delete files
  776. $oStorage = BxDolStorage::getObjectInstance($o->getStorageObjectName());
  777. if ($oStorage && $aFiles)
  778. $oStorage->queueFilesForDeletion($aFiles);
  779. if (null !== $iFiles)
  780. $iFiles += count($aFiles);
  781. // delete unique IDs
  782. $o->deleteUniqueIds($aCmtIds);
  783. }
  784. return true;
  785. }
  786. public function deleteVotes ($mixedCmtId)
  787. {
  788. if(!is_array($mixedCmtId))
  789. $mixedCmtId = [$mixedCmtId];
  790. foreach($mixedCmtId as $iCmtId)
  791. if(($oReport = $this->getVoteObject($this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId))) !== false)
  792. $oReport->onObjectDelete();
  793. }
  794. public function deleteReactions ($mixedCmtId)
  795. {
  796. if(!is_array($mixedCmtId))
  797. $mixedCmtId = [$mixedCmtId];
  798. foreach($mixedCmtId as $iCmtId)
  799. if(($oReport = $this->getReactionObject($this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId))) !== false)
  800. $oReport->onObjectDelete();
  801. }
  802. public function deleteScores ($mixedCmtId)
  803. {
  804. if(!is_array($mixedCmtId))
  805. $mixedCmtId = [$mixedCmtId];
  806. foreach($mixedCmtId as $iCmtId)
  807. if(($oReport = $this->getScoreObject($this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId))) !== false)
  808. $oReport->onObjectDelete();
  809. }
  810. public function deleteReports ($mixedCmtId)
  811. {
  812. if(!is_array($mixedCmtId))
  813. $mixedCmtId = [$mixedCmtId];
  814. foreach($mixedCmtId as $iCmtId)
  815. if(($oReport = $this->getReportObject($this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId))) !== false)
  816. $oReport->onObjectDelete();
  817. }
  818. public function deleteMetaInfo ($mixedCmtId)
  819. {
  820. if(!is_array($mixedCmtId))
  821. $mixedCmtId = array($mixedCmtId);
  822. $oMetatags = false;
  823. if(!empty($this->_sMetatagsObj))
  824. $oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj);
  825. if($oMetatags)
  826. foreach($mixedCmtId as $iCmtId)
  827. $oMetatags->onDeleteContent($this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId));
  828. }
  829. public function deleteUniqueIds ($mixedCmtId)
  830. {
  831. if(!is_array($mixedCmtId))
  832. $mixedCmtId = array($mixedCmtId);
  833. foreach($mixedCmtId as $iCmtId)
  834. $this->_oQuery->deleteCmtIds($this->_aSystem['system_id'], $iCmtId);
  835. }
  836. /**
  837. * Permissions functions
  838. */
  839. public function isAdmin($iCmtAuthorId)
  840. {
  841. return BxDolAcl::getInstance()->isMemberLevelInSet(array(MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR), $iCmtAuthorId);
  842. }
  843. public function checkAction ($sAction, $isPerformAction = false)
  844. {
  845. $iId = $this->_getAuthorId();
  846. $a = checkActionModule($iId, $sAction, 'system', $isPerformAction);
  847. return $a[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  848. }
  849. public function checkActionErrorMsg ($sAction)
  850. {
  851. $iId = $this->_getAuthorId();
  852. $a = checkActionModule($iId, $sAction, 'system');
  853. return $a[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $a[CHECK_ACTION_MESSAGE] : '';
  854. }
  855. public function isViewAllowed ($isPerformAction = false)
  856. {
  857. if (BxDolRequest::serviceExists($this->_aSystem['module'], 'check_allowed_comments_view')) {
  858. $mixedResult = BxDolService::call($this->_aSystem['module'], 'check_allowed_comments_view', array($this->getId(), $this->getSystemName()));
  859. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  860. return $mixedResult;
  861. }
  862. return CHECK_ACTION_RESULT_ALLOWED;
  863. }
  864. public function isVoteAllowed ($aCmt, $isPerformAction = false)
  865. {
  866. if(!$this->isRatable())
  867. return false;
  868. $oVote = $this->getVoteObject($aCmt['cmt_id']);
  869. if($oVote === false)
  870. return false;
  871. $iUserId = (int)$this->_getAuthorId();
  872. if($iUserId == 0)
  873. return false;
  874. if(isAdmin())
  875. return true;
  876. return $oVote->isAllowedVote($isPerformAction);
  877. }
  878. public function isScoreAllowed ($aCmt, $isPerformAction = false)
  879. {
  880. if(!$this->isRatable())
  881. return false;
  882. $oScore = $this->getScoreObject($aCmt['cmt_id']);
  883. if($oScore === false)
  884. return false;
  885. $iUserId = (int)$this->_getAuthorId();
  886. if($iUserId == 0)
  887. return false;
  888. if(isAdmin())
  889. return true;
  890. return $oScore->isAllowedVote($isPerformAction);
  891. }
  892. public function isReportAllowed ($aCmt, $isPerformAction = false)
  893. {
  894. $oReport = $this->getReportObject($aCmt['cmt_id']);
  895. if($oReport === false)
  896. return false;
  897. $iUserId = (int)$this->_getAuthorId();
  898. if($iUserId == 0)
  899. return false;
  900. if(isAdmin())
  901. return true;
  902. return $oReport->isAllowedReport($isPerformAction);
  903. }
  904. public function isPostAllowed ($isPerformAction = false)
  905. {
  906. if (BxDolRequest::serviceExists($this->_aSystem['module'], 'check_allowed_comments_post')) {
  907. $mixedResult = BxDolService::call($this->_aSystem['module'], 'check_allowed_comments_post', array($this->getId(), $this->getSystemName()));
  908. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  909. return false;
  910. }
  911. return $this->checkAction ('comments post', $isPerformAction);
  912. }
  913. public function msgErrPostAllowed ()
  914. {
  915. return $this->checkActionErrorMsg('comments post');
  916. }
  917. /**
  918. * Determines whether a 'reply' action allowed or not.
  919. * @param integer or array $mixedCmt - ID or an array which describes the comment to be replied
  920. * @param boolean $isPerformAction
  921. * @return boolean
  922. */
  923. public function isReplyAllowed ($mixedCmt, $isPerformAction = false)
  924. {
  925. return $this->isPostAllowed($isPerformAction);
  926. }
  927. public function msgErrReplyAllowed ()
  928. {
  929. return $this->msgErrPostAllowed ();
  930. }
  931. public function isQuoteAllowed ($mixedCmt, $isPerformAction = false)
  932. {
  933. return $this->_bPostQuote && $this->isReplyAllowed ($mixedCmt, $isPerformAction);
  934. }
  935. public function msgErrQuoteAllowed ()
  936. {
  937. return $this->msgErrReplyAllowed ();
  938. }
  939. public function isPinAllowed ($aCmt, $isPerformAction = false)
  940. {
  941. if((int)$aCmt['cmt_parent_id'] != 0 || (int)$aCmt['cmt_pinned'] != 0)
  942. return false;
  943. if(isAdmin())
  944. return true;
  945. return $this->checkAction ('comments pin', $isPerformAction);
  946. }
  947. public function msgErrPinAllowed ()
  948. {
  949. return $this->checkActionErrorMsg('comments pin');
  950. }
  951. public function isUnpinAllowed ($aCmt, $isPerformAction = false)
  952. {
  953. if((int)$aCmt['cmt_pinned'] == 0)
  954. return false;
  955. if(isAdmin())
  956. return true;
  957. return $this->checkAction ('comments pin', $isPerformAction);
  958. }
  959. public function msgErrUnpinAllowed ()
  960. {
  961. return $this->checkActionErrorMsg('comments pin');
  962. }
  963. public function isEditAllowed ($aCmt, $isPerformAction = false)
  964. {
  965. if(isAdmin())
  966. return true;
  967. if($this->isEditAllowedAll($isPerformAction))
  968. return true;
  969. $mixedResult = BxDolService::call($this->_aSystem['module'], 'check_allowed_comments_post', array($this->getId(), $this->getSystemName()));
  970. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  971. return false;
  972. return abs($aCmt['cmt_author_id']) == $this->_getAuthorId() && $this->checkAction ('comments edit own', $isPerformAction);
  973. }
  974. public function msgErrEditAllowed ()
  975. {
  976. return $this->checkActionErrorMsg ('comments edit own');
  977. }
  978. public function isEditAllowedAll ($isPerformAction = false)
  979. {
  980. return $this->checkAction('comments edit all', $isPerformAction);
  981. }
  982. public function isRemoveAllowed ($aCmt, $isPerformAction = false)
  983. {
  984. if(isAdmin())
  985. return true;
  986. if($this->isRemoveAllowedAll($isPerformAction))
  987. return true;
  988. if(abs($aCmt['cmt_author_id']) == $this->_getAuthorId() && $this->checkAction ('comments remove own', $isPerformAction))
  989. return true;
  990. $aContentInfo = BxDolRequest::serviceExists($this->_aSystem['module'], 'get_all') ? BxDolService::call($this->_aSystem['module'], 'get_all', array(array('type' => 'id', 'id' => $this->getId()))) : array();
  991. $oModule = BxDolModule::getInstance($this->_aSystem['module']);
  992. $CNF = $oModule->_oConfig->CNF;
  993. if (isset($CNF['FIELD_AUTHOR']) && $aContentInfo[$CNF['FIELD_AUTHOR']] == $this->_getAuthorId() && $this->checkAction('comments remove in own content', $isPerformAction)){
  994. return true;
  995. }
  996. if (isset($CNF['FIELD_ALLOW_VIEW_TO']) && $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']] < 0){
  997. $iGroupProfileId = -(int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']];
  998. $oProfileContext = BxDolProfile::getInstance($iGroupProfileId);
  999. $oModule = BxDolModule::getInstance($oProfileContext->getModule());
  1000. if ($oModule->_oConfig->isRoles() && $oModule->getRole($iGroupProfileId, $this->_getAuthorId()) === BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR && $this->checkAction('comments remove in group context', $isPerformAction)){
  1001. return true;
  1002. }
  1003. }
  1004. return false;
  1005. }
  1006. public function isRemoveAllowedAll ($isPerformAction = false)
  1007. {
  1008. return $this->checkAction ('comments remove all', $isPerformAction);
  1009. }
  1010. public function msgErrRemoveAllowed ()
  1011. {
  1012. return $this->checkActionErrorMsg('comments remove own');
  1013. }
  1014. public function isMoreAllowed ($aCmt, $isPerformAction = false)
  1015. {
  1016. $oMenuManage = BxDolMenu::getObjectInstance($this->_sMenuObjManage);
  1017. $oMenuManage->setCmtsData($this, $aCmt['cmt_id']);
  1018. return $oMenuManage->isVisible();
  1019. }
  1020. public function isModerator($isPerformAction = false)
  1021. {
  1022. return $this->isEditAllowedAll($isPerformAction) || $this->isRemoveAllowedAll($isPerformAction);
  1023. }
  1024. /**
  1025. * Actions functions
  1026. */
  1027. public function actionPin ()
  1028. {
  1029. if (!$this->isEnabled())
  1030. return echoJson(array());
  1031. $iCmtId = 0;
  1032. if(bx_get('Cmt') !== false)
  1033. $iCmtId = bx_process_input(bx_get('Cmt'), BX_DATA_INT);
  1034. $aCmt = $this->getCommentRow($iCmtId);
  1035. if(empty($aCmt) || !is_array($aCmt))
  1036. return echoJson(array());
  1037. $iWay = 0;
  1038. if(bx_get('way') !== false)
  1039. $iWay = bx_process_input(bx_get('way'), BX_DATA_INT);
  1040. if(!$this->_oQuery->updateComments(array('cmt_pinned' => $iWay ? time() : 0), array('cmt_id' => $iCmtId)))
  1041. return echoJson(array());
  1042. $aBp = $aDp = array();
  1043. $this->_getParams($aBp, $aDp);
  1044. $this->_prepareParams($aBp, $aDp);
  1045. echoJson(array(
  1046. 'parent_id' => $aCmt['cmt_parent_id'],
  1047. 'per_view' => $aBp['per_view']
  1048. ));
  1049. }
  1050. public function actionGetFormPost ()
  1051. {
  1052. if (!$this->isEnabled())
  1053. return '';
  1054. $iCmtParentId = bx_get('CmtParent');
  1055. $iCmtParentId = $iCmtParentId !== false ? bx_process_input($iCmtParentId, BX_DATA_INT) : 0;
  1056. $sCmtBrowse = bx_get('CmtBrowse');
  1057. $sCmtBrowse = $sCmtBrowse !== false ? bx_process_input($sCmtBrowse, BX_DATA_TEXT) : '';
  1058. $sCmtDisplay = bx_get('CmtDisplay');
  1059. $sCmtDisplay = $sCmtDisplay !== false ? bx_process_input($sCmtDisplay, BX_DATA_TEXT) : '';
  1060. $bQuote = bx_get('CmtQuote');
  1061. $bQuote = $bQuote !== false ? bx_process_input($bQuote, BX_DATA_INT) == 1 : false;
  1062. $bMinPostForm = bx_get('CmtMinPostForm');
  1063. $bMinPostForm = $bMinPostForm !== false ? bx_process_input($bMinPostForm, BX_DATA_INT) == 1 : $this->_bMinPostForm;
  1064. return $this->getFormBoxPost(array('parent_id' => $iCmtParentId, 'type' => $sCmtBrowse), array('type' => $sCmtDisplay, 'dynamic_mode' => true, 'quote' => $bQuote, 'min_post_form' => $bMinPostForm));
  1065. }
  1066. public function actionGetFormEdit ()
  1067. {
  1068. if (!$this->isEnabled())
  1069. return echoJson(array());
  1070. $iCmtId = bx_process_input(bx_get('Cmt'), BX_DATA_INT);
  1071. echoJson($this->getFormEdit($iCmtId, array('dynamic_mode' => true)));
  1072. }
  1073. public function actionGetCmt ()
  1074. {
  1075. if(!$this->isEnabled())
  1076. return '';
  1077. if($this->isViewAllowed() !== CHECK_ACTION_RESULT_ALLOWED)
  1078. return '';
  1079. $iCmtId = bx_process_input($_REQUEST['Cmt'], BX_DATA_INT);
  1080. $sCmtBrowse = isset($_REQUEST['CmtBrowse']) ? bx_process_input($_REQUEST['CmtBrowse'], BX_DATA_TEXT) : '';
  1081. $sCmtDisplay = isset($_REQUEST['CmtDisplay']) ? bx_process_input($_REQUEST['CmtDisplay'], BX_DATA_TEXT) : '';
  1082. $aCmt = $this->getCommentRow($iCmtId);
  1083. echoJson([
  1084. 'parent_id' => $aCmt['cmt_parent_id'],
  1085. 'vparent_id' => $aCmt['cmt_parent_id'],
  1086. 'content' => $this->getComment($aCmt, ['type' => $sCmtBrowse], ['type' => $sCmtDisplay, 'dynamic_mode' => true])
  1087. ]);
  1088. }
  1089. public function actionGetCmts ()
  1090. {
  1091. if (!$this->isEnabled())
  1092. return '';
  1093. if($this->isViewAllowed() !== CHECK_ACTION_RESULT_ALLOWED)
  1094. return '';
  1095. $aBp = $aDp = array();
  1096. $this->_getParams($aBp, $aDp);
  1097. $aDp['dynamic_mode'] = true;
  1098. //--- Beg: Using pregenerated structure
  1099. if(isset($aDp['structure']) && $aDp['structure'] === true) {
  1100. $iCmtId = isset($aBp['parent_id']) ? (int)$aBp['parent_id'] : 0;
  1101. $mixedStructure = $this->getCommentStructure($iCmtId, $aBp, $aDp);
  1102. if($mixedStructure === false || empty($mixedStructure[$iCmtId]['items']))
  1103. return '';
  1104. $aDp['structure'] = $mixedStructure[$iCmtId]['items'];
  1105. return $this->getCommentsByStructure($aBp, $aDp);
  1106. }
  1107. //--- End: Using pregenerated structure
  1108. else
  1109. return $this->{'getComments' . ($aBp['pinned'] ? 'Pinned' : '')}($aBp, $aDp);
  1110. }
  1111. public function actionSubmitPostForm()
  1112. {
  1113. if(!$this->isEnabled())
  1114. return echoJson(array());
  1115. $iCmtParentId = 0;
  1116. if(bx_get('cmt_parent_id') !== false)
  1117. $iCmtParentId = bx_process_input(bx_get('cmt_parent_id'), BX_DATA_INT);
  1118. echoJson($this->getFormPost($iCmtParentId, array('dynamic_mode' => true)));
  1119. }
  1120. public function actionSubmitEditForm()
  1121. {
  1122. if (!$this->isEnabled()) {
  1123. echoJson(array());
  1124. return;
  1125. };
  1126. $iCmtId = 0;
  1127. if(bx_get('cmt_id') !== false)
  1128. $iCmtId = bx_process_input(bx_get('cmt_id'), BX_DATA_INT);
  1129. echoJson($this->getFormEdit($iCmtId, array('dynamic_mode' => true)));
  1130. }
  1131. public function actionRemove()
  1132. {
  1133. if (!$this->isEnabled())
  1134. return echoJson([]);
  1135. $iCmtId = 0;
  1136. if(bx_get('Cmt') !== false)
  1137. $iCmtId = bx_process_input(bx_get('Cmt'), BX_DATA_INT);
  1138. echoJson($this->remove($iCmtId));
  1139. }
  1140. public function remove($iCmtId)
  1141. {
  1142. $aCmt = $this->_oQuery->getCommentsBy(array('type' => 'id', 'id' => $iCmtId));
  1143. if(!$aCmt)
  1144. return array('msg' => _t('_No such comment'));
  1145. $iObjId = $this->getId();
  1146. if(!$iObjId){
  1147. $this->setId($aCmt['cmt_object_id']);
  1148. $iObjId = $aCmt['cmt_object_id'];
  1149. }
  1150. $iObjAthrId = $this->getObjectAuthorId($iObjId);
  1151. $iObjAthrPrivacyView = $this->getObjectPrivacyView($iObjId);
  1152. if($aCmt['cmt_replies'] > 0) {
  1153. if(!$this->isModerator())
  1154. return array('msg' => _t('_Can not delete comments with replies'));
  1155. $aReplies = $this->_oQuery->getCommentsBy(array('type' => 'parent_id', 'parent_id' => $iCmtId));
  1156. foreach($aReplies as $aReply)
  1157. $this->remove($aReply['cmt_id']);
  1158. }
  1159. $iPerformerId = $this->_getAuthorId();
  1160. if(!$this->isRemoveAllowed($aCmt))
  1161. return array('msg' => $aCmt['cmt_author_id'] == $iPerformerId ? strip_tags($this->msgErrRemoveAllowed()) : _t('_Access denied'));
  1162. $iCmtPrntId = (int)$aCmt['cmt_parent_id'];
  1163. if(!$this->_oQuery->removeComment($iObjId, $iCmtId, $iCmtPrntId))
  1164. return array('msg' => _t('_cmt_err_cannot_perform_action'));
  1165. $this->_triggerComment();
  1166. $oStorage = BxDolStorage::getObjectInstance($this->getStorageObjectName());
  1167. $aImages = $this->_oQuery->getFiles($this->_aSystem['system_id'], $iCmtId);
  1168. foreach($aImages as $aImage)
  1169. $oStorage->deleteFile($aImage['image_id']);
  1170. $this->_oQuery->deleteImages($this->_aSystem['system_id'], $iCmtId);
  1171. $this->isRemoveAllowed($aCmt, true);
  1172. $this->deleteVotes($iCmtId);
  1173. $this->deleteReactions($iCmtId);
  1174. $this->deleteScores($iCmtId);
  1175. $this->deleteReports($iCmtId);
  1176. $this->deleteMetaInfo($iCmtId);
  1177. $aAuditParams = $this->_prepareAuditParams($iCmtId, array('comment_author_id' => $aCmt['cmt_author_id'], 'comment_text' => $aCmt['cmt_text']));
  1178. bx_audit($iObjId, $this->_aSystem['module'], '_sys_audit_action_delete_comment', $aAuditParams);
  1179. $aAlertParams = $this->_prepareAlertParams($aCmt);
  1180. bx_alert($this->_sSystem, 'commentRemoved', $iObjId, $iPerformerId, $aAlertParams);
  1181. bx_alert('comment', 'deleted', $iCmtId, $iPerformerId, $aAlertParams);
  1182. if(!empty($iCmtPrntId)) {
  1183. $aCmtPrnt = $this->_oQuery->getCommentSimple($iObjId, $iCmtPrntId);
  1184. if(!empty($aCmtPrnt) && is_array($aCmtPrnt)) {
  1185. $aAlertParamsReply = $this->_prepareAlertParamsReply($aCmt, $aCmtPrnt);
  1186. bx_alert($this->_sSystem, 'replyRemoved', $iCmtPrntId, $iPerformerId, $aAlertParamsReply);
  1187. bx_alert('reply', 'deleted', $iCmtId, $iPerformerId, $aAlertParamsReply);
  1188. }
  1189. }
  1190. $this->deleteUniqueIds($iCmtId);
  1191. $iCount = (int)$this->getCommentsCountAll(0, true);
  1192. return [
  1193. 'id' => $iCmtId,
  1194. 'count' => $iCount,
  1195. 'countf' => $iCount > 0 ? $this->getCounter() : ''
  1196. ];
  1197. }
  1198. public function add($aValues)
  1199. {
  1200. return $this->_getFormAdd($aValues);
  1201. }
  1202. public function actionResumeLiveUpdate()
  1203. {
  1204. $sKey = $this->getNotificationId();
  1205. bx_import('BxDolSession');
  1206. BxDolSession::getInstance()->unsetValue($sKey);
  1207. }
  1208. public function actionPauseLiveUpdate()
  1209. {
  1210. $sKey = $this->getNotificationId();
  1211. bx_import('BxDolSession');
  1212. BxDolSession::getInstance()->setValue($sKey, 1);
  1213. }
  1214. public function actionGetSiblingFiles()
  1215. {
  1216. $iFileId = 0;
  1217. if(bx_get('file_id') !== false)
  1218. $iFileId = bx_process_input(bx_get('file_id'), BX_DATA_INT);
  1219. if(!($aFileInfo = $this->_oQuery->getFileInfoById((int)$iFileId)))
  1220. return echoJson(['error' => _t('_sys_txt_error_occured')]);
  1221. $aSiblings = [];
  1222. $aFiles = $this->_oQuery->getFiles($aFileInfo['system_id'], $aFileInfo['cmt_id']);
  1223. foreach($aFiles as $iIndex => $aFile) {
  1224. if($aFile['id'] != $iFileId)
  1225. continue;
  1226. $aSiblings['prev'] = isset($aFiles[$iIndex - 1]) ? $aFiles[$iIndex - 1] : false;
  1227. $aSiblings['next'] = isset($aFiles[$iIndex + 1]) ? $aFiles[$iIndex + 1] : false;
  1228. }
  1229. $oStorage = BxDolStorage::getObjectInstance($this->getStorageObjectName());
  1230. $oTranscoder = BxDolTranscoderImage::getObjectInstance($this->getTranscoderPreviewName());
  1231. foreach($aSiblings as $sSibling => $aSibling) {
  1232. if(!$aSibling)
  1233. continue;
  1234. $sFile = '';
  1235. if($oTranscoder && $oTranscoder->isMimeTypeSupported($aSibling['mime_type']))
  1236. $sFile = $oStorage->getFileUrlById($aSibling['image_id']);
  1237. else
  1238. $sFile = $this->_oTemplate->getIconUrl($oStorage->getIconNameByFileName($aFile['file_name']));
  1239. $aImageInfo = BxDolImageResize::getInstance()->getImageSize($sFile);
  1240. $iWidth = $iHeight = 0;
  1241. if(isset($aImageInfo['w']))
  1242. $iWidth = (int)$aImageInfo['w'];
  1243. if(isset($aImageInfo['h']))
  1244. $iHeight = (int)$aImageInfo['h'];
  1245. $aSiblings[$sSibling] = array_merge($aSibling, [
  1246. 'file' => $sFile,
  1247. 'file_id' => $aSibling['id'],
  1248. 'w' => $iWidth,
  1249. 'h' => $iHeight
  1250. ]);
  1251. }
  1252. return echoJson($aSiblings);
  1253. }
  1254. public function onPostAfter($iCmtId, $aDp = [])
  1255. {
  1256. $iObjId = (int)$this->getId();
  1257. $aCmt = $this->_oQuery->getCommentSimple($iObjId, $iCmtId);
  1258. if(empty($aCmt) || !is_array($aCmt))
  1259. return false;
  1260. $iCmtPrntId = (int)$aCmt['cmt_parent_id'];
  1261. $iPerformerId = (int)$aCmt['cmt_author_id'];
  1262. $aAlertParams = $this->_prepareAlertParams($aCmt);
  1263. bx_alert($this->_sSystem, 'commentPost', $iObjId, $iPerformerId, $aAlertParams);
  1264. bx_alert('comment', 'added', $iCmtId, $iPerformerId, $aAlertParams);
  1265. $aAuditParams = $this->_prepareAuditParams($iCmtId, array('comment_author_id' => $aCmt['cmt_author_id'], 'comment_text' => $aCmt['cmt_text']));
  1266. bx_audit($iObjId, $this->_aSystem['module'], '_sys_audit_action_add_comment', $aAuditParams);
  1267. if(!empty($iCmtPrntId)) {
  1268. $aCmtPrnt = $this->_oQuery->getCommentSimple($iObjId, $iCmtPrntId);
  1269. if(!empty($aCmtPrnt) && is_array($aCmtPrnt)) {
  1270. $aAlertParamsReply = $this->_prepareAlertParamsReply($aCmt, $aCmtPrnt);
  1271. bx_alert($this->_sSystem, 'replyPost', $iCmtPrntId, $iPerformerId, $aAlertParamsReply);
  1272. bx_alert('comment', 'replied', $iCmtId, $iPerformerId, $aAlertParamsReply);
  1273. }
  1274. }
  1275. $iCount = (int)$this->getCommentsCountAll(0, true);
  1276. $aResult = [
  1277. 'id' => $iCmtId,
  1278. 'parent_id' => $iCmtPrntId,
  1279. 'count' => $iCount,
  1280. 'countf' => $iCount > 0 ? $this->getCounter(['show_script' => false]) : ''
  1281. ];
  1282. if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnabled())
  1283. $oSockets->sendEvent($this->getSocketName(), $iObjId, 'comment_added', json_encode(array_merge($aResult, [
  1284. 'author_id' => $iPerformerId,
  1285. 'anchor' => $this->getItemAnchor($iCmtId)
  1286. ])));
  1287. return $aResult;
  1288. }
  1289. public function onEditAfter($iCmtId, $aDp = [])
  1290. {
  1291. $aCmt = $this->getCommentRow($iCmtId);
  1292. if(empty($aCmt) || !is_array($aCmt))
  1293. return false;
  1294. $aBp = [];
  1295. $this->_getParams($aBp, $aDp);
  1296. $iObjId = (int)$this->getId();
  1297. $iPerformerId = $this->_getAuthorId();
  1298. $aAlertParams = $this->_prepareAlertParams($aCmt);
  1299. bx_alert($this->_sSystem, 'commentUpdated', $iObjId, $iPerformerId, $aAlertParams);
  1300. bx_alert('comment', 'edited', $iCmtId, $iPerformerId, $aAlertParams);
  1301. $aAuditParams = $this->_prepareAuditParams($iCmtId, ['comment_author_id' => $aCmt['cmt_author_id'], 'comment_text' => $aCmt['cmt_text']]);
  1302. bx_audit($iObjId, $this->_aSystem['module'], '_sys_audit_action_edit_comment', $aAuditParams);
  1303. return ['id' => $iCmtId, 'content' => $this->_getContent($aCmt, $aBp, $aDp)];
  1304. }
  1305. public function serviceGetAuthor ($iContentId)
  1306. {
  1307. $aComment = $this->_oQuery->getCommentsBy(array('type' => 'id', 'id' => $iContentId));
  1308. $this->setId($aComment['cmt_object_id']);
  1309. return $aComment['cmt_author_id'];
  1310. }
  1311. public function serviceGetDateAdded ($iContentId)
  1312. {
  1313. $aComment = $this->_oQuery->getCommentsBy(array('type' => 'id', 'id' => $iContentId));
  1314. $this->setId($aComment['cmt_object_id']);
  1315. return $aComment['cmt_time'];
  1316. }
  1317. public function serviceGetDateChanged ($iContentId)
  1318. {
  1319. return 0;
  1320. }
  1321. public function serviceGetLink ($iContentId)
  1322. {
  1323. $aComment = $this->_oQuery->getCommentsBy(array('type' => 'id', 'id' => $iContentId));
  1324. $this->setId($aComment['cmt_object_id']);
  1325. return $this->getViewUrl($iContentId);
  1326. }
  1327. public function serviceGetTitle ($iContentId)
  1328. {
  1329. return '';
  1330. }
  1331. public function serviceGetText ($iContentId)
  1332. {
  1333. $aComment = $this->_oQuery->getCommentsBy(array('type' => 'id', 'id' => $iContentId));
  1334. $this->setId($aComment['cmt_object_id']);
  1335. return $aComment['cmt_text'];
  1336. }
  1337. public function serviceGetThumb ($iContentId)
  1338. {
  1339. return '';
  1340. }
  1341. public function serviceGetInfo ($iContentId, $bSearchableFieldsOnly = true)
  1342. {
  1343. $aComment = $this->_oQuery->getCommentsBy(array('type' => 'id', 'id' => $iContentId));
  1344. $this->setId($aComment['cmt_object_id']);
  1345. return BxDolContentInfo::formatFields($aComment);
  1346. }
  1347. public function serviceGetSearchResultUnit ($iContentId, $sUnitTemplate = '')
  1348. {
  1349. $aComment = $this->_oQuery->getCommentsBy(array('type' => 'id', 'id' => $iContentId));
  1350. if(empty($aComment) || !is_array($aComment))
  1351. return '';
  1352. $this->setId($aComment['cmt_object_id']);
  1353. return $this->getComment($aComment, array(), array('type' => BX_CMT_DISPLAY_FLAT, 'view_only' => true));
  1354. }
  1355. public function serviceGetAll ($aParams = array())
  1356. {
  1357. if(empty($aParams) || !is_array($aParams))
  1358. $aParams = array('type' => 'all');
  1359. return $this->_oQuery->getCommentsBy($aParams);
  1360. }
  1361. public function serviceGetSearchableFieldsExtended($aInputsAdd = array())
  1362. {
  1363. $oForm = BxDolForm::getObjectInstance('sys_comment', 'sys_comment_post', $this->_oTemplate);
  1364. if(!$oForm)
  1365. return array();
  1366. $aSrchNamesExcept = array();
  1367. $aSrchCaptionsSystem = array(
  1368. 'cmt_author_id' => '_sys_form_comment_input_caption_system_cmt_author_id',
  1369. 'cmt_text' => '_sys_form_comment_input_caption_system_cmt_text'
  1370. );
  1371. $aSrchCaptions = array(
  1372. 'cmt_author_id' => '_sys_form_comment_input_caption_cmt_author_id',
  1373. 'cmt_text' => '_sys_form_comment_input_caption_cmt_text'
  1374. );
  1375. $aResult = array(
  1376. 'cmt_author_id' => array(
  1377. 'type' => 'text_auto',
  1378. 'caption_system' => $aSrchCaptionsSystem['cmt_author_id'],
  1379. 'caption' => $aSrchCaptions['cmt_author_id'],
  1380. 'info' => '',
  1381. 'value' => '',
  1382. 'values' => '',
  1383. 'pass' => ''
  1384. )
  1385. );
  1386. $aInputs = $oForm->aInputs;
  1387. if(!empty($aInputsAdd) && is_array($aInputsAdd))
  1388. $aInputs = array_merge($aInputs, $aInputsAdd);
  1389. foreach($aInputs as $aInput)
  1390. if(in_array($aInput['type'], BxDolSearchExtended::$SEARCHABLE_TYPES) && !in_array($aInput['name'], $aSrchNamesExcept))
  1391. $aResult[$aInput['name']] = array(
  1392. 'type' => $aInput['type'],
  1393. 'caption_system' => !empty($aInput['caption_system_src']) ? $aInput['caption_system_src'] : '',
  1394. 'caption' => !empty($aInput['caption_src']) ? $aInput['caption_src'] : (!empty($aSrchCaptions[$aInput['name']]) ? $aSrchCaptions[$aInput['name']] : ''),
  1395. 'info' => !empty($aInput['info_src']) ? $aInput['info_src'] : '',
  1396. 'value' => !empty($aInput['value']) ? $aInput['value'] : '',
  1397. 'values' => !empty($aInput['values_src']) ? $aInput['values_src'] : '',
  1398. 'pass' => !empty($aInput['db']['pass']) ? $aInput['db']['pass'] : ''
  1399. );
  1400. return $aResult;
  1401. }
  1402. /**
  1403. * Overwrite this method and register transcoder(s) if comments object uses custom transcoder(s),
  1404. * which differs from default one 'sys_cmts_images_preview'
  1405. */
  1406. public function registerTranscoders()
  1407. {}
  1408. /**
  1409. * Overwrite this method and unregister transcoder(s) if comments object uses custom transcoder(s),
  1410. * which differs from default one 'sys_cmts_images_preview'
  1411. */
  1412. public function unregisterTranscoders()
  1413. {}
  1414. public function serviceGetSearchResultExtended($aParams, $iStart = 0, $iPerPage = 0, $bFilterMode = false)
  1415. {
  1416. if((empty($aParams) || !is_array($aParams)) && !$bFilterMode)
  1417. return array();
  1418. return $this->_oQuery->getCommentsBy(array('type' => 'search_ids', 'search_params' => $aParams, 'start' => $iStart, 'per_page' => $iPerPage));
  1419. }
  1420. public function getAuthorInfo($iAuthorId = 0)
  1421. {
  1422. return $this->_getAuthorInfo($iAuthorId);
  1423. }
  1424. public function getParams(&$aBp, &$aDp)
  1425. {
  1426. return $this->_getParams($aBp, $aDp);
  1427. }
  1428. public function prepareParams(&$aBp, &$aDp)
  1429. {
  1430. return $this->_prepareParams($aBp, $aDp);
  1431. }
  1432. /**
  1433. * Internal functions
  1434. */
  1435. protected function _getAuthorId ()
  1436. {
  1437. return isMember() ? bx_get_logged_profile_id() : 0;
  1438. }
  1439. protected function _getAuthorPassword ()
  1440. {
  1441. return getLoggedPassword();
  1442. }
  1443. protected function _getAuthorIp ()
  1444. {
  1445. return getVisitorIP();
  1446. }
  1447. protected function _getAuthorInfo($iAuthorId = 0)
  1448. {
  1449. $iUserId = $this->_getAuthorId();
  1450. $iAuthorId = (int)$iAuthorId;
  1451. $oProfile = $this->_getAuthorObject($iAuthorId);
  1452. if (!$oProfile->isActive() && !isAdmin() && !BxDolAcl::getInstance()->isMemberLevelInSet(array(MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR)))
  1453. $oProfile = BxDolProfileUndefined::getInstance();
  1454. return array(
  1455. $oProfile->getDisplayName(),
  1456. $oProfile->getUrl(),
  1457. $oProfile->getThumb(),
  1458. $oProfile->getUnit(0, array('template' => 'unit_wo_info')),
  1459. $oProfile->getBadges()
  1460. );
  1461. }
  1462. protected function _getAuthorObject($iAuthorId = 0)
  1463. {
  1464. return BxDolProfile::getInstanceMagic((int)$iAuthorId);
  1465. }
  1466. protected function _getFormObject($sAction = BX_CMT_ACTION_POST)
  1467. {
  1468. $sDisplayName = '_sFormDisplay' . ucfirst($sAction);
  1469. return BxDolForm::getObjectInstance($this->_sFormObject, $this->$sDisplayName, false, $this->_sSystem);
  1470. }
  1471. protected function _unsetFormObject($sAction = BX_CMT_ACTION_POST)
  1472. {
  1473. $sDisplayName = '_sFormDisplay' . ucfirst($sAction);
  1474. return BxDolForm::unsetObjectInstance($this->_sFormObject, $this->$sDisplayName, false, $this->_sSystem);
  1475. }
  1476. protected function _getParams(&$aBp, &$aDp)
  1477. {
  1478. //--- Process 'Browse' params.
  1479. $aBp['parent_id'] = isset($aBp['parent_id']) ? (int)$aBp['parent_id'] : 0;
  1480. $aBp['vparent_id'] = isset($aBp['vparent_id']) ? (int)$aBp['vparent_id'] : 0;
  1481. if(bx_get('CmtParent') !== false)
  1482. $aBp['vparent_id'] = bx_process_input(bx_get('CmtParent'), BX_DATA_INT);
  1483. $aBp['type'] = isset($aBp['type']) ? $aBp['type'] : '';
  1484. if(bx_get('CmtBrowse') !== false)
  1485. $aBp['type'] = bx_process_input(bx_get('CmtBrowse'), BX_DATA_TEXT);
  1486. $aBp['filter'] = isset($aBp['filter']) ? $aBp['filter'] : '';
  1487. if(bx_get('CmtFilter') !== false)
  1488. $aBp['filter'] = bx_process_input(bx_get('CmtFilter'), BX_DATA_TEXT);
  1489. $aBp['start'] = isset($aBp['start']) ? (int)$aBp['start'] : -1;
  1490. if(bx_get('CmtStart') !== false)
  1491. $aBp['start'] = bx_process_input($_REQUEST['CmtStart'], BX_DATA_INT);
  1492. $aBp['per_view'] = isset($aBp['per_view']) ? (int)$aBp['per_view'] : -1;
  1493. if(bx_get('CmtPerView') !== false)
  1494. $aBp['per_view'] = bx_process_input($_REQUEST['CmtPerView'], BX_DATA_INT);
  1495. $aBp['pinned'] = isset($aBp['pinned']) ? (int)$aBp['pinned'] : 0;
  1496. if(bx_get('CmtPinned') !== false)
  1497. $aBp['pinned'] = bx_process_input(bx_get('CmtPinned'), BX_DATA_INT);
  1498. //--- Process 'Display' params.
  1499. $aDp['type'] = isset($aDp['type']) ? $aDp['type'] : '';
  1500. if(bx_get('CmtDisplay') !== false)
  1501. $aDp['type'] = bx_process_input($_REQUEST['CmtDisplay'], BX_DATA_TEXT);
  1502. if(bx_get('CmtDisplayStructure') !== false) {
  1503. $aDp['structure'] = bx_process_input($_REQUEST['CmtDisplayStructure'], BX_DATA_INT) == 1;
  1504. if($aDp['structure'] && bx_get('CmtParent') !== false)
  1505. $aBp['parent_id'] = bx_process_input(bx_get('CmtParent'), BX_DATA_INT);
  1506. }
  1507. $aDp['blink'] = isset($aDp['blink']) ? $aDp['blink'] : '';
  1508. if(bx_get('CmtBlink') !== false)
  1509. $aDp['blink'] = bx_process_input($_REQUEST['CmtBlink'], BX_DATA_TEXT);
  1510. $aDp['quote'] = isset($aDp['quote']) ? (int)$aDp['quote'] : 0;
  1511. if(bx_get('CmtQuote') !== false)
  1512. $aDp['quote'] = bx_process_input($_REQUEST['CmtQuote'], BX_DATA_INT);
  1513. $aDp['min_post_form'] = isset($aDp['min_post_form']) ? (bool)$aDp['min_post_form'] : $this->_bMinPostForm;
  1514. if(bx_get('CmtMinPostForm') !== false)
  1515. $aDp['min_post_form'] = bx_process_input(bx_get('CmtMinPostForm'), BX_DATA_INT) == 1;
  1516. $aDp['in_designbox'] = isset($aDp['in_designbox']) ? (bool)$aDp['in_designbox'] : true;
  1517. $aDp['dynamic_mode'] = isset($aDp['dynamic_mode']) ? (bool)$aDp['dynamic_mode'] : false;
  1518. $aDp['show_empty'] = isset($aDp['show_empty']) ? (bool)$aDp['show_empty'] : false;
  1519. }
  1520. protected function _prepareAlertParams($aCmt)
  1521. {
  1522. $iObjId = (int)$this->getId();
  1523. $iObjAthrId = $this->getObjectAuthorId($iObjId);
  1524. $iObjAthrPrivacyView = $this->getObjectPrivacyView($iObjId);
  1525. $iCmtId = (int)$aCmt['cmt_id'];
  1526. $iCmtUniqId = $this->getCommentUniqId($iCmtId);
  1527. $iCmtCf = isset($aCmt['cmt_cf']) ? (int)$aCmt['cmt_cf'] : BxDolContentFilter::getInstance()->getDefaultValue();
  1528. return array(
  1529. 'source' => 'sys_cmts_' . $iCmtUniqId,
  1530. 'object_system' => $this->_sSystem,
  1531. 'object_id' => $iObjId,
  1532. 'object_author_id' => $iObjAthrId,
  1533. 'comment_id' => $iCmtId,
  1534. 'comment_uniq_id' => $iCmtUniqId,
  1535. 'comment_author_id' => $aCmt['cmt_author_id'],
  1536. 'comment_text' => $aCmt['cmt_text'],
  1537. 'privacy_view' => $iObjAthrPrivacyView,
  1538. 'cf' => $iCmtCf
  1539. );
  1540. }
  1541. protected function _prepareAlertParamsReply($aCmt, $aCmtPrnt)
  1542. {
  1543. $iObjId = (int)$this->getId();
  1544. $iObjAthrId = $this->getObjectAuthorId($iObjId);
  1545. $iObjAthrPrivacyView = $this->getObjectPrivacyView($iObjId);
  1546. $iCmtPrntId = (int)$aCmt['cmt_parent_id'];
  1547. $iCmtPrntUniqId = $this->getCommentUniqId($iCmtPrntId);
  1548. $iCmtId = (int)$aCmt['cmt_id'];
  1549. $iCmtUniqId = $this->getCommentUniqId($iCmtId);
  1550. return array(
  1551. 'source' => 'sys_cmts_' . $iCmtUniqId,
  1552. 'object_system' => $this->_sSystem,
  1553. 'object_id' => $iObjId,
  1554. 'object_author_id' => $iObjAthrId,
  1555. 'parent_id' => $iCmtPrntId,
  1556. 'parent_uniq_id' => $iCmtPrntUniqId,
  1557. 'parent_author_id' => $aCmtPrnt['cmt_author_id'],
  1558. 'comment_id' => $iCmtId,
  1559. 'comment_uniq_id' => $iCmtUniqId,
  1560. 'comment_author_id' => $aCmt['cmt_author_id'],
  1561. 'comment_text' => $aCmt['cmt_text'],
  1562. 'privacy_view' => $iObjAthrPrivacyView,
  1563. );
  1564. }
  1565. protected function _prepareAuditParams($iId, $aData)
  1566. {
  1567. $sModule = $this->_aSystem['module'];
  1568. $oModule = BxDolModule::getInstance($sModule);
  1569. $CNF = isset($oModule->_oConfig->CNF) ? $oModule->_oConfig->CNF : array();
  1570. $aContentInfo = BxDolRequest::serviceExists($sModule, 'get_all') ? BxDolService::call($sModule, 'get_all', array(array('type' => 'id', 'id' => $this->getId()))) : array();
  1571. $iContextId = 0;
  1572. if (!empty($aContentInfo)){
  1573. $iContextId = isset($CNF['FIELD_ALLOW_VIEW_TO']) && (!empty($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) && (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']] < 0) ? - $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']] : 0;
  1574. }
  1575. $AuditParams = array(
  1576. 'content_title' => $this->getObjectTitle() ,
  1577. 'context_profile_id' => $iContextId,
  1578. 'content_info_object' => isset($CNF['OBJECT_CMTS_CONTENT_INFO']) ? $CNF['OBJECT_CMTS_CONTENT_INFO'] : '',
  1579. 'data' => array_merge(array('comment_id' => $iId), $aData)
  1580. );
  1581. if ($iContextId > 0)
  1582. $AuditParams['context_profile_title'] = BxDolProfile::getInstance($iContextId)->getDisplayName();
  1583. return $AuditParams;
  1584. }
  1585. protected function _prepareTextForOutput ($s, $iCmtId = 0)
  1586. {
  1587. $iDataAction = !$this->isHtml() ? BX_DATA_TEXT_MULTILINE : BX_DATA_HTML;
  1588. $s = bx_process_output($s, $iDataAction);
  1589. $s = bx_linkify_html($s, 'class="' . BX_DOL_LINK_CLASS . '"');
  1590. if($iCmtId && $this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  1591. $s = $oMetatags->metaParse($this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId), $s);
  1592. return $s;
  1593. }
  1594. protected function _prepareStructureBp($sDType, &$aBp)
  1595. {
  1596. $aBp['type'] = !empty($aBp['type']) ? $aBp['type'] : $this->_sBrowseType;
  1597. $aBp['filter'] = !empty($aBp['filter']) ? $aBp['filter'] : $this->_sBrowseFilter;
  1598. $aBp['parent_id'] = isset($aBp['parent_id']) ? $aBp['parent_id'] : 0;
  1599. $aBp['start'] = isset($aBp['start']) ? $aBp['start'] : -1;
  1600. $aBp['init_view'] = isset($aBp['init_view']) ? $aBp['init_view'] : -1;
  1601. $aBp['per_view'] = isset($aBp['per_view']) ? $aBp['per_view'] : -1;
  1602. $aBp['order']['by'] = isset($aBp['order_by']) ? $aBp['order_by'] : $this->_aOrder['by'];
  1603. $aBp['order']['way'] = isset($aBp['order_way']) ? $aBp['order_way'] : $this->_aOrder['way'];
  1604. if($aBp['per_view'] == -1)
  1605. switch($sDType) {
  1606. case BX_CMT_DISPLAY_FLAT:
  1607. $aBp['per_view'] = $this->getPerView(0);
  1608. break;
  1609. case BX_CMT_DISPLAY_THREADED:
  1610. $aBp['per_view'] = $this->getPerView($aBp['parent_id']);
  1611. break;
  1612. }
  1613. }
  1614. protected function _prepareParams(&$aBp, &$aDp)
  1615. {
  1616. $aBp['type'] = !empty($aBp['type']) ? $aBp['type'] : $this->_sBrowseType;
  1617. $aBp['filter'] = !empty($aBp['filter']) ? $aBp['filter'] : $this->_sBrowseFilter;
  1618. $aBp['parent_id'] = isset($aBp['parent_id']) ? $aBp['parent_id'] : 0;
  1619. $aBp['start'] = isset($aBp['start']) ? $aBp['start'] : -1;
  1620. $aBp['init_view'] = isset($aBp['init_view']) ? $aBp['init_view'] : -1;
  1621. $aBp['per_view'] = isset($aBp['per_view']) ? $aBp['per_view'] : -1;
  1622. $aBp['pinned'] = isset($aBp['pinned']) ? (int)$aBp['pinned'] : 0;
  1623. $aBp['order']['by'] = isset($aBp['order_by']) ? $aBp['order_by'] : $this->_aOrder['by'];
  1624. $aBp['order']['way'] = isset($aBp['order_way']) ? $aBp['order_way'] : $this->_aOrder['way'];
  1625. $aDp['type'] = !empty($aDp['type']) ? $aDp['type'] : $this->_sDisplayType;
  1626. $aDp['blink'] = !empty($aDp['blink']) ? $aDp['blink'] : array();
  1627. if(!is_array($aDp['blink']))
  1628. $aDp['blink'] = explode(',', $aDp['blink']);
  1629. $aDp['quote'] = !empty($aDp['quote']) ? (int)$aDp['quote'] : 0;
  1630. if(!isset($aDp['min_post_form']))
  1631. $aDp['min_post_form'] = $this->_bMinPostForm;
  1632. switch($aDp['type']) {
  1633. case BX_CMT_DISPLAY_FLAT:
  1634. $aBp['vparent_id'] = -1;
  1635. $aBp['per_view'] = $aBp['per_view'] != -1 ? $aBp['per_view'] : $this->getPerView(0);
  1636. break;
  1637. case BX_CMT_DISPLAY_THREADED:
  1638. $iParent = 0;
  1639. if(isset($aBp['vparent_id']))
  1640. $iParent = $aBp['vparent_id'];
  1641. else if(isset($aBp['parent_id']))
  1642. $iParent = $aBp['parent_id'];
  1643. $aBp['per_view'] = $aBp['per_view'] != -1 ? $aBp['per_view'] : $this->getPerView($iParent);
  1644. break;
  1645. }
  1646. switch ($aBp['type']) {
  1647. case BX_CMT_BROWSE_POPULAR:
  1648. $aBp['order'] = array(
  1649. 'by' => BX_CMT_ORDER_BY_POPULAR,
  1650. 'way' => BX_CMT_ORDER_WAY_DESC
  1651. );
  1652. break;
  1653. }
  1654. if(!isset($aBp['count']))
  1655. $aBp['count'] = $this->getCommentsCount($this->_iId, $aBp['vparent_id'], $aBp['filter']);
  1656. if($aBp['start'] != -1)
  1657. return;
  1658. $aBp['start'] = 0;
  1659. if($aBp['type'] == BX_CMT_BROWSE_TAIL) {
  1660. $sPerView = ($aBp['init_view'] != -1 ? 'init' : 'per') . '_view';
  1661. $aBp['start'] = $aBp['count'] - $aBp[$sPerView];
  1662. if($aBp['start'] < 0) {
  1663. $aBp[$sPerView] += $aBp['start'];
  1664. $aBp['start'] = 0;
  1665. }
  1666. }
  1667. $this->_setUserChoice($aDp['type'], $aBp['type'], $aBp['filter']);
  1668. }
  1669. protected function _triggerComment()
  1670. {
  1671. if(!$this->_aSystem['trigger_table'] || !$this->_aSystem['trigger_field_id'] || !$this->_aSystem['trigger_field_comments'])
  1672. return false;
  1673. $iId = $this->getId();
  1674. if(!$iId)
  1675. return false;
  1676. $iCount = $this->getCommentsCount($iId);
  1677. return $this->_oQuery->updateTriggerTable($iId, $iCount);
  1678. }
  1679. /**
  1680. * Replace provided markers in a string
  1681. * @param $mixed string or array to replace markers in
  1682. * @return string where all occured markers are replaced
  1683. */
  1684. protected function _replaceMarkers ($mixed)
  1685. {
  1686. return bx_replace_markers($mixed, $this->_aMarkers);
  1687. }
  1688. protected function _getUserChoice()
  1689. {
  1690. $mixedDp = $mixedBpType = $mixedBpFilter = false;
  1691. if(!isLogged())
  1692. return array($mixedDp, $mixedBpType, $mixedBpFilter);
  1693. $iUserId = $this->_getAuthorId();
  1694. $oSession = BxDolSession::getInstance();
  1695. $mixedDp = $oSession->getValue($this->_sDpSessionKey . $iUserId);
  1696. $mixedBpType = $oSession->getValue($this->_sBpSessionKeyType . $iUserId);
  1697. $mixedBpFilter = $oSession->getValue($this->_sBpSessionKeyFilter . $iUserId);
  1698. return array($mixedDp, $mixedBpType, $mixedBpFilter);
  1699. }
  1700. protected function _setUserChoice($sDp, $sBpType, $sBpFilter)
  1701. {
  1702. if(!isLogged())
  1703. return;
  1704. $iUserId = $this->_getAuthorId();
  1705. $oSession = BxDolSession::getInstance();
  1706. if(!empty($sDp))
  1707. $oSession->setValue($this->_sDpSessionKey . $iUserId, $sDp);
  1708. if(!empty($sBpType))
  1709. $oSession->setValue($this->_sBpSessionKeyType . $iUserId, $sBpType);
  1710. if(!empty($sBpFilter))
  1711. $oSession->setValue($this->_sBpSessionKeyFilter . $iUserId, $sBpFilter);
  1712. }
  1713. protected function _sendNotificationEmail($iCmtId, $iCmtParentId)
  1714. {
  1715. $aCmt = $this->getCommentRow($iCmtId);
  1716. $aCmtParent = $this->getCommentRow($iCmtParentId);
  1717. if(empty($aCmt) || !is_array($aCmt) || empty($aCmtParent) || !is_array($aCmtParent) || (int)$aCmt['cmt_author_id'] == (int)$aCmtParent['cmt_author_id'])
  1718. return;
  1719. $oProfile = $this->_getAuthorObject($aCmtParent['cmt_author_id']);
  1720. if($oProfile instanceof BxDolProfileUndefined)
  1721. return;
  1722. $iAccount = $oProfile->getAccountId();
  1723. $aAccount = BxDolAccount::getInstance($iAccount)->getInfo();
  1724. $aPlus = array();
  1725. $aPlus['sender_display_name'] = $oProfile->getDisplayName();
  1726. $aPlus['reply_text'] = $this->_prepareTextForOutput($aCmt['cmt_text'], $iCmtId);
  1727. $sPageUrl = $this->getBaseUrl();
  1728. if(empty($sPageUrl))
  1729. $sPageUrl = $this->getViewUrl($iCmtParentId);
  1730. else
  1731. $sPageUrl .= $this->getItemAnchor($iCmtParentId, true);
  1732. $aPlus['page_url'] = $sPageUrl;
  1733. $sPageTitle = $this->getObjectTitle();
  1734. if(empty($sPageTitle))
  1735. $sPageTitle = _t('_Content');
  1736. $aPlus['page_title'] = $sPageTitle;
  1737. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('t_CommentReplied', $aPlus);
  1738. return $aTemplate && sendMail($aAccount['email'], $aTemplate['Subject'], $aTemplate['Body']);
  1739. }
  1740. protected function _isShowDoComment($aParams, $isAllowedComment, $bCount)
  1741. {
  1742. $bShowDoComment = !isset($aParams['show_do_comment']) || $aParams['show_do_comment'] == true;
  1743. return $bShowDoComment && ($isAllowedComment || $bCount);
  1744. }
  1745. protected function _isShowCounter($aParams, $isAllowedComment, $bCount)
  1746. {
  1747. $bShowCounter = isset($aParams['show_counter']) && $aParams['show_counter'] === true;
  1748. return $bShowCounter && ($isAllowedComment || $bCount);
  1749. }
  1750. /**
  1751. * Note. By default image based controls aren't used.
  1752. * Therefore it can be overwritten in custom template.
  1753. */
  1754. protected function _getImageDo()
  1755. {
  1756. return '';
  1757. }
  1758. protected function _getIconDo()
  1759. {
  1760. return 'far comment';
  1761. }
  1762. protected function _getTitleDo()
  1763. {
  1764. return '_cmt_txt_do';
  1765. }
  1766. public function _getStructure($mixedItem, $aBp, &$iLevel, &$aStructure)
  1767. {
  1768. $bItem = !empty($mixedItem) && is_array($mixedItem);
  1769. $iCmtId = $bItem ? (int)$mixedItem['cmt_id'] : 0;
  1770. $iCmtIndex = $iCmtId;
  1771. if($bItem)
  1772. $aStructure[$iCmtIndex] = [
  1773. 'id' => $mixedItem['cmt_id'],
  1774. 'order' => isset($mixedItem['cmt_order']) ? $mixedItem['cmt_order'] : 0,
  1775. 'items' => [],
  1776. ];
  1777. if(!$bItem || (int)$mixedItem['cmt_replies'] > 0) {
  1778. $aItems = $this->_oQuery->getStructure($this->_iId, $this->_iAuthorId, $iCmtId, $aBp['filter'], $aBp['order']);
  1779. if(!empty($aItems)) {
  1780. if($bItem && $iLevel < (int)$this->_aSystem['number_of_levels']) {
  1781. $iPassLevel = $iLevel + 1;
  1782. $aPassStructure = &$aStructure[$iCmtIndex]['items'];
  1783. }
  1784. else {
  1785. $iPassLevel = $iLevel;
  1786. $aPassStructure = &$aStructure; //--- Is NEEDED here to correctly check sublevels.
  1787. }
  1788. foreach($aItems as $aItem)
  1789. $this->_getStructure($aItem, $aBp, $iPassLevel, $aPassStructure);
  1790. //--- Sort subitems
  1791. $iWay = isset($aBp['order']['way']) && $aBp['order']['way'] == 'desc' ? -1 : 1;
  1792. uasort($aPassStructure, function($aItem1, $aItem2) use ($iWay) {
  1793. if($aItem1['order'] == $aItem2['order'])
  1794. return 0;
  1795. return $iWay * ($aItem1['order'] < $aItem2['order'] ? -1 : 1);
  1796. });
  1797. }
  1798. }
  1799. }
  1800. public function _getStructureAPI($mixedItem, $aBp, &$iLevel, &$aStructure)
  1801. {
  1802. $bItem = !empty($mixedItem) && is_array($mixedItem);
  1803. $iCmtId = $bItem ? (int)$mixedItem['cmt_id'] : 0;
  1804. $sCmtIndex = 'i' . $iCmtId;
  1805. if($bItem) {
  1806. $oMenuActions = BxDolMenu::getObjectInstance($this->_sMenuObjActions);
  1807. $oMenuActions->setCmtsData($this, $iCmtId, $aBp);
  1808. $oMenuManage = BxDolMenu::getObjectInstance($this->_sMenuObjManage);
  1809. $oMenuManage->setCmtsData($this, $iCmtId, $aBp);
  1810. $aData = $this->getCommentSimple($iCmtId);
  1811. $aData = array_merge($aData, [
  1812. 'author_data' => BxDolProfile::getData($aData['cmt_author_id']),
  1813. 'menu_actions' => $oMenuActions->getCodeAPI(),
  1814. 'menu_manage' => $oMenuManage->getCodeAPI(),
  1815. ]);
  1816. $aStructure[$sCmtIndex] = [
  1817. 'id' => $iCmtId,
  1818. 'order' => isset($mixedItem['cmt_order']) ? $mixedItem['cmt_order'] : 0,
  1819. 'data' => $aData,
  1820. 'files' => $this->_getAttachments($mixedItem),
  1821. 'items' => [],
  1822. ];
  1823. }
  1824. if(!$bItem || (int)$mixedItem['cmt_replies'] > 0) {
  1825. $aItems = $this->_oQuery->getStructure($this->_iId, $this->_iAuthorId, $iCmtId, $aBp['filter'], $aBp['order']);
  1826. if(!empty($aItems)) {
  1827. if($bItem && $iLevel < (int)$this->_aSystem['number_of_levels']) {
  1828. $iPassLevel = $iLevel + 1;
  1829. $aPassStructure = &$aStructure[$sCmtIndex]['items'];
  1830. }
  1831. else {
  1832. $iPassLevel = $iLevel;
  1833. $aPassStructure = [];
  1834. }
  1835. foreach($aItems as $aItem)
  1836. $this->_getStructureAPI($aItem, $aBp, $iPassLevel, $aPassStructure);
  1837. //--- Sort subitems
  1838. $iWay = isset($aBp['order']['way']) && $aBp['order']['way'] == 'desc' ? -1 : 1;
  1839. uasort($aPassStructure, function($aItem1, $aItem2) use ($iWay) {
  1840. if($aItem1['order'] == $aItem2['order'])
  1841. return 0;
  1842. return $iWay * ($aItem1['order'] < $aItem2['order'] ? -1 : 1);
  1843. });
  1844. $aStructure[$sCmtIndex]['items'] = $aPassStructure;
  1845. }
  1846. }
  1847. }
  1848. }
  1849. /** @} */