PHP code example of elstc / cakephp-activity-logger
1. Go to this page and download the library: Download elstc/cakephp-activity-logger library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
elstc / cakephp-activity-logger example snippets
class ArticlesTable extends Table
{
public function initialize(array $config): void
{
// ...
$this->addBehavior('Elastic/ActivityLogger.Logger', [
'scope' => [
'Articles',
'Authors',
],
]);
}
}
$artice = $this-Articles->newEnity([ /* ... */ ]);
$this->Articles->save($artice);
// saved log
// [action='create', scope_model='Articles', scope_id=$article->id]
$artice = $this-Articles->patchEnity(artice, [ /* ... */ ]);
$this->Articles->save($artice);
// saved log
// [action='update', scope_model='Articles', scope_id=$article->id]
$artice = $this-Articles->get($id);
$this->Articles->delete($artice);
// saved log
// [action='delete', scope_model='Articles', scope_id=$article->id]
$this->Articles->setLogIssuer($author); // Set issuer
$artice = $this-Articles->newEnity([ /* ... */ ]);
$this->Articles->save($artice);
// saved log
// [action='create', scope_model='Articles', scope_id=$article->id, ...]
// and
// [action='create', scope_model='Auhtors', scope_id=$author->id, ...]
// In AppController
class AppController extends Controller
{
public function initialize(): void
{
// ...
$this->loadComponent('Elastic/ActivityLogger.AutoIssuer', [
'userModel' => 'Users',
]);
// ...
}
}
// In AppController
class AppController extends Controller
{
public function initialize(): void
{
$this->loadModel('Articles');
$this->loadModel('Awesome.Favorites');
// ...
$this->loadComponent('Elastic/ActivityLogger.AutoIssuer', [
'userModel' => 'Users',
'initializedTables' => [
'Articles',
'Awesome.Favorites',
],
]);
// ...
}
}
class CommentsTable extends Table
{
public function initialize(array $config): void
{
// ...
$this->addBehavior('Elastic/ActivityLogger.Logger', [
'scope' => [
'Articles',
'Authors',
'Users',
],
]);
}
}
$this->Comments->setLogScope([$user, $article]); // Set scope
$comment = $this-Comments->newEnity([ /* ... */ ]);
$this->Comments->save($comment);
// saved log
// [action='create', scope_model='Users', scope_id=$article->id, ...]
// and
// [action='create', scope_model='Articles', scope_id=$author->id, ...]
class ArticlesTable extends Table
{
public function initialize(array $config): void
{
// ...
$this->addBehavior('Elastic/ActivityLogger.Logger', [
'scope' => [
'Articles',
'Authors',
],
]);
// ADD THIS
$this->setLogMessageBuilder(static function (ActivityLog $log, array $context) {
if ($log->message !== null) {
return $log->message;
}
$message = '';
$object = $context['object'] ?: null;
$issuer = $context['issuer'] ?: null;
switch ($log->action) {
case ActivityLog::ACTION_CREATE:
$message = sprintf('%3$s created #%1$s: "%2$s"', $object->id, $object->title, $issuer->username);
break;
case ActivityLog::ACTION_UPDATE:
$message = sprintf('%3$s updated #%1$s: "%2$s"', $object->id, $object->title, $issuer->username);
break;
case ActivityLog::ACTION_DELETE:
$message = sprintf('%3$s deleted #%1$s: "%2$s"', $object->id, $object->title, $issuer->username);
break;
default:
break;
}
return $message;
});
}
}
$this->Articles->setLogMessage('Custom Message');
$this->Articles->save($entity);
// saved log
// [action='update', 'message' => 'Custom Messages', ...]
$this->Articles->activityLog(\Psr\Log\LogLevel::NOTICE, 'Custom Messages', [
'action' => 'custom',
'object' => $artice,
]);
// saved log
// [action='custom', 'message' => 'Custom Messages', scope_model='Articles', scope_id=$article->id, ...]
$logs = $this->Articles->find('activity', ['scope' => $article]);
$this->addPlugin('Elastic/ActivityLogger');