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