<commit_before><?php

Kurogo::includePackage('Calendar');

class CalendarAPIModule extends APIModule
{
    const ERROR_NO_SUCH_EVENT = 50;

    protected $id = 'calendar';
    protected $vmin = 1;
    protected $vmax = 1;

    protected $timezone;
    protected $fieldConfig;
    protected $feeds = array();

    protected function getCalendarsForGroup($groupConfig) {
        $calendars = array();
        $type = $groupConfig['type'];
        if (isset($groupConfig['calendars'])) {
            foreach ($groupConfig['calendars'] as $calendarId) {
                $feedsForType = $this->getFeeds($type);
                if (isset($feedsForType[$calendarId])) {
                    $calendarData = $feedsForType[$calendarId];
                    $calendars[] = array(
                        'id' => strval($calendarId),
                        'title' => $calendarData['TITLE'],
                        'type' => $type,
                        );
                }
            }

        } elseif (isset($groupConfig['all'])) {
            foreach ($this->getFeeds($type) as $feedId => $feedData) {
                $calendars[] = array(
                    'id' => strval($feedId),
                    'title' => $feedData['TITLE'],
                    'type' => $type,
                    );
            }
        }
        return $calendars;
    }

    // modified from CalendarWebModule
    protected function getFeedsByType() {
        $groups = $this->getAPIConfigData('groups');
        $feeds = array();
        foreach ($groups as $groupConfig) {
            $feedGroup = array(
                'id' => strval($groupConfig['id']),
                'title' => $groupConfig['title'],
                'calendars' => $this->getCalendarsForGroup($groupConfig),
                );

            if (count($feedGroup['calendars'])) {
                $feeds[] = $feedGroup;
            }
        }
        return $feeds;
    }

    // from CalendarWebModule

    protected function getFeeds($type) {
        if (isset($this->feeds[$type])) {
            return $this->feeds[$type];
        }

        $feeds = array();
        switch ($type) {
            case 'static':
                $feeds = $this->loadFeedData();
                break;

            case 'user':
                $sectionData = $this->getOptionalModuleSection('user_calendars');
                $listController = isset($sectionData['CONTROLLER_CLASS']) ? $sectionData['CONTROLLER_CLASS'] : '';
                if (strlen($listController)) {
                    $sectionData = array_merge($sectionData, array('SESSION'=>$this->getSession()));
                    $controller = CalendarListController::factory($listController, $sectionData);
                    $feeds = $controller->getUserCalendars();
                }
                break;

            case 'resource':
                $sectionData = $this->getOptionalModuleSection('resources');
                $listController = isset($sectionData['CONTROLLER_CLASS']) ? $sectionData['CONTROLLER_CLASS'] : '';
                if (strlen($listController)) {
                    $sectionData = array_merge($sectionData, array('SESSION'=>$this->getSession()));
                    $controller = CalendarListController::factory($listController, $sectionData);
                    $feeds = $controller->getResources();
                }
                break;
                
            case 'category':
                $sectionData = $this->getOptionalModuleSection('categories');
                $controllerClass = isset($sectionData['CONTROLLER_CLASS']) ? $sectionData['CONTROLLER_CLASS'] : '';
                if (strlen($controllerClass)) {
                    $controller = DataController::factory($controllerClass, $sectionData);
                    foreach ($controller->items() as $category) {
                        $feeds[$category->getId()] = array(
                            'TITLE' => $category->getName(),
                            'CATEGORY' => $category->getId(),
                            'BASE_URL' => $sectionData['EVENT_BASE_URL'],
                            'CONTROLLER_CLASS' => $sectionData['EVENT_CONTROLLER_CLASS'],
                            );
                    }
                }
                break;

            default:
                throw new KurogoConfigurationException($this->getLocalizedString('ERROR_INVALID_FEED', $type));
        }

        if ($feeds) {
            foreach ($feeds as $id => &$feed) {
                $feed['type'] = $type;
            }

            $this->feeds[$type] = $feeds;
        }

        return $feeds;
    }

    public function getDefaultFeed($type) {
        $feeds = $this->getFeeds($type);
        if ($indexes = array_keys($feeds)) {
            return current($indexes);
        }
    }

    private function getFeedData($index, $type) {
        $feeds = $this->getFeeds($type);
        if (isset($feeds[$index])) {
            return $feeds[$index];
        }
    }
    
    public function getFeed($index, $type) {
        $controller = null;
        $feeds = $this->getFeeds($type);
        if (isset($feeds[$index])) {
            $feedData = $feeds[$index];
            if (!isset($feedData['CONTROLLER_CLASS'])) {
                $feedData['CONTROLLER_CLASS'] = 'CalendarDataController';
            }
            $controller = CalendarDataController::factory($feedData['CONTROLLER_CLASS'],$feedData);
        } else {
            throw new KurogoDataException($this->getLocalizedString('ERROR_NO_CALENDAR_FEED', $index));
        }
        return $controller;
    }

    protected function apiArrayFromEvent(ICalEvent $event, $version) {
        $standardAttributes = array(
          'datetime', 'start', 'end', 'uid', 'summary', 'description', 'location', 'geo');
        
        $result = array(
            'id'            => $event->get_uid(),
            'title'         => $event->get_summary(),
            'description'   => $event->get_description(),
            'start'         => $event->get_start(),
            'end'           => $event->get_end(),
            'allday'        => ($event->get_range() instanceOf DayRange),
            'location'      => $event->get_location(),
            'locationLabel' => 'Location', // subclass to add dynamic title to location
        );

        // iCal GEO property -- subclass if event lat/lon comes from somewhere else
        $coords = $event->get_location_coordinates();
        if ($coords) {
          $result['latitude'] = $coords['lat'];
          $result['longitude'] = $coords['lon'];
        }
        
        foreach ($this->fieldConfig as $aField => $fieldInfo) {
            if (in_array($aField, $standardAttributes)) { continue; } // Handled these above
            
            $id      = self::argVal($fieldInfo, 'id', $aField);
            $title   = self::argVal($fieldInfo, 'label', $id);
            $section = self::argVal($fieldInfo, 'section', '');
            $type    = self::argVal($fieldInfo, 'type', '');
            $value   = $event->get_attribute($aField);<commit_msg>Default to no location label<commit_after><?php

Kurogo::includePackage('Calendar');

class CalendarAPIModule extends APIModule
{
    const ERROR_NO_SUCH_EVENT = 50;

    protected $id = 'calendar';
    protected $vmin = 1;
    protected $vmax = 1;

    protected $timezone;
    protected $fieldConfig;
    protected $feeds = array();

    protected function getCalendarsForGroup($groupConfig) {
        $calendars = array();
        $type = $groupConfig['type'];
        if (isset($groupConfig['calendars'])) {
            foreach ($groupConfig['calendars'] as $calendarId) {
                $feedsForType = $this->getFeeds($type);
                if (isset($feedsForType[$calendarId])) {
                    $calendarData = $feedsForType[$calendarId];
                    $calendars[] = array(
                        'id' => strval($calendarId),
                        'title' => $calendarData['TITLE'],
                        'type' => $type,
                        );
                }
            }

        } elseif (isset($groupConfig['all'])) {
            foreach ($this->getFeeds($type) as $feedId => $feedData) {
                $calendars[] = array(
                    'id' => strval($feedId),
                    'title' => $feedData['TITLE'],
                    'type' => $type,
                    );
            }
        }
        return $calendars;
    }

    // modified from CalendarWebModule
    protected function getFeedsByType() {
        $groups = $this->getAPIConfigData('groups');
        $feeds = array();
        foreach ($groups as $groupConfig) {
            $feedGroup = array(
                'id' => strval($groupConfig['id']),
                'title' => $groupConfig['title'],
                'calendars' => $this->getCalendarsForGroup($groupConfig),
                );

            if (count($feedGroup['calendars'])) {
                $feeds[] = $feedGroup;
            }
        }
        return $feeds;
    }

    // from CalendarWebModule

    protected function getFeeds($type) {
        if (isset($this->feeds[$type])) {
            return $this->feeds[$type];
        }

        $feeds = array();
        switch ($type) {
            case 'static':
                $feeds = $this->loadFeedData();
                break;

            case 'user':
                $sectionData = $this->getOptionalModuleSection('user_calendars');
                $listController = isset($sectionData['CONTROLLER_CLASS']) ? $sectionData['CONTROLLER_CLASS'] : '';
                if (strlen($listController)) {
                    $sectionData = array_merge($sectionData, array('SESSION'=>$this->getSession()));
                    $controller = CalendarListController::factory($listController, $sectionData);
                    $feeds = $controller->getUserCalendars();
                }
                break;

            case 'resource':
                $sectionData = $this->getOptionalModuleSection('resources');
                $listController = isset($sectionData['CONTROLLER_CLASS']) ? $sectionData['CONTROLLER_CLASS'] : '';
                if (strlen($listController)) {
                    $sectionData = array_merge($sectionData, array('SESSION'=>$this->getSession()));
                    $controller = CalendarListController::factory($listController, $sectionData);
                    $feeds = $controller->getResources();
                }
                break;
                
            case 'category':
                $sectionData = $this->getOptionalModuleSection('categories');
                $controllerClass = isset($sectionData['CONTROLLER_CLASS']) ? $sectionData['CONTROLLER_CLASS'] : '';
                if (strlen($controllerClass)) {
                    $controller = DataController::factory($controllerClass, $sectionData);
                    foreach ($controller->items() as $category) {
                        $feeds[$category->getId()] = array(
                            'TITLE' => $category->getName(),
                            'CATEGORY' => $category->getId(),
                            'BASE_URL' => $sectionData['EVENT_BASE_URL'],
                            'CONTROLLER_CLASS' => $sectionData['EVENT_CONTROLLER_CLASS'],
                            );
                    }
                }
                break;

            default:
                throw new KurogoConfigurationException($this->getLocalizedString('ERROR_INVALID_FEED', $type));
        }

        if ($feeds) {
            foreach ($feeds as $id => &$feed) {
                $feed['type'] = $type;
            }

            $this->feeds[$type] = $feeds;
        }

        return $feeds;
    }

    public function getDefaultFeed($type) {
        $feeds = $this->getFeeds($type);
        if ($indexes = array_keys($feeds)) {
            return current($indexes);
        }
    }

    private function getFeedData($index, $type) {
        $feeds = $this->getFeeds($type);
        if (isset($feeds[$index])) {
            return $feeds[$index];
        }
    }
    
    public function getFeed($index, $type) {
        $controller = null;
        $feeds = $this->getFeeds($type);
        if (isset($feeds[$index])) {
            $feedData = $feeds[$index];
            if (!isset($feedData['CONTROLLER_CLASS'])) {
                $feedData['CONTROLLER_CLASS'] = 'CalendarDataController';
            }
            $controller = CalendarDataController::factory($feedData['CONTROLLER_CLASS'],$feedData);
        } else {
            throw new KurogoDataException($this->getLocalizedString('ERROR_NO_CALENDAR_FEED', $index));
        }
        return $controller;
    }

    protected function apiArrayFromEvent(ICalEvent $event, $version) {
        $standardAttributes = array(
          'datetime', 'start', 'end', 'uid', 'summary', 'description', 'location', 'geo');
        
        $result = array(
            'id'            => $event->get_uid(),
            'title'         => $event->get_summary(),
            'description'   => $event->get_description(),
            'start'         => $event->get_start(),
            'end'           => $event->get_end(),
            'allday'        => ($event->get_range() instanceOf DayRange),
            'location'      => $event->get_location(),
            'locationLabel' => '', // subclass to add dynamic title to location
        );

        // iCal GEO property -- subclass if event lat/lon comes from somewhere else
        $coords = $event->get_location_coordinates();
        if ($coords) {
          $result['latitude'] = $coords['lat'];
          $result['longitude'] = $coords['lon'];
        }
        
        foreach ($this->fieldConfig as $aField => $fieldInfo) {
            if (in_array($aField, $standardAttributes)) { continue; } // Handled these above
            
            $id      = self::argVal($fieldInfo, 'id', $aField);
            $title   = self::argVal($fieldInfo, 'label', $id);
            $section = self::argVal($fieldInfo, 'section', '');
            $type    = self::argVal($fieldInfo, 'type', '');
            $value   = $event->get_attribute($aField);
{'fraction_non_alphanumeric': 0.144540899146023, 'fraction_numerical': 0.0005646128872891523, 'mean_word_length': 1.7573457871181164, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 123, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3858365', 'n_tokens_mistral': 3837, 'n_tokens_neox': 3704, 'n_words': 662}