ConfigOptionHandler::insert()   B
last analyzed

Complexity

Conditions 9
Paths 27

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 23
c 0
b 0
f 0
nc 27
nop 2
dl 0
loc 36
rs 8.0555
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Xhelp;
4
5
/*
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * @copyright    {@link https://xoops.org/ XOOPS Project}
17
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
18
 * @author       Brian Wahoff <[email protected]>
19
 * @author       Eric Juden <[email protected]>
20
 * @author       XOOPS Development Team
21
 */
22
23
/**
24
 * class ConfigOptionHandler
25
 */
26
class ConfigOptionHandler extends \XoopsConfigOptionHandler
27
{
28
    /**
29
     * Autoincrementing DB fieldname
30
     * @var string
31
     */
32
    public $idfield = 'confop_id';
33
34
    /**
35
     * Constructor
36
     *
37
     * @param \XoopsDatabase|null $db reference to a xoopsDB object
38
     */
39
    public function init(\XoopsDatabase $db = null): void
40
    {
41
        $this->db = $db;
42
    }
43
44
    /**
45
     * DB table name
46
     *
47
     * @var string
48
     */
49
    public $dbtable = 'configoption';
50
51
    private const TABLE = 'xhelp_ticket_fields';
52
    private const ENTITY = TicketField::class;
53
    private const ENTITYNAME = 'TicketField';
54
    private const KEYNAME = 'id';
55
    private const IDENTIFIER = 'name';
56
57
    /**
58
     * delete configoption matching a set of conditions
59
     *
60
     * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link CriteriaElement}
61
     * @param bool                                 $force
62
     * @return bool FALSE if deletion failed
63
     */
64
    public function deleteAll(\CriteriaElement $criteria = null, bool $force = false): bool
65
    {
66
        $sql = 'DELETE FROM ' . $this->db->prefix($this->dbtable);
67
        if (($criteria instanceof \CriteriaCompo) || ($criteria instanceof \Criteria)) {
68
            $sql .= ' ' . $criteria->renderWhere();
69
        }
70
        if ($force) {
71
            $result = $this->db->queryF($sql);
72
        } else {
73
            $result = $this->db->query($sql);
74
        }
75
        if (!$result) {
76
            return false;
77
        }
78
        return true;
79
    }
80
81
    /**
82
     * Insert a new option in the database
83
     *
84
     * @param \XoopsObject $confoption reference to a {@link XoopsConfigOption}
85
     * @param bool         $force
86
     * @return bool|int TRUE if successfull.
87
     */
88
    public function insert(\XoopsObject $confoption, bool $force = false)
89
    {
90
        if ('xoopsconfigoption' !== \mb_strtolower(\get_class($confoption))) {
91
            return false;
92
        }
93
        if (!$confoption->isDirty()) {
94
            return true;
95
        }
96
        if (!$confoption->cleanVars()) {
97
            return false;
98
        }
99
        //TODO mb replace with individual variables
100
        foreach ($confoption->cleanVars as $k => $v) {
101
            ${$k} = $v;
102
        }
103
        if ($confoption->isNew()) {
104
            $confop_id = $this->db->genId('configoption_confop_id_seq');
105
            $sql       = \sprintf('INSERT INTO `%s` (confop_id, confop_name, confop_value, conf_id) VALUES (%u, %s, %s, %u)', $this->db->prefix('configoption'), $confop_id, $this->db->quoteString($confop_name), $this->db->quoteString($confop_value), $conf_id);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $confop_value does not exist. Did you maybe mean $confop_id?
Loading history...
Comprehensibility Best Practice introduced by
The variable $conf_id does not exist. Did you maybe mean $confop_id?
Loading history...
Comprehensibility Best Practice introduced by
The variable $confop_name does not exist. Did you maybe mean $confop_id?
Loading history...
106
        } else {
107
            $sql = \sprintf('UPDATE `%s` SET confop_name = %s, confop_value = %s WHERE confop_id = %u', $this->db->prefix('configoption'), $this->db->quoteString($confop_name), $this->db->quoteString($confop_value), $confop_id);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $confop_id seems to be never defined.
Loading history...
108
        }
109
110
        if ($force) {
111
            $result = $this->db->queryF($sql);
112
        } else {
113
            $result = $this->db->query($sql);
114
        }
115
        if (!$result) {
116
            return false;
117
        }
118
        if (empty($confop_id)) {
119
            $confop_id = $this->db->getInsertId();
120
        }
121
        $confoption->assignVar('confop_id', $confop_id);
122
123
        return $confop_id;
124
    }
125
}
126