Tripal v1.0 (6.x-1.0)
|
00001 <?php 00012 function tripal_feature_edit_ALL_properties_page($node) { 00013 $output = ''; 00014 00015 // get the list of properties for this feature 00016 $values = array('feature_id' => $node->feature->feature_id); 00017 $options = array('order_by' => array('type_id' => 'ASC', 'rank' => 'ASC')); 00018 $properties = tripal_core_generate_chado_var('featureprop', $values, $options); 00019 $properties = tripal_core_expand_chado_vars($properties, 'field', 'featureprop.value'); 00020 00021 $expand_add = (sizeof($properties)) ? FALSE : TRUE; 00022 00023 // add the appopriate form sections 00024 $output .= drupal_get_form('tripal_feature_add_ONE_property_form', $node, $expand_add); 00025 $output .= drupal_get_form('tripal_feature_edit_ALL_properties_form', $node, $properties); 00026 $output .= drupal_get_form('tripal_feature_implement_back_to_feature_button', $node->nid); 00027 00028 return $output; 00029 } 00030 00036 function tripal_feature_add_ONE_property_form($form_state, $node, $expand) { 00037 $form = array(); 00038 $feature_id = $node->feature->feature_id; 00039 00040 $form['add_properties'] = array( 00041 '#type' => 'fieldset', 00042 '#title' => t('Add Property'), 00043 '#collapsible' => TRUE, 00044 '#collapsed' => ($expand) ? FALSE : TRUE, 00045 ); 00046 00047 $form['prop_nid'] = array( 00048 '#type' => 'hidden', 00049 '#value' => $node->nid 00050 ); 00051 00052 $form['add_properties']['feature_id'] = array( 00053 '#type' => 'value', 00054 '#value' => $feature_id, 00055 '#required' => TRUE 00056 ); 00057 00058 // right now this defaults to the 'feature_property' CV 00059 // but in the future it should be more flexible 00060 $form['cv_name'] = array( 00061 '#type' => 'hidden', 00062 '#value' => 'feature_property' 00063 ); 00064 00065 // get the list of property types 00066 $prop_type_options = array(); 00067 $columns = array('cvterm_id', 'name'); 00068 $values = array( 00069 'cv_id' => array( 00070 'name' => $form['cv_name']['#value'], 00071 ) 00072 ); 00073 $results = tripal_core_chado_select('cvterm', $columns, $values); 00074 foreach ($results as $r) { 00075 $prop_type_options[$r->name] = $r->name; 00076 } 00077 00078 $form['add_properties']['property'] = array( 00079 '#type' => 'select', 00080 '#title' => t('Type of Property'), 00081 '#options' => $prop_type_options, 00082 ); 00083 00084 $form['add_properties']['prop_value'] = array( 00085 '#type' => 'textfield', 00086 '#title' => t('Value'), 00087 ); 00088 00089 $form['add_properties']['submit-add'] = array( 00090 '#type' => 'submit', 00091 '#value' => t('Add Property') 00092 ); 00093 00094 return $form; 00095 } 00096 00102 function tripal_feature_add_ONE_property_form_validate($form, &$form_state) { 00103 00104 // Only Require if Adding Property 00105 if ($form_state['clicked_button']['#value'] == t('Add Property') ) { 00106 00107 // Check that there is a feature 00108 if ( $form_state['values']['feature_id'] <= 0 ) { 00109 form_set_error('feature_id', 'There is no associated feature.'); 00110 } 00111 00112 // Check that Selected a type 00113 if ( !$form_state['values']['property']) { 00114 form_set_error('property', 'Please select a type of property.'); 00115 } 00116 } 00117 } 00118 00124 function tripal_feature_add_ONE_property_form_submit($form, &$form_state) { 00125 $feature_id = $form_state['values']['feature_id']; 00126 $property = $form_state['values']['property']; 00127 $value = $form_state['values']['prop_value']; 00128 $cv_name = $form_state['values']['cv_name']; 00129 00130 $succes = tripal_feature_insert_property($feature_id, $property, $value, 0, $cv_name); 00131 if ($succes) { 00132 drupal_set_message(t("Successfully Added Property: %property => %value", array('%property' => $property), array('%value' => $value))); 00133 } 00134 else { 00135 drupal_set_message(t("Failed to Add Property: %property => %value", array('%property' => $property), array('%value' => $value))); 00136 } 00137 } 00138 00145 function tripal_feature_edit_ALL_properties_form($form_state, $node, $properties) { 00146 $form = array(); 00147 $feature_id = $node->feature->feature_id; 00148 00149 $form['nid'] = array( 00150 '#type' => 'hidden', 00151 '#value' => $node->nid 00152 ); 00153 00154 $form['add_properties']['feature_id'] = array( 00155 '#type' => 'value', 00156 '#value' => $feature_id, 00157 '#required' => TRUE 00158 ); 00159 00160 // right now this defaults to the 'feature_property' CV 00161 // but in the future it should be more flexible 00162 $form['cv_name'] = array( 00163 '#type' => 'hidden', 00164 '#value' => 'feature_property' 00165 ); 00166 00167 if (sizeof($properties)) { 00168 00169 // build the select box options for the property name 00170 $prop_type_options = array(); 00171 $columns = array('cvterm_id', 'name'); 00172 $values = array( 00173 'cv_id' => array( 00174 'name' => $form['cv_name']['#value'] 00175 ) 00176 ); 00177 $results = tripal_core_chado_select('cvterm', $columns, $values); 00178 foreach ($results as $r) { 00179 $prop_type_options[$r->name] = $r->name; 00180 } 00181 00182 // iterate through all of the properties and create a set of form elements 00183 foreach ($properties as $i => $property) { 00184 $form["num-$i"] = array( 00185 '#type' => 'fieldset', 00186 '#value' => "Property $i" 00187 ); 00188 $form["num-$i"]["id-$i"] = array( 00189 '#type' => 'hidden', 00190 '#value' => $property->featureprop_id 00191 ); 00192 $default = array_search($property->type, $prop_type_options); 00193 $form["num-$i"]["type-$i"] = array( 00194 '#type' => 'select', 00195 '#options' => $prop_type_options, 00196 '#default_value' => $property->type_id->name 00197 ); 00198 $form["num-$i"]["value-$i"] = array( 00199 '#type' => 'textfield', 00200 '#default_value' => $property->value 00201 ); 00202 $form["num-$i"]["delete-$i"] = array( 00203 '#type' => 'submit', 00204 '#value' => t("Delete"), 00205 '#name' => "delete-$i", 00206 ); 00207 } //end of foreach property 00208 00209 $form['num_properties'] = array( 00210 '#type' => 'hidden', 00211 '#value' => $i 00212 ); 00213 00214 $form["submit-edits"] = array( 00215 '#type' => 'submit', 00216 '#value' => t('Update All Properties') 00217 ); 00218 } 00219 00220 return $form; 00221 } 00222 00228 function tripal_feature_edit_ALL_properties_form_submit($form, &$form_state) { 00229 00230 $cv_name = $form_state['values']['cv_name']; 00231 $feature_id = $form_state['values']["feature_id"]; 00232 $all_good = 1; 00233 00234 // if the update button was clicked then do the update 00235 if ($form_state['clicked_button']['#value'] == t('Update All Properties') ) { 00236 // iterate through each of the properties and set each one 00237 for ($i=1; $i<=$form_state['values']['num_properties']; $i++) { 00238 $featureprop_id = $form_state['values']["id-$i"]; 00239 $property = $form_state['values']["type-$i"]; 00240 $value = $form_state['values']["value-$i"]; 00241 $success = tripal_feature_update_property_by_id($featureprop_id, $property, $value, $cv_name); 00242 if (!$success) { 00243 drupal_set_message(t("Failed to Update Property: %property => %value", array('%property' => $property), array('%value' => $value))); 00244 $all_good = 0; 00245 } 00246 } 00247 if ($all_good) { 00248 drupal_set_message(t("Updated all Properties")); 00249 } 00250 drupal_goto('node/' . $form_state['values']['nid']); 00251 } 00252 // if the delete button was clicked then remove the property 00253 elseif (preg_match('/delete-(\d+)/', $form_state['clicked_button']['#name'], $matches) ) { 00254 $i = $matches[1]; 00255 $featureprop_id = $form_state['values']["id-$i"]; 00256 $property = $form_state['values']["type-$i"]; 00257 $value = $form_state['values']["value-$i"]; 00258 $success = tripal_feature_delete_property_by_id($featureprop_id); 00259 if ($success) { 00260 drupal_set_message(t("Deleted Property")); 00261 } 00262 else { 00263 drupal_set_message(t("Unable to Delete Property")); 00264 } 00265 } 00266 else { 00267 drupal_set_message(t("Unrecognized Button Pressed"), 'error'); 00268 } 00269 } 00275 function theme_tripal_feature_edit_ALL_properties_form($form) { 00276 $output = ''; 00277 00278 $output .= '<br /><fieldset>'; 00279 $output .= '<legend>Edit Already Existing Properties<span class="form-optional" title="This field is optional">(optional)</span></legend>'; 00280 $output .= '<p>Below is a list of already existing properties for this feature, one property per line. The type refers to the type of ' 00281 .'property and the value is the value for that property. </p>'; 00282 $output .= '<table>'; 00283 $output .= '<tr><th>#</th><th>Type</th><th>Value</th><th></th></tr>'; 00284 00285 for ($i=0; $i<=$form['num_properties']['#value']; $i++) { 00286 if (isset($form["num-$i"])) { 00287 $output .= '<tr><td>' . ($i+1) . '</td><td>' . drupal_render($form["num-$i"]["type-$i"]) . '</td><td>' . drupal_render($form["num-$i"]["value-$i"]) . '</td><td>' . drupal_render($form["num-$i"]["delete-$i"]) . '</td></tr>'; 00288 unset($form["num-$i"]); 00289 } 00290 } 00291 00292 $output .= '</table><br />'; 00293 $output .= drupal_render($form); 00294 $output .= '</fieldset>'; 00295 00296 return $output; 00297 } 00298 00304 function tripal_feature_list_properties_for_node($properties) { 00305 00306 if (!empty($properties)) { 00307 $output = '<table>'; 00308 $output .= '<tr><th>Type</th><th>Value</th></tr>'; 00309 00310 00311 if (!empty($properties) ) { 00312 foreach ($properties as $p) { 00313 $output .= '<tr><td>' . $p->type . '</td><td>' . $p->value . '</td></tr>'; 00314 } // end of foreach property 00315 } 00316 00317 $output .= '</table>'; 00318 00319 } 00320 else { 00321 $output = 'No properties exist for the current feature'; 00322 } 00323 00324 return $output; 00325 } 00326 00327