plugin hotal

<?php //hotel
/*
Plugin Name: Hotel Information

*/
?>

<style type=”text/css”>
.silverback{
font-size: 12px;
font-weight: bold;
line-height: 1;
margin: 0;

padding: 7px 9px;
}
.lefttd{
text-align:left;width: 125px; padding: 5px 10px 0pt 5px;”
}

.textwidths{  width:350px }
</style>
<?php function hotel_install(){
global $wpdb;

$table_coupon = $wpdb->prefix.”hotel”;
$structure_hotel = “CREATE TABLE $table_coupon ( `hotel_id` int(11) NOT NULL auto_increment,
`hotel_name` varchar(255) NOT NULL default ”,
`hotel_location` varchar(150) NOT NULL default ”,
`hotel_url` varchar(150) NOT NULL default ”,
`hotel_desc` text NOT NULL,
`hotel_img` varchar(150) NOT NULL default ”,
`hotel_created` datetime NOT NULL default ‘0000-00-00 00:00:00’,
PRIMARY KEY  (`hotel_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1″;

$wpdb->query($structure_hotel);
}
add_action(‘hotel’, ‘hotel_install’);
add_action(‘admin_menu’,’hotel_menu’);

define(WP_HOTEL_TABLE,$table_prefix . ‘hotel’);

add_filter(‘the_content’,’hotels_display’);

function hotel_menu () {
if(function_exists(‘add_submenu_page’)) {
//Count pending bookings
$num = ”;

add_object_page(__(‘Add Hotels’, ‘Add Hotels’),__(‘Hotels’, ‘Hotels’).$num,’edit_posts’,’hotel-list’,’list_hotel’);
// Add a submenu to the custom top-level menu:
$plugin_pages = array();
$plugin_pages[] = add_submenu_page(‘hotel-list’, __(‘List Hotel’),__(‘List Hotel’),’edit_posts’,’hotel-list’,’list_hotel’);
$plugin_pages[] = add_submenu_page(‘hotel-list’, __(‘Add new’, ‘Hotels’), __(‘Add new’,’Hotels’), ‘edit_posts’, ‘hotel-manager’, “add_hotel”);

foreach($plugin_pages as $plugin_page){

add_action( ‘admin_print_styles-‘. $plugin_page, ‘hotel_load_styles’ );
add_action( ‘admin_print_scripts-‘. $plugin_page, ‘hotel_load_scripts’ );
}
}
}
function hotel_load_scripts(){

wp_enqueue_script(‘hotel-manager’, WP_PLUGIN_URL.’/hotel/hotel.js’);

}

function hotel_load_styles(){
wp_enqueue_style(‘hotel-manager’, WP_PLUGIN_URL.’/hotel/hotel.css’);
}

##################### Listing of hotel in back end ################
function list_hotel(){

global $wpdb;

$pagenum = isset( $_GET[‘paged’] ) ? absint( $_GET[‘paged’] ) : 0;
//$per_page = 20;
if ( empty( $per_page ) || $per_page < 1 )
$per_page = 5;

if($pagenum)
$start = ($pagenum – 1) * $per_page;             //first item to display on this page
else
$start = 0;

if(empty($pagenum))
$pagenum = 1;
$action = ( !empty($_GET [‘action’]) ) ? $_GET [‘action’]:”;
/*$limit = 20;

$page = ( !empty($_GET[‘pno’]) ) ? $_GET[‘pno’]:1;

$offset = ( $page > 1 ) ? ($page-1)*$limit : 0;*/

// DELETE action
if ( $action == ‘deleteHotels’) {

$deleteArray=$_GET[‘hotels’];
if(isset($deleteArray) && count($deleteArray)>0){
foreach($deleteArray as $delArray){
$deleteQuery=”DELETE FROM ” . WP_HOTEL_TABLE . ” where hotel_id=$delArray”;
$wpdb->query($deleteQuery);
}
}
$msg=”Hotel has been deleted successfully!!”;
}

//$query=”SELECT * FROM ” . WP_HOTEL_TABLE . ” LIMIT $offset , $limit”;
///$hotels = $wpdb->get_results($query);

$querytot=”SELECT * FROM ” . WP_HOTEL_TABLE;
$itemsTotal = $wpdb->get_results($querytot);
$querystr=”SELECT * FROM ” . WP_HOTEL_TABLE.” LIMIT $start,$per_page”;
$hotels = $wpdb->get_results($querystr);

if(!empty($hotels)){
$counttotal=count($itemsTotal);
$num_pages = ceil(count($itemsTotal)/$per_page);
}
else
$num_pages = $wp_query->max_num_pages;

$page_links = paginate_links( array(
‘base’ => add_query_arg( ‘paged’, ‘%#%’ ),
‘format’ => ”,
‘prev_text’ => __(‘&laquo;’),
‘next_text’ => __(‘&raquo;’),
‘total’ => $num_pages,
‘current’ => $pagenum
));
?>
<div>
<h2>
<?php echo ‘Hotels’; ?>
<a href=”admin.php?page=hotel-manager”><?php _e(‘Add New’,’Hotel’); ?></a>
</h2>

<?php if ( !empty($msg) ) : ?>
<div id=’message’ class=’updated fade’>
<p><?php echo $msg; ?></p>
</div>
<?php endif; ?>

<form name=”posts-filter” id=”posts-filter” action=”” method=”get”>
<input type=’hidden’ name=’page’ value=’hotel-list’ />

<ul>
<li><a href=’#’><?php _e ( ‘Total’, ‘dbem’ ); ?> <span>(<?php echo (count ( $hotels )); ?>)</span></a></li>
</ul>

<div>

<div>
<select name=”action”>
<option value=”-1″ selected=”selected”><?php _e ( ‘Bulk Actions’ ); ?></option>
<option value=”deleteHotels”><?php _e ( ‘Delete selected’,’Hotels’ ); ?></option>
</select>
<input type=”submit” value=”<?php _e ( ‘Apply’ ); ?>” name=”doaction2″ id=”doaction2″ />
</div>
</div>

<table>
<thead>
<tr>
<th id=”cb” class=’manage-column column-cb check-column’ scope=’col’>
<input type=”checkbox” onclick=”checkall(this.form)” value=”1″ id=”check_all” name=”check_all”>
</th>
<th><?php _e ( ‘Name’, ‘Hotel’ ); ?></th>

<th><?php _e ( ‘Location’, ‘Hotel’ ); ?></th>
<th colspan=”2″><?php _e ( ‘Date and time’, ‘Hotel’ ); ?></th>

</tr>
</thead>
<tbody>
<?php
$rowno = 0;
$event_count = 0;
if($hotels && count($hotels)>0){
foreach ( $hotels as $hotel ) {
/* @var $event EM_Event */
if( ($rowno < $limit || empty($limit)) && ($event_count >= $offset || $offset === 0) ) {
$rowno++;
$class = ($rowno % 2) ? ” : ”;

?>
<tr <?php echo “$class”; ?>>

<td height=”40″>
<input type=’checkbox’ class=’row-selector’ id=’hotels[]’ value='<?php echo $hotel->hotel_id; ?>’ name=’hotels[]’ />
</td>
<td>
<strong>
<a href=”<?php bloginfo ( ‘wpurl’ )?>/wp-admin/admin.php?page=hotel-manager&amp;hotel_id=<?php echo $hotel->hotel_id ?>”><?php echo htmlentities(stripslashes(($hotel->hotel_name))); ?></a>
</strong>
</td>

<td>
<?php echo htmlentities(stripslashes($hotel->hotel_location)); ?>
</td>

<td>
<?php echo $hotel->hotel_created; ?>
</td>
<td>

<a href=”<?php bloginfo ( ‘wpurl’ )?>/wp-admin/admin.php?page=hotel-manager&amp;hotel_id=<?php echo $hotel->hotel_id ?>”>
<strong>View/Edit</strong>
</a>
</td>
</tr>
<?php
}
$event_count++;
}
}
?>
</tbody>
</table>

<div>
<?php
if($page_links){ ?>
<div><?php
$count_jobs =$counttotal;
$page_links_text = sprintf( ‘<span>’ . __( ‘Displaying %s–%s of %s’ ) . ‘</span>%s’,
number_format_i18n( ( $pagenum – 1 ) * $per_page + 1 ),
number_format_i18n( min( $pagenum * $per_page, $count_jobs ) ),
number_format_i18n( $count_jobs ),
$page_links
);
echo $page_links_text;
?>
</div>
<?php } ?>
</div>

<!–<div class=’tablenav’>
<div>
<br class=’clear’ />
</div>
<?php if ( $events_count >= $limit ) : ?>
<div>
<?php
echo $events_nav;
?>
</div>
<?php endif; ?>
<br class=’clear’ />
</div>–>
</form>
</div>

<?php

}

################# Add and Update Hotel#################################
function add_hotel(){
global $wpdb;

$insertid=(isset($_REQUEST[‘hotel_id’]))?$_REQUEST[‘hotel_id’]:”;

if(isset($_POST[‘hotel_update’]) && !empty($_POST[‘hotel_update’])){

if(!empty($_POST[‘hotel_name’])){

$post=$_POST;
if(isset($post[‘hotelId’]) && !empty($post[‘hotelId’])){

if($_FILES[‘hotel_img’][‘error’]==0){
$saveto=date(‘YmdHis’).$_FILES[‘hotel_img’][“name”];
$uploadpath=WP_PLUGIN_DIR . ‘/hotel/upload/’.$saveto;
if (move_uploaded_file($_FILES[‘hotel_img’][“tmp_name”],$uploadpath)) {
$img_name=$saveto;
}

}

$sql = “UPDATE  ” . WP_HOTEL_TABLE . ” SET
hotel_name='” . mysql_escape_string($post[‘hotel_name’])
. “‘, hotel_location='” . mysql_escape_string($post[‘hotel_location’])
. “‘, hotel_url='” . mysql_escape_string($post[‘hotel_url’])
. “‘, hotel_desc='” . mysql_escape_string($post[‘hotel_desc’]). “‘”;

if($img_name) $sql .= “, hotel_img='” . mysql_escape_string($img_name) . “‘”;
$sql .= “, hotel_modified='” . date(‘Y-m-d H:i:s’)
. “‘ where hotel_id='” . $post[‘hotelId’] . “‘”;

$wpdb->query($sql);
$msg=”Hotel has been updated sucessfully!!”;
$insertid=$post[‘hotelId’];

}else{

if($_FILES[‘hotel_img’][‘error’]==0){
$saveto=date(‘YmdHis’).$_FILES[‘hotel_img’][“name”];
$uploadpath=WP_PLUGIN_DIR . ‘/hotel/upload/’.$saveto;
if (move_uploaded_file($_FILES[‘hotel_img’][“tmp_name”], $uploadpath)) {
$img_name=$saveto;
}

}

$sql = “INSERT INTO ” . WP_HOTEL_TABLE . ” SET
hotel_name='” . mysql_escape_string($post[‘hotel_name’])
. “‘, hotel_location='” . mysql_escape_string($post[‘hotel_location’])
. “‘, hotel_url='” . mysql_escape_string($post[‘hotel_url’])
. “‘, hotel_desc='” . mysql_escape_string($post[‘hotel_desc’])
. “‘, hotel_img='” . mysql_escape_string($img_name)
. “‘, hotel_created='” . date(‘Y-m-d H:i:s’)
. “‘”;

$wpdb->query($sql);
if($insertid=$wpdb->insert_id){
$msg=”Hotel has been saved sucessfully!!”;
}else{
$msg=”An error is occured during save the hotels. Try again”;
}

}
}else{

$msg=”Please enter hotel name or location”;
}

}

if(!empty($insertid) ){

$event = $wpdb->get_row(“SELECT * FROM ” . WP_HOTEL_TABLE . ” where hotel_id='” .$insertid . “‘”);

if($event){

$hotelId=$event->hotel_id;
$hotelName=htmlentities(stripslashes($event->hotel_name));
$hotelLocation=htmlentities(stripslashes($event->hotel_location));
$hotelUrl=$event->hotel_url;
$hotelDesc=htmlentities(stripslashes($event->hotel_desc));
$hotelImg=$event->hotel_img;
}

}

?>
<script src=”<?php echo WP_PLUGIN_URL?>/hotel/hotel.js”></script>
<form action=”” method=”post” id=”event-form” onsubmit=”return validateHotel()” enctype=”multipart/form-data”>
<div>
<div id=”icon-events”><br></div>
<h2>Insert New Hotel</h2>
<?php if($msg){?>
<div id=”message”>
<p><?php echo $msg?>            </p>
</div>
<?php }?>
<div  style=”width:900px”>
<h3>Hotel Description</h3>
<div>
<table border=”1″ width=”100%”>
<tr>
<td colspan=”3″>&nbsp;</td>
</tr>
<tr>
<th style=”width:200px” valign=”top” align=”left”>Hotel Title</th>
<td colspan=”2″>
<input type=”text” value=”<?php echo $hotelName ?>” id=”hotel-name” name=”hotel_name”>
<p><em>Enter hotel name</em></p>
<?php
if(!empty($insertid)){

echo ‘<input name=”hotelId” type=”hidden” value=”‘ . $hotelId .'”>’;
}

?>
</td>
</tr>
<tr>
<th valign=”top” align=”left”>Hotel Location</th>
<td  colspan=”2″>
<input type=”text” value=”<?php echo $hotelLocation ?>” id=”hotel-location” name=”hotel_location”>
<p><em>Enter hotel location</em></p>

</td>
</tr>
<tr>
<th valign=”top” align=”left”>Hotel Url</th>
<td  colspan=”2″>
<input type=”text” value=”<?php echo $hotelUrl ?>” id=”hotel-url” name=”hotel_url”>
<p><em>Enter hotel Url</em></p>
</td>
</tr>
<tr>
<th valign=”top” align=”left”>Hotel Description</th>
<td colspan=”2″  >

<textarea class=”” rows=”15″ cols=”85″ id=”hotelt-desc” name=”hotel_desc”><?php echo $hotelDesc ?></textarea>
<p><em>Enter hotel Description</em></p>
</td>

</tr>

<tr>
<th valign=”top” align=”left”>Upload Image</th>
<td>
<input type=”file” value=”” id=”hotelt-img” name=”hotel_img”>
<p><em>Upload Hotel Image</em></p>
</td>
<td rowspan=”2″ align=”left” style=”pading:5px 15px 5px 5px”>
<?php if(!empty($hotelImg)){

echo ‘<img src=”‘ . WP_PLUGIN_URL  . ‘/hotel/upload/’ . $hotelImg . ‘” width=”200″ height=”200″ >’;

}
?>
</td>

</tr>
<tr>

<td colspan=”2″>
<p>
<input type=”submit” value=”Submit Hotel” name=”hotel_update”>
</p>
</td>
</tr>
</table>
</div>
</div>
</div>
</form>

<?php

}

####################### Hook at the_content  {HOTELINFO}  ###################################
function hotels_display($content){
if (stripos($content,'{HOTELINFO’)!==false) {
if(isset($_GET[‘hotel_id’]) && !empty($_GET[‘hotel_id’])){
$params=$_GET[‘hotel_id’];

}
$count = 0;
while (stripos($content,'{HOTELINFO’)!==false && $count<=5) {
$start = stripos($content, “{HOTELINFO”);
$end = stripos($content, “}”, $start)+1;
//$parameters = trim(substr($content, $start+10, ($end-$start-11)));
$content_output = displayHotels($params);
$content = substr($content, 0, $start) . $content_output . substr($content, $end);
$count++;
}

}
return $content;

}

####################### To display in fron end ###################################
function displayHotels($params){
global $wpdb;
if(!empty($params)){

$hotel = $wpdb->get_row(“SELECT * FROM ” . WP_HOTEL_TABLE . ” where hotel_id=” .$params);
$img=WP_PLUGIN_URL . “/hotel/upload/” . $hotel->hotel_img;
$new_title= htmlentities(stripslashes($hotel->hotel_name));
$location= htmlentities(stripslashes($hotel->hotel_location));
$website=$hotel->hotel_url;
$desc= nl2br(htmlentities(strip_tags(stripslashes($hotel->hotel_desc))));

?>
<div style=”padding: 0 0 0 0 ;”>
<div>
<div>

<?php if(!empty($img)) { ?>
<img src=”<?php bloginfo(‘template_url’); ?>/image.php?image=<?php echo $img ?>&width=120″/>
<?php } ?>

</div>
<div>
<?php
$gmap_url = get_page_by_title(“Google Map”);
$cur_page_id = get_the_ID();
?>
<h5><?php echo $new_title ?></h5>
<p><?php echo $location; ?>. <span><a href=”?page_id=<?php echo $gmap_url->ID;?>&rpage_id=<?php echo $cur_page_id;?>&Etype=hotel&hotel_id=<?php echo $hotel->hotel_id?>”>View Map    </a></span></p>
<p><?php echo $website; ?></p>
</div>
</div>

<p><?php echo $desc; ?></p>
<div><a href=”javascript:void(0)” onClick=’window.history.go(-1)’>Go Back to Hotels</a></div>
</div>

<?php

}else{

$pagenum = isset( $_GET[‘paged’] ) ? absint( $_GET[‘paged’] ) : 0;
if ( empty( $per_page ) || $per_page < 1 )    $per_page = 5;
if($pagenum)
$start = ($pagenum – 1) * $per_page;             //first item to display on this page
else
$start = 0;

if(empty($pagenum))    $pagenum = 1;
//echo “SELECT * FROM ” . WP_HOTEL_TABLE .” LIMIT $start,$per_page”;
$hotels2 = $wpdb->get_results(“SELECT * FROM ” . WP_HOTEL_TABLE);
$hotels = $wpdb->get_results(“SELECT * FROM ” . WP_HOTEL_TABLE .” LIMIT $start,$per_page”);

$itemsTotal = $hotels2;
if(!empty($hotels)){
$counttotal=count($itemsTotal);
$num_pages = ceil(count($itemsTotal)/$per_page);
}
else
$num_pages = $wp_query->max_num_pages;

$flagShow=($num_pages==5)?true:false;

$page_links = paginate_links( array(
‘base’ => add_query_arg( ‘paged’, ‘%#%’ ),
‘format’ => ”,
‘prev_text’ => __(‘&nbsp;’),
‘next_text’ => __(‘&nbsp;’),
‘total’ => $num_pages,
‘show_all’ =>$flagShow,
‘current’ => $pagenum
));
if($hotels && count($hotels)>0){
foreach($hotels as $hotel){

$img=WP_PLUGIN_URL . “/hotel/upload/” .  $hotel->hotel_img;
$new_title= htmlentities(stripslashes($hotel->hotel_name));
$location= htmlentities(stripslashes($hotel->hotel_location));
$website=$hotel->hotel_url;
$desc= substr(htmlentities(strip_tags(stripslashes($hotel->hotel_desc))),0,70);
?>

<div>
<div>

<?php if(!empty($img)) { ?>
<img src=”<?php bloginfo(‘template_url’); ?>/image.php?image=<?php echo $img ?>&width=50″ alt=”<?php echo $new_title ?>” />
<?php } ?>

</div>
<div>
<p><a href=”<?php echo get_permalink(); ?>&hotel_id=<?php echo $hotel->hotel_id?>”><?php echo $new_title?></a></p>
<?php
$gmap_url = get_page_by_title(“Google Map”);
$cur_page_id = get_the_ID();
?>
<p><?php echo $location?>
<span><a href=”?page_id=<?php echo $gmap_url->ID;?>&rpage_id=<?php echo $cur_page_id;?>&Etype=hotel&hotel_id=<?php echo $hotel->hotel_id?>”>View Map    </a></span>
</p>
<p><a href=”<?php echo $website?>” target=”_blank”><?php echo $website?></a></p>
<p><?php echo substr(strip_tags($desc),0,70)?>.. <a href=”<?php echo get_permalink(); ?>&hotel_id=<?php echo $hotel->hotel_id?>”>Read More &raquo;</a></p>
</div>
</div>

<?php
}
}?>
<?php

if($page_links){ ?>
<div>
<?php

if($_REQUEST[‘paged’]==1 || $_REQUEST[‘paged’]==0){

echo ‘<a href=”javascript:void(0)”>&nbsp;</a>’;
}

echo $page_links;
if($_REQUEST[‘paged’]==$num_pages){
echo ‘<a href=”javascript:void(0)”>&nbsp;</a>’;
}
?>

</div>
<?php } ?>

<?php  }

}

?>
////////////////////////////////////////////

hotal.js

function validateHotel(){

if(document.getElementById(‘hotel-name’).value==”){
alert(“Please enter hotel name”);
document.getElementById(‘hotel-name’).focus();
return false;
}

if(document.getElementById(‘hotel-location’).value==”){
alert(“Please enter hotel location”);
document.getElementById(‘hotel-location’).focus();
return false;
}

}
function checkall(objForm){

len = objForm.elements.length;

var i=0;

for( i=0 ; i<len ; i++) {

if (objForm.elements[i].type==’checkbox’) {

objForm.elements[i].checked=objForm.check_all.checked;

}

}

}

Leave a comment