Completed
Push — master ( 3c5c0f...ec5ff6 )
by Laurent
01:40
created

UserSelect::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 */
5
6
namespace flightlog\form;
7
8
use User;
9
10
/**
11
 * @author Laurent De Coninck <[email protected]>
12
 */
13
class UserSelect extends Select
14
{
15
16
    /**
17
     * @var \DoliDB
18
     */
19
    private $db;
20
21
    /**
22
     * @inheritDoc
23
     */
24
    public function __construct($name, array $options = [], \DoliDB $db)
25
    {
26
        parent::__construct($name, $options);
27
        $this->db = $db;
28
        $this->buildOptions();
29
    }
30
31
    /**
32
     * Build the options of the select
33
     */
34
    private function buildOptions()
35
    {
36
37
        if ((boolean) $this->getOption('show_empty')) {
38
            $this->addValueOption(-1, ' ');
39
        }
40
41
        if ((boolean) $this->getOption('show_every')) {
42
            $this->addValueOption(-2, 'Everybody');
43
        }
44
45
        // Forge request to select users
46
        $sql = "SELECT DISTINCT u.rowid, u.lastname as lastname, u.firstname, u.statut, u.login, u.admin, u.entity";
47
        $sql .= " FROM " . MAIN_DB_PREFIX . "user as u";
48
        $sql .= " WHERE u.entity IN (0,1)";
49
        $sql .= " AND u.statut <> 0";
50
51
        if (empty($this->getOption('MAIN_FIRSTNAME_NAME_POSITION'))) {
52
            $sql .= " ORDER BY u.firstname ASC";
53
        } else {
54
            $sql .= " ORDER BY u.lastname ASC";
55
        }
56
57
        $resql = $this->db->query($sql);
58
        if ($resql) {
59
            $num = $this->db->num_rows($resql);
60
            $i = 0;
61
            if ($num) {
62
                $userstatic = new User($this->db);
63
64
                while ($i < $num) {
65
                    $obj = $this->db->fetch_object($resql);
66
67
                    $userstatic->id = $obj->rowid;
68
                    $userstatic->lastname = $obj->lastname;
69
                    $userstatic->firstname = $obj->firstname;
70
71
                    $this->addValueOption($obj->rowid, $userstatic->getFullName(null));
72
                    $i++;
73
                }
74
            }
75
76
77
        }
78
    }
79
}