Repository navigation
Expand file tree
/
Copy pathShortCode.php
More file actions
104 lines (91 loc) · 3.78 KB
/
Copy pathShortCode.php
File metadata and controls
104 lines (91 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?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\Propel;
use Propel\Runtime\Exception\PropelException;
use ShortCode\Model\ShortCodeQuery;
use Thelia\Core\Install\Database;
use Thelia\Module\BaseModule;
class ShortCode extends BaseModule
{
/** @var string */
const DOMAIN_NAME = 'shortcode';
/**
* Create the tables on a fresh install.
*
* The install script starts with DROP TABLE, so it only runs when the tables are missing.
* The database is asked rather than an "is_initialized" flag in the module configuration:
* a shop whose tables were created another way, or whose configuration was lost, would
* otherwise have every short code wiped by a mere reactivation.
*/
public function preActivation(?ConnectionInterface $con = null): bool
{
$con ??= Propel::getWriteConnection('TheliaMain');
if (!$this->hasTable($con, 'short_code')) {
(new Database($con))->insertSql(null, [__DIR__.'/Config/TheliaMain.sql']);
}
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();
}
}
private function hasTable(ConnectionInterface $con, string $table): bool
{
$statement = $con->prepare('SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = :table');
$statement->execute(['table' => $table]);
return (int) $statement->fetchColumn() > 0;
}
}