mirror of
https://github.com/Combodo/iTop.git
synced 2026-03-01 15:14:11 +01:00
N°5874 - Quick create: improvement for newcomer
This commit is contained in:
@@ -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