Location: PHPKode > scripts > DRBImageGallery - Free PHP Image Gallery > drbimagegallery/includes/attributes.php
<?php
/****************************************************************************
 * DRBImageGallery
 * http://www.dbscripts.net/imagegallery/
 * 
 * Copyright (c) 2007-2009 Don Barnes 
 ****************************************************************************/
 
function find_attributes($where = NULL, $orderby = NULL, $offset = NULL, $count = NULL) {
	global $TABLE_ATTRIBUTE;
	return db_find($TABLE_ATTRIBUTE, $where, $orderby, $offset, $count);
}

function find_attribute($where) {
	global $TABLE_ATTRIBUTE;
	return db_find_fetch($TABLE_ATTRIBUTE, $where);
}

function find_attribute_by_id($attributeId) {
	return find_attribute("attributeId = " . mysql_real_escape_string($attributeId));
}

function find_attribute_by_sequenceNum($sequenceNum) {
	return find_attribute("sequenceNum = " . mysql_real_escape_string($sequenceNum));
}

function delete_attribute($where) {
	global $TABLE_ATTRIBUTE;
	$tablename = full_table_name($TABLE_ATTRIBUTE);
	
	return db_execute("delete from " . $tablename . " where " . $where);
}

function delete_attribute_by_id($attributeId) {
	
	// Delete values
	delete_value_by_attribute_id($attributeId);
	
	// Find attribute and get current sequence number
	$res = find_attribute_by_id($attributeId);
	$sequenceNum = $res['sequenceNum']; 

	// Delete attribute
	if(delete_attribute("attributeId = " . mysql_real_escape_string($attributeId)) !== FALSE) {
		
		// Move up sequence number on remaining attributes
		$res = find_attributes("sequenceNum > $sequenceNum");
		if($res === FALSE) {
			global $dbs_error;
			die('Unable to find attributes: ' . htmlspecialchars_default($dbs_error));
		}
		while( ($assoc = db_fetch($res)) !== FALSE ) {
			update_attribute(Array("sequenceNum" => $assoc['sequenceNum'] - 1), $assoc['attributeId']);
		}
		
	}
}

function validate_attribute($assoc) {
	global $MAX_ATTRIBUTE_NAME_LENGTH;
	if(validate_length($assoc, 'name', $MAX_ATTRIBUTE_NAME_LENGTH) === FALSE) return FALSE;
	if(validate_notempty($assoc, 'name') === FALSE) return FALSE;
	return TRUE; 
}

function insert_attribute($assoc) {
	global $TABLE_ATTRIBUTE;
	if(!validate_attribute($assoc)) return FALSE;
	$literalValues = Array("addedDt" => "now()", "sequenceNum" => next_sequence_number());
	return db_insert($TABLE_ATTRIBUTE, $assoc, $literalValues);
}

function update_attribute($assoc, $attributeId) {
	global $TABLE_ATTRIBUTE;
	if(!validate_attribute($assoc)) return FALSE;
	if(empty($attributeId)) {
		global $dbs_error;
		$dbs_error = "An attribute ID was not provided.";
		return FALSE;
	}
	return db_update($TABLE_ATTRIBUTE, $assoc, "attributeId = " . mysql_real_escape_string($attributeId));
}

function move_attribute($attributeId, $direction) {
	if($direction === "up") {
		$res = find_attribute_by_id($attributeId);
		$lastSequenceNum = $res['sequenceNum']; 
		if($lastSequenceNum == 1) {
			return FALSE;
		}
		$res = find_attribute_by_sequenceNum($lastSequenceNum - 1);
		if($res !== FALSE) {
			update_attribute(Array("sequenceNum" => $lastSequenceNum), $res['attributeId']);
		}
		update_attribute(Array("sequenceNum" => $lastSequenceNum - 1), $attributeId);
	}
	if($direction === "down") {
		$res = find_attribute_by_id($attributeId);
		$lastSequenceNum = $res['sequenceNum']; 
		$res = find_attribute_by_sequenceNum($lastSequenceNum + 1);
		if($res !== FALSE) {
			update_attribute(Array("sequenceNum" => $lastSequenceNum), $res['attributeId']);
			update_attribute(Array("sequenceNum" => $lastSequenceNum + 1), $attributeId);
		}
	}
}


function next_sequence_number() {
	global $TABLE_ATTRIBUTE;
	$res = db_execute_fetch("select max(sequenceNum) as maxSequenceNum from " . full_table_name($TABLE_ATTRIBUTE));
	if($res === FALSE) return 0;
	return $res['maxSequenceNum'] + 1;
}

function attribute_table($where = NULL, $orderby = NULL, $offset = NULL, $count = NULL) {
	
	$res = find_attributes(null, "sequenceNum asc", null, null);
	if($res === FALSE) {
		global $dbs_error;
		die('Could not find attributes: ' . htmlspecialchars_default($dbs_error));
	}
	
	echo("<table>\n");
	echo("<tr>\n");
	if($_SESSION['admin'] == "TRUE") {
		echo("<th colspan=\"4\">Options</th>\n");
	} 
	echo("<th>Name</th>\n");
	echo("<th>Values</th>\n");
	echo("</tr>\n");
	while( ($assoc = db_fetch($res)) !== FALSE ) {
		echo("<tr>\n");
		if($_SESSION['admin'] == "TRUE") {
?>
<td class="buttonCol">
<form method="post" action="index.php" onsubmit="return confirm('Are you sure you want to delete this attribute?  All assignments associated with this attribute will be permanently deleted!')">
<p>
<input type="hidden" name="action" value="deleteattribute" />
<input type="hidden" name="attributeId" value="<?php echo(htmlspecialchars_default($assoc['attributeId'])) ?>" />
<input type="submit" value="Delete" class="submit" />
</p>
</form>
</td>

<td class="buttonCol">
<form method="post" action="index.php">
<p>
<input type="hidden" name="action" value="showattribute" />
<input type="hidden" name="attributeId" value="<?php echo(htmlspecialchars_default($assoc['attributeId'])) ?>" />
<input type="submit" value="Edit" class="submit" />
</p>
</form>
</td>

<td class="buttonCol">
<form method="post" action="index.php">
<p>
<input type="hidden" name="action" value="moveattribute" />
<input type="hidden" name="direction" value="up" />
<input type="hidden" name="attributeId" value="<?php echo(htmlspecialchars_default($assoc['attributeId'])) ?>" />
<input type="submit" value="Up" class="submit" />
</p>
</form>
</td>

<td class="buttonCol">
<form method="post" action="index.php">
<p>
<input type="hidden" name="action" value="moveattribute" />
<input type="hidden" name="direction" value="down" />
<input type="hidden" name="attributeId" value="<?php echo(htmlspecialchars_default($assoc['attributeId'])) ?>" />
<input type="submit" value="Down" class="submit" />
</p>
</form>
</td>

<?php
		}
		echo("<td class=\"nameCol\">" . htmlspecialchars_default($assoc['name']) . "</td>\n");
		echo("<td>\n");
		values_list_by_attributeId($assoc['attributeId']);
		echo("</td>\n");
		
		echo("\n</tr>\n");
	}
	echo("</table>\n");

}

?>
Return current item: DRBImageGallery - Free PHP Image Gallery