OptionModel.class.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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\Model;
  15. class OptionModel{
  16. public function show_option_page($search=''){
  17. $count=M('option')->where($search)->count();
  18. $Page = new \Think\Page($count,C('BACK_PAGE_NUM'));
  19. $show = $Page->show();
  20. $list = M('option')->where($search)->order('option_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
  21. return array(
  22. 'empty'=>'<tr><td colspan="20">~~暂无数据</td></tr>',
  23. 'list'=>$list,
  24. 'page'=>$show
  25. );
  26. }
  27. public function add_option($data){
  28. $option['name']=$data['name'];
  29. $option['type']=$data['type'];
  30. $option['update_time']=date('Y-m-d H:i:s',time());
  31. if(isset($data['store_id']) ) {
  32. $option['store_id'] = $data['store_id'];
  33. }
  34. foreach ($data['option_value'] as $k=> $v) {
  35. if(!empty($v)){
  36. if($v!=end($data['option_value'])){
  37. $option['value'].=$v['name'].',';
  38. }else{
  39. $option['value'].=$v['name'];
  40. }
  41. }
  42. }
  43. $option_id=M('option')->add($option);
  44. if($option_id){
  45. foreach ($data['option_value'] as $k => $v) {
  46. if(!empty($v)){
  47. $value['option_id']=$option_id;
  48. $value['value_name']=$v['name'];
  49. $value['value_sort_order']=$v['sort_order'];
  50. M('OptionValue')->add($value);
  51. }
  52. }
  53. return true;
  54. }else{
  55. return false;
  56. }
  57. }
  58. public function edit_option($data){
  59. $option['option_id']=$data['id'];
  60. $option['name']=$data['name'];
  61. $option['type']=$data['type'];
  62. $option['update_time']=date('Y-m-d H:i:s',time());
  63. foreach ($data['option_value'] as $k=> $v) {
  64. if(!empty($v)){
  65. if($v!=end($data['option_value'])){
  66. $option['value'].=$v['name'].',';
  67. }else{
  68. $option['value'].=$v['name'];
  69. }
  70. }
  71. }
  72. $r=M('option')->save($option);
  73. if($r){
  74. $all_option_value = M('option_value')->where(array('option_id'=>$data['id']))->select();
  75. $all_option_ids = array();
  76. foreach($all_option_value as $val)
  77. {
  78. $all_option_ids[$val['option_value_id']] = $val['option_value_id'];
  79. }
  80. //M('option_value')->where(array('option_id'=>$data['id']))->delete();
  81. foreach ($data['option_value'] as $k => $v) {
  82. if(!empty($v)){
  83. //option_value_id
  84. $value['option_id']=$data['id'];
  85. $value['value_name']=$v['name'];
  86. $value['value_sort_order']=$v['sort_order'];
  87. if(isset($v['option_value_id']) && $v['option_value_id'] >0)
  88. {
  89. unset($all_option_ids[$v['option_value_id']]);
  90. M('OptionValue')->where(array('option_value_id' => $v['option_value_id']))->save($value);
  91. }else{
  92. M('OptionValue')->add($value);
  93. }
  94. }
  95. }
  96. if(!empty($all_option_ids))
  97. {
  98. M('option_value')->where(array('option_value_id'=>array('in',$all_option_ids )))->delete();
  99. }
  100. return true;
  101. }else{
  102. return false;
  103. }
  104. }
  105. function getOptions($filter_name,$store_id = 0) {
  106. $sql = "SELECT * FROM ".C('DB_PREFIX') . "option";
  107. $sql .= ' WHERE 1= 1 ';
  108. if (isset($filter_name) && !is_null($filter_name)) {
  109. $sql .= " and name LIKE '" . $filter_name . "%'";
  110. }
  111. if($store_id > 0)
  112. {
  113. $sql .= " and store_id = {$store_id} ";
  114. }
  115. $query = M()->query($sql);
  116. return $query;
  117. }
  118. function getOptionValues($option_id) {
  119. $option_value_data = array();
  120. $option_value_query = M()->query("SELECT * FROM "
  121. . C('DB_PREFIX') . "option_value ov LEFT JOIN "
  122. . C('DB_PREFIX') . "option o ON (ov.option_id = o.option_id) WHERE ov.option_id ="
  123. . (int)$option_id);
  124. foreach ($option_value_query as $option_value) {
  125. $option_value_data[] = array(
  126. 'option_value_id' => $option_value['option_value_id'],
  127. 'name' => $option_value['name'],
  128. 'value' => $option_value['value_name'],
  129. 'sort_order' => $option_value['value_sort_order']
  130. );
  131. }
  132. return $option_value_data;
  133. }
  134. }
  135. ?>