/**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $group = new \CALwebtool\Group();
     $group->name = "College of Arts and Letters";
     $group->description = "Default Team for the CAL Alumni Awards Board";
     $group->save();
     $group->makeAdmin(User::find(1));
 }
 public function register(Request $request)
 {
     if ($request->has('user')) {
         $user_id = $request->input('user');
     } else {
         flash()->overlay("The user account you tried to activate no longer exists", "Missing User ID");
         return view('users.error');
     }
     if ($request->has('token')) {
         $token = $request->input('token');
     } else {
         flash()->overlay('You cannot activate a user account without a valid registration token.  Please contact your Team Administrator and ask them to set a password for you.', 'Missing Registration Token');
         return view('users.error');
     }
     try {
         $user = User::findOrFail($user_id);
         if ($user->active) {
             flash()->overlay('The user account has already been activated.  If you forgot your password, you can reset it from the login screen', 'User Already Active');
             return redirect(action("HomeController@index"));
         }
         if ($user->register_token !== $token) {
             flash()->overlay('The registration token is invalid.  Please contact your Team Administrator and ask them to set a password for you.', 'Invalid Registration Token');
             return view('users.error');
         }
     } catch (\Exception $e) {
         flash()->overlay("The user account you tried to activate no longer exists", "Invalid User ID");
         return view('users.error');
     }
     return view('users.register', compact('user', 'token'));
 }
 public function update(Request $request, Group $group)
 {
     $this->validate($request, ["action" => "required|in:updateName,updateDescription,addMember,delMember,modMember,promoteAdmin,demoteAdmin", "name" => "string|max:255|requiredIf:action,updateName", "description" => "max:1000|requiredIf:action,updateDescription", "user_id" => 'numeric']);
     if ($request->has('creator')) {
         $creator = true;
     } else {
         $creator = false;
     }
     if ($request->has('moderator')) {
         $moderator = true;
     } else {
         $moderator = false;
     }
     if ($request->has('adjudicator')) {
         $adjudicator = true;
     } else {
         $adjudicator = false;
     }
     if ($request->has('administrator')) {
         $administrator = true;
         $adjudicator = true;
         $moderator = true;
         $creator = true;
     } else {
         $administrator = false;
     }
     if ($request->input("action") == "updateName") {
         $group->name = $request->input("name");
         $group->save();
         return redirect()->back();
     } elseif ($request->input("action") == "updateDescription") {
         $group->description = $request->input("description");
         $group->save();
         return redirect()->back();
     } elseif ($request->input("action") == "addMember") {
         try {
             $user = User::findOrFail($request->input("user_id"));
         } catch (\Exception $e) {
             flash()->overlay("The user couldn't be found, the account may have been deleted recently", "User Not Found");
             return redirect()->back();
         }
         if ($group->users()->get()->contains($user)) {
             flash()->overlay("The user already exists in this group", "User Exists");
             return redirect()->back();
         } else {
             if ($administrator) {
                 $group->makeAdmin($user);
             } else {
                 $group->addUser($user, $creator, $moderator, $adjudicator);
                 $group->modifyPermissions($user, $creator, $moderator, $adjudicator);
             }
             flash()->overlay($user->name . " (" . $user->email . ") is now a member of this team.", "Team Member Added");
             return redirect()->back();
         }
     } elseif ($request->input("action") == "delMember") {
         $user = User::find($request->input("user_id"));
         if ($group->users()->get()->contains($user)) {
             //$group->delUser($user);
             $group->users()->detach([$user->id]);
             flash()->overlay("The user was removed from the team", "User Removed");
             return redirect()->back();
         } else {
             flash()->overlay("The user you requested to delete, does not exist in the team, the user may already have been deleted", "User Deleted");
             return redirect()->back();
         }
     } elseif ($request->input("action") == "modMember") {
         try {
             $user = User::findOrFail($request->input("user_id"));
         } catch (\Exception $e) {
             flash()->overlay("The user couldn't be found or no longer exists<br>User ID was " . $request->input('user_id'), "User Not Found");
             return redirect()->back();
         }
         //dd($request->input());
         if ($administrator) {
             $group->makeAdmin($user);
         } else {
             if ($group->isAdmin($user->id)) {
                 $group->removeAdmin($user);
             }
             $group->modifyPermissions($user, $creator, $moderator, $adjudicator);
         }
         flash()->overlay($user->name . " (" . $user->email . ") now has the permissions<br><br>Administrator: " . ($administrator ? "True" : "False") . "<br>Moderator: " . ($moderator ? "True" : "False") . "<br> Judge: " . ($adjudicator ? "True" : "False"), "Permissions Updated");
         return redirect()->back();
     } elseif ($request->input("action") == "promoteAdmin") {
         flash()->overlay("Do not use this", "Do not use!");
         return redirect()->back();
     }
 }
 /**
  * Create a new user instance after a valid registration. OVVERRIDES LARAVEL FRAMEWORK AUTH
  *
  * @override
  * @param  array  $data
  * @return User
  */
 protected function create(array $data)
 {
     return User::create(['name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), 'active' => false]);
 }
 public function store(Request $request)
 {
     $this->validate($request, ['name' => 'required|unique:formdefinitions|max:100', 'description' => 'required', 'group' => 'required|integer', 'definition' => 'required|array', 'start_date' => 'required|date_format:m#d#Y', 'end_date' => 'required|date_format:m#d#Y', 'scores_date' => 'required|date_format:m#d#Y', 'sub_accept_action' => 'required|in:default,custom_message,custom_redir', 'sub_accept_redir' => 'required_if:sub_accept_action,custom_redir|url', 'sub_accept_content' => 'required_if:sub_accept_action,custom_message|string', 'use_custom_css' => 'required|in:true,false', 'custom_css_url' => 'required_if:use_custom_css,true', 'judges' => 'required|array']);
     try {
         $group = Group::findOrFail($request->input('group'));
     } catch (\Exception $e) {
         flash()->overlay("The team cannot be found, it may have been deleted." . "Team Not Found");
     }
     if (Auth::user()->cannot('create-form', $group)) {
         return response()->json(["Not authorized." => ["You do not have permission to create forms in this team"]], 403);
     }
     if (!(Carbon::createFromFormat("m#d#y", $request->input('start_date')) < Carbon::createFromFormat("m#d#y", $request->input('end_date'))) || !(Carbon::createFromFormat("m#d#y", $request->input('end_date')) < Carbon::createFromFormat("m#d#y", $request->input('scores_date')))) {
         return response()->json(["error" => true, "The dates you provided are not valid"]);
     }
     if ($request->input('sub_accept_action') == 'default') {
         $sub_accept_content = '';
     } elseif ($request->input('sub_accept_action') == 'custom_message') {
         $sub_accept_content = $request->input('sub_accept_content');
     } elseif ($request->input('sub_accept_action') == 'custom_redir') {
         $sub_accept_content = $request->input('sub_accept_redir');
     } else {
         $sub_accept_content = '';
     }
     if ($request->input('use_custom_css') == 'true') {
         $use_custom_css = true;
         $custom_css_url = $request->input("custom_css_url");
     } else {
         $use_custom_css = false;
         $custom_css_url = "";
     }
     $fieldErrors = new Collection();
     $judges = new Collection();
     try {
         foreach ($request->input('judges') as $judge) {
             try {
                 $judge = User::findOrFail($judge);
                 $judges->push($judge);
             } catch (\Exception $e) {
                 //$fieldErrors->push(["Judges"=>"Judge with ID of $judge not found!"]);
                 return response()->json(["Problem with Judges: " => ["The user with ID " . $judge . " cannot be found."]], 422);
             }
         }
     } catch (\Exception $e) {
         return response()->json(["Problem with Judges: " => ["There is a problem with one or more judges selected: " . $e->getMessage()]], 422);
     }
     try {
         $formDef = new FormDefinition(["name" => $request->input('name'), "description" => $request->input('description'), 'group_id' => $request->input('group'), 'user_id' => Auth::user()->id, 'submissions_start' => Carbon::createFromFormat("m#d#y", $request->input('start_date'))->setTime(0, 0, 0), 'submissions_end' => Carbon::createFromFormat("m#d#y", $request->input('end_date'))->setTime(0, 0, 0), 'scores_due' => Carbon::createFromFormat("m#d#y", $request->input('scores_date'))->setTime(0, 0, 0), 'notify_completed_sent' => false, 'status' => 'Scheduled', 'sub_accept_action' => $request->input('sub_accept_action'), 'sub_accept_content' => $sub_accept_content, 'use_custom_css' => $use_custom_css, 'custom_css_url' => $custom_css_url]);
         $formDef->save();
     } catch (\Exception $e) {
         return response()->json(['Error Creating Form' => ["Error creating FormDefinition", $e->getMessage()]], 500);
     }
     foreach ($request->input('definition') as $fieldArray) {
         $fieldDef = collect($fieldArray);
         $type = $fieldDef->get("type");
         if ($type == "Text") {
             $validator = Validator::make($fieldArray, ['id' => 'required|alpha_dash', 'name' => 'required', 'required' => 'required|boolean', 'text_type' => 'required|in:any,num,alpha,email,phone,date,time,multiline']);
             if ($validator->fails()) {
                 $fieldErrors->push($validator->errors());
             } else {
                 $field_options = new Collection();
                 $field_options->put('required', $fieldDef->get('required'));
                 $field_options->put('text_type', $fieldDef->get('text_type'));
                 $field = new Field(['form_definition_id' => $formDef->id, 'type' => $fieldDef->get('type'), 'field_id' => $fieldDef->get('id'), 'name' => $fieldDef->get('name'), 'order' => 0, 'options' => $field_options->toJson()]);
                 $field->save();
             }
         } else {
             if ($type == "Checkbox") {
                 $validator = Validator::make($fieldArray, ['id' => 'required|alpha_dash', 'name' => 'required', 'required' => 'required', 'value_checked' => 'required', 'value_unchecked' => 'required']);
                 if ($validator->fails()) {
                     $fieldErrors->push($validator->errors());
                 } else {
                     $field_options = new Collection();
                     $field_options->put('required', $fieldDef->get('required'));
                     $field_options->put('value_unchecked', $fieldDef->get('value_unchecked'));
                     $field_options->put('value_checked', $fieldDef->get('value_checked'));
                     $field = new Field(['form_definition_id' => $formDef->id, 'type' => $fieldDef->get('type'), 'field_id' => $fieldDef->get('id'), 'name' => $fieldDef->get('name'), 'order' => 0, 'options' => $field_options->toJson()]);
                     $field->save();
                 }
             } else {
                 if ($type == "Select") {
                     $validator = Validator::make($fieldArray, ['id' => 'required|alpha_dash', 'name' => 'required', 'required' => 'required', 'multipleselect' => 'required|boolean', 'options' => 'required|array', 'options.*.label' => 'required', 'options.*.value' => 'required']);
                     if ($validator->fails()) {
                         $fieldErrors->push($validator->errors());
                     } else {
                         $field_options = new Collection();
                         $field_options->put('required', $fieldDef->get('required'));
                         $field_options->put('multipleselect', $fieldDef->get('multipleselect'));
                         $field_options->put('options', $fieldDef->get('options'));
                         $field = new Field(['form_definition_id' => $formDef->id, 'type' => $fieldDef->get('type'), 'field_id' => $fieldDef->get('id'), 'name' => $fieldDef->get('name'), 'order' => 0, 'options' => $field_options->toJson()]);
                         $field->save();
                     }
                 } else {
                     if ($type == "RadioGroup") {
                         $validator = Validator::make($fieldArray, ['id' => 'required|alpha_dash', 'name' => 'required', 'required' => 'required', 'options' => 'required|array', 'options.*.label' => 'required', 'options.*.value' => 'required']);
                         if ($validator->fails()) {
                             $fieldErrors->push($validator->errors());
                         } else {
                             $field_options = new Collection();
                             $field_options->put('required', $fieldDef->get('required'));
                             $field_options->put('options', $fieldDef->get('options'));
                             $field = new Field(['form_definition_id' => $formDef->id, 'type' => $fieldDef->get('type'), 'field_id' => $fieldDef->get('id'), 'name' => $fieldDef->get('name'), 'order' => 0, 'options' => $field_options->toJson()]);
                             $field->save();
                         }
                     } else {
                         if ($type == "Address") {
                             $validator = Validator::make($fieldArray, ['id' => 'required|alpha_dash', 'name' => 'required', 'required' => 'required']);
                             if ($validator->fails()) {
                                 $fieldErrors->push($validator->errors());
                             } else {
                                 $field_options = new Collection();
                                 $field_options->put('required', $fieldDef->get('required'));
                                 //$field_options->put('options',$fieldDef->get('options'));
                                 $field = new Field(['form_definition_id' => $formDef->id, 'type' => $fieldDef->get('type'), 'field_id' => $fieldDef->get('id'), 'name' => $fieldDef->get('name'), 'order' => 0, 'options' => $field_options->toJson()]);
                                 $field->save();
                             }
                         } else {
                             if ($type == "File") {
                                 $validator = Validator::make($fieldArray, ['id' => 'required|alpha_dash', 'name' => 'required', 'required' => 'required']);
                                 if ($validator->fails()) {
                                     $fieldErrors->push($validator->errors());
                                 } else {
                                     $field_options = new Collection();
                                     $field_options->put('required', $fieldDef->get('required'));
                                     $field_options->put('types', $fieldDef->get('types'));
                                     $field = new Field(['form_definition_id' => $formDef->id, 'type' => $fieldDef->get('type'), 'field_id' => $fieldDef->get('id'), 'name' => $fieldDef->get('name'), 'order' => 0, 'options' => $field_options->toJson()]);
                                     $field->save();
                                 }
                             } else {
                                 $fieldErrors->push(["Unknown field type in submission"]);
                             }
                         }
                     }
                 }
             }
         }
     }
     try {
         foreach ($judges as $judge) {
             $formDef->judges()->save($judge);
         }
     } catch (\Exception $e) {
         $formDef->forceDelete();
         return response()->json(['Error Creating Form' => ["Failed to add a judge", $e->getMessage()]], 500);
     }
     if ($fieldErrors->count() == 0) {
         return response()->json([$formDef->id], 200);
     } else {
         //$formDef->forceDelete();
         $errorBag = new Collection();
         foreach ($fieldErrors as $fieldError) {
             foreach ($fieldError->messages() as $error) {
                 $errorBag->push($error);
             }
         }
         return response()->json($errorBag, 422);
     }
     //return response()->json(true);
 }