PackageUpdate::updateBlock()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 19
ccs 0
cts 10
cp 0
rs 9.7666
cc 2
nc 2
nop 4
crap 6
1
<?php
2
/* +**********************************************************************************
3
 * The contents of this file are subject to the vtiger CRM Public License Version 1.0
4
 * ("License"); You may not use this file except in compliance with the License
5
 * The Original Code is:  vtiger CRM Open Source
6
 * The Initial Developer of the Original Code is vtiger.
7
 * Portions created by vtiger are Copyright (C) vtiger.
8
 * All Rights Reserved.
9
 * ********************************************************************************** */
10
11
namespace vtlib;
12
13
/**
14
 * Provides API to update module into vtiger CRM.
15
 */
16
class PackageUpdate extends PackageImport
17
{
18
	public $_migrationinfo = false;
19
	public $listFields = [];
20
	public $listBlocks = [];
21
22
	/**
23
	 * Initialize Update.
24
	 *
25
	 * @param mixed $moduleInstance
26
	 * @param mixed $zipfile
27
	 * @param mixed $overwrite
28
	 */
29
	public function initUpdate($moduleInstance, $zipfile, $overwrite)
0 ignored issues
show
Unused Code introduced by
The parameter $overwrite is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

29
	public function initUpdate($moduleInstance, $zipfile, /** @scrutinizer ignore-unused */ $overwrite)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
30
	{
31
		$module = $this->getModuleNameFromZip($zipfile);
32
		if (!$moduleInstance || $moduleInstance->name != $module) {
33
			\App\Log::trace('Module name mismatch!', __METHOD__);
34
35
			return false;
36
		}
37
		if (null !== $module) {
38
			$zip = \App\Zip::openFile($zipfile, ['checkFiles' => false]);
39
			if ($zip->statName("$module.png")) {
40
				$zip->unzipFile("$module.png", 'layouts/' . \Vtiger_Viewer::getDefaultLayoutName() . "/skins/images/$module.png");
41
			}
42
			$zip->unzip([
43
				'updates' => 'cache/updates',
44
			]);
45
			// If data is not yet available
46
			if (empty($this->_modulexml)) {
47
				$this->__parseManifestFile($zip);
48
			}
49
		}
50
		return $module;
51
	}
52
53
	/**
54
	 * Update Module from zip file.
55
	 *
56
	 * @param Module Instance of the module to update
0 ignored issues
show
Bug introduced by
The type vtlib\Instance was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
57
	 * @param string Zip file name
0 ignored issues
show
Bug introduced by
The type vtlib\Zip was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
58
	 * @param bool True for overwriting existing module
59
	 * @param mixed $moduleInstance
60
	 * @param mixed $zipfile
61
	 * @param mixed $overwrite
62
	 */
63
	public function update($moduleInstance, $zipfile, $overwrite = true)
64
	{
65
		$module = $this->getModuleNameFromZip($zipfile);
66
		if (null !== $module) {
67
			$zip = \App\Zip::openFile($zipfile, ['checkFiles' => false]);
68
			// If data is not yet available
69
			if (empty($this->_modulexml)) {
70
				$this->__parseManifestFile($zip);
71
			}
72
			$installSequenceArray = $buildModuleArray = [];
73
			$moduleBundle = (bool) $this->_modulexml->modulebundle;
74
			if (true === $moduleBundle) {
75
				$moduleList = (array) $this->_modulexml->modulelist;
76
				foreach ($moduleList as $moduleInfos) {
77
					foreach ($moduleInfos as $moduleInfo) {
78
						$moduleInfo = (array) $moduleInfo;
79
						$buildModuleArray[] = $moduleInfo;
80
						$installSequenceArray[] = $moduleInfo['install_sequence'];
81
					}
82
				}
83
				sort($installSequenceArray);
84
				$zip->unzip($this->getTemporaryFilePath());
85
				foreach ($installSequenceArray as $sequence) {
86
					foreach ($buildModuleArray as $moduleInfo) {
87
						if ($moduleInfo['install_sequence'] == $sequence) {
88
							$moduleInstance = Module::getInstance($moduleInfo['name']);
89
							$this->update($moduleInstance, $this->getTemporaryFilePath($moduleInfo['filepath']), $overwrite);
90
						}
91
					}
92
				}
93
			} else {
94
				if (!$moduleInstance || $moduleInstance->name != $module) {
95
					\App\Log::error('Module name mismatch!', __METHOD__);
96
97
					return false;
98
				}
99
				$this->initUpdate($moduleInstance, $zipfile, $overwrite);
100
				// Call module update function
101
				$this->updateModule($moduleInstance);
102
			}
103
		}
104
	}
105
106
	/**
107
	 * Update Module.
108
	 *
109
	 * @param mixed $moduleInstance
110
	 */
111
	public function updateModule($moduleInstance)
112
	{
113
		$tablabel = $this->_modulexml->label;
114
		$tabversion = $this->_modulexml->version;
115
		Module::fireEvent($moduleInstance->name, Module::EVENT_MODULE_PREUPDATE);
116
		$moduleInstance->label = $tablabel;
117
		$moduleInstance->save();
118
119
		$this->handleMigration($this->_modulexml, $moduleInstance);
120
		$this->updateTables($this->_modulexml);
121
		$this->updateBlocks($this->_modulexml, $moduleInstance);
122
		$this->updateCustomViews($this->_modulexml, $moduleInstance);
123
		$this->updateSharingAccess($this->_modulexml, $moduleInstance);
124
		$this->updateEvents($this->_modulexml, $moduleInstance);
125
		$this->updateActions($this->_modulexml, $moduleInstance);
126
		$this->updateRelatedLists($this->_modulexml, $moduleInstance);
127
		$this->updateCustomLinks($this->_modulexml, $moduleInstance);
128
		$this->updateCronTasks($this->_modulexml);
129
		$moduleInstance->__updateVersion($tabversion);
130
131
		Module::fireEvent($moduleInstance->name, Module::EVENT_MODULE_POSTUPDATE);
132
	}
133
134
	/**
135
	 * Parse migration information from manifest.
136
	 *
137
	 * @param mixed $modulenode
138
	 */
139
	public function parseMigration($modulenode)
140
	{
141
		if (empty($this->_migrations)) {
142
			$this->_migrations = [];
0 ignored issues
show
Bug Best Practice introduced by
The property _migrations does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
143
			if (!empty($modulenode->migrations)
144
				&& !empty($modulenode->migrations->migration)) {
145
				foreach ($modulenode->migrations->migration as $migrationnode) {
146
					$migrationattrs = $migrationnode->attributes();
147
					$migrationversion = $migrationattrs['version'];
148
					$this->_migrations["$migrationversion"] = $migrationnode;
149
				}
150
			}
151
			// Sort the migration details based on version
152
			if (\count($this->_migrations) > 1) {
153
				uksort($this->_migrations, 'version_compare');
154
			}
155
		}
156
	}
157
158
	/**
159
	 * Handle migration of the module.
160
	 *
161
	 * @param mixed $modulenode
162
	 * @param mixed $moduleInstance
163
	 */
164
	public function handleMigration($modulenode, $moduleInstance)
165
	{
166
		$this->parseMigration($modulenode);
167
		$cur_version = $moduleInstance->version;
168
		foreach ($this->_migrations as $migversion => $migrationnode) {
169
			// Perform migration only for higher version than current
170
			if (version_compare($cur_version, $migversion, '<')) {
171
				\App\Log::trace("Migrating to $migversion ... STARTED", __METHOD__);
172
				if (!empty($migrationnode->tables) && !empty($migrationnode->tables->table)) {
173
					foreach ($migrationnode->tables->table as $tablenode) {
174
						$tablesql = "$tablenode->sql"; // Convert to string
175
						// Skip SQL which are destructive
176
						if (Utils::isDestructiveSql($tablesql)) {
177
							\App\Log::trace("SQL: $tablesql ... SKIPPED", __METHOD__);
178
						} else {
179
							// Supress any SQL query failures
180
							\App\Log::trace("SQL: $tablesql ... ", __METHOD__);
181
							\App\Db::getInstance()->createCommand($tablesql)->execute();
182
							\App\Log::trace('DONE', __METHOD__);
183
						}
184
					}
185
				}
186
				\App\Log::trace("Migrating to $migversion ... DONE", __METHOD__);
187
			}
188
		}
189
	}
190
191
	/**
192
	 * Update Tables of the module.
193
	 *
194
	 * @param mixed $modulenode
195
	 */
196
	public function updateTables($modulenode)
197
	{
198
		$this->importTables($modulenode);
199
	}
200
201
	/**
202
	 * Update Blocks of the module.
203
	 *
204
	 * @param mixed $modulenode
205
	 * @param mixed $moduleInstance
206
	 */
207
	public function updateBlocks($modulenode, $moduleInstance)
208
	{
209
		if (empty($modulenode->blocks) || empty($modulenode->blocks->block)) {
210
			return;
211
		}
212
213
		foreach ($modulenode->blocks->block as $blocknode) {
214
			$this->listBlocks[] = (string) ($blocknode->blocklabel);
215
			$blockInstance = Block::getInstance((string) $blocknode->blocklabel, $moduleInstance->id);
216
			if (!$blockInstance) {
217
				$blockInstance = $this->importBlock($modulenode, $moduleInstance, $blocknode);
218
			} else {
219
				$this->updateBlock($modulenode, $moduleInstance, $blocknode, $blockInstance);
220
			}
221
222
			$this->updateFields($blocknode, $blockInstance, $moduleInstance);
223
		}
224
		// Deleting removed blocks
225
		$listBlockBeforeUpdate = Block::getAllForModule($moduleInstance);
226
		foreach ($listBlockBeforeUpdate as $blockInstance) {
227
			if (!(\in_array($blockInstance->label, $this->listBlocks))) {
228
				$blockInstance->delete();
229
			}
230
		}
231
		// Deleting removed fields
232
		if ($this->listFields) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->listFields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
233
			$listFieldBeforeUpdate = Field::getAllForModule($moduleInstance);
234
			foreach ($listFieldBeforeUpdate as $fieldInstance) {
235
				if (!(\in_array($fieldInstance->name, $this->listFields))) {
236
					$fieldInstance->delete();
237
				}
238
			}
239
		}
240
	}
241
242
	/**
243
	 * Update Block of the module.
244
	 *
245
	 * @param mixed $modulenode
246
	 * @param mixed $moduleInstance
247
	 * @param mixed $blocknode
248
	 * @param mixed $blockInstance
249
	 */
250
	public function updateBlock($modulenode, $moduleInstance, $blocknode, $blockInstance)
0 ignored issues
show
Unused Code introduced by
The parameter $moduleInstance is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

250
	public function updateBlock($modulenode, /** @scrutinizer ignore-unused */ $moduleInstance, $blocknode, $blockInstance)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $modulenode is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

250
	public function updateBlock(/** @scrutinizer ignore-unused */ $modulenode, $moduleInstance, $blocknode, $blockInstance)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
251
	{
252
		$blockInstance->label = (string) ($blocknode->blocklabel);
253
		if (isset($blocknode->sequence, $blocknode->display_status)) {
254
			$blockInstance->sequence = (string) ($blocknode->sequence);
255
			$blockInstance->showtitle = (string) ($blocknode->show_title);
256
			$blockInstance->visible = (string) ($blocknode->visible);
257
			$blockInstance->increateview = (string) ($blocknode->create_view);
258
			$blockInstance->ineditview = (string) ($blocknode->edit_view);
259
			$blockInstance->indetailview = (string) ($blocknode->detail_view);
260
			$blockInstance->display_status = (string) ($blocknode->display_status);
261
			$blockInstance->iscustom = (string) ($blocknode->iscustom);
262
			$blockInstance->islist = (string) ($blocknode->islist);
263
		} else {
264
			$blockInstance->display_status = null;
265
		}
266
		$blockInstance->save();
267
268
		return $blockInstance;
269
	}
270
271
	/**
272
	 * Update Fields of the module.
273
	 *
274
	 * @param mixed $blocknode
275
	 * @param mixed $blockInstance
276
	 * @param mixed $moduleInstance
277
	 */
278
	public function updateFields($blocknode, $blockInstance, $moduleInstance)
279
	{
280
		if (empty($blocknode->fields) || empty($blocknode->fields->field)) {
281
			return;
282
		}
283
284
		foreach ($blocknode->fields->field as $fieldnode) {
285
			$this->listFields[] = (string) ($fieldnode->fieldname);
286
			$fieldInstance = Field::getInstance((string) $fieldnode->fieldname, $moduleInstance);
287
			if (!$fieldInstance) {
288
				$fieldInstance = $this->importField($blocknode, $blockInstance, $moduleInstance, $fieldnode);
289
			} else {
290
				$this->updateField($blocknode, $blockInstance, $moduleInstance, $fieldnode, $fieldInstance);
291
			}
292
			$this->__AddModuleFieldToCache($moduleInstance, $fieldInstance->name, $fieldInstance);
293
		}
294
	}
295
296
	/**
297
	 * Update Field of the module.
298
	 *
299
	 * @param mixed $blocknode
300
	 * @param mixed $blockInstance
301
	 * @param mixed $moduleInstance
302
	 * @param mixed $fieldnode
303
	 * @param mixed $fieldInstance
304
	 */
305
	public function updateField($blocknode, $blockInstance, $moduleInstance, $fieldnode, $fieldInstance)
0 ignored issues
show
Unused Code introduced by
The parameter $blocknode is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

305
	public function updateField(/** @scrutinizer ignore-unused */ $blocknode, $blockInstance, $moduleInstance, $fieldnode, $fieldInstance)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
306
	{
307
		// strval used because in $fieldnode there is a SimpleXMLElement object
308
		$fieldInstance->name = (string) ($fieldnode->fieldname);
309
		$fieldInstance->label = (string) ($fieldnode->fieldlabel);
310
		$fieldInstance->table = (string) ($fieldnode->tablename);
311
		$fieldInstance->column = (string) ($fieldnode->columnname);
312
		$fieldInstance->uitype = (string) ($fieldnode->uitype);
313
		$fieldInstance->generatedtype = (string) ($fieldnode->generatedtype);
314
		$fieldInstance->readonly = (string) ($fieldnode->readonly);
315
		$fieldInstance->presence = (string) ($fieldnode->presence);
316
		$fieldInstance->defaultvalue = (string) ($fieldnode->defaultvalue);
317
		$fieldInstance->maximumlength = (string) ($fieldnode->maximumlength);
318
		$fieldInstance->sequence = (string) ($fieldnode->sequence);
319
		$fieldInstance->quickcreate = (string) ($fieldnode->quickcreate);
320
		$fieldInstance->quicksequence = (string) ($fieldnode->quickcreatesequence);
321
		$fieldInstance->typeofdata = (string) ($fieldnode->typeofdata);
322
		$fieldInstance->displaytype = (string) ($fieldnode->displaytype);
323
		$fieldInstance->info_type = (string) ($fieldnode->info_type);
324
		$fieldInstance->fieldparams = (string) ($fieldnode->fieldparams);
325
326
		if (!empty($fieldnode->fieldparams)) {
327
			$fieldInstance->fieldparams = (string) ($fieldnode->fieldparams);
328
		}
329
330
		// Check if new parameters are defined
331
		if (isset($fieldnode->columntype)) {
332
			$fieldInstance->columntype = (string) ($fieldnode->columntype);
333
		} else {
334
			$fieldInstance->columntype = null;
335
		}
336
337
		if (!empty($fieldnode->helpinfo)) {
338
			$fieldInstance->setHelpInfo($fieldnode->helpinfo);
339
		}
340
		if (!empty($fieldnode->masseditable)) {
341
			$fieldInstance->setMassEditable($fieldnode->masseditable);
342
		}
343
		if (!empty($fieldnode->summaryfield)) {
344
			$fieldInstance->setSummaryField($fieldnode->summaryfield);
345
		}
346
347
		$fieldInstance->block = $blockInstance;
348
		$fieldInstance->save();
349
350
		// Set the field as entity identifier if marked.
351
		if (!empty($fieldnode->entityidentifier)) {
352
			if (isset($fieldnode->entityidentifier->fieldname) && !empty($fieldnode->entityidentifier->fieldname)) {
353
				$moduleInstance->entityfieldname = (string) ($fieldnode->entityidentifier->fieldname);
354
			} else {
355
				$moduleInstance->entityfieldname = $fieldInstance->name;
356
			}
357
			$moduleInstance->entityidfield = (string) ($fieldnode->entityidentifier->entityidfield);
358
			$moduleInstance->entityidcolumn = (string) ($fieldnode->entityidentifier->entityidcolumn);
359
			$moduleInstance->setEntityIdentifier($fieldInstance);
360
		}
361
362
		// Check picklist values associated with field if any.
363
		if (!empty($fieldnode->picklistvalues) && !empty($fieldnode->picklistvalues->picklistvalue)) {
364
			$picklistvalues = [];
365
			foreach ($fieldnode->picklistvalues->picklistvalue as $picklistvaluenode) {
366
				$picklistvalues[] = $picklistvaluenode;
367
			}
368
			$fieldInstance->setPicklistValues($picklistvalues);
369
		}
370
371
		// Check related modules associated with this field
372
		if (!empty($fieldnode->relatedmodules) && !empty($fieldnode->relatedmodules->relatedmodule)) {
373
			$relatedmodules = [];
374
375
			foreach ($fieldnode->relatedmodules->relatedmodule as $relatedmodulenode) {
376
				$relatedmodules[] = $relatedmodulenode;
377
			}
378
			$fieldInstance->setRelatedModules($relatedmodules);
379
		}
380
		return $fieldInstance;
381
	}
382
383
	/**
384
	 * Import Custom views of the module.
385
	 *
386
	 * @param mixed $modulenode
387
	 * @param mixed $moduleInstance
388
	 */
389
	public function updateCustomViews($modulenode, $moduleInstance)
390
	{
391
		if (empty($modulenode->customviews) || empty($modulenode->customviews->customview)) {
392
			return;
393
		}
394
		foreach ($modulenode->customviews->customview as $customviewnode) {
395
			$filterInstance = Filter::getInstance($customviewnode->viewname, $moduleInstance->id);
396
			if (!$filterInstance) {
397
				$this->importCustomView($modulenode, $moduleInstance, $customviewnode);
398
			} else {
399
				$this->updateCustomView($modulenode, $moduleInstance, $customviewnode, $filterInstance);
400
			}
401
		}
402
	}
403
404
	/**
405
	 * Update Custom View of the module.
406
	 *
407
	 * @param mixed $modulenode
408
	 * @param mixed $moduleInstance
409
	 * @param mixed $customviewnode
410
	 * @param mixed $filterInstance
411
	 */
412
	public function updateCustomView($modulenode, $moduleInstance, $customviewnode, $filterInstance)
413
	{
414
		$filterInstance->delete();
415
		$this->importCustomView($modulenode, $moduleInstance, $customviewnode);
416
	}
417
418
	/**
419
	 * Update Sharing Access of the module.
420
	 *
421
	 * @param mixed $modulenode
422
	 * @param mixed $moduleInstance
423
	 */
424
	public function updateSharingAccess($modulenode, $moduleInstance)
0 ignored issues
show
Unused Code introduced by
The parameter $moduleInstance is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

424
	public function updateSharingAccess($modulenode, /** @scrutinizer ignore-unused */ $moduleInstance)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
425
	{
426
		if (empty($modulenode->sharingaccess)) {
427
			return;
428
		}
429
	}
430
431
	/**
432
	 * Update Events of the module.
433
	 *
434
	 * @param mixed $modulenode
435
	 * @param mixed $moduleInstance
436
	 */
437
	public function updateEvents($modulenode, $moduleInstance)
438
	{
439
		if (empty($modulenode->eventHandlers) || empty($modulenode->eventHandlers->event)) {
440
			return;
441
		}
442
		$moduleId = \App\Module::getModuleId($moduleInstance->name);
443
		\App\Db::getInstance()->createCommand()->delete('vtiger_eventhandlers', ['owner_id' => $moduleId])->execute();
444
		foreach ($modulenode->eventHandlers->event as &$eventNode) {
445
			\App\EventHandler::registerHandler($eventNode->eventName, $eventNode->className, $eventNode->includeModules, $eventNode->excludeModules, $eventNode->priority, $eventNode->isActive, $moduleId);
446
		}
447
	}
448
449
	/**
450
	 * Update actions of the module.
451
	 *
452
	 * @param mixed $modulenode
453
	 * @param mixed $moduleInstance
454
	 */
455
	public function updateActions($modulenode, $moduleInstance)
456
	{
457
		if (empty($modulenode->actions) || empty($modulenode->actions->action)) {
458
			return;
459
		}
460
		foreach ($modulenode->actions->action as $actionnode) {
461
			$this->updateAction($modulenode, $moduleInstance, $actionnode);
462
		}
463
	}
464
465
	/**
466
	 * Update action of the module.
467
	 *
468
	 * @param mixed $modulenode
469
	 * @param mixed $moduleInstance
470
	 * @param mixed $actionnode
471
	 */
472
	public function updateAction($modulenode, $moduleInstance, $actionnode)
0 ignored issues
show
Unused Code introduced by
The parameter $moduleInstance is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

472
	public function updateAction($modulenode, /** @scrutinizer ignore-unused */ $moduleInstance, $actionnode)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $actionnode is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

472
	public function updateAction($modulenode, $moduleInstance, /** @scrutinizer ignore-unused */ $actionnode)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $modulenode is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

472
	public function updateAction(/** @scrutinizer ignore-unused */ $modulenode, $moduleInstance, $actionnode)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
473
	{
474
	}
475
476
	/**
477
	 * Update related lists of the module.
478
	 *
479
	 * @param mixed $modulenode
480
	 * @param mixed $moduleInstance
481
	 */
482
	public function updateRelatedLists($modulenode, $moduleInstance)
483
	{
484
		$moduleInstance->unsetAllRelatedList();
485
		if (!empty($modulenode->relatedlists) && !empty($modulenode->relatedlists->relatedlist)) {
486
			foreach ($modulenode->relatedlists->relatedlist as $relatedlistnode) {
487
				$this->updateRelatedlist($modulenode, $moduleInstance, $relatedlistnode);
488
			}
489
		}
490
		if (!empty($modulenode->inrelatedlists) && !empty($modulenode->inrelatedlists->inrelatedlist)) {
491
			foreach ($modulenode->inrelatedlists->inrelatedlist as $inRelatedListNode) {
492
				$this->updateInRelatedlist($modulenode, $moduleInstance, $inRelatedListNode);
493
			}
494
		}
495
	}
496
497
	/**
498
	 * Import related list of the module.
499
	 *
500
	 * @param mixed $modulenode
501
	 * @param mixed $moduleInstance
502
	 * @param mixed $relatedlistnode
503
	 */
504
	public function updateRelatedlist($modulenode, $moduleInstance, $relatedlistnode)
0 ignored issues
show
Unused Code introduced by
The parameter $modulenode is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

504
	public function updateRelatedlist(/** @scrutinizer ignore-unused */ $modulenode, $moduleInstance, $relatedlistnode)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
505
	{
506
		$relModuleInstance = Module::getInstance((string) $relatedlistnode->relatedmodule);
507
		$label = $relatedlistnode->label;
508
		$actions = false;
509
		if (!empty($relatedlistnode->actions) && !empty($relatedlistnode->actions->action)) {
510
			$actions = [];
511
			foreach ($relatedlistnode->actions->action as $actionnode) {
512
				$actions[] = "$actionnode";
513
			}
514
		}
515
		$fields = [];
516
		if (!empty($relatedlistnode->fields)) {
517
			foreach ($relatedlistnode->fields->field as $fieldNode) {
518
				$fields[] = "$fieldNode";
519
			}
520
		}
521
		if ($relModuleInstance) {
0 ignored issues
show
introduced by
$relModuleInstance is of type vtlib\Module, thus it always evaluated to true.
Loading history...
522
			$moduleInstance->unsetRelatedList($relModuleInstance, "$label", "$relatedlistnode->function");
523
			$moduleInstance->setRelatedList($relModuleInstance, "$label", $actions, "$relatedlistnode->function", "$relatedlistnode->field_name", $fields);
524
		}
525
		return $relModuleInstance;
526
	}
527
528
	public function updateInRelatedlist($modulenode, $moduleInstance, $inRelatedListNode)
0 ignored issues
show
Unused Code introduced by
The parameter $modulenode is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

528
	public function updateInRelatedlist(/** @scrutinizer ignore-unused */ $modulenode, $moduleInstance, $inRelatedListNode)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
529
	{
530
		$inRelModuleInstance = Module::getInstance((string) $inRelatedListNode->inrelatedmodule);
531
		$label = $inRelatedListNode->label;
532
		$actions = false;
533
		if (!empty($inRelatedListNode->actions) && !empty($inRelatedListNode->actions->action)) {
534
			$actions = [];
535
			foreach ($inRelatedListNode->actions->action as $actionnode) {
536
				$actions[] = "$actionnode";
537
			}
538
		}
539
		$fields = [];
540
		if (!empty($inRelatedListNode->fields)) {
541
			foreach ($inRelatedListNode->fields->field as $fieldNode) {
542
				$fields[] = "$fieldNode";
543
			}
544
		}
545
		if ($inRelModuleInstance) {
0 ignored issues
show
introduced by
$inRelModuleInstance is of type vtlib\Module, thus it always evaluated to true.
Loading history...
546
			$inRelModuleInstance->unsetRelatedList($moduleInstance, "$label", "$inRelatedListNode->function", $inRelatedListNode->field_name);
0 ignored issues
show
Unused Code introduced by
The call to vtlib\Module::unsetRelatedList() has too many arguments starting with $inRelatedListNode->field_name. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

546
			$inRelModuleInstance->/** @scrutinizer ignore-call */ 
547
                         unsetRelatedList($moduleInstance, "$label", "$inRelatedListNode->function", $inRelatedListNode->field_name);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
547
			$inRelModuleInstance->setRelatedList($moduleInstance, "$label", $actions, "$inRelatedListNode->function", "$inRelatedListNode->field_name", $fields);
548
		}
549
		return $inRelModuleInstance;
550
	}
551
552
	public function updateCustomLinks($modulenode, $moduleInstance)
553
	{
554
		if (empty($modulenode->customlinks) || empty($modulenode->customlinks->customlink)) {
555
			return;
556
		}
557
		Link::deleteAll($moduleInstance->id);
558
		$this->importCustomLinks($modulenode, $moduleInstance);
559
	}
560
561
	public function updateCronTasks($modulenode)
562
	{
563
		if (empty($modulenode->crons) || empty($modulenode->crons->cron)) {
564
			return;
565
		}
566
		$cronTasks = Cron::listAllInstancesByModule($modulenode->name);
567
		foreach ($modulenode->crons->cron as $importCronTask) {
568
			foreach ($cronTasks as $cronTask) {
569
				if ($cronTask->getName() == $importCronTask->name && $importCronTask->handler == $cronTask->getHandlerClass()) {
570
					Cron::deregister($importCronTask->name);
571
				}
572
			}
573
			if (empty($importCronTask->status)) {
574
				$cronTask->status = Cron::$STATUS_DISABLED;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $cronTask does not seem to be defined for all execution paths leading up to this point.
Loading history...
575
			} else {
576
				$cronTask->status = Cron::$STATUS_ENABLED;
577
			}
578
			if ((empty($importCronTask->sequence))) {
579
				$importCronTask->sequence = Cron::nextSequence();
580
			}
581
			Cron::register("$importCronTask->name", "$importCronTask->handler", "$importCronTask->frequency", "$modulenode->name", "$importCronTask->status", "$importCronTask->sequence", "$importCronTask->description");
0 ignored issues
show
Bug introduced by
$importCronTask->sequence of type string is incompatible with the type integer expected by parameter $sequence of vtlib\Cron::register(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

581
			Cron::register("$importCronTask->name", "$importCronTask->handler", "$importCronTask->frequency", "$modulenode->name", "$importCronTask->status", /** @scrutinizer ignore-type */ "$importCronTask->sequence", "$importCronTask->description");
Loading history...
Bug introduced by
$importCronTask->status of type string is incompatible with the type integer expected by parameter $status of vtlib\Cron::register(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

581
			Cron::register("$importCronTask->name", "$importCronTask->handler", "$importCronTask->frequency", "$modulenode->name", /** @scrutinizer ignore-type */ "$importCronTask->status", "$importCronTask->sequence", "$importCronTask->description");
Loading history...
Bug introduced by
$importCronTask->frequency of type string is incompatible with the type integer expected by parameter $frequency of vtlib\Cron::register(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

581
			Cron::register("$importCronTask->name", "$importCronTask->handler", /** @scrutinizer ignore-type */ "$importCronTask->frequency", "$modulenode->name", "$importCronTask->status", "$importCronTask->sequence", "$importCronTask->description");
Loading history...
582
		}
583
	}
584
}
585