Просмотр исходного кода

#4868 Content update services and API: account

Alex Trofimov 1 год назад
Родитель
Сommit
975aa4525f
2 измененных файлов с 92 добавлено и 1 удалено
  1. 29 0
      template/scripts/BxBaseAccountForms.php
  2. 63 1
      template/scripts/BxBaseServiceContent.php

+ 29 - 0
template/scripts/BxBaseAccountForms.php

@@ -187,6 +187,35 @@ class BxBaseAccountForms extends BxDolProfileForms
 
     }
 
+    public function createAccount ($aValues)
+    {
+        $oForm = $this->getObjectFormAdd ();
+        if (!$oForm)
+            return array('code' => 500, 'error' => _t('_sys_txt_error_occured'));
+
+        if (empty($aValues['email']))
+            return array('code' => 500, 'error' => _t('_Incorrect Email'));
+
+        if (BxDolAccount::getInstance($aValues['email']))
+            return array('code' => 500, 'error' => _t('_sys_form_account_input_email_uniq_error_loggedin'));
+
+        $oForm->aFormAttrs['method'] = BX_DOL_FORM_METHOD_SPECIFIC;
+        $oForm->aParams['csrf']['disable'] = true;
+ 
+        $oForm->initChecker(array(), $aValues);
+
+        $iAccountId = $oForm->insert ([]);
+        if (!$iAccountId)
+            return array('code' => 500, 'error' => _t('_sys_txt_error_account_creation'));
+
+        $iProfileId = $this->onAccountCreated($iAccountId, $oForm->isSetPendingApproval());
+
+        return [
+            'account_id' => $iAccountId,
+            'profile_id' => $iProfileId,
+        ];
+    }
+
     public function onAccountCreated ($iAccountId, $isSetPendingApproval, $iAction = BX_PROFILE_ACTION_MANUAL, $bNeedToLogin = true)
     {
         bx_alert('account', 'add', $iAccountId);

+ 63 - 1
template/scripts/BxBaseServiceContent.php

@@ -34,6 +34,13 @@ class BxBaseServiceContent extends BxDol
      */
     public function serviceGetInfo ($sContentObject, $iContentId, $bRawInfo = false)
     {
+        if ('sys_account' == $sContentObject) {
+            $o = BxDolAccount::getInstance($iContentId);
+            if (!$o)
+                return false;
+            return $o->getInfo();
+        }
+
         $o = BxDolContentInfo::getObjectInstance($sContentObject);
         if (!$o)
             return false;
@@ -65,6 +72,10 @@ class BxBaseServiceContent extends BxDol
      */
     public function serviceGetLink ($sContentObject, $iContentId)
     {
+        if ('sys_account' == $sContentObject) {
+            return false;
+        }
+
         $o = BxDolContentInfo::getObjectInstance($sContentObject);
         if (!$o)
             return false;
@@ -84,6 +95,10 @@ class BxBaseServiceContent extends BxDol
      * Content
      * @param $sContentObject content object name
      * @param $iContentId content id
+     * @param $aParams array of params: 
+     *          'with_content' - true|false: for sys_account, bx_persons, bx_organizations; 
+     *          'force' - true|false: for bx_persons, bx_organizations; 
+     *          'scheduled' - true|false: for sys_account;
      * @return array with code = 0 on success, or array with code != 0 and error message
      * 
      * @see BxBaseServiceContent::serviceDelete
@@ -91,8 +106,21 @@ class BxBaseServiceContent extends BxDol
     /** 
      * @ref bx_system_general-delete "Delete content"
      */
-    public function serviceDelete ($sContentObject, $iContentId)
+    public function serviceDelete ($sContentObject, $iContentId, $aParams = [])
     {
+        if ('sys_account' == $sContentObject) {
+            $o = BxDolAccount::getInstance($iContentId);
+            if (!$o)
+                return false;
+            return $o->delete(isset($aParams['with_content']) ? $aParams['with_content'] : true, isset($aParams['scheduled']) ? $aParams['scheduled'] : false);
+        }
+        if (in_array($sContentObject, ['bx_persons', 'bx_organizations'])) {
+            $o = BxDolProfile::getInstanceByContentAndType($iContentId, $sContentObject);
+            if (!$o)
+                return false;
+            return $o->delete(false, isset($aParams['with_content']) ? $aParams['with_content'] : true, isset($aParams['force']) ? $aParams['force'] : false);
+        }
+
         $o = BxDolContentInfo::getObjectInstance($sContentObject);
         if (!$o)
             return false;
@@ -124,6 +152,22 @@ class BxBaseServiceContent extends BxDol
      */
     public function serviceUpdate ($sContentObject, $iContentId, $aValues)
     {
+        if ('sys_account' == $sContentObject) {
+            $o = BxDolAccount::getInstance($iContentId);
+            if (!$o)
+                return false;
+            $oQuery = BxDolAccountQuery::getInstance();
+            foreach ($aValues as $k => $v) {
+                if (!$oQuery->isFieldExists('sys_accounts', $k))
+                    return ['code' => 500, 'error' => _t('_sys_txt_forms_unknown_field_err', $k)];
+            }
+            foreach ($aValues as $k => $v) {
+                if (!$oQuery->_updateField($o->id(), $k, $v))
+                    return ['code' => 500, 'error' => _t('_error occured')];
+            }
+            return true;
+        }
+
         $o = BxDolContentInfo::getObjectInstance($sContentObject);
         if (!$o)
             return false;
@@ -154,6 +198,24 @@ class BxBaseServiceContent extends BxDol
      */
     public function serviceAdd ($sContentObject, $aValues)
     {
+        if ('sys_account' == $sContentObject) {
+            $o = new BxTemplAccountForms();
+            $a = $o->createAccount ($aValues);
+            if (isset($a['account_id']) && isset($aValues['email_confirmed'])) {
+                $o = BxDolAccount::getInstance($a['account_id']);
+                if ($o) {
+                    $o->updateEmailConfirmed($aValues['email_confirmed'], isset($aValues['auto_send_confrmation_email']) ? $aValues['auto_send_confrmation_email'] : false);
+                }
+            }
+            if (isset($a['account_id']) && isset($aValues['phone_confirmed'])) {
+                $o = BxDolAccount::getInstance($a['account_id']);
+                if ($o) {
+                    $o->updatePhoneConfirmed($aValues['phone_confirmed']);
+                }
+            }
+            return $a;
+        }
+
         $o = BxDolContentInfo::getObjectInstance($sContentObject);
         if (!$o)
             return false;