OptionController.class.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 Admin\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();
  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. $return=$model->add_option($data);
  55. if($return){
  56. $r['redirect']=U('Option/index');
  57. $this->ajaxReturn($r);
  58. die;
  59. }else{
  60. $error['error']='新增失败';
  61. $this->ajaxReturn($error);
  62. die;
  63. }
  64. }
  65. $this->crumbs='新增';
  66. $this->action=U('Option/add');
  67. $this->display('edit');
  68. }
  69. function edit(){
  70. if(IS_POST){
  71. $this->validate_form();
  72. $model=new OptionModel();
  73. $data=I('post.');
  74. //dump($data);
  75. $return=$model->edit_option($data);
  76. if($return){
  77. $r['redirect']=U('Option/index');
  78. $this->ajaxReturn($r);
  79. die;
  80. }else{
  81. $error['error']='编辑失败';
  82. $this->ajaxReturn($error);
  83. die;
  84. }
  85. }
  86. $this->option=M('Option')->find(I('id'));
  87. $this->option_values=M('OptionValue')->where(array('option_id'=>I('id')))->select();
  88. $this->crumbs='编辑';
  89. $this->action=U('Option/edit');
  90. $this->display();
  91. }
  92. function del(){
  93. M('option')->delete(I('id'));
  94. M('option_value')->where(array('option_id'=>I('id')))->delete();
  95. $this->redirect('Option/index');
  96. }
  97. function get_goodsajax_option_value()
  98. {
  99. $goods_id = I('post.goods_id');
  100. $goods_option = M('goods_option')->where( array('goods_id' => $goods_id) )->select();
  101. $result = array('code' => 0);
  102. if( !empty($goods_option) )
  103. {
  104. foreach($goods_option as $key => $val)
  105. {
  106. $goods_option_value_list = M('option_value')->where( array('option_id' => $val['option_id']) )->select();
  107. foreach($goods_option_value_list as $kk => $vv)
  108. {
  109. // $vv['option_value_id']
  110. $has_check_option_value_list = M('goods_option_value')->where( array('option_value_id' => $vv['option_value_id'],'goods_id' =>$goods_id ) )->select();
  111. if( !empty($has_check_option_value_list) )
  112. {
  113. $vv['selected'] = 'selected';
  114. } else{
  115. $vv['selected'] = '';
  116. }
  117. $goods_option_value_list[$kk] = $vv;
  118. }
  119. $val['goods_option_value_list'] = $goods_option_value_list;
  120. $goods_option[$key] = $val;
  121. }
  122. $result['code'] = 1;
  123. $result['data'] = $goods_option;
  124. }
  125. echo json_encode($result);
  126. die();
  127. }
  128. function get_ajax_option_value()
  129. {
  130. //option_value
  131. $option_id = I('post.option_id');
  132. $option_value_list = M('option_value')->where( array('option_id' =>$option_id) )->order('value_sort_order asc')->select();
  133. echo json_encode( array('code' =>1 , 'data' => $option_value_list) );
  134. die();
  135. }
  136. //获取选项
  137. function autocomplete(){
  138. $json = array();
  139. $filter_name=I('filter_name');
  140. if (isset($filter_name)) {
  141. //$m=D('Product');
  142. $m=new OptionModel();
  143. $options = $m->getOptions($filter_name);
  144. foreach ($options as $option) {
  145. $option_value_data = array();
  146. if ($option['type'] == 'select' || $option['type'] == 'radio' || $option['type'] == 'checkbox') {
  147. $option_values = $m->getOptionValues($option['option_id']);
  148. foreach ($option_values as $option_value) {
  149. $option_value_data[] = array(
  150. 'option_value_id' => $option_value['option_value_id'],
  151. 'name' => html_entity_decode($option_value['value'], ENT_QUOTES, 'UTF-8'),
  152. 'image' => $option_value['image']
  153. );
  154. }
  155. $sort_order = array();
  156. foreach ($option_value_data as $key => $value) {
  157. $sort_order[$key] = $value['name'];
  158. }
  159. array_multisort($sort_order, SORT_ASC, $option_value_data);
  160. }
  161. $type = '';
  162. if ($option['type'] == 'select' || $option['type'] == 'radio' || $option['type'] == 'checkbox' ) {
  163. $type = '选择';
  164. }
  165. $json[] = array(
  166. 'option_id' => $option['option_id'],
  167. 'name' => strip_tags(html_entity_decode($option['name'], ENT_QUOTES, 'UTF-8')),
  168. 'category' => $type,
  169. 'type' => $option['type'],
  170. 'option_value' => $option_value_data
  171. );
  172. }
  173. }
  174. $sort_order = array();
  175. foreach ($json as $key => $value) {
  176. $sort_order[$key] = $value['name'];
  177. }
  178. array_multisort($sort_order, SORT_ASC, $json);
  179. echo(json_encode($json));
  180. }
  181. }
  182. ?>