-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpublishproductlinkstotwitter.php
More file actions
203 lines (181 loc) · 8.63 KB
/
publishproductlinkstotwitter.php
File metadata and controls
203 lines (181 loc) · 8.63 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
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
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class PublishProductLinksToTwitter extends Module
{
public function __construct()
{
$this->name = 'publishproductlinkstotwitter';
$this->tab = 'advertising_marketing';
$this->version = '1.0';
$this->author = 'Kamba Abudu';
$this->need_instance = 1;
$this->ps_versions_compliancy = array('min' => '1.5', 'max' => '1.6');
$this->is_configurable = 1;
$this->module_key = 'd8b57cddf079b455b96d1f5554be90c1';
parent::__construct();
$this->displayName = $this->l('Publish product links to Twitter');
$this->description = $this->l('This module enables your Prestashop store to publish product titles, prices and links for your products to Twitter periodically, automatically via a cron job (which you will need to set up). The current version selects products at random but a future version may allow you to specify certain rules for how products should be selected. You need to create a Twitter Application for your prestashop store as you will need the provided consumer key/secret and access token/secret to configure the module.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
if (!Configuration::get('PLTOTWITTER_CONSUMER_KEY')
|| !Configuration::get('PLTOTWITTER_CONSUMER_SECRET')
|| !Configuration::get('PLTOTWITTER_ACCESS_TOKEN')
|| !Configuration::get('PLTOTWITTER_ACCESS_TOKEN_SECRET'))
{
$this->warning = $this->l('No Twitter Consumer Key/Secret, Access token/secret pair provided');
}
}
public function install()
{
if (parent::install() == false)
{
return false;
}
Configuration::updateValue('PLTOTWITTER_LIMIT', 1);
return true;
}
public function uninstall()
{
return parent::uninstall()
&& Configuration::deleteByName('PLTOTWITTER_LIMIT')
&& Configuration::deleteByName('PLTOTWITTER_CONSUMER_KEY')
&& Configuration::deleteByName('PLTOTWITTER_CONSUMER_SECRET')
&& Configuration::deleteByName('PLTOTWITTER_ACCESS_TOKEN')
&& Configuration::deleteByName('PLTOTWITTER_ACCESS_TOKEN_SECRET');
}
public function getContent()
{
$output = null;
if (Tools::isSubmit('submit'.$this->name))
{
$error = false;
$limit = strval(Tools::getValue('PLTOTWITTER_LIMIT'));
$consumer_key = strval(Tools::getValue('PLTOTWITTER_CONSUMER_KEY'));
$consumer_secret = strval(Tools::getValue('PLTOTWITTER_CONSUMER_SECRET'));
$access_token = strval(Tools::getValue('PLTOTWITTER_ACCESS_TOKEN'));
$access_token_secret = strval(Tools::getValue('PLTOTWITTER_ACCESS_TOKEN_SECRET'));
if (!$consumer_key || empty($consumer_key))
{
$output .= $this->displayError( $this->l('The Twitter Consumer Key cannot be empty') );
$error = true;
}
if(!$consumer_secret || empty($consumer_secret))
{
$output .= $this->displayError( $this->l('The Twitter Consumer Secret cannot be empty') );
$error = true;
}
if(!$limit || !is_int((int)$limit))
{
$output .= $this->displayError( $this->l('The Number of Products to Publish cannot be zero or empty') );
$error = true;
}
if(!$access_token || empty($access_token))
{
$output .= $this->displayError( $this->l('The Twitter Access Token cannot be empty') );
$error = true;
}
if(!$access_token_secret || empty($access_token_secret))
{
$output .= $this->displayError( $this->l('The Twitter Access Token Secret cannot be empty') );
$error = true;
}
if($error == false)
{
Configuration::updateValue('PLTOTWITTER_LIMIT', $limit);
Configuration::updateValue('PLTOTWITTER_CONSUMER_KEY', $consumer_key);
Configuration::updateValue('PLTOTWITTER_CONSUMER_SECRET', $consumer_secret);
Configuration::updateValue('PLTOTWITTER_ACCESS_TOKEN', $access_token);
Configuration::updateValue('PLTOTWITTER_ACCESS_TOKEN_SECRET', $access_token_secret);
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
}
return $output.$this->displayForm();
}
public function displayForm()
{
// Get default Language
$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$fields_form = array();
// Init Fields form array
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('Settings'),
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Twitter Consumer Key'),
'name' => 'PLTOTWITTER_CONSUMER_KEY',
'size' => 50,
'required' => true
),
array(
'type' => 'text',
'label' => $this->l('Twitter Consumer Secret'),
'name' => 'PLTOTWITTER_CONSUMER_SECRET',
'size' => 100,
'required' => true
),
array(
'type' => 'text',
'label' => $this->l('Twitter Access Token'),
'name' => 'PLTOTWITTER_ACCESS_TOKEN',
'size' => 100,
'required' => true
),
array(
'type' => 'text',
'label' => $this->l('Twitter Access Token Secret'),
'name' => 'PLTOTWITTER_ACCESS_TOKEN_SECRET',
'size' => 100,
'required' => true
),
array(
'type' => 'text',
'label' => $this->l('Number of Products to Publish'),
'name' => 'PLTOTWITTER_LIMIT',
'size' => 10,
'required' => true
)
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'button'
)
);
$helper = new HelperForm();
// Module, t oken and currentIndex
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
// Language
$helper->default_form_language = $default_lang;
$helper->allow_employee_form_lang = $default_lang;
// Title and toolbar
$helper->title = $this->displayName;
$helper->show_toolbar = true; // false -> remove toolbar
$helper->toolbar_scroll = true; // yes - > Toolbar is always visible on the top of the screen.
$helper->submit_action = 'submit'.$this->name;
$helper->toolbar_btn = array(
'save' =>
array(
'desc' => $this->l('Save'),
'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
'&token='.Tools::getAdminTokenLite('AdminModules'),
),
'back' => array(
'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
'desc' => $this->l('Back to list')
)
);
// Load current value
$helper->fields_value['PLTOTWITTER_CONSUMER_KEY'] = Configuration::get('PLTOTWITTER_CONSUMER_KEY');
$helper->fields_value['PLTOTWITTER_CONSUMER_SECRET'] = Configuration::get('PLTOTWITTER_CONSUMER_SECRET');
$helper->fields_value['PLTOTWITTER_ACCESS_TOKEN'] = Configuration::get('PLTOTWITTER_ACCESS_TOKEN');
$helper->fields_value['PLTOTWITTER_ACCESS_TOKEN_SECRET'] = Configuration::get('PLTOTWITTER_ACCESS_TOKEN_SECRET');
$helper->fields_value['PLTOTWITTER_LIMIT'] = Configuration::get('PLTOTWITTER_LIMIT');
return $helper->generateForm($fields_form);
}
}