Tripal v1.0 (6.x-1.0)
tripal_stock-properties.inc
Go to the documentation of this file.
00001 <?php
00012 function tripal_stock_add_ALL_property_page($node) {
00013   $output = '';
00014 
00015   $output .= tripal_stock_add_chado_properties_progress('properties') . '<br />';
00016   $output .= '<b>All Properties should strictly pertain to THE CURRENT Individual</b><br />';
00017   $output .= '<br />';
00018   $output .= theme('tripal_stock_properties', $node);
00019   $output .= '<br /><br />';
00020   $output .= drupal_get_form('tripal_stock_add_ONE_property_form', $node);
00021   $output .= '<br />';
00022   $output .= drupal_get_form('tripal_stock_add_chado_properties_navigate', 'properties', $node->nid);
00023   return $output;
00024 }
00025 
00031 function tripal_stock_add_ONE_property_form($form_state, $node) {
00032   $form = array();
00033   $stock_id = $node->stock->stock_id;
00034 
00035   $form['add_properties'] = array(
00036     '#type' => 'fieldset',
00037     '#title' => t('Add Property') . '<span class="form-optional" title="This field is optional"> (optional)</span>',
00038   );
00039 
00040   $form['prop_nid'] = array(
00041     '#type' => 'hidden',
00042     '#value' => $node->nid
00043   );
00044 
00045   $tmp_obj = tripal_cv_get_cvterm_by_name('synonym', variable_get('chado_stock_prop_types_cv', 'null'));
00046   $synonym_id = $tmp_obj->cvterm_id;
00047 
00048   $prop_type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_prop_types_cv', 'null') );
00049   $prop_type_options[0] = 'Select a Type';
00050   ksort($prop_type_options);
00051   $form['add_properties']['prop_type_id'] = array(
00052     '#type' => 'select',
00053     '#title' => t('Type of Property'),
00054     '#options' => $prop_type_options,
00055   );
00056 
00057   $form['add_properties']['prop_value'] = array(
00058     '#type' => 'textfield',
00059     '#title' => t('Value') . '<span class="form-optional" title="This field is optional">+</span>',
00060   );
00061 
00062   $form['add_properties']['preferred_synonym'] = array(
00063     '#type' => 'checkbox',
00064     '#title' => t('Preferred Synonym (only applicable if type is synonym)'),
00065   );
00066 
00067   $form['add_properties']['prop_stock_id'] = array(
00068     '#type' => 'value',
00069     '#value' => $stock_id,
00070     '#required' => TRUE
00071   );
00072 
00073   $form['add_properties']['submit-add'] = array(
00074     '#type' => 'submit',
00075     '#value' => t('Add Property')
00076   );
00077 
00078   return $form;
00079 }
00080 
00086 function tripal_stock_add_ONE_property_form_validate($form, &$form_state) {
00087 
00088   // Only Require if Adding Property
00089   if ($form_state['clicked_button']['#value'] == t('Add Property') ) {
00090 
00091     // Check that there is a stock
00092     if ( $form_state['values']['prop_stock_id'] <= 0 ) {
00093       form_set_error('prop_stock_id', 'There is no associated stock.');
00094     }
00095 
00096     // Check that Selected a type
00097     if ( $form_state['values']['prop_type_id'] == 0) {
00098       form_set_error('prop_type_id', 'Please select a type of property.');
00099     }
00100     else {
00101       // Check that type is in chado
00102       $num_rows = db_fetch_object(chado_query("SELECT count(*) as count FROM {cvterm} WHERE cvterm_id=%d", $form_state['values']['prop_type_id']));
00103       if ( $num_rows->count != 1) {
00104         form_set_error('prop_type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)");
00105       } // end of if more or less than 1 row
00106     } // if no prop type
00107 
00108     // only check preferred synonym if type is synonym
00109     if ($form_state['values']['preferred_synonym'] == 1) {
00110       $tmp_obj = tripal_cv_get_cvterm_by_name('synonym', variable_get('chado_stock_prop_types_cv', 'null'));
00111       if ($form_state['values']['prop_type_id'] != $tmp_obj->cvterm_id) {
00112         form_set_error('preferred_synonym', 'Preferred Synonym Checkbox Only Applicable if Type of Property is Synonym');
00113       }
00114     }
00115 
00116   } // if add Property
00117 
00118 }
00119 
00125 function tripal_stock_add_ONE_property_form_submit($form, &$form_state) {
00126 
00127   // if there is a property add it (only won't be a property if clicked next step w/ no property)
00128   if ($form_state['values']['prop_type_id'] != 0) {
00129     //determine the rank for this property
00130     $max_rank = get_max_chado_rank('stockprop',
00131                                   array('stock_id' => array('type' => 'INT', 'value' => $form_state['values']['prop_stock_id']),
00132                                         'type_id' => array('type' => 'INT', 'value' => $form_state['values']['prop_type_id']) ));
00133     if ($max_rank == -1) {
00134     $rank = 0;
00135     }
00136     else { $rank = $max_rank+1; }
00137 
00138     chado_query(
00139       "INSERT INTO {stockprop} (stock_id, type_id, value, rank) VALUES (%d, %d, '%s', %d)",
00140       $form_state['values']['prop_stock_id'],
00141       $form_state['values']['prop_type_id'],
00142       $form_state['values']['prop_value'],
00143       $rank
00144     );
00145 
00146     drupal_set_message(t("Successfully Added Property"));
00147 
00148     // Set Preferred Synonym
00149     if ($form_state['values']['preferred_synonym'] == 1) {
00150 
00151       //use update node form so that both title and name get set
00152       $node = node_load($form_state['values']['prop_nid']);
00153       $node->title = $form_state['values']['prop_value'];
00154       $node_form_state = array(
00155         'values' => array(
00156           'title' => $form_state['values']['prop_value'],
00157           'op' => 'Save'
00158         )
00159       );
00160       module_load_include('inc', 'node', 'node.pages');
00161       drupal_execute('chado_stock_node_form', $node_form_state, $node);
00162 
00163     }
00164 
00165   } //end of if property to add
00166 }
00167 
00173 function tripal_stock_edit_ALL_properties_page($node) {
00174   $output = '';
00175 
00176   $output .= drupal_get_form('tripal_stock_edit_ALL_properties_form', $node);
00177   $output .= '<br />';
00178   $output .= drupal_get_form('tripal_stock_add_ONE_property_form', $node);
00179   $output .= '<br />';
00180   $output .= drupal_get_form('tripal_stock_back_to_stock_button', $node->nid);
00181 
00182   return $output;
00183 }
00184 
00190 function tripal_stock_edit_ALL_properties_form($form_state, $node) {
00191   $form = array();
00192 
00193   // Add properties and synonyms
00194   $node = tripal_core_expand_chado_vars($node, 'table', 'stockprop');
00195 
00196   $form['nid'] = array(
00197     '#type' => 'hidden',
00198     '#value' => $node->nid
00199   );
00200 
00201   $i=0;
00202   if (!$node->stock->stockprop) {
00203     $node->stock->stockprop = array();
00204   }
00205   elseif (!is_array($node->stock->stockprop)) {
00206     $node->stock->stockprop = array($node->stock->stockprop);
00207   }
00208   if (sizeof($node->stock->stockprop) != 0) {
00209   foreach ($node->stock->stockprop as $property) {
00210     $i++;
00211     $form["num-$i"] = array(
00212       '#type' => 'item',
00213       '#value' => $i . '.'
00214     );
00215 
00216     $form["id-$i"] = array(
00217       '#type' => 'hidden',
00218       '#value' => $property->stockprop_id
00219     );
00220 
00221     $prop_type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_prop_types_cv', 'null') );
00222     ksort($prop_type_options);
00223     $form["type-$i"] = array(
00224       '#type' => 'select',
00225       //'#title' => t('Type of Property'),
00226       '#options' => $prop_type_options,
00227       '#default_value' => $property->type_id->cvterm_id
00228     );
00229 
00230     $form["value-$i"] = array(
00231       '#type' => 'textfield',
00232       //'#title' => t('Value'),
00233       '#default_value' => $property->value
00234     );
00235 
00236     $form["submit-$i"] = array(
00237       '#type' => 'submit',
00238       '#value' => t("Delete #$i")
00239     );
00240 
00241   }} //end of foreach property
00242 
00243   $form['num_properties'] = array(
00244     '#type' => 'hidden',
00245     '#value' => $i
00246   );
00247 
00248   $form["submit-edits"] = array(
00249     '#type' => 'submit',
00250     '#value' => t('Update Properties')
00251   );
00252 
00253   return $form;
00254 }
00255 
00261 function tripal_stock_edit_ALL_properties_form_submit($form, &$form_state) {
00262 
00263   if ($form_state['clicked_button']['#value'] == t('Update Properties') ) {
00264     //Update all
00265     for ($i=1; $i<=$form_state['values']['num_properties']; $i++) {
00266       tripal_stock_update_property($form_state['values']["id-$i"], $form_state['values']["type-$i"], $form_state['values']["value-$i"], $form_state['values']["preferred-$i"], $form_state['values']["nid"]);
00267     }
00268     drupal_set_message(t("Updated all Properties"));
00269     drupal_goto('node/' . $form_state['values']['nid']);
00270   }
00271   elseif ( preg_match('/Delete #(\d+)/', $form_state['clicked_button']['#value'], $matches) ) {
00272     $i = $matches[1];
00273     tripal_stock_delete_property($form_state['values']["id-$i"], $form_state['values']["type-$i"], $form_state['values']["value-$i"]);
00274     drupal_set_message(t("Deleted Property"));
00275   }
00276   else {
00277     drupal_set_message(t("Unrecognized Button Pressed"), 'error');
00278   }
00279 
00280 
00281 }
00282 
00288 function tripal_stock_update_property($stockprop_id, $cvterm_id, $value, $preferred, $nid) {
00289 
00290   $old_obj = db_fetch_object(chado_query("SELECT * FROM {stockprop} WHERE stockprop_id=%d", $stockprop_id));
00291 
00292   // if they changed the type need to check rank
00293   //   (if there is another property of the same type then rank needs to be increased to prevent collisions)
00294   if ($cvterm_id == $old_obj->type_id) {
00295     chado_query(
00296       "UPDATE {stockprop} SET type_id=%d, value='%s' WHERE stockprop_id=%d",
00297       $cvterm_id,
00298       $value,
00299       $stockprop_id
00300     );
00301   }
00302   else {
00303       //determine the rank for this property
00304     $max_rank = get_max_chado_rank('stockprop',
00305                                   array('stock_id' => array('type' => 'INT', 'value' => $old_obj->stock_id),
00306                                         'type_id' => array('type' => 'INT', 'value' => $cvterm_id ) ));
00307     if ($max_rank == -1) {
00308       $rank = 0;
00309     }
00310     else { $rank = $max_rank+1; }
00311       chado_query(
00312         "UPDATE {stockprop} SET type_id=%d, value='%s', rank=%d WHERE stockprop_id=%d",
00313         $cvterm_id,
00314         $value,
00315         $rank,
00316         $stockprop_id
00317      );
00318   }
00319 
00320   // Set Preferred Synonym
00321   //use update node form so that both title and name get set
00322   if ($preferred) {
00323     $node = node_load($nid);
00324     $node->title = $value;
00325     $node_form_state = array(
00326       'values' => array(
00327         'title' => $value,
00328         'op' => 'Save'
00329       )
00330     );
00331     module_load_include('inc', 'node', 'node.pages');
00332     drupal_execute('chado_stock_node_form', $node_form_state, $node);
00333   }
00334 }
00335 
00341 function tripal_stock_delete_property($stockprop_id) {
00342 
00343   chado_query(
00344     "DELETE FROM {stockprop} WHERE stockprop_id=%d",
00345     $stockprop_id
00346   );
00347 
00348 }
00349 
00355 function theme_tripal_stock_edit_ALL_properties_form($form) {
00356   $output = '';
00357 
00358   $output .= '<br /><fieldset>';
00359   $output .= '<legend>Edit Already Existing Properties<span class="form-optional" title="This field is optional">(optional)</span></legend>';
00360   $output .= '<p>Below is a list of already existing properties for this stock, one property per line. The type refers to the type of '
00361          .'property and the value is the value for that property. For example, if this stock has a seed coat colour of green then '
00362        .'the property type=sead coat colour and the value=green. When the type of property is synonym, there is an extra checkbox '
00363        .'allowing you to specify which is the <b>Preferred Synonym</b>. This will change the current name of the stock.</p>';
00364   $output .= '<table>';
00365   $output .= '<tr><th>#</th><th>Type</th><th>Value</th><th></th></tr>';
00366 
00367   for ($i=1; $i<=$form['num_properties']['#value']; $i++) {
00368     $output .= '<tr><td>' . drupal_render($form["num-$i"]) . '</td><td>' . drupal_render($form["type-$i"]) . '</td><td>' . drupal_render($form["value-$i"]) . drupal_render($form["preferred-$i"]) . '</td><td>' . drupal_render($form["submit-$i"]) . '</td></tr>';
00369   }
00370 
00371   $output .= '</table><br />';
00372   $output .= drupal_render($form);
00373   $output .= '</fieldset>';
00374 
00375   return $output;
00376 }
00377 
00383 function tripal_stock_list_properties_for_node($properties, $synonyms) {
00384 
00385   if (!empty($properties) OR !empty($synonyms) ) {
00386     $output = '<table>';
00387     $output .= '<tr><th>Type</th><th>Value</th></tr>';
00388 
00389     if (!empty($synonyms) ) {
00390       foreach ($synonyms as $s) {
00391         $output .= '<tr><td>synonym</td><td>' . $s . '</td></tr>';
00392       }
00393     }
00394 
00395     if (!empty($properties) ) {
00396       foreach ($properties as $p) {
00397         $output .= '<tr><td>' . $p->type . '</td><td>' . $p->value . '</td></tr>';
00398       } // end of foreach property
00399     }
00400 
00401     $output .= '</table>';
00402 
00403   }
00404   else {
00405     $output = 'No Properties Added to the Current Stock';
00406   }
00407 
00408   return $output;
00409 }
 All Classes Files Functions Variables