private function saveCategories($subCategoryArray, $prefix = '', $masterCategory = '') { if (empty($subCategoryArray)) { return array(); } else { //decode tree array into actions that we need to do after tree save $subCategoryIds = array(); $productIds = array(); $optionSequenceIds = array(); $updateDescriptionsIds = array(); foreach ($subCategoryArray as $number => $subCategoryItem) { if ($subCategoryItem['isProduct'] && $masterCategory) { $productIds[] = array($subCategoryItem['key'], $subCategoryItem['title'], isset($subCategotyItem['wasActive']) ? true : false); if ($subCategoryItem['children'] != '') { $optionSequenceIds = array_merge($this->saveOptions($subCategoryItem['children'], $subCategoryItem['key'], $updateDescriptionsIds), $optionSequenceIds); } continue; } // initialize the category correctly if it does not exist yet if (($subCategory = oqc_Category::getFromId($subCategoryItem['key'])) == null) { $subCategory = new oqc_Category(); } $subCategory->number = $prefix . ($number + 1); $subCategory->name = $subCategoryItem['title']; $subCategory->description = isset($_POST['categoryDescription_' . $subCategoryItem['key']]) ? $_POST['categoryDescription_' . $subCategoryItem['key']] : ""; if ($subCategoryItem['children'] != '') { $subCategory->subcategories = implode(" ", $this->saveCategories($subCategoryItem['children'], $prefix . ($number + 1) . '.', $subCategoryItem['key'])); } else { $subCategory->subcategories = ""; } $subCategory->catalog_id = $this->bean->id; $subCategory->save(); // put id of this subcategory into the array $subCategoryIds if (array_search($subCategory->id, $subCategoryIds) === FALSE) { $subCategoryIds[] = $subCategory->id; } } //2.2RC1 now process Products and options that are in this particular tree level //1. update Product descriptions if was Active, update ordering of Products, update title of products is was Active, update Product category (it might be changed is was Active //2. For options update ordering string and description if wasActive //$GLOBALS['log']->error("product catalog subcategories: ". var_export($subCategoryIds,true)); //$GLOBALS['log']->error("product catalog products: ". var_export($productIds,true)); //$GLOBALS['log']->error("product catalog options sequences: ". var_export($optionSequenceIds,true)); //$GLOBALS['log']->error("product catalog descriptions: ". var_export($updateDescriptionsIds,true)); if (!empty($productIds)) { foreach ($productIds as $number => $productId) { $product = new oqc_Product(); if ($product->retrieve($productId[0])) { $product->name = $productId[1]; $product->description = isset($_POST['categoryDescription_' . $productId[0]]) ? $_POST['categoryDescription_' . $productId[0]] : ""; if (array_key_exists($productId[0], $optionSequenceIds)) { $product->optionssequence = $optionSequenceIds[$productId[0]]; unset($optionSequenceIds[$productId[0]]); } $product->catalog_position = $number + 1; $product->relatedcategory_id = $masterCategory; $product->save(); } } } if (!empty($optionSequenceIds)) { foreach ($optionSequenceIds as $key => $optionSequenceId) { $option = new oqc_Product(); if ($option->retrieve($key)) { if (isset($_POST['categoryDescription_' . $key]) && array_search($key, $updateDescriptionsIds)) { $option->description = $_POST['categoryDescription_' . $key]; unset($updateDescriptionsIds[array_search($key, $updateDescriptionsIds)]); } if (array_key_exists($key, $optionSequenceIds)) { $option->optionssequence = $optionSequenceIds[$key]; unset($optionSequenceIds[$key]); } // $option->catalog_position = $number+1; // $option->relatedcategory_id = $key; $option->save(); } } } if (!empty($updateDescriptionsIds)) { foreach ($updateDescriptionsIds as $updateDescriptionsId) { if (isset($_POST['categoryDescription_' . $updateDescriptionsId])) { $option = new oqc_Product(); if ($option->retrieve($updateDescriptionsId)) { $option->description = $_POST['categoryDescription_' . $updateDescriptionsId]; // unset($updateDescriptionsIds[array_search($key ,$updateDescriptionsIds)]; $option->save(); } } } } return $subCategoryIds; } }