<?php
/*
VERSION : 3.0
CODENAME : SENAYAN
AUTHOR :
Code and Programming : ARIE NUGRAHA (hide@address.com)
Database Design : HENDRO WICAKSONO (hide@address.com) & WARDIYONO (hide@address.com)
SENAYAN Library Automation System
Copyright (C) 2007
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (GPL License.txt); if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* Biblio Import section */
// start the session
session_start();
require '../../../sysconfig.inc.php';
require SIMBIO_BASE_DIR.'simbio_GUI/table/simbio_table.inc.php';
require SIMBIO_BASE_DIR.'simbio_GUI/form_maker/simbio_form_table_AJAX.inc.php';
require SIMBIO_BASE_DIR.'simbio_FILE/simbio_file_upload.inc.php';
// privileges checking
$can_read = utility::havePrivilege('bibliography', 'r');
$can_write = utility::havePrivilege('bibliography', 'w');
if (!$can_read) {
die('<div class="errorBox">'.lang_sys_common_unauthorized.'</div>');
}
// max chars in line for file operations
$max_chars = 4096;
if (isset($_POST['doImport'])) {
// check for form validity
if (!$_FILES['importFile']['name']) {
utility::jsAlert(lang_mod_biblio_import_file_input_require);
exit();
} else if (empty($_POST['fieldSep']) OR empty($_POST['fieldEnc'])) {
utility::jsAlert(lang_mod_biblio_import_alert_all_field);
exit();
} else {
// set PHP time limit
set_time_limit(7200);
// set ob implicit flush
ob_implicit_flush();
// create upload object
$upload = new simbio_file_upload();
// get system temporary directory location
$temp_dir = sys_get_temp_dir();
// set max size
$max_size = $sysconf['max_upload']*1024;
$upload->setAllowableFormat(array('.csv'));
$upload->setMaxSize($max_size);
$upload->setUploadDir($temp_dir);
$upload_status = $upload->doUpload('importFile');
if ($upload_status != UPLOAD_SUCCESS) {
utility::jsAlert(lang_mod_biblio_import_alert_err_size.($sysconf['max_upload']/1024).' MB');
exit();
}
// uploaded file path
$uploaded_file = $temp_dir.DIRECTORY_SEPARATOR.$_FILES['importFile']['name'];
$row_count = 0;
// check for import setting
$record_num = intval($_POST['recordNum']);
$field_enc = trim($_POST['fieldEnc']);
$field_sep = trim($_POST['fieldSep']);
$record_offset = intval($_POST['recordOffset']);
$record_offset = $record_offset-1;
// get current datetime
$curr_datetime = date('Y-m-d H:i:s');
$curr_datetime = '\''.$curr_datetime.'\'';
// foreign key id cache
$gmd_id_cache = array();
$publ_id_cache = array();
$lang_id_cache = array();
$place_id_cache = array();
$author_id_cache = array();
$subject_id_cache = array();
// read file line by line
$inserted_row = 0;
$file = fopen($uploaded_file, 'r');
while (!feof($file)) {
// record count
if ($record_num > 0 AND $row_count == $record_num) {
break;
}
// go to offset
if ($row_count < $record_offset) {
// pass and continue to next loop
$row = fgets($file, $max_chars);
$row_count++;
continue;
} else {
// get an array of field
$field = fgetcsv($file, $max_chars, $field_sep, $field_enc);
if ($field) {
// strip escape chars from all fields
foreach ($field as $idx => $value) {
$field[$idx] = str_replace('\\', '', trim($value));
$field[$idx] = $dbs->escape_string($field[$idx]);
}
// strip leading field encloser if any
$title = preg_replace('@^\\\s*'.$field_enc.'@i', '', $field[0]);
$title = '\''.$title.'\'';
$gmd_id = utility::getID($dbs, 'mst_gmd', 'gmd_id', 'gmd_name', $field[1], $gmd_id_cache);
$edition = $field[2]?'\''.$field[2].'\'':'NULL';
$isbn_issn = $field[3]?'\''.$field[3].'\'':'NULL';
$publisher_id = utility::getID($dbs, 'mst_publisher', 'publisher_id', 'publisher_name', $field[4], $publ_id_cache);
$publish_year = $field[5]?'\''.$field[5].'\'':'NULL';
$collation = $field[6]?'\''.$field[6].'\'':'NULL';
$series_title = $field[7]?'\''.$field[7].'\'':'NULL';
$call_number = $field[8]?'\''.$field[8].'\'':'NULL';
$language_id = utility::getID($dbs, 'mst_language', 'language_id', 'language_name', $field[9], $lang_id_cache);
$language_id = '\''.$language_id.'\'';
$publish_place_id = utility::getID($dbs, 'mst_place', 'place_id', 'place_name', $field[10], $place_id_cache);
$classification = $field[11]?'\''.$field[11].'\'':'NULL';;
$notes = $field[12]?'\''.$field[12].'\'':'NULL';;
$image = $field[13]?'\''.$field[13].'\'':'NULL';
$file_att = $field[14]?'\''.$field[14].'\'':'NULL';
$authors = $field[15];
$subjects = preg_replace('@\\\s*'.$field_enc.'$@i', '', $field[16]);
// sql insert string
$sql_str = "INSERT IGNORE INTO biblio (title, gmd_id, edition,
isbn_issn, publisher_id, publish_year,
collation, series_title, call_number,
language_id, publish_place_id, classification,
notes, image, file_att, input_date, last_update)
VALUES ($title, $gmd_id, $edition,
$isbn_issn, $publisher_id, $publish_year,
$collation, $series_title, $call_number,
$language_id, $publish_place_id, $classification,
$notes, $image, $file_att, $curr_datetime, $curr_datetime)";
// send query
$dbs->query($sql_str);
$biblio_id = $dbs->insert_id;
if (!$dbs->error) {
$inserted_row++;
// set authors
if (!empty($authors)) {
$biblio_author_sql = 'INSERT IGNORE INTO biblio_author (biblio_id, author_id, level) VALUES ';
$authors = explode('><', $authors);
foreach ($authors as $author) {
$author = trim(str_replace(array('>', '<'), '', $author));
$author_id = utility::getID($dbs, 'mst_author', 'author_id', 'author_name', $author, $author_id_cache);
$biblio_author_sql .= " ($biblio_id, $author_id, 1),";
}
// remove last comma
$biblio_author_sql = substr_replace($biblio_author_sql, '', -1);
// execute query
$dbs->query($biblio_author_sql);
echo $dbs->error;
}
// set topic
if (!empty($subjects)) {
$biblio_subject_sql = 'INSERT IGNORE INTO biblio_topic (biblio_id, topic_id, level) VALUES ';
$subjects = explode('><', $subjects);
foreach ($subjects as $subject) {
$subject = trim(str_replace(array('>', '<'), '', $subject));
$subject_id = utility::getID($dbs, 'mst_topic', 'topic_id', 'topic', $subject, $subject_id_cache);
$biblio_subject_sql .= " ($biblio_id, $subject_id, 1),";
}
// remove last comma
$biblio_subject_sql = substr_replace($biblio_subject_sql, '', -1);
// execute query
$dbs->query($biblio_subject_sql);
echo $dbs->error;
}
}
}
$row_count++;
}
}
// close file handle
fclose($file);
utility::writeLogs($dbs, 'staff', $_SESSION['uid'], 'bibliography', 'Importing '.$inserted_row.' bibliographic records from file : '.$_FILES['importFile']['name']);
echo '<script type="text/javascript">'."\n";
echo 'parent.$(\'importInfo\').update(\'<strong>'.$inserted_row.'</strong> records inserted successfully to bibliographic database, from record <strong>'.$_POST['recordOffset'].'</strong>\');'."\n";
echo 'parent.$(\'importInfo\').setStyle( {display: \'block\'} );'."\n";
echo '</script>';
exit();
}
}
?>
<table id="searchForm" cellpadding="5" cellspacing="0">
<tr>
<td class="imageLeft" valign="top" style="background-image: url(<?php echo $sysconf['admin_template']['dir'].'/'.$sysconf['admin_template']['theme'].'/import.png'; ?>)">
<?php echo lang_mod_biblio_import_header; ?>
<hr />
<?php echo lang_mod_biblio_import_header_text; ?>
</td>
</tr>
</table>
<div id="importInfo" class="infoBox" style="display: none;"> </div><div id="importError" class="errorBox" style="display: none;"> </div>
<?php
// create new instance
$form = new simbio_form_table_AJAX('mainForm', $_SERVER['PHP_SELF'], 'post');
$form->submit_button_attr = 'name="doImport" value="'.lang_mod_biblio_import_form_button_start.'" class="button"';
// form table attributes
$form->table_attr = 'align="center" id="dataList" style="width: 100%;" cellpadding="5" cellspacing="0"';
$form->table_header_attr = 'class="alterCell" style="font-weight: bold;"';
$form->table_content_attr = 'class="alterCell2"';
/* Form Element(s) */
// csv files
$str_input = simbio_form_element::textField('file', 'importFile');
$str_input .= ' Maximum '.$sysconf['max_upload'].' KB';
$form->addAnything(lang_mod_biblio_import_form_field_file_input, $str_input);
// field separator
$form->addTextField('text', 'fieldSep', lang_mod_biblio_import_form_field_separator, ''.htmlentities(',').'', 'style="width: 10%;"');
// field enclosed
$form->addTextField('text', 'fieldEnc', lang_mod_biblio_import_form_field_enclosed, ''.htmlentities('"').'', 'style="width: 10%;"');
// number of records to import
$form->addTextField('text', 'recordNum', lang_mod_biblio_import_form_field_rec_to_export, '0', 'style="width: 10%;"');
// records offset
$form->addTextField('text', 'recordOffset', lang_mod_biblio_import_form_field_rec_start, '1', 'style="width: 10%;"');
// output the form
echo $form->printOut();
?>