whereModel.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. class whereClassModel extends Model
  3. {
  4. private $moders = array();
  5. private $ursarr = array();
  6. public function initModel()
  7. {
  8. $this->settable('flow_where');
  9. $this->admindbs = m('admin');
  10. }
  11. /**
  12. * 条件格式化,返回是没有and开头的
  13. */
  14. public function getstrwhere($str, $uid=0, $fid='')
  15. {
  16. if(isempt($str))return '';
  17. if($uid==0)$uid = $this->adminid;
  18. $dbs = $this->admindbs;
  19. if(isset($this->ursarr[$uid])){
  20. $urs = $this->ursarr[$uid];
  21. }else{
  22. $urs = $dbs->getone($uid);
  23. $this->ursarr[$uid] = $urs;
  24. }
  25. $companyid = arrvalue($urs, 'companyid','0'); //对应单位ID
  26. $deptid = arrvalue($urs, 'deptid','0'); //部门ID
  27. if(ISMORECOM){
  28. $comid = arrvalue($urs, 'comid','0');
  29. if($comid>'0')$companyid = $comid;
  30. }
  31. $sw1 = $this->rock->dbinstr('superid',$uid);
  32. $super = "select `id` from `[Q]admin` where $sw1";//我的直属下属
  33. $allsuper = "select `id` from `[Q]admin` where instr(`superpath`,'[$uid]')>0"; //我所有下属的下属
  34. $companys = "select `id` from `[Q]admin` where `companyid`=".$companyid.""; //对应单位下的
  35. //加上a.
  36. $str = str_replace('[A]`uid`','`uid`', $str);
  37. $str = str_replace('[A]uid','`uid`', $str);
  38. $barr = $this->rock->matcharr($str,2);
  39. $itsha= array('status','uid','optid','optname','applydt','createdt','createid');
  40. $thar = array();
  41. foreach($barr as $bsuid){
  42. if(in_array($bsuid, $itsha) && !in_array($bsuid, $thar)){
  43. $thar[] = $bsuid;
  44. $str = str_replace('`'.$bsuid.'`','{asqom}`'.$bsuid.'`', $str);
  45. }
  46. }
  47. $str = m('base')->strreplace($str, $uid);
  48. $str = str_replace(array('{super}','{allsuper}','{company}'), array($super,$allsuper,$companys), $str);
  49. //未读替换
  50. if(contain($str,'{unread}')){
  51. $rstr = '';
  52. if($this->moders){
  53. $ydid = m('log')->getread($this->moders['table'], $uid);
  54. $rstr = '{asqom}`id` not in('.$ydid.')';
  55. }
  56. $str = str_replace('{unread}', $rstr, $str);
  57. }
  58. //已读替换
  59. if(contain($str,'{read}')){
  60. $rstr = '';
  61. if($this->moders){
  62. $ydid = m('log')->getread($this->moders['table'], $uid);
  63. $rstr = '{asqom}`id` in('.$ydid.')';
  64. }
  65. $str = str_replace('{read}', $rstr, $str);
  66. }
  67. //receid
  68. if(contain($str,'{receid}')){
  69. $rstr= $dbs->getjoinstr('{asqom}`receid`', $uid, 1);
  70. $str = str_replace('{receid}', '('.$rstr.')', $str);
  71. }
  72. //本周一{weekfirst}
  73. if(contain($str,'{weekfirst}')){
  74. $rstr= c('date')->getweekfirst($this->rock->date);
  75. $str = str_replace('{weekfirst}', $rstr, $str);
  76. }
  77. //本周日{weeklast}
  78. if(contain($str,'{weeklast}')){
  79. $rstr= c('date')->getweeklast($this->rock->date);
  80. $str = str_replace('{weeklast}', $rstr, $str);
  81. }
  82. $barr = $this->rock->matcharr($str);
  83. foreach($barr as $match){
  84. $rstr = $type = '';
  85. $_artr= explode(',', $match);
  86. $fie = $_artr[0];
  87. if($fie=='asqom')continue;
  88. if(isset($_artr[1]))$type = $_artr[1];
  89. //包含uid里面:{uid,uidin}
  90. if($type=='uidin'){
  91. $rstr= $this->rock->dbinstr('{asqom}`'.$fie.'`', $uid);
  92. }
  93. //我直属下级:{uid,down}
  94. if($type=='down'){
  95. $rstr= $dbs->getdownwhere('{asqom}`'.$fie.'`', $uid, 1);
  96. }
  97. //我全部直属下级:{uid,downall}
  98. if($type=='downall'){
  99. $rstr= $dbs->getdownwhere('{asqom}`'.$fie.'`', $uid, 0);
  100. }
  101. //字段包含部门人员Id:{uid,receall}
  102. if($type=='receall'){
  103. $rstr= $dbs->getjoinstr('{asqom}`'.$fie.'`', $uid, 1);
  104. }
  105. //字段包含部门人员Id,空全部:{uid,recenot}
  106. if($type=='recenot'){
  107. $rstr= $dbs->getjoinstr('{asqom}`'.$fie.'`', $uid, 1, 1);
  108. }
  109. //我的同级部门人员:{uid,dept}
  110. if($match=='dept'){$type = 'dept';$fie = 'uid';}
  111. if($type=='dept'){
  112. $rstr= '{asqom}`'.$fie.'` in(select `id` from `[Q]admin` where `deptid`='.$deptid.' or '.$this->rock->dbinstr('deptids',$deptid).')';
  113. }
  114. //我的同级部门人员(含子部门):{uid,deptall}
  115. if($match=='deptall'){$type = 'deptall';$fie = 'uid';}
  116. if($type=='deptall'){
  117. $rstr= '{asqom}`'.$fie.'` in(select `id` from `[Q]admin` where instr(`deptpath`,\'['.$deptid.']\')>0)';
  118. }
  119. //所属单位:{uid,company}
  120. if($type=='company'){
  121. $rstr= '{asqom}`'.$fie.'` in('.$companys.')';
  122. }
  123. $str = str_replace('{'.$match.'}', '( '.$rstr.' )', $str); //加上括号
  124. }
  125. return $str;
  126. }
  127. public function getflowwhere($id, $uid=0, $fid='')
  128. {
  129. if(is_array($id)){
  130. $rs = $id;
  131. }else{
  132. $swhe = "`num`='$id'";
  133. if(is_numeric($id))$swhe=$id;
  134. $rs = $this->getone($swhe);
  135. }
  136. if(!$rs)return false;
  137. if($fid=='')$fid='`uid`';
  138. $modeid = (int)$rs['setid'];
  139. $this->moders = m('flow_set')->getone($modeid);
  140. $wheresstr = $this->getstrwhere($this->rock->jm->base64decode($rs['wheresstr']), $uid, $fid);
  141. $whereustr = $this->getstrwhere($this->rock->jm->base64decode($rs['whereustr']), $uid, $fid);
  142. $wheredstr = $this->getstrwhere($this->rock->jm->base64decode($rs['wheredstr']), $uid, $fid);
  143. $str = $wheresstr;if(isempt($str))$str='';
  144. $ustr = $nstr = '';
  145. if(!isempt($rs['receid'])){
  146. $tsrt = m('admin')->gjoin($rs['receid'],'ud', 'where');
  147. if($tsrt=='all'){
  148. $tsrt = '1=1';
  149. }else{
  150. $tsrt = '('.$tsrt.')';
  151. }
  152. $ustr = $tsrt;
  153. }
  154. if(!isempt($whereustr)){
  155. if($ustr!='')$ustr.=' and ';
  156. $ustr .= $whereustr;
  157. }
  158. if(!isempt($rs['nreceid'])){
  159. $tsrt = m('admin')->gjoin($rs['nreceid'],'ud', 'where');
  160. if($tsrt=='all'){
  161. $tsrt = '1=1';
  162. }
  163. $nstr = $tsrt;
  164. }
  165. if(!isempt($wheredstr)){
  166. if($nstr!='')$nstr.=' or ';
  167. $nstr .= $wheredstr;
  168. }
  169. $astr = $str;
  170. if($ustr != '' || $nstr != ''){
  171. $_sar= '1=1';
  172. if($ustr!='')$_sar.=' and '.$ustr.'';
  173. if($nstr!='')$_sar.=' and not ('.$nstr.')';
  174. if(!isempt($astr))$astr.=' and ';
  175. $astr .= '{asqom}'.$fid.' in(select `id` from `[Q]admin` where '.$_sar.')';
  176. }
  177. return array(
  178. 'str' => $str,
  179. 'utr' => $ustr,
  180. 'ntr' => $nstr,
  181. 'atr' => $astr
  182. );
  183. }
  184. public function getwherestr($id, $uid=0, $fid='', $lx=0)
  185. {
  186. $where = '';
  187. $arr = $this->getflowwhere($id, $uid, $fid);
  188. if($arr){
  189. $where = $arr['atr'];
  190. if($lx==0 && !isempt($where))$where = ' and '.$where;
  191. }
  192. return $where;
  193. }
  194. public function getmywhere($modeid,$uid=0, $pnum='')
  195. {
  196. if($uid==0)$uid = $this->adminid;
  197. $where = m('admin')->getjoinstr('syrid', $uid, 1);
  198. $where = '`status`=1 and `setid`='.$modeid.' and `num` is not null and `islb`=1 ';//and ('.$where.')
  199. if(isempt($pnum)){
  200. $where .=" and ifnull(`pnum`,'')=''";
  201. }else{
  202. $where .=" and `pnum`='$pnum'";
  203. }
  204. $rows = $this->getrows($where, '`id`,`num`,`name`', '`sort`');
  205. return $rows;
  206. }
  207. /**
  208. * 判断条件是否可以使用$modeid模块id,$str条件
  209. */
  210. public function checkwhere($modeid, $str)
  211. {
  212. if(isempt($str))return '';
  213. $where = $this->rock->jm->base64decode($str);
  214. if($where=='all' || contain($where, '{super}') ||
  215. contain($where, '{allsuper}') ||
  216. contain($where, '{company}'))return '';
  217. $moders = m('flow_set')->getone($modeid);
  218. $this->moders = $moders;
  219. $where = $this->getstrwhere($where);
  220. $stable = $moders['table'];
  221. $where = '`id`=0 and '.str_replace('{asqom}','', $where);
  222. $sql = 'select * from `[Q]'.$stable.'` a where '.$where.'';
  223. $bool = $this->db->query($sql, false);
  224. if(!$bool)return '条件不能使用:'.$this->db->errorlast.'<hr>'.$this->db->getLastSql().'';
  225. return '';
  226. }
  227. }
粤ICP备19079148号