Skip to content

Instantly share code, notes, and snippets.

@Krassmus
Created March 10, 2010 08:55
Show Gist options
  • Save Krassmus/327696 to your computer and use it in GitHub Desktop.
Save Krassmus/327696 to your computer and use it in GitHub Desktop.
<?php
//ganz normales Suchfeld, vermutlich ist JavaScript aktiviert.
if ($withButton) {
print "<div style=\"width: 250px; background-color: #dddddd; border: 1px #cccccc solid; " .
"-moz-border-radius: 10px 10px; -webkit-border-radius: 10px 10px; border-radius: 10px 10px\">&nbsp;";
$input_style = " style=\"width: 210px; background-color:#dddddd; border: #dddddd 0px solid\"";
}
if ($inputStyle) {
$input_style = " style=\"".$inputStyle."\"";
}
if ($beschriftung) {
$clear_input = " onFocus=\"if (this.value == '$beschriftung'){this.value = ''; $(this).style.color = '#000000'}\" " .
"onBlur=\"if (this.value == ''){this.value = '$beschriftung';$(this).style.color = '#888888'}\"";
}
?>
<input type=hidden id="<?= $name ?>_realvalue" name="<?= $name ?>" value="<?= $defaultID ?>">
<input<?= $input_style.($inputClass ? " class=\"".$inputClass."\"" : "")
?> id="<?= $name ?>"<?= ($clear_input ? $clear_input : "") ?> type=text name="<?=
$name ?>_parameter" value="<?= $defaultName ?>">
<?php
if ($withButton) {
print "<input style=\"vertical-align:middle\" type=\"image\" src=\"".$GLOBALS['ASSETS_URL']."/images/suche2.gif\">";
print "</div>";
}
?>
<div id="<?= $name ?>_choices" class="autocomplete"></div>
<script type="text/javascript" language="javascript">
//Die Autovervollständigen-Funktion aktivieren:
new Ajax.Autocompleter("<?= $name ?>", "<?= $name ?>_choices", "<?= str_replace('"', "", $_SERVER['REQUEST_URI']) ?>", {
paramName: "QuickSearch_ajaxparam[<?= $name."_".$count_QS ?>]",
afterUpdateElement : function (text, li) {
$('<?= $name ?>_realvalue').value = li.id;
},
callback: function(inputtext, querystring) {
//This has to be set, so that the hidden real input field is
//cleared when the user begins typing and not only when
//the next ajax-script is responding.
$('<?= $name ?>_realvalue').value = "";
return querystring;
},
frequency: 0.2
});
<?php
if ($beschriftung && !$defaultID) {
print '$("'.$name.'").value = "'.$beschriftung.'";' ."\n\t\t\t\t".
'$("'.$name.'").style.color = \'#888888\';'."\n";
}
print "\t\t\t\t</script>";
<?php
/**
* QuickSearch.class.php
*
* @author Rasmus Fuhse <[email protected]>, Suchi & Berg GmbH <[email protected]>
* @version $Id:$
*/
// +---------------------------------------------------------------------------+
// This file is part of Stud.IP
// QuickSearch.class.php
// Copyright (C) 2009 Rasmus Fuhse <[email protected]>
// Suchi & Berg GmbH <[email protected]>
// +---------------------------------------------------------------------------+
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or any later version.
// +---------------------------------------------------------------------------+
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
// +---------------------------------------------------------------------------+
require_once("lib/classes/Avatar.class.php");
require_once("lib/classes/CourseAvatar.class.php");
require_once("lib/classes/InstituteAvatar.class.php");
/**
* This class provides a small and intuitive GUI-element for an instant search of
* courses, persons, institutes or other items. Mainly the structure to include
* a QuickSearch-field is the following:
* //code-begin
* $sf = new QuickSearch("username");
* $sf->withButton();
* $sf->specialSearch("SELECT username, CONCAT(Vorname, \" \", Nachname) " .
* "FROM auth_user_md5 " .
* "WHERE CONCAT(Vorname, \" \", Nachname) LIKE :input " .
* "AND perms = 'dozent'", _("Dozenten suchen"));
* $sf->render();
* //code-end
* This code should be included into an html <form>-tag. It will provide a nice looking
* input-field with a button (this is the $sf->withButton() command for), a javascript
* instant-search for your items (in this case 'Dozenten') and also a non-javascript
* oldschool version of a searchfield with a select-box in step 2 of the search (first
* write what you search, click on the button and then select in the select-box what
* you wanted to have).
* You can handle the searchfield in your form-tag as if you have written an
* '<input type="text" name="username">'.
*
* For most cases you may only want to search for persons, courses or institutes.
* Thus a shortcut is implemented in this class. You may write
* //code-begin
* $sf = new QuickSearch("username", "username");
* $sf->withButton();
* $sf->render();
* //code-end
* to receive a serachfield that is automatically searching for users and inserts
* the selected users's username in the searchfield. The first parameter of the
* constructor 'new Quicksearch' is the name of the variable in your form and is
* completely free to name. The (optional) second parameter describes what you are
* searching for: username, user_id, Seminar_id or Institut_id.
*/
class QuickSearch {
static private $count_QS = 0;
private $name;
private $searchfor;
private $avatarLike;
private $withButton;
/**
* constructor which prepares a searchfield for persons, courses, institutes or
* special items you may want to search for. This is a GUI-class, see
* QuickSearch.class.php for further documentation.
* @param name: the name of the destinated variable in your html-form. Handle it
* as if it was an '<input type="text" name="yourname">' input.
* @param searchfor: if set to user_id, username, Seminar_id, Arbeitsgruppe_id or Institute_id
* the searchfield will automatically search for persons, courses, workgroups, institutes and
* you don't need to call the specialSearch-method.
*/
public function QuickSearch($name = "search", $searchfor = "") {
$this->count_QS++;
$this->name = $name;
$this->withButton = false;
$this->avatarLike = "";
$this->template_factory = new Flexi_TemplateFactory(dirname(__FILE__).'/templates/');
if (in_array($searchfor, array("username", "user_id", "Institut_id", "Seminar_id"))) {
$this->searchfor = $searchfor;
} else {
$this->searchfor = "";
}
if ($_REQUEST['QuickSearch_ajaxparam'][$name."_".$this->count_QS]) {
$this->ajax_requested = true;
} else {
$this->ajax_requested = false;
}
}
/**
* if set to true, the searchfield will be a nice-looking grey searchfield with
* a magnifier-symbol as a submit-button. Set this to false to create your own
* submit-button and style of the form.
* @param withbutton: true or false.
*/
public function withButton($withbutton = true) {
$this->withButton = ($withbutton ? true : false);
}
/**
* this will disable a submit button for the searchfield
*/
public function withoutButton() {
$this->withButton(false);
}
/**
* Here you can set a default-value for the searchfield
* @param valueID: the default-ID that should be stored
* @param valueName: the default value, that should be displayed
* - remember that these may not be the same, they may be
* something like "ae2b1fca515949e5d54fb22b8ed95575", "test_dozent"
*/
public function defaultValue($valueID, $valueName) {
$this->defaultID = $valueID;
$this->defaultName = $valueName;
}
/**
* allows to search for special items, defined by the given query
* @param query: an sql-query like "SELECT user_id, Nachname FROM auth_user_md5 WHERE LOCATE(:input, Nachname) > 0 AND perms = 'dozent'"
* The ":input" will be replaced by the search-text
* @param beschriftung: text to describe what the search is for; only displayed when JavaScript is on.
* @param avatarLike: if set to user_id, username, Seminar_id or Institute_id,
* in the Javascript-selectfield will be displayed a small avatar for that person, course or institute.
*/
public function specialSearch($query, $beschriftung = "", $avatarLike = "") {
$this->specialQuery = $query;
$this->specialBeschriftung = $beschriftung;
$this->searchfor = "special";
$this->avatarLike = $avatarLike;
}
/**
* defines a css class for the searchfield
* @param class: any css class name for the "input type=text" tag
*/
public function setInputClass($class) {
$this->inputClass = $class;
}
/**
* defines css-proporties for searchfield that will be included as 'style="$style"'
* @param style: one or more css-proporties separated with ";"
*/
public function setInputStyle($style) {
$this->inputStyle = $style;
}
/**
* last step: display everything and be happy!
* comment: the Ajax-Result (for the javascript-instant-search) will be also displayed here,
* but that does not need to concern you.
*/
public function render() {
global $_SERVER;
if (!$this->ajax_requested) {
if ($_REQUEST[$this->name.'_parameter'] && !$_REQUEST[$this->name]) {
$searchresults = $this->searchresults($_REQUEST[$this->name.'_parameter']);
$template = $this->template_factory->open('selectbox.php');
$template->set_attribute('withButton', $this->withButton);
$template->set_attribute('searchresults', $$searchresults);
$template->set_attribute('name', $this->name);
$template->set_attribute('inputClass', $this->inputClass);
print $template->render();
} else {
$template = $this->template_factory->open('inputfield.php');
$template->set_attribute('withButton', $this->withButton);
$template->set_attribute('inputStyle', $this->inputStyle ? $this->inputStyle : "");
$template->set_attribute('beschriftung', $this->beschriftung());
$template->set_attribute('name', $this->name);
$template->set_attribute('defaultID', $this->defaultID);
$template->set_attribute('defaultName', $this->defaultName);
$template->set_attribute('inputClass', $this->inputClass);
$template->set_attribute('count_QS', $this->count_QS);
print $template->render();
}
} else {
//Exakt dieses QuickSearch-Formular hat einen Ajax-Request gesendet, der beantwortet werden muss:
ob_clean();
ob_start();
$searchresults = $this->searchresults($_REQUEST['QuickSearch_ajaxparam'][$this->name."_".$this->count_QS]);
$output = "";
$output .= "<ul>";
foreach ($searchresults as $result) {
$output .= "<li id=\"".$result[0]."\">";
if ($this->searchfor == "username") {
$output .= Avatar::getAvatar(get_userid($result[0]))->getImageTag(Avatar::SMALL);
}
if ($this->searchfor == "user_id") {
$output .= Avatar::getAvatar($result[0])->getImageTag(Avatar::SMALL);
}
if (($this->searchfor == "Seminar_id") || ($this->searchfor == "Arbeitsgruppe_id")) {
$output .= CourseAvatar::getAvatar($result[0])->getImageTag(Avatar::SMALL);
}
if ($this->searchfor == "Institut_id") {
$output .= InstituteAvatar::getAvatar($result[0])->getImageTag(Avatar::SMALL);
}
if ($this->searchfor == "special") {
switch ($this->avatarLike) {
case "username":
$output .= Avatar::getAvatar(get_userid($result[0]))->getImageTag(Avatar::SMALL);
break;
case "user_id":
$output .= Avatar::getAvatar($result[0])->getImageTag(Avatar::SMALL);
break;
case "Seminar_id":
$output .= CourseAvatar::getAvatar($result[0])->getImageTag(Avatar::SMALL);
break;
case "Institut_id":
$output .= InstituteAvatar::getAvatar($result[0])->getImageTag(Avatar::SMALL);
break;
}
}
$output .= $result[1]."</li>";
}
$output .= "</ul>";
print utf8_encode($output);
exit;
}
}
//////////////////////////////////////////////////////////////////////////////
// private-methods //
//////////////////////////////////////////////////////////////////////////////
/**
* private method to get a result-array in the way of array(array(item_id, item-name)).
* @param request: the request from the searchfield typed by the user.
* @return: array(array(item_id, item-name), ...) for all results limited to 5.
*/
private function searchresults($request) {
$db = DBManager::get();
if ($this->searchfor == "username") {
$statement = $db->prepare("SELECT DISTINCT auth_user_md5.username, CONCAT(auth_user_md5.Vorname, \" \", auth_user_md5.Nachname) " .
"FROM auth_user_md5 LEFT JOIN user_info ON (user_info.user_id = auth_user_md5.user_id) " .
"WHERE CONCAT(auth_user_md5.Vorname, \" \", auth_user_md5.Nachname) LIKE :input " .
"OR auth_user_md5.username LIKE :input ORDER BY user_info.score DESC LIMIT 5", array(PDO::ATTR_CURSOR, PDO::CURSOR_FWDONLY));
$statement->execute(array(':input' => "%".$request."%"));
$result = $statement->fetchAll();
return $result;
}
if ($this->searchfor == "user_id") {
$statement = $db->prepare("SELECT DISTINCT auth_user_md5.user_id, CONCAT(auth_user_md5.Vorname, \" \", auth_user_md5.Nachname) " .
"FROM auth_user_md5 LEFT JOIN user_info ON (user_info.user_id = auth_user_md5.user_id) " .
"WHERE CONCAT(auth_user_md5.Vorname, \" \", auth_user_md5.Nachname)) LIKE :input " .
"OR auth_user_md5.username LIKE :input ORDER BY user_info.score DESC LIMIT 5", array(PDO::ATTR_CURSOR, PDO::CURSOR_FWDONLY));
$statement->execute(array(':input' => "%".$request."%"));
$result = $statement->fetchAll();
return $result;
}
if ($this->searchfor == "Institut_id") {
$statement = $db->prepare("SELECT DISTINCT Institute.Institut_id, Institute.Name " .
"FROM Institute " .
"LEFT JOIN range_tree ON (range_tree.item_id = Institute.Institut_id) " .
"WHERE Institute.Name LIKE :input " .
"OR Institute.Strasse LIKE :input " .
"OR Institute.email LIKE :input " .
"OR range_tree.name LIKE :input " .
"ORDER BY Institute.Name LIMIT 5", array(PDO::ATTR_CURSOR, PDO::CURSOR_FWDONLY));
$statement->execute(array(':input' => "%".$request."%"));
$result = $statement->fetchAll();
return $result;
}
if ($this->searchfor == "Seminar_id") {
$statement = $db->prepare("SELECT DISTINCT seminare.Seminar_id, seminare.Name " .
"FROM seminare " .
"LEFT JOIN seminar_user ON (seminare.Seminar_id = seminar_user.Seminar_id AND seminar_user.status = 'dozent') " .
"LEFT JOIN auth_user_md5 ON (seminar_user.user_id = auth_user_md5.user_id) " .
"WHERE (seminare.Name LIKE :input " .
"OR seminare.Untertitel LIKE :input " .
"OR seminare.Ort LIKE :input " .
"OR seminare.Sonstiges LIKE :input " .
"OR seminare.Beschreibung LIKE :input) " .
"AND seminare.visible = 1 " .
"AND seminare.status != '99' " .
//Suche nach Dozent hat noch nicht funktioniert
"ORDER BY seminare.Name LIMIT 5", array(PDO::ATTR_CURSOR, PDO::CURSOR_FWDONLY));
$statement->execute(array(':input' => "%".$request."%"));
$result = $statement->fetchAll();
return $result;
}
if ($this->searchfor == "Arbeitsgruppe_id") {
$statement = $db->prepare("SELECT DISTINCT seminare.Seminar_id, seminare.Name " .
"FROM seminare " .
"LEFT JOIN seminar_user ON (seminare.Seminar_id = seminar_user.Seminar_id AND seminar_user.status = 'dozent') " .
"LEFT JOIN auth_user_md5 ON (seminar_user.user_id = auth_user_md5.user_id) " .
"WHERE (seminare.Name LIKE :input " .
"OR seminare.Untertitel LIKE :input " .
"OR seminare.Ort LIKE :input " .
"OR seminare.Sonstiges LIKE :input " .
"OR seminare.Beschreibung LIKE :input) " .
"AND seminare.visible = 1 " .
"AND seminare.status = '99' " .
//Suche nach Dozent hat noch nicht funktioniert
"ORDER BY seminare.Name LIMIT 5", array(PDO::ATTR_CURSOR, PDO::CURSOR_FWDONLY));
$statement->execute(array(':input' => "%".$request."%"));
$result = $statement->fetchAll();
return $result;
}
if ($this->searchfor == "special") {
$statement = $db->prepare($this->specialQuery, array(PDO::ATTR_CURSOR, PDO::CURSOR_FWDONLY));
try {
$statement->execute(array(':input' => "%".$request."%"));
$result = $statement->fetchAll();
} catch (Exception $exception) {
return array(array("", $exception->getMessage()));
}
return $result;
}
$result = array(array("", ""));
return $result;
}
/**
* get the label of the searchfield that is written in javascript and disappears
* when the user focusses on the searchfield.
* @return: localized-string
*/
private function beschriftung() {
$beschriftung = "";
if (($this->searchfor == "username") || ($this->searchfor == "username")) {
$beschriftung = _("Nutzer suchen");
}
if ($this->searchfor == "Institut_id") {
$beschriftung = _("Einrichtung suchen");
}
if ($this->searchfor == "Seminar_id") {
$beschriftung = _("Veranstaltungen suchen");
}
if ($this->searchfor == "Arbeitsgruppe_id") {
$beschriftung = _("Arbeitsgruppe suchen");
}
if ($this->searchfor == "special") {
$beschriftung = $this->specialBeschriftung;
}
return $beschriftung;
}
}
<?php
//Kein Javascript aktiviert, also über Select-Box arbeiten. Wir sind automatisch schon in Schritt 2 der
//non-javascript-Suche.
if ($withButton) {
print "<div style=\"width: 250px; background-color: #dddddd; border: 1px #cccccc solid; " .
"-moz-border-radius: 10px 10px; -webkit-border-radius: 10px 10px; border-radius: 10px 10px\">&nbsp;";
$input_style = " style=\"width: 210px; background-color:#dddddd; border: #dddddd 0px solid\"";
}
print "<select$input_style".($inputClass ? " class=\"".$inputClass."\"" : "")." name=\"".$name."\">";
foreach ($searchresults as $result) {
print "<option value=\"".$result[0]."\">".$result[1]."</option>";
}
print "</select>";
if ($withButton) {
print "<input style=\"vertical-align:middle\" type=\"image\" src=\"".$GLOBALS['ASSETS_URL']."/images/suche2.gif\">";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment