/protected/controllers/GroupsApiController.php

https://gitlab.com/ilyales/vigma · PHP · 221 lines · 154 code · 39 blank · 28 comment · 2 complexity · 230f8b4127d022dfd9e8d11cdf07b9f4 MD5 · raw file

  1. <?php
  2. class GroupsApiController extends Controller
  3. {
  4. public function filters()
  5. {
  6. return array(
  7. 'accessControl',
  8. );
  9. }
  10. public function accessRules()
  11. {
  12. return array(
  13. array('allow', // allow authenticated users to perform any action
  14. 'users'=>array('@'),
  15. ),
  16. array('deny', // deny all users
  17. 'users'=>array('*'),
  18. ),
  19. );
  20. }
  21. public function actionImportGroupsAjax()
  22. {
  23. $data = json_decode(file_get_contents('php://input'));
  24. $response = array('status'=>true);
  25. try {
  26. $filePath = UploadFile::getPathByName($data->fileName);
  27. $result = GroupProdsManager::importGroupsFromFile($filePath);
  28. $response['result'] = $result;
  29. CatalogcatsManager::updateProductsCatalogcatIds();
  30. }
  31. catch (Exception $e)
  32. {
  33. $response['status'] = false;
  34. $response['message'] = $e->getMessage();
  35. }
  36. echo json_encode($response);
  37. }
  38. public function actionAddGroupToCatalogcatAjax()
  39. {
  40. $data = json_decode(file_get_contents('php://input'));
  41. $response = array('status'=>true);
  42. try {
  43. CatalogcatsManager::addGroupToCatalogcat($data->catalogcatId,$data->groupId);
  44. CatalogcatsManager::updateProductsCatalogcatIds();
  45. }
  46. catch (Exception $e)
  47. {
  48. $response['status'] = false;
  49. $response['message'] = $e->getMessage();
  50. }
  51. echo json_encode($response);
  52. }
  53. public function actionRemoveGroupFromCatalogcatAjax()
  54. {
  55. $data = json_decode(file_get_contents('php://input'));
  56. $response = array('status'=>true);
  57. try {
  58. CatalogcatsManager::removeGroupFromCatalogcat($data->catalogcatId,$data->groupId);
  59. CatalogcatsManager::updateProductsCatalogcatIds();
  60. }
  61. catch (Exception $e)
  62. {
  63. $response['status'] = false;
  64. $response['message'] = $e->getMessage();
  65. }
  66. echo json_encode($response);
  67. }
  68. public function actionGetPtoductsByCatalogcatAjax()
  69. {
  70. $data = json_decode(file_get_contents('php://input'));
  71. $response = array('status'=>true);
  72. try {
  73. $products = Products::getProductsByCatalogcat($data->catalogcatId);
  74. $response['products'] = $products;
  75. }
  76. catch (Exception $e)
  77. {
  78. $response['status'] = false;
  79. $response['message'] = $e->getMessage();
  80. }
  81. echo json_encode($response);
  82. }
  83. // public function actionGetGroupsByProductIdAjax() {
  84. // $data = json_decode(file_get_contents('php://input'));
  85. // $response = array();
  86. // $response['status'] = true;
  87. // try {
  88. // $products = GroupProdsManager::deleteGroupProd($data->groupId,$data->productId);
  89. // }
  90. // catch (Exception $e)
  91. // {
  92. // $response['status'] = false;
  93. // $response['message'] = $e->getMessage();
  94. // }
  95. // echo json_encode($response);
  96. // }
  97. /**
  98. * Добавить новую связь группы и товара
  99. *
  100. */
  101. public function actionNewGroupProd()
  102. {
  103. $data = json_decode(file_get_contents('php://input'));
  104. $response = array();
  105. $response['status'] = true;
  106. try
  107. {
  108. $res = GroupProdsManager::newGroupProds($data->groupId,$data->productId,$data->chod);
  109. //если связь длбавляется вручную, то надо обновить таблицу catalogcat_prods
  110. if ($data->chod==null) {
  111. CatalogcatsManager::updateProductsCatalogcatIds();
  112. }
  113. if ($res==false)
  114. {
  115. $response['status'] = false;
  116. $response['message'] = "к товару уже привязан группа ".$data->group_num;
  117. }
  118. }
  119. catch (Exception $e)
  120. {
  121. $response['status'] = false;
  122. $response['message'] = $e->getMessage();
  123. }
  124. echo json_encode($response);
  125. }
  126. /**
  127. * Удлить запись в group_prods по group_id и product_id
  128. * @post int $group_id
  129. * @post int $product_id
  130. */
  131. public function actionUnbindGroupFromProductAjax()
  132. {
  133. $data = json_decode(file_get_contents('php://input'));
  134. $response = array();
  135. $response['status'] = true;
  136. try {
  137. $products = GroupProdsManager::deleteGroupProd($data->groupId,$data->productId);
  138. CatalogcatsManager::updateProductsCatalogcatIds();
  139. }
  140. catch (Exception $e)
  141. {
  142. $response['status'] = false;
  143. $response['message'] = $e->getMessage();
  144. }
  145. echo json_encode($response);
  146. }
  147. /**
  148. * Выборка товаров относящихся к группе
  149. * @post $groupId
  150. */
  151. public function actionGetPtoductsByGroupAjax()
  152. {
  153. $headers = array();
  154. array_push($headers, array("key"=>"name","name"=>"Наименование"));
  155. array_push($headers, array("key"=>"chod","name"=>"ЧОД"));
  156. array_push($headers, array("key"=>"chod","name"=>"Изображение"));
  157. array_push($headers, array("key"=>"chod","name"=>"Рейтинг"));
  158. array_push($headers, array("key"=>"groups","name"=>"Группы"));
  159. $result = array();
  160. $result['header'] = $headers;
  161. $page = intval($_GET['page']);
  162. $count = intval($_GET['count']);
  163. $offset = $page*$count-$count;
  164. $filters=array();
  165. $filters['name'] = $_GET['name'];
  166. $filters['chod'] = preg_replace('/[^a-zа-яё\d]/ui','',$_GET['chod']);
  167. $filters['groupId'] = $_GET['groupId'];
  168. $res = GroupProdsManager::getProductsByGroup($filters,$offset,$count);
  169. $result['rows'] = $res['products'];
  170. $resultsCount = $res['resultsCount'];
  171. $pagination = array();
  172. $pagination['count'] = $count;
  173. $pagination['page'] = $page;
  174. $pagination['pages'] = ($resultsCount-$resultsCount%$count)/$count;
  175. $pagination['size'] = intval($resultsCount);
  176. $result['pagination'] = $pagination;
  177. $result['sort-by'] = $_GET['sort-by'];
  178. $result['sort-order'] = $_GET['sort-order'];
  179. $result['products-count'] = count($products);
  180. echo json_encode($result);
  181. }
  182. }