1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* **************************************************************************** |
4
|
|
|
* references - MODULE FOR XOOPS |
5
|
|
|
* Copyright (c) Hervé Thouzard of Instant Zero (http://www.instant-zero.com) |
6
|
|
|
* |
7
|
|
|
* You may not change or alter any portion of this comment or credits |
8
|
|
|
* of supporting developers from this source code or any supporting source code |
9
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
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
|
|
|
* @copyright Hervé Thouzard of Instant Zero (http://www.instant-zero.com) |
15
|
|
|
* @license http://www.fsf.org/copyleft/gpl.html GNU public license |
16
|
|
|
* @package references |
17
|
|
|
* @author Hervé Thouzard of Instant Zero (http://www.instant-zero.com) |
18
|
|
|
* |
19
|
|
|
* **************************************************************************** |
20
|
|
|
*/ |
21
|
|
|
defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined'); |
22
|
|
|
|
23
|
|
|
require_once XOOPS_ROOT_PATH . '/kernel/object.php'; |
24
|
|
|
if (!class_exists('references_XoopsPersistableObjectHandler')) { |
25
|
|
|
require_once XOOPS_ROOT_PATH . '/modules/references/class/PersistableObjectHandler.php'; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
class references_categories extends references_Object |
29
|
|
|
{ |
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
$this->initVar('category_id', XOBJ_DTYPE_INT, null, false); |
33
|
|
|
$this->initVar('category_title', XOBJ_DTYPE_TXTBOX, null, false); |
34
|
|
|
$this->initVar('category_weight', XOBJ_DTYPE_INT, null, false); |
35
|
|
|
$this->initVar('category_description', XOBJ_DTYPE_TXTAREA, null, false); |
36
|
|
|
// Pour autoriser le html |
37
|
|
|
$this->initVar('dohtml', XOBJ_DTYPE_INT, 1, false); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Retourne la chaine de caractères qui peut être utilisée dans l'attribut href d'une balise html A. |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
public function getHrefTitle() |
46
|
|
|
{ |
47
|
|
|
return references_utils::makeHrefTitle($this->getVar('category_title')); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Retourne l'url à utiliser pour atteindre la catégorie |
52
|
|
|
* |
53
|
|
|
* @param bool $shortVersion |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function getUrl($shortVersion = false) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
if (!$shortVersion) { |
59
|
|
|
return REFERENCES_URL . 'category.php?category_id=' . $this->getVar('category_id'); |
60
|
|
|
} else { |
61
|
|
|
return 'category.php?category_id=' . $this->getVar('category_id'); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Retourne les éléments de l'annnonce formatés pour affichage |
67
|
|
|
* |
68
|
|
|
* @param string $format Format à utiliser |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
public function toArray($format = 's') |
72
|
|
|
{ |
73
|
|
|
$ret = array(); |
|
|
|
|
74
|
|
|
$ret = parent::toArray($format); |
75
|
|
|
$ret['category_href_title'] = $this->getHrefTitle(); |
76
|
|
|
$ret['category_url_rewrited'] = $this->getUrl(); |
77
|
|
|
|
78
|
|
|
return $ret; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
class ReferencesReferences_categoriesHandler extends references_XoopsPersistableObjectHandler |
83
|
|
|
{ |
84
|
|
|
public function __construct($db) |
85
|
|
|
{ // Table Classe Id Descr. |
86
|
|
|
parent::__construct($db, 'references_categories', 'references_categories', 'category_id', 'category_title'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Retourne le crit�re � utiliser pour voir les cat�gories en respectant les permissions |
91
|
|
|
* |
92
|
|
|
* @param string $permissionsType Type de permission (pour l'instant permission de voir) |
93
|
|
|
* @return obejct de type Criteria |
94
|
|
|
*/ |
95
|
|
|
public function getPermissionsCategoriesCriteria($permissionsType = REFERENCES_PERM_READ) |
96
|
|
|
{ |
97
|
|
|
global $xoopsUser; |
98
|
|
|
static $permissions = array(); |
99
|
|
|
if (references_utils::isAdmin()) { |
100
|
|
|
return new Criteria('category_id', '0', '>'); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
if (is_array($permissions) && array_key_exists($permissionsType, $permissions)) { |
103
|
|
|
return $permissions[$permissionsType]; |
104
|
|
|
} |
105
|
|
|
$categories = array(); |
|
|
|
|
106
|
|
|
$currentModule = references_utils::getModule(); |
107
|
|
|
$groups = is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS; |
108
|
|
|
$gperm_handler = xoops_getHandler('groupperm'); |
109
|
|
|
$categories = $gperm_handler->getItemIds($permissionsType, $groups, $currentModule->getVar('mid')); |
110
|
|
View Code Duplication |
if (is_array($categories) && count($categories) > 0) { |
|
|
|
|
111
|
|
|
$permissions[$permissionsType] = new Criteria('category_id', '(' . implode(',', $categories) . ')', 'IN'); |
112
|
|
|
} else { // Ne peut rien voir |
113
|
|
|
$permissions[$permissionsType] = new Criteria('category_id', '0', '='); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $permissions[$permissionsType]; |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Retourne les catégories |
121
|
|
|
* |
122
|
|
|
* @param integer $start Indice de début |
123
|
|
|
* @param integer $limit Nombre d'objets à renvoyer |
124
|
|
|
* @param string $sort Zone de tri |
125
|
|
|
* @param string $order Sens du tri |
126
|
|
|
* @return array Objets de type references_categories |
|
|
|
|
127
|
|
|
*/ |
128
|
|
View Code Duplication |
public function getCategories($start = 0, $limit = 0, $sort = 'category_weight', $order = 'ASC') |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
$criteria = new CriteriaCompo(); |
131
|
|
|
$criteria->add(new Criteria('category_id', 0, '<>')); |
132
|
|
|
$criteria->add($this->getPermissionsCategoriesCriteria()); |
133
|
|
|
$criteria->setStart($start); |
134
|
|
|
$criteria->setSort($sort); |
135
|
|
|
$criteria->setOrder($order); |
136
|
|
|
$criteria->setLimit($limit); |
137
|
|
|
|
138
|
|
|
return $this->getObjects($criteria, true); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Retourne la liste des catégories en tant que tableau |
143
|
|
|
* @param integer $start Position de départ |
144
|
|
|
* @param integer $limit Nombre maximum d'objets à retourner |
145
|
|
|
* @param string $sort ordre de tri |
146
|
|
|
* @param string $order Sens du tri |
147
|
|
|
* @return array |
|
|
|
|
148
|
|
|
*/ |
149
|
|
View Code Duplication |
public function getListArray($start = 0, $limit = 0, $sort = 'category_weight', $order = 'ASC') |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$criteria = new CriteriaCompo(); |
152
|
|
|
$criteria->add(new Criteria('category_id', 0, '<>')); |
153
|
|
|
$criteria->add($this->getPermissionsCategoriesCriteria()); |
154
|
|
|
$criteria->setStart($start); |
155
|
|
|
$criteria->setSort($sort); |
156
|
|
|
$criteria->setOrder($order); |
157
|
|
|
$criteria->setLimit($limit); |
158
|
|
|
|
159
|
|
|
return $this->getList($criteria); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Retourne le sélecteur à utiliser pour voir la liste des catégies |
164
|
|
|
* |
165
|
|
|
* @param string $selectName Le nom du sélecteur |
166
|
|
|
* @param integer $selected L'élément sélectionné |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
public function getCategoriesSelect($selectName = 'categoriesSelect', $selected = 0) |
170
|
|
|
{ |
171
|
|
|
$categories = array(); |
|
|
|
|
172
|
|
|
$ret = ''; |
173
|
|
|
$categories = $this->getListArray(0, 0, 'category_weight ASC, category_title', 'ASC'); |
174
|
|
|
if (count($categories) == 0) { |
175
|
|
|
return $ret; |
176
|
|
|
} |
177
|
|
|
$jump = REFERENCES_URL . 'category.php?category_id='; |
178
|
|
|
$extra = " onchange='location=\"" . $jump . "\"+this.options[this.selectedIndex].value'"; |
179
|
|
|
|
180
|
|
|
return references_utils::htmlSelect($selectName, $categories, $selected, true, '', false, 1, $extra); |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Notification de la création d'une nouvelle catégorie |
185
|
|
|
* |
186
|
|
|
* @param object|references_categories $category |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
|
View Code Duplication |
public function notifyNewCategory(references_categories $category) |
|
|
|
|
190
|
|
|
{ |
191
|
|
|
$plugins = references_plugins::getInstance(); |
192
|
|
|
$plugins->fireAction(references_plugins::EVENT_ON_CATEGORY_CREATE, new references_parameters(array('category' => $category))); |
193
|
|
|
|
194
|
|
|
return true; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Indique si une catégorie est visible d'un utilisateur |
199
|
|
|
* |
200
|
|
|
* @param object|references_categories $category La catégorie à controler |
201
|
|
|
* @param integer $uid L'id de l'utilisateur à controler |
202
|
|
|
* @return bool |
203
|
|
|
*/ |
204
|
|
View Code Duplication |
public function userCanSeeCategory(references_categories $category, $uid = 0) |
|
|
|
|
205
|
|
|
{ |
206
|
|
|
global $xoopsUser; |
207
|
|
|
if ($uid == 0) { |
208
|
|
|
$uid = references_utils::getCurrentUserID(); |
209
|
|
|
} |
210
|
|
|
$currentModule = references_utils::getModule(); |
211
|
|
|
$groups = is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS; |
|
|
|
|
212
|
|
|
$gperm_handler = xoops_getHandler('groupperm'); |
213
|
|
|
|
214
|
|
|
return $gperm_handler->checkRight(REFERENCES_PERM_READ, $category->category_id, references_utils::getMemberGroups($uid), $currentModule->getVar('mid')); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.