" . t('Links Module') . "

" . t('This module allows you to maintain a database of links') . "

"; case 'links': return variable_get('linksdb_message', t('A selection of useful links')); } } function linksdb_perm() { return array('manage links', 'suggest links', 'moderate links'); } function linksdb_menu($may_cache) { $items = array(); $items[] = array( 'path' => 'admin/settings/linkdb', 'title' => t('LinksDB'), 'description' => t('Manages the database of links.'), 'callback' => 'drupal_get_form', 'callback arguments' => array('linksdb_settings'), 'access' => user_access('administer site configuration'), 'type' => MENU_NORMAL_ITEM, // optional ); $items[] = array( 'path' => 'links', 'title' => t('Links'), 'callback' => 'linksdb_display', 'access' => user_access('access content'), ); $items[] = array( 'path' => 'links/submit', 'title' => t('suggest a link'), 'callback' => 'linksdb_suggest', 'access' => user_access('suggest links'), ); $items[] = array( 'path' => 'links/view/all', 'callback' => 'linksdb_show_all', 'type' => MENU_CALLBACK, 'access' => user_access('access content'), ); $items[] = array( 'path' => 'links/view', 'title' => t('view'), 'callback' => 'linksdb_display', 'type' => MENU_DEFAULT_LOCAL_TASK, 'access' => user_access('access content'), 'weight' => -10, ); $items[] = array( 'path' => 'links/edit', 'title' => t('add link'), 'callback' => 'linksdb_edit', 'type' => MENU_LOCAL_TASK, 'access' => user_access('manage links'), ); $items[] = array( 'path' => 'links/import', 'title' => t('import from text'), 'callback' => 'linksdb_import', 'type' => MENU_LOCAL_TASK, 'access' => user_access('manage links'), ); $items[] = array( 'path' => 'links/category', 'title' => t('categories'), 'callback' => 'linksdb_categories', 'type' => MENU_LOCAL_TASK, 'access' => user_access('manage links'), ); $items[] = array( 'path' => 'links/moderate', 'title' => t('moderate'), 'callback' => 'linksdb_moderate', 'type' => MENU_LOCAL_TASK, 'access' => user_access('moderate links'), ); $items[] = array( 'path' => 'links/deadlinks', 'title' => t('dead links'), 'callback' => 'linksdb_deadlinks', 'type' => MENU_LOCAL_TASK, 'access' => user_access('manage links'), ); $items[] = array( 'path' => 'links/category/edit', 'callback' => 'linksdb_categories_edit', 'type' => MENU_CALLBACK, 'access' => user_access('manage links'), ); $items[] = array( 'path' => 'links/refer', 'callback' => 'linksdb_refer', 'type' => MENU_CALLBACK, 'access' => TRUE, ); $items[] = array( 'path' => 'links/report', 'callback' => 'linksdb_report', 'type' => MENU_CALLBACK, 'access' => TRUE, ); return $items; } function linksdb_report($id=NULL) { return drupal_get_form('linksdb_report_form', $id, 'linksdb_report'); } function linksdb_report_form($id=NULL) { $form = array(); $form['id'] = array('#type' => 'hidden', '#value' => $id); $form['reason'] = array( '#type' => 'textfield', '#title' => t('Reason'), '#description' => t('Please be clear and consise with your reason why you think this link is dead'), ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Submit report'), ); return $form; } function linksdb_report_submit($fid, $v) { global $user; db_query("UPDATE {links_links} SET dead=1, deadcomment='%s', deadby=%d WHERE id=%d;", $v['reason'], $user->uid, $v['id']); drupal_set_message(t("Thank you for your feedback. The site administrators will check this link soon and take the appropriate action.")); return "links"; } function linksdb_refer($id=NULL) { db_query("UPDATE {links_links} SET clicks=clicks+1 WHERE id=%d;", $id); $link = db_fetch_object(db_query("SELECT url FROM {links_links} WHERE id=%d;", $id)); header("Location: $link->url"); } function linksdb_deadlinks() { return drupal_get_form('linksdb_deadlinks_form', 'linksdb_moderate'); } function linksdb_deadlinks_form() { $form = array(); $c = array(); $cl = db_query("SELECT name, id FROM {links_categories};"); while($cat = db_fetch_object($cl)) { $c[$cat->id] = $cat->name; } $c[0] = "<" . t('Root') . ">"; $ll = db_query("SELECT * FROM {links_links} WHERE dead=1 ORDER BY name;"); while($link = db_fetch_object($ll)) { if(variable_get('linksdb_show_report', 1)==1) { if($link->dead==1) { $link->description .= "
This link is marked as dead: $link->deadcomment"; } else { $link->description .= "
" . l(t("Report this link as dead"), "links/report/$link->id"); } } $form["link$link->id"] = array( '#type' => 'link', '#title' => $link->name, '#description' => $link->description, '#url' => $link->url, '#click' => "links/refer/$link->id", '#edit' => "links/edit/$link->id", '#clicks' => $link->clicks, '#newwindow' => $link->newwindow, ); } return $form; } function linksdb_moderate() { return drupal_get_form('linksdb_moderate_form', 'linksdb_moderate'); } function linksdb_moderate_form() { $form = array(); $cl = db_query("SELECT name, id FROM {links_categories};"); $c = array(); while($cat = db_fetch_object($cl)) { $c[$cat->id] = $cat->name; } $c[0] = '<'.t('Root').'>'; $ll = db_query("SELECT name, url, description, id, category, clicks FROM {links_links} WHERE active=0 ORDER BY name;"); $form['head'] = array('#type' => 'markup', '#value' => ''); if (db_num_rows($ll) > 0) { while($link = db_fetch_object($ll)) { $form["left$link->id"] = array( '#type' => 'markup', '#value' => '', ); } } // no links to moderate else { $form["no-moderate-links"] = array( '#type' => 'markup', '#value' => '', ); } $form['foot'] = array('#type' => 'markup', '#value' => '
LinkCategoryActivate
', ); $form["link$link->id"] = array( '#type' => 'link', '#title' => $link->name, '#description' => $link->description, '#url' => $link->url, '#click' => "links/refer/$link->id", '#edit' => "links/edit/$link->id", '#newwindow' => $link->newwindow, '#clicks' => $link->clicks, ); $form["mid$link->id"] = array( '#type' => 'markup', '#value' => '' . $c[$link->category] . "", ); $form["activate$link->id"] = array( '#type' => 'checkbox', '#default_value' => 0, ); $form["right$link->id"] = array( '#type' => 'markup', '#value' => '
No links to moderate.
'); $form['save'] = array('#type' => 'submit', '#value' => t('Save changes')); return $form; } function linksdb_moderate_form_submit($formid, $v) { while(list($key, $val) = each($v)) { if(preg_match('/activate(\d+)/', $key, $m)) { if($val==1) { db_query("UPDATE {links_links} SET active=1 WHERE id=%d;", $m[1]); drupal_set_message("Link " . $m[1] . " activated"); } } } } function linksdb_suggest() { return drupal_get_form('linksdb_suggest_form'); } function linksdb_suggest_form() { $copt = array(); $copt[0] = '<'.t('Root').'>'; $tree = array(); linksdb_childnodes(0, 1, $tree); foreach($tree as $cat) { if($cat['id'] != $id) { $copt[$cat['id']]=$cat['title']; } } $form = array(); $form['title'] = array( '#type' => 'textfield', '#title' => t('Site name'), '#description' => t('Enter the name of the site here'), ); $form['url'] = array( '#type' => 'textfield', '#title' => t('URL'), '#description' => t('Enter the URL of the site here. Please include the http:// part'), '#default_value' => 'http://', ); $form['description'] = array( '#type' => 'textarea', '#title' => t('Description'), '#description' => t('Please describe the site as fully as you can here'), ); $form['category'] = array( '#type' => 'select', '#title' => t('Category'), '#options' => $copt, '#description' => t('Select the category for this link'), ); $form['submut'] = array( '#type' => 'submit', '#value' => t('Suggest this site'), ); return $form; } function linksdb_suggest_form_submit($fid, $v) { db_query("INSERT INTO {links_links} (name, url, description, active, category) VALUES ('%s', '%s', '%s', 0, %d);", $v['title'], $v['url'], $v['description'], $v['category']); drupal_set_message(t("Thank you for your suggestion. The site staff will review it shortly and if approved will be on the site soon.")); return "links"; } function linksdb_elements() { $type = array(); $type['link'] = array('#input' => 0); return $type; } function theme_link($element) { $target = $element['#newwindow']==1?array("target"=>"_new"):array(); $url = $element['#url']; if(preg_match('/^([^?]+)?.*/', $url, $m)==1) { $url = $m[1]; } $image = theme('image',drupal_get_path('module','linksdb') . '/external.png'); //$title = $image . l($url, $element['#click'], $target); $title = l($url, $element['#click'], $target); if($element['#clicks'] and (variable_get('linksdb_show_clicks', 1)==1)) { $s = ($element['#clicks']==1)?"":"s"; $title .= " [" . $element['#clicks'] . " hit$s]"; } if(user_access('manage links')) { $title .= " - " . l("Edit Link", $element['#edit']); } return theme('form_element', $element, $title); } function linksdb_show_all($cat = NULL) { return drupal_get_form('linksdb_show_all_form', $cat); } function linksdb_show_all_form($cat=NULL) { $form = array(); $ll = db_query("SELECT * FROM {links_links} WHERE category = %d AND active = 1 ORDER BY name;", $cat); while($link = db_fetch_object($ll)) { if(variable_get('linksdb_show_report', 1)==1) { if($link->dead==1) { $link->description .= "
This link is marked as dead: $link->deadcomment"; } else { $link->description .= "
" . l(t("Report this link as dead"), "links/report/$link->id") . "
"; } } $form["link$link->id"] = array( '#type' => 'link', '#title' => $link->name, '#description' => $link->description, '#url' => $link->url, '#click' => "links/refer/$link->id", '#edit' => "links/edit/$link->id", '#newwindow' => $link->newwindow, '#clicks' => $link->clicks, ); } return $form; } function linksdb_display_rec($me, &$form) { $cat = db_fetch_object(db_query("SELECT shown FROM {links_categories} WHERE id=%d;", $me)); if(!$cat) { $cat->shown = 99999999; } $cl = db_query("SELECT * FROM {links_links} WHERE category = %d AND active=1 ORDER BY name;", $me); $cc = 0; while($link = db_fetch_object($cl)) { if($cc < $cat->shown) { // bm add $s = ($element['#clicks']==1)?"":"s"; $addhits = "$link->clicks hit$s"; if(user_access('manage links')) { $addedit = " | edit"; } if(variable_get('linksdb_show_report', 1)==1) { if($link->dead==1) { $deadornot = "This link is marked as dead: $link->deadcomment"; } else { $deadornot = l(t("Report this link as dead"), "links/report/$link->id"); } } $form["link$link->id"] = array( '#type' => 'link', //'#title' => $link->name, '#title' => "
  • $link->name: $link->description [$addhits $addedit | $deadornot]
  • ", //'#description' => $link->description, //'#url' => $link->url, //'#click' => "links/refer/$link->id", //'#clicks' => $link->clicks, //'#newwindow' => $link->newwindow, //'#edit' => "links/edit/$link->id", ); } $cc++; } if($cc>$cat->shown) { $form['showmore'] = array( '#type' => 'markup', '#value' => l('Show more', "links/view/all/$me"), ); } $cl = db_query("SELECT name, id, collapsed FROM {links_categories} WHERE parent=%d ORDER BY name;", $me); while($cat = db_fetch_object($cl)) { $result = db_query("SELECT * FROM {links_links} where category='$cat->id' AND active=1"); $linkcount = db_num_rows($result); $form['cat' . $cat->id] = array( '#type' => 'fieldset', '#title' => "$cat->name ($linkcount)", '#collapsible' => TRUE, '#collapsed' => ($cat->collapsed==1), ); linksdb_display_rec($cat->id, $form['cat' . $cat->id]); } } function linksdb_display() { return drupal_get_form('linksdb_display_form', 'linksdb_display'); } function linksdb_display_form() { $form = array(); linksdb_display_rec(0, $form['root']); $form = form_builder('linksdb_display_form', $form); return $form; } function linksdb_edit($id = NULL) { return drupal_get_form('linksdb_edit_form', $id, 'linksdb_edit'); } function linksdb_edit_form($id = NULL) { $form = array(); $copt = array(); $copt[0] = '<'.t('Root').'>'; $tree = array(); linksdb_childnodes(0, 1, $tree); $ccount = 0; foreach($tree as $cat) { $copt[$cat['id']]=$cat['title']; $ccount++; } if($ccount == 0) { drupal_set_message("You have no categories. Add a category first.", 'error'); return ""; } if($id) { $link = db_fetch_object(db_query("SELECT * FROM {links_links} WHERE id=%d;", $id)); $form['id'] = array('#type' => 'hidden', '#value' => $id); } else { $link->active=1; } $form['cat'] = array( '#type' => 'select', '#options' => $copt, '#title' => t('Category'), '#description' => t('Category this link fits into'), '#default_value' => $link->category, ); $form['title'] = array( '#type' => 'textfield', '#title' => t('Title'), '#description' => t('Title of the website'), '#default_value' => $link->name, ); $form['url'] = array( '#type' => 'textfield', '#title' => t('URL'), '#default_value' => $link->url, '#description' => t('Address of the website. Be sure to include the http:// part'), ); $form['description'] = array( '#type' => 'textarea', '#title' => t('Description'), '#description' => t('Description of this website'), '#default_value' => $link->description, ); $form['active'] = array( '#type' => 'checkbox', '#title' => t('Active'), '#description' => t('Turning this off will push the link into the moderation queue'), '#default_value' => $link->active, ); $form['newwindow'] = array( '#type' => 'checkbox', '#title' => t('New Window'), '#description' => t('Open the link in a new window'), '#default_value' => $link->newwindow, ); $form['dead'] = array( '#type' => 'checkbox', '#title' => t('Dead link'), '#description' => t('This link has been marked as dead'), '#default_value' => $link->dead, ); $form['deadcomment'] = array( '#type' => 'textfield', '#title' => t('Reason'), '#description' => t('This is the reason the link has been marked as dead'), '#default_value' => $link->deadcomment, ); $form['addlink'] = array( '#type' => 'submit', '#value' => t('Add Link'), ); if($id) { $form['dellink'] = array( '#type' => 'submit', '#value' => t('Delete Link'), ); } return $form; } function linksdb_edit_form_submit($form_id, $values) { $op = isset($_POST['op']) ? $_POST['op'] : ''; if($op == t('Add Link')) { $id = $values['id']; if($id) { db_query("UPDATE {links_links} SET name='%s', description='%s', url='%s', category=%d, active=%d, dead=%d, deadcomment='%s', newwindow=%d WHERE id=%d;", $values['title'], $values['description'], $values['url'], $values['cat'], $values['active'], $values['dead'], $values['deadcomment'], $values['newwindow'], $id); drupal_set_message(t('Link updated')); } else { db_query("INSERT INTO {links_links} (name, description, url, category, active, dead, deadcomment, newwindow) VALUES ('%s', '%s', '%s', %d, %d, %d, '%s', %d);", $values['title'], $values['description'], $values['url'], $values['cat'], $values['active'], $values['dead'], $values['deadcomment'], $values['newwindow']); drupal_set_message(t('Link added')); } } if($op == "Delete Link") { $id = $values['id']; db_query("DELETE FROM {links_links} WHERE id=%d;", $id); drupal_set_message(t('Link deleted')); } return "links"; } function linksdb_childnodes($me, $depth, &$tree) { $out = ""; $space = ""; for($x=0; $x<$depth; $x++) { $space .= "--"; } $nodes = db_query("SELECT id, name FROM {links_categories} WHERE parent=%d ORDER BY name;", $me); while($cat = db_fetch_object($nodes)) { $tree[] = array('id' => $cat->id, 'title' => "$space $cat->name"); linksdb_childnodes($cat->id, $depth+1, $tree); } } function linksdb_categories() { $tree = array(); $categories=""; linksdb_childnodes(0, 1, $tree); foreach($tree as $cat) { $categories .= l($cat['title'], "links/category/edit/" . $cat['id']) . "
    "; } $addnew = l("Add new category", "links/category/edit"); return "

    $categories
    $addnew"; } function linksdb_categories_edit($id=NULL) { return drupal_get_form('linksdb_categories_edit_form', $id); } function linksdb_categories_edit_form($id=NULL) { $form = array(); $copt = array(); $copt[0] = '<'.t('Root').'>'; $tree = array(); linksdb_childnodes(0, 1, $tree); foreach($tree as $cat) { if($cat['id'] != $id) { $copt[$cat['id']]=$cat['title']; } } if($id) { $cat = db_fetch_object(db_query("SELECT name, parent, collapsed, shown FROM {links_categories} WHERE id=%d;", $id)); $form['id'] = array('#type' => 'hidden', '#value' => $id); } $form['cname'] = array( '#type' => 'textfield', '#title' => 'Category Name', '#description' => t('Enter the name of the category.'), '#default_value' => $cat->name, ); $form['cparent'] = array( '#type' => 'select', '#title' => t('Parent'), '#options' => $copt, '#default_value' => $cat->parent, ); $form['ccollapsed'] = array( '#type' => 'checkbox', '#title' => t('Collapsed'), '#description' => t('Collapse the category'), '#default_value' => $cat->collapsed, ); $form['cshown'] = array( '#type' => 'textfield', '#size' => 4, '#title' => t('Number to show'), '#description' => t('Number of links to show before a "show all" link is added'), '#default_value' => $cat->shown ? $cat->shown : 10, ); if($id) { $form['save'] = array( '#type' => 'submit', '#value' => ('Update Category'), ); $form['delete'] = array( '#type' => 'submit', '#value' => ('Delete Category'), ); } else { $form['save'] = array( '#type' => 'submit', '#value' => t('Add Category'), ); } return $form; } function linksdb_categories_edit_form_submit($form_id, $values) { $id = $values['id']; $op = isset($_POST['op']) ? $_POST['op'] : ''; if($id) { if($op == t('Delete Category')) { $cat = db_fetch_object(db_query("SELECT parent FROM {links_categories} WHERE id=%d;", $id)); db_query("UPDATE {links_links} SET category=%d WHERE category=%d;", $cat->parent, $id); db_query("DELETE FROM {links_categories} WHERE id=%d;", $id); drupal_set_message("Category deleted"); } else { db_query("UPDATE {links_categories} SET name='%s', parent=%d, collapsed=%d, shown=%d WHERE id=%d;", $values['cname'], $values['cparent'], $values['ccollapsed'], $values['cshown'], $values['id']); drupal_set_message("Category updated"); } } else { db_query("INSERT INTO {links_categories} (name, parent, collapsed, shown) values ('%s', %d, %d, %d);", $values['cname'], $values['cparent'], $values['ccollapsed'], $values['cshown']); drupal_set_message("Category added"); } return "links/category"; } function linksdb_settings() { $form = array(); $form['linksdb_show_clicks'] = array( '#type' => 'checkbox', '#title' => t('Show "[X Clicks]" by links'), '#default_value' => variable_get('linksdb_show_clicks', 1), '#description' => t('Show or hide the number of clicks a link has had'), ); $form['linksdb_show_report'] = array( '#type' => 'checkbox', '#title' => t('Show "report link as dead"'), '#default_value' => variable_get('linksdb_show_report', 1), '#description' => t('Show or hide the link to report a link as dead'), ); $form['linksdb_message'] = array( '#type' => 'textarea', '#title' => t('Message'), '#default_value' => variable_get('linksdb_message', t('A selection of useful links')), '#description' => t('A message to show at the top of the links page'), ); return system_settings_form($form); } function linksdb_import() { return drupal_get_form('linksdb_import_form'); } function linksdb_import_form() { $form = array(); $form["text"] = array( '#type' => 'textarea', '#title' => 'Import text', '#description' => 'Paste in text that contains the links to import. It should be in the following format:
    <cat>Category Title
    http://linkurl.com|link Title|link Description
    http://anotherlink.com|another title|another link description', ); $form['import'] = array( '#type' => 'submit', '#value' => t('Import'), ); return $form; } function linksdb_import_form_validate($form_id, $form_values) { } function linksdb_import_form_submit($form_id, $form_values) { $lines = explode("\r\n",$form_values['text']); $category = ''; //category insert query example //insert into links_categories(name,collapsed,shown,parent) values('new cat2',0,10,0),('new cat3',0,10,0) $catSql = "insert into {links_categories}(id,name,collapsed,shown,parent) values(%d,'%s',0,10,0)"; $linkSql = "insert into links_links(name,url,description,category) values('%s','%s','%s',%d)"; $result = db_query("SELECT max(id) from {links_categories}"); $catId = db_result($result); drupal_set_message(print_r($lines,true)); //$categories = array(); //process categories foreach ($lines as $line) { if (substr($line,0,5) == "") { //a category $catId++; $category = substr($line,5,strlen($line)); $result = db_query($catSql,$catId,$category); drupal_set_message("category found: $category"); drupal_set_message($result); } else { $values = explode('|',$line); //a link //$values[0] - url //$values[1] - name //$values[2] - description $result = db_query($linkSql,$values[1],$values[0],$values[2],$catId); drupal_set_message('url result: ' . $result); } } //db_query("SELECT id,name from {links_categories} where name = %s", //drupal_set_message(print_r($categories,true)); }