1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gewaer\Traits; |
6
|
|
|
|
7
|
|
|
use Gewaer\Models\Users; |
8
|
|
|
use Gewaer\Exception\ServerErrorHttpException; |
9
|
|
|
use Gewaer\Exception\ModelException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait ResponseTrait |
13
|
|
|
* |
14
|
|
|
* @package Gewaer\Traits |
15
|
|
|
* |
16
|
|
|
* @property Users $user |
17
|
|
|
* @property Config $config |
18
|
|
|
* @property Request $request |
19
|
|
|
* @property Auth $auth |
20
|
|
|
* @property \Phalcon\Di $di |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
trait ModelSettingsTrait |
24
|
|
|
{ |
25
|
|
|
protected $settingsModel; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Set the setting model |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
1 |
|
private function createSettingsModel(): void |
33
|
|
|
{ |
34
|
1 |
|
$class = get_class($this) . 'Settings'; |
35
|
|
|
|
36
|
1 |
|
$this->settingsModel = new $class(); |
37
|
1 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the primary key of this model, this will only work on model with just 1 primary key |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
1 |
|
private function getPrimaryKey(): string |
45
|
|
|
{ |
46
|
|
|
// Get the first matching primary key. |
47
|
|
|
// @TODO This will hurt on compound primary keys. |
48
|
1 |
|
$metaData = new \Phalcon\Mvc\Model\MetaData\Memory(); |
49
|
|
|
// Get the first matching primary key. |
50
|
|
|
// @TODO This will hurt on compound primary keys. |
51
|
1 |
|
return $this->getSource() . '_' . $metaData->getPrimaryKeyAttributes($this)[0]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set the settings |
56
|
|
|
* |
57
|
|
|
* @param string $key |
58
|
|
|
* @param string $value |
59
|
|
|
* @return boolean |
60
|
|
|
*/ |
61
|
1 |
|
public function setSettings(string $key, $value) : bool |
62
|
|
|
{ |
63
|
1 |
|
$this->createSettingsModel(); |
64
|
|
|
|
65
|
1 |
|
if (!is_object($this->settingsModel)) { |
66
|
|
|
throw new ServerErrorHttpException('ModelSettingsTrait need to have a settings mdoel configure, check the model setting existe for this class' . get_class($this)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
//setup the user notificatoin setting |
70
|
1 |
|
$this->settingsModel->{$this->getPrimaryKey()} = $this->getId(); |
71
|
1 |
|
$this->settingsModel->name = $key; |
72
|
1 |
|
$this->settingsModel->value = $value; |
73
|
|
|
|
74
|
1 |
|
if (!$this->settingsModel->save()) { |
75
|
|
|
throw new ModelException((string)current($this->settingsModel->getMessages())); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the settings base on the key |
83
|
|
|
* |
84
|
|
|
* @param string $key |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
public function getSettings(string $key): ?string |
88
|
|
|
{ |
89
|
|
|
$this->createSettingsModel(); |
90
|
|
|
$value = $this->settingsModel->findFirst([ |
91
|
|
|
'conditions' => "{$this->getPrimaryKey()} = ?0 and name = ?1", |
92
|
|
|
'bind' => [$this->getId(), $key] |
93
|
|
|
]); |
94
|
|
|
|
95
|
|
|
if (is_object($value)) { |
96
|
|
|
return $value->value; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return null; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|