2017 © Pedro Peláez
 

cakephp-plugin cake-model-history

ModelHistory plugin for CakePHP3

image

codekanzlei/cake-model-history

ModelHistory plugin for CakePHP3

  • Tuesday, October 17, 2017
  • by cleptric
  • Repository
  • 3 Watchers
  • 5 Stars
  • 9,629 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 36 Versions
  • 5 % Grown

The README.md

CakePHP 3 Model History Plugin, (*1)

Build Status codecov License, (*2)

CakePHP 3 Historization for database records. Keeps track of changes performed by users and provides a customizable view element for displaying them., (*3)

Requirements

Installation

1. require the plugin in your composer.json

"require": {
    "codekanzlei/cake-model-history": "2.0.*",
}

Open a terminal in your project-folder and run these commands:, (*4)

`$ composer update`

2. Configure config/bootstrap.php

Load the Plugin:, (*5)

Plugin::load('ModelHistory', ['bootstrap' => false, 'routes' => true]);

Since all changes to a record are saved to the field data (type MEDIUMBLOB) in the ModelHistoryTable in JSON format, you must use custom Type Mapping., (*6)

Type::map('json', 'CkTools\Database\Type\JsonType');

3. Create a table model_history in your project database

We have to create the database schema with help of the migrations plugin., (*7)

    $ bin/cake migrations migrate -p ModelHistory

4. AppController.php

$helpers, (*8)

public $helpers =  [
    'ModelHistory.ModelHistory'
]

Usage & Configuration:

Table setup

Add the Historizable Behavior in the initialize function of the Table you want to use model-history., (*9)

$this->addBehavior('ModelHistory.Historizable');

Note: By default, the model-history plugin attributes changes to a database record to the user that performed and saved them by comparing table-fields 'firstname' and 'lastname' in UsersTable (See $_defaultConfig in HistorizableBehavior.php for these default settings). If your fields are not called 'firstname' and 'lastname', you can easily customize these settings according to the fieldnames in your UsersTable, like so:, (*10)

$this->addBehavior('ModelHistory.Historizable', [
    'userNameFields' => [
        'firstname' => 'User.your_first_name_field',
        'lastname' => 'Users.your_last_name_field',
        'id' => 'Users.id'
    ],
    'userIdCallback' => null,
    'entriesToShow' => 10,
    'fields' => [],
    'associations' => [],
    'ignoreFields' => []
]);

By default, the ModelHistory monitors changes for every field it finds in the schema of the table. It tries to deduct the type to use from the data type and obfuscates all fields containing "password". Otherwise, the default values are 'searchable' => true, 'saveable' => true and 'obfuscated' => false., (*11)

To override specific settings, add an array for the field into the 'fields' array and list the values you want to override., (*12)

$this->addBehavior('ModelHistory.Historizable', [
    'fields' => [
    // The field name
        'firstname' =>[
            // Allowed translation forms: String, Closure returning string
            // Its recommended to use the closure, so translations are made after core initialize when the correct language was set.
            'translation' => function () {
                return __('user.firstname');
            },
            // The searchable indicator is used to show the field in the filter box
            // defaults to true
            'searchable' => true,
            // The savable indicator is used to decide whether the field is tracked
            // defaults to true
            'saveable' => true,
            // obfuscate the values of the field in the history view.
            // defaults to false except for fieldnames containing "password"
            'obfuscated' => false,
            // Allowed: string, bool, number, relation, date, hash, array, association, mass_association.
            'type' => 'string',
            // Optional display parser to modify the value before displaying it,
            // if no displayParser is found, the \ModelHistory\Model\Transform\{$type}Transformer is used.
            'displayParser' => function ($fieldname, $value, $entity) {
                return $value;
            },
            // Optional save parser to modify the value before saving the history entry
            'saveParser' => function ($fieldname, $value, $entity) {
                return $value;
            },
        ],
    ]
]);

Because the default monitored fields are limited to those in the table, n:m associations and associations with foreign_keys in other tables always have to be configured, at least with the type. Here are three real world examples from a UsersTable:, (*13)

$this->addBehavior('ModelHistory.Historizable', [
    'fields' => [
        'customer_id' => [
            'saveable' => false,
            'type' => 'association'
        ],
        'regions' => [
            'type' => 'mass_association',
            'displayParser' => function ($fieldName, $value, $entity) {
                if (is_array($value)) {
                    return implode(', ', $value);
                }

                return $value;
            }
        ],
        'contact' => [
            'type' => 'string',
            'saveParser' => function ($fieldName, $value, $entity) {
                return __('user.associated_contact');
            },
            'displayParser' => function ($fieldName, $value, $entity) {
                return __('user.associated_contact');
            }
        ],
    ]
]);

Types:, (*14)

  • string: for string values.
  • bool: for bool values.
  • number: for integer values.
  • date: for date values.
  • hash: for associative arrays.
  • array: for sequential (indexed) arrays.
  • relation: for 1 to n relations.
  • association: for n to m relations.
  • mass_association: for n to m relations which shall be condensed into one history entry.

The three types relation, association and mass_association are able to link to associated entities. The url defaults to:, (*15)

'url' => [
    'plugin' => null,
    'controller' => $entityController // automatically set to the entities controller
    'action' => 'view'
]

The default url can be overwritten: url in the behavior-array overwrites default and the defined url in the field-config has the highest priority., (*16)

$this->addBehavior('ModelHistory.Historizable', [
    'url' => [
        'plugin' => 'Admin',
        'action' => 'index'
    ],
    'fields' => [
        'firstname' => [
            'translation' => function () {
                return __('user.firstname');
            },
            'searchable' => true,
            'saveable' => true,
            'obfuscated' => false,
            'type' => 'relation',
            'url' => [
                'plugin' => 'Special',
                'action' => 'show'
            ]
        ],
    ]
]);

If saved association's entries shall be viewed within the source entities entries, you can specify the associations key. It has to match the object property keys given in the source entity. Furthermore the model history area has to get the option includeAssociated with value true., (*17)

$this->addBehavior('ModelHistory.Historizable', [
    'fields' => [
        ...
    ],
    'associations' => [
        'contact',
        'contact.address'
    ]
]);

To further specify the context in which the entity was saved and in order to gather additional information, you can implement \ModelHistory\Model\Entity\HistoryContextTrait inside your entity. You have to call setHistoryContext on the entity to add the context information. Currently there are three context types: ModelHistory::CONTEXT_TYPE_CONTROLLER, ModelHistory::CONTEXT_TYPE_SHELL and ModelHistory::CONTEXT_TYPE_SLUG. The optional slug gets translated automatically, when its defined in the entities typeDescriptions., (*18)

    /**
     * Index action of a controller
     */
    public function index()
    {
        if ($this->request->is(['post'])) {
            $entity = $this->Table->newEntity($this->request->data);
            $entity->setHistoryContext(ModelHistory::CONTEXT_TYPE_CONTROLLER, $this->request, 'optional_slug');
            $this->Table->save($entity);
        }
    }

You can also specify a context getter inside an entity to search for defined contexts. Please keep in mind that you have to use the TypeAwareTrait from the CkTools:, (*19)

    use \CkTools\Utility\TypeAwareTrait;

    /**
     * Retrieve defined contexts
     *
     * @return void
     */
    public static function getContexts()
    {
        return self::getTypeMap(
            self::CONTEXT_TYPE_FORGOT_PASSWORD
        );
    }

Ignoring fields, (*20)

By default, the fields id, created and modified are not tracked. If you want to overwrite which fields are ignored or not, give ignoreFields in the config array and only those fields will be ignored:, (*21)

$this->addBehavior('ModelHistory.Historizable', [
    'ignoreFields' => [
        'secret_field',
        'created'
    ]
]);

View setup

Use ModelHistoryHelper.php to create neat view elements containing a record's change history with one call in your view:, (*22)

<?= $this->ModelHistory->modelHistoryArea($user); ?>

modelHistoryArea has the following Options:, (*23)

  • showCommentBox (false), (*24)

    Additionally renders an comment field (input type text). User input will be saved to the model_history table, (*25)

  • showFilterBox (false), (*26)

    Additionally renders a filter box which can be used to search for entries., (*27)

  • includeAssociated (false), (*28)

    Additionally include all associations saved., (*29)

For the modelHistoryArea to fetch its data, add the 'ModelHistory' component to the baseComponents property in your Frontend.AppController under /webroot/js/app/app_controller.js. If you haven't set up the FrontendBridge yet, follow these steps. There you will also find a template for this file., (*30)

Make sure app_controller.js is loaded on the page where you want to show the modelHistoryArea. Then the ModelHistory JS-Component will make AJAX requests to /model_history/ModelHistory/index/$modelName/$primaryKey according to the $entity you gave the helper method and populate the modelHistoryArea by itself., (*31)

The Versions

29/09 2017

dev-feature/convention-over-configuration

dev-feature/convention-over-configuration

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

 

The Development Requires

17/07 2017
28/06 2017
17/05 2017
17/05 2017
17/05 2017

dev-feature/save-associations

dev-feature/save-associations

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

 

The Development Requires

30/03 2017
07/03 2017

dev-transformers

dev-transformers

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

 

The Development Requires

15/02 2017

v2.0.15

2.0.15.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

 

The Development Requires

15/02 2017

v2.0.14

2.0.14.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

 

The Development Requires

13/02 2017

v2.0.13

2.0.13.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

 

The Development Requires

08/02 2017

v2.0.12

2.0.12.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

 

The Development Requires

03/02 2017

v2.0.11

2.0.11.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

 

The Development Requires

02/02 2017

v2.0.10

2.0.10.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

 

The Development Requires

18/01 2017

v2.0.9

2.0.9.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

 

The Development Requires

13/01 2017

v2.0.8

2.0.8.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

 

The Development Requires

12/01 2017

v2.0.7

2.0.7.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

 

The Development Requires

09/12 2016

v2.0.6

2.0.6.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

 

The Development Requires

06/12 2016

v2.0.5

2.0.5.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

 

The Development Requires

30/11 2016

v2.0.4

2.0.4.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

 

The Development Requires

30/11 2016

v2.0.3

2.0.3.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

 

The Development Requires

30/11 2016

v2.0.2

2.0.2.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

 

The Development Requires

28/11 2016

v2.0.1

2.0.1.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

 

The Development Requires

25/11 2016

v2

2.0.0.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

 

The Development Requires

02/08 2016

v1.0.9

1.0.9.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

02/08 2016

v1.0.8

1.0.8.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

19/07 2016

v1.0.7

1.0.7.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

19/07 2016

v1.0.6

1.0.6.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

15/07 2016

v1.0.5

1.0.5.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

08/07 2016

v1.0.4

1.0.4.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

20/06 2016

v1.0.3

1.0.3.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

21/03 2016

v1.0.2

1.0.2.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

24/04 2015

v1.0.1

1.0.1.0

ModelHistory plugin for CakePHP3

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

25/03 2015

v1.0.0

1.0.0.0

ModelHistory plugin for CakePHP

  Sources   Download

The Requires

  • php >=5.4

 

The Development Requires