tripal_feature-properties.inc 11 KB

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