Wordpress Vacancy Plugin
From AshWiki
This page is about the plugin. It is a standard wordpress widget plugin written using the WP_Widget class introduced in Wordpress 2.8. I started with this tutorial.
The contents of the file is below and should be considered as in the public domain.
<?php /* Plugin Name: ASH Co-op Vacancies Plugin URI: http://www.ash.coop/wiki/WordPressVacanciesPlugin Description: A widget for the sidebar that displays the current vacancies at ASH Co-op Version: 0.1 Author: Hamish Downer Author URI: http://www.ash.coop/ */ /* Add our function to the widgets_init hook. */ add_action( 'widgets_init', 'vacancy_load_widgets' ); /* Function that registers our widget. */ function vacancy_load_widgets() { register_widget( 'Vacancy_Widget' ); } function cmpVacancyArray ($a, $b) { if ($a['date'] == $b['date']) { return 0; } return ($a['date'] < $b['date']) ? -1 : 1; } class Vacancy_Widget extends WP_Widget { var $MAX_VACANCIES = 5; // addresses and type of residence var $addresses = array ( 'None', '2 Argyle', '2a Argyle', '4 Argyle', '6 Argyle', '8 Argyle', '10 Argyle', '14 Argyle', '16 Argyle', '18 Argyle', '20 Argyle', '22 Argyle', '24 Argyle', '26 Argyle', '1 Swanns', '2 Swanns', '3 Swanns', '4 Swanns', '5 Swanns', '6 Swanns', '7 Swanns', '1 Fletchers', '2 Fletchers' ); var $house_type = array ( 'None' => 'None', '2 Argyle' => '6 person', '2a Argyle' => '6 person', '4 Argyle' => '4 person', '6 Argyle' => '10 person', '8 Argyle' => '6 person', '10 Argyle' => 'House', '14 Argyle' => 'Flat', '16 Argyle' => 'Flat', '18 Argyle' => 'Flat', '20 Argyle' => 'Flat', '22 Argyle' => '10 person', '24 Argyle' => 'Flat', '26 Argyle' => 'Flat', '1 Swanns' => 'Flat', '2 Swanns' => 'Flat', '3 Swanns' => '6 person', '4 Swanns' => '6 person', '5 Swanns' => '6 person', '6 Swanns' => '4 person', '7 Swanns' => '4 person', '1 Fletchers' => '4 person', '2 Fletchers' => '4 person' ); var $short_months = array ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'); function Vacancy_Widget() { /* Widget settings. */ $widget_ops = array( 'classname' => 'vacancy', 'description' => 'Displays vacancies at the co-op'); /* Widget control settings. */ $control_ops = array( 'width' => 275, 'height' => 350, 'id_base' => 'vacancy-widget' ); /* Create the widget. */ $this->WP_Widget( 'vacancy-widget', 'Vacancy Widget', $widget_ops, $control_ops ); } function instanceToArray ( $instance ) { $vacancyArray = array(); for ($i = 1; $i <= $this->MAX_VACANCIES; $i++) { if (isset($instance['address'.$i]) && 'None' != $instance['address'.$i]) { $vacancydate = strtotime($instance['day'.$i] .' '. $instance['month'.$i] .' '. $instance['year'.$i]); $vacancyArray[] = array ('address' => $instance['address'.$i], 'date' => $vacancydate); } } // sort by date usort ($vacancyArray, 'cmpVacancyArray'); return $vacancyArray; } function widget( $args, $instance ) { extract( $args ); /* User-selected settings. */ $title = apply_filters('widget_title', $instance['title'] ); $last_update = $instance['last_update']; $vacancyArray = $this->instanceToArray($instance); /* Before widget (defined by themes). */ echo $before_widget; /* Title of widget (before and after defined by themes). */ if ( $title ) echo $before_title . $title . $after_title; // show when last updated echo '<p>Last Updated: ' . $last_update . '</p>'; if (count($vacancyArray) == 0) { echo '<p>No vacancies at present.</p>'; } else { echo "<table><tr><th>Address</th><th>Type</th><th>Available</th></tr>"; foreach ($vacancyArray as $vacancy) { // show each vacancy - address, type, when echo '<tr><td>' . $vacancy['address'] . '</td>'; echo '<td>' . $this->house_type[$vacancy['address']] . '</td>'; echo '<td>' . date('j M', $vacancy['date']) . '</td></tr>'; } echo '</table>'; } /* After widget (defined by themes). */ echo $after_widget; } function update( $new_instance, $old_instance ) { $instance = $old_instance; /* Strip tags (if needed) and update the widget settings. */ $instance['title'] = strip_tags( $new_instance['title'] ); for ($i = 1; $i <= $this->MAX_VACANCIES; $i++) { $instance['address'.$i] = $new_instance['address'.$i]; $instance['day'.$i] = $new_instance['day'.$i]; $instance['month'.$i] = $new_instance['month'.$i]; $instance['year'.$i] = $new_instance['year'.$i]; } $instance['last_update'] = date('d-m-Y'); return $instance; } function form( $instance ) { /* Set up some default widget settings. */ $defaults = array( 'title' => 'Current Vacancies', 'address1' => '4 Argyle', 'day1' => 11, 'month1' => 'Jan', 'year1' => '2010'); $instance = wp_parse_args( (array) $instance, $defaults ); ?> <p> <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label> <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" /> </p> <p>To cancel a void, just set the address to 'None'. Voids will be displayed in date order. <a href="http://www.ash.coop/wiki/Site_Admin_Help">Admin Help</a>.</p> <?php for ($i = 1; $i <= $this->MAX_VACANCIES; $i++) { ?> <p> <label for="<?php echo $this->get_field_id( 'address'.$i ); ?>">Address:</label> <select id="<?php echo $this->get_field_id( 'address'.$i ); ?>" name="<?php echo $this->get_field_name( 'address'.$i ); ?>" value="<?php echo $instance['address'.$i]; ?>" style="width:100%;" > <?php foreach ($this->addresses as $address) { if ($address == $instance['address'.$i]) { echo '<option selected="selected">' . $address . '</option>'; } else { echo '<option>' . $address . '</option>'; } } ?> </select> </p> <p> <label for="<?php echo $this->get_field_id( 'day'.$i ); ?>">Day:</label> <select id="<?php echo $this->get_field_id( 'day'.$i ); ?>" name="<?php echo $this->get_field_name( 'day'.$i ); ?>" value="<?php echo $instance['day'.$i]; ?>" style="width:4em;" > <option></option> <?php for ($day = 1; $day <= 31; $day++) { if ($day == $instance['day'.$i]) { echo '<option selected="selected">' . $day . '</option>'; } else { echo '<option>' . $day . '</option>'; } } ?> </select> <label for="<?php echo $this->get_field_id( 'month'.$i ); ?>">Month:</label> <select id="<?php echo $this->get_field_id( 'month'.$i ); ?>" name="<?php echo $this->get_field_name( 'month'.$i ); ?>" value="<?php echo $instance['month'.$i]; ?>" style="width:4.5em;" > <option></option> <?php foreach ($this->short_months as $month) { if ($month == $instance['month'.$i]) { echo '<option selected="selected">' . $month . '</option>'; } else { echo '<option>' . $month . '</option>'; } } ?> </select> <label for="<?php echo $this->get_field_id( 'year'.$i ); ?>">Year:</label> <select id="<?php echo $this->get_field_id( 'year'.$i ); ?>" name="<?php echo $this->get_field_name( 'year'.$i ); ?>" value="<?php echo $instance['year'.$i]; ?>" style="width:5em;" > <?php $this_year = date('Y'); for ($year = $this_year-1; $year <= $this_year+1; $year++) { if ($year == $instance['year'.$i]) { echo '<option selected="selected">' . $year . '</option>'; } else { echo '<option>' . $year . '</option>'; } } ?> </select> </p> <?php } } } ?>
