Skip to content

Commit

Permalink
Fixed #6: Added translation support. If the app has the i18n componen…
Browse files Browse the repository at this point in the history
…t, a translation entry will be added.

Fixed #7: The auditing will now create an entry, even if the regular request logging is not enabled
  • Loading branch information
vagrant committed Jun 2, 2015
1 parent ead7891 commit 5bbed93
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 13 deletions.
10 changes: 10 additions & 0 deletions Auditing.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ public function init()
\Yii::$app->on(Application::EVENT_BEFORE_ACTION, [$this, 'onApplicationAction']);
// After request finalizes the audit entry and optionally does truncating
\Yii::$app->on(Application::EVENT_AFTER_REQUEST, [$this, 'onAfterRequest']);

// Register translation
$app = \Yii::$app;
if ($app->has('i18n')) {
$app->i18n->translations['audit'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en',
'basePath' => '@bedezign/yii2/audit/messages',
];
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion AuditingBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function leaveTrail($action, $name = null, $value = null, $old_value = nu
{
if ($this->active) {
$log = new AuditTrail;
$log->audit_id = Auditing::current()->getEntry()->id;
$log->audit_id = Auditing::current()->getEntry(true)->id;
$log->old_value = $old_value;
$log->new_value = $value;
$log->action = $action;
Expand Down
51 changes: 51 additions & 0 deletions messages/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

return [
// string, required, root directory of all source files
'sourcePath' => __DIR__ . '/..',
// array, required, list of language codes that the extracted messages
// should be translated to. For example, ['zh-CN', 'de'].
'languages' => ['nl'],
// string, the name of the function for translating messages.
// Defaults to 'Yii::t'. This is used as a mark to find the messages to be
// translated. You may use a string for single function name or an array for
// multiple function names.
'translator' => 'Yii::t',
// boolean, whether to sort messages by keys when merging new messages
// with the existing ones. Defaults to false, which means the new (untranslated)
// messages will be separated from the old (translated) ones.
'sort' => false,
// boolean, whether to remove messages that no longer appear in the source code.
// Defaults to false, which means each of these messages will be enclosed with a pair of '@@' marks.
'removeUnused' => true,
// array, list of patterns that specify which files/directories should NOT be processed.
// If empty or not set, all files/directories will be processed.
// A path matches a pattern if it contains the pattern string at its end. For example,
// '/a/b' will match all files and directories ending with '/a/b';
// the '*.svn' will match all files and directories whose name ends with '.svn'.
// and the '.svn' will match all files and directories named exactly '.svn'.
// Note, the '/' characters in a pattern matches both '/' and '\'.
// See helpers/FileHelper::findFiles() description for more details on pattern matching rules.
'only' => ['*.php', '*.js'],
// array, list of patterns that specify which files (not directories) should be processed.
// If empty or not set, all files will be processed.
// Please refer to "except" for details about the patterns.
// If a file/directory matches both a pattern in "only" and "except", it will NOT be processed.
'except' => [
'.svn',
'.git',
'.gitignore',
'.gitkeep',
'.hgignore',
'.hgkeep',
'arcgis',
'/messages',
],

// 'php' output format is for saving messages to php files.
'format' => 'php',
// Root directory containing message translations.
'messagePath' => __DIR__,
// boolean, whether the message file should be overwritten with the merged messages
'overwrite' => true,
];
52 changes: 52 additions & 0 deletions messages/nl/audit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Message translations.
*
* This file is automatically generated by 'yii message' command.
* It contains the localizable messages extracted from source code.
* You may modify this file by translating the extracted messages.
*
* Each array element represents the translation (value) of a message (key).
* If the value is empty, the message is considered as not translated.
* Messages that no longer need translation will have their translations
* enclosed between a pair of '@@' marks.
*
* Message string can be used with plural forms format. Check i18n section
* of the guide for details.
*
* NOTE: this file must be saved in UTF-8 encoding.
*/
return [
'Action' => '',
'Added at' => '',
'Audit Entries' => '',
'Audit Entry #{entry}' => '',
'Audit Entry #{id}' => '',
'Audit Trail #{id}' => '',
'Difference' => '',
'End Time' => '',
'Entry #{i}' => '',
'Entry ID' => '',
'Entry Id' => '',
'Error #{i}' => '',
'Errors ({i})' => '',
'Extra data ({i})' => '',
'Field' => '',
'Guest' => '',
'ID' => '',
'Javascript ({i})' => '',
'Location' => '',
'Max. Memory Usage' => '',
'Memory Usage' => '',
'New Value' => '',
'Old Value' => '',
'Request' => '',
'Request Duration' => '',
'Request method' => '',
'Stamp' => '',
'Start Time' => '',
'Trail ({i})' => '',
'Type' => '',
'User' => '',
'User ID' => '',
];
16 changes: 8 additions & 8 deletions models/AuditEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ public function attributeLabels()
{
return
[
'id' => 'Entry Id',
'created' => 'Added at',
'start_time' => 'Start Time',
'end_time' => 'End Time',
'duration' => 'Request Duration',
'user_id' => 'User',
'memory' => 'Memory Usage',
'memory_max' => 'Max. Memory Usage',
'id' => \Yii::t('audit', 'Entry Id'),
'created' => \Yii::t('audit', 'Added at'),
'start_time' => \Yii::t('audit', 'Start Time'),
'end_time' => \Yii::t('audit', 'End Time'),
'duration' => \Yii::t('audit', 'Request Duration'),
'user_id' => \Yii::t('audit', 'User'),
'memory' => \Yii::t('audit', 'Memory Usage'),
'memory_max' => \Yii::t('audit', 'Max. Memory Usage'),
];
}
}
3 changes: 1 addition & 2 deletions models/AuditModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

use bedezign\yii2\audit\Auditing;
use bedezign\yii2\audit\components\Helper;
use yii\db\Expression;

class AuditModel extends \yii\db\ActiveRecord
{
Expand All @@ -24,7 +23,7 @@ public static function getDb()
public function beforeSave($insert)
{
if ($insert && $this->hasAttribute('created'))
$this->created = new Expression('NOW()');
$this->created = new \yii\db\Expression('NOW()');

if ($this->autoSerialize)
foreach ($this->serializeAttributes as $attribute)
Expand Down
4 changes: 2 additions & 2 deletions views/default/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@
</div>
<div class="col-md-2">
<ul class="nav nav-pills nav-stacked affix">
<li><a href="#entry"><?= Yii::t('auditing', 'Request') ?></a></li>
<li><a href="#entry"><?= Yii::t('audit', 'Request') ?></a></li>

<?php if (count($entry->extraData)): ?>
<li><a href="#extra-data"><?= Yii::t('auditing', 'Extra data ({i})', ['i' => count($entry->extraData)]) ?></a></li>
<li><a href="#extra-data"><?= Yii::t('audit', 'Extra data ({i})', ['i' => count($entry->extraData)]) ?></a></li>
<?php endif ?>

<?php if (count($entry->trail)): ?>
Expand Down

0 comments on commit 5bbed93

Please sign in to comment.