Skip to content

Commit

Permalink
Merge pull request #3 from NicolasBarbey/main
Browse files Browse the repository at this point in the history
Thelia 2.5 compatibility
  • Loading branch information
lopes-vincent authored Aug 27, 2021
2 parents 5f9238e + a52f8d4 commit 8fa5144
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 125 deletions.
7 changes: 3 additions & 4 deletions Config/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@
</services>
-->

<!--
<hooks>
<hook id="maintenance.hook" class="Maintenance\Hook\MySuperHook">
<tag name="hook.event_listener" event="main.body.bottom" type="front|back|pdf|email" method="onMainBodyBottom" />
<hook id="maintenance.hook" class="Maintenance\Hook\BackHook">
<tag name="hook.event_listener" event="module.configuration" type="back" method="onModuleConfig" />
</hook>
</hooks>
-->


<!--
<exports>
Expand Down
4 changes: 2 additions & 2 deletions Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>1.0.1</version>
<version>2.0.0</version>
<authors>
<author>
<name>Vincent Lopes-Vicente</name>
Expand All @@ -36,6 +36,6 @@
<module version="&gt;0.2">HookSearch</module>
</required>
-->
<thelia>2.2.0</thelia>
<thelia>2.5.0</thelia>
<stability>other</stability>
</module>
4 changes: 2 additions & 2 deletions Config/routing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</route>
-->

<route id="maintenance.config" path="/admin/module/Maintenance">
<!--<route id="maintenance.config" path="/admin/module/Maintenance">
<default key="_controller">Maintenance:Configuration:view</default>
</route>
Expand All @@ -38,6 +38,6 @@
<route id="maintenance.toggle_maintenance" path="/admin/module/Maintenance/toggle" methods="post">
<default key="_controller">Maintenance:Configuration:toggleMaintenance</default>
</route>
</route>-->

</routes>
2 changes: 1 addition & 1 deletion Config/schema.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<database defaultIdMethod="native" name="thelia"
<database defaultIdMethod="native" name="TheliaMain"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../vendor/propel/propel/resources/xsd/database.xsd" >
<!--
Expand Down
56 changes: 10 additions & 46 deletions Controller/ConfigurationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,16 @@
use Thelia\Core\Security\AccessManager;
use Thelia\Core\Security\Resource\AdminResources;
use Thelia\Core\Translation\Translator;
use Symfony\Component\Routing\Annotation\Route;

/**
* @Route("/admin/module/Maintenance", name="maintenance")
*/
class ConfigurationController extends BaseAdminController
{
public function viewAction()
{
$maintenanceFile = Maintenance::getMaintenanceFile();

$content = $maintenanceFile->getContents();

preg_match('/<!--TITLE START-->((.|\n)*?)<!--TITLE END-->/', $content, $title);
preg_match('/<!--MESSAGE START-->((.|\n)*?)<!--MESSAGE END-->/', $content, $message);
preg_match('/background_color\*\/((.|\n)*?)\/\*background_color/', $content, $backgroundColor);
preg_match('/font_color\*\/((.|\n)*?)\/\*font_color/', $content, $fontColor);
preg_match('/link_color\*\/((.|\n)*?)\/\*link_color/', $content, $linkColor);


$finder = new Finder();
$finder->files()->depth('== 0')->in(THELIA_WEB_DIR)->name('index.php');

$indexContent = "";

/** @var SplFileInfo $file */
foreach ($finder as $file) {
$indexContent = $file->getContents();
}
$isInMaintenance = 0;

if (preg_match("/\/\/⚠((.|\n)*)⚠/", $indexContent)) {
$isInMaintenance = 1;
}

$isIndexWritable = is_writable(THELIA_WEB_DIR."index.php");
$isWebWritable = is_writable(THELIA_WEB_DIR);

return $this->render(
'maintenance/configuration',
[
'title' => html_entity_decode($title[1]),
'message' => html_entity_decode($message[1]),
'backgroundColor' => html_entity_decode($backgroundColor[1]),
'fontColor' => html_entity_decode($fontColor[1]),
'linkColor' => html_entity_decode($linkColor[1]),
'isInMaintenance' => $isInMaintenance,
'isIndexWritable' => $isIndexWritable,
'isWebWritable' => $isWebWritable,
]
);
}

/**
* @Route("/configuration", name="_save", methods="POST")
*/
public function configurationAction()
{
if (null !== $response = $this->checkAuth(array(AdminResources::MODULE), 'Maintenance', AccessManager::VIEW)) {
Expand Down Expand Up @@ -96,6 +57,9 @@ public function configurationAction()
return $this->generateSuccessRedirect($form);
}

/**
* @Route("/toggle", name="_toggle_maintenance", methods="POST")
*/
public function toggleMaintenanceAction()
{
if (null !== $response = $this->checkAuth(array(AdminResources::MODULE), 'Maintenance', AccessManager::VIEW)) {
Expand Down
13 changes: 7 additions & 6 deletions Form/ConfigurationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Maintenance\Form;

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

class ConfigurationForm extends BaseForm
Expand All @@ -11,26 +12,26 @@ protected function buildForm()
$this->formBuilder
->add(
"title",
"text"
TextType::class
)
->add(
"message",
"text"
TextType::class
)
->add(
"background_color",
"text"
TextType::class
)
->add(
"font_color",
"text"
TextType::class
)
->add(
"link_color",
"text"
TextType::class
);
}
public function getName()
public static function getName()
{
return "maintenance_configuration_form";
}
Expand Down
2 changes: 1 addition & 1 deletion Form/ToggleMaintenanceForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ protected function buildForm()

}

public function getName()
public static function getName()
{
return "toggle_maintenance_form";
}
Expand Down
62 changes: 62 additions & 0 deletions Hook/BackHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php


namespace Maintenance\Hook;


use Maintenance\Maintenance;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Thelia\Core\Event\Hook\HookRenderEvent;
use Thelia\Core\Hook\BaseHook;

class BackHook extends BaseHook
{
public function onModuleConfig(HookRenderEvent $event)
{
$maintenanceFile = Maintenance::getMaintenanceFile();

$content = $maintenanceFile->getContents();

preg_match('/<!--TITLE START-->((.|\n)*?)<!--TITLE END-->/', $content, $title);
preg_match('/<!--MESSAGE START-->((.|\n)*?)<!--MESSAGE END-->/', $content, $message);
preg_match('/background_color\*\/((.|\n)*?)\/\*background_color/', $content, $backgroundColor);
preg_match('/font_color\*\/((.|\n)*?)\/\*font_color/', $content, $fontColor);
preg_match('/link_color\*\/((.|\n)*?)\/\*link_color/', $content, $linkColor);


$finder = new Finder();
$finder->files()->depth('== 0')->in(THELIA_WEB_DIR)->name('index.php');

$indexContent = "";

/** @var SplFileInfo $file */
foreach ($finder as $file) {
$indexContent = $file->getContents();
}
$isInMaintenance = 0;

if (preg_match("/\/\/⚠((.|\n)*)⚠/", $indexContent)) {
$isInMaintenance = 1;
}

$isIndexWritable = is_writable(THELIA_WEB_DIR."index.php");
$isWebWritable = is_writable(THELIA_WEB_DIR);

$event->add(
$this->render(
'maintenance/configuration.html',
[
'title' => html_entity_decode($title[1]),
'message' => html_entity_decode($message[1]),
'backgroundColor' => html_entity_decode($backgroundColor[1]),
'fontColor' => html_entity_decode($fontColor[1]),
'linkColor' => html_entity_decode($linkColor[1]),
'isInMaintenance' => $isInMaintenance,
'isIndexWritable' => $isIndexWritable,
'isWebWritable' => $isWebWritable,
]
)
);
}
}
11 changes: 10 additions & 1 deletion Maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Maintenance;

use Propel\Runtime\Connection\ConnectionInterface;
use Symfony\Component\DependencyInjection\Loader\Configurator\ServicesConfigurator;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Thelia\Module\BaseModule;
Expand All @@ -31,7 +32,7 @@ class Maintenance extends BaseModule
* Have fun !
*/

public function postActivation(ConnectionInterface $con = null)
public function postActivation(ConnectionInterface $con = null): void
{
if (!file_exists(self::MAINTENANCE_FILE)) {
copy(THELIA_MODULE_DIR . 'Maintenance' . DS . 'templates'. DS .'maintenance.html', self::MAINTENANCE_FILE);
Expand All @@ -56,4 +57,12 @@ public static function getMaintenanceFile()
return $file;
}
}

public static function configureServices(ServicesConfigurator $servicesConfigurator): void
{
$servicesConfigurator->load(self::getModuleCode().'\\', __DIR__)
->exclude([THELIA_MODULE_DIR . ucfirst(self::getModuleCode()). "/I18n/*"])
->autowire(true)
->autoconfigure(true);
}
}
104 changes: 42 additions & 62 deletions templates/backOffice/default/maintenance/configuration.html
Original file line number Diff line number Diff line change
@@ -1,66 +1,46 @@
{extends file="admin-layout.tpl"}
<style>
#maintenance_page {
text-align: center;
padding: 150px;
font: 20px Helvetica, sans-serif;
color: {$fontColor};
background-color: {$backgroundColor};
}
#maintenance_page article { display: block; text-align: left; width: 650px; margin: 0 auto; }
#maintenance_page h1 { font-size: 50px; }
#maintenance_page a { color: {$linkColor}; text-decoration: none; }
#maintenance_page a:hover { color: {$fontColor}; text-decoration: none; }
</style>

{block name="after-bootstrap-css"}

{/block}

{block name="no-return-functions"}
{$admin_current_location = 'module'}
{/block}

{block name="page-title"}

{/block}

{block name="check-resource"}admin.module{/block}
{block name="check-access"}view{/block}
{block name="check-module"}Maintenance{/block}

{block name="main-content"}
<style>
#maintenance_page {
text-align: center;
padding: 150px;
font: 20px Helvetica, sans-serif;
color: {$fontColor};
background-color: {$backgroundColor};
}
#maintenance_page article { display: block; text-align: left; width: 650px; margin: 0 auto; }
#maintenance_page h1 { font-size: 50px; }
#maintenance_page a { color: {$linkColor}; text-decoration: none; }
#maintenance_page a:hover { color: {$fontColor}; text-decoration: none; }
</style>

<div class="row">
<div class="col-lg-12">
{if $isIndexWritable !== true}
<div class="alert alert-danger">
{intl l="The file web/index.php is not writable please change the permissions or the module will not work"}
</div>
{/if}
{if $isWebWritable !== true}
<div class="alert alert-danger">
{intl l="The directory web is not writable please change the permissions or the module will not work"}
</div>
{/if}
<h1 class="page-header">
<div class="row">
<div class="col-md-4">
{intl l='Maintenance module configuration' d='maintenance.bo.default'}
</div>
{form name="toggle_maintenance_form"}
<form action="{url path="/admin/module/Maintenance/toggle"}" method="post" class="col-md-6">
{form_hidden_fields form=$form}
{form_field form=$form field='success_url'}
<input type="hidden" name="{$name}" value="{url path={navigate to="current"}}"/>
{/form_field}
{if $isInMaintenance}<input type="submit" class="btn btn-danger" value="{intl l="Disable maintenance"}">
{else}<input type="submit" class="btn btn-success" value="{intl l="Enable maintenance"}">{/if}
</form>
{/form}
<div class="row">
<div class="col-lg-12">
{if $isIndexWritable !== true}
<div class="alert alert-danger">
{intl l="The file web/index.php is not writable please change the permissions or the module will not work"}
</div>
{/if}
{if $isWebWritable !== true}
<div class="alert alert-danger">
{intl l="The directory web is not writable please change the permissions or the module will not work"}
</div>
{/if}
<h1 class="page-header">
<div class="row">
<div class="col-md-4">
{intl l='Maintenance module configuration' d='maintenance.bo.default'}
</div>
</h1>
</div>
{form name="toggle_maintenance_form"}
<form action="{url path="/admin/module/Maintenance/toggle"}" method="post" class="col-md-6">
{form_hidden_fields form=$form}
{form_field form=$form field='success_url'}
<input type="hidden" name="{$name}" value="{url path={navigate to="current"}}"/>
{/form_field}
{if $isInMaintenance}<input type="submit" class="btn btn-danger" value="{intl l="Disable maintenance"}">
{else}<input type="submit" class="btn btn-success" value="{intl l="Enable maintenance"}">{/if}
</form>
{/form}
</div>
</h1>
</div>
</div>

Expand Down Expand Up @@ -125,4 +105,4 @@ <h1>{$title}</h1>
</div>
</article>
</div>
{/block}

0 comments on commit 8fa5144

Please sign in to comment.