class CatDB { var $file; function __construct ($file = "data/CatList.xml") { $this->file = $file; } /** * Reads XML data from a local file and returns it as a PHP Array * @param onlyActive - If to include only active categories; Defaults to true */ function read($onlyActive = true) { $dom = new DomDocument(); $dom->load($this->file); $ebayCatList = array(); $categories = $dom->getElementsByTagName("Category"); foreach($categories as $cat) { if ($cat->getElementsByTagName("Active")->item(0)->nodeValue || !$onlyActive) // Only take active items $ebayCatList[] = array( "Name" => $cat->getElementsByTagName("Name")->item(0)->nodeValue, "ParentID" => $cat->getElementsByTagName("ParentID")->item(0)->nodeValue, "Active" => $cat->getElementsByTagName("Active")->item(0)->nodeValue, "ID" => $cat->getElementsByTagName("ID")->item(0)->nodeValue ); // Load our sub categories if ($cat->getElementsByTagName("Active")->item(0)->nodeValue || !$onlyActive) { $subcategories = $cat->getElementsByTagName("Subcategory"); $ebayCatList[count($ebayCatList)-1]["Subcategories"] = array(); if (count($subcategories)) { foreach ($subcategories as $subcat) { if ($subcat->getElementsByTagName("Active")->item(0)->nodeValue || !$onlyActive) $ebayCatList[count($ebayCatList)-1]["Subcategories"][] = array( "Name" => $subcat->getElementsByTagName("Name")->item(0)->nodeValue, "ParentID" => $subcat->getElementsByTagName("ParentID")->item(0)->nodeValue, "Active" => $subcat->getElementsByTagName("Active")->item(0)->nodeValue, "ID" => $subcat->getElementsByTagName("ID")->item(0)->nodeValue ); } } } } // Return our array return $ebayCatList; } /** * Saves the data in the provided array to the categories list file. * @param $data - The PHP Array to read the category list from. */ function store($data) { $dom = new DomDocument(); // Create our primary element $new_element = $dom->createElement("Categories"); $domRoot = $dom->appendChild($new_element); // Populate our Category list foreach ($data as $Key=>$Value) { $new_element = $dom->createElement("Category"); $domCategory = $domRoot->appendChild($new_element); $new_element = $dom->createElement("ID", $Value["ID"]); $domCategory->appendChild($new_element); $new_element = $dom->createElement("Name", htmlspecialchars($Value["Name"])); $domCategory->appendChild($new_element); $new_element = $dom->createElement("ParentID", $Value["ParentID"]); $domCategory->appendChild($new_element); $new_element = $dom->createElement("Active", $Value["Active"]); $domCategory->appendChild($new_element); $new_element = $dom->createElement("Subcategories"); $domSubcategories = $domCategory->appendChild($new_element); // Populate our Subcategories, if exists if (isset($Value["Subcategories"])) { foreach ($Value["Subcategories"] as $subKey=>$subValue) { $new_element = $dom->createElement("Subcategory"); $domSubcategory = $domSubcategories->appendChild($new_element); $new_element = $dom->createElement("ID", $subValue["ID"]); $domSubcategory->appendChild($new_element); $new_element = $dom->createElement("Name", htmlspecialchars($subValue["Name"])); $domSubcategory->appendChild($new_element); $new_element = $dom->createElement("ParentID", $subValue["ParentID"]); $domSubcategory->appendChild($new_element); $new_element = $dom->createElement("Active", $subValue["Active"]); $domSubcategory->appendChild($new_element); } } } // Now write our file return $dom->save($this->file); } /** * Converts text into url safe text in our own format * @param $string - the string to be converted * @return $converted - the converted string */ function toStub ($string) { return strtolower(preg_replace("/([ ])+([a-z\d])/i", "-\\2", preg_replace("/[^a-z \d]/i", "", str_replace("&", "and", $string)))); } function shortenText($string) { if (strlen($string) > 23) return substr($string, 0, 20)."..."; else return $string; } /** * Create the data needed to append a Custom Field. * * @param array &$catList The list to have the custom field added to * @param string $fieldName The name of the new field */ function createCustom(&$catList, $fieldName, $searchTerm) { $catList[] = array( "Name" => $fieldName, "ParentID" => $searchTerm, "Active" => 1, "ID" => "custom" ); } } ?>