From 4be72bdc7ce3ac591fc08e16714252f4017a915c Mon Sep 17 00:00:00 2001 From: pedrod Date: Thu, 19 Oct 2023 14:43:48 +0000 Subject: [PATCH 1/9] members upload action --- .../Controller/Admin/RoleController.php | 31 ++++ .../Controller/Admin/UnitController.php | 137 ++++++++++++++++++ module/CommonBundle/Form/Admin/Unit/Csv.php | 51 +++++++ .../Resources/config/install/acl.config.php | 4 +- .../common/admin/role/delete-all-members.twig | 3 + .../views/common/admin/role/manage.twig | 83 +++++++++++ .../views/common/admin/unit/csv-upload.twig | 14 ++ .../views/common/admin/unit/manage.twig | 9 ++ .../admin/unit/partials/navigation.twig | 3 + .../views/common/admin/unit/template.twig | 1 + .../Controller/Admin/ArticleController.php | 1 - .../Form/Admin/Article/Csv.php | 8 +- .../Form/Admin/Article/Edit.php | 2 +- .../views/logistics/admin/order/view.twig | 9 -- 14 files changed, 341 insertions(+), 15 deletions(-) create mode 100644 module/CommonBundle/Form/Admin/Unit/Csv.php create mode 100644 module/CommonBundle/Resources/views/common/admin/role/delete-all-members.twig create mode 100644 module/CommonBundle/Resources/views/common/admin/unit/csv-upload.twig create mode 100644 module/CommonBundle/Resources/views/common/admin/unit/template.twig diff --git a/module/CommonBundle/Controller/Admin/RoleController.php b/module/CommonBundle/Controller/Admin/RoleController.php index cd2ed9563b..3791ab2e48 100644 --- a/module/CommonBundle/Controller/Admin/RoleController.php +++ b/module/CommonBundle/Controller/Admin/RoleController.php @@ -185,6 +185,37 @@ public function deleteMemberAction() ); } + // This function deletes all members belonging to a role belonging to a unit, like this each year the roles can be + // updated more easily + public function deleteAllMembersAction() + { + $this->initAjax(); + + $units = $this->getEntityManager() + ->getRepository('CommonBundle\Entity\General\Organization\Unit') + ->findAll(); + + foreach ($units as $unit) { + $roles = $unit->getRoles(); + foreach ($roles as $role) { + $users = $this->getEntityManager() + ->getRepository('CommonBundle\Entity\User\Person') + ->findAllByRole($role); + foreach ($users as $user) { + $user->removeRole($role); + } + } + } + + $this->getEntityManager()->flush(); + + return new ViewModel( + array( + 'result' => (object) array('status' => 'success'), + ) + ); + } + public function pruneAction() { $roles = $this->getEntityManager() diff --git a/module/CommonBundle/Controller/Admin/UnitController.php b/module/CommonBundle/Controller/Admin/UnitController.php index 97d24f952f..ed49983896 100644 --- a/module/CommonBundle/Controller/Admin/UnitController.php +++ b/module/CommonBundle/Controller/Admin/UnitController.php @@ -18,6 +18,7 @@ * UnitController * * @author Pieter Maene + * @author Pedro Devogelaere */ class UnitController extends \CommonBundle\Component\Controller\ActionController\AdminController { @@ -410,6 +411,142 @@ public function pruneAction() return new ViewModel(); } + public function csvUploadAction() + { + $form = $this->getForm('common_unit_csv'); + + $academicYear = $this->getAcademicYearEntity(); + if ($academicYear === null) { + return new ViewModel(); + } + + if ($this->getRequest()->isPost()) { + $formData = $this->getRequest()->getPost(); + $fileData = $this->getRequest()->getFiles(); + + $fileName = $fileData['file']['tmp_name']; + + $membersArray = array(); + + $open = fopen($fileName, 'r'); + if ($open != false) { + $data = fgetcsv($open, 10000, ','); + + while ($data !== false) { + $membersArray[] = $data; + $data = fgetcsv($open, 10000, ','); + } + fclose($open); + } + + $form->setData($formData); + + if ($form->isValid()) { + $count = 0; + + array_shift($membersArray); // Remove header + $total = count($membersArray); + foreach ($membersArray as $data) { + if (in_array(null, array_slice($data, 0, 3))) { + error_log('fail'); + continue; + } + + $name = $data[0]; + $academic = $this->getEntityManager() + ->getRepository('CommonBundle\Entity\User\Person\Academic') + ->findOneByUsername($data[1]); + $unit = $this->getEntityManager() + ->getRepository('CommonBundle\Entity\General\Organization\Unit') + ->findOneById($data[2]); + $description = $data[3]; + $coordinator = $data[4]?:0; + + $repositoryCheck = $this->getEntityManager() + ->getRepository('CommonBundle\Entity\User\Person\Organization\UnitMap\Academic') + ->findOneBy( + array( + 'unit' => $unit, + 'academic' => $academic, + 'academicYear' => $academicYear, + ) + ); + + if ($repositoryCheck === null) { + $member = new UnitMapAcademic($academic, $academicYear, $unit, $coordinator, $description); + + $this->getEntityManager()->persist($member); + } + + $count += 1; + } + $this->getEntityManager()->flush(); + + $this->flashMessenger()->success( + 'Succes', + $count . '/' . $total . ' members imported' + ); + + $this->redirect()->toRoute( + 'common_admin_unit', + array( + 'action' => 'manage', + ) + ); + + return new ViewModel(); + } + } + + return new ViewModel( + array( + 'form' => $form, + ) + ); + } + + public function templateAction() + { + $file = new CsvFile(); + + $unit = $this->getUnitEntity(); + + $heading = array( + 'name', + 'r-number', + 'unit id', + 'description', + 'coordinator (bool)', + ); + + $results = array(); + $results[] = array( + '', + '', + $unit->getId(), + '', + '', + ); + + $document = new CsvGenerator($heading, $results); + $document->generateDocument($file); + + $headers = new Headers(); + $headers->addHeaders( + array( + 'Content-Disposition' => 'attachment; filename="unit_members_template.csv"', + 'Content-Type' => 'text/csv', + ) + ); + $this->getResponse()->setHeaders($headers); + + return new ViewModel( + array( + 'data' => $file->getContent(), + ) + ); + } + /** * @return Unit|null */ diff --git a/module/CommonBundle/Form/Admin/Unit/Csv.php b/module/CommonBundle/Form/Admin/Unit/Csv.php new file mode 100644 index 0000000000..a521d16d4f --- /dev/null +++ b/module/CommonBundle/Form/Admin/Unit/Csv.php @@ -0,0 +1,51 @@ + + */ +class Csv extends \CommonBundle\Component\Form\Admin\Form +{ + const FILE_SIZE = '10MB'; + + protected $hydrator = 'CommonBundle\Hydrator\General\Organization\Academic'; + + public function init() + { + parent::init(); + + $this->add( + array( + 'type' => 'file', + 'name' => 'file', + 'label' => 'Members csv', + 'attributes' => array( + 'data-help' => 'The maximum file size is ' . self::FILE_SIZE . '.', + ), + 'options' => array( + 'input' => array( + 'validators' => array( + array( + 'name' => 'FileSize', + 'options' => array( + 'max' => self::FILE_SIZE, + ), + ), + array( + 'name' => 'FileExtension', + 'options' => array( + 'extension' => 'csv', + ), + ), + ), + ), + ), + ) + ); + + $this->addSubmit('Add', 'member_csv file_add'); + } +} \ No newline at end of file diff --git a/module/CommonBundle/Resources/config/install/acl.config.php b/module/CommonBundle/Resources/config/install/acl.config.php index 352a1bca6e..91bf0c746e 100644 --- a/module/CommonBundle/Resources/config/install/acl.config.php +++ b/module/CommonBundle/Resources/config/install/acl.config.php @@ -24,13 +24,13 @@ 'typeahead', ), 'common_admin_role' => array( - 'add', 'edit', 'delete', 'deleteMember', 'manage', 'members', 'prune', + 'add', 'edit', 'delete', 'deleteMember', 'deleteAllMembers', 'manage', 'members', 'prune', ), 'common_admin_session' => array( 'expire', ), 'common_admin_unit' => array( - 'add', 'delete', 'deleteMember', 'edit', 'manage', 'members', 'prune', 'csv' + 'add', 'delete', 'deleteMember', 'edit', 'manage', 'members', 'prune', 'csv', 'csvUpload', 'template', ), 'common_admin_visit' => array( 'manage', 'search', diff --git a/module/CommonBundle/Resources/views/common/admin/role/delete-all-members.twig b/module/CommonBundle/Resources/views/common/admin/role/delete-all-members.twig new file mode 100644 index 0000000000..54f0735f49 --- /dev/null +++ b/module/CommonBundle/Resources/views/common/admin/role/delete-all-members.twig @@ -0,0 +1,3 @@ +{% autoescape false %} + {{ result|json_encode }} +{% endautoescape %} diff --git a/module/CommonBundle/Resources/views/common/admin/role/manage.twig b/module/CommonBundle/Resources/views/common/admin/role/manage.twig index adafa38821..0a282fe84e 100644 --- a/module/CommonBundle/Resources/views/common/admin/role/manage.twig +++ b/module/CommonBundle/Resources/views/common/admin/role/manage.twig @@ -18,6 +18,19 @@ +
+
Success
+
+

The members were successfully removed!

+
+
+
+
Error
+
+

An error occurred while trying to delete the members.

+
+
+
@@ -74,6 +87,30 @@ + + {% if hasAccess('common_admin_role', 'prune') %} {% endif %} + + {% if hasAccess('common_admin_role', 'deleteAllMembers') %} + + {% endif %} {% endblock %} {% block content_script %} {% endblock %} diff --git a/module/CommonBundle/Resources/views/common/admin/unit/csv-upload.twig b/module/CommonBundle/Resources/views/common/admin/unit/csv-upload.twig new file mode 100644 index 0000000000..0fbf035feb --- /dev/null +++ b/module/CommonBundle/Resources/views/common/admin/unit/csv-upload.twig @@ -0,0 +1,14 @@ +{% extends 'admin/base.twig' %} + +{% block content %} + {% include 'common/admin/unit/partials/navigation.twig' %} + + {% include 'common/admin/unit/partials/manage-years.twig' %} + + {% include 'admin/partials/flashMessenger.twig' %} + +
+ {% import 'admin/partials/form.twig' as forms %} + {{ forms.renderForm(form) }} +
+{% endblock %} diff --git a/module/CommonBundle/Resources/views/common/admin/unit/manage.twig b/module/CommonBundle/Resources/views/common/admin/unit/manage.twig index 9665c0d531..3f78ecc8ff 100644 --- a/module/CommonBundle/Resources/views/common/admin/unit/manage.twig +++ b/module/CommonBundle/Resources/views/common/admin/unit/manage.twig @@ -36,6 +36,9 @@ + + + + {% endfor %} +
{{ unit.getName() }} {% if unit.isActive() %}×{% endif %} + {% if hasAccess('common_admin_unit', 'template') %} + Template + {% endif %} {% if hasAccess('common_admin_unit', 'members') %} Members {% endif %} @@ -68,6 +71,9 @@ {{ workgroup.getName() }} {% if workgroup.isActive() %}×{% endif %} + {% if hasAccess('common_admin_unit', 'template') %} + Template + {% endif %} {% if hasAccess('common_admin_unit', 'members') %} Members {% endif %} @@ -100,6 +106,9 @@ {{ unit.getName() }} {% if unit.isActive() %}×{% endif %} + {% if hasAccess('common_admin_unit', 'template') %} + Template + {% endif %} {% if hasAccess('common_admin_unit', 'members') %} Members {% endif %} diff --git a/module/CommonBundle/Resources/views/common/admin/unit/partials/navigation.twig b/module/CommonBundle/Resources/views/common/admin/unit/partials/navigation.twig index 59fcf497d5..5a419ee3ee 100644 --- a/module/CommonBundle/Resources/views/common/admin/unit/partials/navigation.twig +++ b/module/CommonBundle/Resources/views/common/admin/unit/partials/navigation.twig @@ -6,6 +6,9 @@ {% if hasAccess('common_admin_unit', 'csv') %}
  • CSV
  • {% endif %} + {% if hasAccess('common_admin_unit', 'csvUpload') %} +
  • CSV Upload Members
  • + {% endif %} {% if hasAccess('common_admin_unit', 'add') %}
  • Add
  • {% endif %} diff --git a/module/CommonBundle/Resources/views/common/admin/unit/template.twig b/module/CommonBundle/Resources/views/common/admin/unit/template.twig new file mode 100644 index 0000000000..1930e6cabc --- /dev/null +++ b/module/CommonBundle/Resources/views/common/admin/unit/template.twig @@ -0,0 +1 @@ +{{ data|raw }} diff --git a/module/LogisticsBundle/Controller/Admin/ArticleController.php b/module/LogisticsBundle/Controller/Admin/ArticleController.php index 5ae980387a..36b9e7f747 100644 --- a/module/LogisticsBundle/Controller/Admin/ArticleController.php +++ b/module/LogisticsBundle/Controller/Admin/ArticleController.php @@ -201,7 +201,6 @@ public function csvAction() $unit = $this->getEntityManager() ->getRepository('CommonBundle\Entity\General\Organization\Unit') ->findOneById($formData['unit']); - error_log($unit->getName()); array_shift($articleArray); // Remove header foreach ($articleArray as $data) { diff --git a/module/LogisticsBundle/Form/Admin/Article/Csv.php b/module/LogisticsBundle/Form/Admin/Article/Csv.php index 28ad4895f8..44a65d213b 100644 --- a/module/LogisticsBundle/Form/Admin/Article/Csv.php +++ b/module/LogisticsBundle/Form/Admin/Article/Csv.php @@ -1,7 +1,11 @@ + */ class Csv extends \CommonBundle\Component\Form\Admin\Form { const FILE_SIZE = '10MB'; @@ -60,7 +64,7 @@ public function init() ) ); - $this->addSubmit('Add', 'article_csv'); + $this->addSubmit('Add', 'article_csv file_add'); } /** diff --git a/module/LogisticsBundle/Form/Admin/Article/Edit.php b/module/LogisticsBundle/Form/Admin/Article/Edit.php index 2a1409b562..554e10abd0 100644 --- a/module/LogisticsBundle/Form/Admin/Article/Edit.php +++ b/module/LogisticsBundle/Form/Admin/Article/Edit.php @@ -7,7 +7,7 @@ * * @author Niels Avonds */ -class Edit extends \LogisticsBundle\Form\Admin\Article\Add +class Edit extends \CommonBundle\Form\Admin\Unit\Add { public function init() { diff --git a/module/LogisticsBundle/Resources/views/logistics/admin/order/view.twig b/module/LogisticsBundle/Resources/views/logistics/admin/order/view.twig index 0c08a34831..90c794b95c 100644 --- a/module/LogisticsBundle/Resources/views/logistics/admin/order/view.twig +++ b/module/LogisticsBundle/Resources/views/logistics/admin/order/view.twig @@ -213,10 +213,6 @@ @@ -234,11 +230,6 @@
    {{ forms.renderForm(articleForm) }}
    - {#
    {{ forms.renderForm(articleForm) }}
    #} -{# #} From 644888df4781c7f03a4dc7a8c68d44b9fda2838b Mon Sep 17 00:00:00 2001 From: pedrod Date: Thu, 19 Oct 2023 15:28:16 +0000 Subject: [PATCH 2/9] display both direct and unit members in roles --- .../Controller/Admin/RoleController.php | 97 ++++++++++++------- .../Resources/config/install/acl.config.php | 2 +- .../common/admin/role/delete-all-members.twig | 3 - .../views/common/admin/role/members.twig | 17 +++- .../admin/unit/partials/navigation.twig | 2 +- 5 files changed, 81 insertions(+), 40 deletions(-) delete mode 100644 module/CommonBundle/Resources/views/common/admin/role/delete-all-members.twig diff --git a/module/CommonBundle/Controller/Admin/RoleController.php b/module/CommonBundle/Controller/Admin/RoleController.php index 3791ab2e48..c069d3fadb 100644 --- a/module/CommonBundle/Controller/Admin/RoleController.php +++ b/module/CommonBundle/Controller/Admin/RoleController.php @@ -3,8 +3,10 @@ namespace CommonBundle\Controller\Admin; use CommonBundle\Component\Acl\Acl; +use CommonBundle\Component\Util\AcademicYear; use CommonBundle\Entity\Acl\Action; use CommonBundle\Entity\Acl\Role; +use CommonBundle\Entity\General\AcademicYear as AcademicYearEntity; use CommonBundle\Entity\User\Person; use Laminas\View\Model\ViewModel; @@ -80,14 +82,39 @@ public function membersAction() return new ViewModel(); } + $academicYear = $this->getAcademicYearEntity(); + if ($academicYear === null) { + return new ViewModel(); + } + $members = $this->getEntityManager() ->getRepository('CommonBundle\Entity\User\Person') ->findAllByRole($role); + $units = $this->getEntityManager() + ->getRepository('CommonBundle\Entity\General\Organization\Unit') + ->findAll(); + $unitMembers = array(); + foreach ($units as $unit) { + $unitRoles = $unit->getRoles(); + foreach ($unitRoles as $unitRole) { + if ($unitRole == $role) { + $unitMembers = array_unique(array_merge( + $unitMembers, + $this->getEntityManager() + ->getRepository('CommonBundle\Entity\User\Person\Organization\UnitMap') + ->findBy(array('unit' => $unit, 'academicYear' => $academicYear)) + ), + SORT_REGULAR); + } + } + } + return new ViewModel( array( - 'role' => $role, - 'members' => $members, + 'role' => $role, + 'members' => $members, + 'unitMembers' => $unitMembers, ) ); } @@ -185,37 +212,6 @@ public function deleteMemberAction() ); } - // This function deletes all members belonging to a role belonging to a unit, like this each year the roles can be - // updated more easily - public function deleteAllMembersAction() - { - $this->initAjax(); - - $units = $this->getEntityManager() - ->getRepository('CommonBundle\Entity\General\Organization\Unit') - ->findAll(); - - foreach ($units as $unit) { - $roles = $unit->getRoles(); - foreach ($roles as $role) { - $users = $this->getEntityManager() - ->getRepository('CommonBundle\Entity\User\Person') - ->findAllByRole($role); - foreach ($users as $user) { - $user->removeRole($role); - } - } - } - - $this->getEntityManager()->flush(); - - return new ViewModel( - array( - 'result' => (object) array('status' => 'success'), - ) - ); - } - public function pruneAction() { $roles = $this->getEntityManager() @@ -301,6 +297,41 @@ private function getPersonEntity() return $person; } + /** + * @return \CommonBundle\Entity\General\AcademicYear|null + */ + private function getAcademicYearEntity() + { + if ($this->getParam('academicyear') === null) { + return $this->getCurrentAcademicYear(); + } + + $start = AcademicYear::getDateTime($this->getParam('academicyear')); + $start->setTime(0, 0); + + $academicYear = $this->getEntityManager() + ->getRepository('CommonBundle\Entity\General\AcademicYear') + ->findOneByUniversityStart($start); + + if (!($academicYear instanceof AcademicYearEntity)) { + $this->flashMessenger()->error( + 'Error', + 'No academic year was found!' + ); + + $this->redirect()->toRoute( + 'common_admin_unit', + array( + 'action' => 'manage', + ) + ); + + return; + } + + return $academicYear; + } + /** * @return null */ diff --git a/module/CommonBundle/Resources/config/install/acl.config.php b/module/CommonBundle/Resources/config/install/acl.config.php index 91bf0c746e..af3ceb7f7b 100644 --- a/module/CommonBundle/Resources/config/install/acl.config.php +++ b/module/CommonBundle/Resources/config/install/acl.config.php @@ -24,7 +24,7 @@ 'typeahead', ), 'common_admin_role' => array( - 'add', 'edit', 'delete', 'deleteMember', 'deleteAllMembers', 'manage', 'members', 'prune', + 'add', 'edit', 'delete', 'deleteMember', 'manage', 'members', 'prune', ), 'common_admin_session' => array( 'expire', diff --git a/module/CommonBundle/Resources/views/common/admin/role/delete-all-members.twig b/module/CommonBundle/Resources/views/common/admin/role/delete-all-members.twig deleted file mode 100644 index 54f0735f49..0000000000 --- a/module/CommonBundle/Resources/views/common/admin/role/delete-all-members.twig +++ /dev/null @@ -1,3 +0,0 @@ -{% autoescape false %} - {{ result|json_encode }} -{% endautoescape %} diff --git a/module/CommonBundle/Resources/views/common/admin/role/members.twig b/module/CommonBundle/Resources/views/common/admin/role/members.twig index 3b1da9e7db..7742ec4aa7 100644 --- a/module/CommonBundle/Resources/views/common/admin/role/members.twig +++ b/module/CommonBundle/Resources/views/common/admin/role/members.twig @@ -22,12 +22,12 @@

    Role

    Name: {{ role.getName() }}

    -

    Members

    +

    Direct members

    - + {% for member in members %} @@ -40,6 +40,19 @@ {% endfor %}
    NameActionsActions
    + +

    Unit members

    + + + + + + {% for member in unitMembers %} + + + + {% endfor %} +
    Name
    {{ member.getFullName() }}
    -
    -
    Success
    -
    -

    The members were successfully removed!

    -
    -
    -
    -
    Error
    -
    -

    An error occurred while trying to delete the members.

    -
    -
    -
    @@ -126,31 +113,12 @@ {% endif %} - - {% if hasAccess('common_admin_role', 'deleteAllMembers') %} - - {% endif %} {% endblock %} {% block content_script %} {% endblock %} diff --git a/module/CommonBundle/Resources/views/common/admin/role/members.twig b/module/CommonBundle/Resources/views/common/admin/role/members.twig index 7742ec4aa7..c711577224 100644 --- a/module/CommonBundle/Resources/views/common/admin/role/members.twig +++ b/module/CommonBundle/Resources/views/common/admin/role/members.twig @@ -22,37 +22,41 @@

    Role

    Name: {{ role.getName() }}

    -

    Direct members

    + {% if members %} +

    Direct members

    -
    - - - - - {% for member in members %} - - - +
    NameActions
    {{ member.getFullName() }} - {% if hasAccess('common_admin_role', 'deleteMember') %} - Delete - {% endif %} -
    + + + - {% endfor %} -
    NameActions
    + {% for member in members %} +
    {{ member.getFullName() }} + {% if hasAccess('common_admin_role', 'deleteMember') %} + Delete + {% endif %} +
    + {% endif %} -

    Unit members

    + {% if unitMembers %} +

    Unit members

    - - - - - {% for member in unitMembers %} - - +
    Name
    {{ member.getFullName() }}
    + + - {% endfor %} -
    Name
    + {% for member in unitMembers %} + + {{ member.getFullName() }} + + {% endfor %} + + {% endif %}