-
Notifications
You must be signed in to change notification settings - Fork 1
/
ps_test.php
126 lines (114 loc) · 3.61 KB
/
ps_test.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* 2007-2016 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2016 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
require_once __DIR__.'/vendor/autoload.php';
use Foo\Repository\ProductRepository;
class Ps_Test extends Module
{
public function __construct()
{
$this->name = 'ps_test';
$this->version = '1.0.0';
$this->author = 'Mickaël Andrieu';
parent::__construct();
$this->displayName = 'Tester';
$this->description = 'Module to demonstrate new customization system with PrestaShop 1.7';
$this->ps_versions_compliancy = [
'min' => '1.7.2.0',
'max' => _PS_VERSION_,
];
}
/**
* Module installation.
*
* @return bool Success of the installation
*/
public function install()
{
return parent::install() && $this->registerHook('displayDashboardToolbarIcons');
}
/**
* Uninstall the module.
*
* @return bool Success of the uninstallation
*/
public function uninstall()
{
return parent::uninstall();
}
/**
* Make products export in XML.
*
* @param $params array
*/
public function hookDisplayDashboardToolbarIcons($params)
{
if ($this->isSymfonyContext() && $params['route'] === 'admin_product_catalog') {
$products = $this->getProducts(1);
$productsXml = $this->serializeProducts($products);
$filepath = _PS_ROOT_DIR_.'/products.xml';
$this->writeFile($productsXml, $filepath);
return $this->get('twig')->render('@PrestaShop/Foo/download_link.twig',[
'filepath' => _PS_BASE_URL_.'/products.xml',
]);
}
}
/**
* Get product list from database by lang.
*
* @param int $langId
* @return array the list of products.
*/
public function getProducts(int $langId)
{
return $this->get('product_repository')->findAllByLangId($langId);
}
/**
* Serialize a list of products.
*
* @param array $products
* @return string the XML output
*/
public function serializeProducts(array $products)
{
$productsXml = $this->get('serializer')->serialize($products, 'xml', [
'xml_root_node_name' => 'products',
'xml_format_output' => true,
]);
return $productsXml;
}
/**
* Write file into a specific location.
*
* @param string $fileContent
* @param string $filepath
* @throws IOException if the file cannot be written to
* @return bool true if the file has been created
*/
public function writeFile(string $fileContent, string $filepath)
{
$this->get('filesystem')->dumpFile($filepath, $fileContent);
}
}