Audit::add PHP Метод

add() публичный статический Метод

public static add ( $target, $action, $data = null, $log_message = null, $properties = [] )
    public static function add($target, $action, $data = null, $log_message = null, $properties = array())
    {
        if (!($_target = AuditType::model()->find('name=?', array($target)))) {
            $_target = new AuditType();
            $_target->name = $target;
            if (!$_target->save()) {
                throw new Exception('Unable to save audit target: ' . print_r($_target->getErrors(), true));
            }
        }
        if (!($_action = AuditAction::model()->find('name=?', array($action)))) {
            $_action = new AuditAction();
            $_action->name = $action;
            if (!$_action->save()) {
                throw new Exception('Unable to save audit action: ' . print_r($_action->getErrors(), true));
            }
        }
        $audit = new self();
        $audit->type_id = $_target->id;
        $audit->action_id = $_action->id;
        $audit->data = $data;
        if (!isset($properties['user_id'])) {
            if (Yii::app()->session['user']) {
                $properties['user_id'] = Yii::app()->session['user']->id;
            }
        }
        if (isset($properties['module'])) {
            if ($et = EventType::model()->find('class_name=?', array($properties['module']))) {
                $properties['event_type_id'] = $et->id;
            } else {
                if (!($module = AuditModule::model()->find('name=?', array($properties['module'])))) {
                    $module = new AuditModule();
                    $module->name = $properties['module'];
                    if (!$module->save()) {
                        throw new Exception('Unable to create audit_module: ' . print_r($module->getErrors(), true));
                    }
                }
                $properties['module_id'] = $module->id;
            }
            unset($properties['module']);
        }
        if (isset($properties['model'])) {
            if (!($model = AuditModel::model()->find('name=?', array($properties['model'])))) {
                $model = new AuditModel();
                $model->name = $properties['model'];
                if (!$model->save()) {
                    throw new Exception('Unable to save audit_model: ' . print_r($model->getErrors(), true));
                }
            }
            $properties['model_id'] = $model->id;
            unset($properties['model']);
        }
        foreach ($properties as $key => $value) {
            $audit->{$key} = $value;
        }
        if (!$audit->save()) {
            throw new Exception('Failed to save audit entry: ' . print_r($audit->getErrors(), true));
        }
        if (isset($properties['user_id'])) {
            $username = User::model()->findByPk($properties['user_id'])->username;
        }
        $log_message && OELog::log($log_message, @$username);
        return $audit;
    }

Usage Example

 /**
  * Lists local authorities from Commissioning Body Service
  *
  * @throws CHttpException
  */
 public function actionList()
 {
     \Audit::add('admin-CommissioningBodyService', 'list');
     if (!($commissioning_bt = \CommissioningBodyType::model()->findByAttributes(array('shortname' => 'LA')))) {
         throw new \CHttpException(500, 'Local Authority Commissioning Body Type is not configured.');
     }
     $service_type = \CommissioningBodyServiceType::model()->findByAttributes(array('shortname' => 'SSD'));
     $data['title'] = 'CVI Social Services Depts.';
     $data['commissioning_bt'] = $commissioning_bt;
     $data['service_type'] = $service_type;
     $data['return_url'] = '/OphCoCvi/localAuthoritiesAdmin/list';
     $data['base_data_url'] = 'OphCoCvi/localAuthoritiesAdmin/';
     $this->render('//admin/commissioning_body_services', $data);
 }
All Usage Examples Of Audit::add