Completed
Push — master ( 454ebd...de22fe )
by Michael
03:58
created

SmartDbTable::setData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php namespace XoopsModules\Smartfaq;
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 19 and the first side effect is on line 61.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * Detemines if a table exists in the current db
5
 *
6
 * @param  string $table the table name (without XOOPS prefix)
7
 * @return bool   True if table exists, false if not
8
 *
9
 * @access public
10
 * @author xhelp development team
11
 */
12
13
use XoopsModules\Smartfaq;
14
15
/**
16
 * @param $table
17
 * @return bool
18
 */
19
function smart_TableExists($table)
20
{
21
    $bRetVal = false;
22
    //Verifies that a MySQL table exists
23
    $xoopsDB  = \XoopsDatabaseFactory::getDatabaseConnection();
24
    $realname = $xoopsDB->prefix($table);
25
    $sql      = 'SHOW TABLES FROM ' . XOOPS_DB_NAME;
26
    $ret      = $xoopsDB->queryF($sql);
27
    while (list($m_table) = $xoopsDB->fetchRow($ret)) {
28
        if ($m_table == $realname) {
29
            $bRetVal = true;
30
            break;
31
        }
32
    }
33
    $xoopsDB->freeRecordSet($ret);
34
35
    return $bRetVal;
36
}
37
38
/**
39
 * Contains the classes for updating database tables
40
 *
41
 * @license GNU
42
 * @author  marcan <[email protected]>
43
 * @link    http://www.smartfactory.ca The SmartFactory
44
 * @package SmartObject
45
 */
46
47
/**
48
 * SmartDbTable class
49
 *
50
 * Information about an individual table
51
 *
52
 * @package SmartObject
53
 * @author  marcan <[email protected]>
54
 * @link    http://www.smartfactory.ca The SmartFactory
55
 */
56
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
57
58
/**
59
 * Include the language constants for the SmartObjectDBUpdater
60
 */
61
global $xoopsConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
62
63
/** @var Smartfaq\Helper $helper */
64
$helper = Smartfaq\Helper::getInstance();
65
66
$helper->loadLanguage('smartdbupdater');
67
//
0 ignored issues
show
Unused Code Comprehensibility introduced by
44% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
68
//$common_file = XOOPS_ROOT_PATH . '/modules/smartfaq/language/' . $xoopsConfig['language'] . '/smartdbupdater.php';
69
//if (!file_exists($common_file)) {
70
//    $common_file = XOOPS_ROOT_PATH . '/modules/smartfaq/language/english/smartdbupdater.php';
71
//}
72
//include $common_file;
73
74
/**
75
 * Class SmartDbTable
76
 */
77
class SmartDbTable
78
{
79
    /**
80
     * @var string $_name name of the table
81
     */
82
    private $_name;
83
84
    /**
85
     * @var string $_structure structure of the table
86
     */
87
    private $_structure;
88
89
    /**
90
     * @var array $_data containing valued of each records to be added
91
     */
92
    private $_data;
93
94
    /**
95
     * @var array $_alteredFields containing fields to be altered
96
     */
97
    private $_alteredFields;
98
99
    /**
100
     * @var array $_newFields containing new fields to be added
101
     */
102
    private $_newFields;
103
104
    /**
105
     * @var array $_droppedFields containing fields to be dropped
106
     */
107
    private $_droppedFields;
108
109
    /**
110
     * @var array $_flagForDrop flag table to drop it
111
     */
112
    private $_flagForDrop = false;
113
114
    /**
115
     * @var array $_updatedFields containing fields which values will be updated
116
     */
117
    private $_updatedFields;
118
119
    /**
120
     * @var array $_updatedFields containing fields which values will be updated
121
     */    //felix
122
    private $_updatedWhere;
123
124
    /**
125
     * Constructor
126
     *
127
     * @param string $name name of the table
128
     *
129
     */
130
    public function __construct($name)
131
    {
132
        $this->_name = $name;
133
        $this->_data = [];
134
    }
135
136
    /**
137
     * Return the table name, prefixed with site table prefix
138
     *
139
     * @return string table name
140
     *
141
     */
142
    public function name()
143
    {
144
        global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
145
146
        return $xoopsDB->prefix($this->_name);
147
    }
148
149
    /**
150
     * Checks if the table already exists in the database
151
     *
152
     * @return bool TRUE if it exists, FALSE if not
153
     *
154
     */
155
    public function exists()
156
    {
157
        return smart_TableExists($this->_name);
158
    }
159
160
    /**
161
     * @return mixed
162
     */
163
    public function getExistingFieldsArray()
164
    {
165
        global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
166
        $result = $xoopsDB->queryF('SHOW COLUMNS FROM ' . $this->name());
167
        while ($existing_field = $xoopsDB->fetchArray($result)) {
168
            $fields[$existing_field['Field']] = $existing_field['Type'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$fields was never initialized. Although not strictly required by PHP, it is generally a good practice to add $fields = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

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 the bar 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.

Loading history...
169
            if ('YES' !== $existing_field['Null']) {
170
                $fields[$existing_field['Field']] .= ' NOT NULL';
171
            }
172
            if ($existing_field['Extra']) {
173
                $fields[$existing_field['Field']] .= ' ' . $existing_field['Extra'];
174
            }
175
        }
176
177
        return $fields;
0 ignored issues
show
Bug introduced by
The variable $fields does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
178
    }
179
180
    /**
181
     * @param $field
182
     * @return bool
183
     */
184
    public function fieldExists($field)
185
    {
186
        $existingFields = $this->getExistingFieldsArray();
187
188
        return isset($existingFields[$field]);
189
    }
190
191
    /**
192
     * Set the table structure
193
     *
194
     * @param string $structure table structure
195
     *
196
     */
197
    public function setStructure($structure)
198
    {
199
        $this->_structure = $structure;
200
    }
201
202
    /**
203
     * Return the table structure
204
     *
205
     * @return string table structure
206
     *
207
     */
208
    public function getStructure()
209
    {
210
        return sprintf($this->_structure, $this->name());
211
    }
212
213
    /**
214
     * Add values of a record to be added
215
     *
216
     * @param string $data values of a record
217
     *
218
     */
219
    public function setData($data)
220
    {
221
        $this->_data[] = $data;
222
    }
223
224
    /**
225
     * Get the data array
226
     *
227
     * @return array containing the records values to be added
228
     *
229
     */
230
    public function getData()
231
    {
232
        return $this->_data;
233
    }
234
235
    /**
236
     * Use to insert data in a table
237
     *
238
     * @return bool true if success, false if an error occured
239
     *
240
     */
241
    public function addData()
242
    {
243
        global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
244
245
        foreach ($this->getData() as $data) {
246
            $query = sprintf('INSERT INTO %s VALUES ("%s")', $this->name(), $data);
247
            $ret   = $xoopsDB->queryF($query);
248
            if (!$ret) {
249
                echo '&nbsp;&nbsp;' . sprintf(_SDU_MSG_ADD_DATA_ERR, $this->name()) . '<br>';
250
            } else {
251
                echo '&nbsp;&nbsp;' . sprintf(_SDU_MSG_ADD_DATA, $this->name()) . '<br>';
252
            }
253
        }
254
255
        return $ret;
0 ignored issues
show
Bug introduced by
The variable $ret does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
256
    }
257
258
    /**
259
     * Add a field to be added
260
     *
261
     * @param string $name       name of the field
262
     * @param string $properties properties of the field
263
     * @param bool   $showerror
264
     */
265
    public function addAlteredField($name, $properties, $showerror = true)
266
    {
267
        $field['name']          = $name;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$field was never initialized. Although not strictly required by PHP, it is generally a good practice to add $field = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

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 the bar 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.

Loading history...
268
        $field['properties']    = $properties;
269
        $field['showerror']     = $showerror;
270
        $this->_alteredFields[] = $field;
271
    }
272
273
    /**
274
     * Invert values 0 to 1 and 1 to 0
275
     *
276
     * @param string $name name of the field
277
     * @param        $newValue
278
     * @param        $oldValue
279
     * @internal param string $old old propertie
280
     * @internal param string $new new propertie
281
     */    //felix
282
    public function addUpdatedWhere($name, $newValue, $oldValue)
283
    {
284
        $field['name']         = $name;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$field was never initialized. Although not strictly required by PHP, it is generally a good practice to add $field = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

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 the bar 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.

Loading history...
285
        $field['value']        = $newValue;
286
        $field['where']        = $oldValue;
287
        $this->_updatedWhere[] = $field;
288
    }
289
290
    /**
291
     * Add new field of a record to be added
292
     *
293
     * @param string $name       name of the field
294
     * @param string $properties properties of the field
295
     *
296
     */
297
    public function addNewField($name, $properties)
298
    {
299
        $field['name']       = $name;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$field was never initialized. Although not strictly required by PHP, it is generally a good practice to add $field = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

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 the bar 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.

Loading history...
300
        $field['properties'] = $properties;
301
        $this->_newFields[]  = $field;
302
    }
303
304
    /**
305
     * Get fields that need to be altered
306
     *
307
     * @return array fields that need to be altered
308
     *
309
     */
310
    public function getAlteredFields()
311
    {
312
        return $this->_alteredFields;
313
    }
314
315
    /**
316
     * Add field for which the value will be updated
317
     *
318
     * @param string $name  name of the field
319
     * @param string $value value to be set
320
     *
321
     */
322
    public function addUpdatedField($name, $value)
323
    {
324
        $field['name']          = $name;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$field was never initialized. Although not strictly required by PHP, it is generally a good practice to add $field = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

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 the bar 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.

Loading history...
325
        $field['value']         = $value;
326
        $this->_updatedFields[] = $field;
327
    }
328
329
    /**
330
     * Get new fields to be added
331
     *
332
     * @return array fields to be added
333
     *
334
     */
335
    public function getNewFields()
336
    {
337
        return $this->_newFields;
338
    }
339
340
    /**
341
     * Get fields which values need to be updated
342
     *
343
     * @return array fields which values need to be updated
344
     *
345
     */
346
    public function getUpdatedFields()
347
    {
348
        return $this->_updatedFields;
349
    }
350
351
    /**
352
     * Get fields which values need to be updated
353
     *
354
     * @return array fields which values need to be updated
355
     *
356
     */    //felix
357
    public function getUpdatedWhere()
358
    {
359
        return $this->_updatedWhere;
360
    }
361
362
    /**
363
     * Add values of a record to be added
364
     *
365
     * @param string $name name of the field
366
     *
367
     */
368
    public function addDroppedField($name)
369
    {
370
        $this->_droppedFields[] = $name;
371
    }
372
373
    /**
374
     * Get fields that need to be dropped
375
     *
376
     * @return array fields that need to be dropped
377
     *
378
     */
379
    public function getDroppedFields()
380
    {
381
        return $this->_droppedFields;
382
    }
383
384
    /**
385
     * Set the flag to drop the table
386
     *
387
     */
388
    public function setFlagForDrop()
389
    {
390
        $this->_flagForDrop = true;
0 ignored issues
show
Documentation Bug introduced by
It seems like true of type boolean is incompatible with the declared type array of property $_flagForDrop.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
391
    }
392
393
    /**
394
     * Get the flag to drop the table
395
     *
396
     */
397
    public function getFlagForDrop()
398
    {
399
        return $this->_flagForDrop;
400
    }
401
402
    /**
403
     * Use to create a table
404
     *
405
     * @return bool true if success, false if an error occured
406
     *
407
     */
408
    public function createTable()
409
    {
410
        global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
411
412
        $query = $this->getStructure();
413
414
        $ret = $xoopsDB->queryF($query);
415
        if (!$ret) {
416
            echo '&nbsp;&nbsp;' . sprintf(_SDU_MSG_CREATE_TABLE_ERR, $this->name()) . '<br>';
417
        } else {
418
            echo '&nbsp;&nbsp;' . sprintf(_SDU_MSG_CREATE_TABLE, $this->name()) . '<br>';
419
        }
420
421
        return $ret;
422
    }
423
424
    /**
425
     * Use to drop a table
426
     *
427
     * @return bool true if success, false if an error occured
428
     *
429
     */
430
    public function dropTable()
431
    {
432
        global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
433
434
        $query = sprintf('DROP TABLE %s', $this->name());
435
        $ret   = $xoopsDB->queryF($query);
436
        if (!$ret) {
437
            echo '&nbsp;&nbsp;' . sprintf(_SDU_MSG_DROP_TABLE_ERR, $this->name()) . '<br>';
438
439
            return false;
440
        } else {
441
            echo '&nbsp;&nbsp;' . sprintf(_SDU_MSG_DROP_TABLE, $this->name()) . '<br>';
442
443
            return true;
444
        }
445
    }
446
447
    /**
448
     * Use to alter a table
449
     *
450
     * @return bool true if success, false if an error occured
451
     *
452
     */
453
    public function alterTable()
454
    {
455
        global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
456
457
        $ret = true;
458
459
        foreach ($this->getAlteredFields() as $alteredField) {
460
            $query = sprintf('ALTER TABLE `%s` CHANGE `%s` %s', $this->name(), $alteredField['name'], $alteredField['properties']);
461
            //echo $query;
462
            $ret = $ret && $xoopsDB->queryF($query);
463
            if ($alteredField['showerror']) {
464
                if (!$ret) {
465
                    echo '&nbsp;&nbsp;' . sprintf(_SDU_MSG_CHGFIELD_ERR, $alteredField['name'], $this->name()) . '<br>';
466
                } else {
467
                    echo '&nbsp;&nbsp;' . sprintf(_SDU_MSG_CHGFIELD, $alteredField['name'], $this->name()) . '<br>';
468
                }
469
            }
470
        }
471
472
        return $ret;
473
    }
474
475
    /**
476
     * Use to add new fileds in the table
477
     *
478
     * @return bool true if success, false if an error occured
479
     *
480
     */
481
    public function addNewFields()
482
    {
483
        global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
484
485
        $ret = true;
486
        foreach ($this->getNewFields() as $newField) {
487
            $query = sprintf('ALTER TABLE `%s` ADD `%s` %s', $this->name(), $newField['name'], $newField['properties']);
488
            //echo $query;
489
            $ret = $ret && $xoopsDB->queryF($query);
490
            if (!$ret) {
491
                echo '&nbsp;&nbsp;' . sprintf(_SDU_MSG_NEWFIELD_ERR, $newField['name'], $this->name()) . '<br>';
492
            } else {
493
                echo '&nbsp;&nbsp;' . sprintf(_SDU_MSG_NEWFIELD, $newField['name'], $this->name()) . '<br>';
494
            }
495
        }
496
497
        return $ret;
498
    }
499
500
    /**
501
     * Use to update fields values
502
     *
503
     * @return bool true if success, false if an error occured
504
     *
505
     */
506
    public function updateFieldsValues()
507
    {
508
        global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
509
510
        $ret = true;
511
512
        foreach ($this->getUpdatedFields() as $updatedField) {
513
            $query = sprintf('UPDATE %s SET %s = %s', $this->name(), $updatedField['name'], $updatedField['value']);
514
            $ret   = $ret && $xoopsDB->queryF($query);
515 View Code Duplication
            if (!$ret) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
516
                echo '&nbsp;&nbsp;' . sprintf(_SDU_MSG_UPDATE_TABLE_ERR, $this->name()) . '<br>';
517
            } else {
518
                echo '&nbsp;&nbsp;' . sprintf(_SDU_MSG_UPDATE_TABLE, $this->name()) . '<br>';
519
            }
520
        }
521
522
        return $ret;
523
    }
524
    /**
525
     * Use to update fields values
526
     *
527
     * @return bool true if success, false if an error occured
528
     *
529
     */        //felix
530
    public function updateWhereValues()
531
    {
532
        global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
533
534
        $ret = true;
535
536
        foreach ($this->getUpdatedWhere() as $updatedWhere) {
537
            $query = sprintf('UPDATE %s SET %s = %s WHERE %s  %s', $this->name(), $updatedWhere['name'], $updatedWhere['value'], $updatedWhere['name'], $updatedWhere['where']);
538
            //echo $query."<br>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
539
            $ret = $ret && $xoopsDB->queryF($query);
540 View Code Duplication
            if (!$ret) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
541
                echo '&nbsp;&nbsp;' . sprintf(_SDU_MSG_UPDATE_TABLE_ERR, $this->name()) . '<br>';
542
            } else {
543
                echo '&nbsp;&nbsp;' . sprintf(_SDU_MSG_UPDATE_TABLE, $this->name()) . '<br>';
544
            }
545
        }
546
547
        return $ret;
548
    }
549
550
    /**
551
     * Use to drop fields
552
     *
553
     * @return bool true if success, false if an error occured
554
     *
555
     */
556
    public function dropFields()
557
    {
558
        global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
559
560
        $ret = true;
561
562
        foreach ($this->getDroppedFields() as $droppedField) {
563
            $query = sprintf('ALTER TABLE %s DROP %s', $this->name(), $droppedField);
564
565
            $ret = $ret && $xoopsDB->queryF($query);
566
            if (!$ret) {
567
                echo '&nbsp;&nbsp;' . sprintf(_SDU_MSG_DROPFIELD_ERR, $droppedField, $this->name()) . '<br>';
568
            } else {
569
                echo '&nbsp;&nbsp;' . sprintf(_SDU_MSG_DROPFIELD, $droppedField, $this->name()) . '<br>';
570
            }
571
        }
572
573
        return $ret;
574
    }
575
}
576