office_hours.module in Office Hours 7
Same filename and directory in other branches
Creates a field and widget for inserting working or office hours per day
File
office_hours.moduleView source
<?php
/**
* @file
* Creates a field and widget for inserting working or office hours per day
*/
// @TODO 2012-05-13: it would be nice to include this file in the .info-file, but the OH-block is lost after F5-page refresh.
// @TODO see http://nodesforbreakfast.com/article/2012/02/20/organize-your-module-code-hookhookinfo-drupal-7
// @todo for 'multiple blocks per day', see code with string 'multiple blocks per day'.
module_load_include('inc', 'office_hours', 'includes/office_hours.elements');
module_load_include('inc', 'office_hours', 'includes/office_hours.field');
module_load_include('inc', 'office_hours', 'includes/office_hours.formatter');
module_load_include('inc', 'office_hours', 'includes/office_hours.widget');
/**
* Implements hook_theme().
*/
function office_hours_theme($existing, $type, $theme, $path) {
$base = array(
'render element' => 'element',
'file' => 'includes/office_hours.theme.inc',
'path' => "{$path}",
);
$themes = array(
// Formatter theming functions.
'office_hours_field_formatter_default' => $base,
'office_hours_time_range' => $base + array(),
// Widget theming functions.
'office_hours_week' => $base + array(),
'office_hours_block' => $base + array(),
'office_hours_select' => $base + array(),
);
return $themes;
}
/**
* Implements FAPI hook_element_info().
*/
function office_hours_element_info() {
return array(
'office_hours_block' => array(
'#input' => TRUE,
'#process' => array(
'_office_hours_block_process',
),
'#element_validate' => array(
'_office_hours_block_validate',
),
'#tree' => TRUE,
'#columns' => array(
'day',
'starthours',
'endhours',
'comment',
),
'#theme' => 'office_hours_block',
'#attached' => array(
// 'css' => array(
// drupal_get_path('module', 'office_hours') . '/office_hours.css',
// ),
'js' => array(
drupal_get_path('module', 'office_hours') . '/js/office_hours.widget.js',
),
),
),
'office_hours_select' => array(
'#input' => TRUE,
'#process' => array(
'_office_hours_select_process',
),
'#element_validate' => array(
'_office_hours_select_validate',
),
'#tree' => TRUE,
'#theme' => 'office_hours_select',
'#value_callback' => '_office_hours_select_value_callback',
),
'office_hours_comment' => array(
'#input' => TRUE,
'#process' => array(
'_office_hours_comment_process',
),
'#element_validate' => array(
'_office_hours_comment_validate',
),
),
);
}
/**
* Implements hook_views_api().
*/
function office_hours_views_api() {
return array(
'api' => 3,
);
}
/**
* Helper function to convert a range of hours from '13' to '13:00 (01:00 pm)'.
*
* This is used in the Widget settings form.
*/
function _office_hours_show_ampm($hours) {
foreach ($hours as $key => $hour) {
if (!empty($hour)) {
$hours[$key] = _office_hours_time_to_24hr($hour . '00') . ' (' . _office_hours_convert_to_ampm($hour . '00') . ')';
}
}
return $hours;
}
/**
* Gets the (limited) hours of a day.
*
* Mimics date_hours() function, but that function does not support limiting
* the hours. The limits are set in the Widget settings form, and used in the
* Widget form.
*/
function _office_hours_field_widget_hours($settings) {
// Get the valid hours. Date API doesn't provide a straight method for this.
$hoursformat = $settings['hoursformat'] == 1 ? 'g' : 'H';
// 12 or 24 format.
$required = FALSE;
$start = $settings['limitstart'] == '' ? 0 : max(0, $settings['limitstart']);
$start = (int) $start / 100;
$end = $settings['limitend'] == '' ? 2300 : min(2300, $settings['limitend']);
$end = (int) $end / 100;
// Begin modified copy from date_hours().
$hours = array();
if ($hoursformat == 'h' || $hoursformat == 'g') {
// 12-hour format.
$min = 1;
$max = 24;
for ($i = $min; $i <= $max; $i++) {
if ($i >= $start && $i <= $end || $end - $start >= 11) {
$hour = $i <= 12 ? $i : $i - 12;
$hours[$hour * 100] = $hour < 10 && ($hoursformat == 'H' || $hoursformat == 'h') ? "0{$hour}" : $hour;
}
}
$hours = array_unique($hours);
}
else {
$min = $start;
$max = $end;
for ($i = $min; $i <= $max; $i++) {
$hour = $i;
$hours[$hour * 100] = $hour < 10 && ($hoursformat == 'H' || $hoursformat == 'h') ? "0{$hour}" : $hour;
}
}
$none = array(
'' => '',
);
$hours = !$required ? $none + $hours : $hours;
// End modified copy from date_hours().
return $hours;
}
/**
* Helper function to convert a time to a given format.
*
* For formatting options, see http://www.php.net/manual/en/function.date.php
*/
function _office_hours_time_format($time, $timeformat) {
$date = new DateTime(_office_hours_time_to_24hr($time));
return $date
->format($timeformat);
}
/**
* Helper function to convert '1630' or '16:30' to '4:30 pm'.
*
* For formatting options, see http://www.php.net/manual/en/function.date.php
*/
function _office_hours_convert_to_ampm($time, $timeformat = 'g:i a') {
return _office_hours_time_format($time, $timeformat);
}
/*
* Helper function to convert '08:00' / '8:00' / '800' to '0800'
*/
function _office_hours_time_to_mil($time) {
if (is_null($time)) {
return $time;
}
$time = str_replace(':', '', $time);
$time = substr('0000' . $time, -4);
return $time;
}
/*
* Helper function to convert '800' or '0800' to '08:00'
*/
function _office_hours_time_to_24hr($time = '') {
if (strstr($time, ':')) {
return $time;
}
else {
$time = substr('0000' . $time, -4);
$hour = substr($time, 0, -2);
$min = substr($time, -2);
return $hour . ':' . $min;
}
}
/*
* Helper function to convert a time to its 3 elements: hour, minute, ampm.
* Mimics the date_parse() function.
* g = 12-hour format of an hour without leading zeros 1 through 12
* G = 24-hour format of an hour without leading zeros 0 through 23
* h = 12-hour format of an hour with leading zeros 01 through 12
* H = 24-hour format of an hour with leading zeros 00 through 23
*/
function _office_hours_field_widget_time_parse($time, $hoursformat) {
$hour = '';
$min = '';
$ampm = 'am';
if (is_numeric($time)) {
$timeformat = $hoursformat == 1 ? 'g:i:a' : 'H:i:a';
// 12hrs or 24hrs
$time = _office_hours_time_format($time, $timeformat);
list($hour, $min, $ampm) = explode(':', $time);
$hour = $hour * 100;
}
$result = array(
'hour' => $hour,
'minute' => $min,
'ampm' => $ampm,
);
return $result;
}
/**
* Reorders weekdays to match the first day of the week.
* Copied and modified from date_week_days_ordered().
*
* @param array $weekdays
* An array of weekdays.
*
* @return array
* An array of weekdays reordered to match the first day of the week.
*/
function _office_hours_week_days_ordered($weekdays, $first_day) {
// $first_day = variable_get('date_first_day', 0);
if ($first_day > 0) {
for ($i = 1; $i <= $first_day; $i++) {
$last = array_shift($weekdays);
array_push($weekdays, $last);
}
}
return $weekdays;
}
Functions
Name | Description |
---|---|
office_hours_element_info | Implements FAPI hook_element_info(). |
office_hours_theme | Implements hook_theme(). |
office_hours_views_api | Implements hook_views_api(). |
_office_hours_convert_to_ampm | Helper function to convert '1630' or '16:30' to '4:30 pm'. |
_office_hours_field_widget_hours | Gets the (limited) hours of a day. |
_office_hours_field_widget_time_parse | |
_office_hours_show_ampm | Helper function to convert a range of hours from '13' to '13:00 (01:00 pm)'. |
_office_hours_time_format | Helper function to convert a time to a given format. |
_office_hours_time_to_24hr | |
_office_hours_time_to_mil | |
_office_hours_week_days_ordered | Reorders weekdays to match the first day of the week. Copied and modified from date_week_days_ordered(). |