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
|
|
|
} else { |
72
|
|
|
$this->authorsField = [ |
73
|
|
|
'field_type' => ExtraField::FIELD_TYPE_SELECT_MULTIPLE, |
74
|
|
|
'variable' => 'authors', |
75
|
|
|
'display_text' => 'Authors', |
76
|
|
|
'default_value' => '', |
77
|
|
|
'field_order' => 0, |
78
|
|
|
'visible_to_self' => 1, |
79
|
|
|
'visible_to_others' => 0, |
80
|
|
|
'changeable' => 1, |
81
|
|
|
'filter' => 1, |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Create a new instance of CheckExtraFieldAuthorsCompanyPlugin. |
88
|
|
|
* |
89
|
|
|
* @return CheckExtraFieldAuthorsCompanyPlugin |
90
|
|
|
*/ |
91
|
|
|
public static function create() |
92
|
|
|
{ |
93
|
|
|
static $result = null; |
94
|
|
|
|
95
|
|
|
return $result ? $result : $result = new self(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Perform the plugin installation. |
100
|
|
|
*/ |
101
|
|
|
public function install() |
102
|
|
|
{ |
103
|
|
|
$this->saveCompanyField(); |
104
|
|
|
$this->setCompanyExtrafieldData(); |
105
|
|
|
$this->saveAuthorsField(); |
106
|
|
|
$this->savePrice(); |
107
|
|
|
$this->saveAuthorLPItem(); |
108
|
|
|
$this->saveAuthorLp(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Save the arrangement for company, it is adjusted internally so that the values match the necessary ones. |
113
|
|
|
*/ |
114
|
|
|
public function saveCompanyField() |
115
|
|
|
{ |
116
|
|
|
$data = $this->companyField; |
117
|
|
|
$data['field_type'] = (int) $data['field_type']; |
118
|
|
|
$data['field_order'] = (int) $data['field_order']; |
119
|
|
|
$data['visible_to_self'] = (int) $data['visible_to_self']; |
120
|
|
|
$data['visible_to_others'] = (int) $data['visible_to_others']; |
121
|
|
|
$data['changeable'] = (int) $data['changeable']; |
122
|
|
|
$data['filter'] = (int) $data['filter']; |
123
|
|
|
$data['default_value'] = ''; |
124
|
|
|
$data['variable'] = 'company'; |
125
|
|
|
$data['visible'] = 1; |
126
|
|
|
$data['display_text'] = strtolower(Database::escape_string($data['display_text'])); |
127
|
|
|
$schedule = new ExtraField('user'); |
128
|
|
|
$this->companyField['id'] = $schedule->save($data); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Insert the option fields for company with the generic values Company 1, company 2 and company 3. |
133
|
|
|
*/ |
134
|
|
|
public function setCompanyExtrafieldData() |
135
|
|
|
{ |
136
|
|
|
$companies = [ |
137
|
|
|
0 => 'Company 1', |
138
|
|
|
1 => 'Company 2', |
139
|
|
|
2 => 'Company 3', |
140
|
|
|
]; |
141
|
|
|
$companyId = (int) $this->companyField['id']; |
142
|
|
|
if ($companyId != 0) { |
143
|
|
|
for ($i = 0; $i < count($companies); $i++) { |
|
|
|
|
144
|
|
|
$order = $i + 1; |
145
|
|
|
$extraFieldOptionValue = $companies[$i]; |
146
|
|
|
if ($companyId != null) { |
147
|
|
|
$query = "SELECT * |
148
|
|
|
FROM ".$this->tblExtraFieldOption." |
149
|
|
|
WHERE |
150
|
|
|
option_value = '$extraFieldOptionValue' AND |
151
|
|
|
field_id = $companyId"; |
152
|
|
|
$extraFieldOption = Database::fetch_assoc(Database::query($query)); |
153
|
|
|
if (isset($extraFieldOption['id']) && $extraFieldOption['id'] && $extraFieldOption['field_id'] == $companyId) { |
154
|
|
|
// Update? |
155
|
|
|
} else { |
156
|
|
|
$query = " |
157
|
|
|
INSERT INTO ".$this->tblExtraFieldOption." |
158
|
|
|
(`field_id`, `option_value`, `display_text`, `priority`, `priority_message`, `option_order`) VALUES |
159
|
|
|
( '$companyId', '$extraFieldOptionValue', '$extraFieldOptionValue', NULL, NULL, '$order'); |
160
|
|
|
"; |
161
|
|
|
Database::query($query); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Save the arrangement for authors, it is adjusted internally so that the values match the necessary ones. |
170
|
|
|
*/ |
171
|
|
|
public function saveAuthorsField() |
172
|
|
|
{ |
173
|
|
|
$data = $this->authorsField; |
174
|
|
|
$data['field_type'] = (int) $data['field_type']; |
175
|
|
|
$data['field_order'] = (int) $data['field_order']; |
176
|
|
|
$data['visible_to_self'] = (int) $data['visible_to_self']; |
177
|
|
|
$data['visible_to_others'] = (int) $data['visible_to_others']; |
178
|
|
|
$data['changeable'] = (int) $data['changeable']; |
179
|
|
|
$data['filter'] = (int) $data['filter']; |
180
|
|
|
$data['default_value'] = null; |
181
|
|
|
$data['variable'] = 'authors'; |
182
|
|
|
$data['visible'] = 1; |
183
|
|
|
$data['display_text'] = strtolower($data['display_text']); |
184
|
|
|
$schedule = new ExtraField('lp'); |
185
|
|
|
$schedule->save($data); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Save the arrangement for price, it is adjusted internally so that the values match the necessary ones. |
190
|
|
|
*/ |
191
|
|
|
public function savePrice() |
192
|
|
|
{ |
193
|
|
|
$data = $this->authorsField; |
194
|
|
|
$schedule = new ExtraField('lp_item'); |
195
|
|
|
$data['visible_to_self'] = 1; |
196
|
|
|
$data['visible_to_others'] = 1; |
197
|
|
|
$data['changeable'] = 1; |
198
|
|
|
$data['filter'] = 0; |
199
|
|
|
$data['variable'] = 'price'; |
200
|
|
|
$data['display_text'] = 'SalePrice'; |
201
|
|
|
$data['field_type'] = ExtraField::FIELD_TYPE_INTEGER; |
202
|
|
|
|
203
|
|
|
$schedule->save($data); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Save the arrangement for AuthorLPItem, it is adjusted internally so that the values match the necessary ones. |
208
|
|
|
*/ |
209
|
|
|
public function saveAuthorLPItem() |
210
|
|
|
{ |
211
|
|
|
$data = $this->authorsField; |
212
|
|
|
$schedule = new ExtraField('lp_item'); |
213
|
|
|
$data['visible_to_self'] = 0; |
214
|
|
|
$data['visible_to_others'] = 0; |
215
|
|
|
$data['changeable'] = 1; |
216
|
|
|
$data['filter'] = 0; |
217
|
|
|
$data['variable'] = 'authorlpitem'; |
218
|
|
|
$data['display_text'] = 'LearningPathItemByAuthor'; |
219
|
|
|
$data['field_type'] = ExtraField::FIELD_TYPE_SELECT_MULTIPLE; |
220
|
|
|
|
221
|
|
|
$this->authorsField['id'] = $schedule->save($data); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Save the arrangement for authorlp, it is adjusted internally so that the values match the necessary ones. |
226
|
|
|
*/ |
227
|
|
|
public function saveAuthorLp() |
228
|
|
|
{ |
229
|
|
|
$schedule = new ExtraField('user'); |
230
|
|
|
$data = $schedule->get_handler_field_info_by_field_variable('authorlp'); |
231
|
|
|
if (empty($data)) { |
232
|
|
|
$data = $this->authorsField; |
233
|
|
|
} |
234
|
|
|
if (!isset($data['id']) || (int) $data['id'] == 0) { |
235
|
|
|
$data['variable'] = 'authorlp'; |
236
|
|
|
$data['display_text'] = 'authors'; |
237
|
|
|
$data['changeable'] = 1; |
238
|
|
|
$data['visible_to_self'] = 1; |
239
|
|
|
$data['visible_to_others'] = 0; |
240
|
|
|
$data['filter'] = 0; |
241
|
|
|
$data['field_type'] = ExtraField::FIELD_TYPE_RADIO; |
242
|
|
|
$id = $schedule->save($data); |
243
|
|
|
} else { |
244
|
|
|
$this->authorsField = $data; |
|
|
|
|
245
|
|
|
$id = $data['id']; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
$this->setYesNoToAuthor($id); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Set Yes or Not selector for authorlp field. |
253
|
|
|
* |
254
|
|
|
* @param $authorLpId |
255
|
|
|
*/ |
256
|
|
|
public function setYesNoToAuthor($authorLpId) |
257
|
|
|
{ |
258
|
|
|
$options = [ |
259
|
|
|
0 => 'No', |
260
|
|
|
1 => 'Yes', |
261
|
|
|
]; |
262
|
|
|
$order = 0; |
263
|
|
|
$authorId = (int) $authorLpId; |
264
|
|
|
if ($authorId != 0) { |
265
|
|
|
$extraFieldValueUser = new ExtraFieldOption('user'); |
266
|
|
|
$items = $extraFieldValueUser->get_field_options_by_field($authorLpId); |
267
|
|
|
foreach ($items as $item) { |
268
|
|
|
if (isset($options[0]) && |
269
|
|
|
(isset($item['option_value']) == $options[0] || isset($item['option_value']) == $options[1]) |
270
|
|
|
) { |
271
|
|
|
unset($options[$item['option_value']]); |
272
|
|
|
$order++; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
for ($i = 0; $i < count($options); $i++) { |
|
|
|
|
277
|
|
|
if (isset($options[$i])) { |
278
|
|
|
$extraFieldOptionValue = $options[$i]; |
279
|
|
|
$fieldOption = new ExtraFieldOption('user'); |
280
|
|
|
$fieldOption->saveOptions( |
281
|
|
|
[ |
282
|
|
|
'field_id' => $authorLpId, |
283
|
|
|
'option_value' => $order, |
284
|
|
|
'display_text' => $extraFieldOptionValue, |
285
|
|
|
'option_order' => $order, |
286
|
|
|
] |
287
|
|
|
); |
288
|
|
|
} |
289
|
|
|
$order++; |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Remove the extra fields set by the plugin. |
296
|
|
|
*/ |
297
|
|
|
public function uninstall() |
298
|
|
|
{ |
299
|
|
|
$companyExist = $this->companyFieldExist(); |
300
|
|
|
if ($companyExist == true) { |
|
|
|
|
301
|
|
|
// $this->removeCompanyField(); |
302
|
|
|
} |
303
|
|
|
$authorsExist = $this->authorsFieldExist(); |
304
|
|
|
if ($authorsExist == true) { |
|
|
|
|
305
|
|
|
// $this->removeAuthorsField(); |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Verify that the "company" field exists in the database. |
311
|
|
|
* |
312
|
|
|
* @return bool |
313
|
|
|
*/ |
314
|
|
|
public function companyFieldExist() |
315
|
|
|
{ |
316
|
|
|
$this->getCompanyField(); |
317
|
|
|
$this->companyExist = (isset($this->companyField['id'])) ? true : false; |
318
|
|
|
|
319
|
|
|
return $this->companyExist; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Returns the content of the extra field "company" if it exists in the database, if not, it returns an arrangement |
324
|
|
|
* with the basic elements for its operation. |
325
|
|
|
* |
326
|
|
|
* @return array |
327
|
|
|
*/ |
328
|
|
|
public function getCompanyField() |
329
|
|
|
{ |
330
|
|
|
$companyField = $this->getInfoExtrafield('company'); |
331
|
|
|
if (count($companyField) > 1) { |
332
|
|
|
$this->companyField = $companyField; |
333
|
|
|
} else { |
334
|
|
|
$companyField = $this->companyField; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
return $companyField; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Returns the array of an element in the database that matches the variable. |
342
|
|
|
* |
343
|
|
|
* @param string $variableName |
344
|
|
|
* |
345
|
|
|
* @return array |
346
|
|
|
*/ |
347
|
|
|
protected function getInfoExtrafield($variableName = null) |
348
|
|
|
{ |
349
|
|
|
if ($variableName == null) { |
|
|
|
|
350
|
|
|
return []; |
351
|
|
|
} |
352
|
|
|
$variableName = strtolower(Database::escape_string($variableName)); |
353
|
|
|
$tblExtraField = $this->tblExtraField; |
354
|
|
|
$query = "SELECT * FROM $tblExtraField WHERE variable = '$variableName'"; |
355
|
|
|
$data = Database::fetch_assoc(Database::query($query)); |
356
|
|
|
if ($data == false || !isset($data['display_text'])) { |
357
|
|
|
return []; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
return $data; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Verify that the "authors" field exists in the database. |
365
|
|
|
* |
366
|
|
|
* @return bool |
367
|
|
|
*/ |
368
|
|
|
public function authorsFieldExist() |
369
|
|
|
{ |
370
|
|
|
$this->getAuthorsField(); |
371
|
|
|
$this->authorsExist = (isset($this->authorsField['id'])) ? true : false; |
372
|
|
|
|
373
|
|
|
return $this->authorsExist; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Returns the content of the extra field "authors" if it exists in the database, if not, it returns an arrangement |
378
|
|
|
* with the basic elements for its operation. |
379
|
|
|
* |
380
|
|
|
* @return array |
381
|
|
|
*/ |
382
|
|
|
public function getAuthorsField() |
383
|
|
|
{ |
384
|
|
|
$schedule = new ExtraField('lp'); |
385
|
|
|
$data = $schedule->get_handler_field_info_by_field_variable('authors'); |
386
|
|
|
if (empty($data)) { |
387
|
|
|
$this->authorsField = $data; |
388
|
|
|
} else { |
389
|
|
|
$data = $this->authorsField; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
return $data; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Remove the extra fields "company". |
397
|
|
|
*/ |
398
|
|
|
public function removeCompanyField() |
399
|
|
|
{ |
400
|
|
|
$data = $this->getCompanyField(); |
401
|
|
|
// $this->deleteQuery($data); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* Remove the extra fields "authors". |
406
|
|
|
*/ |
407
|
|
|
public function removeAuthorsField() |
408
|
|
|
{ |
409
|
|
|
$data = $this->getAuthorsField(); |
410
|
|
|
// $this->deleteQuery($data); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* Executes fix removal for authors or company. |
415
|
|
|
* |
416
|
|
|
* @param $data |
417
|
|
|
*/ |
418
|
|
|
protected function deleteQuery($data) |
419
|
|
|
{ |
420
|
|
|
$exist = null; |
421
|
|
|
$validVariable = false; |
422
|
|
|
$variable = $data['variable']; |
423
|
|
|
$extraFieldTypeInt = (int) $data['extra_field_type']; |
424
|
|
|
$FieldType = (int) $data['field_type']; |
425
|
|
|
$id = (int) $data['id']; |
426
|
|
|
$extraFieldType = null; |
427
|
|
|
if ($variable === 'company') { |
428
|
|
|
$validVariable = true; |
429
|
|
|
$extraFieldType = 'user'; |
430
|
|
|
} elseif ($variable === 'authors') { |
431
|
|
|
$validVariable = true; |
432
|
|
|
$extraFieldType = 'lp'; |
433
|
|
|
} |
434
|
|
|
if ($validVariable == true && $id != 0 && !empty($extraFieldType)) { |
|
|
|
|
435
|
|
|
$query = "SELECT id |
436
|
|
|
FROM |
437
|
|
|
".$this->tblExtraField." |
438
|
|
|
WHERE |
439
|
|
|
id = $id AND |
440
|
|
|
variable = '$variable' AND |
441
|
|
|
extra_field_type = $extraFieldTypeInt AND |
442
|
|
|
field_type = $FieldType |
443
|
|
|
"; |
444
|
|
|
$data = Database::fetch_assoc(Database::query($query)); |
445
|
|
|
if (isset($data['id'])) { |
446
|
|
|
$obj = new ExtraField($extraFieldType); |
447
|
|
|
$obj->delete($data['id']); |
448
|
|
|
} |
449
|
|
|
} |
450
|
|
|
} |
451
|
|
|
} |
452
|
|
|
|
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: