<?php
/**
 * [LJCMS] (C)2012-2099 LJdev,Inc.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2013.11.19
*/

class adminuserAModel extends X {
    
    public function getList($items) {
        $where      = " WHERE 1=1".$items['searchsql'];
        $start      = ($items['page']-1)*$items['pagesize'];
        $countsql   = "SELECT COUNT(v.adminid) FROM ".DB_PREFIX."login AS v".$where;
        $total      = parent::$obj->fetch_count($countsql);
        $sql        = "SELECT v.*,g.groupname".
                      " FROM ".DB_PREFIX."login AS v".
                      " LEFT JOIN ".DB_PREFIX."authgroup AS g ON v.groupid=g.groupid".
                      $where." ORDER BY v.adminid DESC LIMIT ".$start.", ".$items['pagesize']."";
        $array = parent::$obj->getall($sql);
        return array($total, $array);
    }


     public function getData($id) {
        $sql = "SELECT * FROM ".DB_PREFIX."login WHERE adminid='".intval($id)."'";
        return parent::$obj->fetch_first($sql);
    }
      public function doDel($id) {
        $sql = "DELETE FROM ".DB_PREFIX."login WHERE adminid='".intval($id)."'";
        return parent::$obj->query($sql);
    }
    public function doEdit($id, $array) {
        if (isset($array['password']) && !empty($array['password'])) {
            $array['password'] = md5($array['password']);
        }
    return parent::$obj->update(DB_PREFIX.'login', $array, 'adminid='.$id.'');
    }
        public function doUserEditPass($password) {
        $md5password = md5($password);
        $array = array(
            '`password`'=>$md5password,
        );
        $result = parent::$obj->update(DB_PREFIX.'login', $array, "adminid='".intval(parent::$wrap_admin['adminid'])."'");
        if (true === $result){           
      
            $sid = '';
            $sid .= parent::$wrap_admin['adminid'].'\t';
            $sid .= parent::$wrap_admin['adminname'].'\t';
            $sid .= $md5password.'\t';
            $sid .= parent::$wrap_admin['lasttime'];
            $code = parent::import('code', 'util');
            $sid = $code->authCode($sid, 'ENCODE', LJCMS_RANDKEY);
            unset($code);                    
      parent::loadUtil('cookie');
            XCookie::del('app_admininfo');
      XCookie::set('app_admininfo', $sid);  
        }
        return $result;
    }



    public function getListUser($items) {
        $countsql = "SELECT COUNT(*) FROM oa_user ".$items['searchsql'] ;
        $total    = parent::$obj->fetch_count($countsql);
        $sql = "SELECT * FROM oa_user  ".$items['searchsql'];
        $data=parent::$obj->getall($sql);

        return array($total, $data);
    }

    public function getuserDataid($id)
    {
        $sql = "SELECT * FROM oa_user WHERE id='" . $id . "'";
        return parent::$obj->fetch_first($sql);
    }

    public function deluser($id)
    {
        $sql = "DELETE FROM oa_user WHERE id='" . intval($id) . "'";
        return parent::$obj->query($sql);
    }

    public function doEditUser($id,$arr){
        if (isset($arr['password']) && !empty($arr['password'])) {
            $arr['password'] = md5($arr['password']);
        }
        return parent::$obj->update('oa_user', $arr, 'id='.$id.'');
    }
}
?>
