-
Notifications
You must be signed in to change notification settings - Fork 17
/
mymodcomments.php
202 lines (164 loc) · 5.15 KB
/
mymodcomments.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
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
<?php
require_once(dirname(__FILE__).'/classes/MyModComment.php');
class MyModComments extends Module
{
public function __construct()
{
$this->name = 'mymodcomments';
$this->tab = 'front_office_features';
$this->version = '0.4';
$this->author = 'Fabien Serny';
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('My Module of product comments');
$this->description = $this->l('With this module, your customers will be able to grade and comments your products.');
}
public function install()
{
// Call install parent method
if (!parent::install())
return false;
// Execute module install SQL statements
$sql_file = dirname(__FILE__).'/install/install.sql';
if (!$this->loadSQLFile($sql_file))
return false;
// Install admin tab
if (!$this->installTab('AdminCatalog', 'AdminMyModComments', 'MyMod Comments'))
return false;
// Register hooks
if (!$this->registerHook('displayProductTabContent') ||
!$this->registerHook('displayBackOfficeHeader') ||
!$this->registerHook('displayAdminProductsExtra') ||
!$this->registerHook('displayAdminCustomers') ||
!$this->registerHook('ModuleRoutes'))
return false;
// Preset configuration values
Configuration::updateValue('MYMOD_GRADES', '1');
Configuration::updateValue('MYMOD_COMMENTS', '1');
// All went well!
return true;
}
public function uninstall()
{
// Call uninstall parent method
if (!parent::uninstall())
return false;
// Execute module install SQL statements
// $sql_file = dirname(__FILE__).'/install/uninstall.sql';
// if (!$this->loadSQLFile($sql_file))
// return false;
// Uninstall admin tab
if (!$this->uninstallTab('AdminMyModComments'))
return false;
// Delete configuration values
Configuration::deleteByName('MYMOD_GRADES');
Configuration::deleteByName('MYMOD_COMMENTS');
// All went well!
return true;
}
public function loadSQLFile($sql_file)
{
// Get install SQL file content
$sql_content = file_get_contents($sql_file);
// Replace prefix and store SQL command in array
$sql_content = str_replace('PREFIX_', _DB_PREFIX_, $sql_content);
$sql_requests = preg_split("/;\s*[\r\n]+/", $sql_content);
// Execute each SQL statement
$result = true;
foreach($sql_requests as $request)
if (!empty($request))
$result &= Db::getInstance()->execute(trim($request));
// Return result
return $result;
}
public function installTab($parent, $class_name, $name)
{
// Create new admin tab
$tab = new Tab();
$tab->id_parent = (int)Tab::getIdFromClassName($parent);
$tab->name = array();
foreach (Language::getLanguages(true) as $lang)
$tab->name[$lang['id_lang']] = $name;
$tab->class_name = $class_name;
$tab->module = $this->name;
$tab->active = 1;
return $tab->add();
}
public function uninstallTab($class_name)
{
// Retrieve Tab ID
$id_tab = (int)Tab::getIdFromClassName($class_name);
// Load tab
$tab = new Tab((int)$id_tab);
// Delete it
return $tab->delete();
}
public function onClickOption($type, $href = false)
{
$confirm_reset = $this->l('Reseting this module will delete all comments from your database, are you sure you want to reset it ?');
$reset_callback = "return mymodcomments_reset('".addslashes($confirm_reset)."');";
$matchType = array(
'reset' => $reset_callback,
'delete' => "return confirm('Confirm delete?')",
);
if (isset($matchType[$type]))
return $matchType[$type];
return '';
}
public function getHookController($hook_name)
{
// Include the controller file
require_once(dirname(__FILE__).'/controllers/hook/'. $hook_name.'.php');
// Build dynamically the controller name
$controller_name = $this->name.$hook_name.'Controller';
// Instantiate controller
$controller = new $controller_name($this, __FILE__, $this->_path);
// Return the controller
return $controller;
}
public function hookDisplayProductTabContent($params)
{
$controller = $this->getHookController('displayProductTabContent');
return $controller->run($params);
}
public function hookDisplayBackOfficeHeader($params)
{
$controller = $this->getHookController('displayBackOfficeHeader');
return $controller->run($params);
}
public function hookDisplayAdminProductsExtra($params)
{
$controller = $this->getHookController('displayAdminProductsExtra');
return $controller->run();
}
public function hookDisplayAdminCustomers($params)
{
$controller = $this->getHookController('displayAdminCustomers');
return $controller->run();
}
public function hookModuleRoutes()
{
$controller = $this->getHookController('modulesRoutes');
return $controller->run();
}
public function getContent()
{
$ajax_hook = Tools::getValue('ajax_hook');
if ($ajax_hook != '')
{
$ajax_method = 'hook'.ucfirst($ajax_hook);
if (method_exists($this, $ajax_method))
die($this->{$ajax_method}(array()));
}
$controller = $this->getHookController('getContent');
return $controller->run();
}
public function smartyGetCacheId($name = null)
{
return $this->getCacheId($name);
}
public function smartyClearCache($template, $cache_id = null, $compile_id = null)
{
return $this->_clearCache($template, $cache_id, $compile_id);
}
}