Mysql($faqDB); $this->data = $data; } // Is this instance valid? function isValid() { if ($this->data["active"] <> "" && $this->data["name"] <> "") { return true; } else { return false; } } // Access to member variables function getID() { return $this->data["id"]; } function setID($i) { $this->data["id"] = $i; } function setActive($g) { $this->data["active"] = $g; } function getActive() { return $this->data["active"]; } function setName($p) { $this->data["name"] = $p; } function getName() { return $this->data["name"]; } function setRank($r) { $this->data["rank"] = $r; } function getRank() { return $this->data["rank"]; } function setLastModDate($d) { $this->data["last_mod_date"] = $d; } function getLastModDate() { return $this->data["last_mod_date"]; } function setLastModUser($u) { $this->data["last_mod_user"] = $u; } function getLastModUser() { return $this->data["last_mod_user"]; } // Create a category (only if the category does not already exist) function create() { global $theUser; global $catTable; if (!$this->isValid()) return; // Ensure that this category does not already exist $name = $this->getName(); $q = "SELECT id FROM $catTable WHERE name = '$name'"; $ret = $this->do_query($q, 1); if ($ret[0]->isValid()) return; $this->setLastModDate(date("Y-m-d")); $this->setLastModUser($theUser->getID()); $q = "INSERT INTO $catTable VALUES (1, NULL, " . ($this->getActive() ? "1" : "0") . ", '$name', " . $this->getRank() . ", '" . $this->getLastModDate() . "', '" . $this->getLastModUser() . "')"; $this->do_query($q, 0); // Update the global "last updated" date $sys = new System(); $sys->update(); } // Update a category (only if the category already exists) function update() { global $theUser; global $catTable; if (!$this->isValid()) return; // Ensure that this category already exists $q = "SELECT id FROM $catTable WHERE name = '" . $this->getName() . "'"; $ret = $this->do_query($q, 1); if (!$ret[0]->isValid()) return; // Set all the relevant info $this->setLastModDate(date("Y-m-d")); $this->setLastModUser($theUser->getID()); $name = str_replace("'", "@", $this->getName()); $q = "UPDATE $catTable SET active = " . ($this->getActive() ? "1" : "0") . ", " . "name = '$name', " . "rank = " . $this->getRank() . ", " . "last_mod_date = '" . $this->getLastModDate() . "', " . "last_mod_user = '" . $this->getLastModUser() . "' " . "WHERE id = " . $this->getID(); $this->do_query($q, 0); // Update the global "last updated" date $sys = new System(); $sys->update(); } } // Function to sort categories function cat_sort($a, $b) { if ($a->getRank() != $b->getRank()) return ($a->getRank() < $b->getRank()) ? -1 : 1; else if ($a->getName() != $b->getName()) return ($a->getName() < $b->getName()) ? -1 : 1; else return 0; } // Function to do a generic query and return an array of Categories function doCatQuery($q, $want_rows) { global $faqDB; $m = new Mysql($faqDB); $cats = $m->do_query($q, $want_rows); if ($cats[0]->isValid()) { for ($i = 0; $i < sizeof($cats); $i++) $ret[] = new Category($cats[$i]->getData()); usort($ret, cat_sort); } else $ret[] = new Category(""); return $ret; } // Function to return an array of all categories in objects function getAllCats() { global $catTable; return doCatQuery("SELECT * FROM $catTable", 1); } // Find a category by ID number function getCatByID($id) { global $catTable; $cats = doCatQuery("SELECT * FROM $catTable WHERE id = $id", 1); return $cats[0]; } // Delete a category by ID number function deleteCatByID($id) { global $catTable; $cats = doCatQuery("DELETE FROM $catTable WHERE id = $id", 0); // Must also delete all category number cross references deleteCatXrefByCID($id); return $cats[0]; } }