TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

March 29, 2024, 04:43:11 AM

Login with username, password and session length
Members
Stats
  • Total Posts: 195,105
  • Total Topics: 21,213
  • Online today: 243
  • Online ever: 3,540 (September 03, 2022, 01:38:54 AM)
Users Online
  • Users: 0
  • Guests: 245
  • Total: 245

[Block] Linkit - A member submittable links block

Started by Thurnok, August 02, 2006, 10:01:53 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Thurnok

Block Type: phpblock
Block Location: Left/Right/Center/Frontpage/Article Block
SMF/TP Supported Versions: SMF 1.1RC2 and up, TP 0.8.6 and up
Description:
This block allows you to have a member submitted links block within your site.  Members are granted access per membergroup.  You can grant ADD, EDIT, and DELETE access individually to any group(s).  Admins of course have all access.  You can also deny access to any member regardless if they are in an allowed group.  Additionally, you can supply a deny group which denies a member access if they are a member of that group even if they are a member of an allowed group.

Members fill in a short description for the site and the URL.  Mouse "hovering" over links displays member who submitted link.  Links are displayed in block, x number of columns as you specify in the configuration.  Members in a membergroup that have EDIT or DELETE access will see a radio button before the link for selection when editing or deleting links.  They will also have an edit button and/or delete button depending on that access.

Additionally, if you allow members to edit and/or delete their own links, then members who have submitted links will have the radio buttons before each of their links and the edit and/or delete button at the bottom to act on those links as appropriate.

Config Variables:
$li_addlink_groups = array('') :: Set the membergroups you want able to ADD links in this array.  Multiple groups supported - Example  array('5', '9', '16').
$li_editlink_groups = array('') :: Set the membergroups you want able to EDIT links in this array.  Multiple groups supported - Example  array('6', '3', '12').
$li_dellink_groups = array('') :: Set the membergroups you want able to DELETE links in this array.  Multiple groups supported - Example  array('2', '4', '6').
$li_deny_groups = array('') :: If user is a member of any of these groups, deny them ADD/EDIT/DELETE access regardless of other group membership they have.
$li_deny_members = array('') :: If user's member ID is in this array, deny them ADD/EDIT/DELETE access regardless of any group membership they have.
$li_edit_own = x :: 0 = No, 1 = Yes - allow users to edit links they submitted
$li_del_own = x :: 0 = No, 1 = Yes - allow users to delete links they submitted

$li_editbox_size = <integer> :: Size of the edit box displayed for Description and URL
$li_urlname_maxsize = <integer> :: Maximum size of the text used for the URL Name (description)
$li_columns = <integer> :: Number of columns of links you want displayed.
$li_start_row = <tr "some options / styles"> :: Allows you to configure row styles and options if you want (default = '<tr>')
$li_start_col = <td "some options / styles"> :: Allows you to configure column styles and options if you want (default = '<td>')
$li_tablename = tablename (without prefix) :: Set this to the table name you want to use.  The smf prefix you have configured in smf will automatically be prepended to this name.
$li_sort = sort order :: 0 = sort chronologically, 1 = sort alphabetically, 2 = sort by submitter name.
$li_sort_direction = ascending or descending :: 0 = ascending, 1 = descending.


There are other configuration variables that are less important or at least don't need to be changed as much.  For info on those, see the comments in the code itself.


//////////////////////////////////////////////
// Linkit Version 1.7.1
// Developed by Thurnok
// thurnok -AT- tinyport .DOT. net
// Complete Computer Services
// August 22, 2006
//
// Last update May 18, 2007
//
// 1.7.1:
// - added explicit sort by ID when $li_sort = 0 so that if you edit your database table, changing ID numbers,
// you can sort by the ID instead of the order data was entered into the table
//
// This is a php block and/or php Article snippet.  It works in any block
// position (left/right/center/frontpage/article).
// It allows for multiple columns of links via a table.
//
// Linkit allows you to give your users the ability to add links to your
// site in a Tiny Portal block that you designate.  Your Admins can
// edit/remove entries, and you determine what 'groups' can post links!
// You can also give groups abilities to edit or delete links as well.
// Additionally, you can allow members to edit/delete their own submissions.
//
// This script will create a table for the links using your current
// TP table prefix followed by linkit (ex: smf_tp_linkit) and using your
// current database credential information.
// NOTE: Your database user/permissions used for SMF must allow you to
// create a table or you will never be able to store the links.  You
// can create the table manually if necessary.
//
//////////////////////////////////////////////

/*
****************************************
****************************************
*** !! User Configuration Section !! ***
****************************************
****************************************
*/
// *****   SECURITY OPTIONS   *****
// who has addlink access? -  format is array('<groupnum>', '<groupnum>', ...)
// Example: $li_addlink_groups = array('14');
$li_addlink_groups = array('');
// who can edit links? - same format as addlink
$li_editlink_groups = array('');
// who can delete links? - same format as addlink
$li_dellink_groups = array('');
// group that you want members denied usage even if in a group above
$li_deny_groups = array('');
// members you want to deny usage even if allowed above
$li_deny_members = array('');
// allow members to edit/delete their own links? (0 = No, 1 = Yes)
$li_edit_own = 0;
$li_del_own = 0;

// *****   LAYOUT OPTIONS   *****
// size of the edit boxes (<input>)
$li_editbox_size = 15;
// max size of the input for the URL Name (30 or less is best)
$li_urlname_maxsize = 30;
// sorting option - 0 = chronological, 1 = alphabetic, 2 = submitter
$li_sort = 0;
// sorting direction - 0 = ascending, 1 = descending
$li_sort_direction = 0;
// number of columns of links to display
$li_columns = 1;
// want to add your own styles to the rows/columns?  change the tag info here
$li_start_row = '<tr>';
$li_start_col = '<td>';
// approximate number of lines displayed in block 0 = unlimited
$li_blocklines = 30;

// you can make the columns use one of TinyPortal's text classes ("normaltext" or "smalltext") or none.
// this will add to the $li_start_col variable the classtype you choose (0 = no class, 1 = smalltext, 2 = normaltext)
// if you are making your own text styles in the $li_start_col variable above, you should set $li_useclass = 0
$li_useclass = 1;

// make it easier to use non-blanking spaces (TP blocks/articles will remove them from your code the next time you edit the same block/article)
// using a variable instead, will let you keep that code intact.
$nbsp = '&'.'nbsp;';

// *****   OTHER OPTIONS   *****
// you can set the tablename to other than linkit if you like
$li_tablename = "linkit";
// and the title displayed
$li_title = "Linkit!";
// add some descriptive text here if you like to display under title
$li_desc = "Member added links! ".$nbsp."Please add only your most favorite links.";
// Print our title - or comment the line to not display a title
if (!empty($li_title))
echo '<center><b>' . $li_title . '</b></center><br />';
if (!empty($li_desc))
echo '<font size=1>' . $li_desc . '</font><p />';
/*
****************************************
****************************************
*/

//////////////////////////////////////////////
//
// The rest of this you should leave as is
// unless you are overly industrious :)
//
//////////////////////////////////////////////
// globals for database vars
global $db_prefix, $tp_prefix;
// globals for user information
global $context, $user_info, $ID_MEMBER;

// fix for TP 0.8.6 and lower
if (empty($tp_prefix)){
$tp_prefix = $settings['tp_prefix'];
}

switch ($li_useclass){
case 1:
$li_classtxt = ' class="smalltext" ';
$li_start_col = substr($li_start_col, 0, -1) . $li_classtxt . '>';
break;
case 2:
$li_classtxt = ' class="normaltext" ';
$li_start_col = substr($li_start_col, 0, -1) . $li_classtxt . '>';
break;
default:
$li_classtxt = '';
break;
}

// get our script url (including parameters - like ?page=6)
$myself = $_SERVER['REQUEST_URL'];

// put the SMF table prefix in front of your tablename from above
$li_tablename = $tp_prefix . $li_tablename;
// check if user is in a group that is allowed to add links
$li_add_auth = array_intersect($li_addlink_groups, $user_info['groups']);
// check if user is in a group that is allowed to edit links
$li_edit_auth = array_intersect($li_editlink_groups, $user_info['groups']);
// check if user is in a group that is allowed to delete links
$li_del_auth = array_intersect($li_dellink_groups, $user_info['groups']);
// deny if in one of the deny groups or members
if (array_intersect($li_deny_groups, $user_info['groups']) || @in_array($ID_MEMBER, $li_deny_members)){
$li_add_auth = false;
$li_edit_auth = false;
$li_del_auth = false;
$li_edit_own = false;
$li_del_own = false;
}
// Admins are always allowed to add/edit/delete links
if ($context['user']['is_admin']){
$li_add_auth = 1;
$li_edit_auth = 1;
$li_del_auth = 1;
}

// set up all our functions ahead of time
// function to create table if not already there
function LinkitCreateTable($li_tablename) {
// set up the query that will create the table appropriately
$dbquery = "CREATE table $li_tablename (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
url_name TEXT, url TEXT, submitted_by TEXT);";
if (!mysql_query($dbquery)) {
die("Query Failed!  Table NOT Created!<br />\n");
}
}

// function to add links to the table
function LinkitAddLink($li_tablename, $li_urlname, $li_urllink, $li_submittedby) {
if ( (strtolower(substr($li_urllink, 0, 7)) != "http://") && (strtolower(substr($li_urllink, 0, 6)) != "ftp://") ){
$li_urllink = "http://" . $li_urllink;
}
// first see if this would be a duplicate, if so, do not post it
$dbquery = "SELECT * FROM $li_tablename
WHERE url LIKE '" . $li_urllink . "'";
$dbresult = mysql_query($dbquery);
if ($row = mysql_fetch_assoc($dbresult)){
// if a row is found, then there's already this link in table, don't dupe it
return;
}
$dbquery = "INSERT INTO $li_tablename VALUES (0, \"$li_urlname\", \"$li_urllink\", \"$li_submittedby\");";
if (!mysql_query($dbquery)) {
die("Query Failed!  Link NOT Inserted into database!<br />\n");
}
}

// function to edit links in the table
function LinkitEditLink($li_tablename, $li_urlname, $li_urllink, $li_id) {
// make sure only one ID is actually in there
if ((string)(int)$li_id === (string)$li_id){
// change only values that were there in form
$dbquery = "UPDATE $li_tablename SET ";
if ($li_urlname){
// if there was a url name provided, add it to UPDATE query
$dbquery .= "url_name=\"$li_urlname\" ";
}
if ($li_urllink){
// add HTTP:// if necessary at front of link to prevent BASE URL applying in front of link provided
if ( (strtolower(substr($li_urllink, 0, 7)) != "http://") && (strtolower(substr($li_urllink, 0, 6)) != "ftp://") ){
$li_urllink = "http://" . $li_urllink;
}
// since url link was provided, add it to UPDATE query
if ($li_urlname){
// since we already added url name, put comma and space before the url link update part
$dbquery .= ", url=\"$li_urllink\" ";
} else {
// didn't have an url name added, so no comma needed
$dbquery .= "url=\"$li_urllink\" ";
}
}
// add rest of query
$dbquery .= "WHERE id=\"$li_id\";";
if (!mysql_query($dbquery)) {
die("Query Failed!  Link NOT modified in database!<br />\n");
}
}
}

// function to delete links from the table
function LinkitDelLink($li_tablename, $li_id) {
// delete link(s) in $li_id
$dbquery = "DELETE FROM $li_tablename WHERE id in ( " . $li_id . ")";
if (!mysql_query($dbquery)) {
die("Query Failed!  Link NOT Deleted from database!<br />\n");
}
}

///////////  MAIN CODE HERE  ////////////
// convert $_POST vars to prevent undefined index errors
$li_add = empty($_POST['li_add']) ? 0 : 1;
$li_edit = empty($_POST['li_edit']) ? 0 : 1;
$li_del = empty($_POST['li_del']) ? 0 : 1;
$li_urlname = empty($_POST['li_urlname']) ? '' : $_POST['li_urlname'];
$li_urllink = empty($_POST['li_urllink']) ? '' : $_POST['li_urllink'];
$li_checklist = empty($_POST['li_checklist']) ? '' : $_POST['li_checklist'];

// if someone just added a link, post it to the database
if ($li_add){
$li_urlname = trim($li_urlname);
$li_urllink = trim($li_urllink);
if ($li_urlname && $li_urllink){
LinkitAddLink($li_tablename, $li_urlname, $li_urllink, $user_info['username']);
}
}

// if someone just edited a link, modify it in database
if ($li_edit && $li_checklist){
$li_urlname = trim($li_urlname);
$li_urllink = trim($li_urllink);
if ($li_urlname || $li_urllink){
LinkitEditLink($li_tablename, $li_urlname, $li_urllink, $li_checklist);
}
}

// if someone just deleted a link, remove it from database
if ($li_del && $li_checklist){
LinkitDelLink($li_tablename, $li_checklist);
}

////////////  MAIN DISPLAY CODE HERE  ///////////////

// set query to select all data in appropriate order
switch ($li_sort){
// alphabetical order
case 1:
$dbquery = $li_sort_direction ? "SELECT * from $li_tablename ORDER BY url_name DESC" : "SELECT * from $li_tablename ORDER BY url_name";
break;
// submitted by order
case 2:
$dbquery = $li_sort_direction ? "SELECT * from $li_tablename ORDER BY submitted_by DESC" : "SELECT * from $li_tablename ORDER BY submitted_by";
break;
// chronological order
default:
$dbquery = $li_sort_direction ? "SELECT * from $li_tablename ORDER BY id DESC" : "SELECT * from $li_tablename ORDER BY id ASC";
}

$dbresult = mysql_query($dbquery);
if (!$dbresult){
if (mysql_errno() == 1146){
// table doesn't exist, create it!
LinkitCreateTable($li_tablename);
// get our result again
$dbresult = mysql_query($dbquery);
if (!$dbresult) die("Unexpected error: " . mysql_error());
} else {
die("Unexpected error: " . mysql_error());
}
}

// javascript validations
echo '
<script type="text/javascript">
<!--
function addCheck(){
urlname = document.li_form.li_urlname;
urllink = document.li_form.li_urllink;
if (urlname.value.replace(/ /g,"") == "" || urllink.value.replace(/ /g,"") == ""){
alert("Missing information - Must supply both URL Name and URL Link!");
return false;
}
}

function editCheck(){
retval = false;
checklist = "";
numchecked = 0;
urlname = document.li_form.li_urlname;
urllink = document.li_form.li_urllink;
checkboxes = document.li_form.li_checkbox;
if (urlname.value.replace(/ /g,"") != "" || urllink.value.replace(/ /g,"") != ""){
for (i=0; i<checkboxes.length; i++){
if (checkboxes[i].checked == true){
checklist = checkboxes[i].value;
numchecked++;
}
}
switch (numchecked){
case 0:
alert("You must select a link to edit first!");
break;
case 1:
document.li_form.li_checklist.value = checklist;
retval = true;
break;
default:
alert("You can only edit one link at a time!");
break;
}
} else {
alert("No information entered!");
}
return retval;
}

function delCheck(){
retval = false;
checklist = "";
checkboxes = document.li_form.li_checkbox;
for (i=0; i<checkboxes.length; i++){
if (checkboxes[i].checked == true){
checklist += (checklist != "" ? "," : "") + checkboxes[i].value;
}
}
document.li_form.li_checklist.value = checklist;
if (checklist != ""){
retval = true;
}
if (!retval){
alert("Select a link first!");
}
return retval;
}

function urlTest(){
urllink = document.li_form.li_urllink.value;
if (urllink.replace(/ /g,"") != ""){
if (urllink.toLowerCase().substr(0, 7) != "http://" && urllink.toLowerCase().substr(0, 8) != "https://"){
urllink = "http://" + urllink;
}
// display a new window and open url in it
window.open(urllink, "TestUrl", "width=600px, height=400px, resizable, scrollbars", true);
} else {
alert("Need a link to test!");
}
}

// -->
</script>
';

// if we set number of lines, make that setting here
if (!empty($li_blocklines)){
// pad according to class chosen
switch ($li_useclass){
case 1:
// smalltext class - padding 8 for IE, and 5 for all other browsers
$li_blocklines += empty($context['browser']['is_ie']) ? 5 : 8;
break;
case 2:
// normaltext class - padding 14 for IE, and 9 for all other browsers
$li_blocklines += empty($context['browser']['is_ie']) ? 9 : 14;
break;
default:
// no class - padding 10 for IE, 9 for all other browsers
$li_blocklines += empty($context['browser']['is_ie']) ? 9 : 10;
}
echo '
<div style="width: 100%; overflow: auto; height: '.$li_blocklines.'em;">
';
}

// start our form
if (empty($context['browser']['is_ie'])){
echo "\n" . '<form name="li_form" action="' . $myself . '" method=post'.$li_classtxt.'>' . "\n";
} else {
echo "\n" . '<form name="li_form" action="' . $myself . '" method=post>' . "\n";
}

// preset our current column to first column
$li_current_column = 1;

// start our table and first row
echo '<center><table width="90%" border="0"><tr>' . "\n";

// gets set to 1 if there is at least one radio button made next to a link
$li_link_owner = 0;

// parse our data out
while ($row = mysql_fetch_assoc($dbresult)){
// new row?
if ($li_current_column > $li_columns){
// time to end current row and start new one
$li_current_column = 1;
echo "</tr>\n" . $li_start_row . "\n";
}
// each link has a column to start with
echo ' ' . $li_start_col;
// if they have edit or delete privileges then display checkbox
if ($li_edit_auth || $li_del_auth){
echo '<input type=checkbox name="li_checkbox" id="li_checkbox" value="'.$row['id'].'" /> ';
} elseif (($user_info['username'] == $row['submitted_by']) && ($li_edit_own || $li_del_own)){
// if they own this link in the list, and either edit own or delete own is on, display checkbox
echo '<input type=checkbox name="li_checkbox" id="li_checkbox" value="'.$row['id'].'" /> ';
$li_link_owner = 1;
}
echo '<a href="' . $row['url'] . '" title="Submitted by ' . $row['submitted_by'] . '" target=_blank>' . $row['url_name'] . '</a></td>' . "\n";
// next column number
$li_current_column++;
}

// end our last row and our table
echo "</tr>\n</table></center>\n";

// if there are checkboxes, add the check/uncheck all
if ($li_edit_auth || $li_del_auth || ($li_link_owner && ($li_edit_own || $li_del_own))){
echo '<div'.$li_classtxt.'><input type=checkbox name="li_checkall" value="" onClick="invertAll(this, this.form, \'li_checkbox\');"> Check/Uncheck all</input></div>';
}

// if user is allowed to add/edit/delete links (or their own and one exists), display URL Name and URL Link edit boxes
if ($li_add_auth || $li_edit_auth || $li_del_auth || ($li_link_owner && ($li_edit_own || $li_del_own))){
echo '<br />
<div'.$li_classtxt.'>URL Name:</div>
<input type=text name="li_urlname" size=' . $li_editbox_size . ' maxlength=' . $li_urlname_maxsize . '><br />
<div'.$li_classtxt.'>URL Link:</div>
<input type=text name="li_urllink" size=' . $li_editbox_size . '><input type=button name="li_testurl" value="Test" onClick="return urlTest()"><br /><br />
';
}
// if user is allowed to add links, display Add button
if ($li_add_auth){
echo '<input type=submit name="li_add" value="Add" onClick="return addCheck()" />&nbsp;&nbsp;';
}
// if user is allowed to edit links (or their own and one exists), display edit link button
if ($li_edit_auth || ($li_link_owner && $li_edit_own)){
echo '<input type=submit name="li_edit" value="Edit" onClick="return editCheck()" />&nbsp;&nbsp;';
}
// if user is allowed to delete links (or their own and one exists), display delete link button
if ($li_del_auth || ($li_link_owner && $li_del_own)){
echo '<input type=submit name="li_del" value="Del" onClick="return delCheck()" />';
}

// our hidden elements
echo '<input type=hidden name="li_checklist" value="">';

// and finally, end our form
echo '</form>';

// if we added the <div> for our blocklines, close it here
if (!empty($li_blocklines))
echo '</div>';

// free the result for good measure
mysql_free_result($dbresult);


rbh

takes a little configurating but i like! forum sponsors can be added to a special group and given access to post what links they wish and edit them as need possibly for specials or what have you. thanks for the snippet!

if anyone else is going to use this, it goes in a php block, and change the wording so it says url doesnt require http:// as this is added automatically.


Thurnok

Sorry... I should have put a bit more explanation in there, but it was a bit late.  :laugh:

Some things to note:
  • putting http:// in front of your link in the URL Link editbox is not required as the script will do this for you if it is absent
  • the edit button (e), for those that have edit privilege, uses the same URL Name and URL Link editboxes for making changes.  If you leave one or the other empty, that value is left unchanged for that link entry.  In otherwords, to keep the same "name" for the link, just leave the URL Name editbox empty (same for URL Link box if you only want to change the name and not the link).  Obviously at least one needs to have something in it to make a change.
  • the delete button (d) does not require either editbox, it simply deletes that entry.
  • each permission (add, edit, delete) is seperate, so theoretically you could have members with edit permission but not add.  Don't know why you would, but the versatility is there.
  • Admins always have add/edit/delete permissions.
  • Mouse hover on name shows who submitted the link
  • The editbox for the URL Name is resticted to 20 characters because I wanted it to fit in a side block and prevent the block from being messed up if someone happened to put in a long name.  Obviously if you put this in a center block you can support much longer names without messing up the "look" of the site.

    Things I still plan to do:
  • link authentication so users with only Add permission don't have to ask an Admin to fix their typo's all the time.  Possibly asking the user if they want a test popup to the link before adding it to the database, with a confirmation after it or something.
  • add another setting to to turn on/off ability for a member to edit and/or delete links "they" added.

    There are a few other things I plan to do (hopefully this weekend - work has been busy), which I will post soon as I get to it.

    If you don't want to play with this code yourself, but have ideas of something you want in it, let me know and I'll try to accommodate.

Xarcell


londonhogfan

This is very good.  I love how it actually uses permissions by membergroup.  I have been trying to add some stuff to my site to give some perks to the people that have donated to our clan.  This is a good feature that the guys at my site will love.

I know this is off topic but does anyone know of a way to restrict certain BBCodes by membergroup?

Thanks again for the great block.

Thurnok

Modified the code a bit to fix the "Undefined Index" warnings left in your Forum error logs.  These would occur if the page was refreshed outside of using the form since the $_POST vars would no longer be set.  Sorry about that.  ;)

New code has replaced original code in the original post.  Get it from there.

G6Cad

Thank you Thurnoc for the code and the work behind it :)

Thurnok

One more quick update before I head out for some weekend fun - Updated the code in first post to not allow duplicates.

zillion

Hmm.  Getting an error message: "Unexpected error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)"  Any ideas what I've...
~not done
~done, but shouldn't have
~done, but done wrong
Bound to be be one of them!