Location: PHPKode > projects > GINO CMS > gino-1.4.0/app/searchSite/class_searchSite.php
<?php
/**
 * @file class_searchSite.php
 * @brief Contiene la classe searchSite
 * 
 * @copyright 2005 Otto srl (http://www.opensource.org/licenses/mit-license.php) The MIT License
 * @author marco guidotti hide@address.com
 * @author abidibo hide@address.com
 */

// Include la libreria per le ricerche full text
require_once(CLASSES_DIR.OS."class.search.php");

/**
 * @brief Gestisce le ricerche full text sui contenuti dell'applicazione
 * 
 * @copyright 2005 Otto srl (http://www.opensource.org/licenses/mit-license.php) The MIT License
 * @author marco guidotti hide@address.com
 * @author abidibo hide@address.com
 */
class searchSite extends AbstractEvtClass {

	private $_optionsValue;
	private $_options;
	public $_optionsLabels;
	
	private $_template, $_sys_mdl, $_inst_mdl;
	private $_title;
	private $_action, $_block;

	function __construct() {
	
		parent::__construct();

		$this->_instance = 0;
		$this->_instanceName = $this->_className;

		$this->setAccess();

		$this->_template = htmlChars($this->setOption('template', true));
		$this->_sys_mdl = $this->setOption('sys_mdl') ? $this->setOption('sys_mdl') : '';
		$this->_inst_mdl = $this->setOption('inst_mdl') ? $this->setOption('inst_mdl') : '';

		// Valori di default
		$this->_optionsValue = array(
		
		);
		
		$this->_options = new options($this->_className, $this->_instance);
		$this->_optionsLabels = array(
			"template"=>array("label"=>array(_("Template"), _("{FIELD}: campo di ricerca<br />{BUTTON}: pulsante di ricerca<br />{CHECK}: selezione moduli da ricercare")), "required"=>false),
			"sys_mdl"=>array("label"=>array(_("Moduli di sistema"), _("Inserire gli ID dei moduli che si vogliono includere nella ricerca separati da virgole")), "required"=>false),
			"inst_mdl"=>array("label"=>array(_("Moduli istanziabili"), _("Inserire gli ID dei moduli che si vogliono includere nella ricerca separati da virgole")), "required"=>false)
		);
		
		$this->_title = _("Ricerca nel sito");

		$this->_action = cleanVar($_REQUEST, 'action', 'string', '');
		$this->_block = cleanVar($_REQUEST, 'block', 'string', '');
	}

	/**
	 * Elenco dei metodi che possono essere richiamati dal menu e dal template
	 * 
	 * @return array
	 */
	public static function outputFunctions() {

		$list = array(
			"form" => array("label"=>_("Visualizza il form di ricerca"), "role"=>'1')
		);

		return $list;
	}

	/**
	 * Interfaccia amministrativa per la gestione delle ricerche
	 * 
	 * @return string
	 */
	public function manageSearchSite() {
	
		$this->accessGroup('ALL');
		
		$htmltab = new htmlTab(array("linkPosition"=>'right', "title"=>$this->_title));	
		$link_admin = "<a href=\"".$this->_home."?evt[$this->_className-manageSearchSite]&block=permissions\">"._("Permessi")."</a>";
		$link_options = "<a href=\"".$this->_home."?evt[$this->_className-manageSearchSite]&block=options\">"._("Opzioni")."</a>";
		$link_dft = "<a href=\"".$this->_home."?evt[".$this->_className."-manageSearchSite]\">"._("Gestione")."</a>";
		$sel_link = $link_dft;

		if($this->_block == 'options') {
			$GINO = sysfunc::manageOptions($this->_instance, $this->_className);		
			$sel_link = $link_options;
		}
		elseif($this->_block == 'permissions') {
			$GINO = sysfunc::managePermissions($this->_instance, $this->_className);		
			$sel_link = $link_admin;
		}
		else {
			$GINO = $this->info();
		}
		
		$htmltab->navigationLinks = array($link_admin, $link_options, $link_dft);
		$htmltab->selectedLink = $sel_link;
		$htmltab->htmlContent = $GINO;
		return $htmltab->render();
	}

	/**
	 * Form di ricerca
	 * 
	 * @return string
	 */
	public function form() {

		$htmlsection = new htmlSection(array('id'=>"searchSite_form",'class'=>'public', 'headerTag'=>'h1', 'headerLabel'=>_("Ricerca nel sito")));
	
		$gform = new Form('search_site_form', 'post', true, array('tblLayout'=>false));
		$gform->load('dataform');

		$registry = registry::instance();
		$registry->addCss($this->_class_www."/searchSite.css");
		$registry->addJs($this->_class_www."/searchSite.js");
		
		$required = '';
		$buffer = $gform->form($this->_home."?evt[".$this->_className."-results]", '', $required);
		$field = "<input type=\"text\" name=\"search_site\" id=\"search_site\"/>";
		$button = "<input type=\"submit\" id=\"search_site_submit\" value=\" \" />";
		
		$check = ($this->_sys_mdl || $this->_inst_mdl) ? "<input type=\"button\" id=\"search_site_check\" value=\" \" />" : '';
		if($this->_template) {
			$tpl = preg_replace("#{FIELD}#", $field, $this->_template);	
			$tpl = preg_replace("#{BUTTON}#", $button, $tpl);	
			$buffer .= preg_replace("#{CHECK}#", $check, $tpl);	
		}
		else {
			$buffer .= $check." ".$field." ".$button;
		}

		$buffer .= $this->checkOptions();
		$buffer .= $gform->cform();

    $htmlsection->content = $buffer;

		return $htmlsection->render();
	}

	private function checkOptions() {
	
		$buffer = "<div id=\"search_site_check_options\" style=\"display:none; position:absolute;text-align:left;\">";
		$buffer .= "<div>";
		$buffer .= "<p><b>"._("Ricerca solo in")."</b></p>";

		$i=1;
		if($this->_sys_mdl)
		{
		foreach(explode(",", $this->_sys_mdl) as $smid) {
			$label = $this->_db->getFieldFromId($this->_tbl_module_app, 'label', 'id', $smid);
			$buffer .= "<input type=\"checkbox\" name=\"sysmdl[]\" value=\"$smid\"> ".htmlChars($label);
			if($i++%3==0) $buffer .= "<br />";
		}
		}
		if($this->_inst_mdl)
		{
		foreach(explode(",", $this->_inst_mdl) as $mid) {
			$label = $this->_db->getFieldFromId($this->_tbl_module, 'label', 'id', $mid);
			$buffer .= "<input type=\"checkbox\" name=\"instmdl[]\" value=\"$mid\"> ".htmlChars($label);
			if($i++%3==0) $buffer .= "<br />";
		}
		}
		$buffer .= "</div>";
		$buffer .= "</div>";

		return $buffer;
	}

	/**
	 * Stampa i risultati di una ricerca
	 * 
	 * La ricerca viene effettuata sui moduli nei quali sono stati definiti i metodi @a searchSite() e @a searchSiteResult()
	 * 
	 * @see search::getSearchResults()
	 * @return string
	 */
	public function results() {

		$keywords = cleanVar($_POST, 'search_site', 'string', '');
		$keywords = cutHtmlText($keywords, 500, '', true, false, true);
		$sysmdl = cleanVar($_POST, 'sysmdl', 'array', '');
		$instmdl = cleanVar($_POST, 'instmdl', 'array', '');

		$opt = (!count($sysmdl) && !count($instmdl)) ? false : true;
		$results = array();
		$buffer = '';

		foreach(explode(",", $this->_sys_mdl) as $smdlid) {
			if(!$opt || in_array($smdlid, $sysmdl)) {
				$classname = $this->_db->getFieldFromId($this->_tbl_module_app, 'name', 'id', $smdlid);
				if(method_exists($classname, "searchSite")) {
					$obj = new $classname();
					$data = $obj->searchSite();
					$searchObj = new search($data['table']);
					foreach($data['weight_clauses'] as $k=>$v) $data['weight_clauses'][$k]['value'] = $keywords;
					$results[$classname] = $searchObj->getSearchResults(db::instance(), $data['selected_fields'], $data['required_clauses'], $data['weight_clauses']);
				}
			}
		}
		foreach(explode(",", $this->_inst_mdl) as $mdlid) {
			if(!$opt || in_array($mdlid, $instmdl)) {
				$instancename = $this->_db->getFieldFromId($this->_tbl_module, 'name', 'id', $mdlid);
				$classname = $this->_db->getFieldFromId($this->_tbl_module, 'class', 'id', $mdlid);
				if(method_exists($classname, "searchSite")) {
					$obj = new $classname($mdlid);
					$data = $obj->searchSite();
					$searchObj = new search($data['table']);
					foreach($data['weight_clauses'] as $k=>$v) $data['weight_clauses'][$k]['value'] = $keywords;
					$results[$classname."||".$mdlid] = $searchObj->getSearchResults(db::instance(), $data['selected_fields'], $data['required_clauses'], $data['weight_clauses']);
				}
			}
		}

		$order_results = array();
		$final_results = array();
		
		if(count($results) > 0)
		{
			$i = 0;
			foreach($results as $classname=>$res) {
				foreach($res as $k=>$v) {
					$order_results[$i] = $v['relevance']*1000 + round($v['occurrences']);
					$final_results[$i] = array_merge(array("class"=>$classname), $v);	
					$i++;
				}	
			}

			arsort($order_results);
		}
		$tot_results = count($final_results);

		$title = _("Ricerca")." \"$keywords\"";
		$name_result = $tot_results == 1 ? _("risultato") : _("risultati");
		$right_title = $tot_results." ".$name_result;
		$htmlsection = new htmlSection(array('id'=>"searchSite",'class'=>'public', 'headerTag'=>'header', 'headerLabel'=>$title, 'headerLinks'=>$right_title));

		if($tot_results) {
			$htmlList = new htmlList(array("numItems"=>sizeof($final_results), "separator"=>true));

			$buffer .= $htmlList->start();
			foreach($order_results as $k=>$point) {
				$fr = $final_results[$k];
				if(preg_match("#(.*?)\|\|(\d+)#", $fr['class'], $matches)) $obj = new $matches[1]($matches[2]);
				else $obj = new $fr['class']();
				$buffer .= $htmlList->item($obj->searchSiteResult($fr), array(), false, true);
			}
			$buffer .= $htmlList->end();
		}
		else $buffer .= "<p class=\"message\">"._("La ricerca non ha prodotto risultati")."</p>";

		$htmlsection->content = $buffer;

		return $htmlsection->render();
	}

	public function info() {
	
		$htmlsection = new htmlSection(array('class'=>'admin', 'headerTag'=>'h1', 'headerLabel'=>_("Modulo di ricerca nel sito")));

		$buffer = "<p>"._("Il modulo mette a disposizione una interfaccia di ricerca nel sito.")."</p>";
		$buffer .= "<p>"._("Nelle <b>Opzioni</b> è possibile definire un template sostitutivo di quello di default, e indicare i valori ID dei moduli (di sistema e non) che si vogliono includere nella ricerca.")."</p>";
		$buffer .= "<p>"._("Per poter funzionare occorre")."</p>";
		$buffer .= "<ul>";
		$buffer .= "<li>"._("caricare sul database la funzione <b>replace_ci</b> (vedi INSTALL.TXT)")."</li>";
		$buffer .= "<li>"._("nei moduli indicati nella ricerca occorre definire e argomentare i metodi <b>searchSite</b> e <b>searchSiteResult</b>")."</li>";
		$buffer .= "</ul>";

		$htmlsection->content = $buffer;

		return $htmlsection->render();
	}
}

?>
Return current item: GINO CMS