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

function find_value($where) {
	global $TABLE_ATTRIBUTE_VALUE;
	return db_find_fetch($TABLE_ATTRIBUTE_VALUE, $where);
}

function find_value_by_id($valueId) {
	return find_value("valueId = " . mysql_real_escape_string($valueId));
}

function find_values_by_attributeId($attributeId, $orderby = "value") {
	return find_values("attributeId = " . mysql_real_escape_string($attributeId), $orderby);
}

function find_image_attribute_value($imageId, $attributeId, $valueId) {
	global $TABLE_IMAGE_ATTRIBUTE_VALUE;
	return db_find_fetch($TABLE_IMAGE_ATTRIBUTE_VALUE, 
		"imageId = " . mysql_real_escape_string($imageId)
		. " and attributeId = " . mysql_real_escape_string($attributeId)
		. " and valueId = " . mysql_real_escape_string($valueId)
	);
}

function delete_image_attribute_values($where) {
	global $TABLE_IMAGE_ATTRIBUTE_VALUE;
	$tableName = full_table_name($TABLE_IMAGE_ATTRIBUTE_VALUE);
	return db_execute("delete from {$tableName} where " . $where);
}

function delete_image_attribute_value($imageId, $attributeId, $valueId) {
	$where = "imageId = " . mysql_real_escape_string($imageId)
		. " and attributeId = " . mysql_real_escape_string($attributeId)
		. " and valueId = " . mysql_real_escape_string($valueId);
	delete_image_attribute_values($where);
}

function delete_image_attribute_values_by_attributeId($attributeId) {
	delete_image_attribute_values("attributeId = " . mysql_real_escape_string($attributeId));
}

function delete_image_attribute_values_by_valueId($valueId) {
	delete_image_attribute_values("valueId = " . mysql_real_escape_string($valueId));
}

function delete_image_attribute_values_by_imageId($imageId) {
	delete_image_attribute_values("imageId = " . mysql_real_escape_string($imageId));
}

function find_image_attribute_values_by_imageId($imageId, $orderby = "c.sequenceNum, b.value") {
	global $TABLE_IMAGE_ATTRIBUTE_VALUE;
	global $TABLE_ATTRIBUTE;
	global $TABLE_ATTRIBUTE_VALUE;
	
	$imgattrvalTable = full_table_name($TABLE_IMAGE_ATTRIBUTE_VALUE);
	$attrvalTable = full_table_name($TABLE_ATTRIBUTE_VALUE);
	$attrTable = full_table_name($TABLE_ATTRIBUTE);
	
	$stmt = "select a.imageId, a.attributeId, a.valueId, c.name, b.value"
		. " from " . $imgattrvalTable . " a" 
		. " inner join " . $attrvalTable . " b on (a.attributeId = b.attributeId and a.valueId = b.valueId) "
		. " inner join " . $attrTable . " c on (a.attributeId = c.attributeId) "
		. "where a.imageId = " . mysql_real_escape_string($imageId)
		. " order by " . $orderby;
		
	return db_execute($stmt);
}

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

function delete_value_by_id($valueId) {
	
	// Delete image attribute values
	delete_image_attribute_values_by_valueId($valueId);
	
	// Delete value
	delete_value("valueId = " . mysql_real_escape_string($valueId));

}

function delete_value_by_attribute_id($attributeId) {
	
	// Delete image attribute values
	delete_image_attribute_values_by_attributeId($attributeId);
	
	// Delete value
	delete_value("attributeId = " . mysql_real_escape_string($attributeId));

}

function validate_value($assoc) {
	global $MAX_ATTRIBUTE_VALUE_LENGTH;
	if(validate_length($assoc, 'value', $MAX_ATTRIBUTE_VALUE_LENGTH) === FALSE) return FALSE;
	if(validate_notempty($assoc, 'value') === FALSE) return FALSE;
	return TRUE; 
}

function validate_image_attribute_value($assoc) {
	if(validate_notempty($assoc, 'attributeId', "attribute") === FALSE) return FALSE;
	if(validate_notempty($assoc, 'valueId', "value") === FALSE) return FALSE;
	return TRUE; 
}

function insert_value($assoc) {
	global $TABLE_ATTRIBUTE_VALUE;
	if(!validate_value($assoc)) return FALSE;
	$literalValues = Array("addedDt" => "now()");
	return db_insert($TABLE_ATTRIBUTE_VALUE, $assoc, $literalValues);
}

function insert_image_attribute_value($assoc) {
	global $TABLE_IMAGE_ATTRIBUTE_VALUE;
	if(!validate_image_attribute_value($assoc)) return FALSE;
	if(find_image_attribute_value($assoc['imageId'], $assoc['attributeId'], $assoc['valueId']) !== FALSE) {
		global $dbs_error;
		$dbs_error = "This image has already been assigned the specified attribute and value.";
		return FALSE;
	}
	$literalValues = Array("addedDt" => "now()");
	return db_insert($TABLE_IMAGE_ATTRIBUTE_VALUE, $assoc, $literalValues);
}

function update_value($assoc, $valueId) {
	global $TABLE_ATTRIBUTE_VALUE;
	if(!validate_value($assoc)) return FALSE;
	return db_update($TABLE_ATTRIBUTE_VALUE, $assoc, "valueId = " . mysql_real_escape_string($valueId));
}

function values_list_by_attributeId($attributeId) {
		$valRes = find_values_by_attributeId($attributeId, "value");
		if($valRes === FALSE) {
			global $dbs_error;
			die('Unable to find values: ' . htmlspecialchars_default($dbs_error));
		}
		$vals = "";
		while( ($assoc = db_fetch($valRes)) !== FALSE ) {
			if($vals !== "") $vals .= ", ";
			$vals .= $assoc['value']; 
		} 
		echo htmlspecialchars_default($vals);
}

function value_table_by_attributeId($attributeId) {
	$res = find_values_by_attributeId($attributeId, "value");
	if($res === FALSE) {
		global $dbs_error;
		die('Could not find values: ' . htmlspecialchars_default($dbs_error));
	}
	value_table($res);
}

function value_table($res) {
	echo("<table>\n");
	echo("<tr>\n");
	if($_SESSION['admin'] == "TRUE") {
		echo("<th colspan=\"1\">Options</th>\n");
	} 
	echo("<th>Value</th>\n");
	echo("</tr>\n");
	if($res === FALSE) {
		global $dbs_error;
		die('Unable to find values: ' . htmlspecialchars_default($dbs_error));
	}
	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 value?  All assignments associated with this value will be permanently deleted!')">
<p>
<input type="hidden" name="action" value="deletevalue" />
<input type="hidden" name="valueId" value="<?php echo(htmlspecialchars_default($assoc['valueId'])) ?>" />
<input type="hidden" name="attributeId" value="<?php echo(htmlspecialchars_default($assoc['attributeId'])) ?>" />
<input type="submit" value="Delete" class="submit" />
</p>
</form>
</td>

<?php
		}

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

function image_attribute_value_table_by_imageId($imageId) {
	$res = find_image_attribute_values_by_imageId($imageId);
	if($res === FALSE) {
		global $dbs_error;
		die('Could not find values: ' . htmlspecialchars_default($dbs_error));
	}
	image_attribute_value_table($res);
}

function image_attribute_value_list_by_imageId($imageId) {
	$res = find_image_attribute_values_by_imageId($imageId);
	if($res === FALSE) {
		global $dbs_error;
		die('Could not find values: ' . htmlspecialchars_default($dbs_error));
	}
	image_attribute_value_list($res);
}

function image_attribute_value_list($res) {
	$attrs = array();
	$names = array();
	if($res === FALSE) {
		global $dbs_error;
		die('Unable to find values: ' . htmlspecialchars_default($dbs_error));
	}
	while( ($assoc = db_fetch($res)) !== FALSE ) {
		$attributeId = $assoc['attributeId'];
		if(!isset($attrs[$attributeId])) $attrs[$attributeId] = array();
		$attrs[$attributeId][] = $assoc['value'];
		if(!isset($names[$attributeId])) $names[$attributeId] = $assoc['name'];
	}
	
	foreach($attrs as $attributeId => $values) {
		echo(htmlspecialchars_default($names[$attributeId]) . ": ");
		$first = TRUE;
		foreach($values as $value) {
			echo( ($first?"":", ") . htmlspecialchars_default($value));
			$first = FALSE;
		}
		echo("<br />\n");
	}
	
	
}

function image_attribute_value_table($res) {
	echo("<table>\n");
	echo("<tr>\n");
	if($_SESSION['admin'] == "TRUE") {
		echo("<th colspan=\"1\">Options</th>\n");
	}
	echo("<th>Attribute</th>\n"); 
	echo("<th>Value</th>\n");
	echo("</tr>\n");
	if($res === FALSE) {
		global $dbs_error;
		die('Unable to find values: ' . htmlspecialchars_default($dbs_error));
	}
	while( ($assoc = db_fetch($res)) !== FALSE ) {
		echo("<tr>\n");
		if($_SESSION['admin'] == "TRUE") {
?>
<td class="buttonCol">
<form method="post" action="index.php">
<input type="hidden" name="imageId" value="<?php echo(htmlspecialchars_default($assoc['imageId'])) ?>" />
<input type="hidden" name="valueId" value="<?php echo(htmlspecialchars_default($assoc['valueId'])) ?>" />
<input type="hidden" name="attributeId" value="<?php echo(htmlspecialchars_default($assoc['attributeId'])) ?>" />
<?php image_nav_input(NULL, "deleteimageattributevalue"); ?>
<input type="submit" value="Delete" class="submit" />
</form>
</td>

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


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