BxBaseServices.php 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * System services.
  11. */
  12. class BxBaseServices extends BxDol implements iBxDolProfileService
  13. {
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. }
  18. public function serviceIsPublicService($s)
  19. {
  20. $sService = bx_gen_method_name($s);
  21. $aServices = $this->serviceGetPublicServices();
  22. return isset($aServices[$sService]);
  23. }
  24. public function serviceGetPublicServices()
  25. {
  26. return array (
  27. 'GetProductsNames' => 'BxBaseServices',
  28. 'GetPageByRequest' => 'BxBaseServicePages',
  29. );
  30. }
  31. public function serviceIsSafeService($s)
  32. {
  33. $sService = bx_gen_method_name($s);
  34. $aSafeServices = $this->serviceGetSafeServices();
  35. return isset($aSafeServices[$sService]);
  36. }
  37. public function serviceGetSafeServices()
  38. {
  39. return array(
  40. 'GetMenu' => 'BxBaseServices',
  41. 'GetCreatePostForm' => 'BxBaseServices',
  42. 'GetProductsNames' => 'BxBaseServices',
  43. 'KeywordSearch' => 'BxBaseServices',
  44. 'GetDataSearchApi' => 'BxBaseServices',
  45. 'Cmts' => 'BxBaseServices',
  46. 'GetFooter' => 'BxBaseServices',
  47. 'SetBadges' => 'BxBaseServices',
  48. 'GetPageContentByRequest' => 'BxBaseServicePages',
  49. 'GetPageBlockData' => 'BxBaseServicePages',
  50. 'SetPageBlockData' => 'BxBaseServicePages',
  51. 'GetPageBlockImageData' => 'BxBaseServicePages',
  52. 'GetUrlInfo' => 'BxBaseServicePages',
  53. 'CreateAccountForm' => 'BxBaseServiceAccount',
  54. 'AccountSettingsEmail' => 'BxBaseServiceAccount',
  55. 'AccountSettingsPassword' => 'BxBaseServiceAccount',
  56. 'AccountSettingsDelAccount' => 'BxBaseServiceAccount',
  57. 'AccountSettingsInfo' => 'BxBaseServiceAccount',
  58. 'ForgotPassword' => 'BxBaseServiceAccount',
  59. 'SwitchProfile' => 'BxBaseServiceAccount',
  60. 'AccountProfileSwitcher' => 'BxBaseServiceAccount',
  61. 'EmailConfirmation' => 'BxBaseServiceAccount',
  62. 'ConfirmEmail' => 'BxBaseServiceAccount',
  63. 'CategoriesList' => 'BxBaseServiceCategory',
  64. 'Test' => 'BxBaseServiceLogin',
  65. 'MemberAuthCode' => 'BxBaseServiceLogin',
  66. 'LoginForm' => 'BxBaseServiceLogin',
  67. 'LoginFormOnly' => 'BxBaseServiceLogin',
  68. 'Logout' => 'BxBaseServiceLogin',
  69. 'KeywordsCloud' => 'BxBaseServiceMetatags',
  70. 'AccountProfileSwitcher' => 'BxBaseServiceProfiles',
  71. 'ProfileMembership' => 'BxBaseServiceProfiles',
  72. 'ProfileNotifications' => 'BxBaseServiceProfiles',
  73. 'GetCountOnlineProfiles' => 'BxBaseServiceProfiles',
  74. 'BrowseRecommendationsFriends' => 'BxBaseServiceProfiles',
  75. 'BrowseRecommendationsSubscriptions' => 'BxBaseServiceProfiles',
  76. 'BrowseFriends' => 'BxBaseServiceProfiles',
  77. 'SetMembership' => 'BxBaseServiceProfiles',
  78. 'ResetMembership' => 'BxBaseServiceProfiles',
  79. 'BrowseFriendRequests' => 'BxBaseServiceProfiles',
  80. 'BrowseFriendRequested' => 'BxBaseServiceProfiles',
  81. 'BrowseSubscribedMe' => 'BxBaseServiceProfiles',
  82. 'BrowseSubscriptions' => 'BxBaseServiceProfiles',
  83. 'BrowseMembers' => 'BxBaseServiceProfiles',
  84. 'BrowseInvitations' => 'BxBaseServiceProfiles',
  85. 'UpdateSettings' => 'BxBaseServiceProfiles',
  86. 'Befriend' => 'BxBaseServiceProfiles',
  87. 'ProfileCounters' => 'BxBaseServiceProfiles',
  88. 'ProfileInfo' => 'BxBaseServiceProfiles',
  89. 'GetChartGrowth' => 'BxBaseChartServices',
  90. 'GetChartStats' => 'BxBaseChartServices',
  91. 'GetDataByInterval' => 'BxBaseChartServices',
  92. 'GetCartItemsCount' => 'BxBasePaymentsServices',
  93. 'GetOrdersCount' => 'BxBasePaymentsServices',
  94. 'Do' => 'BxBaseReportServices',
  95. 'GetPerformedBy' => 'BxBaseReportServices',
  96. 'Do' => 'BxBaseVoteServices',
  97. 'GetPerformedBy' => 'BxBaseVoteServices',
  98. 'Perform' => 'BxBaseFeatureServices',
  99. 'Perform' => 'BxBaseFavoriteServices',
  100. 'GetPerformedBy' => 'BxBaseFavoriteServices',
  101. 'Perform' => 'BxBaseServiceConnections',
  102. 'Perform' => 'BxBaseServiceRecommendations',
  103. 'GetDataApi' => 'BxBaseCmtsServices',
  104. 'GetDataApi' => 'BxBaseUploaderServices',
  105. 'GetStatBlock' => 'BxBaseDashboardServices',
  106. 'PerfomActionApi' => 'BxBaseServiceGrid',
  107. 'GetLabels' => 'BxBaseLabelsServices',
  108. 'GetForm' => 'BxBaseSearchExtendedServices',
  109. 'GetResults' => 'BxBaseSearchExtendedServices',
  110. 'WikiAction' => 'BxBaseServiceWiki'
  111. );
  112. }
  113. public function serviceGetPreloaderContent($sName) {
  114. $sResult = '';
  115. switch($sName) {
  116. case 'icons_css':
  117. if(($sCss = BxDolIconset::getObjectInstance()->getPreloaderCss()) !== false)
  118. $sResult = $sCss;
  119. break;
  120. case 'icons_js':
  121. if(($sJs = BxDolIconset::getObjectInstance()->getPreloaderJs()) !== false)
  122. $sResult = $sJs;
  123. break;
  124. case 'tailwind':
  125. $sFile = getParam('sys_css_tailwind_default');
  126. if(empty($sFile))
  127. $sFile = 'tailwind.min.css';
  128. $sPath = '{dir_plugins_public}tailwind/css/|';
  129. if($sFile == 'cdn')
  130. $sResult = [
  131. 'https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4',
  132. $sPath . 'tailwind.min.css' //TODO: Replace this with correct file name.
  133. ];
  134. else
  135. $sResult = $sPath . $sFile;
  136. break;
  137. }
  138. return $sResult;
  139. }
  140. /**
  141. * @page service Service Calls
  142. * @section bx_system_general System Services
  143. * @subsection bx_system_general-general General
  144. * @subsubsection bx_system_general-module_check_for_update module_check_for_update
  145. *
  146. * @code bx_srv('system', 'module_check_for_update', ["bx_posts"]); @endcode
  147. * @code bx_srv('system', 'module_check_for_update', ["bx_posts"], 'TemplServices'); @endcode
  148. * @code {{~system:module_check_for_update:TemplServices["bx_posts"]~}} @endcode
  149. *
  150. * Checks whether an update is available for requested module or not.
  151. * @param $mixedModule - module ID/name to check the updates for.
  152. * @return array with information about the update, false if nothing was found
  153. *
  154. * @see BxBaseServices::serviceModuleCheckForUpdate
  155. */
  156. /**
  157. * @ref bx_system_general-module_check_for_update "module_check_for_update"
  158. */
  159. public function serviceModuleCheckForUpdate($mixedModule)
  160. {
  161. $aModule = BxDolModuleQuery::getInstance()->{'getModuleBy' . (is_numeric($mixedModule) ? 'Id' : 'Name')}($mixedModule, false);
  162. if(empty($aModule) || !is_array($aModule))
  163. return false;
  164. $aUpdates = BxDolStudioInstallerUtils::getInstance()->checkUpdatesByModule($aModule['name']);
  165. if(empty($aUpdates) || !is_array($aUpdates))
  166. return false;
  167. $aUpdate = array_shift($aUpdates);
  168. return array(
  169. 'version_from' => $aUpdate['file_version'],
  170. 'version_to' => $aUpdate['file_version_to'],
  171. 'file_id' => $aUpdate['file_id']
  172. );
  173. }
  174. /**
  175. * @page service Service Calls
  176. * @section bx_system_general System Services
  177. * @subsection bx_system_general-general General
  178. * @subsubsection bx_system_general-module_update module_update
  179. *
  180. * @code bx_srv('system', 'module_update', ["bx_posts"]); @endcode
  181. * @code bx_srv('system', 'module_update', ["bx_posts"], 'TemplServices'); @endcode
  182. * @code {{~system:module_update:TemplServices["bx_posts"]~}} @endcode
  183. *
  184. * Download and install an update for requested module. The operation may be performed
  185. * immediately or by Transient Cron task. The second way is more common.
  186. * @param $mixedModule - module ID/name to check the updates for.
  187. * @return integer value determining the result of the operation. It can be one of the following values:
  188. * BX_DOL_STUDIO_IU_RC_SUCCESS, BX_DOL_STUDIO_IU_RC_FAILED or BX_DOL_STUDIO_IU_RC_SCHEDULED.
  189. *
  190. * @see BxBaseServices::serviceModuleUpdate
  191. */
  192. /**
  193. * @ref bx_system_general-module_update "module_update"
  194. */
  195. public function serviceModuleUpdate($mixedModule)
  196. {
  197. $aModule = BxDolModuleQuery::getInstance()->{'getModuleBy' . (is_numeric($mixedModule) ? 'Id' : 'Name')}($mixedModule, false);
  198. if(empty($aModule) || !is_array($aModule))
  199. return BX_DOL_STUDIO_IU_RC_FAILED;
  200. $aResult = BxDolStudioInstallerUtils::getInstance()->downloadUpdatePublic($aModule['name'], true);
  201. return $aResult['code'];
  202. }
  203. /**
  204. * @page service Service Calls
  205. * @section bx_system_general System Services
  206. * @subsection bx_system_general-general General
  207. * @subsubsection bx_system_general-module_delete module_delete
  208. *
  209. * @code bx_srv('system', 'module_delete', ["bx_posts"]); @endcode
  210. * @code bx_srv('system', 'module_delete', ["bx_posts", true], 'TemplServices'); @endcode
  211. * @code {{~system:module_delete:TemplServices["bx_posts"]~}} @endcode
  212. *
  213. * Delete the requested module. By default only uninstalled module can be deleted.
  214. * The operation may be performed immediately or by Transient Cron task. The second way is more common.
  215. * @param $sModule - module name to delete.
  216. * @param $bForceUninstall - if 'true' is passed the module will be uninstalled before deletion.
  217. * @return integer value determining the result of the operation. It can be one of the following values:
  218. * BX_DOL_STUDIO_IU_RC_SUCCESS, BX_DOL_STUDIO_IU_RC_FAILED or BX_DOL_STUDIO_IU_RC_SCHEDULED.
  219. *
  220. * @see BxBaseServices::serviceModuleDelete
  221. */
  222. /**
  223. * @ref bx_system_general-module_delete "module_delete"
  224. */
  225. public function serviceModuleDelete($sModule, $bForceUninstall = false)
  226. {
  227. $sModulePath = '';
  228. $oInstallerUtils = BxDolStudioInstallerUtils::getInstance();
  229. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($sModule, false);
  230. if(!empty($aModule) && is_array($aModule)) {
  231. if(!$bForceUninstall)
  232. return BX_DOL_STUDIO_IU_RC_FAILED;
  233. $sModulePath = $aModule['path'];
  234. $aResult = $oInstallerUtils->perform($sModulePath, 'uninstall', array('auto_disable' => $bForceUninstall));
  235. if($aResult['code'] != BX_DOL_STUDIO_IU_RC_SUCCESS)
  236. return BX_DOL_STUDIO_IU_RC_FAILED;
  237. }
  238. else {
  239. $aModules = $oInstallerUtils->getModules(false);
  240. if(!isset($aModules[$sModule]))
  241. return BX_DOL_STUDIO_IU_RC_FAILED;
  242. $sModulePath = $aModules[$sModule]['dir'];
  243. }
  244. $aResult = $oInstallerUtils->perform($sModulePath, 'delete');
  245. return $aResult['code'];
  246. }
  247. /**
  248. * @page service Service Calls
  249. * @section bx_system_general System Services
  250. * @subsection bx_system_general-general General
  251. * @subsubsection bx_system_general-module_install module_install
  252. *
  253. * @code bx_srv('system', 'module_install', ["bx_posts"]); @endcode
  254. * @code bx_srv('system', 'module_install', ["bx_posts", true], 'TemplServices'); @endcode
  255. * @code {{~system:module_install:TemplServices["bx_posts"]~}} @endcode
  256. *
  257. * Install the requested module. By default the module will be installed only.
  258. * @param $sModule - module name to install.
  259. * @param $bForceEnable - if 'true' is passed the module will be automatically enabled after installation.
  260. * @return boolean value determining the result of the operation.
  261. *
  262. * @see BxBaseServices::serviceModuleInstall
  263. */
  264. /**
  265. * @ref bx_system_general-module_install "module_install"
  266. */
  267. public function serviceModuleInstall($sModule, $bForceEnable = false)
  268. {
  269. $oInstallerUtils = BxDolStudioInstallerUtils::getInstance();
  270. $aModules = $oInstallerUtils->getModules(false);
  271. if(!isset($aModules[$sModule]))
  272. return false;
  273. $aResult = $oInstallerUtils->perform($aModules[$sModule]['dir'], 'install', array('auto_enable' => $bForceEnable));
  274. if($aResult['code'] != BX_DOL_STUDIO_IU_RC_SUCCESS)
  275. return false;
  276. return true;
  277. }
  278. /**
  279. * @page service Service Calls
  280. * @section bx_system_general System Services
  281. * @subsection bx_system_general-general General
  282. * @subsubsection bx_system_general-module_uninstall module_uninstall
  283. *
  284. * @code bx_srv('system', 'module_uninstall', ["bx_posts"]); @endcode
  285. * @code bx_srv('system', 'module_uninstall', ["bx_posts", true], 'TemplServices'); @endcode
  286. * @code {{~system:module_uninstall:TemplServices["bx_posts"]~}} @endcode
  287. *
  288. * Uninstall the requested module. By default only disabled module can be uninstalled.
  289. * @param $mixedModule - module ID/name to uninstall.
  290. * @param $bForceDisable - if 'true' is passed the module will be automatically disabled before uninstallation.
  291. * @return boolean value determining the result of the operation.
  292. *
  293. * @see BxBaseServices::serviceModuleUninstall
  294. */
  295. /**
  296. * @ref bx_system_general-module_uninstall "module_uninstall"
  297. */
  298. public function serviceModuleUninstall($mixedModule, $bForceDisable = false)
  299. {
  300. $aModule = BxDolModuleQuery::getInstance()->{'getModuleBy' . (is_numeric($mixedModule) ? 'Id' : 'Name')}($mixedModule, false);
  301. if(empty($aModule) || !is_array($aModule))
  302. return false;
  303. if((int)$aModule['enabled'] != 0 && !$bForceDisable)
  304. return false;
  305. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($aModule['path'], 'uninstall', array('auto_disable' => $bForceDisable));
  306. if($aResult['code'] != BX_DOL_STUDIO_IU_RC_SUCCESS)
  307. return false;
  308. return true;
  309. }
  310. /**
  311. * @page service Service Calls
  312. * @section bx_system_general System Services
  313. * @subsection bx_system_general-general General
  314. * @subsubsection bx_system_general-module_enable module_enable
  315. *
  316. * @code bx_srv('system', 'module_enable', ["bx_posts"]); @endcode
  317. * @code bx_srv('system', 'module_enable', ["bx_posts"], 'TemplServices'); @endcode
  318. * @code {{~system:module_enable:TemplServices["bx_posts"]~}} @endcode
  319. *
  320. * Enable the requested module.
  321. * @param $mixedModule - module ID/name to enable.
  322. * @return boolean value determining the result of the operation.
  323. *
  324. * @see BxBaseServices::serviceModuleEnable
  325. */
  326. /**
  327. * @ref bx_system_general-module_enable "module_enable"
  328. */
  329. public function serviceModuleEnable($mixedModule)
  330. {
  331. $aModule = BxDolModuleQuery::getInstance()->{'getModuleBy' . (is_numeric($mixedModule) ? 'Id' : 'Name')}($mixedModule, false);
  332. if(empty($aModule) || !is_array($aModule))
  333. return false;
  334. if((int)$aModule['enabled'] != 0)
  335. return false;
  336. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($aModule['path'], 'enable');
  337. if($aResult['code'] != BX_DOL_STUDIO_IU_RC_SUCCESS)
  338. return false;
  339. return true;
  340. }
  341. /**
  342. * @page service Service Calls
  343. * @section bx_system_general System Services
  344. * @subsection bx_system_general-general General
  345. * @subsubsection bx_system_general-module_disable module_disable
  346. *
  347. * @code bx_srv('system', 'module_disable', ["bx_posts"]); @endcode
  348. * @code bx_srv('system', 'module_disable', ["bx_posts"], 'TemplServices'); @endcode
  349. * @code {{~system:module_disable:TemplServices["bx_posts"]~}} @endcode
  350. *
  351. * Disable the requested module.
  352. * @param $mixedModule - module ID/name to disable.
  353. * @return boolean value determining the result of the operation.
  354. *
  355. * @see BxBaseServices::serviceModuleDisable
  356. */
  357. /**
  358. * @ref bx_system_general-module_disable "module_disable"
  359. */
  360. public function serviceModuleDisable($mixedModule)
  361. {
  362. $aModule = BxDolModuleQuery::getInstance()->{'getModuleBy' . (is_numeric($mixedModule) ? 'Id' : 'Name')}($mixedModule, false);
  363. if(empty($aModule) || !is_array($aModule))
  364. return false;
  365. if((int)$aModule['enabled'] == 0)
  366. return false;
  367. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($aModule['path'], 'disable');
  368. if($aResult['code'] != BX_DOL_STUDIO_IU_RC_SUCCESS)
  369. return false;
  370. return true;
  371. }
  372. /**
  373. * @page service Service Calls
  374. * @section bx_system_general System Services
  375. * @subsection bx_system_general-general General
  376. * @subsubsection bx_system_general-is_module_installed is_module_installed
  377. *
  378. * @code bx_srv('system', 'is_module_installed', ["bx_posts"]); @endcode
  379. * @code bx_srv('system', 'is_module_installed', ["bx_posts"], 'TemplServices'); @endcode
  380. * @code {{~system:is_module_installed:TemplServices["bx_posts"]~}} @endcode
  381. *
  382. * Checks whether the requested module is installed or not.
  383. * @param $mixedModule - module ID/name to check.
  384. * @return boolean value determining the result of the operation.
  385. *
  386. * @see BxBaseServices::serviceIsModuleInstalled
  387. */
  388. /**
  389. * @ref bx_system_general-is_module_installed "is_module_installed"
  390. */
  391. public function serviceIsModuleInstalled($mixedModule)
  392. {
  393. $aModule = BxDolModuleQuery::getInstance()->{'getModuleBy' . (is_numeric($mixedModule) ? 'Id' : 'Name')}($mixedModule, false);
  394. return !empty($aModule) && is_array($aModule);
  395. }
  396. /**
  397. * @page service Service Calls
  398. * @section bx_system_general System Services
  399. * @subsection bx_system_general-general General
  400. * @subsubsection bx_system_general-is_module_enabled is_module_enabled
  401. *
  402. * @code bx_srv('system', 'is_module_enabled', ["bx_posts"]); @endcode
  403. * @code bx_srv('system', 'is_module_enabled', ["bx_posts"], 'TemplServices'); @endcode
  404. * @code {{~system:is_module_enabled:TemplServices["bx_posts"]~}} @endcode
  405. *
  406. * Checks whether the requested module is enabled or not.
  407. * @param $mixedModule - module ID/name to check.
  408. * @return boolean value determining the result of the operation.
  409. *
  410. * @see BxBaseServices::serviceIsModuleEnabled
  411. */
  412. /**
  413. * @ref bx_system_general-is_module_enabled "is_module_enabled"
  414. */
  415. public function serviceIsModuleEnabled($mixedModule)
  416. {
  417. $aModule = BxDolModuleQuery::getInstance()->{'getModuleBy' . (is_numeric($mixedModule) ? 'Id' : 'Name')}($mixedModule, false);
  418. return !empty($aModule) && is_array($aModule) && (int)$aModule['enabled'] != 0;
  419. }
  420. /**
  421. * Checks whether a module is a 'Content' module or not.
  422. *
  423. * @param string $mixedModule - module name or an instance of Module class.
  424. * @return boolean result of operation.
  425. */
  426. public function serviceIsModuleContent($mixedModule)
  427. {
  428. if($mixedModule instanceof BxDolModule)
  429. $mixedModule = $mixedModule->getName();
  430. return BxDolModuleQuery::getInstance()->isModuleContent($mixedModule);
  431. }
  432. /**
  433. * Checks whether a module is a 'Context' module or not.
  434. *
  435. * @param string $mixedModule - module name or an instance of Module class.
  436. * @return boolean result of operation.
  437. */
  438. public function serviceIsModuleContext($mixedModule)
  439. {
  440. if($mixedModule instanceof BxDolModule)
  441. $mixedModule = $mixedModule->getName();
  442. return BxDolModuleQuery::getInstance()->isModuleContext($mixedModule);
  443. }
  444. /**
  445. * Checks whether a module is a 'Profile' module or not.
  446. *
  447. * @param string $mixedModule - module name or an instance of Module class.
  448. * @return boolean result of operation.
  449. */
  450. public function serviceIsModuleProfile($mixedModule)
  451. {
  452. if($mixedModule instanceof BxDolModule)
  453. $mixedModule = $mixedModule->getName();
  454. return BxDolModuleQuery::getInstance()->isModuleProfile($mixedModule);
  455. }
  456. /**
  457. * Get modules by type. Available types are 'content', 'context', 'profile'.
  458. *
  459. * @param type $sType - string with type.
  460. * @return array of modules.
  461. */
  462. public function serviceGetModulesByType($sType, $aParams = [])
  463. {
  464. $oModuleQuery = BxDolModuleQuery::getInstance();
  465. if(!$oModuleQuery->checkModulesSubtypes()) {
  466. $oInstallerUtils = new BxDolInstallerUtils();
  467. $aModules = $oModuleQuery->getModulesBy(['type' => 'modules']);
  468. foreach($aModules as $aModule)
  469. if(($iModuleSubtypes = $oInstallerUtils->getSubtypes($aModule['name'])) != 0)
  470. $oModuleQuery->updateModule(['subtypes' => $iModuleSubtypes], ['id' => $aModule['id']]);
  471. }
  472. $iSubtypes = 0;
  473. switch($sType) {
  474. case 'content':
  475. $iSubtypes = BX_DOL_MODULE_SUBTYPE_TEXT;
  476. break;
  477. case 'context':
  478. $iSubtypes = BX_DOL_MODULE_SUBTYPE_CONTEXT;
  479. break;
  480. case 'profile':
  481. $iSubtypes = BX_DOL_MODULE_SUBTYPE_PROFILE;
  482. break;
  483. }
  484. $aResults = $oModuleQuery->getModulesBy(array_merge([
  485. 'type' => 'modules_subtypes',
  486. 'value' => $iSubtypes,
  487. 'active' => 1
  488. ], array_intersect_key($aParams, array_flip(['id_as_key', 'name_as_key']))));
  489. return $aResults;
  490. }
  491. public function serviceGetModulesBySubtypeSimple($sType)
  492. {
  493. $a = $this->serviceGetModulesByType($sType, ['name_as_key' => true]);
  494. return array_map(function ($r) {
  495. return $r['title'];
  496. }, $a);
  497. }
  498. public function serviceProfileUnit ($iContentId, $aParams = array())
  499. {
  500. return $this->_serviceProfileFunc('getUnit', $iContentId, $aParams);
  501. }
  502. public function serviceProfileUnitApi ($iContentId, $aParams = [])
  503. {
  504. return $this->_serviceProfileFunc('getUnitApi', $iContentId, $aParams);
  505. }
  506. public function serviceHasImage ($iContentId)
  507. {
  508. return false;
  509. }
  510. public function serviceProfilePicture ($iContentId)
  511. {
  512. return $this->_serviceProfileFunc('getPicture', $iContentId);
  513. }
  514. public function serviceProfileAvatar ($iContentId)
  515. {
  516. return $this->_serviceProfileFunc('getAvatar', $iContentId);
  517. }
  518. public function serviceProfileAvatarBig ($iContentId)
  519. {
  520. return $this->_serviceProfileFunc('getAvatarBig', $iContentId);
  521. }
  522. public function serviceProfileCover ($iContentId)
  523. {
  524. return '';
  525. }
  526. public function serviceProfileEditUrl ($iContentId)
  527. {
  528. return bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=account-settings-info'));
  529. }
  530. public function serviceProfileThumb ($iContentId)
  531. {
  532. return $this->_serviceProfileFunc('getThumb', $iContentId);
  533. }
  534. public function serviceProfileIcon ($iContentId)
  535. {
  536. return $this->_serviceProfileFunc('getIcon', $iContentId);
  537. }
  538. public function serviceProfileName ($iContentId)
  539. {
  540. return $this->_serviceProfileFunc('getDisplayName', $iContentId);
  541. }
  542. public function serviceProfileUrl ($iContentId)
  543. {
  544. return $this->_serviceProfileFunc('getUrl', $iContentId);
  545. }
  546. /**
  547. * @see iBxDolProfileService::serviceCheckAllowedProfileView
  548. */
  549. public function serviceCheckAllowedProfileView($iContentId)
  550. {
  551. return _t('_Access denied');
  552. }
  553. /**
  554. * @see iBxDolProfileService::serviceCheckAllowedProfileContact
  555. */
  556. public function serviceCheckAllowedProfileContact($iContentId)
  557. {
  558. return _t('_Access denied');
  559. }
  560. /**
  561. * @see iBxDolProfileService::serviceCheckAllowedPostInProfile
  562. */
  563. public function serviceCheckAllowedPostInProfile($iContentId, $sPostModule = '')
  564. {
  565. return _t('_Access denied');
  566. }
  567. /**
  568. * @see iBxDolProfileService::serviceCheckAllowedModuleActionInProfile
  569. */
  570. public function serviceCheckAllowedModuleActionInProfile($iContentId, $sPostModule, $sActionName)
  571. {
  572. return _t('_Access denied');
  573. }
  574. /**
  575. * @see iBxDolProfileService::serviceGetSpaceTitle
  576. */
  577. public function serviceGetSpaceTitle()
  578. {
  579. return '';
  580. }
  581. /**
  582. * @see iBxDolProfileService::serviceGetParticipatingProfiles
  583. */
  584. public function serviceGetParticipatingProfiles($iProfileId, $aConnectionObject = false)
  585. {
  586. return array();
  587. }
  588. /**
  589. * @page service Service Calls
  590. * @section bx_system_general System Services
  591. * @subsection bx_system_general-general General
  592. * @subsubsection bx_system_general-get_create_post_form get_create_post_form
  593. *
  594. * @code bx_srv('system', 'get_create_post_form', [false, "bx_posts"], 'TemplServices'); @endcode
  595. * @code {{~system:get_create_post_form:TemplServices[false,"bx_posts"]~}} @endcode
  596. *
  597. * Get United Create Post form.
  598. * @param $mixedContextId - context which the post will be created in:
  599. * - false = 'Public' post form;
  600. * - 0 = 'Profile' post form, which allows to post in your own profile and connections;
  601. * - n = 'Context' post form, which allows to post in context (3d party profile, group, event, etc).
  602. * @param $sDefault - tab selected by default.
  603. * @param $aCustom - an array with custom paramaters.
  604. * @return string with form content
  605. *
  606. * @see BxBaseServices::serviceGetCreatePostForm
  607. */
  608. /**
  609. * @ref bx_system_general-get_create_post_form "get_create_post_form"
  610. */
  611. public function serviceGetCreatePostForm($mixedContextId = false, $sDefault = '', $aCustom = array())
  612. {
  613. if(!isLogged() || ($mixedContextId !== false && !is_numeric($mixedContextId)))
  614. return '';
  615. $oProfile = BxDolProfile::getInstance();
  616. if(!$oProfile)
  617. return '';
  618. if($mixedContextId !== false)
  619. $mixedContextId = (int)(!empty($mixedContextId) ? -$mixedContextId : $oProfile->id());
  620. $oMenu = BxDolMenu::getObjectInstance('sys_create_post');
  621. if(!$oMenu)
  622. return '';
  623. $oMenu->setContextId($mixedContextId);
  624. $aMenuItems = $oMenu->getMenuItems();
  625. if(empty($aMenuItems) || !is_array($aMenuItems))
  626. return '';
  627. if(empty($sDefault)) {
  628. $aDefault = array_shift($aMenuItems);
  629. $sDefault = !empty($aDefault['module']) ? $aDefault['module'] : $aDefault['name'];
  630. }
  631. $oMenu->setSelected($sDefault, $sDefault);
  632. $oContext = false;
  633. $bContext = $mixedContextId !== false;
  634. if($bContext && ($oContext = BxDolProfile::getInstance(abs($mixedContextId))) !== false)
  635. if(bx_srv($oContext->getModule(), 'check_allowed_post_in_profile', [$oContext->getContentId(), $sDefault]) !== CHECK_ACTION_RESULT_ALLOWED)
  636. return '';
  637. $sTitle = '_sys_page_block_title_create_post';
  638. if(!$bContext)
  639. $sTitle = _t($sTitle . '_public');
  640. else if($mixedContextId < 0) {
  641. $sTitle = _t($sTitle . '_context');
  642. if($oContext)
  643. $sTitle = bx_replace_markers($sTitle, [
  644. 'display_name' => $oContext->getDisplayName()
  645. ]);
  646. }
  647. else
  648. $sTitle = _t($sTitle);
  649. $sPlaceholder = _t('_sys_txt_create_post_placeholder', $oProfile->getDisplayName());
  650. $oDbModules = BxDolModuleQuery::getInstance();
  651. $oTemplate = BxDolTemplate::getInstance();
  652. $oTemplate->addJs(['BxDolCreatePost.js']);
  653. $aPreloadingList = [];
  654. $aModules = explode(',', getParam('sys_create_post_form_preloading_list'));
  655. if(!empty($aModules))
  656. foreach($aModules as $sModule) {
  657. $aModule = $oDbModules->getModuleByName($sModule);
  658. if(empty($aModule) || !is_array($aModule))
  659. continue;
  660. $aPreloadingList[$sModule] = $aModule['uri'];
  661. }
  662. if(bx_is_api()) {
  663. return [
  664. 'title' => $sTitle,
  665. 'content' => [
  666. bx_api_get_block('get_create_post_form', [
  667. 'menu' => $oMenu->getCodeAPI(),
  668. 'params' => ['context_id' => $mixedContextId]])
  669. ]
  670. ];
  671. }
  672. $sJsObject = 'oBxDolCreatePost';
  673. $sJsContent = $oTemplate->_wrapInTagJsCode("var " . $sJsObject . " = new BxDolCreatePost(" . json_encode([
  674. 'sObjName' => $sJsObject,
  675. 'sRootUrl' => BX_DOL_URL_ROOT,
  676. 'sDefault' => $sDefault,
  677. 'iContextId' => $bContext ? $mixedContextId : 0,
  678. 'oPreloadingList' => $aPreloadingList,
  679. 'oCustom' => $aCustom
  680. ]) . ");");
  681. return [
  682. 'title' => $sTitle,
  683. 'content' => $oTemplate->parseHtmlByName('create_post_form.html', [
  684. 'default' => $sDefault,
  685. 'placeholder' => $sPlaceholder,
  686. 'user_thumb' => $oProfile->getUnit(0, ['template' => 'unit_wo_info_links']),
  687. 'form' => bx_srv($sDefault, 'get_create_post_form', [[
  688. 'context_id' => $mixedContextId,
  689. 'ajax_mode' => true,
  690. 'absolute_action_url' => true,
  691. 'custom' => $aCustom
  692. ]]),
  693. 'js_object' => $sJsObject,
  694. 'js_content' => $sJsContent
  695. ]),
  696. 'menu' => $oMenu->getMenuItemsCount() > 1 ? $oMenu : ''
  697. ];
  698. }
  699. public function serviceGetOptionsCreatePostFormPreloadingList()
  700. {
  701. $oMenu = BxDolMenu::getObjectInstance('sys_create_post');
  702. if(!$oMenu)
  703. return [];
  704. $aMenuItems = $oMenu->getMenuItems();
  705. $aResult = [];
  706. foreach($aMenuItems as $aMenuItem) {
  707. $sModule = !empty($aMenuItem['module']) ? $aMenuItem['module'] : $aMenuItem['name'];
  708. if(!bx_srv('system', 'is_module_enabled', [$sModule]))
  709. continue;
  710. $aResult[] = [
  711. 'key' => $sModule,
  712. 'value' => _t('_' . $sModule)
  713. ];
  714. }
  715. return $aResult;
  716. }
  717. public function serviceGetBlockStatic($sBlockName)
  718. {
  719. $bIsApi = bx_is_api();
  720. if(!$bIsApi)
  721. return '';
  722. return [bx_api_get_block('static', [
  723. 'element' => $sBlockName
  724. ])];
  725. }
  726. public function serviceGetBlockAuthor($sModule, $iContentId = 0)
  727. {
  728. if(!$iContentId && bx_get('id') !== false)
  729. $iContentId = (int)bx_get('id');
  730. $sMethodGetAuthor = 'get_author';
  731. if(!$sModule || !$iContentId || !BxDolRequest::serviceExists($sModule, $sMethodGetAuthor))
  732. return '';
  733. $iAuthor = bx_srv($sModule, $sMethodGetAuthor, [$iContentId]);
  734. if(!$iAuthor)
  735. return '';
  736. $oAuthor = BxDolProfile::getInstance($iAuthor);
  737. if(!$oAuthor)
  738. return '';
  739. $sModuleAuthor = $oAuthor->getModule();
  740. $sMethodGetCover = 'entity_cover';
  741. if(!BxDolRequest::serviceExists($sModuleAuthor, $sMethodGetCover))
  742. return '';
  743. return bx_srv($sModuleAuthor, $sMethodGetCover, [$oAuthor->getContentId(), [
  744. 'use_as_author' => true,
  745. 'show_text' => false
  746. ]]);
  747. }
  748. public function serviceGetBlockAiAgent($iId = 0, $mixedContextPid = false, $bRequirePost = false)
  749. {
  750. $bIsApi = bx_is_api();
  751. $aAgent = BxDolAiQuery::getAgentObject($iId);
  752. if(!$aAgent || !is_array($aAgent))
  753. return $bIsApi ? [] : '';
  754. $oAi = BxDolAI::getInstance();
  755. $bRequirePost = (bool)$bRequirePost;
  756. if ($mixedContextPid === false || $mixedContextPid === null || $mixedContextPid === '')
  757. $iContextPid = $oAi ? (int)$oAi->resolveChatHistoryContextPidFromPage($bRequirePost) : 0;
  758. else
  759. $iContextPid = $oAi ? $oAi->filterContextPid((int)$mixedContextPid, $bRequirePost) : 0;
  760. if ($bIsApi)
  761. return [bx_api_get_block('ai_agent', [
  762. 'agent_id' => $iId,
  763. 'agent_profile' => BxDolProfile::getData($aAgent['profile_id']),
  764. 'context_profile_id' => $iContextPid,
  765. 'hidden_first_message' => (string)($aAgent['hidden_first_message'] ?? ''),
  766. ])];
  767. $sEndpoint = BX_DOL_URL_ROOT . 'sys-ai-chat.php?id=' . (int)$iId;
  768. if ($iContextPid > 0)
  769. $sEndpoint .= '&context=' . $iContextPid;
  770. $oTemplate = BxDolTemplate::getInstance();
  771. $oTemplate->addJs([BX_DOL_URL_ROOT . 'plugins_public/bundle.js']);
  772. return $oTemplate->parseHtmlByName('agent_ai_chat.html', [
  773. 'id' => (int)$iId,
  774. 'context_profile_id' => (int)$iContextPid,
  775. 'endpoint_js' => json_encode($sEndpoint),
  776. 'bundle_url' => BX_DOL_URL_ROOT . 'plugins_public/bundle.js',
  777. ]);
  778. }
  779. public function serviceGetBlockAskAssistant($aParams = [])
  780. {
  781. $iAssistant = BxDolAI::getAssistantForAskBlock();
  782. if(!$iAssistant)
  783. return '';
  784. return BxDolAIAssistant::getObjectInstance($iAssistant)->getAskBlock($aParams);
  785. }
  786. public function serviceGetMenu($aParams)
  787. {
  788. $bIsApi = bx_is_api();
  789. $mixedResulEmpty = $bIsApi ? [] : '';
  790. if($bIsApi && is_string($aParams))
  791. $aParams = bx_api_get_browse_params($aParams);
  792. if(!isset($aParams['object']))
  793. return $mixedResulEmpty;
  794. $oMenu = BxDolMenu::getObjectInstance($aParams['object']);
  795. if(!$oMenu)
  796. return $mixedResulEmpty;
  797. if(!empty($aParams['params']) && is_array($aParams['params']))
  798. $oMenu->setContentParams($aParams['params']);
  799. return $bIsApi ? $oMenu->getCodeAPI() : $oMenu->getCode();
  800. }
  801. /**
  802. * @page service Service Calls
  803. * @section bx_system_general System Services
  804. * @subsection bx_system_general-general General
  805. * @subsubsection bx_system_general-get_site_submenu get_site_submenu
  806. *
  807. * @code bx_srv('system', 'get_site_submenu', [], 'TemplServices'); @endcode
  808. * @code {{~system:get_site_submenu:TemplServices[]~}} @endcode
  809. *
  810. * Get Site Submenu code.
  811. * @see BxBaseServices::serviceGetSiteSubmenu
  812. */
  813. /**
  814. * @ref bx_system_general-get_site_submenu "get_site_submenu"
  815. */
  816. public function serviceGetSiteSubmenu()
  817. {
  818. $oMenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  819. return $oMenu ? $oMenu->getCode() : '';
  820. }
  821. /**
  822. * @page service Service Calls
  823. * @section bx_system_general System Services
  824. * @subsection bx_system_general-general General
  825. * @subsubsection bx_system_general-get_products_names get_products_names
  826. *
  827. * @code bx_srv('system', 'get_products_names', [], 'TemplServices');
  828. *
  829. * @code http://example.com/m/oauth2/com/get_products_names?module=system&class=BaseServices @endcode
  830. * @code http://hihi.una.io/modules/?r=oauth2/com/get_products_names&module=system&class=BaseServices @endcode
  831. *
  832. * Get an array of products names from all modules with payments functionality.
  833. * @param $iVendorId filter products by vendor ID.
  834. * @param $iLimit limit number of records from one module.
  835. * @return array of products where key id product name and value is module name
  836. *
  837. * @see BxBaseServices::serviceGetProductsNames
  838. */
  839. /**
  840. * @ref bx_system_general-get_products_names "get_products_names"
  841. */
  842. public function serviceGetProductsNames($iVendorId = 0, $iLimit = 1000)
  843. {
  844. $o = BxDolPayments::getInstance();
  845. if (!$o)
  846. return array();
  847. return $o->getProductsNames($iVendorId, $iLimit);
  848. }
  849. /**
  850. * @see iBxDolProfileService::serviceCheckSpacePrivacy
  851. */
  852. public function serviceCheckSpacePrivacy($iContentId)
  853. {
  854. return _t('_Access denied');
  855. }
  856. public function serviceFormsHelper ()
  857. {
  858. return new BxTemplAccountForms();
  859. }
  860. public function serviceActAsProfile ()
  861. {
  862. return false;
  863. }
  864. public function serviceGetModerators($iContentId)
  865. {
  866. $aModerators = BxDolAclQuery::getInstance()->getProfilesByMembership([
  867. MEMBERSHIP_ID_MODERATOR,
  868. MEMBERSHIP_ID_ADMINISTRATOR
  869. ]);
  870. $aIds = [];
  871. array_walk($aModerators, function ($aItem, $iKey, $aParams) {
  872. if(!empty($aItem['id']))
  873. $aParams[0][] = (int)$aItem['id'];
  874. }, [&$aIds]);
  875. return $aIds;
  876. }
  877. public function servicePrepareFields ($aFieldsProfile)
  878. {
  879. return $aFieldsProfile;
  880. }
  881. public function serviceProfilesSearch ($sTerm, $iLimit)
  882. {
  883. $oDb = BxDolAccountQuery::getInstance();
  884. $aRet = array();
  885. $a = $oDb->searchByTerm($sTerm, $iLimit);
  886. foreach ($a as $r)
  887. $aRet[] = array ('label' => $this->serviceProfileName($r['content_id']), 'value' => $r['profile_id']);
  888. return $aRet;
  889. }
  890. /**
  891. * @page service Service Calls
  892. * @section bx_system_general System Services
  893. * @subsection bx_system_general-general General
  894. * @subsubsection bx_system_general-keyword_search keyword_search
  895. *
  896. * @code bx_srv('system', 'keyword_search', ["bx_posts", ["keyword" => "test"}], 'TemplServices'); @endcode
  897. *
  898. * @code {{~system:keyword_search:TemplServices["bx_posts", {"keyword":"test"}]~}} @endcode
  899. * @code {{~system:keyword_search:TemplServices["bx_albums", {"meta_type": "location_country", "keyword": "AU"}, "unit.html"]~}} @endcode
  900. * @code {{~system:keyword_search:TemplServices["bx_albums", {"meta_type": "location_country_state", "state":"NSW", "keyword": "AU"}, "unit.html"]~}} @endcode
  901. * @code {{~system:keyword_search:TemplServices["bx_albums", {"meta_type": "location_country_city", "state":"NSW", "city":"Manly", "keyword": "AU"}, "unit.html"]~}} @endcode
  902. * @code {{~system:keyword_search:TemplServices["bx_posts", {"meta_type": "mention", "keyword": 2}, "unit_gallery.html"]~}} @endcode
  903. * @code {{~system:keyword_search:TemplServices["bx_posts", {"cat": "bx_posts_cats", "keyword": 3}, "unit_gallery.html"]~}} @endcode
  904. *
  905. * Search by keyword
  906. * @param $sSection - search object to search in, usually module name, for example: bx_posts
  907. * @param $aCondition - condition for search, supported conditions:
  908. * - search by keyword: ["keyword" => "test"]
  909. * - search by country: ["meta_type" => "location_country", "keyword" => "AU"]
  910. * - search by country and state: ["meta_type": "location_country_state", "state":"NSW", "keyword": "AU"]
  911. * - search by country, state and city: ["meta_type": "location_country_city", "state":"NSW", "city":"Manly", "keyword": "AU"]
  912. * - search for mentions: ["meta_type" => "mention", "keyword" => 2]
  913. * - search in category: ["cat": "bx_posts_cats", "keyword": 3]
  914. * @param $sTemplate - template for displaying search results, for example: unit.html
  915. * @param $iStart - paginate, display records starting from this number
  916. * @param $iPerPage - paginate, display this number of records per page
  917. * @param $bLiveSearch - search results like in live search
  918. *
  919. * @see BxBaseServices::serviceKeywordSearch
  920. */
  921. /**
  922. * @ref bx_system_general-keyword_search "keyword_search"
  923. */
  924. public function serviceKeywordSearch ($sSection, $aCondition, $sTemplate = '', $iStart = 0, $iPerPage = 0, $bLiveSearch = 0, $bPaginate = false)
  925. {
  926. if (!$sSection || !isset($aCondition['keyword']))
  927. return '';
  928. $sClass = 'BxTemplSearch';
  929. $sElsName = 'bx_elasticsearch';
  930. $sElsMethod = 'is_configured';
  931. if(BxDolRequest::serviceExists($sElsName, $sElsMethod) && BxDolService::call($sElsName, $sElsMethod)) {
  932. $oModule = BxDolModule::getInstance($sElsName);
  933. bx_import('Search', $oModule->_aModule);
  934. $sClass = 'BxElsSearch';
  935. }
  936. $oSearch = new $sClass(array($sSection));
  937. $oSearch->setLiveSearch($bLiveSearch);
  938. $oSearch->setMetaType(isset($aCondition['meta_type']) ? $aCondition['meta_type'] : '');
  939. $oSearch->setCategoryObject(isset($aCondition['cat']) ? $aCondition['cat'] : '');
  940. $oSearch->setCustomSearchCondition($aCondition);
  941. if (!$bPaginate)
  942. $oSearch->setRawProcessing(true);
  943. $oSearch->setCustomCurrentCondition(array(
  944. 'paginate' => array (
  945. 'start' => $iStart,
  946. 'perPage' => $iPerPage ? $iPerPage : BX_DOL_SEARCH_RESULTS_PER_PAGE_DEFAULT,
  947. )));
  948. if ($sTemplate)
  949. $oSearch->setUnitTemplate($sTemplate);
  950. return $oSearch->response();
  951. }
  952. /**
  953. * @page service Service Calls
  954. * @section bx_system_general System Services
  955. * @subsection bx_system_general-general General
  956. * @subsubsection bx_system_general-keyword_search keyword_search
  957. *
  958. * @code bx_srv('system', 'search_keyword_form', 'TemplServices'); @endcode
  959. *
  960. * Block with Search by Keywords Form
  961. *
  962. * @see BxBaseServices::serviceSearchKeywordForm
  963. */
  964. /**
  965. * @ref bx_system_general-keyword_search "keyword_search"
  966. */
  967. public function serviceSearchKeywordForm ()
  968. {
  969. return $this->_getSearchObject()->getForm(BX_DB_PADDING_DEF, false, true);
  970. }
  971. /**
  972. * @page service Service Calls
  973. * @section bx_system_general System Services
  974. * @subsection bx_system_general-general General
  975. * @subsubsection bx_system_general-keyword_search keyword_search
  976. *
  977. * @code bx_srv('system', 'search_keyword_result', 'TemplServices'); @endcode
  978. *
  979. * Block with Search by Keywords Results
  980. *
  981. * @see BxBaseServices::serviceSearchKeywordResult
  982. */
  983. /**
  984. * @ref bx_system_general-keyword_search "keyword_search"
  985. */
  986. public function serviceSearchKeywordResult ()
  987. {
  988. $sType = bx_process_input(bx_get('type'));
  989. $sKeyword = bx_process_input(bx_get('keyword'));
  990. $bKeyword = $sKeyword !== false;
  991. if(bx_is_api())
  992. return $this->serviceGetDataSearchApi(['params' => [
  993. 'type' => $sType,
  994. 'keyword' => $sKeyword,
  995. 'section' => bx_process_input(bx_get('section')),
  996. 'cat' => bx_process_input(bx_get('cat'))
  997. ]]);
  998. $sCode = '';
  999. if($bKeyword) {
  1000. $oSearch = $this->_getSearchObject();
  1001. if(($sCode = $oSearch->response()))
  1002. $sCode = BxDolTemplate::getInstance()->parseHtmlByName('search_result_block.html', [
  1003. 'html_id' => 'bx-search-results-keyword',
  1004. 'class' => 'bx-search-results-container',
  1005. 'attrs' => '',
  1006. 'content' => $sCode,
  1007. 'bx_if:do_center' => [
  1008. 'condition' => false,
  1009. 'content' => [
  1010. 'html_id' => '',
  1011. 'selector_content' => ''
  1012. ]
  1013. ]
  1014. ]);
  1015. else
  1016. $sCode = $oSearch->getEmptyResult();
  1017. }
  1018. return $sCode;
  1019. }
  1020. /**
  1021. * @ref bx_system_general-get_date_search_api "get_date_search_api"
  1022. * @api @ref bx_system_general-get_date_search_api "get_date_search_api"
  1023. */
  1024. public function serviceGetDataSearchApi ($aParams)
  1025. {
  1026. if(!bx_is_api())
  1027. return false;
  1028. if(is_string($aParams))
  1029. $aParams = bx_api_get_browse_params($aParams);
  1030. $bForceAll = ($aParams['params']['type'] ?? '') == 'keyword';
  1031. $aSectionsAvail = explode(',', getParam('sys_api_search_sections'));
  1032. $aSectionsAll = BxDolDb::getInstance()->fromCache(
  1033. 'sys_global_search_pairs',
  1034. 'getPairs',
  1035. 'SELECT `ObjectName` AS `name`, `Title` AS `title` FROM `sys_objects_search` WHERE `GlobalSearch`=\'1\' ORDER BY `Order` ASC',
  1036. 'name', 'title'
  1037. );
  1038. $aSections = [];
  1039. foreach($aSectionsAll as $sSectionName => $sSectionTitle)
  1040. if(in_array($sSectionName, $aSectionsAvail) || $bForceAll)
  1041. $aSections[$sSectionName] = [
  1042. 'name' => $sSectionName,
  1043. 'title' => _t($sSectionTitle)
  1044. ];
  1045. $aParamsBrowse = array_merge([
  1046. 'keyword' => '',
  1047. 'section' => '',
  1048. 'sections' => array_values($aSections),
  1049. 'start' => 0,
  1050. 'per_page' => 12
  1051. ], !empty($aParams['params']) && is_array($aParams['params']) ? $aParams['params'] : []);
  1052. if(empty($aParamsBrowse['section']))
  1053. $aParamsBrowse['section'] = array_keys($aSections);
  1054. else if(is_string($aParamsBrowse['section']))
  1055. $aParamsBrowse['section'] = explode(',', $aParamsBrowse['section']);
  1056. $sClass = 'BxTemplSearch';
  1057. $sSections = $aParamsBrowse['section'];
  1058. if ($aParamsBrowse['live'] !== true && count($sSections) > 1){
  1059. $aParamsBrowse['section'] = [];
  1060. $aParamsBrowse['sections'] = [];
  1061. $aDataRv = [];
  1062. foreach ($sSections as $sSection) {
  1063. $oSearch = new $sClass($sSection);
  1064. $oSearch->setLiveSearch(true);
  1065. $oSearch->setDataProcessing(true);
  1066. $oSearch->setCustomSearchCondition(['keyword' => $aParamsBrowse['keyword']]);
  1067. $oSearch->setCustomCurrentCondition([
  1068. 'paginate' => [
  1069. 'forceStart' => $aParamsBrowse['start'],
  1070. 'perPage' => $aParamsBrowse['per_page'],
  1071. ]
  1072. ]);
  1073. $aData = $oSearch->response();
  1074. if(count($aData) > $aParamsBrowse['per_page'])
  1075. $aData = array_slice($aData, $aParamsBrowse['start'], $aParamsBrowse['per_page']);
  1076. if (count($aData) > 0) {
  1077. $oSearchResult = $oSearch->getSearchResultObject($sSection);
  1078. $sSectionTitle = $oSearchResult->aCurrent['title'];
  1079. $aParamsBrowse['section'][] = $sSection;
  1080. $aParamsBrowse['sections'][] = ['name' => $sSection, 'title' => $sSectionTitle];
  1081. $aDataRv[] = [
  1082. 'section' => $sSection,
  1083. 'section_name' => $sSectionTitle,
  1084. 'data' => $aData,
  1085. 'is_profile' => bx_srv('system', 'is_module_profile', [$oSearchResult->aCurrent['module_name']])
  1086. ];
  1087. }
  1088. }
  1089. return [bx_api_get_block('search_sections', [
  1090. 'data' => $aDataRv,
  1091. 'params' => $aParamsBrowse
  1092. ])];
  1093. }
  1094. else{
  1095. $oSearch = new $sClass($aParamsBrowse['section']);
  1096. $oSearch->setLiveSearch(true);
  1097. $oSearch->setDataProcessing(true);
  1098. $oSearch->setCustomSearchCondition(['keyword' => $aParamsBrowse['keyword']]);
  1099. $oSearch->setCustomCurrentCondition([
  1100. 'paginate' => [
  1101. 'forceStart' => $aParamsBrowse['start'],
  1102. 'perPage' => $aParamsBrowse['per_page'],
  1103. ]
  1104. ]);
  1105. $aData = $oSearch->response();
  1106. if(count($aData) > $aParamsBrowse['per_page'])
  1107. $aData = array_slice($aData, $aParamsBrowse['start'], $aParamsBrowse['per_page']);
  1108. return [
  1109. bx_api_get_block('browse', [
  1110. 'unit' => 'search-results',
  1111. 'request_url' => '/api.php?r=system/get_data_search_api/TemplServices&params[]=',
  1112. 'params' => $aParamsBrowse,
  1113. 'data' => $aData
  1114. ])
  1115. ];
  1116. }
  1117. }
  1118. /**
  1119. * @page service Service Calls
  1120. * @section bx_system_general System Services
  1121. * @subsection bx_system_general-general General
  1122. * @subsubsection bx_system_general-cmts cmts
  1123. *
  1124. * @code bx_srv('system', 'cmts', ["sys_blocks", 1], 'TemplServices'); @endcode
  1125. *
  1126. * @code {{~system:cmts:TemplServices["sys_blocks", 1]~}} @endcode
  1127. *
  1128. * Comments block
  1129. * @param $sObject - comments object name
  1130. * @param $sId - content id assiciated tith the comments
  1131. *
  1132. * @see BxBaseServices::serviceCmts
  1133. */
  1134. /**
  1135. * @ref bx_system_general-cmts "cmts"
  1136. */
  1137. public function serviceCmts ($sObject, $sId)
  1138. {
  1139. $o = BxDolCmts::getObjectInstance($sObject, $sId);
  1140. if (!$o || !$o->isEnabled())
  1141. return '';
  1142. return $o->getCommentsBlock(array(), array('in_designbox' => false, 'show_empty' => true));
  1143. }
  1144. public function _serviceProfileFunc ($sFunc, $iContentId, $aParams = array())
  1145. {
  1146. if (!$iContentId)
  1147. return false;
  1148. if (!($oAccount = BxDolAccount::getInstance($iContentId)))
  1149. return false;
  1150. return $oAccount->$sFunc(false, $aParams);
  1151. }
  1152. public function serviceAlertResponseProcessInstalled()
  1153. {
  1154. BxDolTranscoderImage::registerHandlersSystem();
  1155. }
  1156. public function serviceAlertResponseProcessStorageChange ($oAlert)
  1157. {
  1158. if ('sys_storage_default' != $oAlert->aExtras['option'])
  1159. return;
  1160. $aStorages = BxDolStorageQuery::getStorageObjects();
  1161. foreach ($aStorages as $r) {
  1162. if (0 == $r['current_size'] && 0 == $r['current_number'] && ($oStorage = BxDolStorage::getObjectInstance($r['object'])))
  1163. $oStorage->changeStorageEngine($oAlert->aExtras['value']);
  1164. }
  1165. }
  1166. public function serviceGetOptionsProfileBot()
  1167. {
  1168. $aResult = array(
  1169. array('key' => '', 'value' => _t('_Select_one'))
  1170. );
  1171. $aAccountsIds = BxDolAccountQuery::getInstance()->getOperators();
  1172. foreach($aAccountsIds as $iAccountId) {
  1173. $aProfilesIds = BxDolAccount::getInstance($iAccountId)->getProfilesIds(true, false);
  1174. foreach($aProfilesIds as $iProfileId) {
  1175. $oProfile = BxDolProfile::getInstance($iProfileId);
  1176. $aResult[] = array(
  1177. 'key' => $iProfileId,
  1178. 'value' => _t('_sys_profile_with_type', $oProfile->getDisplayName(), $oProfile->getModule()),
  1179. );
  1180. }
  1181. }
  1182. return $aResult;
  1183. }
  1184. public function serviceGetOptionsModuleListForPrivacySelector()
  1185. {
  1186. $aResult = array();
  1187. $BxDolModuleQuery = BxDolModuleQuery::getInstance();
  1188. $aModules = $BxDolModuleQuery->getModulesBy(array('type' => 'modules', 'active' => 1));
  1189. foreach($aModules as $aModule){
  1190. $oModule = BxDolModule::getInstance($aModule['name']);
  1191. if($oModule instanceof iBxDolContentInfoService){
  1192. if (!BxDolRequest::serviceExists($aModule['name'], 'act_as_profile'))
  1193. continue;
  1194. $aResult[$aModule['name']] = $aModule['title'];
  1195. }
  1196. }
  1197. return $aResult;
  1198. }
  1199. public function serviceGetOptionsCaptchaDefault()
  1200. {
  1201. $aResults = [];
  1202. $aObjects = BxDolCaptchaQuery::getObjects();
  1203. foreach($aObjects as $aObject)
  1204. $aResults[$aObject['object']] = $aObject['title'];
  1205. return $aResults;
  1206. }
  1207. public function serviceGetOptionsEmbedDefault()
  1208. {
  1209. $aResults = array(
  1210. '' => _t('_None')
  1211. );
  1212. $aObjects = BxDolEmbedQuery::getObjects();
  1213. foreach($aObjects as $aObject)
  1214. $aResults[$aObject['object']] = _t($aObject['title']);
  1215. return $aResults;
  1216. }
  1217. public function serviceGetOptionsLocationFieldDefault()
  1218. {
  1219. $aResults = array();
  1220. $aObjects = BxDolLocationFieldQuery::getObjects();
  1221. foreach($aObjects as $aObject)
  1222. $aResults[$aObject['object']] = _t($aObject['title']);
  1223. return $aResults;
  1224. }
  1225. public function serviceGetOptionsLocationMapDefault()
  1226. {
  1227. $aResults = array();
  1228. $aObjects = BxDolLocationMapQuery::getObjects();
  1229. foreach($aObjects as $aObject)
  1230. $aResults[$aObject['object']] = _t($aObject['title']);
  1231. return $aResults;
  1232. }
  1233. public function serviceGetOptionsPushDefault()
  1234. {
  1235. $aResults = [
  1236. '' => _t('_None')
  1237. ];
  1238. $aObjects = BxDolPushQuery::getObjects();
  1239. foreach($aObjects as $aObject)
  1240. $aResults[$aObject['object']] = $aObject['title'];
  1241. return $aResults;
  1242. }
  1243. public function serviceGetOptionsSmsDefault()
  1244. {
  1245. $aResults = [
  1246. '' => _t('_None')
  1247. ];
  1248. $aObjects = BxDolSmsQuery::getObjects();
  1249. foreach($aObjects as $aObject)
  1250. $aResults[$aObject['object']] = $aObject['title'];
  1251. return $aResults;
  1252. }
  1253. public function serviceGetOptionsSocketsFieldDefault()
  1254. {
  1255. $aResults = [];
  1256. $aObjects = ['sys_sockets_disabled', 'sys_sockets_soketi'];
  1257. foreach($aObjects as $sObject)
  1258. $aResults[$sObject] = _t('_adm_stg_cpt_option_sys_sockets_type_' . $sObject);
  1259. return $aResults;
  1260. }
  1261. public function serviceGetOptionsLocationLeafletGetProviders()
  1262. {
  1263. return BxDolLocationMapLeaflet::getProviders();
  1264. }
  1265. public function serviceGetOptionsRelations()
  1266. {
  1267. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  1268. $aProfiles = array();
  1269. foreach($aModules as $aModule) {
  1270. $sMethod = 'act_as_profile';
  1271. if(!BxDolRequest::serviceExists($aModule['name'], $sMethod) || !BxDolService::call($aModule['name'], $sMethod))
  1272. continue;
  1273. $aProfiles[$aModule['name']] = _t('_' . $aModule['name']);
  1274. }
  1275. $aResults = array();
  1276. foreach($aProfiles as $sName1 => $sTitle1)
  1277. foreach($aProfiles as $sName2 => $sTitle2)
  1278. $aResults[$sName1 . '_' . $sName2] = $sTitle1 . ' - ' . $sTitle2;
  1279. return $aResults;
  1280. }
  1281. public function serviceGetOptionsCfProhibited()
  1282. {
  1283. return BxDolFormQuery::getDataItems('sys_content_filter');
  1284. }
  1285. public function serviceGetOptionsCfUnauthenticated()
  1286. {
  1287. return BxDolFormQuery::getDataItems('sys_content_filter');
  1288. }
  1289. public function serviceGetOptionsTaiwindDefault()
  1290. {
  1291. $sPath = BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'tailwind/css';
  1292. $aExcludes = ['.', '..', 'tailwind.css'];
  1293. $aResults = [];
  1294. if(($oHandle = opendir($sPath)) !== false) {
  1295. while(($sFile = readdir($oHandle)) !== false) {
  1296. if(in_array($sFile, $aExcludes))
  1297. continue;
  1298. if(($oHandlerFile = fopen($sPath . '/' . $sFile, 'r')) === false)
  1299. continue;
  1300. if(($sContent = fread($oHandlerFile, 1024)) !== false && strpos($sContent, '@tailwind base') === false)
  1301. $aResults[$sFile] = $sFile;
  1302. fclose($oHandlerFile);
  1303. }
  1304. closedir($oHandle);
  1305. }
  1306. /**
  1307. * Disabled for now, because CDN and custom compiled build with own styles
  1308. * don't work correctly together.
  1309. *
  1310. * $aResults['cdn'] = _t('_adm_stg_cpt_option_sys_css_tailwind_default_cdn');
  1311. */
  1312. return $aResults;
  1313. }
  1314. public function serviceGetOptionsIconsetDefault()
  1315. {
  1316. $aObjects = BxDolIconsetQuery::getObjects();
  1317. foreach($aObjects as $aObject)
  1318. $aResults[$aObject['object']] = $aObject['title'];
  1319. return $aResults;
  1320. }
  1321. public function serviceGetOptionsApiRootPageGuest()
  1322. {
  1323. $sKey = '_adm_stg_cpt_option_sys_api_root_page_';
  1324. return [
  1325. 'home' => _t($sKey .'home'),
  1326. 'splash' => _t($sKey . 'splash')
  1327. ];
  1328. }
  1329. public function serviceGetOptionsApiRootPageMember()
  1330. {
  1331. $sKey = '_adm_stg_cpt_option_sys_api_root_page_';
  1332. return [
  1333. 'home' => _t($sKey .'home'),
  1334. 'dashboard' => _t($sKey . 'dashboard'),
  1335. 'profile' => _t($sKey . 'profile')
  1336. ];
  1337. }
  1338. public function serviceGetOptionsApiMenuTop()
  1339. {
  1340. $aMenus = BxDolMenuQuery::getMenuObjects();
  1341. $aResults = ['' => _t('_sys_please_select')];
  1342. foreach($aMenus as $aMenu)
  1343. if($aMenu['module'] == 'system')
  1344. $aResults[$aMenu['object']] = _t($aMenu['title']);
  1345. return $aResults;
  1346. }
  1347. public function serviceGetOptionsApiContextSwitcher()
  1348. {
  1349. $aContexts = bx_srv('system', 'get_modules_by_type', ['context']);
  1350. $aResults = ['' => _t('_None')];
  1351. foreach($aContexts as $aContext)
  1352. $aResults[$aContext['name']] = _t('_' . $aContext['name']);
  1353. return $aResults;
  1354. }
  1355. public function serviceGetOptionsApiContextConnection()
  1356. {
  1357. $aConnections = ['subscriptions', 'fans', 'participants'];
  1358. $aResults = ['' => _t('_sys_please_select')];
  1359. foreach($aConnections as $sConnection)
  1360. $aResults[$sConnection] = _t('_adm_stg_cpt_option_sys_api_cnt_cnn_' . $sConnection);
  1361. return $aResults;
  1362. }
  1363. public function serviceGetOptionsAgentsModel()
  1364. {
  1365. return ['' => _t('_Select_one')] + BxDolAI::getInstance()->getModels(['active' => true, 'hidden' => false]);
  1366. }
  1367. public function serviceGetOptionsAgentsProfile($bSelectOne = true, $sSelectOneLangKey = '_Select_one')
  1368. {
  1369. $aResult = [];
  1370. if($bSelectOne)
  1371. $aResult[] = ['key' => '', 'value' => _t($sSelectOneLangKey)];
  1372. $aAccountsIds = BxDolAccountQuery::getInstance()->getOperators();
  1373. foreach($aAccountsIds as $iAccountId) {
  1374. $aProfilesIds = BxDolAccount::getInstance($iAccountId)->getProfilesIds(true, false);
  1375. foreach($aProfilesIds as $iProfileId) {
  1376. $oProfile = BxDolProfile::getInstance($iProfileId);
  1377. $sProfileModule = false === $oProfile ? '' : $oProfile->getModule();
  1378. if ($sProfileModule && ($sProfileModule != 'system' || ($sProfileModule == 'system' && empty($oProfile->getAccountObject()->getEmail())))) {
  1379. $aResult[] = [
  1380. 'key' => $iProfileId,
  1381. 'value' => _t('_sys_profile_with_type', $oProfile->getDisplayName(), $sProfileModule === 'system' ? 'System' : _t('_' . $sProfileModule))
  1382. ];
  1383. }
  1384. }
  1385. }
  1386. return $aResult;
  1387. }
  1388. public function serviceGetOptionsStudioAssistant()
  1389. {
  1390. return $this->_getOptionsAiAssistants();
  1391. }
  1392. public function serviceGetOptionsLiveSearchAssistant()
  1393. {
  1394. return $this->_getOptionsAiAssistants();
  1395. }
  1396. public function serviceGetOptionsAskBlockAssistant()
  1397. {
  1398. return $this->_getOptionsAiAssistants();
  1399. }
  1400. public function serviceRedirect($sUrl = false)
  1401. {
  1402. if (!getParam('sys_confirmation_before_redirect')) {
  1403. BxDolTemplate::getInstance()->displayPageNotFound();
  1404. exit;
  1405. }
  1406. if (false === $sUrl)
  1407. $sUrl = bx_get('url');
  1408. if (!$sUrl || !preg_match('@^https?://@', $sUrl)) {
  1409. http_response_code(500);
  1410. return MsgBox(_t('_error occured'));
  1411. }
  1412. else {
  1413. $bSpam = null;
  1414. /**
  1415. * @hooks
  1416. * @hookdef hook-system-check_spam_url 'system', 'check_spam_url' - hook to override sClass for search
  1417. * - $unit_name - equals `system`
  1418. * - $action - equals `check_spam_url`
  1419. * - $object_id - not used
  1420. * - $sender_id - logged account id
  1421. * - $extra_params - array of additional params with the following array keys:
  1422. * - `is_spam` - [bool] by ref, spam url or not, can be overridden in hook processing
  1423. * - `content` - [string] by ref, url of link, can be overridden in hook processing
  1424. * - `where` - [string] equal redirect
  1425. * @hook @ref hook-system-check_spam_url
  1426. */
  1427. bx_alert('system', 'check_spam_url', 0, getLoggedId(), array(
  1428. 'is_spam' => &$bSpam,
  1429. 'content' => &$sUrl,
  1430. 'is_spam_ref' => &$bSpam,
  1431. 'content_ref' => &$sUrl,
  1432. 'where' => 'redirect'
  1433. ));
  1434. $sLangKey = $bSpam ? '_sys_redirect_confirmation_harmful' : '_sys_redirect_confirmation';
  1435. return BxDolTemplate::getInstance()->parseHtmlByName('redirect.html', [
  1436. 'text' => _t($sLangKey, bx_process_output($sUrl), getParam('site_title')),
  1437. 'url' => bx_js_string($sUrl, BX_ESCAPE_STR_APOS),
  1438. ]);
  1439. }
  1440. }
  1441. public function serviceSetBadges($iContentId, $sBadgesIds, $sModule)
  1442. {
  1443. $aSelectedBadges = explode(",", $sBadgesIds);
  1444. $iPerformerId = bx_get_logged_profile_id();
  1445. $aCheck = checkActionModule($iPerformerId, 'set badge', 'system', false);
  1446. if(!isAdmin() && $aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  1447. return false;
  1448. $oBadges = BxDolBadges::getInstance();
  1449. $aBadges = $oBadges->getData(array('type' => 'by_module&object', 'object_id' => $iContentId, 'module' => $sModule));
  1450. foreach ($aBadges as $aBadge) {
  1451. if (isset($aBadge['badge_id'])){
  1452. $oBadges->delete($aBadge['badge_id']);
  1453. }
  1454. }
  1455. foreach ($aSelectedBadges as $iSelectedBadge) {
  1456. $oBadges->add($iSelectedBadge, $iContentId, $sModule);
  1457. }
  1458. checkActionModule($iPerformerId, 'set badge', 'system', true); // perform action
  1459. return true;
  1460. }
  1461. public function serviceGetBadge($aBadge, $bIsCompact = false)
  1462. {
  1463. $sClass = '';
  1464. $sStyleFont = '';
  1465. $sStyleBg = '';
  1466. if ($aBadge['color'] == '')
  1467. $aBadge['color'] = 'purple-600';
  1468. if ($bIsCompact && $aBadge['icon'] != ''){
  1469. $aBadge['is_icon_only'] = 1;
  1470. }
  1471. if ($aBadge['is_icon_only'] == 1){
  1472. $sClass .= ' bx-badge-compact';
  1473. if (substr_count($aBadge['color'], 'rgb('))
  1474. $sStyleFont = 'color: ' . $aBadge['color'];
  1475. else
  1476. $sClass .= ' text-' . $aBadge['color'];
  1477. }
  1478. else{
  1479. if (substr_count($aBadge['color'], 'rgb('))
  1480. $sStyleBg = 'background-color: ' . $aBadge['color'];
  1481. else
  1482. $sClass .= ' bg-' . $aBadge['color'];
  1483. }
  1484. return BxDolTemplate::getInstance()->parseHtmlByName('badge.html', array(
  1485. 'bx_if:content' => array(
  1486. 'condition' => $aBadge['is_icon_only'] != '1',
  1487. 'content' => array('content' => _t($aBadge['text'])),
  1488. ),
  1489. 'bx_if:icon' => array(
  1490. 'condition' => $aBadge['icon'] != '',
  1491. 'content' => array('content' => BxDolTemplate::getInstance()->getIcon($aBadge['icon'], array('class' => 'bx-badge-icon sys-colored', 'style' => $sStyleFont))),
  1492. ),
  1493. 'title' => $aBadge['text'],
  1494. 'style_font' => $sStyleFont,
  1495. 'style_bg' => $sStyleBg,
  1496. 'class' => $sClass,
  1497. )
  1498. );
  1499. }
  1500. public function serviceGetFooter()
  1501. {
  1502. return BxDolTemplate::getInstance()->parseHtmlByName('footer.html', []);
  1503. }
  1504. private function _getSearchObject()
  1505. {
  1506. $sClass = 'BxTemplSearch';
  1507. $sElsName = 'bx_elasticsearch';
  1508. $sElsMethod = 'is_configured';
  1509. if(BxDolRequest::serviceExists($sElsName, $sElsMethod) && BxDolService::call($sElsName, $sElsMethod) && !bx_get('cat') && !bx_get('type')) {
  1510. $oModule = BxDolModule::getInstance($sElsName);
  1511. bx_import('Search', $oModule->_aModule);
  1512. $sClass = 'BxElsSearch';
  1513. }
  1514. /**
  1515. * @hooks
  1516. * @hookdef hook-system-search_keyword 'system', 'search_keyword' - hook to override sClass for search
  1517. * - $unit_name - equals `system`
  1518. * - $action - equals `search_keyword`
  1519. * - $object_id - not used
  1520. * - $sender_id - not used
  1521. * - $extra_params - array of additional params with the following array keys:
  1522. * - `override_result` - [string] by ref, class name for search, can be overridden in hook processing
  1523. * @hook @ref hook-system-search_keyword
  1524. */
  1525. bx_alert('system', 'search_keyword', 0, 0, array('class' => &$sClass, 'class_name_ref' => &$sClass));
  1526. $oSearch = new $sClass(bx_get('section'));
  1527. $oSearch->setLiveSearch(bx_get('live_search') ? 1 : 0);
  1528. $oSearch->setMetaType(bx_process_input(bx_get('type')));
  1529. $oSearch->setCategoryObject(bx_process_input(bx_get('cat')));
  1530. return $oSearch;
  1531. }
  1532. public function serviceHomeRedirect()
  1533. {
  1534. header("Location:" . BX_DOL_URL_ROOT, true, 301);
  1535. exit;
  1536. }
  1537. public function serviceProcessEmbed()
  1538. {
  1539. BxDolEmbed::getObjectInstance()->process();
  1540. }
  1541. public function serviceCallAgent($sType, $aAgent, $aParams)
  1542. {
  1543. $oAi = BxDolAI::getInstance();
  1544. return $oAi->callAgent($sType, $aAgent, $aParams);
  1545. }
  1546. public function serviceCallAgentForFormInput($iAgentId)
  1547. {
  1548. $sJson = file_get_contents('php://input');
  1549. $aData = json_decode($sJson, true);
  1550. if (json_last_error() !== JSON_ERROR_NONE)
  1551. return echoJson(['code' => 500, 'msg' => _t('_sys_agents_json_field_err')]);
  1552. $sPrompt = $aData['prompt'] ?? null;
  1553. $sInputName = $aData['input_name'] ?? null;
  1554. $aValues = $aData['values'] ?? [];
  1555. $aAgent = BxDolAiQuery::getAgentObject($iAgentId);
  1556. if (!$aAgent || !$aAgent['active'] || $aAgent['trigger'] !== 'form-input')
  1557. return echoJson(['code' => 404, 'msg' => _t('_sys_agents_agent_not_found')]);
  1558. $oAi = BxDolAI::getInstance();
  1559. if (!$oAi || !$oAi->canInteract($aAgent))
  1560. return echoJson(['code' => 403, 'msg' => _t('_sys_agents_unauthorized')]);
  1561. $aParams = [
  1562. 'sender_profile_id' => bx_get_logged_profile_id(),
  1563. 'user_prompt' => $sPrompt,
  1564. 'form_field_name' => $sInputName,
  1565. 'form_values' => $aValues,
  1566. ];
  1567. $mixed = $oAi->callAgent('form-input', $aAgent, $aParams);
  1568. return echoJson(['code' => 200, 'msg' => $mixed]);
  1569. }
  1570. public function serviceAiChat($iAgentId)
  1571. {
  1572. $sMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '';
  1573. if('GET' === $sMethod)
  1574. return $this->_aiChatHydrate($iAgentId);
  1575. if('POST' !== $sMethod)
  1576. return echoJson(['code' => 405, 'msg' => _t('_error occured')]);
  1577. $aAgent = $this->_aiChatLoadAgent($iAgentId);
  1578. if(!$aAgent)
  1579. return echoJson(['code' => (int)$iAgentId ? 404 : 400, 'msg' => _t('_sys_agents_agent_not_found')]);
  1580. $oAi = BxDolAI::getInstance();
  1581. if(!$oAi)
  1582. return echoJson(['code' => 503, 'msg' => _t('_sys_agents_exception')]);
  1583. if(!$oAi->canChatDirectly($aAgent))
  1584. return echoJson(['code' => 403, 'msg' => _t('_sys_agents_unauthorized')]);
  1585. $sJson = file_get_contents('php://input');
  1586. $aData = json_decode($sJson, true);
  1587. if(json_last_error() !== JSON_ERROR_NONE)
  1588. return echoJson(['code' => 400, 'msg' => _t('_sys_agents_json_field_err')]);
  1589. $sPrompt = $oAi->extractChatPromptFromRequest($aData);
  1590. if(!$sPrompt)
  1591. return echoJson(['code' => 400, 'msg' => _t('_sys_agents_json_field_err')]);
  1592. $aParams = $this->_aiChatHistoryParams($aAgent);
  1593. if (!$aParams)
  1594. return echoJson(['code' => 403, 'msg' => _t('_sys_agents_unauthorized')]);
  1595. $aParams['request_user_turns'] = $oAi->countChatUserTurns($aData['messages'] ?? []);
  1596. $sThreadId = !empty($aData['threadId']) ? $aData['threadId'] : null;
  1597. if(method_exists($oAi, 'streamAgentChat') && class_exists('NeuronAI\Chat\Messages\Stream\Adapters\AGUIAdapter')) {
  1598. $oAi->streamAgentChat($aAgent['id'], $sPrompt, $aParams, $sThreadId);
  1599. return;
  1600. }
  1601. try {
  1602. $sReply = $oAi->callAgent('manual', $aAgent, $sPrompt);
  1603. } catch (Throwable $o) {
  1604. return echoJson(['code' => 500, 'msg' => $o->getMessage(), 'messages' => []]);
  1605. }
  1606. return echoJson(['code' => 200, 'msg' => $sReply, 'text' => $sReply]);
  1607. }
  1608. protected function _aiChatHydrate($iAgentId)
  1609. {
  1610. if(false !== bx_get('offset'))
  1611. return echoJson(['code' => 405, 'msg' => _t('_error occured'), 'messages' => [], 'activeRun' => null, 'interrupts' => null]);
  1612. $aAgent = $this->_aiChatLoadAgent($iAgentId);
  1613. if(!$aAgent)
  1614. return echoJson(['code' => (int)$iAgentId ? 404 : 400, 'msg' => _t('_sys_agents_agent_not_found'), 'messages' => [], 'activeRun' => null, 'interrupts' => null]);
  1615. $oAi = BxDolAI::getInstance();
  1616. if(!$oAi)
  1617. return echoJson(['code' => 503, 'msg' => _t('_sys_agents_exception'), 'messages' => [], 'activeRun' => null, 'interrupts' => null]);
  1618. if(!$oAi->canChatDirectly($aAgent))
  1619. return echoJson(['code' => 403, 'msg' => _t('_sys_agents_unauthorized'), 'messages' => [], 'activeRun' => null, 'interrupts' => null]);
  1620. $aParams = $this->_aiChatHistoryParams($aAgent);
  1621. if (!$aParams)
  1622. return echoJson(['code' => 403, 'msg' => _t('_sys_agents_unauthorized'), 'messages' => [], 'activeRun' => null, 'interrupts' => null]);
  1623. try {
  1624. $aMessages = method_exists($oAi, 'getChatHistoryUiMessages')
  1625. ? $oAi->getChatHistoryUiMessages($aAgent['id'], $aParams)
  1626. : [];
  1627. } catch (Throwable $o) {
  1628. $aMessages = [];
  1629. }
  1630. if(!is_array($aMessages))
  1631. $aMessages = [];
  1632. return echoJson([
  1633. 'messages' => $aMessages,
  1634. 'activeRun' => null,
  1635. 'interrupts' => null,
  1636. ]);
  1637. }
  1638. /**
  1639. * Member/guest subindex + optional `?context=` (group/org profile id).
  1640. * Invalid context is 403 — never fall back to the site-wide thread.
  1641. */
  1642. protected function _aiChatHistoryParams($aAgent)
  1643. {
  1644. $oAi = BxDolAI::getInstance();
  1645. $mixedContext = $oAi->resolveChatHistoryContextPid();
  1646. if ($mixedContext === false)
  1647. return false;
  1648. $aParams = $oAi->resolveChatHistoryParams((int)$aAgent['id']);
  1649. $aParams['chat_history_context_pid'] = (int)$mixedContext;
  1650. return $aParams;
  1651. }
  1652. protected function _aiChatLoadAgent($iAgentId)
  1653. {
  1654. $iAgentId = (int)$iAgentId;
  1655. if (!$iAgentId)
  1656. return false;
  1657. $aAgent = BxDolAIQuery::getAgentObject($iAgentId);
  1658. if(!$aAgent || empty($aAgent['active']))
  1659. return false;
  1660. return $aAgent;
  1661. }
  1662. protected function _getOptionsAiAssistants()
  1663. {
  1664. $aResult = ['' => _t('_Select_one')];
  1665. $aAgents = BxDolAI::getInstance()->getAgentsByTriggerType('manual');
  1666. if($aAgents && is_array($aAgents))
  1667. foreach($aAgents as $aAgent)
  1668. $aResult[$aAgent['id']] = $aAgent['title'];
  1669. return $aResult;
  1670. }
  1671. }
  1672. /** @} */