Переглянути джерело

Ticket #4558 - Build-in API support: Notifications settings.

AntonLV 2 роки тому
батько
коміт
70192508de

+ 12 - 0
modules/boonex/notifications/classes/BxNtfsConfig.php

@@ -27,6 +27,7 @@ class BxNtfsConfig extends BxBaseModNotificationsConfig
      */
      */
     protected $_bSettingsGrouped;
     protected $_bSettingsGrouped;
     protected $_aSettingsTypes;
     protected $_aSettingsTypes;
+    protected $_aDeliveryTypes;
 
 
     protected $_aModulesProfiles;
     protected $_aModulesProfiles;
     protected $_aModulesContexts;
     protected $_aModulesContexts;
@@ -93,6 +94,12 @@ class BxNtfsConfig extends BxBaseModNotificationsConfig
             //BX_NTFS_STYPE_OTHER           //TODO: May be it can be removed, because there is no events(alerts) for this type.
             //BX_NTFS_STYPE_OTHER           //TODO: May be it can be removed, because there is no events(alerts) for this type.
         );
         );
 
 
+        $this->_aDeliveryTypes = [
+            BX_NTFS_DTYPE_SITE,
+            BX_NTFS_DTYPE_EMAIL,
+            BX_NTFS_DTYPE_PUSH
+        ];
+
         $this->_aModulesProfiles = false;
         $this->_aModulesProfiles = false;
         $this->_aModulesContexts = false;
         $this->_aModulesContexts = false;
 
 
@@ -193,6 +200,11 @@ class BxNtfsConfig extends BxBaseModNotificationsConfig
     {
     {
         return $this->_aSettingsTypes;
         return $this->_aSettingsTypes;
     }
     }
+    
+    public function getDeliveryTypes()
+    {
+        return $this->_aDeliveryTypes;
+    }
 
 
     public function getDeliveryTimeout()
     public function getDeliveryTimeout()
     {
     {

+ 56 - 1
modules/boonex/notifications/classes/BxNtfsGridSettingsAdministration.php

@@ -98,7 +98,7 @@ class BxNtfsGridSettingsAdministration extends BxTemplGrid
         if(!$this->_bGrouped)
         if(!$this->_bGrouped)
             $mixedResult = $this->_oModule->_oDb->updateSetting(array('value' => $iValue), array('id' => $iId));
             $mixedResult = $this->_oModule->_oDb->updateSetting(array('value' => $iValue), array('id' => $iId));
         else 
         else 
-            $mixedResult = $this->_oModule->_changeSettingsValueLike($iId, 'value', $iValue, $this->_bAdministration);
+            $mixedResult = $this->_oModule->changeSettingsValueLike($iId, 'value', $iValue, $this->_bAdministration);
 
 
         $aResult = array();
         $aResult = array();
         if($mixedResult === false)
         if($mixedResult === false)
@@ -203,6 +203,28 @@ class BxNtfsGridSettingsAdministration extends BxTemplGrid
     	return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
     	return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
     }
     }
 
 
+    protected function _getData ($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
+    {
+        if(!$this->_bIsApi)
+            return parent::_getData($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
+        
+        $sSource = $this->_aOptions['source'];
+
+        $aData = [];
+        $aDeliveryTypes = $this->_oModule->_oConfig->getDeliveryTypes();
+        foreach($aDeliveryTypes as $sDeliveryType) {
+            $this->_sDeliveryType = $sDeliveryType;
+            $this->_aOptions['source'] = $sSource;
+
+            $aData[] = [
+                'delivery' => $sDeliveryType,
+                'items' => parent::_getData($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
+            ];
+        }
+
+        return $aData;
+    }
+
     protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
     protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
     {
     {
         $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `ts`.`delivery`=?", $this->_sDeliveryType);
         $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `ts`.`delivery`=?", $this->_sDeliveryType);
@@ -251,6 +273,39 @@ class BxNtfsGridSettingsAdministration extends BxTemplGrid
 
 
         return $iEnabled == 0 || $iDisabled == 0;
         return $iEnabled == 0 || $iDisabled == 0;
     }
     }
+    
+    public function decodeDataAPI($aData)
+    {
+        $sKeyId = $this->_aOptions['field_id'];
+        $sMethodDefault = '_getCellDefault';
+
+        $aDataR = [];
+        foreach($aData as $aDataItem) {
+            $aDataS = [];
+            foreach($aDataItem['items'] as $iKey => $aRow) {
+                if(!is_string($aRow)) {
+                    $aDataS[$iKey] = [$sKeyId => $aRow[$sKeyId]];
+
+                    foreach($this->_aOptions['fields'] as $sKey => $aField) {
+                        $sMethod = '_getCell' . $this->_genMethodName($sKey);
+                        if(!method_exists($this, $sMethod))
+                            $sMethod = $sMethodDefault;
+
+                        $aDataS[$iKey][$sKey] = $this->$sMethod(isset($aRow[$sKey]) ? $aRow[$sKey] : _t('_undefined'), $sKey, $aField, $aRow);
+                    }
+                }
+                else
+                    $aDataS[$iKey] = $aRow;
+            }
+
+            $aDataR[] = [
+                'delivery' => $aDataItem['delivery'],
+                'items' => $aDataS
+            ];
+        }
+
+        return $aDataR;
+    }
 }
 }
 
 
 /** @} */
 /** @} */

+ 29 - 1
modules/boonex/notifications/classes/BxNtfsModule.php

@@ -26,6 +26,13 @@ define('BX_NTFS_STYPE_FOLLOW_MEMBER', 'follow_member');
 define('BX_NTFS_STYPE_FOLLOW_CONTEXT', 'follow_context');
 define('BX_NTFS_STYPE_FOLLOW_CONTEXT', 'follow_context');
 define('BX_NTFS_STYPE_OTHER', 'other');
 define('BX_NTFS_STYPE_OTHER', 'other');
 
 
+/**
+ * DTYPE - Delivery Type
+ */
+define('BX_NTFS_DTYPE_SITE', 'site');
+define('BX_NTFS_DTYPE_EMAIL', 'email');
+define('BX_NTFS_DTYPE_PUSH', 'push');
+
 /**
 /**
  * SLTMODE - Silent mode:
  * SLTMODE - Silent mode:
  * It is needed for alert sending module to tell that the alert should be ignored 
  * It is needed for alert sending module to tell that the alert should be ignored 
@@ -99,6 +106,7 @@ class BxNtfsModule extends BxBaseModNotificationsModule
             'GetUnreadNotificationsNum' => '',
             'GetUnreadNotificationsNum' => '',
             'GetUnreadNotificationsNumEx' => '',
             'GetUnreadNotificationsNumEx' => '',
             'MarkAsRead' => '',
             'MarkAsRead' => '',
+            'ChangeSetting' => '',
         ];
         ];
     }
     }
 
 
@@ -557,7 +565,7 @@ class BxNtfsModule extends BxBaseModNotificationsModule
         return $mixedResult;
         return $mixedResult;
     }
     }
 
 
-    public function _changeSettingsValueLike($iId, $sField, $mixedValue, $bAdministration = false)
+    public function changeSettingsValueLike($iId, $sField, $mixedValue, $bAdministration = false)
     {
     {
         $aSetting = $this->_oDb->getSetting(array(
         $aSetting = $this->_oDb->getSetting(array(
             'by' => $bAdministration ? 'id' : 'tsu_id', 
             'by' => $bAdministration ? 'id' : 'tsu_id', 
@@ -681,6 +689,26 @@ class BxNtfsModule extends BxBaseModNotificationsModule
         return $this->_oDb->markAsRead($iOwnerId, $aEvent['id']) !== false;
         return $this->_oDb->markAsRead($iOwnerId, $aEvent['id']) !== false;
     }
     }
 
 
+    public function serviceChangeSetting($mixedParams)
+    {
+        if(!is_array($mixedParams))
+            $mixedParams = json_decode($mixedParams, true);
+
+        if(!isset($mixedParams['id'], $mixedParams['value']))
+            return false;
+
+        $iId = (int)$mixedParams['id'];
+        $iValue = (int)$mixedParams['value'];
+        $bAdministration = isset($mixedParams['admin']) && $mixedParams['admin'];
+
+        if(!$this->_oConfig->isSettingsGrouped())
+            $mixedResult = $this->_oDb->updateSetting(['value' => $iValue], ['id' => $iId]);
+        else 
+            $mixedResult = $this->changeSettingsValueLike($iId, 'value', $iValue, $bAdministration);
+
+        return $mixedResult !== false;
+    }
+
     /*
     /*
      * INTERNAL METHODS
      * INTERNAL METHODS
      */
      */

+ 3 - 1
modules/boonex/notifications/classes/BxNtfsTemplate.php

@@ -68,7 +68,9 @@ class BxNtfsTemplate extends BxBaseModNotificationsTemplate
         if(method_exists($oGrid, 'setUserId'))
         if(method_exists($oGrid, 'setUserId'))
             $oGrid->setUserId($iUserId);
             $oGrid->setUserId($iUserId);
 
 
-        return $oGrid->getCode();
+        return !$this->_bIsApi ? $oGrid->getCode() : [
+            bx_api_get_block('notifications_settings', $oGrid->getCodeApi(), ['ext' => ['request_url' => '/api.php?r=' . $this->_oConfig->getName() . '/change_setting/&params[]=']])
+        ];
     }
     }
 
 
     public function getPosts($aParams)
     public function getPosts($aParams)

+ 3 - 3
template/scripts/BxBaseGrid.php

@@ -297,7 +297,7 @@ class BxBaseGrid extends BxDolGrid
      * Get grid code API.
      * Get grid code API.
      * @return array
      * @return array
      */
      */
-    public function getCodeAPI()
+    public function getCodeAPI($bForceReturn = false)
     {
     {
         $this->_replaceMarkers();
         $this->_replaceMarkers();
 
 
@@ -339,7 +339,7 @@ class BxBaseGrid extends BxDolGrid
                 'query_append' => $aQueryAppend
                 'query_append' => $aQueryAppend
             ],
             ],
             'header' => $this->_getRowHeadAPI(),
             'header' => $this->_getRowHeadAPI(),
-            'data' => defined('BX_API_PAGE') ? [] : $aData,
+            'data' => defined('BX_API_PAGE') && !$bForceReturn ? [] : $aData,
             'actions' => [
             'actions' => [
                 'independent' => $this->_getActionsAPI('independent'), 
                 'independent' => $this->_getActionsAPI('independent'), 
                 'bulk' => $this->_getActionsAPI('bulk')
                 'bulk' => $this->_getActionsAPI('bulk')
@@ -702,7 +702,7 @@ class BxBaseGrid extends BxDolGrid
     protected function _getCellDefault ($mixedValue, $sKey, $aField, $aRow)
     protected function _getCellDefault ($mixedValue, $sKey, $aField, $aRow)
     {
     {
         if($this->_bIsApi)
         if($this->_bIsApi)
-            return ['type' => 'text', 'value'=> $mixedValue];    
+            return ['type' => 'text', 'value'=> (int)$aField['translatable'] ? _t($mixedValue) : $mixedValue];    
 
 
         $sAttr = $this->_convertAttrs(
         $sAttr = $this->_convertAttrs(
             $aField, 'attr_cell',
             $aField, 'attr_cell',