BxBaseServiceContent.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. if ($sModule != 'all') {
  179. if (isset($a[$sModule])) {
  180. $a = [$sModule => $a[$sModule]];
  181. }
  182. else {
  183. return ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  184. }
  185. }
  186. foreach ($a as $sSystem => $r) {
  187. if ($sSystem == 'system') {
  188. $aModules[$sSystem] = $r;
  189. continue;
  190. }
  191. if ($r['alert_action_add'] == 'commentPost') {
  192. continue;
  193. }
  194. $oModule = BxDolModule::getInstance($sSystem);
  195. if (!$oModule || !$oModule->isEnabled())
  196. continue;
  197. $oFormsHelper = $oModule->getFormsHelper();
  198. $sTableEntries = $oModule->_oConfig->CNF['TABLE_ENTRIES'];
  199. $aFields = BxDolDb::getInstance()->getFields($sTableEntries);
  200. $aModules[$sSystem] = [
  201. 'db_table' => $sTableEntries,
  202. 'db_table_fields' => $aFields['original'],
  203. 'fields_add' => $this->_prepareShortFields($oFormsHelper->getObjectFormAdd()->aInputs, $bVerbose),
  204. 'fields_edit' => $this->_prepareShortFields($oFormsHelper->getObjectFormEdit()->aInputs, $bVerbose),
  205. ];
  206. }
  207. return $aModules;
  208. }
  209. protected function _prepareShortFields($aFields, $bVerbose)
  210. {
  211. if ($bVerbose)
  212. return $aFields;
  213. $aShortList = ['type', 'name', 'caption', 'info', 'required', 'value', 'values'];
  214. $aResult = [];
  215. foreach ($aFields as $sKey => $aField) {
  216. if ($aField['type'] == 'submit')
  217. continue;
  218. $aResult[$sKey] = [];
  219. foreach ($aShortList as $sShortKey) {
  220. if (isset($aField[$sShortKey]) && !empty($aField[$sShortKey])) {
  221. $aResult[$sKey][$sShortKey] = $aField[$sShortKey];
  222. }
  223. }
  224. }
  225. return $aResult;
  226. }
  227. /**
  228. * @page service Service Calls
  229. * @section bx_system_general System Services
  230. * @subsection bx_system_general-content-objects Content Objects
  231. * @subsubsection bx_system_general_cnt-get_info Get content info
  232. *
  233. * @code bx_srv('system', 'get_info', ["bx_posts", 123], 'TemplServiceContent'); @endcode
  234. * @code {{~system:get_info:TemplServiceContent["bx_posts", 123]~}} @endcode
  235. *
  236. * Get post info by post id(123):
  237. * @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
  238. * Get account info by account id(12):
  239. * @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
  240. *
  241. * @param $sContentObject content object name
  242. * @param $iContentId content id
  243. * @param $bRawInfo if true - raw info return data directly from DB, if false - returns strutured array
  244. * @return content info array on success or empty array on error
  245. *
  246. * @see BxBaseServiceContent::serviceGetInfo
  247. */
  248. /**
  249. * @ref bx_system_general_cnt-get_info "Get content info"
  250. */
  251. public function serviceGetInfo ($sContentObject, $iContentId, $bRawInfo = false)
  252. {
  253. if ('sys_account' == $sContentObject || 'system' == $sContentObject) {
  254. $o = BxDolAccount::getInstance($iContentId);
  255. if (!$o)
  256. return false;
  257. return $o->getInfo();
  258. }
  259. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  260. if (!$o)
  261. return false;
  262. if ($bRawInfo)
  263. return $o->getContentInfo($iContentId, false);
  264. else
  265. return $o->getContentInfoAPI($iContentId, false);
  266. }
  267. /**
  268. * @page service Service Calls
  269. * @section bx_system_general System Services
  270. * @subsection bx_system_general-content-objects Content Objects
  271. * @subsubsection bx_system_general_cnt-get_link Get content link
  272. *
  273. * @code bx_srv('system', 'get_link', ["bx_posts", 123], 'TemplServiceContent'); @endcode
  274. * @code {{~system:get_link:TemplServiceContent["bx_posts", 123]~}} @endcode
  275. *
  276. * Get post link by post id(123):
  277. * @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
  278. *
  279. * @param $sContentObject content object name
  280. * @param $iContentId content id
  281. * @return string with link on success or empty string on error
  282. *
  283. * @see BxBaseServiceContent::serviceGetLink
  284. */
  285. /**
  286. * @ref bx_system_general_cnt-get_link "Get content link"
  287. */
  288. public function serviceGetLink ($sContentObject, $iContentId)
  289. {
  290. if ('sys_account' == $sContentObject) {
  291. return false;
  292. }
  293. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  294. if (!$o)
  295. return false;
  296. return $o->getContentLink($iContentId);
  297. }
  298. /**
  299. * @page service Service Calls
  300. * @section bx_system_general System Services
  301. * @subsection bx_system_general-content-objects Content Objects
  302. * @subsubsection bx_system_general_cnt-delete Delete content
  303. *
  304. * @code bx_srv('system', 'delete', ["bx_posts", 123], 'TemplServiceContent'); @endcode
  305. *
  306. * Delete post by post id(123):
  307. * @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
  308. * Delete account with all its content by account id(12):
  309. * @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
  310. *
  311. * @param $sContentObject content object name
  312. * @param $iContentId content id
  313. * @param $aParams array of params:
  314. * - `with_content` - true(default) | false: for `sys_account`, `bx_persons`, `bx_organizations`;
  315. * - `force` - true | false(default): for `bx_persons`, `bx_organizations`;
  316. * - `scheduled` - true | false(default): for `sys_account`;
  317. * @return array with code = 0 on success, or array with code != 0 and error message
  318. *
  319. * @see BxBaseServiceContent::serviceDelete
  320. */
  321. /**
  322. * @ref bx_system_general_cnt-delete "Delete content"
  323. */
  324. public function serviceDelete ($sContentObject, $iContentId, $aParams = [])
  325. {
  326. if ('sys_account' == $sContentObject) {
  327. $o = BxDolAccount::getInstance($iContentId);
  328. if (!$o)
  329. return false;
  330. return $o->delete(isset($aParams['with_content']) ? $aParams['with_content'] : true, isset($aParams['scheduled']) ? $aParams['scheduled'] : false);
  331. }
  332. if (in_array($sContentObject, ['bx_persons', 'bx_organizations'])) {
  333. $o = BxDolProfile::getInstanceByContentAndType($iContentId, $sContentObject);
  334. if (!$o)
  335. return false;
  336. return $o->delete(false, isset($aParams['with_content']) ? $aParams['with_content'] : true, isset($aParams['force']) ? $aParams['force'] : false);
  337. }
  338. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  339. if (!$o)
  340. return false;
  341. if ($sErrorMsg = $o->deleteContent($iContentId))
  342. return ['code' => 500, 'error' => $sErrorMsg];
  343. else
  344. return ['code' => 0];
  345. }
  346. /**
  347. * @page service Service Calls
  348. * @section bx_system_general System Services
  349. * @subsection bx_system_general-content-objects Content Objects
  350. * @subsubsection bx_system_general_cnt-update Update content
  351. *
  352. * @code bx_srv('system', 'update', ["bx_posts", 123, ["title" => "new title"]], 'TemplServiceContent'); @endcode
  353. *
  354. * Update post text by post id(123):
  355. * @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
  356. * Update account email by account id(4):
  357. * @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
  358. * To replace existing picture (for example profile avatar) delete previous picture first,
  359. * then set new one.
  360. *
  361. * @param $sContentObject content object name
  362. * @param $iContentId content id
  363. * @param $aValues key value pairs to update
  364. * @return array with code = 0 on success, or array with code != 0 and error message
  365. *
  366. * @see BxBaseServiceContent::serviceUpdate
  367. */
  368. /**
  369. * @ref bx_system_general_cnt-update "Update content"
  370. */
  371. public function serviceUpdate ($sContentObject, $iContentId, $aValues, $sDisplay = false)
  372. {
  373. if ('sys_account' == $sContentObject) {
  374. $o = BxDolAccount::getInstance($iContentId);
  375. if (!$o)
  376. return false;
  377. $oQuery = BxDolAccountQuery::getInstance();
  378. foreach ($aValues as $k => $v) {
  379. if (!$oQuery->isFieldExists('sys_accounts', $k))
  380. return ['code' => 500, 'error' => _t('_sys_txt_forms_unknown_field_err', $k)];
  381. }
  382. foreach ($aValues as $k => $v) {
  383. if (!$oQuery->_updateField($o->id(), $k, $v))
  384. return ['code' => 500, 'error' => _t('_error occured')];
  385. }
  386. return true;
  387. }
  388. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  389. if (!$o)
  390. return false;
  391. if ($sErrorMsg = $o->updateContent($iContentId, $aValues, $sDisplay))
  392. return ['code' => 500, 'error' => $sErrorMsg];
  393. else
  394. return ['code' => 0];
  395. }
  396. /**
  397. * @page service Service Calls
  398. * @section bx_system_general System Services
  399. * @subsection bx_system_general-content-objects Content Objects
  400. * @subsubsection bx_system_general_cnt-add Add content
  401. *
  402. * @code bx_srv('system', 'add', [$sContentObject, $aValues], 'TemplServiceContent'); @endcode
  403. *
  404. * Add new post with specified title, text and privacy, post authour is identified by memberSession cookie:
  405. * @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
  406. * Add new account with specified name, email and password, also mark email as confirmed:
  407. * @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
  408. *
  409. * @param $sContentObject content object name
  410. * @param $aValues key value pairs to add
  411. * @return array with code = 0 on success, or array with code != 0 and error message
  412. *
  413. * @see BxBaseServiceContent::serviceAdd
  414. */
  415. /**
  416. * @ref bx_system_general_cnt-add "Add content"
  417. */
  418. public function serviceAdd ($sContentObject, $aValues)
  419. {
  420. if ('sys_account' == $sContentObject) {
  421. $o = new BxTemplAccountForms();
  422. $a = $o->createAccount ($aValues);
  423. if (isset($a['account_id']) && isset($aValues['email_confirmed'])) {
  424. $o = BxDolAccount::getInstance($a['account_id']);
  425. if ($o) {
  426. $o->updateEmailConfirmed($aValues['email_confirmed'], isset($aValues['auto_send_confrmation_email']) ? $aValues['auto_send_confrmation_email'] : false);
  427. }
  428. }
  429. if (isset($a['account_id']) && isset($aValues['phone_confirmed'])) {
  430. $o = BxDolAccount::getInstance($a['account_id']);
  431. if ($o) {
  432. $o->updatePhoneConfirmed($aValues['phone_confirmed']);
  433. }
  434. }
  435. return $a;
  436. }
  437. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  438. if (!$o)
  439. return false;
  440. $a = $o->addContent($aValues);
  441. if (isset($a['code']) && !$a['code'] && isset($a['content'])) {
  442. return $a;
  443. } else {
  444. $a['code'] = 500 + $a['code'];
  445. $a['error'] = $a['message'];
  446. $a['data'] = $a['errors'];
  447. return $a;
  448. }
  449. }
  450. /**
  451. * @page service Service Calls
  452. * @section bx_system_general System Services
  453. * @subsection bx_system_general-content-objects Content Objects
  454. * @subsubsection bx_system_general_cnt-upload_from_url Upload file from URL
  455. *
  456. * Upload file and associate with content:
  457. * @code bx_srv('system', 'upload_from_url', [$sContentObject, $sFileUrl, ['content_id' => 123]], 'TemplServiceContent'); @endcode
  458. *
  459. * Upload photo from `http://example.com/a.jpg` URL to `bx_persons_pictures` storage and associate
  460. * uploaded file to a person with content id (123), returned value is newly uploaded file id,
  461. * it can be used in content update API/Service call to set profile picture.
  462. * @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
  463. *
  464. * @param $sStorageObject storage object name
  465. * @param $sFileUrl URL to file to store in the storage
  466. * @param $aParams array of params, possible array keys:
  467. * - `private` - true | false: set file as private or not, if omitted file is uploaded as public
  468. * - `profile_id` - int: set owner of file to this user, of omitted, then currently logged in user is becoming file owner
  469. * - `content_id` - int: associate file with this content
  470. * @return uploaded file Id on success, or array with code != 0 and error message
  471. *
  472. * @see BxBaseServiceContent::serviceUploadFromUrl
  473. */
  474. /**
  475. * @ref bx_system_general_cnt-upload_from_url "Upload file from URL"
  476. */
  477. public function serviceUploadFromUrl ($sStorageObject, $sFileUrl, $aParams = [])
  478. {
  479. $oStorage = BxDolStorage::getObjectInstance($sStorageObject);
  480. if (!$oStorage)
  481. return ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  482. $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);
  483. return $iFileId ? $iFileId : ['code' => $oStorage->getErrorCode(), 'error' => $oStorage->getErrorString()];
  484. }
  485. /**
  486. * @page service Service Calls
  487. * @section bx_system_general System Services
  488. * @subsection bx_system_general-content-objects Content Objects
  489. * @subsubsection bx_system_general_cnt-delete_file Delete file
  490. *
  491. * @code bx_srv('system', 'delete_file', ["bx_persons_pictures", 123], 'TemplServiceContent'); @endcode
  492. *
  493. * Delete file with id = 123 from `bx_persons_pictures` storage engine.
  494. * <b>Please note</b> that associated with this file id content need to be updated by setting
  495. * respective field to new file id or 0.
  496. * @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
  497. *
  498. * @param $sStorageObject storage object name
  499. * @param $iFileId file id
  500. * @return true on success, or array with code != 0 and error message
  501. *
  502. * @see BxBaseServiceContent::serviceDeleteFile
  503. */
  504. /**
  505. * @ref bx_system_general_cnt-delete_file "Delete file"
  506. */
  507. public function serviceDeleteFile ($sStorageObject, $iFileId)
  508. {
  509. $oStorage = BxDolStorage::getObjectInstance($sStorageObject);
  510. if (!$oStorage)
  511. return ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  512. $b = $oStorage->deleteFile($iFileId);
  513. return $b ? $b : ['code' => $oStorage->getErrorCode(), 'error' => $oStorage->getErrorString()];
  514. }
  515. /**
  516. * @page service Service Calls
  517. * @section bx_system_general System Services
  518. * @subsection bx_system_general-content-objects Content Objects
  519. * @subsubsection bx_system_general_cnt-replace_file Replace file
  520. *
  521. * @code bx_srv('system', 'replace_file', ["bx_persons", 123, 'picture', 'http://a.me/i.png'], 'TemplServiceContent'); @endcode
  522. *
  523. * @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
  524. *
  525. * @param $sContentObject content object name
  526. * @param $iContentId content id
  527. * @param $sField field name
  528. * @param $sFileUrl file URL
  529. * @return true on success, or array with code != 0 and error message
  530. *
  531. * @see BxBaseServiceContent::serviceReplaceFile
  532. */
  533. /**
  534. * @ref bx_system_general_cnt-replace_file "Replace file"
  535. */
  536. public function serviceReplaceFile ($sContentObject, $iContentId, $sField, $sFileUrl, $sDisplay = false)
  537. {
  538. // some checks
  539. if (!isLogged())
  540. return ['code' => 500, 'error' => _t('_sys_txt_not_found')];
  541. $a = $this->serviceGetInfo ($sContentObject, $iContentId, true);
  542. if (!$a)
  543. return ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  544. $aParams = [];
  545. if ($sDisplay !== false)
  546. $aParams['display'] = $sDisplay;
  547. $oForm = bx_srv($sContentObject, 'get_object_form', ['edit', $aParams]);
  548. if (!$oForm)
  549. return ['code' => 404, 'error' => _t('_sys_request_module_not_found_cpt')];
  550. if (!isset($oForm->aInputs[$sField]))
  551. return ['code' => 404, 'error' => 'Unknown field'];
  552. if (!isset($oForm->aInputs[$sField]['storage_object']))
  553. return ['code' => 404, 'error' => "'$sField' field isn't file field (storage object isn't found in the specified field)"];
  554. // delete previous file
  555. $sStorageObject = $oForm->aInputs[$sField]['storage_object'];
  556. if ($a[$sField]) {
  557. $a = $this->serviceDeleteFile($sStorageObject, $a[$sField]);
  558. if (is_array($a) && isset($a['code']) && $a['code'])
  559. return $a;
  560. }
  561. // upload new file
  562. $iFileId = $a = $this->serviceUploadFromUrl($sStorageObject, $sFileUrl, ['content_id' => $iContentId, 'profile_id' => bx_get_logged_profile_id()]);
  563. if (is_array($a) && isset($a['code']) && $a['code'])
  564. return $a;
  565. // assign uploaded file id to the field
  566. return $this->serviceUpdate($sContentObject, $iContentId, [$sField => $iFileId], $sDisplay);
  567. }
  568. protected function getUserIds($oAccount)
  569. {
  570. $oProfile = BxDolProfile::getInstanceByAccount($oAccount->id());
  571. if (!$oProfile)
  572. return false;
  573. return [
  574. 'account_id' => $oProfile->getAccountId(),
  575. 'profile_id' => $oProfile->id(),
  576. 'content_id' => $oProfile->getContentId(),
  577. 'content_module' => $oProfile->getModule(),
  578. ];
  579. }
  580. }
  581. /** @} */