<?
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
* Class		:	productDocument
* Version :	1.0
* Date    :	16 October 2009
* Author  :	Darwish A. Khalil
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/

// Class Definition
class productDocument
{
	var $DocumentID;			// Auto increment (Primary Key)
	var $Title;						// Document Title
	var $Description;			// Small description about document
	var $FileName;				// Document's origin file name
	var $PublishedDate;		// Document's published date
	var $ShowDate = 1;		// Used to show/hide published date
	var $DocumentTypeID;	// Document's type
	var $ProductID;				// Document's related product

	/*
	 * Class constructor
	 */
  function productDocument($DocumentID = -1)
  {
  	if ((!empty($DocumentID)) && ($DocumentID != -1))
  	{
  		$this->DocumentID = $DocumentID;
			$this->getInfo();
		}
	}

	/*
	 * Retrieves document information
	 */
	function getInfo()
	{
		$query = "
							Select * From product_document
					 	  Where DocumentID = $this->DocumentID
					 	 ";
		// echo $query;
		$result = mysql_query($query);
		$row = mysql_fetch_object($result);

    // Set class data members
    $this->Title 					= $row->Title;
    $this->Description 		= $row->Description;
    $this->FileName 			= $row->FileName;
    $this->PublishedDate 	= $row->PublishedDate;
    $this->ShowDate 			= $row->ShowDate;
    $this->DocumentTypeID	= $row->DocumentTypeID;
    $this->ProductID 			= $row->ProductID;
  }

	/*
   * Retrieves the count of documents available according to the given search criteria
   */
	function getTotal($Criteria = '')
	{
		global $language;

		$query  = "Select COUNT(*) From product_document ";
		$query .= "Where Deleted = 0 And Enabled = 1 And Language = '$language' And ProductID = $this->ProductID ";

		// Keyword ----------------------------------------------------------------
		if($Criteria['Keyword'] != '')
		{
			$query .= "And ( ";
			$query .= "(Title Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(FileName Like '%" . $Criteria['Keyword'] . "%') ";
			$query .= ") ";
		}
		// Document Type ----------------------------------------------------------
		if ($Criteria['DocumentTypeID'] != -1) {
			$query .= "And (DocumentTypeID = " . $Criteria['DocumentTypeID'] . ") ";
		}

		// echo $query;
   	$result = mysql_result(mysql_query($query), 0, 0);

    return $result;
	}

	/*
	 * Retrieve a list of document regarding a certain search criteria
	 */
	function getList($Criteria = '', $SortField = 'DataEntryDate', $SortType = 'ASC', $Start = -1, $Num = -1)
	{
		global $language;

		$query  =
							"
								Select DocumentID, Title, Description, FileName, Extension, PublishedDate, ShowDate
								From product_document
								Where Deleted = 0 And Enabled = 1 And Language = '$language' And ProductID = $this->ProductID ";

		// Keyword ----------------------------------------------------------------
		if($Criteria['Keyword'] != '')
		{
			$query .= "And ( ";
			$query .= "(Title Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(FileName Like '%" . $Criteria['Keyword'] . "%') ";
			$query .= ") ";
		}
		// Document Type ----------------------------------------------------------
		if ($Criteria['DocumentTypeID'] != -1) {
			$query .= "And (DocumentTypeID = " . $Criteria['DocumentTypeID'] . ") ";
		}

		// Order By
		$query .= "Order By $SortField $SortType ";

		// Limit Results
		if (($Start!=-1) && ($Num!=-1)) {
     $query .= "Limit $Start,$Num";
    }

		// echo($query);
		$result = mysql_query($query);
		return $result;
	}

	/*
	 * Returns the file name of a certain document
	 */
	function getFileName()
	{
		$query  = "Select FileName From product_document Where DocumentID = $this->DocumentID";
		$result = mysql_query($query);
		if (mysql_num_rows($result)>0) {
			$row = mysql_fetch_object($result);
			return $row->FileName;
		}
		return '';
	}
}
?>
