BxBaseServices.php 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931
  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/' . (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 serviceGetMenu($aParams)
  780. {
  781. $bIsApi = bx_is_api();
  782. $mixedResulEmpty = $bIsApi ? [] : '';
  783. if($bIsApi && is_string($aParams))
  784. $aParams = bx_api_get_browse_params($aParams);
  785. if(!isset($aParams['object']))
  786. return $mixedResulEmpty;
  787. $oMenu = BxDolMenu::getObjectInstance($aParams['object']);
  788. if(!$oMenu)
  789. return $mixedResulEmpty;
  790. if(!empty($aParams['params']) && is_array($aParams['params']))
  791. $oMenu->setContentParams($aParams['params']);
  792. return $bIsApi ? $oMenu->getCodeAPI() : $oMenu->getCode();
  793. }
  794. /**
  795. * @page service Service Calls
  796. * @section bx_system_general System Services
  797. * @subsection bx_system_general-general General
  798. * @subsubsection bx_system_general-get_site_submenu get_site_submenu
  799. *
  800. * @code bx_srv('system', 'get_site_submenu', [], 'TemplServices'); @endcode
  801. * @code {{~system:get_site_submenu:TemplServices[]~}} @endcode
  802. *
  803. * Get Site Submenu code.
  804. * @see BxBaseServices::serviceGetSiteSubmenu
  805. */
  806. /**
  807. * @ref bx_system_general-get_site_submenu "get_site_submenu"
  808. */
  809. public function serviceGetSiteSubmenu()
  810. {
  811. $oMenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  812. return $oMenu ? $oMenu->getCode() : '';
  813. }
  814. /**
  815. * @page service Service Calls
  816. * @section bx_system_general System Services
  817. * @subsection bx_system_general-general General
  818. * @subsubsection bx_system_general-get_products_names get_products_names
  819. *
  820. * @code bx_srv('system', 'get_products_names', [], 'TemplServices');
  821. *
  822. * @code http://example.com/m/oauth2/com/get_products_names?module=system&class=BaseServices @endcode
  823. * @code http://hihi.una.io/modules/?r=oauth2/com/get_products_names&module=system&class=BaseServices @endcode
  824. *
  825. * Get an array of products names from all modules with payments functionality.
  826. * @param $iVendorId filter products by vendor ID.
  827. * @param $iLimit limit number of records from one module.
  828. * @return array of products where key id product name and value is module name
  829. *
  830. * @see BxBaseServices::serviceGetProductsNames
  831. */
  832. /**
  833. * @ref bx_system_general-get_products_names "get_products_names"
  834. */
  835. public function serviceGetProductsNames($iVendorId = 0, $iLimit = 1000)
  836. {
  837. $o = BxDolPayments::getInstance();
  838. if (!$o)
  839. return array();
  840. return $o->getProductsNames($iVendorId, $iLimit);
  841. }
  842. /**
  843. * @see iBxDolProfileService::serviceCheckSpacePrivacy
  844. */
  845. public function serviceCheckSpacePrivacy($iContentId)
  846. {
  847. return _t('_Access denied');
  848. }
  849. public function serviceFormsHelper ()
  850. {
  851. return new BxTemplAccountForms();
  852. }
  853. public function serviceActAsProfile ()
  854. {
  855. return false;
  856. }
  857. public function serviceGetModerators($iContentId)
  858. {
  859. $aModerators = BxDolAclQuery::getInstance()->getProfilesByMembership([
  860. MEMBERSHIP_ID_MODERATOR,
  861. MEMBERSHIP_ID_ADMINISTRATOR
  862. ]);
  863. $aIds = [];
  864. array_walk($aModerators, function ($aItem, $iKey, $aParams) {
  865. if(!empty($aItem['id']))
  866. $aParams[0][] = (int)$aItem['id'];
  867. }, [&$aIds]);
  868. return $aIds;
  869. }
  870. public function servicePrepareFields ($aFieldsProfile)
  871. {
  872. return $aFieldsProfile;
  873. }
  874. public function serviceProfilesSearch ($sTerm, $iLimit)
  875. {
  876. $oDb = BxDolAccountQuery::getInstance();
  877. $aRet = array();
  878. $a = $oDb->searchByTerm($sTerm, $iLimit);
  879. foreach ($a as $r)
  880. $aRet[] = array ('label' => $this->serviceProfileName($r['content_id']), 'value' => $r['profile_id']);
  881. return $aRet;
  882. }
  883. /**
  884. * @page service Service Calls
  885. * @section bx_system_general System Services
  886. * @subsection bx_system_general-general General
  887. * @subsubsection bx_system_general-keyword_search keyword_search
  888. *
  889. * @code bx_srv('system', 'keyword_search', ["bx_posts", ["keyword" => "test"}], 'TemplServices'); @endcode
  890. *
  891. * @code {{~system:keyword_search:TemplServices["bx_posts", {"keyword":"test"}]~}} @endcode
  892. * @code {{~system:keyword_search:TemplServices["bx_albums", {"meta_type": "location_country", "keyword": "AU"}, "unit.html"]~}} @endcode
  893. * @code {{~system:keyword_search:TemplServices["bx_albums", {"meta_type": "location_country_state", "state":"NSW", "keyword": "AU"}, "unit.html"]~}} @endcode
  894. * @code {{~system:keyword_search:TemplServices["bx_albums", {"meta_type": "location_country_city", "state":"NSW", "city":"Manly", "keyword": "AU"}, "unit.html"]~}} @endcode
  895. * @code {{~system:keyword_search:TemplServices["bx_posts", {"meta_type": "mention", "keyword": 2}, "unit_gallery.html"]~}} @endcode
  896. * @code {{~system:keyword_search:TemplServices["bx_posts", {"cat": "bx_posts_cats", "keyword": 3}, "unit_gallery.html"]~}} @endcode
  897. *
  898. * Search by keyword
  899. * @param $sSection - search object to search in, usually module name, for example: bx_posts
  900. * @param $aCondition - condition for search, supported conditions:
  901. * - search by keyword: ["keyword" => "test"]
  902. * - search by country: ["meta_type" => "location_country", "keyword" => "AU"]
  903. * - search by country and state: ["meta_type": "location_country_state", "state":"NSW", "keyword": "AU"]
  904. * - search by country, state and city: ["meta_type": "location_country_city", "state":"NSW", "city":"Manly", "keyword": "AU"]
  905. * - search for mentions: ["meta_type" => "mention", "keyword" => 2]
  906. * - search in category: ["cat": "bx_posts_cats", "keyword": 3]
  907. * @param $sTemplate - template for displaying search results, for example: unit.html
  908. * @param $iStart - paginate, display records starting from this number
  909. * @param $iPerPage - paginate, display this number of records per page
  910. * @param $bLiveSearch - search results like in live search
  911. *
  912. * @see BxBaseServices::serviceKeywordSearch
  913. */
  914. /**
  915. * @ref bx_system_general-keyword_search "keyword_search"
  916. */
  917. public function serviceKeywordSearch ($sSection, $aCondition, $sTemplate = '', $iStart = 0, $iPerPage = 0, $bLiveSearch = 0, $bPaginate = false)
  918. {
  919. if (!$sSection || !isset($aCondition['keyword']))
  920. return '';
  921. $sClass = 'BxTemplSearch';
  922. $sElsName = 'bx_elasticsearch';
  923. $sElsMethod = 'is_configured';
  924. if(BxDolRequest::serviceExists($sElsName, $sElsMethod) && BxDolService::call($sElsName, $sElsMethod)) {
  925. $oModule = BxDolModule::getInstance($sElsName);
  926. bx_import('Search', $oModule->_aModule);
  927. $sClass = 'BxElsSearch';
  928. }
  929. $oSearch = new $sClass(array($sSection));
  930. $oSearch->setLiveSearch($bLiveSearch);
  931. $oSearch->setMetaType(isset($aCondition['meta_type']) ? $aCondition['meta_type'] : '');
  932. $oSearch->setCategoryObject(isset($aCondition['cat']) ? $aCondition['cat'] : '');
  933. $oSearch->setCustomSearchCondition($aCondition);
  934. if (!$bPaginate)
  935. $oSearch->setRawProcessing(true);
  936. $oSearch->setCustomCurrentCondition(array(
  937. 'paginate' => array (
  938. 'start' => $iStart,
  939. 'perPage' => $iPerPage ? $iPerPage : BX_DOL_SEARCH_RESULTS_PER_PAGE_DEFAULT,
  940. )));
  941. if ($sTemplate)
  942. $oSearch->setUnitTemplate($sTemplate);
  943. return $oSearch->response();
  944. }
  945. /**
  946. * @page service Service Calls
  947. * @section bx_system_general System Services
  948. * @subsection bx_system_general-general General
  949. * @subsubsection bx_system_general-keyword_search keyword_search
  950. *
  951. * @code bx_srv('system', 'search_keyword_form', 'TemplServices'); @endcode
  952. *
  953. * Block with Search by Keywords Form
  954. *
  955. * @see BxBaseServices::serviceSearchKeywordForm
  956. */
  957. /**
  958. * @ref bx_system_general-keyword_search "keyword_search"
  959. */
  960. public function serviceSearchKeywordForm ()
  961. {
  962. return $this->_getSearchObject()->getForm(BX_DB_PADDING_DEF, false, true);
  963. }
  964. /**
  965. * @page service Service Calls
  966. * @section bx_system_general System Services
  967. * @subsection bx_system_general-general General
  968. * @subsubsection bx_system_general-keyword_search keyword_search
  969. *
  970. * @code bx_srv('system', 'search_keyword_result', 'TemplServices'); @endcode
  971. *
  972. * Block with Search by Keywords Results
  973. *
  974. * @see BxBaseServices::serviceSearchKeywordResult
  975. */
  976. /**
  977. * @ref bx_system_general-keyword_search "keyword_search"
  978. */
  979. public function serviceSearchKeywordResult ()
  980. {
  981. $sType = bx_process_input(bx_get('type'));
  982. $sKeyword = bx_process_input(bx_get('keyword'));
  983. $bKeyword = $sKeyword !== false;
  984. if(bx_is_api())
  985. return $this->serviceGetDataSearchApi(['params' => [
  986. 'type' => $sType,
  987. 'keyword' => $sKeyword,
  988. 'section' => bx_process_input(bx_get('section')),
  989. 'cat' => bx_process_input(bx_get('cat'))
  990. ]]);
  991. $sCode = '';
  992. if($bKeyword) {
  993. $oSearch = $this->_getSearchObject();
  994. if(($sCode = $oSearch->response()))
  995. $sCode = BxDolTemplate::getInstance()->parseHtmlByName('search_result_block.html', [
  996. 'html_id' => 'bx-search-results-keyword',
  997. 'class' => 'bx-search-results-container',
  998. 'attrs' => '',
  999. 'content' => $sCode,
  1000. 'bx_if:do_center' => [
  1001. 'condition' => false,
  1002. 'content' => [
  1003. 'html_id' => '',
  1004. 'selector_content' => ''
  1005. ]
  1006. ]
  1007. ]);
  1008. else
  1009. $sCode = $oSearch->getEmptyResult();
  1010. }
  1011. return $sCode;
  1012. }
  1013. /**
  1014. * @ref bx_system_general-get_date_search_api "get_date_search_api"
  1015. * @api @ref bx_system_general-get_date_search_api "get_date_search_api"
  1016. */
  1017. public function serviceGetDataSearchApi ($aParams)
  1018. {
  1019. if(!bx_is_api())
  1020. return false;
  1021. if(is_string($aParams))
  1022. $aParams = bx_api_get_browse_params($aParams);
  1023. $bForceAll = ($aParams['params']['type'] ?? '') == 'keyword';
  1024. $aSectionsAvail = explode(',', getParam('sys_api_search_sections'));
  1025. $aSectionsAll = BxDolDb::getInstance()->fromCache(
  1026. 'sys_global_search_pairs',
  1027. 'getPairs',
  1028. 'SELECT `ObjectName` AS `name`, `Title` AS `title` FROM `sys_objects_search` WHERE `GlobalSearch`=\'1\' ORDER BY `Order` ASC',
  1029. 'name', 'title'
  1030. );
  1031. $aSections = [];
  1032. foreach($aSectionsAll as $sSectionName => $sSectionTitle)
  1033. if(in_array($sSectionName, $aSectionsAvail) || $bForceAll)
  1034. $aSections[$sSectionName] = [
  1035. 'name' => $sSectionName,
  1036. 'title' => _t($sSectionTitle)
  1037. ];
  1038. $aParamsBrowse = array_merge([
  1039. 'keyword' => '',
  1040. 'section' => '',
  1041. 'sections' => array_values($aSections),
  1042. 'start' => 0,
  1043. 'per_page' => 12
  1044. ], !empty($aParams['params']) && is_array($aParams['params']) ? $aParams['params'] : []);
  1045. if(empty($aParamsBrowse['section']))
  1046. $aParamsBrowse['section'] = array_keys($aSections);
  1047. else if(is_string($aParamsBrowse['section']))
  1048. $aParamsBrowse['section'] = explode(',', $aParamsBrowse['section']);
  1049. $sClass = 'BxTemplSearch';
  1050. $sSections = $aParamsBrowse['section'];
  1051. if ($aParamsBrowse['live'] !== true && count($sSections) > 1){
  1052. $aParamsBrowse['section'] = [];
  1053. $aParamsBrowse['sections'] = [];
  1054. $aDataRv = [];
  1055. foreach ($sSections as $sSection) {
  1056. $oSearch = new $sClass($sSection);
  1057. $oSearch->setLiveSearch(true);
  1058. $oSearch->setDataProcessing(true);
  1059. $oSearch->setCustomSearchCondition(['keyword' => $aParamsBrowse['keyword']]);
  1060. $oSearch->setCustomCurrentCondition([
  1061. 'paginate' => [
  1062. 'forceStart' => $aParamsBrowse['start'],
  1063. 'perPage' => $aParamsBrowse['per_page'],
  1064. ]
  1065. ]);
  1066. $aData = $oSearch->response();
  1067. if(count($aData) > $aParamsBrowse['per_page'])
  1068. $aData = array_slice($aData, $aParamsBrowse['start'], $aParamsBrowse['per_page']);
  1069. if (count($aData) > 0) {
  1070. $oSearchResult = $oSearch->getSearchResultObject($sSection);
  1071. $sSectionTitle = $oSearchResult->aCurrent['title'];
  1072. $aParamsBrowse['section'][] = $sSection;
  1073. $aParamsBrowse['sections'][] = ['name' => $sSection, 'title' => $sSectionTitle];
  1074. $aDataRv[] = [
  1075. 'section' => $sSection,
  1076. 'section_name' => $sSectionTitle,
  1077. 'data' => $aData,
  1078. 'is_profile' => bx_srv('system', 'is_module_profile', [$oSearchResult->aCurrent['module_name']])
  1079. ];
  1080. }
  1081. }
  1082. return [bx_api_get_block('search_sections', [
  1083. 'data' => $aDataRv,
  1084. 'params' => $aParamsBrowse
  1085. ])];
  1086. }
  1087. else{
  1088. $oSearch = new $sClass($aParamsBrowse['section']);
  1089. $oSearch->setLiveSearch(true);
  1090. $oSearch->setDataProcessing(true);
  1091. $oSearch->setCustomSearchCondition(['keyword' => $aParamsBrowse['keyword']]);
  1092. $oSearch->setCustomCurrentCondition([
  1093. 'paginate' => [
  1094. 'forceStart' => $aParamsBrowse['start'],
  1095. 'perPage' => $aParamsBrowse['per_page'],
  1096. ]
  1097. ]);
  1098. $aData = $oSearch->response();
  1099. if(count($aData) > $aParamsBrowse['per_page'])
  1100. $aData = array_slice($aData, $aParamsBrowse['start'], $aParamsBrowse['per_page']);
  1101. return [
  1102. bx_api_get_block('browse', [
  1103. 'unit' => 'search-results',
  1104. 'request_url' => '/api.php?r=system/get_data_search_api/TemplServices&params[]=',
  1105. 'params' => $aParamsBrowse,
  1106. 'data' => $aData
  1107. ])
  1108. ];
  1109. }
  1110. }
  1111. /**
  1112. * @page service Service Calls
  1113. * @section bx_system_general System Services
  1114. * @subsection bx_system_general-general General
  1115. * @subsubsection bx_system_general-cmts cmts
  1116. *
  1117. * @code bx_srv('system', 'cmts', ["sys_blocks", 1], 'TemplServices'); @endcode
  1118. *
  1119. * @code {{~system:cmts:TemplServices["sys_blocks", 1]~}} @endcode
  1120. *
  1121. * Comments block
  1122. * @param $sObject - comments object name
  1123. * @param $sId - content id assiciated tith the comments
  1124. *
  1125. * @see BxBaseServices::serviceCmts
  1126. */
  1127. /**
  1128. * @ref bx_system_general-cmts "cmts"
  1129. */
  1130. public function serviceCmts ($sObject, $sId)
  1131. {
  1132. $o = BxDolCmts::getObjectInstance($sObject, $sId);
  1133. if (!$o || !$o->isEnabled())
  1134. return '';
  1135. return $o->getCommentsBlock(array(), array('in_designbox' => false, 'show_empty' => true));
  1136. }
  1137. public function _serviceProfileFunc ($sFunc, $iContentId, $aParams = array())
  1138. {
  1139. if (!$iContentId)
  1140. return false;
  1141. if (!($oAccount = BxDolAccount::getInstance($iContentId)))
  1142. return false;
  1143. return $oAccount->$sFunc(false, $aParams);
  1144. }
  1145. public function serviceAlertResponseProcessInstalled()
  1146. {
  1147. BxDolTranscoderImage::registerHandlersSystem();
  1148. }
  1149. public function serviceAlertResponseProcessStorageChange ($oAlert)
  1150. {
  1151. if ('sys_storage_default' != $oAlert->aExtras['option'])
  1152. return;
  1153. $aStorages = BxDolStorageQuery::getStorageObjects();
  1154. foreach ($aStorages as $r) {
  1155. if (0 == $r['current_size'] && 0 == $r['current_number'] && ($oStorage = BxDolStorage::getObjectInstance($r['object'])))
  1156. $oStorage->changeStorageEngine($oAlert->aExtras['value']);
  1157. }
  1158. }
  1159. public function serviceGetOptionsProfileBot()
  1160. {
  1161. $aResult = array(
  1162. array('key' => '', 'value' => _t('_Select_one'))
  1163. );
  1164. $aAccountsIds = BxDolAccountQuery::getInstance()->getOperators();
  1165. foreach($aAccountsIds as $iAccountId) {
  1166. $aProfilesIds = BxDolAccount::getInstance($iAccountId)->getProfilesIds(true, false);
  1167. foreach($aProfilesIds as $iProfileId) {
  1168. $oProfile = BxDolProfile::getInstance($iProfileId);
  1169. $aResult[] = array(
  1170. 'key' => $iProfileId,
  1171. 'value' => _t('_sys_profile_with_type', $oProfile->getDisplayName(), $oProfile->getModule()),
  1172. );
  1173. }
  1174. }
  1175. return $aResult;
  1176. }
  1177. public function serviceGetOptionsModuleListForPrivacySelector()
  1178. {
  1179. $aResult = array();
  1180. $BxDolModuleQuery = BxDolModuleQuery::getInstance();
  1181. $aModules = $BxDolModuleQuery->getModulesBy(array('type' => 'modules', 'active' => 1));
  1182. foreach($aModules as $aModule){
  1183. $oModule = BxDolModule::getInstance($aModule['name']);
  1184. if($oModule instanceof iBxDolContentInfoService){
  1185. if (!BxDolRequest::serviceExists($aModule['name'], 'act_as_profile'))
  1186. continue;
  1187. $aResult[$aModule['name']] = $aModule['title'];
  1188. }
  1189. }
  1190. return $aResult;
  1191. }
  1192. public function serviceGetOptionsCaptchaDefault()
  1193. {
  1194. $aResults = [];
  1195. $aObjects = BxDolCaptchaQuery::getObjects();
  1196. foreach($aObjects as $aObject)
  1197. $aResults[$aObject['object']] = $aObject['title'];
  1198. return $aResults;
  1199. }
  1200. public function serviceGetOptionsEmbedDefault()
  1201. {
  1202. $aResults = array(
  1203. '' => _t('_None')
  1204. );
  1205. $aObjects = BxDolEmbedQuery::getObjects();
  1206. foreach($aObjects as $aObject)
  1207. $aResults[$aObject['object']] = _t($aObject['title']);
  1208. return $aResults;
  1209. }
  1210. public function serviceGetOptionsLocationFieldDefault()
  1211. {
  1212. $aResults = array();
  1213. $aObjects = BxDolLocationFieldQuery::getObjects();
  1214. foreach($aObjects as $aObject)
  1215. $aResults[$aObject['object']] = _t($aObject['title']);
  1216. return $aResults;
  1217. }
  1218. public function serviceGetOptionsLocationMapDefault()
  1219. {
  1220. $aResults = array();
  1221. $aObjects = BxDolLocationMapQuery::getObjects();
  1222. foreach($aObjects as $aObject)
  1223. $aResults[$aObject['object']] = _t($aObject['title']);
  1224. return $aResults;
  1225. }
  1226. public function serviceGetOptionsPushDefault()
  1227. {
  1228. $aResults = [
  1229. '' => _t('_None')
  1230. ];
  1231. $aObjects = BxDolPushQuery::getObjects();
  1232. foreach($aObjects as $aObject)
  1233. $aResults[$aObject['object']] = $aObject['title'];
  1234. return $aResults;
  1235. }
  1236. public function serviceGetOptionsSmsDefault()
  1237. {
  1238. $aResults = [
  1239. '' => _t('_None')
  1240. ];
  1241. $aObjects = BxDolSmsQuery::getObjects();
  1242. foreach($aObjects as $aObject)
  1243. $aResults[$aObject['object']] = $aObject['title'];
  1244. return $aResults;
  1245. }
  1246. public function serviceGetOptionsSocketsFieldDefault()
  1247. {
  1248. $aResults = [];
  1249. $aObjects = ['sys_sockets_disabled', 'sys_sockets_soketi'];
  1250. foreach($aObjects as $sObject)
  1251. $aResults[$sObject] = _t('_adm_stg_cpt_option_sys_sockets_type_' . $sObject);
  1252. return $aResults;
  1253. }
  1254. public function serviceGetOptionsLocationLeafletGetProviders()
  1255. {
  1256. return BxDolLocationMapLeaflet::getProviders();
  1257. }
  1258. public function serviceGetOptionsRelations()
  1259. {
  1260. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  1261. $aProfiles = array();
  1262. foreach($aModules as $aModule) {
  1263. $sMethod = 'act_as_profile';
  1264. if(!BxDolRequest::serviceExists($aModule['name'], $sMethod) || !BxDolService::call($aModule['name'], $sMethod))
  1265. continue;
  1266. $aProfiles[$aModule['name']] = _t('_' . $aModule['name']);
  1267. }
  1268. $aResults = array();
  1269. foreach($aProfiles as $sName1 => $sTitle1)
  1270. foreach($aProfiles as $sName2 => $sTitle2)
  1271. $aResults[$sName1 . '_' . $sName2] = $sTitle1 . ' - ' . $sTitle2;
  1272. return $aResults;
  1273. }
  1274. public function serviceGetOptionsCfProhibited()
  1275. {
  1276. return BxDolFormQuery::getDataItems('sys_content_filter');
  1277. }
  1278. public function serviceGetOptionsCfUnauthenticated()
  1279. {
  1280. return BxDolFormQuery::getDataItems('sys_content_filter');
  1281. }
  1282. public function serviceGetOptionsTaiwindDefault()
  1283. {
  1284. $sPath = BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'tailwind/css';
  1285. $aExcludes = ['.', '..', 'tailwind.css'];
  1286. $aResults = [];
  1287. if(($oHandle = opendir($sPath)) !== false) {
  1288. while(($sFile = readdir($oHandle)) !== false) {
  1289. if(in_array($sFile, $aExcludes))
  1290. continue;
  1291. if(($oHandlerFile = fopen($sPath . '/' . $sFile, 'r')) === false)
  1292. continue;
  1293. if(($sContent = fread($oHandlerFile, 1024)) !== false && strpos($sContent, '@tailwind base') === false)
  1294. $aResults[$sFile] = $sFile;
  1295. fclose($oHandlerFile);
  1296. }
  1297. closedir($oHandle);
  1298. }
  1299. /**
  1300. * Disabled for now, because CDN and custom compiled build with own styles
  1301. * don't work correctly together.
  1302. *
  1303. * $aResults['cdn'] = _t('_adm_stg_cpt_option_sys_css_tailwind_default_cdn');
  1304. */
  1305. return $aResults;
  1306. }
  1307. public function serviceGetOptionsIconsetDefault()
  1308. {
  1309. $aObjects = BxDolIconsetQuery::getObjects();
  1310. foreach($aObjects as $aObject)
  1311. $aResults[$aObject['object']] = $aObject['title'];
  1312. return $aResults;
  1313. }
  1314. public function serviceGetOptionsApiRootPageGuest()
  1315. {
  1316. $sKey = '_adm_stg_cpt_option_sys_api_root_page_';
  1317. return [
  1318. 'home' => _t($sKey .'home'),
  1319. 'splash' => _t($sKey . 'splash')
  1320. ];
  1321. }
  1322. public function serviceGetOptionsApiRootPageMember()
  1323. {
  1324. $sKey = '_adm_stg_cpt_option_sys_api_root_page_';
  1325. return [
  1326. 'home' => _t($sKey .'home'),
  1327. 'dashboard' => _t($sKey . 'dashboard'),
  1328. 'profile' => _t($sKey . 'profile')
  1329. ];
  1330. }
  1331. public function serviceGetOptionsApiMenuTop()
  1332. {
  1333. $aMenus = BxDolMenuQuery::getMenuObjects();
  1334. $aResults = ['' => _t('_sys_please_select')];
  1335. foreach($aMenus as $aMenu)
  1336. if($aMenu['module'] == 'system')
  1337. $aResults[$aMenu['object']] = _t($aMenu['title']);
  1338. return $aResults;
  1339. }
  1340. public function serviceGetOptionsApiContextSwitcher()
  1341. {
  1342. $aContexts = bx_srv('system', 'get_modules_by_type', ['context']);
  1343. $aResults = ['' => _t('_None')];
  1344. foreach($aContexts as $aContext)
  1345. $aResults[$aContext['name']] = _t('_' . $aContext['name']);
  1346. return $aResults;
  1347. }
  1348. public function serviceGetOptionsApiContextConnection()
  1349. {
  1350. $aConnections = ['subscriptions', 'fans', 'participants'];
  1351. $aResults = ['' => _t('_sys_please_select')];
  1352. foreach($aConnections as $sConnection)
  1353. $aResults[$sConnection] = _t('_adm_stg_cpt_option_sys_api_cnt_cnn_' . $sConnection);
  1354. return $aResults;
  1355. }
  1356. public function serviceGetOptionsAgentsModel()
  1357. {
  1358. return ['' => _t('_Select_one')] + BxDolAI::getInstance()->getModels(['active' => true]);
  1359. }
  1360. public function serviceGetOptionsAgentsProfile($bSelectOne = true, $sSelectOneLangKey = '_Select_one')
  1361. {
  1362. $aResult = [];
  1363. if($bSelectOne)
  1364. $aResult[] = ['key' => '', 'value' => _t($sSelectOneLangKey)];
  1365. $aAccountsIds = BxDolAccountQuery::getInstance()->getOperators();
  1366. foreach($aAccountsIds as $iAccountId) {
  1367. $aProfilesIds = BxDolAccount::getInstance($iAccountId)->getProfilesIds(true, false);
  1368. foreach($aProfilesIds as $iProfileId) {
  1369. $oProfile = BxDolProfile::getInstance($iProfileId);
  1370. $sProfileModule = false === $oProfile ? '' : $oProfile->getModule();
  1371. if ($sProfileModule && ($sProfileModule != 'system' || ($sProfileModule == 'system' && empty($oProfile->getAccountObject()->getEmail())))) {
  1372. $aResult[] = [
  1373. 'key' => $iProfileId,
  1374. 'value' => _t('_sys_profile_with_type', $oProfile->getDisplayName(), $sProfileModule === 'system' ? 'System' : _t('_' . $sProfileModule))
  1375. ];
  1376. }
  1377. }
  1378. }
  1379. return $aResult;
  1380. }
  1381. public function serviceRedirect($sUrl = false)
  1382. {
  1383. if (!getParam('sys_confirmation_before_redirect')) {
  1384. BxDolTemplate::getInstance()->displayPageNotFound();
  1385. exit;
  1386. }
  1387. if (false === $sUrl)
  1388. $sUrl = bx_get('url');
  1389. if (!$sUrl || !preg_match('@^https?://@', $sUrl)) {
  1390. http_response_code(500);
  1391. return MsgBox(_t('_error occured'));
  1392. }
  1393. else {
  1394. $bSpam = null;
  1395. /**
  1396. * @hooks
  1397. * @hookdef hook-system-check_spam_url 'system', 'check_spam_url' - hook to override sClass for search
  1398. * - $unit_name - equals `system`
  1399. * - $action - equals `check_spam_url`
  1400. * - $object_id - not used
  1401. * - $sender_id - logged account id
  1402. * - $extra_params - array of additional params with the following array keys:
  1403. * - `is_spam` - [bool] by ref, spam url or not, can be overridden in hook processing
  1404. * - `content` - [string] by ref, url of link, can be overridden in hook processing
  1405. * - `where` - [string] equal redirect
  1406. * @hook @ref hook-system-check_spam_url
  1407. */
  1408. bx_alert('system', 'check_spam_url', 0, getLoggedId(), array(
  1409. 'is_spam' => &$bSpam,
  1410. 'content' => &$sUrl,
  1411. 'is_spam_ref' => &$bSpam,
  1412. 'content_ref' => &$sUrl,
  1413. 'where' => 'redirect'
  1414. ));
  1415. $sLangKey = $bSpam ? '_sys_redirect_confirmation_harmful' : '_sys_redirect_confirmation';
  1416. return BxDolTemplate::getInstance()->parseHtmlByName('redirect.html', [
  1417. 'text' => _t($sLangKey, bx_process_output($sUrl), getParam('site_title')),
  1418. 'url' => bx_js_string($sUrl, BX_ESCAPE_STR_APOS),
  1419. ]);
  1420. }
  1421. }
  1422. public function serviceSetBadges($iContentId, $sBadgesIds, $sModule)
  1423. {
  1424. $aSelectedBadges = explode(",", $sBadgesIds);
  1425. $iPerformerId = bx_get_logged_profile_id();
  1426. $aCheck = checkActionModule($iPerformerId, 'set badge', 'system', false);
  1427. if(!isAdmin() && $aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  1428. return false;
  1429. $oBadges = BxDolBadges::getInstance();
  1430. $oBadges->delete([
  1431. 'type' => 'by_module&object',
  1432. 'object_id' => $iContentId,
  1433. 'module' => $sModule
  1434. ]);
  1435. foreach ($aSelectedBadges as $iSelectedBadge)
  1436. $oBadges->add($iSelectedBadge, $iContentId, $sModule);
  1437. checkActionModule($iPerformerId, 'set badge', 'system', true);
  1438. return true;
  1439. }
  1440. public function serviceGetBadge($aBadge, $bIsCompact = false)
  1441. {
  1442. $sClass = '';
  1443. $sStyleFont = '';
  1444. $sStyleBg = '';
  1445. if ($aBadge['color'] == '')
  1446. $aBadge['color'] = 'purple-600';
  1447. if ($bIsCompact && $aBadge['icon'] != ''){
  1448. $aBadge['is_icon_only'] = 1;
  1449. }
  1450. if ($aBadge['is_icon_only'] == 1){
  1451. $sClass .= ' bx-badge-compact';
  1452. if (substr_count($aBadge['color'], 'rgb('))
  1453. $sStyleFont = 'color: ' . $aBadge['color'];
  1454. else
  1455. $sClass .= ' text-' . $aBadge['color'];
  1456. }
  1457. else{
  1458. if (substr_count($aBadge['color'], 'rgb('))
  1459. $sStyleBg = 'background-color: ' . $aBadge['color'];
  1460. else
  1461. $sClass .= ' bg-' . $aBadge['color'];
  1462. }
  1463. return BxDolTemplate::getInstance()->parseHtmlByName('badge.html', array(
  1464. 'bx_if:content' => array(
  1465. 'condition' => $aBadge['is_icon_only'] != '1',
  1466. 'content' => array('content' => _t($aBadge['text'])),
  1467. ),
  1468. 'bx_if:icon' => array(
  1469. 'condition' => $aBadge['icon'] != '',
  1470. 'content' => array('content' => BxDolTemplate::getInstance()->getIcon($aBadge['icon'], array('class' => 'bx-badge-icon sys-colored', 'style' => $sStyleFont))),
  1471. ),
  1472. 'title' => $aBadge['text'],
  1473. 'style_font' => $sStyleFont,
  1474. 'style_bg' => $sStyleBg,
  1475. 'class' => $sClass,
  1476. )
  1477. );
  1478. }
  1479. public function serviceGetFooter()
  1480. {
  1481. return BxDolTemplate::getInstance()->parseHtmlByName('footer.html', []);
  1482. }
  1483. private function _getSearchObject()
  1484. {
  1485. $sClass = 'BxTemplSearch';
  1486. $sElsName = 'bx_elasticsearch';
  1487. $sElsMethod = 'is_configured';
  1488. if(BxDolRequest::serviceExists($sElsName, $sElsMethod) && BxDolService::call($sElsName, $sElsMethod) && !bx_get('cat') && !bx_get('type')) {
  1489. $oModule = BxDolModule::getInstance($sElsName);
  1490. bx_import('Search', $oModule->_aModule);
  1491. $sClass = 'BxElsSearch';
  1492. }
  1493. /**
  1494. * @hooks
  1495. * @hookdef hook-system-search_keyword 'system', 'search_keyword' - hook to override sClass for search
  1496. * - $unit_name - equals `system`
  1497. * - $action - equals `search_keyword`
  1498. * - $object_id - not used
  1499. * - $sender_id - not used
  1500. * - $extra_params - array of additional params with the following array keys:
  1501. * - `override_result` - [string] by ref, class name for search, can be overridden in hook processing
  1502. * @hook @ref hook-system-search_keyword
  1503. */
  1504. bx_alert('system', 'search_keyword', 0, 0, array('class' => &$sClass, 'class_name_ref' => &$sClass));
  1505. $oSearch = new $sClass(bx_get('section'));
  1506. $oSearch->setLiveSearch(bx_get('live_search') ? 1 : 0);
  1507. $oSearch->setMetaType(bx_process_input(bx_get('type')));
  1508. $oSearch->setCategoryObject(bx_process_input(bx_get('cat')));
  1509. return $oSearch;
  1510. }
  1511. public function serviceHomeRedirect()
  1512. {
  1513. header("Location:" . BX_DOL_URL_ROOT, true, 301);
  1514. exit;
  1515. }
  1516. public function serviceProcessEmbed()
  1517. {
  1518. BxDolEmbed::getObjectInstance()->process();
  1519. }
  1520. public function serviceCallAgent($sType, $aAgent, $aParams)
  1521. {
  1522. $oAi = BxDolAI::getInstance();
  1523. return $oAi->callAgent($sType, $aAgent, $aParams);
  1524. }
  1525. public function serviceCallAgentForFormInput($iAgentId)
  1526. {
  1527. $sJson = file_get_contents('php://input');
  1528. $aData = json_decode($sJson, true);
  1529. if (json_last_error() !== JSON_ERROR_NONE)
  1530. return echoJson(['code' => 500, 'msg' => _t('_sys_agents_json_field_err')]);
  1531. $sPrompt = $aData['prompt'] ?? null;
  1532. $sInputName = $aData['input_name'] ?? null;
  1533. $aValues = $aData['values'] ?? [];
  1534. $aAgent = BxDolAiQuery::getAgentObject($iAgentId);
  1535. if (!$aAgent || !$aAgent['active'] || $aAgent['trigger'] !== 'form-input')
  1536. return echoJson(['code' => 404, 'msg' => _t('_sys_agents_agent_not_found')]);
  1537. $oAi = BxDolAI::getInstance();
  1538. if (!$oAi || !$oAi->canInteract($aAgent))
  1539. return echoJson(['code' => 403, 'msg' => _t('_sys_agents_unauthorized')]);
  1540. $aParams = [
  1541. 'sender_profile_id' => bx_get_logged_profile_id(),
  1542. 'user_prompt' => $sPrompt,
  1543. 'form_field_name' => $sInputName,
  1544. 'form_values' => $aValues,
  1545. ];
  1546. $mixed = $oAi->callAgent('form-input', $aAgent, $aParams);
  1547. return echoJson(['code' => 200, 'msg' => $mixed]);
  1548. }
  1549. public function serviceAiChat($iAgentId)
  1550. {
  1551. $sMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '';
  1552. if('GET' === $sMethod)
  1553. return $this->_aiChatHydrate($iAgentId);
  1554. if('POST' !== $sMethod)
  1555. return echoJson(['code' => 405, 'msg' => _t('_error occured')]);
  1556. $aAgent = $this->_aiChatLoadAgent($iAgentId);
  1557. if(!$aAgent)
  1558. return echoJson(['code' => (int)$iAgentId ? 404 : 400, 'msg' => _t('_sys_agents_agent_not_found')]);
  1559. $oAi = BxDolAI::getInstance();
  1560. if(!$oAi)
  1561. return echoJson(['code' => 503, 'msg' => _t('_sys_agents_exception')]);
  1562. if(!$oAi->canChatDirectly($aAgent))
  1563. return echoJson(['code' => 403, 'msg' => _t('_sys_agents_unauthorized')]);
  1564. $sJson = file_get_contents('php://input');
  1565. $aData = json_decode($sJson, true);
  1566. if(json_last_error() !== JSON_ERROR_NONE)
  1567. return echoJson(['code' => 400, 'msg' => _t('_sys_agents_json_field_err')]);
  1568. $sPrompt = $oAi->extractChatPromptFromRequest($aData);
  1569. if(!$sPrompt)
  1570. return echoJson(['code' => 400, 'msg' => _t('_sys_agents_json_field_err')]);
  1571. $aParams = $this->_aiChatHistoryParams($aAgent);
  1572. if (!$aParams)
  1573. return echoJson(['code' => 403, 'msg' => _t('_sys_agents_unauthorized')]);
  1574. $aParams['request_user_turns'] = $oAi->countChatUserTurns($aData['messages'] ?? []);
  1575. $sThreadId = !empty($aData['threadId']) ? $aData['threadId'] : null;
  1576. if(class_exists('NeuronAI\Chat\Messages\Stream\Adapters\AGUIAdapter')) {
  1577. $oAi->streamAgentChat($aAgent['id'], $sPrompt, $aParams, $sThreadId);
  1578. return;
  1579. }
  1580. try {
  1581. $sReply = $oAi->callAgent('manual', $aAgent, $sPrompt);
  1582. } catch (Throwable $o) {
  1583. return echoJson(['code' => 500, 'msg' => $o->getMessage(), 'messages' => []]);
  1584. }
  1585. return echoJson(['code' => 200, 'msg' => $sReply, 'text' => $sReply]);
  1586. }
  1587. protected function _aiChatHydrate($iAgentId)
  1588. {
  1589. if(false !== bx_get('offset'))
  1590. return echoJson(['code' => 405, 'msg' => _t('_error occured'), 'messages' => [], 'activeRun' => null, 'interrupts' => null]);
  1591. $aAgent = $this->_aiChatLoadAgent($iAgentId);
  1592. if(!$aAgent)
  1593. return echoJson(['code' => (int)$iAgentId ? 404 : 400, 'msg' => _t('_sys_agents_agent_not_found'), 'messages' => [], 'activeRun' => null, 'interrupts' => null]);
  1594. $oAi = BxDolAI::getInstance();
  1595. if(!$oAi)
  1596. return echoJson(['code' => 503, 'msg' => _t('_sys_agents_exception'), 'messages' => [], 'activeRun' => null, 'interrupts' => null]);
  1597. if(!$oAi->canChatDirectly($aAgent))
  1598. return echoJson(['code' => 403, 'msg' => _t('_sys_agents_unauthorized'), 'messages' => [], 'activeRun' => null, 'interrupts' => null]);
  1599. $aParams = $this->_aiChatHistoryParams($aAgent);
  1600. if (!$aParams)
  1601. return echoJson(['code' => 403, 'msg' => _t('_sys_agents_unauthorized'), 'messages' => [], 'activeRun' => null, 'interrupts' => null]);
  1602. try {
  1603. $aMessages = $oAi->getChatHistoryUiMessages($aAgent['id'], $aParams);
  1604. } catch (Throwable $o) {
  1605. $aMessages = [];
  1606. }
  1607. if(!is_array($aMessages))
  1608. $aMessages = [];
  1609. return echoJson([
  1610. 'messages' => $aMessages,
  1611. 'activeRun' => null,
  1612. 'interrupts' => null,
  1613. ]);
  1614. }
  1615. /**
  1616. * Member/guest subindex + optional `?context=` (group/org profile id).
  1617. * Invalid context is 403 — never fall back to the site-wide thread.
  1618. */
  1619. protected function _aiChatHistoryParams($aAgent)
  1620. {
  1621. $oAi = BxDolAI::getInstance();
  1622. $mixedContext = $oAi->resolveChatHistoryContextPid();
  1623. if ($mixedContext === false)
  1624. return false;
  1625. $aParams = $oAi->resolveChatHistoryParams((int)$aAgent['id']);
  1626. $aParams['chat_history_context_pid'] = (int)$mixedContext;
  1627. return $aParams;
  1628. }
  1629. protected function _aiChatLoadAgent($iAgentId)
  1630. {
  1631. $iAgentId = (int)$iAgentId;
  1632. if (!$iAgentId)
  1633. return false;
  1634. $aAgent = BxDolAIQuery::getAgentObject($iAgentId);
  1635. if(!$aAgent || empty($aAgent['active']))
  1636. return false;
  1637. return $aAgent;
  1638. }
  1639. }
  1640. /** @} */