LinkController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2018年3月1日
  7. * 友情链接控制器
  8. */
  9. namespace app\admin\controller\content;
  10. use core\basic\Controller;
  11. use app\admin\model\content\LinkModel;
  12. class LinkController extends Controller
  13. {
  14. private $model;
  15. public function __construct()
  16. {
  17. $this->model = new LinkModel();
  18. }
  19. // 友情链接列表
  20. public function index()
  21. {
  22. if ((! ! $id = get('id', 'int')) && $result = $this->model->getLink($id)) {
  23. $this->assign('more', true);
  24. $this->assign('link', $result);
  25. } else {
  26. $this->assign('list', true);
  27. if (! ! ($field = get('field', 'var')) && ! ! ($keyword = get('keyword', 'vars'))) {
  28. $result = $this->model->findLink($field, $keyword);
  29. } else {
  30. $result = $this->model->getList();
  31. }
  32. $this->assign('gids', $this->model->getGid());
  33. $this->assign('links', $result);
  34. }
  35. $this->display('content/link.html');
  36. }
  37. // 友情链接增加
  38. public function add()
  39. {
  40. if ($_POST) {
  41. // 获取数据
  42. $gid = post('gid', 'int');
  43. $name = post('name');
  44. $link = post('link');
  45. $logo = post('logo');
  46. $sorting = post('sorting');
  47. if (! $gid) {
  48. $gid = $this->model->getMaxGid() + 1;
  49. }
  50. if (! $name) {
  51. alert_back('名称不能为空!');
  52. }
  53. if (! $link) {
  54. alert_back('链接不能为空!');
  55. }
  56. if (! $sorting) {
  57. $sorting = 255;
  58. }
  59. // logo图缩放
  60. if ($logo) {
  61. resize_img(ROOT_PATH . $logo, '', $this->config('ico.max_width'), $this->config('ico.max_height'));
  62. }
  63. // 构建数据
  64. $data = array(
  65. 'acode' => session('acode'),
  66. 'gid' => $gid,
  67. 'name' => $name,
  68. 'link' => $link,
  69. 'logo' => $logo,
  70. 'sorting' => $sorting,
  71. 'create_user' => session('username'),
  72. 'update_user' => session('username')
  73. );
  74. // 执行添加
  75. if ($this->model->addLink($data)) {
  76. $this->log('新增友情链接成功!');
  77. if (! ! $backurl = get('backurl')) {
  78. success('新增成功!', base64_decode($backurl));
  79. } else {
  80. success('新增成功!', url('/admin/Link/index'));
  81. }
  82. } else {
  83. $this->log('新增友情链接失败!');
  84. error('新增失败!', - 1);
  85. }
  86. }
  87. }
  88. // 友情链接删除
  89. public function del()
  90. {
  91. if (! $id = get('id', 'int')) {
  92. error('传递的参数值错误!', - 1);
  93. }
  94. if ($this->model->delLink($id)) {
  95. $this->log('删除友情链接' . $id . '成功!');
  96. success('删除成功!', - 1);
  97. } else {
  98. $this->log('删除友情链接' . $id . '失败!');
  99. error('删除失败!', - 1);
  100. }
  101. }
  102. // 友情链接修改
  103. public function mod()
  104. {
  105. if (! ! $submit = post('submit')) {
  106. switch ($submit) {
  107. case 'sorting': // 修改列表排序
  108. $listall = post('listall');
  109. if ($listall) {
  110. $sorting = post('sorting');
  111. foreach ($listall as $key => $value) {
  112. if ($sorting[$key] === '' || ! is_numeric($sorting[$key]))
  113. $sorting[$key] = 255;
  114. $this->model->modLink($value, "sorting=" . $sorting[$key]);
  115. }
  116. $this->log('批量修改链接排序成功!');
  117. success('修改成功!', - 1);
  118. } else {
  119. alert_back('排序失败,无任何内容!');
  120. }
  121. break;
  122. }
  123. }
  124. if (! $id = get('id', 'int')) {
  125. error('传递的参数值错误!', - 1);
  126. }
  127. // 单独修改状态
  128. if (($field = get('field', 'var')) && ! is_null($value = get('value', 'var'))) {
  129. if ($this->model->modLink($id, "$field='$value',update_user='" . session('username') . "'")) {
  130. location(- 1);
  131. } else {
  132. alert_back('修改失败!');
  133. }
  134. }
  135. // 修改操作
  136. if ($_POST) {
  137. // 获取数据
  138. $gid = post('gid', 'int');
  139. $name = post('name');
  140. $link = post('link');
  141. $logo = post('logo');
  142. $sorting = post('sorting');
  143. if (! $gid) {
  144. $gid = $this->model->getMaxGid() + 1;
  145. }
  146. if (! $name) {
  147. alert_back('名称不能为空!');
  148. }
  149. if (! $link) {
  150. alert_back('链接不能为空!');
  151. }
  152. // logo图缩放
  153. if ($logo) {
  154. resize_img(ROOT_PATH . $logo, '', $this->config('ico.max_width'), $this->config('ico.max_height'));
  155. }
  156. // 构建数据
  157. $data = array(
  158. 'gid' => $gid,
  159. 'name' => $name,
  160. 'link' => $link,
  161. 'logo' => $logo,
  162. 'sorting' => $sorting,
  163. 'update_user' => session('username')
  164. );
  165. // 执行添加
  166. if ($this->model->modLink($id, $data)) {
  167. $this->log('修改友情链接' . $id . '成功!');
  168. if (! ! $backurl = get('backurl')) {
  169. success('修改成功!', base64_decode($backurl));
  170. } else {
  171. success('修改成功!', url('/admin/Link/index'));
  172. }
  173. } else {
  174. location(- 1);
  175. }
  176. } else {
  177. // 调取修改内容
  178. $this->assign('mod', true);
  179. if (! $result = $this->model->getLink($id)) {
  180. error('编辑的内容已经不存在!', - 1);
  181. }
  182. $this->assign('gids', $this->model->getGid());
  183. $this->assign('link', $result);
  184. $this->display('content/link.html');
  185. }
  186. }
  187. }