cck_time.module in CCK Time 5
Same filename and directory in other branches
File
cck_time.moduleView source
<?php
/*
* Creates a time widget for CCK text fields
*
* TODO: allow seconds granularity (if it seems necessary?)
*
* steve@openconcept.ca
*/
/**
* Declare information about a widget.
*
* @return
* An array keyed by widget name. Each element of the array is an associative
* array with these keys and values:
* - "label": The human-readable label for the widget.
* - "field types": An array of field type names that can be edited using
* this widget.
*/
function cck_time_widget_info() {
return array(
'cck_time' => array(
'label' => 'Time',
'field types' => array(
'text',
),
),
);
}
/**
* Define the behavior of a widget.
*
* @param $op
* What kind of action is being performed. Possible values:
* - "prepare form values": The editing form will be displayed. The widget
* should perform any conversion necessary from the field's native storage
* format into the storage used for the form. Convention dictates that the
* widget's version of the data should be stored beginning with "default".
* - "form": The node is being edited, and a form should be prepared for
* display to the user.
* - "validate": The user has just finished editing the node and is
* trying to preview or submit it. This hook can be used to check or
* even modify the node. Errors should be set with form_set_error().
* - "process form values": The inverse of the prepare operation. The widget
* should convert the data back to the field's native format.
* - "submit": The user has just finished editing the node and the node has
* passed validation. This hook can be used to modify the node.
* @param &$node
* The node the action is being performed on. This argument is passed by
* reference for performance only; do not modify it.
* @param $field
* The field the action is being performed on.
* @param &$node_field
* The contents of the field in this node. Changes to this variable will
* be saved back to the node object.
* @return
* This varies depending on the operation.
* - The "form" operation should return an array of form elements to display.
* - Other operations have no return value.
*/
function cck_time_widget($op, &$node, $field, &$node_field) {
drupal_add_css(drupal_get_path('module', 'cck_time') . '/cck_time.css', 'module', 'screen', FALSE);
$clock = isset($field['widget']['format']) ? intval($field['widget']['format']) : 24;
$increment = isset($field['widget']['increment']) ? intval($field['widget']['increment']) : 1;
switch ($op) {
case 'prepare form values':
list($hour, $minute) = split(':', $node_field[0]['value']);
if ($clock == 12) {
$meridiem = substr($minute, 2, 2);
$minute = substr($minute, 0, 2);
$node_field['default_meridiem'] = $meridiem;
}
$node_field['default_hour'] = $hour;
$node_field['default_minute'] = $minute;
break;
case 'form':
// hours
for ($i = 1; $i <= $clock; $i++) {
if ($clock == 24) {
$val = $i < 10 ? "0{$i}" : $i;
$hours[$val] = $val;
}
else {
$hours[$i] = $i;
}
}
// minutes
for ($i = 0; $i <= 59; $i += $increment) {
$val = $i < 10 ? "0{$i}" : $i;
$minutes[$val] = $val;
}
$form = array();
$form[$field['field_name']] = array(
'#tree' => TRUE,
);
$form[$field['field_name']]['#type'] = 'fieldset';
$form[$field['field_name']]['#attributes'] = array(
'class' => 'cck-time',
);
$form[$field['field_name']]['#title'] = t($field['widget']['label']);
$form[$field['field_name']]['hour'] = array(
'#type' => 'select',
'#title' => t(''),
'#options' => $hours,
'#required' => $field['required'],
'#default_value' => $node_field['default hour'],
'#prefix' => '<div class="cck-time-element">',
'#suffix' => '</div>',
);
$form[$field['field_name']]['minute'] = array(
'#type' => 'select',
'#title' => t(''),
'#options' => $minutes,
'#required' => $field['required'],
'#default_value' => $node_field['default_minute'],
'#prefix' => '<div class="cck-time-element">',
'#suffix' => '</div>',
);
if ($clock == 12) {
$form[$field['field_name']]['meridem'] = array(
'#type' => 'select',
'#title' => t(''),
'#options' => array(
'AM' => 'AM',
'PM' => 'PM',
),
'#required' => $field['required'],
'#default_value' => $node_field['default_meridiem'],
'#prefix' => '<div class="cck-time-element">',
'#suffix' => '</div>',
);
}
return $form;
case 'validate':
if (!is_numeric($node_field['hour']) || $node_field['hour'] < 1 || $node_field['hour'] > $clock) {
form_set_error($field['field_name'], t('Please select an hour from the list.'));
}
if (!is_numeric($node_field['minute']) || $node_field['minute'] < 0 || $node_field['minute'] > 59) {
form_set_error($field['field_name'], t('Please select a minute from the list.'));
}
if ($clock == 12) {
if ($node_field['meridem'] != 'AM' && $node_field['meridem'] != 'PM') {
form_set_error($field['field_name'], t('Please select AM or PM.'));
}
}
break;
case 'process form values':
$node_field[0]['value'] = $node_field['hour'] . ":" . $node_field['minute'];
if ($clock == '12') {
$node_field[0]['value'] .= $node_field['meridem'];
}
break;
}
}
/**
* Implementation of hook_widget_settings().
*
* @param $op
* The operation to be performed.
* @param $widget
* The widget on which the operation is to be performed.
* @return
* This varies depending on the operation.
* - "form": an array of form elements to add to the settings page.
* - "validate": no return value. Use form_set_error().
* - "save": an array of names of form elements to be saved in the database.
* - "callbacks": an array describing the widget's behaviour regarding hook_widget
* operations. The array is keyed by hook_widget operations ('form', 'validate'...)
* and has the following possible values :
* CONTENT_CALLBACK_NONE : do nothing for this operation
* CONTENT_CALLBACK_CUSTOM : use the behaviour in hook_widget(operation)
* CONTENT_CALLBACK_DEFAULT : use content.module's default bahaviour
* Note : currently only the 'default value' operation implements this feature.
* All other widget operation implemented by the module _will_ be executed
* no matter what.
*/
function cck_time_widget_settings($op, $widget) {
switch ($op) {
case 'callbacks':
return array(
'default value' => CONTENT_CALLBACK_CUSTOM,
);
case 'form':
$form = array();
$form['cck_time']['format'] = array(
'#type' => 'select',
'#title' => t('Time format'),
'#default_value' => isset($widget['format']) ? $widget['format'] : 1,
'#options' => array(
'24' => '24-hour (24:59)',
'12' => '12-hour (12:59AM)',
),
'#description' => t('Record times in 24-hour format or in 12-hour format (with AM/PM).'),
);
$form['cck_time']['increment'] = array(
'#type' => 'select',
'#title' => t('Minute increment'),
'#default_value' => isset($widget['increment']) ? $widget['increment'] : 1,
'#options' => array(
1 => 1,
5 => 5,
10 => 10,
15 => 15,
30 => 30,
),
'#description' => t('Increment the minute values by this amount.'),
);
return $form;
case 'validate':
$format_OK = array(
'12',
'24',
);
$increment_OK = array(
'1',
'5',
'10',
'15',
'30',
);
if (!in_array($widget['format'], $format_OK) || !in_array($widget['increment'], $increment_OK)) {
form_set_error('', 'There was a a problem with the time settings. Please check your values.');
}
break;
case 'save':
//cache_clear_all('date_formats:'. $widget['field_name'] .':'. $widget['type_name'], 'cache');
return array(
'increment',
'format',
);
}
}
Functions
Name![]() |
Description |
---|---|
cck_time_widget | Define the behavior of a widget. |
cck_time_widget_info | Declare information about a widget. |
cck_time_widget_settings | Implementation of hook_widget_settings(). |