private function handleImportPost() { $request = Request::getInstance(); $values = array_merge($request->getRequest(Request::POST), $request->getRequest(Request::FILES)); $grp_used = $request->getValue('grp_used'); if (!$grp_used) { $grp_used = array(); } try { require_once DIF_ROOT . "utils/CsvFile.php"; // check if import file is uploaded if (!array_key_exists('import_file', $values) && !is_array($values['import_file'])) { throw new Exception('No import file set'); } // validate file is really a uploaded file $file = $values['import_file']; if (!array_key_exists('tmp_name', $file) || !is_uploaded_file($file['tmp_name'])) { throw new Exception('wrong file.'); } $csvFile = new CsvFile(); $records = $csvFile->import($file['tmp_name']); // check fields $fields = array_intersect($csvFile->getFields(), $this->exportColumns); if (!$fields || !in_array('username', $fields)) { throw new Exception("Username is not present in import file"); } $db = $this->getDb(); // create temporary table $query = "create temporary table userimport like " . $this->sqlParser->getTable(); $res = $db->query($query); if ($db->isError($res)) { throw new Exception($res->getDebugInfo()); } // filter and insert records $fieldNames = array_intersect($this->sqlParser->getFieldNames(), $fields); ksort($fieldNames); $query = "insert into userimport(" . join(",", array_keys($fieldNames)) . ") values "; $recordFields = array(); foreach ($fieldNames as $name) { $recordFields[$name] = $name; } $rows = array(); foreach ($records as $record) { $record = array_intersect_key($record, $recordFields); if (!$record) { next; } ksort($record); if (array_key_exists('role', $record) && $record['role']) { $record['role'] = $this->getRoleId($record['role']); } foreach ($record as &$item) { //if(!$item) $item = "NULL"; $item = addslashes($item); } $rows[] = "('" . join("','", $record) . "')"; } $query .= join(",", $rows); $res = $db->query($query); if ($db->isError($res)) { throw new Exception($res->getDebugInfo()); } // update records $update = array(); $insert = array(); foreach ($fieldNames as $key => $value) { $insert[] = "a.{$key}"; // skip username and password if ($key == 'username' || $key == 'password') { continue; } $update[] = "a.{$key} = b.{$key}"; } $tablename = $this->sqlParser->getTable(); $query = sprintf("update %s as a inner join userimport as b on a.usr_username = b.usr_username set %s", $tablename, join(",", $update)); $res = $db->query($query); if ($db->isError($res)) { throw new Exception($res->getDebugInfo()); } // insert records $query = sprintf("insert into %s (%s,usr_create) select %s, now() from userimport as a left join %s as b on a.usr_username = b.usr_username where b.usr_id is NULL", $tablename, join(",", array_keys($fieldNames)), join(",", $insert), $tablename); $res = $db->query($query); if ($db->isError($res)) { throw new Exception($res->getDebugInfo()); } // update group foreach ($grp_used as $grp_id) { $query = sprintf("insert into usergroup (usr_id, grp_id) \n\t\t\t\t\t\t\t\t\t\t\t\t\tselect a.usr_id, %d \n\t\t\t\t\t\t\t\t\t\t\t\t\tfrom %s as a \n\t\t\t\t\t\t\t\t\t\t\t\t\tinner join userimport as b \n\t\t\t\t\t\t\t\t\t\t\t\t\ton a.usr_username = b.usr_username \n\t\t\t\t\t\t\t\t\t\t\t\t\tleft join usergroup as c \n\t\t\t\t\t\t\t\t\t\t\t\t\ton a.usr_id = c.usr_id and c.grp_id = %d \n\t\t\t\t\t\t\t\t\t\t\t\t\twhere c.usr_id is NULL", $grp_id, $this->sqlParser->getTable(), $grp_id); $res = $db->query($query); if ($db->isError($res)) { throw new Exception($res->getDebugInfo()); } } viewManager::getInstance()->setType(ViewManager::ADMIN_OVERVIEW); $this->handleAdminOverview(); } catch (Exception $e) { $template = new TemplateEngine(); $template->setVariable('errorMessage', $e->getMessage(), false); $this->handleImportGet(); } }