Tripal v1.1 (6.x-1.1)
tripal_featuremap.form.inc
Go to the documentation of this file.
00001 <?php
00008 function chado_featuremap_form($node) {
00009   tripal_core_ahah_init_form();
00010   $form = array();
00011   
00012   $featuremap = $node->featuremap;
00013   $featuremap_id = $featuremap->featuremap_id;
00014   
00015   $d_title        = $form_state['values']['title']       ? $form_state['values']['title']       : $featuremap->name;
00016   $d_description  = $form_state['values']['description'] ? $form_state['values']['description'] : $featuremap->description;
00017   $d_unittype_id  = $form_state['values']['unittype_id'] ? $form_state['values']['unittype_id'] : $featuremap->unittype_id->cvterm_id;
00018 
00019   // on AHAH callbacks we want to keep a list of all the properties that have been removed
00020   // we'll store this info in a hidden field and retrieve it here
00021   $d_removed = $form_state['values']['removed'];
00022 
00023   // get the number of new fields that have been aded via AHAH callbacks
00024   $num_new = $form_state['values']['num_new'] ? $form_state['values']['num_new'] : 0;
00025 
00026   // initialze default properties array. This is where we store the property defaults
00027   $d_properties = array();
00028   
00029   // get the list of unit types
00030   $values = array(
00031     'cv_id' => array(
00032       'name' => 'featuremap_units',
00033     )
00034   );
00035   $columns = array('cvterm_id','name');
00036   $options = array('order_by' => array('name' => 'ASC'));
00037   $featuremap_units = tripal_core_chado_select('cvterm', $columns, $values, $options);
00038   $units = array();
00039   $units[''] = '';
00040   foreach($featuremap_units as $unit) {
00041     $units[$unit->cvterm_id] = $unit->name;
00042   }
00043   
00044   // get the featuremap properties
00045   $properties_select = array();
00046   $properties_select[] = 'Select a Property';
00047   $properties_list = array();
00048   $sql = "
00049     SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
00050     FROM  {cvterm} CVT
00051       INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
00052     WHERE 
00053       CV.name = 'featuremap_property' AND 
00054       NOT CVT.is_obsolete = 1
00055     ORDER BY CVT.name ASC 
00056   ";
00057   $prop_types = chado_query($sql);
00058   while ($prop = db_fetch_object($prop_types)) {
00059     $properties_select[$prop->cvterm_id] = $prop->name;
00060     $properties_list[$prop->cvterm_id] = $prop;
00061   }
00062   
00063   // keep track of the map id if we have.  If we do have one then
00064   // this is an update as opposed to an insert.
00065   $form['featuremap_id'] = array(
00066     '#type' => 'hidden',
00067     '#value' => $featuremap_id,
00068   );
00069 
00070   $form['title']= array(
00071     '#type'          => 'textfield',
00072     '#title'         => t('Map Name'),
00073     '#description'   => t('Please enter a name for this map'),
00074     '#required'      => TRUE,
00075     '#default_value' => $d_title,
00076     '#maxlength'     => 255
00077   );
00078 
00079   $form['description']= array(
00080     '#type'          => 'textarea',
00081     '#title'         => t('Map Description'),
00082     '#description'   => t('A description of the map.'),
00083     '#required'      => TRUE,
00084     '#default_value' => $d_description,
00085   );
00086   
00087 
00088   $form['unittype_id'] = array(
00089     '#title'       => t('Map Units'),
00090     '#type'        => t('select'),
00091     '#description' => t("Chose the units for this map"),
00092     '#required'    => TRUE,
00093     '#default_value' => $d_unittype_id,
00094     '#options'     => $units,
00095   );
00096 
00097   
00098   // add in the properties from the featuremapprop table
00099   $num_properties += chado_featuremap_node_form_add_featuremapprop_table_props($form, $form_state, $featuremap_id, $d_properties, $d_removed);
00100 
00101   // add in any new properties that have been added by the user through an AHAH callback
00102   $num_new = chado_featuremap_node_form_add_new_props($form, $form_state, $d_properties, $d_removed);
00103 
00104   // add an empty row of field to allow for addition of a new property
00105   chado_featuremap_node_form_add_new_empty_props($form, $properties_select);
00106   
00107 
00108   return $form;
00109 }
00115 function chado_featuremap_validate($node, &$form) {
00116   $name          = trim($node->title);
00117   $featuremap_id = trim($node->featuremap_id);
00118   $unittype_id   = trim($node->unittype_id);
00119   $description   = trim($node->description);
00120   $num_properties = $node->num_properties;
00121   $num_new = $node->num_new;
00122   
00123   $featuremap = 0;
00124   // check to make sure the unique name on the map is unique
00125   // before we try to insert into chado. If this is an update then we will
00126   // have a featuremap_id, therefore we want to look for another map with this
00127   // name but with a different featuremap_id. If this is an insert, just look
00128   // for a case where the name already exists.
00129   if ($node->featuremap_id) {
00130     $sql = "
00131       SELECT * FROM {featuremap} 
00132       WHERE name = '%s' AND NOT featuremap_id = %d
00133     ";
00134     $featuremap = db_fetch_object(chado_query($sql, $node->title, $node->featuremap_id));
00135   }
00136   else {
00137     $sql = "SELECT * FROM {featuremap} WHERE name = '%s'";
00138     $featuremap = db_fetch_object(chado_query($sql, $node->title));
00139   }
00140   if ($featuremap) {
00141     form_set_error('name', t('The unique map name already exists. Please choose another'));
00142   }
00143   
00144 }
00145 /*
00146  *
00147  */
00148 function chado_featuremap_node_form_add_new_empty_props(&$form, $properties_select) {
00149 
00150   // add one more blank set of property fields
00151   $form['properties']['new']["new_id"] = array(
00152     '#type'          => 'select',
00153     '#options'       => $properties_select,
00154     '#ahah' => array(
00155       'path'    => "tripal_featuremap/properties/description",
00156       'wrapper' => 'tripal-featuremap-new_value-desc',
00157       'event'   => 'change',
00158       'method'  => 'replace',          
00159   ),
00160   );
00161   $form['properties']['new']["new_value"] = array(
00162     '#type'          => 'textarea',
00163     '#default_value' => '',
00164     '#cols'          => 5,
00165     '#rows'          => $rows,
00166     '#description'   => '<div id="tripal-featuremap-new_value-desc"></div>'
00167     );
00168     $form['properties']['new']["add"] = array(
00169     '#type'         => 'image_button',      
00170     '#value'        => t('Add'),
00171     '#src'          => drupal_get_path('theme', 'tripal') . '/images/add.png',
00172     '#ahah' => array(
00173       'path'    => "tripal_featuremap/properties/add",
00174       'wrapper' => 'tripal-featuremap-edit-properties-table',
00175       'event'   => 'click',
00176       'method'  => 'replace',          
00177     ),
00178     '#attributes' => array('onClick' => 'return false;'),
00179     );
00180 }
00181 /*
00182  *
00183  */
00184 function chado_featuremap_node_form_add_new_props(&$form, $form_state, &$d_properties, &$d_removed) {
00185    
00186   // first, add in all of the new properties that were added through a previous AHAH callback
00187   $j = 0;
00188   $num_properties++;
00189 
00190   // we need to find the
00191   if ($form_state['values']) {
00192     foreach ($form_state['values'] as $element_name => $value) {
00193       if (preg_match('/new_value-(\d+)-(\d+)/', $element_name, $matches)) {
00194         $new_id = $matches[1];
00195         $rank = $matches[2];
00196 
00197         // skip any properties that the user requested to delete through a previous
00198         // AHAH callback or through the current AHAH callback
00199         if($d_removed["$new_id-$rank"]) {
00200           continue;
00201         }
00202         if($form_state['post']['remove-' . $new_id . '-' . $rank]) {
00203           $d_removed["$new_id-$rank"] = 1;
00204           continue;
00205         }
00206 
00207         // get this new_id information
00208         $cvterm = tripal_core_chado_select('cvterm', array('name', 'definition'), array('cvterm_id' => $new_id));
00209 
00210         // add it to the $d_properties array
00211         $d_properties[$new_id][$rank]['name']  = $cvterm->name;
00212         $d_properties[$new_id][$rank]['id']    = $new_id;
00213         $d_properties[$new_id][$rank]['value'] = $value;
00214         $d_properties[$new_id][$rank]['definition']  = $cvterm->definition;
00215         $num_properties++;
00216 
00217         // determine how many rows we need in the textarea
00218         $rows = 1;
00219         if (preg_match('/Abstract/', $cvterm[0]->name)) {
00220           $rows = 10;
00221         }
00222         if ($cvterm[0]->name == 'Authors') {
00223           $rows = 2;
00224         }
00225 
00226         // add the new fields
00227         $form['properties']['new'][$new_id][$rank]["new_id-$new_id-$rank"] = array(
00228           '#type'          => 'item',
00229           '#value'         => $cvterm[0]->name
00230         );
00231         $form['properties']['new'][$new_id][$rank]["new_value-$new_id-$rank"] = array(
00232           '#type'          => 'textarea',
00233           '#default_value' => $value,
00234           '#cols'          => 50,
00235           '#rows'          => $rows,
00236           '#description'   => $cvterm->definition,
00237         );
00238 
00239         $form['properties']['new'][$new_id][$rank]["remove-$new_id-$rank"] = array(
00240           '#type'         => 'image_button',
00241           '#value'        => t('Remove'),
00242           '#src'          => drupal_get_path('theme', 'tripal') . '/images/minus.png',
00243           '#ahah' => array(
00244             'path'    => "tripal_featuremap/properties/minus/$new_id/$rank",
00245             'wrapper' => 'tripal-featuremap-edit-properties-table',
00246             'event'   => 'click',
00247             'method'  => 'replace',
00248         ),
00249           '#attributes' => array('onClick' => 'return false;'),
00250         );
00251       }
00252     }
00253   }
00254 
00255 
00256   // second add in any new properties added during this callback
00257   if($form_state['post']['add']) {
00258     $new_id = $form_state['values']['new_id'];
00259     $new_value = $form_state['values']['new_value'];
00260 
00261     // get the rank by counting the number of entries
00262     $rank = count($d_properties[$new_id]);
00263 
00264     // get this new_id information
00265     $cvterm = tripal_core_chado_select('cvterm', array('name', 'definition'), array('cvterm_id' => $new_id));
00266 
00267     // add it to the $d_properties array
00268     $d_properties[$new_id][$rank]['name']  = $cvterm->name;
00269     $d_properties[$new_id][$rank]['id']    = $new_id;
00270     $d_properties[$new_id][$rank]['value'] = $value;
00271     $d_properties[$new_id][$rank]['definition']  = $cvterm->definition;
00272     $num_properties++;
00273 
00274     // determine how many rows we need in the textarea
00275     $rows = 1;
00276     if (preg_match('/Abstract/', $cvterm[0]->name)) {
00277       $rows = 10;
00278     }
00279     if ($cvterm[0]->name == 'Authors') {
00280       $rows = 2;
00281     }
00282 
00283     // add the new fields
00284     $form['properties']['new'][$new_id][$rank]["new_id-$new_id-$rank"] = array(
00285       '#type'          => 'item',
00286       '#value'         => $cvterm[0]->name
00287     );
00288     $form['properties']['new'][$new_id][$rank]["new_value-$new_id-$rank"] = array(
00289       '#type'          => 'textarea',
00290       '#default_value' => $new_value,
00291       '#cols'          => 50,
00292       '#rows'          => $rows,
00293       '#description'   => $cvterm->definition,
00294     );
00295 
00296     $form['properties']['new'][$new_id][$rank]["remove-$new_id-$rank"] = array(
00297       '#type'         => 'image_button',
00298       '#value'        => t('Remove'),
00299       '#src'          => drupal_get_path('theme', 'tripal') . '/images/minus.png',
00300       '#ahah' => array(
00301         'path'    => "tripal_featuremap/properties/minus/$new_id/$rank",
00302         'wrapper' => 'tripal-featuremap-edit-properties-table',
00303         'event'   => 'click',
00304         'method'  => 'replace',
00305     ),
00306       '#attributes' => array('onClick' => 'return false;'),
00307     );
00308 
00309   }
00310 
00311   return $num_properties;
00312 }
00313 /*
00314  *
00315  */
00316 function chado_featuremap_node_form_add_featuremapprop_table_props(&$form, $form_state, $featuremap_id, &$d_properties, &$d_removed) {
00317 
00318   // get the properties for this featuremap
00319   $num_properties = 0;
00320 
00321   if(!$featuremap_id) {
00322     return $num_properties;
00323   }
00324 
00325   $sql = "
00326     SELECT CVT.cvterm_id, CVT.name, CVT.definition, PP.value, PP.rank
00327     FROM {featuremapprop} PP
00328       INNER JOIN {cvterm} CVT on CVT.cvterm_id = PP.type_id
00329     WHERE PP.featuremap_id = %d
00330     ORDER BY CVT.name, PP.rank
00331   ";
00332   $featuremap_props = chado_query($sql, $featuremap_id);
00333   while ($prop = db_fetch_object($featuremap_props)) {
00334 
00335     $type_id = $prop->cvterm_id;
00336     $rank = count($d_properties[$type_id]);
00337 
00338     // skip any properties that the user requested to delete through a previous
00339     // AHAH callback or through the current AHAH callback
00340     if($d_removed["$type_id-$rank"]) {
00341       continue;
00342     }
00343     if($form_state['post']['remove-' . $type_id . '-' . $rank]) {
00344       $d_removed["$type_id-$rank"] = 1;
00345       continue;
00346     }
00347 
00348     $d_properties[$type_id][$rank]['name']  = $prop->name;
00349     $d_properties[$type_id][$rank]['id']    = $type_id;
00350     $d_properties[$type_id][$rank]['value'] = $prop->value;
00351     $d_properties[$type_id][$rank]['definition']  = $prop->definition;
00352     $num_properties++;
00353 
00354     $form['properties'][$type_id][$rank]["prop_id-$type_id-$rank"] = array(
00355       '#type'          => 'item',
00356       '#value'         => $prop->name,
00357     );
00358     $form['properties'][$type_id][$rank]["prop_value-$type_id-$rank"] = array(
00359       '#type'          => 'textarea',
00360       '#default_value' => $prop->value,
00361       '#cols'          => 50,
00362       '#rows'          => $rows,
00363       '#description'   => $prop->definition,
00364     );
00365 
00366     $form['properties'][$type_id][$rank]["remove-$type_id-$rank"] = array(
00367       '#type'         => 'image_button',
00368       '#value'        => t('Remove'),
00369       '#src'          => drupal_get_path('theme', 'tripal') . '/images/minus.png',
00370       '#ahah' => array(
00371         'path'    => "tripal_featuremap/properties/minus/$type_id/$rank",
00372         'wrapper' => 'tripal-featuremap-edit-properties-table',
00373         'event'   => 'click',
00374         'method'  => 'replace',
00375     ),
00376       '#attributes' => array('onClick' => 'return false;'),
00377     );
00378   }
00379   return $num_properties;
00380 }
00381 /*
00382  *
00383  */
00384 function tripal_featuremap_theme_node_form_properties($form) {
00385   $rows = array();
00386 
00387   if ($form['properties']) {
00388 
00389     // first add in the properties derived from the featuremapprop table
00390     // the array tree for these properties looks like this:
00391     // $form['properties'][$type_id][$rank]["prop_id-$type_id-$rank"]
00392     foreach ($form['properties'] as $type_id => $elements) {
00393       // there are other fields in the properties array so we only
00394       // want the numeric ones those are our type_id
00395       if (is_numeric($type_id)) {
00396         foreach ($elements as $rank => $element) {
00397           if (is_numeric($rank)) {
00398             $rows[] = array(
00399             drupal_render($element["prop_id-$type_id-$rank"]),
00400             drupal_render($element["prop_value-$type_id-$rank"]),
00401             drupal_render($element["remove-$type_id-$rank"]),
00402             );
00403           }
00404         }
00405       }
00406     }
00407 
00408     // second, add in any new properties added by the user through AHAH callbacks
00409     // the array tree for these properties looks like this:
00410     // $form['properties']['new'][$type_id][$rank]["new_id-$new_id-$rank"]
00411     foreach ($form['properties']['new'] as $type_id => $elements) {
00412       if (is_numeric($type_id)) {
00413         foreach ($elements as $rank => $element) {
00414           if (is_numeric($rank)) {
00415             $rows[] = array(
00416             drupal_render($element["new_id-$type_id-$rank"]),
00417             drupal_render($element["new_value-$type_id-$rank"]),
00418             drupal_render($element["remove-$type_id-$rank"]),
00419             );
00420           }
00421         }
00422       }
00423     }
00424 
00425     // finally add in a set of blank field for adding a new property
00426     $rows[] = array(
00427     drupal_render($form['properties']['new']['new_id']),
00428     drupal_render($form['properties']['new']['new_value']),
00429     drupal_render($form['properties']['new']['add']),
00430     );
00431   }
00432 
00433   $headers = array('Property Type','Value', '');
00434   return theme('table', $headers, $rows, array('id'=> "tripal-featuremap-edit-properties-table"));
00435 }
00436 
00437 /*
00438  *
00439  */
00440 function tripal_featuremap_property_add() {
00441   $status = TRUE;
00442 
00443   // prepare and render the form
00444   $form = tripal_core_ahah_prepare_form();
00445 
00446   // we only want to return the properties as that's all we'll replace with this AHAh callback
00447   $data = tripal_featuremap_theme_node_form_properties($form);
00448 
00449   // bind javascript events to the new objects that will be returned
00450   // so that AHAH enabled elements will work.
00451   $settings = tripal_core_ahah_bind_events();
00452 
00453   // return the updated JSON
00454   drupal_json(
00455   array(
00456       'status'   => $status, 
00457       'data'     => $data,
00458       'settings' => $settings,
00459   )
00460   );
00461 }
00462 /*
00463  *
00464  */
00465 function tripal_featuremap_property_delete() {
00466   $status = TRUE;
00467 
00468   // prepare and render the form
00469   $form = tripal_core_ahah_prepare_form();
00470 
00471   // we only want to return the properties as that's all we'll replace with this AHAh callback
00472   $data = tripal_featuremap_theme_node_form_properties($form);
00473 
00474   // bind javascript events to the new objects that will be returned
00475   // so that AHAH enabled elements will work.
00476   $settings = tripal_core_ahah_bind_events();
00477 
00478   // return the updated JSON
00479   drupal_json(
00480   array(
00481       'status'   => $status, 
00482       'data'     => $data,
00483       'settings' => $settings,
00484   )
00485   );
00486 }
00487 /*
00488  *
00489  */
00490 function tripal_featuremap_property_get_description() {
00491   $new_id = $_POST['new_id'];
00492 
00493   $values = array('cvterm_id' => $new_id);
00494   $cvterm = tripal_core_chado_select('cvterm', array('definition'), $values);
00495 
00496   $description = '&nbsp;';
00497   if ($cvterm[0]->definition) {
00498     $description = $cvterm[0]->definition;
00499   }
00500   drupal_json(
00501     array(
00502       'status' => TRUE,
00503       'data'   => '<div id="tripal-featuremap-new_value-desc">' . $description . '</div>',
00504     )
00505   );
00506 }
00507 /*
00508  *
00509  */
00510 function theme_chado_featuremap_node_form($form) {
00511 
00512   $properties_table = tripal_featuremap_theme_node_form_properties($form);
00513 
00514   $markup  = drupal_render($form['featuremap_id']);
00515   $markup .= drupal_render($form['title']);
00516   $markup .= drupal_render($form['unittype_id']);
00517   $markup .= drupal_render($form['description']);
00518   $markup .= "<b>Include Additional Details</b><br>You may add additional properties to this map by scrolling to the bottom of this table, selecting a property type from the dropdown and adding text.  You may add as many properties as desired by clicking the plus button on the right.  To remove a property, click the minus button";
00519   $markup .= $properties_table;
00520   $markup .= drupal_render($form['is_obsolete']);
00521 
00522   $form['properties'] = array(
00523     '#type' => 'markup',
00524     '#value' =>  $markup,
00525   );
00526   return drupal_render($form);
00527 }
 All Classes Files Functions Variables