Passed
Push — developer ( 6b5868...bed0f9 )
by Radosław
22:42 queued 03:39
created

Vtiger_CalendarRightPanel_Model::getGroupsList()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 23
rs 9.0111
cc 6
nc 5
nop 1
1
<?php
2
/**
3
 * Base calendar right panel file.
4
 *
5
 * @package Model
6
 *
7
 * @copyright YetiForce S.A.
8
 * @license   YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Adrian Kon <[email protected]>
10
 * @author    Mariusz Krzaczkowski <[email protected]>
11
 */
12
13
/**
14
 * Base calendar right panel class.
15
 */
16
class Vtiger_CalendarRightPanel_Model
17
{
18
	/**
19
	 * Get users.
20
	 *
21
	 * @param string $moduleName
22
	 *
23
	 * @return array
24
	 */
25
	public static function getUsersList(string $moduleName): array
26
	{
27
		$currentUser = Users_Record_Model::getCurrentUserModel();
28
		$roleInstance = Settings_Roles_Record_Model::getInstanceById($currentUser->get('roleid'));
29
30
		switch ($roleInstance->get('clendarallorecords')) {
31
			case 3:
32
				if (App\Config::performance('SEARCH_SHOW_OWNER_ONLY_IN_LIST') && !\App\Config::module($moduleName, 'DISABLED_SHOW_OWNER_ONLY_IN_LIST', false)) {
33
					$usersAndGroup = \App\Fields\Owner::getInstance($moduleName, $currentUser)->getUsersAndGroupForModuleList();
34
					$users = $usersAndGroup['users'];
35
				} else {
36
					$users = \App\Fields\Owner::getInstance(false, $currentUser)->getAccessibleUsers();
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $moduleName of App\Fields\Owner::getInstance(). ( Ignorable by Annotation )

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

36
					$users = \App\Fields\Owner::getInstance(/** @scrutinizer ignore-type */ false, $currentUser)->getAccessibleUsers();
Loading history...
37
				}
38
				break;
39
			case 1:
40
			case 2:
41
			default:
42
				$users[$currentUser->getId()] = $currentUser->getName();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$users was never initialized. Although not strictly required by PHP, it is generally a good practice to add $users = array(); before regardless.
Loading history...
43
				break;
44
		}
45
		if (!empty($users) && $favoriteUsers = self::getFavoriteUsers($moduleName)) {
46
			uksort($users,
47
				fn ($a, $b) => (int) (!isset($favoriteUsers[$a]) && isset($favoriteUsers[$b])));
48
		}
49
		return $users;
50
	}
51
52
	/**
53
	 * Get groups.
54
	 *
55
	 * @param string $moduleName
56
	 *
57
	 * @return array
58
	 */
59
	public static function getGroupsList(string $moduleName): array
60
	{
61
		$currentUser = Users_Record_Model::getCurrentUserModel();
62
		$roleInstance = Settings_Roles_Record_Model::getInstanceById($currentUser->get('roleid'));
63
		switch ($roleInstance->get('clendarallorecords')) {
64
			case 1:
65
				$groups = [];
66
				break;
67
			case 2:
68
				$groups = \App\Fields\Owner::getInstance(false, $currentUser)->getAccessibleGroups();
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $moduleName of App\Fields\Owner::getInstance(). ( Ignorable by Annotation )

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

68
				$groups = \App\Fields\Owner::getInstance(/** @scrutinizer ignore-type */ false, $currentUser)->getAccessibleGroups();
Loading history...
69
				break;
70
			case 3:
71
				if (App\Config::performance('SEARCH_SHOW_OWNER_ONLY_IN_LIST') && !\App\Config::module($moduleName, 'DISABLED_SHOW_OWNER_ONLY_IN_LIST', false)) {
72
					$usersAndGroup = \App\Fields\Owner::getInstance($moduleName, $currentUser)->getUsersAndGroupForModuleList();
73
					$groups = $usersAndGroup['group'];
74
				} else {
75
					$groups = \App\Fields\Owner::getInstance(false, $currentUser)->getAccessibleGroups();
76
				}
77
				break;
78
			default:
79
				break;
80
		}
81
		return $groups;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $groups does not seem to be defined for all execution paths leading up to this point.
Loading history...
82
	}
83
84
	/**
85
	 * Get calendar types.
86
	 *
87
	 * @param string $moduleName
88
	 *
89
	 * @return array
90
	 */
91
	public static function getCalendarTypes(string $moduleName): array
92
	{
93
		return Vtiger_Calendar_Model::getInstance($moduleName)->getCalendarTypes();
94
	}
95
96
	/**
97
	 * Return user favorite users.
98
	 *
99
	 * @param string $moduleName
100
	 *
101
	 * @return int[]
102
	 */
103
	public static function getFavoriteUsers(string $moduleName): array
104
	{
105
		$userId = \App\User::getCurrentUserId();
106
		if (\App\Cache::has('FavoriteUsers', $userId)) {
107
			$users = \App\Cache::get('FavoriteUsers', $userId);
108
		} else {
109
			$users = (new \App\Db\Query())->select(['fav_id', 'id'])
110
				->from('u_#__users_pinned')
111
				->where(['user_id' => $userId, 'tabid' => \App\Module::getModuleId($moduleName)])
112
				->createCommand()
113
				->queryAllByGroup();
114
			\App\Cache::save('FavoriteUsers', $userId, $users, \App\Cache::LONG);
115
		}
116
		return $users;
117
	}
118
}
119