-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilescatalog.php
More file actions
174 lines (145 loc) · 5.18 KB
/
Copy pathfilescatalog.php
File metadata and controls
174 lines (145 loc) · 5.18 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class FilesCatalog extends Module
{
const MODULE_NAME = 'filescatalog';
const MODULE_VERSION = '0.1.7';
const MODULE_JSON_PATH = _PS_MODULE_DIR_ . '/' . self::MODULE_NAME . '/views/json/data.json';
const MODULE_DATA_PATH_CONFIG = 'FILESCATALOG_PATH';
public function __construct()
{
$this->name = self::MODULE_NAME;
$this->tab = 'front_office_features';
$this->version = self::MODULE_VERSION;
$this->author = 'nyupyu';
$this->need_instance = 0;
$this->ps_versions_compliancy = [
'min' => '8.0',
'max' => '8.99.99'
];
$this->bootstrap = false;
parent::__construct();
$this->displayName = $this->l('Files Catalog');
$this->description = $this->l('A custom module to show files catalog on page.');
$this->config_path = Configuration::get(self::MODULE_DATA_PATH_CONFIG);
}
public function install()
{
Configuration::updateValue(self::MODULE_DATA_PATH_CONFIG, _PS_ROOT_DIR_ . '/');
return parent::install() && $this->registerHook('actionAdminControllerSetMedia') && $this->registerHook('actionDispatcher');
}
public function uninstall()
{
return parent::uninstall();
}
public function getContent()
{
$output = null;
if (Tools::isSubmit('submitFilescatalog')) {
$output .= $this->postProcess();
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
if (Tools::isSubmit('submitRefresh')) {
$this->saveToJSON(self::MODULE_JSON_PATH);
$output .= $this->displayConfirmation($this->l('The catalog has been updated.'));
}
$output .= $this->displayForm();
return $output;
}
public function displayForm()
{
$fieldsForm = [
'form' => [
'legend' => [
'title' => $this->l('Settings'),
],
'input' => [
[
'type' => 'text',
'label' => $this->l('Path to Analyze: public_html/'),
'name' => 'filescatalog_path',
'size' => 50,
'required' => true,
'value' => '',
]
],
'submit' => [
'title' => $this->l('Save'),
],
],
];
$helper = new HelperForm();
$helper->module = $this;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
$helper->default_form_language = (int)Configuration::get('PS_LANG_DEFAULT');
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$helper->title = $this->displayName;
$helper->submit_action = 'submitFilescatalog';
$form = $helper->generateForm([$fieldsForm]);
$customButton = $this->getCustomButton();
$form .= $customButton;
return $form;
}
public function getCustomButton()
{
$button = '<form action="' . Tools::safeOutput($_SERVER['REQUEST_URI']) . '" method="post">
<input type="hidden" name="Refresh" value="1" />
<button type="submit" name="submitRefresh" class="btn btn-default">
' . $this->l('Refresh') . '
</button>
</form>';
return $button;
}
private function postProcess()
{
$newPath = Tools::getValue('filescatalog_path');
Configuration::updateValue(self::MODULE_DATA_PATH_CONFIG, _PS_ROOT_DIR_ . '/' . $newPath);
}
public function saveToJSON($jsonPath)
{
$data = $this->processFolderStructure($this->config_path);
$json = json_encode($data, JSON_PRETTY_PRINT);
return $this->writeJSONToFile($jsonPath, $json);
}
public function processFolderStructure($directory)
{
$result = array();
$contents = scandir($directory);
foreach ($contents as $item) {
if (!in_array($item, array(".", ".."))) {
$path = $directory . DIRECTORY_SEPARATOR . $item;
if (is_dir($path)) {
$result[$item] = $this->processFolderStructure($path);
} else {
$result[] = $item;
}
}
}
return $result;
}
private function writeJSONToFile($jsonPath, $json)
{
return file_put_contents($jsonPath, $json) !== false;
}
public function hookActionAdminControllerSetMedia()
{
if (Tools::getValue('controller') === 'AdminModules' && Tools::getValue('configure') === $this->name) {
$this->context->controller->addJS($this->_path . 'views/js/back.js');
}
}
public function hookActionDispatcher($params)
{
$controller = $params['controller'];
if ($controller instanceof FrontController && $controller->php_self === 'filescatalog') {
$this->getFrontController();
}
}
private function getFrontController()
{
Tools::redirect($this->context->link->getModuleLink('filescatalog', 'file'));
}
}
?>