forked from klarstil/ShopwareMobile
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBootstrap.php
More file actions
532 lines (463 loc) · 16.9 KB
/
Bootstrap.php
File metadata and controls
532 lines (463 loc) · 16.9 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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
<?php
/**
* [plugin]
* @link http://www.shopware.de
* @package Plugins
* @subpackage [scope]
* @copyright Copyright (c) [year], shopware AG
* @version [version] [date] [revision]
* @author [author]
*/
class Shopware_Plugins_Frontend_SwagMobileTemplate_Bootstrap extends Shopware_Components_Plugin_Bootstrap
{
/** 16px x 16px phone icon for the backend module (base64 encoded) */
private static $icnBase64 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABwElEQVQ4jY3SO6/TMBjG8b/jpAlJSi+0alVoYUEHmJC4iIlP0q1zJyYY+Q6Hjc6I4fA1GBAdGJCABVF0quoAp216cWI7DCAEDVLr0Xqen+zXFuys4+Nn+Ww2wxhDHIW4nodSCqVSms0Gw+FQ/J13d4Hx+C2vTk6o1qrcf/AQ13MZv3nNZDKh3+/vxotAkiTcvnOXXrfL5StdttstRzdv0Wp3UErtBxzp8eTpYxq1+E8h8APOFktePH+5HxACOldjKlEJbeSvkCvxqhdBFPpFwBiDsUu2uSQnR+CQ6pS1NhitD7iCgMn8A5dMiHAFYHE2Zc5PHfRBgOPwZfaO70uDyRVuZFGfGsSre2ht9gMgCIMQ31ekWY5aLXDKKxLxEWsPAIwxlCK4EHl4WuKlEr/uslydYa3dD2RZhso/Q64wQuP4gkRK1iWPTBef4b/A11Mol0NcE5JaSywVer0hVQecYLPZskk6eCIiME02WlNx55RWS7Jsuh+Yz88ZPXqPsWCFpFqtsfjxDaUUN46uFwBnd6NerxP4Pr7n0mm1uNbrEQYlKuWIOI4KQGEqo9Eon06nvz+NAHIApJS0220Gg8E/nZ864b4HQnsY4wAAAABJRU5ErkJggg==';
/**
* Standard install method which creates the event listeners, the plugin configuration form,
* and a new menu entry in the backend.
*
* Note that this method fills in the default configuration
*
* @access public
* @return bool
*/
public function install()
{
$this->deletePreviousEntries();
$this->createEvents();
$this->createBackendMenuEntry();
// Create database table
Shopware()->Db()->query("CREATE TABLE `s_plugin_mobile_settings` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 255 ) NOT NULL ,
`value` VARCHAR( 255 ) NOT NULL
) ENGINE = MYISAM ;"
);
// Fill database table fields
Shopware()->Db()->query("INSERT INTO `s_plugin_mobile_settings` (`name` ,`value`)
VALUES
('supportedDevices', 'iPhone|iPod|Android'),
('supportedPayments', '3|4|5|20'),
('agbInfoID', '4'),
('cancelRightID', '8'),
('infoGroupName', 'gMobile'),
('showNormalVersionLink', '1'),
('useAsSubshop', '0'),
('subshopID', '1'),
('useSenchaIO', '0'),
('showBanner', '0'),
('useVoucher', '0'),
('useNewsletter', '0'),
('useComment', '0'),
('colorTemplate', 'default'),
('checkboxesGreen', '0'),
('logoUpload', ''),
('logoHeight', ''),
('additionalCSS', ''),
('iconUpload', ''),
('startupUpload', ''),
('screenshots', ''),
('statusbarStyle', 'default'),
('glossOnIcon', '1'),
('apptitle', ''),
('appversion', ''),
('publishdate', ''),
('keywords', ''),
('contact_email', ''),
('support_url', ''),
('app_url', ''),
('description', ''),
('changelog', '');"
);
/** Add new mail template */
$sql = "INSERT INTO `s_core_config_mails` (`id`, `name`, `frommail`, `fromname`, `subject`, `content`, `contentHTML`, `ishtml`, `htmlable`, `attachment`) VALUES (NULL, 'sMobileNativeApplicationRequest', 'info@example.com', 'info@example.com', 'Antrag - Native Applikation auf Basis von Shopware Mobile', 'Hallo Shopware-Team,\r\n\r\nich möchte gerne eine native Applikation für meinen Shop bei Ihnen beantragen.\r\n\r\nDie Rahmeninformationen lauten wie folgt:\r\n\r\nAnsprechpartner: {sContactPerson}\r\n\r\nShop-Name: {sShop}\r\n\r\nShop-URL: {sShopURL}\r\n\r\nBeschreibung: {sMessage}\r\n\r\n', '', 0, 0, '');";
Shopware()->Db()->query($sql);
$form = $this->Form();
$form->setElement('checkbox', 'showNotice', array('label'=>'Meldung anzeigen, dass eine mobile Variante zur Verfügung steht', 'value'=>'1', 'scope'=>Shopware_Components_Form::SCOPE_SHOP));
$form->setElement('text', 'startColor', array('label'=>'Start-Farbe der Meldung (Hex-Code)', 'value'=>'#FF6400', 'scope'=>Shopware_Components_Form::SCOPE_SHOP));
$form->setElement('text', 'endColor', array('label'=>'End-Farbe der Meldung (Hex-Code)', 'value'=>'#DD4800', 'scope'=>Shopware_Components_Form::SCOPE_SHOP));
$form->setElement('text', 'textColor', array('label'=>'Text-Farbe der Meldung (Hex-Code)', 'value'=>'#FFFFFF', 'scope'=>Shopware_Components_Form::SCOPE_SHOP));
$form->setElement('controllerbutton', 'Backendmodul aufrufen', array('label'=>'Shopware Mobile Backend Modul öffnen','value'=>'','attributes'=>array('controller'=>'MobileTemplate','action'=>'skeleton')));
$form->save();
Shopware()->Cache()->clean(Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array(
'Shopware_Config'
));
return true;
}
/**
* Standard uninstall method which just drops the configuration
* tables from DB
*
* @access public
* @return bool
*/
public function uninstall()
{
// Delete settings table
Shopware()->Db()->query('DROP TABLE IF EXISTS `s_plugin_mobile_settings`;');
// Delete menu entry
Shopware()->Db()->query("DELETE IGNORE FROM `s_core_menu` WHERE `name` = 'Shopware Mobile'");
/* Delete the mail template */
Shopware()->Db()->query("DELETE IGNORE FROM `s_core_config_mails` WHERE `name` = 'sMobileNativeApplicationRequest'");
// Delete previous entries
$this->deletePreviousEntries();
return true;
}
/**
* Helper method which subscribes the necessary events
*
* @access public
* @return void
*/
public function createEvents()
{
/* Subscribe events */
$event = $this->createEvent(
'Enlight_Controller_Dispatcher_ControllerPath_Frontend_MobileTemplate',
'onGetControllerPath'
);
$this->subscribeEvent($event);
$event = $this->createEvent(
'Enlight_Controller_Dispatcher_ControllerPath_Backend_MobileTemplate',
'onGetControllerPathBackend'
);
$this->subscribeEvent($event);
$event = $this->createEvent(
'Enlight_Controller_Action_PostDispatch',
'onPostDispatch'
);
$this->subscribeEvent($event);
$event = $this->createEvent(
'Enlight_Controller_Action_PostDispatch_Backend_Index',
'onPostDispatchBackend'
);
$this->subscribeEvent($event);
$event = $this->createEvent(
'Enlight_Controller_Action_PostDispatch_Frontend_Register',
'onPostDispatchRegister'
);
$this->subscribeEvent($event);
$event = $this->createEvent(
'Shopware_Modules_Admin_SaveRegister_Start',
'onSaveRegisterStart'
);
$this->subscribeEvent($event);
$event = $this->createEvent(
'Enlight_Controller_Action_PostDispatch_Frontend_Checkout',
'mobileFinishAction'
);
$this->subscribeEvent($event);
}
/**
* Helper method whichs deletes old subscribers from the DB
*
* @access public
* @return void
*/
public function deletePreviousEntries()
{
$sql = "DELETE FROM s_core_subscribes WHERE listener LIKE 'Shopware_Plugins_Frontend_SwagMobileTemplate_Bootstrap%';";
Shopware()->Db()->query($sql);
}
/**
* Helper method which creates the backend menu entry
* for the mobile template
*
* @access public
* @return void
*/
public function createBackendMenuEntry()
{
$parent = $this->Menu()->findOneBy('label', 'Marketing');
$item = $this->createMenuItem(array(
'label' => 'Shopware Mobile',
'onclick' => 'openAction(\'MobileTemplate\');',
'class' => 'ico2 iphone',
'active' => 1,
'parent' => $parent,
'style' => 'background-position: 5px 5px;'
));
$this->Menu()->addItem($item);
$this->Menu()->save();
}
/**
* Returns the meta informations of this plugin
*
* @access public
* @return mixed
*/
public function getInfo()
{
return include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Meta.php');
}
/**
* Passes the plugin configuration to the view, handles the subshop
* support, checks if the the used device is supported and sets
* the backend module icon
*
* @static
* @access public
* @param Enlight_Event_EventArgs $args
* @return
*/
public static function onPostDispatch(Enlight_Event_EventArgs $args)
{
$request = $args->getSubject()->Request();
$response = $args->getSubject()->Response();
$view = $args->getSubject()->View();
$pluginOpts = Shopware()->Plugins()->Frontend()->SwagMobileTemplate()->Config();
if(!$request->isDispatched() || $response->isException() || $request->getModuleName() !== 'frontend') {
return;
}
$session = &Shopware()->Session()->Mobile;
// Get plugin settings from our own db table
$config = Shopware()->Db()->fetchAll('SELECT * FROM `s_plugin_mobile_settings`');
$properties = array();
foreach($config as $prop) {
$properties[$prop['name']] = $prop['value'];
}
$config = $properties;
if(!isset($session['useMobile'])) {
$firstVisit = true;
} else {
$firstVisit = false;
}
// Check the device of the user
$version = self::checkForMobileDevice($config['supportedDevices']);
if(!isset($session['useMobile']) && $version == 'mobile') {
$config['useAsSubshop'] = (bool) $config['useAsSubshop'];
if(Shopware()->System()->sLanguage == $config['subshopID'] && $config['useAsSubshop']) {
$session['useMobile'] = true;
} else {
$session['useMobile'] = $version == 'mobile';
}
}
if($request->getParam('sMobile') !== null) {
$session['useMobile'] = (bool) $request->getPost('sMobile') ;
}
if($session['useMobile'] === true && !$firstVisit) {
// Render the mobile template
$dirs = Shopware()->Template()->getTemplateDir();
$newDirs = array_merge(array(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Views' . DIRECTORY_SEPARATOR), $dirs);
Shopware()->Template()->setTemplateDir($newDirs);
$basepath = Shopware()->Config()->BasePath;
// Assign plugin configuration
$view->assign('shopwareMobile', array(
'additionalCSS' => $config['additionalCSS'],
'isUserLoggedIn' => Shopware()->Modules()->sAdmin()->sCheckUser(),
'useNormalSite' => $config['showNormalVersionLink'],
'template' => 'frontend'. DIRECTORY_SEPARATOR . '_resources'.DIRECTORY_SEPARATOR.'styles' . DIRECTORY_SEPARATOR . trim($config['colorTemplate']) . '.css',
'useVoucher' => $config['useVoucher'],
'useNewsletter' => $config['useNewsletter'],
'useComment' => $config['useComment'],
'logoPath' => $config['logoUpload'],
'logoHeight' => $config['logoHeight'],
'iconPath' => $config['iconUpload'],
'glossOnIcon' => $config['glossOnIcon'],
'startUpPath' => $config['startupUpload'],
'statusBarStyle' => $config['statusbarStyle'],
'payments' => $config['supportedPayments'],
'agbID' => $config['agbInfoID'],
'cancellationID' => $config['cancelRightID'],
'checkboxGreen' => $config['checkboxesGreen'],
'showBanner' => $config['showBanner'],
'basePath' => $basepath
));
return;
}
// Render the normal version and show a hint to use shopware mobile
$view->addTemplateDir(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Views' . DIRECTORY_SEPARATOR);
if(empty($config['useAsSubshop'])) {
$config['useAsSubshop'] = 0;
}
$view->assign('shopwareMobile', array(
'useSubShop' => $config['useAsSubshop'],
'subShopId' => $config['subshopID'],
'userAgents' => $config['supportedDevices'],
'basePath' => $request->getBasePath(),
'active' => $firstVisit,
'version' => $version,
'pluginOpts' => $pluginOpts
));
$view->extendsTemplate('frontend' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'swag_mobiletemplate' . DIRECTORY_SEPARATOR . 'index.tpl');
}
/**
* Returns the path of the frontend controller which handles the
* whole mobile template
*
* @static
* @access public
* @return string
*/
public static function onGetControllerPath()
{
return dirname(__FILE__) . DIRECTORY_SEPARATOR . 'MobileTemplate.php';
}
/**
* Helper event listener which adds the backend module icon
* of Shopware Mobile
*
* @static
* @access public
* @param Enlight_Event_EventArgs $args
* @return
*/
public static function onPostDispatchBackend(Enlight_Event_EventArgs $args)
{
$view = $args->getSubject()->View();
$request = $args->getSubject()->Request();
$response = $args->getSubject()->Response();
if(!$request->isDispatched() || $response->isException() || $request->getModuleName() !== 'backend') {
return;
}
$view->extendsBlock('backend_index_css', '<style type="text/css">a.iphone { background-image: url("'. self::$icnBase64 .'"); background-repeat: no-repeat; }</style>', 'append');
return;
}
/**
* Returns the path of the backend controller which handles the whole
* backend module
*
* @static
* @access public
* @return string
*/
public static function onGetControllerPathBackend()
{
return dirname(__FILE__) . DIRECTORY_SEPARATOR . 'MobileTemplateAdmin.php';
}
/**
* Refactors the error message string in a suitable format (utf8 encoded)
*
* @static
* @access public
* @param Enlight_Event_EventArgs $args
* @return void
*/
public static function onPostDispatchRegister(Enlight_Event_EventArgs $args)
{
$request = $args->getSubject()->Request();
$response = $args->getSubject()->Response();
$view = $args->getSubject()->View();
$mobileSession = Shopware()->Session()->Mobile;
if(!$mobileSession) {
return;
}
$error_flags = array();
if(!empty($view->register->personal->error_messages)) {
$errors = '';
foreach($view->register->personal->error_messages as $k => $v) {
$errors = $errors . '<br/>' . utf8_encode($v);
}
$error_flags[] = $view->register->personal->error_flags;
}
$billingErrorFlags = array();
if(!empty($view->register->billing->error_messages)) {
foreach($view->register->billing->error_messages as $k => $v) {
$errors = $errors . '<br/>' . utf8_encode($v);
}
$error_flags[] = $view->register->billing->error_flags;
}
$view->registerErrors = $errors;
$view->error_flags = $error_flags;
}
/**
* Corrects the character encoding for the registration
* informations due to the fact that sencha touch needs
* the strings utf8 encoded.
*
* @static
* @access public
* @param Enlight_Event_EventArgs $args
* @return void
*/
public static function onSaveRegisterStart(Enlight_Event_EventArgs $args)
{
$subject = $args->getSubject();
$session = &Shopware()->Session();
if($session['Mobile']['useMobile'] && !empty($session['sRegister']['billing'])) {
foreach($session['sRegister']['billing'] as $key => $value) {
$value = utf8_decode($value);
$value = htmlentities($value);
$session['sRegister']['billing'][$key] = $value;
}
}
}
/**
* Helper method which checks if the payment was processed
* by an external payment provider (like PayPal or Heidelpay),
* sets the mandatory informations in the view and loads the
* whole base template structure from the index/index.tpl
*
* Note that this method will be only come in place if the
* request wasn't a XML HTTP request
*
* @static
* @param Enlight_Event_EventArgs $args
*/
public static function mobileFinishAction(Enlight_Event_EventArgs $args)
{
$subject = $args->getSubject();
$request = $subject->Request();
$view = $args->getSubject()->View();
$mobileSession = Shopware()->Session()->Mobile;
$uniqueID = $request->getParam('sUniqueID');
$paymentType = $request->getParam('paymentType');
/** Payment successfully */
if($request->getActionName() === 'finish' && !$request->isXmlHttpRequest() && !empty($uniqueID)) {
/** PayPal Express Mobile payment */
$result = Shopware()->Db()->query('SELECT * FROM s_order WHERE transactionID = ?', array($request->getParam('sUniqueID')));
$result = $result->fetch();
$view->assign('lastOrder', array(
'ordernumber' => $result['ordernumber'],
'invoice_amount' => $result['invoice_amount'],
'date' => date('H:i:s d.m.Y', strtotime($result['ordertime'])),
'payment_method' => 'PayPal'
));
}
/** Payment canceled */
if($request->getActionName() === 'confirm' && !$request->isXmlHttpRequest()) {
if(!empty($paymentType) && $paymentType === 'Sale') {
$view->assign('canceledOrder', true);
}
}
/** Load the basic structure */
if(!$request->isXmlHttpRequest() && $mobileSession['useMobile'] == true) {
$view->loadTemplate(dirname(__FILE__) . '/Views/frontend/index/index.tpl');
}
}
/**
* Checks against the user agent if the user's
* currently used device is supported by the
* mobile template
*
* @access private
* @param $devices
* @return string
*/
private function checkForMobileDevice($devices) {
$agent = $_SERVER['HTTP_USER_AGENT'];
$device = 'desktop';
if(preg_match('/(' . $devices . ')/i', $agent)) {
$device = 'mobile';
}
return $device;
}
/**
* Helper method which redirects the user to the start view
* of the mobile template.
*
* This fixes the broken images due to the fact that the images
* are set relative to the location.
*
* @access private
* @param $request - Request object of the event listener
* @param $response - Response object of the event listener
* @return string - URL
*/
private function redirectToBasePath($request, $response) {
$url = $request->getScheme().'://'.$request->getHttpHost();
$url .= $request->getBaseUrl().'/';
$response->setRedirect($url);
return $url;
}
}