Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function store (&$data) {
- vRequest::vmCheckToken();
- if(!vmAccess::manager('product.edit')){
- vmError('You are not a vendor or administrator, storing of product cancelled');
- return FALSE;
- }
- if ($data and is_object($data)) {
- $data = get_object_vars($data);
- }
- $isChild = FALSE;
- if(!empty($data['isChild'])) $isChild = $data['isChild'];
- if (isset($data['intnotes'])) {
- $data['intnotes'] = trim ($data['intnotes']);
- }
- // Setup some place holders
- $product_data = $this->getTable ('products');
- $data['new'] = '1';
- if(!empty($data['virtuemart_product_id'])){
- $product_data -> load($data['virtuemart_product_id']);
- $data['new'] = '0';
- }
- if( (empty($data['virtuemart_product_id']) or empty($product_data->virtuemart_product_id)) and !vmAccess::manager('product.create')){
- vmWarn('Insufficient permission to create product');
- return false;
- }
- $vendorId = vmAccess::isSuperVendor();
- $vM = VmModel::getModel('vendor');
- $ven = $vM->getVendor($vendorId);
- if(VmConfig::get('multix','none')!='none' and !vmAccess::manager('core')){
- if($ven->max_products!=-1){
- $this->setGetCount (true);
- //$this->setDebugSql(true);
- parent::exeSortSearchListQuery(2,'virtuemart_product_id',' FROM #__virtuemart_products',' WHERE ( `virtuemart_vendor_id` = "'.$vendorId.'" AND `published`="1") ');
- $this->setGetCount (false);
- if($ven->max_products<($this->_total+1)){
- vmWarn('You are not allowed to create more than '.$ven->max_products.' products');
- return false;
- }
- }
- }
- if($ven->force_product_pattern>0 and empty($data['product_parent_id'])){
- $data['product_parent_id'] = $ven->force_product_pattern;
- }
- if(!vmAccess::manager('product.edit.state')){
- if( (empty($data['virtuemart_product_id']) or empty($product_data->virtuemart_product_id))){
- $data['published'] = 0;
- } else {
- $data['published'] = $product_data->published;
- }
- }
- //Set the decimals like product packaging
- foreach(self::$decimals as $decimal){
- if (array_key_exists ($decimal, $data)) {
- if(!empty($data[$decimal])){
- $data[$decimal] = str_replace(',','.',$data[$decimal]);
- //vmdebug('Store product '.$data['virtuemart_product_id'].', set $decimal '.$decimal.' = '.$data[$decimal]);
- } else {
- $data[$decimal] = null;
- $product_data->$decimal = null;
- //vmdebug('Store product '.$data['virtuemart_product_id'].', set $decimal '.$decimal.' = null');
- }
- }
- }
- //We prevent with this line, that someone is storing a product as its own parent
- if(!empty($product_data->product_parent_id) and $product_data->product_parent_id == $data['virtuemart_product_id']){
- $product_data->product_parent_id = 0;
- unset($data['product_parent_id']);
- }
- VmConfig::importVMPlugins('vmcustom');
- $dispatcher = JDispatcher::getInstance();
- $dispatcher->trigger('plgVmBeforeStoreProduct',array(&$data, &$product_data));
- $stored = $product_data->bindChecknStore ($data, false);
- if(!$stored ){
- vmError('You are not an administrator or the correct vendor, storing of product cancelled');
- return FALSE;
- }
- $this->_id = $data['virtuemart_product_id'] = (int)$product_data->virtuemart_product_id;
- if (empty($this->_id)) {
- vmError('Product not stored, no id');
- return FALSE;
- }
- //We may need to change this, the reason it is not in the other list of commands for parents
- if (!$isChild) {
- $modelCustomfields = VmModel::getModel ('Customfields');
- $modelCustomfields->storeProductCustomfields ('product', $data, $product_data->virtuemart_product_id);
- }
- // Get old IDS
- $old_price_ids = $this->loadProductPrices($this->_id,array(0),false);
- if (isset($data['mprices']['product_price']) and count($data['mprices']['product_price']) > 0){
- foreach($data['mprices']['product_price'] as $k => $product_price){
- $pricesToStore = array();
- $pricesToStore['virtuemart_product_id'] = $this->_id;
- $pricesToStore['virtuemart_product_price_id'] = (int)$data['mprices']['virtuemart_product_price_id'][$k];
- if (!$isChild){
- //$pricesToStore['basePrice'] = $data['mprices']['basePrice'][$k];
- $pricesToStore['product_override_price'] = $data['mprices']['product_override_price'][$k];
- $pricesToStore['override'] = isset($data['mprices']['override'][$k])?(int)$data['mprices']['override'][$k]:0;
- $pricesToStore['virtuemart_shoppergroup_id'] = (int)$data['mprices']['virtuemart_shoppergroup_id'][$k];
- $pricesToStore['product_tax_id'] = (int)$data['mprices']['product_tax_id'][$k];
- $pricesToStore['product_discount_id'] = (int)$data['mprices']['product_discount_id'][$k];
- $pricesToStore['product_currency'] = (int)$data['mprices']['product_currency'][$k];
- $pricesToStore['product_price_publish_up'] = $data['mprices']['product_price_publish_up'][$k];
- $pricesToStore['product_price_publish_down'] = $data['mprices']['product_price_publish_down'][$k];
- $pricesToStore['price_quantity_start'] = (int)$data['mprices']['price_quantity_start'][$k];
- $pricesToStore['price_quantity_end'] = (int)$data['mprices']['price_quantity_end'][$k];
- }
- if (!$isChild and isset($data['mprices']['use_desired_price'][$k]) and $data['mprices']['use_desired_price'][$k] == "1") {
- $calculator = calculationHelper::getInstance ();
- if(isset($data['mprices']['salesPrice'][$k])){
- $data['mprices']['salesPrice'][$k] = str_replace(array(',',' '),array('.',''),$data['mprices']['salesPrice'][$k]);
- }
- $pricesToStore['salesPrice'] = $data['mprices']['salesPrice'][$k];
- $pricesToStore['product_price'] = $data['mprices']['product_price'][$k] = $calculator->calculateCostprice ($this->_id, $pricesToStore);
- unset($data['mprices']['use_desired_price'][$k]);
- } else {
- if(isset($data['mprices']['product_price'][$k]) ){
- $pricesToStore['product_price'] = $data['mprices']['product_price'][$k];
- }
- }
- if ($isChild) $childPrices = $this->loadProductPrices($this->_id,array(0),false);
- if ((isset($pricesToStore['product_price']) and $pricesToStore['product_price']!='' and $pricesToStore['product_price']!=='0') || (isset($childPrices) and count($childPrices)>1)) {
- if ($isChild) {
- if(is_array($old_price_ids) and count($old_price_ids)>1){
- //We do not touch multiple child prices. Because in the parent list, we see no price, the gui is
- //missing to reflect the information properly.
- $pricesToStore = false;
- $old_price_ids = array();
- } else {
- unset($data['mprices']['product_override_price'][$k]);
- unset($pricesToStore['product_override_price']);
- unset($data['mprices']['override'][$k]);
- unset($pricesToStore['override']);
- }
- }
- if($pricesToStore){
- $toUnset = array();
- if (!empty($old_price_ids) and count($old_price_ids) ) {
- foreach($old_price_ids as $key => $oldprice){
- if($pricesToStore['virtuemart_product_price_id'] == $oldprice['virtuemart_product_price_id'] ){
- $pricesToStore = array_merge($oldprice,$pricesToStore);
- $toUnset[] = $key;
- }
- }
- }
- $this->updateXrefAndChildTables ($pricesToStore, 'product_prices',$isChild);
- foreach($toUnset as $key){
- unset( $old_price_ids[ $key ] );
- }
- }
- }
- }
- }
- if (!empty($old_price_ids) and count($old_price_ids) ) {
- $oldPriceIdsSql = array();
- foreach($old_price_ids as $oldPride){
- $oldPriceIdsSql[] = $oldPride['virtuemart_product_price_id'];
- }
- $db = JFactory::getDbo();
- // delete old unused Prices
- $db->setQuery( 'DELETE FROM `#__virtuemart_product_prices` WHERE `virtuemart_product_price_id` in ("'.implode('","', $oldPriceIdsSql ).'") ');
- $db->execute();
- $err = $db->getErrorMsg();
- if(!empty($err)){
- vmWarn('In store prodcut, deleting old price error',$err);
- }
- }
- if (!empty($data['childs'])) {
- foreach ($data['childs'] as $productId => $child) {
- if(empty($productId)) continue;
- if($productId!=$data['virtuemart_product_id']){
- if(empty($child['product_parent_id'])) $child['product_parent_id'] = $data['virtuemart_product_id'];
- $child['virtuemart_product_id'] = $productId;
- if(!empty($child['product_parent_id']) and $child['product_parent_id'] == $child['virtuemart_product_id']){
- $child['product_parent_id'] = 0;
- }
- $child['isChild'] = $this->_id;
- $this->store ($child);
- }
- }
- }
- if (!$isChild) {
- $data = $this->updateXrefAndChildTables ($data, 'product_shoppergroups');
- $data = $this->updateXrefAndChildTables ($data, 'product_manufacturers');
- $storeCats = true;
- if (empty($data['categories']) or (!empty($data['categories'][0]) and $data['categories'][0]!="-2")){
- $storeCats = true;
- }
- if($storeCats){
- if (!empty($data['categories']) && count ($data['categories']) > 0) {
- if(VmConfig::get('multix','none')!='none' and !vmAccess::manager('managevendors')){
- if($ven->max_cats_per_product>=0){
- while($ven->max_cats_per_product<count($data['categories'])){
- array_pop($data['categories']);
- }
- }
- }
- $data['virtuemart_category_id'] = $data['categories'];
- } else {
- $data['virtuemart_category_id'] = array();
- }
- $data = $this->updateXrefAndChildTables ($data, 'product_categories');
- }
- // Update waiting list
- //TODO what is this doing?
- if (!empty($data['notify_users'])) {
- if ($data['product_in_stock'] > 0 && $data['notify_users'] == '1') {
- $waitinglist = VmModel::getModel ('Waitinglist');
- $waitinglist->notifyList ($data['virtuemart_product_id']);
- }
- }
- // Process the images
- $mediaModel = VmModel::getModel ('Media');
- $mediaModel->storeMedia ($data, 'product');
- }
- $cache = VmConfig::getCache('com_virtuemart_cat_manus','callback');
- $cache->clean();
- $dispatcher->trigger('plgVmAfterStoreProduct',array(&$data, &$product_data));
- return $product_data->virtuemart_product_id;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement