Tripal v1.0 (6.x-1.0)
tripal_library.module
Go to the documentation of this file.
00001 <?php
00002 
00011 require('api/tripal_library.api.inc');
00012 require('includes/tripal_library.admin.inc');
00013 
00023 function tripal_library_help($path, $arg) {
00024   $output = '';
00025   switch ($path) {
00026     case "admin/help#tripal_library":
00027       $output = '<p>'.
00028       t("Displays links to nodes created on this date") .
00029                '</p>';
00030       break;
00031   }
00032   return $output;
00033 }
00034 
00041 function tripal_library_node_info() {
00042   $nodes = array();
00043   $nodes['chado_library'] = array(
00044     'name' => t('Library'),
00045     'module' => 'chado_library',
00046     'description' => t('A library from the chado database'),
00047     'has_title' => FALSE,
00048     'title_label' => t('Library'),
00049     'has_body' => FALSE,
00050     'body_label' => t('Library Description'),
00051     'locked' => TRUE
00052   );
00053   return $nodes;
00054 }
00055 
00063 function tripal_library_perm() {
00064   return array(
00065     'access chado_library content',
00066     'create chado_library content',
00067     'delete chado_library content',
00068     'edit chado_library content',
00069     'administer tripal libraries',
00070   );
00071 }
00077 function chado_library_access($op, $node, $account) {
00078   if ($op == 'create') {
00079     if (!user_access('create chado_library content', $account)) {
00080       return FALSE;
00081     }
00082   }
00083   if ($op == 'update') {
00084     if (!user_access('edit chado_library content', $account)) {
00085       return FALSE;
00086     }
00087   }
00088   if ($op == 'delete') {
00089     if (!user_access('delete chado_library content', $account)) {
00090       return FALSE;
00091     }
00092   }
00093   if ($op == 'view') {
00094     if (!user_access('access chado_library content', $account)) {
00095       return FALSE;
00096     }
00097   }
00098   return NULL;
00099 }
00107 function tripal_library_menu() {
00108   $items = array();
00109   // The administative settings menu
00110   $items['admin/tripal/tripal_library'] = array(
00111     'title' => 'Libraries',
00112     'description' => 'Basic Description of Tripal Library Module Functionality',
00113     'page callback' => 'theme',
00114     'page arguments' => array('tripal_library_admin'),
00115     'access arguments' => array('administer tripal libraries'),
00116     'type' => MENU_NORMAL_ITEM,
00117   );
00118 
00119   $items['admin/tripal/tripal_library/configuration'] = array(
00120     'title' => 'Library Configuration',
00121     'description' => 'Manage integration of Chado libraries including associated features.',
00122     'page callback' => 'drupal_get_form',
00123     'page arguments' => array('tripal_library_admin'),
00124     'access arguments' => array('administer tripal libraries'),
00125     'type' => MENU_NORMAL_ITEM,
00126   );
00127 
00128   // Synchronizing libraries from Chado to Drupal
00129   $items['chado_sync_libraries'] = array(
00130     'title' => 'Sync Library Data',
00131     'page callback' => 'tripal_library_sync_libraries',
00132     'access arguments' => array('administer tripal libraries'),
00133     'type' => MENU_CALLBACK
00134   );
00135   return $items;
00136 }
00137 
00146 function tripal_library_views_api() {
00147   return array(
00148     'api' => 2.0,
00149   );
00150 }
00151 
00159 function tripal_library_nodeapi(&$node, $op, $teaser, $page) {
00160 
00161   switch ($op) {
00162     // Note that this function only adds library view to an organism/feature
00163     // node.
00164     case 'view':
00165       // add the library to the organism/feature search indexing
00166     if ($node->build_mode == NODE_BUILD_SEARCH_INDEX) {
00167       $node->content['tripal_library_index_version'] = array(
00168     '#value' => theme('tripal_library_search_index', $node),
00169       );
00170     }
00171     elseif ($node->build_mode == NODE_BUILD_SEARCH_RESULT) {
00172       $node->content['tripal_library_index_version'] = array(
00173     '#value' => theme('tripal_library_search_result', $node),
00174       );
00175     }
00176     else {
00177       switch ($node->type) {
00178         case 'chado_organism':
00179           // Show library if the organism/feature is not at teaser view
00180           $node->content['tripal_organism_libraries'] = array(
00181         '#value' => theme('tripal_organism_libraries', $node),
00182           );
00183         break;
00184         case 'chado_feature':
00185           // Show library if the organism/feature is not at teaser view
00186           $node->content['tripal_feature_libraries'] = array(
00187         '#value' => theme('tripal_feature_libraries', $node),
00188           );
00189         break;
00190         }
00191       }
00192     break;
00193   }
00194 }
00195 
00203 function tripal_library_theme() {
00204   return array(
00205     'tripal_library_library_table' => array(
00206        'arguments' => array('libraries'),
00207     ),
00208     'tripal_library_search_index' => array(
00209        'arguments' => array('node'),
00210     ),
00211     'tripal_library_search_result' => array(
00212        'arguments' => array('node'),
00213     ),
00214     'tripal_organism_libraries' => array(
00215        'arguments' => array('node' => NULL),
00216        'template' => 'tripal_organism_libraries',
00217     ),
00218     'tripal_feature_libraries' => array(
00219        'arguments' => array('node' => NULL),
00220        'template' => 'tripal_feature_libraries',
00221     ),
00222     'tripal_library_base' => array(
00223        'arguments' => array('node' => NULL),
00224        'template' => 'tripal_library_base',
00225     ),
00226     'tripal_library_synonyms' => array(
00227        'arguments' => array('node' => NULL),
00228        'template' => 'tripal_library_synonyms',
00229     ),
00230     'tripal_library_references' => array(
00231        'arguments' => array('node' => NULL),
00232        'template' => 'tripal_library_references',
00233     ),
00234     'tripal_library_properties' => array(
00235        'arguments' => array('node' => NULL),
00236        'template' => 'tripal_library_properties',
00237     ),
00238     'tripal_library_terms' => array(
00239        'arguments' => array('node' => NULL),
00240        'template' => 'tripal_library_terms',
00241     ),
00242     'tripal_library_admin' => array(
00243       'template' => 'tripal_library_admin',  
00244       'arguments' =>  array(NULL),  
00245       'path' => drupal_get_path('module', 'tripal_library') . '/theme', 
00246     ),
00247   );
00248 }
00249 
00257 function theme_tripal_library_search_index($node) {
00258 
00259   if ($node->type == 'chado_organism') {
00260     $content = "";
00261     // get the libraries for the organism
00262     $sql = "SELECT * FROM {library} L ".
00263             "WHERE L.organism_id = %d";
00264     $libraries = array();
00265     $results = chado_query($sql, $node->organism->organism_id);
00266     while ($library = db_fetch_object($results)) {
00267       // get the description
00268       $sql = "SELECT * FROM {libraryprop} LP ".
00269              "  INNER JOIN CVTerm CVT ON CVT.cvterm_id = LP.type_id ".
00270             "WHERE LP.library_id = $library->library_id ".
00271             "  AND CVT.name = 'library_description'";
00272       $desc = db_fetch_object(chado_query($sql));
00273       $library->description = $desc->value;
00274       $libraries[] = $library;
00275     }
00276     if (count($libraries) > 0) {
00277       foreach ($libraries as $library) {
00278         $content .= "$library->name ";
00279         $content .= "$library->description";
00280       };
00281     }
00282     // Provide library names to show in a feature page
00283   }
00284   elseif ($node->type == 'chado_feature') {
00285     $content = "";
00286     $organism_id = $node->feature->organism_id;
00287     $sql = "SELECT * FROM {library} L ".
00288            "  INNER JOIN Library_feature LF ON L.library_id = LF.library_id ".
00289             "WHERE LF.feature_id = " . $node->feature->feature_id;
00290     $libraries = array();
00291     $results = chado_query($sql);
00292     while ($library = db_fetch_object($results)) {
00293       $libraries[] = $library;
00294     }
00295     if (count($libraries) > 0) {
00296       $lib_additions = array();
00297       foreach ($libraries as $library) {
00298         $content .= $library->name;
00299       };
00300     }
00301   }
00302   return $content;
00303 }
00304 
00310 function theme_tripal_library_node_libraries($node) {
00311   $content = "";
00312 
00313   // Show library information in a expandable box for a organism page.
00314   // Make sure we have $node->organism_id. In the case of creating a new
00315   // organism, the organism_id is not created until we save. This will cause
00316   // an error when users preview the creation without a $node->organism_id
00317   if ($node->type == 'chado_organism' && $node->organism_id) {
00318     $box_status = variable_get("tripal_library-box-libraries", "menu_off");
00319 
00320     if (strcmp($box_status, "menu_off")==0) {
00321       return get_tripal_library_organism_libraries($node->nid);
00322     }
00323   }
00324   // Provide library names to show in a feature page.
00325   // Make sure we have $node->feature->feature_id or there will be an error
00326   // when a feature is previewed at its creation
00327   elseif ($node->type == 'chado_feature' && $node->feature->feature_id) {
00328     $organism_id = $node->feature->organism_id;
00329     $sql = "SELECT * FROM {library} L ".
00330            " INNER JOIN Library_feature LF ON L.library_id = LF.library_id ".
00331             "WHERE LF.feature_id = " . $node->feature->feature_id;
00332     $libraries = array();
00333     $results = chado_query($sql);
00334     while ($library = db_fetch_object($results)) {
00335       $libraries[] = $library;
00336   }
00337   if (count($libraries) > 0) {
00338     $lib_additions = array();
00339     foreach ($libraries as $library) {
00340       $sql = "SELECT nid FROM {chado_library} WHERE library_id = %d";
00341       $lib_nid = db_result(db_query($sql, $library->library_id));
00342       if ($lib_nid) {
00343         $lib_url = url("node/$lib_nid");
00344       }
00345       $lib_additions[$lib_url] = $library->name;
00346     };
00347     $node->lib_additions = $lib_additions;
00348   }
00349   }
00350   return $content;
00351 }
00352 
00353 
00358 function tripal_library_cron() {
00359 
00360 }
00367 function tripal_library_library_access($op, $node, $account) {
00368   if ($op == 'create') {
00369     if (!user_access('create chado_library content', $account)) {
00370       return FALSE;
00371     }
00372   }
00373 
00374   if ($op == 'update') {
00375     if (!user_access('edit any chado_library content', $account) &&
00376         !user_access('edit own chado_library content', $account)) {
00377         return FALSE;
00378     }
00379     if (user_access('edit own chado_library content', $account) &&
00380       $account->uid != $node->uid) {
00381       return FALSE;
00382     }
00383   }
00384 
00385   if ($op == 'delete') {
00386     if (!user_access('delete any chado_library content', $account) &&
00387       !user_access('delete own chado_library content', $account)) {
00388       return FALSE;
00389     }
00390     if (user_access('delete own chado_library content', $account) &&
00391       $account->uid != $node->uid) {
00392       return FALSE;
00393     }
00394   }
00395   return NULL;
00396 }
00397 
00398 
00405 function chado_library_form($node) {
00406   $form = array();
00407 
00408   $library = $node->library;
00409 
00410   // get the default values
00411   $uniquename = $node->uniquename;
00412   if (!$uniquename) {
00413     $uniquename = $library->uniquename;
00414   }
00415   $library_type = $node->library_type;
00416   if (!$library_type) {
00417     $library_type = $library->type_id->cvterm_id;
00418   }
00419   $organism_id = $node->organism_id;
00420   if (!$organism_id) {
00421     $organism_id = $library->organism_id->organism_id;
00422   }
00423   $library_description = $node->library_description;
00424   if (!$library_description) {
00425     $libprop = tripal_library_get_property($library->library_id, 'library_description');
00426     $library_description = $libprop->value;
00427   }
00428 
00429   // keep track of the library id if we have.  If we do have one then
00430   // this is an update as opposed to an insert.
00431   $form['library_id'] = array(
00432     '#type' => 'value',
00433     '#value' => $library->library_id,
00434   );
00435 
00436   $form['title']= array(
00437     '#type'          => 'textfield',
00438     '#title'         => t('Library Title'),
00439     '#description'   => t('Please enter the title for this library. '.
00440                           'This appears at the top of the library page.'),
00441     '#required'      => TRUE,
00442     '#default_value' => $node->title,
00443     '#weight'        => 1
00444   );
00445 
00446   $form['uniquename']= array(
00447     '#type'          => 'textfield',
00448     '#title'         => t('Unique Library Name'),
00449     '#description'   => t('Please enter a unique name for this library'),
00450     '#required'      => TRUE,
00451     '#default_value' => $uniquename,
00452     '#weight'        => 2
00453   );
00454     
00455   // get the list of library types
00456   $values = array(
00457     'cv_id' => array(
00458       'name' => 'tripal_library_types',
00459     )
00460   );
00461   $columns = array('cvterm_id','name');
00462   $options = array('order_by' => array('name' => 'ASC'));
00463   $lib_types = tripal_core_chado_select('cvterm', $columns, $values, $options);
00464   $types = array();
00465   $types[''] = '';
00466   foreach($lib_types as $type) {
00467     $types[$type->cvterm_id] = $type->name;
00468   }
00469 
00470   $form['library_type'] = array(
00471     '#title'       => t('Library Type'),
00472     '#type'        => t('select'),
00473     '#description' => t("Choose the library type."),
00474     '#required'    => TRUE,
00475     '#default_value' => $library_type,
00476     '#options'     => $types,
00477     '#weight'      => 3
00478   );
00479 
00480   // get the list of organisms
00481   $sql = "SELECT * FROM {Organism}";
00482   $org_rset = chado_query($sql);
00483 
00484   $organisms = array();
00485   $organisms[''] = '';
00486   while ($organism = db_fetch_object($org_rset)) {
00487     $organisms[$organism->organism_id] =
00488     "$organism->genus $organism->species ($organism->common_name)";
00489   }
00490 
00491   $form['organism_id'] = array(
00492    '#title'       => t('Organism'),
00493    '#type'        => t('select'),
00494    '#description' => t("Choose the organism with which this library is ".
00495                        "associated."),
00496    '#required'    => TRUE,
00497    '#default_value' => $organism_id,
00498    '#options'     => $organisms,
00499    '#weight'      => 4,
00500   );
00501 
00502   $form['library_description']= array(
00503     '#type'          => 'textarea',
00504     '#title'         => t('Library Description'),
00505     '#description'   => t('A brief description of the library'),
00506     '#required'      => TRUE,
00507     '#default_value' => $library_description,
00508     '#weight'        => 5
00509   );
00510 
00511   return $form;
00512 }
00518 function chado_library_validate($node) {
00519   $lib = 0;
00520   // check to make sure the unique name on the library is unique
00521   // before we try to insert into chado.
00522   if ($node->library_id) {
00523     $sql = "SELECT * FROM ".
00524            "{Library} WHERE ".
00525            "uniquename = '%s' ".
00526            "AND NOT library_id = %d";
00527     $lib = db_fetch_object(chado_query($sql, $node->uniquename, $node->library_id));
00528   }
00529   else {
00530     $sql = "SELECT * FROM ".
00531            "{Library} ".
00532            "WHERE uniquename = '%s'";
00533     $lib = db_fetch_object(chado_query($sql, $node->uniquename));
00534   }
00535   if ($lib) {
00536     form_set_error('uniquename', t('The unique library name already exists. '.
00537                                   'Please choose another'));
00538   }
00539 }
00547 function chado_library_insert($node) {
00548 
00549   if ($node->library_id) {
00550     $library['library_id'] = $node->library_id;
00551   }
00552   else {    
00553     $values = array(
00554       'name' => $node->title,
00555       'uniquename' => $node->uniquename,
00556       'organism_id' => $node->organism_id,
00557       'type_id' => $node->library_type,
00558     );
00559     $library = tripal_core_chado_insert('library', $values);
00560   }
00561 
00562   if ($library) {
00563      // add the description property
00564     tripal_library_insert_property($library['library_id'], 'library_description', 
00565       $node->library_description);
00566 
00567     // make sure the entry for this feature doesn't already exist in the chado_library table
00568     // if it doesn't exist then we want to add it.
00569     $library_id = chado_get_id_for_node('library', $node) ;
00570     if (!$library_id) {
00571        // next add the item to the drupal table
00572       $sql = "INSERT INTO {chado_library} (nid, vid, library_id) ".
00573              "VALUES (%d, %d, %d)";
00574       db_query($sql, $node->nid, $node->vid, $library['library_id']);
00575     }
00576   }
00577   else {
00578     drupal_set_message(t('Unable to add library.', 'warning'));
00579     watchdog('tripal_library',
00580     'Insert feature: Unable to create library where values: %values',
00581     array('%values' => print_r($values, TRUE)),
00582     WATCHDOG_WARNING
00583     );
00584   }
00585 }
00591 function chado_library_update($node) {
00592   if ($node->revision) {
00593     // there is no way to handle revisions in Chado but leave
00594     // this here just to make not we've addressed it.
00595   }
00596   
00597   $library_id = chado_get_id_for_node('library', $node) ;
00598   // update the library record
00599   $match = array(
00600      'library_id' => $library_id,
00601   );
00602   $values = array(
00603      'name' => $node->title,
00604      'uniquename' => $node->uniquename,
00605      'organism_id' => $node->organism_id,
00606      'type_id' => $node->library_type,
00607   );
00608   $status = tripal_core_chado_update('library', $match, $values);
00609 
00610   tripal_library_update_property($library_id, 'library_description', $node->library_description, 1);
00611 
00612 }
00619 function chado_library_load($node) {
00620   // get the feature details from chado
00621   $library_id = chado_get_id_for_node('library', $node);
00622 
00623   $values = array('library_id' => $library_id);
00624   $library = tripal_core_generate_chado_var('library', $values);
00625 
00626   $additions = new stdClass();
00627   $additions->library = $library;
00628   return $additions;
00629 
00630 }
00637 function chado_library_view($node, $teaser = FALSE, $page = FALSE) {
00638    // use drupal's default node view:
00639   if (!$teaser) {
00640 
00641     $node = node_prepare($node, $teaser);
00642 
00643     // If Hook_view() is called by Hook_form(), we'll only have orgnism_id
00644     // but not genus/species/common_name. We need to get those from chado
00645     // database so they will show up in preview
00646     if (!$node->genus) {
00647       $sql = "SELECT * FROM {organism} WHERE organism_id = %d";
00648       $data = db_fetch_object(chado_query($sql, $node->organism_id));
00649       $node->genus = $data->genus;
00650       $node->species = $data->species;
00651       $node->common_name = $data->common_name;
00652     }
00653   }
00654   return $node;
00655 }
00656 
00661 function chado_library_delete(&$node) {
00662 
00663   $library_id = chado_get_id_for_node('library', $node);
00664   
00665   // if we don't have a library id for this node then this isn't a node of
00666   // type chado_library or the entry in the chado_library table was lost.
00667   if (!$library_id) {
00668     return;
00669   }
00670   
00671   // Remove data from {chado_library}, {node} and {node_revisions} tables of
00672   // drupal database
00673   $sql_del = "DELETE FROM {chado_library} ".
00674             "WHERE nid = %d ".
00675             "AND vid = %d";
00676   db_query($sql_del, $node->nid, $node->vid);
00677   $sql_del = "DELETE FROM {node_revisions} ".
00678             "WHERE nid = %d ".
00679             "AND vid = %d";
00680   db_query($sql_del, $node->nid, $node->vid);
00681   $sql_del = "DELETE FROM {node} ".
00682             "WHERE nid = %d ".
00683             "AND vid = %d";
00684   db_query($sql_del, $node->nid, $node->vid);  
00685 
00686   // Remove data from library and libraryprop tables of chado database as well
00687   chado_query("DELETE FROM {libraryprop} WHERE library_id = %d", $library_id);
00688   chado_query("DELETE FROM {library} WHERE library_id = %d", $library_id);  
00689 }
00690 
00699 function tripal_library_block($op = 'list', $delta = '0', $edit = array()) {
00700   switch ($op) {
00701     case 'list':
00702 
00703     $blocks['libreferences']['info'] = t('Tripal Library Cross References');
00704     $blocks['libreferences']['cache'] = BLOCK_NO_CACHE;
00705 
00706     $blocks['libbase']['info'] = t('Tripal Library Details');
00707     $blocks['libbase']['cache'] = BLOCK_NO_CACHE;
00708     
00709     $blocks['libterms']['info'] = t('Tripal Library Terms');
00710     $blocks['libterms']['cache'] = BLOCK_NO_CACHE;
00711 
00712     $blocks['libsynonyms']['info'] = t('Tripal Library Synonyms');
00713     $blocks['libsynonyms']['cache'] = BLOCK_NO_CACHE;
00714 
00715     $blocks['libproperties']['info'] = t('Tripal Library Properties');
00716     $blocks['libproperties']['cache'] = BLOCK_NO_CACHE;
00717 
00718     $blocks['featurelibs']['info'] = t('Tripal Feature Libraries');
00719     $blocks['featurelibs']['cache'] = BLOCK_NO_CACHE;
00720 
00721     $blocks['orglibs']['info'] = t('Tripal Organism Libraries');
00722     $blocks['orglibs']['cache'] = BLOCK_NO_CACHE;
00723 
00724     return $blocks;
00725 
00726     case 'view':
00727       if (user_access('access chado_library content') and arg(0) == 'node' and is_numeric(arg(1))) {
00728         $nid = arg(1);
00729         $node = node_load($nid);
00730 
00731         $block = array();
00732         switch ($delta) {
00733           case 'libreferences':
00734             $block['subject'] = t('Cross References');
00735             $block['content'] = theme('tripal_library_references', $node);
00736             break;
00737           case 'libbase':
00738             $block['subject'] = t('Library Details');
00739             $block['content'] = theme('tripal_library_base', $node);
00740             break;
00741           case 'libsynonyms':
00742             $block['subject'] = t('Synonyms');
00743             $block['content'] = theme('tripal_library_synonyms', $node);
00744             break;
00745           case 'libproperties':
00746             $block['subject'] = t('Properties');
00747             $block['content'] = theme('tripal_library_properties', $node);
00748             break;
00749           case 'libterms':
00750             $block['subject'] = t('Library Terms');
00751             $block['content'] = theme('tripal_library_terms', $node);
00752             break;
00753           case 'featurelibs':
00754             $block['subject'] = t('Libraries');
00755             $block['content'] = theme('tripal_feature_libraries', $node);
00756             break;
00757           case 'orglibs':
00758             $block['subject'] = t('Libraries');
00759             $block['content'] = theme('tripal_organism_libraries', $node);
00760             break;
00761             default :
00762         }
00763         return $block;
00764       }
00765   }
00766 }
 All Classes Files Functions Variables