tripal_feature-properties.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /*************************************************************************************************************
  3. * @section
  4. * Deals with Adding Properties
  5. */
  6. /**
  7. * Implements Hook_form()
  8. * Handles adding of Properties for features
  9. */
  10. function tripal_getfeature_add_ALL_property_page($node) {
  11. $output = '';
  12. $output .= tripal_feature_implement_add_chado_properties_progress('properties').'<br>';
  13. $output .= '<br><b>Current Properties</b><br>';
  14. $output .= tripal_feature_list_properties_for_node($node->properties);
  15. $output .= '<br><br>';
  16. $output .= drupal_get_form('tripal_feature_add_ONE_property_form', $node);
  17. $output .= '<br>';
  18. $output .= drupal_get_form('tripal_feature_implement_add_chado_properties_navigate', 'properties', $node->nid);
  19. return $output;
  20. }
  21. /**
  22. *
  23. */
  24. function tripal_feature_add_ONE_property_form($form_state, $node) {
  25. $form = array();
  26. $feature_id = $node->feature->feature_id;
  27. $form['add_properties'] = array(
  28. '#type' => 'fieldset',
  29. '#title' => t('Add Property'),
  30. );
  31. $form['prop_nid'] = array(
  32. '#type' => 'hidden',
  33. '#value' => $node->nid
  34. );
  35. $prop_type_options = tripal_cv_get_cvterm_options( variable_get('chado_feature_prop_types_cv', 'null') );
  36. $prop_type_options[0] = 'Select a Type';
  37. ksort($prop_type_options);
  38. $form['add_properties']['prop_type_id'] = array(
  39. '#type' => 'select',
  40. '#title' => t('Type of Property'),
  41. '#options' => $prop_type_options,
  42. );
  43. $form['add_properties']['prop_value'] = array(
  44. '#type' => 'textfield',
  45. '#title' => t('Value'),
  46. );
  47. $form['add_properties']['prop_feature_id'] = array(
  48. '#type' => 'value',
  49. '#value' => $feature_id,
  50. '#required' => TRUE
  51. );
  52. $form['add_properties']['submit-add'] = array(
  53. '#type' => 'submit',
  54. '#value' => t('Add Property')
  55. );
  56. return $form;
  57. }
  58. /**
  59. *
  60. */
  61. function tripal_feature_add_ONE_property_form_validate($form, &$form_state) {
  62. // Only Require if Adding Property
  63. if ($form_state['clicked_button']['#value'] == t('Add Property') ) {
  64. // Check that there is a feature
  65. if ( $form_state['values']['prop_feature_id'] <= 0 ) {
  66. form_set_error('prop_feature_id', 'There is no associated feature.');
  67. }
  68. // Check that Selected a type
  69. if ( $form_state['values']['prop_type_id'] == 0) {
  70. form_set_error('prop_type_id', 'Please select a type of property.');
  71. } else {
  72. // Check that type is in chado
  73. $previous_db = tripal_db_set_active('chado');
  74. $num_rows = db_fetch_object(db_query("SELECT count(*) as count FROM cvterm WHERE cvterm_id=%d", $form_state['values']['prop_type_id']));
  75. tripal_db_set_active($previous_db);
  76. if ( $num_rows->count != 1) {
  77. form_set_error('prop_type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)");
  78. } // end of if more or less than 1 row
  79. } // if no prop type
  80. } // if add Property
  81. }
  82. /**
  83. *
  84. */
  85. function tripal_feature_add_ONE_property_form_submit($form, &$form_state) {
  86. // if there is a property add it (only won't be a property if clicked next step w/ no property)
  87. if ($form_state['values']['prop_type_id'] != 0) {
  88. //determine the rank for this property
  89. $max_rank = get_max_chado_rank('featureprop',
  90. array('feature_id'=>array('type'=>'INT','value'=>$form_state['values']['prop_feature_id']),
  91. 'type_id'=>array('type'=>'INT','value'=> $form_state['values']['prop_type_id']) ));
  92. if ($max_rank == -1) { $rank = 0;
  93. } else { $rank = $max_rank+1; }
  94. $previous_db = tripal_db_set_active('chado');
  95. db_query(
  96. "INSERT INTO featureprop (feature_id, type_id, value, rank) VALUES (%d, %d, '%s', %d)",
  97. $form_state['values']['prop_feature_id'],
  98. $form_state['values']['prop_type_id'],
  99. $form_state['values']['prop_value'],
  100. $rank
  101. );
  102. tripal_db_set_active($previous_db);
  103. drupal_set_message("Successfully Added Property");
  104. } //end of if property to add
  105. }
  106. /*************************************************************************************************************
  107. * @section
  108. * Deals with Editing and Deleting Properties
  109. */
  110. function tripal_feature_edit_ALL_properties_page($node) {
  111. $output = '';
  112. $output .= drupal_get_form('tripal_feature_edit_ALL_properties_form', $node);
  113. $output .= '<br>';
  114. $output .= drupal_get_form('tripal_feature_add_ONE_property_form', $node);
  115. $output .= '<br>';
  116. $output .= drupal_get_form('tripal_feature_implement_back_to_feature_button', $node->nid);
  117. return $output;
  118. }
  119. /**
  120. * Implements Hook_form()
  121. * Handles adding of Properties for features
  122. */
  123. function tripal_feature_edit_ALL_properties_form($form_state, $node) {
  124. $form = array();
  125. $form['nid'] = array(
  126. '#type' => 'hidden',
  127. '#value' => $node->nid
  128. );
  129. $i=0;
  130. $feature = $node->feature;
  131. $properties = tripal_feature_load_properties ($feature->feature_id);
  132. if (sizeof($properties) != 0) {
  133. foreach ($properties as $property) {
  134. $i++;
  135. $form["num-$i"] = array(
  136. '#type' => 'fieldset',
  137. '#value' => "Property $i"
  138. );
  139. $form["num-$i"]["id-$i"] = array(
  140. '#type' => 'hidden',
  141. '#value' => $property->featureprop_id
  142. );
  143. $prop_type_options = tripal_cv_get_cvterm_options( variable_get('chado_feature_prop_types_cv', 'null') );
  144. ksort($prop_type_options);
  145. $default = array_search($property->type, $prop_type_options);
  146. $form["num-$i"]["type-$i"] = array(
  147. '#type' => 'select',
  148. //'#title' => t('Type of Property'),
  149. '#options' => $prop_type_options,
  150. '#default_value' => $default
  151. );
  152. $form["num-$i"]["value-$i"] = array(
  153. '#type' => 'textfield',
  154. //'#title' => t('Value'),
  155. '#default_value' => $property->value
  156. );
  157. $form["num-$i"]["delete-$i"] = array(
  158. '#type' => 'submit',
  159. '#value' => t("Delete"),
  160. '#name' => "delete-$i",
  161. );
  162. }//end of foreach property
  163. }
  164. $form['num_properties'] = array(
  165. '#type' => 'hidden',
  166. '#value' => $i
  167. );
  168. $form["submit-edits"] = array(
  169. '#type' => 'submit',
  170. '#value' => t('Update All Properties')
  171. );
  172. return $form;
  173. }
  174. /**
  175. *
  176. */
  177. function tripal_feature_edit_ALL_properties_form_submit($form, &$form_state) {
  178. if ($form_state['clicked_button']['#value'] == t('Update All Properties') ) {
  179. //Update all
  180. for ($i=1; $i<=$form_state['values']['num_properties']; $i++) {
  181. tripal_feature_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"]);
  182. }
  183. drupal_set_message("Updated all Properties");
  184. drupal_goto('node/'.$form_state['values']['nid']);
  185. } elseif ( preg_match('/Delete #(\d+)/', $form_state['clicked_button']['#value'], $matches) ) {
  186. $i = $matches[1];
  187. tripal_feature_delete_property($form_state['values']["id-$i"], $form_state['values']["type-$i"], $form_state['values']["value-$i"]);
  188. drupal_set_message("Deleted Property");
  189. } else {
  190. drupal_set_message("Unrecognized Button Pressed",'error');
  191. }
  192. }
  193. /**
  194. *
  195. */
  196. function tripal_feature_update_property($featureprop_id, $cvterm_id, $value, $preferred, $nid) {
  197. $previous_db = tripal_db_set_active('chado');
  198. $old_obj = db_fetch_object(db_query("SELECT * FROM featureprop WHERE featureprop_id=%d",$featureprop_id));
  199. tripal_db_set_active($previous_db);
  200. // if they changed the type need to check rank
  201. // (if there is another property of the same type then rank needs to be increased to prevent collisions)
  202. if ($cvterm_id == $old_obj->type_id) {
  203. $previous_db = tripal_db_set_active('chado');
  204. db_query(
  205. "UPDATE featureprop SET type_id=%d, value='%s' WHERE featureprop_id=%d",
  206. $cvterm_id,
  207. $value,
  208. $featureprop_id
  209. );
  210. tripal_db_set_active($previous_db);
  211. } else {
  212. //determine the rank for this property
  213. $max_rank = get_max_chado_rank('featureprop',
  214. array('feature_id'=>array('type'=>'INT','value'=> $old_obj->feature_id),
  215. 'type_id'=>array('type'=>'INT','value'=> $cvterm_id ) ));
  216. if ($max_rank == -1) { $rank = 0;
  217. } else { $rank = $max_rank+1; }
  218. $previous_db = tripal_db_set_active('chado');
  219. db_query(
  220. "UPDATE featureprop SET type_id=%d, value='%s', rank=%d WHERE featureprop_id=%d",
  221. $cvterm_id,
  222. $value,
  223. $rank,
  224. $featureprop_id
  225. );
  226. tripal_db_set_active($previous_db);
  227. }
  228. module_load_include('inc', 'node', 'node.pages');
  229. drupal_execute('chado_feature_node_form', $node_form_state, $node);
  230. }
  231. /**
  232. *
  233. */
  234. function tripal_feature_delete_property($featureprop_id) {
  235. $previous_db = tripal_db_set_active('chado');
  236. db_query(
  237. "DELETE FROM featureprop WHERE featureprop_id=%d",
  238. $featureprop_id
  239. );
  240. tripal_db_set_active($previous_db);
  241. }
  242. /**
  243. *
  244. */
  245. function theme_tripal_feature_edit_ALL_properties_form ($form) {
  246. $output = '';
  247. $output .= '<br><fieldset>';
  248. $output .= '<legend>Edit Already Existing Properties<span class="form-optional" title="This field is optional">(optional)</span></legend>';
  249. $output .= '<p>Below is a list of already existing properties for this feature, one property per line. The type refers to the type of '
  250. .'property and the value is the value for that property. </p>';
  251. $output .= '<table>';
  252. $output .= '<tr><th>#</th><th>Type</th><th>Value</th><th></th></tr>';
  253. for ($i=1; $i<=$form['num_properties']['#value']; $i++) {
  254. $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>';
  255. }
  256. $output .= '</table><br>';
  257. $output .= drupal_render($form);
  258. $output .= '</fieldset>';
  259. return $output;
  260. }
  261. /*************************************************************************************************************
  262. * @section
  263. * Supplimentary functions
  264. */
  265. /**
  266. *
  267. */
  268. function list_properties_for_node($properties) {
  269. if (!empty($properties)) {
  270. $output = '<table>';
  271. $output .= '<tr><th>Type</th><th>Value</th></tr>';
  272. if (!empty($properties) ) {
  273. foreach ($properties as $p) {
  274. $output .= '<tr><td>'.$p->type.'</td><td>'.$p->value.'</td></tr>';
  275. } // end of foreach property
  276. }
  277. $output .= '</table>';
  278. } else {
  279. $output = 'No properties exist forony the current feature';
  280. }
  281. return $output;
  282. }