Skip to content

Commit

Permalink
Adicionando a possibilidade de campo do tipo json
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffersonsimaogoncalves committed Nov 8, 2019
1 parent 5ef2f69 commit d7651de
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
40 changes: 29 additions & 11 deletions src/Model/Behavior/UserActivityBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ private function getId()
*/
public function afterSave(Event $event, Entity $entity, ArrayObject $options)
{
TableRegistry::getTableLocator()->remove('JeffersonSimaoGoncalves/UserActivity.Logs');
TableRegistry::getTableLocator()->remove('JeffersonSimaoGoncalves/UserActivity.LogsDetails');
TableRegistry::getTableLocator()
->remove('JeffersonSimaoGoncalves/UserActivity.Logs');
TableRegistry::getTableLocator()
->remove('JeffersonSimaoGoncalves/UserActivity.LogsDetails');
/** @var \JeffersonSimaoGoncalves\UserActivity\Model\Table\LogsTable $Logs */
$Logs = TableRegistry::getTableLocator()->get('JeffersonSimaoGoncalves/UserActivity.Logs');
/** @var \JeffersonSimaoGoncalves\UserActivity\Model\Table\LogsDetailsTable $LogsDetails */
Expand All @@ -101,8 +103,16 @@ public function afterSave(Event $event, Entity $entity, ArrayObject $options)
}
$field = $LogsDetails->newEntity();
$field->field_name = $property;
$field->new_value = $entity->get($property);
$field->old_value = $entity->isNew() ? null : $entity->getOriginal($property);
if ($tableEntity->getSchema()->getColumnType($property) === 'json') {
$field->new_value = json_encode($entity->get($property));
} else {
$field->new_value = $entity->get($property);
}
if ($tableEntity->getSchema()->getColumnType($property) === 'json') {
$field->old_value = $entity->isNew() ? null : json_encode($entity->get($property));
} else {
$field->old_value = $entity->isNew() ? null : $entity->get($property);
}
array_push($listField, $field);
}
}
Expand All @@ -125,7 +135,7 @@ public function afterSave(Event $event, Entity $entity, ArrayObject $options)
$log->created_by = $this->getId();
$log->name = $this->getName();
$log->recycle = false;
$log->description = __('{0} a record in {1} successfully', $entity->isNew() ? __('Create') : __('Update'), $entity->getSource());
$log->description = __('{0} um registro em {1} com sucesso', $entity->isNew() ? __('Criado') : __('Atualizado'), $entity->getSource());

if ($Logs->save($log)) {
foreach ($listField as $field) {
Expand Down Expand Up @@ -155,8 +165,10 @@ private function getName()
*/
public function afterDelete(Event $event, Entity $entity, ArrayObject $options)
{
TableRegistry::getTableLocator()->remove('JeffersonSimaoGoncalves/UserActivity.Logs');
TableRegistry::getTableLocator()->remove('JeffersonSimaoGoncalves/UserActivity.LogsDetails');
TableRegistry::getTableLocator()
->remove('JeffersonSimaoGoncalves/UserActivity.Logs');
TableRegistry::getTableLocator()
->remove('JeffersonSimaoGoncalves/UserActivity.LogsDetails');
/** @var \JeffersonSimaoGoncalves\UserActivity\Model\Table\LogsTable $Logs */
$Logs = TableRegistry::getTableLocator()->get('JeffersonSimaoGoncalves/UserActivity.Logs');
/** @var \JeffersonSimaoGoncalves\UserActivity\Model\Table\LogsDetailsTable $LogsDetails */
Expand All @@ -175,7 +187,11 @@ public function afterDelete(Event $event, Entity $entity, ArrayObject $options)
$field = $LogsDetails->newEntity();
$field->field_name = $property;
$field->new_value = null;
$field->old_value = $entity->isNew() ? null : $entity->get($property);
if ($tableEntity->getSchema()->getColumnType($property) === 'json') {
$field->old_value = $entity->isNew() ? null : json_encode($entity->get($property));
} else {
$field->old_value = $entity->isNew() ? null : $entity->get($property);
}
array_push($listField, $field);
}
}
Expand All @@ -186,7 +202,9 @@ public function afterDelete(Event $event, Entity $entity, ArrayObject $options)
}

$configLog = $Logs->getConnection()->config();
$configEntity = TableRegistry::getTableLocator()->get($entity->getSource())->getConnection()->config();
$configEntity = TableRegistry::getTableLocator()->get($entity->getSource())
->getConnection()
->config();

$database = isset($configEntity['database']) ? $configEntity['database'] : $configLog['database'];

Expand All @@ -198,7 +216,7 @@ public function afterDelete(Event $event, Entity $entity, ArrayObject $options)
$log->name = $this->getName();
$log->recycle = true;
$log->primary_key = json_encode($primary_key);
$log->description = __('Temporary deleted record {0} successfully', $entity->getSource());
$log->description = __('Registro excluído temporário {0} com êxito', $entity->getSource());
if ($Logs->save($log)) {
foreach ($listField as $field) {
/** @var \JeffersonSimaoGoncalves\UserActivity\Model\Entity\LogsDetail $field */
Expand All @@ -207,4 +225,4 @@ public function afterDelete(Event $event, Entity $entity, ArrayObject $options)
}
}
}
}
}
12 changes: 6 additions & 6 deletions src/Model/Table/LogsDetailsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,20 @@ public function initialize(array $config)
public function validationDefault(Validator $validator)
{
$validator
->add('id', 'valid', ['rule' => 'numeric'])
->allowEmpty('id', 'create');
->integer('id')
->allowEmptyString('id', null, 'create');

$validator
->allowEmpty('object_file');
->allowEmptyString('object_file');

$validator
->allowEmpty('field_name');
->allowEmptyString('field_name');

$validator
->allowEmpty('new_value');
->allowEmptyString('new_value');

$validator
->allowEmpty('old_value');
->allowEmptyString('old_value');

return $validator;
}
Expand Down
18 changes: 9 additions & 9 deletions src/Model/Table/LogsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,34 @@ public function initialize(array $config)
public function validationDefault(Validator $validator)
{
$validator
->add('id', 'valid', ['rule' => 'numeric'])
->allowEmpty('id', 'create');
->integer('id')
->allowEmptyString('id', null, 'create');

$validator
->requirePresence('table_name', 'create')
->notEmpty('table_name');
->notEmptyString('table_name');

$validator
->allowEmpty('database_name');
->allowEmptyString('database_name');

$validator
->requirePresence('action', 'create')
->notEmpty('action');
->notEmptyString('action');

$validator
->requirePresence('description', 'create')
->notEmpty('description');
->notEmptyString('description');

$validator
->add('recycle', 'valid', ['rule' => 'boolean'])
->requirePresence('recycle', 'create')
->notEmpty('recycle');
->notEmptyString('recycle');

$validator
->allowEmpty('created_by');
->allowEmptyString('created_by');

$validator
->allowEmpty('name');
->allowEmptyString('name');

return $validator;
}
Expand Down

0 comments on commit d7651de

Please sign in to comment.