Example #1
0
 public function __invoke($id = null)
 {
     $request = $this->getRequest();
     $a_page = $request->query->get('a_page');
     if (!$a_page) {
         $a_page = 1;
     }
     $a_size = $request->query->get('a_size');
     $article = ArticleModel::getArticle($id);
     $article = $article->toArray();
     // 取出文章的tag标签
     $article_tags = ArticleTagModel::allRelationship(function (QueryBuilder $qb) use($article) {
         $qb->andWhere($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article['id']);
     });
     $article['tags'] = array();
     foreach ($article_tags as $article_tag) {
         $t = TagModel::getTag($article_tag['tag_id']);
         $article['tags'][] = $t->toArray();
     }
     // 分页
     if (!$a_size) {
         $content_pager = explode('<!-- pagebreak -->', $article['content']);
     } else {
         // 全文
         $content_pager = array(str_replace('<!-- pagebreak -->', '', $article['content']));
     }
     $article['content'] = $content_pager[$a_page - 1];
     if (!$a_size) {
         $count = count($content_pager);
     } else {
         $count = 0;
     }
     $a_pager = new Pager(array(), $count, $a_page, 1);
     $a_pager->setPageParamName('a_page');
     // 文章的发布者
     $article['publisher'] = array();
     $article_publishers = ArticlePublisherModel::allRelationship(function (QueryBuilder $qb) use($article) {
         $qb->andWhere($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article['id']);
     });
     if ($article_publishers) {
         $article_publisher = array_shift($article_publishers);
         $publisher_id = $article_publisher['publisher_id'];
         $publisher = PublisherModel::getPublisher($publisher_id);
         if ($publisher) {
             $article['publisher'] = $publisher;
         }
     }
     // 轮播图
     $carousels = MediaModel::allMedias(function (QueryBuilder $qb) use($article) {
         $qb->andWhere($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article['id']);
         $qb->andWhere($qb->expr()->eq('type', ':type'))->setParameter(':type', 'carousel');
     });
     foreach ($carousels as $k => $v) {
         $metadata = json_decode($v['metadata'], true);
         unset($v['metadata']);
         $v['description'] = '';
         if ($metadata) {
             $v['description'] = $metadata['description'];
         }
         $carousels[$k] = $v;
     }
     // 所有的插件
     $data = PluginModel::allPlugins();
     $plugins = array();
     foreach ($data as $row) {
         $row['sources'] = json_decode($row['sources'], true);
         $row['options'] = json_decode($row['options'], true);
         $row['apply_code'] = htmlspecialchars_decode($row['apply_code']);
         $plugins[$row['id']] = $row;
     }
     // 文章的插件
     $data = ArticlePluginModel::allRelationship(function (QueryBuilder $qb) use($article) {
         $qb->andWhere($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article['id']);
     });
     foreach ($data as $row) {
         $row['sources'] = json_decode($row['sources'], true);
         $row['options'] = json_decode($row['options'], true);
         $row['apply_code'] = htmlspecialchars_decode($row['apply_code']);
         if (isset($plugins[$row['plugin_id']])) {
             $plugins[$row['plugin_id']] = $row;
         }
     }
     // 评论
     $c_page = $request->query->get('c_page');
     if (!$c_page) {
         $c_page = 1;
     }
     $c_size = $request->query->get('c_size');
     if (!$c_size) {
         $c_size = 3;
     }
     $c_pager = CommentModel::listComments($c_page, $c_size, function (QueryBuilder $qb) use($article) {
         $qb->andWhere($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article['id']);
         $qb->addOrderBy('create_timestamp');
     });
     $comments_data = $c_pager->getData();
     foreach ($comments_data as $k => $comment_data) {
         // 获取用户数据
         $comment_user_id = $comment_data['user_id'];
         /** @var \Tachigo\User\Aware\Component\UserAwareDispatcher $dispatcher */
         $dispatcher = $this->getContainer()->tag('tachigo_user_dispatcher');
         /** @var \Tachigo\User\Aware\Hook\UserHook $hook */
         $hook = $this->getContainer()->tag('tachigo_user_hook.user');
         $hook->setUserId($comment_user_id);
         $dispatcher->fire($hook);
         $comment_data['user'] = $hook->getResults();
         $comment_data['reply'] = array();
         if ($comment_data['comment_id']) {
             $reply_comment = CommentModel::getComment($comment_data['comment_id'])->toArray();
             $comment_user_id = $reply_comment['user_id'];
             /** @var \Tachigo\User\Aware\Component\UserAwareDispatcher $dispatcher */
             $dispatcher = $this->getContainer()->tag('tachigo_user_dispatcher');
             /** @var \Tachigo\User\Aware\Hook\UserHook $hook */
             $hook = $this->getContainer()->tag('tachigo_user_hook.user');
             $hook->setUserId($comment_user_id);
             $dispatcher->fire($hook);
             $reply_comment['user'] = $hook->getResults();
             $comment_data['reply'] = $reply_comment;
         }
         $comments_data[$k] = $comment_data;
     }
     $c_pager->setData($comments_data);
     $c_pager->setPageParamName('c_page');
     // 分页内容
     $pager_query = array('a_page' => $a_page, 'a_size' => $a_size, 'c_page' => $c_page, 'c_size' => $c_size);
     $a_pager->setQuery($pager_query);
     $c_pager->setQuery($pager_query);
     // 导航条
     $structure_id = $article['structure_id'];
     $structure = StructureModel::getStructure($structure_id);
     /** @var StructureModel[] $breadcrumbs */
     $breadcrumbs = $structure->getNodeSinglePath();
     array_shift($breadcrumbs);
     return $this->render('article/blog.html.twig', array('article' => $article, 'a_pager' => $a_pager, 'c_pager' => $c_pager, 'carousels' => $carousels, 'plugins' => $plugins, 'breadcrumbs' => $breadcrumbs));
 }
Example #2
0
 protected function actionAjax1()
 {
     $request = $this->getRequest();
     $container = $this->getContainer();
     /** @var \Zero\Module\Metronic\AdminModule\Database\MySQL\TestDatabase $db */
     $db = $container->tag('metronic_test_db');
     // 查询 information_schema的schemata表
     $table_name = $db->getTableName('schemata');
     $schema_sql = "show columns from {$table_name}";
     $query = $db->getSelectQuery($schema_sql);
     $schema_data = $query->getResult();
     $data_table = array('head' => array(), 'body' => array());
     $fields = array();
     foreach ($schema_data as $i => $row) {
         if ($i < 5) {
             $data_table['head'][] = $row['Field'];
             $fields[] = $row['Field'];
         }
     }
     $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;
     $columns = $fields;
     $orders = $posts->get('order');
     $sorts = array();
     foreach ($orders as $order) {
         $col = $order['column'];
         $dir = $order['dir'];
         $sorts[] = array('field' => $columns[$col], 'direction' => $dir);
     }
     $pager = null;
     // 直接搜索所有
     $sql = "select count(*) as `num` from {$table_name}";
     $query = $db->getSelectQuery($sql);
     $row = $query->getRow();
     $total = $row['num'];
     if ($is_all) {
         $page_size = $total;
     }
     $page = $page_offset / $page_size + 1;
     $qb = $db->createQueryBuilder();
     $qb->select($fields)->from($table_name)->setFirstResult(max(0, ($page - 1) * $page_size))->setMaxResults($page_size);
     if (!empty($sorts)) {
         foreach ($sorts as $sort) {
             $field = "`{$sort['field']}`";
             $direction = $sort['direction'];
             $qb->addOrderBy($field, $direction);
         }
     }
     if ($search_value) {
         foreach ($fields as $field) {
             $qb->orWhere($qb->expr()->eq("`{$field}`", ":{$field}"))->setParameter(":{$field}", $search_value);
         }
     }
     $sql = $qb->getSQL();
     $query = $db->getSelectQuery($sql, $qb->getParameters());
     $pager = new Pager($query->getResult(), $total, $page, $page_size);
     $total = $pager->getCount();
     $records['recordsTotal'] = $total;
     $records['recordsFiltered'] = $total;
     $data = $pager->getData();
     foreach ($data as $k => $v) {
         $line = array();
         foreach ($v as $kk => $vv) {
             if ($vv) {
                 $line[] = $vv;
             } else {
                 $line[] = '&nbsp;';
             }
         }
         $records['data'][] = $line;
     }
     return new JsonResponse($records);
 }