1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Saito - The Threaded Web Forum |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
9
|
|
|
* @link https://github.com/Schlaefer/Saito |
10
|
|
|
* @license http://opensource.org/licenses/MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace App\Lib\Model\Table; |
14
|
|
|
|
15
|
|
|
use Cake\Core\Configure; |
16
|
|
|
use Cake\Core\InstanceConfigTrait; |
17
|
|
|
use Cake\Database\Expression\QueryExpression; |
18
|
|
|
use Cake\Event\Event; |
19
|
|
|
use Cake\Event\EventManager; |
20
|
|
|
use Cake\ORM\Table; |
21
|
|
|
use Saito\Event\SaitoEventManager; |
22
|
|
|
|
23
|
|
|
class AppTable extends Table |
24
|
|
|
{ |
25
|
|
|
use InstanceConfigTrait { |
26
|
|
|
getConfig as private traitGetConfig; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** @var array default config for InstanceConfigTrait */ |
30
|
|
|
protected $_defaultConfig = []; |
31
|
|
|
|
32
|
|
|
public $SharedObjects; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var SaitoEventManager |
36
|
|
|
*/ |
37
|
|
|
protected $_SEM; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Check that a record exists. |
41
|
|
|
* |
42
|
|
|
* @param int|array|\ArrayAccess $conditions Record-ID or query conditions. |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
public function exists($conditions) |
46
|
|
|
{ |
47
|
|
|
if (is_int($conditions)) { |
48
|
|
|
$conditions = ['id' => $conditions]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return parent::exists($conditions); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Increments value of a field |
56
|
|
|
* |
57
|
|
|
* @param int|array $where entry-id or array with conditions |
58
|
|
|
* @param string $field fielt to increment |
59
|
|
|
* @param int $amount Increment size. |
60
|
|
|
* @return void |
61
|
|
|
* @throws \InvalidArgumentException |
62
|
|
|
*/ |
63
|
|
|
public function increment($where, $field, $amount = 1) |
64
|
|
|
{ |
65
|
|
|
if (!is_int($amount)) { |
|
|
|
|
66
|
|
|
throw new \InvalidArgumentException; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (is_numeric($where)) { |
70
|
|
|
$where = ['id' => $where]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$operator = '+'; |
74
|
|
|
if ($amount < 0) { |
75
|
|
|
$operator = '-'; |
76
|
|
|
$amount *= -1; |
77
|
|
|
} |
78
|
|
|
$expression = new QueryExpression("$field = $field $operator $amount"); |
79
|
|
|
$this->updateAll($expression, $where); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Dispatches an event |
84
|
|
|
* |
85
|
|
|
* - Always passes the issuing model class as subject |
86
|
|
|
* - Wrapper for CakeEvent boilerplate code |
87
|
|
|
* - Easier to test |
88
|
|
|
* |
89
|
|
|
* @param string $event event identifier `Model.<modelname>.<event>` |
90
|
|
|
* @param array $data additional event data |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
public function dispatchDbEvent($event, $data = []) |
94
|
|
|
{ |
95
|
|
|
EventManager::instance()->dispatch(new Event($event, $this, $data)); |
96
|
|
|
// propagate event on Saito's event bus |
97
|
|
|
$this->dispatchSaitoEvent($event, $data); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Dispatch Saito Event |
102
|
|
|
* |
103
|
|
|
* @param string $event event |
104
|
|
|
* @param array $data data |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
public function dispatchSaitoEvent($event, $data) |
108
|
|
|
{ |
109
|
|
|
if (empty($this->_SEM)) { |
110
|
|
|
$this->_SEM = SaitoEventManager::getInstance(); |
111
|
|
|
} |
112
|
|
|
$this->_SEM->dispatch($event, $data + ['Model' => $this]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
public function getConfig($key = null, $default = null) |
119
|
|
|
{ |
120
|
|
|
if (is_string($key)) { |
121
|
|
|
$setting = Configure::read('Saito.Settings.' . $key); |
122
|
|
|
if ($setting !== null) { |
123
|
|
|
return $setting; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this->traitGetConfig($key, $default); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|