1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Source\Models; |
4
|
|
|
|
5
|
|
|
use Source\Core\Model; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @package Source\Models |
9
|
|
|
*/ |
10
|
|
|
class AccessLevel extends Model |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* AccessLevel constructor. |
14
|
|
|
*/ |
15
|
|
|
public function __construct() |
16
|
|
|
{ |
17
|
|
|
parent::__construct("access_level", ["id"], ["name"]); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $name |
22
|
|
|
* @return AccessLevel |
23
|
|
|
*/ |
24
|
|
|
public function bootstrap(string $name): AccessLevel |
25
|
|
|
{ |
26
|
|
|
$this->name = $name; |
|
|
|
|
27
|
|
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $name |
32
|
|
|
* @param string $columns |
33
|
|
|
* @return null|AccessLevel |
34
|
|
|
*/ |
35
|
|
|
public function findByName(string $name, string $columns = "*"): ?AccessLevel |
36
|
|
|
{ |
37
|
|
|
$find = $this->find("name = :name", "name={$name}", $columns); |
38
|
|
|
|
39
|
|
|
return $find->fetch(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
public function save(): bool |
46
|
|
|
{ |
47
|
|
|
if (!$this->required()) { |
48
|
|
|
$this->error = "Faltam campos obrigatórios!"; |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** AccessLevel Update */ |
53
|
|
|
if (!empty($this->id)) { |
|
|
|
|
54
|
|
|
$accessLevelId = $this->id; |
55
|
|
|
|
56
|
|
|
if ($this->find("name = :e AND id != :i", "e={$this->name}&i={$accessLevelId}", "id")->fetch()) { |
57
|
|
|
$this->error = "O sexo informado já está cadastrado"; |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->update($this->safe(), "id = :id", "id={$accessLevelId}"); |
62
|
|
|
if ($this->fail()) { |
63
|
|
|
$this->error = "Erro ao atualizar, verifique os dados"; |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** AccessLevel Create */ |
69
|
|
|
if (empty($this->id)) { |
70
|
|
|
if ($this->findByName($this->name, 'id')) { |
71
|
|
|
$this->error = "O sexo informado já está cadastrado"; |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$accessLevelId = $this->create($this->safe()); |
76
|
|
|
if ($this->fail()) { |
77
|
|
|
$this->error = "Erro ao cadastrar, verifique os dados"; |
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->data = ($this->findById($accessLevelId))->data(); |
|
|
|
|
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|