diff --git a/Admin/Installer.php b/Admin/Installer.php index 32f397f..f3409fb 100644 --- a/Admin/Installer.php +++ b/Admin/Installer.php @@ -42,13 +42,13 @@ class Installer extends InstallerAbstract $dbPool->get()->con->prepare( 'CREATE TABLE if NOT EXISTS `' . $dbPool->get()->prefix . 'auditor_audit` ( `auditor_audit_id` int(11) NOT NULL AUTO_INCREMENT, - `auditor_audit_module` int(11) NOT NULL, - `auditor_audit_ref` int(11) NOT NULL, + `auditor_audit_module` varchar(255) DEFAULT NULL, + `auditor_audit_ref` int(11) DEFAULT NULL, `auditor_audit_type` smallint(3) NOT NULL, `auditor_audit_subtype` smallint(3) NOT NULL, - `auditor_audit_content` text NOT NULL, - `auditor_audit_old` text NOT NULL, - `auditor_audit_new` text NOT NULL, + `auditor_audit_content` text DEFAULT NULL, + `auditor_audit_old` text DEFAULT NULL, + `auditor_audit_new` text DEFAULT NULL, `auditor_audit_created_at` datetime NOT NULL, `auditor_audit_created_by` int(11) NOT NULL, `auditor_audit_ip` int(11) NOT NULL, diff --git a/Controller/ApiController.php b/Controller/ApiController.php index 6c19ecf..e58e732 100644 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -30,7 +30,7 @@ use Modules\Auditor\Models\AuditMapper; final class ApiController extends Controller { public function apiLogCreate( - Account $account, + $account, $old, $new, int $type = 0, @@ -40,12 +40,14 @@ final class ApiController extends Controller string $content = null ) : void { - $audit = new Audit($account, null, $new->__toString(), $type, $subtype, $module, $ref, $content); + $newString = $this->stringify($new); + $audit = new Audit($account, null, $newString, $type, $subtype, $module, $ref, $content); + AuditMapper::create($audit); } public function apiLogUpdate( - Account $account, + $account, $old, $new, int $type = 0, @@ -55,12 +57,15 @@ final class ApiController extends Controller string $content = null ) : void { - $audit = new Audit($account, $old->__toString(), $new->__toString(), $type, $subtype, $module, $ref, $content); + $oldString = $this->stringify($old); + $newString = $this->stringify($new); + $audit = new Audit($account, $oldString, $newString, $type, $subtype, $module, $ref, $content); + AuditMapper::create($audit); } public function apiLogDelete( - Account $account, + $account, $old, $new, int $type = 0, @@ -70,7 +75,28 @@ final class ApiController extends Controller string $content = null ) : void { - $audit = new Audit($account, $new->__toString(), null, $type, $subtype, $module, $ref, $content); + $oldString = $this->stringify($old); + $audit = new Audit($account, $oldString, null, $type, $subtype, $module, $ref, $content); + AuditMapper::create($audit); } + + private function stringify($element) : ?string + { + $stringified = ''; + + if ($element instanceof \JsonSerializable) { + $stringified = \json_encode($element); + } elseif ($element instanceof \Serializable) { + $stringified = $element->serialize(); + } elseif (\is_string($element)) { + $stringified = $element; + } elseif ($element === null) { + return null; + } else { + $stringified = $element->__toString(); + } + + return $stringified; + } } diff --git a/Models/Audit.php b/Models/Audit.php index 922cd60..173d3ef 100644 --- a/Models/Audit.php +++ b/Models/Audit.php @@ -34,14 +34,6 @@ class Audit */ private $id = 0; - /** - * Audit account. - * - * @var int|Account - * @since 1.0.0 - */ - private $account = 0; - /** * Audit type. * @@ -140,9 +132,9 @@ class Audit * @since 1.0.0 */ public function __construct( - Account $account, - ?string $old, - ?string $new, + $account = 0, + string $old = null, + string $new = null, int $type = 0, int $subtype = 0, string $module = null, @@ -150,7 +142,7 @@ class Audit string $content = null ) { $this->createdAt = new \DateTime('now'); - $this->account = $account; + $this->createdBy = $account; $this->old = $old; $this->new = $new; $this->type = $type; @@ -160,6 +152,18 @@ class Audit $this->content = $content; } + /** + * Get id. + * + * @return int + * + * @since 1.0.0 + */ + public function getId() : int + { + return $this->id; + } + /** * Get type. * diff --git a/Models/AuditMapper.php b/Models/AuditMapper.php index 8b0655c..3bbf4b0 100644 --- a/Models/AuditMapper.php +++ b/Models/AuditMapper.php @@ -42,7 +42,7 @@ final class AuditMapper extends DataMapperAbstract 'auditor_audit_created_by' => ['name' => 'auditor_audit_created_by', 'type' => 'int', 'internal' => 'createdBy'], 'auditor_audit_created_at' => ['name' => 'auditor_audit_created_at', 'type' => 'DateTime', 'internal' => 'createdAt'], 'auditor_audit_ip' => ['name' => 'auditor_audit_ip', 'type' => 'int', 'internal' => 'ip'], - 'auditor_audit_module' => ['name' => 'auditor_audit_module', 'type' => 'int', 'internal' => 'module'], + 'auditor_audit_module' => ['name' => 'auditor_audit_module', 'type' => 'string', 'internal' => 'module'], 'auditor_audit_ref' => ['name' => 'auditor_audit_ref', 'type' => 'string', 'internal' => 'ref'], 'auditor_audit_type' => ['name' => 'auditor_audit_type', 'type' => 'int', 'internal' => 'type'], 'auditor_audit_subtype' => ['name' => 'auditor_audit_subtype', 'type' => 'int', 'internal' => 'subtype'], @@ -72,7 +72,7 @@ final class AuditMapper extends DataMapperAbstract * @var string * @since 1.0.0 */ - protected static $primaryField = 'auditor_audit'; + protected static $primaryField = 'auditor_audit_id'; /** * Created at. diff --git a/Theme/Backend/audit-list.tpl.php b/Theme/Backend/audit-list.tpl.php index c2f7fa0..ed569eb 100644 --- a/Theme/Backend/audit-list.tpl.php +++ b/Theme/Backend/audit-list.tpl.php @@ -39,7 +39,15 @@ echo $this->getData('nav')->render(); ?> $audit) : $count++; $url = \phpOMS\Uri\UriFactory::build('/{/lang}/backend/admin/audit/single?{?}&id=' . $audit->getId()); ?>