|
30 | 30 |
|
31 | 31 | class Ps_Googleanalytics extends Module |
32 | 32 | { |
| 33 | + /** |
| 34 | + * @var string Name of the module running on PS 1.6.x. Used for data migration. |
| 35 | + */ |
| 36 | + const PS_16_EQUIVALENT_MODULE = 'ganalytics'; |
| 37 | + |
33 | 38 | protected $js_state = 0; |
34 | 39 | protected $eligible = 0; |
35 | 40 | protected $filterable = 1; |
@@ -59,6 +64,8 @@ public function install() |
59 | 64 | Shop::setContext(Shop::CONTEXT_ALL); |
60 | 65 | } |
61 | 66 |
|
| 67 | + $this->uninstallPrestaShop16Module(); |
| 68 | + |
62 | 69 | if (parent::install() && |
63 | 70 | $this->registerHook('displayHeader') && |
64 | 71 | $this->registerHook('displayAdminOrder') && |
@@ -86,6 +93,30 @@ public function uninstall() |
86 | 93 | return false; |
87 | 94 | } |
88 | 95 |
|
| 96 | + /** |
| 97 | + * Migrate data from 1.6 equivalent module (if applicable), then uninstall |
| 98 | + */ |
| 99 | + public function uninstallPrestaShop16Module() |
| 100 | + { |
| 101 | + if (!Module::isInstalled(self::PS_16_EQUIVALENT_MODULE)) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + $oldModule = Module::getInstanceByName(self::PS_16_EQUIVALENT_MODULE); |
| 105 | + if ($oldModule) { |
| 106 | + if (method_exists($oldModule, 'uninstallTab')) { |
| 107 | + $oldModule->uninstallTab(); |
| 108 | + } |
| 109 | + // This closure calls the parent class to prevent data to be erased |
| 110 | + // It allows the new module to be configured without migration |
| 111 | + $parentUninstallClosure = function() { |
| 112 | + return parent::uninstall(); |
| 113 | + }; |
| 114 | + $parentUninstallClosure = $parentUninstallClosure->bindTo($oldModule, get_class($oldModule)); |
| 115 | + $parentUninstallClosure(); |
| 116 | + } |
| 117 | + return true; |
| 118 | + } |
| 119 | + |
89 | 120 | /** |
90 | 121 | * Creates tables |
91 | 122 | */ |
|
0 commit comments