Replace global_search.php by a LSurl route

This commit is contained in:
Benjamin Renard 2020-05-04 15:24:24 +02:00
parent 07e7c3031f
commit d6848a3d71
4 changed files with 121 additions and 114 deletions

View file

@ -1,112 +0,0 @@
<?php
/*******************************************************************************
* Copyright (C) 2007 Easter-eggs
* http://ldapsaisie.labs.libre-entreprise.org
*
* Author: See AUTHORS file in top-level directory.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* 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 'core.php';
if(LSsession :: startLSsession()) {
if (LSsession :: globalSearch()) {
$LSaccess = LSsession :: getLSaccess();
$pattern = (isset($_REQUEST['pattern'])?$_REQUEST['pattern']:'');
if (empty($pattern)) {
LSerror :: addErrorCode(false, _('You must provide pattern for global search.'));
LSsession :: redirect('index.php');
}
$LSview_actions=array();
$LSview_actions['refresh'] = array (
'label' => _('Refresh'),
'url' => 'global_search.php?pattern='.urlencode($pattern).'&refresh=1',
'action' => 'refresh'
);
LStemplate :: assign('LSview_actions', $LSview_actions);
if (LSsession :: loadLSclass('LSform')) {
LSform :: loadDependenciesDisplayView();
}
$onlyOne = true;
$onlyOneObject = false;
if (LSsession :: loadLSclass('LSsearch')) {
$pages=array();
foreach ($LSaccess as $LSobject => $label) {
if ( $LSobject != SELF && LSsession :: loadLSobject($LSobject) ) {
if (!LSconfig::get("LSobjects.$LSobject.globalSearch", true, 'bool'))
continue;
$object = new $LSobject();
LStemplate :: assign('pagetitle',$object -> getLabel());
$LSsearch = new LSsearch($LSobject,'LSview');
$LSsearch -> setParamsFormPostData();
$LSsearch -> run();
if ($LSsearch -> total > 0) {
$page = $LSsearch -> getPage(0);
LStemplate :: assign('page',$page);
LStemplate :: assign('LSsearch',$LSsearch);
$pages[] = LSsession :: fetchTemplate('global_search_one_page.tpl');
if ($onlyOne) {
if ($LSsearch -> total > 1) {
$onlyOne = false;
}
else {
if ($onlyOneObject === false) {
$onlyOneObject = array (
'LSobject' => $LSobject,
'dn' => $page['list'][0] -> dn,
);
}
else {
// More than one LSobject type result with one object found
$onlyOne = false;
}
}
}
}
$LSsearch -> afterUsingResult();
}
}
}
else {
LSsession :: addErrorCode('LSsession_05','LSsearch');
}
if ($onlyOne && $onlyOneObject && isset($_REQUEST['LSsearch_submit'])) {
LSurl :: redirect('object/'.$onlyOneObject['LSobject'].'/'.urlencode($onlyOneObject['dn']));
}
LStemplate :: assign('pattern',$pattern);
LStemplate :: assign('pages',$pages);
LSsession :: setTemplate('global_search.tpl');
}
else {
LSerror :: addErrorCode('LSsession_11');
}
}
else {
LSsession :: setTemplate('login.tpl');
}
// Print template
LSsession :: displayTemplate();

View file

@ -46,6 +46,125 @@ function handle_index($request) {
}
LSurl :: add_handler('#^(index\.php)?$#', 'handle_index', true);
/*
* Handle global seearch request
*
* @param[in] $request LSurlRequest The request
*
* @retval void
**/
function handle_global_search($request) {
// Check global search is enabled
if (!LSsession :: globalSearch()) {
LSurl :: error_404($request);
return false;
}
if (!LSsession :: loadLSclass('LSsearch')) {
LSsession :: addErrorCode('LSsession_05','LSsearch');
LSsession :: displayTemplate();
return false;
}
$LSaccess = LSsession :: getLSaccess();
$pattern = (isset($_REQUEST['pattern'])?$_REQUEST['pattern']:'');
if (empty($pattern)) {
LSerror :: addErrorCode(false, _('You must provide pattern for global search.'));
LSurl :: redirect();
}
$LSview_actions=array();
$LSview_actions['refresh'] = array (
'label' => _('Refresh'),
'url' => 'search.php?pattern='.urlencode($pattern).'&refresh=1',
'action' => 'refresh'
);
LStemplate :: assign('LSview_actions', $LSview_actions);
if (LSsession :: loadLSclass('LSform')) {
LSform :: loadDependenciesDisplayView();
}
$onlyOne = true;
$onlyOneObject = false;
$pages=array();
foreach ($LSaccess as $LSobject => $label) {
if ( $LSobject == SELF || !LSsession :: loadLSobject($LSobject) )
continue;
if (!LSconfig::get("LSobjects.$LSobject.globalSearch", true, 'bool'))
continue;
$object = new $LSobject();
LStemplate :: assign('pagetitle', $object -> getLabel());
$LSsearch = new LSsearch($LSobject, 'LSview');
$LSsearch -> setParamsFormPostData();
$LSsearch -> run();
if ($LSsearch -> total > 0) {
$page = $LSsearch -> getPage(0);
LStemplate :: assign('page', $page);
LStemplate :: assign('LSsearch', $LSsearch);
$pages[] = LSsession :: fetchTemplate('global_search_one_page.tpl');
if ($onlyOne) {
if ($LSsearch -> total > 1) {
$onlyOne = false;
}
else {
if ($onlyOneObject === false) {
$onlyOneObject = array (
'LSobject' => $LSobject,
'dn' => $page['list'][0] -> dn,
);
}
else {
// More than one LSobject type result with one object found
$onlyOne = false;
}
}
}
$LSsearch -> afterUsingResult();
}
}
if ($onlyOne && $onlyOneObject && isset($_REQUEST['LSsearch_submit'])) {
LSurl :: redirect('object/'.$onlyOneObject['LSobject'].'/'.urlencode($onlyOneObject['dn']));
}
LStemplate :: assign('pattern',$pattern);
LStemplate :: assign('pages',$pages);
LSsession :: setTemplate('global_search.tpl');
// Display template
LSsession :: displayTemplate();
}
LSurl :: add_handler('#^search/?$#', 'handle_global_search');
/*
* Handle old global_search.php request for retro-compatibility
*
* @param[in] $request LSurlRequest The request
*
* @retval void
**/
function handle_old_global_search_php($request) {
if (!isset($_GET['pattern']))
$url = null;
else {
$url = "search?pattern=".$_GET['pattern'];
if (isset($_GET['LSsearch_submit']))
$url .= "&LSsearch_submit";
if (isset($_GET['refresh']))
$url .= "&refresh";
}
LSerror :: addErrorCode('LSsession_26', 'global_search.php');
LSurl :: redirect($url);
}
LSurl :: add_handler('#^global_search\.php#', 'handle_old_global_search_php');
/*
* Handle image request
*

View file

@ -1,7 +1,7 @@
{include file='ls:top.tpl'}
<form action='global_search.php' method='post' class='LSview_search' id='LSsearch_form'>
<form action='search' method='post' class='LSview_search' id='LSsearch_form'>
<div class='LSview_search'>
<input type='text' name='pattern' class='LSview_search' value='{$pattern|escape:"htmlall"}'/>
<input type='submit' value='{tr msg="Search"}' name='LSsearch_submit' class='LSview_search' />

View file

@ -31,7 +31,7 @@
{/if}
{if $globalSearch && $LSaccess}
<form action='global_search.php' method='post' class='LSview_search LSglobal_search'>
<form action='search' method='post' class='LSview_search LSglobal_search'>
<input type='hidden' name='LSsearch_submit' value='1'/>
<input type='text' name='pattern' class='LSview_search LSglobal_search' placeholder='{tr msg='Global search'}' required="required"/>
<input type='image' src='{img name='find'}' alt='{tr msg='Global search'}' title='{tr msg='Global search'}' />