Skip to content

Commit

Permalink
实现导出word
Browse files Browse the repository at this point in the history
  • Loading branch information
lifei6671 committed Feb 9, 2017
1 parent 4cb89ae commit bae057b
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 12 deletions.
97 changes: 96 additions & 1 deletion app/Http/Controllers/DocumentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function index($id)
abort(403);
}

$jsonArray = Project::getProjectTree($id);
$jsonArray = Project::getProjectArrayTree($id);

if(empty($jsonArray) === false){
$jsonArray[0]['state']['selected'] = true;
Expand Down Expand Up @@ -491,4 +491,99 @@ public function sort($id)
}
return $this->jsonResult(0);
}

public function export($id)
{
if($id <= 0){
abort(404);
}
$project = Project::getProjectFromCache($id);

if(empty($project)){
abort(404);
}

$member_id = null;
if(empty($this->member) === false){
$member_id = $this->member->member_id;

}

$permissions = Project::hasProjectShow($id,$member_id);

//校验是否有权限访问文档
if($permissions === 0){
abort(404);
}elseif($permissions === 2){
$role = session_project_role($id);
if(empty($role)){
$this->data = $project;
return view('home.password',$this->data);
}
}else if($permissions === 3 && empty($member_id)){
return redirect(route("account.login"));
}elseif($permissions === 3) {
abort(403);
}

$tree = Project::getProjectArrayTree($id);


$filename = $project->project_name;

header('pragma:public');
header('Content-type:application/vnd.ms-word;charset=utf-8;name="'.$filename.'".doc');
header("Content-Disposition:attachment;filename=$filename.doc");
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');

$path = public_path('static/styles/kancloud.css');
$content = '';

if(file_exists($path)){
$content .= '<head><style type="text/css">' . file_get_contents($path) . '</style></head>';
}
$content .= '<body><div class="m-manual"><div class="manual-article"><div class="article-content"><div class="article-body editor-content"><div style="width: 100%;text-align: center;font-size: 25px;padding: 50px 0;"><h1>' . $project->project_name . '</h1></div>';



$content .= self::recursion('#',1,$tree) . '</div></div></div></div></body>';

echo output_word($content);
}

private static function recursion($parent,$level,&$tree)
{
global $content;
$content .= '';

if ($level > 7) {
$level = 6;
}
foreach ($tree as $item) {

if ($item['parent'] == $parent) {

$doc = Document::getDocumnetHtmlFromCache($item['id']);

if ($doc !== false) {
$text = '<h' . $level . '>' . $item['text'] . '</h' . $level . '>' . '<div style="margin: 50px auto;">' . $doc . '</div>';

$content .= $text;
}

$key = array_search($item['id'], array_column($tree, 'parent'));

if ($key !== false) {
$level++;
self::recursion($item['id'], $level, $tree);
}
}
}
$content .= '';
return $content;
}
}
4 changes: 4 additions & 0 deletions app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
Route::get('/show/{id}',[
'as' => 'home.show', 'uses' => 'HomeController@show'
]);
//项目导出
Route::get('/export/{id}',[
'as' => 'document.export', 'uses' => 'DocumentController@export'
]);
//检查项目权限
Route::post('/check_document_auth',[
'as' => 'home.check_document_auth' , 'uses' => 'HomeController@checkDocumentAuth'
Expand Down
7 changes: 4 additions & 3 deletions app/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public static function isCanCreateProject($member_id){
* @param int $project_id
* @return array
*/
public static function getProjectTree($project_id)
public static function getProjectArrayTree($project_id)
{
if(empty($project_id)){
return [];
Expand All @@ -321,7 +321,7 @@ public static function getProjectTree($project_id)
foreach ($tree as &$item){
$tmp['id'] = $item ->doc_id.'';
$tmp['text'] = $item->doc_name;
$tmp['parent' ] = ($item->parent_id == 0 ? '#' : $item->parent_id).'';
$tmp['parent'] = ($item->parent_id == 0 ? '#' : $item->parent_id).'';

$jsonArray[] = $tmp;
}
Expand Down Expand Up @@ -366,7 +366,8 @@ protected static function getSelecdNode($array,$parent_id)
return 0;
}

protected static function createTree($parent_id,array $array,$selected_id = 0,$selected_parent_id = 0){
protected static function createTree($parent_id,array $array,$selected_id = 0,$selected_parent_id = 0)
{
global $menu;

$menu .= '<ul>';
Expand Down
50 changes: 50 additions & 0 deletions app/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,54 @@ function resolve_attachicons($ext){
return null;
}

}

if(!function_exists('output_word')){
/**
* 导出 word
* @param string $content
* @return string
*/
function output_word($content){

$path = public_path('static/styles/kancloud.css');
$data = '';

if(file_exists($path)){
$data .= file_get_contents($path);
}

$content = str_replace("<thead>\n<tr>","<thead><tr style='background-color: rgb(0, 136, 204); color: rgb(255, 255, 255);'>",$content);
$content = str_replace("<pre><code>","<table width='100%' class='codestyle'><pre><code>",$content);
$content = str_replace("</code></pre>","</code></pre></table>",$content);


$html = '<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<style type="text/css">
table
{
border-collapse: collapse;
border: none;
width: 100%;
}
td
{
border: solid #CCC 1px;
}
.codestyle{
word-break: break-all;
background:silver;mso-highlight:silver;
}
'.$data.'
</style>
<meta name=ProgId content=Word.Document>
<meta name=Generator content="Microsoft Word 11">
<meta name=Originator content="Microsoft Word 11">
<xml><w:WordDocument><w:View>Print</w:View></xml></head>
<body>' .$content.'</body></html>';

return $html;
}
}
2 changes: 1 addition & 1 deletion config/mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
|
*/

'from' => ['address' => env('MAIL_USERNAME'), 'name' => 'SmartWiki'],
'from' => ['address' => env('MAIL_USERNAME'), 'name' => env('MAIL_TITLE','SmartWiki')],

/*
|--------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

define('VERSION','v0.4.0');
header('SmartWiki:'.VERSION);
header('X-Frame-Options:SAMEORIGIN');

/**
* Laravel - A PHP Framework For Web Artisans
Expand Down
2 changes: 1 addition & 1 deletion resources/views/account/login.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<div class="input-group-addon">
<i class="fa fa-lock"></i>
</div>
<input type="password" class="form-control" placeholder="密码" name="passwd" id="passwd">
<input type="password" class="form-control" placeholder="密码" name="passwd" id="passwd" autocomplete="off">
</div>
</div>
@if(wiki_config('ENABLED_CAPTCHA'))
Expand Down
4 changes: 2 additions & 2 deletions resources/views/account/modify_password.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@

<div class="form-group">
<label for="newPasswd">新密码</label>
<input type="password" class="form-control" name="passowrd" id="newPassword" maxlength="20" placeholder="新密码">
<input type="password" class="form-control" name="passowrd" id="newPassword" maxlength="20" placeholder="新密码" autocomplete="off">
</div>
<div class="form-group">
<label for="configPasswd">确认密码</label>
<input type="password" class="form-control" id="confirmPassword" name="confirmPassword" maxlength="20" placeholder="确认密码">
<input type="password" class="form-control" id="confirmPassword" name="confirmPassword" maxlength="20" placeholder="确认密码" autocomplete="off">
</div>

<div class="form-group">
Expand Down
6 changes: 3 additions & 3 deletions resources/views/account/register.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<div class="input-group-addon">
<i class="fa fa-lock"></i>
</div>
<input type="password" class="form-control" placeholder="密码" name="password" id="password">
<input type="password" class="form-control" placeholder="密码" name="password" id="password" autocomplete="off">
</div>
</div>
<div class="form-group">
Expand All @@ -62,7 +62,7 @@
<div class="input-group-addon">
<i class="fa fa-lock"></i>
</div>
<input type="password" class="form-control" placeholder="确认密码" name="confirm_password" id="confirm_password">
<input type="password" class="form-control" placeholder="确认密码" name="confirm_password" id="confirm_password" autocomplete="off">
</div>
</div>
<div class="form-group">
Expand All @@ -72,7 +72,7 @@
<div class="input-group-addon">
<i class="fa fa-envelope-open"></i>
</div>
<input type="email" class="form-control" placeholder="邮箱" name="email" id="email">
<input type="email" class="form-control" placeholder="邮箱" name="email" id="email" autocomplete="off">
</div>
</div>
@if(wiki_config('ENABLED_CAPTCHA'))
Expand Down
1 change: 1 addition & 0 deletions resources/views/home/kancloud.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<ul class="dropdown-menu dropdown-menu-right" role="menu" aria-labelledby="dLabel">
@if($project->project_open_state ==1 || $project->project_open_state ==2)
<li><a href="javascript:" data-toggle="modal" data-target="#shareProject">项目分享</a> </li>
<li><a href="{{route('document.export',['id' => $project->project_id])}}" target="_blank">项目导出</a> </li>
<li role="presentation" class="divider"></li>
@endif

Expand Down
2 changes: 1 addition & 1 deletion resources/views/search/search.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<a href="{{route('home.index')}}" class="navbar-brand">{{wiki_config('SITE_NAME','SmartWiki')}}</a>
<div class="searchbar pull-left visible-lg-inline-block visible-md-inline-block">
<form class="form-inline" action="{{route('search.search')}}" method="get">
<input class="form-control" name="keyword" type="search" placeholder="请输入关键词..." value="{!! $keyword !!}">
<input class="form-control" name="keyword" type="search" placeholder="请输入关键词..." value="{{ $keyword }}">
<button class="search-btn">
<i class="fa fa-search"></i>
</button>
Expand Down

0 comments on commit bae057b

Please sign in to comment.