AdvertType.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
  10. // +----------------------------------------------------------------------
  11. namespace app\cms\admin;
  12. use app\admin\controller\Admin;
  13. use app\common\builder\ZBuilder;
  14. use app\cms\model\AdvertType as AdvertTypeModel;
  15. /**
  16. * 广告分类控制器
  17. * @package app\cms\admin
  18. */
  19. class AdvertType extends Admin
  20. {
  21. /**
  22. * 广告列表
  23. * @author 蔡伟明 <314013107@qq.com>
  24. * @return mixed
  25. */
  26. public function index()
  27. {
  28. // 查询
  29. $map = $this->getMap();
  30. // 排序
  31. $order = $this->getOrder('update_time desc');
  32. // 数据列表
  33. $data_list = AdvertTypeModel::where($map)->order($order)->paginate();
  34. // 使用ZBuilder快速创建数据表格
  35. return ZBuilder::make('table')
  36. ->setSearch(['name' => '分类名称']) // 设置搜索框
  37. ->addColumns([ // 批量添加数据列
  38. ['id', 'ID'],
  39. ['name', '分类名称', 'text.edit'],
  40. ['create_time', '创建时间', 'datetime'],
  41. ['update_time', '更新时间', 'datetime'],
  42. ['status', '状态', 'switch'],
  43. ['right_button', '操作', 'btn']
  44. ])
  45. ->setTableName('cms_advert_type')
  46. ->addTopButton('back', ['href' => url('advert/index')]) // 批量添加顶部按钮
  47. ->addTopButtons('add,enable,disable,delete') // 批量添加顶部按钮
  48. ->addRightButtons(['edit', 'delete' => ['data-tips' => '删除后无法恢复。']]) // 批量添加右侧按钮
  49. ->addOrder('id,name,create_time,update_time')
  50. ->setRowList($data_list) // 设置表格数据
  51. ->addValidate('AdvertType', 'name')
  52. ->fetch(); // 渲染模板
  53. }
  54. /**
  55. * 新增
  56. * @author 蔡伟明 <314013107@qq.com>
  57. * @return mixed
  58. */
  59. public function add()
  60. {
  61. // 保存数据
  62. if ($this->request->isPost()) {
  63. // 表单数据
  64. $data = $this->request->post();
  65. // 验证
  66. $result = $this->validate($data, 'AdvertType');
  67. if(true !== $result) $this->error($result);
  68. if ($type = AdvertTypeModel::create($data)) {
  69. // 记录行为
  70. action_log('advert_type_add', 'cms_advert_type', $type['id'], UID, $data['name']);
  71. $this->success('新增成功', 'index');
  72. } else {
  73. $this->error('新增失败');
  74. }
  75. }
  76. // 显示添加页面
  77. return ZBuilder::make('form')
  78. ->setPageTips('如果出现无法添加的情况,可能由于浏览器将本页面当成了广告,请尝试关闭浏览器的广告过滤功能再试。', 'warning')
  79. ->addFormItems([
  80. ['text', 'name', '分类名称'],
  81. ['radio', 'status', '立即启用', '', ['否', '是'], 1]
  82. ])
  83. ->fetch();
  84. }
  85. /**
  86. * 编辑
  87. * @param null $id 广告分类id
  88. * @author 蔡伟明 <314013107@qq.com>
  89. */
  90. public function edit($id = null)
  91. {
  92. if ($id === null) $this->error('缺少参数');
  93. // 保存数据
  94. if ($this->request->isPost()) {
  95. // 表单数据
  96. $data = $this->request->post();
  97. // 验证
  98. $result = $this->validate($data, 'AdvertType');
  99. if(true !== $result) $this->error($result);
  100. if (AdvertTypeModel::update($data)) {
  101. // 记录行为
  102. action_log('advert_type_edit', 'cms_advert_type', $id, UID, $data['name']);
  103. $this->success('编辑成功', 'index');
  104. } else {
  105. $this->error('编辑失败');
  106. }
  107. }
  108. $info = AdvertTypeModel::get($id);
  109. // 显示编辑页面
  110. return ZBuilder::make('form')
  111. ->setPageTips('如果出现无法编辑的情况,可能由于浏览器将本页面当成了广告,请尝试关闭浏览器的广告过滤功能再试。', 'warning')
  112. ->addFormItems([
  113. ['hidden', 'id'],
  114. ['text', 'name', '分类名称'],
  115. ['radio', 'status', '立即启用', '', ['否', '是']]
  116. ])
  117. ->setFormdata($info)
  118. ->fetch();
  119. }
  120. /**
  121. * 删除广告分类
  122. * @param array $record 行为日志
  123. * @author 蔡伟明 <314013107@qq.com>
  124. * @return mixed
  125. */
  126. public function delete($record = [])
  127. {
  128. return $this->setStatus('delete');
  129. }
  130. /**
  131. * 启用广告分类
  132. * @param array $record 行为日志
  133. * @author 蔡伟明 <314013107@qq.com>
  134. * @return mixed
  135. */
  136. public function enable($record = [])
  137. {
  138. return $this->setStatus('enable');
  139. }
  140. /**
  141. * 禁用广告分类
  142. * @param array $record 行为日志
  143. * @author 蔡伟明 <314013107@qq.com>
  144. * @return mixed
  145. */
  146. public function disable($record = [])
  147. {
  148. return $this->setStatus('disable');
  149. }
  150. /**
  151. * 设置广告分类状态:删除、禁用、启用
  152. * @param string $type 类型:delete/enable/disable
  153. * @param array $record 日志记录
  154. * @author 蔡伟明 <314013107@qq.com>
  155. * @return mixed
  156. */
  157. public function setStatus($type = '', $record = [])
  158. {
  159. $ids = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
  160. $type_name = AdvertTypeModel::where('id', 'in', $ids)->column('name');
  161. return parent::setStatus($type, ['advert_type_'.$type, 'cms_advert_type', 0, UID, implode('、', $type_name)]);
  162. }
  163. /**
  164. * 快速编辑
  165. * @param array $record 行为日志
  166. * @author 蔡伟明 <314013107@qq.com>
  167. * @return mixed
  168. */
  169. public function quickEdit($record = [])
  170. {
  171. $id = input('post.pk', '');
  172. $field = input('post.name', '');
  173. $value = input('post.value', '');
  174. $type = AdvertTypeModel::where('id', $id)->value($field);
  175. $details = '字段(' . $field . '),原值(' . $type . '),新值:(' . $value . ')';
  176. return parent::quickEdit(['advert_type_edit', 'cms_advert_type', $id, UID, $details]);
  177. }
  178. }