d_event_manager.php (11684B)
1 <?php 2 /* 3 * location: admin/model 4 */ 5 6 class ModelExtensionModuleDEventManager extends Model { 7 8 /** 9 10 Modal functions 11 12 **/ 13 14 public function getEvents($data = array()) { 15 $sql = "SELECT "; 16 17 if(!empty($data['unique'])){ 18 $sql .= " DISTINCT "; 19 } 20 21 $sql .= " * FROM `" . DB_PREFIX . "event` "; 22 23 $implode = array(); 24 25 if (!empty($data['filter_code'])) { 26 $implode[] = "`code` LIKE '%" . $this->db->escape($data['filter_code']) . "%'"; 27 } 28 29 if (!empty($data['filter_trigger'])) { 30 $implode[] = "`trigger` LIKE '%" . $this->db->escape($data['filter_trigger']) . "%'"; 31 } 32 33 if (!empty($data['filter_action'])) { 34 $implode[] = "`action` LIKE '%" . $this->db->escape($data['filter_action']) . "%'"; 35 } 36 37 if (isset($data['filter_status']) && !is_null($data['filter_status'])) { 38 $implode[] = "`status` = '" . (int)$data['filter_status'] . "'"; 39 } 40 41 42 if ($implode) { 43 $sql .= " WHERE " . implode(" AND ", $implode); 44 } 45 46 if(!empty($data['unique'])){ 47 $sql .= " GROUP BY code "; 48 } 49 50 $sort_data = array( 51 'code', 52 'trigger', 53 'action', 54 'sort_order', 55 'status' 56 ); 57 58 if (isset($data['sort']) && in_array($data['sort'], $sort_data)) { 59 $sql .= " ORDER BY `" . $data['sort'] . "`"; 60 } else { 61 $sql .= " ORDER BY `sort_order`"; 62 } 63 64 if (isset($data['order']) && ($data['order'] == 'DESC')) { 65 $sql .= " DESC"; 66 } else { 67 $sql .= " ASC"; 68 } 69 70 if (isset($data['start']) || isset($data['limit'])) { 71 if ($data['start'] < 0) { 72 $data['start'] = 0; 73 } 74 75 if ($data['limit'] < 1) { 76 $data['limit'] = 20; 77 } 78 79 $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit']; 80 } 81 82 $query = $this->db->query($sql); 83 84 return $query->rows; 85 } 86 87 public function getTotalEvents($data = array()) { 88 $sql = "SELECT COUNT(*) AS total FROM " . DB_PREFIX . "event"; 89 90 $implode = array(); 91 92 if (!empty($data['filter_code'])) { 93 $implode[] = "`code` LIKE '%" . $this->db->escape($data['filter_code']) . "%'"; 94 } 95 96 if (!empty($data['filter_trigger'])) { 97 $implode[] = "`trigger` LIKE '%" . $this->db->escape($data['filter_trigger']) . "%'"; 98 } 99 100 if (!empty($data['filter_action'])) { 101 $implode[] = "`action` LIKE '%" . $this->db->escape($data['filter_action']) . "%'"; 102 } 103 104 if (isset($data['filter_status']) && !is_null($data['filter_status'])) { 105 $implode[] = "`status` = '" . (int)$data['filter_status'] . "'"; 106 } 107 108 if ($implode) { 109 $sql .= " WHERE " . implode(" AND ", $implode); 110 } 111 112 $query = $this->db->query($sql); 113 114 return $query->row['total']; 115 } 116 117 public function updateEvent($event_id, $data){ 118 if(!isset($data['sort_order'])){ 119 $data['sort_order'] = 0; 120 } 121 $this->db->query("UPDATE " . DB_PREFIX . "event SET 122 `code` = '" . $this->db->escape($data['code'])."', 123 `trigger` = '" . $this->db->escape($data['trigger'])."', 124 `action` = '" . $this->db->escape($data['action'])."', 125 `status` = '". (int)$data['status']."', 126 `sort_order` = '". (int)$data['sort_order']."' 127 WHERE event_id = '" . (int)$event_id . "'"); 128 129 return $this->getEventById($event_id); 130 } 131 132 public function addEvent($code, $trigger, $action, $status = 1, $sort_order = 0) { 133 //fix conflict 134 if(VERSION >= '2.3.0.0' && VERSION < '3.0.0.0'){ 135 $this->installDatabase(); 136 } 137 138 $this->db->query("INSERT INTO `" . DB_PREFIX . "event` SET `code` = '" . $this->db->escape($code) . "', `trigger` = '" . $this->db->escape($trigger) . "', `action` = '" . $this->db->escape($action) . "', `status` = '" . (int)$status . "', `sort_order` = '" . (int)$sort_order . "'"); 139 140 return $this->db->getLastId(); 141 } 142 143 public function deleteEvent($code) { 144 //if you have several events under one code - they will all be deleted. 145 //please use deleteEventById. 146 147 if(VERSION >= '3.0.0.0'){ 148 $this->load->model('setting/event'); 149 return $this->model_setting_event->deleteEventByCode($code); 150 }elseif(VERSION > '2.0.0.0'){ 151 $this->load->model('extension/event'); 152 return $this->model_extension_event->deleteEvent($code); 153 }else{ 154 155 $this->db->query("DELETE FROM " . DB_PREFIX . "event WHERE `code` = '" . $this->db->escape($code) . "'"); 156 157 } 158 159 } 160 161 public function deleteEventById($event_id) { 162 $this->db->query("DELETE FROM `" . DB_PREFIX . "event` WHERE `event_id` = '" . (int)$event_id . "'"); 163 } 164 165 public function deleteEventByCode($code) { 166 $this->db->query("DELETE FROM `" . DB_PREFIX . "event` WHERE `code` = '" . $this->db->escape($code) . "'"); 167 } 168 169 public function getEventById($event_id) { 170 $event = $this->db->query("SELECT * FROM `" . DB_PREFIX . "event` WHERE `event_id` = '" . $this->db->escape($event_id) ."'"); 171 172 return $event->row; 173 } 174 175 public function getEventByCode($code) { 176 $query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "event` WHERE `code` = '" . $this->db->escape($code) . "' LIMIT 1"); 177 178 return $query->row; 179 } 180 181 public function enableEvent($event_id) { 182 if(VERSION >= '3.0.0.0'){ 183 $this->load->model('setting/event'); 184 return $this->model_setting_event->enableEvent($event_id); 185 }elseif(VERSION > '2.3.0.0'){ 186 $this->load->model('extension/event'); 187 return $this->model_extension_event->enableEvent($event_id); 188 }else{ 189 $this->db->query("UPDATE " . DB_PREFIX . "event SET `status` = '1' WHERE event_id = '" . (int)$event_id . "'"); 190 } 191 192 } 193 194 public function disableEvent($event_id) { 195 if(VERSION >= '3.0.0.0'){ 196 $this->load->model('setting/event'); 197 return $this->model_setting_event->disableEvent($event_id); 198 }elseif(VERSION > '2.3.0.0'){ 199 $this->load->model('extension/event'); 200 return $this->model_extension_event->disableEvent($event_id); 201 }else{ 202 $this->db->query("UPDATE " . DB_PREFIX . "event SET `status` = '0' WHERE event_id = '" . (int)$event_id . "'"); 203 } 204 } 205 206 public function installDatabase(){ 207 208 $this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "event` ( 209 `event_id` int(11) NOT NULL AUTO_INCREMENT, 210 `code` varchar(32) NOT NULL, 211 `trigger` text NOT NULL, 212 `action` text NOT NULL, 213 `status` tinyint(1) NOT NULL, 214 `sort_order` int(3) NOT NULL, 215 PRIMARY KEY (`event_id`) 216 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;"); 217 218 219 $result = $this->db->query("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '".DB_DATABASE."' AND TABLE_NAME = '" . DB_PREFIX . "event' ORDER BY ORDINAL_POSITION")->rows; 220 $columns = array(); 221 foreach($result as $column){ 222 $columns[] = $column['COLUMN_NAME']; 223 } 224 225 if(!in_array('status', $columns)){ 226 $this->db->query("ALTER TABLE `" . DB_PREFIX . "event` ADD status int( 1 ) NOT NULL default '1'"); 227 } 228 229 if(!in_array('sort_order', $columns)){ 230 $this->db->query("ALTER TABLE `" . DB_PREFIX . "event` ADD `sort_order` int(3) NOT NULL"); 231 } 232 233 } 234 235 public function isCompatible(){ 236 237 if(VERSION >= '2.3.0.0'){ 238 if(VERSION < '3.0.0.0'){ 239 $this->installDatabase(); 240 } 241 return true; 242 } 243 244 $this->load->model('extension/d_opencart_patch/modification'); 245 246 $compatibility = $this->model_extension_d_opencart_patch_modification->getModificationByName('d_event_manager'); 247 if($compatibility){ 248 if(!empty($compatibility['status'])){ 249 return true; 250 } 251 } 252 253 return false; 254 } 255 256 public function installCompatibility(){ 257 258 $this->installDatabase(); 259 260 if(VERSION >= '2.3.0.0'){ 261 return true; 262 } 263 264 $d_opencart_patch = (file_exists(DIR_SYSTEM.'library/d_shopunity/extension/d_opencart_patch.json')); 265 if(!$d_opencart_patch){ 266 return false; 267 } 268 269 $this->load->model('extension/d_opencart_patch/modification'); 270 271 $compatibility = $this->model_extension_d_opencart_patch_modification->getModificationByName('d_event_manager'); 272 if($compatibility){ 273 if(!empty($compatibility['status'])){ 274 return true; 275 }else{ 276 $this->model_extension_d_opencart_patch_modification->setModification('d_event_manager.xml', 0); 277 } 278 } 279 280 $this->model_extension_d_opencart_patch_modification->setModification('d_event_manager.xml', 1); 281 $this->model_extension_d_opencart_patch_modification->refreshCache(); 282 283 return true; 284 } 285 286 public function uninstallCompatibility(){ 287 288 if(VERSION >= '2.3.0.0'){ 289 return true; 290 } 291 292 $d_opencart_patch = (file_exists(DIR_SYSTEM.'library/d_shopunity/extension/d_opencart_patch.json')); 293 if(!$d_opencart_patch){ 294 return false; 295 } 296 297 $this->load->model('extension/d_opencart_patch/modification'); 298 299 $compatibility = $this->model_extension_d_opencart_patch_modification->getModificationByName('d_event_manager'); 300 if(!$compatibility){ 301 return true; 302 } 303 304 $this->model_extension_d_opencart_patch_modification->setModification('d_event_manager.xml', 0); 305 $this->model_extension_d_opencart_patch_modification->refreshCache(); 306 307 return true; 308 } 309 310 /** 311 312 Helper functions 313 314 **/ 315 316 /* 317 * Format the link to work with ajax requests 318 */ 319 public function ajax($route, $url = '', $ssl = true){ 320 return str_replace('&', '&', $this->url->link($route, $url, $ssl)); 321 } 322 323 324 private $subversions = array('lite', 'light', 'free'); 325 /* 326 * Return name of config file. 327 */ 328 public function getConfigFileName($codename){ 329 330 if(isset($this->request->post['config'])){ 331 return $this->request->post['config']; 332 } 333 334 $setting = $this->config->get($codename.'_setting'); 335 336 if(isset($setting['config'])){ 337 return $setting['config']; 338 } 339 340 $full = DIR_SYSTEM . 'config/'. $codename . '.php'; 341 if (file_exists($full)) { 342 return $codename; 343 } 344 345 foreach ($this->subversions as $subversion){ 346 if (file_exists(DIR_SYSTEM . 'config/'. $codename . '_' . $subversion . '.php')) { 347 return $codename . '_' . $subversion; 348 } 349 } 350 351 return false; 352 } 353 354 /* 355 * Return list of config files that contain the codename of the module. 356 */ 357 public function getConfigFileNames($codename){ 358 $files = array(); 359 $results = glob(DIR_SYSTEM . 'config/'. $codename .'*'); 360 foreach($results as $result){ 361 $files[] = str_replace('.php', '', str_replace(DIR_SYSTEM . 'config/', '', $result)); 362 } 363 return $files; 364 } 365 }