From 1b8ebc5095885d3df41af432f72dfbb3351cadea Mon Sep 17 00:00:00 2001
From: Thibault Dockx <thibault.dockx@fusiondirectory.org>
Date: Fri, 28 Oct 2022 18:07:52 +0100
Subject: [PATCH] :sparkles: Feat(Task) - Dashboard v0.3

Updates for dashboard - version 0.3
---
 contrib/openldap/core-fd.schema              |  2 +-
 plugins/addons/tasks/class_tasksGranular.inc |  8 ++-
 plugins/addons/tasks/class_tasksMail.inc     | 55 ++++++++++++++------
 plugins/admin/tasks/class_tasksDashboard.inc | 21 +++-----
 4 files changed, 49 insertions(+), 37 deletions(-)

diff --git a/contrib/openldap/core-fd.schema b/contrib/openldap/core-fd.schema
index 22613dcf1..6c7a7f662 100644
--- a/contrib/openldap/core-fd.schema
+++ b/contrib/openldap/core-fd.schema
@@ -353,7 +353,7 @@ attributetype ( 1.3.6.1.4.1.38414.89.1.12 NAME 'fdTasksGranularType'
   SINGLE-VALUE )
 
 attributetype ( 1.3.6.1.4.1.38414.89.1.13 NAME 'fdTasksGranularMail'
-  DESC 'Fusion Directory - Task Mail'
+  DESC 'Fusion Directory - Emails recipients if object mail'
   EQUALITY caseExactMatch
   SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )
 
diff --git a/plugins/addons/tasks/class_tasksGranular.inc b/plugins/addons/tasks/class_tasksGranular.inc
index 65b34f5f4..c71f54e77 100644
--- a/plugins/addons/tasks/class_tasksGranular.inc
+++ b/plugins/addons/tasks/class_tasksGranular.inc
@@ -53,7 +53,9 @@ class tasksGranular extends simplePlugin
           new HiddenAttribute('fdTasksGranularStatus', TRUE, '1', '', 'Status', 'Status of the task'),
           new HiddenAttribute('fdTasksGranularMaster', TRUE, '', '', 'Master CN', 'Name of the Master task'),
           new HiddenAttribute('fdTasksGranularType', TRUE, '', '', 'Type', 'Type of the task'),
-          new HiddenAttribute('fdTasksGranularMail', TRUE, '', '', 'Mail', 'Mail of the task'),
+          new MailAttribute(
+             _('Email'),
+             _('Email address which will be sent to'), 'fdTasksGranularMail', TRUE),
           new DateTimeAttribute(
            _('Schedule'), '',
            'fdTasksGranularSchedule', FALSE
@@ -67,10 +69,6 @@ class tasksGranular extends simplePlugin
   {
     global $config;
     parent::__construct($dn, $object, $parent, $mainTab);
-
-    $this->attributesAccess['fdTasksGranularMaster']->setValue('testMaster');
-    $this->attributesAccess['fdTasksGranularType']->setValue('testType');
-    $this->attributesAccess['fdTasksGranularMail']->setValue('testMail');
   }
 
 }
diff --git a/plugins/addons/tasks/class_tasksMail.inc b/plugins/addons/tasks/class_tasksMail.inc
index 5247534ec..37b0723e3 100644
--- a/plugins/addons/tasks/class_tasksMail.inc
+++ b/plugins/addons/tasks/class_tasksMail.inc
@@ -147,39 +147,62 @@ class tasksMail extends simplePlugin
     return $errors;
   }
 
-  public function generateSlaveTasks () : void
+  /*
+   * Generate slave tasks, carefull that main task cannot be changed cause subtasks are not updated (for now).
+   */
+  public function generateSlaveTasks ()
   {
-    $tabobject = objects::create('TasksGranular');
-    $infos = objects::infos('TasksGranular');
     $emails = $this->attributesAccess['fdTasksEmailsFromDN']->getValue();
-
-    // Testing purposes
-    $id = 1;
+    // Incremental id allowing creation of different CN for sub tasks
+    $id = 0;
+
+    // Verification of set attributes required for granular tasks.
+    if ( isset($this->attrs['fdTasksScheduleDate'][0]) && !empty($this->attrs['fdTasksScheduleDate'][0]) ) {
+      $schedule = $this->attrs['fdTasksScheduleDate'][0];
+    } else {
+            // testing purposes.
+            $schedule = '20222222222200';
+    }
 
     if (isset($emails) && !empty($emails)) {
       foreach ($emails as $email) {
-        $id += $id;
+        // Here we create the object taskGranular
+        $tabobject = objects::create('TasksGranular');
+
+                // remove 'dn' keeping only 'cn'
+        $rmDn = preg_replace('/(?=,).*/', '', $this->dn);
+                // only take the cn without dc
+        preg_match('/cn=(.*)/', $rmDn, $matches);
+                $subTaskName = $matches[1].'-SubTask-'.$id;
+        $id ++;
+
         $values['tasksGranular'] = [
-          "cn" => "testCN-.$id",
-          "fdTasksGranularType" => 'Mail Object',
+          "cn"                      => $subTaskName,
+          "fdTasksGranularType"     => 'Mail Object',
+          "fdTasksGranularMaster"   => $this->dn,
+          "fdTasksGranularMail"     => $email,
+          "fdTasksGranularSchedule" => $schedule,
         ];
 
         foreach ($values as $tab => $tabvalues) {
           if (!isset($tabobject->by_object[$tab])) {
-              echo "Error tabObject by object tab is not set";
+            echo "Error tab does not contains attributes values" .PHP_EOL;
           }
-            $error = $tabobject->by_object[$tab]->deserializeValues($tabvalues);
+          $error = $tabobject->by_object[$tab]->deserializeValues($tabvalues);
           if ($error !== TRUE) {
-            echo $error;
+            echo 'Error deserailizing' .PHP_EOL;
           }
 
-            $tabobject->current = $tab;
-            $tabobject->update();
-            $tabobject->loadTabs();
+          $tabobject->current = $tab;
+          $tabobject->update();
+          $tabobject->loadTabs();
         }
+
         $errors = $tabobject->save();
         if (!empty($errors)) {
-            print_r($errors);
+            Throw new FusionDirectoryError(
+                        htmlescape(sprintf( _('SubTasks DN already exist ! You cannot modify a saved task !')))
+                    );
         }
       }
     }
diff --git a/plugins/admin/tasks/class_tasksDashboard.inc b/plugins/admin/tasks/class_tasksDashboard.inc
index 8c6c286ef..5000ad705 100644
--- a/plugins/admin/tasks/class_tasksDashboard.inc
+++ b/plugins/admin/tasks/class_tasksDashboard.inc
@@ -21,9 +21,9 @@
 class tasksDashboard extends management
 {
 
-  // Actions collumn are removed compared to parent
+  // Actions collumn are removed compared to parent.
   public static $columns = [
-    ['LinkColumn',  ['attributes' => 'cn', 'label' => 'Tasks']],
+    ['Column', ['attributes' => 'cn', 'label' => 'Tasks']],
     ['Column', ['attributes' => 'fdTasksGranularMaster', 'label' => 'Master Task']],
     ['Column', ['attributes' => 'fdTasksGranularType', 'label' => 'Types']],
     ['Column', ['attributes' => 'fdTasksGranularMail', 'label' => 'Email']],
@@ -31,7 +31,7 @@ class tasksDashboard extends management
     ['Column', ['attributes' => 'fdTasksGranularStatus', 'label' => 'Status']],
   ];
 
-  // No modification tools are required
+  // No modification tools are required.
   protected $skipCpHandler        = TRUE;
   public static $skipSnapshots    = TRUE;
   public static $skipTemplates    = TRUE;
@@ -56,15 +56,10 @@ class tasksDashboard extends management
     $this->filter   = new tasksDashboardFilter($this, NULL, $filterElementDefinitions);
   }
 
-  // Allowing us to add additional actions and remove unrequired ones
+  // Allowing us to add additional actions and remove unrequired ones.
   protected function configureActions ()
   {
-    $this->registerAction(
-      new Action(
-        'edit', _('See Details'), 'geticon.php?context=actions&icon=document-edit&size=16',
-        '+', 'editEntry'
-      )
-    );
+        // Leaving empty as no default action allowed.
   }
   // End of Class
 }
@@ -72,9 +67,5 @@ class tasksDashboard extends management
 // extending the class allows us to remove the rendering of filter.
 class tasksDashboardFilter extends managementFilter
 {
-  function render () : string
-  {
-    // must respect the string return from parent
-    return '';
-  }
+    // Filter will be
 }
-- 
GitLab