/[drupal]/contributions/modules/custom_links/custom_links.module
ViewVC logotype

Diff of /contributions/modules/custom_links/custom_links.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.4 by eaton, Wed Jan 3 21:37:59 2007 UTC revision 1.5 by eaton, Sun Jan 27 09:16:01 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: custom_links.module,v 1.3 2007/01/02 21:54:55 eaton Exp $  // $Id: custom_links.module,v 1.4.2.3 2007/08/25 17:15:04 eaton Exp $
3    
4  /**  /**
5   * Implementation of hook_menu().   * Implementation of hook_menu().
6   */   */
7  function custom_links_menu($may_cache) {  function custom_links_menu() {
8    $items = array();    $items = array();
9    
10    if ($may_cache) {    $items['admin/build/custom_links'] = array(
11      $access = user_access('administer custom links');      'title' => 'Custom links',
12      $items[] = array(      'description' => 'Add custom links to specific content types.',
13        'path' => 'admin/build/custom_links',      'page callback' => 'custom_links_page',
14        'title' => t('Custom links'),      'access callback' => 'user_access',
15        'description' => t('Add custom links to specific content types.'),      'access arguments' => array('administer custom links'),
16        'callback' => 'custom_links_page',      'file' => 'custom_links.admin.inc',
17        'access' => $access    );
18      );  
19      $items['admin/build/custom_links/add'] = array(
20      $items[] = array(      'title' => 'Add custom breadcrumb',
21        'path' => 'admin/build/custom_links/add',      'type' => MENU_CALLBACK,
22        'title' => t('Add custom link'),      'page callback' => 'drupal_get_form',
23        'type' => MENU_CALLBACK,      'page arguments' => array('custom_links_form'),
24        'callback' => 'drupal_get_form',      'access callback' => 'user_access',
25        'callback arguments' => array('custom_links_form'),      'access arguments' => array('administer custom links'),
26        'access' => $access      'file' => 'custom_links.admin.inc',
27      );    );
28    
29      $items[] = array(    $items['admin/build/custom_links/edit'] = array(
30        'path' => 'admin/build/custom_links/edit',      'title' => 'Edit custom breadcrumb',
31        'title' => t('Edit custom link'),      'type' => MENU_CALLBACK,
32        'type' => MENU_CALLBACK,      'page callback' => 'drupal_get_form',
33        'callback' => 'drupal_get_form',      'page arguments' => array('custom_links_form'),
34        'callback arguments' => array('custom_links_form'),      'access callback' => 'user_access',
35        'access' => $access      'access arguments' => array('administer custom links'),
36      );      'file' => 'custom_links.admin.inc',
37    }    );
38    
39    return $items;    return $items;
40  }  }
41    
42    function custom_links_theme() {
43      $items = array();
44      $items['custom_links_block'] = array(
45        'arguments' => array('links' => array()),
46      );
47      return $items;
48    }
49    
50  function custom_links_link($type, $node = NULL, $teaser = FALSE) {  function custom_links_link($type, $node = NULL, $teaser = FALSE) {
51    if ($type == 'node') {    if ($type == 'node') {
52      return _custom_links_build_links($node, $teaser);      return _custom_links_build_links($node, $teaser);
# Line 64  function custom_links_block($op = 'list' Line 72  function custom_links_block($op = 'list'
72      $links = _custom_links_build_links($node, FALSE, TRUE);      $links = _custom_links_build_links($node, FALSE, TRUE);
73      if (count($links)) {      if (count($links)) {
74        $block['subject'] = t('Links');        $block['subject'] = t('Links');
75        $block['content'] = theme('custom_links_block');        $block['content'] = theme('custom_links_block', $links);
76        return $block;        return $block;
77      }      }
78    }    }
# Line 75  function _custom_links_build_links($node Line 83  function _custom_links_build_links($node
83    $custom_links = _custom_links_load_all_links();    $custom_links = _custom_links_load_all_links();
84    
85    foreach ($custom_links as $link) {    foreach ($custom_links as $link) {
86      if (($link->display != $teaser && $link->display != 2) || ($block != ($link->display == 3))) {      if (($block && $link->display != 3) || (!$block && $link->display == 3)) {
87          continue;
88        }
89        if (($link->display == 0 && $teaser) || ($link->display == 1 && !$teaser)) {
90        continue;        continue;
91      }      }
92      if ($link->node_type && $node->type != $link->node_type) {      if ($link->node_type && $node->type != $link->node_type) {
93        continue;        continue;
94      }      }
   
95      if ($link->viewer_perm && !user_access($link->viewer_perm)) {      if ($link->viewer_perm && !user_access($link->viewer_perm)) {
96        continue;        continue;
97      }      }
   
98      if ($link->author_perm) {      if ($link->author_perm) {
99        $author = user_load(array('uid' => $node->uid));        $author = user_load(array('uid' => $node->uid));
100        if (!user_access($link->author_perm, $author)) {        if (!user_access($link->author_perm, $author)) {
101          continue;          continue;
102        }        }
103      }      }
     $links[$link->link_key]['title'] = module_exists('token') ? token_replace($link->title, 'node', $node) : $link->title;  
     $links[$link->link_key]['html'] = $link->check_html;  
     if ($link->path != '') {  
       $links[$link->link_key]['href'] = module_exists('token') ? token_replace($link->path, 'node', $node) : $link->path;  
     }  
   }  
   return $links;  
 }  
   
 // Lists all current custom links, and provides a link to the edit page.  
 function custom_links_page() {  
   $links = _custom_links_load_all_links(TRUE);  
   
   $header = array(t('key'), t('link'), '');  
   
   $rows = array();  
   foreach ($links as $link) {  
     $row = array();  
     $row[] = $link->link_key;  
     $row[] =  $link->title;  
     $row[] =  l(t('edit'), 'admin/build/custom_links/edit/' . $link->lid);  
     $rows[] = $row;  
   }  
   if (count($rows) == 0) {  
     $rows[] = array(array('data' => t('No custom links have been defined.'), 'colspan' => 3));  
   }  
   
   $rows[] = array(array('data' => l(t('Add a new custom link'), 'admin/build/custom_links/add'), 'colspan' => 3));  
   
   
   return theme('table', $header, $rows);  
 }  
   
   
 // Displays an edit form for a custom link record.  
 function custom_links_form() {  
   $lid = arg(4);  
   if (isset($lid)) {  
     $link = _custom_links_load_link($lid);  
     $form['lid'] = array(  
       '#type' => 'hidden',  
       '#value' => $lid,  
     );  
   }  
   
   $form['link'] = array(  
     '#type' => 'fieldset',  
     '#title' => t('Link details'),  
   );  
   
   $form['link']['link_key'] = array(  
     '#type' => 'textfield',  
     '#title' => t('Link key'),  
     '#description' => t('A unique string to identify this link. It will also be used as the CSS ID of the link in the final HTML.'),  
     '#default_value' => $lid ? $link->link_key : NULL  
   );  
   
   $form['link']['title'] = array(  
     '#type' => 'textfield',  
     '#title' => t('Title'),  
     '#required' => TRUE,  
     '#description' => module_exists('token') ? t('The visible text of the link seen by the user. See placeholder instructions below for more details.') : t('The visible text of the link seen by the user.'),  
     '#default_value' => $lid ? $link->title : NULL  
   );  
   
   $form['link']['check_html'] = array(  
     '#type' => 'checkbox',  
     '#title' => t('Title uses HTML'),  
     '#return_value' => 1,  
     '#default_value' => $lid ? $link->check_html : 0  
   );  
   
   $form['link']['path'] = array(  
     '#type' => 'textfield',  
     '#title' => t('Path'),  
     '#description' => module_exists('token') ? t('The Drupal path for the link. See placeholder instructions below for more details.') : t('The Drupal path for the link.'),  
     '#default_value' => $lid ? $link->path : NULL  
   );  
   
   $form['help'] = array(  
     '#type' => 'fieldset',  
     '#collapsible' => TRUE,  
     '#collapsed' => TRUE,  
     '#title' => t('Placeholder tokens'),  
     '#description' => t("The following placeholder tokens can be used in both paths and titles. When used in a path or title, they will be replaced with the appropriate values."),  
   );  
   
   if (module_exists('token')) {  
     $form['help']['tokens'] = array(  
       '#value' => theme('token_help', 'node'),  
     );  
   }  
   else {  
     $form['help']['#description'] = t("To use dynamic placeholder tokens in your paths and titles (the ID or title of the current node, for example), download and install the <a href='@token'>Token module</a> from Drupal.org.", array('@token' => 'http://www.drupal.org/project/token'));  
     $form['help']['#collapsible'] = FALSE;  
     $form['help']['#collapsed'] = FALSE;  
   }  
   
   $form['filters'] = array(  
     '#type' => 'fieldset',  
     '#title' => t('Link conditions'),  
   );  
   
   $modes = array(  
     0 => t('In full node views'),  
     1 => t('In teaser node views'),  
     2 => t('In both teaser and full node views'),  
     3 => t('In a sidebar block'),  
   );  
   $form['filters']['display'] = array(  
     '#type' => 'select',  
     '#title' => t('Display'),  
     '#options' => $modes,  
     '#default_value' => $lid ? $link->display : 2,  
   );  
   
   $options['*all*'] = t('All node types');  
   foreach (node_get_types('names') as $type => $name) {  
     $options[$type] = $name;  
   }  
   $form['filters']['node_type'] = array(  
     '#type' => 'select',  
     '#title' => t('Node type'),  
     '#options' => $options,  
     '#description' => t('If selected, the link will only be added to nodes of this type.'),  
     '#default_value' => $lid ? $link->node_type : NULL,  
   );  
   
   $form['filters']['author_perm'] = array(  
     '#type' => 'textfield',  
     '#title' => t('Author permission restriction'),  
     '#description' => t('A specific permission that the <i>author</i> of the node must have for the link to be added.'),  
     '#default_value' => $lid ? $link->author_perm : NULL  
   );  
104    
105    $form['filters']['viewer_perm'] = array(      $links[$link->link_key]['title'] = $link->title;
106      '#type' => 'textfield',      if (!empty($link->path)) {
107      '#title' => t('Viewer permission restriction'),        $links[$link->link_key]['href'] = $link->path;
108      '#description' => t('A specific permission that the <i>viewer</i> of the node must have for the link to be added.'),      }
109      '#default_value' => $lid ? $link->viewer_perm : 'access content'      if (!empty($link->query)) {
110    );        $links[$link->link_key]['query'] = $link->query;
111        }
112    $form['buttons']['submit'] = array(      if (!empty($link->fragment)) {
113      '#type' => 'submit',        $links[$link->link_key]['fragment'] = $link->fragment;
114      '#value' => t('Submit'),      }
   );  
   if ($lid) {  
     $form['buttons']['delete'] = array(  
       '#type' => 'submit',  
       '#value' => t('Delete'),  
     );  
   }  
   
   return $form;  
 }  
   
 function custom_links_form_validate($form_id, $form_values) {  
   if (strpos($form_values['link_key'], array(' ', '#', '.', '%', '^')) !== FALSE) {  
     form_set_error('link][link_key', t('The link key may not contain spaces or punctuation.'));  
   }  
 }  
115    
116  function custom_links_form_submit($form_id, $form_values) {      if (module_exists('token')) {
117    if ($form_values['op'] == t('Delete')) {        $links[$link->link_key] = token_replace($links[$link->link_key], 'node', $node);
     _custom_links_delete_link($form_values('lid'));  
   }  
   else {  
     $link = (object)$form_values;  
     if ($link->node_type == '*all*') {  
       $link->node_type = '';  
118      }      }
119      _custom_links_save_link($link);  
120        $links[$link->link_key]['html'] = $link->check_html;
121    }    }
122    return 'admin/build/custom_links';    return $links;
123  }  }
124    
125  function _custom_links_load_link($lid) {  function _custom_links_load_link($lid) {
# Line 291  function _custom_links_load_all_links($r Line 145  function _custom_links_load_all_links($r
145    
146  function _custom_links_save_link($link = NULL) {  function _custom_links_save_link($link = NULL) {
147    if (isset($link->lid)) {    if (isset($link->lid)) {
148      $sql  = "UPDATE {custom_link} SET";      drupal_write_record('custom_link', $link, 'lid');
     $sql .= " link_key = '%s', title = '%s', path = '%s', node_type = '%s', author_perm = '%s', viewer_perm = '%s', check_html = %d, display = %d";  
     $sql .= " WHERE lid = %d";  
   
     db_query($sql, $link->link_key, $link->title, $link->path,  
       $link->node_type, $link->author_perm, $link->viewer_perm, $link->check_html, $link->display, $link->lid);  
149    }    }
150    else {    else {
151      $sql  = "INSERT INTO {custom_link}";      drupal_write_record('custom_link', $link);
     $sql .= " (link_key, title, path, node_type, author_perm, viewer_perm, check_html, display)";  
     $sql .= " VALUES ('%s',  '%s',  '%s',  '%s',  '%s',  '%s', %d, %d)";  
   
     db_query($sql, $link->link_key, $link->title, $link->path,  
       $link->node_type, $link->author_perm, $link->viewer_perm, $link->check_html, $link->display);  
152    }    }
153  }  }
154    

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

  ViewVC Help
Powered by ViewVC 1.1.3