/** * Method to get the attributes with options of the current products * @return array */ function getAttributes() { $finalAttributes = array(); if ($this->_view != 'products' || empty($this->_products) || !$this->_params->get('filter_attributes')) { return $finalAttributes; } Citruscart::load('CitruscartHelperProduct', 'helpers.product'); //check if we have pids //else get the pids from $this->_products if (empty($this->_pids)) { $pids = array(); foreach ($this->_products as $item) { $pids[] = $item->product_id; } $this->_pids = $pids; } //retun if we dont have pids if (empty($this->_pids)) { return $finalAttributes; } //check if we CitruscartQuery class exist if (!class_exists('CitruscartQuery')) { Citruscart::load('CitruscartQuery', 'library.query'); } //get the attributes of the current products $query = new CitruscartQuery(); $query->select('tbl.product_id'); $query->select('tbl.productattribute_name'); $query->select('tbl.productattribute_id'); $query->from('#__citruscart_productattributes AS tbl'); //explode first because mysql needs the attribute ids inside a quote $excluded_attributes = explode(',', $this->_params->get('excluded_attributes')); $query->where("tbl.productattribute_id NOT IN ('" . implode("', '", $excluded_attributes) . "')"); $query->where("tbl.product_id IN ('" . implode("', '", $this->_pids) . "')"); $this->_db->setQuery((string) $query); $attributes = $this->_db->loadObjectList(); //return if no available attributes if (empty($attributes)) { return $finalAttributes; } $newAttributes = array(); //loop to get the available options of the attribute foreach ($attributes as $attribute) { $options = CitruscartHelperProduct::getAttributeOptionsObjects($attribute->productattribute_id); foreach ($options as $option) { $option->product_id = $attribute->product_id; $option->attributename = $attribute->productattribute_name; $this->_options[$option->productattributeoption_id] = $option; } $attr_name = $attribute->productattribute_name; if ($this->_params->get('attributes_case_insensitive', 1)) { $attr_name = strtolower($attribute->productattribute_name); } if (isset($newAttributes[$attr_name])) { $newAttributes[$attr_name] = array_merge($newAttributes[$attr_name], $options); } else { $newAttributes[$attr_name] = $options; } } $link = $this->_link . '&filter_category=' . $this->_filter_category; if (empty($this->_filter_attribute_set)) { $session = JFactory::getSession(); $cleanO = array(); $cleanO[$this->_filter_category] = $this->_options; $session->set('options', $cleanO, 'Citruscart_layered_nav'); } $options_ids = !empty($this->_filter_option_set) ? explode(',', $this->_filter_option_set) : array(); $finalAttributes = array(); foreach ($newAttributes as $key => $options) { foreach ($options as $option) { $addoptionset = ''; if (!in_array($option->productattributeoption_id, $options_ids)) { if (isset($finalAttributes[$key][$option->productattributeoption_name])) { $addoptionset = ',' . $option->productattributeoption_id; $finalAttributes[$key][$option->productattributeoption_name]->products[] = $option->product_id; $finalAttributes[$key][$option->productattributeoption_name]->attributes[] = $option->productattribute_id; } else { $finalAttributes[$key][$option->productattributeoption_name] = new stdClass(); $newoption_set = count($options_ids) ? $this->_filter_option_set . ',' . $option->productattributeoption_id : $option->productattributeoption_id; $finalAttributes[$key][$option->productattributeoption_name]->products = array($option->product_id); $finalAttributes[$key][$option->productattributeoption_name]->attributes = array($option->productattribute_id); } $finalAttributes[$key][$option->productattributeoption_name]->link = $link . '&filter_option_set=' . $newoption_set . $addoptionset; } } } return $finalAttributes; }
/** * Saves an item and redirects based on task * @return void */ function save() { $app = JFactory::getApplication(); $task = $app->input->getString('task'); $post = $app->input->getArray($_POST); //print_r($post); exit; $model = $this->getModel($this->get('suffix')); $isSaveAs = false; $row = $model->getTable(); $row->load($model->getId()); // to set the Product Id $row->product_id = isset($post['id']) ? $post['id'] : ""; $row->bind($post); $row->product_description = $app->input->get('product_description', '', 'RAW'); $row->product_description_short = $app->input->get('product_description_short', '', 'RAW'); // set the id as 0 for new entry if ($task == "save_as") { unset($row); // load WITHOUT EAV! otherwise the save will fail $row = $model->getTable(); $row->load($model->getId(), true, false); $row->bind($app->input->getArray($_POST)); $row->product_description = $app->input->get('product_description', '', 'RAW'); $row->product_description_short = $app->input->get('product_description_short', '', 'RAW'); $isSaveAs = true; $oldProductImagePath = $row->getImagePath(); $pk = $row->getKeyName(); $oldPk = $row->{$pk}; // these get reset $row->{$pk} = 0; $row->product_images_path = ''; $row->product_rating = ''; $row->product_comments = ''; } $row->_isNew = empty($row->product_id); $fieldname = 'product_full_image_new'; //$userfiles = JRequest::getVar( $fieldname, '', 'files', 'array' ); $userfiles = $app->input->post->get($fieldname, '', 'files', 'array'); // save the integrations $row = $this->prepareParameters($row); //check if normal price exists Citruscart::load("CitruscartHelperProduct", 'helpers.product'); if ($isSaveAs) { // and the prices $prices = CitruscartHelperProduct::getPrices($oldPk); } else { $prices = CitruscartHelperProduct::getPrices($row->product_id); } if ($row->save()) { $row->product_id = $row->id; $model->setId($row->id); $this->messagetype = 'message'; $this->message = JText::_('COM_CITRUSCART_SAVED'); // check it's new entry or empty price but not save as if (($row->_isNew || empty($prices)) && !$isSaveAs) { // set price if new or no prices set $price = JTable::getInstance('Productprices', 'CitruscartTable'); $price->product_id = $row->id; $price->product_price = $app->input->get('product_price'); $price->group_id = Citruscart::getInstance()->get('default_user_group', '1'); if (!$price->save()) { $this->messagetype = 'notice'; $this->message .= " :: " . $price->getError(); } } if ($row->_isNew && !$isSaveAs) { // set category $category = JTable::getInstance('Productcategories', 'CitruscartTable'); $category->product_id = $row->id; $category->category_id = $app->input->getInt('category_id'); if (!$category->save()) { $this->messagetype = 'notice'; $this->message .= " :: " . $category->getError(); } // save default quantity $quantity = JTable::getInstance('Productquantities', 'CitruscartTable'); $quantity->product_id = $row->id; $quantity->quantity = $app->input->getInt('product_quantity'); if (!$quantity->save()) { $this->messagetype = 'notice'; $this->message .= " :: " . $quantity->getError(); } } if ($isSaveAs) { // set price when cloning $priceTable = JTable::getInstance('Productprices', 'CitruscartTable'); foreach ($prices as $price) { $priceTable->product_id = $row->id; $priceTable->product_price = $price->product_price; $priceTable->product_price_startdate = $price->product_price_startdate; $priceTable->product_price_enddate = $price->product_price_enddate; $priceTable->created_date = $price->created_date; $priceTable->modified_date = $price->modified_date; $priceTable->group_id = $price->group_id; $priceTable->price_quantity_start = $price->price_quantity_start; $priceTable->price_quantity_end = $price->price_quantity_end; if (!$priceTable->save()) { $this->messagetype = 'notice'; $this->message .= " :: " . $priceTable->getError(); } } // set category $categoryTable = JTable::getInstance('Productcategories', 'CitruscartTable'); $categories = CitruscartHelperProduct::getCategories($oldPk); foreach ($categories as $category) { $categoryTable->product_id = $row->id; $categoryTable->category_id = $category; if (!$categoryTable->save()) { $this->messagetype = 'notice'; $this->message .= " :: " . $categoryTable->getError(); } } // TODO Save Attributes // An array to map attribute id old attribute id are as key and new attribute id are as value $attrbuteMappingArray = array(); $attrbuteParentMappingArray = array(); $attributes = CitruscartHelperProduct::getAttributes($oldPk); foreach ($attributes as $attribute) { $attributeTable = JTable::getInstance('ProductAttributes', 'CitruscartTable'); $attributeTable->productattribute_name = $attribute->productattribute_name; $attributeTable->product_id = $row->id; $attributeTable->ordering = $attribute->ordering; if ($attributeTable->save()) { $attrbuteMappingArray[$attribute->productattribute_id] = $attributeTable->productattribute_id; $attrbuteParentMappingArray[$attributeTable->productattribute_id] = $attribute->parent_productattributeoption_id; } else { $this->messagetype = 'notice'; $this->message .= " :: " . $attributeTable->getError(); } } // set Attribute options $attrbuteOptionsMappingArray = array(); foreach ($attrbuteMappingArray as $oldAttrbuteId => $newAttributeId) { // set Attribute options $options = CitruscartHelperProduct::getAttributeOptionsObjects($oldAttrbuteId); foreach ($options as $option) { $attributeOptionsTable = JTable::getInstance('ProductAttributeOptions', 'CitruscartTable'); $attributeOptionsTable->productattribute_id = $newAttributeId; $attributeOptionsTable->productattributeoption_name = $option->productattributeoption_name; $attributeOptionsTable->productattributeoption_price = $option->productattributeoption_price; $attributeOptionsTable->productattributeoption_prefix = $option->productattributeoption_prefix; $attributeOptionsTable->productattributeoption_code = $option->productattributeoption_code; $attributeOptionsTable->ordering = $option->ordering; if ($attributeOptionsTable->save()) { $attrbuteOptionsMappingArray[$option->productattributeoption_id] = $attributeOptionsTable->productattributeoption_id; } else { $this->messagetype = 'notice'; $this->message .= " :: " . $attributeOptionsTable->getError(); } } // save parent relationship if ($attrbuteParentMappingArray[$newAttributeId]) { $attributeTable = JTable::getInstance('ProductAttributes', 'CitruscartTable'); $attributeTable->load($newAttributeId); $attributeTable->parent_productattributeoption_id = $attrbuteOptionsMappingArray[$attrbuteParentMappingArray[$newAttributeId]]; if (!$attributeTable->save()) { $this->messagetype = 'notice'; $this->message .= " :: " . $attributeTable->getError(); } } } // set quantity $quantityTable = JTable::getInstance('Productquantities', 'CitruscartTable'); $quantities = CitruscartHelperProduct::getProductQuantitiesObjects($oldPk); foreach ($quantities as $quantity) { $quantityTable->product_attributes = $quantity->product_attributes; $quantityTable->product_id = $row->id; $quantityTable->vendor_id = $quantity->vendor_id; $quantityTable->quantity = $quantity->quantity; $optionsCSV = $quantity->product_attributes; $options = explode(",", $optionsCSV); $newOptions = array(); foreach ($options as $option) { $newOptions[] = $attrbuteOptionsMappingArray[$option]; } $optionsCSV = implode(",", $newOptions); $quantityTable->product_attributes = $optionsCSV; if (!$quantityTable->save()) { $this->messagetype = 'notice'; $this->message .= " :: " . $quantityTable->getError(); } } // copy all gallery files jimport('joomla.filesystem.folder'); jimport('joomla.filesystem.file'); $galleryFiles = JFolder::files($oldProductImagePath); // get all gallery images if (count($galleryFiles)) { JFolder::create($row->getImagePath()); // create folder for images JFolder::create($row->getImagePath() . 'thumbs'); // create folder for thumbnails images for ($i = 0, $c = count($galleryFiles); $i < $c; $i++) { // copy only images with both original file and a corresponding thumbnail if (JFile::exists($oldProductImagePath . 'thumbs/' . $galleryFiles[$i]) && JFile::exists($oldProductImagePath . $galleryFiles[$i])) { JFile::copy($oldProductImagePath . $galleryFiles[$i], $row->getImagePath() . DS . $galleryFiles[$i]); JFile::copy($oldProductImagePath . 'thumbs/' . $galleryFiles[$i], $row->getImagePath() . '/thumbs/' . $galleryFiles[$i]); } } } // duplicate product files (only in db) $modelFiles = $this->getModel('productfiles'); $modelFiles->setState('filter_product', $oldPk); $listFiles = $modelFiles->getList(); if (count($listFiles)) { $row_file = JTable::getInstance('Productfiles', 'CitruscartTable'); for ($i = 0, $c = count($listFiles); $i < $c; $i++) { $row_file->bind($listFiles[$i]); // bind old data $row_file->productfile_id = 0; // will be set $row_file->product_id = $row->product_id; // use clone's ID $row_file->save(); // save the data } } // create duplicate connections for EAV custom fields JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_citruscart/models'); $model = JModelLegacy::getInstance('EavAttributes', 'CitruscartModel'); $model->setState('filter_entitytype', 'products'); $model->setState('filter_entityid', $oldPk); $listEAV = $model->getList(); $teav = $model->getTable(); if (is_array($listEAV)) { for ($i = 0, $c = count($listEAV); $i < $c; $i++) { $tblEAV = JTable::getInstance('EavAttributeEntities', 'CitruscartTable'); $tblEAV->eaventity_id = $row->product_id; $tblEAV->eaventity_type = 'products'; $tblEAV->eavattribute_id = $listEAV[$i]->eavattribute_id; $tblEAV->save(); // Clone the values too! $teav->load($listEAV[$i]->eavattribute_id); $value = CitruscartHelperEav::getAttributeValue($teav, 'products', $row->product_id); $newValue = JTable::getInstance('EavValues', 'CitruscartTable'); $newValue->setType($teav->eavattribute_type); $newValue->eavattribute_id = $listEAV[$i]->eavattribute_id; $newValue->eaventity_id = $row->product_id; // Store the value $newValue->eavvalue_value = $value; $newValue->eaventity_type = 'products'; $newValue->store(); } } } // Multiple images processing $i = 0; $error = false; while (!empty($userfiles['size'][$i])) { $dir = $row->getImagePath(true); if ($upload = $this->addimage($fieldname, $i, $dir)) { // The first One is the default (if there is no default yet) if ($i == 0 && (empty($row->product_full_image) || $row->product_full_image == '')) { $row->product_full_image = $upload->getPhysicalName(); // need to re-save in this instance // should we be storing or saving? $row->save(); } } else { $error = true; } $i++; } if ($error) { $this->messagetype = 'notice'; $this->message .= " :: " . $this->getError(); } $helper = new CitruscartHelperProduct(); $helper->onAfterSaveProducts($row); $model->clearCache(); JFactory::getApplication()->triggerEvent('onAfterSave' . $this->get('suffix'), array($row)); } else { $this->messagetype = 'notice'; $this->message = JText::_('COM_CITRUSCART_SAVE_FAILED') . " - " . $row->getError(); } $redirect = "index.php?option=com_citruscart"; switch ($task) { case "save_as": $redirect .= '&view=' . $this->get('suffix') . '&task=edit&id=' . $row->product_id; $this->message .= " - " . JText::_('COM_CITRUSCART_YOU_ARE_NOW_EDITING_NEW_PRODUCT'); break; case "saveprev": $redirect .= '&view=' . $this->get('suffix'); // get prev in list Citruscart::load("CitruscartHelperProduct", 'helpers.product'); $surrounding = CitruscartHelperProduct::getSurrounding($model->getId()); if (!empty($surrounding['prev'])) { $redirect .= '&task=edit&id=' . $surrounding['prev']; } break; case "savenext": $redirect .= '&view=' . $this->get('suffix'); // get next in list Citruscart::load("CitruscartHelperProduct", 'helpers.product'); $surrounding = CitruscartHelperProduct::getSurrounding($model->getId()); if (!empty($surrounding['next'])) { $redirect .= '&task=edit&id=' . $surrounding['next']; } break; case "savenew": $redirect .= '&view=' . $this->get('suffix') . '&task=add'; break; case "apply": $redirect .= '&view=' . $this->get('suffix') . '&task=edit&id=' . $model->getId(); break; case "save": default: $redirect .= "&view=" . $this->get('suffix'); break; } $redirect = JRoute::_($redirect, false); $this->setRedirect($redirect, $this->message, $this->messagetype); }