/** * Unit test for eZProductCollectionItem::cleanupList() * * Outline: * 1) Create 40 eZProductCollectionItemOption objects with an item_id * from 1 to 4 * 2) Call cleanupList with (1, 2) as a parameter * 4) Check that the 20 matching items have been removed * 5) Check that the 20 other, non-matching items haven't been removed */ public function testCleanupList() { // Create a few collections $row = array('item_id' => null, 'option_item_id' => 1, 'object_attribute_id' => 1, 'name' => __FUNCTION__, 'value' => __FUNCTION__, 'price' => 5.5); $deleteIDArray = $keepIDArray = array(); for ($i = 1; $i <= 40; $i++) { $row['item_id'] = ceil($i / 10); $item = new eZProductCollectionItemOption($row); $item->store(); } eZProductCollectionItemOption::cleanupList(array(1, 2)); // Check that each item of $deleteIDArray has been removed foreach (array(1, 2) as $itemID) { $options = eZProductCollectionItemOption::fetchList($itemID); $this->assertEquals(0, count($options)); } // And check that each item of $keepIDArray hasn't been deleted foreach (array(3, 4) as $itemID) { $options = eZProductCollectionItemOption::fetchList($itemID); $this->assertEquals(10, count($options)); foreach ($options as $option) { $this->assertInstanceOf('eZProductCollectionItemOption', $option); } } }
/** * Fetches eZProductCollectionItemOption items that match the given item ID, * sorted by ascending order of option ID * * @param int $productCollectionItemID * @param bool $asObject * * @return array(eZProductCollectionItemOption) */ static function fetchList($productCollectionItemID, $asObject = true) { return eZPersistentObject::fetchObjectList(eZProductCollectionItemOption::definition(), null, array("item_id" => $productCollectionItemID), array("id" => "ASC"), null, $asObject); }
function collectProductCollectionItems($maxTime = false, $sleepTime = false, $limit = false) { $db = eZDB::instance(); if ($maxTime === false and $db->hasRequiredServerVersion('4.0', 'mysql')) { $sql = "DELETE FROM ezproductcollection_item, ezproductcollection_item_opt\nUSING ezproductcollection\n LEFT JOIN ezproductcollection_item\n ON ezproductcollection.id = ezproductcollection_item.productcollection_id\n LEFT JOIN ezproductcollection_item_opt\n ON ezproductcollection_item.id = ezproductcollection_item_opt.item_id\nWHERE ezproductcollection.id IS NULL"; $db->query($sql); return; } // Find all items which are lacking a collection (db leaks) if ($db->hasRequiredServerVersion('8', 'oracle')) { $sql = "SELECT ezproductcollection_item.productcollection_id\nFROM ezproductcollection, ezproductcollection_item\nWHERE ezproductcollection.id = ezproductcollection_item.productcollection_id (+) AND\n ezproductcollection.id IS NULL"; } else { $sql = "SELECT ezproductcollection_item.productcollection_id\nFROM ezproductcollection\n RIGHT JOIN ezproductcollection_item\n ON ezproductcollection.id = ezproductcollection_item.productcollection_id\nWHERE ezproductcollection.id IS NULL"; } if ($limit === false) { $limit = eZDBGarbageCollector::ITEM_LIMIT; } $end = false; if (is_numeric($maxTime)) { $end = time() + $maxTime; } do { $rows = $db->arrayQuery($sql, array('offset' => 0, 'limit' => $limit)); if (count($rows) == 0) { break; } $idList = array(); foreach ($rows as $row) { $idList[] = (int) $row['productcollection_id']; } eZProductCollectionItemOption::cleanupList($idList); $ids = implode(', ', $idList); $db->query("DELETE FROM ezproductcollection_item WHERE productcollection_id IN ( {$ids} )"); // Stop when we used up our time if ($end !== false and time() > $end) { break; } // Sleep a little if required, reduces load on server if ($sleepTime !== false) { sleep($sleepTime); } } while (true); }
if (is_array($optionString)) { foreach ($optionString as $optionID) { $optionIDList[] = array('attribute_id' => $attributeID, 'option_string' => $optionID); } } else { $optionIDList[] = array('attribute_id' => $attributeID, 'option_string' => $optionString); } } foreach ($optionIDList as $optionIDItem) { $attributeID = $optionIDItem['attribute_id']; $optionString = $optionIDItem['option_string']; $attribute = eZContentObjectAttribute::fetch($attributeID, $object->attribute('current_version')); $dataType = $attribute->dataType(); $optionData = $dataType->productOptionInformation($attribute, $optionString, $item); if ($optionData) { $optionItem = eZProductCollectionItemOption::create($item->attribute('id'), $optionData['id'], $optionData['name'], $optionData['value'], 0, $attributeID); $optionItem->store(); //$price += $optionData['additional_price']; } } $db->commit(); //if ( $price != $priceWithoutOptions ) //{ // $item->setAttribute( "price", $price ); // $item->store(); //} } $module->redirectTo($module->functionURI("wishlist") . "/"); return; } if ($http->hasPostVariable("RemoveProductItemButton")) {
function addToBasket($objectID, $optionList, $quantity) { $object = eZContentObject::fetch($objectID); $nodeID = $object->attribute('main_node_id'); $price = 0.0; $isVATIncluded = true; $attributes = $object->contentObjectAttributes(); $priceFound = false; foreach ($attributes as $attribute) { $dataType = $attribute->dataType(); if (eZShopFunctions::isProductDatatype($dataType->isA())) { $priceObj = $attribute->content(); $price += $priceObj->attribute('price'); $priceFound = true; } } if (!$priceFound) { eZDebug::writeError('Attempted to add object without price to basket.'); return array('status' => eZModuleOperationInfo::STATUS_CANCELLED); } $currency = $priceObj->attribute('currency'); // Check for 'option sets' in option list. // If found each 'option set' will be added as a separate product purchase. $hasOptionSet = false; foreach (array_keys($optionList) as $optionKey) { if (substr($optionKey, 0, 4) == 'set_') { $returnStatus = eZShopOperationCollection::addToBasket($objectID, $optionList[$optionKey]); // If adding one 'option set' fails we should stop immediately if ($returnStatus['status'] == eZModuleOperationInfo::STATUS_CANCELLED) { return $returnStatus; } $hasOptionSet = true; } } if ($hasOptionSet) { return $returnStatus; } $unvalidatedAttributes = array(); foreach ($attributes as $attribute) { $dataType = $attribute->dataType(); if ($dataType->isAddToBasketValidationRequired()) { $errors = array(); if ($attribute->validateAddToBasket($optionList[$attribute->attribute('id')], $errors) !== eZInputValidator::STATE_ACCEPTED) { $description = $errors; $contentClassAttribute = $attribute->contentClassAttribute(); $attributeName = $contentClassAttribute->attribute('name'); $unvalidatedAttributes[] = array("name" => $attributeName, "description" => $description); } } } if (count($unvalidatedAttributes) > 0) { return array('status' => eZModuleOperationInfo::STATUS_CANCELLED, 'reason' => 'validation', 'error_data' => $unvalidatedAttributes); } $basket = eZBasket::currentBasket(); /* Check if the item with the same options is not already in the basket: */ $itemID = false; $collection = $basket->attribute('productcollection'); if (!$collection) { eZDebug::writeError('Unable to find product collection.'); return array('status' => eZModuleOperationInfo::STATUS_CANCELLED); } else { $collection->setAttribute('currency_code', $currency); $collection->store(); $count = 0; /* Calculate number of options passed via the HTTP variable: */ foreach (array_keys($optionList) as $key) { if (is_array($optionList[$key])) { $count += count($optionList[$key]); } else { $count++; } } $collectionItems = $collection->itemList(false); foreach ($collectionItems as $item) { /* For all items in the basket which have the same object_id: */ if ($item['contentobject_id'] == $objectID) { $options = eZProductCollectionItemOption::fetchList($item['id'], false); /* If the number of option for this item is not the same as in the HTTP variable: */ if (count($options) != $count) { break; } $theSame = true; foreach ($options as $option) { /* If any option differs, go away: */ if (is_array($optionList[$option['object_attribute_id']]) && !in_array($option['option_item_id'], $optionList[$option['object_attribute_id']]) || !is_array($optionList[$option['object_attribute_id']]) && $option['option_item_id'] != $optionList[$option['object_attribute_id']]) { $theSame = false; break; } } if ($theSame) { $itemID = $item['id']; break; } } } if ($itemID) { /* If found in the basket, just increment number of that items: */ $item = eZProductCollectionItem::fetch($itemID); $item->setAttribute('item_count', $quantity + $item->attribute('item_count')); $item->store(); } else { $item = eZProductCollectionItem::create($basket->attribute("productcollection_id")); $item->setAttribute('name', $object->attribute('name')); $item->setAttribute("contentobject_id", $objectID); $item->setAttribute("item_count", $quantity); $item->setAttribute("price", $price); if ($priceObj->attribute('is_vat_included')) { $item->setAttribute("is_vat_inc", '1'); } else { $item->setAttribute("is_vat_inc", '0'); } $item->setAttribute("vat_value", $priceObj->attribute('vat_percent')); $item->setAttribute("discount", $priceObj->attribute('discount_percent')); $item->store(); $priceWithoutOptions = $price; $optionIDList = array(); foreach (array_keys($optionList) as $key) { $attributeID = $key; $optionString = $optionList[$key]; if (is_array($optionString)) { foreach ($optionString as $optionID) { $optionIDList[] = array('attribute_id' => $attributeID, 'option_string' => $optionID); } } else { $optionIDList[] = array('attribute_id' => $attributeID, 'option_string' => $optionString); } } $db = eZDB::instance(); $db->begin(); foreach ($optionIDList as $optionIDItem) { $attributeID = $optionIDItem['attribute_id']; $optionString = $optionIDItem['option_string']; $attribute = eZContentObjectAttribute::fetch($attributeID, $object->attribute('current_version')); $dataType = $attribute->dataType(); $optionData = $dataType->productOptionInformation($attribute, $optionString, $item); if ($optionData) { $optionData['additional_price'] = eZShopFunctions::convertAdditionalPrice($currency, $optionData['additional_price']); $optionItem = eZProductCollectionItemOption::create($item->attribute('id'), $optionData['id'], $optionData['name'], $optionData['value'], $optionData['additional_price'], $attributeID); $optionItem->store(); $price += $optionData['additional_price']; } } if ($price != $priceWithoutOptions) { $item->setAttribute("price", $price); $item->store(); } $db->commit(); } } return array('status' => eZModuleOperationInfo::STATUS_CONTINUE); }
/** * Removes all product collection items which related to the product * collections specified in the parameter array * * @param array $productCollectionIDList array of eZProductCollection IDs * * @return void */ static function cleanupList($productCollectionIDList) { $db = eZDB::instance(); $db->begin(); $inText = $db->generateSQLINStatement($productCollectionIDList, 'productcollection_id', false, false, 'int'); $rows = $db->arrayQuery("SELECT id FROM ezproductcollection_item WHERE {$inText}"); if (count($rows) > 0) { $itemIDList = array(); foreach ($rows as $row) { $itemIDList[] = $row['id']; } eZProductCollectionItemOption::cleanupList($itemIDList); } $db->query("DELETE FROM ezproductcollection_item WHERE {$inText}"); $db->commit(); }