BxDolCmts.php 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298
  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. public function getNotificationId()
  531. {
  532. return 'cmts-notification-' . $this->_sSystem . '-' . $this->_iId;
  533. }
  534. /**
  535. * @deprecated since version 10.0.0-B3 and can be removed in later versions.
  536. */
  537. public function setTableNameFiles($sTable)
  538. {
  539. $this->_sTableImages = $sTable;
  540. $this->_oQuery->setTableNameFiles($sTable);
  541. }
  542. /**
  543. * @deprecated since version 10.0.0-B3 and can be removed in later versions.
  544. */
  545. public function setTableNameFiles2Entries($sTable)
  546. {
  547. $this->_sTableImages2Entries = $sTable;
  548. $this->_oQuery->setTableNameFiles2Entries($sTable);
  549. }
  550. public function isHtml ()
  551. {
  552. return $this->_aSystem['html'] > 0;
  553. }
  554. public function isRatable ()
  555. {
  556. return $this->_aSystem['is_ratable'];
  557. }
  558. public function isAttachImageEnabled()
  559. {
  560. return true;
  561. }
  562. public function isAutoApprove()
  563. {
  564. return getParam('sys_cmts_enable_auto_approve') == 'on';
  565. }
  566. /**
  567. * set id to operate with votes
  568. */
  569. public function setId ($iId)
  570. {
  571. if ($iId == $this->getId()) return;
  572. $this->_iId = $iId;
  573. }
  574. /**
  575. * Add replace markers.
  576. * @param $a array of markers as key => value
  577. * @return true on success or false on error
  578. */
  579. public function addMarkers ($a)
  580. {
  581. if (empty($a) || !is_array($a))
  582. return false;
  583. $this->_aMarkers = array_merge ($this->_aMarkers, $a);
  584. return true;
  585. }
  586. /**
  587. * Database functions
  588. */
  589. public function getQueryObject ()
  590. {
  591. return $this->_oQuery;
  592. }
  593. public function getCommentsTableName ()
  594. {
  595. return $this->_oQuery->getTableName ();
  596. }
  597. public function getObjectAuthorId ($iObjectId = 0)
  598. {
  599. if(empty($this->_aSystem['trigger_field_author']))
  600. return 0;
  601. return $this->_oQuery->getObjectAuthorId ($iObjectId ? $iObjectId : $this->getId());
  602. }
  603. public function getObjectTitle ($iObjectId = 0)
  604. {
  605. if(empty($this->_aSystem['trigger_field_title']))
  606. return '';
  607. $sTitle = $this->_oQuery->getObjectTitle ($iObjectId ? $iObjectId : $this->getId());
  608. if(get_mb_substr($sTitle, 0, 1) == '_')
  609. $sTitle = _t($sTitle);
  610. return $sTitle;
  611. }
  612. public function getObjectPrivacyView ($iObjectId = 0)
  613. {
  614. if(empty($iObjectId))
  615. $iObjectId = $this->getId();
  616. $sFieldPrivacyView = '';
  617. if(!empty($this->_aSystem['trigger_field_privacy_view']))
  618. $sFieldPrivacyView = $this->_aSystem['trigger_field_privacy_view'];
  619. if(($iPrivacyView = $this->_oQuery->getObjectPrivacyView($iObjectId, $sFieldPrivacyView)) !== false)
  620. return $iPrivacyView;
  621. return BX_DOL_PG_ALL;
  622. }
  623. /*
  624. * Gets Content Filter object.
  625. */
  626. public function getObjectContentFilter ()
  627. {
  628. $oCf = BxDolContentFilter::getInstance();
  629. if(!$oCf->isEnabledForComments())
  630. return false;
  631. return $oCf;
  632. }
  633. public function getCommentsCountAll ($iObjectId = 0, $bForceCalculate = false)
  634. {
  635. return $this->_oQuery->getCommentsCountAll ($iObjectId ? $iObjectId : $this->getId(), $this->_getAuthorId(), $bForceCalculate);
  636. }
  637. public function getCommentsCount ($iObjectId = 0, $iCmtVParentId = -1, $sFilter = '')
  638. {
  639. return $this->_oQuery->getCommentsCount ($iObjectId ? $iObjectId : $this->getId(), $iCmtVParentId, $this->_getAuthorId(), $sFilter);
  640. }
  641. public function getCommentsArray ($iVParentId, $sFilter, $aOrder, $iStart = 0, $iCount = -1)
  642. {
  643. return $this->_oQuery->getComments ($this->getId(), $iVParentId, $this->_getAuthorId(), $sFilter, $aOrder, $iStart, $iCount);
  644. }
  645. public function getCommentsBy ($aParams = [])
  646. {
  647. return $this->_oQuery->getCommentsBy($aParams);
  648. }
  649. /**
  650. * Get comment's unique id.
  651. */
  652. public function getCommentUniqId($iCmtId, $iAuthorId = 0)
  653. {
  654. return $this->_oQuery->getUniqId($this->getSystemId(), $iCmtId, ['author_id' => $iAuthorId]);
  655. }
  656. /**
  657. * Get comment's short info.
  658. */
  659. public function getCommentSimple ($iCmtId)
  660. {
  661. return $this->_oQuery->getCommentSimple ($this->getId(), $iCmtId);
  662. }
  663. /**
  664. * Get comment's full info.
  665. */
  666. public function getCommentRow ($iCmtId)
  667. {
  668. return $this->_oQuery->getComment ($this->getId(), $iCmtId);
  669. }
  670. public function onObjectDelete ($iObjectId = 0)
  671. {
  672. // delete comments
  673. $aFiles = $aCmtIds = [];
  674. $this->_oQuery->deleteObjectComments ($iObjectId ? $iObjectId : $this->getId(), $aFiles, $aCmtIds);
  675. // delete votes
  676. $this->deleteVotes($aCmtIds);
  677. // delete reactions
  678. $this->deleteReactions($aCmtIds);
  679. // delete scores
  680. $this->deleteScores($aCmtIds);
  681. // delete reports
  682. $this->deleteReports($aCmtIds);
  683. // delete meta info
  684. $this->deleteMetaInfo($aCmtIds);
  685. // delete files
  686. if ($aFiles) {
  687. $oStorage = BxDolStorage::getObjectInstance($this->getStorageObjectName());
  688. if ($oStorage)
  689. $oStorage->queueFilesForDeletion($aFiles);
  690. }
  691. // delete unique IDs
  692. $this->deleteUniqueIds($aCmtIds);
  693. }
  694. public static function onAuthorDelete ($iAuthorId)
  695. {
  696. $aSystems = self::getSystems();
  697. foreach($aSystems as $sSystem => $aSystem) {
  698. $o = self::getObjectInstance($sSystem, 0);
  699. $oQuery = $o->getQueryObject();
  700. // delete comments
  701. $aFiles = $aCmtIds = array ();
  702. $oQuery->deleteAuthorComments($iAuthorId, $aFiles, $aCmtIds);
  703. // delete votes
  704. $o->deleteVotes($aCmtIds);
  705. // delete reactions
  706. $o->deleteReactions($aCmtIds);
  707. // delete scores
  708. $o->deleteScores($aCmtIds);
  709. // delete reports
  710. $o->deleteReports($aCmtIds);
  711. // delete meta info
  712. $o->deleteMetaInfo($aCmtIds);
  713. // delete files
  714. $oStorage = BxDolStorage::getObjectInstance($o->getStorageObjectName());
  715. if ($oStorage)
  716. $oStorage->queueFilesForDeletion($aFiles);
  717. // delete unique IDs
  718. $o->deleteUniqueIds($aCmtIds);
  719. }
  720. return true;
  721. }
  722. public static function onModuleEnable ($sModuleName)
  723. {
  724. $aSystems = self::getSystems();
  725. foreach($aSystems as $sSystem => $aSystem) {
  726. if ($sModuleName !== $aSystem['module'])
  727. continue;
  728. $o = self::getObjectInstance($sSystem, 0);
  729. $o->registerTranscoders();
  730. }
  731. return true;
  732. }
  733. public static function onModuleDisable ($sModuleName)
  734. {
  735. $aSystems = self::getSystems();
  736. foreach($aSystems as $sSystem => $aSystem) {
  737. if ($sModuleName !== $aSystem['module'])
  738. continue;
  739. $o = self::getObjectInstance($sSystem, 0);
  740. $o->unregisterTranscoders();
  741. }
  742. return true;
  743. }
  744. public static function onModuleUninstall ($sModuleName, &$iFiles = null)
  745. {
  746. $aSystems = self::getSystems();
  747. foreach($aSystems as $sSystem => $aSystem) {
  748. if ($sModuleName !== $aSystem['module'])
  749. continue;
  750. $o = self::getObjectInstance($sSystem, 0);
  751. $oQuery = $o->getQueryObject();
  752. // delete comments
  753. $aFiles = $aCmtIds = array ();
  754. $oQuery->deleteAll($aSystem['system_id'], $aFiles, $aCmtIds);
  755. // delete votes
  756. $o->deleteVotes($aCmtIds);
  757. // delete reactions
  758. $o->deleteReactions($aCmtIds);
  759. // delete scores
  760. $o->deleteScores($aCmtIds);
  761. // delete reports
  762. $o->deleteReports($aCmtIds);
  763. // delete meta info
  764. $o->deleteMetaInfo($aCmtIds);
  765. // delete files
  766. $oStorage = BxDolStorage::getObjectInstance($o->getStorageObjectName());
  767. if ($oStorage && $aFiles)
  768. $oStorage->queueFilesForDeletion($aFiles);
  769. if (null !== $iFiles)
  770. $iFiles += count($aFiles);
  771. // delete unique IDs
  772. $o->deleteUniqueIds($aCmtIds);
  773. }
  774. return true;
  775. }
  776. public function deleteVotes ($mixedCmtId)
  777. {
  778. if(!is_array($mixedCmtId))
  779. $mixedCmtId = [$mixedCmtId];
  780. foreach($mixedCmtId as $iCmtId)
  781. if(($oReport = $this->getVoteObject($this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId))) !== false)
  782. $oReport->onObjectDelete();
  783. }
  784. public function deleteReactions ($mixedCmtId)
  785. {
  786. if(!is_array($mixedCmtId))
  787. $mixedCmtId = [$mixedCmtId];
  788. foreach($mixedCmtId as $iCmtId)
  789. if(($oReport = $this->getReactionObject($this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId))) !== false)
  790. $oReport->onObjectDelete();
  791. }
  792. public function deleteScores ($mixedCmtId)
  793. {
  794. if(!is_array($mixedCmtId))
  795. $mixedCmtId = [$mixedCmtId];
  796. foreach($mixedCmtId as $iCmtId)
  797. if(($oReport = $this->getScoreObject($this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId))) !== false)
  798. $oReport->onObjectDelete();
  799. }
  800. public function deleteReports ($mixedCmtId)
  801. {
  802. if(!is_array($mixedCmtId))
  803. $mixedCmtId = [$mixedCmtId];
  804. foreach($mixedCmtId as $iCmtId)
  805. if(($oReport = $this->getReportObject($this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId))) !== false)
  806. $oReport->onObjectDelete();
  807. }
  808. public function deleteMetaInfo ($mixedCmtId)
  809. {
  810. if(!is_array($mixedCmtId))
  811. $mixedCmtId = array($mixedCmtId);
  812. $oMetatags = false;
  813. if(!empty($this->_sMetatagsObj))
  814. $oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj);
  815. if($oMetatags)
  816. foreach($mixedCmtId as $iCmtId)
  817. $oMetatags->onDeleteContent($this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId));
  818. }
  819. public function deleteUniqueIds ($mixedCmtId)
  820. {
  821. if(!is_array($mixedCmtId))
  822. $mixedCmtId = array($mixedCmtId);
  823. foreach($mixedCmtId as $iCmtId)
  824. $this->_oQuery->deleteCmtIds($this->_aSystem['system_id'], $iCmtId);
  825. }
  826. /**
  827. * Permissions functions
  828. */
  829. public function isAdmin($iCmtAuthorId)
  830. {
  831. return BxDolAcl::getInstance()->isMemberLevelInSet(array(MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR), $iCmtAuthorId);
  832. }
  833. public function checkAction ($sAction, $isPerformAction = false)
  834. {
  835. $iId = $this->_getAuthorId();
  836. $a = checkActionModule($iId, $sAction, 'system', $isPerformAction);
  837. return $a[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  838. }
  839. public function checkActionErrorMsg ($sAction)
  840. {
  841. $iId = $this->_getAuthorId();
  842. $a = checkActionModule($iId, $sAction, 'system');
  843. return $a[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $a[CHECK_ACTION_MESSAGE] : '';
  844. }
  845. public function isViewAllowed ($isPerformAction = false)
  846. {
  847. if (BxDolRequest::serviceExists($this->_aSystem['module'], 'check_allowed_comments_view')) {
  848. $mixedResult = BxDolService::call($this->_aSystem['module'], 'check_allowed_comments_view', array($this->getId(), $this->getSystemName()));
  849. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  850. return $mixedResult;
  851. }
  852. return CHECK_ACTION_RESULT_ALLOWED;
  853. }
  854. public function isVoteAllowed ($aCmt, $isPerformAction = false)
  855. {
  856. if(!$this->isRatable())
  857. return false;
  858. $oVote = $this->getVoteObject($aCmt['cmt_id']);
  859. if($oVote === false)
  860. return false;
  861. $iUserId = (int)$this->_getAuthorId();
  862. if($iUserId == 0)
  863. return false;
  864. if(isAdmin())
  865. return true;
  866. return $oVote->isAllowedVote($isPerformAction);
  867. }
  868. public function isScoreAllowed ($aCmt, $isPerformAction = false)
  869. {
  870. if(!$this->isRatable())
  871. return false;
  872. $oScore = $this->getScoreObject($aCmt['cmt_id']);
  873. if($oScore === false)
  874. return false;
  875. $iUserId = (int)$this->_getAuthorId();
  876. if($iUserId == 0)
  877. return false;
  878. if(isAdmin())
  879. return true;
  880. return $oScore->isAllowedVote($isPerformAction);
  881. }
  882. public function isReportAllowed ($aCmt, $isPerformAction = false)
  883. {
  884. $oReport = $this->getReportObject($aCmt['cmt_id']);
  885. if($oReport === false)
  886. return false;
  887. $iUserId = (int)$this->_getAuthorId();
  888. if($iUserId == 0)
  889. return false;
  890. if(isAdmin())
  891. return true;
  892. return $oReport->isAllowedReport($isPerformAction);
  893. }
  894. public function isPostAllowed ($isPerformAction = false)
  895. {
  896. if (BxDolRequest::serviceExists($this->_aSystem['module'], 'check_allowed_comments_post')) {
  897. $mixedResult = BxDolService::call($this->_aSystem['module'], 'check_allowed_comments_post', array($this->getId(), $this->getSystemName()));
  898. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  899. return false;
  900. }
  901. return $this->checkAction ('comments post', $isPerformAction);
  902. }
  903. public function msgErrPostAllowed ()
  904. {
  905. return $this->checkActionErrorMsg('comments post');
  906. }
  907. /**
  908. * Determines whether a 'reply' action allowed or not.
  909. * @param integer or array $mixedCmt - ID or an array which describes the comment to be replied
  910. * @param boolean $isPerformAction
  911. * @return boolean
  912. */
  913. public function isReplyAllowed ($mixedCmt, $isPerformAction = false)
  914. {
  915. return $this->isPostAllowed($isPerformAction);
  916. }
  917. public function msgErrReplyAllowed ()
  918. {
  919. return $this->msgErrPostAllowed ();
  920. }
  921. public function isQuoteAllowed ($mixedCmt, $isPerformAction = false)
  922. {
  923. return $this->_bPostQuote && $this->isReplyAllowed ($mixedCmt, $isPerformAction);
  924. }
  925. public function msgErrQuoteAllowed ()
  926. {
  927. return $this->msgErrReplyAllowed ();
  928. }
  929. public function isPinAllowed ($aCmt, $isPerformAction = false)
  930. {
  931. if((int)$aCmt['cmt_parent_id'] != 0 || (int)$aCmt['cmt_pinned'] != 0)
  932. return false;
  933. if(isAdmin())
  934. return true;
  935. return $this->checkAction ('comments pin', $isPerformAction);
  936. }
  937. public function msgErrPinAllowed ()
  938. {
  939. return $this->checkActionErrorMsg('comments pin');
  940. }
  941. public function isUnpinAllowed ($aCmt, $isPerformAction = false)
  942. {
  943. if((int)$aCmt['cmt_pinned'] == 0)
  944. return false;
  945. if(isAdmin())
  946. return true;
  947. return $this->checkAction ('comments pin', $isPerformAction);
  948. }
  949. public function msgErrUnpinAllowed ()
  950. {
  951. return $this->checkActionErrorMsg('comments pin');
  952. }
  953. public function isEditAllowed ($aCmt, $isPerformAction = false)
  954. {
  955. if(isAdmin())
  956. return true;
  957. if($this->isEditAllowedAll($isPerformAction))
  958. return true;
  959. $mixedResult = BxDolService::call($this->_aSystem['module'], 'check_allowed_comments_post', array($this->getId(), $this->getSystemName()));
  960. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  961. return false;
  962. return abs($aCmt['cmt_author_id']) == $this->_getAuthorId() && $this->checkAction ('comments edit own', $isPerformAction);
  963. }
  964. public function msgErrEditAllowed ()
  965. {
  966. return $this->checkActionErrorMsg ('comments edit own');
  967. }
  968. public function isEditAllowedAll ($isPerformAction = false)
  969. {
  970. return $this->checkAction('comments edit all', $isPerformAction);
  971. }
  972. public function isRemoveAllowed ($aCmt, $isPerformAction = false)
  973. {
  974. if(isAdmin())
  975. return true;
  976. if($this->isRemoveAllowedAll($isPerformAction))
  977. return true;
  978. if(abs($aCmt['cmt_author_id']) == $this->_getAuthorId() && $this->checkAction ('comments remove own', $isPerformAction))
  979. return true;
  980. $aContentInfo = BxDolRequest::serviceExists($this->_aSystem['module'], 'get_all') ? BxDolService::call($this->_aSystem['module'], 'get_all', array(array('type' => 'id', 'id' => $this->getId()))) : array();
  981. $oModule = BxDolModule::getInstance($this->_aSystem['module']);
  982. $CNF = $oModule->_oConfig->CNF;
  983. if (isset($CNF['FIELD_AUTHOR']) && $aContentInfo[$CNF['FIELD_AUTHOR']] == $this->_getAuthorId() && $this->checkAction('comments remove in own content', $isPerformAction)){
  984. return true;
  985. }
  986. if (isset($CNF['FIELD_ALLOW_VIEW_TO']) && $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']] < 0){
  987. $iGroupProfileId = -(int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']];
  988. $oProfileContext = BxDolProfile::getInstance($iGroupProfileId);
  989. $oModule = BxDolModule::getInstance($oProfileContext->getModule());
  990. if ($oModule->_oConfig->isRoles() && $oModule->getRole($iGroupProfileId, $this->_getAuthorId()) === BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR && $this->checkAction('comments remove in group context', $isPerformAction)){
  991. return true;
  992. }
  993. }
  994. return false;
  995. }
  996. public function isRemoveAllowedAll ($isPerformAction = false)
  997. {
  998. return $this->checkAction ('comments remove all', $isPerformAction);
  999. }
  1000. public function msgErrRemoveAllowed ()
  1001. {
  1002. return $this->checkActionErrorMsg('comments remove own');
  1003. }
  1004. public function isMoreAllowed ($aCmt, $isPerformAction = false)
  1005. {
  1006. $oMenuManage = BxDolMenu::getObjectInstance($this->_sMenuObjManage);
  1007. $oMenuManage->setCmtsData($this, $aCmt['cmt_id']);
  1008. return $oMenuManage->isVisible();
  1009. }
  1010. public function isModerator($isPerformAction = false)
  1011. {
  1012. return $this->isEditAllowedAll($isPerformAction) || $this->isRemoveAllowedAll($isPerformAction);
  1013. }
  1014. /**
  1015. * Actions functions
  1016. */
  1017. public function actionPin ()
  1018. {
  1019. if (!$this->isEnabled())
  1020. return echoJson(array());
  1021. $iCmtId = 0;
  1022. if(bx_get('Cmt') !== false)
  1023. $iCmtId = bx_process_input(bx_get('Cmt'), BX_DATA_INT);
  1024. $aCmt = $this->getCommentRow($iCmtId);
  1025. if(empty($aCmt) || !is_array($aCmt))
  1026. return echoJson(array());
  1027. $iWay = 0;
  1028. if(bx_get('way') !== false)
  1029. $iWay = bx_process_input(bx_get('way'), BX_DATA_INT);
  1030. if(!$this->_oQuery->updateComments(array('cmt_pinned' => $iWay ? time() : 0), array('cmt_id' => $iCmtId)))
  1031. return echoJson(array());
  1032. $aBp = $aDp = array();
  1033. $this->_getParams($aBp, $aDp);
  1034. $this->_prepareParams($aBp, $aDp);
  1035. echoJson(array(
  1036. 'parent_id' => $aCmt['cmt_parent_id'],
  1037. 'per_view' => $aBp['per_view']
  1038. ));
  1039. }
  1040. public function actionGetFormPost ()
  1041. {
  1042. if (!$this->isEnabled())
  1043. return '';
  1044. $iCmtParentId = bx_get('CmtParent');
  1045. $iCmtParentId = $iCmtParentId !== false ? bx_process_input($iCmtParentId, BX_DATA_INT) : 0;
  1046. $sCmtBrowse = bx_get('CmtBrowse');
  1047. $sCmtBrowse = $sCmtBrowse !== false ? bx_process_input($sCmtBrowse, BX_DATA_TEXT) : '';
  1048. $sCmtDisplay = bx_get('CmtDisplay');
  1049. $sCmtDisplay = $sCmtDisplay !== false ? bx_process_input($sCmtDisplay, BX_DATA_TEXT) : '';
  1050. $bQuote = bx_get('CmtQuote');
  1051. $bQuote = $bQuote !== false ? bx_process_input($bQuote, BX_DATA_INT) == 1 : false;
  1052. $bMinPostForm = bx_get('CmtMinPostForm');
  1053. $bMinPostForm = $bMinPostForm !== false ? bx_process_input($bMinPostForm, BX_DATA_INT) == 1 : $this->_bMinPostForm;
  1054. return $this->getFormBoxPost(array('parent_id' => $iCmtParentId, 'type' => $sCmtBrowse), array('type' => $sCmtDisplay, 'dynamic_mode' => true, 'quote' => $bQuote, 'min_post_form' => $bMinPostForm));
  1055. }
  1056. public function actionGetFormEdit ()
  1057. {
  1058. if (!$this->isEnabled())
  1059. return echoJson(array());
  1060. $iCmtId = bx_process_input(bx_get('Cmt'), BX_DATA_INT);
  1061. echoJson($this->getFormEdit($iCmtId, array('dynamic_mode' => true)));
  1062. }
  1063. public function actionGetCmt ()
  1064. {
  1065. if(!$this->isEnabled())
  1066. return '';
  1067. if($this->isViewAllowed() !== CHECK_ACTION_RESULT_ALLOWED)
  1068. return '';
  1069. $iCmtId = bx_process_input($_REQUEST['Cmt'], BX_DATA_INT);
  1070. $sCmtBrowse = isset($_REQUEST['CmtBrowse']) ? bx_process_input($_REQUEST['CmtBrowse'], BX_DATA_TEXT) : '';
  1071. $sCmtDisplay = isset($_REQUEST['CmtDisplay']) ? bx_process_input($_REQUEST['CmtDisplay'], BX_DATA_TEXT) : '';
  1072. $aCmt = $this->getCommentRow($iCmtId);
  1073. echoJson([
  1074. 'parent_id' => $aCmt['cmt_parent_id'],
  1075. 'vparent_id' => $aCmt['cmt_parent_id'],
  1076. 'content' => $this->getComment($aCmt, ['type' => $sCmtBrowse], ['type' => $sCmtDisplay, 'dynamic_mode' => true])
  1077. ]);
  1078. }
  1079. public function actionGetCmts ()
  1080. {
  1081. if (!$this->isEnabled())
  1082. return '';
  1083. if($this->isViewAllowed() !== CHECK_ACTION_RESULT_ALLOWED)
  1084. return '';
  1085. $aBp = $aDp = array();
  1086. $this->_getParams($aBp, $aDp);
  1087. $aDp['dynamic_mode'] = true;
  1088. //--- Beg: Using pregenerated structure
  1089. if(isset($aDp['structure']) && $aDp['structure'] === true) {
  1090. $iCmtId = isset($aBp['parent_id']) ? (int)$aBp['parent_id'] : 0;
  1091. $mixedStructure = $this->getCommentStructure($iCmtId, $aBp, $aDp);
  1092. if($mixedStructure === false || empty($mixedStructure[$iCmtId]['items']))
  1093. return '';
  1094. $aDp['structure'] = $mixedStructure[$iCmtId]['items'];
  1095. return $this->getCommentsByStructure($aBp, $aDp);
  1096. }
  1097. //--- End: Using pregenerated structure
  1098. else
  1099. return $this->{'getComments' . ($aBp['pinned'] ? 'Pinned' : '')}($aBp, $aDp);
  1100. }
  1101. public function actionSubmitPostForm()
  1102. {
  1103. if(!$this->isEnabled())
  1104. return echoJson(array());
  1105. $iCmtParentId = 0;
  1106. if(bx_get('cmt_parent_id') !== false)
  1107. $iCmtParentId = bx_process_input(bx_get('cmt_parent_id'), BX_DATA_INT);
  1108. echoJson($this->getFormPost($iCmtParentId, array('dynamic_mode' => true)));
  1109. }
  1110. public function actionSubmitEditForm()
  1111. {
  1112. if (!$this->isEnabled()) {
  1113. echoJson(array());
  1114. return;
  1115. };
  1116. $iCmtId = 0;
  1117. if(bx_get('cmt_id') !== false)
  1118. $iCmtId = bx_process_input(bx_get('cmt_id'), BX_DATA_INT);
  1119. echoJson($this->getFormEdit($iCmtId, array('dynamic_mode' => true)));
  1120. }
  1121. public function actionRemove()
  1122. {
  1123. if (!$this->isEnabled())
  1124. return echoJson([]);
  1125. $iCmtId = 0;
  1126. if(bx_get('Cmt') !== false)
  1127. $iCmtId = bx_process_input(bx_get('Cmt'), BX_DATA_INT);
  1128. echoJson($this->remove($iCmtId));
  1129. }
  1130. public function remove($iCmtId)
  1131. {
  1132. $aCmt = $this->_oQuery->getCommentsBy(array('type' => 'id', 'id' => $iCmtId));
  1133. if(!$aCmt)
  1134. return array('msg' => _t('_No such comment'));
  1135. $iObjId = $this->getId();
  1136. if(!$iObjId){
  1137. $this->setId($aCmt['cmt_object_id']);
  1138. $iObjId = $aCmt['cmt_object_id'];
  1139. }
  1140. $iObjAthrId = $this->getObjectAuthorId($iObjId);
  1141. $iObjAthrPrivacyView = $this->getObjectPrivacyView($iObjId);
  1142. if($aCmt['cmt_replies'] > 0) {
  1143. if(!$this->isModerator())
  1144. return array('msg' => _t('_Can not delete comments with replies'));
  1145. $aReplies = $this->_oQuery->getCommentsBy(array('type' => 'parent_id', 'parent_id' => $iCmtId));
  1146. foreach($aReplies as $aReply)
  1147. $this->remove($aReply['cmt_id']);
  1148. }
  1149. $iPerformerId = $this->_getAuthorId();
  1150. if(!$this->isRemoveAllowed($aCmt))
  1151. return array('msg' => $aCmt['cmt_author_id'] == $iPerformerId ? strip_tags($this->msgErrRemoveAllowed()) : _t('_Access denied'));
  1152. $iCmtPrntId = (int)$aCmt['cmt_parent_id'];
  1153. if(!$this->_oQuery->removeComment($iObjId, $iCmtId, $iCmtPrntId))
  1154. return array('msg' => _t('_cmt_err_cannot_perform_action'));
  1155. $this->_triggerComment();
  1156. $oStorage = BxDolStorage::getObjectInstance($this->getStorageObjectName());
  1157. $aImages = $this->_oQuery->getFiles($this->_aSystem['system_id'], $iCmtId);
  1158. foreach($aImages as $aImage)
  1159. $oStorage->deleteFile($aImage['image_id']);
  1160. $this->_oQuery->deleteImages($this->_aSystem['system_id'], $iCmtId);
  1161. $this->isRemoveAllowed($aCmt, true);
  1162. $this->deleteVotes($iCmtId);
  1163. $this->deleteReactions($iCmtId);
  1164. $this->deleteScores($iCmtId);
  1165. $this->deleteReports($iCmtId);
  1166. $this->deleteMetaInfo($iCmtId);
  1167. $aAuditParams = $this->_prepareAuditParams($iCmtId, array('comment_author_id' => $aCmt['cmt_author_id'], 'comment_text' => $aCmt['cmt_text']));
  1168. bx_audit($iObjId, $this->_aSystem['module'], '_sys_audit_action_delete_comment', $aAuditParams);
  1169. $aAlertParams = $this->_prepareAlertParams($aCmt);
  1170. bx_alert($this->_sSystem, 'commentRemoved', $iObjId, $iPerformerId, $aAlertParams);
  1171. bx_alert('comment', 'deleted', $iCmtId, $iPerformerId, $aAlertParams);
  1172. if(!empty($iCmtPrntId)) {
  1173. $aCmtPrnt = $this->_oQuery->getCommentSimple($iObjId, $iCmtPrntId);
  1174. if(!empty($aCmtPrnt) && is_array($aCmtPrnt)) {
  1175. $aAlertParamsReply = $this->_prepareAlertParamsReply($aCmt, $aCmtPrnt);
  1176. bx_alert($this->_sSystem, 'replyRemoved', $iCmtPrntId, $iPerformerId, $aAlertParamsReply);
  1177. bx_alert('reply', 'deleted', $iCmtId, $iPerformerId, $aAlertParamsReply);
  1178. }
  1179. }
  1180. $this->deleteUniqueIds($iCmtId);
  1181. $iCount = (int)$this->getCommentsCountAll(0, true);
  1182. return [
  1183. 'id' => $iCmtId,
  1184. 'count' => $iCount,
  1185. 'countf' => $iCount > 0 ? $this->getCounter() : ''
  1186. ];
  1187. }
  1188. public function add($aValues)
  1189. {
  1190. return $this->_getFormAdd($aValues);
  1191. }
  1192. public function actionResumeLiveUpdate()
  1193. {
  1194. $sKey = $this->getNotificationId();
  1195. bx_import('BxDolSession');
  1196. BxDolSession::getInstance()->unsetValue($sKey);
  1197. }
  1198. public function actionPauseLiveUpdate()
  1199. {
  1200. $sKey = $this->getNotificationId();
  1201. bx_import('BxDolSession');
  1202. BxDolSession::getInstance()->setValue($sKey, 1);
  1203. }
  1204. public function actionGetSiblingFiles()
  1205. {
  1206. $iFileId = 0;
  1207. if(bx_get('file_id') !== false)
  1208. $iFileId = bx_process_input(bx_get('file_id'), BX_DATA_INT);
  1209. if(!($aFileInfo = $this->_oQuery->getFileInfoById((int)$iFileId)))
  1210. return echoJson(['error' => _t('_sys_txt_error_occured')]);
  1211. $aSiblings = [];
  1212. $aFiles = $this->_oQuery->getFiles($aFileInfo['system_id'], $aFileInfo['cmt_id']);
  1213. foreach($aFiles as $iIndex => $aFile) {
  1214. if($aFile['id'] != $iFileId)
  1215. continue;
  1216. $aSiblings['prev'] = isset($aFiles[$iIndex - 1]) ? $aFiles[$iIndex - 1] : false;
  1217. $aSiblings['next'] = isset($aFiles[$iIndex + 1]) ? $aFiles[$iIndex + 1] : false;
  1218. }
  1219. $oStorage = BxDolStorage::getObjectInstance($this->getStorageObjectName());
  1220. $oTranscoder = BxDolTranscoderImage::getObjectInstance($this->getTranscoderPreviewName());
  1221. foreach($aSiblings as $sSibling => $aSibling) {
  1222. if(!$aSibling)
  1223. continue;
  1224. $sFile = '';
  1225. if($oTranscoder && $oTranscoder->isMimeTypeSupported($aSibling['mime_type']))
  1226. $sFile = $oStorage->getFileUrlById($aSibling['image_id']);
  1227. else
  1228. $sFile = $this->_oTemplate->getIconUrl($oStorage->getIconNameByFileName($aFile['file_name']));
  1229. $aImageInfo = BxDolImageResize::getInstance()->getImageSize($sFile);
  1230. $iWidth = $iHeight = 0;
  1231. if(isset($aImageInfo['w']))
  1232. $iWidth = (int)$aImageInfo['w'];
  1233. if(isset($aImageInfo['h']))
  1234. $iHeight = (int)$aImageInfo['h'];
  1235. $aSiblings[$sSibling] = array_merge($aSibling, [
  1236. 'file' => $sFile,
  1237. 'file_id' => $aSibling['id'],
  1238. 'w' => $iWidth,
  1239. 'h' => $iHeight
  1240. ]);
  1241. }
  1242. return echoJson($aSiblings);
  1243. }
  1244. public function onPostAfter($iCmtId, $aDp = [])
  1245. {
  1246. $iObjId = (int)$this->getId();
  1247. $aCmt = $this->_oQuery->getCommentSimple($iObjId, $iCmtId);
  1248. if(empty($aCmt) || !is_array($aCmt))
  1249. return false;
  1250. $iCmtPrntId = (int)$aCmt['cmt_parent_id'];
  1251. $iPerformerId = (int)$aCmt['cmt_author_id'];
  1252. $aAlertParams = $this->_prepareAlertParams($aCmt);
  1253. bx_alert($this->_sSystem, 'commentPost', $iObjId, $iPerformerId, $aAlertParams);
  1254. bx_alert('comment', 'added', $iCmtId, $iPerformerId, $aAlertParams);
  1255. $aAuditParams = $this->_prepareAuditParams($iCmtId, array('comment_author_id' => $aCmt['cmt_author_id'], 'comment_text' => $aCmt['cmt_text']));
  1256. bx_audit($iObjId, $this->_aSystem['module'], '_sys_audit_action_add_comment', $aAuditParams);
  1257. if(!empty($iCmtPrntId)) {
  1258. $aCmtPrnt = $this->_oQuery->getCommentSimple($iObjId, $iCmtPrntId);
  1259. if(!empty($aCmtPrnt) && is_array($aCmtPrnt)) {
  1260. $aAlertParamsReply = $this->_prepareAlertParamsReply($aCmt, $aCmtPrnt);
  1261. bx_alert($this->_sSystem, 'replyPost', $iCmtPrntId, $iPerformerId, $aAlertParamsReply);
  1262. bx_alert('comment', 'replied', $iCmtId, $iPerformerId, $aAlertParamsReply);
  1263. }
  1264. }
  1265. $iCount = (int)$this->getCommentsCountAll(0, true);
  1266. return [
  1267. 'id' => $iCmtId,
  1268. 'parent_id' => $iCmtPrntId,
  1269. 'count' => $iCount,
  1270. 'countf' => $iCount > 0 ? $this->getCounter() : ''
  1271. ];
  1272. }
  1273. public function onEditAfter($iCmtId, $aDp = [])
  1274. {
  1275. $aCmt = $this->getCommentRow($iCmtId);
  1276. if(empty($aCmt) || !is_array($aCmt))
  1277. return false;
  1278. $aBp = [];
  1279. $this->_getParams($aBp, $aDp);
  1280. $iObjId = (int)$this->getId();
  1281. $iPerformerId = $this->_getAuthorId();
  1282. $aAlertParams = $this->_prepareAlertParams($aCmt);
  1283. bx_alert($this->_sSystem, 'commentUpdated', $iObjId, $iPerformerId, $aAlertParams);
  1284. bx_alert('comment', 'edited', $iCmtId, $iPerformerId, $aAlertParams);
  1285. $aAuditParams = $this->_prepareAuditParams($iCmtId, ['comment_author_id' => $aCmt['cmt_author_id'], 'comment_text' => $aCmt['cmt_text']]);
  1286. bx_audit($iObjId, $this->_aSystem['module'], '_sys_audit_action_edit_comment', $aAuditParams);
  1287. return ['id' => $iCmtId, 'content' => $this->_getContent($aCmt, $aBp, $aDp)];
  1288. }
  1289. public function serviceGetAuthor ($iContentId)
  1290. {
  1291. $aComment = $this->_oQuery->getCommentsBy(array('type' => 'id', 'id' => $iContentId));
  1292. $this->setId($aComment['cmt_object_id']);
  1293. return $aComment['cmt_author_id'];
  1294. }
  1295. public function serviceGetDateAdded ($iContentId)
  1296. {
  1297. $aComment = $this->_oQuery->getCommentsBy(array('type' => 'id', 'id' => $iContentId));
  1298. $this->setId($aComment['cmt_object_id']);
  1299. return $aComment['cmt_time'];
  1300. }
  1301. public function serviceGetDateChanged ($iContentId)
  1302. {
  1303. return 0;
  1304. }
  1305. public function serviceGetLink ($iContentId)
  1306. {
  1307. $aComment = $this->_oQuery->getCommentsBy(array('type' => 'id', 'id' => $iContentId));
  1308. $this->setId($aComment['cmt_object_id']);
  1309. return $this->getViewUrl($iContentId);
  1310. }
  1311. public function serviceGetTitle ($iContentId)
  1312. {
  1313. return '';
  1314. }
  1315. public function serviceGetText ($iContentId)
  1316. {
  1317. $aComment = $this->_oQuery->getCommentsBy(array('type' => 'id', 'id' => $iContentId));
  1318. $this->setId($aComment['cmt_object_id']);
  1319. return $aComment['cmt_text'];
  1320. }
  1321. public function serviceGetThumb ($iContentId)
  1322. {
  1323. return '';
  1324. }
  1325. public function serviceGetInfo ($iContentId, $bSearchableFieldsOnly = true)
  1326. {
  1327. $aComment = $this->_oQuery->getCommentsBy(array('type' => 'id', 'id' => $iContentId));
  1328. $this->setId($aComment['cmt_object_id']);
  1329. return BxDolContentInfo::formatFields($aComment);
  1330. }
  1331. public function serviceGetSearchResultUnit ($iContentId, $sUnitTemplate = '')
  1332. {
  1333. $aComment = $this->_oQuery->getCommentsBy(array('type' => 'id', 'id' => $iContentId));
  1334. if(empty($aComment) || !is_array($aComment))
  1335. return '';
  1336. $this->setId($aComment['cmt_object_id']);
  1337. return $this->getComment($aComment, array(), array('type' => BX_CMT_DISPLAY_FLAT, 'view_only' => true));
  1338. }
  1339. public function serviceGetAll ($aParams = array())
  1340. {
  1341. if(empty($aParams) || !is_array($aParams))
  1342. $aParams = array('type' => 'all');
  1343. return $this->_oQuery->getCommentsBy($aParams);
  1344. }
  1345. public function serviceGetSearchableFieldsExtended($aInputsAdd = array())
  1346. {
  1347. $oForm = BxDolForm::getObjectInstance('sys_comment', 'sys_comment_post', $this->_oTemplate);
  1348. if(!$oForm)
  1349. return array();
  1350. $aSrchNamesExcept = array();
  1351. $aSrchCaptionsSystem = array(
  1352. 'cmt_author_id' => '_sys_form_comment_input_caption_system_cmt_author_id',
  1353. 'cmt_text' => '_sys_form_comment_input_caption_system_cmt_text'
  1354. );
  1355. $aSrchCaptions = array(
  1356. 'cmt_author_id' => '_sys_form_comment_input_caption_cmt_author_id',
  1357. 'cmt_text' => '_sys_form_comment_input_caption_cmt_text'
  1358. );
  1359. $aResult = array(
  1360. 'cmt_author_id' => array(
  1361. 'type' => 'text_auto',
  1362. 'caption_system' => $aSrchCaptionsSystem['cmt_author_id'],
  1363. 'caption' => $aSrchCaptions['cmt_author_id'],
  1364. 'info' => '',
  1365. 'value' => '',
  1366. 'values' => '',
  1367. 'pass' => ''
  1368. )
  1369. );
  1370. $aInputs = $oForm->aInputs;
  1371. if(!empty($aInputsAdd) && is_array($aInputsAdd))
  1372. $aInputs = array_merge($aInputs, $aInputsAdd);
  1373. foreach($aInputs as $aInput)
  1374. if(in_array($aInput['type'], BxDolSearchExtended::$SEARCHABLE_TYPES) && !in_array($aInput['name'], $aSrchNamesExcept))
  1375. $aResult[$aInput['name']] = array(
  1376. 'type' => $aInput['type'],
  1377. 'caption_system' => !empty($aInput['caption_system_src']) ? $aInput['caption_system_src'] : '',
  1378. 'caption' => !empty($aInput['caption_src']) ? $aInput['caption_src'] : (!empty($aSrchCaptions[$aInput['name']]) ? $aSrchCaptions[$aInput['name']] : ''),
  1379. 'info' => !empty($aInput['info_src']) ? $aInput['info_src'] : '',
  1380. 'value' => !empty($aInput['value']) ? $aInput['value'] : '',
  1381. 'values' => !empty($aInput['values_src']) ? $aInput['values_src'] : '',
  1382. 'pass' => !empty($aInput['db']['pass']) ? $aInput['db']['pass'] : ''
  1383. );
  1384. return $aResult;
  1385. }
  1386. /**
  1387. * Overwrite this method and register transcoder(s) if comments object uses custom transcoder(s),
  1388. * which differs from default one 'sys_cmts_images_preview'
  1389. */
  1390. public function registerTranscoders()
  1391. {}
  1392. /**
  1393. * Overwrite this method and unregister transcoder(s) if comments object uses custom transcoder(s),
  1394. * which differs from default one 'sys_cmts_images_preview'
  1395. */
  1396. public function unregisterTranscoders()
  1397. {}
  1398. public function serviceGetSearchResultExtended($aParams, $iStart = 0, $iPerPage = 0, $bFilterMode = false)
  1399. {
  1400. if((empty($aParams) || !is_array($aParams)) && !$bFilterMode)
  1401. return array();
  1402. return $this->_oQuery->getCommentsBy(array('type' => 'search_ids', 'search_params' => $aParams, 'start' => $iStart, 'per_page' => $iPerPage));
  1403. }
  1404. public function getAuthorInfo($iAuthorId = 0)
  1405. {
  1406. return $this->_getAuthorInfo($iAuthorId);
  1407. }
  1408. public function getParams(&$aBp, &$aDp)
  1409. {
  1410. return $this->_getParams($aBp, $aDp);
  1411. }
  1412. public function prepareParams(&$aBp, &$aDp)
  1413. {
  1414. return $this->_prepareParams($aBp, $aDp);
  1415. }
  1416. /**
  1417. * Internal functions
  1418. */
  1419. protected function _getAuthorId ()
  1420. {
  1421. return isMember() ? bx_get_logged_profile_id() : 0;
  1422. }
  1423. protected function _getAuthorPassword ()
  1424. {
  1425. return getLoggedPassword();
  1426. }
  1427. protected function _getAuthorIp ()
  1428. {
  1429. return getVisitorIP();
  1430. }
  1431. protected function _getAuthorInfo($iAuthorId = 0)
  1432. {
  1433. $iUserId = $this->_getAuthorId();
  1434. $iAuthorId = (int)$iAuthorId;
  1435. $oProfile = $this->_getAuthorObject($iAuthorId);
  1436. if (!$oProfile->isActive() && !isAdmin() && !BxDolAcl::getInstance()->isMemberLevelInSet(array(MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR)))
  1437. $oProfile = BxDolProfileUndefined::getInstance();
  1438. return array(
  1439. $oProfile->getDisplayName(),
  1440. $oProfile->getUrl(),
  1441. $oProfile->getThumb(),
  1442. $oProfile->getUnit(0, array('template' => 'unit_wo_info')),
  1443. $oProfile->getBadges()
  1444. );
  1445. }
  1446. protected function _getAuthorObject($iAuthorId = 0)
  1447. {
  1448. return BxDolProfile::getInstanceMagic((int)$iAuthorId);
  1449. }
  1450. protected function _getFormObject($sAction = BX_CMT_ACTION_POST)
  1451. {
  1452. $sDisplayName = '_sFormDisplay' . ucfirst($sAction);
  1453. return BxDolForm::getObjectInstance($this->_sFormObject, $this->$sDisplayName, false, $this->_sSystem);
  1454. }
  1455. protected function _unsetFormObject($sAction = BX_CMT_ACTION_POST)
  1456. {
  1457. $sDisplayName = '_sFormDisplay' . ucfirst($sAction);
  1458. return BxDolForm::unsetObjectInstance($this->_sFormObject, $this->$sDisplayName, false, $this->_sSystem);
  1459. }
  1460. protected function _getParams(&$aBp, &$aDp)
  1461. {
  1462. //--- Process 'Browse' params.
  1463. $aBp['parent_id'] = isset($aBp['parent_id']) ? (int)$aBp['parent_id'] : 0;
  1464. $aBp['vparent_id'] = isset($aBp['vparent_id']) ? (int)$aBp['vparent_id'] : 0;
  1465. if(bx_get('CmtParent') !== false)
  1466. $aBp['vparent_id'] = bx_process_input(bx_get('CmtParent'), BX_DATA_INT);
  1467. $aBp['type'] = isset($aBp['type']) ? $aBp['type'] : '';
  1468. if(bx_get('CmtBrowse') !== false)
  1469. $aBp['type'] = bx_process_input(bx_get('CmtBrowse'), BX_DATA_TEXT);
  1470. $aBp['filter'] = isset($aBp['filter']) ? $aBp['filter'] : '';
  1471. if(bx_get('CmtFilter') !== false)
  1472. $aBp['filter'] = bx_process_input(bx_get('CmtFilter'), BX_DATA_TEXT);
  1473. $aBp['start'] = isset($aBp['start']) ? (int)$aBp['start'] : -1;
  1474. if(bx_get('CmtStart') !== false)
  1475. $aBp['start'] = bx_process_input($_REQUEST['CmtStart'], BX_DATA_INT);
  1476. $aBp['per_view'] = isset($aBp['per_view']) ? (int)$aBp['per_view'] : -1;
  1477. if(bx_get('CmtPerView') !== false)
  1478. $aBp['per_view'] = bx_process_input($_REQUEST['CmtPerView'], BX_DATA_INT);
  1479. $aBp['pinned'] = isset($aBp['pinned']) ? (int)$aBp['pinned'] : 0;
  1480. if(bx_get('CmtPinned') !== false)
  1481. $aBp['pinned'] = bx_process_input(bx_get('CmtPinned'), BX_DATA_INT);
  1482. //--- Process 'Display' params.
  1483. $aDp['type'] = isset($aDp['type']) ? $aDp['type'] : '';
  1484. if(bx_get('CmtDisplay') !== false)
  1485. $aDp['type'] = bx_process_input($_REQUEST['CmtDisplay'], BX_DATA_TEXT);
  1486. if(bx_get('CmtDisplayStructure') !== false) {
  1487. $aDp['structure'] = bx_process_input($_REQUEST['CmtDisplayStructure'], BX_DATA_INT) == 1;
  1488. if($aDp['structure'] && bx_get('CmtParent') !== false)
  1489. $aBp['parent_id'] = bx_process_input(bx_get('CmtParent'), BX_DATA_INT);
  1490. }
  1491. $aDp['blink'] = isset($aDp['blink']) ? $aDp['blink'] : '';
  1492. if(bx_get('CmtBlink') !== false)
  1493. $aDp['blink'] = bx_process_input($_REQUEST['CmtBlink'], BX_DATA_TEXT);
  1494. $aDp['quote'] = isset($aDp['quote']) ? (int)$aDp['quote'] : 0;
  1495. if(bx_get('CmtQuote') !== false)
  1496. $aDp['quote'] = bx_process_input($_REQUEST['CmtQuote'], BX_DATA_INT);
  1497. $aDp['min_post_form'] = isset($aDp['min_post_form']) ? (bool)$aDp['min_post_form'] : $this->_bMinPostForm;
  1498. if(bx_get('CmtMinPostForm') !== false)
  1499. $aDp['min_post_form'] = bx_process_input(bx_get('CmtMinPostForm'), BX_DATA_INT) == 1;
  1500. $aDp['in_designbox'] = isset($aDp['in_designbox']) ? (bool)$aDp['in_designbox'] : true;
  1501. $aDp['dynamic_mode'] = isset($aDp['dynamic_mode']) ? (bool)$aDp['dynamic_mode'] : false;
  1502. $aDp['show_empty'] = isset($aDp['show_empty']) ? (bool)$aDp['show_empty'] : false;
  1503. }
  1504. protected function _prepareAlertParams($aCmt)
  1505. {
  1506. $iObjId = (int)$this->getId();
  1507. $iObjAthrId = $this->getObjectAuthorId($iObjId);
  1508. $iObjAthrPrivacyView = $this->getObjectPrivacyView($iObjId);
  1509. $iCmtId = (int)$aCmt['cmt_id'];
  1510. $iCmtUniqId = $this->getCommentUniqId($iCmtId);
  1511. $iCmtCf = isset($aCmt['cmt_cf']) ? (int)$aCmt['cmt_cf'] : BxDolContentFilter::getInstance()->getDefaultValue();
  1512. return array(
  1513. 'source' => 'sys_cmts_' . $iCmtUniqId,
  1514. 'object_system' => $this->_sSystem,
  1515. 'object_id' => $iObjId,
  1516. 'object_author_id' => $iObjAthrId,
  1517. 'comment_id' => $iCmtId,
  1518. 'comment_uniq_id' => $iCmtUniqId,
  1519. 'comment_author_id' => $aCmt['cmt_author_id'],
  1520. 'comment_text' => $aCmt['cmt_text'],
  1521. 'privacy_view' => $iObjAthrPrivacyView,
  1522. 'cf' => $iCmtCf
  1523. );
  1524. }
  1525. protected function _prepareAlertParamsReply($aCmt, $aCmtPrnt)
  1526. {
  1527. $iObjId = (int)$this->getId();
  1528. $iObjAthrId = $this->getObjectAuthorId($iObjId);
  1529. $iObjAthrPrivacyView = $this->getObjectPrivacyView($iObjId);
  1530. $iCmtPrntId = (int)$aCmt['cmt_parent_id'];
  1531. $iCmtPrntUniqId = $this->getCommentUniqId($iCmtPrntId);
  1532. $iCmtId = (int)$aCmt['cmt_id'];
  1533. $iCmtUniqId = $this->getCommentUniqId($iCmtId);
  1534. return array(
  1535. 'source' => 'sys_cmts_' . $iCmtUniqId,
  1536. 'object_system' => $this->_sSystem,
  1537. 'object_id' => $iObjId,
  1538. 'object_author_id' => $iObjAthrId,
  1539. 'parent_id' => $iCmtPrntId,
  1540. 'parent_uniq_id' => $iCmtPrntUniqId,
  1541. 'parent_author_id' => $aCmtPrnt['cmt_author_id'],
  1542. 'comment_id' => $iCmtId,
  1543. 'comment_uniq_id' => $iCmtUniqId,
  1544. 'comment_author_id' => $aCmt['cmt_author_id'],
  1545. 'comment_text' => $aCmt['cmt_text'],
  1546. 'privacy_view' => $iObjAthrPrivacyView,
  1547. );
  1548. }
  1549. protected function _prepareAuditParams($iId, $aData)
  1550. {
  1551. $sModule = $this->_aSystem['module'];
  1552. $oModule = BxDolModule::getInstance($sModule);
  1553. $CNF = isset($oModule->_oConfig->CNF) ? $oModule->_oConfig->CNF : array();
  1554. $aContentInfo = BxDolRequest::serviceExists($sModule, 'get_all') ? BxDolService::call($sModule, 'get_all', array(array('type' => 'id', 'id' => $this->getId()))) : array();
  1555. $iContextId = 0;
  1556. if (!empty($aContentInfo)){
  1557. $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;
  1558. }
  1559. $AuditParams = array(
  1560. 'content_title' => $this->getObjectTitle() ,
  1561. 'context_profile_id' => $iContextId,
  1562. 'content_info_object' => isset($CNF['OBJECT_CMTS_CONTENT_INFO']) ? $CNF['OBJECT_CMTS_CONTENT_INFO'] : '',
  1563. 'data' => array_merge(array('comment_id' => $iId), $aData)
  1564. );
  1565. if ($iContextId > 0)
  1566. $AuditParams['context_profile_title'] = BxDolProfile::getInstance($iContextId)->getDisplayName();
  1567. return $AuditParams;
  1568. }
  1569. protected function _prepareTextForOutput ($s, $iCmtId = 0)
  1570. {
  1571. $iDataAction = !$this->isHtml() ? BX_DATA_TEXT_MULTILINE : BX_DATA_HTML;
  1572. $s = bx_process_output($s, $iDataAction);
  1573. $s = bx_linkify_html($s, 'class="' . BX_DOL_LINK_CLASS . '"');
  1574. if($iCmtId && $this->_sMetatagsObj && ($oMetatags = BxDolMetatags::getObjectInstance($this->_sMetatagsObj)) !== false)
  1575. $s = $oMetatags->metaParse($this->_oQuery->getUniqId($this->_aSystem['system_id'], $iCmtId), $s);
  1576. return $s;
  1577. }
  1578. protected function _prepareStructureBp($sDType, &$aBp)
  1579. {
  1580. $aBp['type'] = !empty($aBp['type']) ? $aBp['type'] : $this->_sBrowseType;
  1581. $aBp['filter'] = !empty($aBp['filter']) ? $aBp['filter'] : $this->_sBrowseFilter;
  1582. $aBp['parent_id'] = isset($aBp['parent_id']) ? $aBp['parent_id'] : 0;
  1583. $aBp['start'] = isset($aBp['start']) ? $aBp['start'] : -1;
  1584. $aBp['init_view'] = isset($aBp['init_view']) ? $aBp['init_view'] : -1;
  1585. $aBp['per_view'] = isset($aBp['per_view']) ? $aBp['per_view'] : -1;
  1586. $aBp['order']['by'] = isset($aBp['order_by']) ? $aBp['order_by'] : $this->_aOrder['by'];
  1587. $aBp['order']['way'] = isset($aBp['order_way']) ? $aBp['order_way'] : $this->_aOrder['way'];
  1588. if($aBp['per_view'] == -1)
  1589. switch($sDType) {
  1590. case BX_CMT_DISPLAY_FLAT:
  1591. $aBp['per_view'] = $this->getPerView(0);
  1592. break;
  1593. case BX_CMT_DISPLAY_THREADED:
  1594. $aBp['per_view'] = $this->getPerView($aBp['parent_id']);
  1595. break;
  1596. }
  1597. }
  1598. protected function _prepareParams(&$aBp, &$aDp)
  1599. {
  1600. $aBp['type'] = !empty($aBp['type']) ? $aBp['type'] : $this->_sBrowseType;
  1601. $aBp['filter'] = !empty($aBp['filter']) ? $aBp['filter'] : $this->_sBrowseFilter;
  1602. $aBp['parent_id'] = isset($aBp['parent_id']) ? $aBp['parent_id'] : 0;
  1603. $aBp['start'] = isset($aBp['start']) ? $aBp['start'] : -1;
  1604. $aBp['init_view'] = isset($aBp['init_view']) ? $aBp['init_view'] : -1;
  1605. $aBp['per_view'] = isset($aBp['per_view']) ? $aBp['per_view'] : -1;
  1606. $aBp['pinned'] = isset($aBp['pinned']) ? (int)$aBp['pinned'] : 0;
  1607. $aBp['order']['by'] = isset($aBp['order_by']) ? $aBp['order_by'] : $this->_aOrder['by'];
  1608. $aBp['order']['way'] = isset($aBp['order_way']) ? $aBp['order_way'] : $this->_aOrder['way'];
  1609. $aDp['type'] = !empty($aDp['type']) ? $aDp['type'] : $this->_sDisplayType;
  1610. $aDp['blink'] = !empty($aDp['blink']) ? $aDp['blink'] : array();
  1611. if(!is_array($aDp['blink']))
  1612. $aDp['blink'] = explode(',', $aDp['blink']);
  1613. $aDp['quote'] = !empty($aDp['quote']) ? (int)$aDp['quote'] : 0;
  1614. if(!isset($aDp['min_post_form']))
  1615. $aDp['min_post_form'] = $this->_bMinPostForm;
  1616. switch($aDp['type']) {
  1617. case BX_CMT_DISPLAY_FLAT:
  1618. $aBp['vparent_id'] = -1;
  1619. $aBp['per_view'] = $aBp['per_view'] != -1 ? $aBp['per_view'] : $this->getPerView(0);
  1620. break;
  1621. case BX_CMT_DISPLAY_THREADED:
  1622. $iParent = 0;
  1623. if(isset($aBp['vparent_id']))
  1624. $iParent = $aBp['vparent_id'];
  1625. else if(isset($aBp['parent_id']))
  1626. $iParent = $aBp['parent_id'];
  1627. $aBp['per_view'] = $aBp['per_view'] != -1 ? $aBp['per_view'] : $this->getPerView($iParent);
  1628. break;
  1629. }
  1630. switch ($aBp['type']) {
  1631. case BX_CMT_BROWSE_POPULAR:
  1632. $aBp['order'] = array(
  1633. 'by' => BX_CMT_ORDER_BY_POPULAR,
  1634. 'way' => BX_CMT_ORDER_WAY_DESC
  1635. );
  1636. break;
  1637. }
  1638. if(!isset($aBp['count']))
  1639. $aBp['count'] = $this->getCommentsCount($this->_iId, $aBp['vparent_id'], $aBp['filter']);
  1640. if($aBp['start'] != -1)
  1641. return;
  1642. $aBp['start'] = 0;
  1643. if($aBp['type'] == BX_CMT_BROWSE_TAIL) {
  1644. $sPerView = ($aBp['init_view'] != -1 ? 'init' : 'per') . '_view';
  1645. $aBp['start'] = $aBp['count'] - $aBp[$sPerView];
  1646. if($aBp['start'] < 0) {
  1647. $aBp[$sPerView] += $aBp['start'];
  1648. $aBp['start'] = 0;
  1649. }
  1650. }
  1651. $this->_setUserChoice($aDp['type'], $aBp['type'], $aBp['filter']);
  1652. }
  1653. protected function _triggerComment()
  1654. {
  1655. if(!$this->_aSystem['trigger_table'] || !$this->_aSystem['trigger_field_id'] || !$this->_aSystem['trigger_field_comments'])
  1656. return false;
  1657. $iId = $this->getId();
  1658. if(!$iId)
  1659. return false;
  1660. $iCount = $this->getCommentsCount($iId);
  1661. return $this->_oQuery->updateTriggerTable($iId, $iCount);
  1662. }
  1663. /**
  1664. * Replace provided markers in a string
  1665. * @param $mixed string or array to replace markers in
  1666. * @return string where all occured markers are replaced
  1667. */
  1668. protected function _replaceMarkers ($mixed)
  1669. {
  1670. return bx_replace_markers($mixed, $this->_aMarkers);
  1671. }
  1672. protected function _getUserChoice()
  1673. {
  1674. $mixedDp = $mixedBpType = $mixedBpFilter = false;
  1675. if(!isLogged())
  1676. return array($mixedDp, $mixedBpType, $mixedBpFilter);
  1677. $iUserId = $this->_getAuthorId();
  1678. $oSession = BxDolSession::getInstance();
  1679. $mixedDp = $oSession->getValue($this->_sDpSessionKey . $iUserId);
  1680. $mixedBpType = $oSession->getValue($this->_sBpSessionKeyType . $iUserId);
  1681. $mixedBpFilter = $oSession->getValue($this->_sBpSessionKeyFilter . $iUserId);
  1682. return array($mixedDp, $mixedBpType, $mixedBpFilter);
  1683. }
  1684. protected function _setUserChoice($sDp, $sBpType, $sBpFilter)
  1685. {
  1686. if(!isLogged())
  1687. return;
  1688. $iUserId = $this->_getAuthorId();
  1689. $oSession = BxDolSession::getInstance();
  1690. if(!empty($sDp))
  1691. $oSession->setValue($this->_sDpSessionKey . $iUserId, $sDp);
  1692. if(!empty($sBpType))
  1693. $oSession->setValue($this->_sBpSessionKeyType . $iUserId, $sBpType);
  1694. if(!empty($sBpFilter))
  1695. $oSession->setValue($this->_sBpSessionKeyFilter . $iUserId, $sBpFilter);
  1696. }
  1697. protected function _sendNotificationEmail($iCmtId, $iCmtParentId)
  1698. {
  1699. $aCmt = $this->getCommentRow($iCmtId);
  1700. $aCmtParent = $this->getCommentRow($iCmtParentId);
  1701. if(empty($aCmt) || !is_array($aCmt) || empty($aCmtParent) || !is_array($aCmtParent) || (int)$aCmt['cmt_author_id'] == (int)$aCmtParent['cmt_author_id'])
  1702. return;
  1703. $oProfile = $this->_getAuthorObject($aCmtParent['cmt_author_id']);
  1704. if($oProfile instanceof BxDolProfileUndefined)
  1705. return;
  1706. $iAccount = $oProfile->getAccountId();
  1707. $aAccount = BxDolAccount::getInstance($iAccount)->getInfo();
  1708. $aPlus = array();
  1709. $aPlus['sender_display_name'] = $oProfile->getDisplayName();
  1710. $aPlus['reply_text'] = $this->_prepareTextForOutput($aCmt['cmt_text'], $iCmtId);
  1711. $sPageUrl = $this->getBaseUrl();
  1712. if(empty($sPageUrl))
  1713. $sPageUrl = $this->getViewUrl($iCmtParentId);
  1714. else
  1715. $sPageUrl .= $this->getItemAnchor($iCmtParentId, true);
  1716. $aPlus['page_url'] = $sPageUrl;
  1717. $sPageTitle = $this->getObjectTitle();
  1718. if(empty($sPageTitle))
  1719. $sPageTitle = _t('_Content');
  1720. $aPlus['page_title'] = $sPageTitle;
  1721. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('t_CommentReplied', $aPlus);
  1722. return $aTemplate && sendMail($aAccount['email'], $aTemplate['Subject'], $aTemplate['Body']);
  1723. }
  1724. protected function _isShowDoComment($aParams, $isAllowedComment, $bCount)
  1725. {
  1726. $bShowDoComment = !isset($aParams['show_do_comment']) || $aParams['show_do_comment'] == true;
  1727. return $bShowDoComment && ($isAllowedComment || $bCount);
  1728. }
  1729. protected function _isShowCounter($aParams, $isAllowedComment, $bCount)
  1730. {
  1731. $bShowCounter = isset($aParams['show_counter']) && $aParams['show_counter'] === true;
  1732. return $bShowCounter && ($isAllowedComment || $bCount);
  1733. }
  1734. /**
  1735. * Note. By default image based controls aren't used.
  1736. * Therefore it can be overwritten in custom template.
  1737. */
  1738. protected function _getImageDo()
  1739. {
  1740. return '';
  1741. }
  1742. protected function _getIconDo()
  1743. {
  1744. return 'far comment';
  1745. }
  1746. protected function _getTitleDo()
  1747. {
  1748. return '_cmt_txt_do';
  1749. }
  1750. public function _getStructure($mixedItem, $aBp, &$iLevel, &$aStructure)
  1751. {
  1752. $bItem = !empty($mixedItem) && is_array($mixedItem);
  1753. $iCmtId = $bItem ? (int)$mixedItem['cmt_id'] : 0;
  1754. $iCmtIndex = $iCmtId;
  1755. if($bItem)
  1756. $aStructure[$iCmtIndex] = [
  1757. 'id' => $mixedItem['cmt_id'],
  1758. 'order' => isset($mixedItem['cmt_order']) ? $mixedItem['cmt_order'] : 0,
  1759. 'items' => [],
  1760. ];
  1761. if(!$bItem || (int)$mixedItem['cmt_replies'] > 0) {
  1762. $aItems = $this->_oQuery->getStructure($this->_iId, $this->_iAuthorId, $iCmtId, $aBp['filter'], $aBp['order']);
  1763. if(!empty($aItems)) {
  1764. if($bItem && $iLevel < (int)$this->_aSystem['number_of_levels']) {
  1765. $iPassLevel = $iLevel + 1;
  1766. $aPassStructure = &$aStructure[$iCmtIndex]['items'];
  1767. }
  1768. else {
  1769. $iPassLevel = $iLevel;
  1770. $aPassStructure = &$aStructure; //--- Is NEEDED here to correctly check sublevels.
  1771. }
  1772. foreach($aItems as $aItem)
  1773. $this->_getStructure($aItem, $aBp, $iPassLevel, $aPassStructure);
  1774. //--- Sort subitems
  1775. $iWay = isset($aBp['order']['way']) && $aBp['order']['way'] == 'desc' ? -1 : 1;
  1776. uasort($aPassStructure, function($aItem1, $aItem2) use ($iWay) {
  1777. if($aItem1['order'] == $aItem2['order'])
  1778. return 0;
  1779. return $iWay * ($aItem1['order'] < $aItem2['order'] ? -1 : 1);
  1780. });
  1781. }
  1782. }
  1783. }
  1784. public function _getStructureAPI($mixedItem, $aBp, &$iLevel, &$aStructure)
  1785. {
  1786. $bItem = !empty($mixedItem) && is_array($mixedItem);
  1787. $iCmtId = $bItem ? (int)$mixedItem['cmt_id'] : 0;
  1788. $sCmtIndex = 'i' . $iCmtId;
  1789. if($bItem) {
  1790. $oMenuActions = BxDolMenu::getObjectInstance($this->_sMenuObjActions);
  1791. $oMenuActions->setCmtsData($this, $iCmtId, $aBp);
  1792. $aData = $this->getCommentSimple($iCmtId);
  1793. $aData = array_merge($aData, [
  1794. 'author_data' => BxDolProfile::getData($aData['cmt_author_id']),
  1795. 'menu_actions' => $oMenuActions->getCodeAPI()
  1796. ]);
  1797. $aStructure[$sCmtIndex] = [
  1798. 'id' => $iCmtId,
  1799. 'order' => isset($mixedItem['cmt_order']) ? $mixedItem['cmt_order'] : 0,
  1800. 'data' => $aData,
  1801. 'files' => $this->_getAttachments($mixedItem),
  1802. 'items' => [],
  1803. ];
  1804. }
  1805. if(!$bItem || (int)$mixedItem['cmt_replies'] > 0) {
  1806. $aItems = $this->_oQuery->getStructure($this->_iId, $this->_iAuthorId, $iCmtId, $aBp['filter'], $aBp['order']);
  1807. if(!empty($aItems)) {
  1808. if($bItem && $iLevel < (int)$this->_aSystem['number_of_levels']) {
  1809. $iPassLevel = $iLevel + 1;
  1810. $aPassStructure = &$aStructure[$sCmtIndex]['items'];
  1811. }
  1812. else {
  1813. $iPassLevel = $iLevel;
  1814. $aPassStructure = [];
  1815. }
  1816. foreach($aItems as $aItem)
  1817. $this->_getStructureAPI($aItem, $aBp, $iPassLevel, $aPassStructure);
  1818. //--- Sort subitems
  1819. $iWay = isset($aBp['order']['way']) && $aBp['order']['way'] == 'desc' ? -1 : 1;
  1820. uasort($aPassStructure, function($aItem1, $aItem2) use ($iWay) {
  1821. if($aItem1['order'] == $aItem2['order'])
  1822. return 0;
  1823. return $iWay * ($aItem1['order'] < $aItem2['order'] ? -1 : 1);
  1824. });
  1825. $aStructure[$sCmtIndex]['items'] = $aPassStructure;
  1826. }
  1827. }
  1828. }
  1829. }
  1830. /** @} */