<?php
require_once "odincom.php";
require_once "dbo/User.php";
require_once "dbo/UserList.php";
require_once "dbo/Patron.php";
require_once "$ui_dir/uicom.inc";
require_once "$ui_dir/renderer.inc";
require_once "$ui_dir/dispuploadmem.inc";
$script ="uploadmem.php";
odin_staff_login();
if ($_GET['nvr']) {
$_SESSION['navbar']->register("root_user");
}
$action = $_POST['action'];
if (!$action && $_GET['browse'] == 1) $action = "browse";
switch ($action) {
case "upload":
if (! handle_upload()) {
break;
}
$_SERVER['QUERY_STRING'] .= "browse=1";
case "browse":
$printForm = "merge";
$renderObj = browse_temp_user();
break;
case "postupload":
$_SESSION['navbar']->register("uploaduser");
post_upload();
break;
default:
if ($mid = $_GET['mid']) {
$printForm = "merge";
$renderObj = user_info($mid);
}
else {
$_SESSION['navbar']->register("uploaduser");
load_config_type($mhdr , 'upload_memlist_hdr');
$printForm = "upload";
}
break;
}
print_header();
switch($printForm) {
case "upload" :
upload_form(PatronGroupList::queryAllNames());
break;
case "merge" :
merge_form();
break;
default:
}
$renderer = new Renderer;
if ($renderObj) {
$renderer->render($renderObj);
}
odin_disconnect();
print_footer();
exit;
function check_data_existance() {
odin_connect();
$result = mysql_list_tables($GLOBALS['sql_database']);
if (!$result) {
myresult(0,"Cannot list tables\n");
return;
}
while (list($row,) = mysql_fetch_row($result)) {
if ($row == "odin_temp_user") {
return 1;
break;
}
}
}
function post_upload() {
if (!check_staff_permission("writememberlist")) return;
$colnames = "patronno, name, namealt, email, phone, dept, college, icq, membership, note";
odin_connect();
$opt = $_POST['opt'];
switch ($opt) {
case "mergeignore":
$query_member = "INSERT IGNORE INTO odin_user ($colnames) SELECT $colnames FROM odin_temp_user";
$query_drop = "DROP TABLE odin_temp_user";
break;
case "mergeupdate":
$query_member = "REPLACE INTO odin_user ($colnames) SELECT $colnames FROM odin_temp_user";
$query_drop = "DROP TABLE odin_temp_user";
break;
case "refresh":
$query_predrop = "DROP TABLE odin_user";
$query_rename = "RENAME TABLE odin_temp_user TO odin_user";
break;
case "cancel":
$query_drop = "DROP TABLE odin_temp_user";
break;
default:
myresult(0, "Unknown option");
return;
}
$query = "LOCK TABLES odin_user WRITE, odin_temp_user WRITE";
$result = mysql_query($query);
if (! $result) {
myresult(0, "Cannot lock table : ". mysql_error());
return;
}
if ($query_predrop) {
$result = mysql_query($query_predrop);
if ($result) {
myresult(1, "Query success<br>$query_predrop\n");
} else {
myresult(0, "query failed : " . mysql_error(). "<br>$query_predrop\n");
return;
}
}
if ($query_member) {
$result = mysql_query($query_member);
if ($result) {
myresult(1, "Query success : ". mysql_affected_rows() ." rows affected.<br>$query_series\n");
} else {
myresult(0, "query failed : " . mysql_error(). "<br>$query_series\n");
return;
}
}
if ($query_rename) {
mysql_query("UNLOCK TABLES");
$result = mysql_query($query_rename);
if ($result) {
myresult(1, "Query success : ". mysql_affected_rows() ." rows affected.<br>$query_rename\n");
} else {
myresult(0, "query failed : " . mysql_error(). "<br>$query_rename\n");
return;
}
}
if ($query_drop) {
$result = mysql_query($query_drop);
if ($result) {
myresult(1, "Query success : ". mysql_affected_rows() ." rows affected.<br>$query_drop\n");
} else {
myresult(0, "query failed : " . mysql_error(). "<br>$query_drop\n");
return;
}
}
mysql_query("UNLOCK TABLES");
$query = "INSERT INTO odin_log SET type='UploadMemlist',"
." staff='{$_SESSION['userinfo']['id']}',"
." ipaddr='{$_SERVER['REMOTE_ADDR']}',"
." itemdesc='opt=$opt',"
." timestamp=NULL";
if (!mysql_query($query) || !mysql_affected_rows()) {
myresult (0, "(Warning) Insert into odin_log failed : ". mysql_error() ."<br>$query");
}
return 1;
}
function handle_upload() {
if (!check_staff_permission("writememberlist")) return;
$membership = $_POST['membership'];
if (is_array($_POST['hdr'])) {
$mhdr = $_POST['hdr'];
while(list($key,$val) = each ($mhdr)) $mhdr[$key] = trim($val);
//array_walk($mhdr, "trim");
//array_map("trim", $mhdr);
} else {
load_config_type($mhdr , 'upload_memlist_hdr');
}
//print_r($mhdr);
$file = "tmp/memlist";
if (! move_uploaded_file($_FILES['memlist']['tmp_name'], $file)) {
myresult(0, "File upload error");
return;
}
$fh = fopen ($file, "r");
//$headerline = array_shift($lines);
$headerline = fgets($fh);
// UNICODE: ignore BOM (byte order mark)
if (substr($headerline, 0, 2) == "\xff\xfe") $headerline = substr($headerline, 2);
elseif (substr($headerline, 0, 3) == "\xef\xbb\xbf") $headerline = substr($headerline, 3);
$header = explode(",", $headerline);
//print_r($header);
if (count($header) > 1) {
$csv = 1;
$delimiter = ",";
} else {
$header = explode("\t", $headerline);
//print "reparse as tsv...<br>\n";
//print_r($header);
$tsv = 1;
$delimiter = "\t";
}
if (count($header) <= 1) {
myresult(0, "File format error. Use either tab separated list or comma separated list");
return;
}
// find header index
$search_hdrs = array('patronno','name','namealt','email','phone','icq','college','dept','note','membership');
foreach ($header as $i=>$h) {
$h = trim($h);
$found = 0;
foreach ($search_hdrs as $k)
{
if (isset($idx[$k])) continue;
//print "<br>$h~$mhdr[$k]~idx[$k]~".strcasecmp($h,$mhdr[$k])."~$i\n";
if (strcasecmp($h,$mhdr[$k])==0) {
$idx[$k] = $i;
$found = 1;
break;
}
}
if (!$found) {
myresult (0, "Warning: I don't understand the field with header $h, ignoring it...");
//print "uncatched : $i $h\n";
}
}
//print_r($idx);
// required fields
//print "$idx_bid,$idx_title,$idx_author,$idx_volume,$idx_shelf,$idx_publisher";
if (!isset($idx['name'])) {
myresult(0,"Incorrect format, can't find name");
return;
}
if (!isset($idx['patronno'])) {
myresult(0,"Incorrect format, can't find patronno");
return;
}
//critical selection ( locking )
if (! ($result = mysql_query("SELECT GET_LOCK(\"temp_memlist\", 5)"))) {
myresult(0,"Error in get_lock for temp_memlist : ".mysql_error());
return;
}
list($lockok, ) = mysql_fetch_array($result);
if (!$lockok) {
myresult(0,"Failed to obtain lock for temp_memlist");
return;
}
$ss = array();
$scount = 0;
$vcount = 0;
//foreach ($lines as $l)
while ($fields = fgetcsv($fh, 1000, $delimiter, '"'))
{
$md = array();
if ($membership) $md['membership'] = $membership;
foreach ($search_hdrs as $k) {
if (isset($idx[$k])) $md[$k] = $fields[$idx[$k]];
}
$mid = $md['patronno'];
if (!$mid) continue;
if (!$md['name']) continue;
$mems[$mid] = $md;
$mcount ++;
}
fclose($fh);
unlink($file);
////////////////////////////////////////////////////// database transaction
odin_connect();
//initialize tables
if (!mysql_query("DROP TABLE IF EXISTS odin_temp_user")) {
myresult(0,"Cannot drop odin_temp_user : ".mysql_error());
return;
}
if (!mysql_query("CREATE TABLE odin_temp_user LIKE odin_user")) {
myresult(0,"Cannot create odin_temp_user : ".mysql_error());
return;
}
foreach ($mems as $mid=>$md)
{
$sql_part = array();
foreach ($search_hdrs as $k) {
if (isset($md[$k])) $sql_part[] = $k."='". mysql_escape_string($md[$k])."'";
}
$query = "INSERT INTO odin_temp_user SET ";
$query .= join(', ', $sql_part);
//print $query;
if (mysql_query($query) && (mysql_affected_rows() == 1)) {
// insert ok
} else {
myresult (0, "Insert odin_temp_user failed : " . mysql_error() . "<br>$query");
// unlikely error. something must be wrong. quiting
//return;
}
$series = mysql_insert_id();
}
$query = "INSERT INTO odin_log SET type='UploadUserlist',"
." staff='{$_SESSION['userinfo']['id']}',"
." ipaddr='{$_SERVER['REMOTE_ADDR']}',"
." itemdesc='opt=uploadfile NumOfItem=$mcount',"
." timestamp=NULL";
if (!mysql_query($query) || !mysql_affected_rows()) {
myresult (0, "(Warning) Insert into odin_log failed : ". mysql_error() ."<br>$query");
}
myresult (1, "Parsed $mcount users");
// save config
save_config_type($mhdr , 'upload_memlist_hdr');
return 1;
}
function browse_temp_user() {
$list = new UserList("odin_temp_user");
$list->page = $_GET['page'];
$list->browse();
if ($list->no_of_match >= 1) {
if ($_GET['query']) $displaystr .= " '{$_GET['query']}'";
if ($_GET['page']) $displaystr .= " pg. {$_GET['page']}";
$_SESSION['navbar']->register("result", "get", $displaystr);
return $list;
}
else
{
myresult(0, "No data in import user list");
}
}
//////////////////////////////////////////////////////////////////////////
function user_info($mid) {
if (!check_staff_permission("readmemberlist")) return;
$class = new User("odin_temp_user");
$class->id = $mid;
if (!$class->loadFromDB()) return;
$midtext = "$class->patronno $class->name";
$_SESSION['navbar']->register("userinfo", "{$GLOBALS['script']}?mid=$mid", $midtext);
return $class;
}
?>