<?php

namespace PointRetrait\Controller\backOffice;

use PointRetrait\Model\PdrPlaces;
use PointRetrait\Model\PdrPlacesQuery;
use PointRetrait\PointRetrait;
use Propel\Runtime\Map\TableMap;
use Propel\Runtime\Propel;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Thelia\Controller\Admin\BaseAdminController;
use Thelia\Core\Security\AccessManager;
use Thelia\Core\Security\Resource\AdminResources;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Tools\URL;

/**
 * Class PlaceController
 * @package PointRetrait\Controller
 */
class PlaceController extends BaseAdminController
{

    public function viewPlace()
    {
        $selectedPlace = PdrPlacesQuery::create()->findOneById($this->getRequest()->query->get("place_id"));

        return $this->render("place-edit", array('module_code' => PointRetrait::getModuleCode(),
            'place_id' => $selectedPlace));
    }


    public function editPlace()
    {
        // Check current user authorization
        if (null !== $response = $this->checkAuth(AdminResources::MODULE, PointRetrait::getModuleCode(), AccessManager::VIEW))
            return $response;

        $con = Propel::getConnection();
        $con->beginTransaction();

        $error_msg = "";
        $changeForm = $this->createForm("pdr.place.view.main", "form");

        try {
            $form = $this->validateForm($changeForm, "POST");

            $data = $form->getData();
            $place = PdrPlacesQuery::create()->findOneById($data['place_id']);
            if ($place === null) {
                $error_msg = "Withdrawal place not found by Id";
            }
            else {
                $place->fromArray($data, TableMap::TYPE_FIELDNAME);
                $place->save();
                $con->commit();
            }
        } catch (FormValidationException $ex) {
            $error_msg = $this->createStandardFormValidationErrorMessage($ex);
        }

        if ($this->getRequest()->get('save_mode') == 'stay')
            return $this->render("place-edit");

        return $this->render("places-list");
    }


    public function createPlace()
    {
        // Check current user authorization
        if (null !== $response = $this->checkAuth(AdminResources::MODULE, PointRetrait::getModuleCode(), AccessManager::VIEW))
            return $response;

        $con = Propel::getConnection();
        $con->beginTransaction();

        $error_msg = "";
        $createForm = $this->createForm("pdr.place.create", "form");

        try {
            $form = $this->validateForm($createForm, "POST");
            $data = $form->getData();

            $newPlace = new PdrPlaces();
            $newPlace->fromArray($data, TableMap::TYPE_FIELDNAME);
            $newPlace->save();
            $con->commit();

        } catch (FormValidationException $ex) {
            $error_msg = $this->createStandardFormValidationErrorMessage($ex);
        }

        if ($this->getRequest()->request->get("success_url") == null) {
            return new RedirectResponse(URL::getInstance()->absoluteUrl(PointRetrait::MODULE_URL));
        } else {
            return new RedirectResponse(URL::getInstance()->absoluteUrl($this->getRequest()->request->get("success_url")));
        }
    }


    public function deletePlace()
    {
        // Check current user authorization
        if (null !== $response = $this->checkAuth(AdminResources::MODULE, PointRetrait::getModuleCode(), AccessManager::VIEW))
            return $response;

        $placeId = $this->getRequest()->get('attr-place-id');
        $query = PdrPlacesQuery::create()->findById($placeId);
        if ($query === null)
            $error_msg = "Place not found by Id";
        else
        {
            $con = Propel::getConnection();
            $con->beginTransaction();
            $query->delete($con);
            $con->commit();
        }

        if ($this->getRequest()->request->get("success_url") == null) {
            return new RedirectResponse(URL::getInstance()->absoluteUrl(PointRetrait::MODULE_URL));
        } else {
            return new RedirectResponse(URL::getInstance()->absoluteUrl($this->getRequest()->request->get("success_url")));
        }
    }

}