Skip to content

Commit

Permalink
Fixed: #16
Browse files Browse the repository at this point in the history
  • Loading branch information
sammi authored and sammi committed Oct 26, 2015
1 parent f1fce4e commit 2560e91
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
24 changes: 15 additions & 9 deletions AuditTrail.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
class AuditTrail extends ActiveRecord
{
private $_message_category = 'audittrail';

/**
* @return string the associated database table name
*/
Expand All @@ -36,7 +36,7 @@ public static function tableName()
/**
* @return \yii\db\Connection the database connection used by this AR class.
*/
public static function getDb()
public static function getDb()
{
if (isset(Yii::$app->params['audittrail.db'])) {
return Yii::$app->get(Yii::$app->params['audittrail.db']);
Expand All @@ -45,16 +45,16 @@ public static function getDb()
}
// return Yii::$app->get('dbUser');
}

public function init()
{
parent::init();

\Yii::$app->i18n->translations[$this->_message_category] = [
'class' => 'yii\i18n\PhpMessageSource',
];
}

/**
* @return array customized attribute labels (name=>label)
*/
Expand All @@ -71,7 +71,7 @@ public function attributeLabels()
'user_id' => Yii::t('audittrail','User'),
'model_id' => Yii::t('audittrail','ID'),
];
}
}

/**
* @return array validation rules for model attributes.
Expand All @@ -88,12 +88,12 @@ public function rules()
[['old_value', 'new_value'], 'safe']
];
}

public static function recently($query)
{
$query->orderBy(['[[stamp]]' => SORT_DESC]);
}

public function getUser()
{
if(isset(Yii::$app->params['audittrail.model']) && isset(Yii::$app->params['audittrail.model'])){
Expand All @@ -104,7 +104,13 @@ public function getUser()
}

public function getParent(){
$model_name = $this->model;
$model_name =
(
isset(Yii::$app->params['audittrail.FQNPrefix']) &&
rtrim(Yii::$app->params['audittrail.FQNPrefix'], '\\') ?
rtrim(Yii::$app->params['audittrail.FQNPrefix'], '\\') . '\\' :
''
) . $this->model;
return new $model_name;
}
}
16 changes: 14 additions & 2 deletions LoggableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,23 @@ public function auditAttributes($insert, $newattributes, $oldattributes = array(
public function leaveTrail($action, $name = null, $value = null, $old_value = null)
{
if ($this->active) {
$log = new AuditTrail();

$log = new AuditTrail();
$className = $this->owner->className();

if(
isset(Yii::$app->params['audittrail.FQNPrefix']) &&
Yii::$app->params['audittrail.FQNPrefix']
){
$classNameParts = explode('\\', $className);
$log->model = end($classNameParts);
}else{
$log->model = $className;
}

$log->old_value = $old_value;
$log->new_value = $value;
$log->action = $action;
$log->model = $this->owner->className(); // Gets a plain text version of the model name
$log->model_id = (string) $this->getNormalizedPk();
$log->field = $name;
$log->stamp = $this->storeTimestamp ? time() : date($this->dateFormat); // If we are storing a timestamp lets get one else lets get the date
Expand Down

0 comments on commit 2560e91

Please sign in to comment.