Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: andrzej
- * Date: 11/26/16
- * Time: 9:41 PM
- */
- namespace Controller;
- use Model\User;
- class UsersController extends Controller
- {
- function index() {
- $users = User::all();
- return ["/users/index.php", ['users' => $users, 'title' => "Users"]];
- }
- function create() {
- return ["/users/create.php", ['title' => "New user"]];
- }
- function store() {
- if(!$this->isFormFullyFilled()){
- $parameters = array_merge(['title' => "New user"], $this->unfilledFields());
- return ["/users/create.php", $parameters];
- }
- else {
- $user = new User();
- $user->name = $_POST["name"];
- $user->surname = $_POST["surname"];
- $user->email = $_POST["email"];
- $user->password = password_hash($_POST["password"], PASSWORD_DEFAULT);
- $user->confirmed = 0;
- $user->token = md5($_POST["email"]);
- $user->save();
- header("Location: /users/show/$user->id");
- }
- }
- function show($id){
- $user = User::find($id);
- return ["/users/show.php", ["user" => $user, "title" => "$user->name $user->surname"]];
- }
- private function isFormFullyFilled(){
- if($this->isNotSetOrEmptyString($_POST["name"])) {
- return false;
- }
- if($this->isNotSetOrEmptyString($_POST["surname"])) {
- return false;
- }
- if($this->isNotSetOrEmptyString($_POST["email"])) {
- return false;
- }
- if($this->isNotSetOrEmptyString($_POST["password"])) {
- return false;
- }
- if($this->isNotSetOrEmptyString($_POST["password_confirmation"])) {
- return false;
- }
- return true;
- }
- private function unfilledFields(){
- $tableOfContent = [];
- if($this->isNotSetOrEmptyString($_POST["name"])) {
- $tableOfContent["nameError"] = true;
- }
- else { // curently filled value should be refilled after refresh
- $tableOfContent["name"] = $_POST["name"];
- }
- if($this->isNotSetOrEmptyString($_POST["surname"])) {
- $tableOfContent["surnameError"] = true;
- }
- else { // curently filled value should be refilled after refresh
- $tableOfContent["surname"] = $_POST["surname"];
- }
- if($this->isNotSetOrEmptyString($_POST["email"])) {
- $tableOfContent["emailError"] = true;
- }
- else { // curently filled value should be refilled after refresh
- $tableOfContent["email"] = $_POST["email"];
- }
- if($this->isNotSetOrEmptyString($_POST["password"])) {
- $tableOfContent["passwordError"] = true;
- }
- if($this->isNotSetOrEmptyString($_POST["password_confirmation"])) {
- $tableOfContent["password_confirmationError"] = true;
- }
- return $tableOfContent;
- }
- private function isNotSetOrEmptyString($var){
- return !isset($var) || $var == "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement