Verified Commit 12af9207 authored by dockx thibault's avatar dockx thibault
Browse files

:sparkles: Feat(Tasks) - Common subtasks creation for added tabs

Generic concept of sub-tasks creation + small fixes.
Showing with 118 additions and 181 deletions
+118 -181
...@@ -93,7 +93,6 @@ class tasks extends simplePlugin ...@@ -93,7 +93,6 @@ class tasks extends simplePlugin
function __construct ($dn = NULL, $object = NULL, $parent = NULL, $mainTab = FALSE) function __construct ($dn = NULL, $object = NULL, $parent = NULL, $mainTab = FALSE)
{ {
global $config;
parent::__construct($dn, $object, $parent, $mainTab); parent::__construct($dn, $object, $parent, $mainTab);
$this->attributesAccess['fdTasksRepeatable']->setManagedAttributes( $this->attributesAccess['fdTasksRepeatable']->setManagedAttributes(
...@@ -112,13 +111,117 @@ class tasks extends simplePlugin ...@@ -112,13 +111,117 @@ class tasks extends simplePlugin
function save (): array function save (): array
{ {
// Verification if the bool of activation is ticked and activate the last exec accordingly. // Verification if the bool of activation is ticked and activate the last exec accordingly.
if ($this->fdSubTasksActivation == TRUE) { if ($this->fdSubTasksActivation === TRUE) {
$currentDateTime = date("Y-m-d h:i:sa", time()); $currentDateTime = date("Y-m-d h:i:sa", time());
$this->fdTasksLastExec = $currentDateTime; $this->fdTasksLastExec = $currentDateTime;
} }
return parent::save(); return parent::save();
} }
public function createSlaveTasks (array $listOfDN, string $attributeType, array $attrs = NULL): void
{
global $config;
$ldap = $config->get_ldap_link();
// Take the attribute from the other tabs - attribute cannot be null or unset by default
$schedule = $this->fdTasksScheduleDate ?? NULL;
// Verify if members can have multiple sub-tasks for that main task.
$newMemberOnly = $this->fdTasksUpdatable;
// remove 'dn' keeping only 'cn'
$rmDn = preg_replace('/(?=,).*/', '', $this->dn);
// only take the cn without dc
preg_match('/cn=(.*)/', $rmDn, $matches);
if (!empty($listOfDN)) {
// Condition allowing the creation of subtasks for existing members
if ($newMemberOnly === TRUE) {
$ldap->cd($config->current['BASE']);
$filter = '(&(objectClass=fdTasksGranular)(fdTasksGranularMaster=' . $this->dn . '))';
$ldap->search($filter, [$attributeType]);
// The while loop is important to get all info from ldap into the array.
while ($info = $ldap->fetch()) {
$subTasks[] = $info;
}
if (!empty($subTasks)) {
// Recuperate members DN from the ldap search.
foreach ($subTasks as $subTask) {
$membersDN[] = $subTask['fdTasksGranularDN'][0];
}
// Verify the DN differences and only keep those.
if (!empty($membersDN)) {
$listOfDN = array_diff($listOfDN, $membersDN);
}
// Simple re-index the array.
$listOfDN = array_values($listOfDN);
}
}
foreach ($listOfDN as $dn) {
// Here we create the object taskGranular
$tabObject = objects::create('TasksGranular');
// Create a unique ID based on timestamp (Allowing duplicate subtasks for same members in case of repeat).
$timestamp = microtime(TRUE); // Get the current timestamp with microseconds
$timestamp = (string)$timestamp; // Convert the float to a string, str_replace expect array or string.
$uniqueID = str_replace(".", "_", $timestamp); // Remove . with _ for correct CN
// Array matches come from preg_match function above with rmDn
$subTaskName = $matches[1] . '-SubTask-' . $uniqueID;
// Define the type of the granular task based on the attribute type passed to this method.
$prepData = NULL;
switch ($attributeType) {
case 'fdTasksGranularMail':
$prepData['tasksGranular'] = [
"fdTasksGranularMail" => $dn,
"fdTasksGranularType" => 'Mail Object',
"fdTasksGranularRef" => $attrs['ref'],
"fdTasksGranularMailFrom" => $attrs['from'],
"fdTasksGranularMailBCC" => $attrs['bcc']
];
break;
case 'fdTasksLifeCycle' :
$prepData['tasksGranular'] = [
"fdTasksGranularDN" => $dn,
"fdTasksGranularType" => 'Life Cycle',
];
break;
}
// Common attributes to be filled for object tasksGranular.
$defaultData['tasksGranular'] = [
"cn" => $subTaskName,
"fdTasksGranularMaster" => $this->dn,
"fdTasksGranularSchedule" => $schedule,
];
// Simply merged the common values and the custom ones depending on the attribute type passed.
$values['tasksGranular'] = array_merge($prepData['tasksGranular'], $defaultData['tasksGranular']);
foreach ($values as $tab => $tabvalues) {
if (!isset($tabObject->by_object[$tab])) {
echo "Error tab does not contains attributes values" . PHP_EOL;
}
$error = $tabObject->by_object[$tab]->deserializeValues($tabvalues);
if ($error !== TRUE) {
echo 'Error during deserializing' . $error . PHP_EOL;
}
$tabObject->current = $tab;
$tabObject->update();
$tabObject->loadTabs();
}
$errors = $tabObject->save();
if (!empty($errors)) {
msg_dialog::displayChecks($errors);
}
}
}
}
/** /**
* @param array $groups * @param array $groups
* @return array * @return array
...@@ -127,7 +230,7 @@ class tasks extends simplePlugin ...@@ -127,7 +230,7 @@ class tasks extends simplePlugin
{ {
global $config; global $config;
$ldap = $config->get_ldap_link(); $ldap = $config->get_ldap_link();
$listMemberDN = []; $listMemberDN = [];
if (!empty($groups)) { if (!empty($groups)) {
......
...@@ -25,7 +25,7 @@ class tasksLifeCycle extends simplePlugin ...@@ -25,7 +25,7 @@ class tasksLifeCycle extends simplePlugin
/** /**
* @var array|array[] * @var array|array[]
*/ */
private $subStates; private array $subStates;
static function plInfo (): array static function plInfo (): array
{ {
...@@ -186,93 +186,11 @@ class tasksLifeCycle extends simplePlugin ...@@ -186,93 +186,11 @@ class tasksLifeCycle extends simplePlugin
*/ */
public function generateSlaveTasks () public function generateSlaveTasks ()
{ {
global $config; $listOfDN = $this->attributesAccess['fdTasksLifeCycleListOfDN']->getValue();
$ldap = $config->get_ldap_link(); $attributeType = 'fdTasksLifeCycle';
$listOfDN = $this->attributesAccess['fdTasksLifeCycleListOfDN']->getValue();
// Take the attribute from the other tabs - attribute cannot be null or unset by default
$schedule = $this->parent->getBaseObject()->fdTasksScheduleDate ?? NULL;
// Verify if members can have multiple sub-tasks for that main task.
$newMemberOnly = $this->parent->getBaseObject()->fdTasksUpdatable;
// remove 'dn' keeping only 'cn'
$rmDn = preg_replace('/(?=,).*/', '', $this->dn);
// only take the cn without dc
preg_match('/cn=(.*)/', $rmDn, $matches);
if (!empty($listOfDN)) {
// Condition allowing the creation of subtasks for existing members
if ($newMemberOnly === TRUE) {
$ldap->cd($config->current['BASE']);
$filter = '(&(objectClass=fdTasksGranular)(fdTasksGranularMaster=' . $this->dn . '))';
$attrs = ['fdTasksLifeCycle'];
$ldap->search($filter, $attrs);
// The while loop is important to get all info from ldap into the array.
while ($info = $ldap->fetch()) {
$subTasks[] = $info;
}
if (!empty($subTasks)) {
// Recuperate members DN from the ldap search.
foreach ($subTasks as $subTask) {
$membersDN[] = $subTask['fdTasksGranularDN'][0];
}
// Verify the DN differences and only keep those.
if (!empty($membersDN)) {
$listOfDN = array_diff($listOfDN, $membersDN);
}
// Simple re-index the array.
$listOfDN = array_values($listOfDN);
}
}
foreach ($listOfDN as $dn) { // Call the method from parent tasks object (first tab) to create sub-tasks.
// Here we create the object taskGranular $this->parent->getBaseObject()->createSlaveTasks($listOfDN, $attributeType);
$tabObject = objects::create('TasksGranular');
// Create a unique ID based on timestamp (Allowing duplicate subtasks for same members in case of repeat).
$timestamp = microtime(TRUE); // Get the current timestamp with microseconds
$timestamp = (string)$timestamp; // Convert the float to a string, str_replace expect array or string.
$uniqueID = str_replace(".", "_", $timestamp); // Remove . with _ for correct CN
// Array matches come from preg_match function above with rmDn
$subTaskName = $matches[1] . '-SubTask-' . $uniqueID;
$values['tasksGranular'] = [
"cn" => $subTaskName,
"fdTasksGranularType" => 'Life Cycle',
"fdTasksGranularMaster" => $this->dn,
"fdTasksGranularDN" => $dn,
"fdTasksGranularSchedule" => $schedule,
];
foreach ($values as $tab => $tabvalues) {
if (!isset($tabObject->by_object[$tab])) {
echo "Error tab does not contains attributes values" . PHP_EOL;
}
$error = $tabObject->by_object[$tab]->deserializeValues($tabvalues);
if ($error !== TRUE) {
echo 'Error during deserializing' . $error . PHP_EOL;
}
$tabObject->current = $tab;
$tabObject->update();
$tabObject->loadTabs();
}
$errors = $tabObject->save();
// Showing errors should be better, enhancement here required.
if (!empty($errors)) {
$show_error = new SimplePluginError($this, htmlescape(sprintf(_('Error : "%s", already exist ! Editing existing tasks is forbidden.'), $subTaskName)));
$show_error->display();
}
}
}
} }
public function update (): bool public function update (): bool
......
...@@ -216,99 +216,15 @@ class tasksMail extends simplePlugin ...@@ -216,99 +216,15 @@ class tasksMail extends simplePlugin
*/ */
public function generateSlaveTasks () public function generateSlaveTasks ()
{ {
global $config; $listOfDN = $this->attributesAccess['fdTasksEmailsFromDN']->getValue();
$ldap = $config->get_ldap_link(); $attributeType = 'fdTasksGranularMail';
$emails = $this->attributesAccess['fdTasksEmailsFromDN']->getValue();
// Ref is supposed to be the mail object CN in this class // Ref is supposed to be the mail object CN in this class
$ref = $this->attributesAccess['fdTasksMailObject']->getValue(); $attrs['ref'] = $this->attributesAccess['fdTasksMailObject']->getValue();
$from = $this->attributesAccess['fdTasksEmailSender']->getValue(); $attrs['from'] = $this->attributesAccess['fdTasksEmailSender']->getValue();
$bcc = $this->attributesAccess['fdTasksEmailBCC']->getValue(); $attrs['bcc'] = $this->attributesAccess['fdTasksEmailBCC']->getValue();
// Take the attribute from the other tabs - attribute cannot be null or unset by default
$schedule = $this->parent->getBaseObject()->fdTasksScheduleDate ?? NULL;
// Verify if members can have multiple sub-tasks for that main task.
$newMemberOnly = $this->parent->getBaseObject()->fdTasksUpdatable;
// remove 'dn' keeping only 'cn'
$rmDn = preg_replace('/(?=,).*/', '', $this->dn);
// only take the cn without dc
preg_match('/cn=(.*)/', $rmDn, $matches);
if (!empty($emails)) {
// Condition allowing the creation of subtasks for existing members
if ($newMemberOnly === TRUE) {
$ldap->cd($config->current['BASE']);
$filter = '(&(objectClass=fdTasksGranular)(fdTasksGranularMaster=' . $this->dn . '))';
$attrs = ['fdTasksGranularMail'];
$ldap->search($filter, $attrs);
// The while loop is important to get all info from ldap into the array.
while ($info = $ldap->fetch()) {
$subTasks[] = $info;
}
if (!empty($subTasks)) {
// Recuperate members email from the ldap search.
foreach ($subTasks as $subTask) {
$membersEmailsList[] = $subTask['fdTasksGranularMail'][0];
}
// Verify the emails differences and only keep those.
if (!empty($membersEmailsList)) {
$emails = array_diff($emails, $membersEmailsList);
}
// Simple re-index the array.
$emails = array_values($emails);
}
}
foreach ($emails as $email) { // Call the method from parent tasks object (first tab) to create sub-tasks.
// Here we create the object taskGranular $this->parent->getBaseObject()->createSlaveTasks($listOfDN, $attributeType, $attrs);
$tabObject = objects::create('TasksGranular');
// Create a unique ID based on timestamp (Allowing duplicate subtasks for same members in case of repeat).
$timestamp = microtime(TRUE); // Get the current timestamp with microseconds
$timestamp = (string)$timestamp; // Convert the float to a string, str_replace expect array or string.
$uniqueID = str_replace(".", "_", $timestamp); // Remove . with _ for correct CN
// Array matches come from preg_match function above with rmDn
$subTaskName = $matches[1] . '-SubTask-' . $uniqueID;
$values['tasksGranular'] = [
"cn" => $subTaskName,
"fdTasksGranularType" => 'Mail Object',
"fdTasksGranularMaster" => $this->dn,
"fdTasksGranularMail" => $email,
"fdTasksGranularSchedule" => $schedule,
"fdTasksGranularRef" => $ref,
"fdTasksGranularMailFrom" => $from,
"fdTasksGranularMailBCC" => $bcc
];
foreach ($values as $tab => $tabvalues) {
if (!isset($tabObject->by_object[$tab])) {
echo "Error tab does not contains attributes values" . PHP_EOL;
}
$error = $tabObject->by_object[$tab]->deserializeValues($tabvalues);
if ($error !== TRUE) {
echo 'Error during deserializing' . $error . PHP_EOL;
}
$tabObject->current = $tab;
$tabObject->update();
$tabObject->loadTabs();
}
$errors = $tabObject->save();
// Showing errors should be better, enhancement here required.
if (!empty($errors)) {
$show_error = new SimplePluginError($this, htmlescape(sprintf(_('Error : "%s", already exist ! Editing existing tasks is forbidden.'), $subTaskName)));
$show_error->display();
}
}
}
} }
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment