create module event and eventListener

This commit is contained in:
Manuel Raynaud
2013-10-17 18:26:23 +02:00
parent 1b0de7144a
commit d25ad53d5a
9 changed files with 256 additions and 6 deletions

View File

@@ -0,0 +1,69 @@
<?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\Action;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\Module\ModuleToggleActivationEvent;
use Thelia\Core\Event\TheliaEvents;
/**
* Class Module
* @package Thelia\Action
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class Module extends BaseAction implements EventSubscriberInterface
{
public function toggleActivation(ModuleToggleActivationEvent $event)
{
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * array('eventName' => 'methodName')
* * array('eventName' => array('methodName', $priority))
* * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
*
* @return array The event names to listen to
*
* @api
*/
public static function getSubscribedEvents()
{
return array(
TheliaEvents::MODULE_TOGGLE_ACTIVATION => array('toggleActivation', 128)
);
}
}

View File

@@ -136,6 +136,11 @@
<tag name="kernel.event_subscriber"/>
</service>
<service id="thelia.action.module" class="Thelia\Action\Module">
<argument type="service" id="service_container"/>
<tag name="kernel.event_subscriber"/>
</service>
</services>
</config>

View File

@@ -820,6 +820,11 @@
<default key="_controller">Thelia\Controller\Admin\ModuleController::indexAction</default>
</route>
<route id="admin.module.toggle-activation" path="/admin/configuration/modules/toggle-activation/{module_id}">
<default key="_controller">Thelia\Controller\Admin\ModuleController::toggleActivationAction</default>
<requirement key="module_id">\d+</requirement>
</route>
<!-- end Modules rule management -->
<!-- taxe rules management -->

View File

@@ -22,6 +22,9 @@
/*************************************************************************************/
namespace Thelia\Controller\Admin;
use Thelia\Core\Event\Module\ModuleToggleActivationEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Module\ModuleManagement;
/**
@@ -35,11 +38,10 @@ class ModuleController extends BaseAdminController
{
if (null !== $response = $this->checkAuth("admin.module.view")) return $response;
$modulemanagement = new ModuleManagement();
$modulemanagement->updateModules();
return $this->render("modules", array("display_module" => 20));
return $this->render("modules");
}
public function updateAction($module_id)
@@ -48,4 +50,28 @@ class ModuleController extends BaseAdminController
"module_id" => $module_id
));
}
public function toggleActivationAction($module_id)
{
if (null !== $response = $this->checkAuth("admin.module.update")) return $response;
$message = null;
try {
$event = new ModuleToggleActivationEvent($module_id);
$this->dispatch(TheliaEvents::MODULE_TOGGLE_ACTIVATION, $event);
} catch (\Exception $e) {
$message = $e->getMessage();
}
if($this->getRequest()->isXmlHttpRequest()) {
if($message) {
$response = $this->nullResponse($message, 500);
}
$response = $this->nullResponse();
} else {
$response = $this->render("modules");
}
return $response;
}
}

View File

@@ -58,7 +58,7 @@ class BaseController extends ContainerAware
/**
* Return an empty response (after an ajax request, for example)
*/
protected function nullResponse($status = 200)
protected function nullResponse($content = null, $status = 200)
{
return new Response(null, $status);
}
@@ -66,9 +66,9 @@ class BaseController extends ContainerAware
/**
* Return a JSON response
*/
protected function jsonResponse($json_data)
protected function jsonResponse($json_data, $status = 200)
{
return new Response($json_data, 200, array('content-type' => 'application/json'));
return new Response($json_data, $status, array('content-type' => 'application/json'));
}
/**

View File

@@ -0,0 +1,69 @@
<?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\Module;
use Thelia\Core\Event\ActionEvent;
use Thelia\Model\Module;
/**
* Class ModuleEvent
* @package Thelia\Core\Event\Module
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class ModuleEvent extends ActionEvent
{
/**
* @var \Thelia\Model\Module
*/
protected $module;
function __construct(Module $module = null)
{
$this->module = $module;
}
/**
* @param \Thelia\Model\Module $module
*
* @return $this
*/
public function setModule(Module $module)
{
$this->module = $module;
return $this;
}
/**
* @return \Thelia\Model\Module
*/
public function getModule()
{
return $this->module;
}
}

View File

@@ -0,0 +1,67 @@
<?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\Module;
/**
* Class ModuleToggleActivationEvent
* @package Thelia\Core\Event\Module
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class ModuleToggleActivationEvent extends ModuleEvent
{
/**
* @var int
*/
protected $module_id;
function __construct($module_id)
{
$this->module_id = $module_id;
}
/**
* @param int $module_id
*
* @return $this
*/
public function setModuleId($module_id)
{
$this->module_id = $module_id;
return $this;
}
/**
* @return int
*/
public function getModuleId()
{
return $this->module_id;
}
}

View File

@@ -657,4 +657,6 @@ final class TheliaEvents
const GENERATE_PDF = 'thelia.generatePdf';
const MODULE_TOGGLE_ACTIVATION = 'thelia.module.toggleActivation';
}

View File

@@ -22,9 +22,16 @@
<td><a href="#">{$TITLE}</a></td>
<td>{$CHAPO}</td>
<td>
<div class="make-switch switch-small" data-on="success" data-off="danger" data-on-label="<i class='glyphicon glyphicon-ok-circle'></i>" data-off-label="<i class='glyphicon glyphicon-remove-circle'></i>">
<div class="make-switch switch-small module-activation" data-on="success" data-off="danger" data-on-label="<i class='glyphicon glyphicon-ok-circle'></i>" data-off-label="<i class='glyphicon glyphicon-remove-circle'></i>">
<input type="checkbox" {if $ACTIVE}checked{/if}>
</div>
<noscript>
{if $ACTIVE}
<a href="{url path="/admin/configuration/modules/toggle-activation/{$ID}"}">{intl l="deactivation"}</a>
{else}
<a href="{url path="/admin/configuration/modules/toggle-activation/{$ID}"}">{intl l="activation"}</a>
{/if}
</noscript>
</td>
{module_include location='modules_table_row'}