Location: PHPKode > projects > Katyshop > katyshop_ro_0.3.2.1/classes/logic/Product.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 Product extends LogicObject
{
	var $id = 0;
	var $id_category = 0;
	var $id_manufacturer = 0;
	var $pos = 0;
	var $title = "";
	var $price = 0;
	var $measuring_unit = "";
	var $description = "";
	var $content = "";
	var $technical_details = "";
	var $picture = "";
	var $date_created = "";
	var $active = 0;

	var $manufacturer = "";

	function Product($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[] = "ID-ul produsului trebuie sa fie numar intreg";
		if(!Tools::validateInt($this->id_category))
			$errors[] = "Proprietatea id_category a obiectului de tip Product trebuie sa fie numar intreg";
		if(!Tools::validateFloat($this->price, true, true))
			$errors[] = "Pretul produsului trebuie sa fie numar real strict pozitiv";
		if(!Tools::validateInt($this->pos) || $this->pos < 1)
			$errors[] = "Pozitia produsului trebuie sa fie numar intreg strict pozitiv";
		if(strlen($this->title) < 3 || strlen($this->title) > 255)
			$errors[] = "Titlul trebuie sa aiba intre 3 si 255 caractere";
		if(!empty($this->picture) && !Tools::isImage($this->picture))
			$errors[] = "Fisierul uploadat nu este imagine";

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

		if($this->active != 0 && $this->active != 1)
			$errors[] = "Proprietatea 'active' a obiectului de tip Product poate lua numai valorile 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->tbProduct->productExists($this->id))
				$errors[] = "Nu a putut fi gasit produsul care trebuie actualizat";
			if($this->id_category > 0 && !$db->tbCategory->categoryExists($this->id_category))
				$errors[] = "Nu a putut fi gasita categoria parinte";
			if(!in_array($this->measuring_unit, Product::getPossibleMeasuringUnits()))
				$errors[] = "Va rugam sa alegeti unitatea de masura";

			$titleProd = $db->tbProduct->getProductByTitle($this->title, $this->id_category);
			if($titleProd->id > 0 && $titleProd->id != $this->id)
				$errors[] = "Deja exista un alt produs cu acest titlu";

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

			if($this->id_manufacturer > 0 && !$db->tbManufacturer->idExists($this->id_manufacturer))
				$errors[] = "Nu a putut fi gasit producatorul selectat pentru acest produs";
		}

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

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

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

	function getPossibleMeasuringUnits()
	{
		return array("pieces", "kg");
	}

	function validateQuantity($q, $measuringUnit)
	{
		switch ($measuringUnit)
		{
			case "buc":
				return Tools::validateInt($q, true, true);
				break;
			case "kg":
				return Tools::validateFloat($q, true, true);
				break;
			default:
				return false;
				break;
		}
	}


}

?>
Return current item: Katyshop