//备课打包下载
public function download(){
$row_key=session("row_key");
$teacher_info=M('teacher_info');
$newlesson_info=M('newlesson_info');
$chapter_info=M('chapter_info');
$newlesson_scort=M('newlesson_scort');
$res_info=M("res_info");
$newlesson_type=M('newlesson_type');
$subject_info=M("subject_info");
$te=$teacher_info->where(array('user_key'=>$row_key))->find();
$path='dabao/'.$row_key;
mkdir(iconv('UTF-8','GBK',$path),0777,true);
sleep(1);
$chapter_key = I('get.chapter_key');
$year_key = I('get.year_key');
$tao_key = I('get.tao_key');
$use = I('get.use');
if (is_dir($path)) {
$handle = opendir($path);
if ($handle) {
while (false !== ( $item = readdir($handle) )) {
if ($item != "." && $item != "..")
is_dir("$path/$item") ? delDirAndFile("$path/$item", $delDir) : @unlink("$path/$item");
}
closedir($handle);
if ($delDir)
return rmdir($path);
}
} else {
if (file_exists($path)) {
return @unlink($path);
} else {
return FALSE;
}
}
clearstatcache();
$chap=$chapter_info->where(array('row_key'=>$chapter_key))->find();
$sname=$subject_info->where(array('row_key'=>$chap['subject_key']))->find();
// p($chap);die;
$res=$res_info->where(array('subject_key'=>$chap['subject_key'],'material_key'=>$chap['material_key'],'chapter_list'=>$chap['row_key'],'creator'=>$row_key))->select();
foreach($res as $key=>$value){
// $rul=copy($value['res_file'],$path);
//echo $value['res_file'];die; iconv("GB2312","UTF-8",$value['res_file']);
/* if(!file_exists( $_SERVER['DOCUMENT_ROOT'].iconv("UTF-8","GB2312",$value['res_file']))){
return false;
} */
//$filename = basename($sourcefile);
$rf=explode(".",$value['res_file']);
$num=count($rf);
$hz=$rf[$num-1];
copy($_SERVER['DOCUMENT_ROOT'].iconv("UTF-8","GB2312",$value['res_file']), $_SERVER['DOCUMENT_ROOT']."/".$path .'/'.iconv("UTF-8","GB2312",$chap['name'].'课件资源.'.$hz));
}
$camp=session("camp");
$cou=$newlesson_scort->where(array('user_key'=>$row_key,'chapter_key'=>$chapter_key,'tao_key'=>$tao_key))->count();
$nscort=$newlesson_scort->where(array('user_key'=>$row_key,'chapter_key'=>$chapter_key,'tao_key'=>$tao_key))->find();
$scort=explode(',',$nscort['tscort']);
$tid=$newlesson_type->where(array('camp'=>$camp,'gc'=>1))->getfield("id");
$less=$newlesson_info->where(array('teacher_key'=>$te['row_key'],'chapter_key'=>$chapter_key,'year_key'=>$year_key,'state'=>1,'use'=>$use,'tao_key'=>$tao_key))->order('tid,scort asc')->select();
$le=$newlesson_info->where(array('teacher_key'=>$te['row_key'],'chapter_key'=>$chapter_key,'year_key'=>$year_key,'state'=>1,'use'=>$use,'tid'=>$tid,'tao_key'=>$tao_key))->order('tid,scort asc')->find();
$cc=$newlesson_info->where(array('teacher_key'=>$te['row_key'],'chapter_key'=>$chapter_key,'year_key'=>$year_key,'state'=>1,'use'=>$use,'tid'=>$tid,'tao_key'=>$tao_key))->count();
if($cou!=0){
foreach($scort as $kk=>$vv){
foreach($less as $ke=>$va){
if($vv==$va['tid']){
$arr[$ke]=$va;
}
}
}
$less=$arr;
}
$fansi=$newlesson_info->where(array('teacher_key'=>$te['row_key'],'chapter_key'=>$chapter_key,'tid'=>0,'use'=>1,'tao_key'=>$tao_key))->order('id desc')->select();
$erci=$newlesson_info->where(array('teacher_key'=>$te['row_key'],'chapter_key'=>$chapter_key,'state'=>2,'use'=>1,'tao_key'=>$tao_key))->limit(1)->order('id desc')->select();
foreach($less as $key=>$value){
foreach($fansi as $ke=>$val){
if($value['id']==$val['id']){
unset($value);
}}
if($value['id']==$le['id']){
$value['st']=1;
}
$less[$key]=$value;
}
$less=array_values(array_filter($less));
//p($less);die;
import("Org.Util.PHPWord");
$this->getExcel1($chap['name'],$less,$row_key,$fansi,$erci,$cc,$chap['grade'],$sname['name']);
$PHPZip = new \myclass\PHPZip();
$zip_dir = 'dabao/'.$row_key;
$PHPZip->ZipDir($zip_dir, $chap['name'].".zip", 1); //文件夹打包 第三个参数1为直接下载,2为保存到服务器
}
//tp导出word(通过PHPword插件)
private function getExcel1($fileNames,$data,$row,$fansi,$erci,$cc,$gname,$sname){
$keyword=urlencode($fileNames);//将关键字编码
$keyword=preg_replace("/(%7E|%60|%21|%40|%23|%24|%25|%5E|%26|%27|%2A|%28|%29|%2B|%7C|%5C|%3D|\-|_|%5B|%5D|%7D|%7B|%3B|%22|%3A|%3F|%3E|%3C|%2C|\.|%2F|%A3%BF|%A1%B7|%A1%B6|%A1%A2|%A1%A3|%A3%AC|%7D|%A1%B0|%A3%BA|%A3%BB|%A1%AE|%A1%AF|%A1%B1|%A3%FC|%A3%BD|%A1%AA|%A3%A9|%A3%A8|%A1%AD|%A3%A4|%A1%A4|%A3%A1|%E3%80%82|%EF%BC%81|%EF%BC%8C|%EF%BC%9B|%EF%BC%9F|%EF%BC%9A|%E3%80%81|%E2%80%A6%E2%80%A6|%E2%80%9D|%E2%80%9C|%E2%80%98|%E2%80%99)+/",'',$keyword);
$fileName=urldecode($keyword);//将过滤后的关键字解码
//创建PHPExcel对象,注意,不能少了
$PHPWord = new \PHPWord();
$subject_info=M('subject_info');
$lesson_info=M('lesson_info');
$school_year=M('school_year');
$material_info=M('material_info');
$chapter_info=M('chapter_info');
$newlesson_type=M('newlesson_type');
$row_key=session("row_key");
$teacher_info=M('teacher_info');
$te=$teacher_info->where(array('user_key'=>$row_key))->find();
//p($cgrade);die;
//对数据进行检验
if(empty($data) || !is_array($data)){
$this->error("该教案不存在");
// die("该教案不存在!!");
}
//检查文件名
if(empty($fileName)){
exit;
}
$date = date("Y_m_d",time());
$fileName .= ".docx";
$sectionStyle=array('orientation'=>null,'marginLeft'=>'1000','marginRight'=>'1000','marginTop'=>'1000','marginBottom'=>'1000');
//设置默认字体
$PHPWord->setDefaultFontName('楷体');
$section = $PHPWord->createSection($sectionStyle);
/**************table start*******************/
//$tableStyle = array('cellMarginTop'=>80, 'cellMarginLeft'=>80, 'cellMarginRight'=>80, 'cellMarginBottom'=>80);
$styleTable = array('borderSize'=>6, 'borderColor'=>'000000','cellMargin'=>80);
$styleCell = array('valign'=>'center', 'align'=>'center','bold'=>true);
$fontStyle = array('size'=>'14');
$cfont = array('size'=>'12');//内容字体大小
$tStyle = array('size'=>'14' ,'bold'=>true);
$fStyle = array('size'=>'24' ,'bold'=>true);
$PHPWord->addTableStyle('myOwnTableStyle', $styleTable, $fontStyle);
$section->addTextBreak();
$table = $section->addTable('myOwnTableStyle');
$table->addRow(800);
$table->addCell(1600, array('cellMerge' => 'restart', 'valign' => "center"))->addText(iconv('utf-8','GB2312',$fileNames), $fStyle, $styleCell);
$table->addCell(1600, array('cellMerge' => 'continue'));
$table->addCell(1600, array('cellMerge' => 'continue'));
$table->addCell(1600, array('cellMerge' => 'continue'));
$table->addCell(1600, array('cellMerge' => 'continue'));
$table->addCell(1600, array('cellMerge' => 'continue'));
$table->addRow(500);
$table->addCell(1300,$styleCell)->addText(iconv('utf-8','GB2312','科目'),$tStyle, $styleCell);
$table->addCell(1500,array('valign'=>'center', 'align'=>'center'))->addText(iconv('utf-8','GB2312',$sname), $fontStyle);
$table->addCell(1500,$styleCell)->addText(iconv('utf-8','GB2312','年级'),$tStyle, $styleCell);
$table->addCell(1500,array('valign'=>'center', 'align'=>'center'))->addText(iconv('utf-8','GB2312',$gname), $fontStyle);
$table->addCell(1500, $styleCell)->addText(iconv('utf-8','GB2312','课时'),$tStyle, $styleCell);
$table->addCell(1800,array('valign'=>'center', 'align'=>'center'))->addText(iconv('utf-8','GB2312',"{$cc}课时"),$fontStyle);
$table->addRow(500);
$table->addCell(1300,$styleCell)->addText(iconv('utf-8','GB2312','授课人'),$tStyle, $styleCell);
$table->addCell(1500,array('valign'=>'center', 'align'=>'center'))->addText(iconv('utf-8','GB2312',$te['name']), $fontStyle);
$table->addCell(1500,$styleCell)->addText(iconv('utf-8','GB2312','授课班级'),$tStyle, $styleCell);
$table->addCell(1500,array('valign'=>'center', 'align'=>'center'))->addText(iconv('utf-8','GB2312',""), $fontStyle);
$table->addCell(1500, $styleCell)->addText(iconv('utf-8','GB2312','授课时间'),$tStyle, $styleCell);
$table->addCell(1800,array('valign'=>'center', 'align'=>'center'))->addText(iconv('utf-8','GB2312',""),$tStyle);
// $table->addCell(30000, array('cellMerge' => 'restart'))->addText(iconv('utf-8','GB2312',$fileNames), $fStyle, $styleCell);
//$table->addCell(30000, array('cellMerge' => 'continue'));
//$table->addCell(30000, array('cellMerge' => 'continue'));
//p($data);die;
$camp=session("camp");
$tid=$newlesson_type->where(array('camp'=>$camp,'gc'=>1))->getfield("id");
foreach($data as $key=>$value){
if($value['tid']!=$tid){
$table->addRow(500);
$table->addCell(1600, array('cellMerge' => 'restart', 'valign' => "center"))->addText(iconv('utf-8','GB2312',str_replace(" "," ",strip_tags(trim($value['title'])))), $tStyle, $styleCell);
$table->addCell(1600, array('cellMerge' => 'continue'));
$table->addCell(1600, array('cellMerge' => 'continue'));
$table->addCell(1600, array('cellMerge' => 'continue'));
$table->addCell(1600, array('cellMerge' => 'continue'));
$table->addCell(1600)->addText(iconv('utf-8','GB2312',"修改"), $tStyle, $styleCell);
$table->addRow(600);
$content1=explode('</span>',trim(str_replace("——", "--",str_replace(" ","",$value['content']))));
$content2=explode('</p>',trim(str_replace("——", "--",str_replace(" ","",$value['content']))));
$content3=explode('<br/>',trim(str_replace("——", "--",str_replace(" ","",$value['content']))));
$a=count($content1);
$b=count($content2);
$c=count($content3);
$e=($c>=($a>=$b? $a:$b )? $c:($a>=$b? $a:$b ));
if($e==$a){
$content=$content1;
}
if($e==$b){
$content=$content2;
}
if($e==$c){
$content=$content3;
}
$c2=$table->addCell(5000, array('cellMerge' => 'restart'));
//$content=trim($content);
$content=array_filter($content);
foreach($content as $k=>$v){
// p(strip_tags($v));
$c2->addText(iconv('utf-8','GB2312',str_replace("ɑ","a",preg_replace('/\s{2,}| /', '',strip_tags($v)))), $cfont);
}
//p($con);
//die;
/* if(is_numeric($value['title'])){
$cell=$table->addCell(30000, array('cellMerge' => 'restart'));
$cell->addText(iconv('utf-8','GB2312',"第".strip_tags($value['title'])."课时"), $tStyle,$styleCell);
}else{*/
//$cell=$table->addCell(30000, array('cellMerge' => 'restart'));
//$cell->addText(iconv('utf-8','GB2312',str_replace(" "," ",strip_tags($value['title']))), $tStyle);
//$section->addTextBreak();
/*if($value['content'] == strip_tags($value['content'],'</p>')){
$content=explode('</span>',str_replace("——", "--",str_replace(" ","",$value['content'])));
}
else{
$content=explode('</p>',str_replace("——", "--",str_replace(" ","",$value['content'])));
}*/
// }
//elseif(){
$table->addCell(1000, array('cellMerge' => 'continue'));
$table->addCell(2000, array('cellMerge' => 'continue'));
$table->addCell(1000, array('cellMerge' => 'continue'));
$table->addCell(2000, array('cellMerge' => 'continue'));
$table->addCell(1600)->addText(iconv('utf-8','GB2312',""));
}else{
if($value['st']==1){
$table->addRow();
$cell=$table->addCell(5000, array('cellMerge' => 'restart'), $styleCell);
$cell->addText(iconv('utf-8','GB2312','教学过程'), $tStyle);
$table->addCell(1000, array('cellMerge' => 'continue'));
$table->addCell(2000, array('cellMerge' => 'continue'));
$table->addCell(1000, array('cellMerge' => 'continue'));
$table->addCell(1000, array('cellMerge' => 'continue'));
$table->addCell(2500, array('rowMerge' => 'restart'))->addText(iconv('utf-8','GB2312','二次备课'), $tStyle, $styleCell);
}
if($value['tid']==$tid){
//标题
$table->addRow(600);
$cell=$table->addCell(5000, array('cellMerge' => 'restart'));
$cell->addText(trim(iconv('utf-8','GB2312',$value['title'])), $tStyle, $styleCell);
$table->addCell(1000, array('cellMerge' => 'continue'));
$table->addCell(2000, array('cellMerge' => 'continue'));
$table->addCell(1000, array('cellMerge' => 'continue'));
$table->addCell(2000, array('cellMerge' => 'continue'));
//$table->addCell(1500)->addText(iconv('utf-8','GB2312','二次教案'), $tStyle, $styleCell);
if($value['st']==1){
$cell3=$table->addCell(2000, array('rowMerge' => 'restart'));
foreach($erci as $kk=>$vv){
$cell3->addText(iconv('utf-8','GB2312',strip_tags($vv['content'])), $cfont);
}
}else{
$table->addCell(2000, array('rowMerge' => 'continue'));
}
//教学内容
$table->addRow(600);
$cell4=$table->addCell(5000, array('cellMerge' => 'restart'));
$content1=explode('</span>',str_replace("——", "--",str_replace(" ","",$value['content'])));
$content2=explode('</p>',str_replace("——", "--",str_replace(" ","",$value['content'])));
$content3=explode('<br/>',str_replace("——", "--",str_replace(" ","",$value['content'])));
$a=count($content1);
$b=count($content2);
$c=count($content3);
$e=($c>=($a>=$b? $a:$b )? $c:($a>=$b? $a:$b ));
if($e==$a){
$content=$content1;
}
if($e==$b){
$content=$content2;
}
if($e==$c){
$content=$content3;
}
$content=array_filter($content);
foreach($content as $v){
$cell4->addText(iconv('utf-8','GB2312',str_replace("ɑ","a",preg_replace('/\s{2,}| /', '',strip_tags($v)))), $cfont);
//$section->addTextBreak();
}
$table->addCell(1000, array('cellMerge' => 'continue'));
$table->addCell(2000, array('cellMerge' => 'continue'));
$table->addCell(1000, array('cellMerge' => 'continue'));
$table->addCell(1000, array('cellMerge' => 'continue'));
}
$table->addCell(2500, array('rowMerge' => 'continue'));
//$table->addCell(2000, array('rowMerge' => 'continue'));
//$table->addCell(2000, array('rowMerge' => 'restart'));
//$table->addCell(2000, array('rowMerge' => 'continue'));
//$table->addCell(2000, array('rowMerge' => 'continue'));
}
/* if($key>2){
//$table->addCell(9000, array('rowMerge' => 'restart'))->addText(iconv('utf-8','GB2312','二次教案'), $tStyle, $styleCell);
$cell=$table->addCell(2000, array('rowMerge' => 'restart'));
$cell->addText(iconv('utf-8','GB2312','二次教案'), $tStyle, $styleCell);
foreach($erci as $kk=>$vv){
$cell->addText(iconv('utf-8','GB2312',$vv['content']), $fontStyle);
$section->addTextBreak();
}
}else{
$table->addCell(2000, array('rowMerge' => 'continue'));
}
*/
}
//$table->addCell(2000, array('rowMerge' => 'continue'));
$table->addRow(600);
$table->addCell(1300, $styleCell)->addText(iconv('utf-8','GB2312',"教学反思"), $tStyle);
$cell5=$table->addCell(5000, array('cellMerge' => 'restart'));
foreach($fansi as $kk=>$vv){
$cell5->addText(iconv('utf-8','GB2312',$vv['content']), $cfont);
// $section->addTextBreak();
}
//$table->addCell(4000, array('cellMerge' => 'restart'))->addText("", $fontStyle);
$table->addCell(1000, array('cellMerge' => 'continue'));
$table->addCell(2000, array('cellMerge' => 'continue'));
$table->addCell(1000, array('cellMerge' => 'continue'));
$table->addCell(2000, array('cellMerge' => 'continue'));
/**************table start*******************/
$fileName = iconv("utf-8", "gb2312", $fileName);
$hstyle = array('align'=>'right','');
$footstyle = array('align'=>'center');
$hfontStyle = array('size'=>'9');
//添加页眉
$header = $section->createHeader();
$footer = $section->createFooter();
$table = $header->addTable();
$time=date('Y/m/d');
$header->addText(iconv('utf-8','GB2312',$sname.$gname.$fileNames."教学设计"),$hfontStyle,$hstyle);
$footer->addText(iconv('utf-8','GB2312','网络备课自动生成教案-'.$time),$hfontStyle,$footstyle);
ob_end_clean();
header("Content-type: application/vnd.ms-word");
header("Content-Disposition:attachment;filename=\"$fileName\"");
//header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header('Cache-Control: max-age=0');
$objWriter = \PHPWord_IOFactory::createWriter($PHPWord,'Word2007');
$objWriter->save('dabao/'.$row_key."/".$fileName); //文件通过浏览器下载
}
public function download(){
$row_key=session("row_key");
$teacher_info=M('teacher_info');
$newlesson_info=M('newlesson_info');
$chapter_info=M('chapter_info');
$newlesson_scort=M('newlesson_scort');
$res_info=M("res_info");
$newlesson_type=M('newlesson_type');
$subject_info=M("subject_info");
$te=$teacher_info->where(array('user_key'=>$row_key))->find();
$path='dabao/'.$row_key;
mkdir(iconv('UTF-8','GBK',$path),0777,true);
sleep(1);
$chapter_key = I('get.chapter_key');
$year_key = I('get.year_key');
$tao_key = I('get.tao_key');
$use = I('get.use');
if (is_dir($path)) {
$handle = opendir($path);
if ($handle) {
while (false !== ( $item = readdir($handle) )) {
if ($item != "." && $item != "..")
is_dir("$path/$item") ? delDirAndFile("$path/$item", $delDir) : @unlink("$path/$item");
}
closedir($handle);
if ($delDir)
return rmdir($path);
}
} else {
if (file_exists($path)) {
return @unlink($path);
} else {
return FALSE;
}
}
clearstatcache();
$chap=$chapter_info->where(array('row_key'=>$chapter_key))->find();
$sname=$subject_info->where(array('row_key'=>$chap['subject_key']))->find();
// p($chap);die;
$res=$res_info->where(array('subject_key'=>$chap['subject_key'],'material_key'=>$chap['material_key'],'chapter_list'=>$chap['row_key'],'creator'=>$row_key))->select();
foreach($res as $key=>$value){
// $rul=copy($value['res_file'],$path);
//echo $value['res_file'];die; iconv("GB2312","UTF-8",$value['res_file']);
/* if(!file_exists( $_SERVER['DOCUMENT_ROOT'].iconv("UTF-8","GB2312",$value['res_file']))){
return false;
} */
//$filename = basename($sourcefile);
$rf=explode(".",$value['res_file']);
$num=count($rf);
$hz=$rf[$num-1];
copy($_SERVER['DOCUMENT_ROOT'].iconv("UTF-8","GB2312",$value['res_file']), $_SERVER['DOCUMENT_ROOT']."/".$path .'/'.iconv("UTF-8","GB2312",$chap['name'].'课件资源.'.$hz));
}
$camp=session("camp");
$cou=$newlesson_scort->where(array('user_key'=>$row_key,'chapter_key'=>$chapter_key,'tao_key'=>$tao_key))->count();
$nscort=$newlesson_scort->where(array('user_key'=>$row_key,'chapter_key'=>$chapter_key,'tao_key'=>$tao_key))->find();
$scort=explode(',',$nscort['tscort']);
$tid=$newlesson_type->where(array('camp'=>$camp,'gc'=>1))->getfield("id");
$less=$newlesson_info->where(array('teacher_key'=>$te['row_key'],'chapter_key'=>$chapter_key,'year_key'=>$year_key,'state'=>1,'use'=>$use,'tao_key'=>$tao_key))->order('tid,scort asc')->select();
$le=$newlesson_info->where(array('teacher_key'=>$te['row_key'],'chapter_key'=>$chapter_key,'year_key'=>$year_key,'state'=>1,'use'=>$use,'tid'=>$tid,'tao_key'=>$tao_key))->order('tid,scort asc')->find();
$cc=$newlesson_info->where(array('teacher_key'=>$te['row_key'],'chapter_key'=>$chapter_key,'year_key'=>$year_key,'state'=>1,'use'=>$use,'tid'=>$tid,'tao_key'=>$tao_key))->count();
if($cou!=0){
foreach($scort as $kk=>$vv){
foreach($less as $ke=>$va){
if($vv==$va['tid']){
$arr[$ke]=$va;
}
}
}
$less=$arr;
}
$fansi=$newlesson_info->where(array('teacher_key'=>$te['row_key'],'chapter_key'=>$chapter_key,'tid'=>0,'use'=>1,'tao_key'=>$tao_key))->order('id desc')->select();
$erci=$newlesson_info->where(array('teacher_key'=>$te['row_key'],'chapter_key'=>$chapter_key,'state'=>2,'use'=>1,'tao_key'=>$tao_key))->limit(1)->order('id desc')->select();
foreach($less as $key=>$value){
foreach($fansi as $ke=>$val){
if($value['id']==$val['id']){
unset($value);
}}
if($value['id']==$le['id']){
$value['st']=1;
}
$less[$key]=$value;
}
$less=array_values(array_filter($less));
//p($less);die;
import("Org.Util.PHPWord");
$this->getExcel1($chap['name'],$less,$row_key,$fansi,$erci,$cc,$chap['grade'],$sname['name']);
$PHPZip = new \myclass\PHPZip();
$zip_dir = 'dabao/'.$row_key;
$PHPZip->ZipDir($zip_dir, $chap['name'].".zip", 1); //文件夹打包 第三个参数1为直接下载,2为保存到服务器
}
//tp导出word(通过PHPword插件)
private function getExcel1($fileNames,$data,$row,$fansi,$erci,$cc,$gname,$sname){
$keyword=urlencode($fileNames);//将关键字编码
$keyword=preg_replace("/(%7E|%60|%21|%40|%23|%24|%25|%5E|%26|%27|%2A|%28|%29|%2B|%7C|%5C|%3D|\-|_|%5B|%5D|%7D|%7B|%3B|%22|%3A|%3F|%3E|%3C|%2C|\.|%2F|%A3%BF|%A1%B7|%A1%B6|%A1%A2|%A1%A3|%A3%AC|%7D|%A1%B0|%A3%BA|%A3%BB|%A1%AE|%A1%AF|%A1%B1|%A3%FC|%A3%BD|%A1%AA|%A3%A9|%A3%A8|%A1%AD|%A3%A4|%A1%A4|%A3%A1|%E3%80%82|%EF%BC%81|%EF%BC%8C|%EF%BC%9B|%EF%BC%9F|%EF%BC%9A|%E3%80%81|%E2%80%A6%E2%80%A6|%E2%80%9D|%E2%80%9C|%E2%80%98|%E2%80%99)+/",'',$keyword);
$fileName=urldecode($keyword);//将过滤后的关键字解码
//创建PHPExcel对象,注意,不能少了
$PHPWord = new \PHPWord();
$subject_info=M('subject_info');
$lesson_info=M('lesson_info');
$school_year=M('school_year');
$material_info=M('material_info');
$chapter_info=M('chapter_info');
$newlesson_type=M('newlesson_type');
$row_key=session("row_key");
$teacher_info=M('teacher_info');
$te=$teacher_info->where(array('user_key'=>$row_key))->find();
//p($cgrade);die;
//对数据进行检验
if(empty($data) || !is_array($data)){
$this->error("该教案不存在");
// die("该教案不存在!!");
}
//检查文件名
if(empty($fileName)){
exit;
}
$date = date("Y_m_d",time());
$fileName .= ".docx";
$sectionStyle=array('orientation'=>null,'marginLeft'=>'1000','marginRight'=>'1000','marginTop'=>'1000','marginBottom'=>'1000');
//设置默认字体
$PHPWord->setDefaultFontName('楷体');
$section = $PHPWord->createSection($sectionStyle);
/**************table start*******************/
//$tableStyle = array('cellMarginTop'=>80, 'cellMarginLeft'=>80, 'cellMarginRight'=>80, 'cellMarginBottom'=>80);
$styleTable = array('borderSize'=>6, 'borderColor'=>'000000','cellMargin'=>80);
$styleCell = array('valign'=>'center', 'align'=>'center','bold'=>true);
$fontStyle = array('size'=>'14');
$cfont = array('size'=>'12');//内容字体大小
$tStyle = array('size'=>'14' ,'bold'=>true);
$fStyle = array('size'=>'24' ,'bold'=>true);
$PHPWord->addTableStyle('myOwnTableStyle', $styleTable, $fontStyle);
$section->addTextBreak();
$table = $section->addTable('myOwnTableStyle');
$table->addRow(800);
$table->addCell(1600, array('cellMerge' => 'restart', 'valign' => "center"))->addText(iconv('utf-8','GB2312',$fileNames), $fStyle, $styleCell);
$table->addCell(1600, array('cellMerge' => 'continue'));
$table->addCell(1600, array('cellMerge' => 'continue'));
$table->addCell(1600, array('cellMerge' => 'continue'));
$table->addCell(1600, array('cellMerge' => 'continue'));
$table->addCell(1600, array('cellMerge' => 'continue'));
$table->addRow(500);
$table->addCell(1300,$styleCell)->addText(iconv('utf-8','GB2312','科目'),$tStyle, $styleCell);
$table->addCell(1500,array('valign'=>'center', 'align'=>'center'))->addText(iconv('utf-8','GB2312',$sname), $fontStyle);
$table->addCell(1500,$styleCell)->addText(iconv('utf-8','GB2312','年级'),$tStyle, $styleCell);
$table->addCell(1500,array('valign'=>'center', 'align'=>'center'))->addText(iconv('utf-8','GB2312',$gname), $fontStyle);
$table->addCell(1500, $styleCell)->addText(iconv('utf-8','GB2312','课时'),$tStyle, $styleCell);
$table->addCell(1800,array('valign'=>'center', 'align'=>'center'))->addText(iconv('utf-8','GB2312',"{$cc}课时"),$fontStyle);
$table->addRow(500);
$table->addCell(1300,$styleCell)->addText(iconv('utf-8','GB2312','授课人'),$tStyle, $styleCell);
$table->addCell(1500,array('valign'=>'center', 'align'=>'center'))->addText(iconv('utf-8','GB2312',$te['name']), $fontStyle);
$table->addCell(1500,$styleCell)->addText(iconv('utf-8','GB2312','授课班级'),$tStyle, $styleCell);
$table->addCell(1500,array('valign'=>'center', 'align'=>'center'))->addText(iconv('utf-8','GB2312',""), $fontStyle);
$table->addCell(1500, $styleCell)->addText(iconv('utf-8','GB2312','授课时间'),$tStyle, $styleCell);
$table->addCell(1800,array('valign'=>'center', 'align'=>'center'))->addText(iconv('utf-8','GB2312',""),$tStyle);
// $table->addCell(30000, array('cellMerge' => 'restart'))->addText(iconv('utf-8','GB2312',$fileNames), $fStyle, $styleCell);
//$table->addCell(30000, array('cellMerge' => 'continue'));
//$table->addCell(30000, array('cellMerge' => 'continue'));
//p($data);die;
$camp=session("camp");
$tid=$newlesson_type->where(array('camp'=>$camp,'gc'=>1))->getfield("id");
foreach($data as $key=>$value){
if($value['tid']!=$tid){
$table->addRow(500);
$table->addCell(1600, array('cellMerge' => 'restart', 'valign' => "center"))->addText(iconv('utf-8','GB2312',str_replace(" "," ",strip_tags(trim($value['title'])))), $tStyle, $styleCell);
$table->addCell(1600, array('cellMerge' => 'continue'));
$table->addCell(1600, array('cellMerge' => 'continue'));
$table->addCell(1600, array('cellMerge' => 'continue'));
$table->addCell(1600, array('cellMerge' => 'continue'));
$table->addCell(1600)->addText(iconv('utf-8','GB2312',"修改"), $tStyle, $styleCell);
$table->addRow(600);
$content1=explode('</span>',trim(str_replace("——", "--",str_replace(" ","",$value['content']))));
$content2=explode('</p>',trim(str_replace("——", "--",str_replace(" ","",$value['content']))));
$content3=explode('<br/>',trim(str_replace("——", "--",str_replace(" ","",$value['content']))));
$a=count($content1);
$b=count($content2);
$c=count($content3);
$e=($c>=($a>=$b? $a:$b )? $c:($a>=$b? $a:$b ));
if($e==$a){
$content=$content1;
}
if($e==$b){
$content=$content2;
}
if($e==$c){
$content=$content3;
}
$c2=$table->addCell(5000, array('cellMerge' => 'restart'));
//$content=trim($content);
$content=array_filter($content);
foreach($content as $k=>$v){
// p(strip_tags($v));
$c2->addText(iconv('utf-8','GB2312',str_replace("ɑ","a",preg_replace('/\s{2,}| /', '',strip_tags($v)))), $cfont);
}
//p($con);
//die;
/* if(is_numeric($value['title'])){
$cell=$table->addCell(30000, array('cellMerge' => 'restart'));
$cell->addText(iconv('utf-8','GB2312',"第".strip_tags($value['title'])."课时"), $tStyle,$styleCell);
}else{*/
//$cell=$table->addCell(30000, array('cellMerge' => 'restart'));
//$cell->addText(iconv('utf-8','GB2312',str_replace(" "," ",strip_tags($value['title']))), $tStyle);
//$section->addTextBreak();
/*if($value['content'] == strip_tags($value['content'],'</p>')){
$content=explode('</span>',str_replace("——", "--",str_replace(" ","",$value['content'])));
}
else{
$content=explode('</p>',str_replace("——", "--",str_replace(" ","",$value['content'])));
}*/
// }
//elseif(){
$table->addCell(1000, array('cellMerge' => 'continue'));
$table->addCell(2000, array('cellMerge' => 'continue'));
$table->addCell(1000, array('cellMerge' => 'continue'));
$table->addCell(2000, array('cellMerge' => 'continue'));
$table->addCell(1600)->addText(iconv('utf-8','GB2312',""));
}else{
if($value['st']==1){
$table->addRow();
$cell=$table->addCell(5000, array('cellMerge' => 'restart'), $styleCell);
$cell->addText(iconv('utf-8','GB2312','教学过程'), $tStyle);
$table->addCell(1000, array('cellMerge' => 'continue'));
$table->addCell(2000, array('cellMerge' => 'continue'));
$table->addCell(1000, array('cellMerge' => 'continue'));
$table->addCell(1000, array('cellMerge' => 'continue'));
$table->addCell(2500, array('rowMerge' => 'restart'))->addText(iconv('utf-8','GB2312','二次备课'), $tStyle, $styleCell);
}
if($value['tid']==$tid){
//标题
$table->addRow(600);
$cell=$table->addCell(5000, array('cellMerge' => 'restart'));
$cell->addText(trim(iconv('utf-8','GB2312',$value['title'])), $tStyle, $styleCell);
$table->addCell(1000, array('cellMerge' => 'continue'));
$table->addCell(2000, array('cellMerge' => 'continue'));
$table->addCell(1000, array('cellMerge' => 'continue'));
$table->addCell(2000, array('cellMerge' => 'continue'));
//$table->addCell(1500)->addText(iconv('utf-8','GB2312','二次教案'), $tStyle, $styleCell);
if($value['st']==1){
$cell3=$table->addCell(2000, array('rowMerge' => 'restart'));
foreach($erci as $kk=>$vv){
$cell3->addText(iconv('utf-8','GB2312',strip_tags($vv['content'])), $cfont);
}
}else{
$table->addCell(2000, array('rowMerge' => 'continue'));
}
//教学内容
$table->addRow(600);
$cell4=$table->addCell(5000, array('cellMerge' => 'restart'));
$content1=explode('</span>',str_replace("——", "--",str_replace(" ","",$value['content'])));
$content2=explode('</p>',str_replace("——", "--",str_replace(" ","",$value['content'])));
$content3=explode('<br/>',str_replace("——", "--",str_replace(" ","",$value['content'])));
$a=count($content1);
$b=count($content2);
$c=count($content3);
$e=($c>=($a>=$b? $a:$b )? $c:($a>=$b? $a:$b ));
if($e==$a){
$content=$content1;
}
if($e==$b){
$content=$content2;
}
if($e==$c){
$content=$content3;
}
$content=array_filter($content);
foreach($content as $v){
$cell4->addText(iconv('utf-8','GB2312',str_replace("ɑ","a",preg_replace('/\s{2,}| /', '',strip_tags($v)))), $cfont);
//$section->addTextBreak();
}
$table->addCell(1000, array('cellMerge' => 'continue'));
$table->addCell(2000, array('cellMerge' => 'continue'));
$table->addCell(1000, array('cellMerge' => 'continue'));
$table->addCell(1000, array('cellMerge' => 'continue'));
}
$table->addCell(2500, array('rowMerge' => 'continue'));
//$table->addCell(2000, array('rowMerge' => 'continue'));
//$table->addCell(2000, array('rowMerge' => 'restart'));
//$table->addCell(2000, array('rowMerge' => 'continue'));
//$table->addCell(2000, array('rowMerge' => 'continue'));
}
/* if($key>2){
//$table->addCell(9000, array('rowMerge' => 'restart'))->addText(iconv('utf-8','GB2312','二次教案'), $tStyle, $styleCell);
$cell=$table->addCell(2000, array('rowMerge' => 'restart'));
$cell->addText(iconv('utf-8','GB2312','二次教案'), $tStyle, $styleCell);
foreach($erci as $kk=>$vv){
$cell->addText(iconv('utf-8','GB2312',$vv['content']), $fontStyle);
$section->addTextBreak();
}
}else{
$table->addCell(2000, array('rowMerge' => 'continue'));
}
*/
}
//$table->addCell(2000, array('rowMerge' => 'continue'));
$table->addRow(600);
$table->addCell(1300, $styleCell)->addText(iconv('utf-8','GB2312',"教学反思"), $tStyle);
$cell5=$table->addCell(5000, array('cellMerge' => 'restart'));
foreach($fansi as $kk=>$vv){
$cell5->addText(iconv('utf-8','GB2312',$vv['content']), $cfont);
// $section->addTextBreak();
}
//$table->addCell(4000, array('cellMerge' => 'restart'))->addText("", $fontStyle);
$table->addCell(1000, array('cellMerge' => 'continue'));
$table->addCell(2000, array('cellMerge' => 'continue'));
$table->addCell(1000, array('cellMerge' => 'continue'));
$table->addCell(2000, array('cellMerge' => 'continue'));
/**************table start*******************/
$fileName = iconv("utf-8", "gb2312", $fileName);
$hstyle = array('align'=>'right','');
$footstyle = array('align'=>'center');
$hfontStyle = array('size'=>'9');
//添加页眉
$header = $section->createHeader();
$footer = $section->createFooter();
$table = $header->addTable();
$time=date('Y/m/d');
$header->addText(iconv('utf-8','GB2312',$sname.$gname.$fileNames."教学设计"),$hfontStyle,$hstyle);
$footer->addText(iconv('utf-8','GB2312','网络备课自动生成教案-'.$time),$hfontStyle,$footstyle);
ob_end_clean();
header("Content-type: application/vnd.ms-word");
header("Content-Disposition:attachment;filename=\"$fileName\"");
//header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header('Cache-Control: max-age=0');
$objWriter = \PHPWord_IOFactory::createWriter($PHPWord,'Word2007');
$objWriter->save('dabao/'.$row_key."/".$fileName); //文件通过浏览器下载
}