BxBaseServiceContent.php 30 KB

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