web/wp-content/plugins/gigs-calendar/gigs-classes.php
branchwordpress
changeset 109 03b0d1493584
equal deleted inserted replaced
-1:000000000000 109:03b0d1493584
       
     1 <?php
       
     2 global $wpdb;
       
     3 define('GIGS_DB_PREFIX', apply_filters('gigCal_db_prefix', $wpdb->prefix));
       
     4 define('TABLE_VENUES', GIGS_DB_PREFIX . 'gigs_venue');
       
     5 define('TABLE_GIGS', GIGS_DB_PREFIX . 'gigs_gig');
       
     6 define('TABLE_PERFORMANCES', GIGS_DB_PREFIX . 'gigs_performance');
       
     7 define('TABLE_TOURS', GIGS_DB_PREFIX . 'gigs_tour');
       
     8 
       
     9 class dtcGigs {
       
    10 	
       
    11 	function generateSlug($x) {
       
    12 		$x = strtolower($x);
       
    13 		$x = str_replace(' ', '-', $x);
       
    14 		$x = preg_replace('#[^a-z\-0-9]#', '', $x);
       
    15 		return $x;
       
    16 	}
       
    17 		
       
    18 	function escapeForInput($x) {
       
    19 		if ( '0000-00-00' == $x ) {
       
    20 			$x = '';
       
    21 		}
       
    22 		echo htmlspecialchars($x);
       
    23 	}
       
    24 	
       
    25 	function dateFormat($date, $type = 'short') {
       
    26 		if ( is_null($date) || $date == '0000-00-00' ) return null;
       
    27 		$options = get_option('gigs-calendar');
       
    28 		if ( isset($options['date'][$type]) ) {
       
    29 			return mysql2date($options['date'][$type], $date);
       
    30 		} else {
       
    31 			return mysql2date($type, $date);
       
    32 		}
       
    33 	}
       
    34 	
       
    35 	function selectFields($fields, $g) {
       
    36 		$options = get_option('gigs-calendar');
       
    37 		
       
    38 		$result = '';
       
    39 		foreach ( $options['gigs-table-show'] as $f ) {
       
    40 			if ( isset($fields[$f]) ) {
       
    41 				$tmp = $fields[$f];
       
    42 				if ( $f == 'venue' && $options['list-venue-link'] && !empty($g->venueLink) ) {
       
    43 					$tmp = preg_replace("~\{(.*?)\}~", '<a href="' . $g->venueLink . '">\1</a>', $tmp);
       
    44 				} elseif ( $f == $options['gig-link-field'] ) {
       
    45 					$tmp = preg_replace("~\{(.*?)\}~", '<a href="' . $g->permalink . '">\1</a>', $tmp);
       
    46 				} else {
       
    47 					$tmp = preg_replace("~\{(.*?)\}~", '\1', $tmp);
       
    48 				}
       
    49 					
       
    50 				$result .= $tmp;
       
    51 			}
       
    52 		}
       
    53 		
       
    54 		return $result;
       
    55 	}
       
    56 	
       
    57 	function timeFormat($time, $format = false) {
       
    58 		$options = get_option('gigs-calendar');
       
    59 		if ( is_null($time) ) {
       
    60 			return $options['tbd-text'];
       
    61 		}
       
    62 		if ( $options['time-12h'] || $format === "12h" ) {
       
    63 			return mysql2date("g:ia", '2008-01-01 ' . $time);
       
    64 		} else {
       
    65 			return mysql2date("H:i", '2000-01-01 ' . $time);
       
    66 		}
       
    67 	}
       
    68 
       
    69 	function mapLink($gig) {
       
    70 		global $options;
       
    71 		if ( empty($gig->customMap) ) {
       
    72 			$a = array();
       
    73 			
       
    74 			if ( empty($gig->address) ) 
       
    75 				$a[] = $gig->name . ' near:';
       
    76 			else {}
       
    77 				$a[] = str_replace("\n", ", ", $gig->address);
       
    78 			if ( !empty($gig->city) ) $a[] = $gig->city;
       
    79 			if ( !empty($gig->state) ) $a[] = $gig->state;
       
    80 			if ( !empty($gig->country) ) $a[] = $gig->country;
       
    81 			if ( !empty($gig->postalCode) ) $a[] = $gig->postalCode;
       
    82 			return 'http://maps.google.com/?q=' . urlencode(str_replace('near:,', 'near:', implode(', ', $a)));
       
    83 		} else {
       
    84 			if ( preg_match("/^https?:\/\//", $gig->customMap) ) {
       
    85 				return $gig->customMap;
       
    86 			} else {
       
    87 				return 'http://maps.google.com/?q=' . urlencode($gig->customMap);
       
    88 			}
       
    89 		}
       
    90 	}
       
    91 	
       
    92 	function getAttribution() {
       
    93 		$options = get_option('gigs-calendar');
       
    94 		?>
       
    95 			<div class="attribution">
       
    96 				<?php if ( $options['b4b-link'] ) : ?>
       
    97 					<span class="b4b">Powered by <a target="_blank" href="http://blogsforbands.com">Blogs for Bands</a></span>
       
    98 				<?php endif; ?>
       
    99 				<?php if ( $options['silk-link'] ) : ?>
       
   100 					<span class="silk">Silk icons by <a target="_blank" href="http://www.famfamfam.com/lab/icons/silk/">Mark James</a></span>
       
   101 				<?php endif; ?>
       
   102 			</div>
       
   103 		<?php
       
   104 	}
       
   105 	
       
   106 	function loadTemplate($file, $template = null, $data = array()) {
       
   107 		$options = get_option('gigs-calendar');
       
   108 		global $gcd;
       
   109 		if ( is_null($template) ) {
       
   110 			$template = $options['template'];
       
   111 		}
       
   112 		
       
   113 		$folder = dtc_gigs::get_url();
       
   114 		
       
   115 		extract($data);
       
   116 		if ( is_file(dirname(__FILE__) . '/templates/' . $template . '/' . $file . '.php') ) {
       
   117 			include(dirname(__FILE__) . '/templates/' . $template . '/' . $file . '.php');
       
   118 		} elseif ( is_file(ABSPATH . 'wp-content/gigs-templates/' . $template . '/' . $file . '.php') ) {
       
   119 			include(ABSPATH . 'wp-content/gigs-templates/' . $template . '/' . $file . '.php');
       
   120 		} else {
       
   121 			include(dirname(__FILE__) . '/templates/basic/' . $file . '.php');				
       
   122 		}
       
   123 	}
       
   124 	
       
   125 	function extraFields($fields, &$data, $gig = "new") {
       
   126 		foreach ( $fields as $key => $field ) {
       
   127 			if ( isset($data[$field['name']]) ) {
       
   128 				$value = array_pop($data[$field['name']]);
       
   129 				if ( empty($data[$field['name']]) ) unset($data[$field['name']]);
       
   130 			} else {
       
   131 				$value = $field['default'];
       
   132 			}
       
   133 			?>
       
   134 				<tr class="<?php echo addslashes($field['name']) ?>">
       
   135 					<td valign="top" class="<?php echo addslashes($field['name']) ?>">
       
   136 						<input type="hidden" value="<?php echo addslashes($field['name']) ?>" name="custom-key[<?php echo $key ?>]" />
       
   137 						<label for="custom-<?php echo $gig ?>-<?php echo $key ?>"><?php echo $field['label'] ?></label>
       
   138 						<textarea style="display:none;" rows="1" name="old-custom-value[<?php echo $key ?>]"><?php echo htmlspecialchars((is_array($value) ? serialize($value) : $value)) ?></textarea>
       
   139 						<input class="delete" type="hidden" name="deletecustom[<?php echo $key ?>]" value="0" />
       
   140 					</td>
       
   141 					<td>
       
   142 						<?php if ( $field['type'] == 'text' ) : ?>
       
   143 							<input name="custom-value[<?php echo $key ?>]" type="text" id="custom-<?php echo $gig ?>-<?php echo $key ?>" value="<?php echo addslashes($value); ?>" />
       
   144 						<?php elseif ( $field['type'] == 'date' ) : ?>
       
   145 							<input class="date-field" name="custom-value[<?php echo $key ?>]" type="text" id="custom-<?php echo $gig ?>-<?php echo $key ?>" value="<?php echo addslashes($value); ?>" />
       
   146 						<?php elseif ( $field['type'] == 'textarea' ) : ?>
       
   147 							<textarea rows="1" id="custom-<?php echo $gig ?>-<?php echo $key ?>" name="custom-value[<?php echo $key ?>]"><?php echo htmlspecialchars($value) ?></textarea>
       
   148 						<?php elseif ( $field['type'] == 'select' ) : ?>
       
   149 							<select id="custom-<?php echo $gig ?>-<?php echo $key ?>" name="custom-value[<?php echo $key ?>]">
       
   150 								<?php foreach ( $field['options'] as $oKey => $oValue ) : ?>
       
   151 									<option value="<?php echo addslashes($oKey) ?>"<?php if ( $oKey == $value ) echo ' selected="selected"' ?>><?php echo $oValue ?></option>
       
   152 								<?php endforeach; ?>
       
   153 							</select>
       
   154 						<?php elseif ( $field['type'] == 'radio' ) : ?>
       
   155 							<?php foreach ( $field['options'] as $oKey => $oValue ) : ?>
       
   156 								<input type="radio" id="custom-<?php echo $gig ?>-<?php echo $key ?>-<?php echo addslashes($oKey) ?>" name="custom-value[<?php echo $key ?>]" value="<?php echo addslashes($oKey) ?>" <?php if ( $value == $oKey ) echo 'checked="checked"' ?> /> 
       
   157 								<label for="custom-<?php echo $gig ?>-<?php echo $key ?>-<?php echo addslashes($oKey) ?>">
       
   158 									<?php echo $oValue ?>
       
   159 								</label>
       
   160 								<br />
       
   161 							<?php endforeach; ?>
       
   162 						<?php elseif ( $field['type'] == 'checkbox' ) : ?>
       
   163 							<?php $values = is_array($value) ? $value : unserialize($value); ?>
       
   164 							<?php foreach ( $field['options'] as $oKey => $oValue ) : ?>
       
   165 								<input type="checkbox" id="custom-<?php echo $gig ?>-<?php echo $key ?>-<?php echo addslashes($oKey) ?>" name="custom-value[<?php echo $key ?>][]" value="<?php echo addslashes($oKey) ?>" <?php if ( in_array($oKey, $values) ) echo 'checked="checked"' ?> /> 
       
   166 								<label for="custom-<?php echo $gig ?>-<?php echo $key ?>-<?php echo addslashes($oKey) ?>">
       
   167 									<?php echo $oValue ?>
       
   168 								</label>
       
   169 								<input type="hidden" name="custom-value[<?php echo $key ?>][]" value="" /> 
       
   170 								<br />
       
   171 							<?php endforeach; ?>
       
   172 						<?php endif; ?>
       
   173 					</td>
       
   174 				</tr>
       
   175 			<?php
       
   176 		}
       
   177 
       
   178 	}
       
   179 	
       
   180 	function get_gig_css_classes($gig) {
       
   181 		if ( is_object($gig) && isset($gig->id) ) {
       
   182 			$gig = $gig->id;
       
   183 		} elseif ( is_array($gig) && isset($gig['id']) ) {
       
   184 			$gig = $gig['id'];
       
   185 		}
       
   186 		
       
   187 		$g = new gig();
       
   188 		if ( !$g->get($gig) ) {
       
   189 			return false;
       
   190 		}
       
   191 		
       
   192 		$classes = array(
       
   193 			'gig',
       
   194 			mysql2date('\yY', $g->date),
       
   195 			mysql2date('\mm', $g->date),
       
   196 			( $g->date >= date('Y-m-d') ? 'upcoming' : 'archive' ),
       
   197 		);
       
   198 		
       
   199 		$classes = array_merge($classes, $g->getTags(true, 'gc-'));
       
   200 		
       
   201 		return implode(' ', apply_filters('gigCal_gigs_css_classes', $classes, $g));
       
   202 	}
       
   203 }
       
   204 
       
   205 class dtc_gigs_baseAR {
       
   206 	var $wpdb, $_rows, $_row, $_new = true, $_table, $_count;
       
   207 	
       
   208 	function dtc_gigs_baseAR($id = null){
       
   209 		global $wpdb;
       
   210 		$this->wpdb = $wpdb;
       
   211 		if ( $id !== null ) {
       
   212 			$this->get($id);
       
   213 		}
       
   214 	}
       
   215 	
       
   216 	function fetch() {
       
   217 		if ( count($this->_rows) ) {
       
   218 			$this->_row = each($this->_rows);
       
   219 			if ( $this->_row ) {
       
   220 				$this->load();
       
   221 				return true;
       
   222 			} else {
       
   223 				return false;
       
   224 			}
       
   225 		} else {
       
   226 			return false;
       
   227 		}
       
   228 	}
       
   229 
       
   230 	function generate_slug() {
       
   231 		if ( isset($this->slug) ) {
       
   232 			$first = true;
       
   233 			$name = $this->_name;
       
   234 			do {
       
   235 				if ( $first ) {
       
   236 					$this->slug = dtcGigs::generateSlug($this->$name);
       
   237 					$first = false;
       
   238 				} else {
       
   239 					ereg('[0-9]*$', $this->slug, $match);
       
   240 					$match = ((int) $match[0]) + 1;
       
   241 					if ( 1 == $match ) $match++;
       
   242 					$this->slug = preg_replace('/[0-9]*$/', '', $this->slug);
       
   243 					$this->slug .= $match;
       
   244 				}
       
   245 				
       
   246 			} while ( $this->wpdb->get_var('SELECT COUNT(*) FROM `' . $this->_table . '` WHERE slug = "' . $this->slug . '"') );
       
   247 		}
       
   248 	}
       
   249 
       
   250 	
       
   251 	function get($id) {
       
   252 		$this->search('`id` = ' . (int) $id);
       
   253 		return $this->fetch();
       
   254 	}
       
   255 	
       
   256 	function getRow() {
       
   257 		$row = array();
       
   258 		foreach ( $this->_fields as $f ) {
       
   259 			$row[$f]  = $this->$f;
       
   260 		}
       
   261 		return $row;
       
   262 	}
       
   263 	
       
   264 	function getTags($slugs = false, $prefix = '') {
       
   265 		if ( function_exists('wp_get_post_tags') ) {
       
   266 			$tags = wp_get_post_tags($this->postID);
       
   267 			foreach ( $tags as $key => $tag ) {
       
   268 				$tags[$key] = $prefix . ($slugs ? $tag->slug : $tag->name);
       
   269 			}
       
   270 			return $tags;
       
   271 		} else {
       
   272 			return array();
       
   273 		}
       
   274 	}
       
   275 	
       
   276 	function getCustom() {
       
   277 		if ( !empty($this->postID) ) {
       
   278 			return get_post_custom($this->postID);
       
   279 		}
       
   280 	}
       
   281 	
       
   282 	function setTags($tags) {
       
   283 		if ( function_exists('wp_set_post_tags') )
       
   284 			wp_set_post_tags($this->postID, $tags, false);
       
   285 	}
       
   286 	
       
   287 
       
   288 
       
   289 
       
   290 	
       
   291 	function load() {
       
   292 		foreach ( $this->_fields as $f ) {
       
   293 			if ( is_null($this->_row['value']->$f) ) {
       
   294 				$this->$f = null;
       
   295 			} else {
       
   296 				$this->$f = stripslashes($this->_row['value']->$f);
       
   297 			}
       
   298 		}
       
   299 	}
       
   300 	
       
   301 	function search($where = null, $order = null) {
       
   302 		$this->_new = false;
       
   303 		$this->_rows = $this->wpdb->get_results('SELECT * FROM `' . $this->_table . '` ' . ($where !== null ? 'WHERE ' . $where : '') . ($order !== null ? ' ORDER BY ' . $order : ''));
       
   304 		$this->_count = count($this->_rows);
       
   305 		return count($this->_rows);
       
   306 	}
       
   307 	
       
   308 	function save($get_after = true) {
       
   309 		$first = true;
       
   310 		$name = $this->_name;
       
   311 		$this->generate_slug();
       
   312 		if ( $this->_new ) {
       
   313 			$fields = $this->_fields;
       
   314 			$values = array();
       
   315 			unset($fields[0]);
       
   316 			
       
   317 			$this->pre_insert();
       
   318 
       
   319 			foreach ( $fields as $f ) {
       
   320 				if ( is_null($this->$f) ) {
       
   321 					$values[$f] = 'null';
       
   322 				} else {
       
   323 					$values[$f] = '"' . $this->wpdb->escape(stripslashes($this->$f)) . '"';
       
   324 				}
       
   325 			}
       
   326 			
       
   327 			if ( !$this->wpdb->query('INSERT INTO `' . $this->_table . '` (`' . implode('`, `', $fields) . '`) VALUES (' . implode(', ', $values) . ')') ) {
       
   328 				return false;
       
   329 			}
       
   330 			$this->id = $this->wpdb->insert_id;
       
   331 
       
   332 			$this->post_insert();
       
   333 			if ( $get_after ) 
       
   334 				$this->get((int) $this->id);
       
   335 
       
   336 			return true;
       
   337 		} else {
       
   338 			$fields = $this->_fields;
       
   339 			$values = array();
       
   340 			unset($fields[0]);
       
   341 			
       
   342 			$this->pre_update();
       
   343 			
       
   344 			foreach ( $fields as $f ) {
       
   345 				if ( is_null($this->$f) ) {
       
   346 					$values[$f] = '`' . $f . '` = null';
       
   347 				} else {
       
   348 					$values[$f] = '`' . $f . '` = "' . $this->wpdb->escape(stripslashes($this->$f)) . '"';
       
   349 				}
       
   350 			}
       
   351 			
       
   352 			$result = (bool) $this->wpdb->query('UPDATE `' . $this->_table . '` SET ' . implode(', ', $values) . ' WHERE `id` = ' . (int) $this->id);
       
   353 			
       
   354 			if ( $result ) $this->post_update();
       
   355 			
       
   356 			if ( $get_after ) 
       
   357 				$this->get((int) $this->id);
       
   358 			
       
   359 			return $result;
       
   360 		}
       
   361 	}
       
   362 	
       
   363 	function pre_insert(){}
       
   364 	function post_insert(){}
       
   365 	function pre_update(){}
       
   366 	function post_update(){}
       
   367 	function extraJSON() {return array();}
       
   368 	
       
   369 	function delete() {
       
   370 		if ( !empty($this->id) ) {
       
   371 			return (bool) $this->wpdb->query('DELETE FROM `' . $this->_table . '` WHERE `id` = ' . (int) $this->id);
       
   372 		}
       
   373 	}
       
   374 	
       
   375 	function toJSON($single = true) {
       
   376 		if ( $single ) {
       
   377 			$fields = array();
       
   378 			if ( function_exists("json_encode") ) {
       
   379 				foreach ( $this->_fields as $f ) {
       
   380 					$fields[$f] = $this->$f;
       
   381 				}
       
   382 				
       
   383 				$extra = $this->extraJSON();
       
   384 				$fields = array_merge($fields, $extra);
       
   385 				return json_encode($fields);
       
   386 			} else {
       
   387 				foreach ( $this->_fields as $f ) {
       
   388 					$fields[] = '"' . $f . '":"' . str_replace("\n", '\n', addslashes($this->$f)) . '"';
       
   389 				}
       
   390 			
       
   391 				$extra = $this->extraJSON();
       
   392 				$fields = array_merge($fields, $extra);
       
   393 
       
   394 				return '{' . implode(', ', $fields) . '}';
       
   395 			}
       
   396 		}
       
   397 	}
       
   398 	
       
   399 	function getError() {
       
   400 		return $this->wpdb->last_error;
       
   401 	}
       
   402 }
       
   403 
       
   404 class venue extends dtc_gigs_baseAR {
       
   405 	var $_table = TABLE_VENUES, $_name = 'name';
       
   406 	var $id, $name, $address, $city, $state, $country, $postalCode, $contact, 
       
   407 		$phone, $email, $link, $notes, $private = 0, $apiID = 0, $deleted = 0, $customMap;
       
   408 	var $_fields = array(
       
   409 		'id',
       
   410 		'name',
       
   411 		'address',
       
   412 		'city',
       
   413 		'state',
       
   414 		'country',
       
   415 		'postalCode',
       
   416 		'contact',
       
   417 		'phone',
       
   418 		'email',
       
   419 		'link',
       
   420 		'notes',
       
   421 		'private',
       
   422 		'apiID',
       
   423 		'deleted',
       
   424 		'customMap',
       
   425 	);
       
   426 	
       
   427 	function delete($forReal = false) {
       
   428 		if ( $forReal ) {
       
   429 			return parent::delete();
       
   430 		} else {
       
   431 			$this->deleted = 1;
       
   432 			return $this->save();
       
   433 
       
   434 		}
       
   435 	}
       
   436 	
       
   437 	function getAddress($oneLine = false) {
       
   438 		$a = array();
       
   439 		$city = "";
       
   440 		if ( $oneLine ) {
       
   441 			if ( !empty($this->address) ) $a[] = str_replace("\n", ', ', $this->address);
       
   442 			if ( !empty($this->city) ) $a[] = $this->city;
       
   443 			if ( !empty($this->state) ) $a[] = $this->state;
       
   444 			if ( !empty($this->postalCode) ) $a[] = $this->postalCode;
       
   445 			if ( !empty($this->country) ) $a[] = $this->country;
       
   446 			return implode(', ', $a);
       
   447 		} else {
       
   448 			if ( !empty($this->address) ) $a[] = $this->address;
       
   449 			if ( !empty($this->city) ) $temp .= $this->city;
       
   450 			if ( !empty($this->state) ) $temp .= (!empty($temp) ? ', ' : '' ) . $this->state;
       
   451 			if ( !empty($this->country) ) $temp .= (!empty($temp) ? ', ' : '' ) . $this->country;
       
   452 			if ( !empty($this->postalCode) ) $temp .= (!empty($temp) ? ' ' : '' ) . $this->postalCode;
       
   453 			if ( !empty($temp) ) $a[] = $temp;
       
   454 			return implode("\n", $a);
       
   455 		}
       
   456 	}
       
   457 	
       
   458 	function getCity() {
       
   459 		$c = array();
       
   460 		if ( !empty($this->city) ) $c[] = $this->city;
       
   461 		if ( !empty($this->state) ) $c[] = $this->state;
       
   462 		//if ( !empty($this->country) ) $c[] = $this->country;
       
   463 		return implode(', ', $c);
       
   464 	}
       
   465 	
       
   466 	function getMapLink() {
       
   467 		global $options;
       
   468 		if ( empty($this->customMap) ) {
       
   469 			$a = array();
       
   470 			
       
   471 			if ( empty($this->address) ) 
       
   472 				$a[] = $this->name . ' near:';
       
   473 			else {}
       
   474 				$a[] = str_replace("\n", ", ", $this->address);
       
   475 			if ( !empty($this->city) ) $a[] = $this->city;
       
   476 			if ( !empty($this->state) ) $a[] = $this->state;
       
   477 			if ( !empty($this->country) ) $a[] = $this->country;
       
   478 			if ( !empty($this->postalCode) ) $a[] = $this->postalCode;
       
   479 			return 'http://maps.google.com/?q=' . urlencode(str_replace('near:,', 'near:', implode(', ', $a)));
       
   480 		} else {
       
   481 			if ( preg_match("/^https?:\/\//", $this->customMap) ) {
       
   482 				return $this->customMap;
       
   483 			} else {
       
   484 				return 'http://maps.google.com/?q=' . urlencode($this->customMap);
       
   485 			}
       
   486 		}
       
   487 	}
       
   488 	
       
   489 	function extraJSON() {
       
   490 		if ( function_exists("json_encode") ) {
       
   491 			$fields['mapLink'] = $this->getMapLink();
       
   492 		} else {
       
   493 			$fields[] = '"mapLink":"' . str_replace("\n", '\n', addslashes($this->getMapLink())) . '"';
       
   494 		}
       
   495 		return $fields;
       
   496 	}
       
   497 }
       
   498 
       
   499 class gig extends dtc_gigs_baseAR {
       
   500 	var $_table = TABLE_GIGS, $_name = 'id';
       
   501 	var $id, $venueID = 0, $date, $notes, $postID = 0, $eventName, $tour_id = 0;
       
   502 	var $_fields = array(
       
   503 		'id',
       
   504 		'venueID',
       
   505 		'date',
       
   506 		'notes',
       
   507 		'postID',
       
   508 		'eventName',
       
   509 		'tour_id',
       
   510 	);
       
   511 
       
   512 	function delete() {
       
   513 		$p = $this->getPerformances();
       
   514 		while ( $p->fetch() ) {
       
   515 			$p->delete();
       
   516 		}
       
   517 		wp_delete_post($this->postID);
       
   518 		return parent::delete();
       
   519 	}
       
   520 	
       
   521 	function getByPostID($postID) {
       
   522 		$this->search("postID = " . (int) $postID);
       
   523 		return $this->fetch();
       
   524 	}
       
   525 	
       
   526 	function getPermalink() {
       
   527 		$post = get_permalink($this->postID);
       
   528 		return $post;
       
   529 	}
       
   530 	
       
   531 	function getPerformances() {
       
   532 		$p = new performance();
       
   533 		if ( $this->id ) {	
       
   534 			$p->search("`gigID` = " . (int) $this->id, "`time`");
       
   535 		}
       
   536 		return $p;
       
   537 	}
       
   538 	
       
   539 	function getVenue() {
       
   540 		$v = new venue();
       
   541 		if ( $this->venueID ) {
       
   542 			$v->get($this->venueID);
       
   543 		}
       
   544 		return $v;
       
   545 	}
       
   546 	
       
   547 	function pre_insert() {
       
   548 		global $options, $wpdb;
       
   549 		$user = wp_get_current_user();
       
   550 		$v = $this->getVenue();
       
   551 		$p = $this->getPerformances();
       
   552 		$p->fetch();
       
   553 		
       
   554 		if ( $this->tour_id == -1 ) $this->tour_id = null;
       
   555 		
       
   556 		$title = array();
       
   557 		foreach ( $options['gig-title-show'] as $f ) {
       
   558 			switch ( $f ) {
       
   559 				case 'city' :
       
   560 					$title[] = $v->getCity();
       
   561 					break;
       
   562 				case 'eventName' :
       
   563 					$title[] = $this->eventName;
       
   564 					break;
       
   565 				case 'venue' :
       
   566 					$title[] = $v->name;
       
   567 					break;
       
   568 				case 'date' :
       
   569 					$title[] = dtcGigs::dateFormat($this->date);
       
   570 					break;
       
   571 				case 'country' :
       
   572 					$title[] = $v->country;
       
   573 					break;
       
   574 			}
       
   575 		}
       
   576 		
       
   577 		foreach ( $title  as $key => $value ) {
       
   578 			if ( empty($value) ) unset($title[$key]);
       
   579 		}
       
   580 
       
   581 		$post_data = array(
       
   582 			'post_author'		=> $user->ID,
       
   583 			'post_title'		=> $wpdb->escape(implode(' - ', $title)),
       
   584 			'post_status'		=> 'publish',
       
   585 			'post_type' 		=> 'post',
       
   586 			'post_category'		=> array($options['category']),
       
   587 			'post_content' 		=> '',
       
   588 		);
       
   589 		
       
   590 		if ( $options['post-filtering'] ) {
       
   591 			$post_data = array_merge($post_data, array(
       
   592 				'post_date'			=> $wpdb->escape($this->date . ' ' . $p->time),
       
   593 				'post_modified'		=> $wpdb->escape($this->date . ' ' . $p->time),
       
   594 				'post_date_gmt'		=> date('Y-m-d H:i:s', strtotime($this->date . ' ' . $p->time) - (60 * 60 * get_option('gmt_offset'))),
       
   595 				'post_modified_gmt'	=> date('Y-m-d H:i:s', strtotime($this->date . ' ' . $p->time) - (60 * 60 * get_option('gmt_offset'))),
       
   596 			));
       
   597 		}
       
   598 		$id = wp_insert_post($post_data);
       
   599 		$this->postID = $id;
       
   600 	}
       
   601 	
       
   602 	function pre_update() {
       
   603 		global $options, $wpdb, $wp_version;
       
   604 		$v = $this->getVenue();
       
   605 		$p = $this->getPerformances();
       
   606 		$p->fetch();
       
   607 
       
   608 		if ( $this->tour_id == -1 ) $this->tour_id = null;
       
   609 		
       
   610 		$title = array();
       
   611 		foreach ( $options['gig-title-show'] as $f ) {
       
   612 			switch ( $f ) {
       
   613 				case 'city' :
       
   614 					$title[] = $v->getCity();
       
   615 					break;
       
   616 				case 'eventName' :
       
   617 					$title[] = $this->eventName;
       
   618 					break;
       
   619 				case 'venue' :
       
   620 					$title[] = $v->name;
       
   621 					break;
       
   622 				case 'date' :
       
   623 					$title[] = dtcGigs::dateFormat($this->date);
       
   624 					break;
       
   625 				case 'country' :
       
   626 					$title[] = $v->country;
       
   627 					break;
       
   628 			}
       
   629 		}
       
   630 		
       
   631 		foreach ( $title  as $key => $value ) {
       
   632 			if ( empty($value) ) unset($title[$key]);
       
   633 		}
       
   634 
       
   635 		
       
   636 		$post = get_post($this->postID);
       
   637 		
       
   638 		if ( $options['post-filtering'] ) {
       
   639 			$post->post_date = $wpdb->escape($this->date . ' ' . $p->time);
       
   640 			$post->post_modified = $wpdb->escape($this->date . ' ' . $p->time);
       
   641 			$post->post_date_gmt = date('Y-m-d H:i:s', strtotime($this->date . ' ' . $p->time) - (60 * 60 * get_option('gmt_offset')));
       
   642 			$post->post_modified_gmt = date('Y-m-d H:i:s', strtotime($this->date . ' ' . $p->time) - (60 * 60 * get_option('gmt_offset')));
       
   643 		}
       
   644 
       
   645 		$post->post_title = $wpdb->escape(implode(' - ', $title));
       
   646 		$post->post_name = "";
       
   647 		$post->post_status = "publish"; // Do this to trick WordPress into resetting the GUID.
       
   648 		$post->post_category = array($options['category']);
       
   649 
       
   650 		wp_update_post($post);
       
   651 		if ( (float) $wp_version < 2.3 ) {  // Hack to make it compatible with WP 2.2
       
   652 			global $wpdb; 
       
   653 			$wpdb->query('UPDATE ' . $wpdb->prefix . 'posts SET post_status = "publish" WHERE ID = ' . (int) $this->postID);
       
   654 		}
       
   655 	}
       
   656 	
       
   657 	function post_insert() {
       
   658 		global $wp_version;
       
   659 		if ( (float) $wp_version < 2.3 ) {  // Hack to make it compatible with WP 2.2
       
   660 			global $wpdb; 
       
   661 			$wpdb->query('UPDATE ' . $wpdb->prefix . 'posts SET post_status = "publish" WHERE ID = ' . (int) $this->postID);
       
   662 		} else {
       
   663 			$post = get_post($this->postID);
       
   664 			$post->post_status = "publish";
       
   665 			wp_update_post($post);
       
   666 		}
       
   667 	}
       
   668 
       
   669 	function extraJSON() {
       
   670 		$post = get_post($this->postID);
       
   671 		$v = $this->getVenue();
       
   672 		$p = $this->getPerformances();
       
   673 		if ( function_exists("json_encode") ) {
       
   674 			$fields['permalink'] = $this->getPermalink();
       
   675 			$fields['venue'] = $v->getRow();
       
   676 			while ( $p->fetch() ) {
       
   677 				$fields['performances'][] = $p->getRow();
       
   678 			}
       
   679 		} else {
       
   680 			$fields[] = '"permalink":"' . str_replace("\n", '\n', addslashes($this->getPermalink())) . '"';
       
   681 			$ps = array();
       
   682 			$fields[] = '"venue":' . $v->toJSON();
       
   683 			while ( $p && $p->fetch() ) {
       
   684 				$ps[] = $p->toJSON();
       
   685 			}
       
   686 			$fields[] = '"performances":[' . implode(', ', $ps) . ']';
       
   687 		}
       
   688 		return $fields;
       
   689 	}
       
   690 }
       
   691 
       
   692 class performance extends dtc_gigs_baseAR {
       
   693 	var $_table = TABLE_PERFORMANCES, $_name = 'id';
       
   694 	var $id, $gigID = 0, $time = '20:00', $link, $shortNotes, $ages;
       
   695 
       
   696 	var $_fields = array(
       
   697 		'id',
       
   698 		'gigID',
       
   699 		'time',
       
   700 		'link',
       
   701 		'shortNotes',
       
   702 		'ages',
       
   703 	);
       
   704 }
       
   705 
       
   706 class tour extends dtc_gigs_baseAR {
       
   707 	var $_table = TABLE_TOURS, $_name = 'id';
       
   708 	var $id, $name, $notes, $pos = 0;
       
   709 	var $_fields = array(
       
   710 		'id',
       
   711 		'name',
       
   712 		'notes',
       
   713 		'pos',
       
   714 	);
       
   715 	
       
   716 	function post_insert() {
       
   717 		$max = $this->wpdb->get_var('SELECT MAX(`pos`) FROM `' . $this->_table . '`') + 1;
       
   718 		$this->wpdb->query('UPDATE `' . $this->_table . '` SET `pos` = ' . $max . ' WHERE `id` = ' . (int) $this->id);
       
   719 	}
       
   720 	
       
   721 	function delete() {
       
   722 		$g = $this->getGigs();
       
   723 		while ( $g->fetch() ) {
       
   724 			$g->tour_id = null;
       
   725 			$g->save();
       
   726 		}
       
   727 		return parent::delete();
       
   728 	}
       
   729 	
       
   730 	function sortTours($type = null) {
       
   731 		$options = get_option('gigs-calendar');
       
   732 		$type = is_null($type) ? $options['tours-sort'] : $type;
       
   733 		switch ($type) {
       
   734 			case 'date' :
       
   735 				return $this->wpdb->get_col('
       
   736 					SELECT DISTINCT
       
   737 						t.id
       
   738 					FROM
       
   739 						' . TABLE_TOURS . ' t  
       
   740 					LEFT JOIN
       
   741 						' . TABLE_GIGS . ' g 
       
   742 					ON 
       
   743 						t.`id` = g.`tour_id`
       
   744 					WHERE
       
   745 						`date` is null OR `date` >= CURDATE()
       
   746 					ORDER BY 
       
   747 						g.`date`, `name`
       
   748 				');
       
   749 				break;
       
   750 			case 'alpha' :
       
   751 				return $this->wpdb->get_col('
       
   752 					SELECT DISTINCT
       
   753 						t.id
       
   754 					FROM
       
   755 						' . TABLE_TOURS . ' t  
       
   756 					ORDER BY 
       
   757 						`name`
       
   758 				');
       
   759 				break;
       
   760 		}
       
   761 	}
       
   762 	
       
   763 	function getGigs($slice = 'all') {
       
   764 		$g = new gig();
       
   765 		if ( $slice == 'past' ) {
       
   766 			$g->search('`date` <= CURDATE() AND `tour_id` = ' . (int) $this->id, '`date`');
       
   767 		} elseif ( $slice == 'future' ) {
       
   768 			$g->search('`date` >= CURDATE() AND `tour_id` = ' . (int) $this->id, '`date`');
       
   769 		} else {
       
   770 			$g->search('`tour_id` = ' . (int) $this->id, '`date`');
       
   771 		}
       
   772 		return $g;
       
   773 	}
       
   774 	
       
   775 	function extraJSON(){
       
   776 		$fields = array();
       
   777 		$g = $this->getGigs();
       
   778 		$gs = array();
       
   779 		if ( function_exists("json_encode") ) {
       
   780 			while ( $g->fetch() ) {
       
   781 				$gs[] = $g->getRow();
       
   782 			}
       
   783 			$fields['gigs'] = $gs;
       
   784 		} else {
       
   785 			while ( $g->fetch() ) {
       
   786 				$gs[] = $g->toJSON();
       
   787 			}
       
   788 			$fields[] = '"gigs":[' . implode(',', $gs) . ']';
       
   789 		}
       
   790 		return $fields;
       
   791 	}
       
   792 }
       
   793 
       
   794 ?>