1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
// ------------------------------------------------------------------------ |
12
|
|
|
|
13
|
|
|
namespace O2System\Framework\Models\Sql\Traits; |
14
|
|
|
|
15
|
|
|
// ------------------------------------------------------------------------ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class TraitRecord |
19
|
|
|
* |
20
|
|
|
* @package O2System\Framework\Models\Sql\Traits |
21
|
|
|
*/ |
22
|
|
|
trait RecordTrait |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* RecordTrait::$unixTimestamp |
26
|
|
|
* |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
public $unixTimestamp = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* RecordTrait::$recordStatus |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $recordStatus = 'PUBLISH'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* RecordTrait::$recordUser |
40
|
|
|
* |
41
|
|
|
* @var int|null |
42
|
|
|
*/ |
43
|
|
|
protected $recordUser = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Record Ordering Flag |
47
|
|
|
* |
48
|
|
|
* @var bool |
49
|
|
|
*/ |
50
|
|
|
public $recordOrdering = false; |
51
|
|
|
|
52
|
|
|
// ------------------------------------------------------------------------ |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* RecordTrait::setRecordUser |
56
|
|
|
* |
57
|
|
|
* @param int $idUser |
58
|
|
|
* |
59
|
|
|
* @return static |
60
|
|
|
*/ |
61
|
|
|
public function setRecordUser($idUser) |
62
|
|
|
{ |
63
|
|
|
if (is_numeric($idUser)) { |
|
|
|
|
64
|
|
|
$this->recordUser = $idUser; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// ------------------------------------------------------------------------ |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* RecordTrait::setRecordStatus |
74
|
|
|
* |
75
|
|
|
* @param string $status |
76
|
|
|
* |
77
|
|
|
* @return static |
78
|
|
|
*/ |
79
|
|
|
public function setRecordStatus($status) |
80
|
|
|
{ |
81
|
|
|
$status = strtoupper($status); |
82
|
|
|
|
83
|
|
|
if (in_array($status, ['UNPUBLISH', 'PUBLISH', 'DRAFT', 'DELETED', 'ARCHIVED', 'LOCKED'])) { |
84
|
|
|
$this->recordStatus = $status; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// ------------------------------------------------------------------------ |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* RecordTrait::insertRecordSets |
94
|
|
|
* |
95
|
|
|
* @param array $sets |
96
|
|
|
*/ |
97
|
|
|
protected function insertRecordSets(array &$sets) |
98
|
|
|
{ |
99
|
|
|
$timestamp = $this->unixTimestamp === true ? strtotime(date('Y-m-d H:i:s')) : date('Y-m-d H:i:s'); |
100
|
|
|
|
101
|
|
|
if(is_null($this->recordUser)) { |
102
|
|
|
if(globals()->offsetExists('account')) { |
103
|
|
|
$this->setRecordUser(globals()->account->id); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ( ! isset($sets[ 'record_status' ])) { |
108
|
|
|
$sets[ 'record_status' ] = $this->recordStatus; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (empty($this->primaryKeys)) { |
112
|
|
|
$primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
113
|
|
|
|
114
|
|
|
if (isset($sets[ $primaryKey ])) { |
115
|
|
|
if (empty($sets[ $primaryKey ])) { |
116
|
|
|
unset($sets[ $primaryKey ]); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if (empty($sets[ $primaryKey ])) { |
121
|
|
|
if ( ! isset($sets[ 'record_create_user' ])) { |
122
|
|
|
$sets[ 'record_create_user' ] = $this->recordUser; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if ( ! isset($sets[ 'record_create_timestamp' ])) { |
126
|
|
|
$sets[ 'record_create_timestamp' ] = $timestamp; |
127
|
|
|
} elseif ($this->unixTimestamp) { |
128
|
|
|
$sets[ 'record_create_timestamp' ] = strtotime($sets[ 'record_create_timestamp' ]); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} else { |
132
|
|
|
foreach ($this->primaryKeys as $primaryKey) { |
133
|
|
|
if (empty($sets[ $primaryKey ])) { |
134
|
|
|
if ( ! isset($sets[ 'record_create_user' ])) { |
135
|
|
|
$sets[ 'record_create_user' ] = $this->recordUser; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if ( ! isset($sets[ 'record_create_timestamp' ])) { |
139
|
|
|
$sets[ 'record_create_timestamp' ] = $timestamp; |
140
|
|
|
} elseif ($this->unixTimestamp) { |
141
|
|
|
$sets[ 'record_create_timestamp' ] = strtotime($sets[ 'record_create_timestamp' ]); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if ( ! isset($sets[ 'record_update_user' ])) { |
148
|
|
|
$sets[ 'record_update_user' ] = $this->recordUser; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if ( ! isset($sets[ 'record_update_timestamp' ])) { |
152
|
|
|
$sets[ 'record_update_timestamp' ] = $timestamp; |
153
|
|
|
} elseif ($this->unixTimestamp) { |
154
|
|
|
$sets[ 'record_update_timestamp' ] = strtotime($sets[ 'record_update_timestamp' ]); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if ( ! isset($sets[ 'record_ordering' ]) && $this->recordOrdering === true) { |
158
|
|
|
$sets[ 'record_ordering' ] = $this->getRecordOrdering(); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// ------------------------------------------------------------------------ |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* RecordTrait::updateRecordSets |
166
|
|
|
* |
167
|
|
|
* @param array $sets |
168
|
|
|
*/ |
169
|
|
|
protected function updateRecordSets(array &$sets) |
170
|
|
|
{ |
171
|
|
|
if(is_null($this->recordUser)) { |
172
|
|
|
if(globals()->offsetExists('account')) { |
173
|
|
|
$this->setRecordUser(globals()->account->id); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if ( ! isset($sets[ 'record_status' ])) { |
178
|
|
|
$sets[ 'record_status' ] = $this->recordStatus; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if ( ! isset($sets[ 'record_update_user' ])) { |
182
|
|
|
$sets[ 'record_update_user' ] = $this->recordUser; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$timestamp = $this->unixTimestamp === true ? strtotime(date('Y-m-d H:i:s')) : date('Y-m-d H:i:s'); |
186
|
|
|
|
187
|
|
|
if ( ! isset($sets[ 'record_update_timestamp' ])) { |
188
|
|
|
$sets[ 'record_update_timestamp' ] = $timestamp; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
// ------------------------------------------------------------------------ |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* RecordTrait::getRecordOrdering |
196
|
|
|
* |
197
|
|
|
* @return int |
198
|
|
|
*/ |
199
|
|
|
public function getRecordOrdering() |
200
|
|
|
{ |
201
|
|
|
if($this->recordOrdering === true) { |
202
|
|
|
return $this->qb->countAllResults($this->table) + 1; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return 0; |
206
|
|
|
} |
207
|
|
|
} |