diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 2d53dbbb..7875c655 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -5,6 +5,7 @@ namespace App\Controller; use App\Ramcar\BaseController; use App\Entity\User; use App\Entity\Role; +use App\Entity\Hub; use Doctrine\ORM\Query; use Symfony\Component\HttpFoundation\Request; @@ -133,6 +134,7 @@ class UserController extends BaseController // get roles $em = $this->getDoctrine()->getManager(); $params['roles'] = $em->getRepository(Role::class)->findAll(); + $params['hubs'] = $em->getRepository(Hub::class)->findAll(); // response return $this->render('user/form.html.twig', $params); @@ -153,7 +155,8 @@ class UserController extends BaseController ->setEmail($req->request->get('email')) ->setContactNumber($req->request->get('contact_no')) ->setEnabled($req->request->get('enabled') ? true : false) - ->clearRoles(); + ->clearRoles() + ->clearHubs(); // set roles $roles = $req->request->get('roles'); @@ -173,6 +176,19 @@ class UserController extends BaseController } } + // set hubs + $hubs = $req->request->get('hubs'); + + if (!empty($hubs)) { + foreach ($hubs as $hub_id) { + // check if hub exists + $hub = $em->getRepository(Hub::class)->find($hub_id); + + if (!empty($hub)) + $obj->addHub($hub); + } + } + // validate $errors = $validator->validate($obj); @@ -238,6 +254,7 @@ class UserController extends BaseController // get roles $params['roles'] = $em->getRepository(Role::class)->findAll(); + $params['hubs'] = $em->getRepository(Hub::class)->findAll(); $params['obj'] = $obj; @@ -264,7 +281,8 @@ class UserController extends BaseController ->setEmail($req->request->get('email')) ->setContactNumber($req->request->get('contact_no')) ->setEnabled($req->request->get('enabled') ? true : false) - ->clearRoles(); + ->clearRoles() + ->clearHubs(); // set roles $roles = $req->request->get('roles'); @@ -279,6 +297,19 @@ class UserController extends BaseController } } + // set hubs + $hubs = $req->request->get('hubs'); + + if (!empty($hubs)) { + foreach ($hubs as $hub_id) { + // check if hub exists + $hub = $em->getRepository(Hub::class)->find($hub_id); + + if (!empty($hub)) + $obj->addHub($hub); + } + } + // validate $errors = $validator->validate($obj); diff --git a/src/Entity/Hub.php b/src/Entity/Hub.php index 74eb1b41..6baf7d92 100644 --- a/src/Entity/Hub.php +++ b/src/Entity/Hub.php @@ -29,6 +29,11 @@ class Hub */ protected $outlets; + /** + * @ORM\ManyToMany(targetEntity="User", mappedBy="hubs", fetch="EXTRA_LAZY") + */ + protected $users; + public function __construct() { $this->time_open = new DateTime(); @@ -46,4 +51,14 @@ class Hub { return $this->outlets; } + + public function getUsers() + { + return $this->users; + } + + public function getUsersCount() + { + return $this->users->count(); + } } diff --git a/src/Entity/User.php b/src/Entity/User.php index 403c8f5f..88cb8cc9 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -42,6 +42,12 @@ class User implements AdvancedUserInterface, Serializable */ protected $roles; + /** + * @ORM\ManyToMany(targetEntity="Hub", inversedBy="users") + * @ORM\JoinTable(name="user_hubs") + */ + protected $hubs; + /** * @ORM\Column(type="boolean") */ @@ -100,6 +106,7 @@ class User implements AdvancedUserInterface, Serializable public function __construct() { $this->roles = new ArrayCollection(); + $this->hubs = new ArrayCollection(); $this->job_orders_created = new ArrayCollection(); $this->job_orders_assigned = new ArrayCollection(); $this->tickets = new ArrayCollection(); @@ -171,6 +178,32 @@ class User implements AdvancedUserInterface, Serializable return $this->roles; } + public function addHub(Hub $hub) + { + $this->hubs->add($hub); + return $this; + } + + public function clearHubs() + { + $this->hubs->clear(); + return $this; + } + + public function getHubs() + { + $str_hubs = []; + foreach ($this->hubs as $hub) + $str_hubs[] = $hub->getID(); + + return $str_hubs; + } + + public function getHubObjects() + { + return $this->hubs; + } + public function eraseCredentials() { return $this; diff --git a/templates/user/form.html.twig b/templates/user/form.html.twig index 05b4f03c..6cf6076c 100644 --- a/templates/user/form.html.twig +++ b/templates/user/form.html.twig @@ -113,7 +113,8 @@ {% endif %} {% if mode != 'profile' %} -