Commit 38432eb4 authored by Côme Chilliet's avatar Côme Chilliet
Browse files

Fixes #4364 Removed DaemonEvent, added clean argonautAction class instead

parent bf62227b
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2006 Fabian Hickert
Copyright (C) 2011-2015 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
class TimeHisAttribute extends CompositeAttribute
{
function __construct($label, $description, $ldapName, $required, $acl = '')
{
$attributes = array(
new IntAttribute (
'', _('Hours'),
$ldapName.'_hours', TRUE,
0, 23, 1
),
new IntAttribute (
':', _('Minutes'),
$ldapName.'_minutes', TRUE,
0, 59, 0
),
new IntAttribute (
':', _('Seconds'),
$ldapName.'_seconds', TRUE,
0, 59, 0
)
);
parent::__construct($description, $ldapName, $attributes, '/^(\d\d)(\d\d)(\d\d)$/', '%2d%2d%2d', $acl, $label);
$this->setLinearRendering(TRUE);
}
}
class DateTimeAttribute extends CompositeAttribute
{
function __construct($label, $description, $ldapName, $required, $acl = '')
{
$attributes = array(
new DateAttribute(
_('Date'), '',
$ldapName.'_date', $required,
'Ymd'
),
new TimeHisAttribute(
_('Time'), '',
$ldapName.'_time', $required
)
);
parent::__construct($description, $ldapName, $attributes, '/^(\d{8})(\d{6})$/', '%s%s', $acl, $label);
}
}
class MacsAttribute extends GenericDialogAttribute
{
protected $dialogClass = 'SystemSelectDialog';
function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = array(), $display_attr = 'cn', $acl = '')
{
parent::__construct ($label, $description, $ldapName, $required, $defaultValue, 'macAddress', $display_attr, $acl);
}
}
class argonautAction extends simplePlugin
{
static function plInfo()
{
return array(
'plShortName' => _('Argonaut task'),
'plDescription' => _('Schedule an argonaut task'),
'plObjectType' => array('argonautTask' => array(
'name' => _('Argonaut task'),
'aclCategory' => 'argonautQueue',
'icon' => 'geticon.php?context=applications&icon=argonaut&size=16',
)),
'plProvidedAcls' => parent::generatePlProvidedAcls(self::getAttributesInfo())
);
}
static function getAttributesInfo ()
{
return array(
'main' => array(
'name' => _('Schedule'),
'attrs' => array(
new HiddenAttribute(
'action'
),
new HiddenAttribute(
'scheduled', FALSE,
TRUE
),
new DateTimeAttribute(
_('Schedule'), '',
'datetime', FALSE
),
new BooleanAttribute(
_('Periodical job'), _('Wether this job should repeat in time'),
'periodical', FALSE
),
new CompositeAttribute(
_('How often this task should be repeated'), 'period',
array(
new IntAttribute(
'', '',
'periodValue', FALSE,
1, FALSE, 7
),
new SelectAttribute(
'', '',
'periodType', TRUE,
array('minutes', 'hours', 'days', 'weeks', 'months'), 'days',
array(_('Minutes'), _('Hours'), _('Days'), _('Weeks'), _('Months'))
)
),
'/^(\d+)_(minutes|hours|days|weeks|months)$/',
'%s_%s',
'',
_('Period')
)
)
),
'targets' => array(
'name' => _('Targets'),
'attrs' => array(
new MacsAttribute(
'', _('Targets for this task'),
'targets', TRUE
)
)
)
);
}
public function __construct($config, $action)
{
parent::__construct($config, $config->current['BASE']);
$this->attributesAccess['scheduled']->setManagedAttributes(
array(
'erase' => array (
FALSE => array (
'datetime', 'period'
)
)
)
);
$this->attributesAccess['periodical']->setManagedAttributes(
array(
'disable' => array (
FALSE => array (
'period'
)
)
)
);
$this->attributesAccess['period']->setLinearRendering(TRUE);
$this->datetime = date('YmdHis');
$this->action = $action;
}
protected function loadAttributes()
{
foreach ($this->attributesAccess as &$attribute) {
$attribute->setInLdap(FALSE);
}
unset($attribute);
parent::loadAttributes();
}
function execute()
{
return parent::execute().$this->getFooter();
}
/*! \brief Returns the plugin footer (save cancel), displayed in the template.
@return String HTML footer part.
*/
protected function getFooter()
{
if (!$this->displayPlugin) {
return '';
}
return '<p class="plugbottom">'.
' <input type="submit" name="save_event_dialog" value="'.msgPool::saveButton().'"/>'.
' <input type="submit" name="abort_event_dialog" value="'.msgPool::cancelButton().'"/>'.
'</p>';
}
/*! brief Returns an array containing all attributes \
That should be written.
@return Array e.g. 'status' => 'bla blub'
*/
public function computeData()
{
timezone::get_default_timezone();
$ret = array('args' => array());
if ($this->scheduled) {
$ret['timestamp'] = strtotime($this->datetime);
if ($this->periodical) {
$ret['periodic'] = $this->period;
}
}
return $ret;
}
/*! \brief Returns event information, like menu strings, images ...
@return Array Event informations.
*/
static public function get_event_info($action)
{
$events = self::get_event_types();
if (isset($events[$action])) {
$infos = $events[$action];
$infos['menuimg'] = '<img src="'.$infos['img'].'" alt="'.$infos['name'].'" class="center"/>';
$infos['listimg'] = '<img src="'.$infos['img'].'" title="'.$infos['name'].
'"alt="'.$infos['name'].'" class="center"/>';
return $infos;
} else {
return FALSE;
}
}
/*! \brief Returns a complete list of all available events.
@return Array Containing info for all available events.
*/
static public function get_event_types()
{
return array(
'System.halt' => array(
'name' => _('Switch off'),
'img' => 'geticon.php?context=actions&icon=system-shutdown&size=16'
),
'Deployment.reboot' => array(
'name' => _('Reboot'),
'img' => 'geticon.php?context=actions&icon=system-reboot&size=16'
),
'Deployment.wake' => array(
'name' => _('Wake up'),
'img' => 'geticon.php?context=status&icon=task-running&size=16'
),
'Deployment.update' => array(
'name' => _('Software update'),
'img' => 'geticon.php?context=actions&icon=system-update&size=16'
),
'Deployment.reinstall' => array(
'name' => _('(Re)Install'),
'img' => 'geticon.php?context=actions&icon=system-reinstall&size=16'
),
);
}
}
?>
......@@ -86,7 +86,7 @@ class argonautImportFile extends simplePlugin
$this->parent = $parent;
$this->events = array();
$this->daemon_events = DaemonEvent::get_event_types();
$this->daemon_events = argonautAction::get_event_types();
}
function execute()
......@@ -129,15 +129,12 @@ class argonautImportFile extends simplePlugin
foreach ($events as $key => $event) {
/* Create event */
$object = new DaemonEvent($this->config, $event['HEADER'], $event);
$object->add_targets(array($event['MAC']));
if (empty($event['TIMESTAMP'])) {
$object->set_type(TRIGGERED_EVENT);
$data = array();
} else {
$object->set_type(SCHEDULED_EVENT);
$data = array('timestamp' => $event['TIMESTAMP']);
}
$this->parent->o_queue->append($object);
$this->parent->o_queue->append_call($event['HEADER'], array($event['MAC']), $data);
if ($this->parent->o_queue->is_error()) {
msg_dialog::display(_('Infrastructure service'), msgPool::siError($this->parent->o_queue->get_error()), ERROR_DIALOG);
$fail++;
......
......@@ -103,7 +103,7 @@ class argonautQueue extends simpleManagement
$task = $tmp['EVENT'];
/* Create a printable job name/description */
$infos = DaemonEvent::get_event_info($task['HEADERTAG']);
$infos = argonautAction::get_event_info($task['HEADERTAG']);
if ($infos) {
$j_name = $task['ID']." - ".$infos['name']."&nbsp;".$task['MACADDRESS'];
} else {
......@@ -130,7 +130,7 @@ class argonautQueue extends simpleManagement
foreach ($this->dns as $dn) {
$tmp = $this->headpage->getEntry($dn);
$task = $tmp['EVENT'];
$infos = DaemonEvent::get_event_info($task['HEADERTAG']);
$infos = argonautAction::get_event_info($task['HEADERTAG']);
if ($infos) {
$objects[] = array(
'name' => $infos['name'],
......@@ -183,10 +183,7 @@ class argonautQueue extends simpleManagement
$taskids[] = $tmp['EVENT']['ID'];
}
$tmp = new DaemonEvent($this->config, 'wakeup');
$tmp->add_targets($macs);
$tmp->set_type(TRIGGERED_EVENT);
$this->o_queue->append($tmp);
$this->o_queue->append_call('wakeup', $macs, array());
if ($this->o_queue->is_error()) {
msg_dialog::display(_('Info'), sprintf(_('%s'), $this->o_queue->get_error()), INFO_DIALOG);
}
......@@ -293,7 +290,7 @@ class argonautQueue extends simpleManagement
/* Check if this event exists as Daemon class
* In this case, display a more accurate entry.
*/
$infos = DaemonEvent::get_event_info($tag);
$infos = argonautAction::get_event_info($tag);
if ($infos) {
$str = $infos['name'];
......
......@@ -27,7 +27,7 @@ class filterArgonautEvents extends filterLDAP
global $config;
$o_queue = new supportDaemon();
$events = DaemonEvent::get_event_types();
$events = argonautAction::get_event_types();
/* Get tags that will be used in queue searches */
$event_tags = array("none");
......
{if $is_new}
<table style='width:100%;'>
<tr>
<td style='width:50%; vertical-align:top; border-right:1px solid #AAA'>
<table>
<tr>
<td style='vertical-align:top'><b>{t}Schedule{/t}</b><br><br>
{$timestamp}</td>
</tr>
</table>
</td>
<td style='width:50%; vertical-align:top;'>
<table style='width:100%;'>
<tr>
<td>
<b>{t}System list{/t}</b>
<br>
{$target_list}
</td>
</tr>
</table>
</td>
</tr>
</table>
{else}
<table style='width:100%;'>
<tr>
<td style='width:50%; vertical-align:top;'>
<table>
<tr>
<td>{t}ID{/t}</td>
<td>{$data.ID}</td>
</tr>
<tr>
<td>{t}Status{/t}</td>
<td>{$data.STATUS}</td>
</tr>
<tr>
<td>{t}Result{/t}</td>
<td>{$data.RESULT}</td>
</tr>
<tr>
<td>{t}Target{/t}</td>
<td>{$data.MACADDRESS}</td>
</tr>
<tr>
<td style='vertical-align:top'>{t}Timestamp{/t}</td>
<td>{$timestamp}</td>
</tr>
</table>
</td>
<td style='width:50%; vertical-align:top;'>
<table >
</table>
</td>
</tr>
</table>
{/if}
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2006 Fabian Hickert
Copyright (C) 2011-2015 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
define("SCHEDULED_EVENT", 1);
define("TRIGGERED_EVENT", 2);
/*! \brief This is the event base class
\author Fabian Hickert <hickert@gonicus.de>
\version 1.00
\date 26.02.2008
This is the base class for all new daemon events.
It implements most of the required functionality.
*/
class DaemonEvent
{
/* Menu Strings */
protected $s_Menu_Name = "s_Menu_Name not set"; // Diplayed in the ActionsMenu->CreateNew
protected $s_Menu_Image = "images/empty.png"; // Image displayed in Actions->New
protected $s_List_Image = ""; // Image displayed in event listing
public $config; // FusionDirectory configuration object
protected $data; // The event data, when edited
protected $a_targets = array(); // The list of assigned Targets (When newly created)
protected $s_Action = ""; // The deamon command name when edited. (e.g. trigger_action_halt)
protected $timestamp = 0; // Event execution time;
protected $id = -1; // The Table ID
protected $status = "unknown";// The current event status
protected $is_new = TRUE; // Is TRUE if this is a new event
protected $mode = SCHEDULED_EVENT; // Default action is sheduled.
/* Sub dialog hanlding */
protected $target_divlist = NULL; // The divselectbox used by the target add dialog
protected $target_add_list_used = FALSE; // Indicates that the target add list was used.
protected $time_select_used = FALSE; // Indicates that we have used the timestamp select boxes.
protected $target_list_used = FALSE; // Target list was diaplayed?
protected $_target_list = array(); // Object Cache of those objects displayed in the target add dialog
protected $workstation_list = array(); // Used as cache in the target list.
protected $server_list = array(); // Used as cache in the target list.
protected $periodType = "days";
protected $periodValue = 7;
protected $activate_periodical_job = FALSE;
protected $attributes = array("timestamp");
function set_type($type)
{
$this->mode = $type;
}
function get_type()
{
return $this->mode;
}
/*! \brief Class contructor.
@param Array FusionDirectory configuration object.
@param Array Event data, only given when edited.
*/
public function __construct($config, $action, $data = array())
{
$this->data = $data;
$this->config = $config;
$this->s_Action = $action;
timezone::get_default_timezone();
$this->timestamp = 0;
/* Load values from given data */
if (isset($data['ID'])) {
$this->id = $data['ID'];
$this->is_new = FALSE;
}
if (isset($data['TIMESTAMP'])) {
$this->timestamp = $this->_event_to_timestamp($data['TIMESTAMP']);
}
if (isset($data['PERIODIC']) && !preg_match("/none/i", $data['PERIODIC'])) {
$tmp = explode("_", $data['PERIODIC']);
if (count($tmp) == 2) {
$this->activate_periodical_job = TRUE;
$this->periodValue = $tmp[0];
$this->periodType = $tmp[1];
}
}
}
/*! \brief Create the HTML output for the plugin.
@return String The generated HTML output.
*/
public function execute()
{
$this->time_select_used = FALSE;
$this->target_list_used = FALSE;
$display = $this->get_header();
$tmp = $this->data;
/* Check if target add dialog is open */
if ($this->is_target_list_open() && $this->is_new) {
return $this->get_target_add_list();
}
$smarty = get_smarty();
$smarty->assign("data", $this->data);
$smarty->assign("target_list", $this->get_target_list());
$smarty->assign("is_new", $this->is_new);
$smarty->assign("timestamp", $this->get_time_select());
$display .= $smarty->fetch(get_template_path('DaemonEvent.tpl', TRUE, dirname(__FILE__)));
$display .= $this->get_footer();
return $display;
}
/*! \brief Returns the plugin header, displayed in the template.
@return String HTML header part.
*/
public function get_header()
{
if ($this->target_add_list_used) {
return "";
}
$events = self::get_event_types();
$str = "<h2>".sprintf(_("Create '%s' job"), $events[$this->s_Action]['name'])."</h2>";
return $str;
}
/*! \brief Returns the plugin footer (save cancel), displayed in the template.
@return String HTML footer part.
*/
public function get_footer()
{
if ($this->target_add_list_used) {
return "";
}
$str = "<p class='seperator'></p>";
$str .= "<div style='text-align:right;width:99%;padding:5px;'>
<input type='submit' name='save_event_dialog' value='".msgPool::saveButton()."'>&nbsp;
<input type='submit' name='abort_event_dialog' value='".msgPool::cancelButton()."'>
</div>";
return $str;
}
/*! \brief Returns HTML representation of a timestamp using <select> boxes.
@return Returns HTML content.
*/
public function get_time_select()
{
timezone::get_default_timezone();
$this->time_select_used = TRUE;
$smarty = get_smarty();
$year = date("Y", $this->timestamp);
$month = date("m", $this->timestamp);
$day = date("d", $this->timestamp);
$hour = date("H", $this->timestamp);
$minute = date("i", $this->timestamp);
$second = date("s", $this->timestamp);
$years = array();
for ($i = date("Y", time()); $i <= 2037; $i++) {
$years[$i] = $i;