Location: PHPKode > projects > CMSmelborp > CMSmelborp/includes/loops.php
<?php
/*******************************************************************************
License:
	Copyright 2005 Ryan Morehart
	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at
	
	http://www.apache.org/licenses/LICENSE-2.0
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
	
Filename: /includes/loops.php

Purpose: Holds loop processing functions for both retrieval and processing and
	creation/updating. Very closly linked /includes/pages.php

Processed parameters: (TYPE - NAME - DESCRIPTION - DEFAULT)
	None
*******************************************************************************/

function getLoopItems(&$connection, $pageName, $loopName = false, $renumber = false)
{
	// Ensure the page and loop names are valid
	if(!isValidPageName($connection, $pageName, true)
		|| ($loopName !== false && !isValidLoopName($connection, $loopName, false)))
	{
		return false;
	}

	// Create query
	$query = "SELECT * FROM page_loop_data WHERE page_name='"	. addslashes($pageName) . "'";
	if(is_string($loopName))
	{
		$query .= " and loop_name='$loopName'";
	}
	$query .= " ORDER BY page_name, loop_name, display_order;";
	
	// Get loop data
	$loopData = mysql_query($query, $connection);
	
	$loops = array();
	while(($item = mysql_fetch_array($loopData, MYSQL_ASSOC)) !== false)
	{
		if(!$renumber)
		{
			$loops[$item['loop_name']][$item['display_order']] = parsePageData($item['data']);
		}
		else
		{
			$loops[$item['loop_name']][] = parsePageData($item['data']);
		}
	}
	
	return $loops;
}

function createLoopInstance(&$connection, $pageName, $loopName, $dispNumber = false)
{
	// Ensure the data passed in is valid
	if(!isValidPageName($connection, $pageName, true)
		|| !isValidLoopName($connection, $loopName, false)
		|| ($dispNumber !== false && (!is_numeric($dispNumber) || $dispNumber < 0)))
	{
		return false;
	}
	
	if($dispNumber !== false)
	{
		$loops = getLoopItems($connection, $pageName, $loopName);
		if(isset($loops[$loopName]))
		{
			$loops = $loops[$loopName];
		}
		
		// Check if this display number is already used
		if(isset($loops[$dispNumber]) || !is_numeric($dispNumber))
		{
			return false;
		}
	}
	else
	{
		// Find the greatest used display number
		$largestDisplayOrder = mysql_query("SELECT MAX(display_order) FROM page_loop_data WHERE page_name='"
			. $pageName . "' and loop_name='" . $loopName . "';", $connection);
		$largestDisplayOrder = mysql_fetch_array($largestDisplayOrder, MYSQL_NUM);
		$largestDisplayOrder = $largestDisplayOrder[0];
		$dispNumber = $largestDisplayOrder + 1;
	}
	
	// Insert into table
	$success = mysql_query("INSERT INTO page_loop_data VALUES('$pageName', '$loopName', $dispNumber, '');", $connection);
	if($success === false)
	{
		return false;
	}
	
	// Just to make it pretty
	cleanLoopList($connection);
	
	return true;
}

function moveLoopInstance(&$connection, $pageName, $loopName, $dispNumber, $direction)
{
	// Ensure the page and loop names are valid
	if(!isValidPageName($connection, $pageName, true)
		|| !isValidLoopName($connection, $loopName, false))
	{
		return false;
	}
	
	// Determine which direction we want to move this loop instance
	// Note how we go the "wrong" way. This is because to move "up" from the user's point
	// of view, it has to have a lower display order
	if($direction > 0)
	{
		// Up
		// Find the number below this one
		$nextLowerNum = mysql_query("SELECT MAX(display_order) FROM page_loop_data WHERE page_name='"
			. $pageName . "' and loop_name='" . $loopName . "' and display_order < $dispNumber", $connection);
		$nextLowerNum = mysql_fetch_array($nextLowerNum, MYSQL_NUM);
		$nextLowerNum = $nextLowerNum[0];
		
		$swapWith = $nextLowerNum;
	}
	else
	{	
		// Down
		// Find the number above this one
		$nextHigherNum = mysql_query("SELECT MIN(display_order) FROM page_loop_data WHERE page_name='"
			. $pageName . "' and loop_name='" . $loopName . "' and display_order > $dispNumber", $connection);
		$nextHigherNum = mysql_fetch_array($nextHigherNum, MYSQL_NUM);
		$nextHigherNum = $nextHigherNum[0];
		
		$swapWith = $nextHigherNum;
	}
	
	// Check if this loop is already at the top/bottom of the list
	if(!is_numeric($swapWith))
	{
		// Report that the loop _was_ moved. It wasn't, but... :)
		return true;
	}
	
	// Save the loop who we are "moving"'s data
	$loop = mysql_query("SELECT * FROM page_loop_data WHERE page_name='"
		. $pageName . "' and loop_name='" . $loopName . "' and display_order="
		. $dispNumber . ";", $connection);
	$loop = mysql_fetch_array($loop, MYSQL_ASSOC);
	if(empty($loop))
	{
		return false;
	}
	
	// Delete the old version of it
	if(mysql_query("DELETE FROM page_loop_data WHERE page_name='"
		. $pageName . "' and loop_name='" . $loopName . "' and display_order="
		. $dispNumber . ";", $connection) === false)
	{
		return false;
	}
	
	// Move the loop we are swapping with
	if(mysql_query("UPDATE page_loop_data SET display_order=$dispNumber WHERE page_name='"
		. $pageName . "' and loop_name='" . $loopName . "' and display_order="
		. $swapWith . ";", $connection) === false)
	{
		return false;
	}
	
	// Put the loop back in with the new display order
	if(mysql_query("INSERT INTO page_loop_data VALUES('$pageName', '$loopName', $swapWith, '"
		. addslashes($loop['data']) . "');", $connection) === false)
	{
		return false;
	}
	
	// Just to make it pretty
	cleanLoopList($connection);
	
	return true;
}

function setLoopInstanceData(&$connection, $pageName, $loopName, $dispNumber, $data)
{
	// Ensure the page and loop names are valid
	if(!isValidPageName($connection, $pageName, true)
		|| !isValidLoopName($connection, $loopName, false))
	{
		return false;
	}
	
	// Update
	$success = mysql_query("UPDATE page_loop_data SET data='" . addslashes($data)
		. "' WHERE page_name='$pageName' and loop_name='$loopName' and display_order='$dispNumber';", $connection);
	if($success === false)
	{
		return false;
	}
	
	return true;
}

function deleteLoopInstance(&$connection, $pageName, $loopName, $dispNumber, $renumber = false)
{
	// Ensure the page and loop names are valid
	if(!isValidPageName($connection, $pageName, true)
		|| !isValidLoopName($connection, $loopName, false))
	{
		return false;
	}
	
	// Attempt to delete
	$success = mysql_query("DELETE FROM page_loop_data WHERE page_name='$pageName'"
		. " and loop_name='" . addslashes($loopName) . "'"
		. " and display_order=$dispNumber;", $connection);
	if($success === false)
	{
		return false;
	}
	
	// Fix the possible bad numbering which resulted
	if($renumber)
	{
		return cleanLoopList($connection, true);
	}
	else
	{
		return true;
	}
}

function cleanLoopList(&$connection, $renumber = false)
{
	// Are we supposed to compress the numbering?
	if($renumber)
	{
		// Get all loops from table
		$loops = mysql_query('SELECT * FROM page_loop_data ORDER BY page_name, loop_name, display_order;', $connection);
		if($loops === false)
		{
			return false;
		}
		
		// Go through each loop, renumbering as we go
		$currentItem = 1;
		$lastPageName = '';
		$lastLoopName = '';
		while(($loop = mysql_fetch_array($loops, MYSQL_ASSOC)) !== false)
		{
			if($lastPageName != $loop['page_name'] || $lastLoopName != $loop['loop_name'])
			{
				// Reset count
				$currentItem = 1;
				
				// Save new data
				$lastPageName = $loop['page_name'];
				$lastLoopName = $loop['loop_name'];
			}
			else
			{
				$currentItem++;
			}
		
			// Into the database it goes! :)
			mysql_query("UPDATE page_loop_data SET display_order=$currentItem WHERE page_name='"
				. $loop['page_name'] . "' AND loop_name='" . $loop['loop_name'] . "' and display_order="
				. $loop['display_order'] . ";", $connection);
		}
	}
	
	// Reorder table
	if(mysql_query("ALTER TABLE page_loop_data ORDER BY page_name, loop_name, display_order",
		$connection) === false)
	{
		return false;
	}
	
	return true;
}

function isValidLoopName(&$connection, $name, $isExistent = false)
{
	// Ensure it conains only valid characters
	//if(!ereg('^[_a-zA-Z0-9]+$', $name))
	//{
	//	return false;
	//}
	
	// Check if this name is already in the database (if we are supposed to)
	if($isExistent)
	{
		return loopExists($connection, $name);
	}
	else
	{
		return true;
	}
}

function loopExists(&$connection, $name)
{
	// See if you can get any loops with this name
	$success = mysql_query("SELECT * FROM page_loop_data WHERE loop_name='$name'");
	if(mysql_fetch_array($exists, MYSQL_ASSOC) === false)
	{
		return false;
	}
	else
	{
		return true;
	}
}
?>
Return current item: CMSmelborp