Location: PHPKode > projects > Informium > class/topic-class.php
<?

//
// Copyright (c) 2002, Cameron McKay
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions 
// are met:
//
// * Redistributions of source code must retain the above copyright 
//   notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright 
//   notice, this list of conditions and the following disclaimer in the 
//   documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

//
// Informium -- Advanced News Script
//
// Topic Class (topic-class.php)
//
// Author: Cameron McKay
// Note:   Class that allows for the manipulation of topics.
//

class topic
{
	// Constructor function.
	function topic()
	{
		// Does nothing.
	}

	//
	// Function: add ( $topic_name, $image_id )
	//
	// Purpose:  Adds an topic to the Informium database.
	//
	// Arguments:
	//   $topic_name -> The name of the topic.
	//   $image_id   -> The image associated with the topic.
	//	
	// Returns:  The new topic_id, or -1 for empty field.
	//

	function add ($topic_name, $image_id)
	{
		// Call the insert() function to insert the topic.
		return $this->insert(0, $topic_name, $image_id);
	}

	//
	// Function: edit ( $topic_id, $topic_name, $image_id )
	//
	// Purpose:  Edits a topic in the Informium database.
	//
	// Arguments:
	//   $topic_id   -> The topic's topic_id number.
	//   $topic_name -> The name of the topic.
	//   $image_id   -> The image associated with the topic.
	//
	// Returns:  The topic_id, or -1 for empty field.
	//

	function edit ($topic_id, $topic_name, $image_id)
	{
		// Call the insert() function to update the topic.
		return $this->insert($topic_id, $topic_name, $image_id);
	}

	//
	// Function: delete ( $topic_id )
	//
	// Purpose:  Deletes a topic in the Informium database.
	//           All articles containing this topic association are reset to topic_id 1.
	//           
	// Arguments:
	//   $topic_id -> The topic's topic_id number.
	//
	// Returns:  Nothing.
	//

	function delete ($topic_id)
	{
		// Import CONF.
		global $CONF;

		// Import the MYSQL class, if needed.
		require_once("$CONF[local_path]/class/mysql-class.php");

		// Create a new MYSQL object.
		$db = new mysql();

		// Connect to the DB.
		$db->pconnect();

		// Prepare and execute the query (change all associated topic_ids to 1).
		$query = "UPDATE news SET topic_id='1' where topic_id='$topic_id'";
		$db->query($query);

		// Prepare and execute the query (delete the topic).
		$query = "DELETE FROM topics WHERE topic_id='$topic_id'";
		$db->query($query);


	}

	//
	// Function: insert ( $topic_id, $topic_name, $image_id )
	//
	// Purpose:  Adds or edits a topic in the Informium database.
	//
	// Arguments:
	//   $topic_id   -> The topic's topic_id number.
	//   $topic_name -> The name of the topic.
	//   $image_id   -> The image associated with the topic.
	//
	// Returns:  The new topic_id, or -1 for empty field.
	//
	
	function insert  ($topic_id, $topic_name, $image_id)
	{
		// Import CONF;
		global $CONF;

		// Import MYSQL class, if needed.
		require_once("$CONF[local_path]/class/mysql-class.php");
 
		// Make new MYSQL object.
		$db = new mysql();

		// Check if the string contains data.
		if(strlen($topic_name) < 1) {

			// Return the 'empty field' error.
			return -1;

		}
		
		// Prepare the topic_name.
		$topic_name = addslashes($topic_name);

		// Check if we have a topic_id that's greater than 0.  If we do, then
		// we're merely updating a topic.
		if ($topic_id > 0) {

			// Get the original create_date (since UPDATE automatically changes the timestamp...)
			$create_date = $this->info($topic_id, 'create_date');

			// Prepare the update.
			$query = "UPDATE topics 
					SET topic_name='$topic_name',
					image_id='$image_id',
					create_date='$create_date' 
					WHERE topic_id='$topic_id'"; 

		// Otherwise we're adding a new one.
		} else { 

			// Prepare the addition.
			$query  = "INSERT INTO topics VALUES('', '$topic_name', '$image_id', NOW())";

		}

		// Connect to the DB.
		$db->pconnect();

		// Execute the query.
		$db->query($query);

		// Fetch the topic_id for return, if needed.
		if ($topic_id < 1) {
			$topic_id = $db->insert_id();

		}

		// Return the values that were added.
		return $topic_id;
	}

	//
	// Function: info ( $topic_id, $type )
	//
	// Purpose:  Fetches certain or all information about a particular topic.
	//
	// Arguments:
	//   $topic_id   -> The topic_id of the topic whose information will be retrieved.
	//	 $type       -> Can be any of the values written below or blank.
	//
	// Returns:
	//   topic_id    -> The topic's topic_id number.
	//   topic_name  -> The name of the topic.
	//   image_id    -> The image associated with the topic.
	//   create_date -> The topic's creation date.
	//

	function info ()
	{
		// Import CONF.
		global $CONF, $INF;

		// Import MYSQL class, if needed.
		require_once("$CONF[local_path]/class/mysql-class.php");

		// Make a new MYSQL object.
		$db = new mysql();

		// Connect to the database to check the access.
		$db->pconnect();

		// If we have no arguments, then get them all.
		if (func_num_args() < 2) {

			$topic_id = func_get_arg(0);
			$query    = "SELECT * FROM topics WHERE topic_id='$topic_id'";
			$result   = $db->query($query);
			$return   = $db->fetch_array($result);

		} else {

			$topic_id = func_get_arg(0);
			$field    = func_get_arg(1);
			$query    = "SELECT ($field) FROM topics WHERE topic_id='$topic_id'";
			$result   = $db->query($query);
			$return   = $db->result($result);

		}

		// Free the result.
		$db->free($result);

		// Return what they want.
		return $return;
	}

	//
	// Function: update ( $topic_id, $field, $value )
	//
	// Purpose:  Changes the setting of a field listed below for the given topic_id.
	//
	// Arguments:
	//   $field -> The field to change.
	//             * topic_name
	//             * image_id
	//             * create_date
	//   $value -> The value to change the field to.
	//
	// Returns:  TRUE.
	//

	function update ($topic_id, $field, $value)
	{
		// Import CONF.
		global $CONF;

		// Import MYSQL class, if needed.
		require_once("$CONF[local_path]/class/mysql-class.php");

		// Make a new MYSQL object.
		$db = new mysql();

		// Connect to the database to check the access.
		$db->pconnect();

		// Check if we're dealing with create_date.
		// If we are, then we need a special query.
		if (!strcasecmp($field, 'create_date')) {
			// Prepare the query.
			$query = "UPDATE topics SET $field='$value' WHERE topic_id='$topic_id'";

		// Otherwise proceed as normal.
		} else {
			// Get the original create_date...
			$create_date = $this->info($topic_id, 'create_date');

			// Prepare the query.
			$query = "UPDATE topics SET $field='$value', create_date='$create_date' WHERE topic_id='$topic_id'";

		}

		// Execute the query.
		$result = $db->query($query);

		return TRUE;
	}
		
	//
	// Function: form ( [$topic_id] )
	//
	// Purpose:  Creates a form used for adding and editing
	//           topic_id.
	//
	// Arguments:
	//   $topic_id -> If topic_id is provided, then use those values.
	//
	// Returns:  Nothing.
	//

	function form()
	{
		// Import CONF.
		global $CONF;

		// Import IMAGE, SYSTEM and XHTML classes, if needed.
		require_once("$CONF[local_path]/class/image-class.php");
		require_once("$CONF[local_path]/class/system-class.php");
		require_once("$CONF[local_path]/class/xhtml-class.php");	

		// Make new IMAGE, SYSTEM and XHTML objects.
		$image  = new image();
		$system = new system();
		$xhtml  = new xhtml();

		// Retrieve the image_id listing.
		$image_list = $image->i_list('image_id', 'topic');

		// Check if we're editing.
		if (func_num_args() > 0) {

			// If we're editing, define the type.
			$type = 'edit';

			// Get the topic_id.
			$topic_id = func_get_arg(0);

			// Fetch details about that topic.
			$topic = $this->info($topic_id);

			// Remove slashes from the topic_name.
			$topic[topic_name] = stripslashes($topic[topic_name]);

			// Prepare date information.
			$topic[create_date]  = $system->date_format($topic_id, 'create_date', 'topics');

		// Otherwise we're adding.
		} else {

			// If we're adding, define the type.
			$type = 'add';

			$now = $system->date_format(0, 'now', 'topics');

			$topic[create_date] = $now;
			$topic[modify_date] = $now;

		}

		// Start a new table.
		$xhtml->table_start('normal', $CONF[table_size]);

		// Escape PHP.
		?>
<br />
<form action='topic.php?exec=<? echo $type; ?>&topic_id=<? echo $topic_id; ?>' method='post'>

<table class='normal' width='100%' cellpadding='5' cellspacing='0' border='0' align='center'>
<tr>
<td>

Topic Name:<br />
<input type='text' name='topic_name' value="<? echo $topic[topic_name]; ?>" size='40' /><br />
<br />

Topic Image:<br />
<select name='image_id'>
<?

// Cycle through image_id list.
for ($i = 0; $i < count($image_list); ++$i)
{
	// Make a temporary image_id variable and reference it the image_id.
	$tmp_image_id   = $image_list[$i];

	// Make a temporary username variable and assign it the image_desc.
	$tmp_image_desc = $image->info($tmp_image_id, 'image_desc');
	
	// Check if it's selected.
	if (!strcmp($topic[image_id], $tmp_image_id)) {
		$att_selected = "selected='selected'";

	} else {
		$att_selected = "";

	}

	// Print the HTML.
	echo "<option value='$tmp_image_id' $att_selected>$tmp_image_desc</option>\n";
}
	
?>
</select>
<br />
<br />

<table class='normal' width='100%' cellpadding='5' cellspacing='0' border='0' align='center'>
<tr>
<td>Creation Date:</td><td><b><? echo $topic[create_date]; ?></b></td>
</tr>
<tr>
<td>Current Date:</td><td><b><? echo $system->date_format(0, 'now', 'topics'); ?></b></td>
</tr>
</table>
<br />

<input type='submit' value='<? echo ucfirst($type); ?> Topic'> <input type='reset' value='Reset'>

</td>
</tr>
</table>

</form>
		<?
		// Re-enter PHP.

		// End the table.
		$xhtml->table_end();

	}

	//
	// Function: dropdown ( $type )
	//
	// Purpose:  Displays a dropdown menu of topics so the user may choose one.
	//
	// Arguments:
	//   $type  -> Type of dropdown (i.e. edit or delete).
	//
	// Returns:  Nothing.
	//
	
	function dropdown ($type)
	{
		// Import CONF;
		global $CONF;

		// Import MYSQL and XHTML classes, if needed.
		require_once("$CONF[local_path]/class/mysql-class.php");
		require_once("$CONF[local_path]/class/xhtml-class.php");

		// Create MYSQL and XHTML objects.
		$db     = new mysql();
		$xhtml  = new xhtml();

		// Connect to the DB.
		$db->pconnect();

		// Retrieve the topics listing.
		$query  = "SELECT * FROM topics ORDER BY topic_name";
		$result = $db->query($query);

		// Make the argument string for the form.
		if (!strcmp($type, 'edit')) {
			$argument = 'form=edit';

		// Otherwise we're deleting.
		} else {
			$argument = 'exec=delete';

		}
		// Start the table.
		$xhtml->table_start('normal', $CONF[table_size]);

		// Start the form.
		echo "<br />\n";
		echo "<form action='topic.php?$argument' method='post'>\n";
		echo "<select name='topic_id'>\n";

		// Check if there are results.
		if ($db->num_rows($result)) {

			// Display the topic list.
			while ($list = $db->fetch_array($result))
			{	
				// Strip the slashes from the 
				$list[topic_name] = stripslashes($list[topic_name]);

				// Print the information.
				echo "<option value='$list[topic_id]'>$list[topic_name]</option>\n";
			}

		// Otherwise say there are not.
		} else {
			echo "<option value='%123%'>(No topics Exist)</option>\n";

		}

		// Prepare string.
		$type = ucfirst($type);

		// End the form and list.
		echo "</select><br />\n";
		echo "<br />\n";
		echo "<input type='submit' value='$type topic'>\n";
		echo "</form>\n";

		// End the table.
		$xhtml->table_end();

		// Free the result.
		$db->free($result);
	}

	//
	// Function: i_list ( $field )
	//
	// Purpose:  Retrieves a list array of any given field name.
	//
	// Arguments:
	//   $field -> Can be any of the values written below.
	//
	// Returns:  An array with one of the following list of values.
	//   topic_name  -> The topic's topic_name.
	//   image_id    -> The topic's image_id.
	//   create_date -> The topic's creation date.
	//

	function i_list ($field)
	{
		// Import CONF.
		global $CONF;

		// Import MYSQL class, if needed.
		require_once("$CONF[local_path]/class/mysql-class.php");

		// Make a new MYSQL object.
		$db = new mysql();

		// Connect to the database to check the access.
		$db->pconnect();

		// Prepare the query.
		$query  = "SELECT ($field) FROM topics ORDER BY topic_name";
		$result = $db->query($query);

		// Cycle through the results, making them into a list.
		while ($list = $db->fetch_array($result))
			$return[] = $list[$field];

		// Free the result.
		$db->free($result);

		// Return the answer.
		return $return;
	}

	//
	// Function: check ( $topic_id )
	//
	// Purpose:  Checks if the topic_id exists.
	//
	// Arguments:
	//   $topic_id -> The topic's topic_id.
	//
	// Returns:  1 if the topic_id exists, 0 if it does not.
	//

	function check ($topic_id)
	{
		// Import CONF.
		global $CONF;

		// Import MYSQL class, if needed.
		require_once("$CONF[local_path]/class/mysql-class.php");

		// Make a new MYSQL object.
		$db = new mysql();
		
		// Connect to the DB.
		$db->pconnect();

		// Prepare and execute the query.
		$query  = "SELECT * FROM topics WHERE topic_id='$topic_id'";
		$result = $db->query($query);

		// Check if we got a result.
		$return = $db->num_rows($result);

		// Free the result.
		$db->free($result);

		// Return the answer.
		return $return;
	}

	//
	// Function: to_name ( $topic_id )
	//
	// Purpose:  Translates a topic_id to a topic_name.
	//
	// Returns:  topic_name for given topic_id.
	//

	function to_name($topic_id)
	{
		// Import CONF.
		global $CONF;

		// Import MYSQL class, if needed.
		require_once("$CONF[local_path]/class/mysql-class.php");

		// Make a new MYSQL object.
		$db = new mysql();

		// Connect to the database.
		$db->pconnect();

		// Execute query.
		$query  = "SELECT topic_name FROM topics WHERE topic_id='$topic_id'";
		$result = $db->query($query);
		$return = $db->result($result);

		// Free result.
		$db->free($result);

		// Return username.
		return $return;
	}

	//
	// Function: to_id ($topic_name OR $topic_id)
	//
	// Purpose:  Translates a topic_name (or topic_id) to a topic_id.
	//
	// Return:   The topic_id for given topic_name.
	//

	function to_id ( $topicdata )
	{
		// Import CONF.
		global $CONF;

		// If it's an integer than we assume it's already a topic_id.
		if (is_numeric($topicdata)) { 
		
			// So we return the value untouched.
			return $topicdata;

		} else {

			// Rename it for aesthetics.
			$topic_name = $topicdata;

		}

		// Import MYSQL class, if needed.
		require_once("$CONF[local_path]/class/mysql-class.php");

		// Make a new MYSQL object.
		$db = new mysql();

		// Connect to the database.
		$db->pconnect();

		// Execute query.
		$query  = "SELECT topic_id FROM topics WHERE topic_name='$topic_name'";
		$result = $db->query($query);
		$return = $db->result($result);

		// Free result.
		$db->free($result);

		// Return user_id.
		return $return;
	}

}

?>
Return current item: Informium