web/wp-content/plugins/gigs-calendar/tours.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 $pageTarget = $folder . 'tours.ajax.php';
       
     5 
       
     6 switch ($_POST['action']) {
       
     7 	case 'load':
       
     8 		$t = new tour();
       
     9 		$t->search(null, ($options['tours-sort'] == 'custom' ? '`pos`' : '`name`'));
       
    10 		?>
       
    11 			<div class="clickable" id="tour-add-trigger">
       
    12 				<img class="icon" src="<?php echo $folder; ?>images/add.png" /> <?php _e('Add a new tour', $gcd) ?>
       
    13 			</div>
       
    14 			
       
    15 			<div id="tour-add-form" style="display: none;">
       
    16 				<form id="new-tour" class="new-item" method="post" action="<?php echo $pageTarget; ?>">
       
    17 					<table>
       
    18 						<tbody>
       
    19 							<tr><td colspan="2">
       
    20 								<h3 class="no-margin"><?php _e('Tour Information', $gcd) ?></h3>
       
    21 							</td></tr>
       
    22 							<tr>
       
    23 							<tr>
       
    24 								<td><label for="new-name"><?php _e('Tour Name:', $gcd) ?></label></td>
       
    25 								<td><input type="text" class="name wide" name="name" id="new-name" /></td>
       
    26 							</tr>
       
    27 							<tr><td colspan="2">
       
    28 								<?php _e('Description/Other notes:', $gcd) ?><br />
       
    29 								<textarea class="notes" name="notes" rows="8" cols="80"></textarea>
       
    30 							</td></tr>
       
    31 						</tbody>
       
    32 					</table>
       
    33 					<div>
       
    34 						<input type="submit" class="button" name="" value="<?php _e('Add tour', $gcd) ?>" />
       
    35 						<input type="reset" class="button cancel" name="" value="<?php _e('Cancel', $gcd) ?>" id="new-tour-reset" />
       
    36 						<input type="hidden" name="action" value="add" />
       
    37 						<input type="hidden" name="nonce" value="<?php echo $_POST['nonce']; ?>" />
       
    38 					</div>
       
    39 				</form>
       
    40 			</div>
       
    41 			
       
    42 			<table id="tour-list" class="tours widefat">
       
    43 				<thead>
       
    44 					<tr>
       
    45 						<th style="text-align: center;" scope="col"><?php _e('ID', $gcd) ?></th>
       
    46 						<th scope="col"><?php _e('Name', $gcd) ?></th>
       
    47 						<th scope="col"><?php _e('Notes', $gcd) ?></th>
       
    48 						<th style="text-align: center" scope="col"><?php _e('Actions', $gcd) ?></th>
       
    49 					</tr>
       
    50 				</thead>
       
    51 				<tbody>
       
    52 					<?php while ( $t->fetch() ) : ?>
       
    53 						<tr id="tour-<?php echo $t->id; ?>" class="tour tour-<?php echo $t->id; ?> <?php echo ++$count % 2 ? "alternate" : "";?>">
       
    54 							<th style="text-align: center;" scope="row"><?php echo $t->id; ?></th>
       
    55 							<td class="name"><?php echo $t->name; ?></td>
       
    56 							<td class="notes"><?php 
       
    57 								if( strlen( strip_tags($t->notes) ) > 40 ) {
       
    58 									echo substr( strip_tags($t->notes), 0, 40 ) . '...';
       
    59 								} else {
       
    60 									echo strip_tags($t->notes);
       
    61 								}
       
    62 							?></td>
       
    63 							<td class="actions" style="text-align: center; position: relative;">
       
    64 								<div style="position: relative">
       
    65 									<img alt="<?php _e('Edit', $gcd) ?>" title="<?php _e('Edit', $gcd) ?>" class="clickable edit" src="<?php echo $folder; ?>images/page_white_edit.png" />
       
    66 									<?php if ( $v ) : ?>
       
    67 										<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>
       
    68 									<?php endif ?>
       
    69 									<img alt="<?php _e('Delete', $gcd) ?>" title="<?php _e('Delete', $gcd) ?>" class="clickable delete" src="<?php echo $folder; ?>images/delete.png" />
       
    70 								</div>
       
    71 							</td>
       
    72 						</tr>
       
    73 						<tr id="tour-panel-<?php echo $t->id; ?>" class="tour tour-<?php echo $t->id; ?> panel <?php echo $count % 2 ? "alternate" : "";?>">
       
    74 							<td style="background-color: white"></td>
       
    75 							<td class="panel" colspan="5"></td>
       
    76 						</tr>
       
    77 					<?php endwhile; ?>
       
    78 				</tbody>
       
    79 			</table>
       
    80 			
       
    81 			<script type="text/javascript">
       
    82 				(function($){
       
    83 					// Trigger to show the new tour form.
       
    84 					$("#tour-add-trigger").click(function(){
       
    85 						$("#tour-add-form:hidden").slideDown(300, function(){
       
    86 							$("#new-name").focus();
       
    87 						});
       
    88 					});
       
    89 					
       
    90 					// Reset form button
       
    91 					$("#new-tour-reset").click(function(){
       
    92 						$("#tour-add-form table.performance[id!=performance-c1]").parents("tr").remove();
       
    93 						$("#tour-add-form").slideUp(300);
       
    94 					});
       
    95 
       
    96 					// Submit the new tour form via ajax.
       
    97 					$("#new-tour").ajaxForm({
       
    98 						url:pageTarget,
       
    99 						dataType: "json",
       
   100 						success:function(json){
       
   101 							tours = $("table#tour-list tbody tr:not(.panel) td.name");
       
   102 							inserted = false;
       
   103 							for ( i = 0; i < tours.length; i++ ) {
       
   104 								if ( json.tour.name < tours.eq(i).html() ) {
       
   105 									t = tours.eq(i);
       
   106 									$.post(pageTarget, {
       
   107 										nonce:nonce,
       
   108 										action:'getRow',
       
   109 										id:json.tour.id
       
   110 									}, function(rsp){
       
   111 										t.parents("tr").before(rsp);
       
   112 										resetTableColors("table#tour-list");
       
   113 										setupEvents();
       
   114 									});
       
   115 									inserted = true;
       
   116 									break;
       
   117 								}
       
   118 							}
       
   119 							if ( !inserted ) {
       
   120 								$.post(pageTarget, {
       
   121 									nonce:nonce,
       
   122 									action:'getRow',
       
   123 									id:json.tour.id
       
   124 								}, function(rsp){
       
   125 									$("table#tour-list tbody").append(rsp);
       
   126 									resetTableColors("table#tour-list");
       
   127 									setupEvents();
       
   128 								});
       
   129 							}
       
   130 							$("#new-tour-reset").click();
       
   131 						}
       
   132 
       
   133 					});
       
   134 					
       
   135 					setupEvents = function() {
       
   136 						$("img.delete").unbind("click");
       
   137 						$("img.delete").click(function(){
       
   138 							if ( confirm("Are you sure you want to delete this tour?") ) {
       
   139 								id = $(this).parents("tr").attr("id").split("-")[1];
       
   140 								$.post(pageTarget, {
       
   141 									nonce:nonce,
       
   142 									action:'delete',
       
   143 									id:id
       
   144 								}, function (rsp){
       
   145 									console.log(rsp)
       
   146 								}, "json");
       
   147 								$(this).parents("tr").next().remove();
       
   148 								$(this).parents("tr").remove();
       
   149 								resetTableColors("table#tour-list");
       
   150 							}
       
   151 						});
       
   152 						
       
   153 						$("img.edit").unbind("click");
       
   154 						$("img.edit").click(function(){
       
   155 							row = $(this).parents("tr");
       
   156 							id = row.attr("id").split("-")[1];
       
   157 							row.next().children("td.panel").load(pageTarget, {
       
   158 								nonce:nonce,
       
   159 								action:"edit",
       
   160 								id:id
       
   161 							}, function(){
       
   162 								row.next().css("display", "table-row");
       
   163 							});
       
   164 						});
       
   165 
       
   166 					};
       
   167 					
       
   168 					setupEvents();
       
   169 				})(jQuery);
       
   170 			</script>
       
   171 			<?php /*
       
   172 
       
   173 			
       
   174 
       
   175 				
       
   176 			<script type="text/javascript">
       
   177 			
       
   178 					setupEvents = function() {
       
   179 						
       
   180 						
       
   181 						setupPerformances();
       
   182 					}
       
   183 					
       
   184 					setupEvents();
       
   185 
       
   186 				}(jQuery));
       
   187 				
       
   188 			
       
   189 			
       
   190 			</script>
       
   191 		<?php
       
   192 		*/
       
   193 		break;
       
   194 	case 'add':
       
   195 		$t = new tour();
       
   196 		
       
   197 		$t->name = $_POST['name'];
       
   198 		$t->notes = $_POST['notes'];
       
   199 		
       
   200 		if ( $t->save() ) {
       
   201 			echo '{success:true, tour:' . $t->toJSON() . '}';
       
   202 		} else {
       
   203 			echo '{success:false}';
       
   204 		}
       
   205 		break;
       
   206 	case 'getRow':
       
   207 		$t = new tour($_POST['id']);
       
   208 		?>
       
   209 						<tr id="tour-<?php echo $t->id; ?>" class="tour tour-<?php echo $t->id; ?> <?php echo ++$count % 2 ? "alternate" : "";?>">
       
   210 							<th style="text-align: center;" scope="row"><?php echo $t->id; ?></th>
       
   211 							<td class="name"><?php echo $t->name; ?></td>
       
   212 							<td class="notes"><?php 
       
   213 								if( strlen( strip_tags($t->notes) ) > 40 ) {
       
   214 									echo substr( strip_tags($t->notes), 0, 40 ) . '...';
       
   215 								} else {
       
   216 									echo strip_tags($t->notes);
       
   217 								}
       
   218 							?></td>
       
   219 							<td class="actions" style="text-align: center; position: relative;">
       
   220 								<div style="position: relative">
       
   221 									<img alt="<?php _e('Edit', $gcd) ?>" title="<?php _e('Edit', $gcd) ?>" class="clickable edit" src="<?php echo $folder; ?>images/page_white_edit.png" />
       
   222 									<?php if ( $v ) : ?>
       
   223 										<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>
       
   224 									<?php endif ?>
       
   225 									<img alt="<?php _e('Delete', $gcd) ?>" title="<?php _e('Delete', $gcd) ?>" class="clickable delete" src="<?php echo $folder; ?>images/delete.png" />
       
   226 								</div>
       
   227 							</td>
       
   228 						</tr>
       
   229 						<tr id="tour-panel-<?php echo $t->id; ?>" class="tour tour-<?php echo $t->id; ?> panel <?php echo $count % 2 ? "alternate" : "";?>">
       
   230 							<td style="background-color: white"></td>
       
   231 							<td class="panel" colspan="5"></td>
       
   232 						</tr>
       
   233 		<?php
       
   234 		break;
       
   235 	
       
   236 	case 'delete':
       
   237 		$t = new tour($_POST['id']);
       
   238 		$result = $t->delete();
       
   239 		echo '{"success": ' . ($result ? 'true' : 'false') . ',"action":"delete"' . ($result ? '' : ',"error":"db"') . '}';
       
   240 		break;
       
   241 	case 'edit':
       
   242 		$t = new tour($_POST['id']);
       
   243 		?>
       
   244 			<form id="edit-tour-<?php echo $t->id ?>" class="edit-item" method="post" action="<?php echo $pageTarget; ?>">
       
   245 				<table>
       
   246 					<tbody>
       
   247 						<tr><td colspan="2">
       
   248 							<h3 class="no-margin"><?php _e('Tour Information', $gcd) ?></h3>
       
   249 						</td></tr>
       
   250 						<tr>
       
   251 							<td><label for="edit-name-<?php echo $t->id ?>"><?php _e('Tour Name:', $gcd) ?></label></td>
       
   252 							<td><input type="text" class="name wide" name="name" id="edit-name-<?php echo $t->id ?>" value="<?php dtcGigs::escapeForInput($t->name); ?>" /></td>
       
   253 						</tr>
       
   254 						<tr><td colspan="2">
       
   255 							<?php _e('Description/Other notes:', $gcd) ?><br />
       
   256 							<textarea class="notes" name="notes" rows="8" cols="80"><?php dtcGigs::escapeForInput($t->notes); ?></textarea>
       
   257 						</td></tr>
       
   258 					</tbody>
       
   259 				</table>
       
   260 				<div class="extra-inputs">
       
   261 					<input type="submit" class="button" name="" value="Save tour" />
       
   262 					<input type="reset" class="button cancel" name="" value="Cancel" id="edit-tour-reset-<?php echo $t->id ?>" />
       
   263 					<input type="hidden" name="action" value="save" />
       
   264 					<input type="hidden" name="id" value="<?php echo $t->id ?>" />
       
   265 					<input type="hidden" name="nonce" value="<?php echo $_POST['nonce']; ?>" />
       
   266 				</div>
       
   267 			</form>
       
   268 			
       
   269 			<script type="text/javascript">
       
   270 				(function($){
       
   271 					$("#edit-tour-reset-<?php echo $t->id ?>").click(function(){
       
   272 						$(this).parents("tr.panel").hide();
       
   273 					});
       
   274 					
       
   275 					$("#edit-tour-<?php echo $t->id ?>").ajaxForm({
       
   276 						url:pageTarget,
       
   277 						dataType: "json",
       
   278 						success:function(json){
       
   279 							row = $("#tour-panel-" + json.tour.id).hide().prev();
       
   280 							row.children("td.name").html(json.tour.name);
       
   281 							if ( json.tour.notes.length > 40 ) {
       
   282 								row.children("td.notes").html(json.tour.notes.substr(0,40) + '...');
       
   283 							} else {
       
   284 								row.children("td.notes").html(json.tour.notes);
       
   285 							}
       
   286 							
       
   287 						}
       
   288 					});
       
   289 				}(jQuery));
       
   290 			</script>
       
   291 		<?php
       
   292 		break;
       
   293 		
       
   294 	case 'save':
       
   295 		$t = new tour($_POST['id']);
       
   296 
       
   297 		$t->name = $_POST['name'];
       
   298 		$t->notes = $_POST['notes'];
       
   299 				
       
   300 		$t->save();
       
   301 		echo '{success:true, tour:' . $t->toJSON() . '}';
       
   302 		break;
       
   303 		
       
   304 	case 'performance-form':
       
   305 		if ( isset($_POST['id']) ) {
       
   306 			pForm($_POST['id'], false);
       
   307 		} else {
       
   308 			pForm((int) $_POST['count'], true);
       
   309 		}
       
   310 		break;
       
   311 }
       
   312 ?>