Passed
Push — 1.11.x ( 972b7a...0e4d67 )
by Julito
11:22
created

saveAuthorsField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 15
rs 9.8666
c 1
b 1
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
class CheckExtraFieldAuthorsCompanyPlugin extends Plugin
6
{
7
    /**
8
     * @var string
9
     */
10
    protected $tblExtraFieldOption;
11
12
    /**
13
     * @var string
14
     */
15
    protected $tblExtraField;
16
17
    /**
18
     * @var bool
19
     */
20
    protected $authorsExist;
21
22
    /**
23
     * @var bool
24
     */
25
    protected $companyExist;
26
27
    /**
28
     * @var array
29
     */
30
    protected $authorsField;
31
32
    /**
33
     * @var array
34
     */
35
    protected $companyField;
36
37
    public function __construct()
38
    {
39
        parent::__construct(
40
            '1.2',
41
            'Carlos Alvarado, Julio Montoya'
42
        );
43
        $this->tblExtraField = Database::get_main_table(TABLE_EXTRA_FIELD);
44
        $this->tblExtraFieldOption = Database::get_main_table(TABLE_EXTRA_FIELD_OPTIONS);
45
        $field = new ExtraField('user');
46
        $companyField = $field->get_handler_field_info_by_field_variable('company');
47
        $this->companyExist = false;
48
        if (empty($companyField)) {
49
            $this->companyExist = true;
50
            $this->companyField = $companyField;
51
        } else {
52
            $this->companyField = [
53
                'field_type' => ExtraField::FIELD_TYPE_RADIO,
54
                'variable' => 'company',
55
                'display_text' => 'Company',
56
                'default_value' => '',
57
                'field_order' => 0,
58
                'visible_to_self' => 0,
59
                'visible_to_others' => 0,
60
                'changeable' => 1,
61
                'filter' => 1,
62
            ];
63
        }
64
        $field = new ExtraField('lp');
65
        $authorsField = $field->get_handler_field_info_by_field_variable('authors');
66
67
        $this->authorsExist = false;
68
        if (empty($authorsField)) {
69
            $this->authorsExist = true;
70
            $this->authorsField = $authorsField;
71
        }
72
    }
73
74
    /**
75
     * Create a new instance of CheckExtraFieldAuthorsCompanyPlugin.
76
     *
77
     * @return CheckExtraFieldAuthorsCompanyPlugin
78
     */
79
    public static function create()
80
    {
81
        static $result = null;
82
83
        return $result ? $result : $result = new self();
84
    }
85
86
    /**
87
     * Perform the plugin installation.
88
     */
89
    public function install()
90
    {
91
        $this->saveCompanyField();
92
        $this->setCompanyExtrafieldData();
93
        $this->saveAuthorsField();
94
        $this->savePrice();
95
        $this->saveAuthorLPItem();
96
        $this->saveAuthorLp();
97
    }
98
99
    /**
100
     * Save the arrangement for company, it is adjusted internally so that the values match the necessary ones.
101
     */
102
    public function saveCompanyField()
103
    {
104
        $data = $this->companyField;
105
        $data['field_type'] = (int) $data['field_type'];
106
        $data['field_order'] = (int) $data['field_order'];
107
        $data['visible_to_self'] = (int) $data['visible_to_self'];
108
        $data['visible_to_others'] = (int) $data['visible_to_others'];
109
        $data['changeable'] = (int) $data['changeable'];
110
        $data['filter'] = (int) $data['filter'];
111
        $data['default_value'] = '';
112
        $data['variable'] = 'company';
113
        $data['visible'] = 1;
114
        $data['display_text'] = strtolower(Database::escape_string($data['display_text']));
115
        $schedule = new ExtraField('user');
116
        $this->companyField['id'] = $schedule->save($data);
117
    }
118
119
    /**
120
     * Insert the option fields for company with the generic values Company 1, company 2 and company 3.
121
     */
122
    public function setCompanyExtrafieldData()
123
    {
124
        $companies = [
125
            0 => 'Company 1',
126
            1 => 'Company 2',
127
            2 => 'Company 3',
128
        ];
129
        $companyId = (int) $this->companyField['id'];
130
        if ($companyId != 0) {
131
            for ($i = 0; $i < count($companies); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
132
                $order = $i + 1;
133
                $extraFieldOptionValue = $companies[$i];
134
                if ($companyId != null) {
135
                    $query = "SELECT *
136
                              FROM ".$this->tblExtraFieldOption."
137
                              WHERE
138
                                    option_value = '$extraFieldOptionValue' AND
139
                                    field_id = $companyId";
140
                    $extraFieldOption = Database::fetch_assoc(Database::query($query));
141
                    if (isset($extraFieldOption['id']) && $extraFieldOption['id'] && $extraFieldOption['field_id'] == $companyId) {
142
                        // Update?
143
                    } else {
144
                        $query = "
145
                        INSERT INTO ".$this->tblExtraFieldOption."
146
                            (`field_id`, `option_value`, `display_text`, `priority`, `priority_message`, `option_order`) VALUES
147
                            ( '$companyId', '$extraFieldOptionValue', '$extraFieldOptionValue', NULL, NULL, '$order');
148
                        ";
149
                        Database::query($query);
150
                    }
151
                }
152
            }
153
        }
154
    }
155
156
    /**
157
     * Save the arrangement for authors, it is adjusted internally so that the values match the necessary ones.
158
     */
159
    public function saveAuthorsField()
160
    {
161
        $data = [
162
            'field_type' => ExtraField::FIELD_TYPE_SELECT_MULTIPLE,
163
            'variable' => 'authors',
164
            'display_text' => 'Authors',
165
            'default_value' => '',
166
            'field_order' => 0,
167
            'visible_to_self' => 1,
168
            'visible_to_others' => 0,
169
            'changeable' => 1,
170
            'filter' => 1,
171
        ];
172
        $schedule = new ExtraField('lp');
173
        $schedule->save($data);
174
    }
175
176
    /**
177
     * Save the arrangement for price, it is adjusted internally so that the values match the necessary ones.
178
     */
179
    public function savePrice()
180
    {
181
        $schedule = new ExtraField('lp_item');
182
        $data = [];
183
        $data['visible_to_self'] = 1;
184
        $data['visible_to_others'] = 1;
185
        $data['changeable'] = 1;
186
        $data['filter'] = 0;
187
        $data['variable'] = 'price';
188
        $data['display_text'] = 'SalePrice';
189
        $data['field_type'] = ExtraField::FIELD_TYPE_INTEGER;
190
191
        $schedule->save($data);
192
    }
193
194
    /**
195
     * Save the arrangement for AuthorLPItem, it is adjusted internally so that the values match the necessary ones.
196
     */
197
    public function saveAuthorLPItem()
198
    {
199
        $schedule = new ExtraField('lp_item');
200
        $data = [];
201
        $data['visible_to_self'] = 0;
202
        $data['visible_to_others'] = 0;
203
        $data['changeable'] = 1;
204
        $data['filter'] = 0;
205
        $data['variable'] = 'authorlpitem';
206
        $data['display_text'] = 'LearningPathItemByAuthor';
207
        $data['field_type'] = ExtraField::FIELD_TYPE_SELECT_MULTIPLE;
208
        $schedule->save($data);
209
    }
210
211
    /**
212
     * Save the arrangement for authorlp, it is adjusted internally so that the values match the necessary ones.
213
     */
214
    public function saveAuthorLp()
215
    {
216
        $schedule = new ExtraField('user');
217
        $data = [];
218
        $data['variable'] = 'authorlp';
219
        $data['display_text'] = 'authors';
220
        $data['changeable'] = 1;
221
        $data['visible_to_self'] = 1;
222
        $data['visible_to_others'] = 0;
223
        $data['filter'] = 0;
224
        $data['field_type'] = ExtraField::FIELD_TYPE_CHECKBOX;
225
        $schedule->save($data);
226
    }
227
228
    /**
229
     * Remove the extra fields set by the plugin.
230
     */
231
    public function uninstall()
232
    {
233
        $companyExist = $this->companyFieldExist();
234
        if ($companyExist == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
235
            // $this->removeCompanyField();
236
        }
237
        $authorsExist = $this->authorsFieldExist();
238
        if ($authorsExist == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
239
            // $this->removeAuthorsField();
240
        }
241
    }
242
243
    /**
244
     * Verify that the "company" field exists in the database.
245
     *
246
     * @return bool
247
     */
248
    public function companyFieldExist()
249
    {
250
        $this->getCompanyField();
251
        $this->companyExist = (isset($this->companyField['id'])) ? true : false;
252
253
        return $this->companyExist;
254
    }
255
256
    /**
257
     * Returns the content of the extra field "company" if it exists in the database, if not, it returns an arrangement
258
     * with the basic elements for its operation.
259
     *
260
     * @return array
261
     */
262
    public function getCompanyField()
263
    {
264
        $companyField = $this->getInfoExtrafield('company');
265
        if (count($companyField) > 1) {
266
            $this->companyField = $companyField;
267
        } else {
268
            $companyField = $this->companyField;
269
        }
270
271
        return $companyField;
272
    }
273
274
    /**
275
     * Returns the array of an element in the database that matches the variable.
276
     *
277
     * @param string $variableName
278
     *
279
     * @return array
280
     */
281
    protected function getInfoExtrafield($variableName = null)
282
    {
283
        if ($variableName == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $variableName of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
284
            return [];
285
        }
286
        $variableName = strtolower(Database::escape_string($variableName));
287
        $tblExtraField = $this->tblExtraField;
288
        $query = "SELECT * FROM $tblExtraField WHERE variable = '$variableName'";
289
        $data = Database::fetch_assoc(Database::query($query));
290
        if ($data == false || !isset($data['display_text'])) {
291
            return [];
292
        }
293
294
        return $data;
295
    }
296
297
    /**
298
     * Verify that the "authors" field exists in the database.
299
     *
300
     * @return bool
301
     */
302
    public function authorsFieldExist()
303
    {
304
        $this->getAuthorsField();
305
        $this->authorsExist = (isset($this->authorsField['id'])) ? true : false;
306
307
        return $this->authorsExist;
308
    }
309
310
    /**
311
     * Returns the content of the extra field "authors" if it exists in the database, if not, it returns an arrangement
312
     * with the basic elements for its operation.
313
     *
314
     * @return array
315
     */
316
    public function getAuthorsField()
317
    {
318
        $schedule = new ExtraField('lp');
319
        $data = $schedule->get_handler_field_info_by_field_variable('authors');
320
        if (empty($data)) {
321
            $this->authorsField = $data;
322
        } else {
323
            $data = $this->authorsField;
324
        }
325
326
        return $data;
327
    }
328
329
    /**
330
     * Remove the extra fields "company".
331
     */
332
    public function removeCompanyField()
333
    {
334
        $data = $this->getCompanyField();
335
        // $this->deleteQuery($data);
336
    }
337
338
    /**
339
     * Remove the extra fields "authors".
340
     */
341
    public function removeAuthorsField()
342
    {
343
        $data = $this->getAuthorsField();
344
        // $this->deleteQuery($data);
345
    }
346
347
    /**
348
     * Executes fix removal for authors or company.
349
     *
350
     * @param $data
351
     */
352
    protected function deleteQuery($data)
353
    {
354
        $exist = null;
355
        $validVariable = false;
356
        $variable = $data['variable'];
357
        $extraFieldTypeInt = (int) $data['extra_field_type'];
358
        $FieldType = (int) $data['field_type'];
359
        $id = (int) $data['id'];
360
        $extraFieldType = null;
361
        if ($variable === 'company') {
362
            $validVariable = true;
363
            $extraFieldType = 'user';
364
        } elseif ($variable === 'authors') {
365
            $validVariable = true;
366
            $extraFieldType = 'lp';
367
        }
368
        if ($validVariable == true && $id != 0 && !empty($extraFieldType)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
369
            $query = "SELECT id
370
                        FROM
371
                            ".$this->tblExtraField."
372
                        WHERE
373
                            id = $id AND
374
                            variable = '$variable' AND
375
                            extra_field_type = $extraFieldTypeInt AND
376
                            field_type = $FieldType
377
                        ";
378
            $data = Database::fetch_assoc(Database::query($query));
379
            if (isset($data['id'])) {
380
                $obj = new ExtraField($extraFieldType);
381
                $obj->delete($data['id']);
382
            }
383
        }
384
    }
385
}
386