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
|
|||
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'] = 1; |
||
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
|
|||
235 | // $this->removeCompanyField(); |
||
236 | } |
||
237 | $authorsExist = $this->authorsFieldExist(); |
||
238 | if ($authorsExist == true) { |
||
0 ignored issues
–
show
|
|||
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 | * Verify that the "authors" field exists in the database. |
||
276 | * |
||
277 | * @return bool |
||
278 | */ |
||
279 | public function authorsFieldExist() |
||
280 | { |
||
281 | $this->getAuthorsField(); |
||
282 | $this->authorsExist = (isset($this->authorsField['id'])) ? true : false; |
||
283 | |||
284 | return $this->authorsExist; |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Returns the content of the extra field "authors" if it exists in the database, if not, it returns an arrangement |
||
289 | * with the basic elements for its operation. |
||
290 | * |
||
291 | * @return array |
||
292 | */ |
||
293 | public function getAuthorsField() |
||
294 | { |
||
295 | $schedule = new ExtraField('lp'); |
||
296 | $data = $schedule->get_handler_field_info_by_field_variable('authors'); |
||
297 | if (empty($data)) { |
||
298 | $this->authorsField = $data; |
||
299 | } else { |
||
300 | $data = $this->authorsField; |
||
301 | } |
||
302 | |||
303 | return $data; |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Remove the extra fields "company". |
||
308 | */ |
||
309 | public function removeCompanyField() |
||
310 | { |
||
311 | $data = $this->getCompanyField(); |
||
312 | // $this->deleteQuery($data); |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Remove the extra fields "authors". |
||
317 | */ |
||
318 | public function removeAuthorsField() |
||
319 | { |
||
320 | $data = $this->getAuthorsField(); |
||
321 | // $this->deleteQuery($data); |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Executes fix removal for authors or company. |
||
326 | * |
||
327 | * @param $data |
||
328 | */ |
||
329 | protected function deleteQuery($data) |
||
330 | { |
||
331 | $exist = null; |
||
332 | $validVariable = false; |
||
333 | $variable = $data['variable']; |
||
334 | $extraFieldTypeInt = (int) $data['extra_field_type']; |
||
335 | $FieldType = (int) $data['field_type']; |
||
336 | $id = (int) $data['id']; |
||
337 | $extraFieldType = null; |
||
338 | if ($variable === 'company') { |
||
339 | $validVariable = true; |
||
340 | $extraFieldType = 'user'; |
||
341 | } elseif ($variable === 'authors') { |
||
342 | $validVariable = true; |
||
343 | $extraFieldType = 'lp'; |
||
344 | } |
||
345 | if ($validVariable == true && $id != 0 && !empty($extraFieldType)) { |
||
0 ignored issues
–
show
|
|||
346 | $query = "SELECT id |
||
347 | FROM |
||
348 | ".$this->tblExtraField." |
||
349 | WHERE |
||
350 | id = $id AND |
||
351 | variable = '$variable' AND |
||
352 | extra_field_type = $extraFieldTypeInt AND |
||
353 | field_type = $FieldType |
||
354 | "; |
||
355 | $data = Database::fetch_assoc(Database::query($query)); |
||
356 | if (isset($data['id'])) { |
||
357 | $obj = new ExtraField($extraFieldType); |
||
358 | $obj->delete($data['id']); |
||
359 | } |
||
360 | } |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * Returns the array of an element in the database that matches the variable. |
||
365 | * |
||
366 | * @param string $variableName |
||
367 | * |
||
368 | * @return array |
||
369 | */ |
||
370 | protected function getInfoExtrafield($variableName = null) |
||
371 | { |
||
372 | if ($variableName == null) { |
||
0 ignored issues
–
show
|
|||
373 | return []; |
||
374 | } |
||
375 | $variableName = strtolower(Database::escape_string($variableName)); |
||
376 | $tblExtraField = $this->tblExtraField; |
||
377 | $query = "SELECT * FROM $tblExtraField WHERE variable = '$variableName'"; |
||
378 | $data = Database::fetch_assoc(Database::query($query)); |
||
379 | if ($data == false || !isset($data['display_text'])) { |
||
380 | return []; |
||
381 | } |
||
382 | |||
383 | return $data; |
||
384 | } |
||
385 | } |
||
386 |
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: