Skip to content

Instantly share code, notes, and snippets.

@PrimozRome
Created March 28, 2013 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PrimozRome/24e98a1822941f4fddd3 to your computer and use it in GitHub Desktop.
Save PrimozRome/24e98a1822941f4fddd3 to your computer and use it in GitHub Desktop.
public function action_update($id = null)
{
$errors = null;
$user = Model_User::find($id);
if(!$user)
{
Session::set_flash('error', 'User with does not exist');
Response::redirect('users');
}
else
{
if (Input::method() == 'POST')
{
try
{
// create user
$user->username = Input::post('email');
$user->email = Input::post('email');
$user->password = Input::post('password');
$user->profile->lastname = Input::post('lastname');
$user->profile->company = Input::post('company');
$user->profile->address1 = Input::post('address1');
$user->profile->address2 = Input::post('address2');
$user->profile->post = Input::post('post');
$user->profile->city = Input::post('city');
$user->profile->country_id = Input::post('country_id');
$user->profile->state_id = Input::post('state_id');
$user->profile->phone = Input::post('phone');
$user->profile->vatid = Input::post('vatid');
$user->profile->subscription = Input::post('subscription');
$user->profile->saop_code = Input::post('saop_code');
$permissions = \Model_Permission::find('all');
foreach($permissions as $permission):
echo $permission->id.'<br/>';
if(in_array($permission->id, Input::post('permissions', array())))
{
$user->permissions[$permission->id] = $permission;
}
else
{
unset($user->permissions[$permission->id]);
}
endforeach;
die();
$roles = \Model_Role::find('all');
foreach($roles as $role):
if(in_array($role->id, Input::post('roles')))
{
$user->roles[$role->id] = $role;
}
else
{
unset($user->roles[$role->id]);
}
endforeach;
if ($user->save())
{
Session::set_flash('success', 'User &raquo;' . $user->profile->get_full_name() . '&laquo; updated');
Model_Activity::create_activity($this->current_user->id, Inflector::singularize(Uri::segment(1)), $id, Uri::segment(2));
//Response::redirect('users');
}
else
{
Session::set_flash('error', 'There was error updating user.');
}
}
catch (Orm\ValidationFailed $e)
{
Session::set_flash('error', 'There was error updating user. Check input data.');
$errors = $e->get_fieldset();
if($errors)
{
$errors = $errors->error();
}
else
{
$errors['password'] = true;
}
}
}
$view = View::forge('users/form');
$view->errors = $errors;
$view->permissions = Model_Permission::get_permissions();
$view->user = $user;
$this->template->title = $view->title = 'Users &raquo; Update';
$this->template->content = $view;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment