From 8cf70243f19ee4d2daf631ef810242fd1ea992c1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come.chilliet@fusiondirectory.org>
Date: Thu, 8 Oct 2020 15:00:40 +0200
Subject: [PATCH] :sparkles: feat(core) Add archive ACL and action if plugin is
 installed

issue #6119
---
 include/class_config.inc                |  4 ++
 include/management/class_management.inc | 64 +++++++++++++++++++++++++
 2 files changed, 68 insertions(+)

diff --git a/include/class_config.inc b/include/class_config.inc
index 47bb3ff79..40cb70773 100644
--- a/include/class_config.inc
+++ b/include/class_config.inc
@@ -1141,6 +1141,10 @@ class config
               $this->data['OBJECTS'][$obj]['snapshotActive']  = TRUE;
               $this->data['CATEGORIES'][$cat]['classes'][]    = 'SnapshotHandler';
             }
+            if (class_available('archivedObject') && archivedObject::isArchiveActive($obj)) {
+              $this->data['OBJECTS'][$obj]['archiveActive']   = TRUE;
+              $this->data['CATEGORIES'][$cat]['classes'][]    = 'archive';
+            }
           }
         }
       }
diff --git a/include/management/class_management.inc b/include/management/class_management.inc
index 72dfcdc96..2f011da0d 100644
--- a/include/management/class_management.inc
+++ b/include/management/class_management.inc
@@ -280,6 +280,13 @@ class management implements FusionDirectoryDialog
       );
     }
 
+    if (class_available('archivedObject')) {
+      $action = archivedObject::getManagementAction($this->objectTypes, 'archiveEntry');
+      if ($action !== NULL) {
+        $this->registerAction($action);
+      }
+    }
+
     $this->registerAction(
       new Action(
         'remove', _('Remove'), 'geticon.php?context=actions&icon=edit-delete&size=16',
@@ -834,6 +841,63 @@ class management implements FusionDirectoryDialog
     $this->dialogObject = new templateDialog($this, $type, NULL, $this->currentDn);
   }
 
+  public function archiveEntry (array $action)
+  {
+    global $ui;
+
+    if (empty($action['targets'])) {
+      return;
+    }
+    $this->currentDns = $action['targets'];
+
+    logging::debug(DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $this->currentDns, 'Archiving');
+
+    // check locks
+    if ($locks = get_locks($this->currentDns)) {
+      return gen_locked_message($locks, $this->currentDns);
+    }
+
+    // Add locks
+    add_lock($this->currentDns, $ui->dn);
+
+    $success = 0;
+    foreach ($this->currentDns as $dn) {
+      $entry = $this->listing->getEntry($dn);
+      if ($entry === NULL) {
+        trigger_error('Could not find '.$dn.', action canceled');
+        $this->remove_lock();
+        $this->currentDns = [];
+        return;
+      }
+
+      if ($entry->isTemplate()) {
+        $error = new FusionDirectoryError(htmlescape(_('Archiving a template is not possible')));
+        $error->display();
+        $this->remove_lock();
+        $this->currentDns = [];
+        return;
+      }
+
+      $errors = archivedObject::archiveObject($entry->type, $dn);
+      if (empty($errors)) {
+        $success++;
+      } else {
+        msg_dialog::displayChecks($errors);
+      }
+      del_lock($dn);
+    }
+
+    if ($success > 0) {
+      msg_dialog::display(
+        _('Archive success'),
+        htmlescape(sprintf(_('%d entries were successfully archived'), $success)),
+        INFO_DIALOG
+      );
+    }
+
+    $this->currentDns = [];
+  }
+
 
   /*!
    * \brief  This method opens an existing object to be edited.
-- 
GitLab