web/wp-content/plugins/gigs-calendar/gigs.ajax.php
branchwordpress
changeset 112 fb7cd02b9848
parent 111 6b96085291d7
child 123 561aa6d282f6
equal deleted inserted replaced
111:6b96085291d7 112:fb7cd02b9848
     1 <?php
       
     2 
       
     3 require_once 'ajaxSetup.php';
       
     4 
       
     5 $pageTarget = $folder . 'gigs.ajax.php';
       
     6 switch ($_POST['action']) {
       
     7 	case 'load':
       
     8 		$v = new venue();
       
     9 		$g = new gig();
       
    10 		$p = new performance();
       
    11 		$g->search("date >= CURDATE()", 'date');
       
    12 		$v->search('deleted = 0', 'name');
       
    13 		?>
       
    14 			<div class="clickable" id="gig-add-trigger">
       
    15 				<img class="icon" src="<?php echo $folder; ?>images/add.png" /> <?php _e('Add a new gig', $gcd) ?>
       
    16 			</div>
       
    17 			<div id="gig-add-form" style="display: none;">
       
    18 				<form id="new-gig" class="new-item" method="post" action="<?php echo $pageTarget; ?>">
       
    19 					<table>
       
    20 						<tbody>
       
    21 							<tr><td colspan="2">
       
    22 								<h3 class="no-margin"><?php _e('Gig Information', $gcd) ?></h3>
       
    23 								<div class="instructions"><?php _e('A "gig" is unique to a date at a venue.', $gcd) ?></div>
       
    24 							</td></tr>
       
    25 							<tr class="venue">
       
    26 								<td><label for="new-venue"><?php _e('Venue:', $gcd) ?></label></td>
       
    27 								<td>
       
    28 									<?php if ( $v->_count ) : ?>
       
    29 										<select name="venueID" id="new-venue">
       
    30 											<option value="-1"><?php _e('Select a venue', $gcd) ?></option>
       
    31 											<?php while ( $v->fetch() ) : ?>
       
    32 												<option value="<?php echo $v->id ?>"><?php echo $v->name . ' - ' . $v->getCity() ?></option>
       
    33 											<?php endwhile; ?>
       
    34 										</select>
       
    35 									<?php else : ?>
       
    36 											<?php _e('No venues found.  You&rsquo;ll need to <span class="link" id="add-venue">add one</span>', $gcd) ?>.
       
    37 									<?php endif; ?>
       
    38 								</td>
       
    39 							</tr>
       
    40 							<tr class="date">
       
    41 								<td><label for="new-date"><?php _e('Date:', $gcd) ?></label></td>
       
    42 								<td><input type="text" class="date" name="date" id="new-date" /></td>
       
    43 							</tr>
       
    44 							<tr class="eventName">
       
    45 								<td><label for="new-eventName"><?php _e('Event Name:', $gcd) ?></label> <img class="inline-icon" alt="" src="<?php echo $folder; ?>images/help.png" title="<?php _e('Some artists perform under several names.  Ignore the event name field if you don&rsquo;t need to specify what this event is called or what band name you are playing under.', $gcd) ?>" /></td>
       
    46 								<td><input type="text" class="eventName wide" name="eventName" id="new-eventName" /></td>
       
    47 							</tr>
       
    48 							<tr class="tour">
       
    49 								<td><label for="new-tour"><?php _e('Tour:', $gcd) ?></label></td>
       
    50 								<td>
       
    51 									<select class="tour_id" name="tour_id" id="new-tour_id">
       
    52 										<option value="-1"><?php _e("--None--", $gcd); ?></option>
       
    53 										<?php $t = new tour(); $t->search(null, '`name`'); ?>
       
    54 										<?php while ( $t->fetch() ) : ?>
       
    55 											<option value="<?php echo $t->id; ?>"><?php echo $t->name; ?></option>
       
    56 										<?php endwhile; ?>
       
    57 									</select>
       
    58 								</td>
       
    59 							</tr>
       
    60 							<?php if ( function_exists('wp_set_post_tags') ) : ?>
       
    61 								<tr class="tags">
       
    62 									<td><label for="new-tags"><?php _e('Post Tags:', $gcd) ?></label></td>
       
    63 									<td><input type="text" class="tags wide" name="tags" id="new-tags" /></td>
       
    64 								</tr>
       
    65 							<?php endif; ?>
       
    66 
       
    67 
       
    68 							<tr class="notes"><td colspan="2">
       
    69 								<?php _e('Description/Other notes:', $gcd) ?><br />
       
    70 								<textarea class="notes" name="notes" rows="8" cols="80"></textarea>
       
    71 							</td></tr>
       
    72 							
       
    73 							<?php if ( $wp_version >= 2.5 ) : ?>
       
    74 								<?php 
       
    75 									$metadata = array();
       
    76 									dtcGigs::extraFields(apply_filters('gigCal_gigs_extraFields', array()), $metadata);
       
    77 								?>
       
    78 								<tr><td colspan="2">
       
    79 									<div id="postcustom-new" class="postcustom">
       
    80 										<h3 title="<?php _e('Click to expand', $gcd); ?>" class="clickable"><?php _e('Custom Fields') ?> <img class="inline-icon" src="<?php echo $folder ?>images/plus.gif" alt="<?php _e('Expand', $gcd); ?>"> <img class="inline-icon" style="display:none;" src="<?php echo $folder ?>images/minus.gif" alt="<?php _e('Collapse', $gcd); ?>"></h3>
       
    81 										<div class="inside">
       
    82 											<div id="postcustomstuff-new">
       
    83 											<table cellpadding="3">
       
    84 												<thead>
       
    85 													<tr>
       
    86 														<th><?php _e('Key', $gcd) ?></th>
       
    87 														<th><?php _e('Value', $gcd) ?></th>
       
    88 														<th></th>
       
    89 													</tr>
       
    90 												</thead>
       
    91 												<tbody>
       
    92 													<tr>
       
    93 														<td><input value="<?php echo addslashes($key) ?>" name="custom-key[]" /></td>
       
    94 														<td>
       
    95 															<textarea rows="1" name="custom-value[]"><?php echo htmlspecialchars($value) ?></textarea>
       
    96 														</td>
       
    97 														<td><img alt="<?php _e('Remove Custom Field', $gcd) ?>" title="<?php _e('Remove Custom Field', $gcd) ?>" class="clickable delete-custom" src="<?php echo $folder; ?>images/delete.png" /></td>
       
    98 													</tr>
       
    99 												</tbody>
       
   100 											</table>
       
   101 											<div class="custom-ajax-response"></div>
       
   102 											</div>
       
   103 											<p>
       
   104 												<span class="clickable add-custom-field">
       
   105 													<img class="inline-icon" src="<?php echo $folder ?>images/add.png" alt="" />
       
   106 													<?php _e('Add a new custom field'); ?>
       
   107 												</span>
       
   108 											</p>
       
   109 											<p><?php _e('Custom fields can be used to add extra metadata to a gig that you can use in a special template.'); ?></p>
       
   110 										</div>
       
   111 									</div>
       
   112 								</td></tr>
       
   113 							<?php endif; ?>
       
   114 							
       
   115 							<tr><td colspan="2">
       
   116 								<h3 class="no-margin"><?php _e('Performances', $gcd) ?></h3>
       
   117 								<div class="instructions"><?php _e('You can add as many performances as<br />you are giving at a venue on the given date.', $gcd) ?></div>
       
   118 							</td></tr>
       
   119 							<tr><td colspan="2">
       
   120 								<?php pForm(1, true, false) ?>
       
   121 							</td></tr>
       
   122 							<tr><td colspan="2">
       
   123 								<div class="clickable add-performance"><img class="icon" src="<?php echo $folder; ?>images/add.png" /> <?php _e('Add another performance', $gcd) ?></div>
       
   124 							</td></tr>
       
   125 
       
   126 						</tbody>
       
   127 					</table>
       
   128 					<div>
       
   129 						<input type="submit" class="button" name="" value="<?php _e('Add Gig', $gcd) ?>" />
       
   130 						<input type="reset" class="button cancel" name="" value="<?php _e('Cancel', $gcd) ?>" id="new-gig-reset" />
       
   131 						<input type="hidden" name="action" value="add" />
       
   132 						<input type="hidden" name="nonce" value="<?php echo $_POST['nonce']; ?>" />
       
   133 					</div>
       
   134 				</form>
       
   135 			</div>
       
   136 			
       
   137 			<table id="gig-list" class="gigs widefat">
       
   138 				<thead>
       
   139 					<tr>
       
   140 						<th style="text-align: center;" scope="col"><?php _e('ID', $gcd) ?></th>
       
   141 						<th scope="col"><?php _e('Event Name', $gcd) ?></th>
       
   142 						<th scope="col"><?php _e('Venue', $gcd) ?></th>
       
   143 						<th scope="col"><?php _e('City', $gcd) ?></th>
       
   144 						<th scope="col"><?php _e('Date', $gcd) ?></th>
       
   145 						<th scope="col"><?php _e('Time', $gcd) ?></th>
       
   146 						<th style="text-align: center" scope="col"><?php _e('Actions', $gcd) ?></th>
       
   147 					</tr>
       
   148 				</thead>
       
   149 				<tbody>
       
   150 					<?php while ( $g->fetch() ) : $v = $g->getVenue(); $p = $g->getPerformances(); $post = get_post($g->postID);?>
       
   151 						<tr id="gig-<?php echo $g->id; ?>" class="gig gig-<?php echo $g->id; ?> <?php echo ++$count % 2 ? "alternate" : "";?>">
       
   152 							<th style="text-align: center;" scope="row"><?php echo $g->id; ?></th>
       
   153 							<td class="eventName"><?php echo $g->eventName; ?></td>
       
   154 							<td class="venue"><?php echo $v->name; ?></td>
       
   155 							<td class="city"><?php echo $v->city . (!empty($v->state) ? ', ' . $v->state :  '') ?></td>
       
   156 							<td class="date"><?php echo $g->date ?></td>
       
   157 							<td class="time">
       
   158 								<?php if ( $p ) : while ( $p->fetch() ) : ?>
       
   159 									<span class="time"><?php echo dtcGigs::timeFormat($p->time) ?></span>
       
   160 								<?php endwhile; endif; ?>
       
   161 							</td>
       
   162 							<td class="actions" style="text-align: center; position: relative;">
       
   163 								<div style="position: relative">
       
   164 									<a target="_blank" class="guid" href="<?php echo $g->getPermalink(); ?>"><img alt="<?php _e('View Gig', $gcd) ?>" title="<?php _e('View Gig', $gcd) ?>" class="clickable view" src="<?php echo $folder; ?>images/page_white_magnify.png" /></a>
       
   165 									<img alt="<?php _e('Edit', $gcd) ?>" title="<?php _e('Edit', $gcd) ?>" class="clickable edit" src="<?php echo $folder; ?>images/page_white_edit.png" />
       
   166 									<?php if ( $v ) : ?>
       
   167 										<a target="_blank" href="<?php echo $v->getMapLink(); ?>"><img alt="<?php _e('Map', $gcd) ?>" title="<?php _e('Map', $gcd) ?>" class="clickable map" src="<?php echo $folder; ?>images/world.png" /></a>
       
   168 									<?php endif ?>
       
   169 									<a target="_blank" href="<?php echo get_option('siteurl') ?>/wp-admin/post.php?action=edit&post=<?php echo $post->ID; ?>">
       
   170 										<img alt="<?php _e('Edit WordPress Post', $gcd) ?>" title="<?php _e('Edit WordPress Post', $gcd) ?>" class="clickable edit-post" src="<?php echo $folder; ?>images/page_edit.png" />
       
   171 									</a>
       
   172 									<img alt="<?php _e('Delete', $gcd) ?>" title="<?php _e('Delete', $gcd) ?>" class="clickable delete" src="<?php echo $folder; ?>images/delete.png" />
       
   173 									
       
   174 								</div>
       
   175 							</td>
       
   176 						</tr>
       
   177 						<tr id="gig-panel-<?php echo $g->id; ?>" class="gig gig-<?php echo $g->id; ?> panel <?php echo $count % 2 ? "alternate" : "";?>">
       
   178 							<td style="background-color: white"></td>
       
   179 							<td class="panel" colspan="6"></td>
       
   180 						</tr>
       
   181 					<?php endwhile; ?>
       
   182 				</tbody>
       
   183 			</table>
       
   184 				
       
   185 			<script type="text/javascript">
       
   186 			
       
   187 				(function($){
       
   188 					(function(form) { // Function to let a plugin add extra JS code to process the form.
       
   189 						<?php do_action('gigCal_gigs_formExtraJS'); ?>
       
   190 					}($("form#new-gig")));
       
   191 					
       
   192 					$("#gig-add-form .add-custom-field").click(function(){
       
   193 						$("#gig-add-form .postcustom table").append('<tr><td><input value="" name="custom-key[]" /></td><td><textarea rows="1" name="custom-value[]"></textarea><textarea style="display:none;" rows="1" name="old-custom-value[]"></textarea><input class="delete" type="hidden" name="deletecustom[]" value="0" /></td><td><img class="delete-custom" alt="<?php _e('Remove Custom Field', $gcd) ?>" title="<?php _e('Remove Custom Field', $gcd) ?>" class="clickable" src="<?php echo $folder; ?>images/delete.png" /></td></tr>');
       
   194 						
       
   195 						$("#gig-add-form .delete-custom").unbind("click");
       
   196 						$("#gig-add-form .delete-custom").click(function(){
       
   197 							$(this).parents("tr").eq(0).remove();
       
   198 						});
       
   199 					});
       
   200 					
       
   201 					$("#gig-add-form .delete-custom").click(function(){
       
   202 						$(this).parents("tr").eq(0).remove();
       
   203 					});
       
   204 					
       
   205 					$("#postcustom-new h3.clickable").click(function(){
       
   206 						$(this).next().toggle();
       
   207 						$(this).children(".inline-icon").toggle();
       
   208 					});
       
   209 					
       
   210 					
       
   211 				
       
   212 					$("#add-venue").click(function(){
       
   213 						gigs_page_load("venues", "new=1");
       
   214 					});
       
   215 					
       
   216 					$("#gig-add-trigger").click(function(){
       
   217 						$("#gig-add-form:hidden").slideDown(300, function(){
       
   218 							$("#new-name").focus();
       
   219 						});
       
   220 					});
       
   221 					
       
   222 					$("#new-gig-reset").click(function(){
       
   223 						$("#performance-c1 select").attr('disabled', false);
       
   224 						$("#gig-add-form table.performance[id!=performance-c1]").parents("tr").remove();
       
   225 						$("#gig-add-form").slideUp(300);
       
   226 					});
       
   227 					
       
   228 					$("#new-date").datepicker({dateFormat:"yy-mm-dd"});
       
   229 					$(".date-field").datepicker({dateFormat:"yy-mm-dd"});
       
   230 					
       
   231 					
       
   232 					$("#new-gig").ajaxForm({
       
   233 						url:pageTarget,
       
   234 						dataType: "json",
       
   235 						success:function(json){
       
   236 							if ( json.success ) {
       
   237 								gigs = $("table#gig-list tbody tr:not(.panel) td.date");
       
   238 								inserted = false;
       
   239 								for ( i = 0; i < gigs.length; i++ ) {
       
   240 									if ( json.gig.date < gigs.eq(i).html() ) {
       
   241 										g = gigs.eq(i);
       
   242 										$.post(pageTarget, {
       
   243 											nonce:nonce,
       
   244 											action:'getRow',
       
   245 											id:json.gig.id
       
   246 										}, function(rsp){
       
   247 											g.parents("tr").before(rsp);
       
   248 											resetTableColors("table#gig-list");
       
   249 											setupEvents();
       
   250 										});
       
   251 										inserted = true;
       
   252 										break;
       
   253 									}
       
   254 								}
       
   255 								if ( !inserted ) {
       
   256 									$.post(pageTarget, {
       
   257 										nonce:nonce,
       
   258 										action:'getRow',
       
   259 										id:json.gig.id
       
   260 									}, function(rsp){
       
   261 										$("table#gig-list tbody").append(rsp);
       
   262 										resetTableColors("table#gig-list");
       
   263 										setupEvents();
       
   264 									});
       
   265 								}
       
   266 								$("div#postcustomstuff-new table tbody tr:not(:first)").remove();
       
   267 								$("#new-gig-reset").click();
       
   268 							} else {
       
   269 								alert(json.error);
       
   270 							}
       
   271 						}
       
   272 
       
   273 					});
       
   274 					
       
   275 					$('.inline-icon').tooltip({
       
   276 						showURL:false
       
   277 					});
       
   278 					
       
   279 					
       
   280 					setupPerformanceRemoval = function() {
       
   281 						$("div.delete-performance img").unbind("click");
       
   282 						$("div.delete-performance img").click(function(){
       
   283 							id = $(this).parents("tr").eq(0).find("input.performanceID").val();
       
   284 							if ( id == "" ) {
       
   285 								$(this).parents("tr").eq(1).remove();
       
   286 							} else {
       
   287 								if ( confirm("<?php _e('Are you sure you want to remove this performance?', $gcd) ?>") ) {
       
   288 									$(this).parents("form").children(".extra-inputs").append('<input type="hidden" name="delete[]" value="' + id + '" />');
       
   289 									$(this).parents("tr").eq(1).remove();
       
   290 								}
       
   291 							}
       
   292 						});
       
   293 					}
       
   294 					
       
   295 					setupPerformances = function() {
       
   296 						$(".add-performance").unbind("click");
       
   297 						$(".add-performance").click(function(){
       
   298 							td = $(this).parents("tr").eq(0).before("<tr><td colspan='2'><div class='loading'><img src='<?php echo $folder; ?>images/ajax-loader.gif' alt='' /></div></td></tr>").prev().children("td").load(pageTarget,{
       
   299 								nonce:nonce,
       
   300 								action:"performance-form",
       
   301 								count:$(this).parents("table").eq(0).find("table.performance").length + 1
       
   302 							}, setupPerformanceRemoval);
       
   303 						});
       
   304 						setupPerformanceRemoval();
       
   305 
       
   306 					}
       
   307 					
       
   308 					setupEvents = function() {
       
   309 						$("img.delete").unbind("click");
       
   310 						$("img.delete").click(function(){
       
   311 							id = $(this).parents("tr").attr("id").split("-")[1];
       
   312 							var thisRow = $(this).parents("tr").hide();
       
   313 							thisRow.next().hide();
       
   314 
       
   315 							$.post(pageTarget, {
       
   316 								nonce:nonce,
       
   317 								action:'delete',
       
   318 								id:id
       
   319 							}, function (rsp){
       
   320 								if ( rsp.success ) {
       
   321 									thisRow.remove();
       
   322 									thisRow.next().remove();
       
   323 								} else {
       
   324 									thisRow.css("display", "table-row");
       
   325 								}
       
   326 								resetTableColors("table#gig-list");
       
   327 							}, "json");
       
   328 						});
       
   329 						
       
   330 						$("img.edit").unbind("click");
       
   331 						$("img.edit").click(function(){
       
   332 							row = $(this).parents("tr");
       
   333 							id = row.attr("id").split("-")[1];
       
   334 							row.next().children("td.panel").load(pageTarget, {
       
   335 								nonce:nonce,
       
   336 								action:"edit",
       
   337 								id:id
       
   338 							}, function(){
       
   339 								row.next().css("display", "table-row");
       
   340 							});
       
   341 						});
       
   342 						
       
   343 						$("postcustom h3.clickable").unbind("click");
       
   344 						$("postcustom h3.clickable").click(function(){
       
   345 							$(this).next().toggle();
       
   346 						});
       
   347 						
       
   348 						
       
   349 						setupPerformances();
       
   350 					}
       
   351 					
       
   352 					setupEvents();
       
   353 
       
   354 				}(jQuery));
       
   355 				
       
   356 			
       
   357 			
       
   358 			</script>
       
   359 		<?php
       
   360 		break;
       
   361 	case 'add':
       
   362 		$g = new gig();
       
   363 		//$p = new performance();
       
   364 		
       
   365 		$g->venueID = $_POST['venueID'];
       
   366 		$g->date = $_POST['date'];
       
   367 		$g->notes = $_POST['notes'];
       
   368 		$g->eventName = $_POST['eventName'];
       
   369 		$g->tour_id = ( $_POST['tour_id'] == -1 ? null : $_POST['tour_id'] );
       
   370 		
       
   371 		if ( $g->save() ) {
       
   372 			$g->setTags($_POST['tags']);
       
   373 			if ( isset($_POST['performanceID']) ) {
       
   374 				foreach ( $_POST['performanceID'] as $key => $pid ) {
       
   375 					$p = new performance((empty($pid) ? null : $pid));
       
   376 					$p->gigID = $g->id;
       
   377 					$p->link = ($_POST['link'][$key] == 'http://') ? '' : $_POST['link'][$key];
       
   378 					$p->shortNotes = $_POST['shortNotes'][$key];
       
   379 					$p->ages = (empty($_POST['ages-custom'][$key]) ? $_POST['ages'][$key] : $_POST['ages-custom'][$key]);
       
   380 					$p->time = ($_POST['tbd'][$key] == "1") ? null : date("H:i:s", strtotime($_POST['hour'][$key] . ':' . $_POST['minute'][$key] . ' ' . $_POST['meridiem'][$key]));
       
   381 					$p->save();
       
   382 				}
       
   383 			}
       
   384 			
       
   385 			foreach ( $_POST['custom-key'] as $key => $field ) {
       
   386 				if ( $_POST['custom-value'][$key] === "" ) {
       
   387 					// If the value is empty, just ignore this field.
       
   388 					continue;
       
   389 				} else {
       
   390 					if ( is_array($_POST['custom-value'][$key]) ) {
       
   391 						foreach ( $_POST['custom-value'][$key] as $k => $v ) {
       
   392 							if ( $v === "" ) {
       
   393 								unset($_POST['custom-value'][$key][$k]);
       
   394 							}
       
   395 						}
       
   396 					}
       
   397 					add_post_meta($g->postID, $_POST['custom-key'][$key], $_POST['custom-value'][$key]);
       
   398 				}
       
   399 			}
       
   400 			
       
   401 			
       
   402 			echo '{success:true, gig:' . $g->toJSON() . '}';
       
   403 		} else {
       
   404 			echo '{success:false,error:"' . ($g->getError() == '' ? 'An unknown error has occurred' : 'DB Error: ' . addslashes($g->getError())) . '"}';
       
   405 		}
       
   406 		break;
       
   407 	case 'getRow':
       
   408 		$g = new gig($_POST['id']);
       
   409 		$v = $g->getVenue();
       
   410 		$p = $g->getPerformances();
       
   411 		$post = get_post($g->postID);
       
   412 		?>
       
   413 			<tr id="gig-<?php echo $g->id; ?>" class="gig gig-<?php echo $g->id; ?> <?php echo ++$count % 2 ? "alternate" : "";?>">
       
   414 				<th style="text-align: center;" scope="row"><?php echo $g->id; ?></th>
       
   415 				<td class="eventName"><?php echo $g->eventName; ?></td>
       
   416 				<td class="venue"><?php echo $v->name; ?></td>
       
   417 				<td class="city"><?php echo $v->city . (!empty($v->state) ? ', ' . $v->state :  '') ?></td>
       
   418 				<td class="date"><?php echo $g->date ?></td>
       
   419 				<td class="time">
       
   420 					<?php if ( $p ) : while ( $p->fetch() ) : ?>
       
   421 						<span class="time"><?php echo dtcGigs::timeFormat($p->time) ?></span>
       
   422 					<?php endwhile; endif; ?>
       
   423 				</td>
       
   424 				<td class="actions" style="text-align: center; position: relative;">
       
   425 					<div style="position: relative">
       
   426 						<a target="_blank" class="guid" href="<?php echo $g->getPermalink(); ?>"><img alt="<?php _e('View Gig', $gcd) ?>" title="<?php _e('View Gig', $gcd) ?>" class="clickable view" src="<?php echo $folder; ?>images/page_white_magnify.png" /></a>
       
   427 						<img alt="<?php _e('Edit', $gcd) ?>" title="<?php _e('Edit', $gcd) ?>" class="clickable edit" src="<?php echo $folder; ?>images/page_white_edit.png" />
       
   428 						<?php if ( $v ) : ?>
       
   429 							<a target="_blank" href="<?php echo $v->getMapLink(); ?>"><img alt="<?php _e('Map', $gcd) ?>" title="<?php _e('Map', $gcd) ?>" class="clickable map" src="<?php echo $folder; ?>images/world.png" /></a>
       
   430 						<?php endif ?>
       
   431 						<a target="_blank" href="<?php echo get_option('siteurl') ?>/wp-admin/post.php?action=edit&post=<?php echo $post->ID; ?>">
       
   432 							<img alt="<?php _e('Edit WordPress Post', $gcd) ?>" title="<?php _e('Edit WordPress Post', $gcd) ?>" class="clickable edit-post" src="<?php echo $folder; ?>images/page_edit.png" />
       
   433 						</a>
       
   434 						<img alt="<?php _e('Delete', $gcd) ?>" title="<?php _e('Delete', $gcd) ?>" class="clickable delete" src="<?php echo $folder; ?>images/delete.png" />
       
   435 					</div>
       
   436 				</td>
       
   437 			</tr>
       
   438 			<tr id="gig-panel-<?php echo $g->id; ?>" class="gig gig-<?php echo $g->id; ?> panel <?php echo $count % 2 ? "alternate" : "";?>">
       
   439 				<td style="background-color: white"></td>
       
   440 				<td class="panel" colspan="6"></td>
       
   441 			</tr>
       
   442 		<?php
       
   443 		break;
       
   444 	
       
   445 	case 'delete':
       
   446 		$g = new gig($_POST['id']);
       
   447 		$result = $g->delete();
       
   448 		echo '{"success": ' . ($result ? 'true' : 'false') . ',"action":"delete"' . ($result ? '' : ',"error":"db"') . '}';
       
   449 		break;
       
   450 	case 'edit':
       
   451 		$g = new gig($_POST['id']);
       
   452 		$p = $g->getPerformances();
       
   453 		$v = $g->getVenue();
       
   454 		?>
       
   455 			<form id="edit-gig-<?php echo $g->id ?>" class="edit-item" method="post" action="<?php echo $pageTarget; ?>">
       
   456 				<table>
       
   457 					<tbody>
       
   458 						<tr><td colspan="2">
       
   459 							<h3 class="no-margin"><?php _e('Gig Information', $gcd) ?></h3>
       
   460 							<div class="instructions"><?php _e('A "gig" is unique to a date at a venue.', $gcd) ?></div>
       
   461 						</td></tr>
       
   462 						<tr class="venue">
       
   463 							<td><?php _e('Venue:', $gcd) ?></td>
       
   464 							<td>
       
   465 								<?php echo $v->name . ' - ' . $v->getCity() ?>
       
   466 							</td>
       
   467 						</tr>
       
   468 						<tr class="date">
       
   469 							<td><label for="edit-date-<?php echo $g->id ?>"><?php _e('Date:', $gcd) ?></label></td>
       
   470 							<td><input type="text" class="date wide" name="date" id="edit-date-<?php echo $g->id ?>" value="<?php echo $g->date ?>" /></td>
       
   471 						</tr>
       
   472 						<tr class="eventName">
       
   473 							<td><label for="new-eventName"><?php _e('Event Name:', $gcd) ?></label> <img class="inline-icon" alt="" src="<?php echo $folder; ?>images/help.png" title="<?php _e('Some artists perform under several names.  Ignore the event name field if you don&rsquo;t need to specify what this event is called or what band name you are playing under.', $gcd) ?>" /></td>
       
   474 							<td><input type="text" class="eventName wide" name="eventName" id="edit-eventName-<?php echo $g->id ?>" value="<?php echo $g->eventName ?>" /></td>
       
   475 						</tr>
       
   476 						<tr class="tour">
       
   477 							<td><label for="edit-tour_id-<?php echo $g->id ?>"><?php _e('Tour:', $gcd) ?></label></td>
       
   478 							<td>
       
   479 								<select class="tour_id" name="tour_id" id="edit-tour_id-<?php echo $g->id ?>">
       
   480 									<option value="-1"><?php _e('--None--', $gcd) ?></option>
       
   481 									<?php $t = new tour(); $t->search(null, '`name`'); ?>
       
   482 									<?php while ( $t->fetch() ) : ?>
       
   483 										<option value="<?php echo $t->id; ?>" <?php if ( $g->tour_id == $t->id ) echo 'selected="selected"'; ?>><?php echo $t->name; ?></option>
       
   484 									<?php endwhile; ?>
       
   485 								</select>
       
   486 							</td>
       
   487 						</tr>
       
   488 						<?php if ( function_exists('wp_set_post_tags') ) : ?>
       
   489 							<tr class="tags">
       
   490 								<td><label for="edit-tags-<?php echo $g->id ?>"><?php _e('Post Tags:', $gcd) ?></label></td>
       
   491 								<td><input type="text" class="tags wide" name="tags" id="edit-tags-<?php echo $g->id ?>" value="<?php echo dtcGigs::escapeForInput(implode(', ', $g->getTags())) ?>" /></td>
       
   492 							</tr>
       
   493 						<?php endif; ?>
       
   494 						
       
   495 						<tr class="notes"><td colspan="2">
       
   496 							<?php _e('Description/Other notes:', $gcd) ?><br />
       
   497 							<textarea class="notes" name="notes" rows="8" cols="80"><?php echo dtcGigs::escapeForInput($g->notes) ?></textarea>
       
   498 						</td></tr>
       
   499 						
       
   500 						<?php if ( $wp_version >= 2.5 ) : ?>
       
   501 							<?php 
       
   502 								$metadata = get_post_custom($g->postID);
       
   503 								dtcGigs::extraFields(apply_filters('gigCal_gigs_extraFields', array()), $metadata, $g->id);
       
   504 							?>
       
   505 							<tr><td colspan="2">
       
   506 								<div id="postcustom-<?php echo $g->id ?>" class="postcustom">
       
   507 									<h3 title="<?php _e('Click to expand', $gcd); ?>" class="clickable"><?php _e('Custom Fields') ?> <img class="inline-icon" src="<?php echo $folder ?>images/plus.gif" alt="<?php _e('Expand', $gcd); ?>"> <img class="inline-icon" style="display:none;" src="<?php echo $folder ?>images/minus.gif" alt="<?php _e('Collapse', $gcd); ?>"></h3>
       
   508 									<div class="inside">
       
   509 										<div id="postcustomstuff-<?php echo $g->id ?>">
       
   510 										<table cellpadding="3">
       
   511 											<thead>
       
   512 												<tr>
       
   513 													<th><?php _e('Key', $gcd) ?></th>
       
   514 													<th><?php _e('Value', $gcd) ?></th>
       
   515 													<th></th>
       
   516 												</tr>
       
   517 											</thead>
       
   518 											<tbody>
       
   519 												<?php
       
   520 													
       
   521 													foreach ( $metadata as $key => $values ) {
       
   522 														foreach ( $values as $index => $value ) {
       
   523 															if ( substr($key, 0, 1) != '_' ) {
       
   524 																?><tr>
       
   525 																	<td><input value="<?php echo addslashes($key) ?>" name="custom-key[]" /></td>
       
   526 																	<td>
       
   527 																		<textarea rows="1" name="custom-value[]"><?php echo htmlspecialchars($value) ?></textarea>
       
   528 																		<textarea style="display:none;" rows="1" name="old-custom-value[]"><?php echo htmlspecialchars($value) ?></textarea>
       
   529 																		<input class="delete" type="hidden" name="deletecustom[]" value="0" />
       
   530 																	</td>
       
   531 																	<td><img alt="<?php _e('Remove Custom Field', $gcd) ?>" title="<?php _e('Remove Custom Field', $gcd) ?>" class="clickable delete-custom" src="<?php echo $folder; ?>images/delete.png" /></td>
       
   532 																</tr><?php
       
   533 															}
       
   534 														}
       
   535 													}
       
   536 												?>
       
   537 											</tbody>
       
   538 										</table>
       
   539 										<div class="custom-ajax-response"></div>
       
   540 										</div>
       
   541 										<p>
       
   542 											<span class="clickable add-custom-field">
       
   543 												<img class="inline-icon" src="<?php echo $folder ?>images/add.png" alt="" />
       
   544 												<?php _e('Add a new custom field'); ?>
       
   545 											</span>
       
   546 										</p>
       
   547 										<p><?php _e('Custom fields can be used to add extra metadata to a gig that you can use in a special template.'); ?></p>
       
   548 									</div>
       
   549 								</div>
       
   550 							</td></tr>
       
   551 						<?php endif; ?>
       
   552 
       
   553 						<tr><td colspan="2">
       
   554 							<h3 class="no-margin"><?php _e('Performances', $gcd) ?></h3>
       
   555 							<div class="instructions"><?php _e('You can add as many performances as<br />you are giving at a venue on the given date.', $gcd) ?></div>
       
   556 						</td></tr>
       
   557 						<?php while ( $p->fetch() ) : ?>
       
   558 							<tr><td colspan="2">
       
   559 								<?php pForm($p->id, false) ?>
       
   560 							</td></tr>
       
   561 						<?php endwhile ?>
       
   562 						<tr><td colspan="2">
       
   563 							<div class="clickable add-performance"><img class="icon" src="<?php echo $folder; ?>images/add.png" /> <?php _e('Add another performance', $gcd) ?></div>
       
   564 						</td></tr>
       
   565 
       
   566 					</tbody>
       
   567 				</table>
       
   568 				<div class="extra-inputs">
       
   569 					<input type="submit" class="button" name="" value="<?php _e('Save Gig', $gcd); ?>" />
       
   570 					<input type="reset" class="button cancel" name="" value="<?php _e('Cancel', $gcd); ?>" id="edit-gig-reset-<?php echo $g->id ?>" />
       
   571 					<input type="hidden" name="action" value="save" />
       
   572 					<input type="hidden" name="id" value="<?php echo $g->id ?>" />
       
   573 					<input type="hidden" name="nonce" value="<?php echo $_POST['nonce']; ?>" />
       
   574 				</div>
       
   575 			</form>
       
   576 			
       
   577 			<script type="text/javascript">
       
   578 				(function($){					
       
   579 
       
   580 					(function(form) { // Function to let a plugin add extra JS code to process the form.
       
   581 						<?php do_action('gigCal_gigs_formExtraJS'); ?>
       
   582 					}($("#edit-gig-<?php echo $g->id ?>")));
       
   583 
       
   584 					$("#edit-gig-<?php echo $g->id ?> .add-custom-field").click(function(){
       
   585 						$("#edit-gig-<?php echo $g->id ?> .postcustom table").append('<tr><td><input value="" name="custom-key[]" /></td><td><textarea rows="1" name="custom-value[]"></textarea><textarea style="display:none;" rows="1" name="old-custom-value[]"></textarea><input class="delete" type="hidden" name="deletecustom[]" value="0" /></td><td><img class="delete-custom" alt="<?php _e('Remove Custom Field', $gcd) ?>" title="<?php _e('Remove Custom Field', $gcd) ?>" class="clickable" src="<?php echo $folder; ?>images/delete.png" /></td></tr>');
       
   586 						
       
   587 						$("#edit-gig-<?php echo $g->id ?> .delete-custom").unbind("click");
       
   588 						$("#edit-gig-<?php echo $g->id ?> .delete-custom").click(function(){
       
   589 							$(this).parents("tr").eq(0).remove();
       
   590 						});
       
   591 					});
       
   592 					
       
   593 					$("#edit-gig-<?php echo $g->id ?> .delete-custom").click(function(){
       
   594 						$(this).parents("td").eq(0).siblings().children("input.delete").val(1);
       
   595 						$(this).parents("tr").eq(0).hide();
       
   596 					});
       
   597 					
       
   598 					$("#postcustom-<?php echo $g->id ?> h3.clickable").click(function(){
       
   599 						$(this).next().toggle();
       
   600 						$(this).children(".inline-icon").toggle();
       
   601 					});
       
   602 					
       
   603 					$("#edit-gig-<?php echo $g->id ?> .date").datepicker({dateFormat:"yy-mm-dd"});
       
   604 					$(".date-field").datepicker({dateFormat:"yy-mm-dd"});
       
   605 
       
   606 					setupPerformances();
       
   607 					
       
   608 					$("#edit-gig-reset-<?php echo $g->id ?>").click(function(){
       
   609 						$(this).parents("tr.panel").hide();
       
   610 					});
       
   611 					
       
   612 					$('.inline-icon').tooltip({
       
   613 						showURL:false
       
   614 					});
       
   615 					
       
   616 					$("#edit-gig-<?php echo $g->id ?>").ajaxForm({
       
   617 						url:pageTarget,
       
   618 						dataType: "json",
       
   619 						success:function(json){
       
   620 							row = $("#gig-panel-" + json.gig.id).hide().prev();
       
   621 							row.children("td.eventName").html(json.gig.eventName);
       
   622 							row.children("td.date").html(json.gig.date);
       
   623 							row.find("a.guid").attr("href", json.gig.permalink);
       
   624 							times = row.children("td.time");
       
   625 							times.html("");
       
   626 							for ( i = 0; i < json.gig.performances.length; i++ ) {
       
   627 								time = json.gig.performances[i].time.split(":");
       
   628 								<?php if ( $options['time-12h'] ) : ?>
       
   629 									if ( time[0] == 0 ) {
       
   630 										time[0] = 12;
       
   631 										time[2] = 'am';
       
   632 									} else if ( time[0] == 12 ) {
       
   633 										time[2] = 'pm';
       
   634 									} else if ( time[0] < 12 ) {
       
   635 										time[2] = 'am';
       
   636 									} else if ( time[0] > 12 ) {
       
   637 										time[0] = time[0] - 12;
       
   638 										time[2] = 'pm';
       
   639 									}
       
   640 									times.append('<span class="time">' + time[0] + ':' + time[1] + time[2] + '</span> ');
       
   641 								<?php else : ?>
       
   642 									times.append('<span class="time">' + time[0] + ':' + time[1] + '</span> ');
       
   643 								<?php endif; ?>
       
   644 							}
       
   645 							
       
   646 						}
       
   647 					});
       
   648 				}(jQuery));
       
   649 			</script>
       
   650 		<?php
       
   651 		break;
       
   652 		
       
   653 	case 'save':
       
   654 		$g = new gig($_POST['id']);
       
   655 
       
   656 		$g->setTags($_POST['tags']);
       
   657 
       
   658 		$g->date = $_POST['date'];
       
   659 		$g->notes = $_POST['notes'];
       
   660 		$g->eventName = $_POST['eventName'];
       
   661 		$g->tour_id = ( $_POST['tour_id'] == -1 ? null : $_POST['tour_id'] );
       
   662 
       
   663 		if ( isset($_POST['performanceID']) ) {
       
   664 			foreach ( $_POST['performanceID'] as $key => $pid ) {
       
   665 				$p = new performance((empty($pid) ? null : $pid));
       
   666 				$p->gigID = $g->id;
       
   667 				$p->link = ($_POST['link'][$key] == 'http://') ? '' : $_POST['link'][$key];
       
   668 				$p->shortNotes = $_POST['shortNotes'][$key];
       
   669 				$p->ages = (empty($_POST['ages-custom'][$key]) ? $_POST['ages'][$key] : $_POST['ages-custom'][$key]);
       
   670 				$p->time = ($_POST['tbd'][$key] == "1") ? null : date("H:i:s", strtotime($_POST['hour'][$key] . ':' . $_POST['minute'][$key] . ' ' . $_POST['meridiem'][$key]));
       
   671 				$p->save();
       
   672 			}
       
   673 		}
       
   674 		
       
   675 		$p = new performance();
       
   676 		if ( isset($_POST['delete']) ) {
       
   677 			foreach ( $_POST['delete'] as $pid ) {
       
   678 				$p->get($pid);
       
   679 				$p->delete();
       
   680 			}
       
   681 		}
       
   682 		
       
   683 		$g->save();
       
   684 		
       
   685 		if ( isset($_POST['deletecustom']) && is_array($_POST['deletecustom']) ) {
       
   686 			foreach ( $_POST['deletecustom'] as $key => $delete ) {
       
   687 				if ( $delete ) {
       
   688 					delete_post_meta($g->postID, $_POST['custom-key'][$key], $_POST['old-custom-value'][$key]);
       
   689 				}
       
   690 			}
       
   691 		}
       
   692 		
       
   693 		if ( isset($_POST['custom-key']) && is_array($_POST['custom-key']) ) {
       
   694 
       
   695 			foreach ( $_POST['custom-key'] as $key => $field ) {
       
   696 				if ( is_array($_POST['custom-value'][$key]) ) {
       
   697 					foreach ( $_POST['custom-value'][$key] as $k => $v ) {
       
   698 						if ( $v === "" ) {
       
   699 							unset($_POST['custom-value'][$key][$k]);
       
   700 						}
       
   701 					}
       
   702 				}
       
   703 				if ( empty($_POST['custom-value'][$key]) && empty($_POST['old-custom-value'][$key]) ) {
       
   704 					// If both value fields are empty, just ignore this field.
       
   705 					continue;
       
   706 				} elseif ( $_POST['custom-value'][$key] === "" ) {
       
   707 					// If the old value is not empty, but the new one is, delete this sucker.
       
   708 					delete_post_meta($g->postID, $_POST['custom-key'][$key], $_POST['old-custom-value'][$key]);
       
   709 				} elseif ( $_POST['old-custom-value'][$key] === "" ) {
       
   710 					// If the old value is empty, but the new one is not, create a new custom field.
       
   711 					add_post_meta($g->postID, $_POST['custom-key'][$key], $_POST['custom-value'][$key]);
       
   712 				} elseif ( $_POST['old-custom-value'][$key] != $_POST['custom-value'][$key] && $_POST['old-custom-value'][$key] != serialize($_POST['custom-value'][$key]) ) {
       
   713 					// If the old and new values are not equal, update the custom field.
       
   714 					update_post_meta($g->postID, $_POST['custom-key'][$key], $_POST['custom-value'][$key], unserialize($_POST['old-custom-value'][$key]));
       
   715 				}
       
   716 			}
       
   717 		}
       
   718 		
       
   719 		echo '{success:true, gig:' . $g->toJSON() . '}';
       
   720 		break;
       
   721 		
       
   722 	case 'performance-form':
       
   723 		if ( isset($_POST['id']) ) {
       
   724 			pForm($_POST['id'], false);
       
   725 		} else {
       
   726 			pForm((int) $_POST['count'], true);
       
   727 		}
       
   728 		break;
       
   729 }
       
   730 
       
   731 
       
   732 function pForm($id, $new = true, $remove = true) {
       
   733 	global $folder, $gcd, $options;
       
   734 	$p = new performance();
       
   735 	if ( $new ) {
       
   736 		$count = 'c' . $id;
       
   737 	} else {
       
   738 		$p->get($id);
       
   739 		$count = 'id' . $id;
       
   740 	}
       
   741 	
       
   742 	$ages = $options['ages-list'];
       
   743 	
       
   744 	$time = explode(":", $p->time);
       
   745 	$minutes = $time[1];
       
   746 	
       
   747 	if ( $options['time-12h'] ) {
       
   748 		if ( $time[0] == 0 ) {
       
   749 			$hour = "12";
       
   750 			$meridiem = "AM";
       
   751 		} elseif ( $time[0] == 12 ) {
       
   752 			$hour = "12";
       
   753 			$meridiem = "PM";
       
   754 		} elseif ( $time[0] < 12 ) {
       
   755 			$hour = $time[0];
       
   756 			$meridiem = "AM";
       
   757 		} else {
       
   758 			$hour = $time[0] - 12;
       
   759 			$meridiem = "PM";
       
   760 		}
       
   761 	} else { 
       
   762 		$hour = $time[0];
       
   763 	}
       
   764 	?>
       
   765 		<table id="performance-<?php echo $count ?>" class="performance performance-<?php echo $count ?> <?php if ( $new ) echo 'new' ?>"><tbody>
       
   766 			<tr>
       
   767 				<td>
       
   768 					<label>
       
   769 						<input type="hidden" class="performanceID" name="performanceID[]" value="<?php echo $p->id; ?>" />
       
   770 						<?php _e('Performance Time:', $gcd) ?>
       
   771 					</label>
       
   772 				</td><td>
       
   773 					<div>
       
   774 						<select <?php if ( is_null($p->time) ) echo 'disabled="disabled"'; ?> name="hour[]">
       
   775 							<?php for ( $i = ($options['time-12h'] ? 1 : 0); $i <= ($options['time-12h'] ? 12 : 23); $i++ ) : ?>
       
   776 								<option <?php if ( $i == $hour ) echo 'selected="selected"' ?>><?php echo $i ?></option>
       
   777 							<?php endfor; ?>
       
   778 						</select>
       
   779 						<select <?php if ( is_null($p->time) ) echo 'disabled="disabled"'; ?> name="minute[]">
       
   780 							<option <?php if ( "00" == $minutes ) echo 'selected="selected"' ?>>00</option>
       
   781 							<option <?php if ( "15" == $minutes ) echo 'selected="selected"' ?>>15</option>
       
   782 							<option <?php if ( "30" == $minutes ) echo 'selected="selected"' ?>>30</option>
       
   783 							<option <?php if ( "45" == $minutes ) echo 'selected="selected"' ?>>45</option>
       
   784 							<option value="00">--</option>
       
   785 							<?php for ( $i = 0; $i <= 59; $i++ ) : ?>
       
   786 								<option <?php if ( !in_array($i, array("00", "15", "30", "45")) && $i == $minutes ) echo 'selected="selected"' ?>><?php echo str_pad($i, 2, STR_PAD_LEFT, '0') ?></option>
       
   787 							<?php endfor; ?>
       
   788 						</select>
       
   789 						<?php if ( $options['time-12h'] ) : ?>
       
   790 							<select <?php if ( is_null($p->time) ) echo 'disabled="disabled"'; ?> name="meridiem[]">
       
   791 								<option <?php if ( 'AM' == $meridiem ) echo 'selected="selected"' ?>><?php _e('AM', $gcd) ?></option>
       
   792 								<option <?php if ( 'PM' == $meridiem ) echo 'selected="selected"' ?>><?php _e('PM', $gcd) ?></option>
       
   793 							</select>
       
   794 						<?php else : ?>
       
   795 							<input type="hidden" name="meridiem[]" value="" />
       
   796 						<?php endif; ?>
       
   797 						<input <?php if ( is_null($p->time) ) echo 'checked="checked"'; ?> type="checkbox" value="1" class="tbd" name="tbd[]" id="performance-tbd-<?php echo $count; ?>"> <label for="performance-tbd-<?php echo $count; ?>"><?php echo $options['tbd-text']; ?></label>
       
   798 						<?php if ( $remove ) : ?>
       
   799 							<div class="delete-performance"><img alt="<?php _e('Remove Performance', $gcd) ?>" title="<?php _e('Remove Performance', $gcd) ?>" class="clickable" src="<?php echo $folder; ?>images/delete.png" /></div>
       
   800 						<?php endif ?>
       
   801 					</div>
       
   802 				</td>
       
   803 			</tr><tr>
       
   804 				<td>
       
   805 					<label for="performance-link[]"><?php _e('External Link (for tickets):', $gcd) ?></label>
       
   806 				</td><td>
       
   807 					<input type="text" class="link wide" name="link[]" id="performance-link[]" value="<?php echo empty($p->link) ? 'http://' : dtcGigs::escapeForInput($p->link) ?>" />
       
   808 				</td>
       
   809 			</tr><tr>
       
   810 				<td>
       
   811 					<label for="performance-shortNotes-<?php echo $count; ?>"><?php _e('Short Notes:', $gcd) ?></label>
       
   812 				</td><td>
       
   813 					<input type="text" class="shortNotes wide" name="shortNotes[]" id="performance-shortNotes-<?php echo $count; ?>" value="<?php echo dtcGigs::escapeForInput($p->shortNotes) ?>" />
       
   814 				</td>
       
   815 			</tr><tr>
       
   816 				<td>
       
   817 					<label for="performance-ages-<?php echo $count; ?>"><?php _e('Ages:', $gcd) ?></label>
       
   818 				</td><td>
       
   819 					<select name="ages[]" id="performance-ages-<?php echo $count; ?>">
       
   820 						<?php $found = false; foreach ( $ages as $age ) : ?>
       
   821 							<option <?php if ( $p->ages == $age ) {echo 'selected="selected"'; $found = true;} ?>><?php echo dtcGigs::escapeForInput($age) ?></option>
       
   822 						<?php endforeach; ?>
       
   823 					</select>
       
   824 					<?php _e('Or...', $gcd) ?>
       
   825 					<input type="text" class="ages" name="ages-custom[]" id="performance-ages-custom[]" value="<?php if ( !$found ) echo $p->ages; ?>" />
       
   826 				</td>
       
   827 			</tr>
       
   828 		</tbody></table>
       
   829 		<script type="text/javascript">
       
   830 			(function($){
       
   831 				
       
   832 				$("input.tbd").unbind("click");
       
   833 				$("input.tbd").click(function(){
       
   834 					el = $(this);
       
   835 					if ( $(this).attr("checked") ) {
       
   836 						$(this).prevAll("select").attr("disabled", true);
       
   837 					} else {
       
   838 						$(this).prevAll("select").attr("disabled", false);
       
   839 					}
       
   840 				});
       
   841 			})(jQuery);
       
   842 		</script>
       
   843 	<?php
       
   844 }
       
   845 ?>