Esempio n. 1
0
 protected function handle()
 {
     // 系统中的未启用的插件
     $all_plugins = $this->getContainer()->getParameter('plugins');
     if ($all_plugins) {
         $all_plugins = $all_plugins->toArray();
     } else {
         $all_plugins = array();
     }
     if (!isset($all_plugins[$this->id])) {
         throw new \Exception('插件不存在');
     }
     $plugin = $all_plugins[$this->id];
     $plugin['id'] = $this->id;
     $plugin['apply_code'] = htmlspecialchars($plugin['apply_code']);
     $dep_s = $plugin['dependency'];
     $plugin['dependency'] = array();
     foreach ($dep_s as $dependent_plugin_id) {
         if (!isset($all_plugins[$dependent_plugin_id])) {
             throw new \Exception('依赖的插件不存在');
         }
         $dependency_plugin = $all_plugins[$dependent_plugin_id];
         $dependency_plugin['id'] = $dependent_plugin_id;
         $dependency_plugin['apply_code'] = htmlspecialchars($dependency_plugin['apply_code']);
         $plugin['dependency'][$dependent_plugin_id] = $dependency_plugin;
     }
     $used = PluginModel::getPlugin($this->id);
     $plugin['enabled'] = $used ? true : false;
     return $this->render('plugin/detail.html.twig', array('plugin' => $plugin));
 }
Esempio n. 2
0
 protected function handle()
 {
     $article_id = $this->id;
     // 插件
     $data = PluginModel::allPlugins();
     $plugins = array();
     foreach ($data as $row) {
         $plugins[$row['id']] = $row;
     }
     $data = ArticlePluginModel::allRelationship(function (QueryBuilder $qb) use($article_id) {
         $qb->andWhere($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article_id);
     });
     $article_plugins = array();
     foreach ($data as $info) {
         $info['options'] = json_decode($info['options'], true);
         $article_plugins[$info['plugin_id']] = $info;
     }
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         /*$posts = $request->request->all();
           foreach ($article_plugins as $plugin_id => $plugin_info) {
               $plugin_info['options']['value'] = $posts[$plugin_id];
               // json
               $plugin_info['options'] = json_encode($plugin_info['options']);
               // 保存
               ArticlePluginModel::editArticlePlugin($plugin_info);
           }*/
         $session = $this->getSession();
         $session->addFlash('success', '操作成功');
         return new RedirectResponse($this->generateUrl('admin_www_plugin_edit', array('id' => $article_id)));
     }
     return $this->render('plugin/edit.html.twig', array('plugins' => $plugins, 'article_plugins' => $article_plugins, 'article_id' => $article_id));
 }
Esempio n. 3
0
 protected function handle()
 {
     $request = $this->getRequest();
     $columns = array('ID', '名称', '描述', '资源', '代码', '依赖');
     $fields = array('id', 'name', 'description', 'sources', 'apply_code', 'dependency');
     // 系统中的未启用的插件
     $all_plugins = $this->getContainer()->getParameter('plugins');
     if ($all_plugins) {
         $all_plugins = $all_plugins->toArray();
     } else {
         $all_plugins = array();
     }
     $unused_plugins = array();
     $used_plugins = array();
     $data = PluginModel::allPlugins();
     foreach ($data as $row) {
         $used_plugins[] = $row['id'];
     }
     foreach ($all_plugins as $plugin_id => $plugin) {
         $plugin['apply_code'] = htmlspecialchars($plugin['apply_code']);
         if (!in_array($plugin_id, $used_plugins)) {
             $unused_plugins[$plugin_id] = $plugin;
         }
     }
     if ($request->isXmlHttpRequest()) {
         $posts = $request->request;
         $type = $posts->get('type');
         $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 = PluginModel::getCount(function (QueryBuilder $qb) use($search_value, $type) {
                 if ($search_value) {
                     $qb->orWhere($qb->expr()->like("`name`", ":name"))->setParameter(":name", "%{$search_value}%");
                     $qb->orWhere($qb->expr()->like("`id`", ":id"))->setParameter(":id", "%{$search_value}%");
                 }
             });
         }
         $page = $page_offset / $page_size + 1;
         $pager = PluginModel::listPlugins($page, $page_size, function (QueryBuilder $qb) use($search_value, $type) {
             if ($search_value) {
                 $qb->orWhere($qb->expr()->like("`name`", ":name"))->setParameter(":name", "%{$search_value}%");
                 $qb->orWhere($qb->expr()->like("`id`", ":id"))->setParameter(":id", "%{$search_value}%");
             }
         });
         $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 == 'sources') {
                         $v[$field] = json_decode($v[$field], true);
                         if (!$v[$field]) {
                             $v[$field] = array();
                         }
                         if (!$v[$field]) {
                             $line[] = '无';
                         } else {
                             $html = '';
                             if (count($v[$field]) > 1) {
                                 $sources = $v[$field];
                                 $html .= '<ul style="margin-bottom: 0px;" class="list-unstyled"><li>';
                                 $first_type = $sources[0]['type'];
                                 $first_url = $sources[0]['url'];
                                 $first_url = substr($first_url, 0, 9);
                                 $html .= "<strong>{$first_type}</strong>:<i>{$first_url}</i>...";
                                 $inner_html = '<dl>';
                                 foreach ($v[$field] as $source) {
                                     $inner_html .= '<dt>' . $source['desc'] . '(' . $source['type'] . ')</dt><dd>' . $source['url'] . '</dd>';
                                 }
                                 $inner_html .= '</dl>';
                                 $html .= '<i data-toggle="popover"
                                            data-placement="top"
                                            data-html="true"
                                            data-original-title="插件 ' . $v['name'] . '的资源"
                                            data-content="' . $inner_html . '"
                                            data-container="body"
                                            data-trigger="hover"
                                            class="popovers icon-question" style="cursor: pointer;"></i>';
                                 $html .= '</li></ul>';
                             } else {
                                 foreach ($v[$field] as $source) {
                                     $html .= "<strong>{$source['type']}</strong>:<i>{$source['url']}</i>";
                                 }
                             }
                             $line[] = $html;
                         }
                     } elseif ($field == 'apply_code') {
                         $html = '';
                         if ($v['apply_code']) {
                             $html .= substr($v['apply_code'], 0, 9) . '...';
                             $html .= '<i data-toggle="popover"
                                data-placement="top"
                                data-html="true"
                                data-original-title="插件 ' . $v['name'] . ' 的示例代码"
                                data-content="<pre>' . htmlspecialchars($v['apply_code']) . '</pre>"
                                data-container="body"
                                data-trigger="hover"
                                class="popovers icon-question" style="cursor: pointer;"></i>';
                         } else {
                             $html = '无';
                         }
                         $line[] = $html;
                     } elseif ($field == 'dependency') {
                         $v['dependency'] = json_decode($v['dependency'], true);
                         if (!$v['dependency']) {
                             $v['dependency'] = array();
                         }
                         $html = '';
                         if ($v['dependency']) {
                             if (count($v['dependency']) > 1) {
                                 $html .= '<ul style="margin-bottom: 0px;" class="list-unstyled"><li>';
                                 $first_plugin_id = $v['dependency'][0];
                                 $html .= '<i>' . $all_plugins[$first_plugin_id]['name'] . '...</i>';
                                 $inner_html = '<ul class="list-unstyled" style="margin-bottom: 0px;">';
                                 foreach ($v['dependency'] as $dependency_plugin_id) {
                                     if (isset($all_plugins[$dependency_plugin_id])) {
                                         $inner_html .= '<li>' . $all_plugins[$dependency_plugin_id]['name'] . '(' . $dependency_plugin_id . ')</li>';
                                     } else {
                                         $inner_html .= '<li>未知的插件 ' . $dependency_plugin_id . '</li>';
                                     }
                                 }
                                 $html .= '<i data-toggle="popover"
                                            data-placement="top"
                                            data-html="true"
                                            data-original-title="依赖插件"
                                            data-content="' . htmlspecialchars($inner_html) . '"
                                            data-container="body"
                                            data-trigger="hover"
                                            class="popovers icon-question" style="cursor: pointer;"></i>';
                                 $html .= '</li></ul>';
                             } else {
                                 foreach ($v['dependency'] as $dependency_plugin_id) {
                                     if (isset($all_plugins[$dependency_plugin_id])) {
                                         $html .= '<i>' . $all_plugins[$dependency_plugin_id]['name'] . '</i>';
                                     } else {
                                         $html .= '<i>未知的插件 ' . $dependency_plugin_id . '</i>';
                                     }
                                 }
                             }
                         } else {
                             $html = '无';
                         }
                         $line[] = $html;
                     } elseif ($field == 'description') {
                         $html = '<i>' . substr($v[$field], 0, 9) . '...</i>';
                         $html .= '<i data-toggle="popover"
                            data-placement="top"
                            data-html="true"
                            data-original-title="插件 ' . $v['name'] . ' 的描述"
                            data-content="<i>' . $v['description'] . '</i>"
                            data-container="body"
                            data-trigger="hover"
                            class="popovers icon-question" style="cursor: pointer;"></i>';
                         $line[] = $html;
                     } elseif ($field == 'name') {
                         $line[] = "<strong>{$v[$field]}</strong>";
                     } else {
                         $line[] = $v[$field];
                     }
                 }
             }
             // 删除url
             $delete_url = $this->generateUrl('admin_www_plugin_delete', array('ids' => array($v['id'])));
             // 详情url
             $detail_url = $this->generateUrl('admin_www_plugin_detail', array('id' => $v['id']));
             $line[] = '<a data-toggle="modal" data-target="#modal" href="' . $delete_url . '">弃用</a>
             &nbsp;<a data-toggle="modal" data-target="#modal" href="' . $detail_url . '">详情</a>';
             $records['data'][] = $line;
         }
         return new JsonResponse($records);
     }
     return $this->render('plugin/index.html.twig', array('columns' => $columns, 'all_plugins' => $all_plugins, 'plugins' => $unused_plugins));
 }
Esempio n. 4
0
 protected function handle()
 {
     // 系统中的未启用的插件
     $all_plugins = $this->getContainer()->getParameter('plugins');
     if ($all_plugins) {
         $all_plugins = $all_plugins->toArray();
     } else {
         $all_plugins = array();
     }
     $unused_plugins = array();
     $used_plugins = array();
     $data = PluginModel::allPlugins();
     foreach ($data as $row) {
         $row['dependency'] = json_decode($row['dependency'], true);
         $used_plugins[$row['id']] = $row;
     }
     foreach ($all_plugins as $plugin_id => $plugin) {
         $plugin['apply_code'] = htmlspecialchars($plugin['apply_code']);
         if (!in_array($plugin_id, $used_plugins)) {
             $unused_plugins[$plugin_id] = $plugin;
         }
     }
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $ids = $request->request->get('ids');
         $db = WWWDatabase::getDb();
         $session = $this->getSession();
         $db->transaction();
         try {
             $plugins = PluginModel::allPlugins(function (QueryBuilder $qb) use($ids) {
                 foreach ($ids as $k => $id) {
                     $qb->orWhere($qb->expr()->eq('id', ':id_' . $k))->setParameter(':id_' . $k, $id);
                 }
             });
             // 检查是否有依赖
             // 找出不删的插件
             $not_delete_used_plugins = array();
             foreach ($used_plugins as $used_plugin) {
                 if (!in_array($used_plugin['id'], $ids)) {
                     $not_delete_used_plugins[] = $used_plugin;
                 }
             }
             foreach ($not_delete_used_plugins as $not_delete_used_plugin) {
                 if (($intersect = array_intersect(array_values($not_delete_used_plugin['dependency']), array_values($ids))) != null) {
                     $dependent_plugin_ids = implode(',', $intersect);
                     throw new \Exception("插件'{$not_delete_used_plugin['id']}'依赖'{$dependent_plugin_ids}',不能弃用");
                 }
             }
             // 删除插件
             foreach ($plugins as $plugin) {
                 PluginModel::deletePlugin($plugin['id']);
                 // 删除插件与文章的关系
                 $article_plugins = ArticlePluginModel::allRelationship(function (QueryBuilder $qb) use($plugin) {
                     $qb->where($qb->expr()->eq('plugin_id', ':plugin_id'))->setParameter(':plugin_id', $plugin['id']);
                 });
                 foreach ($article_plugins as $article_plugin) {
                     ArticlePluginModel::deleteRelationship($article_plugin['article_id'], $plugin['id']);
                 }
             }
             $db->commit();
             $session->addFlash('success', '操作成功');
         } catch (\Exception $e) {
             $db->rollback();
             $session->addFlash('error', $e->getMessage());
         }
         return new RedirectResponse($this->generateUrl('admin_www_plugins'));
     } else {
         $ids = $request->query->get('ids');
         if (is_string($ids)) {
             $ids = json_decode($ids, true);
         }
         if (!$ids) {
             throw new \Exception('没有选择任何插件');
         }
         // 显示要删除的插件
         $plugins = PluginModel::allPlugins(function (QueryBuilder $qb) use($ids) {
             foreach ($ids as $k => $id) {
                 $qb->orWhere($qb->expr()->eq('id', ':id_' . $k))->setParameter(':id_' . $k, $id);
             }
         });
         // 检查是否有依赖
         // 找出不删的插件
         $not_delete_used_plugins = array();
         foreach ($used_plugins as $used_plugin) {
             if (!in_array($used_plugin['id'], $ids)) {
                 $not_delete_used_plugins[] = $used_plugin;
             }
         }
         foreach ($not_delete_used_plugins as $not_delete_used_plugin) {
             if (($intersect = array_intersect(array_values($not_delete_used_plugin['dependency']), array_values($ids))) != null) {
                 $dependent_plugin_ids = implode(',', $intersect);
                 throw new \Exception("插件'{$not_delete_used_plugin['id']}'依赖'{$dependent_plugin_ids}',不能弃用");
             }
         }
         return $this->render('plugin/delete.html.twig', array('plugins' => $plugins));
     }
 }
Esempio n. 5
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));
 }
Esempio n. 6
0
 protected function handle()
 {
     $all_plugins = $this->getContainer()->getParameter('plugins');
     if ($all_plugins) {
         $all_plugins = $all_plugins->toArray();
     } else {
         $all_plugins = array();
     }
     if (!isset($all_plugins[$this->id])) {
         throw new \Exception('插件不存在');
     }
     $unused_plugins = array();
     $used_plugins = array();
     $data = PluginModel::allPlugins();
     foreach ($data as $row) {
         $used_plugins[$row['id']] = $row;
     }
     if (isset($used_plugins[$this->id])) {
         throw new \Exception('插件已启用');
     }
     foreach ($all_plugins as $plugin_id => $plugin) {
         $plugin['id'] = $plugin_id;
         $plugin['apply_code'] = htmlspecialchars($plugin['apply_code']);
         $plugin['dependency'] = isset($plugin['dependency']) ? $plugin['dependency'] : array();
         if (!$plugin['dependency']) {
             $plugin['dependency'] = array();
         }
         if (!in_array($plugin_id, $used_plugins)) {
             $unused_plugins[$plugin_id] = $plugin;
         }
         $all_plugins[$plugin_id] = $plugin;
     }
     $plugin_info = $all_plugins[$this->id];
     $request = $this->getRequest();
     // 检查依赖
     $dependency_plugins = array();
     try {
         $dependency_plugins = $this->checkDependency($plugin_info, $all_plugins, $used_plugins);
         if ($request->getMethod() == 'POST') {
             $db = WWWDatabase::getDb();
             $session = $this->getSession();
             $db->transaction();
             try {
                 // 安装依赖的插件
                 foreach ($dependency_plugins as $dependency_plugin) {
                     if (!$dependency_plugin['enabled']) {
                         $plugin = new PluginModel();
                         $plugin->id = $dependency_plugin['id'];
                         $plugin->name = $dependency_plugin['name'];
                         $plugin->description = $dependency_plugin['description'];
                         $plugin->sources = json_encode($dependency_plugin['sources']);
                         $plugin->applyCode = $dependency_plugin['apply_code'];
                         $plugin->dependency = json_encode($dependency_plugin['dependency']);
                         $plugin->options = json_encode($dependency_plugin['options']);
                         // 保存
                         PluginModel::createPlugin($plugin);
                     }
                 }
                 // 安装指定插件
                 $plugin = new PluginModel();
                 $plugin->id = $plugin_info['id'];
                 $plugin->name = $plugin_info['name'];
                 $plugin->description = $plugin_info['description'];
                 $plugin->sources = json_encode($plugin_info['sources']);
                 $plugin->applyCode = $plugin_info['apply_code'];
                 $plugin->dependency = json_encode($plugin_info['dependency']);
                 $plugin->options = json_encode($plugin_info['options']);
                 PluginModel::createPlugin($plugin);
                 $db->commit();
                 $session->addFlash('success', '操作成功');
             } catch (\Exception $e) {
                 $db->rollback();
                 $session->addFlash('error', $e->getMessage());
             }
             return new RedirectResponse($this->generateUrl('admin_www_plugins'));
         }
     } catch (\Exception $e) {
         if ($request->getMethod() != 'POST') {
             throw $e;
         }
     }
     return $this->render('plugin/add.html.twig', array('plugin' => $plugin_info, 'dependency_plugins' => $dependency_plugins));
 }
Esempio n. 7
0
 /**
  * 保存
  * @param PluginModel $plugin
  * @return PluginModel
  * @throws \Exception
  */
 public static function savePlugin(PluginModel $plugin)
 {
     return self::editPlugin($plugin->toArray());
 }