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

// Class Definition
class productUpdate
{
	var $ReleaseID;				// Auto increment (Primary Key)
	var $FileName;				// Release's file name
	var $VersionNumber;		// Release's version number
	var $PublishedDate;		// Release's published date
	var $DownloadSize;		// Download Size
	var $OperatingSystem;	// Operating System
	var $Description;			// Brief Description
	var $ChangesOverview;	// Changes Overview
	var $ProductID;				// Release's related product id
	var $ProductName;			// Release's related product name

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

	/*
	 * Retrieves release information
	 */
	function getInfo()
	{
		$query = "
							Select u.*, p.Title ProductName From product_update u
							Inner Join product p On u.ProductID = p.ProductID
					 	  Where ReleaseID = $this->ReleaseID
					 	 ";
		// echo $query;
		$result = mysql_query($query);
		$row = mysql_fetch_object($result);

    // Set class data members
    $this->FileName 				= $row->FileName;
    $this->VersionNumber 		= $row->VersionNumber;
    $this->PublishedDate 	  = $row->PublishedDate;
    $this->DownloadSize 		= $row->DownloadSize;
    $this->OperatingSystem  = $row->OperatingSystem;
    $this->Description  		= stripslashes($row->Description);
    $this->ChangesOverview  = stripslashes($row->ChangesOverview);
    $this->ProductID 				= $row->ProductID;
    $this->ProductName 			= $row->ProductName;
  }

  /*
	 * Private utility method
	 */
	function populateFromRowObject($row)
	{
		// Set class data members
    $this->FileName 				= $row->FileName;
    $this->VersionNumber 		= $row->VersionNumber;
    $this->PublishedDate 	  = $row->PublishedDate;
    $this->DownloadSize 		= $row->DownloadSize;
    $this->OperatingSystem  = $row->OperatingSystem;
    $this->Description  		= stripslashes($row->Description);
    $this->ChangesOverview  = stripslashes($row->ChangesOverview);
    $this->ProductID 				= $row->ProductID;
    $this->ProductName 			= $row->ProductName;
	}

	/*
	 * Populates the current product release by providing the file key
	 */
	function populateByFileKey($FileKey)
	{
		$query = "
							Select u.*, p.Title ProductName From product_update u
							Inner Join product p On u.ProductID = p.ProductID
					 	  Where u.FileKey = '$FileKey'
					 	 ";
		// exit($query);
		$result = mysql_query($query);
		if (mysql_num_rows($result) > 0)
		{
			$row = mysql_fetch_object($result);
			$this->populateFromRowObject($row);
			return true;
		}
		return false;
	}

	/*
   * Retrieves the count of releases available according to the given search criteria for a certain product
   */
	function getTotal($Criteria)
	{
		global $language;

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

		// Keyword ----------------------------------------------------------------
		if($Criteria['Keyword'] != '')
		{
			$query .= "And ( ";
			$query .= "(FileName Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(VersionNumber Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(OperatingSystem Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(Description Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(ChangesOverview Like '%" . $Criteria['Keyword'] . "%') ";
			$query .= ") ";
		}

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

    return $result;
	}

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

		$query  =
							"
								Select ReleaseID, FileName, VersionNumber, Description, PublishedDate
								From product_update
								Where Deleted = 0 And Enabled = 1 And Language = '$language' And ProductID = $this->ProductID ";

		// Keyword ----------------------------------------------------------------
		if($Criteria['Keyword'] != '')
		{
			$query .= "And ( ";
			$query .= "(FileName Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(VersionNumber Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(OperatingSystem Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(Description Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(ChangesOverview Like '%" . $Criteria['Keyword'] . "%') ";
			$query .= ") ";
		}

		// 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;
	}

	/*
	 * Checks if a certain update/release exists or not
	 */
  function isExist()
  {
  	global $language;

    $query =
    				"
    					Select ReleaseID From product_update
    					Where Enabled = 1 And Deleted = 0 And Language = '$language'
    					And ReleaseID = $this->ReleaseID And ProductID = $this->ProductID
    				";
    $result = mysql_query($query);
    if (mysql_num_rows($result)>0) {
			return true;
		}
		return false;
	}

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

	/*
	 * Returns all distinct Operating System
	 */
	function getOperatingSystemList()
	{
		global $language;

		$query  =
							"
								Select Distinct OperatingSystem From product_update
								Where (OperatingSystem Is Not Null) And (OperatingSystem <> '')
								And Language = '$language'
								Order By OperatingSystem ASC
							";
  	$result = mysql_query($query);
  	return $result;
	}
}
?>