tripal_feature-properties.inc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. }
  109. else {
  110. drupal_set_message("Failed to Add Property: $property => $value");
  111. }
  112. }
  113. /**
  114. * Implements Hook_form()
  115. * Handles adding of Properties for features
  116. *
  117. * @ingroup tripal_feature
  118. */
  119. function tripal_feature_edit_ALL_properties_form($form_state, $node, $properties) {
  120. $form = array();
  121. $feature_id = $node->feature->feature_id;
  122. $form['nid'] = array(
  123. '#type' => 'hidden',
  124. '#value' => $node->nid
  125. );
  126. $form['add_properties']['feature_id'] = array(
  127. '#type' => 'value',
  128. '#value' => $feature_id,
  129. '#required' => TRUE
  130. );
  131. // right now this defaults to the 'feature_property' CV
  132. // but in the future it should be more flexible
  133. $form['cv_name'] = array(
  134. '#type' => 'hidden',
  135. '#value' => 'feature_property'
  136. );
  137. if (sizeof($properties)) {
  138. // build the select box options for the property name
  139. $prop_type_options = array();
  140. $columns = array('cvterm_id', 'name');
  141. $values = array(
  142. 'cv_id' => array(
  143. 'name' => $form['cv_name']['#value']
  144. )
  145. );
  146. $results = tripal_core_chado_select('cvterm', $columns, $values);
  147. foreach ($results as $r) {
  148. $prop_type_options[$r->name] = $r->name;
  149. }
  150. // iterate through all of the properties and create a set of form elements
  151. foreach ($properties as $i => $property) {
  152. $form["num-$i"] = array(
  153. '#type' => 'fieldset',
  154. '#value' => "Property $i"
  155. );
  156. $form["num-$i"]["id-$i"] = array(
  157. '#type' => 'hidden',
  158. '#value' => $property->featureprop_id
  159. );
  160. $default = array_search($property->type, $prop_type_options);
  161. $form["num-$i"]["type-$i"] = array(
  162. '#type' => 'select',
  163. '#options' => $prop_type_options,
  164. '#default_value' => $property->type_id->name
  165. );
  166. $form["num-$i"]["value-$i"] = array(
  167. '#type' => 'textfield',
  168. '#default_value' => $property->value
  169. );
  170. $form["num-$i"]["delete-$i"] = array(
  171. '#type' => 'submit',
  172. '#value' => t("Delete"),
  173. '#name' => "delete-$i",
  174. );
  175. } //end of foreach property
  176. $form['num_properties'] = array(
  177. '#type' => 'hidden',
  178. '#value' => $i
  179. );
  180. $form["submit-edits"] = array(
  181. '#type' => 'submit',
  182. '#value' => t('Update All Properties')
  183. );
  184. }
  185. return $form;
  186. }
  187. /**
  188. *
  189. *
  190. * @ingroup tripal_feature
  191. */
  192. function tripal_feature_edit_ALL_properties_form_submit($form, &$form_state) {
  193. $cv_name = $form_state['values']['cv_name'];
  194. $feature_id = $form_state['values']["feature_id"];
  195. $all_good = 1;
  196. // if the update button was clicked then do the update
  197. if ($form_state['clicked_button']['#value'] == t('Update All Properties') ) {
  198. // iterate through each of the properties and set each one
  199. for ($i=1; $i<=$form_state['values']['num_properties']; $i++) {
  200. $featureprop_id = $form_state['values']["id-$i"];
  201. $property = $form_state['values']["type-$i"];
  202. $value = $form_state['values']["value-$i"];
  203. $success = tripal_feature_update_property_by_id($featureprop_id, $property, $value, $cv_name);
  204. if (!$success) {
  205. drupal_set_message("Failed to Update Property: $property => $value");
  206. $all_good = 0;
  207. }
  208. }
  209. if ($all_good) {
  210. drupal_set_message("Updated all Properties");
  211. }
  212. drupal_goto('node/' . $form_state['values']['nid']);
  213. }
  214. // if the delete button was clicked then remove the property
  215. elseif (preg_match('/delete-(\d+)/', $form_state['clicked_button']['#name'], $matches) ) {
  216. $i = $matches[1];
  217. $featureprop_id = $form_state['values']["id-$i"];
  218. $property = $form_state['values']["type-$i"];
  219. $value = $form_state['values']["value-$i"];
  220. $success = tripal_feature_delete_property_by_id($featureprop_id);
  221. if ($success) {
  222. drupal_set_message("Deleted Property");
  223. }
  224. else {
  225. drupal_set_message("Unable to Delete Property");
  226. }
  227. }
  228. else {
  229. drupal_set_message("Unrecognized Button Pressed", 'error');
  230. }
  231. }
  232. /**
  233. *
  234. *
  235. * @ingroup tripal_feature
  236. */
  237. function theme_tripal_feature_edit_ALL_properties_form($form) {
  238. $output = '';
  239. $output .= '<br><fieldset>';
  240. $output .= '<legend>Edit Already Existing Properties<span class="form-optional" title="This field is optional">(optional)</span></legend>';
  241. $output .= '<p>Below is a list of already existing properties for this feature, one property per line. The type refers to the type of '
  242. .'property and the value is the value for that property. </p>';
  243. $output .= '<table>';
  244. $output .= '<tr><th>#</th><th>Type</th><th>Value</th><th></th></tr>';
  245. for ($i=0; $i<=$form['num_properties']['#value']; $i++) {
  246. if (isset($form["num-$i"])) {
  247. $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>';
  248. unset($form["num-$i"]);
  249. }
  250. }
  251. $output .= '</table><br>';
  252. $output .= drupal_render($form);
  253. $output .= '</fieldset>';
  254. return $output;
  255. }
  256. /**
  257. *
  258. *
  259. * @ingroup tripal_feature
  260. */
  261. function tripal_feature_list_properties_for_node($properties) {
  262. if (!empty($properties)) {
  263. $output = '<table>';
  264. $output .= '<tr><th>Type</th><th>Value</th></tr>';
  265. if (!empty($properties) ) {
  266. foreach ($properties as $p) {
  267. $output .= '<tr><td>' . $p->type . '</td><td>' . $p->value . '</td></tr>';
  268. } // end of foreach property
  269. }
  270. $output .= '</table>';
  271. }
  272. else {
  273. $output = 'No properties exist for the current feature';
  274. }
  275. return $output;
  276. }