You are here

spaces_calendar.module in Spaces 5.2

Same filename and directory in other branches
  1. 5 spaces_calendar/spaces_calendar.module

File

spaces_calendar/spaces_calendar.module
View source
<?php

/**
 * TODO Make setup easier. Currenly you need to manually create three content types 'event', 
 * 'feed_ical', 'feed_ical_item' that has a field called 'field_date' for this module to work.
 * We need to document the feed api integration as well.
 */

/**
 * Implementation of hook_menu()
 */
function spaces_calendar_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/spaces/calendar',
      'title' => t('Spaces calendar settings'),
      'description' => t('Spaces calendar feature defaults.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'spaces_calendar_settings_form',
      ),
      'access' => user_access('administer group features'),
      'type' => MENU_LOCAL_TASK,
      'weight' => 1,
    );
  }
  return $items;
}

/**
 * Implementation of hook_help()
 */
function spaces_calendar_help($page) {
  if (context_get('spaces', 'feature') == 'calendar') {
    return "<p>" . t('The calendar displays all events in this group. You can add events directly to the calendar or aggregate additional items using iCal feeds.') . "</p>";
  }
}

/**
 * Implementation of hook_context_define()
 */
function spaces_calendar_context_define() {
  $items = array();
  if (_spaces_calendar_enabled()) {
    $items['calendar'] = array(
      'namespace' => 'spaces',
      'attribute' => 'feature',
      'value' => 'calendar',
      'views' => array(
        'spaces_calendar_upcoming',
        'spaces_calendar',
      ),
      'node' => array(
        variable_get('spaces_calendar_nodetype', ''),
      ),
      'block' => array(
        array(
          'module' => 'views',
          'delta' => 'spaces_calendar_upcoming',
          'region' => 'right',
          'weight' => -11,
        ),
      ),
      'spaces' => array(
        'label' => t('Calendar'),
        'description' => t('An event calendar with optional iCal integration.'),
        'menu' => array(
          'calendar' => array(
            'title' => t('Calendar'),
          ),
        ),
      ),
    );
  }
  if (_spaces_calendar_ical_enabled()) {
    $items['calendar']['node'][] = variable_get('spaces_calendar_feed_nodetype', '');
    $items['calendar']['node'][] = variable_get('spaces_calendar_feed_itemtype', '');
    $items['calendar']['spaces']['menu']['calendar/feeds'] = array(
      'title' => t('Feeds'),
    );
    $items['calendar']['views'][] = 'spaces_calendar_feeds';
  }
  return $items;
}

/**
 * Implementation of hook_nodeapi()
 */
function spaces_calendar_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  if ($op == 'view' && !$teaser && $page) {
    _spaces_calendar_alter_links();
    if ($node->type == variable_get('spaces_calendar_feed_nodetype', '')) {
      $view = views_get_view('spaces_calendar_ical_items');
      $view = views_build_view('embed', $view, array(
        $node->nid,
      ), $view->pager, $view->nodes_per_page);
      if ($view) {
        $node->content['item_list'] = array(
          '#value' => $view,
          '#weight' => 10,
        );
      }
      $node->content['buttons'] = array(
        '#value' => "<div class='buttons'>" . l(t('View calendar'), 'calendar', array(
          'class' => 'button',
        )) . "</div>",
        '#weight' => 5,
      );
    }
  }
}

/**
 * Implementation of hook_form_alter()
 */
function spaces_calendar_form_alter($form_id, &$form) {
  if (_spaces_calendar_enabled()) {
    _spaces_calendar_alter_links();
    switch ($form_id) {

      // TODO: use content types in features array to determine calendar node form id
      case variable_get('spaces_calendar_nodetype', '') . '_node_form':

        // add a submit handler to send user to view of submitted event
        $form['#submit']['spaces_calendar_event_submit'] = array();
    }
  }
}
function spaces_calendar_settings_form() {
  if (_spaces_calendar_ical_enabled()) {
    $feedtype = variable_get('spaces_calendar_feed_nodetype', '');
    if (!variable_get('feedapi_mapper_mapping_' . $feedtype, array())) {
      $mapping = array(
        serialize(array(
          'raw',
          'DTSTART',
          'datetime',
        )) => serialize(array(
          'date',
          variable_get('spaces_calendar_datefield', ''),
          'from',
        )),
        serialize(array(
          'raw',
          'DTEND',
          'datetime',
        )) => serialize(array(
          'date',
          variable_get('spaces_calendar_datefield', ''),
          'to',
        )),
      );
      variable_set('feedapi_mapper_mapping_' . $feedtype, $mapping);
      drupal_set_message(t('iCal feed mappings have been set up.'));
    }
  }
  $form = array();
  $types = array();
  $nodetypes = node_get_types();
  foreach ($nodetypes as $ntype => $nname) {
    $types[$ntype] = $nname->name;
  }
  $fields = content_fields();
  $fieldtypes = array();
  foreach ($fields as $field) {
    $fieldtypes[$field['field_name']] = t($field['widget']['label']) . ' (' . $field['field_name'] . ')';
  }
  $form['spaces_calendar_default_settings'] = array(
    '#type' => 'fieldset',
    '#title' => 'Spaces calendar default settings',
  );
  $form['spaces_calendar_default_settings']['spaces_calendar_nodetype'] = array(
    '#type' => 'select',
    '#title' => t('Event node types'),
    '#description' => t('Select the designated node type for events.'),
    '#options' => $types,
    '#default_value' => variable_get('spaces_calendar_nodetype', ''),
    '#multiple' => FALSE,
  );
  $form['spaces_calendar_default_settings']['spaces_calendar_datefield'] = array(
    '#type' => 'select',
    '#title' => t('Event date field'),
    '#description' => t('Select the designated cck date field to use for events events.'),
    '#options' => $fieldtypes,
    '#default_value' => variable_get('spaces_calendar_datefield', ''),
    '#multiple' => FALSE,
  );
  if (module_exists('feedapi') && module_exists('feedapi_mapper')) {
    $form['spaces_calendar_feedapi_settings'] = array(
      '#type' => 'fieldset',
      '#title' => 'Spaces calendar feedapi/ical integration settings',
    );
    $form['spaces_calendar_feedapi_settings']['spaces_calendar_feed_nodetype'] = array(
      '#type' => 'select',
      '#title' => t('FeedAPI feed node type'),
      '#description' => t('Select the type of node that will hold ical feeds.'),
      '#options' => $types,
      '#default_value' => variable_get('spaces_calendar_feed_nodetype', ''),
      '#multiple' => FALSE,
    );
    $form['spaces_calendar_feedapi_settings']['spaces_calendar_feed_itemtype'] = array(
      '#type' => 'select',
      '#title' => t('FeedAPI node item type'),
      '#description' => t('Select the type of node that events from ical feeds should be created into.'),
      '#options' => $types,
      '#default_value' => variable_get('spaces_calendar_feed_itemtype', ''),
      '#multiple' => FALSE,
    );
  }
  return system_settings_form($form);
}

/**
 *  Implementation of hook_default_views
 */
function spaces_calendar_views_default_views() {
  $default_views = array();
  if (variable_get('spaces_calendar_datefield', '') && variable_get('spaces_calendar_nodetype', '')) {
    $default_views[] = '_spaces_calendar_views_calendar';
    $default_views[] = '_spaces_calendar_views_calendar_upcoming';
  }
  if (_spaces_calendar_ical_enabled()) {
    $default_views[] = '_spaces_calendar_views_calendar_feeds';
    $default_views[] = '_spaces_calendar_views_calendar_ical_items';
  }
  foreach ($default_views as $v) {
    $view = call_user_func($v);
    if (is_object($view) && $view->name) {
      $views[$view->name] = $view;
    }
  }
  return $views;
}

// Implementation of hook_views_post_view().
function spaces_calendar_views_post_view(&$view) {
  _spaces_calendar_alter_links();
}

// Push user to the calendar view of their submitted event
function spaces_calendar_event_submit($form_id, $form_values) {
  $n = node_submit($form_values);
  if ($date = $n->field_date[0]['value']) {
    $date = strtotime($date);
    $m = format_date($date, 'custom', 'm');
    $y = format_date($date, 'custom', 'Y');
    return "calendar/{$y}/{$m}";
  }
}

/**
 *  Helper function to display feed links in calendar view footers
 */
function _spaces_calendar_feed_links() {
  $links = array();
  $space = spaces_get_space();
  $feedtype = variable_get('spaces_calendar_feed_nodetype', '');
  if ($space && $feedtype) {
    $results = db_query("SELECT nr.title, n.nid\n      FROM {node} n\n        LEFT JOIN {node_revisions} nr ON n.vid = nr.vid\n        JOIN {og_ancestry} og ON n.nid = og.nid\n      WHERE n.type = '%s'\n        AND n.status = 1\n        AND og.group_nid = %d\n      ORDER BY nr.title ASC", $feedtype, $space->sid);
    while ($feed = db_fetch_object($results)) {
      $crayon = theme('crayon', $feed->nid);
      $links[] = array(
        'title' => "<span class='crayon-marker crayon-{$crayon}'></span> " . $feed->title,
        'html' => true,
      );
    }
  }
  return $links;
}

/* VIEWS */
function _spaces_calendar_views_calendar() {
  $view = new stdClass();
  $view->name = 'spaces_calendar';
  $view->description = t('Calendar view of events.');
  $view->access = array();
  $view->menu = TRUE;
  $view->menu_title = t('Calendar');
  $view->page = TRUE;
  $view->page_title = 'Calendar';
  $view->page_type = 'calendar';
  $view->page_footer = _spaces_calendar_ical_enabled() && module_exists('feedapi_node_views') ? '<?php print theme("links", _spaces_calendar_feed_links(), array("class" => "clear-block calendar-key")); ?>' : '';
  $view->page_footer_format = '2';
  $view->view_args_php = '';
  $view->url = 'calendar';
  $view->use_pager = TRUE;
  $view->nodes_per_page = '100';
  $view->sort = array();
  $view->argument = array(
    array(
      'type' => 'calendar_year',
      'argdefault' => '2',
      'title' => '',
      'options' => '',
      'wildcard' => '',
      'wildcard_substitution' => '',
    ),
    array(
      'type' => 'calendar_month',
      'argdefault' => '2',
      'title' => '',
      'options' => '',
      'wildcard' => '',
      'wildcard_substitution' => '',
    ),
  );
  $view->field = array(
    array(
      'tablename' => 'node',
      'field' => 'title',
      'label' => '',
      'handler' => 'views_handler_field_nodelink',
      'options' => 'link',
    ),
    array(
      'tablename' => 'node_data_' . variable_get('spaces_calendar_datefield', ''),
      'field' => variable_get('spaces_calendar_datefield', '') . '_value',
      'label' => '',
      'handler' => 'content_views_field_handler_ungroup',
      'options' => 'short',
    ),
  );
  if (_spaces_calendar_ical_enabled() && module_exists('feedapi_node_views')) {
    $view->field[] = array(
      'tablename' => 'feedapi_node_item_feed',
      'field' => 'feed_nid',
      'label' => '',
      'options' => 'nolink',
    );
  }
  $view->filter = array(
    array(
      'tablename' => 'node',
      'field' => 'status',
      'operator' => '=',
      'options' => '',
      'value' => '1',
    ),
    array(
      'tablename' => 'node',
      'field' => 'type',
      'operator' => 'OR',
      'options' => '',
      'value' => array(
        0 => variable_get('spaces_calendar_nodetype', ''),
        1 => variable_get('spaces_calendar_feed_itemtype', ''),
      ),
    ),
    array(
      'tablename' => 'spaces',
      'field' => 'type',
      'operator' => 'all',
      'options' => '',
      'value' => 'all',
    ),
  );
  $view->requires = array(
    'node',
    'node_data_' . variable_get('spaces_calendar_datefield', ''),
    'og_ancestry',
  );
  return $view;
}
function _spaces_calendar_views_calendar_upcoming() {
  $view = new stdClass();
  $view->name = 'spaces_calendar_upcoming';
  $view->description = t('Provides a listing of upcoming events');
  $view->access = array();
  $view->view_args_php = '';
  $view->page = FALSE;
  $view->block = TRUE;
  $view->block_title = t('Upcoming Events');
  $view->block_empty = '<p>' . t('No upcoming events found.') . '</p>';
  $view->block_empty_format = '2';
  $view->block_type = 'spaces_datetitle';
  $view->nodes_per_block = '5';
  $view->argument = array();
  $view->sort = array(
    array(
      'tablename' => 'node_data_' . variable_get('spaces_calendar_datefield', ''),
      'field' => variable_get('spaces_calendar_datefield', '') . '_value',
      'sortorder' => 'ASC',
      'options' => '',
    ),
  );
  $view->field = array(
    array(
      'tablename' => 'node',
      'field' => 'title',
      'label' => '',
      'handler' => 'views_handler_field_nodelink',
      'options' => 'link',
    ),
    array(
      'tablename' => 'node_data_' . variable_get('spaces_calendar_datefield', ''),
      'field' => variable_get('spaces_calendar_datefield', '') . '_value',
      'label' => '',
      'handler' => 'content_views_field_handler_ungroup',
      'options' => 'default',
    ),
  );
  $view->filter = array(
    array(
      'tablename' => 'node',
      'field' => 'status',
      'operator' => '=',
      'options' => '',
      'value' => '1',
    ),
    array(
      'tablename' => 'node',
      'field' => 'type',
      'operator' => 'OR',
      'options' => '',
      'value' => array(
        0 => variable_get('spaces_calendar_nodetype', ''),
        1 => variable_get('spaces_calendar_feed_itemtype', ''),
      ),
    ),
    array(
      'tablename' => 'node_data_' . variable_get('spaces_calendar_datefield', ''),
      'field' => variable_get('spaces_calendar_datefield', '') . '_value_default',
      'operator' => '>',
      'options' => 'now',
      'value' => '',
    ),
    array(
      'tablename' => 'spaces',
      'field' => 'type',
      'operator' => 'all',
      'options' => '',
      'value' => 'all',
    ),
  );
  $view->exposed_filter = array();
  $view->requires = array(
    'node_data_' . variable_get('spaces_calendar_datefield', ''),
    'node',
  );
  return $view;
}
function _spaces_calendar_views_calendar_feeds() {
  $view = new stdClass();
  $view->name = 'spaces_calendar_feeds';
  $view->description = 'Displays ical feeds and allows users to administer them';
  $view->access = array();
  $view->view_args_php = '';
  $view->page = TRUE;
  $view->page_title = 'Calendar feeds';
  $view->page_type = 'table';
  $view->url = 'calendar/feeds';
  $view->use_pager = TRUE;
  $view->nodes_per_page = '20';
  $view->menu = TRUE;
  $view->menu_title = 'Calendar feeds';
  $view->menu_tab = FALSE;
  $view->menu_tab_weight = '0';
  $view->menu_tab_default = FALSE;
  $view->menu_tab_default_parent = NULL;
  $view->menu_tab_default_parent_type = 'tab';
  $view->menu_parent_tab_weight = '0';
  $view->menu_parent_title = '';
  $view->sort = array(
    array(
      'tablename' => 'node',
      'field' => 'title',
      'sortorder' => 'ASC',
      'options' => '',
    ),
  );
  $view->argument = array();
  $view->field = array(
    array(
      'tablename' => 'node',
      'field' => 'title',
      'label' => 'Feed',
      'handler' => 'views_handler_field_nodelink',
      'options' => 'link',
    ),
    array(
      'tablename' => 'node',
      'field' => 'created',
      'label' => 'Created On',
      'handler' => 'views_handler_field_date_custom',
      'options' => 'M j, Y',
    ),
    array(
      'tablename' => 'node',
      'field' => 'edit',
      'label' => '',
      'handler' => 'views_handler_node_edit_destination',
    ),
  );
  $view->filter = array(
    array(
      'tablename' => 'node',
      'field' => 'type',
      'operator' => 'OR',
      'options' => '',
      'value' => array(
        0 => 'feed_ical',
      ),
    ),
    array(
      'tablename' => 'node',
      'field' => 'status',
      'operator' => '=',
      'options' => '',
      'value' => '1',
    ),
    array(
      'tablename' => 'spaces',
      'field' => 'type',
      'operator' => 'all',
      'options' => '',
      'value' => 'all',
    ),
  );
  $view->exposed_filter = array();
  $view->requires = array(
    node,
    og_ancestry,
  );
  return $view;
}
function _spaces_calendar_views_calendar_ical_items() {
  $view = new stdClass();
  $view->name = 'spaces_calendar_ical_items';
  $view->description = 'Listing of ical items for the node view of feeds.';
  $view->access = array();
  $view->view_args_php = '';
  $view->page = TRUE;
  $view->page_type = 'table';
  $view->url = '';
  $view->use_pager = TRUE;
  $view->nodes_per_page = '10';
  $view->sort = array(
    array(
      'tablename' => 'node_data_' . variable_get('spaces_calendar_datefield', ''),
      'field' => variable_get('spaces_calendar_datefield', '') . '_value',
      'sortorder' => 'DESC',
      'options' => '',
    ),
  );
  $view->argument = array(
    array(
      'type' => 'feed_nid',
      'argdefault' => '1',
      'title' => '',
      'options' => '',
      'wildcard' => '',
      'wildcard_substitution' => '',
    ),
  );
  $view->field = array(
    array(
      'tablename' => 'node',
      'field' => 'title',
      'label' => 'Title',
      'handler' => 'views_handler_field_nodelink',
      'options' => 'link',
    ),
    array(
      'tablename' => 'node_data_' . variable_get('spaces_calendar_datefield', ''),
      'field' => variable_get('spaces_calendar_datefield', '') . '_value',
      'label' => 'Date',
      'handler' => 'content_views_field_handler_group',
      'options' => 'default',
    ),
  );
  $view->filter = array(
    array(
      'tablename' => 'node',
      'field' => 'status',
      'operator' => '=',
      'options' => '',
      'value' => '1',
    ),
    array(
      'tablename' => 'node',
      'field' => 'type',
      'operator' => 'OR',
      'options' => '',
      'value' => array(
        0 => variable_get('spaces_calendar_feed_itemtype', ''),
      ),
    ),
  );
  $view->exposed_filter = array();
  $view->requires = array(
    'node_data_' . variable_get('spaces_calendar_datefield', ''),
    'node',
  );
  return $view;
}
function _spaces_calendar_enabled() {
  return variable_get('spaces_calendar_nodetype', '') && variable_get('spaces_calendar_datefield', '');
}
function _spaces_calendar_ical_enabled() {
  return module_exists('feedapi') && module_exists('feedapi_mapper') && variable_get('spaces_calendar_feed_itemtype', '') && variable_get('spaces_calendar_feed_nodetype', '');
}
function _spaces_calendar_alter_links() {
  if (context_get('spaces', 'feature') == 'calendar') {
    $links = array();
    $links['feed_ical_item'] = null;
    context_set('spaces', 'links', $links);
  }
}