Link.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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\Link as LinkModel;
  15. /**
  16. * 友情链接控制器
  17. * @package app\cms\admin
  18. */
  19. class Link 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 = LinkModel::where($map)->order($order)->paginate();
  34. // 使用ZBuilder快速创建数据表格
  35. return ZBuilder::make('table')
  36. ->setSearch(['title' => '标题']) // 设置搜索框
  37. ->addColumns([ // 批量添加数据列
  38. ['id', 'ID'],
  39. ['title', '标题', 'text.edit'],
  40. ['url', '链接', 'text.edit'],
  41. ['type', '类型', 'text', '', [1 => '文字链接', 2 => '图片链接']],
  42. ['create_time', '创建时间', 'datetime'],
  43. ['update_time', '更新时间', 'datetime'],
  44. ['status', '状态', 'switch'],
  45. ['right_button', '操作', 'btn']
  46. ])
  47. ->addTopButtons('add,enable,disable,delete') // 批量添加顶部按钮
  48. ->addRightButtons(['edit', 'delete' => ['data-tips' => '删除后无法恢复。']]) // 批量添加右侧按钮
  49. ->addOrder('id,title,type,create_time,update_time')
  50. ->setRowList($data_list) // 设置表格数据
  51. ->addValidate('Link', 'title,url')
  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, 'Link');
  67. if(true !== $result) $this->error($result);
  68. if ($link = LinkModel::create($data)) {
  69. // 记录行为
  70. action_log('link_add', 'cms_link', $link['id'], UID, $data['title']);
  71. $this->success('新增成功', 'index');
  72. } else {
  73. $this->error('新增失败');
  74. }
  75. }
  76. // 显示添加页面
  77. return ZBuilder::make('form')
  78. ->addFormItems([
  79. ['radio', 'type', '链接类型', '', [1 => '文字链接', 2 => '图片链接'], 1],
  80. ['text', 'title', '链接标题'],
  81. ['text', 'url', '链接地址', '请以 <code>http</code> 或 <code>https</code>开头'],
  82. ['image', 'logo', '链接LOGO'],
  83. ['tags', 'keywords', '关键词'],
  84. ['textarea', 'contact', '联系方式'],
  85. ['text', 'sort', '排序', '', 100],
  86. ['radio', 'status', '立即启用', '', ['否', '是'], 1]
  87. ])
  88. ->setTrigger('type', 2, 'logo')
  89. ->fetch();
  90. }
  91. /**
  92. * 编辑
  93. * @param null $id 链接id
  94. * @author 蔡伟明 <314013107@qq.com>
  95. */
  96. public function edit($id = null)
  97. {
  98. if ($id === null) $this->error('缺少参数');
  99. // 保存数据
  100. if ($this->request->isPost()) {
  101. // 表单数据
  102. $data = $this->request->post();
  103. // 验证
  104. $result = $this->validate($data, 'Link');
  105. if(true !== $result) $this->error($result);
  106. if (LinkModel::update($data)) {
  107. // 记录行为
  108. action_log('link_edit', 'cms_link', $id, UID, $data['title']);
  109. $this->success('编辑成功', 'index');
  110. } else {
  111. $this->error('编辑失败');
  112. }
  113. }
  114. $info = LinkModel::get($id);
  115. // 显示编辑页面
  116. return ZBuilder::make('form')
  117. ->addFormItems([
  118. ['hidden', 'id'],
  119. ['radio', 'type', '链接类型', '', [1 => '文字链接', 2 => '图片链接']],
  120. ['text', 'title', '链接标题'],
  121. ['text', 'url', '链接地址', '请以 <code>http</code> 或 <code>https</code>开头'],
  122. ['image', 'logo', '链接LOGO'],
  123. ['tags', 'keywords', '关键词'],
  124. ['textarea', 'contact', '联系方式'],
  125. ['text', 'sort', '排序'],
  126. ['radio', 'status', '立即启用', '', ['否', '是']]
  127. ])
  128. ->setTrigger('type', 2, 'logo')
  129. ->setFormData($info)
  130. ->fetch();
  131. }
  132. /**
  133. * 删除友情链接
  134. * @param array $record 行为日志
  135. * @author 蔡伟明 <314013107@qq.com>
  136. * @return mixed
  137. */
  138. public function delete($record = [])
  139. {
  140. return $this->setStatus('delete');
  141. }
  142. /**
  143. * 启用友情链接
  144. * @param array $record 行为日志
  145. * @author 蔡伟明 <314013107@qq.com>
  146. * @return mixed
  147. */
  148. public function enable($record = [])
  149. {
  150. return $this->setStatus('enable');
  151. }
  152. /**
  153. * 禁用友情链接
  154. * @param array $record 行为日志
  155. * @author 蔡伟明 <314013107@qq.com>
  156. * @return mixed
  157. */
  158. public function disable($record = [])
  159. {
  160. return $this->setStatus('disable');
  161. }
  162. /**
  163. * 设置友情链接状态:删除、禁用、启用
  164. * @param string $type 类型:delete/enable/disable
  165. * @param array $record
  166. * @author 蔡伟明 <314013107@qq.com>
  167. * @return mixed
  168. */
  169. public function setStatus($type = '', $record = [])
  170. {
  171. $ids = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
  172. $link_title = LinkModel::where('id', 'in', $ids)->column('title');
  173. return parent::setStatus($type, ['link_'.$type, 'cms_link', 0, UID, implode('、', $link_title)]);
  174. }
  175. /**
  176. * 快速编辑
  177. * @param array $record 行为日志
  178. * @author 蔡伟明 <314013107@qq.com>
  179. * @return mixed
  180. */
  181. public function quickEdit($record = [])
  182. {
  183. $id = input('post.pk', '');
  184. $field = input('post.name', '');
  185. $value = input('post.value', '');
  186. $link = LinkModel::where('id', $id)->value($field);
  187. $details = '字段(' . $field . '),原值(' . $link . '),新值:(' . $value . ')';
  188. return parent::quickEdit(['link_edit', 'cms_link', $id, UID, $details]);
  189. }
  190. }