-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40544ce
commit 4be72bd
Showing
14 changed files
with
341 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
* UnitController | ||
* | ||
* @author Pieter Maene <[email protected]> | ||
* @author Pedro Devogelaere <[email protected]> | ||
*/ | ||
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 | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
module/CommonBundle/Resources/views/common/admin/role/delete-all-members.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{% autoescape false %} | ||
{{ result|json_encode }} | ||
{% endautoescape %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
module/CommonBundle/Resources/views/common/admin/unit/csv-upload.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
Oops, something went wrong.