1
0

BxBaseServiceContent.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. * Services for content objects
  11. */
  12. class BxBaseServiceContent extends BxDol
  13. {
  14. function serviceCheckLogged()
  15. {
  16. $oAccount = BxDolAccount::getInstance();
  17. $aRet = $oAccount ? $this->getUserIds($oAccount) : null;
  18. if ($aRet)
  19. $aRet['session'] = BxDolSession::getInstance()->getId();
  20. else
  21. $aRet = ['session' => BxDolSession::getInstance()->getId()];
  22. return $aRet;
  23. }
  24. /**
  25. * @page service Service Calls
  26. * @section bx_system_general System Services
  27. * @subsection bx_system_general-content-objects Content Objects
  28. * @subsubsection bx_system_general_cnt-login Login user and get user session
  29. *
  30. * Login user with account id = 12 and get session id back, session id can be used
  31. * as cookie header to perform other API calls under logged user:
  32. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/login/TemplServiceContent&params[]=12" @endcode
  33. *
  34. * @param $iAccountId account id, or account email
  35. * @param $bRememberMe remeber session
  36. * @return array with session and user ids on success or false on error
  37. *
  38. * @see BxBaseServiceContent::serviceLogin
  39. */
  40. /**
  41. * @ref bx_system_general_cnt-login "Login user and get user's session"
  42. */
  43. function serviceLogin($iAccountId, $bRememberMe = false)
  44. {
  45. $oAccount = BxDolAccount::getInstance($iAccountId);
  46. if (!$oAccount)
  47. return false;
  48. bx_login($oAccount->id(), $bRememberMe);
  49. $aRet = $this->getUserIds($oAccount);
  50. if ($aRet)
  51. $aRet['session'] = BxDolSession::getInstance()->getId();
  52. else
  53. $aRet = ['session' => BxDolSession::getInstance()->getId()];
  54. return $aRet;
  55. }
  56. /**
  57. * @page service Service Calls
  58. * @section bx_system_general System Services
  59. * @subsection bx_system_general-content-objects Content Objects
  60. * @subsubsection bx_system_general_cnt-logout Logout current user
  61. *
  62. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/logout/TemplServiceContent" @endcode
  63. *
  64. * @return boolean
  65. *
  66. * @see BxBaseServiceContent::serviceLogout
  67. */
  68. /**
  69. * @ref bx_system_general_cnt-logout "Logout current user"
  70. */
  71. function serviceLogout($bAllSessions = false)
  72. {
  73. if (!isLogged())
  74. return false;
  75. if ($bAllSessions) {
  76. $o = new BxDolSessionQuery();
  77. $o->deleteByAccountId(getLoggedId());
  78. }
  79. else {
  80. bx_logout();
  81. }
  82. return true;
  83. }
  84. /**
  85. * @page service Service Calls
  86. * @section bx_system_general System Services
  87. * @subsection bx_system_general-content-objects Content Objects
  88. * @subsubsection bx_system_general_cnt-get_user_ids Get user's IDs
  89. *
  90. * Get user's account id, profile id and profile module content id by account id.
  91. *
  92. * Get user's IDs by user's account id(12):
  93. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/get_user_ids/TemplServiceContent&params[]=12" @endcode
  94. *
  95. * @param $iAccountId account id, or account email
  96. * @return array with user ids on success or false on error
  97. *
  98. * @see BxBaseServiceContent::serviceGetUserIds
  99. */
  100. /**
  101. * @ref bx_system_general_cnt-get_user_ids "Get user's IDs"
  102. */
  103. function serviceGetUserIds($iAccountId)
  104. {
  105. $oAccount = BxDolAccount::getInstance($iAccountId);
  106. if (!$oAccount)
  107. return false;
  108. return $this->getUserIds($oAccount);
  109. }
  110. /**
  111. * @page service Service Calls
  112. * @section bx_system_general System Services
  113. * @subsection bx_system_general-content-objects Content Objects
  114. * @subsubsection bx_system_general_cnt-modules_list Get modules list
  115. *
  116. * @code bx_srv('system', 'modules_list', [], 'TemplServiceContent'); @endcode
  117. * @code {{~system:modules_list:TemplServiceContent~}} @endcode
  118. *
  119. * Get modules list with content objects, content objects fields
  120. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/modules_list/TemplServiceContent" @endcode
  121. *
  122. * @param $bContentModulesOnly if true - return only modules with content objects, if false - return all modules
  123. * @param $bNamesOnly if true - return only module names, if false - return full module info
  124. * @return content objects info array
  125. *
  126. * @see BxBaseServiceContent::serviceModulesList
  127. */
  128. /**
  129. * @ref bx_system_general_cnt-modules_list "Get modules list"
  130. */
  131. public function serviceModulesList ($bContentModulesOnly = false, $bNamesOnly = false)
  132. {
  133. $aAllowedKeys = ['title', 'type', 'uri', 'db_prefix'];
  134. $a = BxDolModuleQuery::getInstance()->getModules();
  135. $aModules = [];
  136. foreach ($a as $r) {
  137. if ($r['enabled'] != 1)
  138. continue;
  139. $isContentModule = 'system' == $r['name'] ? true : (BxDolContentInfo::getObjectInstance($r['name']) ? true : false);
  140. if ($bContentModulesOnly && !$isContentModule)
  141. continue;
  142. $aModules[$r['name']]['content_module'] = $isContentModule;
  143. foreach ($aAllowedKeys as $sKey) {
  144. if (isset($r[$sKey])) {
  145. $aModules[$r['name']][$sKey] = $r[$sKey];
  146. }
  147. }
  148. }
  149. return $bNamesOnly ? array_keys($aModules) : $aModules;
  150. }
  151. /**
  152. * @page service Service Calls
  153. * @section bx_system_general System Services
  154. * @subsection bx_system_general-content-objects Content Objects
  155. * @subsubsection bx_system_general_cnt-modules_fields Get modules fields
  156. *
  157. * @code bx_srv('system', 'content_modules_fields', [], 'TemplServiceContent'); @endcode
  158. * @code {{~system:content_modules_fields:TemplServiceContent~}} @endcode
  159. *
  160. * Get mcontent odules fields
  161. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/content_modules_fields/TemplServiceContent" @endcode
  162. *
  163. * @param $sModule module name or 'all' for all modules
  164. * @param $bVerbose if true - return all fields info, if false - return only short list of fields info
  165. * @return content objects info array
  166. *
  167. * @see BxBaseServiceContent::serviceContentModulesFields
  168. */
  169. /**
  170. * @ref bx_system_general_cnt-modules_fields "Get content fields"
  171. */
  172. public function serviceContentModulesFields ($sModule = 'all', $bVerbose = false)
  173. {
  174. $a = BxDolContentInfo::getSystems();
  175. $a['system'] = [
  176. 'db_table' => 'sys_accounts',
  177. 'db_table_fields' => BxDolDb::getInstance()->getFields('sys_accounts')['original'],
  178. ];
  179. $a['system']['db_table_fields'][] = 'auto_send_confrmation_email';
  180. if ($sModule != 'all') {
  181. if (isset($a[$sModule])) {
  182. $a = [$sModule => $a[$sModule]];
  183. }
  184. else {
  185. return ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  186. }
  187. }
  188. foreach ($a as $sSystem => $r) {
  189. if ($sSystem == 'system') {
  190. $aModules[$sSystem] = $r;
  191. continue;
  192. }
  193. if ($r['alert_action_add'] == 'commentPost') {
  194. continue;
  195. }
  196. $oModule = BxDolModule::getInstance($sSystem);
  197. if (!$oModule || !$oModule->isEnabled())
  198. continue;
  199. $oFormsHelper = $oModule->getFormsHelper();
  200. $sTableEntries = $oModule->_oConfig->CNF['TABLE_ENTRIES'];
  201. $aFields = BxDolDb::getInstance()->getFields($sTableEntries);
  202. $aModules[$sSystem] = [
  203. 'db_table' => $sTableEntries,
  204. 'db_table_fields' => $aFields['original'],
  205. 'fields_add' => $this->_prepareShortFields($oFormsHelper->getObjectFormAdd()->aInputs, $bVerbose),
  206. 'fields_edit' => $this->_prepareShortFields($oFormsHelper->getObjectFormEdit()->aInputs, $bVerbose),
  207. ];
  208. }
  209. return $aModules;
  210. }
  211. protected function _prepareShortFields($aFields, $bVerbose)
  212. {
  213. if ($bVerbose)
  214. return $aFields;
  215. $aShortList = ['type', 'name', 'caption', 'info', 'required', 'value', 'values'];
  216. $aResult = [];
  217. foreach ($aFields as $sKey => $aField) {
  218. if ($aField['type'] == 'submit')
  219. continue;
  220. $aResult[$sKey] = [];
  221. foreach ($aShortList as $sShortKey) {
  222. if (isset($aField[$sShortKey]) && !empty($aField[$sShortKey])) {
  223. $aResult[$sKey][$sShortKey] = $aField[$sShortKey];
  224. }
  225. }
  226. }
  227. return $aResult;
  228. }
  229. /**
  230. * @page service Service Calls
  231. * @section bx_system_general System Services
  232. * @subsection bx_system_general-content-objects Content Objects
  233. * @subsubsection bx_system_general_cnt-get_info Get content info
  234. *
  235. * @code bx_srv('system', 'get_info', ["bx_posts", 123], 'TemplServiceContent'); @endcode
  236. * @code {{~system:get_info:TemplServiceContent["bx_posts", 123]~}} @endcode
  237. *
  238. * Get post info by post id(123):
  239. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/get_info/TemplServiceContent&params[]=bx_posts&params[]=123" @endcode
  240. * Get account info by account id(12):
  241. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/get_info/TemplServiceContent&params[]=sys_account&params[]=12" @endcode
  242. *
  243. * @param $sContentObject content object name
  244. * @param $iContentId content id
  245. * @param $bRawInfo if true - raw info return data directly from DB, if false - returns strutured array
  246. * @return content info array on success or empty array on error
  247. *
  248. * @see BxBaseServiceContent::serviceGetInfo
  249. */
  250. /**
  251. * @ref bx_system_general_cnt-get_info "Get content info"
  252. */
  253. public function serviceGetInfo ($sContentObject, $iContentId, $bRawInfo = false)
  254. {
  255. if ('sys_account' == $sContentObject || 'system' == $sContentObject) {
  256. $o = BxDolAccount::getInstance($iContentId);
  257. if (!$o)
  258. return false;
  259. return $o->getInfo();
  260. }
  261. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  262. if (!$o)
  263. return false;
  264. if ($bRawInfo)
  265. return $o->getContentInfo($iContentId, false);
  266. else
  267. return $o->getContentInfoAPI($iContentId, false);
  268. }
  269. /**
  270. * @page service Service Calls
  271. * @section bx_system_general System Services
  272. * @subsection bx_system_general-content-objects Content Objects
  273. * @subsubsection bx_system_general_cnt-search Search content
  274. *
  275. * @code bx_srv('system', 'search', ["test"], 'TemplServiceContent'); @endcode
  276. * @code {{~system:search:TemplServiceContent["test"]~}} @endcode
  277. *
  278. * Search for posts containing the keyword "test":
  279. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/search/TemplServiceContent&params[]=test" @endcode
  280. *
  281. * @param $sKeyword keyword to search for
  282. * @param $mixedSections content sections to search in, for example ['bx_posts', 'bx_persons'], if empty - search in all sections
  283. * @param $per_page number of results per page
  284. * @param $start start showing from result number
  285. * @return content info array on success or empty array on error or not found
  286. *
  287. * @see BxBaseServiceContent::serviceSearch
  288. */
  289. /**
  290. * @ref bx_system_general_cnt-search "Search content"
  291. */
  292. public function serviceSearch (string $sKeyword, array|string $mixedSections = '', ?int $per_page = 5, ?int $start = 0): array
  293. {
  294. $sClass = bx_get_search_class_name();
  295. $o = new $sClass($mixedSections);
  296. $o->setApiOutput(true);
  297. $o->setLiveSearch(true);
  298. $o->setDataProcessing(true);
  299. $o->setCustomSearchCondition(['keyword' => $sKeyword]);
  300. $o->setCustomCurrentCondition([
  301. 'paginate' => [
  302. 'forceStart' => $start,
  303. 'perPage' => $per_page,
  304. ]
  305. ]);
  306. return $o->response();
  307. }
  308. /**
  309. * @page service Service Calls
  310. * @section bx_system_general System Services
  311. * @subsection bx_system_general-content-objects Content Objects
  312. * @subsubsection bx_system_general_cnt-get_link Get content link
  313. *
  314. * @code bx_srv('system', 'get_link', ["bx_posts", 123], 'TemplServiceContent'); @endcode
  315. * @code {{~system:get_link:TemplServiceContent["bx_posts", 123]~}} @endcode
  316. *
  317. * Get post link by post id(123):
  318. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/get_link/TemplServiceContent&params[]=bx_posts&params[]=123" @endcode
  319. *
  320. * @param $sContentObject content object name
  321. * @param $iContentId content id
  322. * @return string with link on success or empty string on error
  323. *
  324. * @see BxBaseServiceContent::serviceGetLink
  325. */
  326. /**
  327. * @ref bx_system_general_cnt-get_link "Get content link"
  328. */
  329. public function serviceGetLink ($sContentObject, $iContentId)
  330. {
  331. if ('sys_account' == $sContentObject) {
  332. return false;
  333. }
  334. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  335. if (!$o)
  336. return false;
  337. return $o->getContentLink($iContentId);
  338. }
  339. /**
  340. * @page service Service Calls
  341. * @section bx_system_general System Services
  342. * @subsection bx_system_general-content-objects Content Objects
  343. * @subsubsection bx_system_general_cnt-delete Delete content
  344. *
  345. * @code bx_srv('system', 'delete', ["bx_posts", 123], 'TemplServiceContent'); @endcode
  346. *
  347. * Delete post by post id(123):
  348. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/delete/TemplServiceContent&params[]=bx_posts&params[]=123" @endcode
  349. * Delete account with all its content by account id(12):
  350. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/delete/TemplServiceContent&params[]=sys_account&params[]=12" @endcode
  351. *
  352. * @param $sContentObject content object name
  353. * @param $iContentId content id
  354. * @param $aParams array of params:
  355. * - `with_content` - true(default) | false: for `sys_account`, `bx_persons`, `bx_organizations`;
  356. * - `force` - true | false(default): for `bx_persons`, `bx_organizations`;
  357. * - `scheduled` - true | false(default): for `sys_account`;
  358. * @return array with code = 0 on success, or array with code != 0 and error message
  359. *
  360. * @see BxBaseServiceContent::serviceDelete
  361. */
  362. /**
  363. * @ref bx_system_general_cnt-delete "Delete content"
  364. */
  365. public function serviceDelete ($sContentObject, $iContentId, $aParams = [])
  366. {
  367. if ('sys_account' == $sContentObject) {
  368. $o = BxDolAccount::getInstance($iContentId);
  369. if (!$o)
  370. return ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  371. return $o->delete(isset($aParams['with_content']) ? $aParams['with_content'] : true, isset($aParams['scheduled']) ? $aParams['scheduled'] : false);
  372. }
  373. if (in_array($sContentObject, ['bx_persons', 'bx_organizations'])) {
  374. $o = BxDolProfile::getInstanceByContentAndType($iContentId, $sContentObject);
  375. if (!$o)
  376. return ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  377. return $o->delete(false, isset($aParams['with_content']) ? $aParams['with_content'] : true, isset($aParams['force']) ? $aParams['force'] : false);
  378. }
  379. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  380. if (!$o)
  381. return ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  382. if ($sErrorMsg = $o->deleteContent($iContentId))
  383. return ['code' => 500, 'error' => $sErrorMsg];
  384. else
  385. return ['code' => 0];
  386. }
  387. /**
  388. * @page service Service Calls
  389. * @section bx_system_general System Services
  390. * @subsection bx_system_general-content-objects Content Objects
  391. * @subsubsection bx_system_general_cnt-update Update content
  392. *
  393. * @code bx_srv('system', 'update', ["bx_posts", 123, ["title" => "new title"]], 'TemplServiceContent'); @endcode
  394. *
  395. * Update post text by post id(123):
  396. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/update/TemplServiceContent&params=%5B%22bx_posts%22%2C123%2C%7B%22text%22%3A%22new%20text%22%7D%5D" @endcode
  397. * Update account email by account id(4):
  398. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/update/TemplServiceContent&params=%5B%22sys_account%22%2C4%2C%7B%22email%22%3A%22new%40email.com%22%7D%5D" @endcode
  399. * To replace existing picture (for example profile avatar) delete previous picture first,
  400. * then set new one.
  401. *
  402. * @param $sContentObject content object name
  403. * @param $iContentId content id
  404. * @param $aValues key value pairs to update
  405. * @param $sDisplay form display name to use
  406. * @param $iProfileId profile ID to perform action on behalf of
  407. * @return array with code = 0 on success, or array with code != 0 and error message
  408. *
  409. * @see BxBaseServiceContent::serviceUpdate
  410. */
  411. /**
  412. * @ref bx_system_general_cnt-update "Update content"
  413. */
  414. public function serviceUpdate ($sContentObject, $iContentId, $aValues, $sDisplay = false, $iProfileId = 0)
  415. {
  416. if ($iProfileId) {
  417. $GLOBALS['glForceCurrentProfileId'] = $iProfileId;
  418. }
  419. $a = null;
  420. if ('sys_account' == $sContentObject) {
  421. $o = BxDolAccount::getInstance($iContentId);
  422. if (!$o) {
  423. $a = ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  424. }
  425. else {
  426. $oQuery = BxDolAccountQuery::getInstance();
  427. foreach ($aValues as $k => $v) {
  428. if (!$oQuery->isFieldExists('sys_accounts', $k)) {
  429. $a = ['code' => 500, 'error' => _t('_sys_txt_forms_unknown_field_err', $k)];
  430. break;
  431. }
  432. }
  433. if (!$a) {
  434. foreach ($aValues as $k => $v) {
  435. if (!$oQuery->_updateField($o->id(), $k, $v)) {
  436. $a = ['code' => 500, 'error' => _t('_error occured')];
  437. break;
  438. }
  439. }
  440. }
  441. if (!$a) {
  442. $a = ['code' => 0]; // success
  443. }
  444. }
  445. }
  446. else {
  447. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  448. if (!$o) {
  449. $a = ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  450. }
  451. else {
  452. if ($sErrorMsg = $o->updateContent($iContentId, $aValues, $sDisplay))
  453. $a = ['code' => 500, 'error' => $sErrorMsg];
  454. else
  455. $a = ['code' => 0]; // success
  456. }
  457. }
  458. if ($iProfileId) {
  459. $GLOBALS['glForceCurrentProfileId'] = 0;
  460. }
  461. return $a;
  462. }
  463. /**
  464. * @page service Service Calls
  465. * @section bx_system_general System Services
  466. * @subsection bx_system_general-content-objects Content Objects
  467. * @subsubsection bx_system_general_cnt-add Add content
  468. *
  469. * @code bx_srv('system', 'add', [$sContentObject, $aValues], 'TemplServiceContent'); @endcode
  470. *
  471. * Add new post with specified title, text and privacy, post authour is identified by memberSession cookie:
  472. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/add/TemplServiceContent&params=%5B%22bx_posts%22%2C%7B%22title%22%3A%22Post%20title%22%2C%22text%22%3A%22Some%20text%22%2C%22cat%22%3A2%2C%22allow_view_to%22%3A3%7D%5D" @endcode
  473. * Add new account with specified name, email and password, also mark email as confirmed:
  474. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/add/TemplServiceContent&params=%5B%22sys_account%22%2C%20%7B%22name%22%3A%22Vasya%22%2C%22email%22%3A%22vasya%40vasya.com%22%2C%22email_confirmed%22%3A%221%22%2C%22password%22%3A%221234%22%7D%5D" @endcode
  475. *
  476. * @param $sContentObject content object name
  477. * @param $aValues key value pairs to add
  478. * @param $iProfileId profile ID to perform action on behalf of
  479. * @return array with code = 0 on success, or array with code != 0 and error message
  480. *
  481. * @see BxBaseServiceContent::serviceAdd
  482. */
  483. /**
  484. * @ref bx_system_general_cnt-add "Add content"
  485. */
  486. public function serviceAdd ($sContentObject, $aValues, $iProfileId = 0)
  487. {
  488. if ($iProfileId) {
  489. $GLOBALS['glForceCurrentProfileId'] = $iProfileId;
  490. }
  491. $a = [];
  492. if (bx_get_logged_profile_id() == 0) {
  493. $a = ['code' => 403, 'error' => 'Permission denied. Please login first.'];
  494. }
  495. elseif ('sys_account' == $sContentObject || 'system' == $sContentObject) {
  496. $o = new BxTemplAccountForms();
  497. $a = $o->createAccount ($aValues, BX_PROFILE_ACTION_EXTERNAL, false);
  498. if (isset($a['account_id']) && isset($aValues['email_confirmed'])) {
  499. $o = BxDolAccount::getInstance($a['account_id']);
  500. if ($o) {
  501. $o->updateEmailConfirmed($aValues['email_confirmed'], isset($aValues['auto_send_confrmation_email']) ? $aValues['auto_send_confrmation_email'] : false);
  502. }
  503. }
  504. if (isset($a['account_id']) && isset($aValues['phone_confirmed'])) {
  505. $o = BxDolAccount::getInstance($a['account_id']);
  506. if ($o) {
  507. $o->updatePhoneConfirmed($aValues['phone_confirmed']);
  508. }
  509. }
  510. }
  511. else {
  512. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  513. if (!$o) {
  514. $a = ['code' => 404, 'error' => 'Content object(module) not found'];
  515. }
  516. else {
  517. $a = $o->addContent($aValues);
  518. if (!(isset($a['code']) && !$a['code'] && isset($a['content']))) {
  519. $a['code'] = 500 + $a['code'];
  520. $a['error'] = $a['message'];
  521. $a['data'] = $a['errors'];
  522. }
  523. }
  524. }
  525. if ($iProfileId) {
  526. $GLOBALS['glForceCurrentProfileId'] = 0;
  527. }
  528. return $a;
  529. }
  530. /**
  531. * @page service Service Calls
  532. * @section bx_system_general System Services
  533. * @subsection bx_system_general-content-objects Content Objects
  534. * @subsubsection bx_system_general_cnt-upload_from_url Upload file from URL
  535. *
  536. * Upload file and associate with content:
  537. * @code bx_srv('system', 'upload_from_url', [$sContentObject, $sFileUrl, ['content_id' => 123]], 'TemplServiceContent'); @endcode
  538. *
  539. * Upload photo from `http://example.com/a.jpg` URL to `bx_persons_pictures` storage and associate
  540. * uploaded file to a person with content id (123), returned value is newly uploaded file id,
  541. * it can be used in content update API/Service call to set profile picture.
  542. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/upload_from_url/TemplServiceContent&params=%5B%22bx_persons_pictures%22%2C%22http%3A%2F%2Fexample.com%2Fa.jpg%22%2C%20%7B%22content_id%22%3A123%7D%5D" @endcode
  543. *
  544. * @param $sStorageObject storage object name
  545. * @param $sFileUrl URL to file to store in the storage
  546. * @param $aParams array of params, possible array keys:
  547. * - `private` - true | false: set file as private or not, if omitted file is uploaded as public
  548. * - `profile_id` - int: set owner of file to this user, of omitted, then currently logged in user is becoming file owner
  549. * - `content_id` - int: associate file with this content
  550. * @param $iProfileId profile id to perform action on behalf of
  551. * @return uploaded file Id on success, or array with code != 0 and error message
  552. *
  553. * @see BxBaseServiceContent::serviceUploadFromUrl
  554. */
  555. /**
  556. * @ref bx_system_general_cnt-upload_from_url "Upload file from URL"
  557. */
  558. public function serviceUploadFromUrl ($sStorageObject, $sFileUrl, $aParams = [], $iProfileId = 0)
  559. {
  560. if ($iProfileId) {
  561. $GLOBALS['glForceCurrentProfileId'] = $iProfileId;
  562. }
  563. $oStorage = BxDolStorage::getObjectInstance($sStorageObject);
  564. if (!$oStorage) {
  565. $mixedRet = ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  566. }
  567. else {
  568. $iFileId = $oStorage->storeFileFromUrl($sFileUrl, isset($aParams['private']) && $aParams['private'] ? true : false, isset($aParams['profile_id']) ? $aParams['profile_id'] : 0, isset($aParams['content_id']) ? $aParams['content_id'] : 0);
  569. $mixedRet = $iFileId ? $iFileId : ['code' => $oStorage->getErrorCode(), 'error' => $oStorage->getErrorString()];
  570. }
  571. if ($iProfileId) {
  572. $GLOBALS['glForceCurrentProfileId'] = 0;
  573. }
  574. return $mixedRet;
  575. }
  576. /**
  577. * @page service Service Calls
  578. * @section bx_system_general System Services
  579. * @subsection bx_system_general-content-objects Content Objects
  580. * @subsubsection bx_system_general_cnt-delete_file Delete file
  581. *
  582. * @code bx_srv('system', 'delete_file', ["bx_persons_pictures", 123], 'TemplServiceContent'); @endcode
  583. *
  584. * Delete file with id = 123 from `bx_persons_pictures` storage engine.
  585. * <b>Please note</b> that associated with this file id content need to be updated by setting
  586. * respective field to new file id or 0.
  587. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/delete_file/TemplServiceContent&params[]=bx_persons_pictures&params[]=123" @endcode
  588. *
  589. * @param $sStorageObject storage object name
  590. * @param $iFileId file id
  591. * @return true on success, or array with code != 0 and error message
  592. *
  593. * @see BxBaseServiceContent::serviceDeleteFile
  594. */
  595. /**
  596. * @ref bx_system_general_cnt-delete_file "Delete file"
  597. */
  598. public function serviceDeleteFile ($sStorageObject, $iFileId)
  599. {
  600. $oStorage = BxDolStorage::getObjectInstance($sStorageObject);
  601. if (!$oStorage)
  602. return ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  603. $b = $oStorage->deleteFile($iFileId);
  604. return $b ? $b : ['code' => $oStorage->getErrorCode(), 'error' => $oStorage->getErrorString()];
  605. }
  606. /**
  607. * @page service Service Calls
  608. * @section bx_system_general System Services
  609. * @subsection bx_system_general-content-objects Content Objects
  610. * @subsubsection bx_system_general_cnt-replace_file Replace file
  611. *
  612. * @code bx_srv('system', 'replace_file', ["bx_persons", 123, 'picture', 'http://a.me/i.png'], 'TemplServiceContent'); @endcode
  613. *
  614. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/replace_file/TemplServiceContent&params[]=bx_persons&params[]=123&params[]=picture&params[]=http://a.me/i.png" @endcode
  615. *
  616. * @param $sContentObject content object name
  617. * @param $iContentId content id
  618. * @param $sField field name
  619. * @param $sFileUrl file URL
  620. * @return true on success, or array with code != 0 and error message
  621. *
  622. * @see BxBaseServiceContent::serviceReplaceFile
  623. */
  624. /**
  625. * @ref bx_system_general_cnt-replace_file "Replace file"
  626. */
  627. public function serviceReplaceFile ($sContentObject, $iContentId, $sField, $sFileUrl, $sDisplay = false)
  628. {
  629. // some checks
  630. if (!isLogged())
  631. return ['code' => 500, 'error' => _t('_sys_txt_not_found')];
  632. $a = $this->serviceGetInfo ($sContentObject, $iContentId, true);
  633. if (!$a)
  634. return ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  635. $aParams = [];
  636. if ($sDisplay !== false)
  637. $aParams['display'] = $sDisplay;
  638. $oForm = bx_srv($sContentObject, 'get_object_form', ['edit', $aParams]);
  639. if (!$oForm)
  640. return ['code' => 404, 'error' => _t('_sys_request_module_not_found_cpt')];
  641. if (!isset($oForm->aInputs[$sField]))
  642. return ['code' => 404, 'error' => 'Unknown field'];
  643. if (!isset($oForm->aInputs[$sField]['storage_object']))
  644. return ['code' => 404, 'error' => "'$sField' field isn't file field (storage object isn't found in the specified field)"];
  645. // delete previous file
  646. $sStorageObject = $oForm->aInputs[$sField]['storage_object'];
  647. if ($a[$sField]) {
  648. $a = $this->serviceDeleteFile($sStorageObject, $a[$sField]);
  649. if (is_array($a) && isset($a['code']) && $a['code'])
  650. return $a;
  651. }
  652. // upload new file
  653. $iFileId = $a = $this->serviceUploadFromUrl($sStorageObject, $sFileUrl, ['content_id' => $iContentId, 'profile_id' => bx_get_logged_profile_id()]);
  654. if (is_array($a) && isset($a['code']) && $a['code'])
  655. return $a;
  656. // assign uploaded file id to the field
  657. return $this->serviceUpdate($sContentObject, $iContentId, [$sField => $iFileId], $sDisplay);
  658. }
  659. protected function getUserIds($oAccount)
  660. {
  661. $oProfile = BxDolProfile::getInstanceByAccount($oAccount->id());
  662. if (!$oProfile)
  663. return false;
  664. return [
  665. 'account_id' => $oProfile->getAccountId(),
  666. 'profile_id' => $oProfile->id(),
  667. 'content_id' => $oProfile->getContentId(),
  668. 'content_module' => $oProfile->getModule(),
  669. ];
  670. }
  671. }
  672. /** @} */