tripal_feature-properties.inc 10 KB

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