Advertisement
europcsolutions

FuelPHP Create/Edit Users

Mar 1st, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.68 KB | None | 0 0
  1. User.php
  2. --------
  3.  
  4. <?php
  5.  
  6. class User extends Admin_Controller
  7. {
  8.  
  9.     public function __construct()
  10.     {
  11.         parent::__construct();
  12.     }
  13.  
  14.  
  15.     public function index()
  16.     {
  17.         // Fetch all users
  18.         $this->data['users'] = $this->user_m->get();
  19.         // Load view
  20.         $this->data['subview'] = 'admin/user/index';
  21.         $this->load->view( 'admin/_layout_main', $this->data );
  22.     }
  23.  
  24.  
  25.     public function edit( $id = null )
  26.     {
  27.         // Fetch a user or set a new one
  28.         if( $id )
  29.         {
  30.             $this->data['user'] = $this->user_m->get( $id );
  31.             count( $this->data['user'] ) || $this->data['errors'][] = 'User could not be found';
  32.         }
  33.         else
  34.         {
  35.             $this->data['user'] = $this->user_m->get_new();
  36.         }
  37.  
  38.         // Set up the form
  39.         $rules = $this->user_m->rules_admin;
  40.         $id || $rules['password']['rules'] .= '|required';
  41.         $this->form_validation->set_rules( $rules );
  42.  
  43.         // Process the form
  44.         if( $this->form_validation->run() == TRUE )
  45.         {
  46.             $data = $this->user_m->array_from_post( array( 'name', 'email', 'password' ) );
  47.             $data['password'] = $this->user_m->hash( $data['password'] );
  48.             $this->user_m->save( $data, $id );
  49.             redirect( 'admin/user' );
  50.         }
  51.  
  52.         // Load the view
  53.         $this->data['subview'] = 'admin/user/edit';
  54.         $this->load->view( 'admin/_layout_main', $this->data );
  55.     }
  56.  
  57.  
  58.     public function delete( $id )
  59.     {
  60.         $this->user_m->delete( $id );
  61.         redirect( 'admin/user' );
  62.     }
  63.  
  64.  
  65.     public function login()
  66.     {
  67.         // Redirect the user if he's already logged in
  68.         $this->user_m->loggedin() == FALSE || redirect( 'admin/dashboard' );
  69.  
  70.         // Set up form
  71.         $rules = $this->user_m->rules;
  72.         $this->form_validation->set_rules( $rules );
  73.  
  74.         // Process form
  75.         if( $this->form_validation->run() )
  76.         {
  77.             // Login and redirect
  78.             if( $this->user_m->login() == TRUE )
  79.             {
  80.                 redirect( 'admin/dashboard' );
  81.             }
  82.             else
  83.             {
  84.                 $this->session->set_flashdata( 'error', 'That email/password combination does not exist' );
  85.                 redirect( 'admin/user/login', 'refresh' );
  86.             }
  87.         }
  88.  
  89.         // Load view
  90.         $this->data['subview'] = 'admin/user/login';
  91.         $this->load->view( 'admin/_layout_modal', $this->data );
  92.     }
  93.  
  94.  
  95.     public function logout()
  96.     {
  97.         $this->user_m->logout();
  98.         redirect( 'admin/user/login' );
  99.     }
  100.  
  101.  
  102.     public function _unique_email( $str )
  103.     {
  104.         // Do NOT validate if email aready exists unless it belongs to current user
  105.         $id = $this->uri->segment( 4 );
  106.         $this->db->where( 'email', $this->input->post( 'email' ) );
  107.         // set where condition if there is an id
  108.         !$id || $this->db->where( 'id !=', $id );
  109.         $user = $this->user_m->get();
  110.  
  111.         if( count( $user ) )
  112.         {
  113.             $this->form_validation->set_message( '_unique_email', '%s should be unique' );
  114.             return FALSE;
  115.         }
  116.  
  117.         return TRUE;
  118.     }
  119.  
  120. }
  121.  
  122.  
  123.  
  124. user_m.php
  125. ----------
  126.  
  127. <?php
  128.  
  129. class User_M extends MY_Model
  130. {
  131.  
  132.     protected $_table_name = 'users';
  133.     protected $_order_by = 'name';
  134.  
  135.     public $rules = array(
  136.         'email' => array(
  137.             'field' => 'email',
  138.             'label' => 'Email',
  139.             'rules' => 'trim|required|valid_email|xss_clean'
  140.         ),
  141.         'password' => array(
  142.             'field' => 'password',
  143.             'lable' => 'Password',
  144.             'rules' => 'trim|required'
  145.         )
  146.     );
  147.  
  148.     public $rules_admin = array(
  149.         'name' => array(
  150.             'field' => 'name',
  151.             'label' => 'Name',
  152.             'rules' => 'trim|required|xss_clean'
  153.         ),
  154.         'email' => array(
  155.             'field' => 'email',
  156.             'label' => 'Email',
  157.             'rules' => 'trim|required|valid_email|callback__unique_email|xss_clean'
  158.         ),
  159.         'password' => array(
  160.             'field' => 'password',
  161.             'lable' => 'Password',
  162.             'rules' => 'trim|matches[password_confirm]'
  163.         ),
  164.         'password_confirm' => array(
  165.             'field' => 'password_confirm',
  166.             'lable' => 'Confirm Password',
  167.             'rules' => 'trim|matches[password]'
  168.         )
  169.     );
  170.  
  171.  
  172.     function __construct()
  173.     {
  174.         parent::__construct();
  175.     }
  176.  
  177.  
  178.     public function login()
  179.     {
  180.         $user = $this->get_by( array(
  181.             'email' => $this->input->post( 'email' ),
  182.             'password' => $this->hash( $this->input->post( 'password' ) )
  183.         ), TRUE );
  184.  
  185.         if( count( $user ) )
  186.         {
  187.             $data = array(
  188.                 'name' => $user->name,
  189.                 'email' => $user->email,
  190.                 'id' => $user->id,
  191.                 'loggedin' => TRUE
  192.             );
  193.  
  194.             $this->session->set_userdata( $data );
  195.         }
  196.     }
  197.  
  198.  
  199.     public function logout()
  200.     {
  201.         $this->session->sess_destroy();
  202.     }
  203.  
  204.  
  205.     public function loggedin()
  206.     {
  207.         return (bool) $this->session->userdata( 'loggedin' );
  208.     }
  209.  
  210.  
  211.     public function get_new()
  212.     {
  213.         $user = new stdClass();
  214.         $user->name = '';
  215.         $user->email = '';
  216.         $user->password = '';
  217.         return $user;
  218.     }
  219.  
  220.  
  221.     public function hash( $string )
  222.     {
  223.         return hash( 'sha512', $string . config_item( 'encryption_key' ) );
  224.     }
  225.  
  226. }
  227.  
  228.  
  229.  
  230. MY_Model.php
  231. ------------
  232.  
  233. <?php
  234.  
  235. class MY_Model extends CI_Model
  236. {
  237.  
  238.     protected $_table_name = '';
  239.     protected $_primary_key = 'id';
  240.     protected $_primary_filter = 'intval';
  241.     protected $_order_by = '';
  242.     public $rules = array();
  243.     protected $_timestamps = FALSE;
  244.  
  245.  
  246.     function __construct()
  247.     {
  248.         parent::__construct();
  249.     }
  250.  
  251.  
  252.     public function array_from_post( $fields )
  253.     {
  254.         $data = array();
  255.         foreach( $fields as $field )
  256.         {
  257.             $data[$field] = $this->input->post( $field );
  258.         }
  259.         return $data;
  260.     }
  261.  
  262.  
  263.     public function get( $id = null, $single = FALSE )
  264.     {
  265.         if( $id != null )
  266.         {
  267.             $filter = $this->_primary_filter;
  268.             $id = $filter( $id );
  269.             $this->db->where( $this->_primary_key, $id );
  270.             $method = 'row';
  271.         }
  272.         elseif( $single == TRUE )
  273.         {
  274.             $method = 'row';
  275.         }
  276.         else
  277.         {
  278.             $method = 'result';
  279.         }
  280.  
  281.         if( ! count( $this->db->ar_orderby ) )
  282.         {
  283.             $this->db->order_by( $this->_order_by );
  284.         }
  285.  
  286.         return $this->db->get( $this->_table_name )->$method();
  287.     }
  288.  
  289.  
  290.     public function get_by( $where, $single = FALSE )
  291.     {
  292.         $this->db->where( $where );
  293.         return $this->get( null, $single );
  294.     }
  295.  
  296.  
  297.     public function save( $data, $id = null )
  298.     {
  299.         // Set timestamps
  300.         if( $this->_timestamps == TRUE )
  301.         {
  302.             $now = date( 'Y-m-d H:i:s' );
  303.             $id || $data['created'] = $now;
  304.             $data['modified'] = $now;
  305.         }
  306.  
  307.         // Insert
  308.         if( $id === null )
  309.         {
  310.             // set it to null if it is set
  311.             ! isset( $data[$this->_primary_key] ) || $data[$this->_primary_key] = null;
  312.             $this->db->set( $data );
  313.             $this->db->insert( $this->_table_name );
  314.             $id = $this->db->insert_id();
  315.         }
  316.         // Update
  317.         else
  318.         {
  319.             $filter = $this->_primary_filter;
  320.             $id = $filter( $id );
  321.             $this->db->set( $data );
  322.             $this->db->where( $this->_primary_key, $id );
  323.             $this->db->update( $this->_table_name );
  324.         }
  325.  
  326.         return $id;
  327.     }
  328.  
  329.  
  330.     public function delete( $id )
  331.     {
  332.         $filter = $this->_primary_filter;
  333.         $id = $filter( $id );
  334.  
  335.         if( ! $id )
  336.         {
  337.             return FALSE;
  338.         }
  339.         else
  340.         {
  341.             $this->db->where( $this->_primary_key, $id );
  342.             $this->db->limit( 1 );
  343.             $this->db->delete( $this->_table_name );
  344.         }
  345.     }
  346.  
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement