>

Thursday, November 10, 2016

CI_admin_controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Adminpage extends CI_Controller
{

public function __construct() {

parent::__construct();
if(! $this->session->userdata('session_username'))
redirect('');

}

    public function index()
    {

//First we need to load the model
    $this->load->model('insertuser');

    //Now we need to get our user list using the function we write on our model
    $user_list = $this->insertuser->viewdata();

   //Finally, we send are list to the view so we can display it.
    $data["user_lst"] = $user_list;

    $this->load->view("admin/adminpage", $data); // this view is load and shows result

    }
   
    public function user_edit($id)
{
$this->load->model('insertuser');

$row=$this->insertuser->getonerow($id);

$data['r']=$row;
$this->load->view('admin/useredit',$data);
//redirect('Student/edit');
}

public function user_update()
{

 $id=$this->input->post('userid');
 $usertype=$this->input->post('usertype');
 $username=$this->input->post('username');

 $dataid = array('id' => $id,
                 'usertype' =>$usertype,
 'username' =>$username
 );

 if ($this->form_validation->run('edit_user'))
          {  
// form validation successfull
$data= array(
'firstname' => $this->input->post('firstname'),
'lastname' => $this->input->post('lastname'),
                        //'password' => $hashpwd,
       'email' => $this->input->post('emailid'),
                        'mobilenumber' => $this->input->post('mobilenumber'),
       'altermobilenum' => $this->input->post('altermobilenum'),
       'city' => $this->input->post('city'),
                        'state' => $this->input->post('state'),
       'country' => $this->input->post('country'),
                        'address' => $this->input->post('address'),
       'comment' => $this->input->post('comment'),
'usertype' => $usertype,
);

$this->db->where('userid',$id);
$this->db->update('login',$data);

redirect("admin_cont/adminpage");
 }
          else
          {
        // form validation NOT successfull
    $this->load->view('admin/usereditnotvalidate',$dataid);
 }

}

public function user_delete($id)
{  
        $id=$this->db->where('userid',$id);
$this->db->delete('login',$id);
header('Location:'.base_url().'admin_cont/adminpage');
}

public function resetpass()
{
$this->load->view('admin/resetpass');
}

public function resetpass_validate()
{
 $username=$this->input->post('username');
 $password=$this->input->post('password');

  //codeigniter-bcrypt for password secure
  $this->load->library('bcrypt');
  $hashpwd = $this->bcrypt->hash_password($password);
 
          if ($this->form_validation->run('reset_pass'))
          {
$this->load->model('loginmodel');
$username = $this->loginmodel->reset_pass($username);
if($username != null)
{
$data = array('password' => $hashpwd);

       $this->db->where('username',$username);
           $this->db->update('login',$data);

redirect("admin_cont/adminpage");
}
else
{
$this->load->view('admin/resetusernotexist');
}
 }
 else
 {
$this->load->view('admin/resetpass');
 }
}


// for allowing space in inputbox
public function alpha_space($str)
{   //alpha+space allow for space[a-z ] is valid but [a-z] not valid
   $this->form_validation->set_message('alpha_space', 'Only alphabetic and space are allowed');
return ( ! preg_match("/^([a-z ])+$/i", $str)) ? FALSE : TRUE;

//alpha+numeric+space+dash+underscore+comma allow
//return ( ! preg_match("/^([-a-z_,0-9 ])+$/i", $str)) ? FALSE : TRUE;
}
}