<commit_before><?php

class Cart extends NG {

	private $usr;

	// Constructor: Initialize session and db connections
	function __construct() {
		parent::__construct();

		if ( !isset($_SESSION['cart']) ) $_SESSION['cart'] = array(); // assign empty cart if applicable
		if ( !isset($_SESSION['cart.options']) ) $_SESSION['cart.options'] = array(); // assign empty cart.options if applicable
		if ( !isset($_SESSION['cart.discounts']) ) $_SESSION['cart.discounts'] = array(); // assign empty cart.discounts if applicable
		$this->usr = new User();
	}

	public static function process( $action, &$pass, &$data ) {
		$obj = new Cart();
		switch ( $action ) {
			// Cart Functions
			case 'get': $data = $obj->get(); break; // get entire cart
			case 'rem': $data = $obj->rem(); break; // remove item from cart
			case 'add': $data = $obj->add(); break; // add invoice to cart
			case 'clr': $data = $obj->clr(); break; // clear cart

			// Cart.Options Functions
			case 'getOptions': $data = $obj->getOptions(); break; // get all options
			case 'getOption':  $data = $obj->getOption();  break; // get conference form data
			case 'setOption':  $data = $obj->setOption();  break; // get conference form data

			// Cart.Discount Functions
			case 'getDiscount': $data = $obj->getDiscount(); break; // get all discounts
			case 'addDiscount': $data = $obj->addDiscount(); break; // add discounts
			case 'remDiscount': $data = $obj->remDiscount(); break; // rem discounts

			// Generic Functions
			case 'save': $data = $obj->save(); break;
			case 'getPurchases': $data = $obj->getPurchases(); break;
			default: $pass = false;
		}
	}

	// CART ACTIONS

	// Worker(app): return cart with current prices
	public function get() {
		$user = $this->usr->currentUser(); // gets user if available

		$checkSTH = $this->db->prepare("SELECT purchaseID FROM `purchase` WHERE firmID=? and itemID=? UNION SELECT acquisitionID FROM `acquisition` WHERE itemID=? UNION SELECT memberID FROM `member` WHERE firmID=? AND groupID=?");

		// remove all non-strings to allow invoices, but still grab purchase id's
		$cleanIDs = array();
		foreach($_SESSION['cart'] as $itemID) if (is_string($itemID)) array_push($cleanIDs, $itemID);

		// Grab pending group purchases
		$queryMarks = trim( str_repeat( "?,", sizeof( $cleanIDs ) ), "," );
		$pendingGroupsSTH = $this->db->prepare("SELECT i.settings FROM (SELECT * FROM `item` WHERE itemID IN ($queryMarks)) i LEFT JOIN `product` p ON i.productID=p.productID LEFT JOIN `template` t ON p.templateID=t.templateID WHERE template='group';");
		$pendingGroupsSTH->execute( $cleanIDs );

		// parse out their groupID's
		$arrGroupID = array();
		while ( $row = $pendingGroupsSTH->fetch( PDO::FETCH_ASSOC ) ) {
			$rowData = (array) json_decode($row['settings']);
			array_push( $arrGroupID, $rowData['groupID'] );
		}
		$this->groupCashe = $arrGroupID; // store for later use in getItemByID() -> getProductCost()

		// Iterate through ID's and grab items or pass objects through
		$retData = array();
		setlocale(LC_MONETARY, 'en_US');
		foreach ($_SESSION['cart'] as $itemID) {
			if (is_string($itemID)) {
				$item = $this->getItemByID( $itemID );
				if ($item != null) {

					// warn if item has already been purchased
					$groupID = isset($item['settings']->groupID) ? $item['settings']->groupID : -1;
					$checkSTH->execute( $user['firmID'], $itemID, $itemID, $user['firmID'], $groupID );
					$item['warn'] = ($checkSTH->rowCount() > 0);

					array_push($retData, $item);
				}
			} else { // format cost for carts
				$cost = $itemID['cost'];
				$itemID['cost'] = array(
					'pretty' => '$' . money_format('%n', $cost),<commit_msg>Cart-rewrite: fixing acquisition query<commit_after><?php

class Cart extends NG {

	private $usr;

	// Constructor: Initialize session and db connections
	function __construct() {
		parent::__construct();

		if ( !isset($_SESSION['cart']) ) $_SESSION['cart'] = array(); // assign empty cart if applicable
		if ( !isset($_SESSION['cart.options']) ) $_SESSION['cart.options'] = array(); // assign empty cart.options if applicable
		if ( !isset($_SESSION['cart.discounts']) ) $_SESSION['cart.discounts'] = array(); // assign empty cart.discounts if applicable
		$this->usr = new User();
	}

	public static function process( $action, &$pass, &$data ) {
		$obj = new Cart();
		switch ( $action ) {
			// Cart Functions
			case 'get': $data = $obj->get(); break; // get entire cart
			case 'rem': $data = $obj->rem(); break; // remove item from cart
			case 'add': $data = $obj->add(); break; // add invoice to cart
			case 'clr': $data = $obj->clr(); break; // clear cart

			// Cart.Options Functions
			case 'getOptions': $data = $obj->getOptions(); break; // get all options
			case 'getOption':  $data = $obj->getOption();  break; // get conference form data
			case 'setOption':  $data = $obj->setOption();  break; // get conference form data

			// Cart.Discount Functions
			case 'getDiscount': $data = $obj->getDiscount(); break; // get all discounts
			case 'addDiscount': $data = $obj->addDiscount(); break; // add discounts
			case 'remDiscount': $data = $obj->remDiscount(); break; // rem discounts

			// Generic Functions
			case 'save': $data = $obj->save(); break;
			case 'getPurchases': $data = $obj->getPurchases(); break;
			default: $pass = false;
		}
	}

	// CART ACTIONS

	// Worker(app): return cart with current prices
	public function get() {
		$user = $this->usr->currentUser(); // gets user if available

		// check areas for previously purchased item
		$q[] = "SELECT purchaseID FROM `purchase` WHERE firmID=? and itemID=?";
		$q[] = "SELECT CONCAT('a',acquisitionID) FROM `acquisition` a LEFT JOIN `order` o ON a.orderID = o.orderID WHERE itemID=? AND contactID=?";
		$q[] = "SELECT memberID FROM `member` WHERE firmID=? AND groupID=?";
		$checkSTH = $this->db->prepare( implode(" UNION ", $q) . ";" );

		// remove all non-strings to allow invoices, but still grab purchase id's
		$cleanIDs = array();
		foreach($_SESSION['cart'] as $itemID) if (is_string($itemID)) array_push($cleanIDs, $itemID);

		// Grab pending group purchases
		$queryMarks = trim( str_repeat( "?,", sizeof( $cleanIDs ) ), "," );
		$pendingGroupsSTH = $this->db->prepare("SELECT i.settings FROM (SELECT * FROM `item` WHERE itemID IN ($queryMarks)) i LEFT JOIN `product` p ON i.productID=p.productID LEFT JOIN `template` t ON p.templateID=t.templateID WHERE template='group';");
		$pendingGroupsSTH->execute( $cleanIDs );

		// parse out their groupID's
		$arrGroupID = array();
		while ( $row = $pendingGroupsSTH->fetch( PDO::FETCH_ASSOC ) ) {
			$rowData = (array) json_decode($row['settings']);
			array_push( $arrGroupID, $rowData['groupID'] );
		}
		$this->groupCashe = $arrGroupID; // store for later use in getItemByID() -> getProductCost()

		// Iterate through ID's and grab items or pass objects through
		$retData = array();
		setlocale(LC_MONETARY, 'en_US');
		foreach ($_SESSION['cart'] as $itemID) {
			if (is_string($itemID)) {
				$item = $this->getItemByID( $itemID );
				if ($item != null) {

					// warn if item has already been purchased
					$groupID = isset($item['settings']->groupID) ? $item['settings']->groupID : -1;
					$checkSTH->execute( $user['firmID'], $itemID, $itemID, $user['contactID'], $user['firmID'], $groupID );
					$item['warn'] = ($checkSTH->rowCount() > 0);

					array_push($retData, $item);
				}
			} else { // format cost for carts
				$cost = $itemID['cost'];
				$itemID['cost'] = array(
					'pretty' => '$' . money_format('%n', $cost),
{'fraction_non_alphanumeric': 0.1766807834833245, 'fraction_numerical': 0.0005293806246691371, 'mean_word_length': 3.9103313840155947, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, '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': '5796475', 'n_tokens_mistral': 2686, 'n_tokens_neox': 2385, 'n_words': 712}