Explorar o código

#4868 Content update services and API

Alex Trofimov hai 1 ano
pai
achega
6598fad414

+ 10 - 0
inc/classes/BxDolContentInfo.php

@@ -322,6 +322,16 @@ class BxDolContentInfo extends BxDolFactory implements iBxDolFactoryObject
         return $this->_call('get_search_result_extended', $aParams, $iStart, $iPerPage, $bFilterMode);
     }
 
+    public function deleteContent ($iContentId)
+    {
+        return $this->_call('delete_entity', $iContentId);
+    }
+
+    public function updateContent ($iContentId, $aValues)
+    {
+        return $this->_call('edit_entity', $iContentId, $aValues);
+    }
+
     protected function _call($sMethod)
     {
         if(!BxDolRequest::serviceExists($this->_sSystem, $sMethod))

+ 54 - 0
modules/base/general/classes/BxBaseModGeneralFormsEntryHelper.php

@@ -429,6 +429,60 @@ class BxBaseModGeneralFormsEntryHelper extends BxDolProfileForms
             $this->redirectAfterEdit($aContentInfo);
     }
 
+    /**
+     * @return empty string on success or error message string on error
+     */
+    public function editData ($iContentId, $aValues, $sDisplay = false)
+    {
+        $CNF = &$this->_oModule->_oConfig->CNF;
+
+        // get content data and profile info
+        list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
+        if (!$aContentInfo)
+            return _t('_sys_txt_error_entry_is_not_defined');
+
+        // get display form
+        $oForm = $this->getObjectFormEdit($sDisplay);
+        if (!$oForm)
+            return _t('_sys_txt_error_occured');
+
+        foreach ($aValues as $k => $v) {
+            if (!isset($oForm->aInputs[$k]))
+                return _t('_sys_txt_forms_unknown_field_err', $k);
+        }
+
+        $oForm->aFormAttrs['method'] = BX_DOL_FORM_METHOD_SPECIFIC;
+        $oForm->initChecker($aContentInfo, $aContentInfo);
+
+        // before edit hook
+        $aTrackTextFieldsChanges = null;
+        $this->onDataEditBefore ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
+
+        // update data in the DB
+        if (!$oForm->update ($aContentInfo[$CNF['FIELD_ID']], $aValues, $aTrackTextFieldsChanges)) {
+            return _t('_sys_txt_error_entry_update');
+        }
+
+        // after edit hook
+        list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
+        $sResult = $this->onDataEditAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
+        if ($sResult)
+            return $sResult;
+
+        /*
+         * Process metas.
+         * Note. It's essential to process metas a the very end, 
+         * because all data related to an entry should be already
+         * processed and are ready to be passed to alert. 
+         */
+        $this->_oModule->processMetasEdit($iContentId, $oForm);
+
+        // Create alert about the completed action.
+        $this->_oModule->alertAfterEdit($aContentInfo);
+
+        return '';
+    }
+
     protected function redirectAfterEdit($aContentInfo, $sUrl = '')
     {
         $CNF = &$this->_oModule->_oConfig->CNF;

+ 12 - 0
modules/base/general/classes/BxBaseModGeneralModule.php

@@ -1900,6 +1900,18 @@ class BxBaseModGeneralModule extends BxDolModule
         return $oFormsHelper->$sFuncDelete($iContentId);
     }
 
+    /**
+     * Edit content entry
+     * @param $iContentId content id 
+     * @param $aValues key value pairs to update
+     * @return error message or empty string on success
+     */
+    public function serviceEditEntity ($iContentId, $aValues)
+    {
+        $oFormsHelper = $this->getFormsHelper();
+        return $oFormsHelper->editData($iContentId, $aValues);
+    }
+
     /**
      * @page service Service Calls
      * @section bx_base_general Base General

+ 1 - 0
modules/boonex/english/data/langs/system/en.xml

@@ -3963,6 +3963,7 @@ If it is not enabled then please consider implement this optimization, since it
     <string name="_sys_txt_error_profile_is_not_defined"><![CDATA[Profile is not defined]]></string>
     <string name="_sys_txt_error_profile_update"><![CDATA[Profile update failed]]></string>
     <string name="_sys_txt_err_sound_not_transcoded_yet"><![CDATA[Sound file isn't transcoded yet]]></string>
+    <string name="_sys_txt_forms_unknown_field_err"><![CDATA['{0}' field is unknown]]></string>
     <string name="_sys_txt_field_created"><![CDATA[Created:]]></string>
     <string name="_sys_txt_field_updated"><![CDATA[Updated:]]></string>
     <string name="_sys_txt_forgot_pasword"><![CDATA[Forgot password?]]></string>

+ 1 - 0
modules/boonex/russian/data/langs/system/ru.xml

@@ -3896,6 +3896,7 @@
     <string name="_sys_txt_error_profile_is_not_defined"><![CDATA[Профиль не определён]]></string>
     <string name="_sys_txt_error_profile_update"><![CDATA[Не удалось обновить профиль]]></string>
     <string name="_sys_txt_err_sound_not_transcoded_yet"><![CDATA[Аудиофайл в обработке]]></string>
+    <string name="_sys_txt_forms_unknown_field_err"><![CDATA[Неизвестное поле '{0}']]></string>
     <string name="_sys_txt_forgot_pasword"><![CDATA[Забыли пароль?]]></string>
     <string name="_sys_txt_forgot_pasword_check_email"><![CDATA[Email отослан, пожалуйста, следуйте инструкциям в нём.]]></string>
     <string name="_sys_txt_forgot_pasword_check_phone"><![CDATA[SMS отослано, пожалуйста, следуйте инструкциям в нём.]]></string>

+ 103 - 0
template/scripts/BxBaseServiceContent.php

@@ -0,0 +1,103 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaBaseView UNA Base Representation Classes
+ * @{
+ */
+
+/**
+ * Services for content objects
+ */
+class BxBaseServiceContent extends BxDol
+{
+    /**
+     * @page service Service Calls
+     * @section bx_system_general System Services 
+     * @subsection bx_system_general-content-objects Content Objects
+     * @subsubsection bx_system_general-get_info Get content info
+     * 
+     * @code bx_srv('system', 'get_info', [$sContentObject, $iContentId], 'TemplServiceContent'); @endcode
+     * @code {{~system:get:TemplServiceContent["bx_posts", 123]~}} @endcode
+     * 
+     * Content
+     * @param $sContentObject content object name
+     * @param $iContentId content id
+     * @param $bRawInfo if true - raw info return data directly from DB, if false - returns strutured array
+     * @return content info array on success or false of error
+     * 
+     * @see BxBaseServiceContent::serviceGetInfo
+     */
+    /** 
+     * @ref bx_system_general-get_info "Get content"
+     */
+    public function serviceGetInfo ($sContentObject, $iContentId, $bRawInfo = false)
+    {
+        $o = BxDolContentInfo::getObjectInstance($sContentObject);
+        if (!$o)
+            return false;
+
+        if ($bRawInfo)
+            return $o->getContentInfo($iContentId, false);
+        else
+            return $o->getContentInfoAPI($iContentId, false);
+    }
+
+    /**
+     * @page service Service Calls
+     * @section bx_system_general System Services 
+     * @subsection bx_system_general-content-objects Content Objects
+     * @subsubsection bx_system_general-delete Delete content
+     * 
+     * @code bx_srv('system', 'delete', [$sContentObject, $iContentId], 'TemplServiceContent'); @endcode
+     * @code {{~system:get:TemplServiceContent["bx_posts", 123]~}} @endcode
+     * 
+     * Content
+     * @param $sContentObject content object name
+     * @param $iContentId content id
+     * @return empty string on success or error message on error
+     * 
+     * @see BxBaseServiceContent::serviceDelete
+     */
+    /** 
+     * @ref bx_system_general-delete "Delete content"
+     */
+    public function serviceDelete ($sContentObject, $iContentId)
+    {
+        $o = BxDolContentInfo::getObjectInstance($sContentObject);
+        if (!$o)
+            return false;
+        return $o->deleteContent($iContentId);
+    }
+
+    /**
+     * @page service Service Calls
+     * @section bx_system_general System Services 
+     * @subsection bx_system_general-content-objects Content Objects
+     * @subsubsection bx_system_general-update Update content
+     * 
+     * @code bx_srv('system', 'update', [$sContentObject, $iContentId, $aValues], 'TemplServiceContent'); @endcode
+     * @code {{~system:get:TemplServiceContent["bx_posts", 123, ["title" => "new title"]]~}} @endcode
+     * 
+     * Content
+     * @param $sContentObject content object name
+     * @param $iContentId content id
+     * @param $aValues key value pairs to update
+     * @return empty string on success or error message on error
+     * 
+     * @see BxBaseServiceContent::serviceUpdate
+     */
+    /** 
+     * @ref bx_system_general-update "Update content"
+     */
+    public function serviceUpdate ($sContentObject, $iContentId, $aValues)
+    {
+        $o = BxDolContentInfo::getObjectInstance($sContentObject);
+        if (!$o)
+            return false;
+        return $o->updateContent($iContentId, $aValues);
+    }
+}
+
+/** @} */

+ 17 - 0
template/scripts_templ/BxTemplServiceContent.php

@@ -0,0 +1,17 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaTemplate UNA Template Classes
+ * @{
+ */
+
+/**
+ * @see BxBaseServiceContent
+ */
+class BxTemplServiceContent extends BxBaseServiceContent
+{
+}
+
+/** @} */