-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphpist_github.php
311 lines (270 loc) · 10.2 KB
/
phpist_github.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
/**
* 2017 - 2018 PHPIST
*
* NOTICE OF LICENSE
*
* This file is licenced under the Software License Agreement.
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* You must not modify, adapt or create derivative works of this source code.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future.
*
* @author PHPIST <[email protected]>
* @copyright 2017 - 2018 PHPIST
* @license MIT license
*/
if (!defined('_PS_VERSION_')) {
exit;
}
class Phpist_Github extends Module
{
// define constants to be used throughout the module
const PHPIST_GITHUB_ACCOUNT = 'PHPIST_GITHUB_ACCOUNT';
const PHPIST_GITHUB_REPO = 'PHPIST_GITHUB_REPO';
const PHPIST_GITHUB_COMMITS = 'PHPIST_GITHUB_COMMITS';
private $_html = '';
public function __construct()
{
$this->name = 'phpist_github';
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->author = 'Yassine Belkaid - PHPIST';
$this->need_instance = 0;
$this->secure_key = Tools::encrypt($this->name);
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Github commits API listing');
$this->description = $this->l('A module which allows to display a list of commits from a chosen Github repo.');
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => '1.6.1.17');
}
/**
* @see Module::install()
*
*/
public function install()
{
if (parent::install()
&& $this->registerHook('displayBackOfficeHeader')
&& $this->registerHook('displayHeader')
&& $this->registerHook('displayHome')
) {
// add default configuration
Configuration::updateValue(self::PHPIST_GITHUB_ACCOUNT, 'PrestaShop');
Configuration::updateValue(self::PHPIST_GITHUB_REPO, 'PrestaShop');
Configuration::updateValue(self::PHPIST_GITHUB_COMMITS, 10);
return true;
}
return false;
}
/**
* @see Module::uninstall()
*
*/
public function uninstall()
{
if (parent::uninstall()) {
/* Unset configuration */
Configuration::deleteByName(self::PHPIST_GITHUB_ACCOUNT);
Configuration::deleteByName(self::PHPIST_GITHUB_REPO);
Configuration::deleteByName(self::PHPIST_GITHUB_COMMITS);
return true;
}
return false;
}
public function getContent()
{
// Validate & process
if (Tools::isSubmit('submitConfigurations')) {
if ($this->_postValidation()) {
$this->_postProcess();
}
}
return $this->_html . $this->renderForm();
}
/**
* Create configration form
*
*/
public function renderForm()
{
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l('Github Settings'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Github account'),
'class' => 'fixed-width-lg',
'name' => self::PHPIST_GITHUB_ACCOUNT,
),
array(
'type' => 'text',
'label' => $this->l('Github repo'),
'class' => 'fixed-width-lg',
'name' => self::PHPIST_GITHUB_REPO,
),
array(
'type' => 'text',
'label' => $this->l('Number of commits'),
'class' => 'fixed-width-lg',
'name' => self::PHPIST_GITHUB_COMMITS
),
),
'submit' => array(
'title' => $this->l('Save'),
)
),
);
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
$helper->default_form_language = $lang->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$this->fields_form = array();
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitConfigurations';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'fields_value' => $this->getConfigFieldsValues(),
'id_language' => $this->context->language->id
);
return $helper->generateForm(array($fields_form));
}
public function getConfigFieldsValues()
{
return array(
self::PHPIST_GITHUB_ACCOUNT => Configuration::get(self::PHPIST_GITHUB_ACCOUNT),
self::PHPIST_GITHUB_REPO => Configuration::get(self::PHPIST_GITHUB_REPO),
self::PHPIST_GITHUB_COMMITS => (int)Configuration::get(self::PHPIST_GITHUB_COMMITS),
);
}
public function hookDisplayBackOfficeHeader($params)
{
// run script only on this configuration page.
if (!Tools::getValue('configure') || Tools::getValue('configure') != $this->name) {
return;
}
$this->context->controller->addJS($this->_path .'views/js/pg_bk.js');
Media::addJsDef(array(
'pg_mum_warning_msg' => $this->l('Only numbers are allowed!')
));
}
public function hookDisplayHeader($params)
{
// only serve the css file at homepage
if ($this->context->controller->php_self != 'index') {
return;
}
$this->context->controller->addCSS($this->_path .'views/css/pg_list.css');
}
public function hookDisplayHome($params)
{
$cacheId = $this->name .'::hookDisplayHome';
// cache template for better performance
if (!$this->isCached('commits_list.tpl', $this->getCacheId($cacheId))) {
$commits = array();
$autoloader = __DIR__ .'/vendor/autoload.php';
$configs = $this->getConfigFieldsValues();
if (file_exists($autoloader)) {
// call the composer autoload file to access to the Github client
require_once $autoloader;
try {
$client = new \Github\Client();
$commits = $client->api('repo')->commits()->all($configs[self::PHPIST_GITHUB_ACCOUNT], $configs[self::PHPIST_GITHUB_REPO], array('sha' => 'master', 'per_page' => (int)$configs[self::PHPIST_GITHUB_COMMITS]));
} catch (Github\Exception\RuntimeException $e) {
PrestaShopLogger::addLog('Github client: '. (string)$e->getMessage(), 3, $e->getCode());
return false;
}
$commits = $this->commitPresenter($commits);
}
$this->context->smarty->assign(array(
'commits' => $commits,
'commits_number' => (int)$configs[self::PHPIST_GITHUB_COMMITS],
));
}
return $this->display(__FILE__, 'commits_list.tpl', $this->getCacheId($cacheId));
}
/**
* Validate configuration data
*
* @return boolean
*/
private function _postValidation()
{
$errors = array();
// Validatr configurations
if (!Validate::isGenericName(Tools::getValue(self::PHPIST_GITHUB_ACCOUNT))) {
$errors[] = $this->l('Invalid Github account name.');
}
if (!Validate::isGenericName(Tools::getValue(self::PHPIST_GITHUB_REPO))) {
$errors[] = $this->l('Invalid Github repository.');
}
if (!Validate::isInt(Tools::getValue(self::PHPIST_GITHUB_COMMITS)) || (int)Tools::getValue(self::PHPIST_GITHUB_COMMITS) <= 0) {
$errors[] = $this->l('Invalid number of commits, please provide a valid number.');
}
// Display errors if needed
if (count($errors)) {
foreach ($errors as $error) {
$this->_html .= $this->displayError($error);
}
return false;
}
// validation is ok
return true;
}
/**
* Process configuration data to be updated
*
* @return void
*/
private function _postProcess()
{
$res = true;
$res &= Configuration::updateValue(self::PHPIST_GITHUB_ACCOUNT, Tools::getValue(self::PHPIST_GITHUB_ACCOUNT));
$res &= Configuration::updateValue(self::PHPIST_GITHUB_REPO, Tools::getValue(self::PHPIST_GITHUB_REPO));
$res &= Configuration::updateValue(self::PHPIST_GITHUB_COMMITS, (int)Tools::getValue(self::PHPIST_GITHUB_COMMITS));
if (!$res) {
$this->_html .= $this->displayError($this->l('The configuration could not be updated.'));
} else {
// clear template cache
$this->_clearCache('commits_list.tpl');
$this->_html .= $this->displayConfirmation($this->l('The configuration has been successfully updated.'));
}
}
/**
* Present commits data to be used in displaying content
*
* @param array $commits List of returned commits from the Github API
*
* @return array
*/
public function commitPresenter($commits = array())
{
$presentCommits = array();
if (!is_array($commits) || !count($commits)) {
return $presentCommits;
}
foreach ($commits as $commit) {
$commit_date = new DateTime($commit['commit']['author']['date']);
$presentCommits[] = array(
'message' => $commit['commit']['message'],
'commit_url' => $commit['html_url'],
'author' => $commit['author']['login'],
'author_url' => $commit['author']['html_url'],
'commit_date' => $commit_date->format('d/m/Y'),
);
}
return $presentCommits;
}
}