public function store(AdminUserRequest $request)
 {
     $adminUser = AdminUser::create($request->all());
     $adminUser->password = Hash::make(Input::get('password'));
     //format date to YYYY-mm-DD before update into database
     FormatDateTime::formatDate4DB($adminUser, "birthday");
     //set more info before save data
     $this->setDefaultInfoBeforeSave($adminUser, true);
     if ($adminUser->save()) {
         return Redirect::route('admin.AdminUser.index')->with('message', new MessageBag(['save_complete']));
     } else {
         return Redirect::route('admin.AdminUser.create', $adminUser)->with('message', new MessageBag(['save_error']));
     }
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function update($id, AdminUserRequest $request)
 {
     $data = $request->all();
     $user = AdminUserQuery::create()->findPk($id);
     $user->fromArray($data);
     $user->save();
     Misc::setLocale($data['LanguageId']);
     $adminCredentials = AdminCredentialQuery::create()->find();
     $credentials_arr = $request->only('Credentials');
     $credentials_arr = $credentials_arr['Credentials'];
     foreach ($adminCredentials as $adminCredential) {
         $credential_id = $adminCredential->getId();
         $adminUserCredential = AdminUserCredentialQuery::create()->where('admin_user_id = ' . $id)->where('admin_credential_id = ' . $credential_id)->findOne();
         if (isset($credentials_arr[$credential_id])) {
             if (is_null($adminUserCredential)) {
                 $adminUserCredential = new AdminUserCredential();
                 $adminUserCredential->setAdminUserId($id);
                 $adminUserCredential->setAdminCredentialId($credential_id);
             }
             $adminUserCredential->setPermRead(0);
             $adminUserCredential->setPermWrite(0);
             $adminUserCredential->setPermExec(0);
             foreach ($credentials_arr[$credential_id] as $perm) {
                 if ($perm == 'read') {
                     $adminUserCredential->setPermRead(1);
                 } elseif ($perm == 'write') {
                     $adminUserCredential->setPermWrite(1);
                 } elseif ($perm == 'exec') {
                     $adminUserCredential->setPermExec(1);
                 }
             }
             $adminUserCredential->save();
         } else {
             if (!is_null($adminUserCredential)) {
                 $adminUserCredential->delete();
             }
         }
     }
     if ($id == \Auth::user()->getId()) {
         Misc::setCredentials($id);
     }
     flash()->success("UPDATED");
     session(['attribute' => \Lang::get('general.USER')]);
     return redirect($this->main_page);
 }