-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.php
executable file
·163 lines (134 loc) · 5.98 KB
/
script.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
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
defined('DS') or define('DS', DIRECTORY_SEPARATOR);
class com_virtuemart_myparcelnlInstallerScript
{
public function install($parent) {
$this->install_sql();
$this->replace_files();
}
public function update($parent) {
$this->install_sql();
$this->replace_files();
}
public function uninstall($parent) {
$this->remove_replace_files();
}
function install_sql(){
// db
$db = JFactory::getDBO();
$sql_config = "CREATE TABLE IF NOT EXISTS `myparcel_config` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`updated_at` DATETIME NULL,
`configs` TEXT NULL ,
PRIMARY KEY (`id`)
) DEFAULT COLLATE=utf8_general_ci;";
$sql_orders = "CREATE TABLE IF NOT EXISTS `myparcel_virtuemart_orders` (
`orders_myparcel_id` int(11) NOT NULL AUTO_INCREMENT,
`order_id` int(11) NOT NULL,
`consignment_id` bigint(20) NOT NULL,
`postcode` varchar(6) NOT NULL,
`created_at` datetime NOT NULL,
PRIMARY KEY (`orders_myparcel_id`),
UNIQUE KEY `orders_id_consignment_id` (`order_id`,`consignment_id`)
) DEFAULT COLLATE=utf8_general_ci;";
$db->setQuery($sql_config);
$db->execute();
$db->setQuery($sql_orders);
$db->execute();
}
function remove_replace_files(){
// orders
$templates = array(
'list.php',
'orders.php',
'order.php',
);
foreach($templates as $template_item){
$file = JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart' . DS . 'views' . DS . 'orders' . DS . 'tmpl' . DS . $template_item;
if (file_exists($file)) {
if (is_writable($file)) {
$content = file_get_contents($file);
if (strpos($content, '// include myparcel js file') !== false) { // remove old replace if already existed
$content = preg_replace('/\/\/ include myparcel js file[\s\S]+?AdminUIHelper\:\:startAdminArea/', 'AdminUIHelper::startAdminArea', $content);
file_put_contents($file, $content);
}
}else{
echo '<p>'.$file.' is not writable!</p>';
return false;
}
}
}
return true;
}
function replace_files(){
// list orders
$this->replace_orders_list();
// detail order
$this->replace_order_detail();
}
function replace_orders_list(){
$templates = array(
'list.php',
'orders.php',
);
foreach($templates as $template_item){
$file = JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart' . DS . 'views' . DS . 'orders' . DS . 'tmpl' . DS . $template_item;
if (file_exists($file)) {
if (is_writable($file)) {
$content = file_get_contents($file);
if (strpos($content, '// include myparcel js file') !== false) { // remove old replace if already existed
$content = preg_replace('/\/\/ include myparcel js file[\s\S]+?AdminUIHelper\:\:startAdminArea/', 'AdminUIHelper::startAdminArea', $content);
}
$target_str = 'AdminUIHelper::startAdminArea';
$target_replace = '// include myparcel js file'."\r\n";
$target_replace .= '$document = JFactory::getDocument();'."\r\n";
$target_replace .= '$document->addScriptDeclaration(\'var myparcel_base = "\'.JURI::base().\'"\');'."\r\n";
$target_replace .= '$document->addStyleSheet(JURI::base().\'components/com_virtuemart_myparcelnl/template/js/sweetalert/sweetalert.css\');'."\r\n\r\n";
$target_replace .= '$document->addScript(JURI::base().\'components/com_virtuemart_myparcelnl/template/js/sweetalert/sweetalert.min.js\');'."\r\n\r\n";
$target_replace .= '$document->addScript(JURI::base().\'components/com_virtuemart_myparcelnl/template/js/myparcel.js\');'."\r\n\r\n";
$target_replace .= '$document->addScript(JURI::base().\'components/com_virtuemart_myparcelnl/template/js/myparcel_orders.js\');'."\r\n\r\n";
$target_replace .= 'AdminUIHelper::startAdminArea';
$content = str_replace($target_str, $target_replace, $content);
file_put_contents($file, $content);
}else{
echo '<p>'.$file.' is not writable!</p>';
return false;
}
}
}
return true;
}
function replace_order_detail(){
$templates = array(
'order.php',
);
foreach($templates as $template_item){
$file = JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart' . DS . 'views' . DS . 'orders' . DS . 'tmpl' . DS . $template_item;
if (file_exists($file)) {
if (is_writable($file)) {
$content = file_get_contents($file);
if (strpos($content, '// include myparcel js file') !== false) { // remove old replace if already existed
$content = preg_replace('/\/\/ include myparcel js file[\s\S]+?AdminUIHelper\:\:startAdminArea/', 'AdminUIHelper::startAdminArea', $content);
}
$target_str = 'AdminUIHelper::startAdminArea';
$target_replace = '// include myparcel js file'."\r\n";
$target_replace .= '$document = JFactory::getDocument();'."\r\n";
$target_replace .= '$document->addScriptDeclaration(\'var myparcel_base = "\'.JURI::base().\'"\');'."\r\n";
$target_replace .= '$document->addStyleSheet(JURI::base().\'components/com_virtuemart_myparcelnl/template/js/sweetalert/sweetalert.css\');'."\r\n\r\n";
$target_replace .= '$document->addScript(JURI::base().\'components/com_virtuemart_myparcelnl/template/js/sweetalert/sweetalert.min.js\');'."\r\n\r\n";
$target_replace .= '$document->addScript(JURI::base().\'components/com_virtuemart_myparcelnl/template/js/myparcel.js\');'."\r\n\r\n";
$target_replace .= '$document->addScript(JURI::base().\'components/com_virtuemart_myparcelnl/template/js/myparcel_order_item.js\');'."\r\n\r\n";
$target_replace .= 'AdminUIHelper::startAdminArea';
$content = str_replace($target_str, $target_replace, $content);
file_put_contents($file, $content);
}else{
echo '<p>'.$file.' is not writable!</p>';
return false;
}
}
}
return true;
}
}