| 37 | | $parent_handler_id = preg_replace('/^feed:/', '', $handler_id); |
|---|
| 38 | | if (!isset($data['request_switch'][$parent_handler_id])) |
|---|
| | 50 | // This is required and will throw error if we can't load it |
|---|
| | 51 | $_MIDCOM->load_library('de.bitfolge.feedcreator'); |
|---|
| | 52 | if ($GLOBALS['midcom_config']['positioning_enable']) |
|---|
| | 53 | { |
|---|
| | 54 | // Silently try to load this library if possible |
|---|
| | 55 | $_MIDCOM->componentloader->load_graceful('org.routamc.positioning'); |
|---|
| | 56 | } |
|---|
| | 57 | |
|---|
| | 58 | $data['parent_handler_id'] = preg_replace('/^feed[0-9]*:/', '', $handler_id); |
|---|
| | 59 | if (!isset($data['request_switch'][$data['parent_handler_id']])) |
|---|
| | 68 | |
|---|
| | 69 | if (!is_callable(array($this, $parent_method))) |
|---|
| | 70 | { |
|---|
| | 71 | // Fatal, parent method is not callable |
|---|
| | 72 | debug_add("Handler method \$this->{$parent_method} is not callable", MIDCOM_LOG_ERROR); |
|---|
| | 73 | debug_pop(); |
|---|
| | 74 | return false; |
|---|
| | 75 | } |
|---|
| | 76 | // Use array_pop so we can pass the args on to the parent handler and it will not get confused by too long argument list |
|---|
| | 77 | $feed_type_arg = array_pop($args); |
|---|
| | 78 | |
|---|
| | 79 | // This will also do some validations |
|---|
| | 80 | $this->_normalize_feed_type($feed_type_arg); |
|---|
| | 81 | $this->_resolve_feed_urls($args); |
|---|
| | 82 | |
|---|
| | 83 | debug_add("Calling \$this->{$parent_method}('{$data['parent_handler_id']}', \$args, \$data)"); |
|---|
| | 84 | if (!$this->$parent_method($data['parent_handler_id'], $args, $data)) |
|---|
| | 85 | { |
|---|
| | 86 | debug_add("\$this->{$parent_method}('{$data['parent_handler_id']}', \$args, \$data) returned false", MIDCOM_LOG_ERROR); |
|---|
| | 87 | debug_pop(); |
|---|
| | 88 | return false; |
|---|
| | 89 | } |
|---|
| | 90 | if (!isset($data['photos'])) |
|---|
| | 91 | { |
|---|
| | 92 | debug_add("\$data['photos'] is not set, most probably we hit a method ('{$parent_method}') in the list handler that does not populate it", MIDCOM_LOG_WARN); |
|---|
| | 93 | debug_pop(); |
|---|
| | 94 | return false; |
|---|
| | 95 | } |
|---|
| | 96 | $this->_create_feed(); |
|---|
| | 97 | |
|---|
| 50 | | if (!is_callable(array($this, $parent_method))) |
|---|
| 51 | | { |
|---|
| 52 | | // Fatal, parent method is not callable |
|---|
| 53 | | debug_add("Handler method \$this->{$parent_method} is not callable", MIDCOM_LOG_ERROR); |
|---|
| 54 | | debug_pop(); |
|---|
| 55 | | return false; |
|---|
| 56 | | } |
|---|
| 57 | | // Use array_pop so we can pass the args on to the parent handler and it will not get confused by too long argument list |
|---|
| 58 | | $data['feed_type_arg'] = array_pop($args); |
|---|
| 59 | | |
|---|
| 60 | | // TODO: Validate feed type before continuing |
|---|
| 61 | | |
|---|
| 62 | | debug_add("Calling \$this->{$parent_method}('{$parent_handler_id}', \$args, \$data)"); |
|---|
| 63 | | if (!$this->$parent_method($parent_handler_id, $args, $data)) |
|---|
| 64 | | { |
|---|
| 65 | | debug_add("\$this->{$parent_method}('{$parent_handler_id}', \$args, \$data) returned false", MIDCOM_LOG_ERROR); |
|---|
| 66 | | debug_pop(); |
|---|
| 67 | | return false; |
|---|
| 68 | | } |
|---|
| 69 | | if (!isset($data['photos'])) |
|---|
| 70 | | { |
|---|
| 71 | | debug_add("\$data['photos'] is not set, most probably we hit a method ('{$parent_method}') in the list handler that does not populate it", MIDCOM_LOG_WARN); |
|---|
| 72 | | debug_pop(); |
|---|
| 73 | | /* TODO: Check if this is correct way |
|---|
| 74 | | $_MIDCOM->generate_error(MIDCOM_ERR_NOTFOUND); |
|---|
| 75 | | // This will exit |
|---|
| 76 | | */ |
|---|
| 77 | | return false; |
|---|
| 78 | | } |
|---|
| | 101 | |
|---|
| | 102 | $this->_datamanager = new midcom_helper_datamanager2_datamanager($this->_request_data['schemadb']); |
|---|
| | 103 | |
|---|
| | 104 | // No failures thus far, set content type etc |
|---|
| | 105 | $_MIDCOM->cache->content->content_type("text/xml"); |
|---|
| | 106 | $_MIDCOM->header("Content-type: text/xml; charset=UTF-8"); |
|---|
| | 107 | $_MIDCOM->skip_page_style = true; |
|---|
| | 113 | /** |
|---|
| | 114 | * Reolves sane URL for both the HTML view and the feed |
|---|
| | 115 | */ |
|---|
| | 116 | function _resolve_feed_urls($args) |
|---|
| | 117 | { |
|---|
| | 118 | $data =& $this->_request_data; |
|---|
| | 119 | $data['prefix'] = $_MIDCOM->get_context_data(MIDCOM_CONTEXT_ANCHORPREFIX); |
|---|
| | 120 | $parent_switch =& $data['request_switch'][$data['parent_handler_id']]; |
|---|
| | 121 | $data['list_url'] = $data['prefix'] . implode('/', $parent_switch['fixed_args']) . '/'. implode('/', $args) . '/'; |
|---|
| | 122 | $data['feed_url'] = "{$data['prefix']}feed/" . implode('/', $parent_switch['fixed_args']) . '/'. implode('/', $args) . "/{$data['feed_filename']}"; |
|---|
| | 123 | } |
|---|
| | 124 | |
|---|
| | 125 | /** |
|---|
| | 126 | * Resolves and sanity-checks the feed type from the filename given as last argument to the dispatcher |
|---|
| | 127 | */ |
|---|
| | 128 | function _normalize_feed_type($type_str) |
|---|
| | 129 | { |
|---|
| | 130 | debug_push_class(__CLASS__, __FUNCTION__); |
|---|
| | 131 | $data =& $this->_request_data; |
|---|
| | 132 | $type = preg_replace('/\..*$/', '', strtolower($type_str)); |
|---|
| | 133 | debug_add("normalized '{$type_str}' to '{$type}'"); |
|---|
| | 134 | if ( !isset($this->_supported_types[$type]) |
|---|
| | 135 | || empty($this->_supported_types[$type])) |
|---|
| | 136 | { |
|---|
| | 137 | debug_add("Feed type '{$type}' is not supported", MIDCOM_LOG_ERROR); |
|---|
| | 138 | debug_pop(); |
|---|
| | 139 | $_MIDCOM->generate_error(MIDCOM_ERRNOTFOUND, "Feed type '{$type}' is not supported"); |
|---|
| | 140 | // This will exit() |
|---|
| | 141 | } |
|---|
| | 142 | $data['feed_type'] = $this->_supported_types[$type]; |
|---|
| | 143 | $data['feed_filename'] = "{$type}.xml"; |
|---|
| | 144 | } |
|---|
| | 145 | |
|---|
| | 146 | /** |
|---|
| | 147 | * Instantiate the feed object and set base value |
|---|
| | 148 | */ |
|---|
| | 149 | function _create_feed() |
|---|
| | 150 | { |
|---|
| | 151 | $data =& $this->_request_data; |
|---|
| | 152 | $this->_feed = new UniversalFeedCreator(); |
|---|
| | 153 | $this->_feed->cssStyleSheet = false; |
|---|
| | 154 | if ( isset($data['view_title']) |
|---|
| | 155 | && !empty($data['view_title'])) |
|---|
| | 156 | { |
|---|
| | 157 | $this->_feed->title = $data['view_title']; |
|---|
| | 158 | } |
|---|
| | 159 | else |
|---|
| | 160 | { |
|---|
| | 161 | $this->_feed->title = $this->_topic->extra; |
|---|
| | 162 | } |
|---|
| | 163 | $this->_feed->language = $this->_config->get('rss_language'); |
|---|
| | 164 | $this->_feed->editor = $this->_config->get('rss_webmaster'); |
|---|
| | 165 | $this->_feed->link = $data['list_url']; |
|---|
| | 166 | $this->_feed->syndicationURL = $data['feed_url']; |
|---|
| | 167 | } |
|---|
| | 168 | |
|---|
| | 169 | /** |
|---|
| | 170 | * Add each photo as item to the feed then creates the XML and |
|---|
| | 171 | * finally calls an element if one wishes to mangle the raw feed data |
|---|
| | 172 | */ |
|---|
| 86 | | echo "<p>Handler '{$handler_id}' says: Hello world!</p>"; |
|---|
| 87 | | /* |
|---|
| 88 | | echo "request_data: <pre>\n"; |
|---|
| 89 | | print_r($data); |
|---|
| 90 | | echo "</pre>\n"; |
|---|
| 91 | | */ |
|---|
| | 175 | foreach($data['photos'] as $photo) |
|---|
| | 176 | { |
|---|
| | 177 | $this->_add_photo_to_feed($photo); |
|---|
| | 178 | } |
|---|
| | 179 | $data['feed_data'] = $this->_feed->createFeed($data['feed_type']); |
|---|
| | 180 | ob_start(); |
|---|
| | 181 | midcom_show_style('mangle_feed_data'); |
|---|
| | 182 | ob_end_clean(); |
|---|
| | 183 | echo $data['feed_data']; |
|---|
| | 184 | unset($data['feed_data']); |
|---|
| | 185 | } |
|---|
| | 186 | |
|---|
| | 187 | /** |
|---|
| | 188 | * This method creates a feed item for a photo object given |
|---|
| | 189 | * It uses the style-engine to render the description and to |
|---|
| | 190 | * allow custom hacks to mangle the item data |
|---|
| | 191 | */ |
|---|
| | 192 | function _add_photo_to_feed(&$photo) |
|---|
| | 193 | { |
|---|
| | 194 | $data =& $this->_request_data; |
|---|
| | 195 | $item = new FeedItem(); |
|---|
| | 196 | $item->title = $photo->title; |
|---|
| | 197 | $item->link = "{$data['prefix']}photo/{$photo->guid}/"; |
|---|
| | 198 | $item->date = $photo->taken; |
|---|
| | 199 | $data['item'] =& $item; |
|---|
| | 200 | |
|---|
| | 201 | if (!$this->_datamanager->autoset_storage($photo)) |
|---|
| | 202 | { |
|---|
| | 203 | return false; |
|---|
| | 204 | } |
|---|
| | 205 | $data['datamanager'] =& $this->_datamanager; |
|---|
| | 206 | $data['photo'] =& $photo; |
|---|
| | 207 | $data['photo_view'] = $this->_datamanager->get_content_html(); |
|---|
| | 208 | $thumbnail = false; |
|---|
| | 209 | if ( isset($data['datamanager']->types['photo']) |
|---|
| | 210 | && isset($data['datamanager']->types['photo']->attachments_info['thumbnail'])) |
|---|
| | 211 | { |
|---|
| | 212 | $thumbnail = $data['datamanager']->types['photo']->attachments_info['thumbnail']; |
|---|
| | 213 | } |
|---|
| | 214 | if ($thumbnail) |
|---|
| | 215 | { |
|---|
| | 216 | $item->thumb = $thumbnail['url']; |
|---|
| | 217 | } |
|---|
| | 218 | $tags = net_nemein_tag_handler::get_object_tags($photo); |
|---|
| | 219 | if (!empty($tags)) |
|---|
| | 220 | { |
|---|
| | 221 | // Use first tag as category |
|---|
| | 222 | $item->category = array_shift($tags); |
|---|
| | 223 | } |
|---|
| | 224 | |
|---|
| | 225 | ob_start(); |
|---|
| | 226 | midcom_show_style('render_feed_item'); |
|---|
| | 227 | $item->description = ob_get_contents(); |
|---|
| | 228 | ob_end_clean(); |
|---|
| | 229 | |
|---|
| | 230 | if (class_exists('org_routamc_positioning_object')) |
|---|
| | 231 | { |
|---|
| | 232 | // Attach coordinates to the item if available |
|---|
| | 233 | $object_position = new org_routamc_positioning_object($photo); |
|---|
| | 234 | $coordinates = $object_position->get_coordinates(); |
|---|
| | 235 | if (!is_null($coordinates)) |
|---|
| | 236 | { |
|---|
| | 237 | $item->lat = $coordinates['latitude']; |
|---|
| | 238 | $item->long = $coordinates['longitude']; |
|---|
| | 239 | } |
|---|
| | 240 | } |
|---|
| | 241 | // Replace links, TODO: This should be a feature of feedcreator... |
|---|
| | 242 | $item->description = preg_replace(',<(a|link|img|script|form|input)([^>]+)(href|src|action)="/([^>"\s]+)",ie', '"<\1\2\3=\"' . $_MIDCOM->get_host_name() . '/\4\""', $item->description); |
|---|
| | 243 | |
|---|
| | 244 | ob_start(); |
|---|
| | 245 | midcom_show_style('mangle_feed_item'); |
|---|
| | 246 | ob_end_clean(); |
|---|
| | 247 | $this->_feed->addItem($item); |
|---|