forked from opensourcepos/opensourcepos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule.php
More file actions
161 lines (134 loc) · 4.85 KB
/
Copy pathModule.php
File metadata and controls
161 lines (134 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
namespace App\Models;
use CodeIgniter\Database\ResultInterface;
use CodeIgniter\Model;
/**
* Module class
*/
class Module extends Model
{
protected $table = 'modules';
protected $primaryKey = 'module_id';
protected $useAutoIncrement = false;
protected $useSoftDeletes = false;
protected $allowedFields = [
'name_lang_key',
'desc_lang_key',
'sort'
];
/**
* @param string $module_id
* @return string
*/
public function get_module_name(string $module_id): string
{
$builder = $this->db->table('modules');
$query = $builder->getWhere(['module_id' => $module_id], 1);
if ($query->getNumRows() == 1) { // TODO: ===
$row = $query->getRow();
return lang($row->name_lang_key);
}
return lang('Errors.unknown');
}
/**
* @param string $module_id
* @return string
*/
public function get_module_desc(string $module_id): string // TODO: This method doesn't seem to be called in the code. Is it needed? Also, probably should change the name to get_module_description()
{
$builder = $this->db->table('modules');
$query = $builder->getWhere(['module_id' => $module_id], 1);
if ($query->getNumRows() == 1) { // TODO: ===
$row = $query->getRow();
return lang($row->desc_lang_key);
}
return lang('Errors.unknown');
}
/**
* @return ResultInterface
*/
public function get_all_permissions(): ResultInterface
{
$builder = $this->db->table('permissions');
return $builder->get();
}
/**
* @return ResultInterface
*/
public function get_all_subpermissions(): ResultInterface
{
$builder = $this->db->table('permissions');
$builder->join('modules AS modules', 'modules.module_id = permissions.module_id'); // TODO: can the table parameter just be modules instead of modules AS modules?
// Can't quote the parameters correctly when using different operators
$builder->where('modules.module_id != ', 'permission_id', false);
return $builder->get();
}
/**
* @return ResultInterface
*/
public function get_all_modules(): ResultInterface
{
$builder = $this->db->table('modules');
$builder->orderBy('sort', 'asc');
return $builder->get();
}
/**
* @param int $person_id
* @return ResultInterface
*/
public function get_allowed_home_modules(int $person_id): ResultInterface
{
$menus = ['home', 'both'];
$builder = $this->db->table('modules'); // TODO: this is duplicated with the code below... probably refactor a method and just pass through whether home/office modules are needed.
$builder->join('permissions', 'permissions.permission_id = modules.module_id');
$builder->join('grants', 'permissions.permission_id = grants.permission_id');
$builder->where('person_id', $person_id);
$builder->whereIn('menu_group', $menus);
$builder->where('sort !=', 0);
$builder->orderBy('sort', 'asc');
return $builder->get();
}
/**
* @param int $person_id
* @return ResultInterface
*/
public function get_allowed_office_modules(int $person_id): ResultInterface
{
$menus = ['office', 'both'];
$builder = $this->db->table('modules'); // TODO: Duplicated code
$builder->join('permissions', 'permissions.permission_id = modules.module_id');
$builder->join('grants', 'permissions.permission_id = grants.permission_id');
$builder->where('person_id', $person_id);
$builder->whereIn('menu_group', $menus);
$builder->where('sort !=', 0);
$builder->orderBy('sort', 'asc');
return $builder->get();
}
/**
* This method is used to set the show the office navigation icon on the home page
* which happens when the sort value is greater than zero
*/
public function set_show_office_group(bool $show_office_group): void // TODO: Should we return the value of update() as a bool for consistency?
{
if ($show_office_group) { // TODO: This should be replaced with ternary notation
$sort = 999;
} else {
$sort = 0;
}
$modules_data = ['sort' => $sort];
$builder = $this->db->table('modules');
$builder->where('module_id', 'office');
$builder->update($modules_data);
}
/**
* This method is used to show the office navigation icon on the home page
* which happens when the sort value is greater than zero
*/
public function get_show_office_group(): int
{
$builder = $this->db->table('modules');
$builder->select('sort');
$builder->where('module_id', 'office');
return $builder->get()->getRow()->sort;
}
}