BxBaseServiceContent.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. /**
  15. * @page service Service Calls
  16. * @section bx_system_general System Services
  17. * @subsection bx_system_general-content-objects Content Objects
  18. * @subsubsection bx_system_general-get_info Get content info
  19. *
  20. * @code bx_srv('system', 'get_info', [$sContentObject, $iContentId], 'TemplServiceContent'); @endcode
  21. * @code {{~system:get_info:TemplServiceContent["bx_posts", 123]~}} @endcode
  22. *
  23. * Content
  24. * @param $sContentObject content object name
  25. * @param $iContentId content id
  26. * @param $bRawInfo if true - raw info return data directly from DB, if false - returns strutured array
  27. * @return content info array on success or empty array on error
  28. *
  29. * @see BxBaseServiceContent::serviceGetInfo
  30. */
  31. /**
  32. * @ref bx_system_general-get_info "Get content"
  33. */
  34. public function serviceGetInfo ($sContentObject, $iContentId, $bRawInfo = false)
  35. {
  36. if ('sys_account' == $sContentObject) {
  37. $o = BxDolAccount::getInstance($iContentId);
  38. if (!$o)
  39. return false;
  40. return $o->getInfo();
  41. }
  42. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  43. if (!$o)
  44. return false;
  45. if ($bRawInfo)
  46. return $o->getContentInfo($iContentId, false);
  47. else
  48. return $o->getContentInfoAPI($iContentId, false);
  49. }
  50. /**
  51. * @page service Service Calls
  52. * @section bx_system_general System Services
  53. * @subsection bx_system_general-content-objects Content Objects
  54. * @subsubsection bx_system_general-get_link Get content link
  55. *
  56. * @code bx_srv('system', 'get_link', [$sContentObject, $iContentId], 'TemplServiceContent'); @endcode
  57. * @code {{~system:get_link:TemplServiceContent["bx_posts", 123]~}} @endcode
  58. *
  59. * Content
  60. * @param $sContentObject content object name
  61. * @param $iContentId content id
  62. * @return string with link on success or empty string on error
  63. *
  64. * @see BxBaseServiceContent::serviceGetLink
  65. */
  66. /**
  67. * @ref bx_system_general-get_link "Get content link"
  68. */
  69. public function serviceGetLink ($sContentObject, $iContentId)
  70. {
  71. if ('sys_account' == $sContentObject) {
  72. return false;
  73. }
  74. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  75. if (!$o)
  76. return false;
  77. return $o->getContentLink($iContentId);
  78. }
  79. /**
  80. * @page service Service Calls
  81. * @section bx_system_general System Services
  82. * @subsection bx_system_general-content-objects Content Objects
  83. * @subsubsection bx_system_general-delete Delete content
  84. *
  85. * @code bx_srv('system', 'delete', [$sContentObject, $iContentId], 'TemplServiceContent'); @endcode
  86. * @code {{~system:get:TemplServiceContent["bx_posts", 123]~}} @endcode
  87. *
  88. * Content
  89. * @param $sContentObject content object name
  90. * @param $iContentId content id
  91. * @param $aParams array of params:
  92. * 'with_content' - true|false: for sys_account, bx_persons, bx_organizations;
  93. * 'force' - true|false: for bx_persons, bx_organizations;
  94. * 'scheduled' - true|false: for sys_account;
  95. * @return array with code = 0 on success, or array with code != 0 and error message
  96. *
  97. * @see BxBaseServiceContent::serviceDelete
  98. */
  99. /**
  100. * @ref bx_system_general-delete "Delete content"
  101. */
  102. public function serviceDelete ($sContentObject, $iContentId, $aParams = [])
  103. {
  104. if ('sys_account' == $sContentObject) {
  105. $o = BxDolAccount::getInstance($iContentId);
  106. if (!$o)
  107. return false;
  108. return $o->delete(isset($aParams['with_content']) ? $aParams['with_content'] : true, isset($aParams['scheduled']) ? $aParams['scheduled'] : false);
  109. }
  110. if (in_array($sContentObject, ['bx_persons', 'bx_organizations'])) {
  111. $o = BxDolProfile::getInstanceByContentAndType($iContentId, $sContentObject);
  112. if (!$o)
  113. return false;
  114. return $o->delete(false, isset($aParams['with_content']) ? $aParams['with_content'] : true, isset($aParams['force']) ? $aParams['force'] : false);
  115. }
  116. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  117. if (!$o)
  118. return false;
  119. if ($sErrorMsg = $o->deleteContent($iContentId))
  120. return ['code' => 500, 'error' => $sErrorMsg];
  121. else
  122. return ['code' => 0];
  123. }
  124. /**
  125. * @page service Service Calls
  126. * @section bx_system_general System Services
  127. * @subsection bx_system_general-content-objects Content Objects
  128. * @subsubsection bx_system_general-update Update content
  129. *
  130. * @code bx_srv('system', 'update', [$sContentObject, $iContentId, $aValues], 'TemplServiceContent'); @endcode
  131. * @code {{~system:get:TemplServiceContent["bx_posts", 123, ["title" => "new title"]]~}} @endcode
  132. *
  133. * Content
  134. * @param $sContentObject content object name
  135. * @param $iContentId content id
  136. * @param $aValues key value pairs to update
  137. * @return array with code = 0 on success, or array with code != 0 and error message
  138. *
  139. * @see BxBaseServiceContent::serviceUpdate
  140. */
  141. /**
  142. * @ref bx_system_general-update "Update content"
  143. */
  144. public function serviceUpdate ($sContentObject, $iContentId, $aValues)
  145. {
  146. if ('sys_account' == $sContentObject) {
  147. $o = BxDolAccount::getInstance($iContentId);
  148. if (!$o)
  149. return false;
  150. $oQuery = BxDolAccountQuery::getInstance();
  151. foreach ($aValues as $k => $v) {
  152. if (!$oQuery->isFieldExists('sys_accounts', $k))
  153. return ['code' => 500, 'error' => _t('_sys_txt_forms_unknown_field_err', $k)];
  154. }
  155. foreach ($aValues as $k => $v) {
  156. if (!$oQuery->_updateField($o->id(), $k, $v))
  157. return ['code' => 500, 'error' => _t('_error occured')];
  158. }
  159. return true;
  160. }
  161. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  162. if (!$o)
  163. return false;
  164. if ($sErrorMsg = $o->updateContent($iContentId, $aValues))
  165. return ['code' => 500, 'error' => $sErrorMsg];
  166. else
  167. return ['code' => 0];
  168. }
  169. /**
  170. * @page service Service Calls
  171. * @section bx_system_general System Services
  172. * @subsection bx_system_general-content-objects Content Objects
  173. * @subsubsection bx_system_general-add Add content
  174. *
  175. * @code bx_srv('system', 'add', [$sContentObject, $aValues], 'TemplServiceContent'); @endcode
  176. * @code {{~system:get:TemplServiceContent["bx_posts", ["title" => "new title", "text" => "new text"]]~}} @endcode
  177. *
  178. * Content
  179. * @param $sContentObject content object name
  180. * @param $aValues key value pairs to add
  181. * @return array with code = 0 on success, or array with code != 0 and error message
  182. *
  183. * @see BxBaseServiceContent::serviceAdd
  184. */
  185. /**
  186. * @ref bx_system_general-add "Add content"
  187. */
  188. public function serviceAdd ($sContentObject, $aValues)
  189. {
  190. if ('sys_account' == $sContentObject) {
  191. $o = new BxTemplAccountForms();
  192. $a = $o->createAccount ($aValues);
  193. if (isset($a['account_id']) && isset($aValues['email_confirmed'])) {
  194. $o = BxDolAccount::getInstance($a['account_id']);
  195. if ($o) {
  196. $o->updateEmailConfirmed($aValues['email_confirmed'], isset($aValues['auto_send_confrmation_email']) ? $aValues['auto_send_confrmation_email'] : false);
  197. }
  198. }
  199. if (isset($a['account_id']) && isset($aValues['phone_confirmed'])) {
  200. $o = BxDolAccount::getInstance($a['account_id']);
  201. if ($o) {
  202. $o->updatePhoneConfirmed($aValues['phone_confirmed']);
  203. }
  204. }
  205. return $a;
  206. }
  207. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  208. if (!$o)
  209. return false;
  210. $a = $o->addContent($aValues);
  211. if (isset($a['code']) && !$a['code'] && isset($a['content'])) {
  212. return $a;
  213. } else {
  214. $a['code'] = 500 + $a['code'];
  215. $a['error'] = $a['message'];
  216. $a['data'] = $a['errors'];
  217. return $a;
  218. }
  219. }
  220. }
  221. /** @} */