Issues (67)

src/User/User.php (1 issue)

1
<?php
2
3
namespace Alfs18\User;
4
5
use Anax\DatabaseActiveRecord\ActiveRecordModel;
6
7
/**
8
 * Example of FormModel implementation.
9
 */
10
class User extends ActiveRecordModel
11
{
12
    /**
13
     * @var string $tableName name of the database table.
14
     */
15
    protected $tableName = "User";
16
17
    /**
18
     * Columns in the table.
19
     *
20
     * @var integer $id primary key auto incremented.
21
     */
22
    public $id;
23
    public $acronym;
24
    public $password;
25
    public $info;
26
    public $points;
27
    public $picture;
28
    public $created;
29
    public $updated;
30
    public $deleted;
31
    public $active;
32
33
    /**
34
     * Set the password.
35
     *
36
     * @param string $password the password to use.
37
     *
38
     * @return void
39
     */
40
    public function setPassword($password)
41
    {
42
        $this->password = password_hash($password, PASSWORD_DEFAULT);
43
    }
44
45
46
47
    /**
48
     * Set the profile picture.
49
     *
50
     * @param object $di
51
     *
52
     * @return void
53
     */
54
    public function setPicture($di)
55
    {
56
        $db = $di->get("dbqb");
57
        $db->connect();
58
        $res = $db->select("name")
59
                   ->from("Pictures")
60
                   ->where("acronym = ?")
61
                   ->execute(["default"])
62
                   ->fetchAll();
63
64
        $array = [];
65
        foreach ($res as $val) {
66
            // var_dump($val->name);
67
            array_push($array, $val->name);
68
        }
69
        // var_dump($array);
70
71
        $pic = array_rand($array, 1);
72
        // var_dump($array[$pic]);
73
        $this->picture = $array[$pic];
74
    }
75
76
77
78
    /**
79
     * Verify the acronym and the password, if successful the object
80
     * contains all details from the databse row.
81
     *
82
     * @param string $acronym acronym to check.
83
     * @param string $password the password to use.
84
     *
85
     * @return boolean true if okey, false if something went wrong.
86
     */
87
    public function verifyPassword($acronym, $password)
88
    {
89
        $this->find("acronym", $acronym);
90
        return password_verify($password, $this->password);
91
    }
92
93
94
    /**
95
     * Verify the acronym and the password, if successful the object
96
     * contains all details from the databse row.
97
     *
98
     * @param string $acronym acronym to check.
99
     * @param string $password the password to use.
100
     *
101
     * @return boolean true if okey, false if something went wrong.
102
     */
103
    public function saveUser($di)
104
    {
105
        $db = $di->get("dbqb");
106
        $db->connect()
107
            ->insert("User", ["acronym", "password", "picture", "points", "created"])
108
            ->execute([$this->acronym, $this->password, $this->picture, $this->points, $this->created]);
109
    }
110
111
112
    /**
113
     * Change character to ÅÄÖ if needed.
114
     *
115
     * @param string $text     The string/text to be checked.
116
     *
117
     * @return array
118
     */
119
    public function changeCharacter($text)
120
    {
121
        $characters = [
122
            "/&Aring;/" => "Å",
123
            "/&aring;/" => "å",
124
            "/&Auml;/" => "Ä",
125
            "/&auml;/" => "ä",
126
            "/&Ouml;/" => "Ö",
127
            "/&ouml;/" => "ö"
128
        ];
129
130
        foreach ($characters as $key => $val) {
131
            $text = preg_replace($key, $val, $text);
132
        }
133
        return $text;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $text returns the type string which is incompatible with the documented return type array.
Loading history...
134
    }
135
}
136