| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- dol_include_once('/documentfollower/class/documentfollowerprocess.class.php');
- dol_include_once('/documentfollower/class/dfprocessuserhandler.class.php');
- dol_include_once('/documentfollower/class/documentfollowerprocessstate.class.php');
- dol_include_once('/documentfollower/class/dfprocesslevelemailsender.class.php');
- dol_include_once('/documentfollower/class/dfprocesscloseemail.class.php');
- dol_include_once('/documentfollower/class/dfprocessloghandler.class.php');
- /**
- *
- */
- class DfProcessAjaxHandler
- {
- const OPERATION_ASSIGN = 1;
- const OPERATION_SIGN = 2;
- const OPERATION_RESTORE = 3;
- public $db;
- public $errors;
- public $user;
- public $process;
- public $operation;
- public $userLevel;
- public $level;
- public $description;
- /**
- *
- */
- public function __construct(DocumentFollowerProcess $process, int $operation, int $level= null, string $description = null)
- {
- global $db, $user;
- $this->db = $db;
- $this->user = $user;
- $this->process = $process;
- $this->operation = $operation;
- $this->level = $level;
- $this->description = $description;
- }
- /**
- *
- */
- public function process(): bool
- {
- $processed = false;
- $this->validateProcess();
- $this->validateUser();
- $this->validateOperation();
-
- if ($this->operation === self::OPERATION_ASSIGN) {
- // not send email
- $this->look();
- $processed = true;
- } else if ($this->operation === self::OPERATION_SIGN) {
- $this->sign();
- $processed = true;
- } else if ($this->operation === self::OPERATION_RESTORE) {
- // send email to user(s)
- $this->returnTo();
- $processed = true;
- }
- return $processed;
- }
- /**
- *
- */
- public function saveState(): int
- {
- $state = new DocumentFollowerProcessState($this->db);
- $state->ref = bin2hex(random_bytes(5));
- $state->status = $this->operation;
- $state->is_active = 1; //($this->level) ? 0 : 1;
- $state->fk_process = $this->process->id;
- $state->return_level = $this->level;
- $state->description = $this->description;
- $id = $state->create($this->user);
- if ($id < 1) {
- throw new \Exception('Save failure');
- }
- return $id;
- }
- /**
- *
- */
- public function getProcessLastLevel(): int
- {
- return (new DfProcessUserHandler($this->process->id))->getLastLevel();
- }
- /**
- *
- */
- public function checkEverybodySignedOnTheCurrentLevel(): bool
- {
- return (new DfProcessHandler)->checkEverybodySignedTheLevel($this->process, $this->userLevel);
- }
- /**
- *
- */
- private function validateUser(): void
- {
- $userHandler = new DfProcessUserHandler($this->process->id);
- $isAssigned = $userHandler->isUserAssignedToProcess($this->user->id);
- if ($isAssigned) {
- $this->userLevel = $userHandler->level;
- // check
- $userIds = $userHandler->getLevelMembers($this->process->id, $this->userLevel);
- if (in_array($this->user->id, $userIds)) {
- // ...
- } else {
- throw new \Exception('User not assigned to process on this level');
- }
- } else {
- throw new \Exception('User not assigned to process');
- }
- }
- /**
- *
- */
- private function validateOperation(): void
- {
- $constants = (new ReflectionClass(__CLASS__))->getConstants();
- if (!in_array($this->operation, $constants)) {
- throw new \Exception('Invalid operation!');
- }
- }
- /**
- *
- */
- private function validateProcess(): void
- {
- if (intval($this->process->status) !== DocumentFollowerProcess::STATUS_VALIDATED
- || $this->process->process_status !== 1) {
- throw new \Exception('Not active process!');
- }
- }
- /**
- *
- */
- public function look(): bool
- {
- $this->saveState();
- return true;
- }
- /**
- *
- */
- public function sign(): bool
- {
- // save signed state
- $this->saveState();
- // send email to next step(s), if everybody sign on this level
- if ($this->checkEverybodySignedOnTheCurrentLevel()) {
- // is it the last level
- if ($this->userLevel == $this->getProcessLastLevel()) {
- // close the process
- $this->closeProcess();
- // send email to process creator
- (new DfProcessCloseEmail)->send($this->process);
- } else {
- // send email to next level user(s)
- $nextLevel = $this->userLevel + 1;
- (new DfProcessLevelEmailSender($this->process, $nextLevel))->sendEmailToLevelMembers();
- }
- }
- return true;
- }
- /**
- *
- */
- public function returnTo(): bool
- {
- $this->saveState();
- $levels = (new DocumentFollowerProcessUser($this->db))->fetchAll('ASC', 'level', 0, 0, [
- 'fk_process' => $this->process->id,
- 'customsql' => 'level >= '.$this->level
- ]);
- if (is_array($levels)) {
- foreach ($levels as $level) {
- if ($level->fk_user) {
- $this->inactivateUserState($level->fk_user);
- } elseif ($level->fk_usergroup) {
- $this->inactivateGroupState($level->fk_usergroup);
- }
- }
- }
- (new DfProcessLevelEmailSender($this->process, $this->level))->sendEmailToLevelMembers();
- return true;
- }
- /**
- *
- */
- private function inactivateUserState(int $userId): void
- {
- $states = (new DocumentFollowerProcessState($this->db))->fetchAll('', '', 0, 0, [
- 'fk_process' => $this->process->id,
- 'is_active' => 1,
- 'fk_user_creat' => $userId,
- 'customsql' => 'status != 3'
- ]);
- if (is_array($states)) {
- $state = reset($states);
- $state->is_active = 0;
- $state->status = 3;
- $state->update($this->user);
- }
- }
- /**
- *
- */
- private function inactivateGroupState(int $userGroupId): void
- {
- $group = (new UserGroup($this->db));
- $group->fetch($userGroupId, '', false);
- $userIds = array_keys($group->listUsersForGroup());
- $states = (new DocumentFollowerProcessState($this->db))->fetchAll('', '', 0, 0, [
- 'fk_process' => $this->process->id,
- 'is_active' => 1,
- 'customsql' => 'fk_user IN ('.implode(',', $userIds).') AND status != 3'
- ]);
- if (is_array($states)) {
- foreach ($states as $state) {
- $state->is_active = 0;
- $state->status = '3';
- $state->update($this->user);
- }
- }
- }
- /**
- *
- */
- private function closeProcess(): void
- {
- $this->process->process_status = 3;
- if ($this->process->update($this->user) < 0) {
- throw new \Exception("The process cannot be closed (ID: {$this->process->id})");
- }
- }
- }
|