/[drupal]/contributions/modules/biblio/biblio.contributors.inc
ViewVC logotype

Diff of /contributions/modules/biblio/biblio.contributors.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.18.2.7 by rjerome, Tue Nov 10 22:09:18 2009 UTC revision 1.18.2.8 by rjerome, Mon Nov 16 02:24:22 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  //$Id: biblio.contributors.inc,v 1.18.2.6 2009/11/09 21:22:48 rjerome Exp $  //$Id: biblio.contributors.inc,v 1.18.2.7 2009/11/10 22:09:18 rjerome Exp $
3  /**  /**
4   * @param $aid   * @param $aid
5   * @return unknown_type   * @return unknown_type
# Line 13  function biblio_get_contributor($aid) { Line 13  function biblio_get_contributor($aid) {
13    return $contributor[$aid];    return $contributor[$aid];
14  }  }
15    
16    function biblio_get_contributor_by_name($name) {
17      $query = db_select('biblio_contributor_data', 'bcd');
18      $query->condition('bcd.name', $name);
19      return $query->execute()->fetchObject();
20    }
21    
22  function biblio_get_first_contributor($vid) {  function biblio_get_first_contributor($vid) {
23    static $contributor = array();    static $contributor = array();
24    if (!isset($contributor[$vid])) {    if (!isset($contributor[$vid])) {
# Line 33  function biblio_get_first_contributor($v Line 39  function biblio_get_first_contributor($v
39   */   */
40  function biblio_load_contributors($vid) {  function biblio_load_contributors($vid) {
41    $contributors = array();    $contributors = array();
42    $query = "SELECT * FROM {biblio_contributor} bc    $query = db_select('biblio_contributor', 'bc', array('fetch' => PDO::FETCH_ASSOC));
43                          INNER JOIN {biblio_contributor_data} bcd ON bc.cid=bcd.cid    $query->join('biblio_contributor_data', 'bcd', 'bc.cid = bcd.cid');
44                          WHERE bc.vid=:vid    $query->fields('bcd');
45                          ORDER BY bc.rank ASC"; // do not change order of presentation    $query->fields('bc');
46      $query->condition('bc.vid', $vid)->orderBy('bc.rank', 'ASC');
47      $result = $query->execute();
48    
   $result = db_query($query, array(':vid' => $vid), array('fetch' => PDO::FETCH_ASSOC));  
49    foreach  ($result as $creator ) {    foreach  ($result as $creator ) {
50      $contributors[$creator['auth_category']][] = $creator;      $contributors[$creator['auth_category']][] = $creator;
51    }    }
52    
53    return $contributors;    return $contributors;
54  }  }
55  /**  /**
# Line 69  function biblio_authors_add_etal (&$auth Line 77  function biblio_authors_add_etal (&$auth
77  function biblio_parse_contributors($contributors) {  function biblio_parse_contributors($contributors) {
78    $result = array();    $result = array();
79    foreach ($contributors as $cat => $authors) {    foreach ($contributors as $cat => $authors) {
80    
81      $etal = array();      $etal = array();
82      foreach ($authors as $author) {      foreach ($authors as $author) {
83        // remove any form of "et al" from name field, because it confuses biblio_parse_author        // remove any form of "et al" from name field, because it confuses biblio_parse_author
# Line 93  function biblio_parse_contributors($cont Line 102  function biblio_parse_contributors($cont
102  }  }
103    
104  function biblio_delete_contributors($node) {  function biblio_delete_contributors($node) {
105    db_query('DELETE FROM {biblio_contributor} WHERE nid = :nid', array(':nid' => $node->nid));    $count = db_delete('biblio_contributor')
106                ->condition('nid', $node->nid)
107                ->execute();
108    biblio_delete_orphan_authors();    biblio_delete_orphan_authors();
109    return;    return $count;
110  }  }
111    
112  function biblio_update_contributors($node) {  function biblio_delete_contributors_revision($node) {
113    _save_contributors($node->biblio_contributors, $node->nid, $node->vid, TRUE);    $count = db_delete('biblio_contributor')
114                ->condition('vid', $node->vid)
115                ->execute();
116    biblio_delete_orphan_authors();    biblio_delete_orphan_authors();
117    return;    return $count;
118    }
119    
120    function biblio_delete_contributor($cid) {
121        db_delete('biblio_contributor')
122          ->condition('cid', $cid)
123          ->execute();
124    
125      return db_delete('biblio_contributor_data')
126                ->condition('cid', $cid)
127                ->execute();
128    
129  }  }
130    function biblio_delete_contributor_revision($cid, $vid) {
131      return db_delete('biblio_contributor')
132              ->condition('cid', $cid)
133              ->condition('vid', $vid)
134              ->execute();
135    
136  function biblio_delete_orphan_authors() {  }
137    if (variable_get('biblio_auto_orphaned_author_delete', 0)) {  
138    function biblio_count_orphan_authors() {
139      return db_query('SELECT COUNT(*) FROM {biblio_contributor_data} bcd WHERE bcd.cid NOT IN (SELECT DISTINCT(bc.cid) FROM {biblio_contributor} bc )')->fetchField();
140    }
141    
142    function biblio_get_orphan_authors() {
143      $authors = array();
144      $result =  db_query('SELECT bcd.* FROM {biblio_contributor_data} bcd WHERE bcd.cid NOT IN (SELECT DISTINCT(bc.cid) FROM {biblio_contributor} bc )');
145      foreach($result as $author) {
146        $authors[] = $author;
147      }
148      return $authors;
149    }
150    
151    function biblio_delete_orphan_authors($force = FALSE) {
152      if (variable_get('biblio_auto_orphaned_author_delete', 0) || $force) {
153      db_query('DELETE FROM {biblio_contributor_data} WHERE cid NOT IN (SELECT DISTINCT(cid) FROM {biblio_contributor})');      db_query('DELETE FROM {biblio_contributor_data} WHERE cid NOT IN (SELECT DISTINCT(cid) FROM {biblio_contributor})');
154    }    }
155  }  }
156    
157  /**  function biblio_insert_contributors($node) {
  * Save $node->biblio_contributors to database  
  * @param $node  
  * @param $update  
  * @return success of database operations  
  */  
 function biblio_insert_contributors($node, $update = FALSE) {  
158    if (empty ($node->biblio_contributors)) return true;    if (empty ($node->biblio_contributors)) return true;
159    return _save_contributors($node->biblio_contributors, $node->nid, $node->vid, $update);    return _save_contributors($node->biblio_contributors, $node->nid, $node->vid);
160  }  }
161    
162    function biblio_update_contributors($node) {
163      _save_contributors($node->biblio_contributors, $node->nid, $node->vid, TRUE);
164      biblio_delete_orphan_authors();
165      return;
166    
167    }
168    
169    function biblio_save_contributor(&$author) {
170      return drupal_write_record('biblio_contributor_data', $author);
171    }
172    
173    function biblio_update_contributor(&$author) {
174      if (!isset($author['cid'])) return false;
175      return drupal_write_record('biblio_contributor_data', $author, 'cid');
176    }
177    
178  /**  /**
179   * Save contributors to the database   * Save contributors to the database
180   * @param $authors   * @param $authors
# Line 134  function _save_contributors($contributor Line 188  function _save_contributors($contributor
188    db_query('DELETE FROM {biblio_contributor} WHERE nid = :nid AND vid = :vid', array(':nid' => $nid, ':vid' => $vid));    db_query('DELETE FROM {biblio_contributor} WHERE nid = :nid AND vid = :vid', array(':nid' => $nid, ':vid' => $vid));
189    foreach ($contributors as $cat => $authors) {    foreach ($contributors as $cat => $authors) {
190      foreach ($authors as $key => $author) {      foreach ($authors as $key => $author) {
191        $rank = (isset($author['rank']) && is_numeric($author['rank'])) ? $author['rank'] : $key;        if(empty($author['lastname'])) {
192        if (isset($author['rank'])) unset($author['rank']); //remove rank from author array because it's not in the biblio_contributor_data table          $author = biblio_parse_author($author, $cat);
193        $auth_type = $author['auth_type'];        }
       unset ($author['auth_type']);  
194        if ($update && !empty($author['cid']))  $author['cid'] = null; // null out the cid so we don't do a global change        if ($update && !empty($author['cid']))  $author['cid'] = null; // null out the cid so we don't do a global change
195        if (empty ($author['cid']) && !empty ($md5))        if (empty ($author['cid']) && isset($author['md5']) && !empty ($md5)) {
196          $author['cid'] = array_search($author['md5'], $md5);          $author['cid'] = array_search($author['md5'], $md5);
197          }
198        if (empty ($author['cid'])) {        if (empty ($author['cid'])) {
199          unset ($author['cid']);          biblio_save_contributor($author);
200          $cid = db_insert('biblio_contributor_data')->fields($author)->execute();          if (empty ($author['cid'])) return false;
         if (!$cid) return false;  
       } else {  
         $cid = $author['cid'];  
201        }        }
202    
203        $link_array = array(        $link_array = array(
204          'nid' => $nid,          'nid' => $nid,
205          'vid' => $vid,          'vid' => $vid,
206          'cid' => $cid,          'cid' => $author['cid'],
207          'rank' => $rank,          'rank' => ((isset($author['rank']) && is_numeric($author['rank'])) ? $author['rank'] : $key),
208          'auth_type' => $auth_type,          'auth_type' => $author['auth_type'],
209          'auth_category' => $cat,          'auth_category' => $cat,
210        );        );
211    
# Line 198  function _save_contributors($contributor Line 250  function _save_contributors($contributor
250   * @return unknown_type   * @return unknown_type
251   */   */
252  function biblio_parse_author($author_array, $cat = 0) {  function biblio_parse_author($author_array, $cat = 0) {
253    
254    if ($cat == 5){    if ($cat == 5){
255      $author_array['firstname'] = '';      $author_array['firstname'] = '';
256      $author_array['initials'] = '';      $author_array['initials'] = '';
# Line 219  function biblio_parse_author($author_arr Line 272  function biblio_parse_author($author_arr
272        // clause rules out things such as author="a{\"{o}}"        // clause rules out things such as author="a{\"{o}}"
273        //        //
274        if (preg_match("/(.*){([^\\\].*)}/", $value, $matches) && !(preg_match("/(.*){\\\.{.*}.*}/", $value, $matches2))) {        if (preg_match("/(.*){([^\\\].*)}/", $value, $matches) && !(preg_match("/(.*){\\\.{.*}.*}/", $value, $matches2))) {
275          $author = split(" ", $matches[1]);          $author = explode(" ", $matches[1]);
276          $surname = $matches[2];          $surname = $matches[2];
277        }        }
278        else {        else {
279          $author = split(" ", $value);          $author = explode(" ", $value);
280          // last of array is surname (no prefix if entered correctly)          // last of array is surname (no prefix if entered correctly)
281          $surname = array_pop($author);          $surname = array_pop($author);
282        }        }
# Line 237  function biblio_parse_author($author_arr Line 290  function biblio_parse_author($author_arr
290      // If $size is 3, we're looking at something like Bush, Jr. III, George W      // If $size is 3, we're looking at something like Bush, Jr. III, George W
291      else {      else {
292        // middle of array is 'Jr.', 'IV' etc.        // middle of array is 'Jr.', 'IV' etc.
293        $appellation = join(' ', array_splice($author, 1, 1));        $appellation = implode(' ', array_splice($author, 1, 1));
294        // first of array is surname (perhaps with prefix)        // first of array is surname (perhaps with prefix)
295        list ($surname, $prefix) = _grabSurname(array_shift($author));        list ($surname, $prefix) = _grabSurname(array_shift($author));
296      }      }
297      $remainder = join(" ", $author);      $remainder = implode(" ", $author);
298      list ($firstname, $initials, $prefix2) = _grabFirstnameInitials($remainder);      list ($firstname, $initials, $prefix2) = _grabFirstnameInitials($remainder);
299      if (!empty ($prefix2))      if (!empty ($prefix2))
300      $prefix .= $prefix2;      $prefix .= $prefix2;
# Line 273  function _md5sum($creator) { Line 326  function _md5sum($creator) {
326  function _grabFirstnameInitials($remainder) {  function _grabFirstnameInitials($remainder) {
327    $prefix = array();    $prefix = array();
328    $firstname = $initials = '';    $firstname = $initials = '';
329    $array = split(" ", $remainder);    $array = explode(" ", $remainder);
330    foreach ($array as $value) {    foreach ($array as $value) {
331      $firstChar = drupal_substr($value, 0, 1);      $firstChar = drupal_substr($value, 0, 1);
332      if ((ord($firstChar) >= 97) && (ord($firstChar) <= 122)){      if ((ord($firstChar) >= 97) && (ord($firstChar) <= 122)){
# Line 285  function _grabFirstnameInitials($remaind Line 338  function _grabFirstnameInitials($remaind
338      }      }
339    }    }
340    if (isset ($initialsArray)) {    if (isset ($initialsArray)) {
341      $initials = join(" ", $initialsArray);      $initials = implode(" ", $initialsArray);
342    }    }
343    if (isset ($firstnameArray)) {    if (isset ($firstnameArray)) {
344      $firstname = join(" ", $firstnameArray);      $firstname = implode(" ", $firstnameArray);
345    }    }
346    if (!empty ($prefix)){    if (!empty ($prefix)){
347      $prefix = join(" ", $prefix);      $prefix = implode(" ", $prefix);
348    }    }
349    return array($firstname,$initials,$prefix);    return array($firstname,$initials,$prefix);
350  }  }
# Line 302  function _grabFirstnameInitials($remaind Line 355  function _grabFirstnameInitials($remaind
355   * @return unknown_type   * @return unknown_type
356   */   */
357  function _grabSurname($input) {  function _grabSurname($input) {
358    $surnameArray = split(" ", $input);    $noPrefix = FALSE;
359    $noPrefix = $surname = FALSE;    $surname = FALSE;
360      $prefix  = FALSE;
361    
362      $surnameArray = explode(" ", $input);
363    
364    foreach ($surnameArray as $value) {    foreach ($surnameArray as $value) {
365      $firstChar = substr($value, 0, 1);      $firstChar = substr($value, 0, 1);
366      if (!$noPrefix && (ord($firstChar) >= 97) && (ord($firstChar) <= 122))      if (!$noPrefix && (ord($firstChar) >= 97) && (ord($firstChar) <= 122)) {
367      $prefix[] = $value;        $prefix[] = $value;
368        }
369      else {      else {
370        $surname[] = $value;        $surname[] = $value;
371        $noPrefix = TRUE;        $noPrefix = TRUE;
372      }      }
373    }    }
374    if ($surname)    if (!empty($surname)) {
375    $surname = join(" ", $surname);      $surname = implode(" ", $surname);
376    if (isset ($prefix)) {    }
377      $prefix = join(" ", $prefix);    if (!empty ($prefix)) {
378      return array(      $prefix = implode(" ", $prefix);
379      $surname,    }
380      $prefix    return array($surname, $prefix);
     );  
   }  
   return array(  
   $surname,  
   FALSE  
   );  
381  }  }
382  /**  /**
383   * @return unknown_type   * @return unknown_type
# Line 333  function _grabSurname($input) { Line 385  function _grabSurname($input) {
385  function _loadMD5() {  function _loadMD5() {
386    static $md5 = array();    static $md5 = array();
387    static $count =0;    static $count =0;
388    $db_count = db_query("SELECT COUNT(*) FROM {biblio_contributor_data}")->fetchCol();;    $db_count = db_query("SELECT COUNT(*) FROM {biblio_contributor_data}")->fetchField();;
389    if ($db_count > $count){    if ($db_count > $count){ //refresh the cached data as some new authors may have been added
390      $count = $db_count;      $count = $db_count;
391      $result = db_query('SELECT md5,cid  FROM {biblio_contributor_data} ');      $result = db_query('SELECT md5,cid  FROM {biblio_contributor_data} ');
392      foreach ($result as $row ) {      foreach ($result as $row ) {

Legend:
Removed from v.1.18.2.7  
changed lines
  Added in v.1.18.2.8

  ViewVC Help
Powered by ViewVC 1.1.3