wordModel.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. <?php
  2. class wordClassModel extends Model
  3. {
  4. private $optionobj,$pathss;
  5. public function initModel()
  6. {
  7. $this->optionobj = m('option');
  8. }
  9. //获取路径
  10. public function getpath($id)
  11. {
  12. $this->pathss = array();
  13. $this->getpaths($id);
  14. return $this->pathss;
  15. }
  16. private function getpaths($id)
  17. {
  18. $rs = $this->getone($id);
  19. if($rs){
  20. $this->getpaths($rs['typeid']);
  21. $this->pathss[] = $rs;
  22. }
  23. }
  24. /**
  25. * 删除文件
  26. */
  27. public function delword($id)
  28. {
  29. if($this->rows('`typeid`='.$id.'')>0)return returnerror('有子目录不能删除');
  30. $rs = $this->getone($id);
  31. $fid = arrvalue($rs,'fileid','0');
  32. $this->delete($id);
  33. m('file')->delfile($fid); //同时删除文件
  34. return returnsuccess();
  35. }
  36. /**
  37. * 创建文件夹,$cqid 分区ID, $typeid上级文件夹
  38. */
  39. public function createfolder($name, $cqid, $typeid=0)
  40. {
  41. $arr['optid'] = $this->adminid;
  42. $arr['optname'] = $this->adminname;
  43. $arr['optdt'] = $this->rock->now;
  44. $arr['name'] = $name;
  45. $arr['cid'] = $cqid;
  46. $arr['typeid'] = $typeid;
  47. $arr['comid'] = m('admin')->getcompanyid();
  48. $arr['type'] = 1; //说明是文件夹
  49. $arr['id'] = $this->insert($arr);
  50. return $arr;
  51. }
  52. /**
  53. * 获取文档数据
  54. * $lx=0文档中心,1所有共享,2我共享的
  55. */
  56. public function getdata($lx=0)
  57. {
  58. //用来将原来的合并到现在的功能下
  59. $uid = $this->adminid;
  60. $fnum = 'folder'.$uid.'';
  61. $moopt = m('option');
  62. $onrs = $moopt->getone("`num`='$fnum'");
  63. if($onrs && isempt($onrs['value'])){
  64. $alltypeid = $moopt->getalldownid($onrs['id']);
  65. $nto = $this->rows('cid=0 and `typeid` in('.$alltypeid.')');
  66. //存在的 建自己的分区
  67. if($nto>0){
  68. $adfe['name'] = ''.$this->adminname.'的分区';
  69. $adfe['recename'] = $this->adminname;
  70. $adfe['receid'] = 'u'.$uid.'';
  71. $adfe['guanname'] = $this->adminname;
  72. $adfe['guanid'] = 'u'.$uid.'';
  73. $adfe['optdt'] = $this->rock->now;
  74. $adfe['uid'] = $uid;
  75. $adfe['optname'] = $this->adminname;
  76. $mycid = m('worc')->insert($adfe);//新的分区
  77. $alltypeida = explode(',', $alltypeid);
  78. //建文件夹
  79. foreach($alltypeida as $ntyid){
  80. if($this->rows('typeid='.$ntyid.'')==0)continue;
  81. $fars = $moopt->getone($ntyid);
  82. $wjar = $this->createfolder($fars['name'], $mycid);//创建文件夹
  83. $ntypeid = $wjar['id'];
  84. $this->update(array('cid' => $mycid,'typeid' => $ntypeid),'`typeid`='.$ntyid.'');//更新
  85. }
  86. }
  87. $moopt->update("`value`='success'", $onrs['id']);
  88. }
  89. $cqids = c('check')->onlynumber($this->rock->post('cqids','-1'));
  90. $cqid = (int)$this->rock->post('cqid','0'); //打开的分区
  91. $typeid = (int)$this->rock->post('typeid','0'); //文件夹ID
  92. $atype = $this->rock->post('atype');
  93. if($atype=='shateall')$lx=1;
  94. if($atype=='shatewfx')$lx=2;
  95. $ismanage = 0;
  96. $isup = 0;
  97. $key = $this->rock->post('key');
  98. $where = 'a.`cid` in('.$cqids.') ';
  99. //管理中心
  100. if($lx==0){
  101. if($cqid>0){
  102. $where.=' and a.`cid`='.$cqid.'';
  103. $dbs = m('admin');
  104. //判断是否有管理权限
  105. $cqrs = m('worc')->getone($cqid);
  106. if(!isempt($cqrs['guanid'])){
  107. if($dbs->containjoin($cqrs['guanid'], $uid))$ismanage=1;
  108. }else{
  109. if($dbs->getmou('type', $uid)=='1')$ismanage=1;
  110. }
  111. if(isempt($cqrs['upuserid'])){
  112. if($cqrs['uid']==$uid)$isup = 1;
  113. }else{
  114. if($dbs->containjoin($cqrs['upuserid'], $uid))$isup=1;
  115. }
  116. }
  117. $where.=' and a.`typeid`='.$typeid.'';
  118. if(!isempt($key))$where = 'a.`cid` in('.$cqids.') ';
  119. }
  120. //共享给我的
  121. if($lx==1){
  122. $where1 = m('admin')->getjoinstrs('a.`shateid`', $uid, 1);
  123. //获取共享的目录ID
  124. $frows = $this->db->getall('select a.id,a.cid from `[Q]word` a where a.`type`=1 '.$where1.'');
  125. $alltyids= '-1';
  126. foreach($frows as $k1=>$rs1){
  127. $afflow = $this->getfolders($rs1['cid'], $rs1['id']);
  128. $alltyids.=','.$rs1['id'].'';
  129. foreach($afflow as $k2=>$rs2)$alltyids.=','.$rs2['id'].'';
  130. }
  131. $where = 'a.`type`=0 and ((1 '.$where1.') or (a.`typeid` in('.$alltyids.')))';
  132. $where .= m('admin')->getcompanywhere(1,'a.');
  133. }
  134. //我共享的
  135. if($lx==2){
  136. $where = 'a.`type`=0 and a.`shateid` is not null and a.`optid`='.$uid.'';
  137. $where .= m('admin')->getcompanywhere(1,'a.');
  138. }
  139. //关键词的搜索
  140. if(!isempt($key)){
  141. $where .=" and (b.`filename` like '%$key%' or a.`name` like '%$key%')";
  142. }
  143. $sarr = array(
  144. 'where' => $where,
  145. 'table' => '`[Q]word` a left join `[Q]file` b on a.`fileid`=b.`id`',
  146. 'fields'=> 'a.*,b.filename,a.sort,b.filesizecn,b.filenum,b.thumbplat,b.filesize,b.fileext,b.filepath,b.thumbpath,b.downci,b.`filepathout`',
  147. 'order' => 'a.`type` desc,a.`sort`,a.id desc'
  148. );
  149. $barr = $this->getlimit($sarr['where'], (int)$this->rock->post('page','1'), $sarr['fields'], $sarr['order'], (int)$this->rock->post('limit','15'), $sarr['table']);
  150. $barr['totalCount'] = $barr['count'];
  151. $rows = $barr['rows'];
  152. $fobj = m('file');
  153. //显示路径
  154. foreach($rows as $k=>&$rs){
  155. if($rs['type']=='1'){
  156. $rs['fileext'] = 'folder';
  157. $rs['optname'] = '';
  158. $rs['optdt'] = '';
  159. $rs['fileid'] = '';
  160. $downci = $this->rows('`typeid`='.$rs['id'].'');
  161. $rs['filesizecn'] = ($downci==0)?'':'<font color=#888888>有子目录</font>';
  162. }else{
  163. if(isempt($rs['name']))$rs['name'] = $rs['filename'];
  164. $rs['thumbpath'] = $fobj->getthumbpath($rs); //缩略图的路径
  165. $fpath = $rs['filepath'];
  166. $wjstatus= 1;
  167. if(substr($fpath,0,4)=='http'){
  168. $wjstatus = 2;
  169. }else{
  170. $filepathout = $rs['filepathout'];
  171. if(isempt($filepathout)){
  172. if(!file_exists($fpath) && isempt($rs['filenum'])){
  173. $wjstatus=0;
  174. $rs['ishui']=1;
  175. }
  176. }else{
  177. if($fobj->isimg($rs['fileext']))$rs['filepath'] = $filepathout;
  178. $wjstatus = 2;
  179. }
  180. }
  181. $rs['wjstatus'] = 2;
  182. }
  183. }
  184. if($lx==0){
  185. $barr['cprow']= m('worc')->getone($cqid);
  186. $barr['patha']= $this->getpath($typeid);
  187. }
  188. $barr['rows'] = $rows;
  189. $barr['ismanage'] = $ismanage; //是否有管理权限
  190. $barr['isup'] = $isup; //是否可上传
  191. $barr['officebj'] = getconfig('officebj');
  192. return $barr;
  193. }
  194. /**
  195. * 保存文件
  196. */
  197. public function savefile()
  198. {
  199. $cid = (int)$this->rock->post('cqid',0);
  200. $sid = $this->rock->post('sid');
  201. $typeid = (int)$this->rock->post('typeid','0');
  202. $sadid = explode(',', $sid);
  203. $arr['optid'] = $this->adminid;
  204. $arr['optname'] = $this->adminname;
  205. $arr['optdt'] = $this->rock->now;
  206. $arr['cid'] = $cid;
  207. $arr['comid'] = m('admin')->getcompanyid();
  208. $arr['typeid'] = $typeid;
  209. $file = m('file');
  210. $id = 0;
  211. foreach($sadid as $fid){
  212. $arr['fileid'] = $fid;
  213. $id = $this->insert($arr);
  214. $file->addfile($fid, 'word', $id, 'word');
  215. }
  216. $names = '';
  217. $frows = $file->getall('`id` in('.$sid.')');
  218. $zongs = count($frows);
  219. foreach($frows as $k=>$rs){
  220. if($k<3)$names.=','.$rs['filename'].'';
  221. }
  222. //发送推送通知
  223. $cprs = m('worc')->getone($cid);
  224. $fors = $this->getone($typeid);
  225. if($cprs && $names!=''){
  226. $names= substr($names, 1);
  227. $receid= $cprs['receid'];
  228. if(isempt($receid))$receid = 'u0';
  229. if(!isempt(arrvalue($fors,'shateid')))$receid.=','.$fors['shateid'].''; //同时发给共享的
  230. $cont = "{$this->adminname}在“{$cprs['name']}”上传了{$zongs}个文件“{$names}”";
  231. $flow = m('flow')->initflow('word');
  232. if(arrvalue($flow->moders,'pctx')=='1')$flow->push($receid,'文档', $cont, ''.$this->adminname.'上传了文件',0, array(
  233. 'wxurl' => $flow->getwxurl(),
  234. 'id' => $id
  235. ));
  236. }
  237. }
  238. /**
  239. * 获取分区文件夹
  240. */
  241. public function getworcfolder()
  242. {
  243. $barr = m('worc')->getmywroc();
  244. $ids = $barr['ids'];
  245. $rows = $barr['rows'];
  246. $arr = array();
  247. foreach($rows as $k=>$rs){
  248. if($rs['ismanage']==0)continue;
  249. $cqid = $rs['id']; //区id
  250. $arr[] = array(
  251. 'cqid' => $cqid,
  252. 'typeid'=> 0,
  253. 'iconsimg'=> 'images/wjj.png',
  254. 'iconswidth'=> '24',
  255. 'name' => $rs['name'],
  256. );
  257. $rowa = $this->getfolders($rs['id']);
  258. foreach($rowa as $k1=>$rs1){
  259. $rs1['cqid'] = $cqid;
  260. $rs1['iconsimg'] = 'images/folder.png';
  261. $arr[] = $rs1;
  262. }
  263. }
  264. return $arr;
  265. }
  266. //获取文件夹,获取对应子目录
  267. private $getfoldersa;
  268. public function getfolders($cid, $typeid=0)
  269. {
  270. $rows = $this->getall('`cid`='.$cid.' and `type`=1','`id`,`name`,`typeid`','`sort`');
  271. $this->getfoldersa = array();
  272. $this->getfolderss($rows, $typeid,1);
  273. return $this->getfoldersa;
  274. }
  275. private function getfolderss($rows, $typeid, $lev=1)
  276. {
  277. foreach($rows as $k=>$rs){
  278. if($rs['typeid']==$typeid){
  279. $rs['padding'] = 24*$lev;
  280. $rs['typeid'] = $rs['id'];
  281. $this->getfoldersa[] = $rs;
  282. $this->getfolderss($rows, $rs['id'], $lev+1);
  283. }
  284. }
  285. }
  286. /**
  287. * 移动
  288. */
  289. public function movefile()
  290. {
  291. $cqid = (int)$this->rock->post('cqid','0');
  292. $typeid = (int)$this->rock->post('typeid','0');//文件夹
  293. $ids = $this->rock->post('ids','0');//要移动文件
  294. if($typeid>0){
  295. //判断是否在自己文件夹下
  296. $foldpath = $this->getpath($typeid);
  297. $foldar = array();
  298. foreach($foldpath as $k1=>$rs1)$foldar[] = $rs1['id'];
  299. $idsa = explode(',', $ids);
  300. foreach($idsa as $ids1){
  301. if(in_array($ids1, $foldar))return returnerror('['.$ids1.']不能移动到自己的子目录下');
  302. }
  303. }
  304. $this->update('`cid`='.$cqid.',`typeid`='.$typeid.'', "`id` in($ids)");
  305. //获取所有下级需要更新分区Id
  306. $this->moveaddid = '0';
  307. $this->getmovedanow($ids);
  308. if($this->moveaddid!='0')$this->update('`cid`='.$cqid.'', '`id` in('.$this->moveaddid.')');
  309. return returnsuccess();
  310. }
  311. private function getmovedanow($ids)
  312. {
  313. $addid = '';
  314. $rows = $this->getall('`typeid` in('.$ids.')');
  315. foreach($rows as $k=>$rs){
  316. $addid.=','.$rs['id'].'';
  317. }
  318. if($addid!=''){
  319. $addid = substr($addid,1);
  320. $this->moveaddid.=','.$addid.'';
  321. $this->getmovedanow($addid);
  322. }
  323. }
  324. /**
  325. * 共享
  326. */
  327. public function sharefile()
  328. {
  329. $ids = $this->rock->post('ids','0');
  330. $arr['shateid'] = $this->rock->post('sid');
  331. $arr['shate'] = $this->rock->post('sna');
  332. $this->update($arr, "`id` in($ids)");
  333. //发通知给对应人说共享了
  334. if(!isempt($arr['shateid'])){
  335. $names= '';
  336. $rows = $this->db->getall('select a.name,b.filename,a.id from `[Q]word` a left join `[Q]file` b on a.fileid=b.id where a.`id` in('.$ids.')');
  337. $id = 0;
  338. foreach($rows as $k=>$rs){
  339. $nas = $rs['name'];
  340. if(isempt($nas))$nas = $rs['filename'];
  341. if(!isempt($nas))$names.=','.$nas.'';
  342. if($id==0)$id = $rs['id'];
  343. }
  344. if($names!=''){
  345. $names= substr($names, 1);
  346. $cont = "{$this->adminname}共享了文件“{$names}”";
  347. $flow = m('flow')->initflow('word');
  348. $flow->push($arr['shateid'],'文档', $cont, ''.$this->adminname.'发来共享文件',0, array(
  349. 'wxurl' => $flow->getwxurl(),
  350. 'id' => $id
  351. ));
  352. }
  353. }
  354. }
  355. //----------------以下是旧的,后续会删除-------------
  356. /**
  357. * 读取对应顶级ID
  358. */
  359. public function getfolderid($uid, $isdept=false)
  360. {
  361. $num = "folder".$uid."";
  362. $name= ''.$this->adminname.'文件夹目录';
  363. if($isdept){
  364. $drs = m('dept')->getudept($uid);
  365. $num = 'deptfolder'.$drs['nums'].'';
  366. $name= ''.$drs['name'].'文件夹目录';
  367. }
  368. $id = $this->optionobj->getnumtoid($num, $name, false);
  369. return $id;
  370. }
  371. /**
  372. * 对应对应文件目录
  373. */
  374. public function getfoldrows($uid, $isdept=false)
  375. {
  376. $pid = $this->getfolderid($uid, $isdept);
  377. $rows = $this->optionobj->gettreedata($pid);
  378. return $rows;
  379. }
  380. /**
  381. * 读取所有目录
  382. */
  383. private $allfolder = array();
  384. public function getallfolder($idss='',$level=0)
  385. {
  386. $where = "`num` like 'folder%' or `num` like 'deptfolder%'";
  387. if(!isempt($idss))$where = "`pid` in($idss)";
  388. $rows = $this->db->getall("SELECT * FROM `[Q]option` where $where");
  389. $ids = '';
  390. foreach($rows as $k=>$rs){
  391. $ids.=','.$rs['id'].'';
  392. $rs['level'] = $level;
  393. if(!isempt($idss))$this->allfolder[] = $rs;
  394. }
  395. if($ids!=''){
  396. $this->getallfolder(substr($ids, 1), $level+1);
  397. }
  398. return $this->allfolder;
  399. }
  400. /**
  401. * 获取分享给我的目录(太复杂了,无法进行了)
  402. */
  403. public function getshatefolder($uid)
  404. {
  405. $allfolder = $this->getallfolder();
  406. $urs = $this->db->getone('[Q]admin','`id`='.$uid.'','id,deptpath');
  407. $uarr[] = 'u'.$uid.'';
  408. $deptpath = arrvalue($urs, 'deptpath');
  409. if(!isempt($deptpath)){
  410. $depa = explode(',', str_replace(array('[',']'), array('',''), $deptpath));
  411. foreach($depa as $depas){
  412. $uarr[] = 'd'.$depas.'';
  413. }
  414. }
  415. $rows = $this->getshatefolders($allfolder, 1, $uarr, 0);
  416. return $rows;
  417. }
  418. private function getshatefolders($allfolder, $level, $uarr, $pid=0)
  419. {
  420. $rarr = array();
  421. foreach($allfolder as $k=>$rs){
  422. $receid = $rs['receid'];
  423. if($level==1 && $rs['level']!=$level)continue;
  424. if($level>1 && $pid!=$rs['pid'])continue;
  425. $bo = false;
  426. if(!isempt($receid)){
  427. $receida = explode(',', $receid);
  428. foreach($uarr as $uarrs){
  429. if(in_array($uarrs, $receida))$bo=true;
  430. }
  431. }
  432. $rs['children'] = $this->getshatefolders($allfolder, $level+1, $uarr, $rs['id']);
  433. $rarr[] = $rs;
  434. }
  435. return $rarr;
  436. }
  437. /**
  438. * 模版替换
  439. */
  440. public function replaceWord($fid, $arr)
  441. {
  442. $word = c('PHPWord');
  443. if($fid==0 || !$word->isbool())return;
  444. $frs = m('file')->getone($fid);
  445. if($frs && $frs['fileext']=='docx'){
  446. $filepath = $frs['filepath'];
  447. if(contain($filepath,'rocktpl')){
  448. $tplid = str_replace('.docx','',substr($filepath, strripos($filepath, '_')+1));
  449. $tplrs = m('wordxie')->getone('`fileid`='.$tplid.'');
  450. $npath = ''.UPDIR.'/'.date('Y-m').'/'.date('d_His').''.rand(10,99).'.docx';
  451. if(!$tplrs)return;
  452. $this->rock->createdir($npath);
  453. $tplvar = $tplrs['tplvar'];
  454. $tihad = array();
  455. if(isempt($tplvar)){
  456. $tihad = $arr;
  457. }else{
  458. $tpla = explode(',', $tplvar);
  459. foreach($tpla as $k)$tihad[$k] = arrvalue($arr, $k);
  460. }
  461. if($tihad){
  462. $barr = $word->replaceWord($filepath, $tihad, $npath);
  463. if($barr['success']){
  464. $nnpath = $barr['data'];
  465. if(file_exists($nnpath)){
  466. $filesize = filesize($nnpath);
  467. $uarr['filepathout']= '';
  468. $uarr['pdfpath'] = '';
  469. $uarr['filepath'] = $nnpath;
  470. $uarr['filesize'] = $filesize;
  471. $uarr['filesizecn'] = $this->rock->formatsize($filesize);
  472. m('file')->update($uarr,$fid);
  473. @unlink($filepath);
  474. }
  475. }
  476. }
  477. }
  478. }
  479. }
  480. }
粤ICP备19079148号