/public/simpla/rest/RestProducts.php

https://bitbucket.org/AntonTyutin/pretty-simpla · PHP · 198 lines · 154 code · 20 blank · 24 comment · 27 complexity · a894cc65988f43f044c650bc7911f534 MD5 · raw file

  1. <?php
  2. /**
  3. * Simpla CMS
  4. *
  5. * @copyright 2012 Denis Pikusov
  6. * @link http://simplacms.ru
  7. * @author Denis Pikusov
  8. *
  9. */
  10. require_once('Rest.php');
  11. class RestProducts extends Rest
  12. {
  13. public function get()
  14. {
  15. $items = array();
  16. $filter = array();
  17. // id
  18. $filter['id'] = array();
  19. foreach(explode(',', $this->request->get('id')) as $id)
  20. if(($id = intval($id)) > 0)
  21. $filter['id'][] = $id;
  22. // Сортировка
  23. $filter['sort'] = $this->request->get('sort');
  24. // Категория
  25. $filter['category_id'] = $this->request->get('category');
  26. // Дата последнего изменения
  27. $filter['modified_from'] = $this->request->get('modified_from');
  28. // Бренд
  29. $filter['brand_id'] = $this->request->get('brand');
  30. // Страница
  31. $filter['page'] = $this->request->get('page');
  32. // Количество элементов на странице
  33. $filter['limit'] = $this->request->get('limit');
  34. // Какие поля отдавать
  35. if($fields = $this->request->get('fields'))
  36. $fields = explode(',', $fields);
  37. // Выбираем
  38. foreach($this->products->get_products($filter) as $item)
  39. {
  40. $items[$item->id] = new stdClass();
  41. if($fields)
  42. {
  43. foreach($fields as $field)
  44. if(isset($item->$field))
  45. $items[$item->id]->$field = $item->$field;
  46. }
  47. else
  48. $items[$item->id] = $item;
  49. }
  50. if(empty($items))
  51. return false;
  52. // Выбранные id
  53. $items_ids = array_keys($items);
  54. // Присоединяемые данные
  55. if($join = $this->request->get('join'))
  56. {
  57. $join = explode(',', $join);
  58. // Изображения
  59. if(in_array('images', $join))
  60. {
  61. foreach($this->products->get_images(array('product_id'=>$items_ids)) as $i)
  62. if(isset($items[$i->product_id]))
  63. $items[$i->product_id]->images[] = $i;
  64. }
  65. // Варианты
  66. if(in_array('variants', $join))
  67. {
  68. foreach($this->variants->get_variants(array('product_id'=>$items_ids)) as $v)
  69. if(isset($items[$v->product_id]))
  70. $items[$v->product_id]->variants[] = $v;
  71. }
  72. // Категории
  73. $categories_ids = array();
  74. if(in_array('categories', $join))
  75. {
  76. foreach($this->categories->get_products_categories(array('product_id'=>$items_ids)) as $pc)
  77. {
  78. if(isset($items[$pc->product_id]))
  79. {
  80. $c = $pc;
  81. $c = $this->categories->get_category(intval($pc->category_id));
  82. unset($c->path);
  83. unset($c->subcategories);
  84. unset($c->children);
  85. $items[$pc->product_id]->categories[] = $c;
  86. $categories_ids[] = $pc->category_id;
  87. }
  88. }
  89. }
  90. // Свойства
  91. if(in_array('features', $join))
  92. {
  93. $features_ids = array();
  94. foreach($this->features->get_options(array('product_id'=>$items_ids)) as $o)
  95. {
  96. if(isset($items[$o->product_id]))
  97. {
  98. $options[$o->feature_id] = $o;
  99. $features_ids[] = $o->feature_id;
  100. }
  101. }
  102. foreach($this->features->get_features(array('id'=>$features_ids)) as $f)
  103. {
  104. if(isset($options[$f->id]))
  105. {
  106. $f->value = $o->value;
  107. $items[$o->product_id]->features[] = $f;
  108. }
  109. }
  110. }
  111. }
  112. return array_values($items);
  113. }
  114. public function post()
  115. {
  116. $product = json_decode($this->request->post());
  117. print_r($product);
  118. $variants = $product->variants;
  119. unset($product->variants);
  120. $id = $this->products->add_product($product);
  121. if(!empty($variants))
  122. {
  123. foreach($variants as $v)
  124. {
  125. $v->product_id = $id;
  126. $varinat_id = $this->variants->add_variant($v);
  127. }
  128. }
  129. if(!$id)
  130. return false;
  131. else
  132. return $id;
  133. header("Content-type: application/json");
  134. header("Location: ".$this->config->root_url."/simpla/rest/products/".$id, true, 201);
  135. }
  136. public function put()
  137. {
  138. $id = intval($this->request->get('id'));
  139. if(empty($id) || !$this->products->get_product($id))
  140. {
  141. header("HTTP/1.0 404 Not Found");
  142. exit();
  143. }
  144. $product = json_decode($this->request->post());
  145. $variants = $product->variants;
  146. unset($product->variants);
  147. $id = $this->products->update_product($id, $product);
  148. if(!empty($variants))
  149. {
  150. $variants_ids = array();
  151. foreach($variants as $v)
  152. {
  153. $v->product_id = $id;
  154. if($v->stock == '∞' || $v->stock == '')
  155. $v->stock = null;
  156. if($v->id)
  157. $this->variants->update_variant($v->id, $v);
  158. else
  159. {
  160. $v->product_id = $id;
  161. $v->id = $this->variants->add_variant($v);
  162. }
  163. $variants_ids[] = $v->id;
  164. // Удалить непереданные варианты
  165. $current_variants = $this->variants->get_variants(array('product_id'=>$id));
  166. foreach($current_variants as $current_variant)
  167. if(!in_array($current_variant->id, $variants_ids))
  168. $this->variants->delete_variant($current_variant->id);
  169. }
  170. }
  171. if(!$id)
  172. return false;
  173. else
  174. return $id;
  175. header("Content-type: application/json");
  176. header("Location: ".$this->config->root_url."/simpla/rest/products/".$id, true, 201);
  177. }
  178. }