XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
search.php
Go to the documentation of this file.
1 <?php
21 include dirname(__FILE__) . DIRECTORY_SEPARATOR . 'header.php';
23 
25 $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : "search";
26 $groups = $GLOBALS['xoopsUser'] ? $GLOBALS['xoopsUser']->getGroups() : array(XOOPS_GROUP_ANONYMOUS);
28  'textbox',
29  'select',
30  'radio',
31  'yesno',
32  'date',
33  'datetime',
34  'timezone',
35  'language');
36 
37 switch ($op ) {
38  default:
39  case "search":
40  $xoopsOption['cache_group'] = implode('', $groups);
41  $xoopsOption['template_main'] = "profile_search.html";
42  include $GLOBALS['xoops']->path('header.php');
43  $xoBreadcrumbs[] = array('title' => _SEARCH);
44  $sortby_arr = array();
45 
46  // Dynamic fields
48  // Get fields
49  $fields = $profile_handler->loadFields();
50  // Get ids of fields that can be searched
51  $gperm_handler =& xoops_gethandler('groupperm');
52  $searchable_fields = $gperm_handler->getItemIds('profile_search', $groups, $GLOBALS['xoopsModule']->getVar('mid'));
53 
54  include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
55  $searchform = new XoopsThemeForm("", "searchform", "search.php", "post");
56 
57  $name_tray = new XoopsFormElementTray(_US_NICKNAME);
58  $name_tray->addElement(new XoopsFormSelectMatchOption('', 'uname_match'));
59  $name_tray->addElement(new XoopsFormText('', 'uname', 35, 255) );
60  $searchform->addElement($name_tray);
61 
63  $email_tray->addElement(new XoopsFormSelectMatchOption('', 'email_match'));
64  $email_tray->addElement(new XoopsFormText('', 'email', 35, 255));
65  $searchform->addElement($email_tray);
66 
67  // add search groups , only for Webmasters
68  if ($GLOBALS['xoopsUser'] && $GLOBALS['xoopsUser']->isAdmin()) {
69  $group_tray = new XoopsFormElementTray(_US_GROUPS);
70  $group_tray->addElement(new XoopsFormSelectGroup('', "selgroups", null, false, 5, true));
71  $searchform->addElement($group_tray);
72  }
73 
74  foreach (array_keys($fields) as $i) {
75  if (!in_array($fields[$i]->getVar('field_id'), $searchable_fields) || !in_array($fields[$i]->getVar('field_type'), $searchable_types)) {
76  continue;
77  }
78  $sortby_arr[$i] = $fields[$i]->getVar('field_title');
79  switch ($fields[$i]->getVar('field_type')) {
80  case "textbox":
81  if ($fields[$i]->getVar('field_valuetype') == XOBJ_DTYPE_INT) {
82  $searchform->addElement(new XoopsFormText(sprintf(_PROFILE_MA_LARGERTHAN, $fields[$i]->getVar('field_title') ), $fields[$i]->getVar('field_name')."_larger", 35, 35));
83  $searchform->addElement(new XoopsFormText(sprintf(_PROFILE_MA_SMALLERTHAN, $fields[$i]->getVar('field_title') ), $fields[$i]->getVar('field_name')."_smaller", 35, 35));
84  } else {
85  $tray = new XoopsFormElementTray($fields[$i]->getVar('field_title'));
86  $tray->addElement(new XoopsFormSelectMatchOption('', $fields[$i]->getVar('field_name')."_match"));
87  $tray->addElement(new XoopsFormText('', $fields[$i]->getVar('field_name'), 35, $fields[$i]->getVar('field_maxlength')));
88  $searchform->addElement($tray);
89  unset($tray);
90  }
91  break;
92 
93  case "radio":
94  case "select":
95  $options = $fields[$i]->getVar('field_options');
96  $size = MIN( count($options), 10 );
97  $element = new XoopsFormSelect($fields[$i]->getVar('field_title'), $fields[$i]->getVar('field_name'), null, $size, true);
98  asort($options);
99  $element->addOptionArray($options);
100  $searchform->addElement($element);
101  unset($element);
102  break;
103 
104  case "yesno":
105  $element = new XoopsFormSelect($fields[$i]->getVar('field_title'), $fields[$i]->getVar('field_name'), null, 2, true);
106  $element->addOption(1, _YES);
107  $element->addOption(0, _NO);
108  $searchform->addElement($element);
109  unset($element);
110  break;
111 
112  case "date":
113  case "datetime":
114  $searchform->addElement(new XoopsFormTextDateSelect(sprintf(_PROFILE_MA_LATERTHAN, $fields[$i]->getVar('field_title') ), $fields[$i]->getVar('field_name')."_larger", 15, 0));
115  $searchform->addElement(new XoopsFormTextDateSelect(sprintf(_PROFILE_MA_EARLIERTHAN, $fields[$i]->getVar('field_title') ), $fields[$i]->getVar('field_name')."_smaller", 15, time()));
116  break;
117 
118  case "timezone":
119  $element = new XoopsFormSelect($fields[$i]->getVar('field_title'), $fields[$i]->getVar('field_name'), null, 6, true);
120  include_once $GLOBALS['xoops']->path('class/xoopslists.php');
121  $element->addOptionArray(XoopsLists::getTimeZoneList());
122  $searchform->addElement($element);
123  unset($element);
124  break;
125 
126  case "language":
127  $element = new XoopsFormSelectLang($fields[$i]->getVar('field_title'), $fields[$i]->getVar('field_name'), null, 6);
128  $searchform->addElement($element);
129  unset($element);
130  break;
131  }
132  }
133  asort($sortby_arr);
134  $sortby_arr = array_merge(array("" => _NONE, "uname" =>_US_NICKNAME, "email" => _US_EMAIL), $sortby_arr);
135  $sortby_select = new XoopsFormSelect(_PROFILE_MA_SORTBY, 'sortby');
136  $sortby_select->addOptionArray($sortby_arr);
137  $searchform->addElement($sortby_select);
138 
139  $order_select = new XoopsFormRadio(_PROFILE_MA_ORDER, 'order', 0);
140  $order_select->addOption(0, _ASCENDING);
141  $order_select->addOption(1, _DESCENDING);
142  $searchform->addElement($order_select);
143 
144  $limit_text = new XoopsFormText(_PROFILE_MA_PERPAGE, 'limit', 15, 10, $limit_default);
145  $searchform->addElement($limit_text);
146  $searchform->addElement(new XoopsFormHidden('op', 'results'));
147  $searchform->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
148 
149  $searchform->assign($GLOBALS['xoopsTpl']);
150  $GLOBALS['xoopsTpl']->assign('page_title', _PROFILE_MA_SEARCH);
151 
152  //added count user
153  $member_handler =& xoops_gethandler('member');
154  $acttotal = $member_handler->getUserCount(new Criteria('level', 0, '>'));
155  $total = sprintf(_PROFILE_MA_ACTUS, "<span style='color:#ff0000;'>{$acttotal}</span>");
156  $GLOBALS['xoopsTpl']->assign('total_users', $total);
157  break;
158 
159  case "results":
160  $xoopsOption['template_main'] = "profile_results.html";
161  include_once $GLOBALS['xoops']->path('header.php');
162  $GLOBALS['xoopsTpl']->assign('page_title', _PROFILE_MA_RESULTS);
163  $xoBreadcrumbs[] = array('link' => XOOPS_URL . "/modules/" . $GLOBALS['xoopsModule']->getVar('dirname', 'n') . '/search.php', 'title' => _SEARCH);
164  $xoBreadcrumbs[] = array('title' => _PROFILE_MA_RESULTS);
165 
166  $member_handler =& xoops_gethandler('member');
167  // Dynamic fields
169  // Get fields
170  $fields = $profile_handler->loadFields();
171  // Get ids of fields that can be searched
172  $gperm_handler =& xoops_gethandler('groupperm');
173  $searchable_fields = $gperm_handler->getItemIds('profile_search', $groups, $GLOBALS['xoopsModule']->getVar('mid') );
174  $searchvars = array();
175  $search_url = array();
176 
177  $criteria = new CriteriaCompo(new Criteria('level', 0, '>'));
178 
179  if (isset($_REQUEST['uname']) && $_REQUEST['uname'] != '') {
180  $string = $myts->addSlashes(trim($_REQUEST['uname']));
181  switch ($_REQUEST['uname_match']) {
182  case XOOPS_MATCH_START:
183  $string .= "%";
184  break;
185 
186  case XOOPS_MATCH_END:
187  $string = "%" . $string;
188  break;
189 
190  case XOOPS_MATCH_CONTAIN:
191  $string = "%" . $string . "%";
192  break;
193  }
194  $criteria->add(new Criteria('uname', $string, "LIKE"));
195  $search_url[] = "uname=" . $_REQUEST['uname'];
196  $search_url[] = "uname_match=" . $_REQUEST['uname_match'];
197  $searchvars[] = "uname";
198  }
199  if (isset($_REQUEST['email']) && $_REQUEST['email'] != '') {
200  $string = $myts->addSlashes(trim($_REQUEST['email']));
201  switch ($_REQUEST['email_match']) {
202  case XOOPS_MATCH_START:
203  $string .= "%";
204  break;
205 
206  case XOOPS_MATCH_END:
207  $string = "%" . $string;
208  break;
209 
210  case XOOPS_MATCH_CONTAIN:
211  $string = "%" . $string . "%";
212  break;
213  }
214  $searchvars[] = "email";
215  $search_url[] = "email=" . $_REQUEST['email'];
216  $search_url[] = "email_match=" . $_REQUEST['email_match'];
217  $criteria->add(new Criteria('email', $string, "LIKE"));
218  $criteria->add(new Criteria('user_viewemail', 1) );
219  }
220 
221  //$search_url = array();
222  foreach (array_keys($fields) as $i ) {
223  //Radio and Select fields
224  if (!in_array($fields[$i]->getVar('field_id'), $searchable_fields) || !in_array($fields[$i]->getVar('field_type'), $searchable_types)) {
225  continue;
226  }
227  $fieldname = $fields[$i]->getVar('field_name');
228  if (in_array($fields[$i]->getVar('field_type'), array("select", "radio"))) {
229  if (empty($_REQUEST[$fieldname])) {
230  continue;
231  }
232 
233  //If field value is sent through request and is not an empty value
234  switch ($fields[$i]->getVar('field_valuetype')) {
235  case XOBJ_DTYPE_OTHER:
236  case XOBJ_DTYPE_INT:
237  $value = array_map('intval', $_REQUEST[$fieldname]);
238  $searchvars[] = $fieldname;
239  $criteria->add(new Criteria($fieldname, "(" . implode(',', $value) . ")", "IN"));
240  break;
241 
242  case XOBJ_DTYPE_URL:
243  case XOBJ_DTYPE_TXTBOX:
244  case XOBJ_DTYPE_TXTAREA:
245  $value = array_map(array($GLOBALS['xoopsDB'], "quoteString"), $_REQUEST[$fieldname]);
246  $searchvars[] = $fieldname;
247  $criteria->add(new Criteria($fieldname, "(" . implode(',', $value) . ")", "IN"));
248  break;
249  }
250  foreach ($_REQUEST[$fieldname] as $value) {
251  $search_url[] = $fieldname . "[]=" . $value;
252  }
253  } else {
254  //Other fields (not radio, not select)
255  switch ($fields[$i]->getVar('field_valuetype')) {
256  case XOBJ_DTYPE_OTHER:
257  case XOBJ_DTYPE_INT:
258  switch ($fields[$i]->getVar('field_type')) {
259  case "date":
260  case "datetime":
261  $value = $_REQUEST[$fieldname."_larger"];
262  if (!($value = strtotime($_REQUEST[$fieldname."_larger"]))) {
263  $value = intval($_REQUEST[$fieldname . "_larger"]);
264  }
265  if ($value > 0) {
266  $search_url[] = $fieldname . "_larger=" . $value;
267  $searchvars[] = $fieldname;
268  $criteria->add(new Criteria($fieldname, $value, ">="));
269  }
270 
271  $value = $_REQUEST[$fieldname . "_smaller"];
272  if (!($value = strtotime($_REQUEST[$fieldname . "_smaller"]))) {
273  $value = intval($_REQUEST[$fieldname . "_smaller"]);
274  }
275  if ($value > 0) {
276  $search_url[] = $fieldname . "_smaller=" . $value;
277  $searchvars[] = $fieldname;
278  $criteria->add(new Criteria($fieldname, $value + 24 * 3600, "<="));
279  }
280  break;
281 
282  default:
283  if (isset($_REQUEST[$fieldname . "_larger"]) && intval($_REQUEST[$fieldname . "_larger"]) != 0) {
284  $value = intval($_REQUEST[$fieldname . "_larger"]);
285  $search_url[] = $fieldname . "_larger=" . $value;
286  $searchvars[] = $fieldname;
287  $criteria->add(new Criteria($fieldname, $value, ">="));
288  }
289 
290  if (isset($_REQUEST[$fieldname . "_smaller"]) && intval($_REQUEST[$fieldname . "_smaller"]) != 0) {
291  $value = intval($_REQUEST[$fieldname . "_smaller"]);
292  $search_url[] = $fieldname . "_smaller=" . $value;
293  $searchvars[] = $fieldname;
294  $criteria->add(new Criteria($fieldname, $value, "<="));
295  }
296  break;
297  }
298 
299  if (isset($_REQUEST[$fieldname]) && !isset($_REQUEST[$fieldname . "_smaller"]) && !isset($_REQUEST[$fieldname . "_larger"])) {
300  if (!is_array($_REQUEST[$fieldname])) {
301  $value = intval($_REQUEST[$fieldname]);
302  $search_url[] = $fieldname . "=" . $value;
303  $criteria->add(new Criteria($fieldname, $value, "="));
304  } else {
305  $value = array_map("intval", $_REQUEST[$fieldname]);
306  foreach ($value as $thisvalue) {
307  $search_url[] = $fieldname . "[]=" . $thisvalue;
308  }
309  $criteria->add(new Criteria($fieldname, "(" . implode(',', $value) . ")", "IN"));
310  }
311 
312  $searchvars[] = $fieldname;
313  }
314  break;
315 
316  case XOBJ_DTYPE_URL:
317  case XOBJ_DTYPE_TXTBOX:
318  case XOBJ_DTYPE_TXTAREA:
319  if (isset($_REQUEST[$fieldname]) && $_REQUEST[$fieldname] != "") {
320  $value = $myts->addSlashes(trim($_REQUEST[$fieldname]));
321  switch ($_REQUEST[$fieldname . '_match'] ) {
322  case XOOPS_MATCH_START:
323  $value .= "%";
324  break;
325 
326  case XOOPS_MATCH_END:
327  $value = "%" . $value;
328  break;
329 
330  case XOOPS_MATCH_CONTAIN:
331  $value = "%" . $value . "%";
332  break;
333  }
334  $search_url[] = $fieldname . "=" . $_REQUEST[$fieldname];
335  $search_url[] = $fieldname . "_match=" . $_REQUEST[$fieldname . '_match'];
336  $operator = "LIKE";
337  $criteria->add(new Criteria($fieldname, $value, $operator));
338  $searchvars[] = $fieldname;
339  }
340  break;
341  }
342  }
343  }
344 
345  if ($_REQUEST['sortby'] == "name") {
346  $criteria->setSort("name");
347  } else if ($_REQUEST['sortby'] == "email") {
348  $criteria->setSort("email");
349  } else if ($_REQUEST['sortby'] == "uname") {
350  $criteria->setSort("uname");
351  } else if (isset($fields[$_REQUEST['sortby']])) {
352  $criteria->setSort($fields[$_REQUEST['sortby']]->getVar('field_name'));
353  }
354 
355  // add search groups , only for Webmasters
356  $searchgroups = array();
357  if ($GLOBALS['xoopsUser'] && $GLOBALS['xoopsUser']->isAdmin()) {
358  $searchgroups = empty($_REQUEST['selgroups']) ? array() : array_map("intval", $_REQUEST['selgroups']);
359  foreach($searchgroups as $group) {
360  $search_url[] = 'selgroups[]=' . $group;
361  }
362  }
363 
364  $order = $_REQUEST['order'] == 0 ? "ASC" : "DESC";
365  $criteria->setOrder($order);
366 
367  $limit = empty($_REQUEST['limit']) ? $limit_default : intval($_REQUEST['limit']);
368  $criteria->setLimit($limit);
369 
370  $start = isset($_REQUEST['start']) ? intval($_REQUEST['start']) : 0;
371  $criteria->setStart($start);
372 
373  list($users, $profiles, $total_users) = $profile_handler->search($criteria, $searchvars,$searchgroups);
374 
375  $total =sprintf(_PROFILE_MA_FOUNDUSER, "<span class='red'>{$total_users}</span>")." ";
376  $GLOBALS['xoopsTpl']->assign('total_users', $total);
377 
378  //Sort information
379  foreach (array_keys($users) as $k) {
380  $userarray = array();
381  $userarray["output"][] = "<a href='userinfo.php?uid=" . $users[$k]->getVar('uid') . "' title=''>" . $users[$k]->getVar('uname') . "</a>";
382  $userarray["output"][] = ( $users[$k]->getVar('user_viewemail') == 1 || (is_object($GLOBALS['xoopsUser']) && $GLOBALS['xoopsUser']->isAdmin()) ) ? $users[$k]->getVar('email') : "";
383 
384  foreach (array_keys($fields) as $i) {
385  if (in_array($fields[$i]->getVar('field_id'), $searchable_fields) && in_array($fields[$i]->getVar('field_type'), $searchable_types) && in_array($fields[$i]->getVar('field_name'), $searchvars)) {
386  $userarray["output"][] = $fields[$i]->getOutputValue($users[$k], $profiles[$k]);
387  }
388  }
389  $GLOBALS['xoopsTpl']->append('users', $userarray);
390  unset($userarray);
391  }
392 
393  //Get captions
394  $captions[] = _US_NICKNAME;
395  $captions[] = _US_EMAIL;
396  foreach (array_keys($fields) as $i) {
397  if (in_array($fields[$i]->getVar('field_id'), $searchable_fields) && in_array($fields[$i]->getVar('field_type'), $searchable_types) && in_array($fields[$i]->getVar('field_name'), $searchvars)) {
398  $captions[] = $fields[$i]->getVar('field_title');
399  }
400  }
401  $GLOBALS['xoopsTpl']->assign('captions', $captions);
402 
403  if ($total_users > $limit) {
404  $search_url[] = "op=results";
405  $search_url[] = "order=" . $order;
406  $search_url[] = "sortby=" . htmlspecialchars($_REQUEST['sortby']);
407  $search_url[] = "limit=" . $limit;
408  if (isset($search_url)) {
409  $args = implode("&amp;", $search_url);
410  }
411 
412  include_once $GLOBALS['xoops']->path('class/pagenav.php');
413  $nav = new XoopsPageNav($total_users, $limit, $start, "start", $args);
414  $GLOBALS['xoopsTpl']->assign('nav', $nav->renderNav(5));
415  }
416  break;
417 }
418 include dirname(__FILE__) . DIRECTORY_SEPARATOR . 'footer.php';
419 ?>