/** * Imports an item as an event */ private function import_event($item) { // Check that we're trying to import item suitable to be an event if (!isset($item['xcal']) && !isset($item['gd']['when@'])) { // Not an event return false; } // Get start and end times $start = null; $end = null; if (isset($item['xcal']['dtstart'])) { // xCal RSS feed, for example Upcoming or Last.fm $start = strtotime($item['xcal']['dtstart']); } elseif (isset($item['gd']['when@starttime'])) { // gData Atom feed, for example Dopplr $start = strtotime($item['gd']['when@starttime']); } if (isset($item['xcal']['dtend'])) { $end = strtotime($item['xcal']['dtend']); } elseif (isset($item['gd']['when@starttime'])) { $end = strtotime($item['gd']['when@endtime']); } if (!$start || !$end) { return false; } if (!$this->_datamanager) { $schemadb = midcom_helper_datamanager2_schema::load_database($this->_node_config->get('schemadb')); $this->_datamanager = new midcom_helper_datamanager2_datamanager($schemadb); } // TODO: Move to real geocoded stuff $location_parts = array(); if (isset($item['xcal']['x-calconnect-venue_adr_x-calconnect-venue-name'])) { $location_parts[] = $item['xcal']['x-calconnect-venue_adr_x-calconnect-venue-name']; } if (isset($item['xcal']['x-calconnect-venue_adr_x-calconnect-street'])) { $location_parts[] = $item['xcal']['x-calconnect-venue_adr_x-calconnect-street']; } if (isset($item['xcal']['x-calconnect-venue_adr_x-calconnect-city'])) { $location_parts[] = $item['xcal']['x-calconnect-venue_adr_x-calconnect-city']; } if (isset($item['gd']['where@valuestring'])) { $wherevalues = explode(' ', $item['gd']['where@valuestring']); foreach ($wherevalues as $val) { $location_parts[] = $val; } } $qb = net_nemein_calendar_event_dba::new_query_builder(); $qb->add_constraint('node', '=', $this->_feed->node); $qb->add_constraint('extra', '=', md5($item['guid'])); $events = $qb->execute(); if (count($events) > 0) { // This item has been imported already earlier. Update $event = $events[0]; $event->_activitystream_verb = 'http://community-equity.org/schema/1.0/clone'; $event->_rcs_message = sprintf(midcom::get('i18n')->get_string('%s was imported from %s', 'net.nemein.rss'), $event->title, $this->_feed->title); $event->allow_name_catenate = true; if (empty($event->name)) { $resolver = new midcom_helper_reflector_nameresolver($event); // To prevent validation errors in case the auto-catenate is not allowed in the urlname datatype $event->name = $resolver->generate_unique_name(); } } else { $node = new midcom_db_topic($this->_feed->node); $node_lang_code = $node->get_parameter('net.nemein.calendar', 'language'); // This is a new item $event = new net_nemein_calendar_event_dba(); $event->start = $start; $event->end = $end; $event->extra = md5($item['guid']); $event->node = $this->_feed->node; if ($node->get_parameter('net.nemein.calendar', 'symlink_topic') != '') { try { $symlink_topic = new midcom_db_topic($node->get_parameter('net.nemein.calendar', 'symlink_topic')); $event->node = $symlink_topic->id; } catch (midcom_error $e) { $e->log(); } } if ($node_lang_code != '') { $lang_id = midcom::get('i18n')->code_to_id($node_lang_code); $event->lang = $lang_id; } $event->allow_name_catenate = true; $event->title = (string) $item['title']; $event->_activitystream_verb = 'http://community-equity.org/schema/1.0/clone'; $event->_rcs_message = sprintf(midcom::get('i18n')->get_string('%s was imported from %s', 'net.nemein.rss'), $event->title, $this->_feed->title); $resolver = new midcom_helper_reflector_nameresolver($event); $event->name = $resolver->generate_unique_name(); if (!$event->create()) { return false; } } $this->_datamanager->autoset_storage($event); $this->_datamanager->types['start']->value = new DateTime(strftime('%Y-%m-%d %H:%M:%S', $start)); $this->_datamanager->types['end']->value = new DateTime(strftime('%Y-%m-%d %H:%M:%S', $end)); if (is_a($this->_datamanager->types['location'], 'midcom_helper_datamanager2_type_position')) { // Position type, give all values we got, assume order "Street, City, Country" $location_parts = array_reverse($location_parts); if (count($location_parts) > 0) { $country = org_routamc_positioning_country_dba::get_by_name($location_parts[0]); if ($country && $country->code) { $this->_datamanager->types['location']->location->county = $country->code; } } if (count($location_parts) > 1) { $city = org_routamc_positioning_city_dba::get_by_name($location_parts[1]); if ($city && $city->id) { $this->_datamanager->types['location']->location->city = $city->id; } } if (count($location_parts) > 2) { $this->_datamanager->types['location']->location->street = $location_parts[2]; } if (isset($item['gml'])) { $gml_parts = explode(' ', $item['gml']['where_point_pos']); if (count($gml_parts) == 2) { $this->_datamanager->types['location']->location->latitude = (double) $gml_parts[0]; $this->_datamanager->types['location']->location->longitude = (double) $gml_parts[1]; } } } else { // Just give the location string we got $this->_datamanager->types['location']->value = implode(', ', $location_parts); } foreach ($item as $key => $value) { if (isset($this->_datamanager->types[$key])) { $this->_datamanager->types[$key]->value = $value; } } if (!$this->_datamanager->save()) { return false; } // This should be unnecessary but left in place just to be sure if (strlen($this->_datamanager->storage->object->name) == 0) { // Generate something to avoid empty "/" links in case of failures $this->_datamanager->storage->object->name = time(); $this->_datamanager->storage->object->update(); } $this->parse_tags($event, $item, 'description'); $this->parse_parameters($event, $item); return $event->guid; }
/** * Loads the datamanager for this instance. This will patch the schema in case we * are dealing with an article. */ function load_datamanager() { static $schemadbs = array(); if (!array_key_exists($this->_schemadb_path, $schemadbs)) { $schemadbs[$this->_schemadb_path] = midcom_helper_datamanager2_schema::load_database($this->_schemadb_path); } $this->_schemadb = $schemadbs[$this->_schemadb_path]; $this->_datamanager = new midcom_helper_datamanager2_datamanager($this->_schemadb); // Check if we have metadata schema defined in the schemadb specific for the object's schema or component $object_schema = $this->__object->get_parameter('midcom.helper.datamanager2', 'schema_name'); $component_schema = str_replace('.', '_', midcom_core_context::get()->get_key(MIDCOM_CONTEXT_COMPONENT)); if ($object_schema == '' || !isset($this->_schemadb[$object_schema])) { if (isset($this->_schemadb[$component_schema])) { // No specific metadata schema for object, fall back to component-specific metadata schema $object_schema = $component_schema; } else { // No metadata schema for component, fall back to default $object_schema = 'metadata'; } } $this->_datamanager->set_schema($object_schema); if (!$this->_datamanager->set_storage($this->__object)) { throw new midcom_error('Failed to initialize the metadata datamanager instance, see the Debug Log for details.'); } }