Passed
Push — master ( 18111a...f46ab8 )
by Julito
12:19
created

SkillProfileModel::save()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 21
rs 9.5555
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
class SkillProfileModel extends Model
6
{
7
    public $columns = ['id', 'name', 'description'];
8
9
    public function __construct()
10
    {
11
        $this->table = Database::get_main_table(TABLE_MAIN_SKILL_PROFILE);
12
        $this->table_rel_profile = Database::get_main_table(TABLE_MAIN_SKILL_REL_PROFILE);
13
    }
14
15
    /**
16
     * @return array
17
     */
18
    public function getProfiles()
19
    {
20
        $sql = "SELECT * FROM $this->table p
21
                INNER JOIN $this->table_rel_profile sp
22
                ON (p.id = sp.profile_id) ";
23
        $result = Database::query($sql);
24
25
        return Database::store_result($result, 'ASSOC');
26
    }
27
28
    /**
29
     * This function is for editing profile info from profile_id.
30
     *
31
     * @param int    $profileId
32
     * @param string $name
33
     * @param string $description
34
     *
35
     * @return bool
36
     */
37
    public function updateProfileInfo($profileId, $name, $description)
38
    {
39
        $profileId = (int) $profileId;
40
41
        if (empty($profileId)) {
42
            return false;
43
        }
44
45
        $name = Database::escape_string($name);
46
        $description = Database::escape_string($description);
47
48
        $sql = "UPDATE $this->table SET
49
                    name = '$name',
50
                    description = '$description'
51
                WHERE id = $profileId ";
52
        Database::query($sql);
53
54
        return true;
55
    }
56
57
    /**
58
     * Call the save method of the parent class and the SkillRelProfile object.
59
     *
60
     * @param array $params
61
     * @param bool  $showQuery Whether to show the query in parent save() method
62
     *
63
     * @return mixed Profile ID or false if incomplete params
64
     */
65
    public function save($params, $showQuery = false)
66
    {
67
        if (!empty($params)) {
68
            $profile_id = parent::save($params, $showQuery);
69
            if ($profile_id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $profile_id of type false|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
70
                $skill_rel_profile = new SkillRelProfileModel();
71
                if (isset($params['skills'])) {
72
                    foreach ($params['skills'] as $skill_id) {
73
                        $attributes = [
74
                            'skill_id' => $skill_id,
75
                            'profile_id' => $profile_id,
76
                        ];
77
                        $skill_rel_profile->save($attributes);
78
                    }
79
                }
80
81
                return $profile_id;
82
            }
83
        }
84
85
        return false;
86
    }
87
88
    /**
89
     * Delete a skill profile.
90
     *
91
     * @param int $id The skill profile id
92
     *
93
     * @return bool Whether delete a skill profile
94
     */
95
    public function delete($id)
96
    {
97
        Database::delete(
98
            $this->table_rel_profile,
99
            [
100
                'profile_id' => $id,
101
            ]
102
        );
103
104
        return parent::delete($id);
105
    }
106
}
107