Location: PHPKode > projects > FeedMeLinks, Social BookmarkingSince2002 > feedmelinks.com/edit-folder.php
<%
	# $Id: edit-folder.php,v 1.23 2005/01/19 05:34:49 hirokai Exp $
	$page = "edit-folder";
	$errorPage = "index";
	$shouldShowCreateEditForm = 1;
	$hotRow = -1;
	$shouldDump = 1;

	#$debug = 0;

	include_once( "modules/utils.inc.php" );
	$tok =  $authenticated = $_COOKIE["c_pass_token"];
	$u   =  $sessionUserId = $_COOKIE["c_uid"];

	$mesg = "create a new " . getCatName() . ":";
	$buttonMesg = "create a new " . getCatName() . ":";
%>
<title> Feed Me <%= getCatsName() %> </title>
<% include( "$modules/header.inc.php" ); %>
<%
	if( $tok && $tok == md5( getPasswdForUser( $u ))) {
		$con_editFolder = mysql_connect( );
		mysql_selectdb( getDBName() );

		if( $op == "edit" ) {
			$mesg = "editing " . getCatName() . " ";
			$folderID = $id;
			$buttonMesg = "save";
			$folderToEdit = $name;
		} else if( $op == "commitEdit" ) {
			$shouldShowCreateEditForm = 0;
			if( $debug ) 
				echo "<br />DEBUG: saving \"$name\", id $id<br />";

			$name = $isPrivate ? $name : makePublicFolderName( $name );
			$name = urlencode( $name );
			$qs = "UPDATE linksGroups SET name = '$name' WHERE id = $id AND userid = '$u'";

			if( $debug ) {
				echo "DEBUG: \$isPrivate: $isPrivate<br />";
				echo "DEBUG: $qs<br />";
			}
			$q = mysql_query( $qs );
			if( $q ) {
				if( $isPrivate ) {
					if( $debug ) 
						echo "DEBUG: the " . getCatName() . " \"$name\" is now/still private; making links private:<br/>";
					echo "making sure all links inside $name are private...<br/>";
					setFolderOfLinksPrivate( $id, $u );
				} else {
						if( $debug )
							echo "DEBUG: " . getCatName() . " is now/still public; leave the links alone<br/>";
					}
				
%>
	<div class="attention">
		<%= getCatName() %> renamed!
	</div>
<%
				if( !$debug ) {
					echo bounceTo( "/index" );
				}
			} else
				echo "fail!";
		} else if( $op == "confirmDelete" ) {
			$shouldShowCreateEditForm = 0;
			$shouldDump = 1;
			$hotRow = $id;
%>
	<br />
	<br />
	<div class="attention" style="width: 300px;">
		<b>delete <%= getCatName() %>:</b>
		<br />
		<img src="/img/dk-gray.gif" height="1" width="250" />
		<br />
		<p>
			really delete the <%= getCatName() %> "<%= $name %>"?
		</p>

		<p style="text-align: right;">
			<a href="<%= $page %>?op=deleteGroup&id=<%= $id %><%= $debug ? "&debug=y" : ""%>"><b>yup!</b> (just delete the <%= getCatName() %>)</a> 
			<br />
			<a href="<%= $page %>?op=deleteGroup&deleteLinks=y&id=<%= $id %><%= $debug ? "&debug=y" : ""%>"><b>yes</b>, and delete the links too</a>
			<br />
			<a href="<%= $page %>"><b>no way, man!</b></a>
		</p>
	</div><br /> 
<%
	} else if( $op == "deleteGroup" ) {
		$shouldDump = 0;
		$shouldShowCreateEditForm = 0;
  
		# for UI messages:
		$folderName = getNameForFolder( $id );
		
		$con_deleteFolder = mysql_connect();
		mysql_selectdb( getDBName() );

		$qs = "SELECT userid FROM linksGroups where ID = $id;";
		$q = mysql_query( $qs );
		if( $q )
			$numRows = mysql_num_rows($q);
		if( $numRows ) {
			$dbUserId = mysql_result($q,0,"userid");
			if( $dbUserId == $u ) {
				$qs = getQuery( "delete_category", $id );
				$q = mysql_query( $qs );
				if( $q ) { 
%>

<div class="attention">
	<h3>deleting "<%= $folderName %>"...</h3>

<%
					if( $deleteLinks ) {
						echo "deleting the links<br />";

						$qs = "SELECT linkID from linksCategoriesXRef WHERE catID = $id;";
						$q = mysql_query( $qs );
						if( $q ) {
							$dl = Array(); # deleting links
							$numRows = mysql_num_rows($q);
							
							#dum array cache cause i dont know how to make mysql do a nested query here:
							for( $i = 0; $i < $numRows; $i++ ) {
								deleteLink( mysql_result($q,$i,"linkID"), $u );
							}
						}

						$success = "all links deleted!";
						$fail = "sorry, an error occurred and the links weren&apos;t deleted!";
					} else {
						# need to also clean the fuck out of linksCategoriesXRef

						$qs = getQuery( "delete_tag_refs", $id );
						$q = mysql_query( $qs );
						if( $q ) { 
							#echo "we are just clearing the groupid of the links<br />";
							$success = "all links untagged!";
						}
						$fail = "ugh. some weird error occurred. i hate being a dumb computer. sorry!";
						$qs = "UPDATE links SET groupid = NULL WHERE submitter = '$u' AND groupid = $id";
						#$qs = "SELECT * from links where submitter = '$u' AND groupid = $id";
					}
					if( $debug ) print "DEBUG: $qs<br />\n";
					$q = mysql_query( $qs );
					if( $q ) { echo "$success<br />\n"; } else { echo "$fail<br />\n"; }
%>

</div>

<%= bounceTo( "index?mesg=" . urlencode( "'" . makePrettyFolderName( $folderName ) . "' deleted!" )) %>

<%
				} else { echo "deleting " . getCatName() . " failed! sorry, i suk!"; } 
			}
		} else {
			echo "database returned: |$q| for \"$qs\"<br>";
			echo "Sorry, can't find a group with ID #$id<br>";
		}
		conClose( $con_deleteFolder );

	} else if( $op == "createGroup" ) {

		if( $name ) {

			if( $debug ) {
				echo "<br/>";
				echo "<br/>";
				echo "\$u: $u<br/>";
				echo "\$name: $name<br/>";
			}
			
			$folderId = addFolder( urlencode( $name ), $u );
			if( $debug )
				echo "\$folderId: $folderId<br/>";

			if( $folderId ) {

				$shouldShowCreateEditForm = 0;
%>
<h3>adding <%= getCatName() %><blink>...</blink></h3>
<script language="javascript"> window.location = '<%= "index?mesg=" . urlencode( "New " . getCatName() . " $name added successfully!" ) %>'; </script>

<%
			} else { 
				$shouldShowCreateEditForm = 1;
				$mesg = "ERROR: creating " . getCatName() . " $name didn't seem to work.";
			}
		} else {
			echo "ERROR: No " . getCatName() . " name. Please try again.";

		}
	}

	if( $debug ) 
		echo "DEBUG: op: $op"; 
%>

<% if( $shouldShowCreateEditForm ) { %>
<br />
<br />

<form method="post" action="<%= $page %>">
<div class="attention" style="width: 300px;">

<b><% if( $op == "edit" ) {
		$isPrivate = ! isPublicFolder( $id );
		if( $debug ) {
			echo "\$id: $id<br/>";
			echo "\$isPrivate: $isPrivate<br/>";
		}
%>
editing <%= getCatName() %> '<%= makeFolderLinkSimple( $id ) %>'
<% } else { %>
	<%= $mesg %>
<% } %></b>
	<br />
	<img src="/img/dk-gray.gif" height="1" width="250" />
	<br />
	<br />
	
	<%= $debug ? "editing " . getCatName() . " \"$folderToEdit\" (id# $folderID)<br><br>" : "" %>
	<%= getCatName() %> name: <input name="name" size="25" value="<%= makePrettyFolderName( $folderToEdit ) %>" type="text" />
	<% if( $op == "edit" ) { %>
		<input type="hidden" name="id" value="<%= $folderID %>" />
		<input type="hidden" name="op" value="commitEdit" />
		<br />
		<label>
			<table border="0">
				<tr>
					<td valign="center">
						<input type="checkbox" name="isPrivate" <%= $isPrivate ? "checked" : "" %> />
					</td>
					<td valign="center">
						private
					</td>
				</tr>
			</table>
		</label>
	<% } else {  %>
		<input type="hidden" name="op" value="createGroup" />
	<% } %>
	
	<% if( $debug ) { %>
		<input type="hidden" name="debug" value="y" />
	<% } %>
	<p style="text-align: right;">
	<input type="submit" value="<%= $buttonMesg %>" />
	</p>
<br />
</div> </form>

<% }
%>

<%
	$shouldDump = false; # this was confusing and weird!
	if( $shouldDump ) {
		$qs = "SELECT * from linksGroups WHERE userId = '$u' ORDER BY name";
		$q = mysql_query( $qs );
		$numRows = mysql_num_rows($q);
		if( $numRows ) {
			$i = 0;
			%>
			<table cellspacing="0" cellpadding="3">
			<tr>
				<td colspan="2">
					your <%= getCatsName() %>:
				</td>
			<%
			$links = array();
			while ($i < $numRows) {
				$id   = mysql_result($q,$i,"ID");
				$options   = mysql_result($q,$i,"options");
				$name = makeSafeName( mysql_result($q,$i,"name"));
				$public = 0;
				if( isPublic( $name )) {
					$name = makePrettyFolderName( $name );
					++$public;
				}
				$i++;
				$links[$i] = array( $name, $id, $public, $options );
			}
			conClose( $con_editFolder );
			sort( $links );
			$i = 0;
			foreach( $links as $key=>$link ) {
				$name = $link[0];
				$id = $link[1];
				$public = $link[2];
				$options = $link[3];
				if( $id == $hotRow ) { 
					$bg = "bgcolor='#BA5757'"; 
				} else {
					if( $i % 2 == 0 )
						$bg = "bgcolor='#ffffff'"; 
					else
						$bg = "bgcolor='#eeeeee'"; 
				}
%>
				<tr>
					<td <%= $bg %>>
						<%= $debug ? $i + 1 . "  " : "" %>
						<%= makeFolderLink( $id, "<b>$name" . ($debug ? " ($id)" : "") . "</b>" ) %><%
				if( $public ) {%>
				[public]
<% 			
				}
%></td>
				<td <%= $bg %> width="82" align="center"><a href="<%= $page %>?op=edit&name=<%= urlencode( $name) %>&id=<%= $id %><%= $debug ? "" : ""%>"><img src="/img/edit.gif"  border="0" alt="delete" height="16" /> [edit]</a></td>
				<td <%= $bg %> width="82" align="center"><a href="<%= $page %>?op=confirmDelete&id=<%= $id %>&name=<%= urlencode( $name ) %><%= $debug ? "" : ""%>"><img src="/img/trash.gif"  border="0" alt="delete" height="16" /> [delete]</a></td>
				</tr>
<%
				$i++;
			} # end foreach
%>
			</table>
<%
			if( $debug ) echo "<br> (found $i <%= getCatsName() %>) \n<br>";
		}
	} 
%>


<br />
<% include( "$modules/footer.inc.php" ); %>
<% 
} else {  

	# end auth
	echo bounceTo( urldecode( $errorPage ));

} %>
Return current item: FeedMeLinks, Social BookmarkingSince2002