/** * This function save/update attribute. * @param :: integer attributeOptions_id * @return : * */ public function store($data, $sku = '') { global $mainframe; $mainframe = JFactory::getApplication(); // OnBeforeq2cAttributeSave $dispatcher = JDispatcher::getInstance(); JPluginHelper::importPlugin('system'); $result = $dispatcher->trigger('OnBeforeq2cAttributeSave', array($data)); if (!empty($result[0])) { $data = $result[0]; } // to store attribute name in #__kart_itemattributes table // Field type= textbox then there will not be any options $userFields = array(); $userFields[] = 'Textbox'; $userdata = !empty($data['fieldType']) && in_array($data['fieldType'], $userFields) ? 1 : 0; // 0: mean dont have options if (empty($userdata)) { $ind = 0; if (!empty($data['attri_opt'])) { foreach ($data['attri_opt'] as $key => $att_options) { $ind = $key; } } // if 0 menas :atleast one option is present then only save attribute if (!$this->validateAttributeOption($data['attri_opt'], $ind)) { //return false; } } else { } $DelTask = 0; if (!empty($data['delete_attri'])) { $DelTask = 1; $data = $this->deleteAttributeOption($data); } if (empty($userdata)) { $data = $this->removeInvalideOption($data); // if Options r not present if (count($data['attri_opt']) == 0) { $this->noOptionThenDelAttr($data['attri_id']); $return = $DelTask == 1 ? true : false; // if true when delete task return $return; } } $row = new stdClass(); if ($data['attri_name']) { //$row = new stdClass; $row->itemattribute_name = $data['attri_name']; $row->attributeFieldType = $data['fieldType']; //1. store attribute name if (isset($data['iscompulsary_attr'])) { $row->attribute_compulsary = true; } else { $row->attribute_compulsary = false; } // While updating the attribute if (!empty($data['attri_id'])) { //@TODO VM: if fieldtype = text then delete all attribute option (in db) $row->itemattribute_id = $data['attri_id']; if (!$this->_db->updateObject('#__kart_itemattributes', $row, "itemattribute_id")) { echo $this->_db->stderr(); return false; } /*if ATTRIB IS GOING TO UPDATE THEN COMPARE (DB OPTIONS AND POST OPTION) ,DEL EXTRA OPTION FROM DB ONLY */ $att_option_ids = array(); //GETTING OPTION ID'S ARRAY foreach ($data['attri_opt'] as $option) { $att_option_ids[] = $option['id']; } //DELETING EXTRA OPTIONS if (!empty($att_option_ids)) { $productHelper = new productHelper(); $productHelper->deleteExtraAttributeOptions($data['attri_id'], $att_option_ids); } } else { // for new attribute //load Attributes model // REQUIRE WHEN CALLED FROM BACKEND $comquick2cartHelper = new comquick2cartHelper(); $path = JPATH_SITE . DS . 'components' . DS . 'com_quick2cart' . DS . 'models' . DS . 'cart.php'; $Quick2cartModelcart = $comquick2cartHelper->loadqtcClass($path, "Quick2cartModelcart"); //$Quick2cartModelcart = new Quick2cartModelcart; if (!empty($data['item_id'])) { $item_id = $data['item_id']; } elseif (!empty($data['product_id']) && !empty($data['client'])) { $item_id = $Quick2cartModelcart->getitemid($data['product_id'], $data['client']); } else { if (!empty($data['sku'])) { $item_id = $Quick2cartModelcart->getitemid(0, $data['client'], $data['sku']); } } $row->item_id = $item_id; /*$row->parent = $data['client']; $row->product_id = $data['product_id'];*/ //~ // If checked //~ if (isset($data['iscompulsary_attr'])) //~ { //~ $row->attribute_compulsary = true; //~ } //~ else //~ { //~ $row->attribute_compulsary = false; //~ } if (!$this->_db->insertObject('#__kart_itemattributes', $row, 'itemattribute_id')) { echo $this->_db->stderr(); return false; } } } if (!empty($userdata)) { // add extra option for user data $quick2cartModelAttributes = new quick2cartModelAttributes(); // $quick2cartModelAttributes->addDefaultEntryForUserFields($row->itemattribute_id,$row->itemattribute_name); $option = array(); /*$option['itemattribute_id'] = $row->itemattribute_id; $option['itemattributeoption_name'] = $row->itemattribute_name; $option['itemattributeoption_price'] = 0; $option['itemattributeoption_prefix'] = '+'; $option['ordering'] = 1;*/ $op = $data['attri_opt'][0]; $op['name'] = $row->itemattribute_name; //$option['itemattributeoption_price'] = 0; $op['prefix'] = '+'; foreach ($op['currency'] as $key => $curr) { $op['currency'][$key] = 0; } $data['attri_opt'][0] = $op; // set default option to data // return true; } //2. to store attribute option in #__itemattributeoptions_ table foreach ($data['attri_opt'] as $key => $attri_opt) { if ($attri_opt['name'] && $attri_opt['currency'] && $attri_opt['prefix'] && $attri_opt['order']) { $opt = new stdClass(); $opt->itemattributeoption_name = $attri_opt['name']; $currkeys = array_keys($attri_opt['currency']); //make array of currency keys $currkey = $currkeys[0]; $opt->itemattributeoption_price = $attri_opt['currency'][$currkey]; $opt->itemattributeoption_prefix = $attri_opt['prefix']; $opt->ordering = $attri_opt['order']; // UPDATING ATT OPTION if (!empty($attri_opt['id'])) { // update attribute option $opt->itemattributeoption_id = $attri_opt['id']; if (!$this->_db->updateObject('#__kart_itemattributeoptions', $opt, 'itemattributeoption_id')) { echo $this->_db->stderr(); return false; } else { // After success update table #__kart_option_currency $db = JFactory::getDBO(); $query = "select * from `#__kart_option_currency` where itemattributeoption_id=" . (int) $opt->itemattributeoption_id; $db->setQuery($query); $result = $db->loadAssocList(); if ($result) { foreach ($attri_opt['currency'] as $key => $value) { $flag = -1; $updateid = -1; // To check currency field is present or not for that product foreach ($result as $dbkey => $dbvalue) { if ($key == $dbvalue['currency']) { $flag = 1; //print"FGlag =" . $flag; $updateid = $dbvalue['id']; break; } } if ($flag == 1 && $updateid) { $updateobj = new stdClass(); $updateobj->id = $updateid; $updateobj->price = $value; if (!$this->_db->updateObject('#__kart_option_currency', $updateobj, 'id')) { echo $this->_db->stderr(); return false; } } else { $updateobj = new stdClass(); $updateobj->id = NULL; $updateobj->itemattributeoption_id = (int) $opt->itemattributeoption_id; $updateobj->currency = $key; $updateobj->price = $value; if (!$this->_db->insertObject('#__kart_option_currency', $updateobj, 'id')) { //if error to insert echo $this->_db->stderr(); return false; } } } } else { foreach ($attri_opt['currency'] as $key => $value) { $currobj = new stdClass(); $currobj->id = NULL; $currobj->itemattributeoption_id = (int) $opt->itemattributeoption_id; $currobj->currency = $key; $currobj->price = $value; if (!$this->_db->insertObject('#__kart_option_currency', $currobj, 'id')) { echo $this->_db->stderr(); return false; } } } } } else { // adding new ATT OPTION $opt->itemattribute_id = $row->itemattribute_id; if (!$this->_db->insertObject('#__kart_itemattributeoptions', $opt, 'itemattributeoption_id')) { //if error to insert echo $this->_db->stderr(); return false; } else { // if INSERTED AND NOT ERROR THEN STORE OPTION CURRENCY //add attribute option to DB $insert_id = $opt->itemattributeoption_id; // get last inserted id foreach ($attri_opt['currency'] as $key => $value) { $option = new stdClass(); $option->id = NULL; $option->itemattributeoption_id = (int) $insert_id; $option->currency = $key; $option->price = (double) $value; if (!$this->_db->insertObject('#__kart_option_currency', $option, 'id')) { //if error to insert echo $this->_db->stderr(); return false; } } } } } } // end of foreach option return true; //$mainframe->redirect('index.php?option=com_broadcast&view=settings', $msg); }