Skip to content
This repository has been archived by the owner on Apr 5, 2018. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Olde Hampsink committed Feb 24, 2016
2 parents 6e6b000 + de3ce3e commit e312b16
Show file tree
Hide file tree
Showing 18 changed files with 1,959 additions and 354 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
7 changes: 7 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
checks:
php:
code_rating: true
duplication: false

tools:
external_code_coverage: true
43 changes: 43 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# see http://about.travis-ci.org/docs/user/languages/php/ for more hints
language: php

# list any PHP version you want to test against
php:
# using major version aliases

# aliased to a recent 5.4.x version
- 5.4
# aliased to a recent 5.5.x version
- 5.5
# aliased to a recent 5.6.x version
- 5.6
# aliased to a recent hhvm version
- hhvm

# optionally set up exclutions and allowed failures in the matrix
matrix:
allow_failures:
- php: hhvm

# execute any number of scripts before the test run, custom env's are available as variables
before_script:
- curl -sS https://codeload.github.com/pixelandtonic/Craft-Release/zip/master > craft.zip
- unzip craft.zip
- rm craft.zip
- mv Craft-Release-master craft
- mkdir craft/config
- echo "<?php return array('user' => 'test');" > craft/config/db.php
- mkdir craft/storage
- mkdir -p craft/plugins/auditlog
- for item in *; do if [[ ! "$item" == "craft" ]]; then mv $item craft/plugins/auditlog; fi; done
- cd craft/app
- composer require mockery/mockery
- cd ../..

# execute tests
script: phpunit --bootstrap craft/app/tests/bootstrap.php --configuration craft/plugins/auditlog/phpunit.xml.dist --coverage-clover coverage.clover craft/plugins/auditlog/tests

# upload coverage to scrutinizer
after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
2 changes: 1 addition & 1 deletion AuditLogPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function getName()
*/
public function getVersion()
{
return '0.6.2';
return '0.7.0';
}

/**
Expand Down
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Audit Log plugin for Craft CMS
Audit Log plugin for Craft CMS [![Build Status](https://scrutinizer-ci.com/g/boboldehampsink/auditlog/badges/build.png?b=develop)](https://scrutinizer-ci.com/g/boboldehampsink/auditlog/build-status/develop) [![Code Coverage](https://scrutinizer-ci.com/g/boboldehampsink/auditlog/badges/coverage.png?b=develop)](https://scrutinizer-ci.com/g/boboldehampsink/auditlog/?branch=develop) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/boboldehampsink/auditlog/badges/quality-score.png?b=develop)](https://scrutinizer-ci.com/g/boboldehampsink/auditlog/?branch=develop)
=================

Plugin that allows you to log adding/updating/deleting of categories/entries/users.
Expand All @@ -12,7 +12,7 @@ Features:
- Has hooks that you can use to extend this plugin
- registerAuditLogSources
- getAuditLogTableAttributeHtml
- modifyAuditLogTableAttributes
- defineAvailableTableAttributes
- modifyAuditLogSortableAttributes
- Has events that you can listen to
- auditLog.onElementChanged
Expand All @@ -23,8 +23,20 @@ Roadmap:
Important:
The plugin's folder should be named "auditlog"

Development
=================
Run this from your Craft installation to test your changes to this plugin before submitting a Pull Request
```bash
phpunit --bootstrap craft/app/tests/bootstrap.php --configuration craft/plugins/auditlog/phpunit.xml.dist --coverage-text craft/plugins/auditlog/tests
```

Changelog
=================
###0.7.0###
- Added Craft 2.5 compatibility
- Refactored plugin for better readability, quality and testability
- All service code is now fully covered by unit tests

###0.6.2###
- Fixed a bug where the date range didn't fully work
- Fixed criteria attributes not fully working
Expand Down
11 changes: 8 additions & 3 deletions controllers/AuditLogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,15 @@ public function actionDownload()
$log = craft()->auditLog->log($criteria);

// Set status attribute
$attributes['status'] = Craft::t('Status');
$attributes = array('status' => Craft::t('Status'));

// Get table attributes
$attributes += $elementType->defineTableAttributes();
// Get nice attributes
$availableAttributes = $elementType->defineAvailableTableAttributes();

// Make 'em fit
foreach ($availableAttributes as $key => $result) {
$attributes[$key] = $result['label'];
}

// Ditch the changes button
unset($attributes['changes']);
Expand Down
140 changes: 82 additions & 58 deletions elementtypes/AuditLogElementType.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,39 +43,52 @@ public function hasStatuses()
public function getStatuses()
{
return array(
AuditLogModel::CREATED => Craft::t('Created'),
AuditLogModel::CREATED => Craft::t('Created'),
AuditLogModel::MODIFIED => Craft::t('Modified'),
AuditLogModel::DELETED => Craft::t('Deleted'),
AuditLogModel::DELETED => Craft::t('Deleted'),
);
}

/**
* Define table column names.
*
* @param string $source
* Define available table column names.
*
* @return array
*/
public function defineTableAttributes($source = null)
public function defineAvailableTableAttributes()
{
// Define default attributes
$attributes = array(
'type' => Craft::t('Type'),
'user' => Craft::t('User'),
'origin' => Craft::t('Origin'),
'dateUpdated' => Craft::t('Modified'),
'type' => array('label' => Craft::t('Type')),
'user' => array('label' => Craft::t('User')),
'origin' => array('label' => Craft::t('Origin')),
'dateUpdated' => array('label' => Craft::t('Modified')),
);

// Allow plugins to modify the attributes
craft()->plugins->call('modifyAuditLogTableAttributes', array(&$attributes, $source));
$pluginAttributes = craft()->plugins->call('defineAdditionalAuditLogTableAttributes', array(), true);
foreach ($pluginAttributes as $thisPluginAttributes) {
$attributes = array_merge($attributes, $thisPluginAttributes);
}

// Set changes at last
$attributes['changes'] = Craft::t('Changes');
$attributes['changes'] = array('label' => Craft::t('Changes'));

// Return the attributes
return $attributes;
}

/**
* Returns the default table attributes.
*
* @param string $source
*
* @return string[]
*/
public function getDefaultTableAttributes($source = null)
{
return array('type', 'user', 'origin', 'dateUpdated', 'changes');
}

/**
* Return table attribute html.
*
Expand All @@ -101,29 +114,24 @@ public function getTableAttributeHtml(BaseElementModel $element, $attribute)
case 'dateCreated':
case 'dateUpdated':
return craft()->dateFormatter->formatDateTime($element->$attribute);
break;

// Return clickable user link
case 'user':
$user = $element->getUser();

return $user ? '<a href="'.$user->getCpEditUrl().'">'.$user.'</a>' : Craft::t('Guest');
break;

// Return clickable event origin
case 'origin':
return '<a href="'.preg_replace('/'.craft()->config->get('cpTrigger').'\//', '', UrlHelper::getUrl($element->origin), 1).'">'.$element->origin.'</a>';
break;

// Return view changes button
case 'changes':
return '<a class="btn" href="'.UrlHelper::getCpUrl('auditlog/'.$element->id).'">'.Craft::t('View').'</a>';
break;

// Default behavior
default:
return $element->$attribute;
break;
}
}

Expand All @@ -135,26 +143,24 @@ public function getTableAttributeHtml(BaseElementModel $element, $attribute)
public function defineCriteriaAttributes()
{
return array(
'type' => AttributeType::String,
'userId' => AttributeType::Number,
'origin' => AttributeType::String,
'modified' => AttributeType::DateTime,
'before' => AttributeType::String,
'after' => AttributeType::String,
'status' => AttributeType::String,
'from' => AttributeType::DateTime,
'to' => AttributeType::DateTime,
'order' => array(AttributeType::String, 'default' => 'auditlog.id desc'),
'type' => AttributeType::String,
'userId' => AttributeType::Number,
'origin' => AttributeType::String,
'modified' => AttributeType::DateTime,
'before' => AttributeType::String,
'after' => AttributeType::String,
'status' => AttributeType::String,
'from' => AttributeType::DateTime,
'to' => AttributeType::DateTime,
'order' => array(AttributeType::String, 'default' => 'auditlog.id desc'),
);
}

/**
* Cancel the elements query.
* Modify the elements query.
*
* @param DbCommand $query
* @param ElementCriteriaModel $criteria
*
* @return bool
*/
public function modifyElementsQuery(DbCommand $query, ElementCriteriaModel $criteria)
{
Expand Down Expand Up @@ -190,11 +196,6 @@ public function modifyElementsQuery(DbCommand $query, ElementCriteriaModel $crit
$query->andWhere(DbHelper::parseParam('auditlog.origin', $criteria->origin, $query->params));
}

// Check for date modified
if (!empty($criteria->modified)) {
$query->andWhere(DbHelper::parseDateParam('auditlog.dateUpdated', $criteria->modified, $query->params));
}

// Check before
if (!empty($criteria->before)) {
$query->andWhere(DbHelper::parseParam('auditlog.before', $criteria->before, $query->params));
Expand All @@ -205,6 +206,31 @@ public function modifyElementsQuery(DbCommand $query, ElementCriteriaModel $crit
$query->andWhere(DbHelper::parseParam('auditlog.after', $criteria->after, $query->params));
}

// Check for status
if (!empty($criteria->status)) {
$query->andWhere(DbHelper::parseParam('auditlog.status', $criteria->status, $query->params));
}

// Dates
$this->applyDateCriteria($criteria, $query);

// Search
$this->applySearchCriteria($criteria, $query);
}

/**
* Apply date criteria.
*
* @param ElementCriteriaModel $criteria
* @param DbCommand $query
*/
private function applyDateCriteria(ElementCriteriaModel $criteria, DbCommand $query)
{
// Check for date modified
if (!empty($criteria->modified)) {
$query->andWhere(DbHelper::parseDateParam('auditlog.dateUpdated', $criteria->modified, $query->params));
}

// Check for date from
if (!empty($criteria->from)) {
$query->andWhere(DbHelper::parseDateParam('auditlog.dateUpdated', '>= '.DateTimeHelper::formatTimeForDb($criteria->from), $query->params));
Expand All @@ -215,18 +241,16 @@ public function modifyElementsQuery(DbCommand $query, ElementCriteriaModel $crit
$criteria->to->add(new DateInterval('PT23H59M59S'));
$query->andWhere(DbHelper::parseDateParam('auditlog.dateUpdated', '<= '.DateTimeHelper::formatTimeForDb($criteria->to), $query->params));
}
}

// Check for type
if (!empty($criteria->type)) {
$query->andWhere(DbHelper::parseParam('auditlog.type', $criteria->type, $query->params));
}

// Check for status
if (!empty($criteria->status)) {
$query->andWhere(DbHelper::parseParam('auditlog.status', $criteria->status, $query->params));
}

// Search
/**
* Apply search criteria.
*
* @param ElementCriteriaModel $criteria
* @param DbCommand $query
*/
private function applySearchCriteria(ElementCriteriaModel $criteria, DbCommand $query)
{
if (!empty($criteria->search)) {

// Always perform a LIKE search
Expand Down Expand Up @@ -273,37 +297,37 @@ public function getSources($context = null)
// Set default sources
$sources = array(
'*' => array(
'label' => Craft::t('All logs'),
'label' => Craft::t('All logs'),
),
array('heading' => Craft::t('Elements')),
);

// Show sources for entries when enabled
if (in_array(ElementType::Entry, $settings->enabled)) {
$sources['entries'] = array(
'label' => Craft::t('Entries'),
'criteria' => array(
'type' => ElementType::Entry,
'label' => Craft::t('Entries'),
'criteria' => array(
'type' => ElementType::Entry,
),
);
}

// Show sources for categories when enabled
if (in_array(ElementType::Category, $settings->enabled)) {
$sources['categories'] = array(
'label' => Craft::t('Categories'),
'criteria' => array(
'type' => ElementType::Category,
'label' => Craft::t('Categories'),
'criteria' => array(
'type' => ElementType::Category,
),
);
}

// Show sources for users when enabled
if (in_array(ElementType::User, $settings->enabled)) {
$sources['users'] = array(
'label' => Craft::t('Users'),
'criteria' => array(
'type' => ElementType::User,
'label' => Craft::t('Users'),
'criteria' => array(
'type' => ElementType::User,
),
);
}
Expand Down Expand Up @@ -331,10 +355,10 @@ public function getSources($context = null)
public function defineSortableAttributes()
{
// Set modified first
$attributes['dateUpdated'] = Craft::t('Modified');
$attributes = array('dateUpdated' => Craft::t('Modified'));

// Get table attributes
$attributes = array_merge($attributes, $this->defineTableAttributes());
$attributes = array_merge($attributes, parent::defineSortableAttributes());

// Unset unsortable attributes
unset($attributes['user'], $attributes['changes']);
Expand Down
Loading

0 comments on commit e312b16

Please sign in to comment.