1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace XoopsModules\Extcal; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* You may not change or alter any portion of this comment or credits |
7
|
|
|
* of supporting developers from this source code or any supporting source code |
8
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @copyright {@link https://xoops.org/ XOOPS Project} |
17
|
|
|
* @license {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later} |
18
|
|
|
* @package extcal |
19
|
|
|
* @since |
20
|
|
|
* @author XOOPS Development Team, |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
use XoopsModules\Extcal\{ |
24
|
|
|
Helper, |
|
|
|
|
25
|
|
|
Perm, |
|
|
|
|
26
|
|
|
Time |
|
|
|
|
27
|
|
|
}; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class CategoryHandler. |
32
|
|
|
*/ |
33
|
|
|
class CategoryHandler extends ExtcalPersistableObjectHandler |
34
|
|
|
{ |
35
|
|
|
public $extcalPerm; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param \XoopsDatabase|null $db |
39
|
|
|
*/ |
40
|
|
|
public function __construct(\XoopsDatabase $db = null) |
41
|
|
|
{ |
42
|
|
|
|
43
|
|
|
$this->extcalPerm = Perm::getHandler(); |
44
|
|
|
// parent::__construct($db, 'extcal_cat', _EXTCAL_CLN_CAT, 'cat_id'); |
45
|
|
|
parent::__construct($db, 'extcal_cat', Category::class, 'cat_id'); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param $data |
50
|
|
|
* |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
|
public function createCategory($data) |
54
|
|
|
{ |
55
|
|
|
$cat = $this->create(); |
56
|
|
|
$cat->setVars($data); |
57
|
|
|
$this->insert($cat); |
58
|
|
|
|
59
|
|
|
$catId = $this->getInsertId(); |
|
|
|
|
60
|
|
|
|
61
|
|
|
// Retriving permission mask |
62
|
|
|
/** @var \XoopsGroupPermHandler $grouppermHandler */ |
63
|
|
|
$grouppermHandler = \xoops_getHandler('groupperm'); |
64
|
|
|
$moduleId = $GLOBALS['xoopsModule']->getVar('mid'); |
65
|
|
|
|
66
|
|
|
$criteria = new \CriteriaCompo(); |
67
|
|
|
$criteria->add(new \Criteria('gperm_name', 'extcal_perm_mask')); |
68
|
|
|
$criteria->add(new \Criteria('gperm_modid', $moduleId)); |
69
|
|
|
$permMask = $grouppermHandler->getObjects($criteria); |
70
|
|
|
|
71
|
|
|
// Retriving group list |
72
|
|
|
$memberHandler = \xoops_getHandler('member'); |
73
|
|
|
$glist = $memberHandler->getGroupList(); |
|
|
|
|
74
|
|
|
|
75
|
|
|
// Applying permission mask |
76
|
|
|
foreach ($permMask as $perm) { |
77
|
|
|
if (1 == $perm->getVar('gperm_itemid')) { |
78
|
|
|
$grouppermHandler->addRight('extcal_cat_view', $cat->getVar('cat_id'), $perm->getVar('gperm_groupid'), $moduleId); |
79
|
|
|
} |
80
|
|
|
if (2 == $perm->getVar('gperm_itemid')) { |
81
|
|
|
$grouppermHandler->addRight('extcal_cat_submit', $cat->getVar('cat_id'), $perm->getVar('gperm_groupid'), $moduleId); |
82
|
|
|
} |
83
|
|
|
if (4 == $perm->getVar('gperm_itemid')) { |
84
|
|
|
$grouppermHandler->addRight('extcal_cat_autoapprove', $cat->getVar('cat_id'), $perm->getVar('gperm_groupid'), $moduleId); |
85
|
|
|
} |
86
|
|
|
if (8 == $perm->getVar('gperm_itemid')) { |
87
|
|
|
$grouppermHandler->addRight('extcal_cat_edit', $cat->getVar('cat_id'), $perm->getVar('gperm_groupid'), $moduleId); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $catId |
96
|
|
|
* @param $data |
97
|
|
|
* |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
public function modifyCat($catId, $data) |
101
|
|
|
{ |
102
|
|
|
$cat = $this->get($catId); |
103
|
|
|
$cat->setVars($data); |
104
|
|
|
|
105
|
|
|
return $this->insert($cat); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $catId |
110
|
|
|
*/ |
111
|
|
|
public function deleteCategory($catId) |
112
|
|
|
{ |
113
|
|
|
/* TODO : |
114
|
|
|
- Delete all events in this category |
115
|
|
|
*/ |
116
|
|
|
$this->deleteById($catId); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// Return one cat selected by his id |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param $catId |
123
|
|
|
* @param bool $skipPerm |
124
|
|
|
* |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
public function getCat($catId, $skipPerm = false) |
128
|
|
|
{ |
129
|
|
|
$criteriaCompo = new \CriteriaCompo(); |
130
|
|
|
$criteriaCompo->add(new \Criteria('cat_id', $catId)); |
131
|
|
|
if (!$skipPerm) { |
132
|
|
|
$this->addCatPermCriteria($criteriaCompo, $GLOBALS['xoopsUser']); |
133
|
|
|
} |
134
|
|
|
$ret = $this->getObjects($criteriaCompo); |
135
|
|
|
if (isset($ret[0])) { |
136
|
|
|
return $ret[0]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param $user |
144
|
|
|
* @param string $perm |
145
|
|
|
* |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
public function getAllCat($user, $perm = 'extcal_cat_view') |
149
|
|
|
{ |
150
|
|
|
$criteriaCompo = new \CriteriaCompo(); |
151
|
|
|
if ('all' !== $perm) { |
152
|
|
|
$this->addCatPermCriteria($criteriaCompo, $user, $perm); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $this->getObjects($criteriaCompo); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param $user |
160
|
|
|
* @param string $perm |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
public function getAllCatById($user, $perm = 'all') |
165
|
|
|
{ |
166
|
|
|
$criteriaCompo = new \CriteriaCompo(); |
167
|
|
|
if ('all' !== $perm) { |
168
|
|
|
$this->addCatPermCriteria($criteriaCompo, $user, $perm); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$t = $this->objectToArray($this->getObjects($criteriaCompo)); |
172
|
|
|
$r = []; |
173
|
|
|
// while (list($k, $v) = each($t)) { |
174
|
|
|
foreach ($t as $k => $v) { |
175
|
|
|
$r[$v['cat_id']] = $v; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $r; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param \CriteriaElement $criteria |
183
|
|
|
* @param $user |
184
|
|
|
* @param string $perm |
185
|
|
|
*/ |
186
|
|
|
public function addCatPermCriteria(\CriteriaElement $criteria, $user, $perm = 'extcal_cat_view') |
|
|
|
|
187
|
|
|
{ |
188
|
|
|
$authorizedAccessCats = $this->extcalPerm->getAuthorizedCat($user, 'extcal_cat_view'); |
189
|
|
|
$count = \count($authorizedAccessCats); |
|
|
|
|
190
|
|
|
if ($count > 0) { |
191
|
|
|
$in = '(' . $authorizedAccessCats[0]; |
192
|
|
|
\array_shift($authorizedAccessCats); |
|
|
|
|
193
|
|
|
foreach ($authorizedAccessCats as $authorizedAccessCat) { |
194
|
|
|
$in .= ',' . $authorizedAccessCat; |
195
|
|
|
} |
196
|
|
|
$in .= ')'; |
197
|
|
|
$criteria->add(new \Criteria('cat_id', $in, 'IN')); |
|
|
|
|
198
|
|
|
} else { |
199
|
|
|
$criteria->add(new \Criteria('cat_id', '(0)', 'IN')); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param \XoopsUser|string $xoopsUser |
205
|
|
|
* |
206
|
|
|
* @return bool |
207
|
|
|
*/ |
208
|
|
|
public function haveSubmitRight($xoopsUser) |
209
|
|
|
{ |
210
|
|
|
return \count($this->extcalPerm->getAuthorizedCat($xoopsUser, 'extcal_cat_submit')) > 0; |
|
|
|
|
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: