فهرست منبع

Ticket #4558 - Build-in API support: Invitations.

AntonLV 2 سال پیش
والد
کامیت
bd2d584b4c
2فایلهای تغییر یافته به همراه168 افزوده شده و 104 حذف شده
  1. 101 64
      modules/boonex/invites/classes/BxInvGridInvites.php
  2. 67 40
      modules/boonex/invites/classes/BxInvGridRequests.php

+ 101 - 64
modules/boonex/invites/classes/BxInvGridInvites.php

@@ -42,53 +42,48 @@ class BxInvGridInvites extends BxTemplGrid
         }
         $this->_sParamsDivider = '#-#';
     }
-    
-    protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
+
+    public function getFormCallBackUrlAPI($sAction, $iId = 0)
     {
-        $CNF = $this->_oModule->_oConfig->CNF;
-        $sTableInvites = $CNF['TABLE_INVITES'];
-        
-        if(strpos($sFilter, $this->_sParamsDivider) !== false)
-            list($this->_sFilter1Value, $sFilter) = explode($this->_sParamsDivider, $sFilter);
-        
-        $sFilterSql = "";
-        if(isset($this->_sFilter1Value) && $this->_sFilter1Value != ''){
-            if ($this->_sFilter1Value == BX_INV_FILTER_STATUS_ACCEPTED){
-                $sFilterSql = " AND " . $sTableInvites . ".`joined_account_id` IS NOT NULL";
-            }
-            else{
-                $sFilterSql = " AND " . $sTableInvites . ".`joined_account_id` IS NULL";
-            }
-        }
-        $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString($sFilterSql);
-        return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
+         return '/api.php?r=system/perfom_action_api/TemplServiceGrid/&params[]=&o=' . $this->_sObject . '&a=' . $sAction;
     }
     
     public function performActionAdd()
     {
         $sAction = 'add';
+
         $oForm = $this->_oModule->getFormObjectInvite();
-        if (!$oForm)
-            return '';
+        if(!$oForm)
+            return $this->_getActionResult([]);
 
         $oForm->aInputs['text']['value'] = _t('_bx_invites_msg_invitation');
         $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?' . bx_encode_url_params($_GET, array('ids', '_r'));
         $oForm->initChecker();
-        
+
+        $aResult = [];
         if($oForm->isSubmittedAndValid()) {
             $sResult = $this->_oModule->processFormObjectInvite($oForm);
-            $aRes = array('msg' => $sResult);
-            echoJson($aRes);
+            if($this->_bIsApi)
+                $aResult = [bx_api_get_msg($sResult)];
+            else
+                $aResult = ['msg' => $sResult];
         }
         else {
-            $sContent = BxTemplFunctions::getInstance()->popupBox('_bx_invites_form_invite', _t('_bx_invites_form_invite'), $this->_oModule->_oTemplate->parseHtmlByName('popup_invite.html', array(
-                'form_id' => $oForm->id,
-                'form' => $oForm->getCode(true),
-                'object' => $this->_sObject,
-                'action' => $sAction
-            )));
-            echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => true))));
+            if($this->_bIsApi)
+                $aResult = $this->getFormBlockAPI($oForm, $sAction);
+            else
+                $aResult = ['popup' => [
+                    'html' => BxTemplFunctions::getInstance()->popupBox('_bx_invites_form_invite', _t('_bx_invites_form_invite'), $this->_oModule->_oTemplate->parseHtmlByName('popup_invite.html', array(
+                        'form_id' => $oForm->id,
+                        'form' => $oForm->getCode(true),
+                        'object' => $this->_sObject,
+                        'action' => $sAction
+                    ))), 
+                    'options' => ['closeOnOuterClick' => true]
+                ]];
         }
+
+        return $this->_getActionResult($aResult);
     }
     
     public function performActionDelete($aParams = array())
@@ -173,62 +168,83 @@ class BxInvGridInvites extends BxTemplGrid
     
     protected function _getCellDate($mixedValue, $sKey, $aField, $aRow)
     {
+        if($this->_bIsApi)
+            return ['type' => 'time', 'data' => $mixedValue];
+
         return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
     }
     
     protected function _getCellDateSeen($mixedValue, $sKey, $aField, $aRow)
     {
-        if ($mixedValue == 'undefined')
+        if($this->_bIsApi)
+            return ['type' => 'time', 'data' => (int)$mixedValue];
+
+        if($mixedValue == 'undefined')
             return parent::_getCellDefault('', $sKey, $aField, $aRow);
+
         return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
     }
     
     protected function _getCellDateJoined($mixedValue, $sKey, $aField, $aRow)
     {
-        if ($mixedValue == 'undefined')
+        if($this->_bIsApi)
+            return ['type' => 'time', 'data' => (int)$mixedValue];
+
+        if($mixedValue == 'undefined')
             return parent::_getCellDefault('', $sKey, $aField, $aRow);
+
         return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
     }
     
     protected function _getCellWhoSend($mixedValue, $sKey, $aField, $aRow)
     {
-        $oProfile = BxDolProfile::getInstanceMagic($aRow['profile_id']);
-        if ($oProfile)
+        if(($oProfile = BxDolProfile::getInstanceMagic($aRow['profile_id'])) !== false) {
+            if ($this->_bIsApi)
+                return ['type' => 'profile', 'data' => BxDolProfile::getData($aRow['profile_id'])];
+
             return $this->_getProfileCell($oProfile, $sKey, $aField, $aRow);
-       
+        }
+
         return parent::_getCellDefault('', $sKey, $aField, $aRow);
     }
     
     protected function _getCellJoinedAccount($mixedValue, $sKey, $aField, $aRow)
     {
-        if ($aRow['joined_account_id'] == '')
-            return parent::_getCellDefault('', $sKey, $aField, $aRow);
-        
-        $oProfile = BxDolProfile::getInstanceAccountProfile($aRow['joined_account_id']);
-        if ($oProfile)
+        if(($iAccountId = (int)$aRow['joined_account_id']) != 0 && ($oProfile = BxDolProfile::getInstanceAccountProfile($iAccountId)) !== false) {
+            if($this->_bIsApi)
+                return ['type' => 'profile', 'data' => BxDolProfile::getData($oProfile)];
+
             return $this->_getProfileCell($oProfile, $sKey, $aField, $aRow);
-        
+        }
+
         return parent::_getCellDefault('', $sKey, $aField, $aRow);
     }
     
     protected function _getCellRequest($mixedValue, $sKey, $aField, $aRow)
     {
-        if ($aRow['request_id'] == '')
+        $CNF = &$this->_oModule->_oConfig->CNF;
+
+        if($aRow['request_id'] == '')
             return parent::_getCellDefault('', $sKey, $aField, $aRow);
         
-        $aRequest = $this->_oModule->_oDb->getRequests(array('type' => 'by_id', 'value' => $aRow['request_id']));
-        if ($aRequest || true){
-            $CNF = &$this->_oModule->_oConfig->CNF;
-            $sLink = bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_REQUESTS']));
-            $sLink = bx_append_url_params($sLink, array('filter' => $aRequest['email']));
-            $sAddon = $this->_oTemplate->parseHtmlByName('account_link.html', array(
-                    'href' => $sLink,
-                    'title' => _t('_bx_invites_grid_txt_view_request'),
-                    'content' => _t('_bx_invites_grid_txt_view_request'),
-                    'class' => ''
-            ));
+        $aRequest = $this->_oModule->_oDb->getRequests(['type' => 'by_id', 'value' => $aRow['request_id']]);
+        if(!empty($aRequest) && is_array($aRequest)) {
+            $sTitle = _t('_bx_invites_grid_txt_view_request');
+            $sLink = bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_REQUESTS'], ['filter' => $aRequest['email']]));
+
+            if($this->_bIsApi)
+                return ['type' => 'link', 'data' => ['url' => bx_api_get_relative_url($sLink), 'text' => $sTitle]];
+
+            $sAddon = $this->_oTemplate->parseHtmlByName('account_link.html', [
+                'href' => $sLink,
+                'title' => bx_html_attribute($sTitle),
+                'content' => $sTitle,
+                'class' => ''
+            ]);
+
             return parent::_getCellDefault($sAddon, $sKey, $aField, $aRow);
         }
+
         return parent::_getCellDefault('', $sKey, $aField, $aRow);
     }
     
@@ -263,20 +279,41 @@ class BxInvGridInvites extends BxTemplGrid
     {
     	$sModuleAccounts = 'bx_accounts';
     	if(!BxDolModuleQuery::getInstance()->isEnabledByName($sModuleAccounts))
-    		return '';
+            return '';
+
+        $oModuleAccounts = BxDolModule::getInstance($sModuleAccounts);
+        if(!$oModuleAccounts || empty($oModuleAccounts->_oConfig->CNF['URL_MANAGE_ADMINISTRATION']))
+            return 'cccc';
 
-		$oModuleAccounts = BxDolModule::getInstance($sModuleAccounts);
-		if(!$oModuleAccounts || empty($oModuleAccounts->_oConfig->CNF['URL_MANAGE_ADMINISTRATION']))
-			return 'cccc';
+        $sLink = $oModuleAccounts->_oConfig->CNF['URL_MANAGE_ADMINISTRATION'];
 
-		$sLink = $oModuleAccounts->_oConfig->CNF['URL_MANAGE_ADMINISTRATION'];
+        $sLink = bx_absolute_url(BxDolPermalinks::getInstance()->permalink($sLink));
 
-		$sLink = bx_absolute_url(BxDolPermalinks::getInstance()->permalink($sLink));
-		
-		if(!empty($sFilter))
-			$sLink = bx_append_url_params($sLink, array('filter' => $sFilter));
+        if(!empty($sFilter))
+            $sLink = bx_append_url_params($sLink, array('filter' => $sFilter));
 
-		return $sLink;
+        return $sLink;
+    }
+
+    protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
+    {
+        $CNF = $this->_oModule->_oConfig->CNF;
+        $sTableInvites = $CNF['TABLE_INVITES'];
+        
+        if(strpos($sFilter, $this->_sParamsDivider) !== false)
+            list($this->_sFilter1Value, $sFilter) = explode($this->_sParamsDivider, $sFilter);
+        
+        $sFilterSql = "";
+        if(isset($this->_sFilter1Value) && $this->_sFilter1Value != ''){
+            if ($this->_sFilter1Value == BX_INV_FILTER_STATUS_ACCEPTED){
+                $sFilterSql = " AND " . $sTableInvites . ".`joined_account_id` IS NOT NULL";
+            }
+            else{
+                $sFilterSql = " AND " . $sTableInvites . ".`joined_account_id` IS NULL";
+            }
+        }
+        $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString($sFilterSql);
+        return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
     }
 }
 

+ 67 - 40
modules/boonex/invites/classes/BxInvGridRequests.php

@@ -49,33 +49,12 @@ class BxInvGridRequests extends BxTemplGrid
         }
         $this->_sParamsDivider = '#-#';
     }
-    
-    protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
-    {
-        $CNF = $this->_oModule->_oConfig->CNF;
-        $sTableRequests = $CNF['TABLE_REQUESTS'];
-        
-        if(strpos($sFilter, $this->_sParamsDivider) !== false)
-            list($this->_sFilter1Value, $sFilter) = explode($this->_sParamsDivider, $sFilter);
-       
-        $sFilterSql = "";
-        if(isset($this->_sFilter1Value) && $this->_sFilter1Value != ''){
-            if ($this->_sFilter1Value == BX_INV_FILTER_STATUS_INVITED){
-                $sFilterSql = " AND " . $sTableRequests . ".`status` IN (" . BX_INV_FILTER_STATUS_INVITED . ', ' . BX_INV_FILTER_STATUS_SEEN . ', ' . BX_INV_FILTER_STATUS_JOINED . ')';
-            }
-            else{
-                $sFilterSql = " AND " . $sTableRequests . ".`status` = " . $this->_sFilter1Value;
-            }
-        }
-        $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString($sFilterSql);
-        return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
-    }
-    
-    protected function _getDataSqlOrderClause ($sOrderByFilter, $sOrderField, $sOrderDir, $bFieldsOnly = false)
+
+    public function getFormCallBackUrlAPI($sAction, $iId = 0)
     {
-        return " ORDER BY `status` ASC, `date` DESC";
+         return '/api.php?r=system/perfom_action_api/TemplServiceGrid/&params[]=&o=' . $this->_sObject . '&a=' . $sAction;
     }
-    
+
     public function performActionAdd()
     {
         $sAction = 'add';
@@ -90,7 +69,11 @@ class BxInvGridRequests extends BxTemplGrid
 
         $aResult = [];
         if($oForm->isSubmittedAndValid()) {
-            $aResult = ['msg' => $this->_oModule->processFormObjectInvite($oForm)];
+            $sResult = $this->_oModule->processFormObjectInvite($oForm);
+            if($this->_bIsApi)
+                $aResult = [bx_api_get_msg($sResult)];
+            else
+                $aResult = ['msg' => $sResult];
         }
         else {
             if($this->_bIsApi)
@@ -296,29 +279,47 @@ class BxInvGridRequests extends BxTemplGrid
     protected function _getCellStatus($mixedValue, $sKey, $aField, $aRow)
     {
         $sStatus = _t('_bx_invites_request_status_new');
-        switch ($mixedValue) {
-            case 1:
-            case 2:
-                $sLinkHtml = $this->_oTemplate->parseLink('javascript:void(0)', $this->_oModule->_oDb->getInvites(array('type' => 'count_by_request', 'value' => $aRow['id'])), array(
-                    'title' => _t('_bx_invites_grid_action_title_adm_invite_info'),
+
+        if(in_array($mixedValue, [1, 2, 3])) {
+            switch ($mixedValue) {
+                case 1:
+                case 2:
+                    $iInvites = $this->_oModule->_oDb->getInvites(['type' => 'count_by_request', 'value' => $aRow['id']]);
+                    $sStatus = _t($mixedValue == 1 ? '_bx_invites_request_status_invited' : '_bx_invites_request_status_seen') . ' (' . $iInvites . ')';
+                    break;
+
+                case 3:
+                    $sStatus = _t('_bx_invites_request_status_joined');
+                    break;
+            }
+
+            if(!$this->_bIsApi)
+                $sStatus = $this->_oTemplate->parseLink('javascript:void(0)', $sStatus, [
+                    'title' => bx_html_attribute(_t('_bx_invites_grid_action_title_adm_invite_info')),
                     'bx_grid_action_single' => 'invite_info',
                     'bx_grid_action_data' => $aRow['id']
-                ));
-                $sStatus = ($mixedValue == 1 ? _t('_bx_invites_request_status_invited') : _t('_bx_invites_request_status_seen')) . ' (' . $sLinkHtml . ')';
-                break;
-            case 3:
-                $sStatus = _t('_bx_invites_request_status_joined') . ' (' . bx_time_js($this->_oModule->_oDb->getInvites(array('type' => 'date_joined_by_request', 'value' => $aRow['id']))) . ')';
-                break;
+                ]);
         }
+
         return parent::_getCellDefault($sStatus, $sKey, $aField, $aRow);
     }
-    
+
     protected function _getCellJoinedAccount($mixedValue, $sKey, $aField, $aRow)
     {
         $sAccountInfo = "";
-        if(isset($aRow["status"]) && $aRow["status"] == 3){
-            $sAccountInfo = $this->_oModule->_oTemplate->getProfilesByAccount($this->_oModule->_oDb->getInvites(array('type' => 'account_by_request', 'value' => $aRow['id'])));
+        if(isset($aRow["status"]) && $aRow["status"] == 3) {
+            $iAccountId = $this->_oModule->_oDb->getInvites(['type' => 'account_by_request', 'value' => $aRow['id']]);
+            if($this->_bIsApi) {
+                $iProfileId = 0;
+                if(($mixedProfileId = BxDolProfileQuery::getInstance()->getCurrentProfileByAccount($iAccountId, true)) !== false)
+                    $iProfileId = (int)$mixedProfileId;
+
+                return ['type' => 'profile', 'data' => BxDolProfile::getData($iProfileId)];
+            }
+
+            $sAccountInfo = $this->_oModule->_oTemplate->getProfilesByAccount($iAccountId);
         }
+
         return parent::_getCellDefault($sAccountInfo, $sKey, $aField, $aRow);
     }
     
@@ -334,6 +335,32 @@ class BxInvGridRequests extends BxTemplGrid
         return '';
     }
 
+    protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
+    {
+        $CNF = $this->_oModule->_oConfig->CNF;
+        $sTableRequests = $CNF['TABLE_REQUESTS'];
+
+        if(strpos($sFilter, $this->_sParamsDivider) !== false)
+            list($this->_sFilter1Value, $sFilter) = explode($this->_sParamsDivider, $sFilter);
+
+        $sFilterSql = "";
+        if(isset($this->_sFilter1Value) && $this->_sFilter1Value != ''){
+            if ($this->_sFilter1Value == BX_INV_FILTER_STATUS_INVITED){
+                $sFilterSql = " AND " . $sTableRequests . ".`status` IN (" . BX_INV_FILTER_STATUS_INVITED . ', ' . BX_INV_FILTER_STATUS_SEEN . ', ' . BX_INV_FILTER_STATUS_JOINED . ')';
+            }
+            else{
+                $sFilterSql = " AND " . $sTableRequests . ".`status` = " . $this->_sFilter1Value;
+            }
+        }
+        $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString($sFilterSql);
+        return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
+    }
+
+    protected function _getDataSqlOrderClause ($sOrderByFilter, $sOrderField, $sOrderDir, $bFieldsOnly = false)
+    {
+        return " ORDER BY `status` ASC, `date` DESC";
+    }
+
     protected function _getId()
     {
         $iId = 0;