Преглед изворни кода

Ticket #4148 - Build-in API support: Notifications.

AntonLV пре 3 година
родитељ
комит
f367558cda

+ 10 - 1
modules/boonex/notifications/classes/BxNtfsDb.php

@@ -111,12 +111,14 @@ class BxNtfsDb extends BxBaseModNotificationsDb
             WHERE 1 " . $sWhereClausePc . (!$bCountOnly ? " " . $sOrderClausePc . " " . $sLimitClause : "");
             WHERE 1 " . $sWhereClausePc . (!$bCountOnly ? " " . $sOrderClausePc . " " . $sLimitClause : "");
 
 
         //--- Combine both queries in one
         //--- Combine both queries in one
+        $bStartFromItem = isset($aParams['start_from_item']) && $aParams['start_from_item'] === true;
+
         $sUnionMethod = 'getColumn';
         $sUnionMethod = 'getColumn';
         $sUnionOrderClause = $sUnionLimitClause = '';
         $sUnionOrderClause = $sUnionLimitClause = '';
         if(!$bCountOnly) {
         if(!$bCountOnly) {
             $sUnionMethod = $sMethod;
             $sUnionMethod = $sMethod;
             $sUnionOrderClause = "ORDER BY `date` DESC, `id` DESC";
             $sUnionOrderClause = "ORDER BY `date` DESC, `id` DESC";
-            $sUnionLimitClause = isset($aParams['per_page']) ? "LIMIT " . $aParams['start'] . ", " . $aParams['per_page'] : "";
+            $sUnionLimitClause = isset($aParams['per_page']) ? "LIMIT " . (!$bStartFromItem ? $aParams['start'] : 0) . ", " . $aParams['per_page'] : "";
         }
         }
 
 
         $aAlertParams = $aParams;
         $aAlertParams = $aParams;
@@ -182,6 +184,11 @@ class BxNtfsDb extends BxBaseModNotificationsDb
     {
     {
         $sJoinClause = $sWhereClause = "";
         $sJoinClause = $sWhereClause = "";
 
 
+        //--- Apply 'start from'  filter
+        $sWhereClauseStartFrom = '';
+        if(isset($aParams['start_from_item']) && $aParams['start_from_item'] === true && $aParams['start'] != 0)
+            $sWhereClauseStartFrom = $this->prepareAsString("AND `{$this->_sTable}`.`id`<? ", (int)$aParams['start']);
+
         //--- Apply status filter
         //--- Apply status filter
         $sWhereClauseStatus = '';
         $sWhereClauseStatus = '';
         if(isset($aParams['active']))
         if(isset($aParams['active']))
@@ -299,6 +306,7 @@ class BxNtfsDb extends BxBaseModNotificationsDb
             'table' => $this->_sTable,
             'table' => $this->_sTable,
             'join_clause' => &$sJoinClause,
             'join_clause' => &$sJoinClause,
             'where_clause' => &$sWhereClause,
             'where_clause' => &$sWhereClause,
+            'where_clause_start_from' => &$sWhereClauseStartFrom,
             'where_clause_status' => &$sWhereClauseStatus,
             'where_clause_status' => &$sWhereClauseStatus,
             'where_clause_modules' => &$sWhereClauseModules,
             'where_clause_modules' => &$sWhereClauseModules,
             'where_clause_new' => &$sWhereClauseNew,
             'where_clause_new' => &$sWhereClauseNew,
@@ -306,6 +314,7 @@ class BxNtfsDb extends BxBaseModNotificationsDb
             'where_clause_type' => &$sWhereClauseType
             'where_clause_type' => &$sWhereClauseType
         ]);
         ]);
 
 
+        $sWhereClause .= $sWhereClauseStartFrom;
         $sWhereClause .= $sWhereClauseStatus;
         $sWhereClause .= $sWhereClauseStatus;
         $sWhereClause .= $sWhereClauseModules;
         $sWhereClause .= $sWhereClauseModules;
         $sWhereClause .= $sWhereClauseNew;
         $sWhereClause .= $sWhereClauseNew;

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

@@ -186,6 +186,7 @@ class BxNtfsModule extends BxBaseModNotificationsModule
         if(is_string($aParams)){
         if(is_string($aParams)){
             $aParams = json_decode($aParams, true);
             $aParams = json_decode($aParams, true);
         }
         }
+
         return $this->serviceGetBlockView($aParams['params']['type'], $aParams['params']['start'], $aParams['params']['per_page'], $aParams['params']['modules']);
         return $this->serviceGetBlockView($aParams['params']['type'], $aParams['params']['start'], $aParams['params']['per_page'], $aParams['params']['modules']);
     }
     }
     
     
@@ -206,45 +207,51 @@ class BxNtfsModule extends BxBaseModNotificationsModule
     /** 
     /** 
      * @ref bx_notifications-get_block_view "get_block_view"
      * @ref bx_notifications-get_block_view "get_block_view"
      */
      */
-    public function serviceGetBlockView($sType = '', $iStart = -1, $iPerPage = -1, $aModules = array())
+    public function serviceGetBlockView($sType = '', $iStart = -1, $iPerPage = -1, $aModules = [])
     {
     {
-    	$aBrowseTypes = array(BX_NTFS_TYPE_OBJECT_OWNER_AND_CONNECTIONS, BX_BASE_MOD_NTFS_TYPE_CONNECTIONS, BX_BASE_MOD_NTFS_TYPE_OBJECT_OWNER);
+    	$aBrowseTypes = [BX_NTFS_TYPE_OBJECT_OWNER_AND_CONNECTIONS, BX_BASE_MOD_NTFS_TYPE_CONNECTIONS, BX_BASE_MOD_NTFS_TYPE_OBJECT_OWNER];
 
 
     	if(empty($sType)) {
     	if(empty($sType)) {
-    		$mixedType = bx_get('type');
-    		if($mixedType !== false && in_array($mixedType, $aBrowseTypes))
-    			$sType = $mixedType;
+            $mixedType = bx_get('type');
+            if($mixedType !== false && in_array($mixedType, $aBrowseTypes))
+                $sType = $mixedType;
 
 
-	    	if(empty($sType))
-    			$sType = BX_NTFS_TYPE_DEFAULT;
+            if(empty($sType))
+                $sType = BX_NTFS_TYPE_DEFAULT;
     	}
     	}
 
 
     	$iOwnerId = $this->getUserId();
     	$iOwnerId = $this->getUserId();
         if(!$iOwnerId)
         if(!$iOwnerId)
-            return array('content' => MsgBox(_t('_bx_ntfs_txt_msg_no_results')));
+            return ['content' => MsgBox(_t('_bx_ntfs_txt_msg_no_results'))];
 
 
-		$aParams = $this->_prepareParams($sType, $iOwnerId, $iStart, $iPerPage, $aModules);
-		$sContent = $this->_oTemplate->getViewBlock($aParams);
-        
-        if (bx_is_api()){
+        $aParams = $this->_prepareParams($sType, $iOwnerId, $iStart, $iPerPage, $aModules);
+
+        if(bx_is_api()) {
+            $aParams['start_from_item'] = true;
+
+            $aContent = $this->_oTemplate->getPosts($aParams);
+            $iContent = count($aContent);
+            if($iContent > $aParams['per_page'])
+                $aContent = array_slice($aContent, 0, -($iContent - $aParams['per_page']));
+
+            $aParams['start'] = end($aContent)['id'];
             return [bx_api_get_block('browse', [
             return [bx_api_get_block('browse', [
                 'unit' => 'notifications',  
                 'unit' => 'notifications',  
                 'request_url' => '/api.php?r=bx_notifications/get_data/&params[]=',
                 'request_url' => '/api.php?r=bx_notifications/get_data/&params[]=',
                 'params' => $aParams,
                 'params' => $aParams,
-                'id' => bx_get_logged_profile_id(),
-                'data' => $sContent]),
+                'id' => $iOwnerId,
+                'data' => $aContent]),
             ];
             ];
         }
         }
 
 
-		$aParams['browse'] = 'first';
+        $sContent = $this->_oTemplate->getViewBlock($aParams);
+
+        $aParams['browse'] = 'first';
     	$aEvent = $this->_oDb->getEvents($aParams);
     	$aEvent = $this->_oDb->getEvents($aParams);
     	if(!empty($aEvent))
     	if(!empty($aEvent))
-			$this->_oDb->markAsRead($iOwnerId, $aEvent['id']);
-
-		$sModule = $this->_oConfig->getName();
-		$sJsObject = $this->_oConfig->getJsObject('view');
+            $this->_oDb->markAsRead($iOwnerId, $aEvent['id']);
 
 
-        return array('content' => $sContent); 
+        return ['content' => $sContent]; 
     }
     }
 
 
     /**
     /**
@@ -647,6 +654,7 @@ class BxNtfsModule extends BxBaseModNotificationsModule
             'type' => !empty($sType) ? $sType : BX_NTFS_TYPE_DEFAULT,
             'type' => !empty($sType) ? $sType : BX_NTFS_TYPE_DEFAULT,
             'owner_id' => $iOwnerId,
             'owner_id' => $iOwnerId,
             'start' => (int)$iStart > 0 ? $iStart : 0,
             'start' => (int)$iStart > 0 ? $iStart : 0,
+            'start_from_item' => false,
             'per_page' => (int)$iPerPage > 0 ? $iPerPage : $this->_oConfig->getPerPage(),
             'per_page' => (int)$iPerPage > 0 ? $iPerPage : $this->_oConfig->getPerPage(),
             'modules' => is_array($aModules) && !empty($aModules) ? $aModules : [],
             'modules' => is_array($aModules) && !empty($aModules) ? $aModules : [],
             'last_read' => $this->_oDb->getLastRead($iOwnerId),
             'last_read' => $this->_oDb->getLastRead($iOwnerId),
@@ -669,6 +677,7 @@ class BxNtfsModule extends BxBaseModNotificationsModule
 
 
         $iStart = bx_get('start');
         $iStart = bx_get('start');
         $aParams['start'] = $iStart !== false ? bx_process_input($iStart, BX_DATA_INT) : 0;
         $aParams['start'] = $iStart !== false ? bx_process_input($iStart, BX_DATA_INT) : 0;
+        $aParams['start_from_item'] = false;
 
 
         $iPerPage = bx_get('per_page');
         $iPerPage = bx_get('per_page');
         $aParams['per_page'] = $iPerPage !== false ? bx_process_input($iPerPage, BX_DATA_INT) : $this->_oConfig->getPerPage();
         $aParams['per_page'] = $iPerPage !== false ? bx_process_input($iPerPage, BX_DATA_INT) : $this->_oConfig->getPerPage();

+ 16 - 19
modules/boonex/notifications/classes/BxNtfsTemplate.php

@@ -35,22 +35,19 @@ class BxNtfsTemplate extends BxBaseModNotificationsTemplate
 
 
     public function getViewBlock($aParams)
     public function getViewBlock($aParams)
     {
     {
-        if (bx_is_api())
-            return $this->getPosts($aParams);
-        
         return $this->parseHtmlByName('block_view.html', array(
         return $this->parseHtmlByName('block_view.html', array(
-        	'html_id_view_block' => $this->_oConfig->getHtmlIds('view', 'block'),
-        	'html_id_events' => $this->_oConfig->getHtmlIds('view', 'events'),
+            'html_id_view_block' => $this->_oConfig->getHtmlIds('view', 'block'),
+            'html_id_events' => $this->_oConfig->getHtmlIds('view', 'events'),
             'style_prefix' => $this->_oConfig->getPrefix('style'),
             'style_prefix' => $this->_oConfig->getPrefix('style'),
             'content' => $this->getPosts($aParams),
             'content' => $this->getPosts($aParams),
             'js_content' => $this->getJsCode('view', array(
             'js_content' => $this->getJsCode('view', array(
-        		'oRequestParams' => array(
-	                'type' => $aParams['type'],
-	                'owner_id' => $aParams['owner_id'],
-	                'start' => $aParams['start'],
-	                'per_page' => $aParams['per_page'],
-	                'modules' => $aParams['modules']
-       			)
+                'oRequestParams' => array(
+                    'type' => $aParams['type'],
+                    'owner_id' => $aParams['owner_id'],
+                    'start' => $aParams['start'],
+                    'per_page' => $aParams['per_page'],
+                    'modules' => $aParams['modules']
+                )
             ))
             ))
         ));
         ));
     }
     }
@@ -106,19 +103,19 @@ class BxNtfsTemplate extends BxBaseModNotificationsTemplate
 
 
         if (bx_is_api())
         if (bx_is_api())
             return $aTmplVarsEvents;
             return $aTmplVarsEvents;
-        
+
         $oPaginate = new BxTemplPaginate(array(
         $oPaginate = new BxTemplPaginate(array(
-        	'start' => $aParams['start'],
+            'start' => $aParams['start'],
             'per_page' => $aParams['per_page'],
             'per_page' => $aParams['per_page'],
-        	'page_url' => $this->_oConfig->getViewUrl(),
-        	'on_change_page' => $sJsObject . ".changePage(this, {start}, {per_page})"
+            'page_url' => $this->_oConfig->getViewUrl(),
+            'on_change_page' => $sJsObject . ".changePage(this, {start}, {per_page})"
         ));
         ));
         $oPaginate->setNumFromDataArray($aTmplVarsEvents);
         $oPaginate->setNumFromDataArray($aTmplVarsEvents);
 
 
         return $this->parseHtmlByName('events.html', array(
         return $this->parseHtmlByName('events.html', array(
-        	'style_prefix' => $this->_oConfig->getPrefix('style'),
-        	'bx_repeat:events' => $aTmplVarsEvents,
-        	'paginate' => $oPaginate->getSimplePaginate()
+            'style_prefix' => $this->_oConfig->getPrefix('style'),
+            'bx_repeat:events' => $aTmplVarsEvents,
+            'paginate' => $oPaginate->getSimplePaginate()
         ));
         ));
     }
     }