enabled change default country

This commit is contained in:
Manuel Raynaud
2013-10-08 10:16:18 +02:00
parent d45074051e
commit 43467681fd
9 changed files with 107 additions and 8 deletions

View File

@@ -26,6 +26,7 @@ namespace Thelia\Action;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\Country\CountryCreateEvent;
use Thelia\Core\Event\Country\CountryDeleteEvent;
use Thelia\Core\Event\Country\CountryToggleDefaultEvent;
use Thelia\Core\Event\Country\CountryUpdateEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Model\Country as CountryModel;
@@ -70,6 +71,16 @@ class Country extends BaseAction implements EventSubscriberInterface
}
}
public function toggleDefault(CountryToggleDefaultEvent $event)
{
if( null !== $country = CountryQuery::create()->findPk($event->getCountryId()))
{
$country->toggleDefault();
$event->setCountry($country);
}
}
/**
* Returns an array of event names this subscriber wants to listen to.
@@ -94,9 +105,10 @@ class Country extends BaseAction implements EventSubscriberInterface
public static function getSubscribedEvents()
{
return array(
TheliaEvents::COUNTRY_CREATE => array('create', 128),
TheliaEvents::COUNTRY_UPDATE => array('update', 128),
TheliaEvents::COUNTRY_DELETE => array('delete', 128),
TheliaEvents::COUNTRY_CREATE => array('create', 128),
TheliaEvents::COUNTRY_UPDATE => array('update', 128),
TheliaEvents::COUNTRY_DELETE => array('delete', 128),
TheliaEvents::COUNTRY_TOGGLE_DEFAULT => array('toggleDefault', 128)
);
}
}

View File

@@ -424,6 +424,10 @@
<default key="_controller">Thelia\Controller\Admin\CountryController::deleteAction</default>
</route>
<route id="admin.configuration.toggle-default" path="/admin/configuration/country/toggleDefault">
<default key="_controller">Thelia\Controller\Admin\CountryController::toggleDefaultAction</default>
</route>
<!-- content routes management -->
<route id="admin.content.create" path="/admin/content/create">
<default key="_controller">Thelia\Controller\Admin\ContentController::createAction</default>

View File

@@ -24,6 +24,7 @@
namespace Thelia\Controller\Admin;
use Thelia\Core\Event\Country\CountryCreateEvent;
use Thelia\Core\Event\Country\CountryDeleteEvent;
use Thelia\Core\Event\Country\CountryToggleDefaultEvent;
use Thelia\Core\Event\Country\CountryUpdateEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Form\CountryCreationForm;
@@ -245,4 +246,25 @@ class CountryController extends AbstractCrudController
{
$this->redirectToRoute('admin.configuration.countries.default');
}
public function toggleDefaultAction()
{
if (null !== $response = $this->checkAuth($this->updatePermissionIdentifier)) return $response;
$content = null;
if (null !== $country_id = $this->getRequest()->get('country_id')) {
$toogleDefaultEvent = new CountryToggleDefaultEvent($country_id);
try {
$this->dispatch(TheliaEvents::COUNTRY_TOGGLE_DEFAULT, $toogleDefaultEvent);
if($toogleDefaultEvent->hasCountry()) {
return $this->nullResponse();
}
} catch (\Exception $ex) {
$content = $ex->getMessage();
}
}
return $this->nullResponse($content, 500);
}
}

View File

@@ -58,9 +58,9 @@ class BaseController extends ContainerAware
/**
* Return an empty response (after an ajax request, for example)
*/
protected function nullResponse()
protected function nullResponse($status = 200)
{
return new Response();
return new Response(null, $status);
}
/**

View File

@@ -0,0 +1,48 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Core\Event\Country;
/**
* Class CountryToggleDefaultEvent
* @package Thelia\Core\Event\Country
* @author manuel raynaud <mraynaud@openstudio.fr>
*/
class CountryToggleDefaultEvent extends CountryEvent
{
protected $country_id;
function __construct($country_id)
{
$this->country_id = $country_id;
}
/**
* @return mixed
*/
public function getCountryId()
{
return $this->country_id;
}
}

View File

@@ -219,7 +219,7 @@ final class TheliaEvents
const COUNTRY_CREATE = "action.createCountry";
const COUNTRY_UPDATE = "action.updateCountry";
const COUNTRY_DELETE = "action.deleteCountry";
const COUNTRY_TOGGLE_VISIBILITY = "action.toggleCountryVisibility";
const COUNTRY_TOGGLE_DEFAULT = "action.toggleCountryDefault";
//const COUNTRY_UPDATE_POSITION = "action.updateFolderPosition";

View File

@@ -115,8 +115,10 @@ class Country extends BaseI18nLoop
->set("POSTSCRIPTUM", $country->getVirtualColumn('i18n_POSTSCRIPTUM'))
->set("ISOCODE", $country->getIsocode())
->set("ISOALPHA2", $country->getIsoalpha2())
->set("ISOALPHA3", $country->getIsoalpha3());
->set("ISOALPHA3", $country->getIsoalpha3())
->set('IS_DEFAULT', $country->getByDefault())
;
$loopResult->addRow($loopResultRow);
}

View File

@@ -11,6 +11,17 @@ class Country extends BaseCountry
{
use \Thelia\Model\Tools\ModelEventDispatcherTrait;
public function toggleDefault()
{
CountryQuery::create()
->filterByByDefault(1)
->update(array('ByDefault' => 0));
$this
->setByDefault(1)
->save();
}
public function preInsert(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::BEFORE_CREATECOUNTRY, new CountryEvent($this));

View File

@@ -55,7 +55,7 @@
<td>{$TITLE}</td>
<td>
<div class="make-switch switch-small switch-radio change-default-toggle" data-id="{$ID}" data-on="success" data-off="danger" data-on-label="<i class='glyphicon glyphicon-ok'></i>" data-off-label="<i class='glyphicon glyphicon-remove'></i>">
<input class="change-default-toggle" type="radio" name="by_default" value="{$ID}" {if $IS_DEFAULT}selected="selected"{/if}/>
<input class="change-default-toggle" type="radio" name="by_default" value="{$ID}" {if $IS_DEFAULT}checked="checked"{/if}/>
</div>
</td>
<td>