Location: PHPKode > projects > AlkalinePHP > forum_funcs.inc
<?

//**************** Objects *************************

//************ The Thread Object *********************
class thread {
	var $id;
	var $owner_id;
	var $title;
	var $content;
	var $date;
	//var $date_mod;
	var $parent_id;
	var $is_catagory;
	var $is_subcatagory;
	var $is_closed;
	var $last_post_id;
	var $allow_level;
	var $views;
	
	// class function for loading all thread data
	function load($id = 0) {
        global $main_settings;
        global $main_user;
        
        if ($id == 0) {
            return;
        }
        $table_name = "forum";

        $sql = "
		SELECT *
		FROM $table_name
		WHERE id = $id
		ORDER BY id
    	";
    	
        $result = @mysql_query($sql, $main_settings->connection) or die("Couldn't execute query.");
        $row = mysql_fetch_array($result);
        
        $this->id = $id;
        $this->owner_id = $row['owner_id'];
        $this->title = $row['title'];
        $this->content = $row['content'];
        $this->date = $row['date'];
        
        $this->date = $main_user->user_time($this->date);
        
        $this->date = date($main_settings->date_format . " " . $main_settings->time_format, $this->date);
        
        $this->parent_id = $row['parent_id'];
        $this->is_catagory = $row['is_catagory'];
        $this->is_subcatagory = $row['is_subcatagory'];
        $this->is_closed = $row['is_closed'];
        $this->last_post_id = $row['last_post_id'];
        $this->allow_level = $row['allow_level'];
        $this->views = $row['views'];
    }
    
    function last_post() {
        if ( ($this->last_post_id != NULL) && ($this->last_post_id != 0) ) {
            $last_post = new thread;
            $last_post->load($this->last_post_id);
        
            echo "<i><a href=\"thread.php?thread_id=" . $this->id . "#" . $last_post->id . "\">$last_post->title</a></i> on $last_post->date by ";
            print_user_name($last_post->owner_id);
        } else {
            echo "<i>No posts yet</i>\n";
        }
    }
    
    function display($first = true) {
        global $main_settings;
        global $main_user;
        
        // Threads
        if ( ($this->is_subcatagory != 1) && ($this->is_catagory != 1)) {
           echo "<a name=\"$this->id\"></a>";
          echo "<p class=\"grouphead\">";
          if ($first == true) {
		      echo "<b>Subject: $this->title</b>";
		  } else {
              echo "<b>Subject:</b> $this->title";
          }
		  echo "</p>";
		  echo "<p class=\"group\">";
		  
		  //***** side **************
		  echo "<table class=\"main\" width=\"100%\"><tr><td width=\"20%\" class=\"border\" valign=\"top\">";
		  echo "Posted by: ";
		  print_user_name( $this->owner_id );
		  echo "on $this->date";
		  echo "<br>\n";
		  //echo "$cat_owner_id<br>";
		  show_user_posts( $this->owner_id );
		  
		  //ICON
		  show_user_icon( $this->owner_id );
		  echo "</td><td width=\"80%\" class=\"border\">";
		      //******** content *********
		      $this->content = filter($this->content,2);
	       	echo "$this->content";
    		echo "<br><br><table class=\"bot\">";

		  echo "<td align=\"left\" valign=\"bottom\">";
	       //echo "[icon] [icon] [icon]";
    	   echo "</td>";

		  echo "<td align=\"right\">";
		  if ($main_user->level >= 7) {
			echo " [<a href=\"forumsubmit.php?action=editthread&thread_id=$this->id\">edit</a>]";
			echo " [<a href=\"forumsubmit.php?action=delete&parent_id=$this->id\">delete</a>]";
		  }
		  echo "</td></table>";
		  echo "</td></tr></table>";
		  echo "</p>";
		  echo "<br>";
		  
		  if ($first == true) {
		      $sql = "
		      SELECT id
		      FROM forum
		      WHERE parent_id = $this->id
		      ORDER BY id ASC
	           ";
	           //echo "$sql<br>";
	           $result = @mysql_query($sql, $main_settings->connection) or die("Couldn't execute query.");
	           while ($row = mysql_fetch_array($result)) {
                    $temp = new thread;
                    $temp->load($row['id']);
                    $temp->display(false);
                }
            }
        }
        
        // Catagories... non-threads...
        if ( ($this->is_subcatagory == 1) || ($this->is_catagory == 1)) {
		  echo "<p class=\"grouphead\">";
		  echo "<b>$this->title</b>";
		  echo "</p>";
		  
		  echo "<p class=\"group\">";
          echo "<table class=\"list\">";
          
          if ( ($this->is_catagory == 1) ) {
			echo "<th>Subject</th>";
	      } else {
			echo "<th>Thread</th>";
		  }
		  echo "<th>Owner</th>";
		  if ( ($this->is_catagory == 1) ) {
			echo "<th>Threads</th>";
		  }
		  echo "<th>Posts</th>";

          if ( !($this->is_catagory == 1) ) {
		      echo "<th>Views</th>";
          }
		
		  echo "<th>Last Post</th>
		  <th>Options</th>";

          $sql = "
		  SELECT id
		  FROM forum
            WHERE parent_id = $this->id
		  ORDER BY date DESC
	       ";
	       $result = @mysql_query($sql, $main_settings->connection) or die("Couldn't execute query.");
	       while ($row = mysql_fetch_array($result)) {
            $thread_id = $row['id'];
            $sub_thread = new thread;
            $sub_thread->load($thread_id);
            //SUBJECT field
            echo "<tr><td class=\"border\">\n";
            echo "<a href=\"thread.php?thread_id=$sub_thread->id\">$sub_thread->title</a></td>\n";
            echo "<td class=\"border\" align=\"center\">\n";
            
            //OWNER field
			print_user_name($sub_thread->owner_id);
			echo "</td>";
			
			//POSTS / THREADS field
			echo "<td class=\"border\" align=\"center\">\n";
			if ( ($this->is_catagory == 1) ) {
			     $temp = get_total_posts( $sub_thread->id );
			     echo $temp;
            } else {
                $temp = get_all_posts( $sub_thread->id );
                echo $temp;
            }
			echo "</td>";

            //VIEWS / POSTS field
			echo "<td class=\"border\" align=\"center\">\n";
			if ( ($this->is_catagory == 1) ) {
			     $temp = get_all_posts( $sub_thread->id );
			     echo $temp;
            } else {
                echo "$sub_thread->views";
            }
			echo "</td>";
			
			//LAST POST field
			echo "<td class=\"border\" align=\"center\">\n";
			$sub_thread->last_post();
			echo "</td>";

            //OPTIONS field
            echo "<td class=\"border\" align=\"center\">\n";
			echo "[<a href=\"forumsubmit.php?action=editcat&cat_id=$sub_thread->id\">edit</a>]\n";
			echo " [<a href=\"forumsubmit.php?action=delete&parent_id=$sub_thread->id\">delete</a>]</td></tr>\n";
		    
        }
        echo "</table>";
        echo "<br><br><table class=\"bot\"><td>
			<div name=\"comments\" align=\"right\">\n";

		echo " [<a href=\"forumsubmit.php?action=newthread&owner_id=$this->id\">Post</a> a new thread]\n";
        echo "</div></td></table>";
		//botcontent();
		echo "</p>";
		echo "<br>";
	   }
}
}

//**************** functions *************************


// Display the most recent posts
function show_recent_posts($limit, $short = false) {
    global $main_settings;
    global $main_user;
    
    $sql = "
		SELECT id
		FROM forum
        WHERE is_catagory = 0 AND is_subcatagory = 0
		ORDER BY date DESC
		LIMIT $limit
	";

	$result = @mysql_query($sql, $main_settings->connection) or die("Couldn't execute query.");

    if (!$short) {
        echo "<p class=\"grouphead\"><b>Recent Posts</b></p>\n";
        echo "<p class=\"group\">";
    }
	while ($row = mysql_fetch_array($result)) {
        $recent_id = $row['id'];
        $recent_thread = new thread;
        $recent_thread->load($recent_id);
        //$new_content = filter($recent_thread->content,0, 40);
        
        echo "<a href=\"thread.php?thread_id=$recent_thread->parent_id#$recent_id\">$recent_thread->title</a> ";
        if (!$short) {
            echo "at $recent_thread->date\n";
        }
        echo " by ";
        print_user_name($recent_thread->owner_id);
        echo "<br>";
        //echo "<i>--$new_content</i><br>\n";
	}
	if (!$short) {
	   echo "</p>";
  }
}

// find and display all applicable catagories/subs
function show_cats($cat_id = 0) {
    global $main_settings;
    global $main_user;
    
    $cats = array();
    $cat_index = 0;

    $table_name = "forum";

    if ($cat_id == 0) {
        $sql = "
		SELECT *
		FROM $table_name
		WHERE is_catagory = 1
		ORDER BY id DESC
	   ";
        $result = @mysql_query($sql, $main_settings->connection) or die("Couldn't execute query.");

        while ($row = mysql_fetch_array($result)) {
            $cat_id = $row['id'];
            $cats[$cat_index] = new thread;
            $cats[$cat_index]->load($cat_id);
            // Display catagory
            echo "<br><p class=\"grouphead\">\n";
	        echo "<b>" . $cats[$cat_index]->title . "</b> [<a href=\"forumsubmit.php?action=editcat&cat_id=" . $cats[$cat_index]->id . "\" class=\"header\">edit</a>]\n";
	        echo "[<a href=\"forumsubmit.php?action=delete&parent_id=" . $cats[$cat_index]->id . "\" class=\"header\">delete</a>]<br>\n";
	        echo "<i>" . $cats[$cat_index]->content . "</i>\n";
	        echo "</p>\n";
            
            $cat_index++;
            show_cats($cats[$cat_index - 1]->id);
            echo "<br><br><table class=\"bot\"><td>
		    <div name=\"comments\" align=\"right\">\n";
	        if ($main_user->level >= 7) {
		      echo " [<a href=\"forumsubmit.php?action=newsubcat&cat_id=" . $cat_id . "\">Create</a> new sub-catagory]\n";
         }
	        echo "</div></td></table>\n";
	        echo "</p><br>\n";
        }
    } else { // These are subcatagories
        $sql = "
		SELECT *
		FROM $table_name
		WHERE is_subcatagory = 1
		AND parent_id = $cat_id
		ORDER BY id DESC
	   ";
	   
	   $result = @mysql_query($sql, $main_settings->connection) or die("Couldn't execute query.");

       echo "<p class=\"group\">\n";

	   echo "<table class=\"list\">
	   <th>Subject</th>
	   <th>Owner</th>
	   <th>Threads</th>
	   <th>Posts</th>
	   <th>Last Post</th>
	   <th>Options</th>\n";
	
        while ($row = mysql_fetch_array($result)) {
            $cat_id = $row['id'];
            $cats[$cat_index] = new thread;
            $cats[$cat_index]->load($cat_id);
            //echo "--" . $cats[$cat_index]->title . "<br>";
            echo "<tr><td class=\"border\">\n";
            echo "<a href=\"thread.php?thread_id=" . $cats[$cat_index]->id . "\">" . $cats[$cat_index]->title . "</a><br><i>" . $cats[$cat_index]->content . "</i></td>\n";
		    echo "<td class=\"border\" align=\"center\">\n";
		    print_user_name($cats[$cat_index]->owner_id);
		    echo "</td>\n";
		    echo "<td class=\"border\" align=\"center\">\n";
		    echo get_total_posts($cats[$cat_index]->id);
		    echo "</td>\n";
		    echo "<td class=\"border\" align=\"center\">\n";
		    echo get_all_posts( $cats[$cat_index]->id );
		    echo "</td>\n";
		    //************** LAST THREAD ***************
		    echo "<td class=\"border\" align=\"center\">\n";
            $cats[$cat_index]->last_post();
			echo "</td>";
			//******************************************
			echo "<td class=\"border\" align=\"center\">\n";
		    echo "[<a href=\"forumsubmit.php?action=editcat&cat_id=" . $cats[$cat_index]->id . "\">edit</a>] \n";
		    echo "[<a href=\"forumsubmit.php?action=delete&parent_id=" . $cats[$cat_index]->id . "\">delete</a>]</td></tr>\n";
		    $cat_index++;
        }
         echo "</table>";
    }
}

//Displays a drop-down list of catagories and subcatagories for the user to jump to
function show_jump_cats() {
	global $main_settings;
	
	echo "<select name=\"field_type[]\">";
	
	$table_name = "forum";

	$sql = "
		SELECT *
		FROM $table_name
		WHERE is_catagory = 1
	";
	
	$result = mysql_query($sql, $main_settings->connection) or die("Couldn't retrieve root info.");
	while ($row = mysql_fetch_array($result)) {
		$cat_id = $row['id'];
		$cat_title = $row['title'];
		echo "<option value=\"$cat_id\" onClick=\"document.location='thread.php?thread_id=$cat_id'\">$cat_title</option>\n";
		$sql = "
			SELECT *
			FROM $table_name
			WHERE is_subcatagory = 1
			AND parent_id = $cat_id
		";
	
		$result2 = mysql_query($sql, $main_settings->connection) or die("Couldn't retrieve root info.");
		while ($row2 = mysql_fetch_array($result2)) {
			$subcat_id = $row2['id'];
			$subcat_title = $row2['title'];
			echo "<option value=\"$subcat_id\" onClick=\"document.location='thread.php?thread_id=$subcat_id'\">--$subcat_title</option>\n";
		}
		
	}
	echo "</select>";
}

function kill_children($parent_id, $post_count, $level) {
	global $main_settings;

	$table_name = "forum";

	$sql = "
		SELECT *
		FROM $table_name
		WHERE parent_id = $parent_id
	";
	
	$result = mysql_query($sql, $main_settings->connection) or die("Couldn't retrieve root info.");
	while ($row = mysql_fetch_array($result)) {
		$post_count++;
		$child_id = $row['id'];
		$post_count = kill_children($child_id, $post_count, $level+1);
	}
	for ($i = 0; $i < $level; $i++) {
		echo "-";
	}
	echo "Killing post id: $parent_id ...";

	$sql = "DELETE FROM $table_name WHERE id = \"$parent_id\"";
	$result = @mysql_query($sql, $main_settings->connection) or die("Couldn't execute query.");
	echo "done<br>";

	return $post_count;
}

function update_parents_lastpost( $root_id, $new_id ) {
	global $main_settings;
	$temp = date("m/d/Y g:i a");
	$thread_date = strtotime($temp);
	$table_name = "forum";
	
	//echo "root: $root_id | new: $new_id<br>";

	$root_thread = new thread;
	$root_thread->load($root_id);

//	echo "$parent_id<br>";

	if ($root_thread->parent_id != 0) {
		$sql = "UPDATE $table_name SET last_post_id= '$new_id'
		 WHERE id=$root_thread->parent_id
";
		$result = @mysql_query($sql, $main_settings->connection) or die("Couldn't execute query.");

		update_parents_lastpost($root_thread->parent_id, $new_id);
	}

}

function get_location( $current_id ) {
	global $main_settings;
	$table_name = "forum";

	$sql = "
		SELECT *
		FROM $table_name
		WHERE id = $current_id
	";
	//echo "$sql<br>";
	$result = mysql_query($sql, $main_settings->connection) or die("Couldn't retrieve root info.");
	$row = mysql_fetch_array($result);

	$parent_id = $row['parent_id'];
	$current_title = $row['title'];

	if ($parent_id == 0) {
		echo "-><a href=\"thread.php?thread_id=$current_id\">$current_title</a>\n";
	} else {
		get_location( $parent_id );
		echo "-><a href=\"thread.php?thread_id=$current_id\">$current_title</a>\n";
	}

}

function get_total_posts( $parent_id ) {
	global $main_settings;
	$table_name = "forum";

	$sql = "
		SELECT COUNT(*)
		FROM $table_name
		WHERE parent_id = $parent_id
	";
	//echo "$sql<br>";
	$result = mysql_query($sql, $main_settings->connection) or die("Couldn't retrieve root info.");
	$row = mysql_fetch_array($result);
	
	$count =$row['COUNT(*)'];
	
	return $count;
}

function show_user_posts( $user_id ) {
	global $main_settings;
	$table_name = "forum";

	$sql = "
		SELECT COUNT(*)
		FROM $table_name
		WHERE owner_id = $user_id 
		AND is_catagory = 0
		AND is_subcatagory = 0
	";
	//echo "$sql<br>";
	$result = mysql_query($sql, $main_settings->connection) or die("Couldn't retrieve root info.");
	$row = mysql_fetch_array($result);
	
	$count =$row['COUNT(*)'];
	return $count;
}

function get_all_posts( $parent_id ) {
	global $main_settings;
	$table_name = "forum";

	$total = 0;
	$sql = "
		SELECT *
		FROM $table_name
		WHERE parent_id = $parent_id
	";
	//echo "$sql<br>";
	$result = mysql_query($sql, $main_settings->connection) or die("Couldn't retrieve root info.");
	while ($row = mysql_fetch_array($result)) {
		$post_id = $row['id'];
		$total = $total + get_total_posts($post_id) +1;
	}

	//echo "$total";
	return $total;
}

//******* unfinished -- does not work **************
function get_thread_info( $thread_id ) {
	global $main_settings;
	$table_name = "forum";

	$sql = "
		SELECT *
		FROM $table_name
		WHERE id = $thread_id
	";

	$result = mysql_query($sql, $main_settings->connection) or die("Couldn't retrieve root info.");
	$row = mysql_fetch_array($result);

	$poopsnot = $row['title'];
}

?>
Return current item: AlkalinePHP