forked from thelia-modules/CreditAccount
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreditAccount.php
84 lines (70 loc) · 3.63 KB
/
CreditAccount.php
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
<?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 CreditAccount;
use Propel\Runtime\Connection\ConnectionInterface;
use Thelia\Core\Event\Hook\HookCreateEvent;
use Thelia\Core\Event\Hook\ModuleHookCreateEvent;
use Thelia\Core\Event\Hook\ModuleHookToggleActivationEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\Template\TemplateDefinition;
use Thelia\Core\Translation\Translator;
use Thelia\Exception\TheliaProcessException;
use Thelia\Install\Database;
use Thelia\Model\HookQuery;
use Thelia\Module\BaseModule;
class CreditAccount extends BaseModule
{
const DOMAIN = 'creditaccount';
const CREDIT_ACCOUNT_ADD_AMOUNT = 'creditAccount.addAccount';
public function postActivation(ConnectionInterface $con = null)
{
$database = new Database($con->getWrappedConnection());
$database->insertSql(null, [__DIR__ . '/Config/thelia.sql']);
// Add order-invoice.before-discount hook if not already defined
if (null === HookQuery::create()->findOneByCode('order-invoice.before-discount')) {
try {
$hookEvent = new HookCreateEvent();
$hookEvent
->setCode('order-invoice.before-discount')
->setType(TemplateDefinition::FRONT_OFFICE)
->setNative(false)
->setActive(true)
->setLocale('en_US')
->setTitle("Before discount code form block");
$this->getDispatcher()->dispatch(TheliaEvents::HOOK_CREATE, $hookEvent);
if ($hookEvent->hasHook()) {
// Assign module to this hook
$moduleHookEvent = new ModuleHookCreateEvent();
$moduleHookEvent
->setModuleId($this->getModuleId())
->setHookId($hookEvent->getHook()->getId())
->setClassname('creditaccount.order_invoice.hook')
->setMethod('orderInvoiceForm');
// Activate module hook
$this->getDispatcher()->dispatch(TheliaEvents::MODULE_HOOK_CREATE, $moduleHookEvent);
if ($moduleHookEvent->hasModuleHook()) {
$event = new ModuleHookToggleActivationEvent($moduleHookEvent->getModuleHook());
$this->getDispatcher()->dispatch(TheliaEvents::MODULE_HOOK_TOGGLE_ACTIVATION, $event);
}
}
} catch (\Exception $ex) {
throw new TheliaProcessException(
Translator::getInstance()->trans(
"Failed to put module in 'order-invoice.before-discount' hook (%err)",
['%err' => $ex->getMessage()]
),
$ex
);
}
}
}
}