Location: PHPKode > projects > Katyshop > katyshop_ro_0.3.2.1/classes/logic/Category.php
<?php

/**
 * Product: Katyshop
 * @version 0.3.2.1
 * @author Catalin Hulea - hide@address.com
 * @copyright Copyright (C) 2007 Catalin Hulea
 * @license GNU General Public License version 3
 * 			You can find a copy of GNU GPL v3 at this path: /docs/LICENSE
 * @link https://sourceforge.net/projects/katyshop
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

class Category extends LogicObject
{
	var $id = 0;
	var $id_parent = 0;
	var $pos = 0;
	var $nest_level = 0;
	var $title = "";
	var $description = "";
	var $picture = "";
	var $date_created = "";
	var $active = 0;

	function Category($argId = 0, $argTitle = "")
	{
		parent::LogicObject();
		$this->date_created = date("Y-m-d H:i:s");
		$this->id = $argId;
		$this->title = $argTitle;
	}

	function getInstanceProps($myClass = "")
	{
		if(empty($myClass))
			$myClass = __CLASS__;
		return parent::getInstanceProps($myClass);
	}

	function validateCommonFields()
	{
		$errors = array();
		if(!Tools::validateInt($this->id))
			$errors[] = "Proprietatea ID al obiectului de tip Category trebuie sa fie intreg";
		if(!Tools::validateInt($this->id_parent))
			$errors[] = "Proprietatea id_parent a obiectului de tip Category trebuie sa fie intreg";
		if($this->id == $this->id_parent && $this->id > 0)
			$errors[] = "O categorie nu poate fi propriul parinte";
		if(!Tools::validateInt($this->pos) || $this->pos < 1)
			$errors[] = "Pozitia categoriei trebuie sa fie intreg pozitiv";
		if(strlen($this->title) < 3 || strlen($this->title) > 255)
			$errors[] = "Titlul trebuie sa aiba inte 3 si 255 caractere";
		if(!empty($this->picture) && !Tools::isImage($this->picture))
			$errors[] = "Fisierul uploadat nu este o imagine";

		$df = new DateFormat();
		if(!$df->readDateTime($this->date_created) || !$df->validate())
			$errors[] = "Data crearii categoriei nu este valida";

		if($this->active != 0 && $this->active != 1)
			$errors[] = "Proprietatea 'active' a obiectului de tip Category poate fi doar 0 sau 1";

		return $errors;
	}

	function validate()
	{
		$this->title = trim($this->title);
		$errors = $this->validateCommonFields();

		if(count($errors) == 0)
		{
			$db = Application::getDb();
			if($this->id > 0 && !$db->tbCategory->categoryExists($this->id))
				$errors[] = "Nu am putut gasi categoria care trebuie actualizata";
			if($this->id_parent > 0 && !$db->tbCategory->categoryExists($this->id_parent))
				$errors[] = "Nu am putut gasi categoria parinte";
			$bread = $db->tbCategory->getBreadcrumb($this->id_parent);
			for($i = 0; $i < count($bread); $i++)
			{
				if($bread[$i]->id == $this->id)
				{
					$errors[] = "Categoria nu poate fi mutata intr-o subcategorie de-a ei insasi";
					break;
				}
			}

			$titleCat = $db->tbCategory->getCategoryByTitle($this->title, $this->id_parent);
			if($titleCat->id > 0 && $titleCat->id != $this->id)
				$errors[] = "Deja exista o alta categorie cu acest titlu";

			$maxPos = $db->tbCategory->getMaxPos($this->id_parent);
			if($this->id > 0)
			{
				$old = $db->tbCategory->getRecordById($this->id);
				if($old->id_parent != $this->id_parent)
					$maxPos++;
			}
			else
			{
				$maxPos++;
			}
			if($maxPos < $this->pos)
				$errors[] = "Valoarea maxima pentru pozitie este $maxPos";
		}

		Application::appendErrors($errors);
		return (count($errors) == 0);
	}

	/**
	 * Return false if this category or any of it's parents is not active
	 */
	function canBeDisplayed()
	{
		$db = Application::getDb();
		$bread = $db->tbCategory->getBreadcrumb($this->id);
		for($i = 0; $i < count($bread); $i++)
		{
			if($bread[$i]->active != 1)
				return false;
		}
		return true;
	}

	function removeImages()
	{
		if(empty($this->picture))
			return ;
		@unlink(WEB_DIR . "/img/categories/{$this->picture}");
	}

}

?>
Return current item: Katyshop