Code xây dựng modun quản lý customer CI

Code xây dựng modun quản lý customer CI

Code các phần hoản chỉnh về chức năng quản lý khách hàng đơn giản

Model

<?php

/**
 *--Project: lavarel
  --Design by: ATC PRO
  -- Date time: 24/4/2022 7:31:02 AM
 */
class Crm_customers_model extends CI_Model
{
    public function create_Crm_customers($data)
    {
        $this->db->insert('Crm_customers', $data);
        $id=$this->db->insert_id();
        if($id==0)
        {
            return base_url().'backend/Crm_customers';
        }
        return $this->db->get_where('Crm_customers', ['id' => $id])->row();
    }

    public function get_Crm_customers()
    {
        $this->db->select('Crm_customers'.'.*', FALSE);
        $this->db->from('Crm_customers');
        $query = $this->db->get();  
        return $query->result();
        
    }
    public function get_where_Crm_customers($id)
    {
        return $this->db->get_where('Crm_customers', ['id' => $id])->result();
    }

    public function get_Crm_customers_where( $id)
    {
        return $this->db->get_where('Crm_customers', ['id' => $id])->row();
    }

    public function update_Crm_customers( $data, $id)
    {
        return $this->db->where(['id' => $id])->update('Crm_customers', $data);
    }

    public function delete_Crm_customers( $id)
    {
       
        return $this->db->where(['id' => $id])->delete('Crm_customers');
    }

    
}

Controller

<?php
/**
 *--Project: lavarel
  --Design by: ATC PRO
  -- Date time: 24/4/2022 7:31:51 AM
 * This controller handles project management.
 */
class Crm_customers extends CI_Controller
{
    public $user_id;
    public $user_name;

    public function __construct(Type $foo = null)
    {

        parent::__construct();
        
        if (!$this->session->userdata('logged_in')) {
            $this->session->set_flashdata('flash_danger', 'Vui lòng đăng nhập để xem trang');
            header('location: ' .base_url().'backend/login');
        }
        else
        {
         $user_id = $this->session->userdata('user_id');
         $user_name = $this->session->userdata('user_name');
        
        }
        $this->load->model('crm_customers_model');
       
    }

    public function index()
    {
       
        $this->load->view('back_end_template/content', [
            'template' => 'backend/crm_customers/index','sl'=>1,
            'data' => $this->crm_customers_model->get_crm_customers()
             
        ]);
    }

    public function create()
    {
 $data = $this->crm_customers_model->get_where_crm_customers('0');
            $this->load->view('back_end_template/content', ['template' => 'backend/crm_customers/create','data' => $data  ]);
        
      }
    public function add()
    {
       
       
        
            $data = $this->crm_customers_model->create_crm_customers([
            'id' => $this->input->post('id'),
            'first_name' => $this->input->post('first_name'),
            'last_name' => $this->input->post('last_name'),
            'email' => $this->input->post('email'),
            'phone' => $this->input->post('phone'),
            'address' => $this->input->post('address'),
            'skype' => $this->input->post('skype'),
            'website' => $this->input->post('website'),
            'description' => $this->input->post('description'),
            'created_at' => $this->input->post('created_at'),
            'updated_at' => $this->input->post('updated_at'),
            'deleted_at' => $this->input->post('deleted_at'),

               
            ]);

            if ($data) {
                $this->session->set_flashdata('flash_success', 'Your crm_customers has been created');
                return header('location: ' .base_url().'backend/crm_customers/show/'.$data->id);
            }
        
    }

     public function search(){
        $data['template'] = 'backend/crm_customers/index';
        $keyword = $this->input->post('keyword');   
        $column = $this->input->post('column');   
        $list_col = array('id' => $keyword,'first_name' => $keyword,'last_name' => $keyword,'email' => $keyword,'phone' => $keyword,'address' => $keyword,'skype' => $keyword,'website' => $keyword,'description' => $keyword,'created_at' => $keyword,'updated_at' => $keyword,'deleted_at' => $keyword,); 
        if($column!="")
        {
          $columns="crm_customers.$column";
          $list_col =array($columns => $keyword);
         }
        $config['$keyword'] = $keyword;
        $config['$columnsearch'] = $column;  
        $config['base_url'] = site_url('backend/crm_customers/search/');
        $config['total_rows'] = $this->get_model->atc_get_count_search('crm_customers',$list_col);
        $data['sl'] = $config['total_rows'];
        $config['per_page'] = 10;  
        $config['uri_segment'] = 3;
        $config['cur_tag_open'] = '<a class="current_page" href="backend/crm_customers/">';
        $config['cur_tag_close'] = '</a>';
        $this->pagination->initialize($config);
        
        $data['data'] = $this->get_model->atc_get_search('crm_customers',$list_col,$config['per_page'], $this->uri->segment($config['uri_segment'])); 
        $data['link'] = $this->pagination->create_links();
        
    
        
        $this->load->view('back_end_template/content', $data);
        
    }
    public function show($id)
    {
        $data = $this->crm_customers_model->get_where_crm_customers($id);
        if (!$data) {
            $this->denied_message();

           return header('location: ' .base_url().'backend/crm_customers');
        }
        $this->load->view('back_end_template/content', [
            'template' => 'backend/crm_customers/show',
            'data' => $data 
         
        ]);
    }

    public function edit($id)
    {
        $data = $this->crm_customers_model->get_where_crm_customers($id);
        if (!$data) {
            $this->denied_message();
         	return	header('location: ' .base_url().'backend/crm_customers');
           
        }
     
     
        
            $this->load->view('back_end_template/content', ['template' => 'backend/crm_customers/edit', 'data' => $data ]);
       
    }

    public function update()
    {
        
            $data = [
            'id' => $this->input->post('id'),
            'first_name' => $this->input->post('first_name'),
            'last_name' => $this->input->post('last_name'),
            'email' => $this->input->post('email'),
            'phone' => $this->input->post('phone'),
            'address' => $this->input->post('address'),
            'skype' => $this->input->post('skype'),
            'website' => $this->input->post('website'),
            'description' => $this->input->post('description'),
            'created_at' => $this->input->post('created_at'),
            'updated_at' => $this->input->post('updated_at'),
            'deleted_at' => $this->input->post('deleted_at'),

              
            ];
         	$ID=$this->input->post('id');
            $url=$this->input->post('urlre');
            if ($this->crm_customers_model->update_crm_customers( $data,$ID)) {
                $this->session->set_flashdata('flash_success', 'Your crm_customers has been updated');
                if($url=="save")
               {
                   return header('location: ' .base_url().'backend/crm_customers/');
               }
                return header('location: ' .base_url().'backend/crm_customers/show/'.$ID);
                }
           
        
    }
     public function deleteall()
    {
    
         $con = $this->input->post('id_check');
         $num_array = count($con);

            if($num_array>0){
                     for($i=1;$i<=$num_array;$i++)
                     {
                     $id = $con[$i-1];
                     $this->crm_customers_model->delete_crm_customers($id);
                     }
                      $this->session->set_flashdata('flash_success', 'Tất cả đã xóa thành công !');
              }
               return  header('location: ' .base_url().'backend/crm_customers');
    
    }
    public function delete($id)
    {
       if(isset($_POST['id']))
       {
          $id=$_POST['id'];
        }
        $data= $this->crm_customers_model->get_where_crm_customers($id);
        if (!$data) {
            $this->denied_message();

          return  header('location: ' .base_url().'backend/crm_customers');
        }
        if ($this->crm_customers_model->delete_crm_customers($id)) {
            $this->session->set_flashdata('flash_success', 'Your crm_customers has been deleted');
        } else {
            $this->denied_message();
        }
         return   header('location: ' .base_url().'backend/crm_customers');

    }

    private function denied_message()
    {
        $this->session->set_flashdata('flash_danger', 'That crm_customers does not exist or you do not have permission');
    }
}

View

 <!-- Content Header  -->
<section class="content-header">
      <h1>
       <a href="/admin/crm_customers/index">
         <button type="button" class="btn btn-flat btn-success"><i class="fa fa-mail-reply-all"></i>&nbsp;Trở về</button>
       </a>
 crm_customers Index
        
      </h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
        <li><a href="#">crm_customers</a></li>
        <li class="active"> crm_customers Index</li>
      </ol>
</section>
 <!-- Main content -->
 <section class="content">
    
  <div class="row">
  @if(ViewBag.alert!=null) {
    <div class="alert [email protected] alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa [email protected]"></i> Thông báo!</h4>
  @ViewBag.alert
</div>
}
    <!--  column -->
     <div class="box">
        <div class="box-body">
            <div class="panel panel-primary">
                <div class="panel-heading">
                  <h3 class="panel-title"><i class="fa fa-plus"></i> crm_customers Index</h3>
                </div>
              <div class="box-body">
<form action='<?=base_url()?>backend/crm_customers/Add' method='post' id='form_crm_customers'>
<div class="box-body">
<input   id='urlre' value='' name='urlre'  type='hidden'  />    <div class="form-group">
    <label for="Input_id">id</label>
  <input  placeholder="id" class="form-control" id="id" name="id"   required='required' value='<?php if(isset($data[0]->id)) echo $data[0]->id;?>' type="text" >
  </div>
    <div class="form-group">
    <label for="Input_first_name">first_name</label>
  <input  placeholder="first_name" class="form-control" id="first_name" name="first_name"   required='required'   value='<?php if(isset($data[0]->first_name)) echo $data[0]->first_name;?>'  type="text" >
  </div>
    <div class="form-group">
    <label for="Input_last_name">last_name</label>
  <input  placeholder="last_name" class="form-control" id="last_name" name="last_name"   required='required'   value='<?php if(isset($data[0]->last_name)) echo $data[0]->last_name;?>'  type="text" >
  </div>
    <div class="form-group">
    <label for="Input_email">email</label>
  <input  placeholder="email" class="form-control" id="email" name="email"   required='required'   value='<?php if(isset($data[0]->email)) echo $data[0]->email;?>'  type="text" >
  </div>
    <div class="form-group">
    <label for="Input_phone">phone</label>
  <input  placeholder="phone" class="form-control" id="phone" name="phone"   required='required'   value='<?php if(isset($data[0]->phone)) echo $data[0]->phone;?>'  type="text" >
  </div>
    <div class="form-group">
    <label for="Input_address">address</label>
  <input  placeholder="address" class="form-control" id="address" name="address"   required='required'   value='<?php if(isset($data[0]->address)) echo $data[0]->address;?>'  type="text" >
  </div>
    <div class="form-group">
    <label for="Input_skype">skype</label>
  <input  placeholder="skype" class="form-control" id="skype" name="skype"   required='required'   value='<?php if(isset($data[0]->skype)) echo $data[0]->skype;?>'  type="text" >
  </div>
    <div class="form-group">
    <label for="Input_website">website</label>
  <input  placeholder="website" class="form-control" id="website" name="website"   required='required'   value='<?php if(isset($data[0]->website)) echo $data[0]->website;?>'  type="text" >
  </div>
    <div class="form-group">
    <label for="Input_description">description</label>
  <textarea  class="form-control" id="description" name="description" placeholder=" required='required' " ><?php if(isset($data[0]->description)) echo $data[0]->description;?></textarea>
<script type='text/javascript'>
            	    CKEDITOR.replace( 'description',{
            	    height : '250'
            		
            	});		
            </script>  </div>
    <div class="form-group">
    <label for="Input_created_at">created_at</label>
  <input  placeholder="created_at" class="form-control" id="created_at" name="created_at"   required='required'   value='<?php if(isset($data[0]->created_at)) echo $data[0]->created_at;?>'  type="text" >
  </div>
    <div class="form-group">
    <label for="Input_updated_at">updated_at</label>
  <input  placeholder="updated_at" class="form-control" id="updated_at" name="updated_at"   required='required'   value='<?php if(isset($data[0]->updated_at)) echo $data[0]->updated_at;?>'  type="text" >
  </div>
    <div class="form-group">
    <label for="Input_deleted_at">deleted_at</label>
  <input  placeholder="deleted_at" class="form-control" id="deleted_at" name="deleted_at"   required='required'   value='<?php if(isset($data[0]->deleted_at)) echo $data[0]->deleted_at;?>'  type="text" >
  </div>

<div class="box-footer"> <button type="submit" class="btn btn-primary">Save changes</button></div>
</form>
              </div>
            </div>
        </div>
      </div>
 </section>

 

nguyen tran

Leave a Reply

Your email address will not be published. Required fields are marked *