<commit_before><?php

/**
 * ShoppingCart is a session handler that stores
 * information about what products are in a user's
 * cart on the site.
 *
 * @package ecommerce
 * @Description
 ** Non URL based adding	add_buyable->find_or_make_order_item->add_(new)_item
 ** URL based adding	additem->getNew/ExistingOrderItem->add_(new)_item
 * @authors: Silverstripe, Jeremy, Nicolaas
 **/

class ShoppingCart extends Controller {

	public static $url_segment = 'shoppingcart';
		static function set_url_segment($v) {self::$url_segment = $v;}
		static function get_url_segment() {return self::$url_segment;}

	protected static $order = null; // for temp caching
		static function set_order(Order $v) {self::$order = $v;}
		static function get_order() {user_error("Use self::current_order() to get order.", E_USER_ERROR);}

	protected static $cartid_session_name = 'shoppingcartid';
		public static function set_cartid_session_name($v) {self::$cartid_session_name = $v;}
		public static function get_cartid_session_name() {return self::$cartid_session_name;}

	protected static $response_class = "CartResponse";
		public static function set_response_class($v) {self::$url_segment = $v;}
		public static function get_response_class() {return self::$url_segment;}

	static $allowed_actions = array (
		'additem',
		'incrementitem',
		'decrementitem',
		'removeitem',
		'removeallitem',
		'removemodifier',
		'setcountry',
		'setquantityitem',
		'clear',
		'numberofitemsincart',
		'showcart',
		'debug' => 'SHOP_ADMIN'
	);


	/**
	 *	used for allowing certian url parameters to be applied to orderitems
	 *	eg: ?Color=red will set OrderItem color to 'red'
	 *	name - defaultvalue (needed for default orderitems)
	 *
	 *	array(
	 *		'Color' => 'Red' //default to red
	 *	)
	 *
	*/
	protected static $default_param_filters = array();
		static function set_default_param_filters($array){self::$default_param_filters = $array;}
		static function add_default_param_filters($array){self::$default_param_filters = array_merge(self::$default_param_filters,$array);}
		static function get_default_param_filters(){return self::$default_param_filters;}

	protected static $country_setting_index = 'ShoppingCartCountry';
		static function set_country_setting_index($v) {self::$country_setting_index = $v;}
		static function get_country_setting_index() {return self::$country_setting_index;}

	static function set_country($country) {Session::set(self::get_country_setting_index(), $country);}
	static function get_country() {return Session::get(self::get_country_setting_index());}
	static function remove_country() {Session::clear(self::get_country_setting_index());}

	protected static $clear_days = 90;
		function set_clear_days($days = 90){self::$clear_days = $days;}
		function get_clear_days(){return self::$clear_days;}

	protected static $never_delete_if_linked_to_member = false;
		function set_never_delete_if_linked_to_member($b){self::$never_delete_if_linked_to_member = $b;}
		function get_never_delete_if_linked_to_member(){return self::$never_delete_if_linked_to_member;}


/*******************************************************
   * DELETE OLD SHOPPING CARTS
*******************************************************/

	public static function delete_old_carts(){
		$time = date('Y-m-d H:i:s', strtotime("-".self::$clear_days." days"));
		$generalWhere = "\"StatusID\" = ".OrderStatus::get_status_id("CREATED")." AND \"LastEdited\" < '$time'";
		if(self::$never_delete_if_linked_to_member) {
			$oldcarts = DataObject::get('Order',$generalWhere." AND \"Member\".\"ID\" IS NULL", $sort = "", $join = "LEFT JOIN \"Member\" ON \"Member\".\"ID\" = \"Order\".\"MemberID\" ");
		}
		else {
			$oldcarts = DataObject::get('Order',$generalWhere);
		}
		if($oldcarts){
			foreach($oldcarts as $cart){
				$cart->delete();
				$cart->destroy();
			}
		}
	}


/*******************************************************
   * STARTUP
*******************************************************/

	function init() {
		parent::init();
		self::current_order()->init();
	}

	public static function current_order() {
		$order = self::$order;
		$hasWritten = false;
		if (!self::$order) {
			//find order by id saved to session (allows logging out and retaining cart contents)
			$cartID = Session::get(self::$cartid_session_name);
			//TODO: make clear cart on logout optional
			if ($cartID) {
				$cartIDParts = Convert::raw2sql(explode(".", $cartID));
				if(is_array($cartIDParts) && count($cartIDParts) == 2) {
					$orders = DataObject::get(
						'Order',
						"\"Order_Status\".\"CanEdit\" = 1 AND \"Order\".\"ID\" = '".intval($cartIDParts[0])."' AND \"Order\".\"SessionID\" = '".$cartIDParts[1]."'",
						"\"LastEdited\" DESC",
						"INNER JOIN \"OrderStatus\" ON \"OrderStatus\" .\"ID\" = \"Order\".\"StatusID\"",
						"1"
					);
					if($orders) {
						self::$order = $orders->First();
					}
					else {
						//TO DO: create notice that order can not be found
					}
				}
			}
			if(!self::$order){
				//TODO: is this the right time to delete them???
				self::delete_old_carts();
				self::$order = new Order();
				self::$order->start(session_id());<commit_msg>Fixed bug in ShoppingCart::current_order()<commit_after><?php

/**
 * ShoppingCart is a session handler that stores
 * information about what products are in a user's
 * cart on the site.
 *
 * @package ecommerce
 * @Description
 ** Non URL based adding	add_buyable->find_or_make_order_item->add_(new)_item
 ** URL based adding	additem->getNew/ExistingOrderItem->add_(new)_item
 * @authors: Silverstripe, Jeremy, Nicolaas
 **/

class ShoppingCart extends Controller {

	public static $url_segment = 'shoppingcart';
		static function set_url_segment($v) {self::$url_segment = $v;}
		static function get_url_segment() {return self::$url_segment;}

	protected static $order = null; // for temp caching
		static function set_order(Order $v) {self::$order = $v;}
		static function get_order() {user_error("Use self::current_order() to get order.", E_USER_ERROR);}

	protected static $cartid_session_name = 'shoppingcartid';
		public static function set_cartid_session_name($v) {self::$cartid_session_name = $v;}
		public static function get_cartid_session_name() {return self::$cartid_session_name;}

	protected static $response_class = "CartResponse";
		public static function set_response_class($v) {self::$url_segment = $v;}
		public static function get_response_class() {return self::$url_segment;}

	static $allowed_actions = array (
		'additem',
		'incrementitem',
		'decrementitem',
		'removeitem',
		'removeallitem',
		'removemodifier',
		'setcountry',
		'setquantityitem',
		'clear',
		'numberofitemsincart',
		'showcart',
		'debug' => 'SHOP_ADMIN'
	);


	/**
	 *	used for allowing certian url parameters to be applied to orderitems
	 *	eg: ?Color=red will set OrderItem color to 'red'
	 *	name - defaultvalue (needed for default orderitems)
	 *
	 *	array(
	 *		'Color' => 'Red' //default to red
	 *	)
	 *
	*/
	protected static $default_param_filters = array();
		static function set_default_param_filters($array){self::$default_param_filters = $array;}
		static function add_default_param_filters($array){self::$default_param_filters = array_merge(self::$default_param_filters,$array);}
		static function get_default_param_filters(){return self::$default_param_filters;}

	protected static $country_setting_index = 'ShoppingCartCountry';
		static function set_country_setting_index($v) {self::$country_setting_index = $v;}
		static function get_country_setting_index() {return self::$country_setting_index;}

	static function set_country($country) {Session::set(self::get_country_setting_index(), $country);}
	static function get_country() {return Session::get(self::get_country_setting_index());}
	static function remove_country() {Session::clear(self::get_country_setting_index());}

	protected static $clear_days = 90;
		function set_clear_days($days = 90){self::$clear_days = $days;}
		function get_clear_days(){return self::$clear_days;}

	protected static $never_delete_if_linked_to_member = false;
		function set_never_delete_if_linked_to_member($b){self::$never_delete_if_linked_to_member = $b;}
		function get_never_delete_if_linked_to_member(){return self::$never_delete_if_linked_to_member;}


/*******************************************************
   * DELETE OLD SHOPPING CARTS
*******************************************************/

	public static function delete_old_carts(){
		$time = date('Y-m-d H:i:s', strtotime("-".self::$clear_days." days"));
		$generalWhere = "\"StatusID\" = ".OrderStatus::get_status_id("CREATED")." AND \"LastEdited\" < '$time'";
		if(self::$never_delete_if_linked_to_member) {
			$oldcarts = DataObject::get('Order',$generalWhere." AND \"Member\".\"ID\" IS NULL", $sort = "", $join = "LEFT JOIN \"Member\" ON \"Member\".\"ID\" = \"Order\".\"MemberID\" ");
		}
		else {
			$oldcarts = DataObject::get('Order',$generalWhere);
		}
		if($oldcarts){
			foreach($oldcarts as $cart){
				$cart->delete();
				$cart->destroy();
			}
		}
	}


/*******************************************************
   * STARTUP
*******************************************************/

	function init() {
		parent::init();
		self::current_order()->init();
	}

	public static function current_order() {
		$order = self::$order;
		$hasWritten = false;
		if (!self::$order) {
			//find order by id saved to session (allows logging out and retaining cart contents)
			$cartID = Session::get(self::$cartid_session_name);
			//TODO: make clear cart on logout optional
			if ($cartID) {
				$cartIDParts = Convert::raw2sql(explode(".", $cartID));
				if(is_array($cartIDParts) && count($cartIDParts) == 2) {
					$orders = DataObject::get(
						'Order',
						"\"Order_Status\".\"CanEdit\" = 1 AND \"Order\".\"ID\" = '".intval($cartIDParts[0])."' AND \"Order\".\"SessionID\" = '".$cartIDParts[1]."'",
						"\"LastEdited\" DESC",
						"INNER JOIN \"OrderStatus\" ON \"OrderStatus\" .\"ID\" = \"Order\".\"StatusID\"",
						"1"
					);
					if($orders) {
						self::$order = $orders->First();
					}
					else {
						//TO DO: create notice that order can not be found
					}
				}
			}
			if(!self::$order){
				//TODO: is this the right time to delete them???
				self::delete_old_carts();
				self::$order = new Order();
				self::$order->init(session_id());
{'fraction_non_alphanumeric': 0.19288063455213775, 'fraction_numerical': 0.001934610176049526, 'mean_word_length': 5.088928150765606, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6117421', 'n_tokens_mistral': 3579, 'n_tokens_neox': 3250, 'n_words': 720}