00001 <?php
00002
00003
00015 function aggregator_page_last() {
00016 drupal_add_feed(url('aggregator/rss'), variable_get('site_name', 'Drupal') . ' ' . t('aggregator'));
00017
00018 $items = aggregator_feed_items_load('SELECT i.*, f.title AS ftitle, f.link AS flink FROM {aggregator_item} i INNER JOIN {aggregator_feed} f ON i.fid = f.fid ORDER BY i.timestamp DESC, i.iid DESC');
00019
00020 return _aggregator_page_list($items, arg(1));
00021 }
00022
00035 function aggregator_page_source($arg1, $arg2 = NULL) {
00036
00037
00038 $feed = is_array($arg2) ? $arg2 : $arg1;
00039 $feed = (object)$feed;
00040 drupal_set_title(check_plain($feed->title));
00041 $feed_source = theme('aggregator_feed_source', $feed);
00042
00043
00044
00045 $items = aggregator_feed_items_load('SELECT * FROM {aggregator_item} WHERE fid = ' . $feed->fid . ' ORDER BY timestamp DESC, iid DESC');
00046
00047 return _aggregator_page_list($items, arg(3), $feed_source);
00048 }
00049
00062 function aggregator_page_category($arg1, $arg2 = NULL) {
00063 drupal_set_breadcrumb(array_merge(drupal_get_breadcrumb(), array(l(t('Categories'), 'aggregator/categories'))));
00064
00065
00066 $category = is_array($arg2) ? $arg2 : $arg1;
00067
00068 drupal_add_feed(url('aggregator/rss/' . $category['cid']), variable_get('site_name', 'Drupal') . ' ' . t('aggregator - @title', array('@title' => $category['title'])));
00069
00070
00071
00072 $items = aggregator_feed_items_load('SELECT i.*, f.title AS ftitle, f.link AS flink FROM {aggregator_category_item} c LEFT JOIN {aggregator_item} i ON c.iid = i.iid LEFT JOIN {aggregator_feed} f ON i.fid = f.fid WHERE cid = ' . $category['cid'] . ' ORDER BY timestamp DESC, i.iid DESC');
00073
00074 return _aggregator_page_list($items, arg(3));
00075 }
00076
00085 function aggregator_feed_items_load($sql) {
00086 $items = array();
00087 if (isset($sql)) {
00088 $result = pager_query($sql, 20);
00089 while ($item = db_fetch_object($result)) {
00090 $result_category = db_query('SELECT c.title, c.cid FROM {aggregator_category_item} ci LEFT JOIN {aggregator_category} c ON ci.cid = c.cid WHERE ci.iid = %d ORDER BY c.title', $item->iid);
00091 $item->categories = array();
00092 while ($item_categories = db_fetch_object($result_category)) {
00093 $item->categories[] = $item_categories;
00094 }
00095 $items[$item->iid] = $item;
00096 }
00097 }
00098
00099 return $items;
00100 }
00101
00116 function _aggregator_page_list($items, $op, $feed_source = '') {
00117 if (user_access('administer news feeds') && ($op == 'categorize')) {
00118
00119 $output = aggregator_categorize_items($items, $feed_source);
00120 }
00121 else {
00122
00123 $output = $feed_source;
00124 foreach ($items as $item) {
00125 $output .= theme('aggregator_item', $item);
00126 }
00127 $output = theme('aggregator_wrapper', $output);
00128 }
00129
00130 return $output;
00131 }
00132
00146 function aggregator_categorize_items($items, $feed_source = '') {
00147 $form['#submit'][] = 'aggregator_categorize_items_submit';
00148 $form['#validate'][] = 'aggregator_categorize_items_validate';
00149 $form['#theme'] = 'aggregator_categorize_items';
00150 $form['feed_source'] = array(
00151 '#value' => $feed_source,
00152 );
00153 $categories = array();
00154 $done = FALSE;
00155 $form['items'] = array();
00156 $form['categories'] = array(
00157 '#tree' => TRUE,
00158 );
00159 foreach ($items as $item) {
00160 $form['items'][$item->iid] = array('#value' => theme('aggregator_item', $item));
00161 $form['categories'][$item->iid] = array();
00162 $categories_result = db_query('SELECT c.cid, c.title, ci.iid FROM {aggregator_category} c LEFT JOIN {aggregator_category_item} ci ON c.cid = ci.cid AND ci.iid = %d', $item->iid);
00163 $selected = array();
00164 while ($category = db_fetch_object($categories_result)) {
00165 if (!$done) {
00166 $categories[$category->cid] = check_plain($category->title);
00167 }
00168 if ($category->iid) {
00169 $selected[] = $category->cid;
00170 }
00171 }
00172 $done = TRUE;
00173 $form['categories'][$item->iid] = array(
00174 '#type' => variable_get('aggregator_category_selector', 'checkboxes'),
00175 '#default_value' => $selected,
00176 '#options' => $categories,
00177 '#size' => 10,
00178 '#multiple' => TRUE
00179 );
00180 }
00181 $form['submit'] = array('#type' => 'submit', '#value' => t('Save categories'));
00182
00183 return $form;
00184 }
00185
00189 function aggregator_categorize_items_validate($form, &$form_state) {
00190 if (!user_access('administer news feeds')) {
00191 form_error($form, t('You are not allowed to categorize this feed item.'));
00192 }
00193 }
00194
00198 function aggregator_categorize_items_submit($form, &$form_state) {
00199 if (!empty($form_state['values']['categories'])) {
00200 foreach ($form_state['values']['categories'] as $iid => $selection) {
00201 db_query('DELETE FROM {aggregator_category_item} WHERE iid = %d', $iid);
00202 foreach ($selection as $cid) {
00203 if ($cid) {
00204 db_query('INSERT INTO {aggregator_category_item} (cid, iid) VALUES (%d, %d)', $cid, $iid);
00205 }
00206 }
00207 }
00208 }
00209 drupal_set_message(t('The categories have been saved.'));
00210 }
00211
00221 function theme_aggregator_categorize_items($form) {
00222 $output = drupal_render($form['feed_source']);
00223 $rows = array();
00224 if ($form['items']) {
00225 foreach (element_children($form['items']) as $key) {
00226 if (is_array($form['items'][$key])) {
00227 $rows[] = array(
00228 drupal_render($form['items'][$key]),
00229 array('data' => drupal_render($form['categories'][$key]), 'class' => 'categorize-item'),
00230 );
00231 }
00232 }
00233 }
00234 $output .= theme('table', array('', t('Categorize')), $rows);
00235 $output .= drupal_render($form['submit']);
00236 $output .= drupal_render($form);
00237
00238 return theme('aggregator_wrapper', $output);
00239 }
00240
00246 function template_preprocess_aggregator_wrapper(&$variables) {
00247 $variables['pager'] = theme('pager', NULL, 20, 0);
00248 }
00249
00255 function template_preprocess_aggregator_item(&$variables) {
00256 $item = $variables['item'];
00257
00258 $variables['feed_url'] = check_url($item->link);
00259 $variables['feed_title'] = check_plain($item->title);
00260 $variables['content'] = aggregator_filter_xss($item->description);
00261
00262 $variables['source_url'] = '';
00263 $variables['source_title'] = '';
00264 if (isset($item->ftitle) && isset($item->fid)) {
00265 $variables['source_url'] = url("aggregator/sources/$item->fid");
00266 $variables['source_title'] = check_plain($item->ftitle);
00267 }
00268 if (date('Ymd', $item->timestamp) == date('Ymd')) {
00269 $variables['source_date'] = t('%ago ago', array('%ago' => format_interval(time() - $item->timestamp)));
00270 }
00271 else {
00272 $variables['source_date'] = format_date($item->timestamp, 'custom', variable_get('date_format_medium', 'D, m/d/Y - H:i'));
00273 }
00274
00275 $variables['categories'] = array();
00276 foreach ($item->categories as $category) {
00277 $variables['categories'][$category->cid] = l($category->title, 'aggregator/categories/' . $category->cid);
00278 }
00279 }
00280
00284 function aggregator_page_sources() {
00285 $result = db_query('SELECT f.fid, f.title, f.description, f.image, MAX(i.timestamp) AS last FROM {aggregator_feed} f LEFT JOIN {aggregator_item} i ON f.fid = i.fid GROUP BY f.fid, f.title, f.description, f.image ORDER BY last DESC, f.title');
00286
00287 $output = '';
00288 while ($feed = db_fetch_object($result)) {
00289
00290 $summary_items = array();
00291 if (variable_get('aggregator_summary_items', 3)) {
00292 $items = db_query_range('SELECT i.title, i.timestamp, i.link FROM {aggregator_item} i WHERE i.fid = %d ORDER BY i.timestamp DESC', $feed->fid, 0, variable_get('aggregator_summary_items', 3));
00293 while ($item = db_fetch_object($items)) {
00294 $summary_items[] = theme('aggregator_summary_item', $item);
00295 }
00296 }
00297 $feed->url = url('aggregator/sources/' . $feed->fid);
00298 $output .= theme('aggregator_summary_items', $summary_items, $feed);
00299 }
00300 $output .= theme('feed_icon', url('aggregator/opml'), t('OPML feed'));
00301
00302 return theme('aggregator_wrapper', $output);
00303 }
00304
00308 function aggregator_page_categories() {
00309 $result = db_query('SELECT c.cid, c.title, c.description FROM {aggregator_category} c LEFT JOIN {aggregator_category_item} ci ON c.cid = ci.cid LEFT JOIN {aggregator_item} i ON ci.iid = i.iid GROUP BY c.cid, c.title, c.description');
00310
00311 $output = '';
00312 while ($category = db_fetch_object($result)) {
00313 if (variable_get('aggregator_summary_items', 3)) {
00314 $summary_items = array();
00315 $items = db_query_range('SELECT i.title, i.timestamp, i.link, f.title as feed_title, f.link as feed_link FROM {aggregator_category_item} ci LEFT JOIN {aggregator_item} i ON i.iid = ci.iid LEFT JOIN {aggregator_feed} f ON i.fid = f.fid WHERE ci.cid = %d ORDER BY i.timestamp DESC', $category->cid, 0, variable_get('aggregator_summary_items', 3));
00316 while ($item = db_fetch_object($items)) {
00317 $summary_items[] = theme('aggregator_summary_item', $item);
00318 }
00319 }
00320 $category->url = url('aggregator/categories/' . $category->cid);
00321 $output .= theme('aggregator_summary_items', $summary_items, $category);
00322 }
00323
00324 return theme('aggregator_wrapper', $output);
00325 }
00326
00330 function aggregator_page_rss() {
00331 $result = NULL;
00332
00333 if (arg(2)) {
00334 $category = db_fetch_object(db_query('SELECT cid, title FROM {aggregator_category} WHERE cid = %d', arg(2)));
00335 $sql = 'SELECT i.*, f.title AS ftitle, f.link AS flink FROM {aggregator_category_item} c LEFT JOIN {aggregator_item} i ON c.iid = i.iid LEFT JOIN {aggregator_feed} f ON i.fid = f.fid WHERE cid = %d ORDER BY timestamp DESC, i.iid DESC';
00336 $result = db_query_range($sql, $category->cid, 0, variable_get('feed_default_items', 10));
00337 }
00338
00339 else {
00340 $category = NULL;
00341 $sql = 'SELECT i.*, f.title AS ftitle, f.link AS flink FROM {aggregator_item} i INNER JOIN {aggregator_feed} f ON i.fid = f.fid ORDER BY i.timestamp DESC, i.iid DESC';
00342 $result = db_query_range($sql, 0, variable_get('feed_default_items', 10));
00343 }
00344
00345 $feeds = array();
00346 while ($item = db_fetch_object($result)) {
00347 $feeds[] = $item;
00348 }
00349
00350 return theme('aggregator_page_rss', $feeds, $category);
00351 }
00352
00362 function theme_aggregator_page_rss($feeds, $category = NULL) {
00363 drupal_set_header('Content-Type: application/rss+xml; charset=utf-8');
00364
00365 $items = '';
00366 $feed_length = variable_get('feed_item_length', 'teaser');
00367 foreach ($feeds as $feed) {
00368 switch ($feed_length) {
00369 case 'teaser':
00370 $teaser = node_teaser($feed->description);
00371 if ($teaser != $feed->description) {
00372 $teaser .= '<p><a href="' . check_url($feed->link) . '">' . t('read more') . "</a></p>\n";
00373 }
00374 $feed->description = $teaser;
00375 break;
00376 case 'title':
00377 $feed->description = '';
00378 break;
00379 }
00380 $items .= format_rss_item($feed->ftitle . ': ' . $feed->title, $feed->link, $feed->description, array('pubDate' => date('r', $feed->timestamp)));
00381 }
00382
00383 $site_name = variable_get('site_name', 'Drupal');
00384 $url = url((isset($category) ? 'aggregator/categories/' . $category->cid : 'aggregator'), array('absolute' => TRUE));
00385 $description = isset($category) ? t('@site_name - aggregated feeds in category @title', array('@site_name' => $site_name, '@title' => $category->title)) : t('@site_name - aggregated feeds', array('@site_name' => $site_name));
00386
00387 $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
00388 $output .= "<rss version=\"2.0\">\n";
00389 $output .= format_rss_channel(t('@site_name aggregator', array('@site_name' => $site_name)), $url, $description, $items);
00390 $output .= "</rss>\n";
00391
00392 print $output;
00393 }
00394
00403 function aggregator_page_opml($cid = NULL) {
00404 if ($cid) {
00405 $result = db_query('SELECT f.title, f.url FROM {aggregator_feed} f LEFT JOIN {aggregator_category_feed} c on f.fid = c.fid WHERE c.cid = %d ORDER BY title', $cid);
00406 }
00407 else {
00408 $result = db_query('SELECT * FROM {aggregator_feed} ORDER BY title');
00409 }
00410
00411 $feeds = array();
00412 while ($item = db_fetch_object($result)) {
00413 $feeds[] = $item;
00414 }
00415
00416 return theme('aggregator_page_opml', $feeds);
00417 }
00418
00426 function theme_aggregator_page_opml($feeds) {
00427 drupal_set_header('Content-Type: text/xml; charset=utf-8');
00428
00429 $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
00430 $output .= "<opml version=\"1.1\">\n";
00431 $output .= "<head>\n";
00432 $output .= '<title>' . check_plain(variable_get('site_name', 'Drupal')) . "</title>\n";
00433 $output .= '<dateModified>' . gmdate('r') . "</dateModified>\n";
00434 $output .= "</head>\n";
00435 $output .= "<body>\n";
00436 foreach ($feeds as $feed) {
00437 $output .= '<outline text="' . check_plain($feed->title) . '" xmlUrl="' . check_url($feed->url) . "\" />\n";
00438 }
00439 $output .= "</body>\n";
00440 $output .= "</opml>\n";
00441
00442 print $output;
00443 }
00444
00450 function template_preprocess_aggregator_summary_items(&$variables) {
00451 $variables['title'] = check_plain($variables['source']->title);
00452 $variables['summary_list'] = theme('item_list', $variables['summary_items']);
00453 $variables['source_url'] = $variables['source']->url;
00454 }
00455
00461 function template_preprocess_aggregator_summary_item(&$variables) {
00462 $item = $variables['item'];
00463
00464 $variables['feed_url'] = check_url($item->link);
00465 $variables['feed_title'] = check_plain($item->title);
00466 $variables['feed_age'] = t('%age old', array('%age' => format_interval(time() - $item->timestamp)));
00467
00468 $variables['source_url'] = '';
00469 $variables['source_title'] = '';
00470 if (!empty($item->feed_link)) {
00471 $variables['source_url'] = check_url($item->feed_link);
00472 $variables['source_title'] = check_plain($item->feed_title);
00473 }
00474 }
00475
00481 function template_preprocess_aggregator_feed_source(&$variables) {
00482 $feed = $variables['feed'];
00483
00484 $variables['source_icon'] = theme('feed_icon', $feed->url, t('!title feed', array('!title' => $feed->title)));
00485 $variables['source_image'] = $feed->image;
00486 $variables['source_description'] = aggregator_filter_xss($feed->description);
00487 $variables['source_url'] = check_url(url($feed->link, array('absolute' => TRUE)));
00488
00489 if ($feed->checked) {
00490 $variables['last_checked'] = t('@time ago', array('@time' => format_interval(time() - $feed->checked)));
00491 }
00492 else {
00493 $variables['last_checked'] = t('never');
00494 }
00495
00496 if (user_access('administer news feeds')) {
00497 $variables['last_checked'] = l($variables['last_checked'], 'admin/content/aggregator');
00498 }
00499 }