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