summaryrefslogtreecommitdiff
path: root/protected/modules
diff options
context:
space:
mode:
Diffstat (limited to 'protected/modules')
-rw-r--r--protected/modules/auditTrail/.DS_Storebin0 -> 12292 bytes
-rw-r--r--protected/modules/auditTrail/AuditTrailModule.php74
-rw-r--r--protected/modules/auditTrail/README.txt19
-rw-r--r--protected/modules/auditTrail/behaviors/LoggableBehavior.php133
-rw-r--r--protected/modules/auditTrail/controllers/AdminController.php46
-rw-r--r--protected/modules/auditTrail/controllers/DefaultController.php36
-rw-r--r--protected/modules/auditTrail/migrations/m110517_155003_create_tables_audit_trail.php64
-rw-r--r--protected/modules/auditTrail/models/.DS_Storebin0 -> 6148 bytes
-rw-r--r--protected/modules/auditTrail/models/AuditTrail.php125
-rw-r--r--protected/modules/auditTrail/views/.DS_Storebin0 -> 6148 bytes
-rw-r--r--protected/modules/auditTrail/views/admin/_search.php59
-rw-r--r--protected/modules/auditTrail/views/admin/admin.php39
-rw-r--r--protected/modules/auditTrail/views/default/.DS_Storebin0 -> 6148 bytes
-rw-r--r--protected/modules/auditTrail/views/default/index.php95
-rw-r--r--protected/modules/auditTrail/widgets/.DS_Storebin0 -> 6148 bytes
-rw-r--r--protected/modules/auditTrail/widgets/portlets/.DS_Storebin0 -> 6148 bytes
-rw-r--r--protected/modules/auditTrail/widgets/portlets/ShowAuditTrail.php157
17 files changed, 847 insertions, 0 deletions
diff --git a/protected/modules/auditTrail/.DS_Store b/protected/modules/auditTrail/.DS_Store
new file mode 100644
index 0000000..dd74794
--- /dev/null
+++ b/protected/modules/auditTrail/.DS_Store
Binary files differ
diff --git a/protected/modules/auditTrail/AuditTrailModule.php b/protected/modules/auditTrail/AuditTrailModule.php
new file mode 100644
index 0000000..56d2d25
--- /dev/null
+++ b/protected/modules/auditTrail/AuditTrailModule.php
@@ -0,0 +1,74 @@
+<?php
+
+class AuditTrailModule extends CWebModule
+{
+ /**
+ * @var string the name of the User class. Defaults to "User"
+ */
+ public $userClass = "User";
+
+ /**
+ * @var string the name of the column of the user class that is the primary key. Defaults to "id"
+ */
+ public $userIdColumn = "id";
+
+ /**
+ * @var string the name of the column of the user class that is the username. Defaults to "username"
+ */
+ public $userNameColumn = "username";
+
+ /**
+ * @var AuditTrailModule static variable to hold the module so we don't have to instantiate it a million times to get config values
+ */
+ private static $__auditTrailModule;
+
+ public function init()
+ {
+ // this method is called when the module is being created
+ // you may place code here to customize the module or the application
+
+ // import the module-level models and components
+ $this->setImport(array(
+ 'auditTrail.models.*',
+ 'auditTrail.components.*',
+ ));
+ }
+
+ public function beforeControllerAction($controller, $action)
+ {
+ if(parent::beforeControllerAction($controller, $action))
+ {
+ // this method is called before any module controller action is performed
+ // you may place customized code here
+ return true;
+ }
+ else
+ return false;
+ }
+
+
+ /**
+ * Returns the value you want to look up, either from the config file or a user's override
+ * @var value The name of the value you would like to look up
+ * @return the config value you need
+ */
+ public static function getFromConfigOrObject($value) {
+ $at = Yii::app()->modules['auditTrail'];
+
+ //If we can get the value from the config, do that to save overhead
+ if( isset( $at[$value]) && !empty($at[$value] ) ) {
+ return $at[$value];
+ }
+
+ //If we cannot get the config value from the config file, get it from the
+ //instantiated object. Only instantiate it once though, its probably
+ //expensive to do. PS I feel this is a dirty trick and I don't like it
+ //but I don't know a better way
+ if(!is_object(self::$__auditTrailModule)) {
+ self::$__auditTrailModule = new AuditTrailModule(microtime(), null);
+ }
+
+ return self::$__auditTrailModule->$value;
+ }
+
+} \ No newline at end of file
diff --git a/protected/modules/auditTrail/README.txt b/protected/modules/auditTrail/README.txt
new file mode 100644
index 0000000..0ebb757
--- /dev/null
+++ b/protected/modules/auditTrail/README.txt
@@ -0,0 +1,19 @@
+In your main.php config file, add the follow line in your modules section:
+
+
+ 'modules'=>array(
+ ..
+ 'auditTrail'=>array(),
+ ..
+
+
+Then go to
+
+http://myapp.com/index.php?r=auditTrail
+
+
+and view the instructions for setup!
+
+For a more detailed walkthrough, please see the instructions at
+
+http://www.yiiframework.com/extension/audittrail \ No newline at end of file
diff --git a/protected/modules/auditTrail/behaviors/LoggableBehavior.php b/protected/modules/auditTrail/behaviors/LoggableBehavior.php
new file mode 100644
index 0000000..ff28507
--- /dev/null
+++ b/protected/modules/auditTrail/behaviors/LoggableBehavior.php
@@ -0,0 +1,133 @@
+<?php
+class LoggableBehavior extends CActiveRecordBehavior
+{
+ private $_oldattributes = array();
+
+ public function afterSave($event)
+ {
+ try {
+ $username = Yii::app()->user->Name;
+ $userid = Yii::app()->user->id;
+ } catch(Exception $e) { //If we have no user object, this must be a command line program
+ $username = "NO_USER";
+ $userid = null;
+ }
+
+ if(empty($username)) {
+ $username = "NO_USER";
+ }
+
+ if(empty($userid)) {
+ $userid = null;
+ }
+
+ $newattributes = $this->Owner->getAttributes();
+ $oldattributes = $this->getOldAttributes();
+
+ if (!$this->Owner->isNewRecord) {
+ // compare old and new
+ foreach ($newattributes as $name => $value) {
+ if (!empty($oldattributes)) {
+ $old = $oldattributes[$name];
+ } else {
+ $old = '';
+ }
+
+ if ($value != $old) {
+ $log=new AuditTrail();
+ $log->old_value = $old;
+ $log->new_value = $value;
+ $log->action = 'CHANGE';
+ $log->model = get_class($this->Owner);
+ $log->model_id = $this->Owner->getPrimaryKey();
+ $log->field = $name;
+ $log->stamp = date('Y-m-d H:i:s');
+ $log->user_id = $userid;
+
+ $log->save();
+ }
+ }
+ } else {
+ $log=new AuditTrail();
+ $log->old_value = '';
+ $log->new_value = '';
+ $log->action= 'CREATE';
+ $log->model= get_class($this->Owner);
+ $log->model_id= $this->Owner->getPrimaryKey();
+ $log->field= 'N/A';
+ $log->stamp= date('Y-m-d H:i:s');
+ $log->user_id= $userid;
+
+ $log->save();
+
+
+ foreach ($newattributes as $name => $value) {
+ $log=new AuditTrail();
+ $log->old_value = '';
+ $log->new_value = $value;
+ $log->action= 'SET';
+ $log->model= get_class($this->Owner);
+ $log->model_id= $this->Owner->getPrimaryKey();
+ $log->field= $name;
+ $log->stamp= date('Y-m-d H:i:s');
+ $log->user_id= $userid;
+ $log->save();
+ }
+
+
+
+ }
+ return parent::afterSave($event);
+ }
+
+ public function afterDelete($event)
+ {
+
+ try {
+ $username = Yii::app()->user->Name;
+ $userid = Yii::app()->user->id;
+ } catch(Exception $e) {
+ $username = "NO_USER";
+ $userid = null;
+ }
+
+ if(empty($username)) {
+ $username = "NO_USER";
+ }
+
+ if(empty($userid)) {
+ $userid = null;
+ }
+
+ $log=new AuditTrail();
+ $log->old_value = '';
+ $log->new_value = '';
+ $log->action= 'DELETE';
+ $log->model= get_class($this->Owner);
+ $log->model_id= $this->Owner->getPrimaryKey();
+ $log->field= 'N/A';
+ $log->stamp= date('Y-m-d H:i:s');
+ $log->user_id= $userid;
+ $log->save();
+ return parent::afterDelete($event);
+ }
+
+ public function afterFind($event)
+ {
+ // Save old values
+ $this->setOldAttributes($this->Owner->getAttributes());
+
+ return parent::afterFind($event);
+ }
+
+ public function getOldAttributes()
+ {
+ return $this->_oldattributes;
+ }
+
+ public function setOldAttributes($value)
+ {
+ $this->_oldattributes=$value;
+ }
+}
+?> \ No newline at end of file
diff --git a/protected/modules/auditTrail/controllers/AdminController.php b/protected/modules/auditTrail/controllers/AdminController.php
new file mode 100644
index 0000000..f03b373
--- /dev/null
+++ b/protected/modules/auditTrail/controllers/AdminController.php
@@ -0,0 +1,46 @@
+<?php
+
+class AdminController extends Controller
+{
+ public $defaultAction = "admin";
+ public $layout='//layouts/column1';
+
+ /**
+ * @return array action filters
+ */
+ public function filters()
+ {
+ return array(
+ 'accessControl', // perform access control for CRUD operations
+ );
+ }
+
+ /**
+ * Specifies the access control rules.
+ * This method is used by the 'accessControl' filter.
+ * @return array access control rules
+ */
+ public function accessRules()
+ {
+ return array(
+ array('allow', // allow admin user to perform actions
+ 'users'=>array('admin'),
+ ),
+ array('deny', // deny all users
+ 'users'=>array('*'),
+ ),
+ );
+ }
+
+ public function actionAdmin()
+ {
+ $model=new AuditTrail('search');
+ $model->unsetAttributes(); // clear any default values
+ if(isset($_GET['AuditTrail'])) {
+ $model->attributes=$_GET['AuditTrail'];
+ }
+ $this->render('admin',array(
+ 'model'=>$model,
+ ));
+ }
+} \ No newline at end of file
diff --git a/protected/modules/auditTrail/controllers/DefaultController.php b/protected/modules/auditTrail/controllers/DefaultController.php
new file mode 100644
index 0000000..76da57d
--- /dev/null
+++ b/protected/modules/auditTrail/controllers/DefaultController.php
@@ -0,0 +1,36 @@
+<?php
+
+class DefaultController extends Controller
+{
+
+ /**
+ * @return array action filters
+ */
+ public function filters()
+ {
+ return array(
+ 'accessControl', // perform access control for CRUD operations
+ );
+ }
+
+ /**
+ * Specifies the access control rules.
+ * This method is used by the 'accessControl' filter.
+ * @return array access control rules
+ */
+ public function accessRules()
+ {
+ return array(
+ array('allow', // allow admin user to perform actions
+ 'users'=>array('admin'),
+ ),
+ array('deny', // deny all users
+ 'users'=>array('*'),
+ ),
+ );
+ }
+ public function actionIndex()
+ {
+ $this->render('index');
+ }
+} \ No newline at end of file
diff --git a/protected/modules/auditTrail/migrations/m110517_155003_create_tables_audit_trail.php b/protected/modules/auditTrail/migrations/m110517_155003_create_tables_audit_trail.php
new file mode 100644
index 0000000..48e94cd
--- /dev/null
+++ b/protected/modules/auditTrail/migrations/m110517_155003_create_tables_audit_trail.php
@@ -0,0 +1,64 @@
+<?php
+
+class m110517_155003_create_tables_audit_trail extends CDbMigration
+{
+
+ /**
+ * Creates initial version of the audit trail table
+ */
+ public function up()
+ {
+
+ //Create our first version of the audittrail table
+ //Please note that this matches the original creation of the
+ //table from version 1 of the extension. Other migrations will
+ //upgrade it from here if we ever need to. This was done so
+ //that older versions can still use migrate functionality to upgrade.
+ $this->createTable( 'tbl_audit_trail',
+ array(
+ 'id' => 'pk',
+ 'old_value' => 'text',
+ 'new_value' => 'text',
+ 'action' => 'string NOT NULL',
+ 'model' => 'string NOT NULL',
+ 'field' => 'string NOT NULL',
+ 'stamp' => 'datetime NOT NULL',
+ 'user_id' => 'string',
+ 'model_id' => 'string NOT NULL',
+ )
+ );
+
+ //Index these bad boys for speedy lookups
+ $this->createIndex( 'idx_audit_trail_user_id', 'tbl_audit_trail', 'user_id');
+ $this->createIndex( 'idx_audit_trail_model_id', 'tbl_audit_trail', 'model_id');
+ $this->createIndex( 'idx_audit_trail_model', 'tbl_audit_trail', 'model');
+ $this->createIndex( 'idx_audit_trail_field', 'tbl_audit_trail', 'field');
+ $this->createIndex( 'idx_audit_trail_action', 'tbl_audit_trail', 'action');
+ }
+
+ /**
+ * Drops the audit trail table
+ */
+ public function down()
+ {
+ $this->dropTable( 'tbl_audit_trail' );
+ }
+
+ /**
+ * Creates initial version of the audit trail table in a transaction-safe way.
+ * Uses $this->up to not duplicate code.
+ */
+ public function safeUp()
+ {
+ $this->up();
+ }
+
+ /**
+ * Drops the audit trail table in a transaction-safe way.
+ * Uses $this->down to not duplicate code.
+ */
+ public function safeDown()
+ {
+ $this->down();
+ }
+} \ No newline at end of file
diff --git a/protected/modules/auditTrail/models/.DS_Store b/protected/modules/auditTrail/models/.DS_Store
new file mode 100644
index 0000000..5008ddf
--- /dev/null
+++ b/protected/modules/auditTrail/models/.DS_Store
Binary files differ
diff --git a/protected/modules/auditTrail/models/AuditTrail.php b/protected/modules/auditTrail/models/AuditTrail.php
new file mode 100644
index 0000000..e21a283
--- /dev/null
+++ b/protected/modules/auditTrail/models/AuditTrail.php
@@ -0,0 +1,125 @@
+<?php
+
+/**
+ * This is the model class for table "tbl_audit_trail".
+ */
+class AuditTrail extends CActiveRecord
+{
+ /**
+ * The followings are the available columns in table 'tbl_audit_trail':
+ * @var integer $id
+ * @var string $new_value
+ * @var string $old_value
+ * @var string $action
+ * @var string $model
+ * @var string $field
+ * @var string $stamp
+ * @var integer $user_id
+ * @var string $model_id
+ */
+
+ /**
+ * Returns the static model of the specified AR class.
+ * @return AuditTrail the static model class
+ */
+ public static function model($className=__CLASS__)
+ {
+ return parent::model($className);
+ }
+
+ /**
+ * @return string the associated database table name
+ */
+ public function tableName()
+ {
+ return 'tbl_audit_trail';
+ }
+
+ /**
+ * @return array validation rules for model attributes.
+ */
+ public function rules()
+ {
+ // NOTE: you should only define rules for those attributes that
+ // will receive user inputs.
+ return array(
+ array('action, model, field, stamp, model_id', 'required'),
+ array('action', 'length', 'max'=>255),
+ array('model', 'length', 'max'=>255),
+ array('field', 'length', 'max'=>255),
+ array('model_id', 'length', 'max'=>255),
+ array('user_id', 'length', 'max'=>255),
+ // The following rule is used by search().
+ // Please remove those attributes that should not be searched.
+ array('id, new_value, old_value, action, model, field, stamp, user_id, model_id', 'safe', 'on'=>'search'),
+ );
+ }
+
+ /**
+ * @return array relational rules.
+ */
+ public function relations()
+ {
+ // NOTE: you may need to adjust the relation name and the related
+ // class name for the relations automatically generated below.
+ return array(
+ );
+ }
+
+ /**
+ * @return array customized attribute labels (name=>label)
+ */
+ public function attributeLabels()
+ {
+ return array(
+ 'id' => 'ID',
+ 'old_value' => 'Old Value',
+ 'new_value' => 'New Value',
+ 'action' => 'Action',
+ 'model' => 'Model',
+ 'field' => 'Field',
+ 'stamp' => 'Stamp',
+ 'user_id' => 'User',
+ 'model_id' => 'Model',
+ );
+ }
+
+ /**
+ * Retrieves a list of models based on the current search/filter conditions.
+ * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
+ */
+ public function search($options = array())
+ {
+ // Warning: Please modify the following code to remove attributes that
+ // should not be searched.
+ $criteria=new CDbCriteria;
+ $criteria->compare('id',$this->id);
+ $criteria->compare('old_value',$this->old_value,true);
+ $criteria->compare('new_value',$this->new_value,true);
+ $criteria->compare('action',$this->action,true);
+ $criteria->compare('model',$this->model);
+ $criteria->compare('field',$this->field,true);
+ $criteria->compare('stamp',$this->stamp,true);
+ $criteria->compare('user_id',$this->user_id);
+ $criteria->compare('model_id',$this->model_id);
+ $criteria->mergeWith($this->getDbCriteria());
+ return new CActiveDataProvider(
+ get_class($this),
+ array_merge(
+ array(
+ 'criteria'=>$criteria,
+ ),
+ $options
+ )
+ );
+ }
+
+ public function scopes() {
+ return array(
+ 'recently' => array(
+ 'order' => ' t.stamp DESC ',
+ ),
+
+ );
+ }
+} \ No newline at end of file
diff --git a/protected/modules/auditTrail/views/.DS_Store b/protected/modules/auditTrail/views/.DS_Store
new file mode 100644
index 0000000..6f1c5a1
--- /dev/null
+++ b/protected/modules/auditTrail/views/.DS_Store
Binary files differ
diff --git a/protected/modules/auditTrail/views/admin/_search.php b/protected/modules/auditTrail/views/admin/_search.php
new file mode 100644
index 0000000..f7d4571
--- /dev/null
+++ b/protected/modules/auditTrail/views/admin/_search.php
@@ -0,0 +1,59 @@
+<div class="wide form">
+
+<?php $form=$this->beginWidget('CActiveForm', array(
+ 'action'=>Yii::app()->createUrl($this->route),
+ 'method'=>'get',
+)); ?>
+
+ <div class="row">
+ <?php echo $form->label($model,'id'); ?>
+ <?php echo $form->textField($model,'id',array('size'=>11,'maxlength'=>11)); ?>
+ </div>
+
+ <div class="row">
+ <?php echo $form->label($model,'old_value'); ?>
+ <?php echo $form->textArea($model,'old_value',array('rows'=>6, 'cols'=>50)); ?>
+ </div>
+
+ <div class="row">
+ <?php echo $form->label($model,'new_value'); ?>
+ <?php echo $form->textArea($model,'new_value',array('rows'=>6, 'cols'=>50)); ?>
+ </div>
+
+ <div class="row">
+ <?php echo $form->label($model,'action'); ?>
+ <?php echo $form->textField($model,'action',array('size'=>20,'maxlength'=>20)); ?>
+ </div>
+
+ <div class="row">
+ <?php echo $form->label($model,'model'); ?>
+ <?php echo $form->textField($model,'model',array('size'=>60,'maxlength'=>255)); ?>
+ </div>
+
+ <div class="row">
+ <?php echo $form->label($model,'field'); ?>
+ <?php echo $form->textField($model,'field',array('size'=>60,'maxlength'=>64)); ?>
+ </div>
+
+ <div class="row">
+ <?php echo $form->label($model,'stamp'); ?>
+ <?php echo $form->textField($model,'stamp'); ?>
+ </div>
+
+ <div class="row">
+ <?php echo $form->label($model,'user_id'); ?>
+ <?php echo $form->textField($model,'user_id'); ?>
+ </div>
+
+ <div class="row">
+ <?php echo $form->label($model,'model_id'); ?>
+ <?php echo $form->textField($model,'model_id',array('size'=>60,'maxlength'=>65)); ?>
+ </div>
+
+ <div class="row buttons">
+ <?php echo CHtml::submitButton('Search'); ?>
+ </div>
+
+<?php $this->endWidget(); ?>
+
+</div><!-- search-form --> \ No newline at end of file
diff --git a/protected/modules/auditTrail/views/admin/admin.php b/protected/modules/auditTrail/views/admin/admin.php
new file mode 100644
index 0000000..b34a861
--- /dev/null
+++ b/protected/modules/auditTrail/views/admin/admin.php
@@ -0,0 +1,39 @@
+<?php
+$this->breadcrumbs=array(
+ 'Audit Trails',
+);
+/*
+$this->menu=array(
+ array('label'=>'List AuditTrail', 'url'=>array('index')),
+ array('label'=>'Create AuditTrail', 'url'=>array('create')),
+);
+*/
+?>
+
+<h1>Audit Trails</h1>
+
+<?php $this->renderPartial('//common/_comparison_text'); ?>
+
+<?php $this->renderPartial('//common/_advanced_search',array(
+ 'model'=>$model,
+)); ?>
+
+<?php $this->widget('zii.widgets.grid.CGridView', array(
+ 'id'=>'audit-trail-grid',
+ 'dataProvider'=>$model->search(),
+ 'filter'=>$model,
+ 'columns'=>array(
+ 'id',
+ 'old_value',
+ 'new_value',
+ 'action',
+ 'model',
+ 'field',
+ 'stamp',
+ 'user_id',
+ 'model_id',
+// array(
+// 'class'=>'CButtonColumn',
+// ),
+ ),
+)); ?> \ No newline at end of file
diff --git a/protected/modules/auditTrail/views/default/.DS_Store b/protected/modules/auditTrail/views/default/.DS_Store
new file mode 100644
index 0000000..5008ddf
--- /dev/null
+++ b/protected/modules/auditTrail/views/default/.DS_Store
Binary files differ
diff --git a/protected/modules/auditTrail/views/default/index.php b/protected/modules/auditTrail/views/default/index.php
new file mode 100644
index 0000000..f9a114f
--- /dev/null
+++ b/protected/modules/auditTrail/views/default/index.php
@@ -0,0 +1,95 @@
+<?php
+$this->breadcrumbs=array(
+ $this->module->id,
+);
+?>
+<h1>Welcome to Audit Trail!</h1>
+<h2>Introduction</h2>
+<p>
+ This is the audit trail module. This module provides basic access to any changes performed via active record through any class that has the LoggableBehavior assigned. It is based off of <?php echo CHtml::link('this cookbook article', 'http://www.yiiframework.com/wiki/9/how-to-log-changes-of-activerecords'); ?>. I've noticed I always do the same things with it, and I hoped to help others who probably do the same thing.
+</p>
+<h2>Changes</h2>
+<p>
+ <ul>
+ <li><b><em>The widget now uses the zii Cportlet widget</em></b> and does not need the <?php echo CHtml::link('xportlet widget', 'http://www.yiiframework.com/extension/portlet/'); ?> any longer</li>
+ <li>This extension now uses migrations, so the db schema files are no longer necessary.</li>
+ </ul>
+</p>
+<h2>Requirements</h2>
+<p>
+ This module requires:
+ <ul>
+ <li>Yii 1.1.6 or higher</li>
+ <li>command line access to use database migrations</li>
+ <li>a database connection. So far this has only been tested on MySQL, but it should work on any DB as long as the initDb script is properly modified to create tables in the syntax of your RDBMS. Any RDBMS translations would be very much appreciated!</li>
+ <li>a user object with an id and a username field. The name of the class, the id field, and the username field can be overridden in the config file.</li>
+ </ul>
+</p>
+<h2>Installation</h2>
+<p>
+ If you are looking at this page, you at least enabled the module in your main.php config. Good job! Now we can continue with the installation:
+ <ol>
+ <li>Make sure your components->db is configured in protected/config/main.php</li>
+ <li>Make sure the rest of this module is set up in your protected/config/main.php. See <a href="#config">configuration</a> for help with this.</li>
+ <li>
+ Run the database migrations to create the tables for audit trail. Keep in mind that you will have to use the --migrationPath flag to tell the yiic tool where the migrations are. It should look something like this:
+ <blockquote><code>prompt:> php ./yiic.php migrate up --migrationPath=application.modules.auditTrail.migrations</code></blockquote>
+ Keep in mind that you may have to change the mirgrationPath to match where you installed the extension. My examples assumes you put it in MyWebApp/protected/modules
+ </li>
+ <li>Make sure any active record objects you want to log are using the <a href="#loggable">loggable behavior</a></li>
+ <li>
+ Add the <a href="#widget">audit trail widget</a> to any admin pages you want (optional)
+ </li>
+ <li>
+ Build in RBAC rules if using RBAC (optional). This controllers in this module automatically extend the Controller class of the current web app, so any logic you built into your app for RBAC should work fine. You may need to adjust settings in your RBAC management interface, but specific instructions depend on which implementation you are using. If you need a recommendation, I really like <a href="http://www.yiiframework.com/extension/rights/">Rights</a> by Chris83.
+ </li>
+ <li>Use the <a href="#manager">Audit Trail Manager</a> to manage your audit trail!</li>
+ </ol>
+</p>
+<h2>Parts</h2>
+<a name="config"><h3>main.php Configuration</h3></a>
+<p>
+ Please add the AuditTrail model to the import section of your main.php config file so that all models that need it can find the AR model:
+<blockquote><code><pre>
+ 'import'=>array(
+ 'application.models.*',
+ 'application.components.*',
+ 'application.modules.auditTrail.models.AuditTrail',
+ .....
+ ),
+</pre></code></blockquote>
+ Here are the following options for your main.php configurations (the defaults for all of them should work, so you may not need to use any of them, but if you need to override them you can)
+<blockquote><code><pre>
+ 'modules'=>array(
+ 'auditTrail'=>array(
+ 'userClass' => 'User', // the class name for the user object
+ 'userIdColumn' => 'id', // the column name of the primary key for the user
+ 'userNameColumn' => 'username', // the column name of the primary key for the user
+ ),
+ .......
+</pre></code></blockquote>
+</p>
+
+<a name="loggable"><h3>Loggable Behavior</h3></a>
+<p>
+ You should make sure your ActiveRecord objects use the LoggableBehavior. If you installed AuditTrail to your modules directory, this would typically be referenced by adding this function to your AR model:
+<code>public function behaviors()
+{
+ return array(
+ 'LoggableBehavior'=>
+ 'application.modules.auditTrail.behaviors.LoggableBehavior',
+ );
+}</code>
+</p>
+<a name="widget"><h3>Audit Trail Widget</h3></a>
+<p>
+ You can easily add the audit trail widget to any page that is specifcally about one row of one thing (ie: one instance of one model, like an update or view page, not like an admin or list page), and it will give you insight into changes for just that object.
+<code>$this->widget(
+ 'application.modules.auditTrail.widgets.portlets.ShowAuditTrail',
+ array(
+ 'model' => $model,
+ )
+);</code>
+</p>
+<a name="manager"><h3>Audit Trail Manager</h3></a>
+<p>The manager is just a searchable table of all audits. You can find it here: <?php echo Chtml::link('Audit Trail Manager', array('/auditTrail/admin')); ?></p>
diff --git a/protected/modules/auditTrail/widgets/.DS_Store b/protected/modules/auditTrail/widgets/.DS_Store
new file mode 100644
index 0000000..e2f4aeb
--- /dev/null
+++ b/protected/modules/auditTrail/widgets/.DS_Store
Binary files differ
diff --git a/protected/modules/auditTrail/widgets/portlets/.DS_Store b/protected/modules/auditTrail/widgets/portlets/.DS_Store
new file mode 100644
index 0000000..1e58340
--- /dev/null
+++ b/protected/modules/auditTrail/widgets/portlets/.DS_Store
Binary files differ
diff --git a/protected/modules/auditTrail/widgets/portlets/ShowAuditTrail.php b/protected/modules/auditTrail/widgets/portlets/ShowAuditTrail.php
new file mode 100644
index 0000000..eb5a94f
--- /dev/null
+++ b/protected/modules/auditTrail/widgets/portlets/ShowAuditTrail.php
@@ -0,0 +1,157 @@
+<?php
+/**
+ * ShowAuditTrail shows the audit trail for the current item
+ */
+
+Yii::import('zii.widgets.CPortlet');
+require_once(realpath(dirname(__FILE__) . '/../../AuditTrailModule.php'));
+
+class ShowAuditTrail extends CPortlet
+{
+ /**
+ * @var CActiveRecord the model you want to use with this field
+ */
+ public $model;
+
+ /**
+ * @var boolean whether or not to show the widget
+ */
+ public $visible = true;
+
+ /**
+ * @var this allows you to override individual columns' display properties in the datagrid.
+ * Column definitions should be indexed by column name, and the value should match the column
+ * format of CDataGrid. For example:
+ *
+ * 'dataGridColumnsOverride' => array(
+ * 'old_value' => array(
+ * 'name' => 'old_value',
+ * 'filter' => '',
+ * ),
+ * 'new_value' => array(
+ * 'name' => 'new_value',
+ * 'filter' => '',
+ * ),
+ * )
+ *
+ * Please do not specify a column if you do not wish to override the defaults of that column.
+ * Also, please be careful when specifying a format for user_id, as special handling exists
+ * to format the user name
+ */
+ public $dataGridColumnsOverride = array( );
+
+ /**
+ * @var AuditTrailModule static variable to hold the module so we don't have to instantiate it a million times to get config values
+ */
+ private static $__auditTrailModule;
+
+ /**
+ * Sets the title of the portlet
+ */
+ public function init() {
+ $this->title = "Audit Trail For " . get_class($this->model) . " " . $this->model->id;
+ parent::init();
+ }
+
+ /**
+ * generates content of widget the widget.
+ * This renders the widget, if it is visible.
+ */
+ public function renderContent()
+ {
+ if($this->visible) {
+ $auditTrail = AuditTrail::model()->recently();
+ $auditTrail->model = get_class($this->model);
+ $auditTrail->model_id = $this->model->primaryKey;
+ $columnFormat = $this->getColumnFormat();
+ $this->widget('zii.widgets.grid.CGridView', array(
+ 'id'=>'audit-trail-grid',
+ 'dataProvider'=>$auditTrail->search(),
+ 'columns'=> $this->getColumnFormat(),
+ ));
+ }
+ }
+
+ /**
+ * Builds the label code we need to display usernames correctly
+ * @return The code to be evaled to display the user info correctly
+ */
+ protected function getEvalUserLabelCode() {
+ $userClass = $this->getFromConfigOrObject('userClass');
+ $userNameColumn = $this->getFromConfigOrObject('userNameColumn');
+ $userEvalLabel = ' ( ($t = '
+ . $userClass
+ . '::model()->findByPk($data->user_id)) == null ? "": $t->'
+ . $userNameColumn
+ . ' ) ';
+ return $userEvalLabel;
+ }
+
+ /**
+ * Returns the value you want to look up, either from the config file or a user's override
+ * @var value The name of the value you would like to look up
+ * @return the config value you need
+ */
+ protected function getFromConfigOrObject($value) {
+ $at = Yii::app()->modules['auditTrail'];
+
+ //If we can get the value from the config, do that to save overhead
+ if( isset( $at[$value]) && !empty($at[$value] ) ) {
+ return $at[$value];
+ }
+
+ //If we cannot get the config value from the config file, get it from the
+ //instantiated object. Only instantiate it once though, its probably
+ //expensive to do. PS I feel this is a dirty trick and I don't like it
+ //but I don't know a better way
+ if(!is_object(self::$__auditTrailModule)) {
+ self::$__auditTrailModule = new AuditTrailModule(microtime(), null);
+ }
+
+ return self::$__auditTrailModule->$value;
+ }
+
+ /**
+ * Gets final column format. Starts with default column format (specified in this method
+ * and checks $this->dataGridColumnsOverride array to see if any columns need to use a
+ * user specified format.
+ * @return array The final format array, with any user specified formats taking precedent over defaults
+ */
+ protected function getColumnFormat() {
+ $evalUserLabel = $this->getEvalUserLabelCode();
+ $columnFormat = array();
+ $defaultColumnFormat = array(
+ 'old_value' => array(
+ 'name' => 'old_value',
+ 'filter' => '',
+ ),
+ 'new_value' => array(
+ 'name' => 'new_value',
+ 'filter' => '',
+ ),
+ 'action' => array(
+ 'name' => 'action',
+ 'filter'=> '',
+ ),
+ 'field' => array(
+ 'name' => 'field',
+ 'filter' => '',
+ ),
+ 'stamp' => array(
+ 'name' => 'stamp',
+ 'filter' => '',
+ ),
+ 'user_id' => array(
+ 'name' => 'user_id',
+ 'value'=>$evalUserLabel,
+ 'filter'=> '',
+ ),
+ );
+
+ foreach($defaultColumnFormat as $key => $format) {
+ $columnFormat[] = isset($this->dataGridColumnsOverride[$key]) ? $this->dataGridColumnsOverride[$key] : $defaultColumnFormat[$key];
+ }
+
+ return $columnFormat;
+ }
+} \ No newline at end of file