/**
 * @copyright 2015 City of Bloomington, Indiana
 * @license http://www.gnu.org/licenses/agpl.txt GNU/AGPL, see LICENSE.txt
 * @param array $data['calendarId' => '', 'type' => '']
 */
$service = cob_calendar_service();
$calendar = $service->calendars->get($data['calendarId']);
$events = cob_calendar_events($data['calendarId'], new \DateTime(), null, true, 4);
/*echo '<pre>';
print_r($events);
echo '</pre>';
*/
if ($calendar) {
    $title = $data['type'] == 'board_commission' ? 'Meeting Schedule' : $calendar->summary;
    $url = cob_calendar_url($data['calendarId']);
    echo "\n    <section class=\"cob-upcomingEvents\">\n        <header class=\"cob-upcomingEvents-header\">\n            <h1>{$title}</h1>\n            <a href=\"{$url}\" target=\"_blank\">View Google Calendar</a>\n        </header>\n    ";
    if (count($events)) {
        echo '<ol class="cob-upcomingEvents-list">';
        foreach ($events as $e) {
            if ($e->start->dateTime) {
                $allDay = false;
                $start = new \DateTime($e->start->dateTime);
                $end = new \DateTime($e->end->dateTime);
            } else {
                # All day event
                $allDay = true;
                $start = new \DateTime($e->start->date);
                $end = new \DateTime($e->end->date);
            }
            if ($start->format('Y-m-d') === $end->format('Y-m-d')) {
*   - field-name-[field_name]: The current field name. For example, if the
*     field name is "field_description" it would result in
*     "field-name-field-description".
*   - field-type-[field_type]: The current field type. For example, if the
*     field type is "text" it would result in "field-type-text".
*   - field-label-[label_display]: The current label position. For example, if
*     the label position is "above" it would result in "field-label-above".
*
* Other variables:
* - $element['#object']: The entity to which the field is attached.
* - $element['#view_mode']: View mode, e.g. 'full', 'teaser'...
* - $element['#field_name']: The field name.
* - $element['#field_type']: The field type.
* - $element['#field_language']: The field language.
* - $element['#field_translatable']: Whether the field is translatable or not.
* - $element['#label_display']: Position of label display, inline, above, or
*   hidden.
* - $field_name_css: The css-compatible field name.
* - $field_type_css: The css-compatible field type.
* - $classes_array: Array of html class attribute values. It is flattened
*   into a string within the variable $classes.
*`
* @see template_preprocess_field()
* @see theme_field()
*
* @ingroup themeable
*/
foreach ($element['#items'] as $item) {
    $url = cob_calendar_url($item['safe_value']);
    echo "<a href=\"{$url}\" class=\"cob-ext-google-calendar\" target=\"_new\">Explore on Google Calendar</a>";
}