private function getCleanedEcommerceItems($items)
{
// Clean up the items array
$cleanedItems = array();
foreach ($items as $item) {
$name = $category = $category2 = $category3 = $category4 = $category5 = false;
$price = 0;
$quantity = 1;
// items are passed in the request as an array: ( $sku, $name, $category, $price, $quantity )
if (empty($item[self::INDEX_ITEM_SKU])) {
continue;
}
$sku = $item[self::INDEX_ITEM_SKU];
if (!empty($item[self::INDEX_ITEM_NAME])) {
$name = $item[self::INDEX_ITEM_NAME];
}
if (!empty($item[self::INDEX_ITEM_CATEGORY])) {
$category = $item[self::INDEX_ITEM_CATEGORY];
}
if (isset($item[self::INDEX_ITEM_PRICE]) && is_numeric($item[self::INDEX_ITEM_PRICE])) {
$price = $this->getRevenue($item[self::INDEX_ITEM_PRICE]);
}
if (!empty($item[self::INDEX_ITEM_QUANTITY]) && is_numeric($item[self::INDEX_ITEM_QUANTITY])) {
$quantity = (int) $item[self::INDEX_ITEM_QUANTITY];
}
// self::INDEX_ITEM_* are in order
$cleanedItems[] = array(self::INTERNAL_ITEM_SKU => $sku, self::INTERNAL_ITEM_NAME => $name, self::INTERNAL_ITEM_CATEGORY => $category, self::INTERNAL_ITEM_CATEGORY2 => $category2, self::INTERNAL_ITEM_CATEGORY3 => $category3, self::INTERNAL_ITEM_CATEGORY4 => $category4, self::INTERNAL_ITEM_CATEGORY5 => $category5, self::INTERNAL_ITEM_PRICE => $price, self::INTERNAL_ITEM_QUANTITY => $quantity);
}
// Lookup Item SKUs, Names & Categories Ids
$actionsToLookupAllItems = array();
// Each item has 7 potential "ids" to lookup in the lookup table
$columnsInEachRow = 1 + 1 + self::MAXIMUM_PRODUCT_CATEGORIES;
foreach ($cleanedItems as $item) {
$actionsToLookup = array();
list($sku, $name, $category, $price, $quantity) = $item;
$actionsToLookup[] = array(trim($sku), Action::TYPE_ECOMMERCE_ITEM_SKU);
$actionsToLookup[] = array(trim($name), Action::TYPE_ECOMMERCE_ITEM_NAME);
// Only one category
if (!is_array($category)) {
$actionsToLookup[] = array(trim($category), Action::TYPE_ECOMMERCE_ITEM_CATEGORY);
} else {
$countCategories = 0;
foreach ($category as $productCategory) {
$productCategory = trim($productCategory);
if (empty($productCategory)) {
continue;
}
$countCategories++;
if ($countCategories > self::MAXIMUM_PRODUCT_CATEGORIES) {
break;
}
$actionsToLookup[] = array($productCategory, Action::TYPE_ECOMMERCE_ITEM_CATEGORY);
}
}
// Ensure that each row has the same number of columns, fill in the blanks
for ($i = count($actionsToLookup); $i < $columnsInEachRow; $i++) {
$actionsToLookup[] = array(false, Action::TYPE_ECOMMERCE_ITEM_CATEGORY);
}
$actionsToLookupAllItems = array_merge($actionsToLookupAllItems, $actionsToLookup);
}
$actionsLookedUp = TableLogAction::loadIdsAction($actionsToLookupAllItems);
// Replace SKU, name & category by their ID action
foreach ($cleanedItems as $index => &$item) {
// SKU
$item[0] = $actionsLookedUp[$index * $columnsInEachRow + 0];
// Name
$item[1] = $actionsLookedUp[$index * $columnsInEachRow + 1];
// Categories
$item[2] = $actionsLookedUp[$index * $columnsInEachRow + 2];
$item[3] = $actionsLookedUp[$index * $columnsInEachRow + 3];
$item[4] = $actionsLookedUp[$index * $columnsInEachRow + 4];
$item[5] = $actionsLookedUp[$index * $columnsInEachRow + 5];
$item[6] = $actionsLookedUp[$index * $columnsInEachRow + 6];
}
return $cleanedItems;
}