OptionController.class.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /**
  3. * 小梦科技资源nanodreamtech.com
  4. *
  5. * ==========================================================================
  6. * @link https://www.nanodreamtech.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license https://www.nanodreamtech.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. *
  13. */
  14. namespace Seller\Controller;
  15. use Admin\Model\OptionModel;
  16. class OptionController extends CommonController{
  17. protected function _initialize(){
  18. parent::_initialize();
  19. $this->breadcrumb1='商品管理';
  20. $this->breadcrumb2='商品规格';
  21. }
  22. function index(){
  23. $model=new OptionModel();
  24. $data=$model->show_option_page('store_id='.SELLERUID);
  25. $this->assign('empty',$data['empty']);// 赋值数据集
  26. $this->assign('list',$data['list']);// 赋值数据集
  27. $this->assign('page',$data['page']);// 赋值分页输出
  28. $this->display();
  29. }
  30. protected function validate_form() {
  31. $data=I('post.');
  32. $error=array();
  33. if ((utf8_strlen($data['name']) <1) || (utf8_strlen($data['name']) > 10)) {
  34. $error['error'] = '选项名称必须大于1小于10个字!!';
  35. }elseif (($data['type'] == 'select' || $data['type'] == 'radio' || $data['type'] == 'checkbox'|| $data['type'] == 'image') && !isset($data['option_value'])) {
  36. $error['error'] ='选项值必填!!';
  37. }elseif (isset($data['option_value'])) {
  38. foreach ($data['option_value'] as $option_value_id => $option_value) {
  39. if ((utf8_strlen($option_value['name']) < 1) || (utf8_strlen($option_value['name']) > 10)) {
  40. $error['error'] ='选项值必须大于0小于10个字!!';
  41. }
  42. }
  43. }
  44. if($error){
  45. $this->ajaxReturn($error);
  46. die;
  47. }
  48. }
  49. function add(){
  50. if(IS_POST){
  51. $this->validate_form();
  52. $model=new OptionModel();
  53. $data=I('post.');
  54. $data['store_id'] = SELLERUID;
  55. $return=$model->add_option($data);
  56. if($return){
  57. $r['redirect']=U('Option/index');
  58. $this->ajaxReturn($r);
  59. die;
  60. }else{
  61. $error['error']='新增失败';
  62. $this->ajaxReturn($error);
  63. die;
  64. }
  65. }
  66. $this->crumbs='新增';
  67. $this->action=U('Option/add');
  68. $this->display('edit');
  69. }
  70. function edit(){
  71. if(IS_POST){
  72. $this->validate_form();
  73. $model=new OptionModel();
  74. $data=I('post.');
  75. //dump($data);
  76. $return=$model->edit_option($data);
  77. if($return){
  78. $r['redirect']=U('Option/index');
  79. $this->ajaxReturn($r);
  80. die;
  81. }else{
  82. $error['error']='编辑失败';
  83. $this->ajaxReturn($error);
  84. die;
  85. }
  86. }
  87. $this->option=M('Option')->find(I('id'));
  88. $this->option_values=M('OptionValue')->where(array('option_id'=>I('id')))->select();
  89. $this->crumbs='编辑';
  90. $this->action=U('Option/edit');
  91. $this->display();
  92. }
  93. function del(){
  94. M('option')->delete(I('id'));
  95. M('option_value')->where(array('option_id'=>I('id')))->delete();
  96. $this->redirect('Option/index');
  97. }
  98. function get_goodsajax_option_value()
  99. {
  100. $goods_id = I('post.goods_id');
  101. $goods_option = M('goods_option')->where( array('goods_id' => $goods_id) )->select();
  102. $result = array('code' => 0);
  103. if( !empty($goods_option) )
  104. {
  105. foreach($goods_option as $key => $val)
  106. {
  107. $goods_option_value_list = M('option_value')->where( array('option_id' => $val['option_id']) )->select();
  108. foreach($goods_option_value_list as $kk => $vv)
  109. {
  110. // $vv['option_value_id']
  111. $has_check_option_value_list = M('goods_option_value')->where( array('option_value_id' => $vv['option_value_id'],'goods_id' =>$goods_id ) )->select();
  112. if( !empty($has_check_option_value_list) )
  113. {
  114. $vv['selected'] = 'selected';
  115. } else{
  116. $vv['selected'] = '';
  117. }
  118. $goods_option_value_list[$kk] = $vv;
  119. }
  120. $val['goods_option_value_list'] = $goods_option_value_list;
  121. $goods_option[$key] = $val;
  122. }
  123. $result['code'] = 1;
  124. $result['data'] = $goods_option;
  125. }
  126. echo json_encode($result);
  127. die();
  128. }
  129. function get_ajax_option_value()
  130. {
  131. //option_value
  132. $option_id = I('post.option_id');
  133. $option_value_list = M('option_value')->where( array('option_id' =>$option_id) )->order('value_sort_order asc')->select();
  134. echo json_encode( array('code' =>1 , 'data' => $option_value_list) );
  135. die();
  136. }
  137. //获取选项
  138. function autocomplete(){
  139. $json = array();
  140. $filter_name=I('filter_name');
  141. if (isset($filter_name)) {
  142. //$m=D('Product');
  143. $m=new OptionModel();
  144. //getOptions
  145. $options = $m->getOptions($filter_name,SELLERUID);
  146. foreach ($options as $option) {
  147. $option_value_data = array();
  148. if ($option['type'] == 'select' || $option['type'] == 'radio' || $option['type'] == 'checkbox') {
  149. $option_values = $m->getOptionValues($option['option_id']);
  150. foreach ($option_values as $option_value) {
  151. $option_value_data[] = array(
  152. 'option_value_id' => $option_value['option_value_id'],
  153. 'name' => html_entity_decode($option_value['value'], ENT_QUOTES, 'UTF-8'),
  154. 'image' => $option_value['image']
  155. );
  156. }
  157. $sort_order = array();
  158. foreach ($option_value_data as $key => $value) {
  159. $sort_order[$key] = $value['name'];
  160. }
  161. array_multisort($sort_order, SORT_ASC, $option_value_data);
  162. }
  163. $type = '';
  164. if ($option['type'] == 'select' || $option['type'] == 'radio' || $option['type'] == 'checkbox' ) {
  165. $type = '选择';
  166. }
  167. $json[] = array(
  168. 'option_id' => $option['option_id'],
  169. 'name' => strip_tags(html_entity_decode($option['name'], ENT_QUOTES, 'UTF-8')),
  170. 'category' => $type,
  171. 'type' => $option['type'],
  172. 'option_value' => $option_value_data
  173. );
  174. }
  175. }
  176. $sort_order = array();
  177. foreach ($json as $key => $value) {
  178. $sort_order[$key] = $value['name'];
  179. }
  180. array_multisort($sort_order, SORT_ASC, $json);
  181. echo(json_encode($json));
  182. }
  183. }
  184. ?>