| 1 |
<?php
|
| 2 |
// $Id: calendar.module,v 1.121.2.33 2008/11/25 16:12:36 karens Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_views_api().
|
| 6 |
*
|
| 7 |
* This one is used as the base to reduce errors when updating.
|
| 8 |
*/
|
| 9 |
function calendar_views_api() {
|
| 10 |
return array(
|
| 11 |
'api' => 2,
|
| 12 |
'path' => drupal_get_path('module', 'calendar') .'/includes',
|
| 13 |
);
|
| 14 |
}
|
| 15 |
|
| 16 |
/**
|
| 17 |
* @file
|
| 18 |
* Adds calendar filtering and displays to Views.
|
| 19 |
*/
|
| 20 |
/**
|
| 21 |
* Implementation of hook_help().
|
| 22 |
*/
|
| 23 |
function calendar_help($section, $arg) {
|
| 24 |
switch ($section) {
|
| 25 |
case 'admin/help#calendar':
|
| 26 |
return t("<p>View complete documentation at !link.</p>", array('!link' => 'http://drupal.org/node/120710'));
|
| 27 |
}
|
| 28 |
}
|
| 29 |
|
| 30 |
function calendar_init() {
|
| 31 |
drupal_add_css(drupal_get_path('module', 'calendar') .'/calendar.css');
|
| 32 |
// The css for Farbtastic color picker, painless to add it here
|
| 33 |
// even though it isn't needed everywhere.
|
| 34 |
drupal_add_css('misc/farbtastic/farbtastic.css');
|
| 35 |
|
| 36 |
require_once('./'. drupal_get_path('module', 'calendar') .'/theme/theme.inc');
|
| 37 |
}
|
| 38 |
|
| 39 |
function calendar_theme() {
|
| 40 |
$path = drupal_get_path('module', 'calendar');
|
| 41 |
require_once "./$path/theme/theme.inc";
|
| 42 |
|
| 43 |
$base = array(
|
| 44 |
'file' => 'theme.inc',
|
| 45 |
'path' => "$path/theme",
|
| 46 |
);
|
| 47 |
return array(
|
| 48 |
'calendar_day_node' => $base + array(
|
| 49 |
'template' => 'calendar-day-node',
|
| 50 |
'arguments' => array('node' => NULL, 'view' => NULL),
|
| 51 |
),
|
| 52 |
'calendar_month_node' => $base + array(
|
| 53 |
'template' => 'calendar-month-node',
|
| 54 |
'arguments' => array('node' => NULL, 'view' => NULL),
|
| 55 |
),
|
| 56 |
'calendar_week_node' => $base + array(
|
| 57 |
'template' => 'calendar-week-node',
|
| 58 |
'arguments' => array('node' => NULL, 'view' => NULL),
|
| 59 |
),
|
| 60 |
'calendar_month_multiple_node' => $base + array(
|
| 61 |
'template' => 'calendar-month-multiple-node',
|
| 62 |
'arguments' => array('curday' => NULL, 'count' => NULL, 'view' => NULL, 'types' => NULL),
|
| 63 |
),
|
| 64 |
'calendar_week_multiple_node' => $base + array(
|
| 65 |
'template' => 'calendar-week-multiple-node',
|
| 66 |
'arguments' => array('curday' => NULL, 'count' => NULL, 'view' => NULL, 'types' => NULL),
|
| 67 |
),
|
| 68 |
'calendar_datebox' => $base + array(
|
| 69 |
'template' => 'calendar-datebox',
|
| 70 |
'arguments' => array(
|
| 71 |
'date' => NULL, 'view' => NULL, 'items' => NULL, 'selected' => NULL),
|
| 72 |
),
|
| 73 |
'calendar_title' => $base + array(
|
| 74 |
'arguments' => array('date_type' => NULL, 'view' => NULL),
|
| 75 |
),
|
| 76 |
'calendar_date_combo' => $base + array(
|
| 77 |
'arguments' => array('node', 'lable', 'view'),
|
| 78 |
),
|
| 79 |
'calendar_empty_day' => $base + array(
|
| 80 |
'arguments' => array('curday', 'view'),
|
| 81 |
),
|
| 82 |
'calendar_stripe_legend' => $base + array(
|
| 83 |
'arguments' => array('stripe_labels'),
|
| 84 |
),
|
| 85 |
'calendar_stripe_stripe' => $base + array(
|
| 86 |
'arguments' => array('node'),
|
| 87 |
),
|
| 88 |
'calendar_colorpicker' => $base + array(
|
| 89 |
'arguments' => array('element'),
|
| 90 |
),
|
| 91 |
'calendar_colorfield' => $base + array(
|
| 92 |
'arguments' => array('element'),
|
| 93 |
),
|
| 94 |
'calendar_time_row_heading' => $base + array(
|
| 95 |
'arguments' => array('start_time', 'next_start_time', 'curday_date'),
|
| 96 |
),
|
| 97 |
);
|
| 98 |
}
|
| 99 |
|
| 100 |
/**
|
| 101 |
* TODO need to identify type of timezone handling needed for each date field
|
| 102 |
*/
|
| 103 |
function calendar_offset($field_name) {
|
| 104 |
$default_offset = variable_get('date_default_timezone', 0);
|
| 105 |
$configurable_zones = variable_get('configurable_timezones', 1);
|
| 106 |
}
|
| 107 |
|
| 108 |
/**
|
| 109 |
* A function to test the validity of various date parts
|
| 110 |
*/
|
| 111 |
function calendar_part_is_valid($value, $type) {
|
| 112 |
if ( !preg_match('/^[0-9]*$/', $value) ) {
|
| 113 |
return false;
|
| 114 |
}
|
| 115 |
$value = intval($value);
|
| 116 |
if ($value <= 0) return false;
|
| 117 |
switch ($type) {
|
| 118 |
case 'year':
|
| 119 |
if ($value < DATE_MIN_YEAR) return false;
|
| 120 |
break;
|
| 121 |
case 'month':
|
| 122 |
if ($value < 0 || $value > 12) return false;
|
| 123 |
break;
|
| 124 |
case 'day':
|
| 125 |
if ($value < 0 || $value > 31) return false;
|
| 126 |
break;
|
| 127 |
case 'week':
|
| 128 |
if ($value < 0 || $value > 53) return false;
|
| 129 |
}
|
| 130 |
return true;
|
| 131 |
}
|
| 132 |
|
| 133 |
/**
|
| 134 |
* implementation of hook_block()
|
| 135 |
*/
|
| 136 |
function calendar_block($op = 'list', $delta = 0) {
|
| 137 |
switch ($op) {
|
| 138 |
case 'list' :
|
| 139 |
$blocks[0]['info'] = t('Calendar Legend.');
|
| 140 |
return $blocks;
|
| 141 |
break;
|
| 142 |
case 'view' :
|
| 143 |
switch ($delta) {
|
| 144 |
case 0:
|
| 145 |
$block['subject'] = t('Calendar Legend');
|
| 146 |
$content = theme('calendar_stripe_legend');
|
| 147 |
$block['content'] = !empty($content) ? '<div class="calendar legend">'. $content .'</div>' : '';
|
| 148 |
return $block;
|
| 149 |
}
|
| 150 |
}
|
| 151 |
}
|
| 152 |
|
| 153 |
/**
|
| 154 |
* Calendar display types
|
| 155 |
*/
|
| 156 |
function calendar_display_types() {
|
| 157 |
return array('year' => t('Year'), 'month' => t('Month'), 'day' => t('Day'), 'week' => t('Week'));
|
| 158 |
}
|
| 159 |
|
| 160 |
/**
|
| 161 |
* Figure out which type of display to use,
|
| 162 |
* based on the current argument.
|
| 163 |
*
|
| 164 |
* @return year, month, day, or week.
|
| 165 |
*/
|
| 166 |
function calendar_current_type($view) {
|
| 167 |
if (!is_object($view) || !isset($view->argument) || !is_array($view->argument)) {
|
| 168 |
if (!empty($view->date_info->default_display)) {
|
| 169 |
return $view->date_info->default_display;
|
| 170 |
}
|
| 171 |
return FALSE;
|
| 172 |
}
|
| 173 |
$i = 0;
|
| 174 |
$date_handler = new date_sql_handler();
|
| 175 |
foreach ($view->argument as $argument) {
|
| 176 |
if ($argument['id'] == 'date_argument') {
|
| 177 |
$parts = array_keys($date_handler->arg_parts($view->args[$i]));
|
| 178 |
break;
|
| 179 |
}
|
| 180 |
$i++;
|
| 181 |
}
|
| 182 |
return array_pop($parts);
|
| 183 |
}
|
| 184 |
|
| 185 |
/**
|
| 186 |
* Create a stripe.
|
| 187 |
*
|
| 188 |
* @param $node - the node object
|
| 189 |
* @param $query_name - the views queryname for this date field
|
| 190 |
* @param $delta - the delta for this field, used to distinguish fields that appear more than once in the calendar
|
| 191 |
* @param $stripe - the hex code for this stripe.
|
| 192 |
* @param $label - the label to give this stripe.
|
| 193 |
*
|
| 194 |
* TODO Reconsider use of $GLOBALS as a method of triggering the legend, there
|
| 195 |
* may be a better way.
|
| 196 |
*/
|
| 197 |
function calendar_node_stripe($view, &$node, $query_name, $delta, $stripe = NULL, $label = '') {
|
| 198 |
$colors = isset($view->date_info->calendar_colors) ? $view->date_info->calendar_colors : array();
|
| 199 |
if (empty($colors)) {
|
| 200 |
return;
|
| 201 |
}
|
| 202 |
|
| 203 |
$type_names = node_get_types('names');
|
| 204 |
$type = $node->raw->node_type;
|
| 205 |
if(!(isset($node->stripe))){
|
| 206 |
$node->stripe = array();
|
| 207 |
$node->stripe_label = array();
|
| 208 |
}
|
| 209 |
if (!$label && array_key_exists($type, $type_names)) {
|
| 210 |
$label = $type_names[$type];
|
| 211 |
}
|
| 212 |
if (!$stripe) {
|
| 213 |
if (array_key_exists($type, $colors)) {
|
| 214 |
$stripe = $colors[$type];
|
| 215 |
}
|
| 216 |
else {
|
| 217 |
$stripe = '';
|
| 218 |
}
|
| 219 |
}
|
| 220 |
|
| 221 |
$node->stripe[] = $stripe;
|
| 222 |
$node->stripe_label[] = $label;
|
| 223 |
$GLOBALS['calendar_stripe_labels'][][$type] = array('stripe' => $stripe, 'label' => $label);
|
| 224 |
return $stripe;
|
| 225 |
}
|
| 226 |
|
| 227 |
/**
|
| 228 |
* Create a stripe based on a taxonomy term.
|
| 229 |
*
|
| 230 |
* @param $node - the node object
|
| 231 |
* @param $query_name - the views queryname for this date field
|
| 232 |
* @param $delta - the delta for this field, used to distinguish fields that appear more than once in the calendar
|
| 233 |
* @param $stripe - the hex code for this stripe.
|
| 234 |
* @param $label - the label to give this stripe.
|
| 235 |
*
|
| 236 |
* TODO Reconsider use of $GLOBALS as a method of triggering the legend, there
|
| 237 |
* may be a better way.
|
| 238 |
*/
|
| 239 |
|
| 240 |
function calendar_node_taxonomy_stripe($view, &$node, $query_name, $delta, $stripe = NULL, $label = '') {
|
| 241 |
$colors_taxonomy = isset($view->date_info->calendar_colors_taxonomy) ? $view->date_info->calendar_colors_taxonomy : array();
|
| 242 |
if (empty($colors_taxonomy)) {
|
| 243 |
return;
|
| 244 |
}
|
| 245 |
|
| 246 |
// Rename the vid added by Views to the normal name that
|
| 247 |
// taxonomy will expect, it's in the raw results.
|
| 248 |
$node->vid = $node->raw->node_vid;
|
| 249 |
$terms_for_node = taxonomy_node_get_terms($node);
|
| 250 |
if(!(isset($node->stripe))){
|
| 251 |
$node->stripe = array();
|
| 252 |
$node->stripe_label = array();
|
| 253 |
}
|
| 254 |
if (count($terms_for_node)){
|
| 255 |
foreach($terms_for_node as $term_for_node){
|
| 256 |
if (!array_key_exists($term_for_node->tid, $colors_taxonomy)) {
|
| 257 |
continue;
|
| 258 |
}
|
| 259 |
$stripe = $colors_taxonomy[$term_for_node->tid];
|
| 260 |
$stripe_label = $term_for_node->name;
|
| 261 |
$node->stripe[] = $stripe;
|
| 262 |
$node->stripe_label[] = $stripe_label;
|
| 263 |
$GLOBALS['calendar_stripe_labels'][][$term_for_node->tid] = array('stripe' => $stripe, 'label' => $stripe_label);
|
| 264 |
}
|
| 265 |
}
|
| 266 |
else {
|
| 267 |
$node->stripe[] = '';
|
| 268 |
$node->stripe_label[] = '';
|
| 269 |
}
|
| 270 |
return;
|
| 271 |
}
|
| 272 |
|
| 273 |
|
| 274 |
/**
|
| 275 |
* Helper function to figure out a group gid to use in blocks.
|
| 276 |
*
|
| 277 |
* @return an array of group nodes that are relevant.
|
| 278 |
* @todo this may need more work.
|
| 279 |
*/
|
| 280 |
function calendar_og_groups($view) {
|
| 281 |
if (!$groupnode = og_get_group_context()) {
|
| 282 |
global $user;
|
| 283 |
$groupnodes = array_keys($user->og_groups);
|
| 284 |
}
|
| 285 |
else {
|
| 286 |
$groupnodes = array($groupnode->nid);
|
| 287 |
}
|
| 288 |
return $groupnodes;
|
| 289 |
}
|
| 290 |
|
| 291 |
/**
|
| 292 |
* A selector to jump to a new date in the calendar.
|
| 293 |
*
|
| 294 |
* @param unknown_type $view
|
| 295 |
* @return unknown
|
| 296 |
*/
|
| 297 |
function calendar_date_select($view) {
|
| 298 |
return '<div class="calendar-date-select">'. drupal_get_form('calendar_date_select_form', $view) .'</div>';
|
| 299 |
}
|
| 300 |
|
| 301 |
/**
|
| 302 |
* The date selector form.
|
| 303 |
*
|
| 304 |
* @param object $view
|
| 305 |
* @return the form element
|
| 306 |
*
|
| 307 |
* @TODO is the title desired here or does it take up too much space??
|
| 308 |
*/
|
| 309 |
function calendar_date_select_form(&$form_state, $view) {
|
| 310 |
$format = date_limit_format(variable_get('date_format_short', 'm/d/Y - H:i'), array('year', 'month', 'day'));
|
| 311 |
$form['calendar_goto'] = array(
|
| 312 |
//'#title' => t('Calendar date'),
|
| 313 |
'#type' => module_exists('date_popup') ? 'date_popup' : 'date_select',
|
| 314 |
'#default_value' => date_format($view->date_info->min_date, 'Y-m-d'),
|
| 315 |
'#date_timezone' => date_default_timezone_name(),
|
| 316 |
'#date_format' => $format,
|
| 317 |
);
|
| 318 |
$form['calendar_type'] = array(
|
| 319 |
'#type' => 'hidden',
|
| 320 |
'#value' => $view->date_info->calendar_type,
|
| 321 |
);
|
| 322 |
$form['view_name'] = array(
|
| 323 |
'#type' => 'hidden',
|
| 324 |
'#value' => $view->name,
|
| 325 |
);
|
| 326 |
$form['view_url'] = array(
|
| 327 |
'#type' => 'hidden',
|
| 328 |
'#value' => $view->get_url(),
|
| 329 |
);
|
| 330 |
$pos = calendar_arg_position($view);
|
| 331 |
$form['calendar_previous_arg'] = array(
|
| 332 |
'#type' => 'hidden',
|
| 333 |
'#value' => $view->args[$pos],
|
| 334 |
);
|
| 335 |
$form['submit'] = array(
|
| 336 |
'#type' => 'submit',
|
| 337 |
'#value' => t('Change date'),
|
| 338 |
);
|
| 339 |
return $form;
|
| 340 |
}
|
| 341 |
|
| 342 |
function calendar_arg_position($view) {
|
| 343 |
$pos = 0;
|
| 344 |
foreach ($view->argument as $argument) {
|
| 345 |
if ($argument->definition['handler'] == 'date_api_argument_handler') {
|
| 346 |
return $pos;
|
| 347 |
}
|
| 348 |
$pos++;
|
| 349 |
}
|
| 350 |
}
|
| 351 |
/**
|
| 352 |
* Get the url for a calendar node.
|
| 353 |
*
|
| 354 |
* @param $node - a calendar node object
|
| 355 |
* @param $default - a default url to use when nothing specific is provided.
|
| 356 |
*/
|
| 357 |
function calendar_get_node_link($node, $default = NULL) {
|
| 358 |
if (isset($node->url)) {
|
| 359 |
return url($node->url, array('absolute' => TRUE));
|
| 360 |
}
|
| 361 |
elseif (empty($node->remote) && is_numeric($node->nid)) {
|
| 362 |
return url("node/$node->nid", array('absolute' => TRUE));
|
| 363 |
}
|
| 364 |
elseif (!empty($default)) {
|
| 365 |
return url($default, array('absolute' => TRUE));
|
| 366 |
}
|
| 367 |
}
|
| 368 |
|
| 369 |
/**
|
| 370 |
* Trim a post to a certain number of characters, removing all HTML.
|
| 371 |
*/
|
| 372 |
function calendar_trim_text($text, $length = 128) {
|
| 373 |
$original_text = $text;
|
| 374 |
// remove any HTML or line breaks so these don't appear in the text
|
| 375 |
$text = trim(str_replace(array("\n", "\r"), ' ', strip_tags($text)));
|
| 376 |
$text = trim(substr($text, 0, $length));
|
| 377 |
|
| 378 |
// only truncate if original text is longer than the length we want
|
| 379 |
if (strlen($original_text) > $length) {
|
| 380 |
$lastchar = substr($text, -1, 1);
|
| 381 |
// check to see if the last character in the title is a non-alphanumeric character, except for ? or !
|
| 382 |
// if it is strip it off so you don't get strange looking titles
|
| 383 |
if (preg_match('/[^0-9A-Za-z\!\?]/', $lastchar)) {
|
| 384 |
$text = substr($text, 0, -1);
|
| 385 |
}
|
| 386 |
// ? and ! are ok to end a title with since they make sense
|
| 387 |
if ($lastchar != '!' and $lastchar != '?') {
|
| 388 |
$text .= '...';
|
| 389 |
}
|
| 390 |
}
|
| 391 |
|
| 392 |
return $text;
|
| 393 |
}
|
| 394 |
|
| 395 |
function calendar_groupby_times($type = '') {
|
| 396 |
$times = array();
|
| 397 |
switch ($type) {
|
| 398 |
case 'hour':
|
| 399 |
for ($i = 0; $i <= 23; $i++) {
|
| 400 |
$times[] = date_pad($i) .':00:00';
|
| 401 |
}
|
| 402 |
break;
|
| 403 |
case 'half':
|
| 404 |
for ($i = 0; $i <= 23; $i++) {
|
| 405 |
$times[] = date_pad($i) .':00:00';
|
| 406 |
$times[] = date_pad($i) .':30:00';
|
| 407 |
}
|
| 408 |
break;
|
| 409 |
default:
|
| 410 |
break;
|
| 411 |
}
|
| 412 |
return $times;
|
| 413 |
}
|
| 414 |
|
| 415 |
/**
|
| 416 |
* Define some error messages.
|
| 417 |
*/
|
| 418 |
function calendar_errors($error) {
|
| 419 |
switch ($error) {
|
| 420 |
case 'missing_argument_default':
|
| 421 |
return t("The Date argument in this view must be set up to provide a default value set to the current date. Edit the argument, find 'Action to take if argument is not present.', choose 'Provide default argument', then select 'Current date'.");
|
| 422 |
}
|
| 423 |
}
|
| 424 |
/**
|
| 425 |
* Implementation of hook_elements.
|
| 426 |
*
|
| 427 |
* Much of the colorpicker code was adapted from the Colorpicker module.
|
| 428 |
* That module has no stable release yet nor any D6 branch.
|
| 429 |
*
|
| 430 |
* TODO Consider dropping the duplicate code and adding a dependency
|
| 431 |
* when that module is more stable, if calendar module customizations will
|
| 432 |
* work in it.
|
| 433 |
*/
|
| 434 |
function calendar_elements() {
|
| 435 |
// the Farbtastic colorpicker
|
| 436 |
$type['calendar_colorpicker'] = array(
|
| 437 |
'#attributes' => array('class' => 'calendar_colorpicker'),
|
| 438 |
'#input' => TRUE,
|
| 439 |
);
|
| 440 |
|
| 441 |
// a textfield to associate with the Farbtastic colorpicker
|
| 442 |
$type['calendar_colorfield'] = array(
|
| 443 |
'#attributes' => array('class' => 'calendar_colorfield'),
|
| 444 |
'#input' => TRUE,
|
| 445 |
'#validate' => array('calendar_validate_hex_color' => array())
|
| 446 |
);
|
| 447 |
return $type;
|
| 448 |
}
|
| 449 |
|
| 450 |
/**
|
| 451 |
* Check to make sure the user has entered a valid 6 digit hex color.
|
| 452 |
*/
|
| 453 |
function calendar_validate_hex_color($element) {
|
| 454 |
if (!$element['#required'] && empty($element['#value'])) {
|
| 455 |
return;
|
| 456 |
}
|
| 457 |
if (!preg_match('/^#(?:(?:[a-f\d]{3}){1,2})$/i', $element['#value'])) {
|
| 458 |
form_error($element, "'". check_plain($element['#value']) ."'". t(' is not a valid hex color'));
|
| 459 |
}
|
| 460 |
else {
|
| 461 |
form_set_value($element, $element['#value']);
|
| 462 |
}
|
| 463 |
}
|
| 464 |
|
| 465 |
/**
|
| 466 |
* Format calendar_colorpicker.
|
| 467 |
*/
|
| 468 |
function theme_calendar_colorpicker($element) {
|
| 469 |
|
| 470 |
$output = '';
|
| 471 |
$output .= '<div id="'. $element['#id'] .'" '. drupal_attributes($element['#attributes']) .' ></div>';
|
| 472 |
return theme('form_element', $element, $output);
|
| 473 |
}
|
| 474 |
|
| 475 |
/**
|
| 476 |
* Format calendar_color textfield.
|
| 477 |
*/
|
| 478 |
function theme_calendar_colorfield($element) {
|
| 479 |
$size = isset($element['#size']) ? ' size="' . $element['#size'] . '"' : '';
|
| 480 |
$maxlength = isset($element['#maxlength']) ? 'maxlength="'.$element['#maxlength'] .'"' : '';
|
| 481 |
$output = '';
|
| 482 |
if (isset($element['#calendar_colorpicker'])) {
|
| 483 |
$element['#attributes']['class'] .= ' edit-'. str_replace("_", "-", $element['#calendar_colorpicker']);
|
| 484 |
}
|
| 485 |
$output .= '<input type="text" name="'. $element['#name'] .'" id="'. $element['#id'] .'" '. $maxlength . $size .' value="'. check_plain($element['#value']) .'"'. drupal_attributes($element['#attributes']) .' />';
|
| 486 |
return theme('form_element', $element, $output);
|
| 487 |
}
|