Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.06 KB | None | 0 0
  1. public function store (&$data) {
  2.  
  3. vRequest::vmCheckToken();
  4.  
  5. if(!vmAccess::manager('product.edit')){
  6.  
  7. vmError('You are not a vendor or administrator, storing of product cancelled');
  8.  
  9. return FALSE;
  10.  
  11. }
  12.  
  13. if ($data and is_object($data)) {
  14.  
  15. $data = get_object_vars($data);
  16.  
  17. }
  18.  
  19. $isChild = FALSE;
  20.  
  21. if(!empty($data['isChild'])) $isChild = $data['isChild'];
  22.  
  23. if (isset($data['intnotes'])) {
  24.  
  25. $data['intnotes'] = trim ($data['intnotes']);
  26.  
  27. }
  28.  
  29. // Setup some place holders
  30.  
  31. $product_data = $this->getTable ('products');
  32.  
  33. $data['new'] = '1';
  34.  
  35. if(!empty($data['virtuemart_product_id'])){
  36.  
  37. $product_data -> load($data['virtuemart_product_id']);
  38.  
  39. $data['new'] = '0';
  40.  
  41. }
  42.  
  43. if( (empty($data['virtuemart_product_id']) or empty($product_data->virtuemart_product_id)) and !vmAccess::manager('product.create')){
  44.  
  45. vmWarn('Insufficient permission to create product');
  46.  
  47. return false;
  48.  
  49. }
  50.  
  51. $vendorId = vmAccess::isSuperVendor();
  52.  
  53. $vM = VmModel::getModel('vendor');
  54.  
  55. $ven = $vM->getVendor($vendorId);
  56.  
  57. if(VmConfig::get('multix','none')!='none' and !vmAccess::manager('core')){
  58.  
  59. if($ven->max_products!=-1){
  60.  
  61. $this->setGetCount (true);
  62.  
  63. //$this->setDebugSql(true);
  64.  
  65. parent::exeSortSearchListQuery(2,'virtuemart_product_id',' FROM #__virtuemart_products',' WHERE ( `virtuemart_vendor_id` = "'.$vendorId.'" AND `published`="1") ');
  66.  
  67. $this->setGetCount (false);
  68.  
  69. if($ven->max_products<($this->_total+1)){
  70.  
  71. vmWarn('You are not allowed to create more than '.$ven->max_products.' products');
  72.  
  73. return false;
  74.  
  75. }
  76.  
  77. }
  78.  
  79. }
  80.  
  81. if($ven->force_product_pattern>0 and empty($data['product_parent_id'])){
  82.  
  83. $data['product_parent_id'] = $ven->force_product_pattern;
  84.  
  85. }
  86.  
  87. if(!vmAccess::manager('product.edit.state')){
  88.  
  89. if( (empty($data['virtuemart_product_id']) or empty($product_data->virtuemart_product_id))){
  90.  
  91. $data['published'] = 0;
  92.  
  93. } else {
  94.  
  95. $data['published'] = $product_data->published;
  96.  
  97. }
  98.  
  99. }
  100.  
  101. //Set the decimals like product packaging
  102.  
  103. foreach(self::$decimals as $decimal){
  104.  
  105. if (array_key_exists ($decimal, $data)) {
  106.  
  107. if(!empty($data[$decimal])){
  108.  
  109. $data[$decimal] = str_replace(',','.',$data[$decimal]);
  110.  
  111. //vmdebug('Store product '.$data['virtuemart_product_id'].', set $decimal '.$decimal.' = '.$data[$decimal]);
  112.  
  113. } else {
  114.  
  115. $data[$decimal] = null;
  116.  
  117. $product_data->$decimal = null;
  118.  
  119. //vmdebug('Store product '.$data['virtuemart_product_id'].', set $decimal '.$decimal.' = null');
  120.  
  121. }
  122.  
  123. }
  124.  
  125. }
  126.  
  127. //We prevent with this line, that someone is storing a product as its own parent
  128.  
  129. if(!empty($product_data->product_parent_id) and $product_data->product_parent_id == $data['virtuemart_product_id']){
  130.  
  131. $product_data->product_parent_id = 0;
  132.  
  133. unset($data['product_parent_id']);
  134.  
  135. }
  136.  
  137. VmConfig::importVMPlugins('vmcustom');
  138.  
  139. $dispatcher = JDispatcher::getInstance();
  140.  
  141. $dispatcher->trigger('plgVmBeforeStoreProduct',array(&$data, &$product_data));
  142.  
  143. $stored = $product_data->bindChecknStore ($data, false);
  144.  
  145. if(!$stored ){
  146.  
  147. vmError('You are not an administrator or the correct vendor, storing of product cancelled');
  148.  
  149. return FALSE;
  150.  
  151. }
  152.  
  153. $this->_id = $data['virtuemart_product_id'] = (int)$product_data->virtuemart_product_id;
  154.  
  155. if (empty($this->_id)) {
  156.  
  157. vmError('Product not stored, no id');
  158.  
  159. return FALSE;
  160.  
  161. }
  162.  
  163. //We may need to change this, the reason it is not in the other list of commands for parents
  164.  
  165. if (!$isChild) {
  166.  
  167. $modelCustomfields = VmModel::getModel ('Customfields');
  168.  
  169. $modelCustomfields->storeProductCustomfields ('product', $data, $product_data->virtuemart_product_id);
  170.  
  171. }
  172.  
  173. // Get old IDS
  174.  
  175. $old_price_ids = $this->loadProductPrices($this->_id,array(0),false);
  176.  
  177. if (isset($data['mprices']['product_price']) and count($data['mprices']['product_price']) > 0){
  178.  
  179. foreach($data['mprices']['product_price'] as $k => $product_price){
  180.  
  181. $pricesToStore = array();
  182.  
  183. $pricesToStore['virtuemart_product_id'] = $this->_id;
  184.  
  185. $pricesToStore['virtuemart_product_price_id'] = (int)$data['mprices']['virtuemart_product_price_id'][$k];
  186.  
  187. if (!$isChild){
  188.  
  189. //$pricesToStore['basePrice'] = $data['mprices']['basePrice'][$k];
  190.  
  191. $pricesToStore['product_override_price'] = $data['mprices']['product_override_price'][$k];
  192.  
  193. $pricesToStore['override'] = isset($data['mprices']['override'][$k])?(int)$data['mprices']['override'][$k]:0;
  194.  
  195. $pricesToStore['virtuemart_shoppergroup_id'] = (int)$data['mprices']['virtuemart_shoppergroup_id'][$k];
  196.  
  197. $pricesToStore['product_tax_id'] = (int)$data['mprices']['product_tax_id'][$k];
  198.  
  199. $pricesToStore['product_discount_id'] = (int)$data['mprices']['product_discount_id'][$k];
  200.  
  201. $pricesToStore['product_currency'] = (int)$data['mprices']['product_currency'][$k];
  202.  
  203. $pricesToStore['product_price_publish_up'] = $data['mprices']['product_price_publish_up'][$k];
  204.  
  205. $pricesToStore['product_price_publish_down'] = $data['mprices']['product_price_publish_down'][$k];
  206.  
  207. $pricesToStore['price_quantity_start'] = (int)$data['mprices']['price_quantity_start'][$k];
  208.  
  209. $pricesToStore['price_quantity_end'] = (int)$data['mprices']['price_quantity_end'][$k];
  210.  
  211. }
  212.  
  213. if (!$isChild and isset($data['mprices']['use_desired_price'][$k]) and $data['mprices']['use_desired_price'][$k] == "1") {
  214.  
  215. $calculator = calculationHelper::getInstance ();
  216.  
  217. if(isset($data['mprices']['salesPrice'][$k])){
  218.  
  219. $data['mprices']['salesPrice'][$k] = str_replace(array(',',' '),array('.',''),$data['mprices']['salesPrice'][$k]);
  220.  
  221. }
  222.  
  223. $pricesToStore['salesPrice'] = $data['mprices']['salesPrice'][$k];
  224.  
  225. $pricesToStore['product_price'] = $data['mprices']['product_price'][$k] = $calculator->calculateCostprice ($this->_id, $pricesToStore);
  226.  
  227. unset($data['mprices']['use_desired_price'][$k]);
  228.  
  229. } else {
  230.  
  231. if(isset($data['mprices']['product_price'][$k]) ){
  232.  
  233. $pricesToStore['product_price'] = $data['mprices']['product_price'][$k];
  234.  
  235. }
  236.  
  237. }
  238.  
  239. if ($isChild) $childPrices = $this->loadProductPrices($this->_id,array(0),false);
  240.  
  241. if ((isset($pricesToStore['product_price']) and $pricesToStore['product_price']!='' and $pricesToStore['product_price']!=='0') || (isset($childPrices) and count($childPrices)>1)) {
  242.  
  243. if ($isChild) {
  244.  
  245. if(is_array($old_price_ids) and count($old_price_ids)>1){
  246.  
  247. //We do not touch multiple child prices. Because in the parent list, we see no price, the gui is
  248.  
  249. //missing to reflect the information properly.
  250.  
  251. $pricesToStore = false;
  252.  
  253. $old_price_ids = array();
  254.  
  255. } else {
  256.  
  257. unset($data['mprices']['product_override_price'][$k]);
  258.  
  259. unset($pricesToStore['product_override_price']);
  260.  
  261. unset($data['mprices']['override'][$k]);
  262.  
  263. unset($pricesToStore['override']);
  264.  
  265. }
  266.  
  267. }
  268.  
  269. if($pricesToStore){
  270.  
  271. $toUnset = array();
  272.  
  273. if (!empty($old_price_ids) and count($old_price_ids) ) {
  274.  
  275. foreach($old_price_ids as $key => $oldprice){
  276.  
  277. if($pricesToStore['virtuemart_product_price_id'] == $oldprice['virtuemart_product_price_id'] ){
  278.  
  279. $pricesToStore = array_merge($oldprice,$pricesToStore);
  280.  
  281. $toUnset[] = $key;
  282.  
  283. }
  284.  
  285. }
  286.  
  287. }
  288.  
  289. $this->updateXrefAndChildTables ($pricesToStore, 'product_prices',$isChild);
  290.  
  291. foreach($toUnset as $key){
  292.  
  293. unset( $old_price_ids[ $key ] );
  294.  
  295. }
  296.  
  297. }
  298.  
  299. }
  300.  
  301. }
  302.  
  303. }
  304.  
  305. if (!empty($old_price_ids) and count($old_price_ids) ) {
  306.  
  307. $oldPriceIdsSql = array();
  308.  
  309. foreach($old_price_ids as $oldPride){
  310.  
  311. $oldPriceIdsSql[] = $oldPride['virtuemart_product_price_id'];
  312.  
  313. }
  314.  
  315. $db = JFactory::getDbo();
  316.  
  317. // delete old unused Prices
  318.  
  319. $db->setQuery( 'DELETE FROM `#__virtuemart_product_prices` WHERE `virtuemart_product_price_id` in ("'.implode('","', $oldPriceIdsSql ).'") ');
  320.  
  321. $db->execute();
  322.  
  323. $err = $db->getErrorMsg();
  324.  
  325. if(!empty($err)){
  326.  
  327. vmWarn('In store prodcut, deleting old price error',$err);
  328.  
  329. }
  330.  
  331. }
  332.  
  333. if (!empty($data['childs'])) {
  334.  
  335. foreach ($data['childs'] as $productId => $child) {
  336.  
  337. if(empty($productId)) continue;
  338.  
  339. if($productId!=$data['virtuemart_product_id']){
  340.  
  341. if(empty($child['product_parent_id'])) $child['product_parent_id'] = $data['virtuemart_product_id'];
  342.  
  343. $child['virtuemart_product_id'] = $productId;
  344.  
  345. if(!empty($child['product_parent_id']) and $child['product_parent_id'] == $child['virtuemart_product_id']){
  346.  
  347. $child['product_parent_id'] = 0;
  348.  
  349. }
  350.  
  351. $child['isChild'] = $this->_id;
  352.  
  353. $this->store ($child);
  354.  
  355. }
  356.  
  357. }
  358.  
  359. }
  360.  
  361. if (!$isChild) {
  362.  
  363. $data = $this->updateXrefAndChildTables ($data, 'product_shoppergroups');
  364.  
  365. $data = $this->updateXrefAndChildTables ($data, 'product_manufacturers');
  366.  
  367. $storeCats = true;
  368.  
  369. if (empty($data['categories']) or (!empty($data['categories'][0]) and $data['categories'][0]!="-2")){
  370.  
  371. $storeCats = true;
  372.  
  373. }
  374.  
  375. if($storeCats){
  376.  
  377. if (!empty($data['categories']) && count ($data['categories']) > 0) {
  378.  
  379. if(VmConfig::get('multix','none')!='none' and !vmAccess::manager('managevendors')){
  380.  
  381. if($ven->max_cats_per_product>=0){
  382.  
  383. while($ven->max_cats_per_product<count($data['categories'])){
  384.  
  385. array_pop($data['categories']);
  386.  
  387. }
  388.  
  389. }
  390.  
  391. }
  392.  
  393. $data['virtuemart_category_id'] = $data['categories'];
  394.  
  395. } else {
  396.  
  397. $data['virtuemart_category_id'] = array();
  398.  
  399. }
  400.  
  401. $data = $this->updateXrefAndChildTables ($data, 'product_categories');
  402.  
  403. }
  404.  
  405. // Update waiting list
  406.  
  407. //TODO what is this doing?
  408.  
  409. if (!empty($data['notify_users'])) {
  410.  
  411. if ($data['product_in_stock'] > 0 && $data['notify_users'] == '1') {
  412.  
  413. $waitinglist = VmModel::getModel ('Waitinglist');
  414.  
  415. $waitinglist->notifyList ($data['virtuemart_product_id']);
  416.  
  417. }
  418.  
  419. }
  420.  
  421. // Process the images
  422.  
  423. $mediaModel = VmModel::getModel ('Media');
  424.  
  425. $mediaModel->storeMedia ($data, 'product');
  426.  
  427. }
  428.  
  429. $cache = VmConfig::getCache('com_virtuemart_cat_manus','callback');
  430.  
  431. $cache->clean();
  432.  
  433. $dispatcher->trigger('plgVmAfterStoreProduct',array(&$data, &$product_data));
  434.  
  435. return $product_data->virtuemart_product_id;
  436.  
  437. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement