Skip to content

Commit

Permalink
change source_id to user_id, update tables
Browse files Browse the repository at this point in the history
  • Loading branch information
imsamurai committed Oct 27, 2014
1 parent c0fa6bc commit d830807
Show file tree
Hide file tree
Showing 22 changed files with 1,171 additions and 79 deletions.
40 changes: 25 additions & 15 deletions Config/Schema/schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@ public function after($event = array()) {
* @access public
*/
public $audit_deltas = array(
'id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36, 'key' => 'primary', 'collate' => 'utf8_general_ci', 'comment' => '', 'charset' => 'utf8'),
'audit_id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36, 'key' => 'index', 'collate' => 'utf8_general_ci', 'comment' => '', 'charset' => 'utf8'),
'property_name' => array('type' => 'string', 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'charset' => 'utf8'),
'old_value' => array('type' => 'text', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'charset' => 'utf8'),
'new_value' => array('type' => 'text', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'charset' => 'utf8'),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1), 'audit_id' => array('column' => 'audit_id', 'unique' => 0)),
'id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36, 'key' => 'primary', 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'audit_id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36, 'key' => 'index', 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'property_name' => array('type' => 'string', 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'old_value' => array('type' => 'text', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'new_value' => array('type' => 'text', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'indexes' => array(
'PRIMARY' => array('column' => 'id', 'unique' => 1),
'audit_id' => array('column' => 'audit_id', 'unique' => 0)
),
'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'MyISAM')
);

Expand All @@ -67,15 +70,22 @@ public function after($event = array()) {
* @access public
*/
public $audits = array(
'id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36, 'key' => 'primary', 'collate' => 'utf8_general_ci', 'comment' => '', 'charset' => 'utf8'),
'event' => array('type' => 'string', 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'charset' => 'utf8'),
'model' => array('type' => 'string', 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'charset' => 'utf8'),
'entity_id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36, 'collate' => 'utf8_general_ci', 'comment' => '', 'charset' => 'utf8'),
'json_object' => array('type' => 'text', 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'charset' => 'utf8'),
'description' => array('type' => 'text', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'charset' => 'utf8'),
'source_id' => array('type' => 'string', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'charset' => 'utf8'),
'created' => array('type' => 'datetime', 'null' => false, 'default' => null, 'collate' => null, 'comment' => ''),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)),
'id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36, 'key' => 'primary', 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'event' => array('type' => 'string', 'null' => false, 'default' => null, 'key' => 'index', 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'model' => array('type' => 'string', 'null' => false, 'default' => null, 'key' => 'index', 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'entity_id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'json_object' => array('type' => 'text', 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'description' => array('type' => 'text', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'user_id' => array('type' => 'biginteger', 'null' => true, 'default' => '0', 'unsigned' => true, 'key' => 'index'),
'created' => array('type' => 'datetime', 'null' => false, 'default' => null, 'key' => 'index'),
'indexes' => array(
'PRIMARY' => array('column' => 'id', 'unique' => 1),
'id_UNIQUE' => array('column' => 'id', 'unique' => 1),
'event' => array('column' => 'event', 'unique' => 0),
'model' => array('column' => 'model', 'unique' => 0),
'user_id' => array('column' => 'user_id', 'unique' => 0),
'created' => array('column' => 'created', 'unique' => 0)
),
'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'MyISAM')
);

Expand Down
65 changes: 16 additions & 49 deletions Config/Schema/schema.sql
Original file line number Diff line number Diff line change
@@ -1,59 +1,26 @@
-- phpMyAdmin SQL Dump
-- version 3.4.5deb1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Mar 11, 2012 at 10:10 PM
-- Server version: 5.1.58
-- PHP Version: 5.3.6-13ubuntu3.6

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `42viral_default`
--

-- --------------------------------------------------------

--
-- Table structure for table `audits`
--
CREATE TABLE `audit_deltas` (
`id` varchar(36) NOT NULL,
`audit_id` varchar(36) NOT NULL,
`property_name` varchar(255) NOT NULL,
`old_value` text,
`new_value` text,
PRIMARY KEY (`id`),
KEY `audit_id` (`audit_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `audits` (
CREATE TABLE `audits` (
`id` varchar(36) NOT NULL,
`event` varchar(255) NOT NULL,
`model` varchar(255) NOT NULL,
`entity_id` varchar(36) NOT NULL,
`json_object` text NOT NULL,
`description` text,
`source_id` varchar(255) DEFAULT NULL,
`user_id` bigint(20) unsigned DEFAULT '0',
`created` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table `audit_deltas`
--

CREATE TABLE IF NOT EXISTS `audit_deltas` (
`id` varchar(36) NOT NULL,
`audit_id` varchar(36) NOT NULL,
`property_name` varchar(255) NOT NULL,
`old_value` text,
`new_value` text,
PRIMARY KEY (`id`),
KEY `audit_id` (`audit_id`)
UNIQUE KEY `id_UNIQUE` (`id`),
KEY `event` (`event`),
KEY `model` (`model`),
KEY `user_id` (`user_id`),
KEY `created` (`created`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
21 changes: 21 additions & 0 deletions Config/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* Author: imsamurai <im.samuray@gmail.com>
* Date: 27.10.2014
* Time: 12:51:06
*/
Configure::write('Pagination.pages', Configure::read('Pagination.pages') ? Configure::read('Pagination.pages') : 10);
$config = (array)Configure::read('AuditLog');
$config = Hash::mergeDiff($config, array(
'User' => array(
'name' => 'email',
'url' => array(
'plugin' => false,
'controller' => 'users',
'action' => 'view',
'___id___'
)
)
));
Configure::write('AuditLog', $config);
147 changes: 147 additions & 0 deletions Controller/AuditController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

/**
* Author: imsamurai <im.samuray@gmail.com>
* Date: 27.10.2014
* Time: 12:36:50
* Format: http://book.cakephp.org/2.0/en/controllers.html
*/
App::uses('AuditLogAppController', 'AuditLog.Controller');

/**
* AuditController
*
* @property Audit $Audit Audit model
* @property User $User User model
*
* @package AuditLog
* @subpackage Controller
*/
class AuditController extends AuditLogAppController {

/**
* {@inheritdoc}
*
* @var array
*/
public $uses = array(
'User', 'AuditLog.Audit'
);

/**
* {@inheritdoc}
*
* @var array
*/
public $helpers = array(
'AuditLog.Audit', 'Time'
);

/**
* List of audit records
*/
public function index() {
$this->request->data('Audit', $this->request->query);
$list = $this->request->data('Audit.list');
$count = $this->request->data('Audit.count');
$this->paginate = array(
'Audit' => array(
'limit' => ($list && $count) ? $count : Configure::read('Pagination.limit'),
'fields' => array(
'id',
'event',
'model',
'created',
'user_id',
'entity_id'
),
'contain' => array(
'User' => array(
'fields' => array(
'id',
Configure::read('AuditLog.User.name')
)
)
),
'conditions' => $this->_paginationFilter(),
'order' => array('created' => 'desc'),
)
);
$this->set(array(
'data' => $this->paginate("Audit"),
'users' => $this->User->find('list', array(
'fields' => array('id', Configure::read('AuditLog.User.name'))
)),
'_serialize' => array('data')
));
if ($list) {
$this->render('list');
}
}

/**
* View audit details
*
* @param string $id
* @throws NotFoundException
*/
public function view($id) {
$data = $this->Audit->find('first', array(
'contain' => array(
'Delta',
'User' => array(
'fields' => array(
'id',
Configure::read('AuditLog.User.name')
)
)
),
'conditions' => array(
$this->Audit->alias . '.id' => $id
)
));
if (!$data) {
throw new NotFoundException(__("Audit #%s does not exists!", $id));
}
$this->set('data', $data);
}

/**
* Builds pagination conditions from search form
*
* @return array
*/
protected function _paginationFilter() {
$conditions = array_filter($this->request->query, function($var) {
return $var !== '';
});
unset($conditions['url']);
foreach (array('created') as $dateRangeField) {
if (empty($conditions[$dateRangeField])) {
continue;
}
if (preg_match('/^(?P<start>.*)\s(-|to)\s(?P<end>.*)$/is', $conditions[$dateRangeField], $range)) {
$conditions[$this->Audit->alias . '.' . $dateRangeField . ' BETWEEN ? AND ?'] = array(
(new DateTime($range['start']))->format('Y-m-d H:i:s'),
(new DateTime($range['end']))->format('Y-m-d H:i:s')
);
}
unset($conditions[$dateRangeField]);
}

if (!empty($conditions['model'])) {
$conditions['LOWER('.$this->Audit->alias.'.model) LIKE'] = "%" . mb_strtolower($conditions['model']) . "%";
}
unset($conditions['model']);

if (!empty($conditions['id'])) {
$conditions[$this->Audit->alias . '.id'] = $conditions['id'];
}
unset($conditions['id']);
unset($conditions['list']);
unset($conditions['count']);

return $conditions;
}

}
19 changes: 19 additions & 0 deletions Controller/AuditLogAppController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* Author: imsamurai <im.samuray@gmail.com>
* Date: 27.10.2014
* Time: 12:28:40
* Format: http://book.cakephp.org/2.0/en/controllers.html
*/
App::uses('AppController', 'Controller');

/**
* AuditLogAppController
*
* @package AuditLog
* @subpackage Controller
*/
class AuditLogAppController extends AppController {

}
46 changes: 46 additions & 0 deletions Model/Audit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/**
* Author: imsamurai <im.samuray@gmail.com>
* Date: 27.10.2014
* Time: 12:27:44
* Format: http://book.cakephp.org/2.0/en/models.html
*/
App::uses('AuditLogAppModel', 'AuditLog.Model');

/**
* Audit Model
*
* @package AuditLog
* @subpackage Model
*/
class Audit extends AuditLogAppModel {

/**
* {@inheritdoc}
*
* @var string
*/
public $name = 'Audit';

/**
* {@inheritdoc}
*
* @var array
*/
public $hasMany = array(
'Delta' => array(
'className' => 'AuditLog.AuditDelta'
)
);

/**
* {@inheritdoc}
*
* @var array
*/
public $belongsTo = array(
'User'
);

}
26 changes: 26 additions & 0 deletions Model/AuditDelta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* Author: imsamurai <im.samuray@gmail.com>
* Date: 27.10.2014
* Time: 12:27:44
* Format: http://book.cakephp.org/2.0/en/models.html
*/
App::uses('AuditLogAppModel', 'AuditLog.Model');

/**
* AuditDelta Model
*
* @package AuditLog
* @subpackage Model
*/
class AuditDelta extends AuditLogAppModel {

/**
* {@inheritdoc}
*
* @var string
*/
public $name = 'AuditDelta';

}
Loading

0 comments on commit d830807

Please sign in to comment.