-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataManager.php
More file actions
258 lines (208 loc) · 5.6 KB
/
Copy pathDataManager.php
File metadata and controls
258 lines (208 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
define('DM_PATH', dirname(__FILE__));
define( 'DS', DIRECTORY_SEPARATOR );
include_once DM_PATH.DS.'params.php';
include_once DM_PATH.DS.'classes'.DS.'DBMysql.php';
include_once DM_PATH.DS.'classes'.DS.'DBTable.php';
include_once DM_PATH.DS.'classes'.DS.'DMRenderer.php';
class DataManager extends DBTable{
private $mode = '';
private $page = 1;
private $items_per_page = 20;
private $key_value = 0;
private $titles = array();
private $rendered_fields = array();
private $fields = array();
private $table_fields_params = array();
private $nondb_fields_params = array();
private $fields_params = array();
private $fields_prefix = '';
protected $profile = '';
function __construct($profile) {
$this->profile = $profile;
$db = DBMysql::getInstance();
$db->setQuery( 'SET NAMES "utf8"' );
$db->query();
}
function addFieldParam($param, $value) {
$this->fields_params[$param] = $value;
}
function delete($key_value) {
if( !$this->loadFields() ) {
return false;
}
if ($this->key_field && $key_value) {
$this->addWhereCondition(" `{$this->key_field}` = '$key_value' ");
return parent::delete();
}
return false;
}
function getMode() {
return ($this->mode?$this->mode:'list');
}
function getTableFieldsParams() {
}
function getTitles() {
if( !$this->loadData() ) {
return false;
}
return $this->titles;
}
function getFields() {
if( !$this->loadData() ) {
return false;
}
return $this->rendered_fields;
}
function getData() {
return $this->data;
}
function loadData() {
if ($this->rendered_fields) {
return true;
}
if( !$this->loadFields() ) {
return false;
}
if ($this->key_value && $this->key_field) {
$this->addWhereCondition(" `{$this->key_field}` = '$this->key_value' ");
}
$this->limit = $this->items_per_page;
$this->limit_start = ($this->page - 1) * $this->items_per_page;
$mode = $this->getMode();
if ( !($mode == 'single' && !$this->key_value)
&& parent::loadData() === false ) {
return false;
}
if ($this->mode == 'list' && !$this->data) {
return true;
}
$renderer = new DMRenderer();
$renderer->setFields($this->fields);
$renderer->setData($this->data);
$renderer->setFieldsPrefix($this->fields_prefix);
$aditional_params = array (
'key_field' => $this->key_field
, 'mode' => $mode
);
$renderer->addParams($aditional_params);
$renderer->addParams($this->fields_params);
$this->rendered_fields = $renderer->getFields();
if ($this->rendered_fields === false) {
$this->errors = $renderer->getErrors();
return false;
}
return true;
}
function loadFields() {
if ($this->fields) {
return true;
}
if (!$this->profile) {
$this->errors[] = 'Profile not set';
return false;
}
if (!$this->mode) {
$this->mode = 'list';
}
$profile_file_path = DM_PATH.DS.'profiles'.DS.$this->profile.'.php';
if (! file_exists($profile_file_path) ) {
$this->errors[] = 'File '.$this->profile.'.php not found in profiles folder';
return false;
}
include $profile_file_path;
$this->table = $this->profile;
if(isset($table)) {
$this->table = $table;
}
if (!isset($fields)) {
$this->errors[] = '"fields" array not found in '.$this->profile.'.php';
return false;
}
$this->fields = $fields;
foreach($this->fields as $key => $field) {
if (isset($field[$this->mode.'_mode']) && !$field[$this->mode.'_mode']) {
unset($this->fields[$key]);
continue;
}
if (!isset($field['non_db']) || !$field['non_db']) {
$this->table_fields[] = $field['name'];
$this->table_fields_params[] = $field;
}else {
$this->nondb_fields_params[] = $field;
}
if (!isset($field['title'])) {
$field['title'] = $field['name'];
}
$this->titles[$field['name']] = $field['title'];
if ($field['type'] == 'key') {
$this->key_field = $field['name'];
}
}
return true;
}
function save($data) {
$this->setMode('save');
if( !$this->loadFields() ) {
return false;
}
$update_fields_params = array();
foreach($this->fields as $field) {
if ($field['type'] != 'key') {
$field = array_merge($field, $this->fields_params);
$update_fields_params[] = $field;
}
}
$renderer = new DMRenderer();
$renderer->setFields($update_fields_params);
$renderer->setData($data);
$renderer->setFieldsPrefix($this->fields_prefix);
$mode = $this->getMode();
$aditional_params = array (
'key_field' => $this->key_field
, 'mode' => $mode
);
$renderer->addParams($aditional_params);
$data = $renderer->getFieldsData();
if ($data === false) {
$this->errors = $renderer->getErrors();
return false;
}
$this->key_value = (isset($data[$this->key_field])?$data[$this->key_field]:0);
if ($this->key_field && $this->key_value) {
$this->addWhereCondition(" `{$this->key_field}` = '$this->key_value' ");
$result = parent::update($data);
}else {
$result = parent::insert($data);
$db = DBMysql::getInstance();
$this->key_value = $db->insertid();
}
if(!$result) {
return $result;
}
$renderer->afterSaveEvent($this->key_value);
return $result;
}
function getPagesCount() {
$rows_count = $this->getTableRowsCount();
if($rows_count === false) {
return false;
}
return ceil($rows_count / $this->items_per_page);
}
function setFieldsPrefix($prefix) {
$this->fields_prefix = $prefix;
}
function setItemsPerPage($items_per_page) {
$this->items_per_page = $items_per_page;
}
function setKeyValue($key_value) {
$this->key_value = $key_value;
}
function setMode($mode) {
$this->mode = $mode;
}
function setPage($page) {
$this->page = $page;
}
}