Tripal v1.0 (6.x-1.0)
tripal_cv_admin.inc
Go to the documentation of this file.
00001 <?php
00008 function tripal_cv_edit_page() {
00009   $output .= drupal_get_form('tripal_cv_select_form');
00010   $output .= '<div id="db-edit-div">Please select a vocabulary above to view or edit</div>';
00011 
00012   return $output;
00013 }
00014 
00022 function tripal_cv_select_form() {
00023 
00024   // get a list of db from chado for user to choose
00025   $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
00026   $results = chado_query($sql);
00027 
00028   $cvs = array();
00029   $cvs[] = '';
00030   while ($cv = db_fetch_object($results)) {
00031     $cvs[$cv->cv_id] = $cv->name;
00032   }
00033 
00034   $form['cvid'] = array(
00035     '#title' => t('Controlled Vocabulary/Ontology Name'),
00036     '#type' => 'select',
00037     '#options' => $cvs,
00038     '#ahah' => array(
00039       'path' => 'admin/tripal/tripal_cv/cv/edit/js',
00040       'wrapper' => 'db-edit-div',
00041       'effect' => 'fade',
00042       'event' => 'change',
00043       'method' => 'replace',
00044     ),
00045   );
00046 
00047   return $form;
00048 }
00049 
00055 function tripal_ajax_cv_edit() {
00056 
00057   // get the database id, build the form and then return the JSON object
00058   $cvid = filter_xss($_POST['cvid']);
00059   $form = drupal_get_form('tripal_cv_edit_form', $cvid);
00060   drupal_json(array('status' => TRUE, 'data' => $form));
00061 
00062 }
00063 
00069 function tripal_cv_edit_form(&$form_state = NULL, $cvid = NULL) {
00070 
00071   $sql = "SELECT * FROM {cv} WHERE cv_id = %d ";
00072   $cv = db_fetch_object(chado_query($sql, $cvid));
00073 
00074   // set the default values.  If there is a value set in the
00075   // form_state then let's use that, otherwise, we'll pull
00076   // the values from the database
00077   $default_db = $form_state['values']['name'];
00078   $default_desc = $form_state['values']['description'];
00079   $default_url = $form_state['values']['url'];
00080   $default_urlprefix = $form_state['values']['urlprefix'];
00081   if (!$default_db) {
00082     $default_cv = $cv->name;
00083   }
00084   if (!$default_desc) {
00085     $default_desc = $cv->definition;
00086   }
00087 
00088   $form['cvid'] = array(
00089     '#type' => 'hidden',
00090     '#value' => $cvid
00091   );
00092 
00093   $form['name']= array(
00094     '#type'          => 'textfield',
00095     '#title'         => t("Controlled Vocabulary name"),
00096     '#description'   => t('Please enter the name for this vocabulary.'),
00097     '#required'      => FALSE,
00098     '#default_value' => $default_cv,
00099     '#weight'        => 1
00100   );
00101 
00102   $form['definition']= array(
00103     '#type'          => 'textarea',
00104     '#title'         => t('Description'),
00105     '#description'   => t('Please enter a description for this vocabulary'),
00106     '#default_value' => $default_desc,
00107     '#weight'        => 2
00108   );
00109 
00110   $form['update'] = array(
00111     '#type'         => 'submit',
00112     '#value'        => t('Update'),
00113     '#weight'       => 5,
00114     '#executes_submit_callback' => TRUE,
00115   );
00116   $form['delete'] = array(
00117     '#type'         => 'submit',
00118     '#value'        => t('Delete'),
00119     '#weight'       => 6,
00120     '#executes_submit_callback' => TRUE,
00121   );
00122 
00123   $form['#redirect'] = 'admin/tripal/tripal_cv';
00124 
00125 
00126   return $form;
00127 }
00128 
00134 function tripal_cv_edit_form_submit($form, &$form_state) {
00135 
00136   $name =  $form_state['values']['name'];
00137   $desc =  $form_state['values']['definition'];
00138   $cvid =  $form_state['values']['cvid'];
00139   $op   =  $form_state['values']['op'];
00140 
00141   if (strcmp($op, 'Update') == 0) {
00142     $sql = "
00143        UPDATE {cv} SET
00144          name = '%s',
00145          definition = '%s'
00146        WHERE cv_id = %d
00147     ";
00148     $db = chado_query($sql, $name, $desc, $cvid);
00149     if ($db) {
00150       drupal_set_message(t("Controlled vocabulary updated"));
00151     }
00152     else {
00153       drupal_set_message(t("Failed to update controlled vocabulary."), 'error');
00154     }
00155   }
00156   if (strcmp($op, 'Delete')==0) {
00157     $sql = "
00158        DELETE FROM {cv}
00159        WHERE cv_id = %d
00160     ";
00161     $db = chado_query($sql, $cvid);
00162     if ($db) {
00163       drupal_set_message(t("Controlled vocabulary deleted"));
00164     }
00165     else {
00166       drupal_set_message(t("Failed to delete controlled vocabulary."), 'error');
00167     }
00168   }
00169 
00170 }
00171 
00172 
00178 function tripal_cv_add_form(&$form_state = NULL) {
00179 
00180   $form['cvid'] = array(
00181     '#type' => 'hidden',
00182     '#value' => $cvid
00183   );
00184 
00185   $form['name']= array(
00186     '#type'          => 'textfield',
00187     '#title'         => t("Controlled Vocabulary name"),
00188     '#description'   => t('Please enter the name for this vocabulary.  This field will be ignored if an OBO file or URL is provided above'),
00189     '#required'      => FALSE,
00190     '#default_value' => $default_cv,
00191     '#weight'        => 1
00192   );
00193 
00194   $form['definition']= array(
00195     '#type'          => 'textarea',
00196     '#title'         => t('Description'),
00197     '#description'   => t('Please enter a description for this vocabulary'),
00198     '#default_value' => $default_desc,
00199     '#weight'        => 2
00200   );
00201 
00202   $form['add'] = array(
00203     '#type'         => 'submit',
00204     '#value'        => t('Add'),
00205     '#weight'       => 5,
00206     '#executes_submit_callback' => TRUE,
00207   );
00208 
00209   $form['#redirect'] = 'admin/tripal/tripal_cv';
00210 
00211   return $form;
00212 }
00213 
00219 function tripal_cv_add_form_submit($form, &$form_state) {
00220 
00221   $name =  $form_state['values']['name'];
00222   $desc =  $form_state['values']['definition'];
00223 
00224   $sql = "
00225     INSERT INTO {cv}
00226      (name,definition)
00227     VALUES
00228      ('%s','%s')
00229   ";
00230   $db = chado_query($sql, $name, $desc);
00231   if ($db) {
00232     drupal_set_message(t("Controlled vocabulary added"));
00233   }
00234   else {
00235     drupal_set_message(t("Failed to add controlled vocabulary."), 'error');
00236   }
00237 
00238 }
00239 
00246 function tripal_cv_cvterm_form(&$form_state, $action = 'add') {
00247   tripal_core_ahah_init_form();
00248   
00249   $form = array();
00250 
00251   // get defaults  
00252   $cv_id = $form_state['values']['cv_id'] ? $form_state['values']['cv_id'] : FALSE;
00253   $name  = $form_state['values']['name'] ? $form_state['values']['name'] : '';
00254   
00255   // if we have a cv_id and a term name then get the rest of the term details
00256   if ($cv_id and $name) {
00257      $values = array(
00258        'cv_id' => $cv_id,
00259        'name' => $name,
00260      );     
00261      $results = tripal_core_chado_select('cvterm', array('*'), $values);
00262      if (!$results or count($results) == 0) {
00263        // we can't find the cvterm so reset the name to blank
00264        $name = '';
00265      }
00266      else {
00267        $cvterm = $results[0];
00268        $definition = $cvterm->definition;
00269        $is_relationshiptype = $cvterm->is_relationshiptype;
00270        $is_obsolete = $cvterm->is_obsolete;
00271        
00272        // now get the database
00273        $values = array('dbxref_id' => $cvterm->dbxref_id);
00274        $results = tripal_core_chado_select('dbxref', array('*'), $values);
00275        $dbxref = $results[0];
00276        $accession = $dbxref->accession;
00277        $db_id = $dbxref->db_id;
00278      }
00279   }
00280   
00281   $values = array();
00282   $columns = array('cv_id', 'name');
00283   $options = array('order_by' => array('name' => 'ASC'));
00284   $results = tripal_core_chado_select('cv', $columns, $values, $options);
00285   $cvs = array();
00286   $cvs[] = '';
00287   foreach ($results as $cv) {
00288     $cvs[$cv->cv_id] = $cv->name;
00289   }
00290 
00291   $form['wrapper-top'] = array(
00292     '#type' => 'markup',
00293     '#value' => '<div id="cvterm-form">', 
00294   );
00295   
00296   $form['form_action'] = array(
00297     '#type' => 'hidden',
00298     '#value' => $action, 
00299   );   
00300   
00301   $form['cv_id'] = array(
00302     '#title' => t('Controlled Vocabulary (Ontology) Name'),
00303     '#type' => 'select',
00304     '#options' => $cvs,
00305     '#required' => TRUE,
00306     '#default_value' => $cv_id,
00307     '#ahah' => array(
00308        'path'    => 'admin/tripal/tripal_cv/cvterm/ahah',
00309        'wrapper' => 'cvterm-form',
00310        'event'   => 'change',
00311        'method'  => 'replace',
00312     ),    
00313   );
00314   
00315   if ($cv_id) {
00316     $form['add_cvterm'] = array(
00317       '#type'           => 'fieldset',
00318       '#title'          => t('Term Details'),
00319       '#prefix'         => '<div id="cvterm-add-div">',
00320       '#suffix'         => '</div>'
00321     );
00322     $description = t('Please enter the name for this vocabulary term.');
00323     if ($action == 'edit') {
00324       $description = t('Enter the name of the term to edit.  This field will update automatically as you type. Click outside of the box after entering the term.');
00325     }
00326     $form['add_cvterm']['name']= array(
00327       '#type'          => 'textfield',
00328       '#title'         => t("Term Name"),
00329       '#description'   => $description,
00330       '#default_value' => $name,
00331       '#required'      => TRUE,
00332     );
00333     if ($action == 'edit') {
00334       if ($name) {
00335         $form['add_cvterm']['name']['#attributes'] = array('readonly' => 'readonly');
00336         $form['add_cvterm']['name']['#description'] = 'The term name cannot be changed. If the name is incorrect, please create a new term and make this one as obsolete.';
00337       } else {
00338         $form['add_cvterm']['name']['#autocomplete_path'] = "admin/tripal/tripal_cv/cvterm/auto_name/$cv_id";
00339         $form['add_cvterm']['name']['#ahah'] = array(
00340            'path'    => 'admin/tripal/tripal_cv/cvterm/ahah',
00341            'wrapper' => 'cvterm-form',
00342            'method'  => 'replace',
00343         );
00344       }
00345     }   
00346 
00347     if ($action == 'add' or $name) { 
00348       
00349       
00350       $form['add_cvterm']['definition']= array(
00351         '#type'          => 'textarea',
00352         '#title'         => t('Description'),
00353         '#description'   => t('Please enter a description for this term'),
00354         '#default_value' => $definition,
00355       );
00356   
00357       $form['add_cvterm']['is_relationshiptype'] = array(
00358         '#type'          => 'checkbox',
00359         '#title'         => t('This term describes a relationship?'),
00360         '#default_value' => $is_relationshiptype,
00361       );
00362   
00363       $form['add_cvterm']['is_obsolete'] = array(
00364         '#type'          => 'checkbox',
00365         '#title'         => t('This term is obsolete?'),
00366         '#default_value' => $is_obsolete,
00367       );
00368   
00369       $values = array();
00370       $columns = array('db_id', 'name');
00371       $options = array('order_by' => array('name' => 'ASC'));
00372       $results = tripal_core_chado_select('db', $columns, $values, $options);
00373       $dbs = array();
00374       $dbs[] = '';
00375       foreach ($results as $db) {
00376         $dbs[$db->db_id] = $db->name;
00377       }
00378       $form['add_cvterm']['db_id'] = array(
00379         '#type'         => 'select',
00380         '#title'         => t('Database'),
00381         '#description'   => t('All terms must be assocated with an external database.
00382                             Please select the external database to associate with
00383                             this term'),
00384         '#options'      => $dbs,
00385         '#default_value' => $db_id,
00386         '#required' => TRUE,
00387       );
00388       if ($action == 'edit') {
00389         // we don't want to allow the user to change the database on an edit.
00390         $form['add_cvterm']['db_id']['#disabled'] = TRUE;
00391         $form['add_cvterm']['db_id']['#description'] = 'The database to which this term belongs cannot be changed.';
00392       }
00393       
00394       $form['add_cvterm']['accession']= array(
00395         '#type'          => 'textfield',
00396         '#title'         => t("Accession"),
00397         '#description'   => t('If this term has an existing accession (unique identifier) in the database 
00398            please enter that here.  If the accession is numeric with a database prefix (e.g. GO:003023), please
00399            enter just the numeric value.  The database prefix will be appended whenever the term is displayed. 
00400            If the accession is not numeric then enter it as is.  If no value is provied, the term name 
00401            provided above will be used as the accession.'),
00402         '#required'      => FALSE,
00403         '#default_value' => $accession,
00404       );
00405       if ($action == 'edit') {
00406         $form['add_cvterm']['accession']['#attributes'] = array('readonly' => 'readonly');
00407         $form['add_cvterm']['accession']['#description'] = 'Cannot change the term accession.';
00408       }
00409       $button_text = 'Add Term';
00410       if ($action == 'edit') {
00411         $button_text = 'Update Term';
00412       }
00413       $form['add_cvterm']['submit'] = array(
00414         '#type'  => 'submit',
00415         '#value' => $button_text,
00416       );
00417     } // end if name selected (or action == 'add')
00418   } //end of if cv selected
00419     
00420   
00421   $form['wrapper-bottom'] = array(
00422     '#type' => 'markup',
00423     '#value' => '</div>',
00424   );
00425 
00426   return $form;
00427 }
00428 
00429 /*
00430  * @ingroup tripal_cv
00431  */
00432 function tripal_cv_cvterm_name_autocomplete($cv_id, $string = '') {
00433   $sql = "SELECT cvterm_id, name FROM cvterm WHERE cv_id = %d and name like '%s%%' ORDER by name";
00434   $results = chado_query($sql, $cv_id, $string);
00435   $items = array();
00436   while($term = db_fetch_object($results)) {
00437      $items[$term->name] = $term->name;
00438   }  
00439   drupal_json($items);   
00440 }
00446 function tripal_cv_cvterm_form_validate($form, &$form_state) {
00447 
00448   // Ensure that submit does not get called unless the AHAH in the form was called
00449   if (!empty($form_state['ahah_submission'])) {
00450     return;
00451   }
00452 
00453 }
00454 
00460 function tripal_cv_cvterm_form_submit($form, &$form_state) {
00461 
00462   // Ensure the AHAH in the form was called
00463   if (!empty($form_state['ahah_submission'])) {
00464     return;
00465   }
00466     
00467   // get the database
00468   $values = array('db_id' => $form_state['values']['db_id']);
00469   $results = tripal_core_chado_select('db', array('name'), $values);
00470   if (!$results or count($results) == 0){
00471     drupal_set_message(t('Unable to add term.  Cannot find the database.'), 'error');
00472     return;
00473   }
00474   $db = $results[0];
00475   
00476   // get the cv
00477   $values = array('cv_id' => $form_state['values']['cv_id']);
00478   $results = tripal_core_chado_select('cv', array('name'), $values);
00479   if (!$results or count($results) == 0){
00480     drupal_set_message(t('Unable to add term.  Cannot find the vocabulary.'), 'error');
00481     return;
00482   }
00483   $cv = $results[0];
00484   
00485   // get the accession for this term
00486   $accession = $form_state['values']['accession'];
00487   if (!$accession) {
00488     $accession = $form_state['values']['name'];
00489   }  
00490   if (is_numeric($accession)) {
00491     $accession = $db->name . ":" . $accession;
00492   }
00493   
00494   
00495   $update = 0;
00496   if ($form_state['values']['form_action'] == 'edit') {
00497     $update = 1;
00498   }
00499   
00500   // now add the term
00501   $term = array(    
00502     'name' => $form_state['values']['name'],
00503     'namespace' => $cv->name,
00504     'id' => $accession,
00505     'def' => $form_state['values']['definition'],
00506     'is_obsolete' => $form_state['values']['is_obsolete'],
00507   );  
00508   
00509   $is_relationship = $form_state['values']['is_relationshiptype'];
00510   $cvterm = tripal_cv_add_cvterm($term, $cv->name, $is_relationship, $update, $db->name);
00511   if ($cvterm) {
00512     if (!$update) {
00513       drupal_set_message('Term added successfully.');
00514     }
00515     else {
00516       drupal_set_message('Term updated successfully.');
00517     }
00518   } 
00519   else {
00520     drupal_set_message('Could not add term. Check Drupal recent logs for error messages.', 'error');  
00521   }
00522 
00523 }
00524 
00532 function tripal_cv_cvterm_callback() {
00533   $status = TRUE;
00534 
00535   // prepare and render the form
00536   $form = tripal_core_ahah_prepare_form();   
00537   $data = drupal_render($form);  
00538 
00539   // bind javascript events to the new objects that will be returned 
00540   // so that AHAH enabled elements will work.
00541   $settings = tripal_core_ahah_bind_events();
00542    
00543   // return the updated JSON
00544   drupal_json(
00545     array(
00546       'status'   => $status, 
00547       'data'     => $data,
00548       'settings' => $settings,
00549     )  
00550   );
00551 }
00552 
00558 function tripal_cv_cvtermpath_form_submit($form, &$form_state) {
00559   global $user;
00560 
00561   $cvid =  $form_state['values']['cvid'];
00562 
00563   // first get the controlled vocabulary name:
00564   $cv = db_fetch_object(chado_query("SELECT * FROM {cv} WHERE cv_id = %d", $cvid));
00565 
00566   // Submit a job to update cvtermpath
00567   $job_args = array($cvid);
00568   if ($form_state['values']['op'] == t('Update cvtermpath')) {
00569     tripal_add_job("Update cvtermpath: $cv->name", 'tripal_cv',
00570        'tripal_cv_update_cvtermpath', $job_args, $user->uid);
00571   }
00572 }
 All Classes Files Functions Variables