tripal_feature-properties.inc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /**
  3. *
  4. *
  5. * @ingroup tripal_feature
  6. */
  7. function tripal_feature_edit_ALL_properties_page($node) {
  8. $output = '';
  9. // get the list of properties for this feature
  10. $values = array('feature_id' => $node->feature->feature_id);
  11. $options = array('order_by' => array('type_id' => 'ASC','rank'=>'ASC'));
  12. $properties = tripal_core_generate_chado_var('featureprop',$values,$options);
  13. $properties = tripal_core_expand_chado_vars($properties,'field','featureprop.value');
  14. $expand_add = (sizeof($properties)) ? FALSE : TRUE;
  15. // add the appopriate form sections
  16. $output .= drupal_get_form('tripal_feature_add_ONE_property_form', $node, $expand_add);
  17. $output .= drupal_get_form('tripal_feature_edit_ALL_properties_form', $node, $properties);
  18. $output .= drupal_get_form('tripal_feature_implement_back_to_feature_button', $node->nid);
  19. return $output;
  20. }
  21. /**
  22. *
  23. *
  24. * @ingroup tripal_feature
  25. */
  26. function tripal_feature_add_ONE_property_form($form_state, $node, $expand) {
  27. $form = array();
  28. $feature_id = $node->feature->feature_id;
  29. $form['add_properties'] = array(
  30. '#type' => 'fieldset',
  31. '#title' => t('Add Property'),
  32. '#collapsible' => TRUE,
  33. '#collapsed' => ($expand) ? FALSE : TRUE,
  34. );
  35. $form['prop_nid'] = array(
  36. '#type' => 'hidden',
  37. '#value' => $node->nid
  38. );
  39. $form['add_properties']['feature_id'] = array(
  40. '#type' => 'value',
  41. '#value' => $feature_id,
  42. '#required' => TRUE
  43. );
  44. // right now this defaults to the 'feature_property' CV
  45. // but in the future it should be more flexible
  46. $form['cv_name'] = array(
  47. '#type' => 'hidden',
  48. '#value' => 'feature_property'
  49. );
  50. // get the list of property types
  51. $prop_type_options = array();
  52. $columns = array('cvterm_id','name');
  53. $values = array(
  54. 'cv_id' => array(
  55. 'name' => $form['cv_name']['#value'],
  56. )
  57. );
  58. $results = tripal_core_chado_select('cvterm',$columns,$values);
  59. foreach ($results as $r) {
  60. $prop_type_options[$r->name] = $r->name;
  61. }
  62. $form['add_properties']['property'] = array(
  63. '#type' => 'select',
  64. '#title' => t('Type of Property'),
  65. '#options' => $prop_type_options,
  66. );
  67. $form['add_properties']['prop_value'] = array(
  68. '#type' => 'textfield',
  69. '#title' => t('Value'),
  70. );
  71. $form['add_properties']['submit-add'] = array(
  72. '#type' => 'submit',
  73. '#value' => t('Add Property')
  74. );
  75. return $form;
  76. }
  77. /**
  78. *
  79. *
  80. * @ingroup tripal_feature
  81. */
  82. function tripal_feature_add_ONE_property_form_validate($form, &$form_state) {
  83. // Only Require if Adding Property
  84. if ($form_state['clicked_button']['#value'] == t('Add Property') ) {
  85. // Check that there is a feature
  86. if ( $form_state['values']['feature_id'] <= 0 ) {
  87. form_set_error('feature_id', 'There is no associated feature.');
  88. }
  89. // Check that Selected a type
  90. if ( !$form_state['values']['property']) {
  91. form_set_error('property', 'Please select a type of property.');
  92. }
  93. }
  94. }
  95. /**
  96. *
  97. *
  98. * @ingroup tripal_feature
  99. */
  100. function tripal_feature_add_ONE_property_form_submit($form, &$form_state) {
  101. $feature_id = $form_state['values']['feature_id'];
  102. $property = $form_state['values']['property'];
  103. $value = $form_state['values']['prop_value'];
  104. $cv_name = $form_state['values']['cv_name'];
  105. $succes = tripal_feature_insert_property($feature_id,$property,$value,0,$cv_name);
  106. if($succes){
  107. drupal_set_message("Successfully Added Property: $property => $value");
  108. } else {
  109. drupal_set_message("Failed to Add Property: $property => $value");
  110. }
  111. }
  112. /**
  113. * Implements Hook_form()
  114. * Handles adding of Properties for features
  115. *
  116. * @ingroup tripal_feature
  117. */
  118. function tripal_feature_edit_ALL_properties_form($form_state, $node, $properties) {
  119. $form = array();
  120. $feature_id = $node->feature->feature_id;
  121. $form['nid'] = array(
  122. '#type' => 'hidden',
  123. '#value' => $node->nid
  124. );
  125. $form['add_properties']['feature_id'] = array(
  126. '#type' => 'value',
  127. '#value' => $feature_id,
  128. '#required' => TRUE
  129. );
  130. // right now this defaults to the 'feature_property' CV
  131. // but in the future it should be more flexible
  132. $form['cv_name'] = array(
  133. '#type' => 'hidden',
  134. '#value' => 'feature_property'
  135. );
  136. if (sizeof($properties)) {
  137. // build the select box options for the property name
  138. $prop_type_options = array();
  139. $columns = array('cvterm_id','name');
  140. $values = array(
  141. 'cv_id' => array(
  142. 'name' => $form['cv_name']['#value']
  143. )
  144. );
  145. $results = tripal_core_chado_select('cvterm',$columns, $values);
  146. foreach ($results as $r) {
  147. $prop_type_options[$r->name] = $r->name;
  148. }
  149. // iterate through all of the properties and create a set of form elements
  150. foreach ($properties as $i => $property) {
  151. $form["num-$i"] = array(
  152. '#type' => 'fieldset',
  153. '#value' => "Property $i"
  154. );
  155. $form["num-$i"]["id-$i"] = array(
  156. '#type' => 'hidden',
  157. '#value' => $property->featureprop_id
  158. );
  159. $default = array_search($property->type, $prop_type_options);
  160. $form["num-$i"]["type-$i"] = array(
  161. '#type' => 'select',
  162. '#options' => $prop_type_options,
  163. '#default_value' => $property->type_id->name
  164. );
  165. $form["num-$i"]["value-$i"] = array(
  166. '#type' => 'textfield',
  167. '#default_value' => $property->value
  168. );
  169. $form["num-$i"]["delete-$i"] = array(
  170. '#type' => 'submit',
  171. '#value' => t("Delete"),
  172. '#name' => "delete-$i",
  173. );
  174. } //end of foreach property
  175. $form['num_properties'] = array(
  176. '#type' => 'hidden',
  177. '#value' => $i
  178. );
  179. $form["submit-edits"] = array(
  180. '#type' => 'submit',
  181. '#value' => t('Update All Properties')
  182. );
  183. }
  184. return $form;
  185. }
  186. /**
  187. *
  188. *
  189. * @ingroup tripal_feature
  190. */
  191. function tripal_feature_edit_ALL_properties_form_submit($form, &$form_state) {
  192. $cv_name = $form_state['values']['cv_name'];
  193. $feature_id = $form_state['values']["feature_id"];
  194. $all_good = 1;
  195. // if the update button was clicked then do the update
  196. if ($form_state['clicked_button']['#value'] == t('Update All Properties') ) {
  197. // iterate through each of the properties and set each one
  198. for ($i=1; $i<=$form_state['values']['num_properties']; $i++) {
  199. $featureprop_id = $form_state['values']["id-$i"];
  200. $property = $form_state['values']["type-$i"];
  201. $value = $form_state['values']["value-$i"];
  202. $success = tripal_feature_update_property_by_id($featureprop_id,$property,$value,$cv_name);
  203. if(!$success){
  204. drupal_set_message("Failed to Update Property: $property => $value");
  205. $all_good = 0;
  206. }
  207. }
  208. if($all_good){
  209. drupal_set_message("Updated all Properties");
  210. }
  211. drupal_goto('node/'.$form_state['values']['nid']);
  212. }
  213. // if the delete button was clicked then remove the property
  214. elseif (preg_match('/delete-(\d+)/', $form_state['clicked_button']['#name'], $matches) ) {
  215. $i = $matches[1];
  216. $featureprop_id = $form_state['values']["id-$i"];
  217. $property = $form_state['values']["type-$i"];
  218. $value = $form_state['values']["value-$i"];
  219. $success = tripal_feature_delete_property_by_id($featureprop_id);
  220. if($success){
  221. drupal_set_message("Deleted Property");
  222. } else {
  223. drupal_set_message("Unable to Delete Property");
  224. }
  225. } else {
  226. drupal_set_message("Unrecognized Button Pressed",'error');
  227. }
  228. }
  229. /**
  230. *
  231. *
  232. * @ingroup tripal_feature
  233. */
  234. function theme_tripal_feature_edit_ALL_properties_form ($form) {
  235. $output = '';
  236. $output .= '<br><fieldset>';
  237. $output .= '<legend>Edit Already Existing Properties<span class="form-optional" title="This field is optional">(optional)</span></legend>';
  238. $output .= '<p>Below is a list of already existing properties for this feature, one property per line. The type refers to the type of '
  239. .'property and the value is the value for that property. </p>';
  240. $output .= '<table>';
  241. $output .= '<tr><th>#</th><th>Type</th><th>Value</th><th></th></tr>';
  242. for ($i=0; $i<=$form['num_properties']['#value']; $i++) {
  243. if (isset($form["num-$i"])) {
  244. $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>';
  245. unset($form["num-$i"]);
  246. }
  247. }
  248. $output .= '</table><br>';
  249. $output .= drupal_render($form);
  250. $output .= '</fieldset>';
  251. return $output;
  252. }
  253. /**
  254. *
  255. *
  256. * @ingroup tripal_feature
  257. */
  258. function tripal_feature_list_properties_for_node($properties) {
  259. if (!empty($properties)) {
  260. $output = '<table>';
  261. $output .= '<tr><th>Type</th><th>Value</th></tr>';
  262. if (!empty($properties) ) {
  263. foreach ($properties as $p) {
  264. $output .= '<tr><td>'.$p->type.'</td><td>'.$p->value.'</td></tr>';
  265. } // end of foreach property
  266. }
  267. $output .= '</table>';
  268. } else {
  269. $output = 'No properties exist for the current feature';
  270. }
  271. return $output;
  272. }