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

Fixes #3997 Migrated argonautImportFile to simplePlugin

parent c5a6304c
<h2>{t}Import jobs{/t}</h2>
<p>
{t}You can import a list of jobs into the FusionDirectory job queue. This should be a semicolon seperated list of items in the following format:{/t}
</p>
<i>{t}timestamp{/t};{t}MAC-address{/t};{t}job type{/t};{t}object group{/t}[;{t}import base{/t};{t}full hostname{/t};{t}IP-address{/t};{t}DHCP group{/t} ]</i>
<br>
<br>
{if !$count}
{t}Example{/t}:
<br>
20080626162556<b>;</b>00:0C:29:99:1E:37<b>;</b>job_trigger_activate_new<b>;</b>goto-client<b>;</b>dc=test,dc=fusiondirectory,dc=org
<br>
<br>
{/if}
<p class="seperator"></p>
&nbsp;
<table>
<tr>
<td>
{t}Select list to import{/t}
</td>
<td>
<input type='file' name='file' value="{t}Browse{/t}">
<input type='submit' name='import' value='{t}Upload{/t}'>
</td>
</tr>
</table>
{if $count}
<p class="seperator">&nbsp;</p>
<br>
<br>
<div style='width:100%; height:300px; overflow: scroll;'>
<table style='width:100%; background-color: #CCCCCC; border: solid 1px #CCCCCC;'>
<tr>
<td><b>{t}Timestamp{/t}</b></td>
<td><b>{t}MAC{/t}</b></td>
<td><b>{t}Event{/t}</b></td>
<td><b>{t}Object group{/t}</b></td>
<td><b>{t}Base{/t}</b></td>
<td><b>{t}FQDN{/t}</b></td>
<td><b>{t}IP{/t}</b></td>
<td><b>{t}DHCP{/t}</b></td>
</tr>
{foreach from=$info item=item key=key}
{if $item.ERROR}
<tr style='background-color: #F0BBBB;'>
<td>{$item.TIMESTAMP}</td>
<td>{$item.MAC}</td>
<td>{$item.HEADER}</td>
<td>{$item.OGROUP}</td>
<td>{$item.BASE}</td>
<td>{$item.FQDN}</td>
<td>{$item.IP}</td>
<td>{$item.DHCP}</td>
</tr>
<tr style='background-color: #F0BBBB;'>
<td colspan="7"><b>{$item.ERROR}</b></td>
</tr>
{else}
{if ($key % 2)}
<tr class="rowxp0">
{else}
<tr class="rowxp1">
{/if}
<td>{$item.TIMESTAMP}</td>
<td style='border-left: solid 1px #BBBBBB;'>{$item.MAC}</td>
<td style='border-left: solid 1px #BBBBBB;'>{$item.HEADER}</td>
<td style='border-left: solid 1px #BBBBBB;'>{$item.OGROUP}</td>
<td style='border-left: solid 1px #BBBBBB;'>{$item.BASE}</td>
<td style='border-left: solid 1px #BBBBBB;'>{$item.FQDN}</td>
<td style='border-left: solid 1px #BBBBBB;'>{$item.IP}</td>
<td style='border-left: solid 1px #BBBBBB;'>{$item.DHCP}</td>
</tr>
{/if}
{/foreach}
</table>
</div>
{/if}
<br>
<p class='seperator'></p>
<div style='text-align:right;width:99%; padding-right:5px; padding-top:5px;'>
<input type='submit' name='start_import' value='{t}Import{/t}' >&nbsp;
<input type='submit' name='import_abort' value='{msgPool type=backButton}'>
</div>
<br>
......@@ -19,101 +19,117 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
class argonautImportFile extends plugin
class argonautImportFile extends simplePlugin
{
var $events = array();
var $csv_fields = array();
var $import_successful = FALSE; // Indicates that we have successfully imported everything.
public function __construct($config, &$parent)
protected $csv_fields = array(
'0' => 'TIMESTAMP',
'1' => 'MAC',
'2' => 'HEADER',
'3' => 'OGROUP'
);
static function plInfo()
{
parent::__construct($config, NULL);
$this->parent = $parent;
$this->daemon_events = DaemonEvent::get_event_types();
$this->csv_fields = array(
"0" => "TIMESTAMP","1" => "MAC", "2" => "HEADER", "3" => "OGROUP",
"4" => "BASE", "5" => "FQDN", "6" => "IP", "7" => "DHCP");
return array(
'plShortName' => _('Argonaut task import'),
'plDescription' => _('Imports argonaut tasks from CSV file'),
'plObjectType' => array('argonautQueueImport' => array(
'name' => _('Argonaut task import'),
'aclCategory' => 'argonautQueue',
'icon' => 'geticon.php?context=applications&icon=argonaut&size=16',
)),
'plProvidedAcls' => parent::generatePlProvidedAcls(self::getAttributesInfo())
);
}
private function parse_csv($str)
static function getAttributesInfo ()
{
/* Some file checks */
$lines = preg_split("/\n/", $str);
if (empty($str) || !count($lines)) {
msg_dialog::display(_("Import"), msgPool::incorrectUpload(_("file is empty")), ERROR_DIALOG);
return;
}
return array(
'main' => array(
'name' => _('Import actions from CSV file'),
'class' => array('fullwidth'),
'attrs' => array(
new CompositeAttribute (
_('Import a list of task into argonaut'),
'import',
array(
new FileAttribute (
'', '',
'import_file', FALSE
),
new ButtonAttribute (
'', '',
'import_submit',
_('Upload')
)
),
'', '%s%s', '',
_('Import file')
)
)
),
'events' => array(
'name' => _('Imported tasks'),
'attrs' => array(new FakeAttribute('events')),
'template' => get_template_path('import_events.tpl', TRUE, dirname(__FILE__)),
),
);
}
/* Reset current events */
$this->events = array();
public function __construct($config, &$parent)
{
parent::__construct($config, $config->current['BASE']);
/* Parse each line of the given file */
foreach ($lines as $line) {
/* Skip empty lines */
if (empty($line)) {
continue;
}
$this->attributesAccess['import']->setInLdap(FALSE);
$this->attributesAccess['import']->setLinearRendering(TRUE);
/* Load values from file */
$fields = explode(";", $line);
$event = array();
foreach ($this->csv_fields as $key => $val) {
$event[$val] = "";
if (isset($fields[$key])) {
$event[$val] = $fields[$key];
}
}
$this->events[] = $event;
}
$this->check_fields();
$this->parent = $parent;
$this->events = array();
$this->daemon_events = DaemonEvent::get_event_types();
}
function execute()
{
$smarty = get_smarty();
$smarty->assign('jobtypes', join(',',array_keys($this->daemon_events)));
return parent::execute().
'<p class="plugbottom">'.
' <input type="submit" name="start_import" value="'._('Import').'" >'.
' <input type="submit" name="import_abort" value="'.msgPool::backButton().'">'.
'</p>';
}
public function execute()
function handle_import_submit()
{
/* Import file */
if (isset($_POST['import']) && isset($_FILES['file'])) {
if (isset($_FILES['file']['tmp_name'])) {
$str = file_get_contents($_FILES['file']['tmp_name']);
$this->parse_csv($str);
}
}
$this->parse_csv($this->import);
}
function save_object()
{
/* Import started */
$confirmed = FALSE;
foreach ($_POST as $name => $value) {
if (preg_match("/^MSG_OK/", $name)) {
$confirmed = TRUE;
}
}
if (isset($_POST['start_import']) || $confirmed) {
$error = FALSE;
if (!$confirmed) {
foreach ($this->events as $event) {
if (!empty($event['ERROR'])) {
$error = TRUE;
break;
}
}
if ($error) {
msg_dialog::display(_("Import"),
_("Selected entries will be skipped because of errors. Do you want to proceed?"), CONFIRM_DIALOG);
if (isset($_POST['start_import'])) {
$events = $this->events;
$error = FALSE;
foreach ($events as $event) {
if (!empty($event['ERROR'])) {
$error = TRUE;
break;
}
}
if (!$error) {
if ($error) {
msg_dialog::display(
_('Import'),
_('Fix the errors in your CSV file first'),
ERROR_DIALOG
);
} else {
$success = 0;
$fail = 0;
foreach ($this->events as $key => $event) {
if (!empty($event['ERROR'])) {
$fail++;
continue;
}
foreach ($events as $key => $event) {
/* Create event */
$o_data = $this->daemon_events[$event['HEADER']];
$object = new DaemonEvent($this->config, $event['HEADER']);
$object = new DaemonEvent($this->config, $event['HEADER'], $event);
$object->add_targets(array($event['MAC']));
if (empty($event['TIMESTAMP'])) {
$object->set_type(TRIGGERED_EVENT);
......@@ -121,66 +137,61 @@ class argonautImportFile extends plugin
$object->set_type(SCHEDULED_EVENT);
}
/* Update values like fqdn a.s.o */
foreach ($this->csv_fields as $name) {
if ($name == "TIMESTAMP" && empty($event[$name])) {
continue;
}
$object->set_value($name, $event[$name]);
}
$this->parent->o_queue->append($object);
if ($this->parent->o_queue->is_error()) {
msg_dialog::display(_("Infrastructure service"), msgPool::siError($this->parent->o_queue->get_error()), ERROR_DIALOG);
$fail ++;
msg_dialog::display(_('Infrastructure service'), msgPool::siError($this->parent->o_queue->get_error()), ERROR_DIALOG);
$fail++;
} else {
unset($this->events[$key]);
$success ++;
unset($events[$key]);
$success++;
}
}
msg_dialog::display(_("Import"), sprintf(_("Import complete: %s events successfully send, %s failed"), $success, $fail), INFO_DIALOG);
$this->import_successful = (count($this->events) == 0);
msg_dialog::display(_('Import'), sprintf(_('Import complete: %s events successfully send, %s failed'), $success, $fail), INFO_DIALOG);
$this->events = $events;
}
}
/* Prepare output */
$evts = $this->events;
foreach ($this->events as $id => $evt) {
foreach ($evt as $key => $val) {
if (in_array($key, $this->csv_fields)) {
$evts[$id][$key] = "<span style=\"white-space: nowrap;\">".strip_tags($val)."</span>";
}
}
}
$smarty = get_smarty();
$smarty->assign("info", $evts);
$smarty->assign("count", count($evts));
return $smarty->fetch(get_template_path('argonaut_import_file.tpl', TRUE));
parent::save_object();
}
public function check()
private function parse_csv($str)
{
$message = plugin::check();
$this->check_fields();
return $message;
}
/* Some file checks */
$lines = preg_split('/\n/', $str);
if (empty($str) || !count($lines)) {
msg_dialog::display(_('Import'), msgPool::incorrectUpload(_('file is empty')), ERROR_DIALOG);
return;
}
/* Reset current events */
$events = array();
private function check_fields()
{
foreach ($this->events as $key => $event) {
$this->events[$key]['ERROR'] = "";
/* Parse each line of the given file */
foreach ($lines as $line) {
/* Skip empty lines */
if (empty($line)) {
continue;
}
/* Load values from file */
$fields = explode(';', $line);
$event = array();
foreach ($this->csv_fields as $key => $val) {
$event[$val] = '';
if (isset($fields[$key])) {
$event[$val] = $fields[$key];
}
}
$event['ERROR'] = '';
if (empty($event['MAC']) || !tests::is_mac($event['MAC'])) {
$this->events[$key]['ERROR'] .= msgPool::invalid(_("MAC")).", ";
$event['ERROR'] .= msgPool::invalid(_('MAC')).', ';
}
if (empty($event['HEADER']) || !isset($this->daemon_events[$event['HEADER']])) {
$this->events[$key]['ERROR'] .= msgPool::invalid(_("Event")).", ";
$event['ERROR'] .= msgPool::invalid(_('Event')).', ';
}
$this->events[$key]['ERROR'] = trim($this->events[$key]['ERROR'], ", ");
$event['ERROR'] = trim($event['ERROR'], ', ');
$events[] = $event;
}
$this->attributesAccess['events']->setPostValue($events);
}
}
?>
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2006 Fabian Hickert
......@@ -70,7 +69,6 @@ class DaemonEvent
$this->mode = $type;
}
function get_type()
{
return $this->mode;
......@@ -89,13 +87,13 @@ class DaemonEvent
$this->timestamp = 0;
/* Load values from given data */
if (count($data)) {
if (isset($data['ID'])) {
$this->id = $data['ID'];
$this->is_new = FALSE;
}
$this->id = $data['ID'];
if (isset($data['TIMESTAMP'])) {
$this->timestamp = $this->_event_to_timestamp($data['TIMESTAMP']);
}
if (isset($data['TIMESTAMP'])) {
$this->timestamp = $this->_event_to_timestamp($data['TIMESTAMP']);
}
if (isset($data['PERIODIC']) && !preg_match("/none/i", $data['PERIODIC'])) {
......@@ -109,7 +107,6 @@ class DaemonEvent
}
}
/*! \brief Create the HTML output for the plugin.
@return String The generated HTML output.
*/
......@@ -150,7 +147,6 @@ class DaemonEvent
return $str;
}
/*! \brief Returns the plugin footer (save cancel), displayed in the template.
@return String HTML footer part.
*/
......@@ -167,7 +163,6 @@ class DaemonEvent
return $str;
}
/*! \brief Returns HTML representation of a timestamp using <select> boxes.
@return Returns HTML content.
*/
......@@ -244,7 +239,6 @@ class DaemonEvent
return $smarty->fetch(get_template_path('timestamp_select.tpl', TRUE, dirname(__FILE__)));
}
/*! \brief HTML representation of all currently assigned targets using (divSelectBox).
@return String Returns a listbox with all used targets.
*/
......@@ -263,7 +257,6 @@ class DaemonEvent
return $divselectbox->DrawList().$list_footer;
}
/*! \brief Returns HTML content, displaying a dialog which allows to add new targets.
@return String HTML content. (EventAddSystemDialog)
*/
......@@ -280,7 +273,6 @@ class DaemonEvent
return $smarty->fetch(get_template_path('target_list.tpl', TRUE, dirname(__FILE__)));
}
/*! \brief Handles all posted HTML data, including target add,remove...
*/
public function save_object()
......@@ -346,7 +338,6 @@ class DaemonEvent
}
}
/*! \brief Converts a daemon timestamp into an unix timestamp. \
e.g. 20080101125959 -> 1199188799
@param A daemon timestamp YYYYddmmHHiiss
......@@ -358,7 +349,6 @@ class DaemonEvent
return strtotime($str);
}
/*! \brief Converts a unix timestamp in to a goto-si timestamp. \
e.g. 1199188799 -> 20080101125959
@param A unix timestamp (e.g. 1199188799)
......@@ -370,7 +360,6 @@ class DaemonEvent
return date("YmdHis", $stamp);
}
/*! \brief Returns event information, like menu strings, images ...
@return Array Event informations.
*/
......@@ -389,7 +378,6 @@ class DaemonEvent
}
}
/*! \brief Check if we have opened the target add dialog.
@return Boolean TRUE if we have opened the target add dialog else FALSE.
*/
......@@ -427,7 +415,6 @@ class DaemonEvent
);
}
/*! \brief Returns TRUE if this event is new. (Not edited)
@return Boolean TRUE if new, else FALSE.
*/
......@@ -462,7 +449,6 @@ class DaemonEvent
return $this->s_Action;
}
/*! brief Returns an array containig all attributes \
That should be written.
@return Array e.g. 'status' => 'bla blub'
......@@ -488,7 +474,6 @@ class DaemonEvent
return $ret;
}
/*! \brief Returns the event targets
@return Array All selected targets.
*/
......@@ -497,7 +482,6 @@ class DaemonEvent
return $this->a_targets;
}
/*! \brief Returns the event timestamp in goto-si format.
@return Returns the event timestamp (20081231120000)
*/
......@@ -510,7 +494,6 @@ class DaemonEvent
}
}
/*! \brief Returns the event ID
@return Returns the event ID
*/
......@@ -523,7 +506,6 @@ class DaemonEvent
}
}
/*! \brief Add a target MAC address
@param Array A List of all target that should be added.
*/
......@@ -541,7 +523,6 @@ class DaemonEvent
$this->timestamp = time();
}
/*! \brief Add a target MAC address
@param Array A List of all target that should be added.
*/
......@@ -556,16 +537,5 @@ class DaemonEvent
{
return array();
}
/*! \brief Update a class variable from outside
*/
public function set_value($name, $value)
{
$name = strtolower($name);
if (isset($this->$name) && in_array($name, $this->attributes)) {
$this->$name = $value;
}
}
}
?>
<div id="{$sectionId}" class="plugin_section fullwidth">
<span class="legend">
{$section}
</span>
<div>
{if $attributes.events|@count == 0}
<p>
{t}You can import a list of jobs into the FusionDirectory job queue. This should be a semicolon seperated list of items in the following format:{/t}<br/><br/>
<i>{t}timestamp{/t};{t}MAC-address{/t};{t}job type{/t};[{t}object group{/t}]</i><br/><br/>
{t 1=$jobtypes}Available job types: %1{/t}<br/>
</p>
{else}
<table style="width: 100%;" class="listingTable">
<thead>
<tr>
<th>{t}Timestamp{/t}</th>
<th>{t}MAC{/t}</th>
<th>{t}Event{/t}</th>
<th>{t}Object group{/t}</th>
<th>{t}Error{/t}</th>
</tr>
</thead>
<tbody>
{foreach from=$attributes.events item=event}
<tr>
<td>{$event.TIMESTAMP}</td>
<td>{$event.MAC}</td>
<td>{$event.HEADER}</td>
<td>{$event.OGROUP}</td>
{if $event.ERROR}
<td style="background-color: #F0BBBB;"><b>{$event.ERROR}</b></td>
{else}
<td></td>
{/if}
</tr>
{/foreach}
</tbody>
</table>
{/if}
</div>
</div>