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

Fixes #5091 Moved and cleaned stuff from management into simpleManagement

parent 0197df01
......@@ -207,9 +207,9 @@ class SnapshotRestoreDialog extends simplePlugin
function updateList ()
{
if ($this->global) {
$this->snapshots = $this->parent->getAllDeletedSnapshots(TRUE);
$this->snapshots = $this->parent->getAllDeletedSnapshots();
} else {
$this->snapshots = $this->parent->getAvailableSnapsShots($this->object_dn, TRUE);
$this->snapshots = $this->parent->getAvailableSnapsShots($this->object_dn);
}
}
......
......@@ -95,62 +95,16 @@ class SnapshotHandler
}
/*!
* \brief Get the deleted snapshots
*
* \param string $objectBase
*
* \param boolean $raw FALSE
* \brief Check if there are deleted snapshots
*/
function getDeletedSnapshots($objectBase, $raw = FALSE)
function hasDeletedSnapshots()
{
global $config;
if (!$this->enabled()) {
return array();
}
$ldap = $config->get_ldap_link();
// Initialize base
$base = $this->snapshot_dn($objectBase);
/* Fetch all objects and check if they do not exist anymore */
$objects = array();
$ldap->cd($base);
$ldap->search(
'(objectClass=gosaSnapshotObject)',
array('gosaSnapshotTimestamp', 'gosaSnapshotDN', 'description'),
'one'
);
while ($entry = $ldap->fetch()) {
$chk = str_replace($base, "", $entry['dn']);
if (preg_match("/,ou=/", $chk)) {
continue;
foreach ($this->getSnapshotBases() as $base) {
if (count($this->getAllDeletedSnapshots($base)) > 0) {
return TRUE;
}
if (!isset($entry['description'][0])) {
$entry['description'][0] = "";
}
$objects[] = $entry;
}
/* Check if entry still exists */
foreach ($objects as $key => $entry) {
$ldap->cat($entry['gosaSnapshotDN'][0]);
if ($ldap->count()) {
unset($objects[$key]);
}
}
/* Format result as requested */
if ($raw) {
return $objects;
} else {
$tmp = array();
foreach ($objects as $key => $entry) {
$tmp[base64_encode($entry['dn'])] = $entry['description'][0];
}
}
return $tmp;
return FALSE;
}
/*!
......@@ -336,7 +290,7 @@ class SnapshotHandler
*
* \return available snapshots for the given base
*/
function getAvailableSnapsShots($dn, $raw = FALSE)
function getAvailableSnapsShots($dn)
{
global $config;
if (!$this->enabled()) {
......@@ -366,16 +320,7 @@ class SnapshotHandler
$tmp[] = $entry;
}
/* Return the raw array, or format the result */
if ($raw) {
return $tmp;
} else {
$tmp2 = array();
foreach ($tmp as $entry) {
$tmp2[base64_encode($entry['dn'])] = $entry['description'][0];
}
}
return $tmp2;
return $tmp;
}
/*!
......@@ -385,7 +330,7 @@ class SnapshotHandler
*
* \param boolean $raw FALSE
*/
function getAllDeletedSnapshots($base_of_object, $raw = FALSE)
function getAllDeletedSnapshots($base_of_object)
{
global $config;
if (!$this->enabled()) {
......@@ -405,7 +350,6 @@ class SnapshotHandler
'one'
);
while ($entry = $ldap->fetch()) {
$chk = str_replace($new_base, "", $entry['dn']);
if (preg_match("/,ou=/", $chk)) {
continue;
......@@ -425,16 +369,7 @@ class SnapshotHandler
}
}
/* Format result as requested */
if ($raw) {
return $tmp;
} else {
$tmp2 = array();
foreach ($tmp as $key => $entry) {
$tmp2[base64_encode($entry['dn'])] = $entry['description'][0];
}
}
return $tmp2;
return $tmp;
}
......
......@@ -1724,15 +1724,8 @@ class listing
$ui = get_userinfo();
if ($ui->allow_snapshot_restore($this->base, $this->categories)) {
// Check if there is something to restore
$restore = FALSE;
foreach ($this->snapshotHandler->getSnapshotBases() as $base) {
$restore = $restore || count($this->snapshotHandler->getDeletedSnapshots($base)) > 0;
}
// Draw icons according to the restore flag
if ($restore) {
if ($this->snapshotHandler->hasDeletedSnapshots()) {
$result .= $this->renderActionMenuActionLink($separator, 'restore', _('Restore snapshots'), 'geticon.php?context=actions&icon=document-restore&size=16');
} else {
$result .= "<li$separator><a href='#'><img src='geticon.php?context=actions&amp;icon=document-restore&amp;size=16&amp;disabled=1' alt='restore' class='center'>&nbsp;"._("Restore snapshots")."</a></li>";
......
......@@ -78,48 +78,9 @@ class management
// A list of configured actions/events
protected $actions = array();
// Attributes managed by this plugin, can be used in post events;
public $attributes = array();
// Some management classes are used in tab groups and needs this set to FALSE.
var $is_template = FALSE;
function __construct($headpage)
{
global $config;
$this->headpage = $headpage;
// Add copy&paste and snapshot handler.
if (!$this->skipCpHandler) {
$this->cpHandler = new CopyPasteHandler();
$this->headpage->setCopyPasteHandler($this->cpHandler);
}
if (!$this->skipSnapHandler && ($config->get_cfg_value("enableSnapshots") == "TRUE")) {
$this->snapHandler = new SnapshotHandler();
$this->headpage->setSnapshotHandler($this->snapHandler);
}
// Register default actions
$this->registerAction("new", "newEntry");
$this->registerAction("edit", "editEntry");
$this->registerAction("apply", "applyChanges");
$this->registerAction("save", "saveChanges");
$this->registerAction("cancel", "cancelEdit");
$this->registerAction("cancelDelete", "cancelEdit");
$this->registerAction("remove", "removeEntryRequested");
$this->registerAction("removeConfirmed", "removeEntryConfirmed");
$this->registerAction("copy", "copyPasteHandler");
$this->registerAction("cut", "copyPasteHandler");
$this->registerAction("paste", "copyPasteHandler");
if ($config->get_cfg_value('enableSnapshots') == 'TRUE') {
$this->registerAction('snapshot', 'createSnapshotDialog');
$this->registerAction('restore', 'restoreSnapshotDialog');
}
}
// Some management classes are used in tab groups and needs this
public $is_template = FALSE;
public $attributes = array();
/*!
* \brief Execute this plugin
......@@ -132,7 +93,7 @@ class management
session::set('LOCK_VARS_TO_USE', $vars);
/* Display the copy & paste dialog, if it is currently open */
$ret = $this->copyPasteHandler("", array());
$ret = $this->copyPasteHandler();
if ($ret) {
return $this->getHeader().$ret;
}
......@@ -143,7 +104,7 @@ class management
session::global_set(get_class($this)."_filter", $this->filter);
session::set('autocomplete', $this->filter);
if (!$this->filter->isValid()) {
msg_dialog::display(_("Filter error"), _("The filter is incomplete!"), ERROR_DIALOG);
msg_dialog::display(_('Filter error'), _('The filter is incomplete!'), ERROR_DIALOG);
}
}
......@@ -192,16 +153,6 @@ class management
return $this->renderList();
}
function getHeadpage()
{
return $this->headpage;
}
function getFilter()
{
return $this->filter;
}
/*!
* \brief Generates the plugin header which is displayed whenever a tab object is
* opened.
......@@ -222,7 +173,6 @@ class management
return print_header($plIcon, $plTitle, LDAP::fix(get_object_info()));
}
/*!
* \brief Generates the footer which is used whenever a tab object is
* displayed.
......@@ -231,7 +181,7 @@ class management
{
// Do not display tab footer for non tab objects
if (!($this->tabObject instanceOf simpleTabs)) {
return "";
return '';
}
// Check if there is a dialog opened - We don't need any buttons in this case.
......@@ -241,11 +191,11 @@ class management
// Skip footer if requested;
if ($this->skipFooter) {
return "";
return '';
}
// In case an of locked entry, we may have opened a read-only tab.
$str = "";
$str = '';
if ($this->tabObject->readOnly()) {
$str .= '<p class="plugbottom">'."\n".
'<input type="submit" name="edit_cancel" value="'.msgPool::cancelButton().'">'."\n".
......@@ -266,131 +216,6 @@ class management
return $str;
}
/*!
* \brief Detects actions/events send by the ui
* and the corresponding targets.
*/
function detectPostActions()
{
if (!is_object($this->headpage)) {
trigger_error("No valid headpage given....!");
return array();
}
$action = $this->headpage->getAction();
if (isset($_POST['edit_cancel'])) {
$action['action'] = 'cancel';
} elseif (isset($_POST['edit_finish'])) {
$action['action'] = 'save';
} elseif (isset($_POST['edit_apply'])) {
$action['action'] = 'apply';
}
if (!$this->is_modal_dialog()) {
if (isset($_POST['delete_confirmed'])) {
$action['action'] = 'removeConfirmed';
}
if (isset($_POST['delete_cancel'])) {
$action['action'] = 'cancelDelete';
}
}
return $action;
}
/*!
* \brief Calls the registered method for a given action/event.
*/
function handleActions($action)
{
// Start action
if (isset($this->actions[$action['action']])) {
$func = $this->actions[$action['action']];
if (!isset($action['targets'])) {
$action['targets'] = array();
}
return $this->$func($action['action'], $action['targets'], $action);
}
}
/*!
* \brief Creates a new snapshot entry
*/
function createSnapshot($dn, $description)
{
global $ui;
if ($this->dn !== $dn) {
trigger_error('There was a problem with the snapshot workflow');
return;
}
if (!empty($dn) && $ui->allow_snapshot_create($dn, $this->dialogObject->aclCategory)) {
$this->snapHandler->createSnapshot($dn, $description);
@DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $dn, 'Snaptshot created!');
} else {
msg_dialog::display(_('Permission'), sprintf(_('You are not allowed to restore a snapshot for %s.'), $dn),
ERROR_DIALOG);
}
}
/*!
* \brief Restores a snapshot object.
*
* \param String $dn The DN of the snapshot
*/
function restoreSnapshot($dn)
{
global $ui;
if (!empty($dn) && $ui->allow_snapshot_restore($dn, $this->dialogObject->aclCategory)) {
$this->snapHandler->restoreSnapshot($dn);
@DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $dn, 'Snaptshot restored');
$this->closeDialogs();
} else {
msg_dialog::display(_('Permission'), sprintf(_('You are not allowed to restore a snapshot for %s.'), $dn),
ERROR_DIALOG);
}
}
/*!
* \brief Get all deleted snapshots
*
* \param string $base The base
*
* \param string boolean $raw
*/
function getAllDeletedSnapshots($raw = FALSE)
{
$tmp = array();
$snapshotBases = $this->snapHandler->getSnapshotBases();
if (is_array($snapshotBases)) {
foreach ($snapshotBases as $base) {
$tmp = array_merge($tmp, $this->snapHandler->getAllDeletedSnapshots($base, $raw));
}
} else {
$tmp = $this->snapHandler->getAllDeletedSnapshots($snapshotBases, $raw);
}
return $tmp;
}
/*
* \brief Return available snapshots for the given base
*
* \param string $dn The DN
*
* \param string boolean $raw
*/
function getAvailableSnapsShots($dn, $raw = FALSE)
{
return $this->snapHandler->getAvailableSnapsShots($dn, $raw);
}
/*!
* \brief Delete a snapshot
*
* \param string $dn DN of the snapshot
*/
function removeSnapshot($dn)
{
$this->snapHandler->removeSnapshot($dn);
}
/*!
* \brief Save object modifications and keep dialogs opened
*/
......@@ -456,16 +281,6 @@ class management
}
}
/*!
* \brief Set a new filter
*
* \param object $filter The new filter
*/
function setFilter($filter)
{
$this->filter = $filter;
}
function is_modal_dialog()
{
return (is_object($this->tabObject) || is_object($this->dialogObject));
......
......@@ -166,7 +166,7 @@ class simpleManagement extends management
}
$this->storagePoints = array_unique($this->storagePoints);
if (count($this->storagePoints) == 0) {
$this->storagePoints[] = "";
$this->storagePoints[] = '';
}
// Build filter
......@@ -179,10 +179,35 @@ class simpleManagement extends management
$this->setFilter($filter);
// Build headpage
$headpage = new $this->headpageClass($this->parseXML($this->listXMLPath));
$headpage->setFilter($filter);
$this->headpage = new $this->headpageClass($this->parseXML($this->listXMLPath));
$this->headpage->setFilter($filter);
// Add copy&paste and snapshot handler.
if (!$this->skipCpHandler) {
$this->cpHandler = new CopyPasteHandler();
$this->headpage->setCopyPasteHandler($this->cpHandler);
$this->registerAction('copy', 'copyPasteHandler');
$this->registerAction('cut', 'copyPasteHandler');
$this->registerAction('paste', 'copyPasteHandler');
}
if (!$this->skipSnapHandler && ($config->get_cfg_value('enableSnapshots') == 'TRUE')) {
$this->snapHandler = new SnapshotHandler();
$this->headpage->setSnapshotHandler($this->snapHandler);
$this->registerAction('snapshot', 'createSnapshotDialog');
$this->registerAction('restore', 'restoreSnapshotDialog');
}
// Register default actions
$this->registerAction('new', 'newEntry');
$this->registerAction('edit', 'editEntry');
$this->registerAction('apply', 'applyChanges');
$this->registerAction('save', 'saveChanges');
parent::__construct($headpage);
$this->registerAction('cancel', 'cancelEdit');
$this->registerAction('cancelDelete', 'cancelEdit');
$this->registerAction('remove', 'removeEntryRequested');
$this->registerAction('removeConfirmed', 'removeEntryConfirmed');
$this->configureHeadpage();
$this->configureFilter();
......@@ -382,6 +407,26 @@ class simpleManagement extends management
return $data;
}
function getHeadpage()
{
return $this->headpage;
}
function getFilter()
{
return $this->filter;
}
/*!
* \brief Set a new filter
*
* \param object $filter The new filter
*/
function setFilter($filter)
{
$this->filter = $filter;
}
function getType($dn)
{
return $this->getHeadpage()->getType($dn);
......@@ -410,7 +455,6 @@ class simpleManagement extends management
return $this->getHeader().$display;
}
/*!
* \brief This method intiates the object creation.
*
......@@ -633,7 +677,52 @@ class simpleManagement extends management
}
}
function handleSubAction($all)
/*!
* \brief Detects actions/events send by the ui
* and the corresponding targets.
*/
function detectPostActions()
{
if (!is_object($this->headpage)) {
trigger_error("No valid headpage given....!");
return array();
}
$action = $this->headpage->getAction();
if (isset($_POST['edit_cancel'])) {
$action['action'] = 'cancel';
} elseif (isset($_POST['edit_finish'])) {
$action['action'] = 'save';
} elseif (isset($_POST['edit_apply'])) {
$action['action'] = 'apply';
}
if (!$this->is_modal_dialog()) {
if (isset($_POST['delete_confirmed'])) {
$action['action'] = 'removeConfirmed';
}
if (isset($_POST['delete_cancel'])) {
$action['action'] = 'cancelDelete';
}
}
return $action;
}
/*!
* \brief Calls the registered method for a given action/event.
*/
function handleActions($action)
{
// Start action
if (isset($this->actions[$action['action']])) {
$func = $this->actions[$action['action']];
if (!isset($action['targets'])) {
$action['targets'] = array();
}
return $this->$func($action['action'], $action['targets'], $action);
}
}
protected function handleSubAction($all)
{
if (preg_match('/^tab_/', $all['subaction'])) {
$tab = preg_replace('/^tab_/', '', $all['subaction']);
......@@ -893,6 +982,84 @@ class simpleManagement extends management
return "";
}
/* Methods related to Snapshots */
/*!
* \brief Creates a new snapshot entry
*/
function createSnapshot($dn, $description)
{
global $ui;
if ($this->dn !== $dn) {
trigger_error('There was a problem with the snapshot workflow');
return;
}
if (!empty($dn) && $ui->allow_snapshot_create($dn, $this->dialogObject->aclCategory)) {
$this->snapHandler->createSnapshot($dn, $description);
@DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $dn, 'Snaptshot created!');
} else {
msg_dialog::display(_('Permission'), sprintf(_('You are not allowed to restore a snapshot for %s.'), $dn),
ERROR_DIALOG);
}
}
/*!
* \brief Restores a snapshot object.
*
* \param String $dn The DN of the snapshot
*/
function restoreSnapshot($dn)
{
global $ui;
if (!empty($dn) && $ui->allow_snapshot_restore($dn, $this->dialogObject->aclCategory)) {
$this->snapHandler->restoreSnapshot($dn);
@DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $dn, 'Snaptshot restored');
$this->closeDialogs();
} else {
msg_dialog::display(_('Permission'), sprintf(_('You are not allowed to restore a snapshot for %s.'), $dn),
ERROR_DIALOG);
}
}
/*!
* \brief Get all deleted snapshots
*
* \param string $base The base
*/
function getAllDeletedSnapshots()
{
$tmp = array();
$snapshotBases = $this->snapHandler->getSnapshotBases();
if (is_array($snapshotBases)) {
foreach ($snapshotBases as $base) {
$tmp = array_merge($tmp, $this->snapHandler->getAllDeletedSnapshots($base));
}
} else {
$tmp = $this->snapHandler->getAllDeletedSnapshots($snapshotBases);
}