You are here

feed_path_publisher.module in Feed Path Publisher 5

Same filename and directory in other branches
  1. 6 feed_path_publisher.module
  2. 7 feed_path_publisher.module

File

feed_path_publisher.module
View source
<?php

function feed_path_publisher_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/content/feed_path_publisher',
      'title' => t('Feed path publisher'),
      'callback' => 'feed_path_publisher_list',
      'access' => user_access('administer site configuration'),
    );
    $items[] = array(
      'path' => 'admin/content/feed_path_publisher/list',
      'title' => t('List'),
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => -1,
    );
    $items[] = array(
      'path' => 'admin/content/feed_path_publisher/add',
      'title' => t('Add'),
      'type' => MENU_LOCAL_TASK,
      'callback' => 'feed_path_publisher_add',
    );
  }
  else {
    _feed_path_publisher_add_feeds($_GET['q']);
    if (arg(2) == 'feed_path_publisher' && is_numeric(arg(3)) && arg(1) == 'content' && arg(0) == 'admin') {
      $items[] = array(
        'path' => 'admin/content/feed_path_publisher/' . arg(3) . '/edit',
        'callback' => 'feed_path_publisher_edit',
        'callback arguments' => array(
          arg(3),
        ),
      );
    }
  }
  return $items;
}
function _feed_path_publisher_add_feeds($internal_path) {
  global $user;

  //dpm($user->roles);
  $aliased_path = $internal_path;
  $front_page_path = $internal_path;

  // Add feeds based on aliased paths
  if (module_exists('path')) {
    $aliased_path = drupal_get_path_alias($internal_path);
  }
  if (drupal_is_front_page()) {
    $front_page_path = '<front>';
  }
  $res = db_query('SELECT title, feed, fppid FROM {feed_path_publisher} WHERE path_prefix = LEFT("%s", LENGTH(path_prefix)) OR path_prefix = LEFT("%s", LENGTH(path_prefix)) OR path_prefix = LEFT("%s", LENGTH(path_prefix)) ORDER BY weight, path_prefix', $internal_path, $aliased_path, $front_page_path);
  while ($row = db_fetch_object($res)) {
    $path_rids = '';
    $path_rids = db_result(db_query('SELECT rids FROM {feed_path_publisher_roles} WHERE fppid = %d', $row->fppid));
    $path_rids = explode(',', $path_rids);
    if ($path_rids) {
      $show_hide = db_result(db_query('SELECT show_hide FROM {feed_path_publisher_roles} WHERE fppid = %d', $row->fppid));
      $role_found = false;
      foreach ($user->roles as $key => $value) {
        if (in_array($key, $path_rids)) {
          $role_found = true;
        }
      }
      if (!$role_found && $show_hide == 'show' || $role_found && $show_hide == 'hide') {
        continue;
      }
    }
    drupal_add_link(array(
      'rel' => 'alternate',
      'type' => 'application/rss+xml',
      'title' => $row->title,
      'href' => url($row->feed),
    ));
  }
}
function feed_path_publisher_list() {
  drupal_set_title('Feed path publisher');
  $res = db_query('SELECT * FROM {feed_path_publisher} ORDER BY weight, path_prefix, title');
  $cols = array(
    'Title',
    'Path Prefix',
    'Feed',
    'Weight',
    'Operations',
  );
  $rows = array();
  while ($row = db_fetch_object($res)) {
    $ops = array();
    $ops[] = l('edit', 'admin/content/feed_path_publisher/' . $row->fppid . '/edit');

    //$ops[] = l('delete', 'admin/content/feed_path_publisher/' . $row->fppid . '/delete');
    $path_prefix = $row->path_prefix;
    if ($row->path_prefix == '') {
      $path_prefix = '<em>Global</em>';
    }
    $rows[] = array(
      check_plain($row->title),
      $path_prefix == '<em>Global</em>' ? $path_prefix : check_plain($path_prefix),
      check_plain($row->feed),
      $row->weight,
      implode(' ', $ops),
    );
  }
  $content = theme('table', $cols, $rows);
  return $content;
}
function feed_path_publisher_add() {
  return drupal_get_form('feed_path_publisher_edit_form');
}
function feed_path_publisher_edit($fppid) {
  return drupal_get_form('feed_path_publisher_edit_form', $fppid);
}
function feed_path_publisher_edit_form($fppid = NULL) {
  $form = array();
  $item = (object) NULL;
  $use_roles = false;
  $show_hide = 0;
  $checked_roles = '';
  if (isset($fppid)) {
    $item = db_fetch_object(db_query('SELECT * FROM {feed_path_publisher} WHERE fppid = %d', $fppid));
    if (db_result(db_query('SELECT count(*) FROM {feed_path_publisher_roles} WHERE fppid = %d', $fppid)) > 0) {
      $use_roles = true;
      if (db_result(db_query('SELECT show_hide FROM {feed_path_publisher_roles} WHERE fppid = %d limit 1', $fppid)) == 'show') {
        $show_hide = 0;
      }
      else {
        $show_hide = 1;
      }
      $checked_roles = explode(',', db_result(db_query('SELECT rids FROM {feed_path_publisher_roles} where fppid = %d' . $fppid)));
    }
    $form['fppid'] = array(
      '#type' => 'hidden',
      '#value' => $item->fppid,
    );
  }
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => $item->title,
    '#required' => TRUE,
  );
  $form['path_prefix'] = array(
    '#type' => 'textfield',
    '#title' => t('Path prefix'),
    '#default_value' => $item->path_prefix,
    '#description' => t('Leaving this blank publishes to the entire site. You can use <front> to have your feed link added only to the home page.'),
  );
  $form['feed'] = array(
    '#type' => 'textfield',
    '#title' => t('Feed'),
    '#default_value' => $item->feed,
    '#required' => TRUE,
    '#description' => t('Relative paths should not begin with a slash.'),
  );
  $form['weight'] = array(
    '#type' => 'weight',
    '#title' => t('Weight'),
    '#default_value' => $item->weight,
    '#description' => t('Optional. In the feed listings, the heavier items will sink and the lighter items will be positioned nearer the top.'),
  );
  $form['roles'] = array(
    '#type' => 'fieldset',
    '#title' => t('Role-based feed publishing'),
    '#collapsible' => TRUE,
    '#collapsed' => $use_roles ? FALSE : TRUE,
  );
  $form['roles']['enable_roles'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable role-based feed publishing according to role'),
    '#default_value' => $use_roles,
    '#collapsed' => TRUE,
  );
  $form['roles']['show_hide'] = array(
    '#type' => 'radios',
    '#title' => t('Behavior'),
    '#default_value' => $show_hide,
    '#options' => array(
      t('Show this feed for the following roles'),
      t('Hide this feed for the following roles'),
    ),
  );
  $roles = db_query('SELECT * FROM {role}');
  while ($role = db_fetch_object($roles)) {
    $options[$role->rid] = $role->name;
  }
  $form['roles']['roles_list'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Roles'),
    '#default_value' => $checked_roles,
    '#options' => $options,
  );
  $form[] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  if (isset($fppid)) {
    $form[] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
    );
  }
  return $form;
}
function feed_path_publisher_edit_form_submit($form_id, $form_values) {

  //drupal_set_message(print_r($form_values,true));
  $rids = array();
  foreach ($form_values['roles_list'] as $role => $value) {
    if ($value) {
      $rids[] = $role;
    }
  }
  $show_hide = '';
  if ($form_values['show_hide'] == 1) {
    $show_hide = 'hide';
  }
  else {
    $show_hide = 'show';
  }
  if (isset($form_values['fppid'])) {
    if ($form_values['op'] == t('Delete')) {
      db_query('DELETE FROM {feed_path_publisher} WHERE fppid = %d', $form_values['fppid']);
      db_query('DELETE FROM {feed_path_publisher_roles} WHERE fppid = %d', $form_values['fppid']);
      drupal_set_message(t('Feed path deleted.'));
    }
    else {
      db_query('UPDATE {feed_path_publisher} SET title = "%s", path_prefix = "%s", feed = "%s", weight = %d WHERE fppid = %d', $form_values['title'], $form_values['path_prefix'], $form_values['feed'], $form_values['weight'], $form_values['fppid']);
      if ($form_values['enable_roles'] == 1) {
        $rids = implode(',', $rids);
        $id = db_result(db_query('select fpprid  from {feed_path_publisher_roles} WHERE fppid = %d', $form_values['fppid']));
        if ($id) {
          db_query('UPDATE {feed_path_publisher_roles} SET show_hide = "%s", rids = "%s" WHERE fpprid = %d', $show_hide, $rids, $id);
        }
        else {
          db_query('INSERT INTO {feed_path_publisher_roles} (fppid,show_hide,rids) VALUES (%d, "%s", "%s")', $form_values['fppid'], $show_hide, $rids);
        }
      }
      else {
        db_query('DELETE FROM {feed_path_publisher_roles} WHERE fppid = %d', $form_values['fppid']);
      }
      drupal_set_message(t('Feed path updated.'));
    }
  }
  else {
    db_query('INSERT INTO {feed_path_publisher} (title, path_prefix, feed, weight) VALUES ("%s", "%s", "%s", %d)', $form_values['title'], $form_values['path_prefix'], $form_values['feed'], $form_values['weight']);
    if ($form_values['enable_roles'] == 1) {
      $fppid = db_result(db_query("select last_insert_id()"));
      if ($rids) {
        $rids = implode(',', $rids);
        db_query('INSERT INTO {feed_path_publisher_roles} (fppid,show_hide,rids) VALUES (%d, "%s", "%s")', $fppid, $show_hide, $rids);
      }
    }
    drupal_set_message(t('Feed path created.'));
  }
  return 'admin/content/feed_path_publisher';
}