NetworkOrganizationRepository.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace AppBundle\Entity\Network\Repository;
  3. use Doctrine\ORM\EntityRepository;
  4. use Doctrine\ORM\Query\ResultSetMapping;
  5. /**
  6. * Description of NetworkOrganizationRepository
  7. *
  8. * @author Sébastien Hupin <sebastien.hupin at gmail.com>
  9. */
  10. class NetworkOrganizationRepository extends EntityRepository
  11. {
  12. public function find($id, $lockMode = null, $lockVersion = null) {
  13. $query = $this->createQueryBuilder('no')
  14. ->select('no')
  15. ->addSelect('n')
  16. ->addSelect('a')
  17. ->join('no.network', 'n')
  18. ->join('n.image', 'a')
  19. ->where('no.id=:id')
  20. ->setParameter('id', $id)
  21. ;
  22. return $query->getQuery()->getOneOrNullResult();
  23. }
  24. public function findOrganizations($id, \DateTime $startEffectiveDate = null,
  25. \DateTime $endEffectiveDate = null, \DateTime $startExitDate = null,
  26. \DateTime $endExitDate = null, $leadingCause = null, $draft = 0,
  27. array $orderBy = null, $limit = null, $offset = null)
  28. {
  29. $organizations = array();
  30. $whereClause = array('n.network_id = :network_id', 'n.draft = :draft');
  31. $parameters = array('network_id' => $id, 'draft'=>$draft);
  32. $orderBy = array_merge((array) $orderBy, array(
  33. 'startDate' => 'ASC'));
  34. $limit = array_merge((array) $limit, (array) $offset);
  35. if (null !== $startEffectiveDate) {
  36. $whereClause[] = 'n.startDate >= :startEffectiveDate';
  37. $parameters['startEffectiveDate'] = $startEffectiveDate->format('Y-m-d');
  38. }
  39. if (null !== $endEffectiveDate) {
  40. $whereClause[] = 'n.startDate <= :endEffectiveDate';
  41. $parameters['endEffectiveDate'] = $endEffectiveDate->format('Y-m-d');
  42. }
  43. if (null !== $startExitDate) {
  44. $whereClause[] = 'n.endDate >= :startExitDate';
  45. $parameters['startExitDate'] = $startExitDate->format('Y-m-d');
  46. }
  47. if (null !== $endExitDate) {
  48. $whereClause[] = 'n.endDate <= :endExitDate';
  49. $parameters['endExitDate'] = $endExitDate->format('Y-m-d');
  50. }
  51. if (null !== $leadingCause) {
  52. $leadingCauseEnum = new \AppBundle\Enum\Network\LeadingCauseEnum($leadingCause);
  53. $whereClause[] = 'n.leadingCause = :leadingCause';
  54. $parameters['leadingCause'] = $leadingCauseEnum->getValue();
  55. }
  56. $sql = sprintf("
  57. SELECT o.id AS id, o.name AS name
  58. FROM Organization AS o
  59. INNER JOIN NetworkOrganization AS n ON n.organization_id = o.id
  60. WHERE %s
  61. ORDER BY %s
  62. %s
  63. ", implode(' AND ', $whereClause),
  64. implode(',', array_map(
  65. function($k, $v) {
  66. return "$k $v";
  67. },
  68. array_keys($orderBy), array_values($orderBy)
  69. )
  70. ),
  71. (2 === count($limit)) ? sprintf('LIMIT %s', implode(',',$limit)) : ''
  72. );
  73. $rsm = new ResultSetMapping();
  74. $rsm->addScalarResult('id', 'id', 'integer');
  75. $rsm->addScalarResult('name', 'name');
  76. $query = $this->getEntityManager()->createNativeQuery($sql, $rsm);
  77. $query->setParameters($parameters);
  78. $result = $query->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
  79. foreach ($result as $o) {
  80. $organization = new \AppBundle\Entity\Organization\Organization();
  81. $organization->setId($o['id']);
  82. $organization->setName($o['name']);
  83. $organizations[] = $organization;
  84. }
  85. return $organizations;
  86. }
  87. /**
  88. * Check if the identifier exist
  89. *
  90. * @param string $networkName
  91. * @param string $identifier
  92. * @return boolean
  93. */
  94. public function checkUniqIdentifier($networkName, $identifier) {
  95. $query = $this->createQueryBuilder('no')
  96. ->select('no')
  97. ->join('no.network', 'n')
  98. ->join('no.organization', 'o')
  99. ->where('n.name=:name')
  100. ->andWhere('o.identifier=:identifier')
  101. ->setParameter('name', $networkName)
  102. ->setParameter('identifier', $identifier)
  103. ;
  104. return $query->getQuery()->getOneOrNullResult() ? false : true;
  105. }
  106. }