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
|
|
|
|
14
|
|
|
namespace O2System\Framework\Models\NoSql\Traits; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class TraitRecord |
20
|
|
|
* |
21
|
|
|
* @package O2System\Framework\Models\NoSql\Traits |
22
|
|
|
*/ |
23
|
|
|
trait RecordTrait |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Unix Timestamp Flag |
27
|
|
|
* |
28
|
|
|
* @access protected |
29
|
|
|
* @type bool |
30
|
|
|
*/ |
31
|
|
|
protected $isUnixTimestamp = false; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Default Record Status |
35
|
|
|
* |
36
|
|
|
* @access protected |
37
|
|
|
* @type string |
38
|
|
|
*/ |
39
|
|
|
protected $recordStatus = 'PUBLISH'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Default Record User |
43
|
|
|
* |
44
|
|
|
* @access protected |
45
|
|
|
* @type int |
46
|
|
|
*/ |
47
|
|
|
protected $recordUser = null; |
48
|
|
|
|
49
|
|
|
// ------------------------------------------------------------------------ |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param $idUser |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
|
|
protected function setRecordUser($idUser) |
56
|
|
|
{ |
57
|
|
|
if (is_numeric($idUser)) { |
58
|
|
|
$this->recordUser = $idUser; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// ------------------------------------------------------------------------ |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param $status |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
|
|
protected function setRecordStatus($status) |
71
|
|
|
{ |
72
|
|
|
$status = strtoupper($status); |
73
|
|
|
|
74
|
|
|
if (in_array($status, ['UNPUBLISH', 'PUBLISH', 'DRAFT', 'DELETE', 'ARCHIVE'])) { |
75
|
|
|
$this->recordStatus = $status; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// ------------------------------------------------------------------------ |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $sets |
85
|
|
|
*/ |
86
|
|
|
protected function insertRecordSets(array &$sets) |
87
|
|
|
{ |
88
|
|
|
if (is_null($this->recordUser) and function_exists('globals')) { |
|
|
|
|
89
|
|
|
if ($account = globals()->offsetGet('account')) { |
90
|
|
|
$this->recordUser = isset($account->id_user_account) |
91
|
|
|
? $account->id_user_account |
92
|
|
|
: $account->id; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$timestamp = $this->isUnixTimestamp === true ? strtotime(date('Y-m-d H:i:s')) : date('Y-m-d H:i:s'); |
97
|
|
|
|
98
|
|
|
if ( ! isset($sets[ 'record_status' ])) { |
99
|
|
|
$sets[ 'record_status' ] = $this->recordStatus; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (empty($this->primary_keys)) { |
103
|
|
|
$primary_key = isset($this->primary_key) ? $this->primary_key : 'id'; |
104
|
|
|
|
105
|
|
|
if (empty($sets[ $primary_key ])) { |
106
|
|
|
if ( ! isset($sets[ 'record_create_user' ])) { |
107
|
|
|
$sets[ 'record_create_user' ] = $this->recordUser; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ( ! isset($sets[ 'record_create_timestamp' ])) { |
111
|
|
|
$sets[ 'record_create_timestamp' ] = $timestamp; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} else { |
115
|
|
|
foreach ($this->primary_keys as $primary_key) { |
116
|
|
|
if (empty($sets[ $primary_key ])) { |
117
|
|
|
if ( ! isset($sets[ 'record_create_user' ])) { |
118
|
|
|
$sets[ 'record_create_user' ] = $this->recordUser; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ( ! isset($sets[ 'record_create_timestamp' ])) { |
122
|
|
|
$sets[ 'record_create_timestamp' ] = $timestamp; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$sets[ 'record_update_user' ] = $this->recordUser; |
129
|
|
|
|
130
|
|
|
if ( ! isset($sets[ 'record_update_timestamp' ])) { |
131
|
|
|
$sets[ 'record_update_timestamp' ] = $timestamp; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// ------------------------------------------------------------------------ |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param array $sets |
139
|
|
|
*/ |
140
|
|
|
protected function updateRecordSets(array &$sets) |
141
|
|
|
{ |
142
|
|
|
$sets[ 'record_status' ] = $this->recordStatus; |
143
|
|
|
$sets[ 'record_update_user' ] = $this->recordUser; |
144
|
|
|
|
145
|
|
|
$timestamp = $this->isUnixTimestamp === true ? strtotime(date('Y-m-d H:i:s')) : date('Y-m-d H:i:s'); |
146
|
|
|
|
147
|
|
|
if ( ! isset($sets[ 'record_update_timestamp' ])) { |
148
|
|
|
$sets[ 'record_update_timestamp' ] = $timestamp; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if ($this->recordStatus === 'PUBLISH') { |
152
|
|
|
$sets[ 'record_delete_timestamp' ] = null; |
153
|
|
|
$sets[ 'record_delete_user' ] = null; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |