whereModel.php 7.5 KB

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