<?php
namespace Example;
class Users extends \System\SCRUD {
public $structure = [
'ID' => self::ID,
'ACTIVE' => [
'TYPE' => 'BOOL',
'NAME' => 'Active',
'DEFAULT' => true,
],
'LOGIN' => [
'TYPE' => 'STRING',
'NAME' => 'Login',
'NOT_NULL' => true,
'INDEX' => true,
],
'PASSWORD' => [
'TYPE' => 'STRING',
'NAME' => 'Password',
'NOT_NULL' => true,
],
];
public function Create($fields) {
$ID = parent::Create($fields);
UsersHistory::I()->Create([
'USER' => $ID,
'CHANGES' => $fields,
'EVENT' => 'Create',
]);
return $ID;
}
public function Update($filter = [], $fields = [], $event = 'Update') {
parent::Update($filter, $fields);
if ($event) {
$user_ids = $this->GetColumn($filter, 'ID');
foreach ($user_ids as $user_id) {
UsersHistory::I()->Create([
'USER' => $user_id,
'CHANGES' => $fields,
'EVENT' => $event,
]);
}
}
}
public function Ban(int $ID) {
$is_active = $this->Get($ID, 'ACTIVE');
if (!$is_active) {
throw new \System\Exception("User is already banned");
}
$this->Update($ID, ['ACTIVE' => false], 'Ban');
}
}