<?php
namespace App\Controller\Accounting;
use Sonata\AdminBundle\Controller\CRUDController as BaseController;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class CertificateAdminController extends BaseController
{
public function editAction($id = null)
{
$user = $this->container->get('security.token_storage')->getToken()->getUser();
$object = $this->admin->getObject($id);
if ($user->isAccountant() && $object->getUser()->getManager()->getId() != $user->getAccountant()->getId()) {
die('Este cliente no es tuya.');
}
return parent::editAction($id);
}
public function deleteAction($id)
{
$user = $this->container->get('security.token_storage')->getToken()->getUser();
$object = $this->admin->getObject($id);
if ($user->isAccountant() && $object->getUser()->getManager()->getId() != $user->getAccountant()->getId()) {
die('Este cliente no es tuya.');
}
return parent::deleteAction($id);
}
/**
* @param Request $request
*
* @return RedirectResponse
*/
public function batchActionDownload(ProxyQueryInterface $selectedModelQuery, Request $request = null)
{
$this->admin->checkAccess('edit');
$this->admin->checkAccess('delete');
$modelManager = $this->admin->getModelManager();
$selectedModels = $selectedModelQuery->execute();
try {
return $this->zipDownloadDocumentsAction($selectedModels);
} catch (\Exception $e) {
$this->addFlash('sonata_flash_error', 'flash_batch_download_error');
return new RedirectResponse(
$this->admin->generateUrl('list', [
'filter' => $this->admin->getFilterParameters(),
])
);
}
$this->addFlash('sonata_flash_success', 'flash_batch_download_success');
return new RedirectResponse(
$this->admin->generateUrl('list', [
'filter' => $this->admin->getFilterParameters(),
])
);
}
/**
* Create and download some zip documents.
*
* @param array $documents
* @param mixed $certificates
*
* @return Symfony\Component\HttpFoundation\Response
*/
public function zipDownloadDocumentsAction($certificates)
{
$files = [];
$em = $this->getDoctrine()->getManager();
foreach ($certificates as $certificate) {
$webPath = __DIR__.'/../../../private/assets/asesor/certificados/'.$certificate->getUser()->getId().'-'.$certificate->getFolder().'/'.$certificate->getFilename();
if (is_file($webPath)) {
array_push($files, $webPath);
}
}
$zip = new \ZipArchive();
$zipName = 'Certificados-'.time().'.zip';
$zipPath = __DIR__.'/../../../public/assets/'.$zipName;
$zip->open($zipPath, \ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFromString(basename($file), file_get_contents($file));
}
$zip->close();
if (is_file($zipPath) && !is_dir($zipPath)) {
$response = new Response(file_get_contents($zipPath));
$response->headers->set('Content-Type', 'application/zip');
$response->headers->set('Content-Disposition', 'attachment;filename="'.$zipName.'"');
$response->headers->set('Content-length', filesize($zipPath));
@unlink($zipPath);
} else {
dd($zipName, is_file($zipPath));
}
return $response;
}
}