public function add() { if ($_POST) { $post = new Validation($_POST); $post->add_rules('title', 'required'); $post->add_rules('seoURL', 'required'); if ($post->validate()) { $page = ORM::factory('page'); foreach ($post as $key => $val) { // Set user data $page->{$key} = $val; } if ($page->save()) { $this->index(); $this->__throw_success("Page \"" . $page->title . "\" has been created."); log::activity($this->user->username . " has created a new page " . $page->title, $this->user->username . " (" . $this->user->email . ") has created a new page."); } else { $this->index(); $this->__throw_error("Sorry, there has been an error, please try again."); } } else { $this->index(); $this->__throw_error("Please make sure that both title and SEO URL are filled out correctly"); } } else { $this->index(); } }
private function __user_form($form_fields = null) { $username = ""; $email = ""; if (!$form_fields) { $form_fields = array('username' => '', 'email' => '', 'password' => '', 'role' => '', 'type' => 'new'); } if (isset($form_fields['id'])) { $id = $form_fields['id']; } // copy the form as errors, so the errors will be stored with keys corresponding to the form field names $form_type = $form_fields['type']; if ($_POST) { $errors = $form_fields; $post = new Validation($_POST); // Add some filters $post->pre_filter('trim', TRUE); $post->add_rules('username', 'required'); $post->add_rules('email', 'required'); if ($form_fields['type'] == 'new') { $post->add_rules('password', 'required'); } $user = ORM::factory('user'); if ($post->validate()) { $array = array(); $array['email'] = $post->email; $array['username'] = $post->username; $array['password'] = $post->password; if ($post->password == "") { unset($array['password']); } $role = $post->role; if ($form_fields['type'] == 'edit') { // Create new user $user = ORM::factory('user', $form_fields['id']); } else { $user = ORM::factory('user'); } if (!$user->username_exists($array['username']) || strtolower($form_fields['username']) == strtolower($array['username'])) { foreach ($array as $key => $val) { // Set user data $user->{$key} = $val; } if ($form_fields['type'] == 'new') { if ($user->add(ORM::factory('role', $role)) && $user->save()) { $this->template->success_message = "New user, " . $post->username . ", has been created."; log::activity($this->user->username . " has created a new user " . $post->username, $this->user->username . " (" . $this->user->email . ") has created a new user."); } } if ($form_fields['type'] == 'edit') { $arr = $user->roles; foreach ($arr as $role) { $user->remove(ORM::factory('role', $role)); } if ($user->add(ORM::factory('role', $post->role)) and $user->save()) { $this->template->success_message = "User " . $post->username . " has been updated."; url::redirect('admin/users'); } } } else { $form_fields = arr::overwrite($form_fields, $post->as_array()); $errors = arr::overwrite($errors, $post->errors('form_error_messages')); if ($user->username_exists($array['username']) || $form_fields['username'] == $array['username']) { $this->template->error_message = "That username has been taken! Please try again!"; } } } else { $form_fields = arr::overwrite($form_fields, $post->as_array()); $errors = arr::overwrite($errors, $post->errors('form_error_messages')); $this->template->error_message = "There has been an error! Please try again!"; } } if ($form_type == "new") { $form = form::open("admin/users/#tab2", array('class' => 'valid_form')); } if ($form_type == "edit") { $form = form::open("admin/users/edit/" . $id, array('class' => 'valid_form')); } $form .= "<label for='username'>Username</label>"; $form .= empty($errors['username']) ? '' : "<p class='error'>Please make sure you enter a unique username</p>"; $form .= form::input('username', $form_fields['username'], 'class="required fullWidth"'); $form .= "<label for='email'>Email</label>"; $form .= empty($errors['email']) ? '' : "<p class='error'>Please enter a valid email address</p>"; $form .= form::input('email', $form_fields['email'], 'class="required email fullWidth"'); $form .= "<label for='password'>Password</label>"; $form .= empty($errors['password']) ? '' : "<p class='error'>Please enter a password</p>"; $form .= form::password('password', $form_fields['password'], 'class="fullWidth required"'); $form .= "<label for='role'>User Type</label>"; $this->session = Session::instance(); $user = $this->session->get('user'); $superuser = FALSE; $roles = $user->roles; $arr = array(); foreach ($roles as $role) { if ($role->id == 1) { $superuser = TRUE; } } $roles = ORM::factory('role')->find_all(); foreach ($roles as $role) { if ($role->id == 1 && !$superuser) { } else { $arr[$role->name] = $role->name; } } $form .= form::dropdown('role', $arr, $form_fields['role'], "class='fullWidth'"); $form .= form::submit('submit', 'Save', "class='submit'"); $form .= form::close(); return $form; }