diff --git a/src/Model/Behavior/UserActivityBehavior.php b/src/Model/Behavior/UserActivityBehavior.php index 7150902..819c62b 100644 --- a/src/Model/Behavior/UserActivityBehavior.php +++ b/src/Model/Behavior/UserActivityBehavior.php @@ -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 */ @@ -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); } } @@ -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) { @@ -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 */ @@ -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); } } @@ -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']; @@ -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 */ @@ -207,4 +225,4 @@ public function afterDelete(Event $event, Entity $entity, ArrayObject $options) } } } -} \ No newline at end of file +} diff --git a/src/Model/Table/LogsDetailsTable.php b/src/Model/Table/LogsDetailsTable.php index 3a897b8..c31c2ba 100644 --- a/src/Model/Table/LogsDetailsTable.php +++ b/src/Model/Table/LogsDetailsTable.php @@ -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; } diff --git a/src/Model/Table/LogsTable.php b/src/Model/Table/LogsTable.php index 4083bc8..f45ebc3 100644 --- a/src/Model/Table/LogsTable.php +++ b/src/Model/Table/LogsTable.php @@ -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; }