' : '';
return $block;
case 1:
$block['subject'] = t('Switch Calendar');
$block['content'] = $GLOBALS['calendar_is_calendar'] ? drupal_get_form('calendar_switch_view') : '';
return $block;
}
}
}
/**
* A block with a drop-down list that allows the user to switch between
* views of the current period
*/
function calendar_switch_view() {
$options[''] = t('Calendar');
$options['list'] = t('List');
$options['teasers'] = t('Teasers');
$options['nodes'] = t('Nodes');
$options['table'] = t('Table');
$form = array(
'#method' => 'GET',
'view' => array(
'#type' => 'select',
'#default_value' => $_GET['view'],
'#options' => $options,
),
'q' => array(
'#type' => 'hidden',
'#value' => $_GET['q'],
),
'submit' => array('#type' => 'submit', '#value' => t('Switch')),
);
return $form;
}
/**
* Valid calendar arguments.
*/
function calendar_args() {
return array('calendar_year', 'calendar_week', 'calendar_month', 'calendar_day');
}
/**
* Figure out what the URL of the calendar view we're currently looking at is.
*/
function calendar_real_url($view, $args) {
// Add non-calendar arguments to the base url.
$parts = explode('/', $view->url);
$bump = 0;
foreach ($parts as $delta => $part) {
// If one of the args is buried in the url, add it here and adjust
// the delta values we'll compare the calendar arg positions to.
if (substr($part, 0, 1) == '$') {
$parts[$delta] = array_shift($args);
$bump++;
}
}
foreach ($args as $delta => $arg) {
if (!in_array($delta + $bump, calendar_arg_positions($view)) && !empty($arg)) {
array_push($parts, $arg);
}
}
return implode('/', $parts);
}
/**
* Count the args in the url
*
* Looking for a pattern like '/$'.
*/
function calendar_args_in_url($view) {
ereg('\/\$', $view->url, $regs);
return count($regs);
}
/**
* Pick up filter and sort info from url.
*/
function calendar_url_append($view) {
if ($view->build_type == 'page') {
foreach ($_GET as $key => $val) {
if ($key != 'q' && $key != 'view') {
if (!is_array($val)) {
$url .= '&'. $key .'='. $val;
}
else {
foreach ($val as $v) {
$url .= '&'. $key .'[]='. $v;
}
}
}
}
}
return $url;
}
/**
* An alternative to views_get_url()
* that will correctly substitute replacement
* values like $group or $node.
*/
function calendar_get_url($view, $args, $as_array = FALSE) {
// build an array of the current path and its parts
$real_url = calendar_real_url($view, $view->real_args);
$buried_args = calendar_args_in_url($view);
$i = 0;
$path[0] = array(
'path' => $real_url,
'type' => 'url',
);
$start_calendar = FALSE;
foreach ($view->argument as $delta => $arg) {
if ($delta < $buried_args) {
continue;
}
if (in_array($arg['type'], calendar_args())) {
$start_calendar = TRUE;
$pathtype = str_replace('calendar_', '', $arg['type']);
$path[$delta + 1] = array(
'path' => $args[$delta] != CALENDAR_EMPTY_ARG ? $args[$delta] : CALENDAR_EMPTY_ARG,
'type' => $pathtype,
);
}
// Args prior to the calendar arg are already reflected in the $real_url.
elseif ($start_calendar) {
$path[$delta + 1] = array(
'path' => $args[$delta],
'type' => '',
);
}
}
if ($as_array) {
return $path;
}
else {
$string = '';
$first = TRUE;
foreach ($path as $part) {
if (!$first) {
$string .= '/';
}
$string .= $part['path'];
$first = FALSE;
}
return $string;
}
}
/**
* Substitute a calendar argument with a wildcard.
*/
function calendar_wildcard($arg, $value) {
if (empty($value) && substr($arg['type'], 0, 8) == 'calendar') {
$value = CALENDAR_EMPTY_ARG;
}
elseif (!empty($arg['wildcard_substitution']) && !empty($arg['wildcard']) && !empty($value) && $value == $arg['wildcard']) {
$value = $arg['wildcard_substitution'];
}
return $value;
}
/**
* Function to test whether this is a view that uses the calendar plugin theme.
*/
function calendar_is_calendar($view) {
$calendar_info = calendar_info();
if (!empty($calendar_info) && !empty($calendar_info[$view->name])) {
return $calendar_info[$view->name][$view->build_type];
}
return FALSE;
}
/**
* Function to test whether any calendar args are used in this view.
*/
function calendar_has_calendar_args($view, $reset = FALSE) {
$calendar_info = calendar_info();
if (!empty($calendar_info) && !empty($calendar_info[$view->name])
&& count($calendar_info[$view->name]['args']) > 0) {
return TRUE;
}
else {
return FALSE;
}
}
/**
* The positions in the view that hold calendar arguments.
*/
function calendar_arg_positions($view) {
$calendar_info = calendar_info();
if (array_key_exists($view->name, $calendar_info)) {
return array_keys($calendar_info[$view->name]['args']);
}
else {
return array();
}
}
/**
* Is the current argument a calendar argument.
* Used to sort out whether or not to display the calendar at each point.
*/
function calendar_is_calendar_arg($view) {
if (empty($view->real_args)) {
$delta = 0;
}
else {
$delta = max(array_keys($view->real_args));
}
if (in_array($delta, calendar_arg_positions($view))) {
return TRUE;
}
return FALSE;
}
/**
* Identify the final calendar argument in this view.
* Needed because we can't construct a query until we know all the calendar elements.
* Used to tell when to add the filter to the query object.
*
* @param object $view
* @return string $argtype
*/
function calendar_is_last_arg($view, $reset = FALSE) {
foreach ($view->argument as $argument) {
if (in_array($argument['id'], calendar_args())) {
$calendar_arg = $argument['id'];
}
$max = $argument['id'];
}
return $max < $calendar_arg;
}
/**
* Helper function to find the display formats
* for each part of this view.
*/
function calendar_get_formats($view) {
return variable_get('calendar_display_format_'. $view->name, array(
'year' => 'calendar',
'month' => 'calendar',
'week' => 'calendar',
'day' => 'calendar',
'block' => 'calendar'));
}
/**
* Create a stripe.
*
* @param $node - the node object
* @param $query_name - the views queryname for this date field
* @param $delta - the delta for this field, used to distinguish fields that appear more than once in the calendar
* @param $stripe - the hex code for this stripe.
* @param $label - the label to give this stripe.
*
* TODO this is still too hard-wired to node types.
* Come back later and make it possible to use taxonomy terms or other
* values as stripe keys.
*
* TODO Need to add in hook so ical and other modules can add things to the legend.
*
* TODO Reconsider use of $GLOBALS as a method of triggering the legend, there
* may be a better way.
*/
function calendar_node_stripe($view, &$node, $query_name, $delta, $stripe = NULL, $label = '') {
$type_names = node_get_types('names');
$colors = variable_get('calendar_color_'. $view->name, array());
if (!$label) {
$label = $type_names[$node->type];
}
if (!$stripe) {
if (array_key_exists($node->type, $colors)) {
$stripe = $colors[$node->type];
}
else {
$stripe = '';
}
}
$node->stripe = $stripe;
$node->stripe_label = $label;
$GLOBALS['calendar_stripe_labels'][$node->type] = array('stripe' => $stripe, 'label' => $label);
return $stripe;
}
/**
* Moved the following infrequently-used functions to separate file
* so the code is not parsed on every page.
*/
/**
* Implementation of hook_views_style_plugins()
*/
function calendar_views_style_plugins() {
include_once('./'. drupal_get_path('module', 'calendar') .'/calendar_admin.inc');
return _calendar_views_style_plugins();
}
/**
* Implementation of hook_views_default_views()
*/
function calendar_views_default_views() {
include_once('./'. drupal_get_path('module', 'calendar') .'/calendar_admin.inc');
return _calendar_views_default_views();
}
/**
* Implementation of hook_views_arguments()
*/
function calendar_views_arguments() {
include_once('./'. drupal_get_path('module', 'calendar') .'/calendar_admin.inc');
return _calendar_views_arguments();
}
/**
* Function to return all possible calendar views page display types.
*/
function calendar_view_types($reset = FALSE) {
include_once('./'. drupal_get_path('module', 'calendar') .'/calendar_admin.inc');
return _calendar_view_types($reset);
}
/**
* Function to get information about all views that have calendar components.
*/
function calendar_info($reset = FALSE) {
static $calendar_views;
if (empty($calendar_views) || $reset) {
$cid = 'calendar_views';
if (!$reset && $cached = cache_get($cid, 'cache_views')) {
$calendar_views = unserialize($cached->data);
}
else {
include_once('./'. drupal_get_path('module', 'calendar') .'/calendar_admin.inc');
$calendar_views = _calendar_info();
}
}
return $calendar_views;
}
/**
* Identify all potential date/timestamp fields
*/
function calendar_fields($reset = FALSE) {
static $fields;
if (empty($fields) || $reset) {
$cid = 'calendar_fields';
if (!$reset && $cached = cache_get($cid, 'cache_views')) {
$fields = unserialize($cached->data);
}
else {
include_once('./'. drupal_get_path('module', 'calendar') .'/calendar_admin.inc');
$fields = _calendar_fields();
}
}
return $fields;
}
/**
* Validate a view during Views administration.
*/
function calendar_views_validate($type, $view, $form) {
include_once('./'. drupal_get_path('module', 'calendar') .'/calendar_admin.inc');
return _calendar_views_validate($type, $view, $form);
}
/**
* Setup Calendar parameters in the Setup Tab.
*/
function calendar_setup_form($view_name) {
include_once('./'. drupal_get_path('module', 'calendar') .'/calendar_admin.inc');
return _calendar_setup_form($view_name);
}
/**
* Save Setup values.
*/
function calendar_setup_form_submit($form_id, $form_values) {
include_once('./'. drupal_get_path('module', 'calendar') .'/calendar_admin.inc');
return _calendar_setup_form_submit($form_id, $form_values);
}
/**
* Save Setup values.
*/
function calendar_legend_form_submit($form_id, $form_values) {
include_once('./'. drupal_get_path('module', 'calendar') .'/calendar_admin.inc');
return _calendar_legend_form_submit($form_id, $form_values);
}
/**
* Empty or reset cached values.
*
* @param $remove
* whether or not to completely remove the caches.
*/
function calendar_clear_all($remove = FALSE) {
if ($remove) {
cache_clear_all('calendar_views', 'cache_views');
cache_clear_all('calendar_fields', 'cache_views');
}
else {
calendar_fields(TRUE);
calendar_info(TRUE);
}
}
/**
* Helper function to figure out a group gid to use in blocks.
*
* @return an array of group nodes that are relevant.
* @todo this may need more work.
*/
function calendar_og_groups($view) {
if (!$groupnode = og_get_group_context()) {
global $user;
$groupnodes = array_keys($user->og_groups);
}
else {
$groupnodes = array($groupnode->nid);
}
return $groupnodes;
}
/**
* A selector to jump to a new date in the calendar.
*
* @param unknown_type $view
* @return unknown
*/
function calendar_date_select($view) {
return '
';
}
/**
* The date selector form.
*
* @param object $view
* @return the form element
*
* @TODO is the title desired here or does it take up too much space??
*/
function calendar_date_select_form($view) {
$format = 'Y/m/d';
$form['calendar_goto'] = array(
//'#title' => t('Calendar date'),
'#type' => 'date_popup',
'#default_value' => date_format($view->min_date, 'Y-m-d'),
'#date_timezone' => date_default_timezone_name(),
'#date_format' => $format,
);
$form['calendar_type'] = array(
'#type' => 'hidden',
'#value' => $view->calendar_type,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Change date'),
);
return $form;
}
/**
* Implementation of hook_elements.
*
* Much of the colorpicker code was adapted from the Colorpicker module.
* That module has no stable release yet nor any D6 branch.
*
* TODO Consider dropping the duplicate code and adding a dependency
* when that module is more stable, if calendar module customizations will
* work in it.
*/
function calendar_elements() {
// the Farbtastic colorpicker
$type['calendar_colorpicker'] = array(
'#attributes' => array('class' => 'calendar_colorpicker'),
'#input' => TRUE,
);
// a textfield to associate with the Farbtastic colorpicker
$type['calendar_colorfield'] = array(
'#attributes' => array('class' => 'calendar_colorfield'),
'#input' => TRUE,
'#validate' => array('calendar_validate_hex_color' => array())
);
return $type;
}
/**
* Check to make sure the user has entered a valid 6 digit hex color.
*/
function calendar_validate_hex_color($element) {
if (!$element['#required'] && empty($element['#value'])) {
return;
}
if (!preg_match('/^#(?:(?:[a-f\d]{3}){1,2})$/i', $element['#value'])) {
form_error($element, "'". check_plain($element['#value']) ."'". t(' is not a valid hex color'));
}
else {
form_set_value($element, $element['#value']);
}
}
/**
* Format calendar_colorpicker.
*/
function theme_calendar_colorpicker($element) {
$path = drupal_get_path('module', 'calendar');
// Add Farbtastic color picker
drupal_add_css('misc/farbtastic/farbtastic.css');
drupal_add_js('misc/farbtastic/farbtastic.js');
// Add our custom js and css for our calendar_color
drupal_add_js($path . '/js/calendar_colorpicker.js');
$output = '';
$output .= '';
return theme('form_element', $element, $output);
}
/**
* Format calendar_color textfield.
*/
function theme_calendar_colorfield($element) {
$size = $element['#size'] ? ' size="' . $element['#size'] . '"' : '';
$output = '';
if (isset($element['#calendar_colorpicker'])) {
$element['#attributes']['class'] .= ' edit-'. str_replace("_", "-", $element['#calendar_colorpicker']);
}
$output .= '';
return theme('form_element', $element, $output);
}