tripal_analysis-properties.inc 9.5 KB

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