-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcblisthelper.php
145 lines (114 loc) · 4.56 KB
/
cblisthelper.php
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
<?php
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
/* this file will build the query to get the profiles in the same way as the cblist.
* By seperating it it can be easier used in other applications.
* @copyright 2022
* @author Tazzios
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
function createcblistquerymod($cblistid,$cblistname) {
$where = '' ;
if (!empty($cblistid)) {
$where = 'listid = '. $cblistid ;
}
else {
$where = 'title = \''. $cblistname . '\'';
}
// Obtain a database connection
$db = Factory::getDbo();
// Retrieve the selected list
$query = $db->getQuery(true)
->select('params')
->select('usergroupids')
->from('#__comprofiler_lists')
->where($where . ' AND published=1');
//->order('ordering ASC');
$db->setQuery($query);
// Load the List row.
$row = $db->loadAssoc();
$select_sql_raw = $row['params'];
$select_sql =""; //declare variable
// Process the filterfields to make it usefull for SQL query
$json_a=json_decode($select_sql_raw,true);
if (isset($json_a['filter_basic'])) $filters_basic = $json_a['filter_basic'];
if ($json_a['filter_mode'] == 0) {
$i = 0;
foreach ($filters_basic as $filter) {
if ($filter['column']<>'') {
// If it is not the first filter add AND
if ($i>0) {
$select_sql .= " AND " ;
}
// add qoutes if value is text.
if (!is_numeric($filter['value'])) {
$value = "'".$filter['value']."'";
} else {
$value = $filter['value'];
}
// Replace operators from json if needed else default
switch ($filter['operator']) {
case "<>||ISNULL": // CB Not equal to
$select_sql .= "(".$filter['column'] . "<> ".$value ." OR ". $filter['column'] . " IS NULL)";
break;
case "NOT REGEXP||ISNULL": // CB is not regexp
$select_sql .= "(".$filter['column'] . " NOT REGEXP ".$value ." OR ". $filter['column'] . " IS NULL)";
break;
case "NOT LIKE||ISNULL"; //CB Does not contain
$value = "'%" . trim($value,'\'"') . "%'"; // any combination of ' and "
$select_sql .= "(".$filter['column'] . " NOT LIKE ".$value ." OR ". $filter['column'] . " IS NULL)";
break;
case "LIKE"; //CB Does contain
$value = "'%" . trim($value,'\'"') . "%'"; // any combination of ' and "
$select_sql = "(".$filter['column'] . " LIKE " . $value . ")" ;
break;
case "IN"; //CB IN
$i = 0;
$include = "";
//loop al the values from the in filter value. Fetch original value so no aurrounding qoutes are present
foreach ((explode(",",$filter['value'])) as $value) {
// Start with separator is not first one.
if ($i>0) {
$include .= ", " ;
}
// place quotes if text
if (!is_numeric($value)) {
$value = "'".$filter['value']."'";
}
$include .= "".$value."";
$i++;
}
$select_sql .= "".$filter['column'] . " IN (". $include .") ";
break;
default:
// Default way to process json values to query
$select_sql .= "(".$filter['column']." ".$filter['operator']." ".$value.")";
break;
}
$i++;
}
}
}
else if ($json_a['filter_mode'] == 1) {
$select_sql = $json_a['filter_advanced'];
}
// Set a base-sql for connecting users, fields and lists
$usergroupids = str_replace("|*|", ",", $row['usergroupids']); //CMJ ADDED
$usergroupids = trim($usergroupids,','); // prevent that the range starts (or ends) with a comma if you also have selected '--- Select User group (CTR/CMD-Click: multiple)---' at the usergroups
$list_show_unapproved = $json_a['list_show_unapproved'];
$list_show_blocked = $json_a['list_show_blocked'];
$list_show_unconfirmed = $json_a['list_show_unconfirmed'];
$fetch_sql = "SELECT DISTINCT ue.id FROM #__users u JOIN #__user_usergroup_map g ON g.`user_id` = u.`id` JOIN #__comprofiler ue ON ue.`id` = u.`id` WHERE g.group_id IN (".$usergroupids.")";
if ($list_show_blocked == 0) {$fetch_sql.=" AND u.block = 0 ";}
if ($list_show_unapproved == 0) {$fetch_sql.=" AND ue.approved = 1 ";}
if ($list_show_unconfirmed == 0) {$fetch_sql.=" AND ue.confirmed = 1 ";}
// add CB list filters only if there are any
if ($select_sql <>'') $fetch_sql = $fetch_sql . " AND (" . $select_sql . ")";
$cblistquery['cblistselect'] = $fetch_sql;
//Add ordering if list is configured for that
// order will be given seperates so it can be overwritten
$cblistquery['cblistsortby'] = $json_a['sort_basic'][0]['column'];
$cblistquery['cblistsortorder'] = $json_a['sort_basic'][0]['direction'];
return $cblistquery;
}
?>