Code Duplication    Length = 90-91 lines in 2 locations

src/User/HTMLForm/AdminUpdateUser.php 1 location

@@ 12-101 (lines=90) @@
9
/**
10
 * Form to update an item.
11
 */
12
class AdminUpdateUser extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container and the id to update.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     * @param integer             $id to update
19
     */
20
    public function __construct(DIInterface $di, $id)
21
    {
22
        parent::__construct($di);
23
        $user = $this->getItemDetails($id);
24
        $this->form->create(
25
            [
26
                "id"     => __CLASS__,
27
                "legend" => "Update details of the item",
28
            ],
29
            [
30
                "id" => [
31
                    "type"       => "hidden",
32
                    "value"      => $user->id,
33
                ],
34
35
                "name" => [
36
                    "type"       => "text",
37
                    "validation" => ["not_empty"],
38
                    "value"      => $user->name,
39
                ],
40
41
                "email" => [
42
                    "type"       => "text",
43
                    "validation" => ["not_empty"],
44
                    "value"      => $user->email,
45
                ],
46
47
                "age" => [
48
                    "type"       => "number",
49
                    "validation" => ["not_empty"],
50
                    "value"      => $user->age,
51
                ],
52
53
                "submit" => [
54
                    "type"     => "submit",
55
                    "value"    => "Save",
56
                    "callback" => [$this, "callbackSubmit"],
57
                ],
58
59
                "reset" => [
60
                    "type" => "reset",
61
                ],
62
            ]
63
        );
64
    }
65
66
67
    /**
68
     * Get details on item to load form with.
69
     *
70
     * @param integer $id get details on item with id.
71
     *
72
     * @return $user
73
     */
74
    public function getItemDetails($id)
75
    {
76
        $user = new User();
77
        $user->setDb($this->di->get("db"));
78
        $user->find("id", $id);
79
        return $user;
80
    }
81
82
83
    /**
84
     * Callback for submit-button which should return true if it could
85
     * carry out its work and false if something failed.
86
     *
87
     * @return boolean true if okey, false if something went wrong.
88
     */
89
    public function callbackSubmit()
90
    {
91
        $user = new User();
92
        $user->setDb($this->di->get("db"));
93
        $user->find("id", $this->form->value("id"));
94
        $user->name = htmlentities($this->form->value("name"));
95
        $user->email = htmlentities($this->form->value("email"));
96
        $user->age = htmlentities($this->form->value("age"));
97
        $user->save();
98
        $url = $this->di->get("url")->create("admin");
99
        $this->di->get("response")->redirect($url . "/update/{$user->id}");
100
    }
101
}
102

src/User/HTMLForm/UpdateProfileForm.php 1 location

@@ 12-102 (lines=91) @@
9
/**
10
 * Form to update an item.
11
 */
12
class UpdateProfileForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container and the id to update.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     * @param integer             $id to update
19
     */
20
    public function __construct(DIInterface $di, $id)
21
    {
22
        parent::__construct($di);
23
        $profileInfo = $this->getItemDetails($id);
24
        $this->form->create(
25
            [
26
                "id"     => __CLASS__,
27
                "legend" => "Update details of the item",
28
            ],
29
            [
30
                "id"    => [
31
                    "type"     => "hidden",
32
                    "value"    => $profileInfo->id,
33
                ],
34
                "email" => [
35
                    "type"       => "text",
36
                    "validation" => ["not_empty"],
37
                    "value"      => $profileInfo->email,
38
                ],
39
40
                "name" => [
41
                    "type"       => "text",
42
                    "validation" => ["not_empty"],
43
                    "value"      => $profileInfo->name,
44
                ],
45
46
                "age" => [
47
                    "type"       => "number",
48
                    "validation" => ["not_empty"],
49
                    "value"      => $profileInfo->age,
50
                ],
51
52
                "submit" => [
53
                    "type"     => "submit",
54
                    "value"    => "Save",
55
                    "callback" => [$this, "callbackSubmit"],
56
                ],
57
58
                "reset" => [
59
                    "type" => "reset",
60
                ],
61
            ]
62
        );
63
    }
64
65
66
    /**
67
     * Get details on item to load form with.
68
     *
69
     * @param integer $id get details on item with id.
70
     *
71
     * @return $user
72
     */
73
    public function getItemDetails($id)
74
    {
75
        $user = new User();
76
        $user->setDb($this->di->get("db"));
77
        $user->find("id", $id);
78
        return $user;
79
    }
80
81
82
    /**
83
     * Callback for submit-button which should return true if it could
84
     * carry out its work and false if something failed.
85
     *
86
     * @return boolean true if okey, false if something went wrong.
87
     */
88
    public function callbackSubmit()
89
    {
90
        $user = new User();
91
        $user->setDb($this->di->get("db"));
92
        $user->find("id", $this->form->value("id"));
93
        $user->email = htmlentities($this->form->value("email"));
94
        $user->name = htmlentities($this->form->value("name"));
95
        $user->age = htmlentities($this->form->value("age"));
96
        $user->save();
97
        $this->di->get("session")->set("email", $user->email);
98
        $url = $this->di->get("url")->create("user/login");
99
        $this->di->get("response")->redirect($url);
100
        //$this->di->get("response")->redirect($url . "/update/{$book->id}");
101
    }
102
}
103