protected function handle() { $request = $this->getRequest(); $columns = array('ID', '名称', '状态', '分组数', '版块数', '主题数', '回复数', '创建', '更新'); $fields = array('id', 'name', 'status', 'group_count', 'board_count', 'thread_count', 'post_count', 'create_timestamp', 'update_timestamp'); if ($request->isXmlHttpRequest()) { $posts = $request->request; $page_offset = $posts->get('start'); $page_offset = intval($page_offset); $page_size = $posts->get('length'); $page_size = intval($page_size); $is_all = false; if ($page_size < 0) { $is_all = true; } $s_echo = $posts->get('draw'); $s_echo = intval($s_echo); $search = $posts->get('search'); $search_value = $search['value']; $records = array(); $records['data'] = array(); $records['draw'] = $s_echo; $records['recordsTotal'] = 0; $records['recordsFiltered'] = 0; if ($is_all) { $page_size = ForumModel::getCount(function (QueryBuilder $qb) use($search_value) { if ($search_value) { $qb->orWhere($qb->expr()->like("`name`", ":name"))->setParameter(":name", "%{$search_value}%"); } }); } $page = $page_offset / $page_size + 1; $pager = ForumModel::listForums($page, $page_size, function (QueryBuilder $qb) use($search_value) { if ($search_value) { $qb->orWhere($qb->expr()->like("`name`", ":name"))->setParameter(":name", "%{$search_value}%"); } $qb->addOrderBy('create_timestamp', 'desc'); }); $total = $pager->getCount(); $records['recordsTotal'] = $total; $records['recordsFiltered'] = $total; $data = $pager->getData(); foreach ($data as $k => $v) { $line = array(); $line[] = '<input type="checkbox" name="id[]" value="' . $v['id'] . '">'; foreach ($fields as $field) { if (isset($v[$field])) { if ($field == 'create_timestamp' || $field == 'update_timestamp') { // 时间 $line[] = date('Y-m-d H:i:s', $v[$field]); } elseif ($field == 'status') { if ($v[$field] == 1) { // 可用 $line[] = '<span class="text-success">有效</span>'; } elseif ($v[$field] == 0) { // 不可用 $line[] = '<span class="text-danger">无效</span>'; } } else { $line[] = $v[$field]; } } } $edit_url = $this->generateUrl('admin_forum_edit', array('id' => $v['id'])); $group_url = $this->generateUrl('admin_forum_group_index', array('forum_id' => $v['id'])); $operation = '<div class="btn-group"> <a href="javascript:void(0);" data-toggle="dropdown" aria-expanded="false">操作 <i class="fa fa-angle-down"></i></a> <ul class="dropdown-menu pull-right" role="menu"> <li role="presentation"><a href="' . $edit_url . '"><i class="fa fa-edit"></i> 编辑</a></li> <li role="presentation"><a href="' . $group_url . '"><i class="fa fa-wrench"></i> 管理</a></li> </ul> </div>'; $line[] = $operation; $records['data'][] = $line; } return new JsonResponse($records); } return $this->render('forum/index.html.twig', array('columns' => $columns)); }