added a few more entities to globalsearch (admin-side)
Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
@@ -4,9 +4,7 @@ namespace Froxlor\Ajax;
|
||||
|
||||
use Exception;
|
||||
use Froxlor\Http\HttpClient;
|
||||
use Froxlor\PhpHelper;
|
||||
use Froxlor\Settings;
|
||||
use Froxlor\User;
|
||||
use Froxlor\UI\Panel\UI;
|
||||
use Froxlor\UI\Request;
|
||||
|
||||
@@ -223,152 +221,16 @@ class Ajax
|
||||
$searchtext = Request::get('searchtext');
|
||||
|
||||
$result = [];
|
||||
if ($searchtext && strlen(trim($searchtext)) > 2) {
|
||||
|
||||
$processed = [];
|
||||
// settings
|
||||
$result_settings = GlobalSearch::searchSettings($searchtext, $this->userinfo);
|
||||
|
||||
$stparts = explode(" ", $searchtext);
|
||||
// all searchable entities
|
||||
$result_entities = GlobalSearch::searchGlobal($searchtext, $this->userinfo);
|
||||
|
||||
foreach ($stparts as $searchtext) {
|
||||
$searchtext = trim($searchtext);
|
||||
$result = array_merge($result_settings, $result_entities);
|
||||
|
||||
// settings (if allowed)
|
||||
if (isset($this->userinfo['adminsession']) && $this->userinfo['adminsession'] == 1) {
|
||||
|
||||
if ($this->userinfo['change_serversettings'] == 1) {
|
||||
$settings_data = PhpHelper::loadConfigArrayDir(\Froxlor\Froxlor::getInstallDir() . '/actions/admin/settings/');
|
||||
$results = array();
|
||||
if (!isset($processed['settings'])) {
|
||||
$processed['settings'] = [];
|
||||
}
|
||||
PhpHelper::recursive_array_search($searchtext, $settings_data, $results);
|
||||
foreach ($results as $pathkey) {
|
||||
$pk = explode(".", $pathkey);
|
||||
if (count($pk) > 4) {
|
||||
$settingkey = $pk[0] . '.' . $pk[1] . '.' . $pk[2] . '.' . $pk[3];
|
||||
if (is_array($processed['settings']) && !array_key_exists($settingkey, $processed['settings'])) {
|
||||
$processed['settings'][$settingkey] = true;
|
||||
$sresult = $settings_data[$pk[0]][$pk[1]][$pk[2]][$pk[3]];
|
||||
if ($sresult['type'] != 'hidden') {
|
||||
if (!isset($result['settings'])) {
|
||||
$result['settings'] = [];
|
||||
}
|
||||
$result['settings'][] = [
|
||||
'title' => (is_array($sresult['label']) ? $sresult['label']['title'] : $sresult['label']),
|
||||
'href' => 'admin_settings.php?page=overview&part=' . $pk[1] . '&em=' . $pk[3]
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// customers
|
||||
$searchfields = [
|
||||
'c.loginname',
|
||||
'c.name',
|
||||
'c.firstname',
|
||||
'c.company',
|
||||
'c.street',
|
||||
'c.zipcode',
|
||||
'c.city',
|
||||
'c.email',
|
||||
'c.customernumber'
|
||||
];
|
||||
$collection = (new \Froxlor\UI\Collection(\Froxlor\Api\Commands\Customers::class, $this->userinfo))
|
||||
->addParam(['sql_search' => [
|
||||
'_plainsql' => $this->searchStringSql($searchfields, $searchtext)
|
||||
]]);
|
||||
if ($collection->count() > 0) {
|
||||
if (!isset($processed['customer'])) {
|
||||
$processed['customer'] = [];
|
||||
}
|
||||
foreach ($collection->getList() as $cresult) {
|
||||
if (is_array($processed['customer']) && !array_key_exists($cresult['customerid'], $processed['customer'])) {
|
||||
$processed['customer'][$cresult['customerid']] = true;
|
||||
if (!isset($result['customer'])) {
|
||||
$result['customer'] = [];
|
||||
}
|
||||
$result['customer'][] = [
|
||||
'title' => User::getCorrectFullUserDetails($cresult),
|
||||
'href' => 'admin_customers.php?page=customers&action=edit&id=' . $cresult['customerid']
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// domains
|
||||
$searchfields = [
|
||||
'd.domain',
|
||||
'd.domain_ace',
|
||||
'd.documentroot'
|
||||
];
|
||||
$collection = (new \Froxlor\UI\Collection(\Froxlor\Api\Commands\Domains::class, $this->userinfo))
|
||||
->addParam(['sql_search' => [
|
||||
'_plainsql' => $this->searchStringSql($searchfields, $searchtext)
|
||||
]]);
|
||||
if ($collection->count() > 0) {
|
||||
if (!isset($processed['domains'])) {
|
||||
$processed['domains'] = [];
|
||||
}
|
||||
foreach ($collection->getList() as $cresult) {
|
||||
if (is_array($processed['domains']) && !array_key_exists($cresult['id'], $processed['domains'])) {
|
||||
$processed['domains'][$cresult['id']] = true;
|
||||
if (!isset($result['domains'])) {
|
||||
$result['domains'] = [];
|
||||
}
|
||||
$result['domains'][] = [
|
||||
'title' => $cresult['domain_ace'],
|
||||
'href' => 'admin_domains.php?page=domains&action=edit&id=' . $cresult['id']
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
} // is-admin
|
||||
else {
|
||||
// subdomains
|
||||
$searchfields = [
|
||||
'd.domain',
|
||||
'd.domain_ace',
|
||||
'd.documentroot'
|
||||
];
|
||||
$collection = (new \Froxlor\UI\Collection(\Froxlor\Api\Commands\Domains::class, $this->userinfo))
|
||||
->addParam(['sql_search' => [
|
||||
'_plainsql' => $this->searchStringSql($searchfields, $searchtext)
|
||||
]]);
|
||||
if ($collection->count() > 0) {
|
||||
if (!isset($processed['domains'])) {
|
||||
$processed['domains'] = [];
|
||||
}
|
||||
foreach ($collection->getList() as $cresult) {
|
||||
if (is_array($processed['domains']) && !array_key_exists($cresult['domains'], $processed['domains'])) {
|
||||
$processed['domains'][$cresult['id']] = true;
|
||||
if (!isset($result['domains'])) {
|
||||
$result['domains'] = [];
|
||||
}
|
||||
$result['domains'][] = [
|
||||
'title' => $cresult['domain_ace'],
|
||||
'href' => 'customer_domains.php?page=domains&action=edit&id=' . $cresult['id']
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
} // is-customer
|
||||
} // foreach splitted search-term
|
||||
}
|
||||
header("Content-type: application/json");
|
||||
echo json_encode($result);
|
||||
}
|
||||
|
||||
private function searchStringSql(array $searchfields, $searchtext)
|
||||
{
|
||||
$result = ['sql' => [], 'values' => []];
|
||||
$result['sql'] = "(";
|
||||
foreach ($searchfields as $sf) {
|
||||
$result['sql'] .= $sf . " LIKE :searchtext OR ";
|
||||
}
|
||||
$result['sql'] = substr($result['sql'], 0, -3) . ")";
|
||||
$result['values'] = ['searchtext' => '%' . $searchtext . '%'];
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
270
lib/Froxlor/Ajax/GlobalSearch.php
Normal file
270
lib/Froxlor/Ajax/GlobalSearch.php
Normal file
@@ -0,0 +1,270 @@
|
||||
<?php
|
||||
|
||||
namespace Froxlor\Ajax;
|
||||
|
||||
use Froxlor\Froxlor;
|
||||
use Froxlor\PhpHelper;
|
||||
use Froxlor\UI\Collection;
|
||||
|
||||
/**
|
||||
* This file is part of the Froxlor project.
|
||||
* Copyright (c) 2010 the Froxlor Team (see authors).
|
||||
*
|
||||
* For the full copyright and license information, please view the COPYING
|
||||
* file that was distributed with this source code. You can also view the
|
||||
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
|
||||
*
|
||||
* @copyright (c) the authors
|
||||
* @author Froxlor team <team@froxlor.org> (2010-)
|
||||
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
|
||||
* @package AJAX
|
||||
*
|
||||
*/
|
||||
class GlobalSearch
|
||||
{
|
||||
protected array $userinfo;
|
||||
|
||||
public static function searchSettings(string $searchtext, array $userinfo): array
|
||||
{
|
||||
$result = [];
|
||||
if ($searchtext && strlen(trim($searchtext)) > 2) {
|
||||
$processed = [];
|
||||
$stparts = explode(" ", $searchtext);
|
||||
foreach ($stparts as $searchtext) {
|
||||
$searchtext = trim($searchtext);
|
||||
|
||||
// settings (if allowed)
|
||||
if (isset($userinfo['adminsession']) && $userinfo['adminsession'] == 1 && $userinfo['change_serversettings'] == 1) {
|
||||
|
||||
$settings_data = PhpHelper::loadConfigArrayDir(Froxlor::getInstallDir() . '/actions/admin/settings/');
|
||||
$results = array();
|
||||
if (!isset($processed['settings'])) {
|
||||
$processed['settings'] = [];
|
||||
}
|
||||
PhpHelper::recursive_array_search($searchtext, $settings_data, $results);
|
||||
foreach ($results as $pathkey) {
|
||||
$pk = explode(".", $pathkey);
|
||||
if (count($pk) > 4) {
|
||||
$settingkey = $pk[0] . '.' . $pk[1] . '.' . $pk[2] . '.' . $pk[3];
|
||||
if (is_array($processed['settings']) && !array_key_exists($settingkey, $processed['settings'])) {
|
||||
$processed['settings'][$settingkey] = true;
|
||||
$sresult = $settings_data[$pk[0]][$pk[1]][$pk[2]][$pk[3]];
|
||||
if ($sresult['type'] != 'hidden') {
|
||||
if (!isset($result['settings'])) {
|
||||
$result['settings'] = [];
|
||||
}
|
||||
$result['settings'][] = [
|
||||
'title' => (is_array($sresult['label']) ? $sresult['label']['title'] : $sresult['label']),
|
||||
'href' => 'admin_settings.php?page=overview&part=' . $pk[1] . '&em=' . $pk[3]
|
||||
];
|
||||
} // not hidden
|
||||
} // if not processed
|
||||
} // correct settingkey
|
||||
} // foreach
|
||||
} // admin + change_serversetting
|
||||
} // foreach
|
||||
} // searchtext min 3 chars
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static function searchGlobal(string $searchtext, array $userinfo): array
|
||||
{
|
||||
$result = [];
|
||||
if ($searchtext && strlen(trim($searchtext)) > 2) {
|
||||
$processed = [];
|
||||
|
||||
$stparts = explode(" ", $searchtext);
|
||||
|
||||
foreach ($stparts as $searchtext) {
|
||||
$searchtext = trim($searchtext);
|
||||
|
||||
// admin
|
||||
if (isset($userinfo['adminsession']) && $userinfo['adminsession'] == 1) {
|
||||
|
||||
$toSearch = [
|
||||
// customers
|
||||
'customer' => [
|
||||
'class' => \Froxlor\Api\Commands\Customers::class,
|
||||
'searchfields' => [
|
||||
'c.loginname',
|
||||
'c.name',
|
||||
'c.firstname',
|
||||
'c.company',
|
||||
'c.street',
|
||||
'c.zipcode',
|
||||
'c.city',
|
||||
'c.email',
|
||||
'c.customernumber',
|
||||
'c.custom_notes'
|
||||
],
|
||||
'result_key' => 'customerid',
|
||||
'result_format' => [
|
||||
'title' => ['\\Froxlor\\User', 'getCorrectFullUserDetails'],
|
||||
'href' => 'admin_customers.php?page=customers&action=edit&id='
|
||||
]
|
||||
],
|
||||
// domains
|
||||
'domains' => [
|
||||
'class' => \Froxlor\Api\Commands\Domains::class,
|
||||
'searchfields' => [
|
||||
'd.domain',
|
||||
'd.domain_ace',
|
||||
'd.documentroot'
|
||||
],
|
||||
'result_key' => 'id',
|
||||
'result_format' => [
|
||||
'title' => ['self', 'getFieldFromResult'],
|
||||
'title_args' => 'domain_ace',
|
||||
'href' => 'admin_domains.php?page=domains&action=edit&id='
|
||||
]
|
||||
],
|
||||
// ips and ports
|
||||
'ipsandports' => [
|
||||
'class' => \Froxlor\Api\Commands\IpsAndPorts::class,
|
||||
'searchfields' => [
|
||||
'ip',
|
||||
'vhostcontainer',
|
||||
'specialsettings'
|
||||
],
|
||||
'result_key' => 'id',
|
||||
'result_groupkey' => 'ip',
|
||||
'result_format' => [
|
||||
'title' => ['self', 'getFieldFromResult'],
|
||||
'title_args' => 'ip',
|
||||
'href' => 'admin_ipsandports.php?page=ipsandports&action=edit&id='
|
||||
]
|
||||
],
|
||||
// hosting-plans
|
||||
'hostingplans' => [
|
||||
'class' => \Froxlor\Api\Commands\HostingPlans::class,
|
||||
'searchfields' => [
|
||||
'p.name',
|
||||
'p.description'
|
||||
],
|
||||
'result_key' => 'id',
|
||||
'result_format' => [
|
||||
'title' => ['self', 'getFieldFromResult'],
|
||||
'title_args' => 'name',
|
||||
'href' => 'admin_plans.php?page=overview&action=edit&id='
|
||||
]
|
||||
],
|
||||
// PHP configs
|
||||
'phpconfigs' => [
|
||||
'class' => \Froxlor\Api\Commands\PhpSettings::class,
|
||||
'searchfields' => [
|
||||
'c.description',
|
||||
'fd.description',
|
||||
'c.binary'
|
||||
],
|
||||
'result_key' => 'id',
|
||||
'result_format' => [
|
||||
'title' => ['self', 'getFieldFromResult'],
|
||||
'title_args' => 'description',
|
||||
'href' => ((bool) $userinfo['change_serversettings'] ? 'admin_phpsettings.php?page=overview&action=edit&id=' : 'admin_phpsettings.php?page=overview&sid=')
|
||||
]
|
||||
],
|
||||
// FPM daemons
|
||||
'fpmconfigs' => [
|
||||
'class' => \Froxlor\Api\Commands\FpmDaemons::class,
|
||||
'searchfields' => [
|
||||
'description',
|
||||
'reload_cmd'
|
||||
],
|
||||
'result_key' => 'id',
|
||||
'result_format' => [
|
||||
'title' => ['self', 'getFieldFromResult'],
|
||||
'title_args' => 'description',
|
||||
'href' => ((bool) $userinfo['change_serversettings'] ? 'admin_phpsettings.php?page=fpmdaemons&action=edit&id=' : 'admin_phpsettings.php?page=fpmdaemons&sid=')
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
if ((bool) $userinfo['change_serversettings']) {
|
||||
// admins
|
||||
$toSearch['admins'] = [
|
||||
'class' => \Froxlor\Api\Commands\Admins::class,
|
||||
'searchfields' => [
|
||||
'loginname',
|
||||
'name',
|
||||
'email',
|
||||
'custom_notes'
|
||||
],
|
||||
'result_key' => 'adminid',
|
||||
'result_format' => [
|
||||
'title' => ['self', 'getFieldFromResult'],
|
||||
'title_args' => 'name',
|
||||
'href' => 'admin_admins.php?page=admins&action=edit&id='
|
||||
]
|
||||
];
|
||||
}
|
||||
} else {
|
||||
$toSearch = [
|
||||
// (sub)domains
|
||||
'domains' => [
|
||||
'class' => \Froxlor\Api\Commands\Domains::class,
|
||||
'searchfields' => [
|
||||
'd.domain',
|
||||
'd.domain_ace',
|
||||
'd.documentroot'
|
||||
],
|
||||
'result_key' => 'id',
|
||||
'result_format' => [
|
||||
'title' => ['self', 'getFieldFromResult'],
|
||||
'title_args' => 'domain_ace',
|
||||
'href' => 'customer_domains.php?page=domains&action=edit&id='
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
foreach ($toSearch as $entity => $edata) {
|
||||
|
||||
$collection = (new Collection($edata['class'], $userinfo))
|
||||
->addParam(['sql_search' => [
|
||||
'_plainsql' => self::searchStringSql($edata['searchfields'], $searchtext)
|
||||
]]);
|
||||
if ($collection->count() > 0) {
|
||||
if (!isset($processed[$entity])) {
|
||||
$processed[$entity] = [];
|
||||
}
|
||||
$group_key = $edata['result_groupkey'] ?? $edata['result_key'];
|
||||
foreach ($collection->getList() as $cresult) {
|
||||
if (is_array($processed[$entity]) && !array_key_exists($cresult[$group_key], $processed[$entity])) {
|
||||
$processed[$entity][$cresult[$group_key]] = true;
|
||||
if (!isset($result[$entity])) {
|
||||
$result[$entity] = [];
|
||||
}
|
||||
$result[$entity][] = [
|
||||
'title' => call_user_func($edata['result_format']['title'], $cresult, ($edata['result_format']['title_args'] ?? null)),
|
||||
'href' => $edata['result_format']['href'] . $cresult[$edata['result_key']]
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
} // foreach entity
|
||||
|
||||
} // foreach splitted search-term
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
private static function searchStringSql(array $searchfields, $searchtext)
|
||||
{
|
||||
$result = ['sql' => [], 'values' => []];
|
||||
$result['sql'] = "(";
|
||||
foreach ($searchfields as $sf) {
|
||||
$result['sql'] .= $sf . " LIKE :searchtext OR ";
|
||||
}
|
||||
$result['sql'] = substr($result['sql'], 0, -3) . ")";
|
||||
$result['values'] = ['searchtext' => '%' . $searchtext . '%'];
|
||||
return $result;
|
||||
}
|
||||
|
||||
private static function getFieldFromResult(array $resultset, string $field = null)
|
||||
{
|
||||
return $resultset[$field] ?? '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user