Skip to content

Commit

Permalink
Add option to show/Hide stats bloc
Browse files Browse the repository at this point in the history
  • Loading branch information
zawaze committed Mar 28, 2019
1 parent 637cdfa commit bb9d43b
Show file tree
Hide file tree
Showing 11 changed files with 344 additions and 23 deletions.
8 changes: 8 additions & 0 deletions local/modules/HookAdminHome/Config/config.xml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://thelia.net/schema/dic/config http://thelia.net/schema/dic/config/thelia-1.0.xsd"> xsi:schemaLocation="http://thelia.net/schema/dic/config http://thelia.net/schema/dic/config/thelia-1.0.xsd">


<forms>
<form name="hookadminhome.config.form" class="HookAdminHome\Form\Configuration" />
</forms>

<hooks> <hooks>
<hook id="hookadminhome.configuration.hook" class="HookAdminHome\Hook\HookAdminManager" scope="request">
<tag name="hook.event_listener" event="module.configuration" type="back" method="onModuleConfiguration" />
</hook>

<hook id="hookadminhome.hook.css"> <hook id="hookadminhome.hook.css">
<tag name="hook.event_listener" event="main.head-css" type="back" templates="css:assets/css/home.css" /> <tag name="hook.event_listener" event="main.head-css" type="back" templates="css:assets/css/home.css" />
</hook> </hook>
Expand Down
2 changes: 1 addition & 1 deletion local/modules/HookAdminHome/Config/module.xml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
</author> </author>
</authors> </authors>
<type>classic</type> <type>classic</type>
<thelia>2.4.0</thelia> <thelia>2.3.4</thelia>
<stability>prod</stability> <stability>prod</stability>
</module> </module>
5 changes: 5 additions & 0 deletions local/modules/HookAdminHome/Config/routing.xml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@
<default key="not-logged">1</default> <default key="not-logged">1</default>
</route> </route>




<route id="admin.home.config" path="/admin/module/HookAdminHome/configure" methods="post">
<default key="_controller">HookAdminHome\Controller\ConfigurationController::editConfiguration</default>
</route>
</routes> </routes>
72 changes: 72 additions & 0 deletions local/modules/HookAdminHome/Controller/ConfigurationController.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php


namespace HookAdminHome\Controller;

use HookAdminHome\HookAdminHome;
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 ConfigurationController extends BaseAdminController
{

public function editConfiguration()
{
if (null !== $response = $this->checkAuth(
AdminResources::MODULE,
[HookAdminHome::DOMAIN_NAME],
AccessManager::UPDATE
)) {
return $response;
}

$form = $this->createForm('hookadminhome.config.form');
$error_message = null;

try {
$validateForm = $this->validateForm($form);
$data = $validateForm->getData();

HookAdminHome::setConfigValue(HookAdminHome::ACTIVATE_NEWS, 0);
HookAdminHome::setConfigValue(HookAdminHome::ACTIVATE_SALES, 0);
HookAdminHome::setConfigValue(HookAdminHome::ACTIVATE_INFO, 0);
HookAdminHome::setConfigValue(HookAdminHome::ACTIVATE_STATS, 0);

if($data['enabled-news']){
HookAdminHome::setConfigValue(HookAdminHome::ACTIVATE_NEWS, 1);
}

if($data['enabled-sales']){
HookAdminHome::setConfigValue(HookAdminHome::ACTIVATE_SALES, 1);
}

if($data['enabled-info']){
HookAdminHome::setConfigValue(HookAdminHome::ACTIVATE_INFO, 1);
}

if($data['enabled-stats']){
HookAdminHome::setConfigValue(HookAdminHome::ACTIVATE_STATS, 1);
}

return RedirectResponse::create(URL::getInstance()->absoluteUrl('/admin/module/HookAdminHome'));

} catch (FormValidationException $e) {
$error_message = $this->createStandardFormValidationErrorMessage($e);
}

if (null !== $error_message) {
$this->setupFormErrorContext(
'configuration',
$error_message,
$form
);
$response = $this->render("module-configure", ['module_code' => 'HookAdminHome']);
}
return $response;
}

}
2 changes: 2 additions & 0 deletions local/modules/HookAdminHome/Controller/HomeController.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -151,4 +151,6 @@ protected function testHexColor($key, $default)


return preg_match('/^#[a-f0-9]{6}$/i', $hexColor) ? $hexColor : $default; return preg_match('/^#[a-f0-9]{6}$/i', $hexColor) ? $hexColor : $default;
} }


} }
93 changes: 93 additions & 0 deletions local/modules/HookAdminHome/Form/Configuration.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace HookAdminHome\Form;

use HookAdminHome\HookAdminHome;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Thelia\Core\Translation\Translator;
use Thelia\Form\BaseForm;

class Configuration extends BaseForm
{

protected function buildForm()
{
$this->formBuilder->add(
"enabled-news",
"checkbox",
array(
"label" => "Enabled News",
"label_attr" => [
"for" => "enabled-news",
"help" => Translator::getInstance()->trans(
'Check if you want show news',
[],
HookAdminHome::DOMAIN_NAME
)
],
"required" => false,
"value" => HookAdminHome::getConfigValue(HookAdminHome::ACTIVATE_NEWS, 0),
)
);

$this->formBuilder->add(
"enabled-info",
"checkbox",
array(
"label" => "Enabled Info",
"label_attr" => [
"for" => "enabled-info",
"help" => Translator::getInstance()->trans(
'Check if you want show info',
[],
HookAdminHome::DOMAIN_NAME
)
],
"required" => false,
"value" => HookAdminHome::getConfigValue(HookAdminHome::ACTIVATE_INFO, 0),
)
);

$this->formBuilder->add(
"enabled-stats",
"checkbox",
array(
"label" => "Enabled default Home Stats",
"label_attr" => [
"for" => "enabled-stats",
"help" => Translator::getInstance()->trans(
'Check if you want show default Home Stats',
[],
HookAdminHome::DOMAIN_NAME
)
],
"required" => false,
"value" => HookAdminHome::getConfigValue(HookAdminHome::ACTIVATE_STATS, 0),
)
);


$this->formBuilder->add(
"enabled-sales",
"checkbox",
array(
"label" => "Enabled Sales Statistics",
"label_attr" => [
"for" => "enabled-sales",
"help" => Translator::getInstance()->trans(
'Check if you want show sales stats',
[],
HookAdminHome::DOMAIN_NAME
)
],
"required" => false,
"value" => HookAdminHome::getConfigValue(HookAdminHome::ACTIVATE_SALES, 0),
)
);
}

public function getName()
{
return "hookadminhomeconfigform";
}
}
57 changes: 35 additions & 22 deletions local/modules/HookAdminHome/Hook/AdminHook.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,46 +26,59 @@ class AdminHook extends BaseHook
{ {
public function blockStatistics(HookRenderEvent $event) public function blockStatistics(HookRenderEvent $event)
{ {
$event->add($this->render('block-statistics.html')); if (1 == HookAdminHome::getConfigValue(HookAdminHome::ACTIVATE_STATS, 1)) {
$event->add($this->render('block-statistics.html'));
}

$event->add($this->render('hook-admin-home-config.html'));

} }


public function blockStatisticsJs(HookRenderEvent $event) public function blockStatisticsJs(HookRenderEvent $event)
{ {
$event->add($this->render('block-statistics-js.html')); if (1 == HookAdminHome::getConfigValue(HookAdminHome::ACTIVATE_STATS, 1)) {
$event->add($this->render('block-statistics-js.html'));
}
} }


public function blockSalesStatistics(HookRenderBlockEvent $event) public function blockSalesStatistics(HookRenderBlockEvent $event)
{ {
$content = trim($this->render("block-sales-statistics.html")); if (1 == HookAdminHome::getConfigValue(HookAdminHome::ACTIVATE_SALES, 1)) {
if (!empty($content)) { $content = trim($this->render("block-sales-statistics.html"));
$event->add([ if (!empty($content)) {
"id" => "block-sales-statistics", $event->add([
"title" => $this->trans("Sales statistics", [], HookAdminHome::DOMAIN_NAME), "id" => "block-sales-statistics",
"content" => $content "title" => $this->trans("Sales statistics", [], HookAdminHome::DOMAIN_NAME),
]); "content" => $content
]);
}
} }
} }


public function blockNews(HookRenderBlockEvent $event) public function blockNews(HookRenderBlockEvent $event)
{ {
$content = trim($this->render("block-news.html")); if (1 == HookAdminHome::getConfigValue(HookAdminHome::ACTIVATE_NEWS, 1)) {
if (!empty($content)) { $content = trim($this->render("block-news.html"));
$event->add([ if (!empty($content)) {
"id" => "block-news", $event->add([
"content" => $content "id" => "block-news",
]); "content" => $content
]);
}
} }
} }


public function blockTheliaInformation(HookRenderBlockEvent $event) public function blockTheliaInformation(HookRenderBlockEvent $event)
{ {
$content = trim($this->render("block-thelia-information.html")); if (1 == HookAdminHome::getConfigValue(HookAdminHome::ACTIVATE_INFO, 1)) {
if (!empty($content)) { $content = trim($this->render("block-thelia-information.html"));
$event->add([ if (!empty($content)) {
"id" => "block-thelia-information", $event->add([
"title" => $this->trans("Thelia informations", [], HookAdminHome::DOMAIN_NAME), "id" => "block-thelia-information",
"content" => $content "title" => $this->trans("Thelia informations", [], HookAdminHome::DOMAIN_NAME),
]); "content" => $content
]);
}
} }
} }
} }
15 changes: 15 additions & 0 deletions local/modules/HookAdminHome/Hook/HookAdminManager.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace HookAdminHome\Hook;

use Thelia\Core\Event\Hook\HookRenderEvent;
use Thelia\Core\Hook\BaseHook;

class HookAdminManager extends BaseHook
{
public function onModuleConfiguration(HookRenderEvent $event)
{
$event->add(
$this->render("admin-home-config.html")
);
}
}
25 changes: 25 additions & 0 deletions local/modules/HookAdminHome/HookAdminHome.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,10 +12,35 @@


namespace HookAdminHome; namespace HookAdminHome;


use Thelia\Core\Template\TemplateDefinition;
use Thelia\Module\BaseModule; use Thelia\Module\BaseModule;


class HookAdminHome extends BaseModule class HookAdminHome extends BaseModule
{ {
/** @var string */ /** @var string */
const DOMAIN_NAME = 'hookadminhome'; const DOMAIN_NAME = 'hookadminhome';

/** @var string */
const ACTIVATE_NEWS = 'activate_home_news';

/** @var string */
const ACTIVATE_SALES= 'activate_home_sales';

/** @var string */
const ACTIVATE_INFO= 'activate_home_info';

/** @var string */
const ACTIVATE_STATS= 'activate_stats';

public function getHooks()
{
return array(
array(
"type" => TemplateDefinition::BACK_OFFICE,
"code" => "hook_home_stats",
"title" => "Hook Home Stats",
"description" => "Hook to change default stats",
)
);
}
} }
Loading

0 comments on commit bb9d43b

Please sign in to comment.