Code Sample:
<?
include "../includes/constants.php";
class category
{
//vars
private $db;
private $smarty;
public function __construct($dbRef, $smartyRef)
{
//vars
$this->db = $dbRef;
$this->smarty = $smartyRef;
//if adding a category
if(isset($_POST['SubmitForm']))
{
$this->AddCategory($_POST['textCat'], $_POST['select'], $_POST['textDesc'],$_POST['selectLogin'], "new");
}
//see what we are doing here...
switch ($_GET['what'])
{
case "add";
$this->displayAddCategory(0);
$this->smarty->assign("site_page", "Add category");
break;
case "manage";
if(isset($_POST['StartEditDetails']))
{
$this->displayEditDetails($this->db->escape($_POST['hidden']));
}
elseif(isset($_POST['StartDelete']))
{
$this->displayConfirmDelete();
}
elseif(isset($_POST['ConfirmDelete']))
{
$this->proccessDelete();
$this->displayCategoryListings();
}
elseif(isset($_POST['EditDetails']))
{
$this->AddCategory($_POST['textCat'], $_POST['select'], $_POST['textDesc'],$_POST['selectLogin'], "edit");
}
else
{
$this->smarty->assign("site_page", "Manage Categories");
$this->displayCategoryListings();
}
break;
}
}
//delete categories and file's
public function proccessDelete()
{
//current category
$rec = $this->db->fetch("SELECT catSubID, catTitle FROM " . CAT_TABLE . " WHERE catID='" . $this->db->escape($_POST['hidden']) . "'");
$rows = $this->db->fetch_array("SELECT imageID, imageFileType FROM " . IMAGE_TABLE . " WHERE imageCatID='" . $rec['catSubID'] . "'");
//delete current category images
foreach($rows as $rec2)
{
unlink("../images/" . $rec2['imageID'] . '.' . $rec2['imageFileType']);
}
//get sub categories
$recs = $this->db->fetch("SELECT COUNT(catID) AS ammount FROM " . CAT_TABLE . " WHERE catSubID='" . $this->db->escape($_POST['hidden']) . "'");
//if sub categories
if($recs['ammount'] > 0)
{
//set some default values..
$cont = false;
$max = 1;
$i = 0;
$IDS[] = $this->db->escape($_POST['hidden']);
//loop threw all words
while($max > $i)
{
$rec = $this->db->fetch("SELECT *, COUNT(catID) AS amount FROM " . CAT_TABLE . " WHERE catSubID='" . $IDS[$i] . "'");
$msg .= $rec['catTitle'] . " Category and Images deleted...<br>";
if($rec['amount'] > 0)
{
$IDS[] = $rec['catID'];
$max++;
}
$i++;
}
//loop threw all cat ids to be deleted
for($i=0; $i<count($IDS);$i++)
{
//get all images associated with category being deleted
$rows = $this->db->fetch_array("SELECT imageID, imageFileType FROM " . IMAGE_TABLE . " WHERE imageCatID='" . $IDS[$i] . "'");
//cycle threw all image records
foreach($rows as $rec)
{
//if file exists
if(is_file("../images/" . $rec['imageID'] . '.' . $rec['imageFileType']))
{
//delete image file
unlink("../images/" . $rec['imageID'] . '.' . $rec['imageFileType']);
}
}
//delete records
$this->db->del("DELETE FROM " . CAT_TABLE . " WHERE catID='" . $IDS[$i] . "'");
$this->db->del("DELETE FROM " . IMAGE_TABLE . " WHERE imageCatID='" . $IDS[$i] . "'");
}
}
//first category.. delete from
$this->db->del("DELETE FROM " . IMAGE_TABLE . " WHERE imageCatID='" . $this->db->escape($_POST['hidden']) . "'");
$this->db->del("DELETE FROM " . CAT_TABLE . " WHERE catID='" . $this->db->escape($_POST['hidden']) . "'");
$this->smarty->assign("msg", $msg);
}
//display confirm delete buttton
public function displayConfirmDelete()
{
$this->smarty->assign("delete", 'y');
$this->smarty->assign("id", $_POST['hidden']);
}
//display details to edit
public function displayEditDetails($id)
{
//query
$rec = $this->db->fetch("SELECT catID, catSubID, catSubID, catTitle, catDesc, catReqLogin FROM " . CAT_TABLE . " WHERE catID='$id'");
//set template vars
$this->smarty->assign("edit", 'y');
$this->smarty->assign("id", $rec['catID']);
$this->smarty->assign("name", $rec['catTitle']);
$this->smarty->assign("desc", $rec['catDesc']);
$this->smarty->assign("login_" . $rec['catReqLogin'], "selected");
//list categories
$this->displayAddCategory($rec['catSubID']);
}
//attempt to add category
public function addCategory($cat_name, $select, $desc, $login_req, $action)
{
//query to check if category name exists for sub category if one selected..
$rec = $this->db->fetch("SELECT COUNT(*) AS ammount FROM " . CAT_TABLE . " WHERE catTitle='" . $this->db->escape($cat_name) . "' AND catSubID='" . $this->db->escape($select) . "'");
//setup data
$data = array("catTitle" => $cat_name, "catDesc" => $desc,"catReqLogin" => $login_req, "catSubID" => $select, "catDate" => time());
if($action == "edit")
{
//update record
$this->db->update(CAT_TABLE, $data, "catID='" . $_POST['hidden'] . "'");
$this->smarty->assign("msg", "<BR>Category updated...");
$this->displayCategoryListings();
}
//category doesn't exist, create it
if($rec['ammount'] == 0 && $action == "new")
{
//insert data
$this->db->insert(CAT_TABLE, $data);
$this->smarty->assign("msg", "<BR>Category created...");
}
elseif($action == "new")
{
$this->smarty->assign("msg", "<BR>Category name allready exists...");
}
}
//display categories for category add form
public function displayAddCategory($selected)
{
//query
$rows = $this->db->fetch_array("SELECT catID, catSubID, catTitle FROM " . CAT_TABLE . " ORDER BY catTitle");
$selects[0] = "None";
//loop through records
foreach($rows as $rec)
{
$selects[$rec['catID']] = $rec['catTitle'];
}
//set template var
$this->smarty->assign("selects", $selects);
$this->smarty->assign("selected", $selected);
}
//display all categories with some stats
public function displayCategoryListings()
{
$Data = array();
//query
$rows = $this->db->fetch_array("SELECT * FROM " . CAT_TABLE . " ORDER BY catTitle");
//loop through records
foreach($rows as $rec)
{
//set data for category in template
$Data[] = array('name' => $rec['catTitle'], 'login_req' => $rec['catReqLogin'], 'id' => $rec['catID'], 'downloads' => $row['ammount'], 'views' => $rec['catViews'], 'description' => $rec['catDesc']);
}
//assign template vars
$this->smarty->assign("looper", $Data);
$this->smarty->assign("list", 'y');
}
}
?>