api/App/Models/Doc.php

313 lines
10 KiB
PHP

<?php
namespace App\Models;
use Core\Model;
use PDO;
class Doc extends Model
{
public $tableName = 'doc';
public function __construct() {
parent::__construct();
}
/**
* Get all the users as an associative array
*
* @return array
*/
public function getAll()
{
$db = static::getDB();
$stmt = $db->query('SELECT * FROM doc');
return $stmt->fetchAll();
}
public function Columns(){
return $this->columns;
}
public function getTypeDocs($type){
$db = static::getDB();
//var_dump($db);
$stmt = $db->query('select doc.id,doc.title,doc.type from doc where doc.type= \''.strval($type). '\' and is_public = 0');
$result = $stmt->fetchAll(PDO::FETCH_UNIQUE);
//print_r($stmt->error);
return $result;
}
public function getGroupType($group){
$db = static::getDB();
$stmt = $db->query('select * from doc_type where doc_type.group= '.strval($group));
$result = $stmt->fetchAll(PDO::FETCH_UNIQUE);
return $result;
}
public function getTypes(){
$db = static::getDB();
$stmt = $db->query('select id,type_name from doc_type');
$result = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
return $result;
}
public function getGroups(){
$db = static::getDB();
$stmt = $db->query('select * from doc_group');
$result = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
return $result;
}
public function docs($id){
$result = [];
if(is_integer($id)){
$db = static::getDB();
$stmt = $db->query('select id,title,type from doc where type = '.$id.' and is_public = 0');
$recv = $stmt->fetchAll();
foreach ($recv as $key => $value){
$s['id']= $value['id'];
$s['title']= $value['title'];
array_push($result,$s);
}
return $result;
}
}
public function topDoc(){
$result = [];
$db = static::getDB();
$stmt = $db->query('select * from doc where doc.is_public = 0 order by TIMESTAMP(update_time) desc LIMIT 5 ');
$recv = $stmt->fetchAll();
return $recv;
}
public function DocVersTime($id){
$db = static::getDB();
$stmt = $db->query('select edit_time from doc_history where doc_history.doc_id = ' .$id);
$recv = $stmt->fetchAll();
return $recv;
}
public function typeCounts($type){
$db = static::getDB();
$stmt = $db->query('select count(id) from doc where doc.type = '.$type);
$recv = $stmt->fetchAll();
return $recv;
}
public function pageDoc($limit ,$offset,$type){
$result = [];
$db = static::getDB();
$stmt = $db->query('select * from doc where doc.is_public = 0 and doc.type = '.$type. ' limit '.$limit.' offset '.$offset);
$recv = $stmt->fetchAll();
return $recv;
}
public function doc_history($doc_id){
$result = [];
$db = static::getDB();
$stmt = $db->query('select * from doc_history where doc_history.doc_id = '.$doc_id);
$recv = $stmt->fetchAll();
return $recv;
}
public function top5Doc(){
$result = [];
$db = static::getDB();
$stmt = $db->query('select id,title,type from doc_copy1 where doc_copy1.is_public = 1 order by id desc limit 5;');
$recv = $stmt->fetchAll();
return $recv;
}
public function doc($id){
if(is_integer($id)){
$db = static::getDB();
$stmt = $db->query('select * from doc where id = '.$id.' and is_public = \'0\'');
$recv = $stmt->fetchAll();
return $recv;
}
}
public function docTree($id){
if(is_integer($id)){
$db = static::getDB();
$stmt = $db->query('select * from doc_copy1 where id = '.$id);
$recv = $stmt->fetchAll();
return $recv;
}
}
public function docHistoryTree($id,$time){
if(is_integer($id)){
$db = static::getDB();
$stmt = $db->query('select * from
doc_history where doc_id = '.$id . '
and edit_time = '.'\''. $time.'\'');
$recv = $stmt->fetchAll();
return $recv;
}
}
public function titleDoc($title){
$db = static::getDB();
$stmt = $db->query('select * from doc_copy1 where title = \''.$title.'\'');
$recv = $stmt->fetchAll();
return $recv;
}
public function titleLikeDoc($title){
$db = static::getDB();
$stmt = $db->query('select title from doc_copy1 where title like \'%'.$title.'%\' and doc.is_public = 0');
$recv = $stmt->fetchAll();
return $recv;
}
public function Count(){
$db = static::getDB();
$stmt = $db->query('select count(*) from doc_copy1');
$result = $stmt->fetchAll();
return $result;
}
public function DocTypes(){
$db = static::getDB();
$stmt = $db->query('select * from doc_type');
$result = $stmt->fetchAll();
return $result;
}
public function AllTitle(){
$db = static::getDB();
$stmt = $db->query('select title from doc_copy1');
$result = $stmt->fetchAll();
return $result;
}
public function getChildrenDocs($id){
$db = static::getDB();
$stmt = $db->query('select * from doc_copy1 where doc_copy1.father = '.$id);
$result = $stmt->fetchAll();
return $result;
}
public function getAllTypeDocs(){
$typedocs = [];
$doctypes = $this->getTypes();
foreach ($doctypes as $key => $value){
$val = [];
$typedoc = $this->getTypeDocs($key);
$val["arr"] = $typedoc;
$val["key"] = $key;
$typedocs[$value] = $val;
}
return $typedocs;
}
public function getArticlesTree($opendocid){
$db = static::getDB();
$mapforbuildtree = [];
$articletree = [];
$stmt = $db->query('select max(level) as maxlevel from doc_copy1');
$max_level = $stmt->fetchAll();
$max_level = $max_level[0]['maxlevel'];
$stmt = $db->query('select id,title,father,level from doc_copy1');
$doc_all = $stmt->fetchAll();
foreach ($doc_all as $key => $value){
$tmp = array(
'name' => $value['title'],
'id' =>$value['id'],
'isParent'=> false,
'url' => \App\Config::Url() . "ArticleTree/article/" . $value['id'],
'father' => (int)$value['father'],
'level' => $value['level']
);
if($tmp['level'] == '0') {
$tmp = array(
'name' => $value['title'],
'id' =>$value['id'],
'isParent'=> false,
'url' => \App\Config::Url() . "ArticleTree/article/" . $value['id'],
'father' => (int)$value['father'],
'level' => $value['level'],
'open' => true
);
}
if($opendocid == $value['father'])
$tmp['open'] = true;
$mapforbuildtree[$value['id']] = $tmp;
}
for($i = $max_level;$i >= 0;$i --){
foreach ($doc_all as $key => $value){
if($value['level'] == strval($i)){
if($value['father'] != 0){
$father = &$mapforbuildtree[$value['father']];
if(!isset( $father['children'])){
$mapforbuildtree[$value['father']]['children'] = [];
}
$father_of_children = &$father['children'];
$tmp = &$mapforbuildtree[$value['id']];
array_push($father_of_children,
$tmp);
$father["isParent"] = true;
}
}
}
}
$stack = [];
if($opendocid > 0){
array_push($stack,$opendocid);
// var_dump($mapforbuildtree[$opendocid]);
$fahterid = $mapforbuildtree[$opendocid]['father'];
array_push($stack,$fahterid);
while($fahterid != '0'){
// var_dump($mapforbuildtree[$fahterid]);
$fahterid = $mapforbuildtree[$fahterid]['father'];
array_push($stack,$fahterid);
}
// var_dump(1234,$mapforbuildtree[$fahterid]);
if(count($stack) > 2) {
$i = count($stack) - 2;
$tmp = &$mapforbuildtree[$stack[$i]];
for(;$i > 0;$i--){
$tmp['open'] = true;
if((isset($tmp['children']))){
foreach($tmp['children'] as $key => $value){
if($value['id'] == $stack[$i - 1]){
$tmp = &$tmp['children'][$key];
}
}
}
}
}
// $tmp = $mapforbuildtree[$stack[$i]];
}
foreach ($doc_all as $key => $value){
if($value['level'] == 0){
array_push($articletree,$mapforbuildtree[$value['id']]);
}
}
$dat = json_encode ( $articletree ,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE );
return $dat;
}
public function getAllDocGroup(){
$groups = $this->getGroups();
return $groups;
}
public function GetAllGroupType(){
$grouptype = [];
$groups = $this->getGroups();
foreach ($groups as $key => $value){
$val = [];
$typedoc = $this->getGroupType($key);
$val["arr"] = $typedoc;
$val["key"] = $key;
$grouptype[$key] = $val;
}
return $grouptype;
}
public function TypeGroup($id){
$db = static::getDB();
$stmt = $db->query("select doc_type.group from doc_type where id = ".$id);
$result = $stmt->fetchAll();
return $result;
}
}