/src/plugins/orangehrmCorePlugin/Dao/AccessFlowStateMachineDao.php

https://github.com/orangehrm/OrangeHRM · PHP · 377 lines · 233 code · 33 blank · 111 comment · 8 complexity · ffe61c2f2b71c42a8370c12935abaaf8 MD5 · raw file

  1. <?php
  2. /**
  3. * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
  4. * all the essential functionalities required for any enterprise.
  5. * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
  6. *
  7. * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
  8. * the GNU General Public License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  12. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with this program;
  16. * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA
  18. */
  19. namespace OrangeHRM\Core\Dao;
  20. use Exception;
  21. use OrangeHRM\Core\Exception\DaoException;
  22. use OrangeHRM\Entity\WorkflowStateMachine;
  23. class AccessFlowStateMachineDao extends BaseDao
  24. {
  25. /**
  26. * @param string $workflow
  27. * @param string|null $state
  28. * @param string $role
  29. * @return WorkflowStateMachine[]|null
  30. * @throws DaoException
  31. */
  32. public function getAllowedActions(string $workflow, ?string $state, string $role): ?array
  33. {
  34. try {
  35. $q = $this->createQueryBuilder(WorkflowStateMachine::class, 'sm');
  36. $q->andWhere('sm.workflow = :workflow')
  37. ->setParameter('workflow', $workflow);
  38. $q->andWhere('sm.role = :role')
  39. ->setParameter('role', $role);
  40. if ($state != null) {
  41. $q->andWhere('sm.state = :state')
  42. ->setParameter('state', $state);
  43. }
  44. $results = $q->getQuery()->execute();
  45. return empty($results) ? null : $results;
  46. } catch (Exception $ex) {
  47. throw new DaoException($ex->getMessage());
  48. }
  49. }
  50. /**
  51. * @param string $workflow
  52. * @param string|null $state
  53. * @param string $role
  54. * @return WorkflowStateMachine[]
  55. * @throws DaoException
  56. */
  57. public function getAllowedWorkflowItems(string $workflow, ?string $state, string $role): array
  58. {
  59. try {
  60. $q = $this->createQueryBuilder(WorkflowStateMachine::class, 'sm');
  61. $q->andWhere('sm.workflow = :workflow')
  62. ->setParameter('workflow', $workflow);
  63. $q->andWhere('sm.role = :role')
  64. ->setParameter('role', $role);
  65. if ($state != null) {
  66. $q->andWhere('sm.state = :state')
  67. ->setParameter('state', $state);
  68. }
  69. return $q->getQuery()->execute();
  70. } catch (Exception $ex) {
  71. throw new DaoException($ex->getMessage());
  72. }
  73. }
  74. /**
  75. * @param string $workflow
  76. * @param string $state
  77. * @param string $role
  78. * @param string $action
  79. * @return WorkflowStateMachine|null
  80. * @throws DaoException
  81. */
  82. public function getNextState(string $workflow, string $state, string $role, string $action): ?WorkflowStateMachine
  83. {
  84. try {
  85. $q = $this->createQueryBuilder(WorkflowStateMachine::class, 'sm');
  86. $q->andWhere('sm.workflow = :workflow')
  87. ->setParameter('workflow', $workflow);
  88. $q->andWhere('sm.state = :state')
  89. ->setParameter('state', $state);
  90. $q->andWhere('sm.action = :action')
  91. ->setParameter('action', $action);
  92. $q->andWhere('sm.role = :role')
  93. ->setParameter('role', $role);
  94. $results = $q->getQuery()->execute();
  95. return $results[0] ?? null;
  96. } catch (Exception $ex) {
  97. throw new DaoException($ex->getMessage());
  98. }
  99. }
  100. /**
  101. * @param string $workflow
  102. * @param string $role
  103. * @param array $actions
  104. * @return WorkflowStateMachine[]|null
  105. * @throws DaoException
  106. */
  107. public function getActionableStates(string $workflow, string $role, array $actions): ?array
  108. {
  109. try {
  110. $q = $this->createQueryBuilder(WorkflowStateMachine::class, 'sm');
  111. $q->andWhere('sm.workflow = :workflow')
  112. ->setParameter('workflow', $workflow);
  113. $q->andWhere('sm.role = :role')
  114. ->setParameter('role', $role);
  115. $q->andWhere($q->expr()->in('sm.action', ':actions'))
  116. ->setParameter('actions', $actions);
  117. $results = $q->getQuery()->execute();
  118. return empty($results) ? null : $results;
  119. } catch (Exception $ex) {
  120. throw new DaoException($ex->getMessage());
  121. }
  122. }
  123. /**
  124. * @param WorkflowStateMachine $workflowStateMachine
  125. * @return WorkflowStateMachine
  126. * @throws DaoException
  127. */
  128. public function saveWorkflowStateMachineRecord(WorkflowStateMachine $workflowStateMachine): WorkflowStateMachine
  129. {
  130. try {
  131. $this->persist($workflowStateMachine);
  132. return $workflowStateMachine;
  133. } catch (Exception $ex) {
  134. throw new DaoException($ex->getMessage());
  135. }
  136. }
  137. /**
  138. * @param WorkflowStateMachine[] $workflowStateMachineRecordArray
  139. * @return WorkflowStateMachine[]
  140. * @throws DaoException
  141. */
  142. public function saveWorkflowStateMachineRecordAsArray(array $workflowStateMachineRecordArray): array
  143. {
  144. try {
  145. foreach ($workflowStateMachineRecordArray as $workflowStateMachineRecord) {
  146. $this->getEntityManager()->persist($workflowStateMachineRecord);
  147. }
  148. $this->getEntityManager()->flush();
  149. return $workflowStateMachineRecordArray;
  150. } catch (Exception $ex) {
  151. throw new DaoException($ex->getMessage());
  152. }
  153. }
  154. /**
  155. * @param string $workflow
  156. * @param string $state
  157. * @param string $role
  158. * @param string $action
  159. * @param string $resultingState
  160. * @return bool
  161. * @throws DaoException
  162. */
  163. public function deleteWorkflowStateMachineRecord(
  164. string $workflow,
  165. string $state,
  166. string $role,
  167. string $action,
  168. string $resultingState
  169. ): bool {
  170. try {
  171. $q = $this->createQueryBuilder(WorkflowStateMachine::class, 'sm');
  172. $q->delete();
  173. $q->andWhere('sm.workflow = :workflow')
  174. ->setParameter('workflow', $workflow);
  175. $q->andWhere('sm.state = :state')
  176. ->setParameter('state', $state);
  177. $q->andWhere('sm.action = :action')
  178. ->setParameter('action', $action);
  179. $q->andWhere('sm.role = :role')
  180. ->setParameter('role', $role);
  181. $q->andWhere('sm.resultingState = :resultingState')
  182. ->setParameter('resultingState', $resultingState);
  183. $result = $q->getQuery()->execute();
  184. return !empty($result);
  185. } catch (Exception $e) {
  186. throw new DaoException($e->getMessage());
  187. }
  188. }
  189. /**
  190. * @param string|null $workflow
  191. * @param string $role
  192. * @return int
  193. * @throws DaoException
  194. */
  195. public function deleteWorkflowRecordsForUserRole(?string $workflow, string $role): int
  196. {
  197. try {
  198. $q = $this->createQueryBuilder(WorkflowStateMachine::class, 'sm');
  199. $q->delete();
  200. $q->andWhere('sm.role = :role')
  201. ->setParameter('role', $role);
  202. if (!is_null($workflow)) {
  203. $q->andWhere('sm.workflow = :workflow')
  204. ->setParameter('workflow', $workflow);
  205. }
  206. return $q->getQuery()->execute();
  207. } catch (Exception $e) {
  208. throw new DaoException($e->getMessage(), $e->getCode(), $e);
  209. }
  210. }
  211. /**
  212. * @param string $oldRoleName
  213. * @param string $newRoleName
  214. * @return int
  215. * @throws DaoException
  216. */
  217. public function handleUserRoleRename(string $oldRoleName, string $newRoleName): int
  218. {
  219. try {
  220. $q = $this->createQueryBuilder(WorkflowStateMachine::class, 'sm');
  221. $q->update();
  222. $q->set('sm.role', ':role')
  223. ->setParameter('role', $newRoleName);
  224. $q->andWhere('sm.role = :oldRoleName')
  225. ->setParameter('oldRoleName', $oldRoleName);
  226. return $q->getQuery()->execute();
  227. } catch (Exception $e) {
  228. throw new DaoException($e->getMessage(), $e->getCode(), $e);
  229. }
  230. }
  231. /**
  232. * @param string $workflow
  233. * @param string $role
  234. * @return WorkflowStateMachine[]|null
  235. * @throws DaoException
  236. */
  237. public function getAllAlowedRecruitmentApplicationStates(string $workflow, string $role): ?array
  238. {
  239. try {
  240. $q = $this->createQueryBuilder(WorkflowStateMachine::class, 'sm');
  241. $q->andWhere('sm.workflow = :workflow')
  242. ->setParameter('workflow', $workflow);
  243. $q->andWhere('sm.role = :role')
  244. ->setParameter('role', $role);
  245. $results = $q->getQuery()->execute();
  246. return empty($results) ? null : $results;
  247. } catch (Exception $e) {
  248. throw new DaoException($e->getMessage());
  249. }
  250. }
  251. /**
  252. * @param string $workflow
  253. * @param string|null $role
  254. * @return WorkflowStateMachine[]
  255. * @throws DaoException
  256. */
  257. public function getWorkFlowStateMachineRecords(string $workflow, ?string $role = null): array
  258. {
  259. try {
  260. $q = $this->createQueryBuilder(WorkflowStateMachine::class, 'sm');
  261. $q->andWhere('sm.workflow = :workflow')
  262. ->setParameter('workflow', $workflow);
  263. if ($role != null) {
  264. $q->andWhere('sm.role = :role')
  265. ->setParameter('role', $role);
  266. }
  267. return $q->getQuery()->execute();
  268. } catch (Exception $e) {
  269. throw new DaoException($e->getMessage());
  270. }
  271. }
  272. /**
  273. *
  274. * @param string $workflow
  275. * @param string $state
  276. * @param string $role
  277. * @param string $action
  278. * @return bool
  279. * @throws DaoException
  280. */
  281. public function isActionAllowed(string $workflow, string $state, string $role, string $action): bool
  282. {
  283. try {
  284. $q = $this->createQueryBuilder(WorkflowStateMachine::class, 'sm');
  285. $q->andWhere('sm.workflow = :workflow')
  286. ->setParameter('workflow', $workflow);
  287. $q->andWhere('sm.state = :state')
  288. ->setParameter('state', $state);
  289. $q->andWhere('sm.role = :role')
  290. ->setParameter('role', $role);
  291. $q->andWhere('sm.action = :action')
  292. ->setParameter('action', $action);
  293. return $this->count($q) > 0;
  294. } catch (Exception $ex) {
  295. throw new DaoException($ex->getMessage());
  296. }
  297. }
  298. /**
  299. * @param int $id
  300. * @return WorkflowStateMachine|null
  301. * @throws DaoException
  302. */
  303. public function getWorkflowItem(int $id): ?WorkflowStateMachine
  304. {
  305. try {
  306. $workflowStateMachine = $this->getRepository(WorkflowStateMachine::class)->find($id);
  307. if ($workflowStateMachine instanceof WorkflowStateMachine) {
  308. return $workflowStateMachine;
  309. }
  310. return null;
  311. } catch (Exception $e) {
  312. throw new DaoException($e->getMessage());
  313. }
  314. }
  315. /**
  316. * @param string $workflow
  317. * @param string $state
  318. * @param string $action
  319. * @param string $role
  320. * @return WorkflowStateMachine|null
  321. * @throws DaoException
  322. */
  323. public function getWorkflowItemByStateActionAndRole(
  324. string $workflow,
  325. string $state,
  326. string $action,
  327. string $role
  328. ): ?WorkflowStateMachine {
  329. try {
  330. $q = $this->createQueryBuilder(WorkflowStateMachine::class, 'sm');
  331. $q->andWhere('sm.workflow = :workflow')
  332. ->setParameter('workflow', $workflow);
  333. $q->andWhere('sm.state = :state')
  334. ->setParameter('state', $state);
  335. $q->andWhere('sm.role = :role')
  336. ->setParameter('role', $role);
  337. $q->andWhere('sm.action = :action')
  338. ->setParameter('action', $action);
  339. return $this->fetchOne($q);
  340. } catch (Exception $e) {
  341. throw new DaoException($e->getMessage());
  342. }
  343. }
  344. }