Add user hub assignments #17

This commit is contained in:
Ramon Gutierrez 2018-02-18 17:46:10 +08:00
parent 137a46d3ce
commit fb0b66de3d
4 changed files with 106 additions and 3 deletions

View file

@ -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);

View file

@ -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();
}
}

View file

@ -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;

View file

@ -113,7 +113,8 @@
{% endif %}
</div>
{% if mode != 'profile' %}
<div class="m-form__section m-form__section--last">
<div class="m-form__seperator m-form__seperator--dashed"></div>
<div class="m-form__section">
<div class="m-form__heading">
<h3 class="m-form__heading-title">
Roles
@ -138,6 +139,29 @@
</div>
</div>
</div>
<div class="m-form__seperator m-form__seperator--dashed"></div>
<div class="m-form__section m-form__section--last">
<div class="m-form__heading">
<h3 class="m-form__heading-title">
Hubs
</h3>
</div>
<div class="form-group m-form__group row">
<div class="col-lg-12">
<div class="m-checkbox-list">
{% for hub in hubs %}
<label class="m-checkbox">
<input type="checkbox" name="hubs[]" value="{{ hub.getID() }}"{{ hub.getID() in obj.getHubs() ? ' checked' : '' }}>
{{ hub.getFullName() }}
<span></span>
</label>
{% endfor %}
</div>
<div class="form-control-feedback hide" data-field="hubs"></div>
<span class="m-form__help">Check all hubs assigned to this user</span>
</div>
</div>
</div>
{% endif %}
</div>
<div class="m-portlet__foot m-portlet__foot--fit">