/** * Do the migration * * @return array */ function _migrate($datas) { $queries = array(); $results = array(); $n = 0; // Loop though the rows foreach ($datas as $data) { // Check for product_name. Explode() could have generated an empty row if (!empty($data['product_name'])) { $isNew = false; if (!$data['product_id']) { $data['product_id'] = 0; $isNew = true; } JTable::addIncludePath(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_citruscart' . DS . 'tables'); $product = JTable::getInstance('Products', 'CitruscartTable'); if (!$isNew) { if (!$product->load($data['product_id'])) { $isNew = true; $data['product_id'] = 0; } } // If is a new product, use product->create() if ($isNew) { $product->product_price = 0; $product->product_quantity = 0; $product->bind($data); if ($product->product_full_image) { Citruscart::load('CitruscartFile', 'library.file'); // Do the same cleaning to the image title that the image helper does $name = explode('.', $product->product_full_image); $name = CitruscartFile::cleanTitle($name[0]) . '.' . $name[count($name) - 1]; $product->product_full_image = $name; } $product->create(); $this->_migrateAttributes($product->product_id, $data['product_attributes']); } else { $product->bind($data); //check if normal price exists Citruscart::load("CitruscartHelperProduct", 'helpers.product'); $prices = CitruscartHelperProduct::getPrices($product->product_id); $quantities = CitruscartHelperProduct::getProductQuantities($product->product_id); if ($product->save()) { $product->product_id = $product->id; // New price? if (empty($prices)) { // set price if new or no prices set $price = JTable::getInstance('Productprices', 'CitruscartTable'); $price->product_id = $product->id; $price->product_price = $data['product_price']; $price->group_id = Citruscart::getInstance()->get('default_user_group', '1'); $price->save(); } else { // set price if new or no prices set $price = JTable::getInstance('Productprices', 'CitruscartTable'); $price->load($prices[0]->product_price_id); $price->product_price = $data['product_price']; $price->group_id = Citruscart::getInstance()->get('default_user_group', '1'); $price->save(); } // New quantity? if (empty($quantities)) { // save default quantity $quantity = JTable::getInstance('Productquantities', 'CitruscartTable'); $quantity->product_id = $product->id; $quantity->quantity = $data['product_quantity']; $quantity->save(); } else { // save default quantity $quantity = JTable::getInstance('Productquantities', 'CitruscartTable'); $quantity->load($quantities[0]->productquantity_id); $quantity->product_id = $product->id; $quantity->quantity = $data['product_quantity']; $quantity->save(); } } } // at this point, the product is saved, so now do additional relationships // such as categories if (!empty($product->product_id) && !empty($data['product_categories'])) { foreach ($data['product_categories'] as $category_id) { // This is probably not the best way to do it // Numeric = id, string = category name if (!is_numeric($category_id)) { // check for existance JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_citruscart' . DS . 'models'); $model = JModelLegacy::getInstance('Categories', 'CitruscartModel'); $model->setState('filter_name', $category_id); $matches = $model->getList(); $matched = false; if ($matches) { foreach ($matches as $match) { // is a perfect match? if (strtolower($category_id) == strtolower($match->category_name)) { $category_id = $match->category_id; $matched = true; } } } // Not matched, create category if (!$matched) { $category = JTable::getInstance('Categories', 'CitruscartTable'); $category->category_name = $category_id; $category->parent_id = 1; $category->category_enabled = 1; $category->save(); $category_id = $category->category_id; } } // save xref in every case $xref = JTable::getInstance('ProductCategories', 'CitruscartTable'); $xref->product_id = $product->product_id; $xref->category_id = $category_id; $xref->save(); } } $results[$n]->title = $product->product_name; $results[$n]->query = ""; $results[$n]->error = implode('\\n', $product->getErrors()); $results[$n]->affectedRows = 1; $n++; $this->_migrateImages($product->product_id, $data['product_images'], $results); } } return $results; }
private function _migrateProduct($data) { // Check for product_name. if (!empty($data->product_name)) { $isNew = false; if (!empty($data->product_id)) { if ((int) $data->product_id) { $data['product_id'] = 0; $isNew = true; } } JTable::addIncludePath(JPATH_ADMINISTRATOR . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_citruscart' . DIRECTORY_SEPARATOR . 'tables'); $product = JTable::getInstance('Products', 'CitruscartTable'); if (!$isNew) { if (!$product->load($data['product_id'])) { $isNew = true; $data['product_id'] = 0; } } // If is a new product, use product->create() if ($isNew) { $product->product_price = 0; $product->product_quantity = 0; $product->bind($this->simpleXml2Array($data)); if ($product->product_full_image) { Citruscart::load('CitruscartFile', 'library.file'); // Do the same cleaning to the image title that the image helper does $name = explode('.', $product->product_full_image); $name = CitruscartFile::cleanTitle($name[0]) . '.' . $name[count($name) - 1]; $product->product_full_image = $name; } $product->create(); $this->_migrateAttributes($product->product_id, $data->product_attributes); $this->_migrateCategories($product->product_id, $data->product_categories); $this->_migrateFiles($product->product_id, $data->product_files); $this->_migrateImages($product->product_id, $data->product_images); $this->_migrateChildren($product->product_id, $data->product_children); $this->_migrateRelated($product->product_id, $data->related_products); $this->_migrateCustomFields($product->product_id, $data->product_custom_fields); } else { $product->bind($this->simpleXml2Array($data)); //check if normal price exists Citruscart::load("CitruscartHelperProduct", 'helpers.product'); $prices = CitruscartHelperProduct::getPrices($product->product_id); $quantities = CitruscartHelperProduct::getProductQuantities($product->product_id); if ($product->save()) { $product->product_id = $product->id; // New price? if (empty($prices)) { // set price if new or no prices set $price = JTable::getInstance('Productprices', 'CitruscartTable'); $price->product_id = $product->id; $price->product_price = (string) $data->product_price; $price->group_id = Citruscart::getInstance()->get('default_user_group', '1'); $price->save(); } else { // set price if new or no prices set $price = JTable::getInstance('Productprices', 'CitruscartTable'); $price->load($prices[0]->product_price_id); $price->product_price = (string) $data->product_price; $price->group_id = Citruscart::getInstance()->get('default_user_group', '1'); $price->save(); } // New quantity? if (empty($quantities)) { // save default quantity $quantity = JTable::getInstance('Productquantities', 'CitruscartTable'); $quantity->product_id = $product->id; $quantity->quantity = (string) $data->product_quantity; $quantity->save(); } else { // save default quantity $quantity = JTable::getInstance('Productquantities', 'CitruscartTable'); $quantity->load($quantities[0]->productquantity_id); $quantity->product_id = $product->id; $quantity->quantity = (string) $data->product_quantity; $quantity->save(); } } } return $product; } return null; }
/** * Updates a product and its related informations (price, quantity & categories) * The price will be created from the $this->product_price property * The quantity will be created from the $this->product_quantity property * The categories will be created from the $this->product_category property */ function update() { // If this product is already stored, we shouldn't create the product! if (!$this->product_id) { $this->setError(JText::_('COM_CITRUSCART_YOU_CANNOT_UPDATE_A_NON_EXISTING_PRODUCT')); return false; } $product_price = $this->product_price; $product_quantity = $this->product_quantity; $product_categories = $this->product_category; if (!is_array($product_categories)) { $product_categories = array($product_categories); } unset($this->product_price); unset($this->product_quantity); unset($this->product_category); // Save the product First $success = $this->save(); if ($success) { // now the price if ($product_price) { // Load the default price Citruscart::load('CitruscartHelperProduct', 'helpers.product'); $prices = CitruscartHelperProduct::getPrices($this->product_id); if (count($prices)) { $price_id = $prices[0]->product_price_id; } else { $price_id = 0; } Citruscart::load('CitruscartTableProductPrices', 'tables.productprices'); $price = JTable::getInstance('ProductPrices', 'CitruscartTable'); // load the price if it does exist if ($price_id) { $price->load($price_id); } // else just save it as a new price $price->product_id = $this->product_id; $price->product_price = $product_price; $price->group_id = Citruscart::getInstance()->get('default_user_group', '1'); $success = $price->save(); if (!$success) { $this->setError($price->getError()); return false; } } // now the quantities if ($product_quantity) { // Load the default quantity Citruscart::load('CitruscartHelperProduct', 'helpers.product'); $quantities = CitruscartHelperProduct::getProductQuantities($this->product_id); if (count($quantities)) { $quantity_id = $quantities[0]->productquantity_id; } else { $quantity_id = 0; } Citruscart::load('CitruscartTableProductQuantities', 'tables.productquantities'); $quantity = JTable::getInstance('ProductQuantities', 'CitruscartTable'); // load the quantity if it does exist if ($quantity_id) { $quantity->load($quantity_id); } // else just save it as a new quantity $quantity->product_id = $this->product_id; $quantity->quantity = $product_quantity; $success = $quantity->save(); if (!$success) { $this->setError($quantity->getError()); return false; } } // now the categories if ($product_categories) { foreach ($product_categories as $product_category) { // This is probably not the best way to do it // Numeric = id, string = category name if (!is_numeric($product_category)) { // check for existance JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_citruscart' . DIRECTORY_SEPARATOR . 'models'); $model = JModelLegacy::getInstance('Categories', 'CitruscartModel'); $model->setState('filter_name', $product_category); $matches = $model->getList(); $matched = false; if ($matches) { foreach ($matches as $match) { // is a perfect match? if (strtolower($product_category) == strtolower($match->category_name)) { $product_category = $match->category_id; $matched = true; } } } // Not matched, create category if (!$matched) { Citruscart::load('CitruscartTableCategories', 'tables.categories'); $category = JTable::getInstance('Categories', 'CitruscartTable'); $category->category_name = $product_category; $category->parent_id = 1; $category->category_enabled = 1; $category->save(); $product_category = $category->category_id; } } // save xref in every case Citruscart::load('CitruscartTableProductCategories', 'tables.productcategories'); $xref = JTable::getInstance('ProductCategories', 'CitruscartTable'); $xref->product_id = $this->product_id; $xref->category_id = $product_category; $xref->save(); } } } else { return false; } return true; }