[11/06/2024] Les premières modifs + installation de quelques modules indispensables

This commit is contained in:
2024-06-11 14:57:59 +02:00
parent 5ac5653ae5
commit 77cf2c7cc6
1626 changed files with 171457 additions and 131 deletions

View File

@@ -0,0 +1,25 @@
# This is a fix for InnoDB in MySQL >= 4.1.x
# It "suspends judgement" for fkey relationships until are tables are set.
SET FOREIGN_KEY_CHECKS = 0;
-- ---------------------------------------------------------------------
-- short_code
-- ---------------------------------------------------------------------
DROP TABLE IF EXISTS `short_code`;
CREATE TABLE `short_code`
(
`id` INTEGER NOT NULL AUTO_INCREMENT,
`tag` VARCHAR(55) NOT NULL,
`event` VARCHAR(255) NOT NULL,
`active` TINYINT,
`created_at` DATETIME,
`updated_at` DATETIME,
PRIMARY KEY (`id`),
UNIQUE INDEX `short_code_U_1` (`tag`)
) ENGINE=InnoDB;
# This restores the fkey checks, after having unset them earlier
SET FOREIGN_KEY_CHECKS = 1;

View File

@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8" ?>
<config xmlns="http://thelia.net/schema/dic/config"
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">
<loops>
<!-- sample definition
<loop name="MySuperLoop" class="ShortCode\Loop\MySuperLoop" />
-->
</loops>
<forms>
<!--
<form name="MyFormName" class="ShortCode\Form\MySuperForm" />
-->
</forms>
<commands>
<!--
<command class="ShortCode\Command\MySuperCommand" />
-->
</commands>
<services>
<service id="shortcode.response.listener" class="ShortCode\EventListener\ResponseListener">
<tag name="kernel.event_subscriber"/>
<argument type="service" id="event_dispatcher"/>
</service>
</services>
<!--
<hooks>
<hook id="shortcode.hook" class="ShortCode\Hook\MySuperHook">
<tag name="hook.event_listener" event="main.body.bottom" type="front|back|pdf|email" method="onMainBodyBottom" />
</hook>
</hooks>
-->
<!--
<exports>
</exports>
-->
<!--
<imports>
</imports>
-->
</config>

View File

@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="http://thelia.net/schema/dic/module"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://thelia.net/schema/dic/module http://thelia.net/schema/dic/module/module-2_2.xsd">
<fullnamespace>ShortCode\ShortCode</fullnamespace>
<descriptive locale="en_US">
<title>Add short codes Wordpress'ShortCode syntax</title>
<!--
<subtitle></subtitle>
<description></description>
<postscriptum></postscriptum>
-->
</descriptive>
<descriptive locale="fr_FR">
<title>Ajoute les short codes avec la syntax wordpress</title>
</descriptive>
<!-- <logo></logo> -->
<!--<images-folder>images</images-folder>-->
<languages>
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>2.0.0</version>
<authors>
<author>
<name>Vincent Lopes-Vicente</name>
<email>vlopes@openstudio.fr</email>
</author>
</authors>
<type>classic</type>
<!--
module dependencies
<required>
<module version="&gt;=0.1">Front</module>
<module version="~1.0">HookCart</module>
<module version="&gt;0.2">HookSearch</module>
</required>
-->
<thelia>2.5.0</thelia>
<stability>other</stability>
<mandatory>0</mandatory>
<hidden>0</hidden>
</module>

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<!--
if a /admin/module/shortcode/ route is provided, a "Configuration" button will be displayed
for the module in the module list. Clicking this button will invoke this route.
<route id="my_route_id" path="/admin/module/shortcode">
<default key="_controller">ShortCode\Full\Class\Name\Of\YourConfigurationController::methodName</default>
</route>
<route id="my_route_id" path="/admin/module/shortcode/route-name">
<default key="_controller">ShortCode\Full\Class\Name\Of\YourAdminController::methodName</default>
</route>
<route id="my_route_id" path="/my/route/name">
<default key="_controller">ShortCode\Full\Class\Name\Of\YourOtherController::methodName</default>
</route>
...add as many routes as required.
<route>
...
</route>
-->
</routes>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<database defaultIdMethod="native" name="TheliaMain"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../vendor/thelia/propel/resources/xsd/database.xsd" >
<table name="short_code" namespace="ShortCode\Model">
<column autoIncrement="true" name="id" primaryKey="true" required="true" type="INTEGER" />
<column name="tag" size="55" required="true"/>
<column name="event" size="255" required="true"/>
<column name="active" type="TINYINT" />
<behavior name="timestampable" />
<unique>
<unique-column name="tag" />
</unique>
</table>
<external-schema filename="local/config/schema.xml" />
</database>

View File

@@ -0,0 +1,2 @@
# Sqlfile -> Database map
thelia.sql=thelia

View File

@@ -0,0 +1,60 @@
<?php
namespace ShortCode\Event;
use Thelia\Core\Event\ActionEvent;
class ShortCodeEvent extends ActionEvent
{
/** @var string */
protected $content;
/** @var array */
protected $attributes;
/** @var string */
protected $result;
/**
* @param string $content
* @param array $attributes
*/
public function __construct($content, $attributes)
{
$this->content = $content;
$this->attributes = $attributes;
$this->result = $content;
}
/**
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}
/**
* @return string
*/
public function getResult()
{
return $this->result;
}
/**
* @param string $result
*/
public function setResult($result)
{
$this->result = $result;
}
}

View File

@@ -0,0 +1,95 @@
<?php
/*
* This file is part of the Thelia package.
* http://www.thelia.net
*
* (c) OpenStudio <info@thelia.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ShortCode\EventListener;
use Maiorano\Shortcodes\Library\SimpleShortcode;
use Maiorano\Shortcodes\Manager\ShortcodeManager;
use ShortCode\Event\ShortCodeEvent;
use ShortCode\Model\ShortCode;
use ShortCode\Model\ShortCodeQuery;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Thelia\Log\Tlog;
class ResponseListener implements EventSubscriberInterface
{
/** @var EventDispatcherInterface */
protected $eventDispatcher;
public function __construct(
EventDispatcherInterface $eventDispatcher
) {
$this->eventDispatcher = $eventDispatcher;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::RESPONSE => [['dispatchShortCodeEvents', 64]],
];
}
public function dispatchShortCodeEvents(ResponseEvent $event): void
{
if ($event->getRequest()->get('disable_shortcode', 0) == 1) {
return;
}
$response = $event->getResponse();
if (
$response instanceof BinaryFileResponse
|| $response instanceof StreamedResponse
|| $response instanceof RedirectResponse
|| $response instanceof JsonResponse
) {
return;
}
$dispatcher = $this->eventDispatcher;
$simpleShortCodes = [];
$shortCodes = ShortCodeQuery::create()
->filterByActive(1)
->find();
/** @var ShortCode $shortCode */
foreach ($shortCodes as $shortCode) {
$simpleShortCodes[$shortCode->getTag()] = new SimpleShortcode($shortCode->getTag(), null, function ($content, $attributes) use ($shortCode, $dispatcher) {
$shortCodeEvent = new ShortCodeEvent($content, $attributes);
$dispatcher->dispatch($shortCodeEvent, $shortCode->getEvent());
return $shortCodeEvent->getResult();
});
}
$manager = new ShortcodeManager($simpleShortCodes);
$content = $response->getContent();
try {
$content = $manager->doShortCode($content, null, true);
} catch (\Exception $exception) {
Tlog::getInstance()->error($exception->getMessage());
}
$response->setContent($content);
}
}

View File

@@ -0,0 +1,4 @@
<?php
return array(
// 'an english string' => 'The displayed english string',
);

View File

@@ -0,0 +1,4 @@
<?php
return array(
// 'an english string' => 'La traduction française de la chaine',
);

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2018
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,10 @@
<?php
namespace ShortCode\Model;
use ShortCode\Model\Base\ShortCode as BaseShortCode;
class ShortCode extends BaseShortCode
{
}

View File

@@ -0,0 +1,21 @@
<?php
namespace ShortCode\Model;
use ShortCode\Model\Base\ShortCodeQuery as BaseShortCodeQuery;
/**
* Skeleton subclass for performing query and update operations on the 'short_code' table.
*
*
*
* You should add additional methods to this class to meet the
* application requirements. This class will only be generated as
* long as it does not already exist in the output directory.
*
*/
class ShortCodeQuery extends BaseShortCodeQuery
{
} // ShortCodeQuery

View File

@@ -0,0 +1,45 @@
# ShortCode
Implementation of Wordpress' Shortcode syntax in Thelia (with https://github.com/maiorano84/shortcodes).
This module scan the Thelia response at the research of registred short code (in DB),
if a short code is find the module dispatch the associated event.
## Installation
### Manually
* Copy the module into ```<thelia_root>/local/modules/``` directory and be sure that the name of the module is ShortCode.
* Activate it in your thelia administration panel
### Composer
Add it in your main thelia composer.json file
```
composer require thelia/shortcode-module:~1.0
```
## Usage
This module https://github.com/thelia-modules/ShortCodeMeta is a good example of how to use ShortCode.
### 1. Register your short codes
You can do this at the post activation of your module. The best way to do this is to use the module method : `ShortCode::createNewShortCodeIfNotExist`
the first parameter is the name you will use to call the short code in your templates (like this `[my_shortcode_name], second parameter is the event dispatched when the short code is detected.
### 2. Add short codes to your templates
The short codes syntax is as follows:
```
[shortcode] - No content, no attributes
[shortcode]My Content[/shortcode] - Short code with content
[shortcode attribute=value foo=bar] - Short code with attributes
[shortcode attribute=value foo=bar]My Content[/shortcode] - Short code with content and attributes
```
### 3. Listen events associated to your short codes
When a short code is detected in response a `ShortCodeEvent` is dispatched with the event name given at the creation.
So if you want replace your short code by something you have to listen this event.
In this event you have 3 properties :
- `content` (string) The content between your tags it will be `My Content` for the example above.
- `attributes` (array) The array of all attributes passed to your short code `['attribute'=>'value', 'foo'=>'bar']` for the example above.
- `result` (string) Your short code will be replaced by this value in response (equal to content by default)

View File

@@ -0,0 +1,89 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace ShortCode;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Exception\PropelException;
use ShortCode\Model\ShortCodeQuery;
use Thelia\Install\Database;
use Thelia\Module\BaseModule;
class ShortCode extends BaseModule
{
/** @var string */
const DOMAIN_NAME = 'shortcode';
public function preActivation(ConnectionInterface $con = null)
{
if (!$this->getConfigValue('is_initialized', false)) {
$database = new Database($con);
$database->insertSql(null, array(__DIR__ . '/Config/TheliaMain.sql'));
$this->setConfigValue('is_initialized', true);
}
return true;
}
/**
* Create a new ShortCode
* @param string $shortCodeName the name for call the ShortCode in template
* @param string $eventName the name of the event dispatched when shortcode is in template
* @throws PropelException
*/
public static function createNewShortCodeIfNotExist($shortCodeName, $eventName)
{
if (null === ShortCodeQuery::create()->findOneByTag($shortCodeName)) {
$shortCode = new \ShortCode\Model\ShortCode();
$shortCode->setTag($shortCodeName)
->setEvent($eventName)
->setActive(1)
->save();
}
}
/**
* Active a ShortCode by his name
* @param string $shortCodeName the name for call the ShortCode in template
* @throws PropelException
*/
public static function activateShortCode($shortCodeName)
{
$shortCode = ShortCodeQuery::create()
->filterByTag($shortCodeName)
->findOne();
if (null !== $shortCode) {
$shortCode->setActive(1)
->save();
}
}
/**
* Deactive a ShortCode by his name
* @param string $shortCodeName the name for call the ShortCode in template
* @throws PropelException
*/
public static function deactivateShortCode($shortCodeName)
{
$shortCode = ShortCodeQuery::create()
->filterByTag($shortCodeName)
->findOne();
if (null !== $shortCode) {
$shortCode->setActive(0)
->save();
}
}
}

View File

@@ -0,0 +1,12 @@
{
"name": "thelia/short-code-module",
"license": "LGPL-3.0+",
"type": "thelia-module",
"require": {
"thelia/installer": "~1.1",
"maiorano84/shortcodes": "v2.0.0-beta"
},
"extra": {
"installer-name": "ShortCode"
}
}