/halogy/application/modules/events/models/events_model.php
PHP | 473 lines | 343 code | 85 blank | 45 comment | 27 complexity | 4d41869f0de57ac2d30e816d3a2cca09 MD5 | raw file
1<?php 2/** 3 * Halogy 4 * 5 * A user friendly, modular content management system for PHP 5.0 6 * Built on CodeIgniter - http://codeigniter.com 7 * 8 * @package Halogy 9 * @author Haloweb Ltd. 10 * @copyright Copyright (c) 2008-2011, Haloweb Ltd. 11 * @license http://halogy.com/license 12 * @link http://halogy.com/ 13 * @since Version 1.0 14 * @filesource 15 */ 16 17// ------------------------------------------------------------------------ 18 19class Events_Model extends Model { 20 21 function Events_Model() 22 { 23 parent::Model(); 24 25 // get siteID, if available 26 if (defined('SITEID')) 27 { 28 $this->siteID = SITEID; 29 } 30 } 31 32 function get_all_events() 33 { 34 $this->db->where('deleted', 0); 35 $this->db->where('published', 1); 36 $this->db->where('siteID', $this->siteID); 37 38 $query = $this->db->get('events'); 39 40 if ($query->num_rows() > 0) 41 { 42 $result = $query->result_array(); 43 return $result; 44 } 45 else 46 { 47 return FALSE; 48 } 49 } 50 51 function get_event($eventID) 52 { 53 $this->db->where('eventID', $eventID); 54 $this->db->where('deleted', 0); 55 $this->db->where('published', 1); 56 $this->db->where('siteID', $this->siteID); 57 58 $query = $this->db->get('events', 1); 59 60 if ( $query->num_rows() == 1 ) 61 { 62 $event = $query->row_array(); 63 64 return $event; 65 } 66 else 67 { 68 return FALSE; 69 } 70 } 71 72 function get_post_by_id($eventID) 73 { 74 $this->db->where('eventID', $eventID); 75 76 $query = $this->db->get('event_post', 1); 77 78 if ($query->num_rows()) 79 { 80 $post = $query->row_array(); 81 82 return $post; 83 } 84 else 85 { 86 return FALSE; 87 } 88 } 89 90 function get_tags() 91 { 92 $this->db->join('tags_ref', 'tags_ref.tag_id = tags.id'); 93 $this->db->where('tags_ref.siteID', $this->siteID); 94 95 $query = $this->db->get('tags'); 96 97 if ($query->num_rows()) 98 { 99 return $query->result_array(); 100 } 101 else 102 { 103 return FALSE; 104 } 105 } 106 107 function update_tags($eventID = '', $tags = '') 108 { 109 // add tags 110 if ($tags) 111 { 112 $this->tags->delete_tag_ref( 113 array( 114 'table' => 'events', 115 'row_id' => $eventID, 116 'siteID' => $this->siteID) 117 ); 118 119 $tags = str_replace(',', ' ', trim($tags)); 120 $tagsArray = explode(' ', $tags); 121 foreach($tagsArray as $key => $tag) 122 { 123 $tag = trim($tag); 124 if (isset($tag) && $tags != '' && strlen($tag) > 0) 125 { 126 $tidyTagsArray[] = $tag; 127 } 128 } 129 $tags = array( 130 'table' => 'events', 131 'tags' => $tidyTagsArray, 132 'row_id' => $eventID, 133 'siteID' => $this->siteID 134 ); 135 $this->tags->add_tags($tags); 136 137 return true; 138 } 139 else 140 { 141 return FALSE; 142 } 143 } 144 145 function get_events($num = '') 146 { 147 // default where 148 $where = array( 149 'deleted' => 0, 150 'published' => 1, 151 'siteID' => $this->siteID 152 ); 153 154 // where event is not old 155 $where['eventDate >'] = date("Y-m-d H:i:s", time()); 156 157 // wheres 158 $this->db->where($where); 159 160 // order by event date 161 $this->db->order_by('eventDate', 'asc'); 162 163 // get rows with paging 164 $query = $this->db->get('events', $num); 165 166 if ($query->num_rows()) 167 { 168 return $query->result_array(); 169 } 170 else 171 { 172 return FALSE; 173 } 174 } 175 176 function get_featured_events($num = '') 177 { 178 // default where 179 $where = array( 180 'deleted' => 0, 181 'published' => 1, 182 'featured' => 1, 183 'siteID' => $this->siteID 184 ); 185 186 // wheres 187 $this->db->where($where); 188 189 // order by event date 190 $this->db->order_by('eventDate', 'desc'); 191 192 // get rows with paging 193 $query = $this->db->get('events'); 194 195 if ($query->num_rows()) 196 { 197 return $query->result_array(); 198 } 199 else 200 { 201 return FALSE; 202 } 203 } 204 205 function get_month($month = '', $year = '') 206 { 207 // default where 208 $where = array( 209 'deleted' => 0, 210 'published' => 1, 211 'siteID' => $this->siteID 212 ); 213 214 // where event is not old and is in this month 215 $month = ($month) ? $month : date("m", time()); 216 $next_month = $month + 1; 217 $year = ($year) ? $year : date("Y", time()); 218 219 $from = date("Y-m-d H:i:s", mktime(0, 0, 0, $month, 1, $year)); 220 $to = date("Y-m-d H:i:s", mktime(23, 59, 59, $next_month, 0, $year)); 221 222 $where['eventDate >'] = $from; 223 $where['eventDate <'] = $to; 224 225 // wheres 226 $this->db->where($where); 227 228 // order by event date 229 $this->db->order_by('eventDate', 'asc'); 230 231 // get rows with paging 232 $query = $this->db->get('events'); 233 234 if ($query->num_rows()) 235 { 236 return $query->result_array(); 237 } 238 else 239 { 240 return FALSE; 241 } 242 } 243 244 function get_events_by_tag($tag, $limit = 20) 245 { 246 // get rows based on this tag 247 $result = $this->tags->fetch_rows(array( 248 'table' => 'events', 249 'tags' => array(1, $tag), 250 'limit' => $limit, 251 'siteID' => $this->siteID 252 )); 253 $tags = $result->result_array(); 254 foreach ($tags as $tag) 255 { 256 $tagsArray[] = $tag['row_id']; 257 } 258 259 // default where 260 $this->db->where(array( 261 'deleted' => 0, 262 'published' => 1, 263 'siteID' => $this->siteID 264 )); 265 266 // where event is not old 267 $where['eventDate >'] = date("Y-m-d H:i:s", time()); 268 269 // where tags 270 $this->db->where_in('eventID', $tagsArray); 271 $this->db->order_by('eventDate', 'asc'); 272 273 $query = $this->db->get('events', $limit); 274 275 if ($query->num_rows() > 0) 276 { 277 return $query->result_array(); 278 } 279 else 280 { 281 return FALSE; 282 } 283 } 284 285 function get_events_by_date($year, $month = '', $day = 0) 286 { 287 if ($month) 288 { 289 $from = date("Y-m-d H:i:s", mktime(0, 0, 0, $month, ((!$day) ? 1 : $day), $year)); 290 $to = ($day < 1) ? date("Y-m-d H:i:s", mktime(23, 59, 59, ($month+1), $day, $year)) : date("Y-m-d H:i:s", mktime(23, 59, 59, $month, $day, $year)); 291 } 292 else 293 { 294 $from = date("Y-m-d H:i:s", mktime(0, 0, 0, 1, ((!$day) ? 1 : $day), $year)); 295 $to = date("Y-m-d H:i:s", mktime(0, 0, 0, 1, 1, ($year+1))); 296 } 297 298 $this->db->where('eventDate >', $from); 299 $this->db->where('eventDate <', $to); 300 $this->db->where('deleted', 0); 301 $this->db->where('published', 1); 302 $this->db->where('siteID', $this->siteID); 303 304 $this->db->order_by('eventDate'); 305 306 $query = $this->db->get('events'); 307 308 if ($query->num_rows() > 0) 309 { 310 return $query->result_array(); 311 } 312 else 313 { 314 return FALSE; 315 } 316 } 317 318 function get_post_by_title($title = '') 319 { 320 $this->db->where('eventTitle', $title); 321 $this->db->where('deleted', 0); 322 $this->db->where('published', 1); 323 $this->db->where('siteID', $this->siteID); 324 325 $query = $this->db->get('events'); 326 327 if ($query->num_rows() > 0) 328 { 329 return $query->row_array(); 330 } 331 else 332 { 333 return FALSE; 334 } 335 } 336 337 function get_archive() 338 { 339 // selects 340 $this->db->select('COUNT(eventID) as numEvents, DATE_FORMAT(eventDate, "%M %Y") as dateStr, DATE_FORMAT(eventDate, "%m") as month, DATE_FORMAT(eventDate, "%Y") as year', FALSE); 341 $this->db->where('deleted', 0); 342 $this->db->where('published', 1); 343 $this->db->where('siteID', $this->siteID); 344 345 // where event is old 346 $this->db->where('eventDate <', date("Y-m-d H:i:s", time())); 347 348 // group by month 349 $this->db->group_by('dateStr'); 350 351 $query = $this->db->get('events'); 352 353 if ($query->num_rows() > 0) 354 { 355 return $query->result_array(); 356 } 357 else 358 { 359 return FALSE; 360 } 361 } 362 363 function get_headlines($num = 10) 364 { 365 $this->db->select('eventID, eventTitle, eventDate, description'); 366 return $this->get_events($num); 367 } 368 369 function search_events($query = '', $ids = '') 370 { 371 if (!$query && !$ids) 372 { 373 return FALSE; 374 } 375 376 // default wheres 377 $this->db->where('deleted', 0); 378 $this->db->where('published', 1); 379 $this->db->where('siteID', $this->siteID); 380 381 // search 382 if ($query) 383 { 384 // tidy query 385 $q = $this->db->escape_like_str($query); 386 387 $sql = '(eventTitle LIKE "%'.$q.'%" OR description LIKE "%'.$q.'%")'; 388 } 389 if ($ids) 390 { 391 $sql .= ' OR eventID IN ('.implode(',', $ids).')'; 392 } 393 $this->db->where($sql); 394 395 // where event is not old 396 $this->db->where('eventDate >', date("Y-m-d H:i:s", time())); 397 398 $this->db->order_by('eventDate', 'asc'); 399 400 $query = $this->db->get('events'); 401 402 if ($query->num_rows() > 0) 403 { 404 405 return $query->result_array(); 406 } 407 else 408 { 409 return FALSE; 410 } 411 } 412 413 function lookup_user($userID, $display = FALSE) 414 { 415 // default wheres 416 $this->db->where('userID', $userID); 417 418 // grab 419 $query = $this->db->get('users', 1); 420 421 if ($query->num_rows()) 422 { 423 $row = $query->row_array(); 424 425 if ($display !== FALSE) 426 { 427 return ($row['displayName']) ? $row['displayName'] : $row['firstName'].' '.$row['lastName']; 428 } 429 else 430 { 431 return $row; 432 } 433 } 434 else 435 { 436 return FALSE; 437 } 438 } 439 440 441 442 443 /// OLD!!! /// 444 445 446 function tag_cloud($num) 447 { 448 $this->db->select('t.tag, COUNT(pt.tagID) as qty', FALSE); 449 $this->db->join('tags pt', 'pt.tag_id = t.id', 'inner'); 450 $this->db->groupby('t.id'); 451 452 $query = $this->db->get('tags t'); 453 454 $built = array(); 455 456 if ($query->num_rows > 0) 457 { 458 $result = $query->result_array(); 459 460 foreach ($result as $row) 461 { 462 $built[$row['tag']] = $row['qty']; 463 } 464 465 return $built; 466 } 467 else 468 { 469 return array(); 470 } 471 } 472 473}