فهرست منبع

#2382 Audit for site actions

RomanL 7 سال پیش
والد
کامیت
669c74a9ca

+ 28 - 6
inc/utils.inc.php

@@ -1390,15 +1390,37 @@ function bx_unicode_urldecode($s)
  * @param string $sContextProfileTitle - needed in case when original context was deleted
  * @param array $aActionParams - serialized array of parameters to pass to 'action lang key', for example friend name in case if 'befriend' action
  */
-function bx_audit($iContentId, $sContentModule, $sAction, $sContentTitle = '', $sContentInfoObject = '', $iContextProfileId = 0, $sContextProfileTitle = '', $aExtras = array(), $aActionParams = array(), $iProfileId = false)
+function bx_audit($iContentId, $sContentModule, $sAction, $aParams)
 {
-    if (!$iProfileId)
-        $iProfileId  = bx_get_logged_profile_id();
-    $oDb = BxDolDb::getInstance();
+    $sContentTitle = $sContentInfoObject = $sContextProfileTitle = $sData = $sActionParams = '';
+    $iContextProfileId = 0;
+    
+    $iProfileId = bx_get_logged_profile_id();
+    
+    if (isset($aParams['profile_id']))
+        $iProfileId = (int)$aParams['profile_id'];
+    
+    if (isset($aParams['content_title']))
+        $sContentTitle = $aParams['content_title'];
     
+    if (isset($aParams['context_profile_id']))
+        $iContextProfileId = (int)$aParams['context_profile_id'];
     
-    $sSql = $oDb->prepare("INSERT INTO `sys_audit`(profile_id, content_id, content_title, content_module, context_profile_id, context_profile_title, action_lang_key, action_lang_key_params, content_info_object, extras) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
-    , $iProfileId, $iContentId, $sContentTitle, $sContentModule, $iContextProfileId, $sContextProfileTitle, $sAction, (count($aActionParams) > 0 ? serialize($aActionParams) : '') ,$sContentInfoObject, (count($aExtras) > 0 ? serialize($aExtras) : ''));
+    if (isset($aParams['context_profile_title']))
+        $sContextProfileTitle = $aParams['context_profile_title'];
+    
+    if (isset($aParams['action_params']) && is_array($aParams['action_params']) && count($aParams['action_params']))
+        $sActionParams = serialize($aParams['action_params']);
+    
+    if (isset($aParams['content_info_object']))
+        $sContentInfoObject = $aParams['content_info_object'];
+     
+    if (isset($aParams['data']) && is_array($aParams['data']) && count($aParams['data']))
+        $sData = serialize($aParams['data']);
+    
+    $oDb = BxDolDb::getInstance();
+    $sSql = $oDb->prepare("INSERT INTO `sys_audit`(`added`, `profile_id`, `content_id`, `content_title`, `content_module`, `context_profile_id`, `context_profile_title`, `action_lang_key`, `action_lang_key_params`, `content_info_object`, `extras`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
+     ,time(), $iProfileId, $iContentId, $sContentTitle, $sContentModule, $iContextProfileId, $sContextProfileTitle, $sAction, $sActionParams ,$sContentInfoObject, $sData);
     return !$oDb->query($sSql);
 }
 

+ 20 - 18
modules/base/general/classes/BxBaseModGeneralFormsEntryHelper.php

@@ -492,16 +492,11 @@ class BxBaseModGeneralFormsEntryHelper extends BxDolProfileForms
         if(($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
             $oPrivacy->deleteGroupCustomByContentId($iContentId);
 
-        $mContextId = isset($CNF['FIELD_ALLOW_VIEW_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) ? -$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']] : false;
         bx_audit(
             $iContentId, 
             $this->_oModule->getName(), 
             '_sys_audit_action_detete',  
-            isset($CNF['FIELD_TITLE']) ? $aContentInfo[$CNF['FIELD_TITLE']] : '', 
-            isset($CNF['OBJECT_CONTENT_INFO']) ? $CNF['OBJECT_CONTENT_INFO'] : '',
-            $mContextId, 
-            ($mContextId && $mContextId != -3) ? BxDolProfile::getInstance($mContextId)->getDisplayName() : '', 
-            $aContentInfo
+            $this->_prepareAuditParams($aContentInfo)
         );
         
         return '';
@@ -534,16 +529,11 @@ class BxBaseModGeneralFormsEntryHelper extends BxDolProfileForms
         if(isset($CNF['FIELD_ALLOW_VIEW_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
             $oPrivacy->reassociateGroupCustomWithContent($oProfile->id(), $iContentId, (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]);
         
-        $mContextId = isset($CNF['FIELD_ALLOW_VIEW_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) ? -$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']] : false;
         bx_audit(
             $iContentId, 
             $this->_oModule->getName(), 
             '_sys_audit_action_edit',  
-            isset($CNF['FIELD_TITLE']) ? $aContentInfo[$CNF['FIELD_TITLE']] : '', 
-            isset($CNF['OBJECT_CONTENT_INFO']) ? $CNF['OBJECT_CONTENT_INFO'] : '',
-            $mContextId, 
-            ($mContextId && $mContextId != -3) ? BxDolProfile::getInstance($mContextId)->getDisplayName() : '', 
-            $aContentInfo
+            $this->_prepareAuditParams($aContentInfo)
         );
         
         return '';
@@ -563,16 +553,11 @@ class BxBaseModGeneralFormsEntryHelper extends BxDolProfileForms
         if(isset($CNF['FIELD_ALLOW_VIEW_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
             $oPrivacy->associateGroupCustomWithContent($oProfile->id(), $iContentId, (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]);
 
-        $mContextId = isset($CNF['FIELD_ALLOW_VIEW_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) ? -$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']] : false;
         bx_audit(
             $iContentId, 
             $this->_oModule->getName(), 
             '_sys_audit_action_add',  
-            isset($CNF['FIELD_TITLE']) ? $aContentInfo[$CNF['FIELD_TITLE']] : '', 
-            isset($CNF['OBJECT_CONTENT_INFO']) ? $CNF['OBJECT_CONTENT_INFO'] : '',
-            $mContextId, 
-            ($mContextId && $mContextId != -3) ? BxDolProfile::getInstance($mContextId)->getDisplayName() : '', 
-            $aContentInfo
+            $this->_prepareAuditParams($aContentInfo)
         );
         
         return '';
@@ -603,6 +588,23 @@ class BxBaseModGeneralFormsEntryHelper extends BxDolProfileForms
         return $s;
     }
 
+    private function _prepareAuditParams($aContentInfo)
+    {
+        $CNF = &$this->_oModule->_oConfig->CNF;
+        
+        $iContextId = isset($CNF['FIELD_ALLOW_VIEW_TO']) && (!empty($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) && (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']] < 0) ? - $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']] : 0;
+        
+        $AuditParams = array(
+            'content_title' => isset($CNF['FIELD_TITLE']) ? $aContentInfo[$CNF['FIELD_TITLE']] : '',
+            'context_profile_id' => $iContextId,
+            'content_info_object' =>  isset($CNF['OBJECT_CONTENT_INFO']) ? $CNF['OBJECT_CONTENT_INFO'] : '',
+            'data' => $aContentInfo
+        );
+        if ($iContextId > 0)
+            $AuditParams['context_profile_title'] = BxDolProfile::getInstance($iContextId)->getDisplayName();
+        return $AuditParams;
+    }
+    
     protected function prepareResponse($mixedResponse, $bAsJson = false, $sKey = 'msg', $aAdditional = array())
     {
     	if(!$bAsJson)

+ 24 - 0
studio/classes/BxDolStudioAuditGrid.php

@@ -9,9 +9,33 @@
 
 class BxDolStudioAuditGrid extends BxTemplStudioGrid
 {
+    protected $sModule;
+    
     public function __construct ($aOptions, $oTemplate = false)
     {
         parent::__construct ($aOptions, $oTemplate);
+        
+        $this->_sDefaultSortingOrder = 'DESC';
+        
+        $this->oDb = new BxDolStudioFormsQuery();
+
+        $sModule = bx_get('module');
+        if(!empty($sModule)) {
+            $this->sModule = bx_process_input($sModule);
+            $this->_aQueryAppend['module'] = $this->sModule;
+        }
+    }
+    
+    protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
+    {
+        $sModule = $this->sModule;
+        if(strpos($sFilter, $this->sParamsDivider) !== false)
+            list($sModule, $sFilter) = explode($this->sParamsDivider, $sFilter);
+
+        if($sModule != '')
+            $this->_aOptions['source'] .= $this->oDb->prepareAsString(" AND `content_module`=?", $sModule);
+        
+        return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
     }
 }
 

+ 1 - 1
studio/classes/BxDolStudioFormsCategories.php

@@ -35,7 +35,7 @@ class BxDolStudioFormsCategories extends BxTemplStudioGrid
             list($sModule, $sFilter) = explode($this->sParamsDivider, $sFilter);
 
         if($sModule != '')
-            $this->_aOptions['source'] .= $this->oDb->prepareAsString(" AND `module`=?", $sModule);
+            $this->_aOptions['source'] .= $this->oDb->prepareAsString(" AND `module` = ?", $sModule);
         
         return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
     }

+ 43 - 0
studio/classes/BxDolStudioFormsQuery.php

@@ -515,6 +515,49 @@ class BxDolStudioFormsQuery extends BxDolDb
         return (int)$this->getOne("SELECT FOUND_ROWS()");
     }
     
+    function getAudit($aParams, &$aItems, $bReturnCount = true)
+    {
+        $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
+        $sSelectClause = $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
+
+        if(!isset($aParams['order']) || empty($aParams['order']))
+            $sOrderClause = "ORDER BY `tv`.`content_module` ASC";
+
+        switch($aParams['type']) {
+            case 'by_id':
+                $aMethod['name'] = 'getRow';
+                $aMethod['params'][1] = array(
+                	'id' => $aParams['value']
+                );
+
+                $sWhereClause = " AND `tv`.`id`=:id ";
+                break;
+
+            case 'counter_by_modules':
+                $aMethod['name'] = 'getPairs';
+                $aMethod['params'][1] = 'module';
+                $aMethod['params'][2] = 'counter';
+                $sSelectClause = ", `tv`.`content_module` AS `module`, COUNT(`tv`.`id`) AS `counter`";
+                $sGroupClause = "GROUP BY `tv`.`content_module`";
+                break;
+
+            case 'all':
+                break;
+        }
+
+        $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . "
+             `tv`.`id` AS `id`
+             " . $sSelectClause . "
+            FROM `sys_audit` AS `tv` " . $sJoinClause . "
+            WHERE 1 " . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
+        $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
+
+        if(!$bReturnCount)
+            return !empty($aItems);
+
+        return (int)$this->getOne("SELECT FOUND_ROWS()");
+    }
+    
     function deleteCategories($CategoryId)
     {
         $sSql = "DELETE FROM `sys_categories2objects` WHERE `category_id` = :category_id";

+ 49 - 0
studio/js/forms_audit.js

@@ -0,0 +1,49 @@
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaStudio UNA Studio
+ * @{
+ */
+function BxDolStudioFormsAudit(oOptions) {
+    this.sActionsUrl = oOptions.sActionUrl;
+    this.sPageUrl = oOptions.sPageUrl;
+    this.sObjNameGrid = oOptions.sObjNameGrid;
+    this.sObjName = oOptions.sObjName == undefined ? 'oBxDolStudioFormsAudit' : oOptions.sObjName;
+    this.sAnimationEffect = oOptions.sAnimationEffect == undefined ? 'fade' : oOptions.sAnimationEffect;
+    this.iAnimationSpeed = oOptions.iAnimationSpeed == undefined ? 'slow' : oOptions.iAnimationSpeed;
+    this.sParamsDivider = oOptions.sParamsDivider == undefined ? '#-#' : oOptions.sParamsDivider;
+
+    this.sTextSearchInput = oOptions.sTextSearchInput == undefined ? '' : oOptions.sTextSearchInput;
+}
+
+BxDolStudioFormsAudit.prototype.onChangeModule = function () {
+    this.reloadGrid($('#bx-grid-module-' + this.sObjNameGrid).val());
+};
+
+BxDolStudioFormsAudit.prototype.reloadGrid = function (sModule) {
+    var bReload = false;
+    var oActions = $("[bx_grid_action_independent]");
+    if(!sModule){
+        oActions.addClass('bx-btn-disabled');
+    }
+    else {
+        oActions.removeClass('bx-btn-disabled');
+    }
+    console.log(glGrids);
+    console.log(2);
+    console.log(this.sObjNameGrid);
+    if (glGrids[this.sObjNameGrid]._oQueryAppend['module'] != sModule) {
+        glGrids[this.sObjNameGrid]._oQueryAppend['module'] = sModule;
+        bReload = true;
+    }
+
+    if (bReload) {
+        var sValueSearch = $('#bx-grid-search-' + this.sObjNameGrid).val();
+        if (sValueSearch == this.sTextSearchInput)
+            sValueSearch = '';
+
+        glGrids[this.sObjNameGrid].setFilter(sModule + this.sParamsDivider + sValueSearch, true);
+    }
+};
+/** @} */

+ 10 - 0
studio/template/forms_audit.html

@@ -0,0 +1,10 @@
+__content__
+<script type="text/javascript">
+    var __js_object__ = new BxDolStudioFormsAudit({
+        sActionUrl: '<bx_url_root />audit.php',
+        sPageUrl: '__page_url__',
+        sObjNameGrid: '__grid_object__',
+        sParamsDivider: '__params_divider__',
+        sTextSearchInput: '<bx_text_js:_sys_grid_search />'
+    });
+</script>

+ 4 - 8
studio/template/scripts/BxBaseStudioAudit.php

@@ -28,22 +28,17 @@ class BxBaseStudioAudit extends BxDolStudioAudit
         	'audit' => 'sys_studio_audit'
     );
     }
+	
     function getPageCss()
     {
         return array_merge(parent::getPageCss(), array('forms.css', 'paginate.css'));
     }
+	
     function getPageJs()
     {
         return array_merge(parent::getPageJs(), array('settings.js'));
     }
-    function getPageJsClass()
-    {
-        return 'BxDolStudioAudit';
-    }
-    function getPageJsObject()
-    {
-        return 'oBxDolStudioAudit';
-    }
+
     function getPageJsCode($aOptions = array(), $bWrap = true)
     {
         $aOptions = array_merge($aOptions, array(
@@ -52,6 +47,7 @@ class BxBaseStudioAudit extends BxDolStudioAudit
 
         return parent::getPageJsCode($aOptions, $bWrap);
     }
+    
     function getPageMenu($aMenu = array(), $aMarkers = array())
     {
         $sJsObject = $this->getPageJsObject();

+ 49 - 0
studio/template/scripts/BxBaseStudioAuditGrid.php

@@ -15,6 +15,15 @@ class BxBaseStudioAuditGrid extends BxDolStudioAuditGrid
         parent::__construct($aOptions, $oTemplate);
     }
     
+    protected function _addJsCss()
+    {
+        parent::_addJsCss();
+        $this->_oTemplate->addJs(array('jquery.form.min.js', 'forms_audit.js'));
+
+        $oForm = new BxTemplStudioFormView(array());
+        $oForm->addCssJs();
+    }
+    
 	protected function _getCellAdded($mixedValue, $sKey, $aField, $aRow)
     {
         return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
@@ -103,6 +112,46 @@ class BxBaseStudioAuditGrid extends BxDolStudioAuditGrid
         }
         return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
     }
+    
+    protected function _getFilterControls ()
+    {
+        parent::_getFilterControls();
+
+        $aInputModules = $this->getModulesSelectOneArray('getAudit', false, false);
+		$aInputModules['values'] = array_merge(array('' => _t('_adm_txt_select_module')), $aInputModules['values']);
+		$oForm = new BxTemplStudioFormView(array());
+        $sContent = $oForm->genRow($aInputModules);
+        $oForm = new BxTemplStudioFormView(array());
+
+        $aInputSearch = array(
+            'type' => 'text',
+            'name' => 'keyword',
+            'attrs' => array(
+                'id' => 'bx-grid-search-' . $this->_sObject,
+            ),
+            'tr_attrs' => array(
+                'style' => 'display:none;'
+            )
+        );
+        $sContent .= $oForm->genRow($aInputSearch);
+
+        return  $sContent;
+    }
+    
+    function getJsObject()
+    {
+        return 'oBxDolStudioFormsAudit';
+    }
+    
+    function getCode($isDisplayHeader = true)
+    {
+        return $this->_oTemplate->parseHtmlByName('forms_audit.html', array(
+            'content' => parent::getCode($isDisplayHeader),
+            'js_object' => $this->getJsObject(),
+            'grid_object' => $this->_sObject,
+            'params_divider' => $this->sParamsDivider
+        ));
+    }
 }
 
 /** @} */