CulturePetTypeController.class.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. namespace Admin\Controller;
  3. /**
  4. * 宠物类型管理
  5. * @author 晓宇
  6. * @version 0.0.1
  7. * @datetime 2019-03-29
  8. */
  9. class CulturePetTypeController extends CommonController
  10. {
  11. /**
  12. * [_initialize 前置操作-继承公共前置方法]
  13. */
  14. public function _initialize()
  15. {
  16. // 调用父类前置方法
  17. parent::_initialize();
  18. // 登录校验
  19. $this->Is_Login();
  20. // 权限校验
  21. $this->Is_Power();
  22. }
  23. /**
  24. * [Index 宠物类型列表]
  25. */
  26. public function Index()
  27. {
  28. // 参数
  29. $param = array_merge($_POST, $_GET);
  30. // 模型模型
  31. $m = D('Culture/CulturePetType');
  32. // 条件
  33. $where = $this->GetIndexWhere();
  34. // 分页
  35. $number = MyC('admin_page_number');
  36. $page_param = array(
  37. 'number' => $number,
  38. 'total' => $m->where($where)->count(),
  39. 'where' => $param,
  40. 'url' => U('Culture/CulturePetType/Index'),
  41. );
  42. $page = new \My\Page($page_param);
  43. // 获取列表
  44. $field = array('pet_type_id', 'pet_type_name', 'thumb', 'description', 'created_at', 'updated_at');
  45. $list = $this->SetDataHandle($m->field($field)->where($where)->limit($page->GetPageStarNumber(), $number)->order('pet_type_id desc')->select());
  46. // 参数
  47. $this->assign('param', $param);
  48. // 分页
  49. $this->assign('page_html', $page->GetPageHtml());
  50. // 数据列表
  51. $this->assign('list', $list);
  52. $this->display('Index');
  53. }
  54. /**
  55. * [SetDataHandle 数据处理]
  56. * @param [array] $data [宠物类型数据]
  57. * @return [array] [处理好的数据]
  58. */
  59. private function SetDataHandle($data)
  60. {
  61. return $data;
  62. }
  63. /**
  64. * [GetIndexWhere 宠物类型列表条件]
  65. */
  66. private function GetIndexWhere()
  67. {
  68. $where = array();
  69. // 模糊
  70. if(!empty($_REQUEST['keyword']))
  71. {
  72. $like_keyword = array('like', '%'.I('keyword').'%');
  73. $where[] = array(
  74. 'pet_type_name' => $like_keyword,
  75. );
  76. }
  77. // 是否更多条件
  78. if(I('is_more', 0) == 1)
  79. {
  80. // 表达式
  81. if(!empty($_REQUEST['time_start']))
  82. {
  83. $where['created_at'][] = array('gt', I('time_start') . ' 00:00:00');
  84. }
  85. if(!empty($_REQUEST['time_end']))
  86. {
  87. $where['created_at'][] = array('lt', I('time_end') . ' 23:59:59');
  88. }
  89. }
  90. $where['deleted_at'] = ["exp", "is null"];
  91. return $where;
  92. }
  93. /**
  94. * [SaveInfo 宠物类型添加/编辑页面]
  95. */
  96. public function SaveInfo()
  97. {
  98. // 宠物类型信息
  99. $data = empty($_REQUEST['pet_type_id']) ? array() : D('Culture/CulturePetType')->find(I('pet_type_id'));
  100. $this->assign('data', $data);
  101. // 场景信息
  102. $scene_list = D('Culture/CultureScene')->field("scene_id, scene_name")->where($map)->select();
  103. $this->assign('scene_list', $scene_list);
  104. $this->display('SaveInfo');
  105. }
  106. /**
  107. * [Save 宠物类型添加/编辑]
  108. */
  109. public function Save()
  110. {
  111. // 是否ajax请求
  112. if(!IS_AJAX)
  113. {
  114. $this->error(L('common_unauthorized_access'));
  115. }
  116. // 添加
  117. if(empty($_POST['pet_type_id']))
  118. {
  119. $this->Add();
  120. // 编辑
  121. } else {
  122. $this->Edit();
  123. }
  124. }
  125. /**
  126. * [Add 宠物类型添加]
  127. */
  128. private function Add()
  129. {
  130. // 宠物类型模型
  131. $m = D('Culture/CulturePetType');
  132. // 数据自动校验
  133. if($m->create($_POST, 1))
  134. {
  135. $m->created_at = date('Y-m-d H:i:s');
  136. $m->updated_at = date('Y-m-d H:i:s');
  137. // 数据添加
  138. if($m->add())
  139. {
  140. $this->ajaxReturn(L('common_operation_add_success'));
  141. } else {
  142. $this->ajaxReturn(L('common_operation_add_error'), -100);
  143. }
  144. } else {
  145. $this->ajaxReturn($m->getError(), -1);
  146. }
  147. }
  148. /**
  149. * [Edit 宠物类型编辑]
  150. */
  151. private function Edit()
  152. {
  153. // 宠物类型模型
  154. $m = D('Culture/CulturePetType');
  155. // 数据自动校验
  156. if($m->create($_POST, 2))
  157. {
  158. $m->updated_at = date('Y-m-d H:i:s');
  159. // 更新数据库
  160. if($m->where(array('pet_type_id'=>I('pet_type_id')))->save())
  161. {
  162. $this->ajaxReturn(L('common_operation_edit_success'));
  163. } else {
  164. $this->ajaxReturn(L('common_operation_edit_error'), -100);
  165. }
  166. } else {
  167. $this->ajaxReturn($m->getError(), -1);
  168. }
  169. }
  170. /**
  171. * [Delete 宠物类型删除]
  172. */
  173. public function Delete()
  174. {
  175. // 是否ajax请求
  176. if(!IS_AJAX)
  177. {
  178. $this->error(L('common_unauthorized_access'));
  179. }
  180. // 参数处理
  181. $id = I('id');
  182. // 删除数据
  183. if(!empty($id))
  184. {
  185. // 宠物类型模型
  186. $m = D('Culture/CulturePetType');
  187. // 宠物类型是否存在
  188. $scene = $m->where(array('pet_type_id'=>$id))->count();
  189. if(empty($scene))
  190. {
  191. $this->ajaxReturn(L('common_user_no_exist_error'), -2);
  192. }
  193. // 删除宠物类型
  194. $update_data['deleted_at'] = date('Y-m-d H:i:s');
  195. $state = $m->where(array('pet_type_id'=>$id))->save($update_data);
  196. if($state !== false)
  197. {
  198. $this->ajaxReturn(L('common_operation_delete_success'));
  199. } else {
  200. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  201. }
  202. } else {
  203. $this->ajaxReturn(L('common_param_error'), -1);
  204. }
  205. }
  206. }