Skip to content

Commit 3f238f0

Browse files
authored
Merge pull request #13 from Tazzios/field-autorisation
3.1.0
2 parents 3636366 + cec0f5b commit 3f238f0

File tree

7 files changed

+159
-155
lines changed

7 files changed

+159
-155
lines changed

Changelog.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
v3.0.5 (not released)
2-
Fix: Escape special chars for value #11
3-
1+
v3.1.0
2+
Add: field autorisation, also works for existing fields #12
3+
Improved: removed a foreach for.
4+
Improved: Order of the rules should have less influence on the result
5+
Fix: Escape special chars for value #11
6+
Fix: Fix cast array Update helper.php #10 (@magnushasselquist)
7+
48
v3.0.4 oct 2023
59
Improve: PHP warnings when multifield options has changes #7 (@magnushasselquist)
610
Improve: PHP warnings when a list is not filtered #8 (@magnushasselquist)

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Example of presentation in front-end:
1313
![cblistfront](https://user-images.githubusercontent.com/23451105/120665837-6a21d600-c48c-11eb-9815-c243f2310b37.png)
1414

1515
Back-end configuration:
16-
![cblistbackend](https://user-images.githubusercontent.com/23451105/120667634-f84a8c00-c48d-11eb-9cd5-a8e6279bb936.png)
16+
![config](https://github.com/Tazzios/cblistmodule/assets/23451105/9f0f5639-8138-4484-9b43-bb955efdd57d)
1717

1818
## Configuration
1919
The only mandatory configuration for the module is selecting a CB list to show the users from. all other settings have default options which you can change.
@@ -27,3 +27,9 @@ The only mandatory configuration for the module is selecting a CB list to show t
2727
### rule examples;
2828
A basic set of rules will be created when creating the module.
2929
When creating custom tags make sure the tags that you are using within always have a value. For example see the avatar rule and show_avatar rule.
30+
31+
You can also show fields only to specific usergroups.
32+
For existing databace fields like cb_example you can also create a rule to set autorisation on by creatign the following rule:
33+
tag name: cb_example
34+
Usergroup: to what you want
35+
htmlcode to replace tag with: [cb_example]

cblistmodule-3.0.4.zip

8.14 KB
Binary file not shown.

config.png

27.9 KB
Loading

helper.php

Lines changed: 135 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -12,130 +12,10 @@
1212

1313
require_once( dirname(__FILE__) . '/cblisthelper.php' );
1414

15-
function checkString(array $arr, $str) {
1615

17-
$str = preg_replace( array('/[^ \w]+/', '/\s+/'), ' ', strtolower($str) ); // Remove Special Characters and extra spaces -or- convert to LowerCase
1816

19-
$matchedString = array_intersect( explode(' ', $str), $arr);
2017

21-
if ( count($matchedString) > 0 ) {
22-
return true;
23-
}
24-
return false;
25-
}
26-
27-
28-
function db_field_replace($before_str, $user_id,$rules,$fields,$search_paramtofind) {
29-
30-
//Get data from current user
31-
$db = JFactory::getDbo();
32-
$query = "select * from #__users inner join #__comprofiler on #__users.id = #__comprofiler.user_id WHERE #__users.id =".$user_id;
33-
// echo $query;
34-
$db->setQuery($query);
35-
$person = $db->loadAssoc();
36-
37-
$after_str = $before_str;
38-
39-
40-
// The while will only run multiple times if you have complex rules like using [canvas] in your avatar htmlcode.
41-
// With this while loop we are certain that all paramtofind will be replaced.
42-
$i=0;
43-
while ((str_replace($search_paramtofind, '', $after_str) !== $after_str) and $i<>5){
44-
$i++; // safety count to stop the loop if the user created one. While will run expected once or twice to replace everything.
45-
46-
foreach ($fields as $field) { //for every field that may be in the before_str
47-
$paramtofind = "[".$field['name']."]";
48-
$fieldtouse = $field['name'];
49-
$fieldtype = $field['type'];
5018

51-
$datatoinsert = null;
52-
//check if the fieldtouse exist (or is null)
53-
if (isset($person[$fieldtouse]) ) {
54-
$datatoinsert = $person[$fieldtouse];
55-
}
56-
57-
58-
59-
// if it is an image check the approved and create full url
60-
$show = 'yes';
61-
if ($fieldtype=='image') {
62-
63-
if ( $person[$fieldtouse.'approved']==0 or (empty($datatoinsert)) ) {
64-
$show = 'no';
65-
} else {
66-
//url to the default canvas images are incorrect in stored in the database
67-
if ($fieldtouse=='canvas') {
68-
$datatoinsert = str_ireplace('Gallery/', 'gallery/canvas/', $datatoinsert);
69-
}
70-
//create the full image path
71-
$datatoinsert = JURI::base(). "images/comprofiler/" .$datatoinsert;
72-
73-
}
74-
}
75-
76-
//Fieldtypes with a label name in the comprofiler_field_values
77-
// normal checkbox currently returns a 0 or 1
78-
if ( !empty($datatoinsert) and ($fieldtype=='multicheckbox' or $fieldtype=='multiselect' or $fieldtype=='select' or $fieldtype=='radio')) {
79-
80-
81-
$values= explode("|*|", $datatoinsert);
82-
// clear unexploded data from data to insert
83-
$datatoinsert= '';
84-
85-
86-
87-
foreach ($values as $value) {
88-
89-
//Get label from value
90-
$dblabel = JFactory::getDbo();
91-
$query = "select fieldlabel from #__comprofiler_field_values WHERE fieldtitle ='". addslashes($value) . "'";
92-
$dblabel->setQuery($query);
93-
$labels = (array) $dblabel->loadAssoc();
94-
95-
if (is_iterable($labels)) {
96-
foreach ($labels as $label) {
97-
if(empty($label)) {
98-
$datatoinsert .= $value. " " ;
99-
}
100-
else{
101-
$datatoinsert .= $label. " " ;
102-
}
103-
}
104-
}
105-
else {
106-
print("Can't iterate array\n");
107-
}
108-
}
109-
}
110-
111-
112-
//check if there is a rule for this field
113-
if (null !==(array_search($fieldtouse,array_column($rules,'tag_name'))) ) {
114-
115-
//loop through the rules to find the rule
116-
foreach ($rules as $rule) {
117-
118-
// If the rule is found:
119-
if (strtolower($rule['tag_name']) == $fieldtouse) {
120-
121-
// check if show still true and data is not empty or that it is a custom tag created in the module.
122-
if ($show == 'yes' and ((!empty($datatoinsert)) or $fieldtype=='custom') ) {
123-
$datatoinsert = str_ireplace($paramtofind, ($datatoinsert ?? ''), $rule['htmlcode']);
124-
} else {
125-
$datatoinsert = str_ireplace($paramtofind, ($datatoinsert ?? ''), $rule['htmlcode_no']);
126-
}
127-
}
128-
}
129-
}
130-
131-
$after_str = str_ireplace($paramtofind, ($datatoinsert ?? ''), $after_str); // replace the param name with '' if not found.
132-
133-
} // end for each fields
134-
}// end while
135-
136-
return $after_str;
137-
138-
}
13919

14020
class modcbListHelper
14121
{
@@ -145,17 +25,11 @@ class modcbListHelper
14525
* @param array $params An object containing the module parameters
14626
* @access public
14727
*/
148-
149-
150-
151-
15228

15329

15430
public static function getData( $params )
15531
{
15632

157-
158-
15933
//retrieve $rules
16034
$subform = $params->get('rules');
16135
$arr = (array) $subform;
@@ -165,36 +39,31 @@ public static function getData( $params )
16539
$additional_names = '';
16640
foreach ($arr as $value)
16741
{
42+
16843
$rules[$i]['tag_name']= strtolower($value->tag_name);
44+
$rules[$i]['accesslevel']= $value->accesslevel;
16945
$rules[$i]['htmlcode'] = $value->htmlcode;
17046
$rules[$i]['htmlcode_no'] = $value->htmlcode_no;
17147

172-
$additional_names .= " UNION SELECT '". strtolower($value->tag_name). "' AS name, 'custom' as type ";
48+
$additional_names .= " UNION SELECT '". strtolower($value->tag_name). "' AS name, 'rule' as type ";
17349

174-
$i++;
50+
$i++;
17551
}
176-
52+
17753

17854
// get all the fields that could possibly be part of template to be replaced to get us something to loop through. Also add id and user_id as fields.
17955
$db = JFactory::getDbo();
180-
$query = "SELECT name, type FROM #__comprofiler_fields WHERE (#__comprofiler_fields.table = '#__users' OR #__comprofiler_fields.table = '#__comprofiler') and name not in ('password','params')
181-
UNION SELECT 'id' AS name, '' as type
182-
UNION SELECT 'user_id' AS name, '' as type ";
56+
$query = "SELECT fields.name, fields.type FROM #__comprofiler_fields as fields
57+
WHERE (fields.table = '#__users' OR fields.table = '#__comprofiler') and name not in ('password','params')
58+
UNION SELECT 'id' AS name, 'id' as type
59+
UNION SELECT 'user_id' AS name, 'id' as type ";
18360
// add additional names created in the parameters
18461
$query .= $additional_names ;
18562
// retrieve fields from type images as first. this way other tags in the htmlcode then from the image will also be replaced without additional while loop
186-
$query .= " order by FIELD(type,'image') desc";
63+
$query .= " order by FIELD(type,'image' ) desc";
18764
$db->setQuery($query);
18865
$fields = $db->loadAssocList();
189-
190-
// create an one row array with paramtofind to use for the while check
191-
$search_paramtofind = array ();
192-
foreach ($fields as $field) {
193-
$search_paramtofind[] = "[".$field['name']."]";
194-
}
19566

196-
197-
19867

19968
$result=''; //reset result
20069
// Get the parameters
@@ -259,17 +128,15 @@ public static function getData( $params )
259128
$result .= " <div style=\" margin: 0 auto; display: grid; grid-gap: 0.2rem;grid-template-columns: repeat(". $columns .", minmax(".$minwidth."rem, 1fr));\" class=\"cblist\"> " ;
260129

261130
// Now, lets use the final SQL to get all Users from Joomla/CB
262-
$query = $fetch_sql;
263131

264132
$debug_text= '';
265-
if ($list_debug == 1) { $debug_text .= "<p>DEBUG: <pre>".$query."</pre></p>"; }
266-
$db->setQuery($query);
133+
if ($list_debug == 1) { $debug_text .= "<p>DEBUG: <pre>".$fetch_sql."</pre></p>"; }
134+
$db->setQuery($fetch_sql);
267135
$persons = $db->loadAssocList();
268136
if (!empty($persons)){
269-
foreach ($persons as $person) { //for every person that is a reciever, lets do an email.
270-
// $result .= $person['username']."<br/>";
137+
foreach ($persons as $person) {
271138
// Lets loop over the Users and create the output using the Template, replacing [fileds] in Template
272-
$result .= "<div style=\"padding: 5px;overflow-wrap: break-word;\" class=\"cblist-user\" >". db_field_replace($list_template, $person['id'],$rules,$fields,$search_paramtofind) ."</div >" ;
139+
$result .= "<div style=\"padding: 5px;overflow-wrap: break-word;\" class=\"cblist-user\" >". db_field_replace($list_template, $person['id'],$rules,$fields) ."</div >" ;
273140
}
274141
} else if ($list_debug == 1) { $debug_text .= "<p>DEBUG: Empty list?!</p>"; }
275142

@@ -279,5 +146,126 @@ public static function getData( $params )
279146
return $list_textabove . $debug_text . $result . $list_textbelow;
280147

281148
}
149+
150+
282151
}
152+
153+
function db_field_replace($before_str, $user_id,$rules,$fields) {
154+
155+
//Get data from current user
156+
$db = JFactory::getDbo();
157+
$query = "select * from #__users inner join #__comprofiler on #__users.id = #__comprofiler.user_id WHERE #__users.id =".$user_id;
158+
// echo $query;
159+
$db->setQuery($query);
160+
$person = $db->loadAssoc();
161+
162+
$after_str = $before_str;
163+
164+
165+
// The while will only run multiple times if you have complex rules like using [canvas] in your avatar htmlcode.
166+
// With this while loop we are certain that all paramtofind will be replaced.
167+
$i=0;
168+
while (/*(str_replace($search_paramtofind, '', $after_str) !== $after_str) and*/ $i<>4){
169+
$i++; // safety count to stop the loop if the user created one. While will run expected once or twice to replace everything.
170+
171+
172+
foreach ($fields as $field) { //for every field that may be in the before_str
173+
$paramtofind = "[".$field['name']."]";
174+
$fieldtouse = $field['name'];
175+
$fieldtype = $field['type'];
176+
177+
178+
/*set value to insert for normal fields*/
179+
$datatoinsert = null;
180+
181+
//check if the fieldtouse exist (or is null)
182+
if (isset($person[$fieldtouse]) ) {
183+
$datatoinsert = $person[$fieldtouse];
184+
}
185+
186+
187+
/*set value to insert for images */
188+
// if it is an image check the approved and create full url
189+
//if there is an '[fieldname]approved' column it is an image. By checking the exsting of the column instead of type 'image' it will also be aplied to rules with the same name.
190+
$show = true;
191+
if (isset($person[$fieldtouse.'approved']) ) {
192+
193+
if ( $person[$fieldtouse.'approved']==0 or (empty($datatoinsert)) ) {
194+
$datatoinsert = 'no image available';
195+
} else {
196+
//url to the default canvas images are incorrect in stored in the database
197+
if ($fieldtouse=='canvas') {
198+
$datatoinsert = str_ireplace('Gallery/', 'gallery/canvas/', $datatoinsert);
199+
}
200+
//create the full image path
201+
$datatoinsert = JURI::base(). "images/comprofiler/" .$datatoinsert;
202+
}
203+
}
204+
205+
206+
/*set value to insert for multiple value fields */
207+
//Fieldtypes with a label name in the comprofiler_field_values
208+
// TODO normal checkbox currently returns a 0 or 1
209+
if ( !empty($datatoinsert) and ($fieldtype=='multicheckbox' or $fieldtype=='multiselect' or $fieldtype=='select' or $fieldtype=='radio')) {
210+
211+
$values= explode("|*|", $datatoinsert);
212+
// clear unexploded data from data to insert
213+
$datatoinsert= '';
214+
215+
foreach ($values as $value) {
216+
//Get label from value
217+
$dblabel = JFactory::getDbo();
218+
$query = "select fieldlabel from #__comprofiler_field_values WHERE fieldtitle ='". addslashes($value) . "'";
219+
$dblabel->setQuery($query);
220+
$labels = (array) $dblabel->loadAssoc();
221+
222+
if (is_iterable($labels)) {
223+
foreach ($labels as $label) {
224+
if(empty($label)) {
225+
$datatoinsert .= $value. " " ;
226+
} else {
227+
$datatoinsert .= $label. " " ;
228+
}
229+
}
230+
} else {
231+
print("Can't iterate array\n");
232+
}
233+
}
234+
}
235+
236+
// Check if there is an rule. A rule can have the same name as an CB field.
237+
//array_search will return false of an array ID.
238+
$rule_id = array_search($fieldtouse,array_column($rules,'tag_name'));
239+
if ( $rule_id !== false) {
240+
241+
// get usergroups from loggedin user
242+
$user_accesslevels = JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id'), $recursive = true);
243+
244+
$autorised = false;
245+
if ( empty($rules[$rule_id]['accesslevel']) or array_sum(array_count_values(array_intersect($user_accesslevels, $rules[$rule_id]['accesslevel'])))>0 ) { // if not set show the data
246+
$autorised = true;
247+
}
248+
249+
if ($autorised == true) {
250+
// check if (data is not empty or that it is a rule tag created) and incase of an image tag if there is an image to show.
251+
if ( ( !empty($datatoinsert) or $fieldtype=='rule' ) and $datatoinsert != 'no image available') {
252+
$datatoinsert = str_ireplace($paramtofind, ($datatoinsert ), $rules[$rule_id]['htmlcode']);
253+
} else {
254+
$datatoinsert = str_ireplace($paramtofind, ($datatoinsert ?? ''), $rules[$rule_id]['htmlcode_no'] );
255+
}
256+
} else {
257+
//Set to empty when not autorised
258+
$datatoinsert= null;
259+
}
260+
}
261+
262+
$after_str = str_ireplace($paramtofind, ($datatoinsert ?? ''), $after_str); // replace the param name with '' if not found.
263+
264+
265+
} // end for each fields
266+
}// end while
267+
268+
return $after_str;
269+
270+
}
283271
?>

0 commit comments

Comments
 (0)