| Current Path : /var/www/v3.cesa.co.za/src/ContactBundle/Controller/ |
| Current File : /var/www/v3.cesa.co.za/src/ContactBundle/Controller/MessageCreditController.php |
<?php
namespace App\ContactBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use App\ContactBundle\Form\Type\RequestCreditType;
use App\ContactBundle\Form\Type\TransferCreditType;
use App\CoreBundle\Services\ServiceGetterManager;
class MessageCreditController extends AbstractController {
/**
* @IsGranted({"ROLE_USER"})
* @param Request $request
* @param ServiceGetterManager $sg
*
* @return Response
*/
public function listAction(Request $request, ServiceGetterManager $sg) {
$pagination = array();
try {
$userList = $this->getUser();
if (!is_object($userList)) {
return $this->redirect(
$this->generateUrl('iga_core_500', array('message' => 'You do not have access to this page'))
);
}
$settingManager = $sg->get('system_setting.manager');
$paginationQuery = $sg->get('contact_message_credit.manager')->getSiteListQuery(array(
'user_list' => $userList,
)
);
// echo $paginationQuery->getSQL() . '<br />';
$paginator = $sg->get('knp_paginator');
$pagination = $paginator->paginate($paginationQuery, $request->get('page', 1), 20, array('distinct' => false));
} catch (\Exception $ex) {
$logger = $sg->get('error_log.manager');
$logger->error('Error viewing credits: ' . $ex->getMessage());
}
return $this->render('@ContactBundle/MessageCredit/list.html.twig', array('pagination' => $pagination));
}
public function requestCreditAction(Request $request, ServiceGetterManager $sg, $cultureString = '') {
try {
$userList = $this->getUser();
if (!is_object($userList)) {
throw new \Exception('You do not have access to this page', '4003');
}
$requestCreditForm = $this->createForm(RequestCreditType::class);
if ($request->getMethod() == 'POST') {
$requestCreditForm->handleRequest($request);
if ($requestCreditForm->isValid()) {
$requestValues = $requestCreditForm->getData();
$testCreate = $sg->get('contact_message.manager')->sendRequestCredit($requestValues);
if (strlen($testCreate) <= 0) {
$this->get('session')->getFlashBag()->add(
'success', 'Your request has been sent'
);
} else {
$requestCreditForm->get('fromName')->addError(new FormError($testCreate));
}
} else {
$this->get('session')->getFlashBag()->add(
'danger', 'Unable to send your request. Please check the form and try again.'
);
}
}
} catch (\Exception $ex) {
$logger = $sg->get('error_log.manager');
$logger->error('Error on send request credit: ' . $ex->getMessage());
return $this->redirect($this->generateUrl('iga_core_500', array('message' => $ex->getMessage() . ' - ' . $ex->getTraceAsString())));
}
return $this->render(
'@ContactBundle/MessageCredit/request.credits.html.twig', array(
'form' => $requestCreditForm->createView(), 'cultureString' => $cultureString
)
);
}
public function transferCredit(Request $request, ServiceGetterManager $sg, $cultureString = '') {
try {
$userList = $this->getUser();
if (!is_object($userList)) {
throw new \Exception('You do not have access to this page', '4003');
}
$transferCreditForm = $this->createForm(TransferCreditType::class);
if ($request->getMethod() == 'POST') {
$transferCreditForm->handleRequest($request);
if ($transferCreditForm->isValid()) {
$requestValues = $transferCreditForm->getData();
$testCreate = $sg->get('contact_message_credit.manager')->transferCredit($requestValues);
if (strlen($testCreate) <= 0) {
$this->get('session')->getFlashBag()->add(
'success', 'Your transfer was successfull'
);
return $this->redirect($this->generateUrl('iga_contact_transfer_credits'));
} else {
$this->get('session')->getFlashBag()->add(
'danger', $testCreate
);
}
} else {
$this->get('session')->getFlashBag()->add(
'danger', 'Unable to transfer credits. Please check the form and try again.'
);
}
}
} catch (\Exception $ex) {
$logger = $sg->get('error_log.manager');
$logger->error('Error on transfer credit: ' . $ex->getMessage());
return $this->redirect($this->generateUrl('iga_core_500', array('message' => $ex->getMessage() . ' - ' . $ex->getTraceAsString())));
}
return $this->render(
'@ContactBundle/MessageCredit/transfer.credits.html.twig', array(
'form' => $transferCreditForm->createView(), 'cultureString' => $cultureString
)
);
}
}