Skip to content

Commit

Permalink
members upload action
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrodevog committed Oct 19, 2023
1 parent 40544ce commit 4be72bd
Show file tree
Hide file tree
Showing 14 changed files with 341 additions and 15 deletions.
31 changes: 31 additions & 0 deletions module/CommonBundle/Controller/Admin/RoleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
137 changes: 137 additions & 0 deletions module/CommonBundle/Controller/Admin/UnitController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* UnitController
*
* @author Pieter Maene <[email protected]>
* @author Pedro Devogelaere <[email protected]>
*/
class UnitController extends \CommonBundle\Component\Controller\ActionController\AdminController
{
Expand Down Expand Up @@ -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
*/
Expand Down
51 changes: 51 additions & 0 deletions module/CommonBundle/Form/Admin/Unit/Csv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace CommonBundle\Form\Admin\Unit;

/**
* The form used to add multiple members across different units via CSV.
*
* @author Pedro Devogelaere <[email protected]>
*/
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');
}
}
4 changes: 2 additions & 2 deletions module/CommonBundle/Resources/config/install/acl.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% autoescape false %}
{{ result|json_encode }}
{% endautoescape %}
83 changes: 83 additions & 0 deletions module/CommonBundle/Resources/views/common/admin/role/manage.twig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@
</div>
</div>

<div class="flashmessage success_message full_width members_removed_success hide">
<div class="title">Success</div>
<div class="content">
<p>The members were successfully removed!</p>
</div>
</div>
<div class="flashmessage error_message full_width members_removed_error hide">
<div class="title">Error</div>
<div class="content">
<p>An error occurred while trying to delete the members.</p>
</div>
</div>

<div id="controller_action">
<table class="manage">
<thead>
Expand Down Expand Up @@ -74,6 +87,30 @@
</div>
</div>

<div class="modal hide fade" id="removeMembers" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<span>Litus Admin</span>
/Delete Members
</div>
<div class="modal-body">
<p>
You are about to delete all the members from roles linked to units!
Please note that this operation cannot be undone!
</p>
<p>
Are you sure you want to continue?
</p>
<div class="footer">
<button class="delete">Yes</button>
<button class="cancel" data-dismiss="modal">No</button>
</div>
</div>
</div>
</div>
</div>

{% if hasAccess('common_admin_role', 'prune') %}
<aside>
<div class="sidebox">
Expand All @@ -89,12 +126,31 @@
</div>
</aside>
{% endif %}

{% if hasAccess('common_admin_role', 'deleteAllMembers') %}
<aside>
<div class="sidebox">
<div class="title">Delete Members</div>
<div class="content">
<p>
<i>Please hit the link below to delete all members from the roles linked to units!</i>
</p>
<p>
<a href="#" class="delete-members">
&rarr; Delete Members
</a>
</p>
</div>
</div>
</aside>
{% endif %}
{% endblock %}

{% block content_script %}
<script type="text/javascript">
$(document).ready(function () {
$('.item .delete').click(openModal);
$('.delete-members').click(openDeleteMembersModal);
});
function openModal(e) {
Expand All @@ -121,10 +177,37 @@
removeRole.modal();
}
function openDeleteMembersModal(e) {
var $this = $(this);
e.preventDefault();
var removeMembers = $('#removeMembers');
removeMembers.find('.cancel').one('click', function () {
removeMembers.modal('hide');
});
removeMembers.find('.delete').unbind('click').click(function () {
$.post('{{ url('common_admin_role', {"action": "deleteAllMembers"})}}', function (data) {
if (data && 'success' == data.status) {
$('.flashmessage').addClass('hide');
$('.members_removed_success').removeClass('hide');
removeMembers.modal('hide');
} else {
errorRemoveMembers();
}
}, 'json').error(errorRemoveMembers);
});
removeMembers.modal();
}
function errorRemove() {
$('.flashmessage').addClass('hide');
$('.role_removed_error').removeClass('hide');
$('#removeRole').modal('hide');
}
function errorRemoveMembers() {
$('.flashmessage').addClass('hide');
$('.members_removed_error').removeClass('hide');
$('#removeMembers').modal('hide');
}
</script>
{% endblock %}
Original file line number Diff line number Diff line change
@@ -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' %}

<div id="controller_action">
{% import 'admin/partials/form.twig' as forms %}
{{ forms.renderForm(form) }}
</div>
{% endblock %}
Loading

0 comments on commit 4be72bd

Please sign in to comment.