|
- <?php
- /**
- * Created by PhpStorm.
- * User: tianlionfire
- * Date: 2018/1/24
- * Time: 15:15
- */
-
- namespace backend\modules\zzcs\logic;
-
-
- use backend\modules\zzcs\models\BaseArea;
- use backend\modules\zzcs\models\BaseUser;
- use backend\modules\zzcs\models\OperaDocument;
- use Yii;
-
- class OperaDocumentManager
- {
- /**
- * Function Description:获取文件管理列表
- * Function Name: getDocumentList
- * @param $type
- * @param $module
- * @param $search_more
- * @param $page_size
- * @param $current
- *
- * @return array
- *
- * @author 李健
- */
- public function getDocumentList($type, $module, $search_more, $page_size, $current)
- {
- $opera_document = new OperaDocument();
- $doc_res = $opera_document->getDocumentList($type, $module, $search_more, $page_size, $current);
- foreach ($doc_res as $k => $v) {
- $doc_res[$k]['num'] = $k + 1 + ($current - 1) * $page_size;
- }
-
- // 获取总数
- $total_page = $opera_document->getDocumentListCount($type, $module, $search_more);
-
- $res = [];
- if ($total_page > 0) {
- $res['code'] = '0';
- $res['info'] = '返回文档管理列表成功';
- $res['list'] = $doc_res;
- $res['count'] = $total_page;
- $res['total_page'] = (string)ceil($total_page / $page_size);
- $res['current'] = $current;
- } else {
- $res['code'] = '0';
- $res['info'] = '没有符合条件的数据';
- $res['list'] = array();
- $res['count'] = '0';
- $res['total_page'] = '0';
- $res['current'] = $current;
- }
- return $res;
- }
-
- /**
- * Function Description:查询已发布的模块的功能说明和系统简介文档(首页)
- * Function Name: getInfo
- * @return array|\yii\db\ActiveRecord[]
- * @author 田玲菲
- */
- public function getInfo()
- {
- $operaDocument = new OperaDocument();
- $res = $operaDocument->getInfo();
- foreach ($res as $k => $v2) {
- if (isset($res[$k]['function'])) {
- foreach ($res[$k]['function'] as &$v) {
- $v['title'] = strip_tags($v['title']);
- $v['content'] = strip_tags($v['content']);
- if(isset($v['content']{200})){
- $v['content'] = mb_substr($v['content'],0,200,'UTF-8').'...';
- }
- }
- }
- if (isset($res[$k]['system'])) {
- foreach ($res[$k]['system'] as &$v) {
- $v['title'] = strip_tags($v['title']);
- $v['content'] = strip_tags($v['content']);
- }
- }
- }
- return $res;
- }
-
-
- /**
- * Function Description:关键字高亮,不区分大小写
- * Function Name: str_highlight
- * @param $subject
- * @param $keyword
- * @param string $pre
- * @param string $suf
- * @return string
- * @author 田玲菲
- */
- public function str_highlight($subject,$keyword,$pre = '<span style="color:red">',$suf = '</span>'){
- if(empty($subject) || empty($keyword)){
- return $subject;
- }
- $k_len = strlen($keyword);
- if($k_len > strlen($subject)){
- return $subject;
- }
-
- $keyword = strtolower($keyword);
- $offset=0;
- $return=array();
- while (1) {
- $sub = substr($subject, $offset, $k_len);
- if(strlen($sub)<$k_len){
- if($sub){
- $return[]=$sub;
- }
- break;
- }
- if(strtolower($sub) == $keyword){
- $return[]=$pre;
- $return[]=$sub;
- $return[]=$suf;
-
- $offset+=$k_len;
- }else{
- $return[]=substr($subject, $offset,1);
- $offset++;
- }
- }
- return join('', $return);
- }
-
-
- /**
- * Function Description:获取文章列表
- * Function Name: getArticleList
- * @param $type
- * @param $module
- * @param $search_more
- * @param $page_size
- * @param $current
- * @return array
- * @author 田玲菲
- */
- public function getArticleList($type, $module, $search_more, $page_size, $current)
- {
- $opera_document = new OperaDocument();
- //获取文档列表
- $doc_res = $opera_document->getDocumentListReleased($type, $module, $search_more, $page_size, $current);
- //剥去html和php标签
- foreach ($doc_res as &$v) {
- $v['content'] = strip_tags($v['content']);
- if(isset($v['content']{200})){
- $v['content'] = mb_substr($v['content'],0,200,'UTF-8').'...';
- }
- }
- //搜索关键字变红
- if (!empty($search_more)) {
- // $replace_str = '<span style="color:red">' . $search_more . '</span>';
- // foreach ($doc_res as &$v) {
- // $v['title'] = str_replace($search_more, $replace_str, $v['title']);
- // $v['content'] = str_replace($search_more, $replace_str, $v['content']);
- // }
- foreach ($doc_res as &$v){
- $v['title'] = $this->str_highlight($v['title'],$search_more);
- $v['content'] = $this->str_highlight($v['content'],$search_more);
- }
- }
-
- // 获取总数
- $total_page = $opera_document->getDocumentListReleaseCount($type, $module, $search_more);
-
- $res = [];
- $base_area = new BaseArea();
- $page_arr = $base_area->getPage($total_page, $page_size, $current);
- $res['page_size_arr'] = [10, 30, 50, 100];
- if ($total_page > 0) {
- $res['code'] = '0';
- $res['info'] = '返回文档列表成功';
- $res['list'] = $doc_res;
- $res['page']['current_page'] = $current;
- $res['page']['total_row'] = $total_page;
- $res['page']['page_size'] = $page_size;
- $res['count'] = $total_page;
- $res['total_page'] = (string)ceil($total_page / $page_size);
- $res['page_arr'] = $page_arr;
- $res['module'] = $module;
- } else {
- $res['code'] = '1';
- $res['info'] = '没有符合条件的数据';
- $res['list'] = array();
- $res['count'] = '0';
- $res['total_page'] = '0';
- $res['page']['current_page'] = $current;
- $res['page']['total_row'] = $total_page;
- $res['page']['page_size'] = $page_size;
- $res['count'] = $total_page;
- $res['total_page'] = (string)ceil($total_page / $page_size);
- $res['page_arr'] = $page_arr;
- $res['module'] = $module;
- }
-
- return $res;
- }
-
-
- /**
- * Function Description:根据id获取文章内容
- * Function Name: getDocumentContent
- * @param $id
- * @return array|\yii\db\ActiveRecord[]
- * @author 田玲菲
- */
- public function getDocumentContent($id)
- {
- $operaDocument = new OperaDocument();
- return $operaDocument->getDocumentContent($id);
- }
-
-
- /**
- * Function Description:获取常见问题列表和内容
- * Function Name: getCommonQuestion
- * @return array|\yii\db\ActiveRecord[]
- * @author 田玲菲
- */
- public function getCommonQuestion()
- {
- $operaDocument = new OperaDocument();
- $res = $operaDocument->getCommonQuestion();
- foreach ($res as $k => $v2) {
- if (isset($res[$k]['function'])) {
- foreach ($res[$k]['function'] as &$v) {
- $v['title'] = strip_tags($v['title']);
- $v['content'] = strip_tags($v['content']);
- }
- }
- if (isset($res[$k]['system'])) {
- foreach ($res[$k]['system'] as &$v) {
- $v['title'] = strip_tags($v['title']);
- $v['content'] = strip_tags($v['content']);
- }
- }
- }
- return $res;
- }
-
-
- /**
- * Function Description:增加文档的阅读次数
- * Function Name: addSeeCount
- * @param $id
- * @return mixed
- * @author 田玲菲
- */
- public function addSeeCounts($id)
- {
- $operaDocument = new OperaDocument();
- return $operaDocument->addSeeCount($id);
- }
-
-
- /**
- * Function Description:删除文档
- * Function Name: deleteDocument
- * @param $id
- * @return mixed
- * @author 李健
- */
- public function deleteDocument($id)
- {
- $operaDocument = new OperaDocument();
- $res = $operaDocument->deleteDocument($id);
- if (!$res) {
- $json['code'] = '1';
- $json['info'] = '删除文档失败';
- } else {
- $json['code'] = '0';
- $json['info'] = '删除文档成功';
- }
- return $json;
- }
-
- /**
- * Function Description:修改状态
- * Function Name: changeStatus
- * @param $id
- * @return mixed
- * @author 李健
- */
- public function changeStatus($id)
- {
- $operaDocument = new OperaDocument();
- $res = $operaDocument->changeStatus($id);
- if (!$res) {
- $json['code'] = '1';
- $json['info'] = '修改失败';
- } else {
- $json['code'] = '0';
- $json['info'] = '修改成功';
- }
- return $json;
- }
-
- /**
- * Function Description:上传图片
- * Function Name: uploadImg
- * @param $pic
- *
- * @return string|void
- *
- * @author 李健
- */
- public function uploadImg($pic)
- {
- // 允许的图片类型
- $allowedExts = array("gif", "jpeg", "jpg", "png");
- $extension = end(explode('.',$pic['name']));
- if(!in_array($extension,$allowedExts)){
- return;
- }
- if(!is_uploaded_file($pic['tmp_name'])) {
- return ;
- }
- $upload_path = "http://". CS1_DOMAIN. "/upload/image/";
- $name = sha1(microtime()) . "." . $extension;
- if(move_uploaded_file($pic["tmp_name"],'upload/image/' . $name)){
- $response['link'] = $upload_path.$name;
- return json_encode($response);
- }
- }
-
- /**
- * Function Description:保存文档
- * Function Name: saveDocument
- * @param $type
- * @param $module
- * @param $sort
- * @param $title
- * @param $content
- * @param $release_type
- *
- * @return mixed
- *
- * @author 李健
- */
- public function saveDocument($type,$module,$sort,$title,$content,$release_type)
- {
- // 校验模块
- if($type==3){
- $json = $this->checkFunction($type,$module);
- if($json['code']!=0){
- return $json;
- }
- }
-
- // 校验标题
- $json = $this->checkTitle($title);
- if($json['code']!=0){
- return $json;
- }
- $opera_document = new OperaDocument();
- $res = $opera_document->saveDocument($type,$module,$sort,$title,$content,$release_type);
- if(!$res){
- $json['code'] = 1;
- $json['info'] = '保存文档失败';
- } else {
- $json['code'] = 0;
- if($release_type==1){
- $json['info'] = '保存并发布文档成功';
- } else {
- $json['info'] = '保存文档成功';
- }
-
- }
- return $json;
- }
-
- /**
- * Function Description:修改文档
- * Function Name: updateDocument
- * @param $id
- * @param $type
- * @param $module
- * @param $sort
- * @param $title
- * @param $content
- * @param $release_type
- *
- * @return mixed
- *
- * @author 李健
- */
- public function updateDocument($id,$type,$module,$sort,$title,$content,$release_type)
- {
- // 校验模块
- if($type==3){
- $json = $this->checkFunction($type,$module,$id);
- if($json['code']!=0){
- return $json;
- }
- }
- // 校验标题
- $json = $this->checkTitle($title,$id);
- if($json['code']!=0){
- return $json;
- }
- $opera_document = new OperaDocument();
- $res = $opera_document->updateDocument($id,$type,$module,$sort,$title,$content,$release_type);
- if(!$res){
- $json['code'] = 1;
- $json['info'] = '修改保存文档失败';
- } else {
- $json['code'] = 0;
- if($release_type==1){
- $json['info'] = '修改并发布文档成功';
- } else {
- $json['info'] = '修改并保存文档成功';
- }
-
- }
- return $json;
- }
-
- /**
- * Function Description:检查功能说明是否存在
- * Function Name: checkFunction
- * @param $type
- * @param $module
- * @param $id
- *
- * @return array|null|\yii\db\ActiveRecord
- *
- * @author 李健
- */
- public function checkFunction($type,$module,$id=0)
- {
- $opera_document = new OperaDocument();
- $res = $opera_document->checkFunction($type,$module);
- if($res){
- if($id!=0 && $res['id'] == $id){
- $res['code'] = 0;
- } else {
- $res['code'] = 1;
- $res['info'] = '该模块功能说明已存在请勿重复添加';
- }
- } else {
- $res['code'] = 0;
- }
- return $res;
- }
-
- /**
- * Function Description:校验标题唯一性
- * Function Name: checkTitle
- * @param $title
- * @param int $id
- *
- * @return array|null|\yii\db\ActiveRecord
- *
- * @author 李健
- */
- public function checkTitle($title,$id=0)
- {
- $opera_document = new OperaDocument();
- $res = $opera_document->checkTitle($title);
- if($res){
- if($id!=0 && $res['id'] == $id){
- $res['code'] = 0;
- } else {
- $res['code'] = 1;
- $res['info'] = '该标题已存在请勿重复添加';
- }
- } else {
- $res['code'] = 0;
- }
- return $res;
- }
-
- /**
- * Function Description:更新排序
- * Function Name: updateSort
- * @param $sort
- * @param $id
- *
- * @return mixed
- *
- * @author 李健
- */
- public function updateSort($sort,$id)
- {
- $opera_document = new OperaDocument();
- $res = $opera_document->updateSort($sort,$id);
- if($res){
- $json['code'] = 0;
- $json['info'] = '排序更新成功';
- } else {
- $json['code'] = 1;
- $json['info'] = '排序更新失败';
- }
- return $json;
- }
- }
|