=")) { define('USE_TIMEZONES', 1); } else { define('USE_TIMEZONES', 0); } /* * Configuration Defaults */ define('GCAL_EVENTS_DEFAULT_TEMPLATE_EVENT', '

#TITLE##LOC##DATE##TIME#'); define('GCAL_EVENTS_DEFAULT_TEMPLATE_TITLE', '#TITLE#
'); define('GCAL_EVENTS_DEFAULT_TEMPLATE_DATE', '#DATE#
'); define('GCAL_EVENTS_DEFAULT_TEMPLATE_TIME', '#TIME#
'); define('GCAL_EVENTS_DEFAULT_TEMPLATE_ENDTIME', '#ENDTIME#'); define('GCAL_EVENTS_DEFAULT_TEMPLATE_DESC', '#DESC#
'); define('GCAL_EVENTS_DEFAULT_TEMPLATE_LOC', '#LOC#'); define('GCAL_EVENTS_DEFAULT_TEMPLATE_URL', '#URL#'); define('GCAL_EVENTS_DEFAULT_TIME_FORMAT', 'g:ia'); define('GCAL_EVENTS_DEFAULT_DATE_FORMAT', 'j F, Y'); define('GCAL_EVENTS_DEFAULT_NUM_BLOCKS', 1); define('GCAL_EVENTS_DEFAULT_ITEM_LIST', TRUE); define('GCAL_EVENTS_DEFAULT_NUM_EVENTS', 5); define('GCAL_EVENTS_DEFAULT_CACHE', file_directory_path() ."/gcal_events/cache"); define('GCAL_EVENTS_DEFAULT_CACHE_DURATION', 3600); define('GCAL_EVENTS_DEFAULT_FOOTER', ''); define('GCAL_EVENTS_DEFAULT_INPUT_FORMAT', '1'); define('GCAL_EVENTS_DEFAULT_TODAY_ONLY', '0'); define('GCAL_EVENTS_DEFAULT_HEADING_TODAY', 'Today


'); define('GCAL_EVENTS_DEFAULT_HEADING_TOMORROW', 'Tomorrow
'); define('GCAL_EVENTS_DEFAULT_HEADING_THISWEEK', 'This Week
'); define('GCAL_EVENTS_DEFAULT_HEADING_THISMONTH', 'This Month
'); define('GCAL_EVENTS_DEFAULT_HEADING_OTHER', 'Other
'); if (USE_TIMEZONES) { define('GCAL_EVENTS_DEFAULT_TIMEZONE', date_default_timezone_get()); } /* * administration settings panel */ function gcal_events_admin() { // Only Admin Allowed if (!user_access("access administration pages")) { drupal_set_message('You do not have sufficient permissions to access this page', 'error'); return; } // Set the number of blocks available for use $form['gcal_events_num_blocks'] = array( '#type' => 'textfield', '#title' => 'Number of blocks', '#size' => 5, '#default_value' => variable_get('gcal_events_num_blocks', GCAL_EVENTS_DEFAULT_NUM_BLOCKS), '#description' => 'The number of event blocks to enable', '#required' => TRUE ); $form['cache'] = array( '#type' => 'fieldset', '#title' => t('Caching'), '#collapsible' => TRUE ); // Set the cache directory $form['cache']['gcal_events_cache'] = array( '#type' => 'textfield', '#title' => t('Cache Directory'), '#default_value' => variable_get('gcal_events_cache', GCAL_EVENTS_DEFAULT_CACHE), '#description' => t('Cache directory for xml files. Must be writeable by webserver. Leave blank for no caching (not recommended!)') ); $form['cache']['gcal_events_cache_duration'] = array( '#type' => 'textfield', '#title' => t('Cache Duration'), '#size' => 5, '#default_value' => variable_get('gcal_events_cache_duration', GCAL_EVENTS_DEFAULT_CACHE_DURATION), '#description' => t('Cache duration in seconds. Set to zero to disable caching (not recommended!)') ); $form['datetime'] = array( '#type' => 'fieldset', '#title' => 'Date/Time', '#collapsible' => TRUE, '#description' => t('The format to display the time/date on the event blocks. The default date format is j F Y, which corresponds to 10 March, 2008. The default time format is g:ia, which corresponds to 12:15am. See the PHP Date Reference for details') ); // Set the date format to be used for display $form['datetime']['gcal_events_dateformat'] = array( '#type' => 'textfield', '#title' => 'Date Format', '#size' => 10, '#default_value' => variable_get('gcal_events_dateformat', GCAL_EVENTS_DEFAULT_DATE_FORMAT), '#required' => TRUE ); // Set the time format to be used for display $form['datetime']['gcal_events_timeformat'] = array( '#type' => 'textfield', '#title' => 'Time Format', '#size' => 10, '#default_value' => variable_get('gcal_events_timeformat', GCAL_EVENTS_DEFAULT_TIME_FORMAT), '#required' => TRUE ); // Timezone selection. if (USE_TIMEZONES) { $form['datetime']['gcal_events_timezone'] = array( '#type' => 'textfield', '#title' => 'Timezone', '#size' => 30, '#default_value' => variable_get('gcal_events_timezone', GCAL_EVENTS_DEFAULT_TIMEZONE), '#description' => 'The Timezone to use. See the List of Supported Timezones.', '#required' => TRUE ); } $form['day_grouping'] = array( '#type' => 'fieldset', '#title' => 'Day Grouping', '#collapsible' => TRUE, '#description' => t('This section deals with grouping events by days "Today", "Tomorrow", "This Week", "This Month" and "Other", by default.') ); // Enable day grouping $form['day_grouping']['gcal_events_day_grouping'] = array( '#type' => 'checkbox', '#title' => 'Enable Day Grouping', '#default_value' => variable_get('gcal_events_day_grouping', FALSE), '#description' => t('Enable grouping events by day ("Today", "Tomorrow", "This Week", "This Month" and "Other"') ); $form['day_grouping']['gcal_events_heading_today'] = array( '#type' => 'textfield', '#title' => t('Heading for Todays Events'), '#default_value' => variable_get('gcal_events_heading_today', GCAL_EVENTS_DEFAULT_HEADING_TODAY), '#description' => t('The heading to display before a day grouping for todays events.') ); $form['day_grouping']['gcal_events_heading_tomorrow'] = array( '#type' => 'textfield', '#title' => t('Heading for Tomorrows Events'), '#default_value' => variable_get('gcal_events_heading_tomorrow', GCAL_EVENTS_DEFAULT_HEADING_TOMORROW), '#description' => t('The heading to display before a day grouping for tomorrows events.') ); $form['day_grouping']['gcal_events_heading_thisweek'] = array( '#type' => 'textfield', '#title' => t('Heading for This Weeks Events'), '#default_value' => variable_get('gcal_events_heading_thisweek', GCAL_EVENTS_DEFAULT_HEADING_THISWEEK), '#description' => t('The heading to display before a day grouping for this weeks events.') ); $form['day_grouping']['gcal_events_heading_thismonth'] = array( '#type' => 'textfield', '#title' => t('Heading for This Months Events'), '#default_value' => variable_get('gcal_events_heading_thismonth', GCAL_EVENTS_DEFAULT_HEADING_THISMONTH), '#description' => t('The heading to display before a day grouping for this months events.') ); $form['day_grouping']['gcal_events_heading_other'] = array( '#type' => 'textfield', '#title' => t('Heading for other Events'), '#default_value' => variable_get('gcal_events_heading_other', GCAL_EVENTS_DEFAULT_HEADING_OTHER), '#description' => t('The heading to display before a day grouping for events that are not this month.') ); // Get input formats and reorder into a useful manner $input_formats = filter_formats(); foreach ($input_formats as $key=>$value) { $input_formats[$key] = $value->name; } // Input format to use $form['gcal_events_input_format'] = array( '#type' => 'select', '#title' => t('Input Format'), '#options' => $input_formats, '#default_value' => variable_get('gcal_events_input_format', GCAL_EVENTS_DEFAULT_INPUT_FORMAT) ); // Turn on debugging mode $form['gcal_events_debug'] = array( '#type' => 'select', '#title' => t('Debug Level'), '#options' => array(0=>"0 - none",1=>"1 - low",2,3,4,5=>"5 - high"), '#default_value' => variable_get('gcal_events_debug', 0), '#description' => 'Enable debugging mode level. Select 0 for no debugging.' ); // Use templated "Item List" $form['gcal_events_item_list'] = array( '#type' => 'checkbox', '#title' => 'Item List', '#default_value' => variable_get('gcal_events_item_list', GCAL_EVENTS_DEFAULT_ITEM_LIST), '#description' => 'Use unordered list with class gcal_events for event lists. If unchecked, it will just include the exact text as specified in the templates' ); return system_settings_form($form); } /* * Validate the admin settings form. Check to make sure number of blocks is positive. Time/Date formats are hard to check....so we just don't. */ function gcal_events_admin_validate($form, &$form_state) { $cache = $form_state['values']['gcal_events_cache']; if (strlen($cache)>0) { if (!file_exists($cache)) { if (!mkdir($cache)) { form_set_error('gcal_events_cache', t('Cache Directory "%path" does not exist. Please create it before saving the settings.', array('%path' => $cache))); } else { drupal_set_message(t("Cache directory created at %path.", array('%path' => $cache))); } } if (!is_dir($cache)) { form_set_error('gcal_events_cache', t('Cache directory "%path" is not a directory!', array('%path' => $cache))); } if (!is_writeable($cache)) { form_set_error('gcal_events_cache', t('Cache directory "%path" is not writeable', array('%path' => $cache))); } } if (USE_TIMEZONES) { $timezone = $form_state['values']['gcal_events_timezone']; if (!date_default_timezone_set($timezone)) { form_set_error('gcal_events_timezone', t('Timezone is not valid')); } } // Make sure this is last $num_blocks = $form_state['values']['gcal_events_num_blocks']; if (!is_numeric($num_blocks)) { form_set_error('gcal_events_num_blocks', t('You must select a number for the number of blocks.')); } else if ($num_blocks <= 0) { form_set_error('gcal_events_num_blocks', t('Number of blocks must be positive.')); } $old_num_blocks = variable_get('gcal_events_num_blocks', GCAL_EVENTS_DEFAULT_NUM_BLOCKS); if ($old_num_blocks > $num_blocks) { for ($a = $num_blocks; $a < $old_num_blocks; $a++) { drupal_set_message("Deleting block ". ($a+1) ." [". variable_get('gcal_events_admin_name_'. $a, 'not named') ."]"); gcal_events_delete_block($a); } } return TRUE; } /* * Add the gcal events admin settings panel to the admin panel */ function gcal_events_menu() { $items = array(); $items['admin/settings/gcal_events'] = array( 'title' => 'GCal Events module settings', 'description' => 'Configure settings for google calendar upcoming event blocks, including the cache directory, number of blocks, and date/time formats', 'page callback' => 'drupal_get_form', 'page arguments' => array('gcal_events_admin'), 'access arguments' => array('access administration pages'), 'type' => MENU_NORMAL_ITEM, ); return $items; } function gcal_events_admin_name_validate($element) { form_set_error('gcal_events_admin_name', 'Admin name not funny enough'); } /** Implementation of hook_theme() * New registration function introduced in Drupal 6 * * @return array */ function gcal_events_theme() { return array( 'gcal_events_contents' => array( 'arguments' => array('gcal_events' => NULL) ) ); } /** * Implementation of hook_block(). * * This hook both declares to Drupal what blocks are provided by the module, and * generates the contents of the blocks themselves. */ function gcal_events_block($op = 'list', $delta = 0, $edit = array()) { // The $op parameter determines what piece of information is being requested. switch ($op) { case 'list': $blocks = array(); // If $op is "list", we just need to return a list of block descriptions. // This is used to provide a list of possible blocks to the administrator, // end users will not see these descriptions. for ($index=0; $index 'hidden', '#default_value' => $delta ); $form['gcal_events_admin_name'] = array( '#type' => 'textfield', '#title' => t('Admin Name'), '#size' => 40, '#description' => t('An optional short name for this block. This will only be seen in the admin panel to differentiate between calendars when sorting blocks.'), '#default_value' => variable_get('gcal_events_admin_name_'. $delta, 'Block '. ($delta+1)), ); $form['calendar'] = array( '#type' => 'fieldset', '#title' => t('Calendar Settings'), '#collapsible' => TRUE ); $form['calendar']['gcal_events_developer_id'] = array( '#type' => 'textfield', '#title' => t('Developer ID'), '#size' => 60, '#maxlength' => 300, '#description' => t('The Developer ID to use when obtaining events for this calendar. See the Google Calendar API for more information.', array('@google_calendar' => 'https://developers.google.com/google-apps/calendar/')), '#default_value' => variable_get('gcal_events_developer_id_' . $delta, ''), '#required' => TRUE, ); $form['calendar']['gcal_events_calendar_id'] = array( '#type' => 'textarea', '#title' => t('Calendar ID(s)'), '#size' => 60, '#maxlength' => 1000, '#description' => t('Calendar IDs, as shown on Calendar Settings page in google calendar. Will be in the form something@group.calendar.google.com, someting@gmail.com, etc. Multiple Calendar IDs can be separated by space or return.'), '#default_value' => variable_get('gcal_events_calendar_id_'. $delta, '') ); $form['calendar']['gcal_events_private_id'] = array( '#type' => 'textfield', '#title' => t('Private ID'), '#size' => 60, '#maxlength' => 300, '#description' => t('To use a private calendar, include the Private ID here. The Private ID will be part of the Private XML address that looks like private-XXXXXXXXXXXXX/basic. Include only the string after private- but before /basic. Leave this field blank to use the public calendar. Note: You still must include Calendar ID, even if you are using a private calendar. You can also put the entire private xml address in here and the private ID will be parsed out. Using multiple private calendars will not work.'), '#default_value' => variable_get('gcal_events_private_id_'. $delta, '') ); $form['calendar']['gcal_events_num_events'] = array( '#type' => 'textfield', '#title' => t('Number of Events'), '#size' => 5, '#description' => t('The number of upcoming events to display in this block'), '#default_value' => variable_get('gcal_events_num_events_'. $delta, GCAL_EVENTS_DEFAULT_NUM_EVENTS) ); $form['calendar']['gcal_events_today_only'] = array( '#type' => 'checkbox', '#title' => t('Show events from today only'), '#description' => t('Show only events from this calendar day, (not events in the next 24 hours)'), '#default_value' => variable_get('gcal_events_today_only_'. $delta, GCAL_EVENTS_DEFAULT_TODAY_ONLY) ); $form['datetime'] = array( '#type' => 'fieldset', '#title' => 'Date/Time', '#collapsible' => TRUE, '#description' => t('The format to display the time/date on the event blocks. Default is j F Y, which corresponds to 10 March, 2008. See the PHP Date Reference for details') ); $form['datetime']['gcal_events_dateformat'] = array( '#type' => 'textfield', '#title' => t('Date Format'), '#description' => t('Date format to use for this block. Leave blank for default.'), '#default_value' => variable_get('gcal_events_dateformat_'. $delta, "") ); $form['datetime']['gcal_events_timeformat'] = array( '#type' => 'textfield', '#title' => t('Time Format'), '#description' => t('Time format to use for this block. Leave blank for default.'), '#default_value' => variable_get('gcal_events_timeformat_'. $delta, "") ); $form['template'] = array( '#type' => 'fieldset', '#title' => t('Templates'), '#collapsible' => TRUE, '#description' => t('These are the templates to use. Variables #TITLE#, #DESC#, #DATE#, #TIME#, #ENDTIME#, #LOC#, #URL#, and #LOCURL# are availble for use. It is recommended to only use multiple variables inside the event template.') ); $form['template']['gcal_events_template_event'] = array( '#type' => 'textarea', '#title' => 'Event Template', '#rows' => 5, '#cols' => 20, '#description' => t('Use the variables #TITLE#, #DESC#, #DATE#, #TIME#, #ENDTIME#, #LOC#, #URL#, to insert values. These values are generated by the templates that follow.'), '#default_value' => variable_get('gcal_events_template_event_'. $delta, GCAL_EVENTS_DEFAULT_TEMPLATE_EVENT) ); $form['template']['gcal_events_template_title'] = array( '#type' => 'textfield', '#title' => 'Title Template (#TITLE#)', '#description' => t('Use #TITLE# to insert the title.'), '#default_value' => variable_get('gcal_events_template_title_'. $delta, GCAL_EVENTS_DEFAULT_TEMPLATE_TITLE) ); $form['template']['gcal_events_template_desc'] = array( '#type' => 'textfield', '#title' => 'Description Template (#DESC#)', '#description' => t('Use #DESC# to insert the description.'), '#default_value' => variable_get('gcal_events_template_desc_'. $delta, GCAL_EVENTS_DEFAULT_TEMPLATE_DESC) ); $form['template']['gcal_events_template_date'] = array( '#type' => 'textfield', '#title' => 'Date Template (#DATE#)', '#description' => t('Use #DATE# to insert the date.'), '#default_value' => variable_get('gcal_events_template_date_'. $delta, GCAL_EVENTS_DEFAULT_TEMPLATE_DATE) ); $form['template']['gcal_events_template_time'] = array( '#type' => 'textfield', '#title' => 'Time Template (#TIME#)', '#description' => t('Use #TIME# to insert the time.'), '#default_value' => variable_get('gcal_events_template_time_'. $delta, GCAL_EVENTS_DEFAULT_TEMPLATE_TIME) ); $form['template']['gcal_events_template_loc'] = array( '#type' => 'textfield', '#title' => 'Location Template (#LOC#)', '#description' => t('Use #LOC# to insert the location.'), '#default_value' => variable_get('gcal_events_template_loc_'. $delta, GCAL_EVENTS_DEFAULT_TEMPLATE_LOC) ); $form['text'] = array( '#type' => 'fieldset', '#title' => 'Custom Text', '#collapsible' => TRUE ); $form['text']['gcal_events_empty'] = array( '#type' => 'textarea', '#title' => t('No Events'), '#description' => t('Text to display when there are no events to list. Leaving this blank will result in the block not appearing when there are no events.'), '#default_value' => variable_get('gcal_events_empty_'. $delta, 'none') ); $form['text']['gcal_events_header'] = array( '#type' => 'textarea', '#title' => t('Header'), '#description' => t('Text to display at the top of this event list.'), '#default_value' => variable_get('gcal_events_header_'. $delta, ''), ); $form['text']['gcal_events_footer'] = array( '#type' => 'textarea', '#title' => t('Footer'), '#description' => t('Text to display at the bottom of this event list. Typically contains a link to the source for the calendar.'), '#default_value' => variable_get('gcal_events_footer_'. $delta, '') ); $form['gcal_events_reset'] = array( '#type' => 'button', '#value' => t('Reset to defaults'), '#executes_submit_callback' => TRUE, '#submit' => array('block_reset_defaults') ); return $form; case 'save': // If $op is "save", we need to save settings from the configuration form. // Since the first block is the only one that allows configuration, we // need to check $delta to make sure we only save it. // Have Drupal save the string to the database. $calendar_id = check_plain($edit['gcal_events_calendar_id']); //if (preg_match_all('#feeds/(\S+)/public#', $calendar_id, $temp_calendar_id)) { // $calendar_id = $temp_calendar_id[1][0]; //} variable_set('gcal_events_calendar_id_'. $delta, $calendar_id); variable_set('gcal_events_developer_id_' . $delta, check_plain($edit['gcal_events_developer_id'])); $private_id = check_plain($edit['gcal_events_private_id']); if (preg_match_all('#private-(\S+)/basic#', $private_id, $temp_private_id)) { $private_id = $temp_private_id[1][0]; } variable_set('gcal_events_private_id_'. $delta, $private_id); // error check if ($edit['gcal_events_num_events'] > 0) { variable_set('gcal_events_num_events_'. $delta, check_plain($edit['gcal_events_num_events'])); } variable_set('gcal_events_today_only_'. $delta, $edit['gcal_events_today_only']); variable_set('gcal_events_empty_'. $delta, $edit['gcal_events_empty']); variable_set('gcal_events_header_'. $delta, $edit['gcal_events_header']); variable_set('gcal_events_footer_'. $delta, $edit['gcal_events_footer']); variable_set('gcal_events_admin_name_'. $delta, check_plain($edit['gcal_events_admin_name'])); variable_set('gcal_events_template_event_'. $delta, $edit['gcal_events_template_event'], 'gcal_events' ); variable_set('gcal_events_template_title_'. $delta, $edit['gcal_events_template_title']); variable_set('gcal_events_template_desc_'. $delta, $edit['gcal_events_template_desc']); variable_set('gcal_events_template_time_'. $delta, $edit['gcal_events_template_time']); variable_set('gcal_events_template_date_'. $delta, $edit['gcal_events_template_date']); variable_set('gcal_events_template_loc_'. $delta, $edit['gcal_events_template_loc']); variable_set('gcal_events_timeformat_'. $delta, check_plain($edit['gcal_events_timeformat'])); variable_set('gcal_events_dateformat_'. $delta, check_plain($edit['gcal_events_dateformat'])); return; case 'view': default: // If $op is "view", then we need to generate the block for display // purposes. The $delta parameter tells us which block is being requested. $block['subject'] = t('Calendar '. ($delta+1)); $block['content'] = gcal_events_contents($delta); return $block; } } function block_reset_defaults($form, &$form_state) { // Deletes all the variables for this block, resetting them to their defaults $block_number = check_plain($form_state['values']['gcal_events_block']); //print "This is block number [".$block_number."]"; drupal_set_message('Reset to defaults'); gcal_events_delete_block($block_number); } function gcal_events_delete_block($block_number) { variable_del('gcal_events_today_only_'. $block_number); variable_del('gcal_events_header_'. $block_number); variable_del('gcal_events_footer_'. $block_number); variable_del('gcal_events_admin_name_'. $block_number); variable_del('gcal_events_developer_id_'. $block_number); variable_del('gcal_events_calendar_id_'. $block_number); variable_del('gcal_events_private_id_'. $block_number); variable_del('gcal_events_num_events_'. $block_number); variable_del('gcal_events_dateformat_'. $block_number); variable_del('gcal_events_timeformat_'. $block_number); variable_del('gcal_events_template_event_'. $block_number); variable_del('gcal_events_template_title_'. $block_number); variable_del('gcal_events_template_desc_'. $block_number); variable_del('gcal_events_template_date_'. $block_number); variable_del('gcal_events_template_time_'. $block_number); variable_del('gcal_events_template_loc_'. $block_number); variable_del('gcal_events_empty_'. $block_number); variable_del('gcal_events_header_'. $block_number); variable_del('gcal_events_footer_'. $block_number); } /** * A block content function. */ function gcal_events_get_events($which_block) { // for timing $start_time = microtime(true); gcal_events_debug_2("PHP Memory Limit: ". ini_get('memory_limit')); gcal_events_debug("PHP Initial Memory Usage: ". round(memory_get_usage(FALSE)/1024/1024, 1 ) ."MB"); gcal_events_debug("PHP Initial Memory Usage (real): ". round(memory_get_usage(TRUE)/1024/1024, 1 ) ."MB"); $gcal_events = array(); $developer_id = variable_get('gcal_events_developer_id_' . $which_block, ''); $private_id = variable_get('gcal_events_private_id_'. $which_block, ''); $calendar_id = variable_get('gcal_events_calendar_id_'. $which_block, ''); $today_only = variable_get('gcal_events_today_only_'. $which_block, ''); $max_results = variable_get('gcal_events_num_events_' . $which_block, GCAL_EVENTS_DEFAULT_NUM_EVENTS); $dateformat = variable_get('gcal_events_dateformat_'. $which_block, ""); if ($dateformat == "") { $dateformat = variable_get('gcal_events_dateformat', GCAL_EVENTS_DEFAULT_DATE_FORMAT); } $timeformat = variable_get('gcal_events_timeformat_' . $which_block, ''); if ($timeformat == "") { $timeformat = variable_get('gcal_events_timeformat', GCAL_EVENTS_DEFAULT_TIME_FORMAT); } $admin_name = variable_get('gcal_events_admin_name_' . $which_block, 'Block '. ($which_block+1)); if ($calendar_id == "") { drupal_set_message('GCal Events['. $admin_name .'] Calendar ID Not Set!', 'error'); return; } if (USE_TIMEZONES) { gcal_events_debug('Timezone: '. date_default_timezone_get()); $timezone = variable_get('gcal_events_timezone', GCAL_EVENTS_DEFAULT_TIMEZONE); if (!date_default_timezone_set($timezone)) { drupal_set_message('GCal Events: Timezone $timezone is not valid!'); } } else { gcal_events_debug('Timezone Support Disabled! PHP Version '. PHP_VERSION .' < 5.1'); } // Including the google-api-php-client library (required). $autoload_path = libraries_get_path('google-api-php-client') . '/autoload.php'; if (!file_exists($autoload_path)) { drupal_set_message('GCal Events: The google-api-php-client library was not found.', 'error'); return; } include_once($autoload_path); // https://developers.google.com/google-apps/calendar/v3/reference $client = new Google_Client(); $client->setApplicationName('gcal_events_feed'); $client->setDeveloperKey($developer_id); $service = new Google_Service_Calendar($client); $optParams = array( 'orderBy' => 'startTime', 'singleEvents' => 'true', 'timeMin' => date('Y-m-d') . 'T00:00:00' . date('P'), 'timeZone' => $timezone, ); if ($today_only) { $optParams['timeMax'] = date('Y-m-d') . 'T23:59:00' . date('P'); } $events = array(); try { $events = $service->events->listEvents($calendar_id, $optParams); } catch (Exception $e) { drupal_set_message(t('GCal Events: Bad call to list events'), 'error'); return; } $count = 0; foreach ($events->getItems() as $event) { if ($count >= $max_results) { break; } $event_id = $event->getId(); $event = $service->events->get($calendar_id, $event_id); $title = trim($event->getSummary()); $description = $event->getDescription(); $location = $event->getLocation(); $status = $event->getStatus(); $start = $event->getStart(); $end = $event->getEnd(); $allday = !empty($start->date); $date = !empty($start->date) ? $start->date : $start->dateTime; $gcal_time = !empty($start->dateTime) ? format_date($start->dateTime, 'custom', $timeformat) : ''; $gcal_endtime = !empty($end->dateTime) ? format_date($end->dateTime, 'custom', $timeformat) : ''; $unixdate = strtotime($date); if (strlen($title) > 1 && $status != 'canceled' && strlen($date) > 0) { $temp[] = array( 'title' => $title, 'description' => $description, 'allday' => $allday, 'where' => $location, 'date' => format_date($unixdate, 'custom', $dateformat), 'timestamp' => $unixdate, 'time' => $gcal_time, 'endtime' => $gcal_endtime, 'link' => '', // TODO 'today' => date("Y-m-d") == date("Y-m-d", $unixdate), 'tomorrow' => (date("Y-m-d",strtotime("+1 day")) == date("Y-m-d", $unixdate)), 'thisweek' => (date("Y-W") == date("Y-W", $unixdate)), 'thismonth' => (date("Y-m") == date("Y-m", $unixdate)), ); } $count++; } gcal_events_debug("PHP Memory Usage: ". round(memory_get_usage(FALSE)/1024/1024, 1 ) ."MB"); gcal_events_debug("PHP Memory Usage (real): ". round(memory_get_usage(TRUE)/1024/1024, 1 ) ."MB"); gcal_events_debug_block($admin_name, 'Feed processing took ' . round((microtime(true) - $time_feed_process),2) . ' seconds'); return $temp; } function template_events($events, $which_block) { foreach ($events as $item) { // Now, let's run it through some str_replaces, and store it with the date for easy sorting later $temp_event=variable_get('gcal_events_template_event_'. $which_block, GCAL_EVENTS_DEFAULT_TEMPLATE_EVENT); if ($item['title']) { $temp_event=str_replace("#TITLE#", $item['title'], str_replace("#TITLE#", variable_get('gcal_events_template_title_'. $which_block, GCAL_EVENTS_DEFAULT_TEMPLATE_TITLE), $temp_event)); } else { $temp_event=str_replace("#TITLE#", "", $temp_event); } if ($item['description']) { $temp_event=str_replace("#DESC#", $item['description'], str_replace("#DESC#", variable_get('gcal_events_template_desc_'. $which_block, GCAL_EVENTS_DEFAULT_TEMPLATE_DESC), $temp_event)); } else { $temp_event=str_replace("#DESC#", "", $temp_event); } if ($item['date']) { $temp_event=str_replace("#DATE#", $item['date'], str_replace("#DATE#", variable_get('gcal_events_template_date_'. $which_block, GCAL_EVENTS_DEFAULT_TEMPLATE_DATE), $temp_event)); } else { $temp_event=str_replace("#DATE#", "", $temp_event); } if ($item['time']) { $temp_event=str_replace("#TIME#", $item['time'], str_replace("#TIME#", variable_get('gcal_events_template_time_'. $which_block, GCAL_EVENTS_DEFAULT_TEMPLATE_TIME), $temp_event)); } else { $temp_event=str_replace("#TIME#", "", $temp_event); } if ($item['endtime']) { $temp_event=str_replace("#ENDTIME#", $item['endtime'], str_replace("#ENDTIME#", GCAL_EVENTS_DEFAULT_TEMPLATE_ENDTIME, $temp_event)); } else { $temp_event=str_replace("#ENDTIME#", "", $temp_event); } if ($item['where']) { $temp_event=str_replace("#LOC#", $item['where'], str_replace("#LOC#", variable_get('gcal_events_template_loc_'. $which_block, GCAL_EVENTS_DEFAULT_TEMPLATE_LOC), $temp_event)); $temp_event=str_replace("#LOCURL#", urlencode($item['where']), $temp_event); } else { $temp_event=str_replace("#LOC#", "", $temp_event); } if ($item['link']) { $temp_event=str_replace("#URL#", $item['link'], str_replace("#URL#", variable_get('gcal_events_template_url_'. $which_block, GCAL_EVENTS_DEFAULT_TEMPLATE_URL), $temp_event)); } else { $temp_event=str_replace("#URL#", "", $temp_event); } $input_format = variable_get('gcal_events_input_format', GCAL_EVENTS_DEFAULT_INPUT_FORMAT); if (!filter_access($input_format)) { drupal_set_message('Input Format ' . $input_format . ' is not accessible by the current user'); return "Error: Input format is not accessible"; } // Check for invalid html $temp_event = check_markup($temp_event, $input_format, FALSE); $item['event'] = $temp_event; $gcal_events[] = $item; } return $gcal_events; } function gcal_events_contents($which_block) { $events = gcal_events_get_events($which_block); $events = template_events($events, $which_block); return theme('gcal_events_contents', $events, $which_block); } function theme_gcal_events_contents($gcal_events, $which_block) { $full_path = drupal_get_path('module', 'gcal_events'). '/gcal_events.css'; drupal_add_css($full_path); $output = ""; $output .= variable_get('gcal_events_header_'. $which_block, ''); $use_item_list = variable_get('gcal_events_item_list', '1'); $day_grouping = variable_get('gcal_events_day_grouping', FALSE); if (empty($gcal_events)) { $output .= variable_get('gcal_events_empty_'. $which_block, 'none'); return $output; } if ($day_grouping) { $today_events = array_filter($gcal_events, 'is_today'); $tomorrow_events = array_filter($gcal_events, 'is_tomorrow'); $thisweek_events = array_filter($gcal_events, 'is_thisweek_exc'); $thismonth_events = array_filter($gcal_events, 'is_thismonth_exc'); $other_events = array_filter($gcal_events, 'isnt_thismonth'); if (count($today_events)>0) { $output .= variable_get('gcal_events_heading_today', GCAL_EVENTS_DEFAULT_HEADING_TODAY); $output .= gcal_events_display_events($use_item_list, $today_events, 'today'); } if (count($tomorrow_events)>0) { $output .= variable_get('gcal_events_heading_tomorrow', GCAL_EVENTS_DEFAULT_HEADING_TOMORROW); $output .= gcal_events_display_events($use_item_list, $tomorrow_events, 'tomorrow'); } if (count($thisweek_events)>0) { $output .= variable_get('gcal_events_heading_thisweek', GCAL_EVENTS_DEFAULT_HEADING_THISWEEK); $output .= gcal_events_display_events($use_item_list, $thisweek_events, 'thisweek'); } if (count($thismonth_events) > 0) { $output .= variable_get('gcal_events_heading_thismonth', GCAL_EVENTS_DEFAULT_HEADING_THISMONTH); $output .= gcal_events_display_events($use_item_list, $thismonth_events, 'thismonth'); } if (count($other_events)>0) { $output .= variable_get('gcal_events_heading_other', GCAL_EVENTS_DEFAULT_HEADING_OTHER); $output .= gcal_events_display_events($use_item_list, $other_events, 'other'); } } else { $output .= gcal_events_display_events($use_item_list, $gcal_events); } $output .= variable_get('gcal_events_footer_'. $which_block, ''); return $output; } function gcal_events_display_events($use_item_list, $events, $class='gcal_events') { if ($class != "gcal_events") { $class = "gcal_events_" . $class; } if ($use_item_list) { return theme('item_list', gcal_events_just_entries($events), NULL, 'ul', array('class' => $class)); } else { return join("", gcal_events_just_entries($events)); } } function is_today($event) { if ($event['today']) return TRUE; return FALSE; } function is_tomorrow($event) { if ($event['tomorrow']) return TRUE; return FALSE; } function is_thisweek_inc($event) { if ($event['thisweek']) return TRUE; return FALSE; } function is_thisweek_exc($event) { if ($event['thisweek'] && !$event['today'] && !$event['tomorrow']) return TRUE; return FALSE; } function is_thismonth_inc($event) { if ($event['thismonth']) return TRUE; return FALSE; } function isnt_thismonth($event) { return !is_thismonth_inc($event); } function is_thismonth_exc($event) { if ($event['thismonth'] && !$event['thisweek'] && !$event['tomorrow'] && !$event['today']) return TRUE; return FALSE; } function gcal_events_just_entries($events) { foreach ($events as $event) { $just_entries[] = $event['event']; } return $just_entries; } function gcal_events_debug($string) { gcal_events_debug_master(array(level=>1, msg=>$string)); } function gcal_events_debug_2($string) { gcal_events_debug_master(array(level=>2, msg=>$string)); } function gcal_events_debug_3($string) { gcal_events_debug_master(array(level=>3, msg=>$string)); } function gcal_events_debug_4($string) { gcal_events_debug_master(array(level=>4, msg=>$string)); } function gcal_events_debug_5($string) { gcal_events_debug_master(array(level=>5, msg=>$string)); } function gcal_events_debug_block($block, $string) { gcal_events_debug_master(array(block=>$block, msg=>$string)); } function gcal_events_debug_block_2($block, $string) { gcal_events_debug_master(array(level=>2, block=>$block, msg=>$string)); } /* * Debug Message function master. It's best to call the other wrapper functions instead. */ function gcal_events_debug_master($data) { // Get the debug variable static $debug_mode = -1; if ($debug_mode == -1) { $debug_mode = variable_get('gcal_events_debug', FALSE); } if ($debug_mode) { if (is_array($data)) { if ($data['level'] && $data['level'] > $debug_mode) return; $disp_string = ''; $disp_string .= "GCal Events"; if ($data['block']) { $disp_string .= "[". $data['block'] ."]: "; } else { $disp_string .= ": "; } $disp_string .= $data['msg']; drupal_set_message($disp_string); } else { drupal_set_message("gcal_events_debug_master called incorrectly!"); } } }