<commit_before><?php
/**
 * @author <NAME> <<EMAIL>>
 * @copyright 2012 <NAME>
 * @see https://www.elabftw.net Official website
 * @license AGPL-3.0
 * @package elabftw
 */
declare(strict_types=1);

namespace Elabftw\Elabftw;

use function count;
use Elabftw\Controllers\SearchController;
use Elabftw\Exceptions\ImproperActionException;
use Elabftw\Models\Database;
use Elabftw\Models\Experiments;
use Elabftw\Models\ItemsTypes;
use Elabftw\Models\Status;
use Elabftw\Models\Tags;
use Elabftw\Models\TeamGroups;
use Elabftw\Services\Check;
use Elabftw\Services\Filter;
use function filter_var;
use function rtrim;
use function trim;

/**
 * The search page
 * Here be dragons!
 *
 */
require_once 'app/init.inc.php';
$App->pageTitle = _('Search');

$Experiments = new Experiments($App->Users);
$Database = new Database($App->Users);
$Tags = new Tags($Experiments);
$tagsArr = $Tags->readAll();

$ItemsTypes = new ItemsTypes($App->Users);
$Status = new Status($App->Users);
if ($Request->query->get('type') !== 'experiments') {
    $categoryArr = $ItemsTypes->readAll();
} else {
    $categoryArr = $Status->read();
}

$TeamGroups = new TeamGroups($App->Users);
$teamGroupsArr = $TeamGroups->read();

$usersArr = $App->Users->readAllFromTeam();

// ANDOR
$andor = ' AND ';
if ($Request->query->has('andor') && $Request->query->get('andor') === 'or') {
    $andor = ' OR ';
}

// WHERE do we search?
if ($Request->query->get('type') === 'experiments') {
    $Entity = $Experiments;
} else {
    $Entity = $Database;
}

// TITLE
$title = '';
if ($Request->query->has('title') && !empty($Request->query->get('title'))) {
    $title = filter_var(trim($Request->query->get('title')), FILTER_SANITIZE_STRING);
    if ($title !== false) {
        $Entity->titleFilter = Tools::getSearchSql($title, $andor, 'title', $Entity->type);
    }
}

// BODY
$body = '';
if ($Request->query->has('body') && !empty($Request->query->get('body'))) {
    $body = filter_var(trim($Request->query->get('body')), FILTER_SANITIZE_STRING);
    if ($body !== false) {
        $Entity->bodyFilter = Tools::getSearchSql($body, $andor, 'body', $Entity->type);
    }
}

// VISIBILITY
$vis = '';
if ($Request->query->has('vis') && !empty($Request->query->get('vis'))) {
    $vis = Check::visibility($Request->query->get('vis'));
}

// FROM
$from = '';
if ($Request->query->has('from') && !empty($Request->query->get('from'))) {
    $from = Filter::kdate($Request->query->get('from'));
}

// TO
$to = '';
if ($Request->query->has('to') && !empty($Request->query->get('to'))) {
    $to = Filter::kdate($Request->query->get('to'));
}

// RENDER THE FIRST PART OF THE PAGE (search form)
$renderArr = array(
    'Request' => $Request,
    'Experiments' => $Experiments,
    'Database' => $Database,
    'andor' => $andor,
    'body' => $body,
    'categoryArr' => $categoryArr,
    'tagsArr' => $tagsArr,
    'teamGroupsArr' => $teamGroupsArr,
    'title' => $title,
    'statusArr' => $categoryArr,
    'usersArr' => $usersArr,
);
echo $App->render('search.html', $renderArr);

/**
 * Here the search begins
 * If there is a search, there will be get parameters, so this is our main switch<commit_msg>fix incorrect status list on search page. fix #2295<commit_after><?php
/**
 * @author <NAME> <<EMAIL>>
 * @copyright 2012 <NAME>
 * @see https://www.elabftw.net Official website
 * @license AGPL-3.0
 * @package elabftw
 */
declare(strict_types=1);

namespace Elabftw\Elabftw;

use function count;
use Elabftw\Controllers\SearchController;
use Elabftw\Exceptions\ImproperActionException;
use Elabftw\Models\Database;
use Elabftw\Models\Experiments;
use Elabftw\Models\ItemsTypes;
use Elabftw\Models\Status;
use Elabftw\Models\Tags;
use Elabftw\Models\TeamGroups;
use Elabftw\Services\Check;
use Elabftw\Services\Filter;
use function filter_var;
use function rtrim;
use function trim;

/**
 * The search page
 * Here be dragons!
 *
 */
require_once 'app/init.inc.php';
$App->pageTitle = _('Search');

$Experiments = new Experiments($App->Users);
$Database = new Database($App->Users);
$Tags = new Tags($Experiments);
$tagsArr = $Tags->readAll();

$ItemsTypes = new ItemsTypes($App->Users);
$Status = new Status($App->Users);
if ($Request->query->get('type') !== 'experiments') {
    $categoryArr = $ItemsTypes->readAll();
} else {
    $categoryArr = $Status->read();
}
$statusArr = $Status->read();

$TeamGroups = new TeamGroups($App->Users);
$teamGroupsArr = $TeamGroups->read();

$usersArr = $App->Users->readAllFromTeam();

// ANDOR
$andor = ' AND ';
if ($Request->query->has('andor') && $Request->query->get('andor') === 'or') {
    $andor = ' OR ';
}

// WHERE do we search?
if ($Request->query->get('type') === 'experiments') {
    $Entity = $Experiments;
} else {
    $Entity = $Database;
}

// TITLE
$title = '';
if ($Request->query->has('title') && !empty($Request->query->get('title'))) {
    $title = filter_var(trim($Request->query->get('title')), FILTER_SANITIZE_STRING);
    if ($title !== false) {
        $Entity->titleFilter = Tools::getSearchSql($title, $andor, 'title', $Entity->type);
    }
}

// BODY
$body = '';
if ($Request->query->has('body') && !empty($Request->query->get('body'))) {
    $body = filter_var(trim($Request->query->get('body')), FILTER_SANITIZE_STRING);
    if ($body !== false) {
        $Entity->bodyFilter = Tools::getSearchSql($body, $andor, 'body', $Entity->type);
    }
}

// VISIBILITY
$vis = '';
if ($Request->query->has('vis') && !empty($Request->query->get('vis'))) {
    $vis = Check::visibility($Request->query->get('vis'));
}

// FROM
$from = '';
if ($Request->query->has('from') && !empty($Request->query->get('from'))) {
    $from = Filter::kdate($Request->query->get('from'));
}

// TO
$to = '';
if ($Request->query->has('to') && !empty($Request->query->get('to'))) {
    $to = Filter::kdate($Request->query->get('to'));
}

// RENDER THE FIRST PART OF THE PAGE (search form)
$renderArr = array(
    'Request' => $Request,
    'Experiments' => $Experiments,
    'Database' => $Database,
    'andor' => $andor,
    'body' => $body,
    'categoryArr' => $categoryArr,
    'tagsArr' => $tagsArr,
    'teamGroupsArr' => $teamGroupsArr,
    'title' => $title,
    'statusArr' => $statusArr,
    'usersArr' => $usersArr,
);
echo $App->render('search.html', $renderArr);

/**
 * Here the search begins
 * If there is a search, there will be get parameters, so this is our main switch
{'fraction_non_alphanumeric': 0.21370463078848562, 'fraction_numerical': 0.002816020025031289, 'mean_word_length': 4.445485519591141, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 144, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3547875', 'n_tokens_mistral': 2254, 'n_tokens_neox': 2221, 'n_words': 453}