WorkflowRepository.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace WorkflowBundle\Repository;
  3. use \Doctrine\ORM\EntityRepository;
  4. class WorkflowRepository extends EntityRepository
  5. {
  6. /**
  7. * Retorna los workflow habilitados (enable=true) y
  8. * que estén por defecto (usedByDefault=true) según la clase
  9. * si no encuentra, retorna los habilitados
  10. *
  11. * @param string $class
  12. *
  13. * @return array
  14. */
  15. public function findAllByClass($class)
  16. {
  17. $qb = $this->createQueryBuilder('Workflow')
  18. ->where('Workflow.enable = :enable')->setParameter('enable', true);
  19. $results = $qb->andWhere('Workflow.usedByDefault = :usedByDefault')
  20. ->setParameter('usedByDefault', true)
  21. ->getQuery()->getResult();
  22. // No hay workflow enable y usedByDefault, busco los enable
  23. if (count($results) == 0) {
  24. $results = $qb->getQuery()->getResult();
  25. }
  26. foreach ($results as $key => &$result) {
  27. if (!in_array($class, $result->getSupport())) {
  28. unset($results[$key]);
  29. }
  30. }
  31. return $results;
  32. }
  33. /**
  34. * Retorna los workflow soportados por $class
  35. *
  36. * @param string $class
  37. *
  38. * @return array
  39. */
  40. public function findAllSupportedBy($class, $id = null)
  41. {
  42. $qb = $this->createQueryBuilder('Workflow')
  43. ->select('Workflow.id, Workflow.support');
  44. if ($id) {
  45. $qb->andWhere('Workflow.id <> :id')->setParameter('id', $id);
  46. }
  47. $results = $qb->getQuery()->getResult();
  48. foreach ($results as $key => &$result) {
  49. if (!in_array($class, $result['support'])) {
  50. unset($results[$key]);
  51. }
  52. }
  53. return array_map(function($result) {
  54. return $result['id'];
  55. }, $results);
  56. }
  57. }