<?php
class UserController extends \BaseController
{
public function index()
{
$users = User::all();
return View::make('users.index')->with('users', $users);
}
public function create()
{
return View::make('users.create');
}
public function store()
{
$rules = array(
'first' => 'required|min:3',
'last' => 'required|min:3',
'email' => 'required|email',
'password' => 'required|alphaNum|min:3'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails())
{
return Redirect::route('create')
->withErrors($validator)
->withInput(Input::except('password'));
} else
{
$user = User::firstOrCreate(
array('first' => Input::get('first'),
'last' => Input::get('last'),
'email' => Input::get('email'),
'level' => 'user',
'password' => Hash::make(\Input::get('password'))));
if($user->save())
{
$user_id = $user->id;
Mail::queue('users.welcome', array('name'=> $user->first), function($message) use ($user)
{
$message->to($user->email, $user->first . ' '. $user->last)->subject('Welcome To Mentconnect Community');
});
return Redirect::route('group', array('user_id' => $user_id));
}else
{
return \Redirect::route('create')->with('errors', 'Something terrible happened');
}
}
}
/**
* Display the two buttons for choosing between mentor and mentee
* @return View response
*/
public function chooseGroup($user_id)
{
return View::make('users.group')->with('user_id', $user_id);
}
public function completeRegistration()
{
$user_id = \Request::segment(3);
$group_id = \Request::segment(4);
$user = User::find($user_id);
return View::make('users.complete')->with('user', $user)->with('group_id', $group_id);
}
public function saveProfile()
{
$group_id = \Input::get('group_id');
$user_id = \Input::get('user_id');
$user = \User::find($user_id);
$skills = array();
if (Input::get('java') === 'Java')
{
array_push($skills, 'Java');
}
if (Input::get('php') === 'PHP')
{
array_push($skills, 'PHP');
}
if (Input::get('python') === 'Python')
{
array_push($skills, 'Python');
}
if (Input::get('cplus') === 'Cplus')
{
array_push($skills, 'C++');
}
if (Input::get('objc') === 'Objc')
{
array_push($skills, 'Objective C');
}
if (Input::get('htmljscss') === 'Html')
{
array_push($skills, 'HTML JS CSS');
}
if (Input::get('android') === 'Android')
{
array_push($skills, 'Android');
}
if (Input::get('ux') === 'UXDesign')
{
array_push($skills, 'Java');
}
if (Input::get('webapps') === 'Webapps')
{
array_push($skills, 'Web Apps');
}
if (Input::get('ruby') === 'Ruby')
{
array_push($skills, 'Ruby on Rails');
}
if (Input::get('csharp') === 'CSharp')
{
array_push($skills, 'C#');
}
if (Input::get('people') === 'People')
{
array_push($skills, 'People Skills');
}
$skills = serialize($skills);
$user->first = \Input::get('first');
$user->last = \Input::get('last');
$user->email = \Input::get('email');
$user->location = \Input::get('location');
$user->bio = \Input::get('bio');
$user->skills = $skills;
if (\Input::hasFile('photo'))
{
$photo = \Input::file('photo');
$filename = uniqid('profile_').'.'.$photo->getClientOriginalExtension();
$photo->move(public_path('images/users'), $filename);
$user->photo = 'images/users/'.$filename;
}
$user_role = UserXrefRole::find($user_id);
if(count($user_role))
{
$user_role->user_id = $user_id;
$user_role->role_id = $group_id;
}else
{
$user_role = UserXrefRole::create(array(
'user_id' => $user_id,
'role_id' => $group_id
));
}
$user_role->save();
if ($user->save())
{
return Redirect::route('user');
}else
{
\Redirect::back('complete', array($user_id, $group_id ));
}
}
public function showProfile()
{
$user = Auth::user();
return View::make('users.profile')->with('user', $user);
}
public function showUser($id)
{
$user = User::find($id);
return View::make('users.profile')->with('user', $user);
}
public function edit($id)
{
if( Auth::check() && Auth::id() != $id)
{
return Redirect::to('/');
}else
{
$user = User::find($id);
$group = UserXrefRole::where('user_id', '=', $id)->get();
$group_id = (int)$group[0]->role_id;
return View::make('users.complete')->with('user', $user)->with('group_id', $group_id);
}
}
public function update($id)
{
if( Auth::check() && Auth::user()->id != $id)
{
return Redirect::to('/');
}else
{
$user = User::find($id);
//update the user here
\Session::flash('message', 'You have successfully updated your profile.');
return Redirect::route('user');
}
}
public function mentors()
{
$mentors = \DB::select(\DB::raw('SELECT * FROM
users, roles, user_xref_role
WHERE users.id = user_xref_role.user_id
AND roles.id = user_xref_role.role_id
AND roles.id = 1'));
return View::make('users.mentors')->with('mentors', $mentors);
}
public function mentees()
{
$mentees = \DB::select(\DB::raw('SELECT * FROM
users, roles, user_xref_role
WHERE users.id = user_xref_role.user_id
AND roles.id = user_xref_role.role_id
AND roles.id = 2'));
return View::make('users.mentees')->with('mentees', $mentees);
}
}