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++) { |
|
|
|
|
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_RADIO; |
225
|
|
|
$id = $schedule->save($data); |
226
|
|
|
$this->setYesNoToAuthor($id); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Set Yes or Not selector for authorlp field. |
231
|
|
|
* |
232
|
|
|
* @param $authorLpId |
233
|
|
|
*/ |
234
|
|
|
public function setYesNoToAuthor($authorLpId) |
235
|
|
|
{ |
236
|
|
|
$options = [ |
237
|
|
|
0 => 'No', |
238
|
|
|
1 => 'Yes', |
239
|
|
|
]; |
240
|
|
|
$order = 0; |
241
|
|
|
$authorId = (int) $authorLpId; |
242
|
|
|
if ($authorId != 0) { |
243
|
|
|
$extraFieldValueUser = new ExtraFieldOption('user'); |
244
|
|
|
$items = $extraFieldValueUser->get_field_options_by_field($authorLpId); |
245
|
|
|
if (!empty($items)) { |
246
|
|
|
foreach ($items as $item) { |
247
|
|
|
if (isset($options[0]) && |
248
|
|
|
(isset($item['option_value']) == $options[0] || isset($item['option_value']) == $options[1]) |
249
|
|
|
) { |
250
|
|
|
unset($options[$item['option_value']]); |
251
|
|
|
$order++; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
for ($i = 0; $i < count($options); $i++) { |
|
|
|
|
257
|
|
|
if (isset($options[$i])) { |
258
|
|
|
$extraFieldOptionValue = $options[$i]; |
259
|
|
|
$fieldOption = new ExtraFieldOption('user'); |
260
|
|
|
$fieldOption->saveOptions( |
261
|
|
|
[ |
262
|
|
|
'field_id' => $authorLpId, |
263
|
|
|
'option_value' => $order, |
264
|
|
|
'display_text' => $extraFieldOptionValue, |
265
|
|
|
'option_order' => $order, |
266
|
|
|
] |
267
|
|
|
); |
268
|
|
|
} |
269
|
|
|
$order++; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Remove the extra fields set by the plugin. |
276
|
|
|
*/ |
277
|
|
|
public function uninstall() |
278
|
|
|
{ |
279
|
|
|
$companyExist = $this->companyFieldExist(); |
280
|
|
|
if ($companyExist == true) { |
|
|
|
|
281
|
|
|
// $this->removeCompanyField(); |
282
|
|
|
} |
283
|
|
|
$authorsExist = $this->authorsFieldExist(); |
284
|
|
|
if ($authorsExist == true) { |
|
|
|
|
285
|
|
|
// $this->removeAuthorsField(); |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Verify that the "company" field exists in the database. |
291
|
|
|
* |
292
|
|
|
* @return bool |
293
|
|
|
*/ |
294
|
|
|
public function companyFieldExist() |
295
|
|
|
{ |
296
|
|
|
$this->getCompanyField(); |
297
|
|
|
$this->companyExist = (isset($this->companyField['id'])) ? true : false; |
298
|
|
|
|
299
|
|
|
return $this->companyExist; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Returns the content of the extra field "company" if it exists in the database, if not, it returns an arrangement |
304
|
|
|
* with the basic elements for its operation. |
305
|
|
|
* |
306
|
|
|
* @return array |
307
|
|
|
*/ |
308
|
|
|
public function getCompanyField() |
309
|
|
|
{ |
310
|
|
|
$companyField = $this->getInfoExtrafield('company'); |
311
|
|
|
if (count($companyField) > 1) { |
312
|
|
|
$this->companyField = $companyField; |
313
|
|
|
} else { |
314
|
|
|
$companyField = $this->companyField; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
return $companyField; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Returns the array of an element in the database that matches the variable. |
322
|
|
|
* |
323
|
|
|
* @param string $variableName |
324
|
|
|
* |
325
|
|
|
* @return array |
326
|
|
|
*/ |
327
|
|
|
protected function getInfoExtrafield($variableName = null) |
328
|
|
|
{ |
329
|
|
|
if ($variableName == null) { |
|
|
|
|
330
|
|
|
return []; |
331
|
|
|
} |
332
|
|
|
$variableName = strtolower(Database::escape_string($variableName)); |
333
|
|
|
$tblExtraField = $this->tblExtraField; |
334
|
|
|
$query = "SELECT * FROM $tblExtraField WHERE variable = '$variableName'"; |
335
|
|
|
$data = Database::fetch_assoc(Database::query($query)); |
336
|
|
|
if ($data == false || !isset($data['display_text'])) { |
337
|
|
|
return []; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
return $data; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Verify that the "authors" field exists in the database. |
345
|
|
|
* |
346
|
|
|
* @return bool |
347
|
|
|
*/ |
348
|
|
|
public function authorsFieldExist() |
349
|
|
|
{ |
350
|
|
|
$this->getAuthorsField(); |
351
|
|
|
$this->authorsExist = (isset($this->authorsField['id'])) ? true : false; |
352
|
|
|
|
353
|
|
|
return $this->authorsExist; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Returns the content of the extra field "authors" if it exists in the database, if not, it returns an arrangement |
358
|
|
|
* with the basic elements for its operation. |
359
|
|
|
* |
360
|
|
|
* @return array |
361
|
|
|
*/ |
362
|
|
|
public function getAuthorsField() |
363
|
|
|
{ |
364
|
|
|
$schedule = new ExtraField('lp'); |
365
|
|
|
$data = $schedule->get_handler_field_info_by_field_variable('authors'); |
366
|
|
|
if (empty($data)) { |
367
|
|
|
$this->authorsField = $data; |
368
|
|
|
} else { |
369
|
|
|
$data = $this->authorsField; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
return $data; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Remove the extra fields "company". |
377
|
|
|
*/ |
378
|
|
|
public function removeCompanyField() |
379
|
|
|
{ |
380
|
|
|
$data = $this->getCompanyField(); |
381
|
|
|
// $this->deleteQuery($data); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Remove the extra fields "authors". |
386
|
|
|
*/ |
387
|
|
|
public function removeAuthorsField() |
388
|
|
|
{ |
389
|
|
|
$data = $this->getAuthorsField(); |
390
|
|
|
// $this->deleteQuery($data); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Executes fix removal for authors or company. |
395
|
|
|
* |
396
|
|
|
* @param $data |
397
|
|
|
*/ |
398
|
|
|
protected function deleteQuery($data) |
399
|
|
|
{ |
400
|
|
|
$exist = null; |
401
|
|
|
$validVariable = false; |
402
|
|
|
$variable = $data['variable']; |
403
|
|
|
$extraFieldTypeInt = (int) $data['extra_field_type']; |
404
|
|
|
$FieldType = (int) $data['field_type']; |
405
|
|
|
$id = (int) $data['id']; |
406
|
|
|
$extraFieldType = null; |
407
|
|
|
if ($variable === 'company') { |
408
|
|
|
$validVariable = true; |
409
|
|
|
$extraFieldType = 'user'; |
410
|
|
|
} elseif ($variable === 'authors') { |
411
|
|
|
$validVariable = true; |
412
|
|
|
$extraFieldType = 'lp'; |
413
|
|
|
} |
414
|
|
|
if ($validVariable == true && $id != 0 && !empty($extraFieldType)) { |
|
|
|
|
415
|
|
|
$query = "SELECT id |
416
|
|
|
FROM |
417
|
|
|
".$this->tblExtraField." |
418
|
|
|
WHERE |
419
|
|
|
id = $id AND |
420
|
|
|
variable = '$variable' AND |
421
|
|
|
extra_field_type = $extraFieldTypeInt AND |
422
|
|
|
field_type = $FieldType |
423
|
|
|
"; |
424
|
|
|
$data = Database::fetch_assoc(Database::query($query)); |
425
|
|
|
if (isset($data['id'])) { |
426
|
|
|
$obj = new ExtraField($extraFieldType); |
427
|
|
|
$obj->delete($data['id']); |
428
|
|
|
} |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
} |
432
|
|
|
|
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: