1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System PHP 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\Reactor\Models\NoSql\Traits; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class TraitRecord |
20
|
|
|
* |
21
|
|
|
* @package O2System\Reactor\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
|
|
|
protected function setRecordUser($idUser) |
50
|
|
|
{ |
51
|
|
|
if (is_numeric($idUser)) { |
52
|
|
|
$this->recordUser = $idUser; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function setRecordStatus($status) |
59
|
|
|
{ |
60
|
|
|
$status = strtoupper($status); |
61
|
|
|
|
62
|
|
|
if (in_array($status, ['UNPUBLISH', 'PUBLISH', 'DRAFT', 'DELETE', 'ARCHIVE'])) { |
63
|
|
|
$this->recordStatus = $status; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function insertRecordSets(array &$sets) |
70
|
|
|
{ |
71
|
|
|
if (is_null($this->recordUser) and function_exists('globals')) { |
|
|
|
|
72
|
|
|
if ($account = globals()->offsetGet('account')) { |
73
|
|
|
$this->recordUser = isset($account->id_user_account) |
74
|
|
|
? $account->id_user_account |
75
|
|
|
: $account->id; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$timestamp = $this->isUnixTimestamp === true ? strtotime(date('Y-m-d H:i:s')) : date('Y-m-d H:i:s'); |
80
|
|
|
|
81
|
|
|
if ( ! isset($sets[ 'record_status' ])) { |
82
|
|
|
$sets[ 'record_status' ] = $this->recordStatus; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (empty($this->primary_keys)) { |
86
|
|
|
$primary_key = isset($this->primary_key) ? $this->primary_key : 'id'; |
87
|
|
|
|
88
|
|
|
if (empty($sets[ $primary_key ])) { |
89
|
|
|
if ( ! isset($sets[ 'record_create_user' ])) { |
90
|
|
|
$sets[ 'record_create_user' ] = $this->recordUser; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ( ! isset($sets[ 'record_create_timestamp' ])) { |
94
|
|
|
$sets[ 'record_create_timestamp' ] = $timestamp; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} else { |
98
|
|
|
foreach ($this->primary_keys as $primary_key) { |
99
|
|
|
if (empty($sets[ $primary_key ])) { |
100
|
|
|
if ( ! isset($sets[ 'record_create_user' ])) { |
101
|
|
|
$sets[ 'record_create_user' ] = $this->recordUser; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ( ! isset($sets[ 'record_create_timestamp' ])) { |
105
|
|
|
$sets[ 'record_create_timestamp' ] = $timestamp; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$sets[ 'record_update_user' ] = $this->recordUser; |
112
|
|
|
|
113
|
|
|
if ( ! isset($sets[ 'record_update_timestamp' ])) { |
114
|
|
|
$sets[ 'record_update_timestamp' ] = $timestamp; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected function updateRecordSets(array &$sets) |
119
|
|
|
{ |
120
|
|
|
$sets[ 'record_status' ] = $this->recordStatus; |
121
|
|
|
$sets[ 'record_update_user' ] = $this->recordUser; |
122
|
|
|
|
123
|
|
|
$timestamp = $this->isUnixTimestamp === true ? strtotime(date('Y-m-d H:i:s')) : date('Y-m-d H:i:s'); |
124
|
|
|
|
125
|
|
|
if ( ! isset($sets[ 'record_update_timestamp' ])) { |
126
|
|
|
$sets[ 'record_update_timestamp' ] = $timestamp; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if ($this->recordStatus === 'PUBLISH') { |
130
|
|
|
$sets[ 'record_delete_timestamp' ] = null; |
131
|
|
|
$sets[ 'record_delete_user' ] = null; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |