Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function action_add_user($id = "new")
- {
- perm::instance()->user_assert_login();
- $form = array();
- //Creates an array of the form fields
- foreach ($_POST as $key => $value)
- $form += array($key => '');
- $errors = $form;
- //Hack because validate rule not working on array
- //Check if a user is being submitted as a customer then see if he is assigned a group
- $groups = true;
- foreach ($_POST['role'] as $r){
- // echo kohana::debug($r);
- if ($r == 4){
- if (empty($_POST['groups'])){
- $groups = false;
- }
- }
- }
- // exit;
- $user = ($id == 'new') ? ORM::factory('user') : ORM::factory('user', $id);
- if($id!='new'){$user->remove_rules();}
- $user->validate()->filter(true, 'trim');
- $user->validate()->filter('first_name', 'ucfirst');
- $user->validate()->filter('last_name', 'ucfirst');
- // $user->validate()->rule('groups', 'not_empty');
- // $user->validate()->callback('groups', 'controller_panelx::is_array_empty');
- // $user->validate()->rule('eno', 'not_empty');
- // $user->validate()->rule('eno', 'numeric');
- // $user->validate()->rule('first_name', 'not_empty');
- // $user->validate()->rule('last_name', 'not_empty');
- // ADD USERNAME VALIDATION
- if ($user->values($_POST)->check() and $groups )
- {
- $user->save();
- //create array of avaliable roles
- if (perm::instance()->logged_in("manager")){
- $all_roles = ORM::factory('role')->where('type' , '=' ,0)->find_all()->as_array();
- //get all the roles and the roles to remove
- $remove_roles = array();
- $all = array();
- foreach ($all_roles as $r ){
- $all[] = $r->id;
- if (!in_array($r->id ,$_POST['role'])){
- $remove_roles[] = $r->id;
- }
- }
- //find all the roles to add
- $add_roles = array_diff($all, $remove_roles);
- //only add the roles we don't already have
- foreach ($add_roles as $r){
- if (!$user->has('roles',ORM::factory('role', $r))){
- $user->add('roles',ORM::factory('role', $r));
- }
- }
- //remove all roles that we don't need.
- foreach ($remove_roles as $r){
- $user->remove('roles',ORM::factory('role', $r));
- }
- //**THIS IS GROUP STUFF
- $all_groups = ORM::factory('group')->find_all()->as_array();
- //get all the groups and the groups to remove
- $remove_groups = array();
- $all2 = array();
- foreach ($all_groups as $r ){
- $all2[] = $r->id;
- if (!in_array($r->id ,$_POST['groups'])){
- $remove_groups[] = $r->id;
- }
- }
- //find all the groups to add
- $add_groups = array_diff($all2, $remove_groups);
- //only add the groups we don't already have
- foreach ($add_groups as $r){
- if (!$user->has('groups',ORM::factory('group', $r))){
- $user->add('groups',ORM::factory('group', $r));
- }
- }
- //remove all groups that we don't need.
- foreach ($remove_groups as $r){
- $user->remove('groups',ORM::factory('group', $r));
- }
- }
- // echo kohana::debug($_POST['groups'], $remove_groups, $add_groups, $all2);
- //ENDS HERE
- $_SESSION['success'][] = "Changes saved you may edit them below if something is not correct.";
- fun::redirect('panel/add_user/'.$user->id); //user->id contains last inserted id
- }
- else
- {
- $_SESSION['error'] = $user->validate()->errors('user');
- if (!$groups){
- $_SESSION['error'][] = "User with customer role must belong to a group";
- }
- // $post->password = '';
- // $post->password_confirm = '';
- // print_r($user->validate()->errors('user'));
- //echo kohana::debug('fail'); exit;
- $this->session->set('form' , arr::overwrite($form, $_POST));
- $this->session->set('formError' , arr::overwrite($errors, $user->validate()->errors('user')));
- fun::redirect('panel/add_user/' . $id);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement