Location: PHPKode > scripts > Sortable Elements > sorting.inc.php
<?
/************************************************************************/
/* sorting.inc.php                                                      */
/* ===========================                                          */
/* Copyright (c) 2008 by Sean Dempsey (hide@address.com)            */
/* http://www.seandempsey.com                                           */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/
//
// Use:
// // Create a Sortable Elements object.
// $sortable = new Sortable_Elements(name of item, database table, primary key, database field to sort, database sorting_field);
// Example:
// $sortable = new Sortable_Elements("Friend", "tblFriends", "pkFriendID", "fldName", "fldSort");

class Sortable_Elements {

	var $item_plural;
	var $item;

	var $db_table;
	var $db_pk;
	var $db_main_field;
	var $db_sort_field;

	// Constructor
	function Sortable_Elements($item, $db_table, $db_pk, $db_main_field, $db_sort_field) {

		$this->insertHeader();

		$this->item 			= $item;
		$this->item_plural		= $item . "s";

		$this->db_table			= $db_table;
		$this->db_pk			= $db_pk;
		$this->db_main_field 	= $db_main_field;
		$this->db_sort_field 	= $db_sort_field;

		if ($_REQUEST['action'] != ''){
			$this->doAction($_REQUEST['action']);
		}

		$this->insertContent();

		return true;
	}

	function insertHeader(){
		echo "<link rel=\"stylesheet\" href=\"sortable_elements.css\" type=\"text/css\">\n";
		echo "<script src=\"scriptaculous/prototype.js\" type=\"text/javascript\"></script>\n";
		echo "<script src=\"scriptaculous/scriptaculous.js\" type=\"text/javascript\"></script>\n";
		echo "<script src=\"scriptaculous/unittest.js\" type=\"text/javascript\"></script>\n";
		echo "<script src=\"js/ajax_functions.js\" type=\"text/javascript\"></script>\n";
		echo "<script src=\"js/javascript_functions.js\" type=\"text/javascript\"></script>\n";

		return true;
	}

	function doAction($action){

		global $error_msg;
		global $report_msg;

	    if ($action == 'save_sorting'){
	        $sort_list = $_REQUEST['sortingParams'];
	        $item_order = 1;
	        $order_array = explode(",", urldecode($sort_list));

	        foreach($order_array as $k=> $key) {
	            qr("UPDATE $this->db_table SET $this->db_sort_field = $item_order WHERE $this->db_pk = $key");
	            $item_order++;
	        }
	        $report_msg[] = "Order of $this->item_plural Has Been Modified";

	    }

		if ($action == 'add'){
			$new_field = $_REQUEST['new_field'];
			$success = qr("INSERT INTO $this->db_table ($this->db_main_field) VALUES ('$new_field')");
			if ($success){
				$report_msg[] = "$this->item Added";
			}
			else{
				$error_msg[] = "$this->item could not be added. Please try again.";
			}
		}

		if ($action == 'delete' && $_REQUEST['id'] != ''){
			$delete_id = $_REQUEST['id'];
			$success = qr("DELETE FROM $this->db_table WHERE $this->db_pk = $delete_id");
			if ($success){
				$report_msg[] = "$this->item Deleted";
			}
			else{
				$error_msg[] = "$this->item could not be deleted. Please try again.";
			}
		}

	}//doAction


	function insertContent(){

		#this is an optional function which will allow you to display errors or report messages as desired.
		global $error_msg;
		global $report_msg;
		if (function_exists(echo_msg_box)){
			echo_msg_box();
		}

		#############################################
		#
		# Begin code for displaying sortable element
		#
		#############################################

		echo "<h3>Edit " . $this->item_plural . "</h3>\n";

		echo "<div id=\"mainList\">\n";

		echo "	<ul id=\"Sortable_list\" class=\"sortableList\">\n";

		$items = $this->query("SELECT $this->db_pk, $this->db_main_field, $this->db_sort_field FROM $this->db_table ORDER BY $this->db_sort_field");
		foreach ($items as $item){
			list($item_id, $item_main, $item_sort) = $item;
				echo "<li id=\"item_" . $item_id . "\">";
					$this->makeAjaxEditor($item_id, $this->db_main_field, $item_main);
				echo "</li><span class=\"handle\"></span> &nbsp;&nbsp;<a href=\"#\" class=\"editingSize\" onClick=\"confirmDelete('" . $_SERVER['PHP_SELF'] . "?action=delete&id=" . $item_id . "');\">delete</a>\n";
		}
		echo "	</ul>\n";

		echo "</div>\n";

		echo "<center>\n";
		echo "<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"GET\" onSubmit=\"populateHiddenVars();\" name=\"sortableListForm\" id=\"sortableListForm\">\n";
		echo "	<input type=\"hidden\" name=\"action\" value=\"save_sorting\">\n";
		echo "	<input type=\"text\" style=\"display:none;\" name=\"sortingParams\" id=\"sortingParams\" size=\"60\">\n";
		echo "	<input type=\"submit\" value=\"Commit Sorting Changes\" class=\"button\" onClick=\"populateHiddenVars();\">\n";
		echo "	<input type=\"button\" value=\"Add Item\" class=\"button\" onClick=\"document.getElementById('add_form').style.display = '';\">\n";
		echo "	<input type=\"button\" value=\"Go Back\" class=\"button\" onClick=\"window.location = '" . $_SERVER['PHP_SELF'] . "';\">\n";
		echo "</form>\n";
		echo "</center>";

		echo "<form action=\"" . $_SERVER['PHP_SELF'] . "\" id=\"add_form\" method=\"POST\" style=\"display:none;\">\n";
		echo "<table class=\"form\" align=\"center\">\n";
		echo "	<tr>\n";
		echo "	<td class=\"form_td\">New <b>Item</b></td>\n";
		echo "	<td class=\"form_td\"><input class=\"editingSize\" type=\"text\" name=\"new_field\" size=\"25\"></td>\n";
		echo "	<td class=\"form_td\"><input class=\"editingSize\" type=\"submit\" value=\"Add Item\"></td>\n";
		echo "	</tr>\n";
		echo "</table>\n";

		echo "<input type=\"hidden\" name=\"action\" value=\"add\">\n";
		echo "</form>\n";

		echo "<script type=\"text/javascript\">\n";
		echo " Sortable.create('Sortable_list',{tag:'li'});\n";
		echo "</script>\n";

	}


	function makeAjaxEditor($unique_id, $field_name, $field_value){
		$prefield = $unique_id . "_" . $field_name;
	?>
		<span class="editable" id="<?=$prefield?>_rg_display_section" onClick="
			document.getElementById('<?=$prefield?>_hv_editing_section').style.display = '';
			document.getElementById('<?=$prefield?>_rg_display_section').style.display = 'none';
			document.getElementById('<?=$prefield?>').focus();
			"><?=$field_value?></span>
		<span id="<?=$prefield?>_hv_editing_section">
			<form style="display: inline;" name="form_<?=$prefield?>" id="form_<?=$prefield?>" onSubmit="
				document.getElementById('<?=$prefield?>_hv_editing_section').style.display='none';
				document.getElementById('<?=$prefield?>_hv_saving_section').style.display='';
				var req = 'ajax_save.php?table=<?=$this->db_table?>&pk=<?=$this->db_pk?>&id=<?=$unique_id?>&field=<?=$field_name?>&val=' + document.getElementById('<?=$prefield?>').value;
				sndReq(req);
				return false;
			">
			<input type="text" class="editingSize editMode" size="20" value="<?=$field_value?>" id="<?=$prefield?>" />
			<input type="button" class="editingSize" value="Cancel" onClick="
				document.getElementById('<?=$prefield?>_rg_display_section').style.display = '';
				document.getElementById('<?=$prefield?>_hv_editing_section').style.display = 'none';
			"/>
			</form>
		</span>
		<span id="<?=$prefield?>_hv_saving_section" class="savingAjaxWithBackground">
			Saving...
		</span>
		<script type="text/javascript">
			document.getElementById('<?=$prefield?>_hv_editing_section').style.display = 'none';
			document.getElementById('<?=$prefield?>_hv_editing_section').style.display = 'none';

			document.getElementById('<?=$prefield?>_hv_saving_section').style.display = 'none';
			document.getElementById('<?=$prefield?>_hv_saving_section').style.display = 'none';
		</script>
<?
	}


	function query($q, $debug = 0){
		$r = mysql_query($q);
		if(mysql_error()){
			echo mysql_error();
			echo "$q<br>";
		}

		if(mysql_num_rows($r) > 1){
			while($row = mysql_fetch_array($r)){
				$results[] = $row;
			}
		}
		else if(mysql_num_rows($r) == 1){
			$results = array();
			$results[] = mysql_fetch_array($r);
		}

		else
			$results = array();

		return $results;
	}

}//class

?>
Return current item: Sortable Elements