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

Fixes #3998 Migrated snapshot dialogs to simplePlugin

parent 125373cf
<div style="padding:5px;">
<div style="font-size:18px;">
<img alt="" src="geticon.php?context=status&amp;icon=dialog-warning&amp;size=32" class="center"/>&nbsp;{t}Warning: you are about to restore the following snapshot{/t}
</div>
<p>
<ul>
{foreach from=$objects item=object}
<li style="list-style-image:url('{$object.icon}');" title="{$object.type}">
{$object.name}&nbsp;(<i>{$object.dn}</i>)
</li>
{/foreach}
</ul>
{t}Any modification made to the object since this snapshot will be lost.{/t}
{t}Please double check if you really want to do this since there is no way for FusionDirectory to get your data back.{/t}
</p>
<p>
{t}So - if you're sure - press 'Continue' to continue or 'Cancel' to abort.{/t}
</p>
<p class="plugbottom">
<input type="submit" name="restore_confirmed" value="{t}Continue{/t}"/>
<input type="submit" name="restore_cancel" value="{msgPool type=cancelButton}"/>
</p>
</div>
{if $RestoreMode}
<h2>{t}Restoring object snapshots{/t}</h2>
<hr/>
<br>
{t}This procedure will restore a snapshot of the selected object. It will replace the existing object after pressing the folder icon.{/t}
<br>
<br>
{t}Remember that DNS configuration and database entries could not be restored. For some objects it is only necessary to open and save them again (goFon), but some entries must be recreated manually.{/t}
<br>
<br>
{t}Don't forget to check references to other objects, for example does the selected printer still exists ?{/t}
<br>
<hr/>
<br>
<table style="width:100%">
{if !$restore_deleted}
<tr>
<td>
<b>{t}Object{/t}</b>&nbsp;
{$CurrentDN}
</td>
</tr>
{/if}
<tr>
<td>
<br>
{if $CountSnapShots==0}
{t}There is no snapshot available that could be restored{/t}
{else}
{t}Choose a snapshot and click the folder image, to restore the snapshot{/t}
{/if}
</td>
</tr>
<tr>
<td>
{$SnapShotDivlist}
</td>
</tr>
</table>
<hr/>
<div style="width:100%; text-align:right;">
<input type="submit" name="CancelSnapshot" value="{t}Cancel{/t}">
</div>
{else}
<h2>{t}Creating object snapshots{/t}</h2>
<hr/>
<br>
{t}This procedure will create a snapshot of the selected object. It will be stored inside a special branch of your directory system and can be restored later on.{/t}
<br>
<br>
{t}Remember that database entries, DNS configurations and possibly created zones in server extensions will not be stored in the snapshot.{/t}
<br>
<hr/>
<br>
<table style="width:100%">
<tr>
<td>
<b>{t}Object{/t}</b>
</td>
<td style="width:95%">
{$CurrentDN}
</td>
</tr>
<tr>
<td>
<b>{t}Timestamp{/t}</b>
</td>
<td>
{$CurrentDate}
</td>
</tr>
<tr>
<td style="vertical-align:top;" colspan="2">
<br>
{t}Reason for generating this snapshot{/t}<br>
<textarea name="CurrentDescription" style="width:100%;height:160px;" rows=10 cols=100>{$CurrentDescription}</textarea>
</td>
</tr>
</table>
<hr/>
<div style="width:100%; text-align:right;">
<input type="submit" name="CreateSnapshot" value="{t}Continue{/t}">
&nbsp;
<input type="submit" name="CancelSnapshot" value="{t}Cancel{/t}">
</div>
<script type="text/javascript">
<!-- // First input field on page
document.mainform.CurrentDescription.focus();
-->
</script>
{/if}
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2003-2010 Cajus Pollmeier
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.
*/
/*!
* \file class_SnapShotDialog.inc
* Source code for SnapShotDialog
*/
/*!
* \brief This class contains all the function needed to make dialogs
* for the snapshot functionality
*
* \see plugin
*/
class SnapShotDialog extends plugin
{
var $attributes = array("CurrentDescription");
var $CurrentDescription = "";
var $display_restore_dialog = FALSE;
/* Defines the restore mode */
var $display_all_removed_objects = FALSE;
/* Specifies which objects will be listed,
* all snapshots for a single entry or
* all snapshots of already deleted objects */
var $del_dn = "";
var $last_list = array();
var $snapHandler;
/*!
* \brief Create a SnapShotDialog
*
* \param string $config
*
* \param string $dn The DN
*
* \param string $parent
*/
function __construct(&$config, $dn, &$parent)
{
parent::__construct($config, $dn);
$this->snapHandler = new SnapshotHandler($this->config);
}
/*!
* \brief Get all deleted snapshots
*/
function getAllDeletedSnapshots($base_of_object, $raw = FALSE)
{
return $this->snapHandler->getAllDeletedSnapshots($base_of_object, $raw);
}
/*
* \brief Return available snapshots for the given base
*
* \param string $dn The DN
*
* \param string boolean $raw FALSE
*/
function Available_SnapsShots($dn, $raw = FALSE)
{
return $this->snapHandler->Available_SnapsShots($dn, $raw);
}
/*!
* \brief Remove a snapshot
*
* \param string $dn The DN
*/
function remove_snapshot($dn)
{
$this->snapHandler->remove_snapshot($dn);
}
/*!
* \brief Set a snapshot base
*
* Show deleted snapshots from these bases
*
* \param string $bases
*/
function set_snapshot_bases($bases)
{
$this->snapHandler->setSnapshotBases($bases);
}
/*!
* \brief Display snapshot dialog
*/
function execute()
{
parent::execute();
$smarty = get_smarty();
$matches = array();
foreach ($_POST as $name => $value) {
if (preg_match("/^RemoveSnapshot_(.*)_[xy]$/", $name, $matches)) {
$entry = intval($matches[1]);
if (isset($this->last_list[$entry])) {
$this->del_dn = $this->last_list[$entry]['dn'];
$smarty = get_smarty();
$smarty->assign("intro", sprintf(_("You're about to delete the snapshot '%s'."), LDAP::fix($this->del_dn)));
return $smarty->fetch (get_template_path('remove.tpl'));
}
break;
}
}
/* Remove snapshot */
if (isset($_POST['delete_confirm']) && !empty($this->del_dn)) {
$this->remove_snapshot($this->del_dn);
$this->del_dn = "";
}
/* We must restore a snapshot */
if ($this->display_restore_dialog) {
/* Should we only display all snapshots of already deleted objects
or the snapshots for the given object dn */
$tmp = array();
if ($this->display_all_removed_objects) {
$snap_shot_bases = $this->snapHandler->getSnapshotBases();
if (count($snap_shot_bases)) {
foreach ($snap_shot_bases as $dn) {
$tmp = array_merge($tmp, $this->getAllDeletedSnapshots($dn, TRUE));
}
} else {
$tmp = $this->getAllDeletedSnapshots($snap_shot_bases, TRUE);
}
} else {
$tmp = $this->Available_SnapsShots($this->dn, TRUE);
}
$DivListSnapShots = new divSelectBox("SnapShotRestore");
$DivListSnapShots->SetHeight(180);
$list_of_elements = array();
/* Walk through all entries and setup the display text */
foreach ($tmp as $entry) {
$list_of_elements[$entry['gosaSnapshotTimestamp'][0]] = $entry;
}
/* Sort generated list */
krsort($list_of_elements);
/* Add Elements to divselectbox */
$this->last_list = array_values($list_of_elements);
foreach ($this->last_list as $key => $entry) {
$actions = "<input type='image' src='geticon.php?context=actions&amp;icon=document-restore&amp;size=16' name='RestoreSnapshot_$key'
class='center' title='"._("Restore snapshot")."'>&nbsp;";
$actions .= "<input type='image' src='geticon.php?context=actions&amp;icon=edit-delete&amp;size=16' name='RemoveSnapshot_$key'
class='center' title='"._("Remove snapshot")."'>&nbsp;";
$time_stamp = date(_("Y-m-d, H:i:s"), preg_replace("/\-.*$/", "", $entry['gosaSnapshotTimestamp'][0]));
$display_data = $entry['description'][0];
if ($this->display_all_removed_objects) {
$display_data .= " - ".$entry['gosaSnapshotDN'][0];
}
$field0 = array("html" => $time_stamp, "attach" => "style='vertical-align:top;width:120px;'");
$field1 = array("html" => htmlentities(utf8_decode(LDAP::fix($display_data))), "attach" => "");
$field2 = array("html" => $actions,
"attach" => "style='border-right:0px;vertical-align:top;width:40px;text-align:right;'");
$DivListSnapShots->AddEntry(array($field0,$field1,$field2));
}
$smarty->assign("SnapShotDivlist", $DivListSnapShots->DrawList());
$smarty->assign("CountSnapShots", count($list_of_elements));
}
$smarty->assign("restore_deleted", $this->display_all_removed_objects);
$smarty->assign("RestoreMode", $this->display_restore_dialog);
$smarty->assign("CurrentDate", date(_("Y-m-d, H:i:s")));
$smarty->assign("CurrentDN", LDAP::fix($this->dn));
$smarty->assign("CurrentDescription", $this->CurrentDescription);
return $smarty->fetch(get_template_path("snapshotdialog.tpl"));
}
/*!
* \brief Check the snapshot dialog
*/
function check()
{
$message = plugin::check();
if (!$this->display_restore_dialog) {
if (empty($this->CurrentDescription)) {
$message[] = msgPool::required(_("Description"));
}
}
return $message;
}
/*!
* \brief Save an object
*/
function save_object()
{
plugin::save_object();
foreach ($this->attributes as $name) {
if (isset($_POST[$name])) {
$this->$name = stripslashes($_POST[$name]);
}
}
}
}
?>
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2003-2010 Cajus Pollmeier
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 SnapshotCreateDialog extends simplePlugin
{
public $aclCategory;
static function getAttributesInfo ()
{
return array(
'main' => array(
'name' => _('Creating an object snapshot'),
'attrs' => array(
new DisplayAttribute (
_('Object'), _('DN of the object you are creating a snapshot of'),
'object_dn', FALSE
),
new DisplayAttribute (
_('Timestamp'), _('Timestamp of this snapshot creation'),
'timestamp', FALSE
),
new TextAreaAttribute (
_('Reason'), _('Reason for creating this snapshot'),
'description', TRUE
),
)
),
);
}
function __construct (&$config, $dn, $parent, $aclCategory)
{
parent::__construct($config);
$this->attributesAccess['description']->setInLdap(FALSE);
$this->object_dn = $dn;
$this->parent = $parent;
$this->aclCategory = $aclCategory;
}
function execute ()
{
$this->timestamp = date(_('Y-m-d, H:i:s'));
$str = parent::execute();
$str .= '<p class="plugbottom">'.
' <input type="submit" name="edit_finish" value="'.msgPool::saveButton().'"/>&nbsp;'.
' <input type="submit" name="edit_cancel" value="'.msgPool::cancelButton().'"/>'.
'</p>';
return $str;
}
function save ()
{
$this->parent->createSnapshot($this->object_dn, $this->description);
}
}
class SnapshotsAttribute extends OrderedArrayAttribute
{
function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = array(), $acl = '')
{
Attribute::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
$this->edit_enabled = FALSE;
$this->order = FALSE;
$this->attribute = FALSE;
$this->setInLdap(FALSE);
}
function loadPostValue ()
{
if ($this->isVisible()) {
parent::loadPostValue();
parent::applyPostValue();
}
}
protected function handleEdit($key)
{
}
function applyPostValue ()
{
}
function addValue($value)
{
$this->value[] = $value;
$this->reIndexValues();
}
function renderButtons ()
{
return '';
}
protected function getAttributeArrayValue($key, $value)
{
$values = array(
date(_('Y-m-d, H:i:s'), preg_replace('/\-.*$/', '', $value['gosaSnapshotTimestamp'][0])),
$value['description'][0]
);
if ($this->plugin->global) {
array_unshift($values, $value['gosaSnapshotDN'][0]);
}
return $values;
}
protected function genRowIcons($key, $value)
{
$id = $this->getHtmlId();
list ($img, $width) = parent::genRowIcons($key, $value);
$img = $this->renderInputField(
'image', $id.'_restore_'.$key,
array(
'src' => 'geticon.php?context=actions&amp;icon=document-restore&amp;size=16',
'title' => _('Restore'),
'alt' => _('Restore'),
'class' => 'center'
)
).'&nbsp;'.$img;
$width += 15;
return array ($img, $width);
}
protected function handlePostValueActions($id, $postValue)
{
if (parent::handlePostValueActions($id, $postValue)) {
return TRUE;
}
if (preg_match('/^'.$id.'_restore_/', $postValue)) {
$key = preg_replace('/^'.$id.'_restore_/', '', $postValue);
$key = preg_replace('/_[xy]$/', '', $key);
$this->plugin->triggerRestore($this->value[$key]['dn']);
return TRUE;
}
return FALSE;
}
function delPostValue ($key)
{
$this->plugin->triggerDelete($this->value[$key]['dn']);
}
}
class SnapshotRestoreDialog extends simplePlugin
{
public $global;
public $aclCategory;
protected $snapDn;
protected $snapAction;
static function getAttributesInfo ()
{
return array(
'main' => array(
'name' => _('Restoring snapshots'),
'class' => array('fullwidth'),
'attrs' => array(
new DisplayAttribute (
_('Object'), _('DN of the object you are creating a snapshot of'),
'object_dn', FALSE
),
new SnapshotsAttribute (
_('Snapshots'), _('Existing snapshots for this object'),
'snapshots', FALSE
),
)
),
);
}
function __construct (&$config, $dn, $parent, $global, $aclCategory)
{
parent::__construct($config);
$this->object_dn = $dn;
$this->parent = $parent;
$this->global = $global;
$this->aclCategory = $aclCategory;
if ($this->global) {
$this->attributesAccess['object_dn']->setVisible(FALSE);
}
$this->updateList();
}
function updateList ()
{
if ($this->global) {
$this->snapshots = $this->parent->getAllDeletedSnapshots(TRUE);
} else {
$this->snapshots = $this->parent->getAvailableSnapsShots($this->object_dn, TRUE);
}
}
function execute ()
{
if ($this->dialog == 'delete') {
$objects = array(
array(
'name' => 'snapshot',
'dn' => LDAP::fix($this->snapDn),
'icon' => 'geticon.php?context=actions&amp;icon=document-restore&amp;size=16',
'type' => 'Snapshot'
)
);
// Display confirmation dialog.
$smarty = get_smarty();
$smarty->assign('objects', $objects);
$str = $smarty->fetch(get_template_path('simple-remove.tpl'));
} elseif ($this->dialog == 'restore') {
$objects = array(
array(
'name' => 'snapshot',
'dn' => LDAP::fix($this->snapDn),
'icon' => 'geticon.php?context=actions&amp;icon=document-restore&amp;size=16',
'type' => 'Snapshot'
)
);
// Display confirmation dialog.
$smarty = get_smarty();
$smarty->assign('objects', $objects);
$str = $smarty->fetch(get_template_path('restore-confirm.tpl'));
} else {
$str = parent::execute();
$str .= '<p class="plugbottom">'.
' <input type="submit" name="edit_cancel" value="'.msgPool::backButton().'"/>'.
'</p>';
}
return $str;
}
function triggerRestore($dn)