Return to Snippet

Revision: 24965
at March 17, 2010 02:17 by fwso


Initial Code
/**
 * Get one or more random date value between the start date
 * and the end date
 * 
 * The end date is not included.
 * 
 * Date formate: yyyy-mm-dd
 * 
 * @param date $dateStart
 * @param date $dateEnd
 * @param integer $number
 * @return array
 */
function getRandomDates ($dateStart, $dateEnd, $number = 1) {
	
	$dates = array ();
	
	$hh1 = 8;
	$hh2 = 24;
	
	$ii1 = 0;
	$ii2 = 60;
	
	$ss1 = 0;
	$ss2 = 60;
	
	$parts1 = explode('-',$dateStart);
	$parts2 = explode('-',$dateEnd);
	
	if (sizeof($parts1) == 3 && sizeof($parts2) == 3) {
		
		$days = abs(strtotime($dateEnd) - strtotime($dateStart)) / (3600*24);
		$randomDays = getRandomIntegers(0, $days, $number);
		
		$yy1 = $parts1[0];
		$mm1 = $parts1[1];
		$dd1 = $parts1[2];
		
		for ($i = 0; $i < $number; $i++) {
			
			$dates[] = date('Y-m-d H:i:s', mktime(getRandomIntegers ($hh1, $hh2), getRandomIntegers ($ii1, $ii2), 
							getRandomIntegers ($ss1, $ss2), $mm1, $dd1 + $randomDays[$i], $yy1));
			
		}
	
	}
	
	return $dates;
}


/**
 * Get one or more random number between the start and the end
 * The end date is not included.
 * 
 * @param integer $start
 * @param integer $end
 * @param integer $number
 * @return array
 */
function getRandomIntegers ($start, $end, $number = 1) {

	$ris = array();
	
	//reduce one for mt_rand is inclusive.
	$end--;
	
	for ($i = 0; $i < $number; $i++) {
		
		$ris[] = mt_rand($start, $end);
		
	}
	
	if (sizeof($ris) == 1) {
		return $ris[0];
	}
	
	return $ris;
	
}

/**
 * Get all the subcategory IDs of the parent category whose ID 
 * is specified.
 *  
 * @param integer $parentID
 * @return array
 */
function getSubcategories ($parentID, $self = false) {
	
	
	$subs = array ();
	
	if ($self) {
		$subs[] = $parentID;
	}
	
	_getSubcategories ($parentID, &$subs);
	
	return $subs;
	
}

/**
 * Help function for getSubcategories()
 * 
 * @param integer $pid
 * @param array $cats
 * @see getSubcategories()
 */
function _getSubcategories ($pid, &$cats) {
	
	global $db;
	
	$sql = "SELECT c.categories_id FROM " . TABLE_CATEGORIES . " c WHERE c.parent_id=$pid";
	
	$result = $db->Execute($sql);
	
	while (!$result->EOF) {
		
		$cats[] = $result->fields['categories_id'];
		
		_getSubcategories ($result->fields['categories_id'], &$cats);
		
		$result->MoveNext();
	}
	
}

/**
 * Get all the product ids of the specified ids
 * 
 * @param integer $categoryID
 * 
 */
function getAllProductsIds ($categoryID) {

	global $db;
	
	$ids = array ();

	$allCats = getSubcategories($categoryID, true);
	
	$sql = "SELECT DISTINCT p2c.products_id FROM " . TABLE_PRODUCTS_TO_CATEGORIES . 
			" p2c WHERE p2c.categories_id IN (" . implode(',', $allCats) . ")";
	
	$result = $db->Execute ($sql);
	
	while (!$result->EOF) {
		
		$ids [] = $result->fields['products_id'];
		
		$result->MoveNext();
		
	}
	
	return $ids;
	
}

/**
 * Get the file extension
 * 
 * @param string $fileName
 * @return string
 */
function getFileExtension ($fileName) {
	
	return strtolower(substr($fileName, strrpos($fileName, '.')));
	
}

/**
 * Create a new file name in order to avoid file name conflict.
 * 
 * @param string $fileName
 * @return string
 */
function createNewFileName ($fileName) {
	return md5($filename . date('YmdHis'));
}

/**
 * main function
 * 
 * Create Fake Orders
 * 
 * @param integer $categoryID
 * @param array $names
 * @param array $countries
 * @param date $fDate
 * @param date $tDate
 * @param integer $fQ
 * @param integer $tQ
 */
function createFakeOrders ($categoryID, $names, $countries, $fDate, $tDate, $fQ, $tQ, $numbers = 1) {
	
	global $db,$messageStack;
	
	$products = getAllProductsIds ($categoryID);
	
	$counts = sizeof($products) * $numbers;
	
	if ($counts > 0) {
		
		$nameIdxes = getRandomIntegers(0, sizeof($names), $counts);
	
		$countryIdxes = getRandomIntegers(0, sizeof($countries), $counts);
		
		$dates = getRandomDates($fDate, $tDate, $counts);
		
		$quantities = getRandomIntegers($fQ, $tQ, $counts);
		
		$sql = "INSERT INTO " . TABLE_ORERS_FAKE . " (products_id, name, customers_country, date, quantity) VALUES ";
		
		
		$idx = 0;
		
		for ($i = 0; $i < sizeof($products); $i++) {
			
			for ($k = 0; $k < $numbers; $k++) {
				
				if ($idx == 0) {
					$sql .= "(". $products[$i] . ",'" . mysql_real_escape_string($names[$nameIdxes[$idx]]) . "','" . 
						mysql_real_escape_string ($countries[$countryIdxes[$idx]]) . "','" . $dates[$idx] . "'," . $quantities[$idx] .")";
					
				} else {
				
					$sql .= ",(". $products[$i] . ",'" . mysql_real_escape_string ($names[$nameIdxes[$idx]]) . "','" . 
					 	mysql_real_escape_string ($countries[$countryIdxes[$idx]]) . "','" . $dates[$idx] . "'," . $quantities[$idx] .")";
				}
				
				$idx++;
			}
			
		}
		
		//echo $sql . '<br />';
		$db->Execute($sql);
		$rows = mysql_affected_rows();
		if ($rows > 0) {
			$messageStack->add_session('Fake orders generated:' . $rows, 'success');
		} else {
			$messageStack->add_session('Failed to generate fake orders', 'caution');
		}
		
		zen_redirect(FILENAME_FAKE_ORDER);
	}
	
}

Initial URL
http://ifuturetech.com

Initial Description
Used to generates fake orders for Zen-Cart.

Initial Title
Zen-Cart Fake Order Process

Initial Tags
date

Initial Language
PHP