添加子菜单

来源:互联网 发布:深圳云计算招聘岗位 编辑:程序博客网 时间:2024/06/05 16:58
<?php
function get_attr($a,$pid){  


    global $tree;                                //每次都声明一个新数组用来放子元素  
    foreach($a as $v){  
        if($v['parentid'] == $pid){  
foreach($a as $av )
{
if($av['deptid']==$pid)
$v['parentname']=$av['deptname'];
}

$tree[]= $v;
//匹配子记录  
              get_attr($a,$v['deptid']); //递归获取子记录  
         
                                    //将记录存入新数组  
        }  
    }





return $tree;
                                //返回新数组  
}  




function ishas($name,$partentname,$alldept,$rootid){
//检测根,是否存在


if(empty($partentname))
{
return array('insert'=>1,'parentid'=>0);
}

    $depts=get_attr($alldept,$rootid);


//出路无根的情况


     if(empty($depts))
 {


      return array('insert'=>1,'parentid'=>$rootid);
}
//检测是否本类存在
foreach($depts as $dv)
{
if($dv['deptname']==$name)
{
return array('insert'=>0);
}
}


//处理有同级目录的情况
foreach($depts as $dv)
{






if($dv['parentname']==$partentname)
{
return array('insert'=>1,'parentid'=>$dv['parentid']);
}
}




}
class DepartmentzjModel extends BaseModel {
public $deptid;
public $sortorder;
    public function getDbConnection()
{
self::$db=Yii::app()->db;
if(self::$db instanceof CDbConnection)
return self::$db;
else
throw new CDbException(Yii::t('yii','Active Record requires a "dboa" CDbConnection application component.'));
}




    public  static function model($classname=__CLASS__)
    {
        return parent::model($classname);
    }
    
    public function tableName()
    {
        return "zjzm_departments";
    }
    
    public function rules()
    {
        return array(
            array("compid,deep,parentid,deptname,spellname,sortorder,phone,addr,postaddr,zip,email,fax,depturi,status,createtime,deletetime,dn,parentdn,detail","safe"),
  array('deptname','required','on'=>'save,update','message'=>'请检查必填项'),
        );
    }


public function attributeLabels(){
return array(
'deptname'=>'部门名称'
);

}
    
    public function GetReceiveList($pageno, $pagesize, $deptid)
{
$sql=$sql2 = "select * from zjzm_departments as m where 1=1 and compid=".Yii::app()->session["userlogin"]->compid." and parentid=".$deptid;
    $sql.=" order by sortorder desc";


$pagestart= (($pageno)*$pagesize);
$limit_condition = "  limit ". $pagestart . "," .$pagesize;
$sql = $sql . $limit_condition;






//$params["pagestart"] = $pagestart;
//$params["pagesize"] = $pagesize;


if(isset($params))
$list = $this->findAllBySql($sql,$params);
else
$list = $this->findAllBySql($sql);


return $list;


//$count = $this->count($limit_condition, $params);

//return array("list"=>$list, "count"=>$count);
}


public function GetReceiveListCount($deptid)
{
$sql=$sql2 = "select count(*) from zjzm_departments as m where 1=1 and compid=".Yii::app()->session["userlogin"]->compid." and parentid=".$deptid;


   if(isset($params))
$count = $this->countBySql($sql,$params);
else
$count = $this->countBySql($sql);
return $count;
}







public function resort($post,$upid,$downid)
{



 $connection = Yii :: app () -> db;
          $transaction = $connection -> beginTransaction ();






 
        try
        {
            // //eid,title,desc,createtime,endtime,deploytype,status,scope
            $sql = "update   zjzm_departments  set sortorder=".intval($upid)." where deptid=".intval($post[0]['deptid']);



            $cmd = $connection -> createCommand ( $sql );

            $res=$cmd ->execute();


            //surveyid,title,desc,type
            $sql = "update   zjzm_departments  set sortorder=".intval($downid)." where deptid=".intval($post[1]['deptid']);
            $cmd = $connection -> createCommand ( $sql );
            $result = $cmd -> execute ();
            $transaction -> commit ();
        }
        catch ( Exception $e )
        {
        
var_dump($e->getMessage());
            $transaction -> rollBack ();


return false;
        }


return true;
}


//删除功能
public function delete($deptid){
   $dept = new DepartmentzjModel();
        $all = $dept->findAll("compid=:compid",array(":compid"=>Yii::app()->session["userlogin"]->compid));
if(is_array($deptid))
{
foreach($deptid as $val)
{

$deptids=$this->get_child($val,$all);
$deptids[]=$val;
foreach($deptids as $v)
{
$count=EmployeesModel::model()->updateAll(array('deptid'=>0),'deptid=:pass',array(':pass'=>$v));
}
$deptidstr=implode(',',$deptids);
$connection = Yii :: app () -> db;
$transaction = $connection -> beginTransaction ();
try
{
// //eid,title,desc,createtime,endtime,deploytype,status,scope
$sql = "delete  from zjzm_departments where deptid in ($deptidstr)";
$cmd = $connection -> createCommand ( $sql );
$res=$cmd ->execute();  
$transaction -> commit ();
}
catch ( Exception $e )
{
var_dump($e->getMessage());
$transaction -> rollBack ();


return false;
}

}


return true;
}
else
{
$deptids=$this->get_child($deptid,$all);
$deptids[]=$deptid;
foreach($deptids as $v)
{
$count=EmployeesModel::model()->updateAll(array('deptid'=>0),'deptid=:pass',array(':pass'=>$v));
}
$deptidstr=implode(',',$deptids);
   $connection = Yii :: app () -> db;
$transaction = $connection -> beginTransaction ();
try
{
// //eid,title,desc,createtime,endtime,deploytype,status,scope
$sql = "delete  from zjzm_departments where deptid in ($deptidstr)";
$cmd = $connection -> createCommand ( $sql );
$res=$cmd ->execute();  
$transaction -> commit ();
}
catch ( Exception $e )
{
var_dump($e->getMessage());
$transaction -> rollBack ();


return false;
}
return true;
}


      
}


//获取子部门id
private function get_child($id,$all){


 static $data;
foreach($all as $val)
{
if($val['parentid']==$id)
{
 
$data[]=$val['deptid'];


$this->get_child($val['deptid'],$all);
}
}
return $data;
}




public function ImportDepart($arrs,$domain="@ims.test")
{
$table = new zjzm_departments();

foreach ($arrs as $ar)
{
$rnd = rand();

//$url = "dept_".$ar["compid"].'-'.$rnd.$domain;

try
{
if(empty($ar["parentname"]))
{
$inarr = array(
"compid"=>$ar["compid"],
"parentid"=>0,
"deptname"=>trim($ar["deptname"]),
"createtime"=>date("Y-m-d h:i:s"),
"deletetime"=>date("Y-m-d h:i:s"),
);
$count = $this->deptCount($ar["deptname"]);
if($count==0)
{
$result = $table->insert($inarr);
$url = "dept_".$ar["compid"].'-'.$result['deptid'].$domain;
$inarr = array(
"depturi"=>$url
);
$where = $this->db->quoteInto(" deptid=?", $result['deptid']);
$table->update($inarr, $where);
}
}
else
{
$count = $this->deptCount($ar["deptname"]);
if($count==0)
{
$where=$this->db->quoteInto(" deptname=?",trim($ar["parentname"]));
$dpt = $table->fetchRow($where);
$dptarr = $dpt->toArray();
$pid = $dptarr["deptid"];
$inarr = array(
"compid"=>$ar["compid"],
"parentid"=>$pid,
"deptname"=>$ar["deptname"],
"createtime"=>date("Y-m-d h:i:s"),
"deletetime"=>date("Y-m-d h:i:s"),
);

$result = $table->insert($inarr);
$url = "dept_".$ar["compid"].'-'.$result['deptid'].$domain;
$inarr = array(
"depturi"=>$url
);
$where = $this->db->quoteInto(" deptid=?", $result['deptid']);
$table->update($inarr, $where);
}
}
}
catch (Exception $e)
{
print_r($e->getMessage());
}
}
}

public  function deptCount($deptname)
{
$connection = Yii :: app () -> db;
$sql="select * from zjzm_departments where deptname='$deptname'";


         $cmd = $connection -> createCommand ( $sql );

         $res=$cmd ->queryRow();


return $res;




}


public  function deptCount2($deptname,$pid=0)
{
$connection = Yii :: app () -> db;
$sql="select * from zjzm_departments where deptname='$deptname' and parentid=$pid";

         $cmd = $connection -> createCommand ( $sql );

         $res=$cmd ->queryRow();

return $res;




}






public  function maxsortorder()
{
$connection = Yii :: app () -> db;
$sql="select max(sortorder) as maxorder from zjzm_departments";

         $cmd = $connection -> createCommand ( $sql );

         $res=$cmd ->queryRow();

return $res;




}





//导入
public function import($arr){
       
            global $tree;







foreach($arr as $key=>$val)
{
 



foreach($val as $k=>$v)
{
              
$alldept=array_map(function($record) {return $record->attributes;},$this->findAll());
   $connection = Yii :: app () -> db;
$sql="select max(sortorder) as maxorder from zjzm_departments";
$cmd = $connection -> createCommand ( $sql );
 $maxorder=$cmd ->queryRow();
 $maxorder=$maxorder['maxorder']+1;
$rootdept=$this->deptCount2($val[0]);
if($k==0)
{

 
 $ress=$this->deptCount2($v,0);


 $resaa=ishas($v,$ress['deptname'],$alldept,0);

                 

 



}else
{


      $resaa=ishas($v,$val[$k-1],$alldept,$rootdept['deptid']);
 
  
      
 



                  


}
          


         

               $tree=null;

if($resaa['insert'])
{


$sql="insert into zjzm_departments(deptname,compid,parentid,sortorder)value('$v',".Yii::app()->session["userlogin"]->compid.",".$resaa['parentid'].",".($maxorder).")";




$cmd = $connection -> createCommand ( $sql );
$res=$cmd ->execute();


   $sql=" SELECT LAST_INSERT_ID() as deptid";
$cmd = $connection -> createCommand ( $sql );
$res=$cmd ->queryRow();
                   
$domain = '@open-ims.test';
$url = "dept_".Yii::app()->session["userlogin"]->compid.'-'.$res['deptid'].$domain;
$sql="update zjzm_departments set depturi='$url' where deptid=$res[deptid]";

$cmd = $connection -> createCommand ( $sql );

$res=$cmd ->execute();




  $company = CompanyModel::model()->findByPk(Yii::app()->session["userlogin"]->compid);
  $company->corpversion += 1;$company->update();
 



}
             }





 
 



}


return true;
















foreach($newarr as $key=>$val)
{
//每次都更新newarr
  //获取根节点id
             $alldept=array_map(function($record) {return $record->attributes;},$this->findAll());




         
$ress=$this->deptCount2($val['parentname']);
$ress2=$this->deptCount2($val['deptname']);


if(count($ress2))
continue;

        $resaa=$this->ishas($val['deptname'],$ress['deptname'],$alldept,$ress['parentid']);


if($resaa['insert'])
{


$sql="insert into zjzm_departments(deptname,compid,sortorder)value('$val[deptname]',".Yii::app()->session["userlogin"]->compid.",".($maxorder+$key).")";




$cmd = $connection -> createCommand ( $sql );
$res=$cmd ->execute();


   $sql=" SELECT LAST_INSERT_ID() as deptid";
$cmd = $connection -> createCommand ( $sql );
$res=$cmd ->queryRow();
                   
$domain = '@open-ims.test';
$url = "dept_".Yii::app()->session["userlogin"]->compid.'-'.$res['deptid'].$domain;
$sql="update zjzm_departments set depturi='$url' where deptid=$res[deptid]";

$cmd = $connection -> createCommand ( $sql );

$res=$cmd ->execute();




  $company = CompanyModel::model()->findByPk(Yii::app()->session["userlogin"]->compid);
  $company->corpversion += 1;$company->update();
 



}
}

/*
  //获取根节点id
        $rootdeptid=0;
foreach($arr as $key=>$val)
{

foreach($val as $k=>$v)
{
 if($k==0)
{
$rootdept= $this->deptCount($v);
if(count($rootdept))
$rootdeptid=$rootdept['deptid'];

}
  
$arr2=array('deptname'=>$v,'rootdeptid'=>$rootdeptid);
if(isset($val[$k-1]))
$arr2=array_merge($arr2,array('parentname'=>$val[$k-1]));
$newarr[]=$arr2;


}
}



$connection = Yii :: app () -> db;
      $sql="select max(sortorder) as maxorder from zjzm_departments";
      $cmd = $connection -> createCommand ( $sql );
      $maxorder=$cmd ->queryRow();
 $maxorder=$maxorder['maxorder']+1;





foreach($newarr as $key=>$val)
{
//每次都更新newarr
  //获取根节点id
        $rootdeptid=0;
foreach($arr as $key=>$val)
{

foreach($val as $k=>$v)
{
 if($k==0)
{
$rootdept= $this->deptCount($v);
if(count($rootdept))
$rootdeptid=$rootdept['deptid'];

}
  
$arr2=array('deptname'=>$v,'rootdeptid'=>$rootdeptid);
if(isset($val[$k-1]))
$arr2=array_merge($arr2,array('parentname'=>$val[$k-1]));
$newarr[]=$arr2;


}
}
//----end
if(empty($val['parentname']))
{

$res=$this->deptCount($val['deptname']);
if($res['count']==0)
{
$sql="insert into zjzm_departments(deptname,compid,sortorder)value('$val[deptname]',".Yii::app()->session["userlogin"]->compid.",".($maxorder+$key).")";

$cmd = $connection -> createCommand ( $sql );
$res=$cmd ->execute();


   $sql=" SELECT LAST_INSERT_ID() as deptid";
$cmd = $connection -> createCommand ( $sql );
$res=$cmd ->queryRow();
                   
$domain = '@open-ims.test';
$url = "dept_".Yii::app()->session["userlogin"]->compid.'-'.$res['deptid'].$domain;
$sql="update zjzm_departments set depturi='$url' where deptid=$res[deptid]";

$cmd = $connection -> createCommand ( $sql );

$res=$cmd ->execute();




  $company = CompanyModel::model()->findByPk(Yii::app()->session["userlogin"]->compid);
  $company->corpversion += 1;$company->update();
 
}


}else
{
 $res = $this->deptCount($val['deptname']);
if($res['count']==0)
{
$sql="select * from zjzm_departments where deptname='$val[parentname]'";
$connection = Yii :: app () -> db;
$cmd = $connection -> createCommand ( $sql );
    $res=$cmd ->queryRow();
$pid = $res["deptid"];
$inarr = array(
"compid"=>Yii::app()->session["userlogin"]->compid,
"parentid"=>$pid,
"deptname"=>$val["deptname"],
"createtime"=>date("Y-m-d h:i:s"),
);

$sql="insert into zjzm_departments(compid,parentid,deptname,createtime,sortorder)values(".Yii::app()->session["userlogin"]->compid.",".$pid.",'$val[deptname]','".date("Y-m-d h:i:s")."',".($maxorder+$key).")";

$cmd = $connection -> createCommand ( $sql );
   $res=$cmd ->execute();


$sql=" SELECT LAST_INSERT_ID() as deptid";
$cmd = $connection -> createCommand ( $sql );
$res=$cmd ->queryRow();
 
$domain = '@open-ims.test';
$url = "dept_".Yii::app()->session["userlogin"]->compid.'-'.$res['deptid'].$domain;
$sql="update zjzm_departments set depturi='$url' where deptid=$res[deptid]";
$cmd = $connection -> createCommand ( $sql );
$res=$cmd ->execute();




  $company = CompanyModel::model()->findByPk(Yii::app()->session["userlogin"]->compid);
  $company->corpversion += 1;$company->update();
}

}
}
*/
}




 public function Getusers($deptid)
{
$sql=$sql2 = "select *,u.email,u.imageurl,u.fullspell,u.realname,u.username from syberos_userinfo as u left join  syberos_employees as m on u.userid=m.userid left join zjzm_departments as  d on m.deptid=d.deptid where 1=1 and m.compid=".Yii::app()->session["userlogin"]->compid." and m.deptid in (".$deptid.")";

$connection = Yii :: app () -> db;
$cmd = $connection -> createCommand ( $sql );
$list=$cmd ->queryAll();


return $list;


//$count = $this->count($limit_condition, $params);

//return array("list"=>$list, "count"=>$count);
}


public function getdeptstr($deptid)
{
$sql=$sql2 = "select * from zjzm_departments as sd where 1=1 and sd.compid=".Yii::app()->session["userlogin"]->compid." and sd.deptid in (".$deptid.")";

$connection = Yii :: app () -> db;
$cmd = $connection -> createCommand ( $sql );
$list=$cmd ->queryAll();


return $list;


//$count = $this->count($limit_condition, $params);

//return array("list"=>$list, "count"=>$count);
}






//获取未分配部门用户
public function getnodept(){
$sql=$sql2 = "select *,u.email,u.imageurl,u.fullspell,u.realname,u.username from syberos_userinfo as u left join  syberos_employees as m on u.userid=m.userid left join zjzm_departments as  d on m.deptid=d.deptid where 1=1 and m.compid=".Yii::app()->session["userlogin"]->compid." and m.deptid=0";




//$params["pagestart"] = $pagestart;
//$params["pagesize"] = $pagesize;


$command = Yii::app()->db->createCommand($sql);
   $list= $command->queryAll($sql);
return $list;



}


}

原创粉丝点击