Kimai_Database_Mysql::get_customers PHP Method

get_customers() public method

returns list of customers in a group as array
Author: th
public get_customers ( array $groups = null ) : array
$groups array ID of group in table groups or "all" for all groups
return array
    public function get_customers(array $groups = null)
    {
        $p = $this->kga['server_prefix'];
        if (empty($groups)) {
            $query = "SELECT customerID, name, contact, visible\n              FROM {$p}customers\n              WHERE trash=0\n              ORDER BY visible DESC, name;";
        } else {
            $query = "SELECT DISTINCT customerID, name, contact, visible\n              FROM {$p}customers\n              JOIN {$p}groups_customers AS g_c USING (customerID)\n              WHERE g_c.groupID IN (" . implode($groups, ',') . ")\n                AND trash=0\n              ORDER BY visible DESC, name;";
        }
        $result = $this->conn->Query($query);
        if ($result == false) {
            $this->logLastError('get_customers');
            return false;
        }
        $i = 0;
        if ($this->conn->RowCount()) {
            $arr = array();
            $this->conn->MoveFirst();
            while (!$this->conn->EndOfSeek()) {
                $row = $this->conn->Row();
                $arr[$i]['customerID'] = $row->customerID;
                $arr[$i]['name'] = $row->name;
                $arr[$i]['contact'] = $row->contact;
                $arr[$i]['visible'] = $row->visible;
                $i++;
            }
            return $arr;
        }
        return array();
    }

Usage Example

Beispiel #1
0
/**
 * @param Kimai_Database_Mysql $database
 * @param array $kgaUser
 * @param bool $viewOtherGroupsAllowed
 * @return array
 */
function getCustomersData(Kimai_Database_Mysql $database, $kgaUser, $viewOtherGroupsAllowed)
{
    if ($database->global_role_allows($kgaUser['globalRoleID'], 'core-customer-otherGroup-view')) {
        $customers = $database->get_customers();
    } else {
        $customers = $database->get_customers($kgaUser['groups']);
    }
    foreach ($customers as $row => $data) {
        $groupNames = array();
        $groups = $database->customer_get_groupIDs($data['customerID']);
        if ($groups !== false) {
            foreach ($groups as $groupID) {
                if (!$viewOtherGroupsAllowed && array_search($groupID, $kgaUser['groups']) === false) {
                    continue;
                }
                $data = $database->group_get_data($groupID);
                $groupNames[] = $data['name'];
            }
            $customers[$row]['groups'] = implode(", ", $groupNames);
        }
    }
    return array('customers' => $customers);
}
Kimai_Database_Mysql