1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* smartpartnerBaseObjectHandler class |
5
|
|
|
* |
6
|
|
|
* @author Nazar Aziz <[email protected]> |
7
|
|
|
* @access public |
8
|
|
|
* @package xhelp |
9
|
|
|
*/ |
10
|
|
|
class SmartpartnerBaseObjectHandler extends XoopsObjectHandler |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Database connection |
14
|
|
|
* |
15
|
|
|
* @var object |
16
|
|
|
* @access private |
17
|
|
|
*/ |
18
|
|
|
public $_db; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Autoincrementing DB fieldname |
22
|
|
|
* @var string |
23
|
|
|
* @access private |
24
|
|
|
*/ |
25
|
|
|
public $_idfield = 'id'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Constructor |
29
|
|
|
* |
30
|
|
|
* @param object|XoopsDatabase $db reference to a xoopsDB object |
31
|
|
|
*/ |
32
|
|
|
public function init(XoopsDatabase $db) |
33
|
|
|
{ |
34
|
|
|
$this->_db = $db; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* create a new object |
39
|
|
|
* @return object {@link smartpartnerBaseObject} |
40
|
|
|
* @access public |
41
|
|
|
*/ |
42
|
|
|
public function create() |
43
|
|
|
{ |
44
|
|
|
return new $this->classname(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* retrieve an object from the database, based on. use in child classes |
49
|
|
|
* @param int $id ID |
50
|
|
|
* @return mixed object if id exists, false if not |
51
|
|
|
* @access public |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public function &get($id) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$id = (int)$id; |
56
|
|
|
if ($id > 0) { |
57
|
|
|
$sql = $this->_selectQuery(new Criteria($this->_idfield, $id)); |
58
|
|
|
if (!$result = $this->_db->query($sql)) { |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
$numrows = $this->_db->getRowsNum($result); |
62
|
|
|
if ($numrows == 1) { |
63
|
|
|
$obj = new $this->classname($this->_db->fetchArray($result)); |
64
|
|
|
|
65
|
|
|
return $obj; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* retrieve objects from the database |
74
|
|
|
* |
75
|
|
|
* @param object $criteria {@link CriteriaElement} conditions to be met |
|
|
|
|
76
|
|
|
* @param bool $id_as_key Should the department ID be used as array key |
77
|
|
|
* @return array array of objects |
78
|
|
|
* @access public |
79
|
|
|
*/ |
80
|
|
|
public function &getObjects($criteria = null, $id_as_key = false) |
81
|
|
|
{ |
82
|
|
|
$ret = array(); |
83
|
|
|
$limit = $start = 0; |
84
|
|
|
$sql = $this->_selectQuery($criteria); |
85
|
|
|
$id = $this->_idfield; |
86
|
|
|
|
87
|
|
|
if (isset($criteria)) { |
88
|
|
|
$limit = $criteria->getLimit(); |
89
|
|
|
$start = $criteria->getStart(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$result = $this->_db->query($sql, $limit, $start); |
93
|
|
|
// if no records from db, return empty array |
94
|
|
|
if (!$result) { |
95
|
|
|
return $ret; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// Add each returned record to the result array |
99
|
|
|
while ($myrow = $this->_db->fetchArray($result)) { |
100
|
|
|
$obj = new $this->classname($myrow); |
101
|
|
|
if (!$id_as_key) { |
102
|
|
|
$ret[] =& $obj; |
103
|
|
|
} else { |
104
|
|
|
$ret[$obj->getVar($id)] =& $obj; |
105
|
|
|
} |
106
|
|
|
unset($obj); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $ret; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param XoopsObject $obj |
114
|
|
|
* @param bool $force |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
public function insert($obj, $force = false) |
118
|
|
|
{ |
119
|
|
|
// Make sure object is of correct type |
120
|
|
|
if (strcasecmp($this->classname, get_class($obj)) != 0) { |
121
|
|
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Make sure object needs to be stored in DB |
125
|
|
|
if (!$obj->isDirty()) { |
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// Make sure object fields are filled with valid values |
130
|
|
|
if (!$obj->cleanVars()) { |
131
|
|
|
return false; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// Create query for DB update |
135
|
|
|
if ($obj->isNew()) { |
136
|
|
|
// Determine next auto-gen ID for table |
137
|
|
|
$id = $this->_db->genId($this->_db->prefix($this->_dbtable) . '_uid_seq'); |
|
|
|
|
138
|
|
|
$sql = $this->_insertQuery($obj); |
139
|
|
|
} else { |
140
|
|
|
$sql = $this->_updateQuery($obj); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// Update DB |
144
|
|
View Code Duplication |
if (false != $force) { |
|
|
|
|
145
|
|
|
$result = $this->_db->queryF($sql); |
146
|
|
|
} else { |
147
|
|
|
$result = $this->_db->query($sql); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if (!$result) { |
151
|
|
|
$obj->setErrors('The query returned an error. ' . $this->db->error()); |
152
|
|
|
|
153
|
|
|
return false; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
//Make sure auto-gen ID is stored correctly in object |
157
|
|
|
if ($obj->isNew()) { |
158
|
|
|
$obj->assignVar($this->_idfield, $this->_db->getInsertId()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return true; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Create a "select" SQL query |
166
|
|
|
* @param object $criteria {@link CriteriaElement} to match |
|
|
|
|
167
|
|
|
* @return string SQL query |
168
|
|
|
* @access private |
169
|
|
|
*/ |
170
|
|
View Code Duplication |
public function _selectQuery($criteria = null) |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
$sql = sprintf('SELECT * FROM %s', $this->_db->prefix($this->_dbtable)); |
173
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) { |
|
|
|
|
174
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
175
|
|
|
if ($criteria->getSort() != '') { |
176
|
|
|
$sql .= ' ORDER BY ' . $criteria->getSort() . ' |
177
|
|
|
' . $criteria->getOrder(); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $sql; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* count objects matching a criteria |
186
|
|
|
* |
187
|
|
|
* @param object $criteria {@link CriteriaElement} to match |
|
|
|
|
188
|
|
|
* @return int count of objects |
189
|
|
|
* @access public |
190
|
|
|
*/ |
191
|
|
View Code Duplication |
public function getCount($criteria = null) |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
$sql = 'SELECT COUNT(*) FROM ' . $this->_db->prefix($this->_dbtable); |
194
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) { |
|
|
|
|
195
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
196
|
|
|
} |
197
|
|
|
if (!$result = $this->_db->query($sql)) { |
198
|
|
|
return 0; |
199
|
|
|
} |
200
|
|
|
list($count) = $this->_db->fetchRow($result); |
201
|
|
|
|
202
|
|
|
return $count; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* delete object based on id |
207
|
|
|
* |
208
|
|
|
* @param object $obj {@link XoopsObject} to delete |
209
|
|
|
* @param bool $force override XOOPS delete protection |
210
|
|
|
* @return bool deletion successful? |
211
|
|
|
* @access public |
212
|
|
|
*/ |
213
|
|
|
public function delete($obj, $force = false) |
214
|
|
|
{ |
215
|
|
|
if (strcasecmp($this->classname, get_class($obj)) != 0) { |
216
|
|
|
return false; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$sql = $this->_deleteQuery($obj); |
220
|
|
|
|
221
|
|
View Code Duplication |
if (false != $force) { |
|
|
|
|
222
|
|
|
$result = $this->_db->queryF($sql); |
223
|
|
|
} else { |
224
|
|
|
$result = $this->_db->query($sql); |
225
|
|
|
} |
226
|
|
|
if (!$result) { |
227
|
|
|
return false; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return true; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* delete department matching a set of conditions |
235
|
|
|
* |
236
|
|
|
* @param object $criteria {@link CriteriaElement} |
|
|
|
|
237
|
|
|
* @return bool FALSE if deletion failed |
238
|
|
|
* @access public |
239
|
|
|
*/ |
240
|
|
View Code Duplication |
public function deleteAll($criteria = null) |
|
|
|
|
241
|
|
|
{ |
242
|
|
|
$sql = 'DELETE FROM ' . $this->_db->prefix($this->_dbtable); |
243
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) { |
|
|
|
|
244
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
245
|
|
|
} |
246
|
|
|
if (!$result = $this->_db->query($sql)) { |
247
|
|
|
return false; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return true; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Assign a value to 1 field for tickets matching a set of conditions |
255
|
|
|
* |
256
|
|
|
* @param $fieldname |
257
|
|
|
* @param $fieldvalue |
258
|
|
|
* @param object $criteria {@link CriteriaElement} |
|
|
|
|
259
|
|
|
* @return bool FALSE if update failed |
260
|
|
|
* @access public |
261
|
|
|
*/ |
262
|
|
View Code Duplication |
public function updateAll($fieldname, $fieldvalue, $criteria = null) |
|
|
|
|
263
|
|
|
{ |
264
|
|
|
$set_clause = is_numeric($fieldvalue) ? $fieldname . ' = ' . $fieldvalue : $fieldname . ' = ' . $this->_db->quoteString($fieldvalue); |
265
|
|
|
$sql = 'UPDATE ' . $this->_db->prefix($this->_dbtable) . ' SET ' . $set_clause; |
266
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) { |
|
|
|
|
267
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
268
|
|
|
} |
269
|
|
|
if (!$result = $this->_db->query($sql)) { |
270
|
|
|
return false; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return true; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @param $obj |
278
|
|
|
* @return bool |
279
|
|
|
*/ |
280
|
|
|
public function _insertQuery($obj) |
281
|
|
|
{ |
282
|
|
|
return false; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param $obj |
287
|
|
|
* @return bool |
288
|
|
|
*/ |
289
|
|
|
public function _updateQuery($obj) |
290
|
|
|
{ |
291
|
|
|
return false; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @param $obj |
296
|
|
|
* @return bool |
297
|
|
|
*/ |
298
|
|
|
public function _deleteQuery($obj) |
299
|
|
|
{ |
300
|
|
|
return false; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Singleton - prevent multiple instances of this class |
305
|
|
|
* |
306
|
|
|
* @param object|XoopsDatabase $db |
307
|
|
|
* @return object <a href='psi_element://pagesCategoryHandler'>pagesCategoryHandler</a> |
308
|
|
|
* @access public |
309
|
|
|
*/ |
310
|
|
|
public function getInstance(XoopsDatabase $db) |
311
|
|
|
{ |
312
|
|
|
static $instance; |
313
|
|
|
if (null === $instance) { |
314
|
|
|
$classname = $this->classname . 'Handler'; |
315
|
|
|
$instance = new $classname($db); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
return $instance; |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.