/[drupal]/contributions/modules/flexinode/field_timestamp.inc
ViewVC logotype

Contents of /contributions/modules/flexinode/field_timestamp.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.12 - (show annotations) (download) (as text)
Sat Apr 29 19:22:56 2006 UTC (3 years, 7 months ago) by ber
Branch: MAIN
CVS Tags: DRUPAL-4-7--0-3, DRUPAL-4-7--0-2, HEAD
Branch point for: DRUPAL-4-7
Changes since 1.11: +2 -2 lines
File MIME type: text/x-php
removed "eh?".
1 <?php
2 // $Id: field_timestamp.inc,v 1.11 2006/04/29 18:32:23 ber Exp $
3
4 function flexinode_field_timestamp_name($field) {
5 return t('date/time');
6 }
7
8 function flexinode_field_timestamp_form($field, $node) {
9 $fieldname = 'flexinode_'. $field->field_id;
10 return flexinode_form_date($node->$fieldname, $field);
11 }
12
13 function flexinode_field_timestamp_db_select($field) {
14 $fieldname = 'flexinode_'. $field->field_id;
15 return $fieldname .'.numeric_data AS '. $fieldname;
16 }
17
18 function flexinode_field_timestamp_db_sort_column($field) {
19 return 'flexinode_'. $field->field_id .'.numeric_data';
20 }
21
22 function flexinode_field_timestamp_insert($field, $node) {
23 $fieldname = 'flexinode_'. $field->field_id;
24 $stamp = flexinode_validate_date($node, $fieldname);
25 db_query('INSERT INTO {flexinode_data} (nid, field_id, numeric_data) VALUES (%d, %d, %d)', $node->nid, $field->field_id, $stamp);
26 }
27
28 function flexinode_field_timestamp_format($field, $node, $brief = 0) {
29 $fieldname = 'flexinode_'. $field->field_id;
30 return format_date($node->$fieldname);
31 }
32
33
34 /**
35 * A copy of event_form_date().
36 */
37 function flexinode_form_date($timestamp, $field) {
38 $prefix = 'flexinode_'. $field->field_id;
39
40 //determine settings for form's hour selector
41 if (variable_get('event_ampm', '0')) {
42 $hour_format = t('g');
43 $first_hour = 1;
44 $last_hour = 12;
45 }
46 else {
47 $hour_format = t('H');
48 $first_hour = 0;
49 $last_hour = 23;
50 }
51
52 if(!$timestamp) {
53 $timestamp = time();
54 // Round to nearest hour:
55 $timestamp -= $timestamp % (60 * 60);
56 }
57
58 $months = array(1 => t('January'), t('February'), t('March'), t('April'), t('May'), t('June'), t('July'), t('August'), t('September'), t('October'), t('November'), t('December'));
59 for ($i = 1; $i <= 31; $i++) $days[$i] = $i;
60 for ($i = $first_hour; $i <= $last_hour; $i++) $hours[$i] = $i;
61 for ($i = 0; $i <= 59; $i++) $minutes[$i] = $i < 10 ? "0$i" : $i;
62 $am_pms = array('am' => t('am'), 'pm' => t('pm'));
63
64 // Use format_date(), it handles user timezone and locale.
65 $year = format_date($timestamp, 'custom', 'Y');
66 $month = format_date($timestamp, 'custom', 'm');
67 $day = format_date($timestamp, 'custom', 'd');
68 $hour = format_date($timestamp, 'custom', $hour_format);
69 $minute = format_date($timestamp, 'custom', 'i');
70 $am_pm = format_date($timestamp, 'custom', 'a');
71
72 $form[$prefix]['#type'] = 'fieldset';
73 $form[$prefix]['#title'] = $field->label;
74 $form[$prefix]['#weight'] = $field->weight;
75 $form[$prefix]['#prefix'] = '<div class="container-inline">';
76 $form[$prefix]['#suffix'] = '</div>';
77 $form[$prefix][$prefix .'_day'] = array(
78 '#type' => 'textfield',
79 '#default_value' => $day,
80 '#maxlength' => 2,
81 '#size' => 2,
82 '#description' => t('day'),
83 '#required' => TRUE,
84 );
85 $form[$prefix][$prefix .'_month'] = array(
86 '#type' => 'select',
87 '#default_value' => $month,
88 '#options' => $months,
89 '#description' => t('month'),
90 '#required' => TRUE,
91 );
92 $form[$prefix][$prefix .'_year'] = array(
93 '#type' => 'textfield',
94 '#default_value' => $year,
95 '#maxlength' => 4,
96 '#size' => 4,
97 '#description' => t('year'),
98 '#required' => TRUE,
99 );
100 $form[$prefix][$prefix .'_hour'] = array(
101 '#prefix' => '',
102 '#type' => 'select',
103 '#default_value' => $hour,
104 '#options' => $hours,
105 '#required' => TRUE,
106 '#description' => t('hour'),
107 );
108 $form[$prefix][$prefix .'_minute'] = array(
109 '#prefix' => ':',
110 '#type' => 'select',
111 '#default_value' => $minute,
112 '#options' => $minutes,
113 '#required' => TRUE,
114 '#description' => t('minutes'),
115 );
116 if (variable_get('event_ampm', '0')) {
117 $form[$prefix][$prefix .'_ampm'] = array(
118 '#type' => 'radios',
119 '#default_value' => $am_pm,
120 '#options' => $am_pms,
121 '#required' => TRUE,
122 );
123 }
124
125 return $form;
126 }
127
128
129 function flexinode_validate_date(&$node, $prefix = '') {
130 if (isset($node->{$prefix . '_year'}) && isset($node->{$prefix . '_month'}) && isset($node->{$prefix . '_day'}) && isset($node->{$prefix . '_hour'}) && isset($node->{$prefix . '_minute'})) {
131 $hour = $node->{$prefix . '_hour'};
132 if (variable_get('event_ampm', '0')) {
133 if ($node->{$prefix . '_ampm'} == 'pm') {
134 $hour += 12;
135 }
136 elseif ($hour == 12) {
137 $hour -= 12;
138 }
139 }
140 $result = gmmktime($hour, $node->{$prefix . '_minute'}, 0, $node->{$prefix . '_month'}, $node->{$prefix . '_day'}, $node->{$prefix . '_year'}) - $GLOBALS['user']->timezone;
141 }
142 else if (isset($node->$prefix)) {
143 $result = $node->$prefix;
144 }
145
146 return $result;
147 }
148
149
150 /**
151 * @addtogroup themeable
152 * @{
153 */
154
155 /**
156 * Format a timestamp for display in a node.
157 *
158 * @param field_id
159 * Which field is being displayed (useful when overriding this function
160 * if you want to style one particular field differently).
161 * @param label
162 * The label for the field as displayed on the node form.
163 * @param value
164 * The value that the user entered for the field.
165 * @param formatted_value
166 * The value that the user entered for the field as pre-formatted by the module.
167 */
168 function theme_flexinode_timestamp($field_id, $label, $value, $formatted_value) {
169 $output = theme('form_element', $label, $formatted_value);
170 $output = '<div class="flexinode-timestamp-'. $field_id .'">'. $output .'</div>';
171 return $output;
172 }
173
174 /** @} End of addtogroup themeable */

  ViewVC Help
Powered by ViewVC 1.1.2