/
class.entry.php
executable file
·189 lines (124 loc) · 5.02 KB
/
class.entry.php
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
<?php
define_safe('__ENTRY_OK__', 0);
define_safe('__ENTRY_FIELD_ERROR__', 100);
Class Entry{
protected $_fields;
protected $_data;
public $creationDate;
function __construct(){
$this->_fields = array();
$this->_data = array();
$this->creationDate = DateTimeObj::getGMT('c');
}
function set($field, $value){
$this->_fields[$field] = $value;
}
function get($field=NULL){
if($field == NULL) return $this->_fields;
return $this->_fields[$field];
}
public function fetchAllAssociatedEntryCounts($associated_sections=NULL) {
if(is_null($this->get('section_id'))) return NULL;
if(is_null($associated_sections)) {
$section = SectionManager::instance()->fetch($this->get('section_id'));
$associated_sections = $section->fetchAssociatedSections();
}
if(!is_array($associated_sections) || empty($associated_sections)) return NULL;
$counts = array();
foreach($associated_sections as $as){
$field = FieldManager::instance()->fetch($as['child_section_field_id']);
$parent_section_field_id = $as['parent_section_field_id'];
$search_value = NULL;
if(!is_null($parent_section_field_id)){
$search_value = $field->fetchAssociatedEntrySearchValue(
$this->getData($as['parent_section_field_id']),
$as['parent_section_field_id'],
$this->get('id')
);
}
else{
$search_value = $this->get('id');
}
$counts[$as['child_section_id']] = $field->fetchAssociatedEntryCount($search_value);
}
return $counts;
}
function checkPostData($data, &$errors, $ignore_missing_fields=false){
$errors = NULL;
$status = __ENTRY_OK__;
$section = SectionManager::instance()->fetch($this->get('section_id'));
$schema = $section->fetchFieldsSchema();
foreach($schema as $info){
$result = NULL;
$field = FieldManager::instance()->fetch($info['id']);
if($ignore_missing_fields && !isset($data[$field->get('element_name')])) continue;
if(Field::__OK__ != $field->checkPostFieldData((isset($data[$info['element_name']]) ? $data[$info['element_name']] : NULL), $message, $this->get('id'))){
$strict = false;
$status = __ENTRY_FIELD_ERROR__;
$errors[$info['id']] = $message;
}
}
return $status;
}
public function assignEntryId() {
$fields = $this->get();
$fields['creation_date'] = DateTimeObj::get('Y-m-d H:i:s');
$fields['creation_date_gmt'] = DateTimeObj::getGMT('Y-m-d H:i:s');
$fields['user_id'] = is_null($this->get('user_id')) ? '1' : $this->get('user_id'); // user_id cannot be NULL
Symphony::Database()->insert($fields, 'tbl_entries');
if (!$entry_id = Symphony::Database()->getInsertID()) return null;
$this->set('id', $entry_id);
return $entry_id;
}
function setDataFromPost($data, &$error, $simulate=false, $ignore_missing_fields=false){
$error = NULL;
$status = __ENTRY_OK__;
// Entry has no ID, create it:
if (!$this->get('id') && $simulate == false) {
$entry_id = $this->assignEntryId();
if (is_null($entry_id)) return __ENTRY_FIELD_ERROR__;
}
$section = SectionManager::instance()->fetch($this->get('section_id'));
$schema = $section->fetchFieldsSchema();
foreach($schema as $info){
$result = NULL;
$field = FieldManager::instance()->fetch($info['id']);
if($ignore_missing_fields && !isset($data[$field->get('element_name')])) continue;
$result = $field->processRawFieldData(
(isset($data[$info['element_name']]) ? $data[$info['element_name']] : NULL), $s, $simulate, $this->get('id')
);
if($s != Field::__OK__){
$status = __ENTRY_FIELD_ERROR__;
$error = array('field_id' => $info['id'], 'message' => $m);
}
$this->setData($info['id'], $result);
}
// Failed to create entry, cleanup
if($status != __ENTRY_OK__ and !is_null($entry_id)) {
Symphony::Database()->delete('tbl_entries', " `id` = '$entry_id' ");
}
return $status;
}
function setData($field_id, $data){
$this->_data[$field_id] = $data;
}
function getData($field_id=NULL, $asObject=false){
if(!$field_id) return $this->_data;
return ($asObject == true ? (object)$this->_data[$field_id] : $this->_data[$field_id]);
}
function findDefaultData(){
$section = SectionManager::instance()->fetch($this->get('section_id'));
$schema = $section->fetchFields();
foreach($schema as $field){
if(isset($this->_data[$field->get('field_id')])) continue;
$field->processRawFieldData(NULL, $result, $status, false, $this->get('id'));
$this->setData($field->get('field_id'), $result);
}
if(!$this->get('creation_date')) $this->set('creation_date', DateTimeObj::get('c'));
if(!$this->get('creation_date_gmt')) $this->set('creation_date_gmt', DateTimeObj::getGMT('c'));
}
function commit(){
$this->findDefaultData();
return ($this->get('id') ? EntryManager::instance()->edit($this) : EntryManager::instance()->add($this));
}
}