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

// Class Definition
class search
{

	/*
	 * Class constructor
	 */
  function search()
  {
  	// ------------
	}

	/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
	*                         Products Info Search Functions
	*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
	/*
   * Retrieves the count of products
   */
	function getTotalProducts($Criteria)
	{
		global $language;

		$query =
						"
							Select COUNT(*) From product
							Where Enabled = 1 And Deleted = 0 And Language = '$language' ";

		// Keyword ---------------------------------------------------------------------
		if($Criteria['Keyword'] != '')
		{
			$query .= "And ( ";
			$query .= "(Title Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(Description Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(GenericInfo Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(CatalogFileName Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(Roadmap Like '%" . $Criteria['Keyword'] . "%') ";
			$query .= ") ";
		}
	  // echo $query;
   	$result = mysql_result(mysql_query($query), 0, 0);
    return $result;
	}

	/*
   * Retrieves a list of all products
   */
	function getProductsList($Criteria = '', $SortField = "Title", $SortType = "ASC", $Start = -1, $Num = -1)
	{
		global $language;

		$query  =
							"
								Select ProductID From product
								Where Enabled = 1 And Deleted = 0 And Language = '$language' ";

	// Keyword ---------------------------------------------------------------------
		if($Criteria['Keyword'] != '')
		{
			$query .= "And ( ";
			$query .= "(Title Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(Description Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(GenericInfo Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(CatalogFileName Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(Roadmap 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;
	}

	/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
	*                         Product FAQs Search Functions
	*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
	/*
   * Retrieves the count of questions available according to the given search criteria
   */
	function getTotalFAQs($Criteria)
	{
		global $language;
		global $customer;

		$query =
						"
							Select COUNT(Distinct q.FAQID) From product_faq q
							Inner Join customer_license cl On q.ProductID = cl.ProductID
							Where q.Deleted = 0 And q.Enabled = 1 And q.Language = '$language'
							And cl.CustomerID = $customer->CustomerID And cl.Status = 'Activated' ";

		// Keyword ----------------------------------------------------------------
		if($Criteria['Keyword'] != '')
		{
			$query .= "And ( ";
			$query .= "(q.Question Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(q.Answer Like '%" . $Criteria['Keyword'] . "%') ";
			$query .= ") ";
		}

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

    return $result;
	}

	/*
	 * Retrieve a list of questions regarding a certain search criteria
	 */
	function getFAQsList($Criteria = '', $SortField = "q.DisplayOrder", $SortType = "ASC", $Start = -1, $Num = -1)
	{
		global $language;
		global $customer;

		$query =
					  "
					  	Select Distinct q.FAQID From product_faq q
					  	Inner Join customer_license cl On q.ProductID = cl.ProductID
							Where q.Deleted = 0 And q.Enabled = 1 And q.Language = '$language'
							And cl.CustomerID = $customer->CustomerID And cl.Status = 'Activated' ";

		// Keyword ----------------------------------------------------------------
		if($Criteria['Keyword'] != '')
		{
			$query .= "And ( ";
			$query .= "(q.Question Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(q.Answer Like '%" . $Criteria['Keyword'] . "%') ";
			$query .= ") ";
		}

		// Order By
		$query .= "Order By q.ProductID, $SortField $SortType ";

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

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

	/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
	*                         Product Updates Search Functions
	*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
	/*
   * Retrieves the count of releases available according to the given search criteria
   */
	function getTotalUpdates($Criteria)
	{
		global $language;
		global $customer;

		$query =
						"
							Select COUNT(Distinct u.ReleaseID) From product_update u
							Inner Join customer_license cl On u.ProductID = cl.ProductID
							Where u.Deleted = 0 And u.Enabled = 1 And u.Language = '$language'
							And cl.CustomerID = $customer->CustomerID And cl.Status = 'Activated' ";

		// Keyword ----------------------------------------------------------------
		if($Criteria['Keyword'] != '')
		{
			$query .= "And ( ";
			$query .= "(u.FileName Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(u.DownloadSize Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(u.VersionNumber Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(u.OperatingSystem Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(u.Description Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(u.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 getUpdatesList($Criteria = '', $SortField = 'u.ReleaseID', $SortType = 'DESC', $Start = -1, $Num = -1)
	{
		global $language;
		global $customer;

		$query =
						"
							Select Distinct u.ReleaseID From product_update u
							Inner Join customer_license cl On u.ProductID = cl.ProductID
							Where u.Deleted = 0 And u.Enabled = 1 And u.Language = '$language'
							And cl.CustomerID = $customer->CustomerID And cl.Status = 'Activated' ";


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

		// Order By
		$query .= "Order By u.ProductID, $SortField $SortType ";

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

}
?>