Test Setup Failed
Push — dev ( 608138...99eb65 )
by Rafael
61:41 queued 16s
created

Constant::selectByName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* Copyright (C) 2024       Rafael San José         <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * any later version.
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.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace Dolibarr\Core\Model;
20
21
use Carbon\Carbon;
22
use Dolibarr\Core\Base\Model;
23
24
/**
25
 * Class Const
26
 *
27
 * @property int $rowid
28
 * @property string $name
29
 * @property int $entity
30
 * @property string $value
31
 * @property string|null $type
32
 * @property int $visible
33
 * @property string|null $note
34
 * @property Carbon|null $tms
35
 */
36
class Constant extends Model
37
{
38
    public $timestamps = false;
39
    protected $table = 'const';
40
    protected $primaryKey = 'rowid';
41
    protected $casts = [
42
        'entity' => 'int',
43
        'visible' => 'int',
44
        'tms' => 'datetime'
45
    ];
46
    protected $fillable = [
47
        'name',
48
        'entity',
49
        'value',
50
        'type',
51
        'visible',
52
        'note',
53
        'tms'
54
    ];
55
56
    public static function getConstants()
57
    {
58
        global $conf, $user;
59
60
        $query = Constant::select('rowid')
61
            ->selectRaw(static::decrypt('name') . ' AS name')
62
            ->selectRaw(static::decrypt('value') . ' AS value')
63
            ->addSelect('type', 'note', 'entity');
64
65
        if (isModEnabled('multicompany')) {
66
            if ($user->entity) {
67
                $query->whereIn('entity', explode(',', $user->entity . ',' . $conf->entity));
68
            }
69
        } else {
70
            $query->whereIn('entity', [0, $conf->entity]);
71
        }
72
73
        $query->orderBy('entity')
74
            ->orderBy('name', 'ASC');
75
76
        return $query->get();
77
    }
78
79
    public static function selectByName($name, $entity = null)
80
    {
81
        $query = Constant::where('name', static::decrypt($name));
82
        if ($query === null) {
83
            return null;
84
        }
85
        if (isset($entity)) {
86
            $query->whereIn('entity', [0, $entity]);
87
        }
88
        return $query->get();
89
    }
90
91
    public static function getByName($name, $entity = null)
92
    {
93
        $query = Constant::where('name', static::decrypt($name));
94
        if ($query === null) {
95
            return null;
96
        }
97
        if (isset($entity)) {
98
            $query->whereIn('entity', [0, $entity]);
99
        }
100
        return $query->first();
101
    }
102
103
    /**
104
     * Returns the highest name starting with $value
105
     *
106
     * TODO: I suspect that Dolibarr does not work properly with field encryption enabled.
107
     *
108
     * @param $value
109
     * @return null
110
     */
111
    public static function getMaxNameLike(string $value)
112
    {
113
        $query = Constant::where('name', 'like', $value . '%')
114
            ->max('name');
115
116
        if (!isset($query)) {
117
            return null;
118
        }
119
120
        return $query;
121
    }
122
123
    public static function insert(string $name, string $value, string $type, string $note, int $visible = 0, int $entity = 0): bool
124
    {
125
        return Constant::create([
126
            'name' => static::encrypt($name),
127
            'value' => static::encrypt($value),
128
            'type' => static::encrypt($type),
129
            'visible' => $visible,
130
            'note' => $note,
131
            'entity' => $entity
132
        ]);
133
    }
134
135
    public static function deleteByName(string $name, ?int $entity = null): ?bool
136
    {
137
        $query = Constant::where('name', static::decrypt($name));
138
        if ($query === null) {
139
            return null;
140
        }
141
        if (isset($entity)) {
142
            $query->whereIn('entity', [0, $entity]);
143
        }
144
        return $query
145
            ->delete();
146
    }
147
148
    public static function selectByNameAndSuffix(string $name_like, string $suffix, ?int $entity = null)
149
    {
150
        $query = Constant::where('name', 'like', $name_like . '_%_' . strtoupper($suffix));
151
        if ($query === null) {
152
            return null;
153
        }
154
        if (isset($entity)) {
155
            $query->where('entity', $entity);
156
        }
157
        return $query
158
            ->get(['name', 'entity', 'value']);  // Return only the columns 'name', 'entity' y 'value'.
159
    }
160
161
    public static function deleteByNameLike(string $name_like, ?int $entity = null): ?bool
162
    {
163
        $query = Constant::where('name', 'like', $name_like . '%');
164
        if ($query === null) {
165
            return null;
166
        }
167
        if (isset($entity)) {
168
            $query->where('entity', $entity);
169
        }
170
        return $query
171
            ->delete();
172
    }
173
174
    public static function updateByName(string $name, array $values): bool
175
    {
176
        $encrypted_fields = ['name', 'value', 'type'];
177
        foreach ($encrypted_fields as $field) {
178
            if (isset($values[$field])) {
179
                $values[$field] = static::encrypt($values[$field]);
180
            }
181
        }
182
183
        return Constant::where('name', static::encrypt($name))
0 ignored issues
show
Bug Best Practice introduced by
The expression return Dolibarr\Core\Mod...name))->update($values) returns the type Illuminate\Database\Eloq...s\HasOneThrough|integer which is incompatible with the type-hinted return boolean.
Loading history...
184
            ->update($values);
185
    }
186
187
    public static function updateValueByName(string $name, string $value): bool
188
    {
189
        return Constant::where('name', static::encrypt($name))
0 ignored issues
show
Bug Best Practice introduced by
The expression return Dolibarr\Core\Mod...atic::encrypt($value))) returns the type Illuminate\Database\Eloq...s\HasOneThrough|integer which is incompatible with the type-hinted return boolean.
Loading history...
190
            ->update(['value', static::encrypt($value)]);
191
    }
192
193
    public static function isActivated(string $name, int $entity = 0): ?bool
194
    {
195
        $result = Constant::selectRaw(static::decrypt('name') . ' AS value')
196
            ->where('name', static::encrypt($name))
197
            ->whereIn('entity', [0, $entity]);
198
        if ($result === null) {
199
            return null;
200
        }
201
        return !empty($result->first());
202
    }
203
204
    public static function getAllModulesConstants()
205
    {
206
        return Constant::where('name', 'LIKE', 'MAIN_MODULE_%_TPL')
207
            ->orWhere('name', 'LIKE', 'MAIN_MODULE_%_CSS')
208
            ->orWhere('name', 'LIKE', 'MAIN_MODULE_%_JS')
209
            ->orWhere('name', 'LIKE', 'MAIN_MODULE_%_HOOKS')
210
            ->orWhere('name', 'LIKE', 'MAIN_MODULE_%_TRIGGERS')
211
            ->orWhere('name', 'LIKE', 'MAIN_MODULE_%_THEME')
212
            ->orWhere('name', 'LIKE', 'MAIN_MODULE_%_SUBSTITUTIONS')
213
            ->orWhere('name', 'LIKE', 'MAIN_MODULE_%_MODELS')
214
            ->orWhere('name', 'LIKE', 'MAIN_MODULE_%_MENUS')
215
            ->orWhere('name', 'LIKE', 'MAIN_MODULE_%_LOGIN')
216
            ->orWhere('name', 'LIKE', 'MAIN_MODULE_%_BARCODE')
217
            ->orWhere('name', 'LIKE', 'MAIN_MODULE_%_TABS_%')
218
            ->orWhere('name', 'LIKE', 'MAIN_MODULE_%_MODULEFOREXTERNAL')
219
            ->orderBy('name')
220
            ->orderBy('entity')
221
            ->get(['name', 'entity', 'value']);  // Return only the columns 'name', 'entity' y 'value'.
222
    }
223
}
224