forked from Llewellynvdm/mod_pergroup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpergroup.php
More file actions
187 lines (174 loc) · 4.92 KB
/
Copy pathpergroup.php
File metadata and controls
187 lines (174 loc) · 4.92 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
*
* @version 1.0.0 February 06, 2014
* @package Per Group
* @author Llewellyn van der Merwe <llewellyn@vdm.io>
* @copyright Copyright (C) 2014 Vast Development Method <http://www.vdm.io>
* @license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
*
**/
defined( '_JEXEC' ) or die;
jimport('joomla.application.component.helper');
class perGroup
{
protected $userId;
protected $userGroups = false;
protected $redirect = false;
protected $document;
protected $jQuery_set;
protected $params;
protected $found = false;
protected $range_low = 0;
protected $range_high = 8;
public $results;
public $groups;
public function __construct(&$params)
{
// set params
$this->params = $params;
// check if loged in
$this->userId = JFactory::getUser()->id;
if($this->userId){
// setup user groups
$this->userGroups = JUserHelper::getUserGroups($this->userId);
} else {
$params_users = JComponentHelper::getParams('com_users');
// setup quest user groups
$this->userGroups = array($params_users->get('guest_usergroup'));
}
// check direction of execution
$up = $this->params->get('execution_direction');
if($up){
// set group range
$this->appfields = array_reverse(range($this->range_low,$this->range_high));
} else {
// set group range
$this->appfields = range($this->range_low,$this->range_high);
}
// get documnet object
$this->document = JFactory::getDocument();
// check if jQuery is loaded
$head_data = $this->document->getHeadData();
foreach (array_keys($head_data['scripts']) as $script) {
if (stristr($script, 'jquery')) {
$this->jQuery_set = true;
break;
} else {
$this->jQuery_set = false;
}
}
// set the results
$this->setResults();
}
// set data found per field
protected function setResults()
{
foreach($this->appfields as $field_id){
$this->results[$field_id] = $this->getData($field_id);
// of first redirect stop and redirect
if($this->redirect){
$app = JFactory::getApplication();
$app->redirect($this->results[$field_id]);
return true;
}
// if set so that not all content should be loaded, then break when firt data is found
if($this->found && $this->params->get('first_found_content')){
break;
}
}
return true;
}
// get data per field
protected function getData($id)
{
// check if field is active
$fieldActive = $this->params->get('group'.$id.'-active');
if($fieldActive){
// get the set groups
$fieldGroups = (array)$this->params->get('group'.$id);
// get group type
$in_group = (int)$this->params->get('group'.$id.'-type');
// set default text array
$text = array();
foreach($this->userGroups as $userGroup){
if($in_group){
// check user is in appGroup
if (in_array($userGroup, $fieldGroups)){
$result = $this->find($id);
if($this->redirect){
return $result;
}
// set result only if not a redirect
$text[] = $result;
$this->found = true;
break;
} else {
// when user is not in active group
$text[] = false;
}
} else {
// check if yous is not in the appGroup
if (!in_array($userGroup, $fieldGroups)){
$result = $this->find($id);
if($this->redirect){
return $result;
}
// set result only if not a redirect
$text[] = $result;
$this->found = true;
break;
} else {
// when user is not in active group
$text[] = false;
}
}
}
return $text;
}
// when group is not active
return false;
}
protected function find($id)
{
// check redirect
$this->redirect = $this->params->get('group'.$id.'-redirect');
if($this->redirect){
// check redirect url
$redirect_url = $this->params->get('group'.$id.'-url');
// check redirect menu
$redirect_menu = $this->params->get('group'.$id.'-menu');
if($redirect_url){
return $redirect_url;
} elseif($redirect_menu){
return 'index.php?Itemid='.$redirect_menu;
}
} else {
// get css
$group_css = $this->params->get('group'.$id.'-css');
if ($group_css){
$this->document->addStyleDeclaration($group_css);
}
// get javascript
$group_js = $this->params->get('group'.$id.'-js');
if ($group_js){
if (!$this->jQuery_set){
$this->document->addScript(JURI::base().'modules/mod_pergroup/js/jquery.js');
$this->jQuery_set = true;
}
$script = ''.$group_js.'';
$this->document->addScriptDeclaration($script);
}
// get php if set
$php = $this->params->get('group'.$id.'-php');
// get text
$html = $this->params->get('group'.$id.'-notice');
// run the php here
strlen($php) ? eval( " " . $php . " " ) : '';
if ($html){
return $html;
}
return false;
}
}
}