mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-26 12:08:47 +02:00
N°5874 - Quick create: improvement for newcomer
This commit is contained in:
@@ -59,10 +59,14 @@ class QuickCreate extends UIBlock implements iKeyboardShortcut
|
||||
protected $aLastClasses;
|
||||
/** @var int $iMaxAutocompleteResults Max. number of elements returned by the autocomplete */
|
||||
protected $iMaxAutocompleteResults;
|
||||
/** @var bool $bShowHistory Whether or not to display the elements in the history */
|
||||
/** @var bool $bShowHistory Whether to display the elements in the history */
|
||||
protected $bShowHistory;
|
||||
/** @var int $iMaxHistoryResults Max. number of elements in the history */
|
||||
protected $iMaxHistoryResults;
|
||||
/** @var int $iMaxPopularResults Max. number of elements in popular */
|
||||
protected $iMaxPopularResults;
|
||||
/** @var array $aPopularClasses */
|
||||
protected $aPopularClasses;
|
||||
|
||||
/**
|
||||
* QuickCreate constructor.
|
||||
@@ -82,6 +86,8 @@ class QuickCreate extends UIBlock implements iKeyboardShortcut
|
||||
$this->iMaxAutocompleteResults = (int) MetaModel::GetConfig()->Get('quick_create.max_autocomplete_results');
|
||||
$this->bShowHistory = (bool) MetaModel::GetConfig()->Get('quick_create.show_history');
|
||||
$this->iMaxHistoryResults = (int) MetaModel::GetConfig()->Get('quick_create.max_history_results');
|
||||
$this->iMaxPopularResults = (int) MetaModel::GetConfig()->Get('quick_create.max_popular_results');
|
||||
$this->aPopularClasses = QuickCreateHelper::GetPopularClasses();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,7 +95,7 @@ class QuickCreate extends UIBlock implements iKeyboardShortcut
|
||||
* If $bRelativeUrl is true, then $sEndpoint will be complete with the app_root_url
|
||||
*
|
||||
* @param string $sEndpoint URL to the endpoint
|
||||
* @param bool $bRelativeUrl Whether or not the $sEndpoint parameter is a relative URL
|
||||
* @param bool $bRelativeUrl Whether the $sEndpoint parameter is a relative URL
|
||||
*
|
||||
* @return $this
|
||||
* @throws \Exception
|
||||
@@ -203,6 +209,24 @@ class QuickCreate extends UIBlock implements iKeyboardShortcut
|
||||
return $this->iMaxHistoryResults;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see $aMaxPopularClasses
|
||||
* @return array
|
||||
*/
|
||||
public function GetPopularClasses(): array
|
||||
{
|
||||
return $this->aPopularClasses;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see $iMaxPopularResults
|
||||
* @return int
|
||||
*/
|
||||
public function GetMaxPopularResults(): int
|
||||
{
|
||||
return $this->iMaxPopularResults;
|
||||
}
|
||||
|
||||
public static function GetShortcutKeys(): array
|
||||
{
|
||||
return [['id' => 'ibo-open-quick-create', 'label' => 'UI:Component:QuickCreate:KeyboardShortcut:OpenDrawer', 'key' => 'c', 'event' => 'open_drawer']];
|
||||
|
||||
@@ -24,6 +24,7 @@ use appUserPreferences;
|
||||
use DBObject;
|
||||
use MetaModel;
|
||||
use utils;
|
||||
use UserRights;
|
||||
|
||||
/**
|
||||
* Class QuickCreateHelper
|
||||
@@ -71,7 +72,7 @@ class QuickCreateHelper
|
||||
array_unshift($aHistoryEntries, $aNewEntry);
|
||||
|
||||
// Truncate history
|
||||
static::TruncateHistory($aHistoryEntries);
|
||||
static::Truncate($aHistoryEntries, 'quick_create.max_history_results');
|
||||
|
||||
appUserPreferences::SetPref(static::USER_PREF_CODE, $aHistoryEntries);
|
||||
}
|
||||
@@ -88,7 +89,8 @@ class QuickCreateHelper
|
||||
{
|
||||
/** @var array $aHistoryEntries */
|
||||
$aHistoryEntries = appUserPreferences::GetPref(static::USER_PREF_CODE, []);
|
||||
static::TruncateHistory($aHistoryEntries);
|
||||
|
||||
static::Truncate($aHistoryEntries, 'quick_create.max_history_results');
|
||||
|
||||
for($iIdx = 0; $iIdx < count($aHistoryEntries); $iIdx++)
|
||||
{
|
||||
@@ -125,16 +127,70 @@ class QuickCreateHelper
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncate $aHistoryEntries to 'global_search.max_history_results' entries
|
||||
* Return an array of popular object classes
|
||||
*
|
||||
* @param array $aHistoryEntries
|
||||
* @return array
|
||||
* @throws \CoreException
|
||||
* @throws \CoreUnexpectedValue
|
||||
* @throws \MySQLException
|
||||
*/
|
||||
protected static function TruncateHistory(array &$aHistoryEntries): void
|
||||
public static function GetPopularClasses(): array
|
||||
{
|
||||
$iMaxHistoryResults = (int) MetaModel::GetConfig()->Get('quick_create.max_history_results');
|
||||
if(count($aHistoryEntries) > $iMaxHistoryResults)
|
||||
$aHistoryEntries = appUserPreferences::GetPref(static::USER_PREF_CODE, []);
|
||||
static::Truncate($aHistoryEntries, 'quick_create.max_history_results');
|
||||
$aPopularClassesNames = UserRights::GetAllowedClasses(UR_ACTION_CREATE, array('popular'), false);
|
||||
|
||||
// Prevent classes in both Popular and Recent to appear twice
|
||||
for($iIdx = 0; $iIdx < count($aHistoryEntries); $iIdx++)
|
||||
{
|
||||
$aHistoryEntries = array_slice($aHistoryEntries, 0, $iMaxHistoryResults);
|
||||
if (($key = array_search($aHistoryEntries[$iIdx]['class'], $aPopularClassesNames)) !== false) {
|
||||
unset($aPopularClassesNames[$key]);
|
||||
}
|
||||
}
|
||||
//die(var_dump($aPopularClassesNames));
|
||||
static::Truncate($aPopularClassesNames, 'quick_create.max_popular_results');
|
||||
$aPopularClasses = array();
|
||||
foreach($aPopularClassesNames as $sClass)
|
||||
{
|
||||
if (!MetaModel::IsValidClass($sClass)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Add class icon
|
||||
$sClassIconUrl = MetaModel::GetClassIcon($sClass, false);
|
||||
// Mind that some classes don't have an icon
|
||||
$sClassIconUrl = !empty($sClassIconUrl) ? $sClassIconUrl : null;
|
||||
|
||||
// Add class label
|
||||
$sLabelHtml = utils::EscapeHtml(MetaModel::GetName($sClass));
|
||||
|
||||
// Add URL
|
||||
$sTargetUrl = DBObject::ComputeStandardUIPage($sClass).'?operation=new&class='.$sClass;
|
||||
|
||||
$aPopularClasses[] = array(
|
||||
'class' => $sClass,
|
||||
'icon_url' => $sClassIconUrl,
|
||||
'label_html' => $sLabelHtml,
|
||||
'target_url' => $sTargetUrl
|
||||
);
|
||||
}
|
||||
return $aPopularClasses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncate $aHistoryEntries to 'global_search.max_history_results' entries OR $aPopularClasses to 'global_search.max_popular_results'
|
||||
*
|
||||
* @param array $aEntries
|
||||
* @param string $sMaxEntriesParam
|
||||
*/
|
||||
protected static function Truncate(array &$aEntries, string $sMaxEntriesParam = 'global_search.max_history_results'): void
|
||||
{
|
||||
$iMaxResults = (int) MetaModel::GetConfig()->Get($sMaxEntriesParam);
|
||||
if(count($aEntries) > $iMaxResults)
|
||||
{
|
||||
$aEntries = array_slice($aEntries, 0, $iMaxResults);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user