Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.94 KB | None | 0 0
  1.     function action_add_user($id = "new")
  2.     {
  3.         perm::instance()->user_assert_login();
  4.         $form = array();
  5.  
  6.         //Creates an array of the form fields
  7.         foreach ($_POST as $key => $value)
  8.         $form += array($key => '');
  9.  
  10.         $errors = $form;
  11.        
  12.        
  13.         //Hack because validate rule not working on array
  14.         //Check if a user is being submitted as a customer then see if he is assigned a group
  15.         $groups = true;
  16.         foreach ($_POST['role'] as $r){
  17. //            echo kohana::debug($r);
  18.             if ($r == 4){
  19.                 if (empty($_POST['groups'])){
  20.                     $groups = false;
  21.                 }
  22.             }
  23.         }
  24. //        exit;
  25.        
  26.         $user = ($id == 'new') ?  ORM::factory('user') :  ORM::factory('user', $id);
  27.         if($id!='new'){$user->remove_rules();}
  28.         $user->validate()->filter(true, 'trim');
  29.         $user->validate()->filter('first_name', 'ucfirst');
  30.         $user->validate()->filter('last_name', 'ucfirst');
  31. //        $user->validate()->rule('groups', 'not_empty');
  32. //        $user->validate()->callback('groups', 'controller_panelx::is_array_empty');
  33. //        $user->validate()->rule('eno', 'not_empty');
  34. //        $user->validate()->rule('eno', 'numeric');
  35. //        $user->validate()->rule('first_name', 'not_empty');
  36. //        $user->validate()->rule('last_name', 'not_empty');
  37. //        ADD USERNAME VALIDATION
  38.         if ($user->values($_POST)->check() and $groups )
  39.         {
  40.  
  41.             $user->save();
  42.  
  43.             //create array of avaliable roles
  44.             if (perm::instance()->logged_in("manager")){
  45.                 $all_roles = ORM::factory('role')->where('type' , '=' ,0)->find_all()->as_array();
  46.                
  47.                 //get all the roles and the roles to remove
  48.                 $remove_roles = array();
  49.                 $all = array();
  50.                 foreach ($all_roles as $r ){
  51.                     $all[] = $r->id;
  52.                     if (!in_array($r->id ,$_POST['role'])){
  53.                         $remove_roles[] = $r->id;
  54.                     }
  55.                 }
  56.                
  57.                 //find all the roles to add
  58.                 $add_roles = array_diff($all, $remove_roles);
  59.                
  60.                 //only add the roles we don't already have
  61.                 foreach ($add_roles as $r){
  62.                     if (!$user->has('roles',ORM::factory('role', $r))){
  63.                         $user->add('roles',ORM::factory('role', $r));
  64.                     }
  65.                 }
  66.                
  67.                 //remove all roles that we don't need.
  68.                 foreach ($remove_roles as $r){
  69.                     $user->remove('roles',ORM::factory('role', $r));
  70.                 }
  71.    
  72.                 //**THIS IS GROUP STUFF
  73.                 $all_groups = ORM::factory('group')->find_all()->as_array();
  74.                 //get all the groups and the groups to remove
  75.                 $remove_groups = array();
  76.                 $all2 = array();
  77.                 foreach ($all_groups as $r ){
  78.                     $all2[] = $r->id;
  79.                     if (!in_array($r->id ,$_POST['groups'])){
  80.                         $remove_groups[] = $r->id;
  81.                     }
  82.                 }
  83.                
  84.                 //find all the groups to add
  85.                 $add_groups = array_diff($all2, $remove_groups);
  86.                
  87.                 //only add the groups we don't already have
  88.                 foreach ($add_groups as $r){
  89.                     if (!$user->has('groups',ORM::factory('group', $r))){
  90.                         $user->add('groups',ORM::factory('group', $r));
  91.                     }
  92.                 }
  93.                
  94.                 //remove all groups that we don't need.
  95.                 foreach ($remove_groups as $r){
  96.                     $user->remove('groups',ORM::factory('group', $r));
  97.                 }
  98.            
  99.             }
  100.                
  101. //                echo kohana::debug($_POST['groups'], $remove_groups, $add_groups, $all2);
  102.             //ENDS HERE
  103.            
  104.  
  105.             $_SESSION['success'][] = "Changes saved you may edit them below if something is not correct.";
  106.             fun::redirect('panel/add_user/'.$user->id); //user->id contains last inserted id
  107.         }
  108.         else
  109.         {
  110.            
  111.             $_SESSION['error'] = $user->validate()->errors('user');
  112.            
  113.             if (!$groups){
  114.                 $_SESSION['error'][] = "User with customer role must belong to a group";
  115.             }
  116. //            $post->password = '';
  117. //            $post->password_confirm = '';
  118. //            print_r($user->validate()->errors('user'));
  119. //echo kohana::debug('fail'); exit;
  120.             $this->session->set('form' , arr::overwrite($form, $_POST));
  121.             $this->session->set('formError' , arr::overwrite($errors, $user->validate()->errors('user')));
  122.             fun::redirect('panel/add_user/' . $id);
  123.         }
  124.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement