2023-01-24 19:00:39 +01:00
< ? php
/*
* To change this license header , choose License Headers in Project Properties .
* To change this template file , choose Tools | Templates
* and open the template in the editor .
*/
/**
* Description of author
*
* @ author may
*/
class author {
//put your code here
protected $PDO ;
private static function _cleanCitation ( & $citation ) {
// make sure it is in form Surname, N. no leading/trailing whitespaces, no double whitespaces,...)
// also remove dot (and add it again, later)
$t = trim ( $citation , " \n \r \t \ v \0 . " );
$parts = explode ( " , " , $t );
foreach ( $parts as $off => $part ) {
$parts [ $off ] = trim ( $part , " \n \r \t \ v \0 . " );
}
$citation = $parts [ 0 ] . ', ' . $parts [ 1 ] . '.' ;
}
// check if we have author from the citation
public static function isAuthor ( $citation ) {
global $PDO ;
author :: _cleanCitation ( $citation );
try {
$stmt = $PDO -> prepare ( " SELECT id FROM l_person where citation=:citation " );
$stmt -> execute ( array ( $citation ));
// this instead of rowCount to make in compatible with non-mysql DB
if ( $r = $stmt -> fetch ( PDO :: FETCH_NUM )) {
return $r [ 0 ];
}
return false ;
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
}
return false ;
}
public static function findOrAdd ( $name1 = " " , $name2 = " " , $surname1 = " " , $surname2 = " " , $citation = " " ) {
$id = author :: isAuthorByName ( $name1 , $name2 , $surname1 , $surname2 , $citation );
if ( $id == false ) {
$id = author :: insertAuthorDetailed ( $name1 , $name2 , $surname1 , $surname2 , $citation );
}
return $id ;
}
/**
* check if we have an author by this name , surname , name2 , surname2 , citation
*
* @ param type $name1
* @ param type $name2
* @ param type $surname1
* @ param type $surname2
* @ param type $citation
* @ return boolean
*/
public static function isAuthorByName ( $name1 = " " , $name2 = " " , $surname1 = " " , $surname2 = " " , $citation = " " ) {
global $PDO ;
$citation = trim ( $citation );
try {
$stmt = $PDO -> prepare ( " SELECT id FROM l_person where first_name=:name1 AND middle_name=:name2 AND last_name=:surname1 AND last_name_2=:surname2 AND citation=:citation " );
$stmt -> execute ( array ( 'name1' => $name1 , 'name2' => $name2 , 'surname1' => $surname1 , 'surname2' => $surname2 , 'citation' => $citation ));
// this instead of rowCount to make in compatible with non-mysql DB
if ( $r = $stmt -> fetch ( PDO :: FETCH_NUM )) {
return $r [ 0 ];
}
else {
unset ( $stmt );
// maybe citation is off?
$stmt = $PDO -> prepare ( " SELECT id FROM l_person where first_name=:name1 AND middle_name=:name2 AND last_name=:surname1 AND last_name_2=:surname2 " );
$stmt -> execute ( array ( 'name1' => $name1 , 'name2' => $name2 , 'surname1' => $surname1 , 'surname2' => $surname2 ));
// this instead of rowCount to make in compatible with non-mysql DB
if ( $r = $stmt -> fetch ( PDO :: FETCH_NUM )) {
return $r [ 0 ];
}
}
return false ;
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
}
return false ;
}
/**
* Insert author by name , surname , name2 , surname2 , citation ( email later : ))
*
* @ param type $name1 first name
* @ param type $name2 middle name
* @ param type $surname1 last name
* @ param type $surname2 last name 2
* @ param type $citation citation
* @ return boolean
*/
public static function insertAuthorDetailed ( $name1 , $name2 , $surname1 , $surname2 , $citation ) {
global $PDO ;
$citation = trim ( $citation );
try {
$stmt = $PDO -> prepare ( " INSERT INTO l_person (first_name, middle_name, last_name, last_name_2, citation) VALUES (:first_name, :middle_name, :last_name, :last_name_2, :citation) " );
$stmt -> execute ( array (
'first_name' => $name1 ,
'middle_name' => $name2 ,
'last_name' => $surname1 ,
'last_name_2' => $surname2 ,
'citation' => $citation
));
return $PDO -> lastInsertId ();
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
return false ;
}
}
// $author is short version from citation
public static function insertAuthor ( $citation ) {
global $PDO ;
author :: _cleanCitation ( $citation );
$nameSurname = explode ( " , " , $citation );
try {
$stmt = $PDO -> prepare ( " INSERT INTO l_person (citation, last_name, first_name, editMe) VALUES (:citation, :last_name, :first_name, 'Y') " );
$stmt -> execute ( array (
'citation' => $citation ,
'last_name' => trim ( $nameSurname [ 0 ]),
'first_name' => trim ( $nameSurname [ 1 ])
));
return $PDO -> lastInsertId ();
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
2023-03-08 21:07:47 +01:00
common :: except ( 'ERR: podatki citata: ' . $citation );
2023-01-24 19:00:39 +01:00
return false ;
}
}
}