uid == $node->uid)) {
return TRUE;
}
//if (user_access('edit group weblinks') && ($user->uid == $node->uid)) {
/*if (user_access('edit group weblinks')) {
return TRUE;
}*/
}
}
/**
* Implementation of hook_node_info().
*/
function weblinks_node_info() {
return array(
'weblinks' => array(
'name' => t('Web link'),
'module' => 'weblinks',
'description' => t('Create a new web link.'),
'title_label' => t('Link Name'),
)
);
}
/**
* Implementation of hook_menu().
*/
function weblinks_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array('path' => 'node/add/weblinks',
'title' => t('web link'),
'access' => user_access('create weblinks'));
$items[] = array('path' => 'node/edit/weblinks',
'access' => user_access('edit group weblinks'));
$items[] = array('path' => 'weblinks',
'title' => t('Web Links'),
'callback' => 'weblinks_page',
'access' => user_access('access content'),
'type' => MENU_SUGGESTED_ITEM);
$items[] = array('path' => 'admin/content/weblinks',
'title' => t('Web Links'),
'description' => t('Control web links and their categories and change web link settings.'),
'callback' => 'weblinks_overview',
'access' => user_access('administer weblinks'),
'type' => MENU_NORMAL_ITEM);
$items[] = array('path' => 'admin/content/weblinks/list', 'title' => t('list'),
'access' => user_access('edit own weblinks'),
'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
$items[] = array('path' => 'admin/content/weblinks/add/container',
'title' => t('Add category'),
'callback' => 'weblinks_form_main',
'callback arguments' => array('container'),
'access' => user_access('administer weblinks'),
'type' => MENU_LOCAL_TASK);
$items[] = array('path' => 'admin/content/weblinks/add/weblink', 'title' => t('add weblink'),
'callback' => 'weblinks_form_weblink',
'access' => user_access('create weblinks'),
'type' => MENU_LOCAL_TASK);
$items[] = array(
'path' => 'admin/settings/weblinks',
'title' => t('Web Links'),
'description' => t('Set a variety of options for web link display and categorization.'),
'callback' => 'drupal_get_form',
'callback arguments' => array('weblinks_admin_settings'),
'access' => user_access('administer site configuration'),
'type' => MENU_NORMAL_ITEM, // optional
);
}
elseif (is_numeric(arg(5))) {
$term = taxonomy_get_term(arg(5));
// Check if this is a valid term.
if ($term) {
$items[] = array('path' => 'admin/content/weblinks/edit/container',
'title' => t('Edit categoryr'),
'callback' => 'weblinks_form_main',
'callback arguments' => array('container', (array)$term),
'access' => user_access('administer weblinks'),
'type' => MENU_CALLBACK);
$items[] = array('path' => 'node/edit/weblink/'.$term->tid,
'title' => t('Edit web link'),
/*'callback' => 'weblinks_form_main',
'callback arguments' => array('weblink', (array)$term),*/
'access' => user_access('administer weblinks'),
'type' => MENU_CALLBACK);
}
} else {
if (arg(0) == 'user' && is_numeric(arg(1))) {
global $user;
$account = user_load(array('uid' => arg(1)));
if (($user->uid != $account->uid && $account->contact) || user_access('administer users')) {
$items[] = array('path' => 'user/'. arg(1) .'/weblinks',
'title' => t('Web Links'),
'callback' => 'weblinks_user_page',
'type' => MENU_LOCAL_TASK,
'access' => $user->uid,
'weight' => 2,
);
}
}
}
return $items;
}
/**
* Implementation of hook_load().
*/
function weblinks_load($node) { // Not sure if this works or not...
$bw = db_fetch_object(db_query('SELECT url, weight FROM {weblinks} WHERE nid = %d', $node->nid));
return $bw;
}
/**
* Module configuration settings
* @return settings HTML or deny access
*/
function weblinks_admin_settings() {
// only administrators can access this module
/*if (!user_access("administer weblinks")) {
return message_access();
}*/
$form['weblinks_pagedesc'] = array('#type' => 'textarea',
'#title' => t('Page information'),
'#cols' => 40,
'#rows' => 5,
'#default_value' => variable_get("weblinks_pagedesc", NULL),
'#description' => t('This description will appear at the top of the links page'),
'#required' => FALSE,
'#weight' => -10
);
$form['weblinks_simplelinks'] = array('#type' => 'checkbox',
'#title' => t('Use Simple Links'),
'#default_value' => variable_get("weblinks_simplelinks", 0),
'#description' => t('Enable this if you only want links on the main page'),
'#required' => FALSE,
'#weight' => -9
);
$form['weblinks_urlnode'] = array('#type' => 'radios',
'#title' => t('Links lead URL or to Web Link node'),
'#default_value' => variable_get("weblinks_urlnode", 'url'),
'#description' => t('Most people will want to leave this as URL'),
'#options' => array('url' => 'URL', 'node' => 'node'),
'#required' => FALSE,
'#weight' => -8
);
$form['weblinks_block_urlnode'] = array('#type' => 'radios',
'#title' => t('Block links lead URL or to Web Link node'),
'#default_value' => variable_get("weblinks_block_urlnode", 'url'),
'#description' => t('Most people will want to leave this as URL'),
'#options' => array('url' => 'URL', 'node' => 'node'),
'#required' => FALSE,
'#weight' => -7
);
$form['weblinks_catdesc'] = array('#type' => 'checkbox',
'#title' => t('Display link category descriptions'),
'#default_value' => variable_get("weblinks_catdesc", 1),
'#required' => FALSE,
'#weight' => -6
);
$form['weblinks_subcatdesc'] = array('#type' => 'checkbox',
'#title' => t('Display link sub-category descriptions'),
'#default_value' => variable_get("weblinks_subcatdesc", 0),
'#required' => FALSE,
'#weight' => -5
);
$form['weblinks_linkdesc'] = array('#type' => 'checkbox',
'#title' => t('Display individual link descriptions'),
'#default_value' => variable_get("weblinks_linkdesc", 1),
'#description' => t($row['description']),
'#required' => FALSE,
'#weight' => -4
);
$form['weblinks_linkinfo'] = array('#type' => 'checkbox',
'#title' => t('Display link information'),
'#default_value' => variable_get("weblinks_linkinfo", 1),
'#description' => t('This displays link author, publish date, and revision date'),
'#required' => FALSE,
'#weight' => -3
);
$form['weblinks_maxfrontdepth'] = array('#type' => 'textfield',
'#title' => t('Maximum category depth (front page)'),
'#default_value' => variable_get("weblinks_maxfrontdepth", 1),
'#description' => t('This is affects how many subcategories, if any, will display on the main links page'),
'#required' => FALSE,
'#size' => 2,
'#weight' => -2
);
$form['weblinks_maxdepth'] = array('#type' => 'textfield',
'#title' => t('Maximum category depth'),
'#default_value' => variable_get("weblinks_maxdepth", 1),
'#description' => t('This is affects how many subcategories, if any, will display on every other links page'),
'#required' => FALSE,
'#size' => 2,
'#weight' => -1
);
$form['weblinks_clickthru'] = array('#type' => 'textfield',
'#title' => t('User click through'),
'#default_value' => variable_get("weblinks_clickthru", NULL),
'#description' => t('How many clicks a user can make before all the menus are automatically expanded. Leave blank if you do not want to set a limit.'),
'#required' => FALSE,
'#size' => 2,
'#weight' => 0
);
$form['weblinks_maxdisp_block_recent'] = array('#type' => 'textfield',
'#title' => t('Maximum Recent Web Links'),
'#default_value' => variable_get("weblinks_maxdisp_block_recent", NULL),
'#description' => t('Maximum number of links in the Recent Web Links block.'),
'#required' => FALSE,
'#size' => 2,
'#weight' => 1
);
// Get the weblinks categories to add to the blogroll
$vid = _weblinks_get_vid();
$result = db_query("SELECT td.*
FROM {term_data} td
WHERE td.vid = {$vid}
ORDER BY td.weight ASC");
while ($row = db_fetch_array($result)) {
// Set which link categories get displayed on the links page
$form[$row['tid']] = array('#type' => 'fieldset', '#title' => t($row['name']), '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => 2);
$form[$row['tid']]['weblinks_page_'.$row['tid']] = array('#type' => 'checkbox',
'#title' => t($row['name']),
'#default_value' => variable_get('weblinks_page_'.$row['tid'], 1),
'#required' => FALSE,
'#description' => 'Check to enable this category',
);
// Set the maximum number of links to display for each category
$form[$row['tid']]['weblinks_maxdisp_'.$row['tid']] = array('#type' => 'textfield',
'#title' => t('Maximum links to display'),
'#default_value' => variable_get('weblinks_maxdisp_'.$row['tid'], NULL),
'#size' => 2,
'#required' => FALSE,
'#description' => 'Leave blank if you would not like to limit the number of links displayed'
);
// Set the maximum number of links to display in the block for each category
$form[$row['tid']]['weblinks_maxdisp_block_'.$row['tid']] = array('#type' => 'textfield',
'#title' => t('Maximum block entries to display'),
'#default_value' => variable_get('weblinks_maxdisp_block_'.$row['tid'], NULL),
'#size' => 2,
'#required' => FALSE,
'#description' => 'Leave blank if you would not like to limit the number of links displayed'
);
}
return system_settings_form($form);
}
function weblinks_term_path($term) {
return 'weblinks/'. $term->tid;
}
/**
* Implementation of hook_nodeapi().
*/
function weblinks_nodeapi(&$node, $op, $teaser, $page) {
switch ($op) {
case 'delete revision':
db_query('DELETE FROM {weblinks} WHERE vid = %d', $node->vid);
break;
}
}
/**
* Implementation of hook_taxonomy().
*/
function weblinks_taxonomy($op, $type, $term = NULL) {
if ($op == 'delete' && $term['vid'] == _weblinks_get_vid()) {
switch ($type) {
case 'term':
$results = db_query('SELECT b.nid FROM {weblinks} b WHERE b.tid = %d', $term['tid']);
while ($node = db_fetch_object($results)) {
// node_delete will also remove any association with non-forum vocabularies.
$count = db_fetch_object(db_query('SELECT COUNT(b.tid) AS num FROM {weblinks} b WHERE b.nid = %d', $node->nid));
if ($count->num == 1) { node_delete($node->nid); } //Only delete links that are NOT cross listed
}
// For containers, remove the tid from the weblinks_containers variable.
$containers = variable_get('weblinks_containers', array());
$key = array_search($term['tid'], $containers);
if ($key !== FALSE) {
unset($containers[$key]);
}
variable_set('weblinks_containers', $containers);
break;
case 'vocabulary': // This will probably NEVER be the case
variable_del('weblinks_nav_vocabulary');
}
}
}
/**
* Implementation of hook_form().
*/
function weblinks_form(&$node) {
$type = node_get_types('type', $node);
$form['title'] = array('#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#default_value' => $node->title,
'#maxlength' => 64,
'#description' => t('This is the name that will be hyperlinked.'),
'#required' => TRUE,
);
$form['url'] = array('#type' => 'textfield',
'#title' => t('URL'),
'#default_value' => $node->url,
'#maxlength' => 64,
'#description' => t('The description can provide additional information about the link.'),
'#required' => TRUE,
);
$form['weight'] = array('#type' => 'weight',
'#title' => t('Weight'),
'#default_value' => $node->weight,
'#maxlength' => 10,
'#description' => t('In listings, the heavier terms (with a larger weight) will sink and the lighter terms will be positioned nearer the top.'),
'#required' => TRUE,
);
$form['body_filter']['body'] = array('#type' => 'textarea',
'#title' => check_plain($type->body_label),
'#default_value' => $node->body,
'#cols' => 60,
'#rows' => 5,
'#required' => FALSE);
$form['body_filter']['format'] = filter_form($node->format);
$ref=$_REQUEST['ref'];
$form['ref'] = array('#type' => 'hidden', '#value' => $ref);
return $form;
}
/**
* Returns a form for adding a container to the weblink vocabulary
*
* @param $edit Associative array containing a container term to be added or edited.
*/
function weblinks_form_container($edit = array()) {
// Handle a delete operation.
/* if ($_POST['op'] == t('Delete') || $_POST['edit']['confirm']) {
return _weblinks_confirm_delete($edit['tid']);
}*/
$form['name'] = array('#type' => 'textfield',
'#title' => t('Category Name'),
'#default_value' => $edit['name'],
'#maxlength' => 64,
'#description' => t('This categorizes the links and allows you to sort them.'),
'#required' => TRUE,
);
$form['description'] = array('#type' => 'textarea',
'#title' => t('Category Description'),
'#default_value' => $edit['description'],
'#cols' => 60,
'#rows' => 5,
'#description' => t('The description can provide additional information about the link grouping.'),
);
$form['synonyms'] = array('#type' => 'textarea', '#title' => t('Synonyms'), '#default_value' => implode("\n", taxonomy_get_synonyms($edit['tid'])), '#description' => t('Synonyms of this term, one synonym per line.', array('%help-url' => url('admin/help/taxonomy', NULL, NULL, 'synonyms'))));
$form['parent']['#tree'] = TRUE;
$form['parent'][0] = _weblinks_parent_select($edit['tid'], t('Parent'), 'weblinks');
$form['weight'] = array('#type' => 'weight',
'#title' => t('Weight'),
'#default_value' => $edit['weight'],
'#maxlength' => 10,
'#description' => t('In listings, the heavier terms (with a larger weight) will sink and the lighter terms will be positioned nearer the top.'),
'#required' => TRUE,
);
$vid = _weblinks_get_vid();
$form['vid'] = array('#type' => 'hidden', '#value' => $vid);
$form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
if ($edit['tid']) {
$form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
$form['tid'] = array('#type' => 'hidden', '#value' => $edit['tid']);
}
//$form['#base'] = 'weblinks_form'; //??? - UPDATE
return $form;
}
function weblinks_form_main($type, $edit = array()) {
if ($_POST['op'] == t('Delete') || $_POST['confirm']) {
return drupal_get_form('weblinks_confirm_delete', $edit['tid']);
}
switch ($type) {
case 'weblink':
return drupal_get_form('weblinks_form_weblink', $edit);
break;
case 'container':
return drupal_get_form('weblinks_form_container', $edit);
break;
}
}
/**
* Returns a form for adding a weblink
*/
function weblinks_form_weblink() {
drupal_goto("node/add/weblinks", $query = "ref=admin");
}
/**
* Process forum form and container form submissions.
*/
function weblinks_form_container_submit($form_id, $form_values) {
$container = TRUE;
$type = t('weblinks container');
$status = taxonomy_save_term($form_values);
switch ($status) {
case SAVED_NEW:
$containers = variable_get('weblinks_containers', array());
$containers[] = $form_values['tid'];
variable_set('weblinks_containers', $containers);
drupal_set_message(t('Created new %type %term.', array('%term' => theme('placeholder', $form_values['name']), '%type' => $type)));
break;
case SAVED_UPDATED:
drupal_set_message(t('The %type %term has been updated.', array('%term' => $form_values['name'], '%type' => $type)));
break;
}
return 'admin/content/weblinks';
}
/**
* Returns the vocabulary id for weblinks navigation.
*/
function _weblinks_get_vid() {
$vid = variable_get('weblinks_nav_vocabulary', '');
if (empty($vid)) {
// Check to see if a forum vocabulary exists
$vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module='%s'", 'weblinks'));
if (!$vid) {
$edit = array('name' => 'Web Links', 'multiple' => 1, 'required' => 0, 'hierarchy' => 2, 'relations' => 0, 'module' => 'weblinks', 'nodes' => array('weblinks' => 1));
taxonomy_save_vocabulary($edit);
$vid = $edit['vid'];
}
variable_set('weblinks_nav_vocabulary', $vid);
}
return $vid;
}
/**
* Returns a select box for available parent terms
*
* @param $tid ID of the term which is being added or edited
* @param $title Title to display the select box with
* @param $child_type Whether the child is weblink or category
*/
function _weblinks_parent_select($tid, $title, $child_type) {
$parents = taxonomy_get_parents($tid);
if ($parents) {
$parent = array_shift($parents);
$parent = $parent->tid;
}
else {
$parent = 0;
}
$children = taxonomy_get_tree(_weblinks_get_vid(), $tid);
// A term can't be the child of itself, nor of its children.
foreach ($children as $child) {
$exclude[] = $child->tid;
}
$exclude[] = $tid;
// If taxonomy_access is enabled, there may be group permissions to check
$include = _weblinks_get_access();
$tree = taxonomy_get_tree(_weblinks_get_vid());
if (user_access('administer weblinks')) {$options[0] = '<'. t('root') .'>';}
if ($tree) {
foreach ($tree as $term) {
if (!in_array($term->tid, $exclude)) {
// If user doesn't have admin priveledges, then user must have a specific role to be here
if ((user_access('administer weblinks')) || (user_access('edit group weblinks') && (in_array($term->tid, $include)))) {
//$options[$term->tid] = _taxonomy_depth($term->depth) . $term->name;
$options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
}
}
}
}
if ($child_type == 'container') {
$description = t('Containers are usually placed at the top (root) level of your web link categories but you can also place a container inside a parent container or category.');
}
else if ($child_type == 'forum') {
$description = t('You may place your web link inside a parent container or category, or at the top (root) level of your categories.');
}
return array('#type' => 'select', '#title' => $title, '#default_value' => $parent, '#options' => $options, '#description' => $description, '#required' => TRUE);
}
/**
* Returns an array of terms a user has access to
*/
function _weblinks_get_access() {
global $user;
$roles = $user->roles;
if (module_exists('taxonomy_access')) {
foreach ($roles as $key => $rolename) {
$result = db_query("SELECT tid FROM {term_access} WHERE rid = {$key} AND grant_create = 1");
while ($row = db_fetch_array($result)) {
$include[] = $row['tid'];
}
}
}
return $include;
}
/**
* Returns the term id for weblinks categories.
*/
function _weblinks_get_tid() {
$vid = _weblinks_get_vid();
if (!empty($vid)) {
// Check to see if a forum vocabulary exists
$tid = db_fetch_array(db_query("SELECT tid FROM term_data WHERE vid=$vid"));
}
return $tid;
}
/**
* Implementation of hook_view; this adds the url to viewable link node
*/
function weblinks_view(&$node, $teaser = FALSE, $page = FALSE) {
if ($page) {
$vocabulary = taxonomy_get_vocabulary(variable_get('weblinks_nav_vocabulary', ''));
// Breadcrumb navigation
$breadcrumb = array();
$breadcrumb[] = array('path' => 'weblinks', 'title' => $vocabulary->name);
if ($parents = taxonomy_get_parents_all($node->tid)) {
$parents = array_reverse($parents);
foreach ($parents as $p) {
$breadcrumb[] = array('path' => 'weblinks/'. $p->tid, 'title' => $p->name);
}
}
$breadcrumb[] = array('path' => 'node/'. $node->nid);
menu_set_location($breadcrumb);
}
$node = node_prepare($node, $teaser);
$url = db_fetch_object(db_query('SELECT url FROM {weblinks} WHERE nid = %d LIMIT 1', $node->nid));
//UPDATE
$node->url = $url->url;
/*$node->body = l(t($url->url), $url->url) . $node->body;*/
$node->content['url'] = array(
'#value' => $node->url,
'#weight' => 1,
);
return $node;
}
/**
* Implementation of hook_prepare; assign bw taxonomy when adding a weblink from within a category.
*/
function weblinks_prepare(&$node) {
if (!$node->nid) {
// new topic
//$node->taxonomy[arg(3)]->vid = _weblinks_get_vid();
//$node->taxonomy[arg(3)]->tid = arg(3);
}
}
/**
* Implementation of hook_insert().
*/
function weblinks_insert($node) {
$vid = _weblinks_get_vid();
foreach($node->taxonomy[$vid] as $tid) {
db_query('INSERT INTO {weblinks} (nid, tid, vid, url, weight) VALUES (%d, %d, %d, "%s", %d)', $node->nid, $tid, $node->vid, $node->url, $node->weight);
}
if ($node->ref == 'admin') {drupal_goto('admin/content/weblinks');}
}
/**
* Implementation of hook_update().
*/
function weblinks_update($node) {
$vid = _weblinks_get_vid();
if (!($node->is_new || $node->revision)){
db_query('DELETE FROM {weblinks} WHERE nid = %d', $node->nid);
}
foreach($node->taxonomy[$vid] as $tid) {
db_query('INSERT INTO {weblinks} (nid, tid, vid, url, weight) VALUES (%d, %d, %d, "%s", %d)', $node->nid, $tid, $node->vid, $node->url, $node->weight);
}
if ($node->ref == 'admin') {drupal_goto('admin/content/weblinks');}
}
/**
* Implementation of hook_delete().
*/
function weblinks_delete(&$node) {
db_query('DELETE FROM {weblinks} WHERE nid = %d', $node->nid);
if ($node->ref == 'admin') {drupal_goto('admin/content/weblinks');}
}
/**
* Returns a confirmation page for deleting a weblinks taxonomy term
*
* @param $tid ID of the term to be deleted
*/
function _weblinks_confirm_delete($tid) {
$term = taxonomy_get_term($tid);
$form['tid'] = array('#type' => 'hidden', '#value' => $tid);
$form['name'] = array('#type' => 'hidden', '#value' => $term->name);
return confirm_form('weblinks_confirm_delete', $form, t('Are you sure you want to delete the category %name?', array('%name' => theme('placeholder', $term->name))), 'admin/content/weblinks', t('Deleting a weblink or container will delete all sub-categories and associated links as well. This action cannot be undone.'), t('Delete'), t('Cancel'));
}
/**
* Implementation of forms api _submit call. Deletes a link category after confirmation.
*/
function weblinks_confirm_delete_submit($form) {
taxonomy_del_term($form['tid']);
drupal_set_message(t('The category %term and all sub-categories and associated links have been deleted.', array('%term' => theme('placeholder', $form['name']))));
watchdog('content', t('weblinks: deleted %term and all its sub-categories and associated links.', array('%term' => theme('placeholder', $form['name']))));
return 'admin/content/weblinks';
}
/**
* Returns a confirmation page for deleting a weblinks web link
*
* @param $id ID of the link to be deleted
*/
function _weblinks_confirm_delete_weblink($id) {
$term = db_result(db_query("SELECT title FROM link_data WHERE lid = {$id}"));
$form = "Are you sure you want to delete the weblink {$term}
This action cannot be undone.
";
$form .= form_hidden('id', $id);
$form .= form_submit(t('Cancel'), $name = 'cancel_delete');
$form .= form_submit(t('Delete'), $name = 'delete_link');
$output = theme("page", form($form));
print $output;
}
/**
* Returns an overview list of existing weblinks and containers
*/
function weblinks_overview() {
drupal_add_js();
drupal_add_js('misc/collapse.js');
$header = array(t('Name'), t('Operations'));
$tree = taxonomy_get_tree(_weblinks_get_vid(), $parent = 0, $depth = -1, $max_depth = 1);
global $bwrow;
global $i;
$i = 0;
$menu = '';
$bwrow = array();
if ($tree) {
foreach ($tree as $term) {
$menu .= '