-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBootstrap.php
More file actions
executable file
·265 lines (233 loc) · 8.07 KB
/
Bootstrap.php
File metadata and controls
executable file
·265 lines (233 loc) · 8.07 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
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
<?php
class Shopware_Plugins_Backend_CronMailNotifier_Bootstrap extends Shopware_Components_Plugin_Bootstrap
{
const EMAIL_KEY_NOTIFICATION = 'sOrderStatistic';
/**
* Helper for availability capabilities
* @return array
*/
public function getCapabilities(){
return array(
'install' => true,
'update' => true,
'enable' => true,
'secureUninstall' => true
);
}
/**
* Returns the meta information about the plugin.
*
* @return array
* @throws Exception
*/
public function getInfo()
{
return array(
'version' => $this->getVersion(),
'author' => 'Best Shopware Newbie',
'supplier' => 'Best Shopware Newbie',
'label' => $this->getLabel(),
'copyright' => 'Copyright © '.date('Y').', Best Shopware Newbie',
'description' => file_get_contents( __DIR__ . '/description.html' ),
'support' => 'info@BestShopwareNewbie',
'link' => 'http://BestShopwareNewbie/'
);
}
/**
* Returns the version of plugin as string.
*
* @return string
*/
public function getVersion() {
return '0.0.1';
}
/**
* Returns the plugin name for backend
*
* @return string
*/
public function getLabel() {
return 'Cron Mail Notifier';
}
/**
* Standard plugin install method to register all required components.
* @throws \Exception
* @return bool success
*/
public function install(){
// Check min shopware version - if need
if (!$this->assertMinimumVersion('4.3.3')) {
throw new Exception('This plugin requires Shopware 4.3.3 or a later version.');
}
try{
$this->subscribeEvents();
$this->createConfig();
$this->registerCronJobs();
$this->createNotifyTemplate();
} catch(Exception $e){
return array(
'success' => false,
'message' => $e->getMessage(),
'invalidateCache' => $this->getInvalidateCacheArray()
);
}
return array(
'success' => true,
'message' => 'Plugin was sucessfully installed',
'invalidateCache' => $this->getInvalidateCacheArray()
);
}
/**
* On uninstall remove attributes and re-generate customer-attribute models
* @return bool
*/
public function uninstall(){
try{
$this->unRegisterCronJobs();
$this->removeNotifyTemplate();
}catch(Exception $e){
return array(
'success' => false,
'message' => $e->getMessage(),
'invalidateCache' => $this->getInvalidateCacheArray()
);
}
return array(
'success' => true,
'message' => 'Plugin was sucessfully uninstalled',
'invalidateCache' => $this->getInvalidateCacheArray()
);
}
/*
* Add subscription for b-e events
*/
public function onStartFrontDispatch(){
$container = Shopware()->Container();
$subscribers = array(
new \ShopwarePlugins\CronMailNotifier\Subscriber\Cronjob($this, $container),
);
foreach($subscribers as $subscriber)
$this->get('events')->addSubscriber( $subscriber );
}
/**
* Register Plugin namespace in autoloader
*/
public function afterInit()
{
$this->Application()->Loader()->registerNamespace(
'ShopwarePlugins\CronMailNotifier',
$this->Path()
);
}
private function subscribeEvents(){
$this->subscribeEvent('Enlight_Controller_Front_StartDispatch','onStartFrontDispatch');
}
private function createConfig(){
$store = $preDefinedStatuses = array();
$statuses = \Shopware()->Db()->fetchAll("
SELECT `id`, `name`, `description`
FROM `s_core_states`
WHERE `group`='state'
ORDER BY `position` ASC");
foreach($statuses as $status){
if($status['name']=='completely_delivered') $preDefinedStatuses[] = $status['id'];
$store[] = array(
$status['id'], $status['description'] . (isset($status['name'])? ' (' . $status['name'] . ')': '')
);
}
$this->Form()->setElement('select', 'status', array(
'label' => 'Order Status',
'store' => $store,
'value' => $preDefinedStatuses,
'multiSelect' => true,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP,
'description' => 'Mail will be count all orders with such statuses.'
));
$store = $preDefinedGroups = array();
$groups = \Shopware()->Db()->fetchAll("SELECT `id`, `groupkey`, `description`
FROM s_core_customergroups
ORDER BY `groupkey` ASC");
foreach($groups as $group){
$preDefinedGroups[] = $group['groupkey'];
$store[] = array(
$group['groupkey'], $group['description']
);
}
$this->Form()->setElement('select', 'groupKeys', array(
'label' => 'Customer Group',
'store' => $store,
'value' => $preDefinedGroups,
'multiSelect' => true,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP,
'description' => 'Mail will be count all orders only from these customer group.'
));
$this->Form()->setElement('text', 'operator',
array(
'label' => 'EMail',
'value' => Shopware()->Config()->Mail,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP,
'description' => 'Cron mails will be send to this email.'
)
);
$this->Form()->setElement('select', 'period', array(
'label' => 'Period',
'store' => array(
array(1,'1 day'),
array(7,'7 days'),
array(14,'14 days'),
),
'value' => 7,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP,
'description' => 'Count order for N days.'
));
return true;
}
private function registerCronJobs()
{
try{
$this->createCronJob(
'Cron Mail Notifier',
'CronMailNotifier',
604800
);
} catch(\Exception $e){ }
}
private function createNotifyTemplate(){
try{
Shopware()->Db()->insert('s_core_config_mails', array(
'name' => Shopware_Plugins_Backend_CronMailNotifier_Bootstrap::EMAIL_KEY_NOTIFICATION,
'frommail'=>'{config name=mail}',
'fromname'=>'{config name=shopName}',
'subject'=>'Count of orders by `CronMailNotifier` for {config name=shopName}',
'content'=>
'Hello,'."\n\n".'Total {$sCount} orders for the last {$sPeriod} days.'."\n\n".'Team {config name=shopName} {config name=address}',
'contentHTML'=>
'Hello,<br/><br/>
Total {$sCount} orders for the last {$sPeriod} days.<br/><br/>
Team {config name=shopName}<br/>{config name=address}',
'ishtml'=>1,
'mailtype'=>1,
));
} catch(Exception $e){}
}
private function unRegisterCronJobs()
{
$sql = "DELETE FROM s_crontab WHERE pluginID = ?";
Shopware()->Db()->query($sql, array($this->getId()));
}
private function removeNotifyTemplate()
{
try{
Shopware()->Db()->exec( 'DELETE FROM `s_core_config_mails` WHERE `name`="'.Shopware_Plugins_Backend_CronMailNotifier_Bootstrap::EMAIL_KEY_NOTIFICATION.'"' );
} catch(Exception $e){}
}
/**
* Helper for cache array
* @return array
*/
private function getInvalidateCacheArray(){
return array(
'config', 'backend', 'proxy'
);
}
}