Page MenuHomeIn-Portal Phabricator

in-portal
No OneTemporary

File Metadata

Created
Wed, Nov 5, 7:58 AM

in-portal

Index: branches/5.2.x/core/units/scheduled_tasks/scheduled_task_eh.php
===================================================================
--- branches/5.2.x/core/units/scheduled_tasks/scheduled_task_eh.php (revision 16861)
+++ branches/5.2.x/core/units/scheduled_tasks/scheduled_task_eh.php (revision 16862)
@@ -1,303 +1,304 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class ScheduledTaskEventHandler extends kDBEventHandler {
/**
* Allows to override standard permission mapping
*
* @return void
* @access protected
* @see kEventHandler::$permMapping
*/
protected function mapPermissions()
{
parent::mapPermissions();
$permissions = Array (
'OnMassCancel' => Array ('self' => 'add|edit'),
'OnRun' => Array ('self' => 'add|edit'),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
* Does custom validation
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnBeforeItemValidate(kEvent $event)
{
parent::OnBeforeItemValidate($event);
/** @var kDBItem $object */
$object = $event->getObject();
$event_string = $object->GetDBField('Event');
if ( !$event_string ) {
return;
}
try {
$this->Application->eventImplemented(new kEvent($event_string));
}
catch (Exception $e) {
$object->SetError('Event', 'invalid_event', '+' . $e->getMessage());
}
}
/**
* [HOOK] Refreshes scheduled task list in database based on cached data from unit configs
*
* @param kEvent $event
*/
function OnRefresh($event)
{
$scheduled_tasks_from_cache = $this->Application->EventManager->getScheduledTasks(true);
- /** @var kDBItem $object */
- $object = $event->getObject( Array ('skip_autoload' => true) );
+ /** @var ScheduledTaskItem $object */
+ $object = $event->getObject(array('skip_autoload' => true));
+ $object->removeAbsentDatabaseColumns();
$processed_ids = Array ();
$scheduled_tasks_from_db = $this->Conn->Query($object->GetSelectSQL(), 'Name');
/** @var kCronHelper $cron_helper */
$cron_helper = $this->Application->recallObject('kCronHelper');
foreach ($scheduled_tasks_from_cache as $scheduled_task_name => $scheduled_task_params) {
if ( !isset($scheduled_tasks_from_db[$scheduled_task_name]) ) {
$fields_hash = Array (
'Event' => $scheduled_task_params['Event'],
'Name' => $scheduled_task_name,
'Type' => ScheduledTask::TYPE_SYSTEM,
'Status' => isset($scheduled_task_params['Status']) ? $scheduled_task_params['Status'] : STATUS_ACTIVE,
'RunSchedule' => $scheduled_task_params['RunSchedule'],
);
$object->Clear();
$object->SetDBFieldsFromHash($fields_hash);
$cron_helper->load($object, 'RunSchedule');
$object->Create();
}
else {
$object->LoadFromHash( $scheduled_tasks_from_db[$scheduled_task_name] );
}
$processed_ids[] = $object->GetID();
}
// delete all non-processed scheduled tasks (ones, that were deleted from unit configs)
$sql = 'SELECT ' . $object->IDField . '
FROM ' . $object->TableName . '
WHERE (Type = ' . ScheduledTask::TYPE_SYSTEM . ') AND (' . $object->IDField . ' NOT IN (' . implode(',', $processed_ids) . '))';
$delete_ids = $this->Conn->GetCol($sql);
if ($delete_ids) {
/** @var kTempTablesHandler $temp_handler */
$temp_handler = $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
$temp_handler->DeleteItems($event->Prefix, $event->Special, $delete_ids);
}
$this->Application->removeObject($event->getPrefixSpecial());
}
/**
* Don't allow to delete other user's messages
*
* @param kEvent $event
* @param string $type
* @return void
* @access protected
*/
protected function customProcessing(kEvent $event, $type)
{
if ( $event->Name == 'OnMassDelete' && $type == 'before' ) {
if ( $this->Application->isDebugMode() ) {
// allow to delete system scheduled tasks in debug mode
return;
}
$ids = $event->getEventParam('ids');
if ( $ids ) {
$id_field = $this->Application->getUnitOption($event->Prefix, 'IDField');
$table_name = $this->Application->getUnitOption($event->Prefix, 'TableName');
$sql = 'SELECT ' . $id_field . '
FROM ' . $table_name . '
WHERE ' . $id_field . ' IN (' . implode(',', $ids) . ') AND Type <> ' . ScheduledTask::TYPE_SYSTEM;
$event->setEventParam('ids', $this->Conn->GetCol($sql));
}
}
}
/**
* Cancels scheduled tasks, that are currently running
*
* @param kEvent $event
*/
function OnMassCancel($event)
{
$ids = $this->StoreSelectedIDs($event);
if ($ids) {
/** @var kDBItem $object */
$object = $event->getObject( Array ('skip_autoload' => true) );
foreach ($ids as $id) {
$object->Load($id);
if ($object->GetDBField('LastRunStatus') == ScheduledTask::LAST_RUN_RUNNING) {
// only changes status, doesn't affect currency running scheduled tasks
$object->SetDBField('LastRunStatus', ScheduledTask::LAST_RUN_FAILED);
$object->Update();
}
}
}
$this->clearSelectedIDs($event);
}
/**
* Runs selected scheduled tasks
*
* @param kEvent $event
*/
function OnRun($event)
{
$ids = $this->StoreSelectedIDs($event);
if ($ids) {
/** @var kDBItem $object */
$object = $event->getObject( Array ('skip_autoload' => true) );
$where_clause = Array (
$object->TableName . '.' . $object->IDField . ' IN (' . implode(',', $ids) . ')',
$object->TableName . '.Status = ' . STATUS_ACTIVE,
$object->TableName . '.LastRunStatus <> ' . ScheduledTask::LAST_RUN_RUNNING,
);
$sql = $object->GetSelectSQL() . '
WHERE (' . implode(') AND (', $where_clause) . ')';
$scheduled_tasks = $this->Conn->Query($sql);
foreach ($scheduled_tasks as $scheduled_task_data) {
$scheduled_task_data['EventName'] = $scheduled_task_data['Event'];
$this->Application->EventManager->runScheduledTask($scheduled_task_data);
}
}
$this->clearSelectedIDs($event);
}
/**
* Loads schedule from database to virtual fields
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnAfterItemLoad(kEvent $event)
{
parent::OnAfterItemLoad($event);
/** @var kDBItem $object */
$object = $event->getObject();
/** @var kCronHelper $cron_helper */
$cron_helper = $this->Application->recallObject('kCronHelper');
$cron_helper->load($object, 'RunSchedule');
}
/**
* Validates schedule
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnBeforeItemCreate(kEvent $event)
{
parent::OnBeforeItemCreate($event);
$this->_itemChanged($event);
}
/**
* Validates schedule
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnBeforeItemUpdate(kEvent $event)
{
parent::OnBeforeItemUpdate($event);
$this->_itemChanged($event);
}
/**
* Validates schedule
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function _itemChanged(kEvent $event)
{
/** @var kDBItem $object */
$object = $event->getObject();
/** @var kCronHelper $cron_helper */
$cron_helper = $this->Application->recallObject('kCronHelper');
if ( $cron_helper->validateAndSave($object, 'RunSchedule') && !$object->GetDBField('NextRunOn_date') ) {
$next_run = $cron_helper->getMatch($object->GetDBField('RunSchedule'));
$object->SetDBField('NextRunOn_date', $next_run);
$object->SetDBField('NextRunOn_time', $next_run);
}
}
/**
* Creates schedule fields
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnAfterConfigRead(kEvent $event)
{
parent::OnAfterConfigRead($event);
// Don't initialize 'scheduled-task' unit, because scheduled tasks are not run during install.
if ( defined('IS_INSTALL') && IS_INSTALL ) {
return;
}
/** @var kCronHelper $cron_helper */
$cron_helper = $this->Application->recallObject('kCronHelper');
$cron_helper->initUnit($event->Prefix, 'RunSchedule');
}
}
Index: branches/5.2.x/core/units/scheduled_tasks/scheduled_task_item.php
===================================================================
--- branches/5.2.x/core/units/scheduled_tasks/scheduled_task_item.php (nonexistent)
+++ branches/5.2.x/core/units/scheduled_tasks/scheduled_task_item.php (revision 16862)
@@ -0,0 +1,21 @@
+<?php
+/**
+* @version $Id$
+* @package In-Portal
+* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
+* @license GNU/GPL
+* In-Portal is Open Source software.
+* This means that this software may have been modified pursuant
+* the GNU General Public License, and as distributed it includes
+* or is derivative of works licensed under the GNU General Public License
+* or other free or open source software licenses.
+* See http://www.in-portal.org/license for copyright notices and details.
+*/
+
+defined('FULL_PATH') or die('restricted access!');
+
+
+class ScheduledTaskItem extends kDBItem
+{
+ use TDatabaseTableValidatorItemFeature;
+}
Property changes on: branches/5.2.x/core/units/scheduled_tasks/scheduled_task_item.php
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+LF
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Index: branches/5.2.x/core/units/scheduled_tasks/scheduled_tasks_config.php
===================================================================
--- branches/5.2.x/core/units/scheduled_tasks/scheduled_tasks_config.php (revision 16861)
+++ branches/5.2.x/core/units/scheduled_tasks/scheduled_tasks_config.php (revision 16862)
@@ -1,172 +1,172 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
$config = Array (
'Prefix' => 'scheduled-task',
- 'ItemClass' => Array ('class' => 'kDBItem', 'file' => '', 'build_event' => 'OnItemBuild'),
+ 'ItemClass' => Array ('class' => 'ScheduledTaskItem', 'file' => 'scheduled_task_item.php', 'build_event' => 'OnItemBuild'),
'ListClass' => Array ('class' => 'kDBList', 'file' => '', 'build_event' => 'OnListBuild'),
'EventHandlerClass' => Array ('class' => 'ScheduledTaskEventHandler', 'file' => 'scheduled_task_eh.php', 'build_event' => 'OnBuild'),
'TagProcessorClass' => Array ('class' => 'kDBTagProcessor', 'file' => '', 'build_event' => 'OnBuild'),
'AutoLoad' => true,
'QueryString' => Array (
1 => 'id',
2 => 'Page',
3 => 'PerPage',
4 => 'event',
5 => 'mode',
),
'Hooks' => Array (
Array (
'Mode' => hAFTER,
'Conditional' => false,
'HookToPrefix' => 'adm',
'HookToSpecial' => '*',
'HookToEvent' => Array ('OnAfterCacheRebuild'),
'DoPrefix' => '',
'DoSpecial' => '*',
'DoEvent' => 'OnRefresh',
),
),
'IDField' => 'ScheduledTaskId',
'TableName' => TABLE_PREFIX . 'ScheduledTasks',
'TitleField' => 'Name',
'StatusField' => Array ('Status'),
'TitlePresets' => Array (
'default' => Array (
'new_status_labels' => Array ('scheduled-task' => '!la_title_AddingScheduledTask!'),
'edit_status_labels' => Array ('scheduled-task' => '!la_title_EditingScheduledTask!'),
'new_titlefield' => Array ('scheduled-task' => '!la_title_NewScheduledTask!'),
),
'scheduled_task_list' => Array (
'prefixes' => Array ('scheduled-task_List'), 'format' => "!la_title_ScheduledTasks!",
'toolbar_buttons' => Array ('new_item', 'edit', 'delete', 'approve', 'decline', 'process', 'cancel', 'view', 'dbl-click'),
),
'scheduled_task_edit' => Array (
'prefixes' => Array ('scheduled-task'), 'format' => "#scheduled-task_status# '#scheduled-task_titlefield#'",
'toolbar_buttons' => Array ('select', 'cancel', 'reset_edit', 'prev', 'next'),
),
),
'PermSection' => Array ('main' => 'in-portal:scheduled_tasks'),
'Sections' => Array (
'in-portal:scheduled_tasks' => Array (
'parent' => 'in-portal:website_setting_folder',
'icon' => 'conf_agents',
'label' => 'la_title_ScheduledTasks',
'url' => Array ('t' => 'scheduled_tasks/scheduled_task_list', 'pass' => 'm'),
'permissions' => Array ('view', 'add', 'edit', 'delete'),
'priority' => 6,
'type' => stTREE,
),
),
'ListSQLs' => Array (
'' => ' SELECT %1$s.* %2$s
FROM %1$s',
),
'ListSortings' => Array (
'' => Array (
'Sorting' => Array ('Name' => 'asc'),
)
),
'Fields' => Array (
'ScheduledTaskId' => Array ('type' => 'int', 'not_null' => 1, 'default' => 0),
'Name' => Array (
'type' => 'string', 'max_len' => 255,
'unique' => Array (),
'required' => 1, 'not_null' => 1, 'default' => ''
),
'Type' => Array (
'type' => 'int',
'formatter' => 'kOptionsFormatter', 'options' => Array (1 => 'la_opt_User', 2 => 'la_opt_System'), 'use_phrases' => 1,
'required' => 1, 'not_null' => 1, 'default' => 1
),
'Status' => Array (
'type' => 'int',
'formatter' => 'kOptionsFormatter', 'options' => Array (1 => 'la_opt_Active', 0 => 'la_opt_Disabled'), 'use_phrases' => 1,
'required' => 1, 'not_null' => 1, 'default' => 1
),
'Event' => Array (
'type' => 'string', 'max_len' => 255,
'required' => 1, 'not_null' => 1, 'default' => ''
),
'RunSchedule' => Array ('type' => 'string', 'max_len' => 255, 'not_null' => 1, 'default' => '* * * * *'),
'LastRunOn' => Array ('type' => 'int', 'formatter' => 'kDateFormatter', 'default' => NULL),
'LastRunStatus' => Array (
'type' => 'int',
'formatter' => 'kOptionsFormatter', 'options' => Array (1 => 'la_opt_Success', 0 => 'la_opt_Failed', 2 => 'la_opt_Running'), 'use_phrases' => 1,
'not_null' => 1, 'default' => 1
),
'NextRunOn' => Array ('type' => 'int', 'formatter' => 'kDateFormatter', 'default' => NULL),
'RunTime' => Array (
'type' => 'int',
'formatter' => 'kDateFormatter', 'date_format' => '', 'input_date_format' => '', 'time_format' => 'H:i:s', 'use_timezone' => 0,
'not_null' => 1, 'default' => 0
),
'Timeout' => Array (
'type' => 'int',
'min_value_inc' => 1,
'not_null' => 1, 'default' => NULL
),
'LastTimeoutOn' => Array ('type' => 'int', 'formatter' => 'kDateFormatter', 'default' => NULL),
'SiteDomainLimitation' => Array (
'type' => 'string', 'max_len' => 255,
'formatter' => 'kOptionsFormatter', 'options_sql' => 'SELECT %s FROM ' . TABLE_PREFIX . 'SiteDomains ORDER BY DomainName ASC', 'option_key_field' => 'DomainId', 'option_title_field' => 'DomainName', 'multiple' => 1,
'not_null' => 1, 'default' => ''
),
),
'Grids' => Array (
'Default' => Array (
'Icons' => Array (
'default' => 'icon16_item.png',
0 => 'icon16_disabled.png',
),
'Fields' => Array (
'ScheduledTaskId' => Array ('title' => 'column:la_fld_Id', 'filter_block' => 'grid_range_filter', 'width' => 80),
'Name' => Array ('filter_block' => 'grid_like_filter', 'width' => 200),
'Type' => Array ('filter_block' => 'grid_options_filter', 'width' => 60),
'Event' => Array ('filter_block' => 'grid_like_filter', 'width' => 280),
'RunSchedule' => Array ('filter_block' => 'grid_range_filter', 'width' => 100),
'LastRunOn' => Array ('filter_block' => 'grid_date_range_filter', 'width' => 145),
'RunTime' => Array ('data_block' => 'grid_runtime_td', 'filter_block' => 'grid_range_filter', 'width' => 100),
'LastRunStatus' => Array ('filter_block' => 'grid_options_filter', 'width' => 90),
'NextRunOn' => Array ('filter_block' => 'grid_date_range_filter', 'width' => 145),
'Status' => Array ('filter_block' => 'grid_options_filter', 'width' => 65),
'Timeout' => Array ('filter_block' => 'grid_range_filter', 'width' => 85),
'LastTimeoutOn' => Array ('filter_block' => 'grid_date_range_filter', 'width' => 145),
'SiteDomainLimitation' => Array ('data_block' => 'grid_picker_td', 'filter_block' => 'grid_multioptions_filter', 'separator' => ', ', 'width' => 145),
),
),
),
);
Index: branches/5.2.x/core/units/traits/TDatabaseTableValidatorItemFeature.php
===================================================================
--- branches/5.2.x/core/units/traits/TDatabaseTableValidatorItemFeature.php (nonexistent)
+++ branches/5.2.x/core/units/traits/TDatabaseTableValidatorItemFeature.php (revision 16862)
@@ -0,0 +1,49 @@
+<?php
+/**
+ * @version $Id$
+ * @package In-Portal
+ * @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
+ * @license GNU/GPL
+ * In-Portal is Open Source software.
+ * This means that this software may have been modified pursuant
+ * the GNU General Public License, and as distributed it includes
+ * or is derivative of works licensed under the GNU General Public License
+ * or other free or open source software licenses.
+ * See http://www.in-portal.org/license for copyright notices and details.
+ */
+
+/**
+ * Allows to remove absent database columns from an already constructed object.
+ *
+ * @mixin kDBItem
+ */
+trait TDatabaseTableValidatorItemFeature
+{
+
+ /**
+ * Removes absent database columns from the object.
+ *
+ * @return void
+ */
+ public function removeAbsentDatabaseColumns()
+ {
+ $sql = 'DESCRIBE ' . $this->TableName;
+ $actual_db_fields = $this->Conn->Query($sql, 'Field');
+ $declared_db_fields = array_diff_key($this->getFields(), $this->getVirtualFields());
+
+ $absent_db_fields = array_keys(array_diff_key($declared_db_fields, $actual_db_fields));
+
+ if ( !$absent_db_fields ) {
+ return;
+ }
+
+ $fields = $this->getFields();
+
+ foreach ( $absent_db_fields as $field ) {
+ unset($fields[$field]);
+ }
+
+ $this->setFields($fields);
+ }
+
+}
Property changes on: branches/5.2.x/core/units/traits/TDatabaseTableValidatorItemFeature.php
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+LF
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Index: branches/5.2.x/core/units/traits/traits_config.php
===================================================================
--- branches/5.2.x/core/units/traits/traits_config.php (nonexistent)
+++ branches/5.2.x/core/units/traits/traits_config.php (revision 16862)
@@ -0,0 +1,25 @@
+<?php
+/**
+ * @version $Id$
+ * @package In-Portal
+ * @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
+ * @license GNU/GPL
+ * In-Portal is Open Source software.
+ * This means that this software may have been modified pursuant
+ * the GNU General Public License, and as distributed it includes
+ * or is derivative of works licensed under the GNU General Public License
+ * or other free or open source software licenses.
+ * See http://www.in-portal.org/license for copyright notices and details.
+ */
+
+defined('FULL_PATH') or die('restricted access!');
+
+$config = array(
+ 'Prefix' => 'core-traits',
+ 'EventHandlerClass' => array('class' => 'kEventHandler', 'file' => '', 'build_event' => 'OnBuild'),
+ 'TagProcessorClass' => array('class' => 'kTagProcessor', 'file' => '', 'build_event' => 'OnBuild'),
+
+ 'RegisterClasses' => array(
+ array('pseudo' => 'TDatabaseTableValidatorItemFeature', 'class' => 'TDatabaseTableValidatorItemFeature', 'file' => 'TDatabaseTableValidatorItemFeature.php', 'build_event' => ''),
+ ),
+);
Property changes on: branches/5.2.x/core/units/traits/traits_config.php
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+LF
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property

Event Timeline