get_source_params($source_id);
if ($params["PARAMETERS"]) {
// Affichage du formulaire avec $params["PARAMETERS"]
$vars = unserialize($params["PARAMETERS"]);
foreach ( $vars as $key => $val ) {
global ${$key};
${$key} = $val;
}
}
// Champ perso de notice à utiliser
$form = "
" . $this->msg["odilotk_source_field"] . "
";
$query = "select idchamp, titre from notices_custom where datatype='small_text'";
$result = pmb_mysql_query($query);
if ($result && pmb_mysql_num_rows($result)) {
while ( $row = pmb_mysql_fetch_object($result) ) {
$form .= "
idchamp == $cp_field ? "selected='selected'" : "") . ">" . htmlentities($row->titre, ENT_QUOTES, $charset) . " ";
}
} else {
$form .= "
" . $this->msg["odilotk_no_field"] . " ";
}
$form .= "
";
$form .= "
";
return $form;
}
public function make_serialized_source_properties($source_id) {
global $cp_field;
$t["cp_field"] = $cp_field;
$this->sources[$source_id]["PARAMETERS"] = serialize($t);
}
/**
* Formulaire des propriétés générales
*/
public function get_property_form() {
global $charset;
$this->fetch_global_properties();
// Affichage du formulaire en fonction de $this->parameters
if ($this->parameters) {
$keys = unserialize($this->parameters);
$endpoint_url = $keys['endpoint_url'];
$api_endpoint_url = $keys['api_endpoint_url'];
$client_id = $keys['client_id'];
$client_secret = $keys['client_secret'];
$shared_secret_key = $keys['shared_secret_key'];
} else {
$endpoint_url = '';
$api_endpoint_url = '';
$client_id = '';
$client_secret = '';
$shared_secret_key = '';
}
$r = "
";
return $r;
}
public function make_serialized_properties() {
global $endpoint_url, $api_endpoint_url, $client_id, $client_secret, $shared_secret_key;
// Mise en forme des paramètres à partir de variables globales (mettre le résultat dans $this->parameters)
$keys = array();
$keys['endpoint_url'] = $endpoint_url;
$keys['api_endpoint_url'] = $api_endpoint_url;
$keys['client_id'] = $client_id;
$keys['client_secret'] = $client_secret;
$keys['shared_secret_key'] = $shared_secret_key;
$this->parameters = serialize($keys);
}
public function maj_entrepot($source_id, $callback_progress = "", $recover = false, $recover_env = "") {global $form_from;
global $form_radio;
global $form_from;
global $charset, $base_path;
$this->fetch_global_properties();
$keys = unserialize($this->parameters);
$this->callback_progress = $callback_progress;
$this->source_id = $source_id;
$this->n_recu = 0;
$curl = new Curl();
$curl->set_option('CURLOPT_USERPWD', $keys['client_id'] . ':' . $keys['client_secret']);
$response = $curl->post($keys['api_endpoint_url'] . '/token', array(
'grant_type' => 'client_credentials'
));
if ($response->headers['Status-Code'] != 200) {
$this->error = true;
$this->error_message = $this->msg["odilotk_authentication_failed"];
}
$content = json_decode($response->body);
$authentication_token = $content->token;
$authentication_type = $content->type;
$curl->options = array();
$curl->set_option('CURLOPT_HTTPHEADER', array(
'Authorization: ' . $authentication_type . ' ' . $authentication_token
));
$this->n_total = 0;
$this->n_total_inactive = 0;
$source_params = $this->unserialize_source_params($source_id);
$p = $source_params["PARAMETERS"];
$query = "select name from notices_custom where idchamp = " . $p['cp_field'];
$result = pmb_mysql_query($query);
if ($row = pmb_mysql_fetch_object($result)) {
$cp_odilotk = array(
'cp_odilotk' => $row->name
);
} else {
$cp_odilotk = array();
}
$modification_date = '';
if ($form_radio == 'last_sync') {
$sql = " SELECT MAX(UNIX_TIMESTAMP(date_import)) FROM entrepot_source_" . $source_id;
$res = pmb_mysql_result(pmb_mysql_query($sql), 0, 0);
$modification_date = date("Y-m-d", $res);
} else if ($form_radio == 'date_sync') {
$modification_date = $form_from;
}
do {
$response = $curl->get($keys['api_endpoint_url'] . '/records?limit=' . $this->limit . '&offset=' . ($this->limit * $this->page) . ($modification_date ? '&modificationDate='.$modification_date : ''));
if ($response->headers['Status-Code'] != 200) {
$this->error = true;
$this->error_message = $this->msg["odilotk_authentication_failed"];
}
$content = json_decode($response->body);
$this->n_total += count($content);
foreach ( $content as $record ) {
if (! $record->active) {
$this->n_total--;
$this->n_total_inactive++;
$this->progress();
continue;
}
$this->rec_record($this->odilotk_2_uni($record, $cp_odilotk), $source_id, '');
}
$this->page++;
if (! $this->n_total) {
return $this->n_total;
}
} while ( count($content) );
return $this->n_recu;
}
public function progress() {
$callback_progress = $this->callback_progress;
if ($this->n_total) {
$percent = ($this->n_recu / $this->n_total);
$nlu = $this->n_recu;
$ntotal = $this->n_total . ' (' . $this->n_total_inactive . ' ' . $this->msg['odilotk_records_inactive'] . ')';
} else {
$percent = 0;
$nlu = $this->n_recu;
$ntotal = "inconnu";
}
call_user_func($callback_progress, $percent, $nlu, $ntotal);
}
public function odilotk_2_uni($nt, $cp) {
global $charset;
$unimarc = array();
// Construction du 001
$unimarc["001"][0] = md5(serialize($nt));
// id odilotk
if ($cp['cp_odilotk']) {
$unimarc["900"][0]["a"][0] = $nt->id;
$unimarc["900"][0]["n"][0] = $cp['cp_odilotk'];
}
// source
$unimarc["801"][0]["a"][0] = 'EN';
$unimarc["801"][0]["b"][0] = 'OdiloTK';
// title
if (! empty($nt->title)) {
// Dans la majorité des cas, il y a l'auteur après un /, on l'enlève
$title = $nt->title;
if (strpos($title, '/') !== false) {
$title = substr($title, 0, strrpos($title, '/'));
}
$unimarc["200"][0]["a"][0] = $title;
}
// author
if (! empty($nt->author)) {
// 2 types de présentation de l'auteur ("Doe, John" et "John Doe")
if (strpos($nt->author, ',') !== false) {
// "Doe, John"
$author_names = explode(',', $nt->author);
$unimarc['700'][0]['a'][0] = trim($author_names[0]);
$unimarc['700'][0]['b'][0] = trim($author_names[1]);
$unimarc['700'][0]['4'][0] = '070';
} else {
// "John Doe"
$author_names = explode(' ', $nt->author);
$unimarc['700'][0]['a'][0] = trim(array_pop($author_names));
$unimarc['700'][0]['b'][0] = implode(' ', $author_names);
$unimarc['700'][0]['4'][0] = '070';
}
}
// thumbnail
if (! empty($nt->coverImageUrl)) {
$unimarc["896"][0]["a"][0] = $nt->coverImageUrl;
}
if (! empty($nt->description)) {
$unimarc["330"][0]["a"][0] = $nt->description;
}
if (! empty($nt->formats)) {
$unimarc["215"][0]["d"][0] = implode(', ', $nt->formats);
}
if (! empty($nt->gradeLevel)) {
// TODO
}
if (! empty($nt->isbn)) {
$unimarc["010"][0]["a"][0] = $nt->isbn;
}
if (! empty($nt->language)) {
$unimarc["101"][0]["a"][0] = $nt->language;
}
if (! empty($nt->publicationDate)) {
$unimarc["210"][0]["d"][0] = str_replace('.', '', $nt->publicationDate);
}
if (! empty($nt->publisher)) {
$unimarc["210"][0]["c"][0] = str_replace(',', '', $nt->publisher);
}
if (! empty($nt->releaseDate)) {
// TODO
}
if (! empty($nt->subject)) {
global $pmb_keyword_sep;
$subjects = explode('/', $nt->subject);
$unimarc["610"][0]["a"][0] = '';
for($i = 0; $i < count($subjects); $i++) {
if ($unimarc["610"][0]["a"][0]) {
$unimarc["610"][0]["a"][0] .= $pmb_keyword_sep;
}
$unimarc["610"][0]["a"][0] .= trim($subjects[$i]);
}
}
if (! empty($nt->type)) {
// TODO
}
return $unimarc;
}
public function rec_record($record, $source_id, $search_id) {
global $charset, $base_path, $url, $search_index;
$date_import = date("Y-m-d H:i:s", time());
// Recherche du 001
$ref = $record["001"][0];
// Mise à jour
if ($ref) {
$ref_exists = $this->has_ref($source_id, $ref);
if ($ref_exists)
return false;
// Si conservation des anciennes notices, on regarde si elle existe
$ref_exists = false;
if (! $this->del_old) {
$ref_exists = $this->has_ref($source_id, $ref);
}
// Si pas de conservation des anciennes notices, on supprime
if ($this->del_old) {
$this->delete_from_entrepot($source_id, $ref);
$this->delete_from_external_count($source_id, $ref);
}
if (($this->del_old) || ((! $this->del_old) && (! $ref_exists))) {
// Insertion de l'entête
$n_header["rs"] = "*";
$n_header["ru"] = "*";
$n_header["el"] = "1";
$n_header["bl"] = "m";
$n_header["hl"] = "0";
$n_header["dt"] = "a";
// Récupération d'un ID
$recid = $this->insert_into_external_count($source_id, $ref);
foreach ( $n_header as $hc => $code ) {
$this->insert_header_into_entrepot($source_id, $ref, $date_import, $hc, $code, $recid, $search_id);
}
$field_order = 0;
foreach ( $record as $field => $val ) {
for($i = 0; $i < count($val); $i++) {
if (is_array($val[$i])) {
foreach ( $val[$i] as $sfield => $vals ) {
for($j = 0; $j < count($vals); $j++) {
if ($charset != "utf-8")
$vals[$j] = utf8_decode($vals[$j]);
$this->insert_content_into_entrepot($source_id, $ref, $date_import, $field, $sfield, $field_order, $j, $vals[$j], $recid, $search_id);
}
}
} else {
if ($charset != "utf-8")
$vals[$i] = utf8_decode($vals[$i]);
$this->insert_content_into_entrepot($source_id, $ref, $date_import, $field, '', $field_order, 0, $val[$i], $recid, $search_id);
}
$field_order++;
}
}
$this->rec_isbd_record($source_id, $ref, $recid);
}
$this->n_recu++;
$this->progress();
}
return true;
}
public function form_pour_maj_entrepot($source_id, $sync_form = "sync_form") {
global $form_from;
global $form_radio;
$source_id = (int) $source_id;
$sql = " SELECT MAX(UNIX_TIMESTAMP(date_import)) FROM entrepot_source_" . $source_id;
$res = pmb_mysql_result(pmb_mysql_query($sql), 0, 0);
$latest_date_database_string = $res ? formatdate(date("Y-m-d", $res)) : "" . $this->msg["odilotk_nonotice_sync"] . " ";
$dateuntil = "";
$form = "";
$form .= "
" . $this->msg["odilotk_get_notices"] . "
" . $this->msg["odilotk_last_sync"] . "
" . $this->msg["odilotk_sync_from"] . "
";
$form .= " " . sprintf($this->msg["odilotk_syncinfo_date_baserecent"], $latest_date_database_string) . " ";
$form .= " ";
return $form;
}
//Nécessaire pour passer les valeurs obtenues dans form_pour_maj_entrepot au javascript asynchrone
public function get_maj_environnement($source_id) {
global $form_from;
global $form_radio;
$envt=array();
$envt["form_from"]=$form_from;
$envt["form_radio"]=$form_radio;
return $envt;
}
}// class end