1
|
|
|
<?php namespace XoopsModules\Smartobject; |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* You may not change or alter any portion of this comment or credits |
5
|
|
|
* of supporting developers from this source code or any supporting source code |
6
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
7
|
|
|
* |
8
|
|
|
* This program is distributed in the hope that it will be useful, |
9
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
10
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @copyright XOOPS Project https://xoops.org/ |
15
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
16
|
|
|
* @link http://www.smartfactory.ca The SmartFactory |
17
|
|
|
* @package SmartObject |
18
|
|
|
* @author marcan <[email protected]> |
19
|
|
|
* @author XOOPS Development Team |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
use XoopsModules\Smartobject; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Contains the classes for updating database tables |
26
|
|
|
* |
27
|
|
|
* @license GNU |
28
|
|
|
|
29
|
|
|
*/ |
30
|
|
|
/** |
31
|
|
|
* SmartDbTable class |
32
|
|
|
* |
33
|
|
|
* Information about an individual table |
34
|
|
|
* |
35
|
|
|
* @package SmartObject |
36
|
|
|
* @author marcan <[email protected]> |
37
|
|
|
* @link http://www.smartfactory.ca The SmartFactory |
38
|
|
|
*/ |
39
|
|
|
// defined('XOOPS_ROOT_PATH') || die('Restricted access'); |
40
|
|
|
if (!defined('SMARTOBJECT_ROOT_PATH')) { |
41
|
|
|
require_once XOOPS_ROOT_PATH . '/modules/smartobject/include/common.php'; |
42
|
|
|
} |
43
|
|
|
/** |
44
|
|
|
* Include the language constants for the SmartObjectDBUpdater |
45
|
|
|
*/ |
46
|
|
|
global $xoopsConfig; |
47
|
|
|
$common_file = SMARTOBJECT_ROOT_PATH . 'language/' . $xoopsConfig['language'] . '/smartdbupdater.php'; |
48
|
|
|
if (!file_exists($common_file)) { |
49
|
|
|
$common_file = SMARTOBJECT_ROOT_PATH . 'language/english/smartdbupdater.php'; |
50
|
|
|
} |
51
|
|
|
include $common_file; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Class SmartDbTable |
55
|
|
|
*/ |
56
|
|
|
class DbTable |
57
|
|
|
{ |
58
|
|
|
/** |
59
|
|
|
* @var string $_name name of the table |
60
|
|
|
*/ |
61
|
|
|
public $_name; |
62
|
|
|
/** |
63
|
|
|
* @var string $_structure structure of the table |
64
|
|
|
*/ |
65
|
|
|
public $_structure; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var array $_data containing valued of each records to be added |
69
|
|
|
*/ |
70
|
|
|
public $_data; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var array $_alteredFields containing fields to be altered |
74
|
|
|
*/ |
75
|
|
|
public $_alteredFields; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var array $_newFields containing new fields to be added |
79
|
|
|
*/ |
80
|
|
|
public $_newFields; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var array $_droppedFields containing fields to be dropped |
84
|
|
|
*/ |
85
|
|
|
public $_droppedFields; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var array $_flagForDrop flag table to drop it |
89
|
|
|
*/ |
90
|
|
|
public $_flagForDrop = false; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @var array $_updatedFields containing fields which values will be updated |
94
|
|
|
*/ |
95
|
|
|
public $_updatedFields; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @var array $_updatedFields containing fields which values will be updated |
99
|
|
|
*/ //felix |
100
|
|
|
public $_updatedWhere; |
101
|
|
|
|
102
|
|
|
public $_existingFieldsArray = false; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Constructor |
106
|
|
|
* |
107
|
|
|
* @param string $name name of the table |
108
|
|
|
* |
109
|
|
|
*/ |
110
|
|
|
public function __construct($name) |
111
|
|
|
{ |
112
|
|
|
$this->_name = $name; |
113
|
|
|
$this->_data = []; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Return the table name, prefixed with site table prefix |
118
|
|
|
* |
119
|
|
|
* @return string table name |
120
|
|
|
* |
121
|
|
|
*/ |
122
|
|
|
public function name() |
123
|
|
|
{ |
124
|
|
|
global $xoopsDB; |
125
|
|
|
|
126
|
|
|
return $xoopsDB->prefix($this->_name); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Checks if the table already exists in the database |
131
|
|
|
* |
132
|
|
|
* @return bool TRUE if it exists, FALSE if not |
133
|
|
|
* |
134
|
|
|
*/ |
135
|
|
|
public function exists() |
136
|
|
|
{ |
137
|
|
|
return Smartobject\Utility::isTable($this->_name); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return mixed |
142
|
|
|
*/ |
143
|
|
|
public function getExistingFieldsArray() |
144
|
|
|
{ |
145
|
|
|
global $xoopsDB; |
146
|
|
|
$result = $xoopsDB->query('SHOW COLUMNS FROM ' . $this->name()); |
147
|
|
|
while (false !== ($existing_field = $xoopsDB->fetchArray($result))) { |
148
|
|
|
$fields[$existing_field['Field']] = $existing_field['Type']; |
|
|
|
|
149
|
|
|
if ('YES' !== $existing_field['Null']) { |
150
|
|
|
$fields[$existing_field['Field']] .= ' NOT NULL'; |
151
|
|
|
} |
152
|
|
|
if ($existing_field['Extra']) { |
153
|
|
|
$fields[$existing_field['Field']] .= ' ' . $existing_field['Extra']; |
154
|
|
|
} |
155
|
|
|
if (!(null === $existing_field['Default']) |
156
|
|
|
&& ($existing_field['Default'] |
157
|
|
|
|| '' === $existing_field['Default'] |
158
|
|
|
|| 0 == $existing_field['Default'])) { |
159
|
|
|
$fields[$existing_field['Field']] .= " default '" . $existing_field['Default'] . "'"; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $fields; |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param $field |
168
|
|
|
* @return bool |
169
|
|
|
*/ |
170
|
|
|
public function fieldExists($field) |
171
|
|
|
{ |
172
|
|
|
$existingFields = $this->getExistingFieldsArray(); |
173
|
|
|
|
174
|
|
|
return isset($existingFields[$field]); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Set the table structure |
179
|
|
|
* |
180
|
|
|
* Example: |
181
|
|
|
* |
182
|
|
|
* $table->setStructure("`transactionid` int(11) NOT NULL auto_increment, |
183
|
|
|
* `date` int(11) NOT NULL default '0', |
184
|
|
|
* `status` int(1) NOT NULL default '-1', |
185
|
|
|
* `itemid` int(11) NOT NULL default '0', |
186
|
|
|
* `uid` int(11) NOT NULL default '0', |
187
|
|
|
* `price` float NOT NULL default '0', |
188
|
|
|
* `currency` varchar(100) NOT NULL default '', |
189
|
|
|
* PRIMARY KEY (`transactionid`)"); |
190
|
|
|
* |
191
|
|
|
* @param string $structure table structure |
192
|
|
|
* |
193
|
|
|
*/ |
194
|
|
|
public function setStructure($structure) |
195
|
|
|
{ |
196
|
|
|
$this->_structure = $structure; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Return the table structure |
201
|
|
|
* |
202
|
|
|
* @return string table structure |
203
|
|
|
* |
204
|
|
|
*/ |
205
|
|
|
public function getStructure() |
206
|
|
|
{ |
207
|
|
|
return sprintf($this->_structure, $this->name()); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Add values of a record to be added |
212
|
|
|
* |
213
|
|
|
* @param string $data values of a record |
214
|
|
|
* |
215
|
|
|
*/ |
216
|
|
|
public function setData($data) |
217
|
|
|
{ |
218
|
|
|
$this->_data[] = $data; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get the data array |
223
|
|
|
* |
224
|
|
|
* @return array containing the records values to be added |
225
|
|
|
* |
226
|
|
|
*/ |
227
|
|
|
public function getData() |
228
|
|
|
{ |
229
|
|
|
return $this->_data; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Use to insert data in a table |
234
|
|
|
* |
235
|
|
|
* @return bool true if success, false if an error occured |
236
|
|
|
* |
237
|
|
|
*/ |
238
|
|
|
public function addData() |
239
|
|
|
{ |
240
|
|
|
global $xoopsDB; |
241
|
|
|
foreach ($this->getData() as $data) { |
242
|
|
|
$query = sprintf('INSERT INTO %s VALUES (%s)', $this->name(), $data); |
243
|
|
|
$ret = $xoopsDB->query($query); |
244
|
|
|
if (!$ret) { |
245
|
|
|
echo ' ' . sprintf(_SDU_MSG_ADD_DATA_ERR, $this->name()) . '<br>'; |
246
|
|
|
} else { |
247
|
|
|
echo ' ' . sprintf(_SDU_MSG_ADD_DATA, $this->name()) . '<br>'; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return $ret; |
|
|
|
|
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Add a field to be added |
256
|
|
|
* |
257
|
|
|
* @param string $name name of the field |
258
|
|
|
* @param string $properties properties of the field |
259
|
|
|
* @param bool $newname |
260
|
|
|
* @param bool $showerror |
261
|
|
|
*/ |
262
|
|
|
public function addAlteredField($name, $properties, $newname = false, $showerror = true) |
263
|
|
|
{ |
264
|
|
|
$field['name'] = $name; |
|
|
|
|
265
|
|
|
$field['properties'] = $properties; |
266
|
|
|
$field['showerror'] = $showerror; |
267
|
|
|
$field['newname'] = $newname; |
268
|
|
|
$this->_alteredFields[] = $field; |
269
|
|
|
} |
270
|
|
|
/** |
271
|
|
|
* Invert values 0 to 1 and 1 to 0 |
272
|
|
|
* |
273
|
|
|
* @param string $name name of the field |
274
|
|
|
* @param $newValue |
275
|
|
|
* @param $oldValue |
276
|
|
|
* @internal param string $old old propertie |
277
|
|
|
* @internal param string $new new propertie |
278
|
|
|
*/ //felix |
279
|
|
|
public function addUpdatedWhere($name, $newValue, $oldValue) |
280
|
|
|
{ |
281
|
|
|
$field['name'] = $name; |
|
|
|
|
282
|
|
|
$field['value'] = $newValue; |
283
|
|
|
$field['where'] = $oldValue; |
284
|
|
|
$this->_updatedWhere[] = $field; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Add new field of a record to be added |
289
|
|
|
* |
290
|
|
|
* @param string $name name of the field |
291
|
|
|
* @param string $properties properties of the field |
292
|
|
|
* |
293
|
|
|
*/ |
294
|
|
|
public function addNewField($name, $properties) |
295
|
|
|
{ |
296
|
|
|
$field['name'] = $name; |
|
|
|
|
297
|
|
|
$field['properties'] = $properties; |
298
|
|
|
$this->_newFields[] = $field; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Get fields that need to be altered |
303
|
|
|
* |
304
|
|
|
* @return array fields that need to be altered |
305
|
|
|
* |
306
|
|
|
*/ |
307
|
|
|
public function getAlteredFields() |
308
|
|
|
{ |
309
|
|
|
return $this->_alteredFields; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Add field for which the value will be updated |
314
|
|
|
* |
315
|
|
|
* @param string $name name of the field |
316
|
|
|
* @param string $value value to be set |
317
|
|
|
* |
318
|
|
|
*/ |
319
|
|
|
public function addUpdatedField($name, $value) |
320
|
|
|
{ |
321
|
|
|
$field['name'] = $name; |
|
|
|
|
322
|
|
|
$field['value'] = $value; |
323
|
|
|
$this->_updatedFields[] = $field; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Get new fields to be added |
328
|
|
|
* |
329
|
|
|
* @return array fields to be added |
330
|
|
|
* |
331
|
|
|
*/ |
332
|
|
|
public function getNewFields() |
333
|
|
|
{ |
334
|
|
|
return $this->_newFields; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Get fields which values need to be updated |
339
|
|
|
* |
340
|
|
|
* @return array fields which values need to be updated |
341
|
|
|
* |
342
|
|
|
*/ |
343
|
|
|
public function getUpdatedFields() |
344
|
|
|
{ |
345
|
|
|
return $this->_updatedFields; |
346
|
|
|
} |
347
|
|
|
/** |
348
|
|
|
* Get fields which values need to be updated |
349
|
|
|
* |
350
|
|
|
* @return array fields which values need to be updated |
351
|
|
|
* |
352
|
|
|
*/ //felix |
353
|
|
|
public function getUpdatedWhere() |
354
|
|
|
{ |
355
|
|
|
return $this->_updatedWhere; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Add values of a record to be added |
360
|
|
|
* |
361
|
|
|
* @param string $name name of the field |
362
|
|
|
* |
363
|
|
|
*/ |
364
|
|
|
public function addDroppedField($name) |
365
|
|
|
{ |
366
|
|
|
$this->_droppedFields[] = $name; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Get fields that need to be dropped |
371
|
|
|
* |
372
|
|
|
* @return array fields that need to be dropped |
373
|
|
|
* |
374
|
|
|
*/ |
375
|
|
|
public function getDroppedFields() |
376
|
|
|
{ |
377
|
|
|
return $this->_droppedFields; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Set the flag to drop the table |
382
|
|
|
* |
383
|
|
|
*/ |
384
|
|
|
public function setFlagForDrop() |
385
|
|
|
{ |
386
|
|
|
$this->_flagForDrop = true; |
|
|
|
|
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Use to create a table |
391
|
|
|
* |
392
|
|
|
* @return bool true if success, false if an error occured |
393
|
|
|
* |
394
|
|
|
*/ |
395
|
|
|
public function createTable() |
396
|
|
|
{ |
397
|
|
|
global $xoopsDB; |
398
|
|
|
$query = $this->getStructure(); |
399
|
|
|
$query = 'CREATE TABLE `' . $this->name() . '` (' . $query . ") ENGINE=MyISAM COMMENT='The SmartFactory <www.smartfactory.ca>'"; |
400
|
|
|
//xoops_debug($query); |
401
|
|
|
$ret = $xoopsDB->query($query); |
402
|
|
|
if (!$ret) { |
403
|
|
|
echo ' ' . sprintf(_SDU_MSG_CREATE_TABLE_ERR, $this->name()) . ' (' . $xoopsDB->error() . ')<br>'; |
404
|
|
|
} else { |
405
|
|
|
echo ' ' . sprintf(_SDU_MSG_CREATE_TABLE, $this->name()) . '<br>'; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
return $ret; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* Use to drop a table |
413
|
|
|
* |
414
|
|
|
* @return bool true if success, false if an error occured |
415
|
|
|
* |
416
|
|
|
*/ |
417
|
|
|
public function dropTable() |
418
|
|
|
{ |
419
|
|
|
global $xoopsDB; |
420
|
|
|
$query = sprintf('DROP TABLE %s', $this->name()); |
421
|
|
|
$ret = $xoopsDB->query($query); |
422
|
|
|
if (!$ret) { |
423
|
|
|
echo ' ' . sprintf(_SDU_MSG_DROP_TABLE_ERR, $this->name()) . ' (' . $xoopsDB->error() . ')<br>'; |
424
|
|
|
|
425
|
|
|
return false; |
426
|
|
|
} else { |
427
|
|
|
echo ' ' . sprintf(_SDU_MSG_DROP_TABLE, $this->name()) . '<br>'; |
428
|
|
|
|
429
|
|
|
return true; |
430
|
|
|
} |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* Use to alter a table |
435
|
|
|
* |
436
|
|
|
* @return bool true if success, false if an error occured |
437
|
|
|
* |
438
|
|
|
*/ |
439
|
|
|
public function alterTable() |
440
|
|
|
{ |
441
|
|
|
global $xoopsDB; |
442
|
|
|
$ret = true; |
443
|
|
|
|
444
|
|
|
foreach ($this->getAlteredFields() as $alteredField) { |
445
|
|
|
if (!$alteredField['newname']) { |
446
|
|
|
$alteredField['newname'] = $alteredField['name']; |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
$query = sprintf('ALTER TABLE `%s` CHANGE `%s` `%s` %s', $this->name(), $alteredField['name'], $alteredField['newname'], $alteredField['properties']); |
450
|
|
|
$ret = $ret && $xoopsDB->query($query); |
451
|
|
|
if ($alteredField['showerror']) { |
452
|
|
|
if (!$ret) { |
453
|
|
|
echo ' ' . sprintf(_SDU_MSG_CHGFIELD_ERR, $alteredField['name'], $this->name()) . ' (' . $xoopsDB->error() . ')<br>'; |
454
|
|
|
} else { |
455
|
|
|
echo ' ' . sprintf(_SDU_MSG_CHGFIELD, $alteredField['name'], $this->name()) . '<br>'; |
456
|
|
|
} |
457
|
|
|
} |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
return $ret; |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* Use to add new fileds in the table |
465
|
|
|
* |
466
|
|
|
* @return bool true if success, false if an error occured |
467
|
|
|
* |
468
|
|
|
*/ |
469
|
|
|
public function addNewFields() |
470
|
|
|
{ |
471
|
|
|
global $xoopsDB; |
472
|
|
|
$ret = true; |
473
|
|
|
foreach ($this->getNewFields() as $newField) { |
474
|
|
|
$query = sprintf('ALTER TABLE `%s` ADD `%s` %s', $this->name(), $newField['name'], $newField['properties']); |
475
|
|
|
//echo $query; |
476
|
|
|
$ret = $ret && $xoopsDB->query($query); |
477
|
|
|
if (!$ret) { |
478
|
|
|
echo ' ' . sprintf(_SDU_MSG_NEWFIELD_ERR, $newField['name'], $this->name()) . '<br>'; |
479
|
|
|
} else { |
480
|
|
|
echo ' ' . sprintf(_SDU_MSG_NEWFIELD, $newField['name'], $this->name()) . '<br>'; |
481
|
|
|
} |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
return $ret; |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
/** |
488
|
|
|
* Use to update fields values |
489
|
|
|
* |
490
|
|
|
* @return bool true if success, false if an error occured |
491
|
|
|
* |
492
|
|
|
*/ |
493
|
|
|
public function updateFieldsValues() |
494
|
|
|
{ |
495
|
|
|
global $xoopsDB; |
496
|
|
|
$ret = true; |
497
|
|
|
foreach ($this->getUpdatedFields() as $updatedField) { |
498
|
|
|
$query = sprintf('UPDATE %s SET %s = %s', $this->name(), $updatedField['name'], $updatedField['value']); |
499
|
|
|
$ret = $ret && $xoopsDB->query($query); |
500
|
|
View Code Duplication |
if (!$ret) { |
|
|
|
|
501
|
|
|
echo ' ' . sprintf(_SDU_MSG_UPDATE_TABLE_ERR, $this->name()) . ' (' . $xoopsDB->error() . ')<br>'; |
502
|
|
|
} else { |
503
|
|
|
echo ' ' . sprintf(_SDU_MSG_UPDATE_TABLE, $this->name()) . '<br>'; |
504
|
|
|
} |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
return $ret; |
508
|
|
|
} |
509
|
|
|
/** |
510
|
|
|
* Use to update fields values |
511
|
|
|
* |
512
|
|
|
* @return bool true if success, false if an error occured |
513
|
|
|
* |
514
|
|
|
*/ //felix |
515
|
|
|
public function updateWhereValues() |
516
|
|
|
{ |
517
|
|
|
global $xoopsDB; |
518
|
|
|
$ret = true; |
519
|
|
|
foreach ($this->getUpdatedWhere() as $updatedWhere) { |
520
|
|
|
$query = sprintf('UPDATE %s SET %s = %s WHERE %s %s', $this->name(), $updatedWhere['name'], $updatedWhere['value'], $updatedWhere['name'], $updatedWhere['where']); |
521
|
|
|
//echo $query."<br>"; |
522
|
|
|
$ret = $ret && $xoopsDB->query($query); |
523
|
|
View Code Duplication |
if (!$ret) { |
|
|
|
|
524
|
|
|
echo ' ' . sprintf(_SDU_MSG_UPDATE_TABLE_ERR, $this->name()) . ' (' . $xoopsDB->error() . ')<br>'; |
525
|
|
|
} else { |
526
|
|
|
echo ' ' . sprintf(_SDU_MSG_UPDATE_TABLE, $this->name()) . '<br>'; |
527
|
|
|
} |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
return $ret; |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
/** |
534
|
|
|
* Use to drop fields |
535
|
|
|
* |
536
|
|
|
* @return bool true if success, false if an error occured |
537
|
|
|
* |
538
|
|
|
*/ |
539
|
|
|
public function dropFields() |
540
|
|
|
{ |
541
|
|
|
global $xoopsDB; |
542
|
|
|
$ret = true; |
543
|
|
|
foreach ($this->getDroppedFields() as $droppedField) { |
544
|
|
|
$query = sprintf('ALTER TABLE %s DROP %s', $this->name(), $droppedField); |
545
|
|
|
$ret = $ret && $xoopsDB->query($query); |
546
|
|
|
if (!$ret) { |
547
|
|
|
echo ' ' . sprintf(_SDU_MSG_DROPFIELD_ERR, $droppedField, $this->name()) . ' (' . $xoopsDB->error() . ')<br>'; |
548
|
|
|
} else { |
549
|
|
|
echo ' ' . sprintf(_SDU_MSG_DROPFIELD, $droppedField, $this->name()) . '<br>'; |
550
|
|
|
} |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
return $ret; |
554
|
|
|
} |
555
|
|
|
} |
556
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.