Advertisement
Guest User

Untitled

a guest
May 18th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.63 KB | None | 0 0
  1. <?PHP
  2.  
  3. require_once('api/simpla.php');
  4.  
  5. ############################################
  6. # Class Product - edit the static section
  7. ############################################
  8. class ProductAdmin extends simpla
  9. {
  10. public function fetch()
  11. {
  12.  
  13. $options = array();
  14. $product_categories = array();
  15. $variants = array();
  16. $images = array();
  17. $product_features = array();
  18. $related_products = array();
  19.  
  20. if($this->request->method('post') && !empty($_POST))
  21. {
  22. $product->id = $this->request->post('id', 'integer');
  23. $product->name = $this->request->post('name');
  24. $product->visible = $this->request->post('visible', 'boolean');
  25. $product->new = $this->request->post('new', 'boolean');
  26. $product->complect = $this->request->post('complect', 'boolean');
  27. $product->featured = $this->request->post('featured');
  28. $product->brand_id = $this->request->post('brand_id', 'integer');
  29.  
  30. $product->url = $this->request->post('url', 'string');
  31. $product->meta_title = $this->request->post('meta_title');
  32. $product->meta_keywords = $this->request->post('meta_keywords');
  33. $product->meta_description = $this->request->post('meta_description');
  34.  
  35. $product->annotation = $this->request->post('annotation');
  36. $product->body = $this->request->post('body');
  37. $product->video = $this->request->post('video');
  38.  
  39. // Варианты товара
  40. if($this->request->post('variants'))
  41. foreach($this->request->post('variants') as $n=>$va) foreach($va as $i=>$v)
  42. $variants[$i]->$n = $v;
  43.  
  44. // Категории товара
  45. $product_categories = $this->request->post('categories');
  46. if(is_array($product_categories))
  47. {
  48. foreach($product_categories as $c)
  49. $pc[]->id = $c;
  50. $product_categories = $pc;
  51. }
  52.  
  53. // Свойства товара
  54. $options = $this->request->post('options');
  55. if(is_array($options))
  56. {
  57. foreach($options as $f_id=>$val)
  58. {
  59. $po[$f_id]->feature_id = $f_id;
  60. $po[$f_id]->value = $val;
  61. }
  62. $options = $po;
  63. }
  64.  
  65. // Связанные товары
  66. if(is_array($this->request->post('related_products')))
  67. {
  68. foreach($this->request->post('related_products') as $k=>$p)
  69. {
  70. $rp[$k]->product_id = $product->id;
  71. $rp[$k]->related_id = $k;
  72. $rp[$k]->items = $p;
  73. }
  74. $related_products = $rp;
  75. }
  76.  
  77. // Не допустить пустое название товара.
  78. if(empty($product->name))
  79. {
  80. $this->design->assign('message_error', 'empty_name');
  81. $images = $this->products->get_images(array('product_id'=>$product->id));
  82. }
  83. // Не допустить одинаковые URL разделов.
  84. elseif(($p = $this->products->get_product($product->url)) && $p->id!=$product->id)
  85. {
  86. $this->design->assign('message_error', 'url_exists');
  87. $images = $this->products->get_images(array('product_id'=>$product->id));
  88. }
  89. else
  90. {
  91. if(empty($product->id))
  92. {
  93. $product->id = $this->products->add_product($product);
  94. $product = $this->products->get_product($product->id);
  95. $this->design->assign('message_success', 'added');
  96. }
  97. else
  98. {
  99. $this->products->update_product($product->id, $product);
  100. $product = $this->products->get_product($product->id);
  101. $this->design->assign('message_success', 'updated');
  102. }
  103.  
  104. if($product->id)
  105. {
  106. // Категории товара
  107. $query = $this->db->placehold('DELETE FROM __products_categories WHERE product_id=?', $product->id);
  108. $this->db->query($query);
  109. if(is_array($product_categories))
  110. {
  111. foreach($product_categories as $i=>$category)
  112. $this->categories->add_product_category($product->id, $category->id, $i);
  113. }
  114.  
  115. // Варианты
  116. if(is_array($variants))
  117. {
  118. $variants_ids = array();
  119. foreach($variants as $index=>&$variant)
  120. {
  121. if($variant->stock == '∞' || $variant->stock == '')
  122. $variant->stock = null;
  123.  
  124. // Удалить файл
  125. if(!empty($_POST['delete_attachment'][$variant->id]))
  126. {
  127. $this->variants->delete_attachment($variant->id);
  128. }
  129.  
  130. // Загрузить файлы
  131. if(!empty($_FILES['attachment']['tmp_name'][$index]) && !empty($_FILES['attachment']['name'][$index]))
  132. {
  133. $attachment_tmp_name = $_FILES['attachment']['tmp_name'][$index];
  134. $attachment_name = $_FILES['attachment']['name'][$index];
  135. move_uploaded_file($attachment_tmp_name, $this->config->root_dir.'/'.$this->config->downloads_dir.$attachment_name);
  136. $variant->attachment = $attachment_name;
  137. }
  138.  
  139.  
  140. if($variant->id)
  141. $this->variants->update_variant($variant->id, $variant);
  142. else
  143. {
  144. $variant->product_id = $product->id;
  145. $variant->id = $this->variants->add_variant($variant);
  146. }
  147. $variant = $this->variants->get_variant($variant->id);
  148.  
  149. $variants_ids[] = $variant->id;
  150. }
  151.  
  152.  
  153. // Удалить непереданные варианты
  154. $current_variants = $this->variants->get_variants(array('product_id'=>$product->id));
  155. foreach($current_variants as $current_variant)
  156. if(!in_array($current_variant->id, $variants_ids))
  157. $this->variants->delete_variant($current_variant->id);
  158.  
  159. //if(!empty($))
  160.  
  161. // Отсортировать варианты
  162. asort($variants_ids);
  163. $i = 0;
  164. foreach($variants_ids as $variant_id)
  165. {
  166. $this->variants->update_variant($variants_ids[$i], array('position'=>$variant_id));
  167. $i++;
  168. }
  169. }
  170.  
  171. // Удаление изображений
  172. $images = (array)$this->request->post('images');
  173. $current_images = $this->products->get_images(array('product_id'=>$product->id));
  174. foreach($current_images as $image)
  175. {
  176. if(!in_array($image->id, $images))
  177. $this->products->delete_image($image->id);
  178. }
  179.  
  180. // Порядок изображений
  181. if($images = $this->request->post('images'))
  182. {
  183. $i=0;
  184. foreach($images as $id)
  185. {
  186. $this->products->update_image($id, array('position'=>$i));
  187. $i++;
  188. }
  189. }
  190. // Загрузка изображений
  191. if($images = $this->request->files('images'))
  192. {
  193. for($i=0; $i<count($images['name']); $i++)
  194. {
  195. if ($image_name = $this->image->upload_image($images['tmp_name'][$i], $images['name'][$i]))
  196. {
  197. $this->products->add_image($product->id, $image_name);
  198. }
  199. else
  200. {
  201. $this->design->assign('error', 'error uploading image');
  202. }
  203. }
  204. }
  205. // Загрузка изображений из интернета
  206. if($images = $this->request->post('images_urls'))
  207. {
  208. foreach($images as $url)
  209. {
  210. $url = str_replace('https://', 'http://', $url);
  211. if(!empty($url) && $url != 'http://')
  212. $this->products->add_image($product->id, $url);
  213. }
  214. }
  215. $images = $this->products->get_images(array('product_id'=>$product->id));
  216.  
  217. // Характеристики товара
  218.  
  219. // Удалим все из товара
  220. foreach($this->features->get_product_options($product->id) as $po)
  221. $this->features->delete_option($product->id, $po->feature_id);
  222.  
  223. // Свойства текущей категории
  224. $category_features = array();
  225. //foreach($this->features->get_features(array('category_id'=>$product_categories[0])) as $f)
  226. // $category_features[] = $f->id;
  227. foreach($this->features->get_features() as $f)
  228. $category_features[] = $f->id;
  229.  
  230.  
  231. if(is_array($options))
  232. foreach($options as $option)
  233. {
  234. if(in_array($option->feature_id, $category_features))
  235. $this->features->update_option($product->id, $option->feature_id, $option->value);
  236. }
  237.  
  238. // Новые характеристики
  239. $new_features_names = $this->request->post('new_features_names');
  240. $new_features_values = $this->request->post('new_features_values');
  241. if(is_array($new_features_names) && is_array($new_features_values))
  242. {
  243. foreach($new_features_names as $i=>$name)
  244. {
  245. $value = $new_features_values[$i];
  246. if(!empty($name) && !empty($value))
  247. {
  248. $query = $this->db->placehold("SELECT * FROM __features WHERE name=? LIMIT 1", $name);
  249. $this->db->query($query);
  250. $feature_id = $this->db->result('id');
  251. if(empty($feature_id))
  252. {
  253. $feature_id = $this->features->add_feature(array('name'=>$name));
  254. }
  255. $this->features->add_feature_category($feature_id, reset($product_categories)->id);
  256. $this->features->update_option($product->id, $feature_id, $value);
  257. }
  258. }
  259. // Свойства товара
  260. $options = $this->features->get_product_options($product->id);
  261. }
  262. // Связанные товары
  263. $query = $this->db->placehold('DELETE FROM __related_products WHERE product_id=?', $product->id);
  264. $this->db->query($query);
  265. if(is_array($related_products))
  266. {
  267. $pos = 0;
  268. foreach($related_products as $i=>$related_product)
  269. $this->products->add_related_product($product->id, $related_product->related_id, $pos++, $related_product->items);
  270. }
  271. }
  272. }
  273.  
  274. //header('Location: '.$this->request->url(array('message_success'=>'updated')));
  275. }
  276. else
  277. {
  278. $product->id = $this->request->get('id', 'integer');
  279. $product = $this->products->get_product(intval($product->id));
  280.  
  281. if($product && $product->id)
  282. {
  283.  
  284. // Категории товара
  285. $product_categories = $this->categories->get_categories(array('product_id'=>$product->id));
  286.  
  287. // Варианты товара
  288. $variants = $this->variants->get_variants(array('product_id'=>$product->id));
  289.  
  290. // Изображения товара
  291. $images = $this->products->get_images(array('product_id'=>$product->id));
  292.  
  293. // Свойства товара
  294. $options = $this->features->get_options(array('product_id'=>$product->id));
  295.  
  296. // Связанные товары
  297. $related_products = $this->products->get_related_products(array('product_id'=>$product->id));
  298. }
  299. else
  300. {
  301. // Сразу активен
  302. $product->visible = 1;
  303. }
  304. }
  305.  
  306.  
  307. if(empty($variants))
  308. $variants = array(1);
  309.  
  310. if(empty($product_categories))
  311. {
  312. if($category_id = $this->request->get('category_id'))
  313. $product_categories[0]->id = $category_id;
  314. else
  315. $product_categories = array(1);
  316. }
  317. if(empty($product->brand_id) && $brand_id=$this->request->get('brand_id'))
  318. {
  319. $product->brand_id = $brand_id;
  320. }
  321.  
  322. if(!empty($related_products))
  323. {
  324. foreach($related_products as &$r_p)
  325. $r_products[$r_p->related_id] = &$r_p;
  326. $temp_products = $this->products->get_products(array('id'=>array_keys($r_products)));
  327. foreach($temp_products as $temp_product) {
  328. $items = $r_products[$temp_product->id]->items;
  329. $r_products[$temp_product->id] = $temp_product;
  330. $r_products[$temp_product->id]->items = $items;
  331. }
  332.  
  333. $related_products_images = $this->products->get_images(array('product_id'=>array_keys($r_products)));
  334. foreach($related_products_images as $image)
  335. {
  336. $r_products[$image->product_id]->images[] = $image;
  337. }
  338.  
  339. $related_products_variants = $this->variants->get_variants(array('product_id'=>array_keys($r_products)));
  340. foreach($related_products_variants as $r_variant)
  341. {
  342. $r_products[$r_variant->product_id]->variants[] = $r_variant;
  343. }
  344. }
  345. if(is_array($options))
  346. {
  347. $temp_options = array();
  348. foreach($options as $option)
  349. $temp_options[$option->feature_id] = $option;
  350. $options = $temp_options;
  351. }
  352.  
  353.  
  354. $this->design->assign('product', $product);
  355.  
  356. $this->design->assign('product_categories', $product_categories);
  357. $this->design->assign('product_variants', $variants);
  358. $this->design->assign('product_images', $images);
  359. $this->design->assign('options', $options);
  360. $this->design->assign('related_products', $related_products);
  361.  
  362.  
  363. // Все бренды
  364. $brands = $this->brands->get_brands();
  365. $this->design->assign('brands', $brands);
  366.  
  367. // Все категории
  368. $categories = $this->categories->get_categories_tree();
  369. $this->design->assign('categories', $categories);
  370.  
  371. // Все свойства
  372. $features = $this->features->get_features();
  373. $this->design->assign('features', $features);
  374.  
  375. // Связка категорий со свойствами
  376. $categories_features = array();
  377. $query = $this->db->placehold('SELECT category_id, feature_id FROM __categories_features');
  378. $this->db->query($query);
  379. $c_f = $this->db->results();
  380. foreach($c_f as $i)
  381. {
  382. $categories_features[$i->category_id][] = $i->feature_id;
  383. }
  384. $this->design->assign('categories_features', $categories_features);
  385.  
  386. //$this->design->assign('message_success', $this->request->get('message_success', 'string'));
  387. //$this->design->assign('message_error', $this->request->get('message_error', 'string'));
  388.  
  389. return $this->design->fetch('product.tpl');
  390. }
  391. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement