diff --git a/application/utils.inc.php b/application/utils.inc.php index 92c34a691..376103a34 100644 --- a/application/utils.inc.php +++ b/application/utils.inc.php @@ -194,7 +194,7 @@ class utils */ public static function ShowObsoleteData() { - $bDefault = MetaModel::GetConfig()->Get('show_obsolete_data'); // default is false + $bDefault = MetaModel::GetConfig()->Get('obsolescence.show_obsolete_data'); // default is false $bShow = appUserPreferences::GetPref('show_obsolete_data', $bDefault); if (static::IsArchiveMode()) { diff --git a/core/background.inc.php b/core/background.inc.php new file mode 100644 index 000000000..25bc762b7 --- /dev/null +++ b/core/background.inc.php @@ -0,0 +1,55 @@ + + + +/** + * Tasks performed in the background + * + * @copyright Copyright (C) 2017 Combodo SARL + * @license http://opensource.org/licenses/AGPL-3.0 + */ + + +class ObsolescenceDateUpdater implements iBackgroundProcess +{ + public function GetPeriodicity() + { + return MetaModel::GetConfig()->Get('obsolescence.date_update_interval'); // 10 mn + } + + public function Process($iUnixTimeLimit) + { + $iCountSet = 0; + $iCountReset = 0; + $iClasses = 0; + foreach (MetaModel::EnumObsoletableClasses() as $sClass) + { + $oObsoletedToday = new DBObjectSearch($sClass); + $oObsoletedToday->AddCondition('obsolescence_flag', 1, '='); + $oObsoletedToday->AddCondition('obsolescence_date', null, '='); + $sToday = date(AttributeDate::GetSQLFormat()); + $iCountSet += MetaModel::BulkUpdate($oObsoletedToday, array('obsolescence_date' => $sToday)); + + $oObsoletedToday = new DBObjectSearch($sClass); + $oObsoletedToday->AddCondition('obsolescence_flag', 1, '!='); + $oObsoletedToday->AddCondition('obsolescence_date', null, '!='); + $iCountReset += MetaModel::BulkUpdate($oObsoletedToday, array('obsolescence_date' => null)); + } + echo "Obsolescence date updated (classes: $iClasses ; set: $iCountSet ; reset: $iCountReset)\n"; + } +} diff --git a/core/config.class.inc.php b/core/config.class.inc.php index eb0016aba..7c7dccdf3 100644 --- a/core/config.class.inc.php +++ b/core/config.class.inc.php @@ -929,7 +929,7 @@ class Config 'source_of_value' => '', 'show_in_conf_sample' => false, ), - 'show_obsolete_data' => array( + 'obsolescence.show_obsolete_data' => array( 'type' => 'bool', 'description' => 'Default value for the user preference "show obsolete data"', 'default' => false, @@ -937,6 +937,14 @@ class Config 'source_of_value' => '', 'show_in_conf_sample' => false, ), + 'obsolescence.date_update_interval' => array( + 'type' => 'integer', + 'description' => 'Delay in seconds between two refreshes of the obsolescence dates.', + 'default' => 600, + 'value' => 600, + 'source_of_value' => '', + 'show_in_conf_sample' => false, + ), ); public function IsProperty($sPropCode) diff --git a/core/metamodel.class.php b/core/metamodel.class.php index 1bf5e376d..bb0f1e39c 100644 --- a/core/metamodel.class.php +++ b/core/metamodel.class.php @@ -2554,13 +2554,14 @@ abstract class MetaModel } return $aRes; } - public static function EnumObsoletableClasses() + public static function EnumObsoletableClasses($bRootClassesOnly = true) { $aRes = array(); foreach (self::GetClasses() as $sClass) { if (self::IsObsoletable($sClass)) { + if ($bRootClassesOnly && !static::IsRootClass($sClass)) continue; $aRes[] = $sClass; } } @@ -4868,6 +4869,11 @@ abstract class MetaModel } } + /** + * @param DBObjectSearch $oFilter + * @param array $aValues array of attcode => value + * @return int Modified objects + */ public static function BulkUpdate(DBObjectSearch $oFilter, array $aValues) { // $aValues is an array of $sAttCode => $value @@ -4876,6 +4882,7 @@ abstract class MetaModel { CMDBSource::Query($sSQL); } + return CMDBSource::AffectedRows(); } /** diff --git a/pages/preferences.php b/pages/preferences.php index 14ed7c106..87e3cb942 100644 --- a/pages/preferences.php +++ b/pages/preferences.php @@ -90,7 +90,7 @@ function DisplayPreferences($oP) $iDefaultPageSize = appUserPreferences::GetPref('default_page_size', MetaModel::GetConfig()->GetMinDisplayLimit()); $oP->add('

'.Dict::Format('UI:Favorites:Default_X_ItemsPerPage', '').'

'); - $bDefaultShow = appUserPreferences::GetPref('show_obsolete_data', MetaModel::GetConfig()->Get('show_obsolete_data')); + $bDefaultShow = appUserPreferences::GetPref('show_obsolete_data', MetaModel::GetConfig()->Get('obsolescence.show_obsolete_data')); $sSelected = $bDefaultShow ? ' checked="checked"' : ''; $oP->add( '

' diff --git a/webservices/cron.php b/webservices/cron.php index 9ea73951b..7a381b3e8 100644 --- a/webservices/cron.php +++ b/webservices/cron.php @@ -29,6 +29,7 @@ require_once(APPROOT.'/application/application.inc.php'); require_once(APPROOT.'/application/nicewebpage.class.inc.php'); require_once(APPROOT.'/application/webpage.class.inc.php'); require_once(APPROOT.'/application/clipage.class.inc.php'); +require_once(APPROOT.'/core/background.inc.php'); $sConfigFile = APPCONF.ITOP_DEFAULT_ENV.'/'.ITOP_CONFIG_FILE; if (!file_exists($sConfigFile))