CommonDevice::import PHP Method

import() public method

Import a device is not exists
public import ( array $input ) : interger
$input array array of datas
return interger ID of existing or new Device
    function import(array $input)
    {
        global $DB;
        if (!isset($input['designation']) || empty($input['designation'])) {
            return 0;
        }
        $where = array();
        $a_criteria = $this->getImportCriteria();
        foreach ($a_criteria as $field => $compare) {
            if (isset($input[$field])) {
                $compare = explode(':', $compare);
                switch ($compare[0]) {
                    case 'equal':
                        $where[] = "`" . $field . "`='" . $input[$field] . "'";
                        break;
                    case 'delta':
                        $where[] = "`" . $field . "`>'" . ($input[$field] - $compare[1]) . "'";
                        $where[] = "`" . $field . "`<'" . ($input[$field] + $compare[1]) . "'";
                        break;
                }
            }
        }
        $query = "SELECT `id`\n                FROM `" . $this->getTable() . "`\n                WHERE " . implode(" AND ", $where);
        $result = $DB->query($query);
        if ($DB->numrows($result) > 0) {
            $line = $DB->fetch_assoc($result);
            return $line['id'];
        }
        return $this->add($input);
    }