tripal_feature-properties.inc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. /**
  7. *
  8. *
  9. * @ingroup tripal_feature
  10. */
  11. function tripal_feature_edit_ALL_properties_page($node) {
  12. $output = '';
  13. // get the list of properties for this feature
  14. $values = array('feature_id' => $node->feature->feature_id);
  15. $options = array('order_by' => array('type_id' => 'ASC', 'rank' => 'ASC'));
  16. $properties = tripal_core_generate_chado_var('featureprop', $values, $options);
  17. $properties = tripal_core_expand_chado_vars($properties, 'field', 'featureprop.value');
  18. $expand_add = (sizeof($properties)) ? FALSE : TRUE;
  19. // add the appopriate form sections
  20. $output .= drupal_get_form('tripal_feature_add_ONE_property_form', $node, $expand_add);
  21. $output .= drupal_get_form('tripal_feature_edit_ALL_properties_form', $node, $properties);
  22. $output .= drupal_get_form('tripal_feature_implement_back_to_feature_button', $node->nid);
  23. return $output;
  24. }
  25. /**
  26. *
  27. *
  28. * @ingroup tripal_feature
  29. */
  30. function tripal_feature_add_ONE_property_form($form_state, $node, $expand) {
  31. $form = array();
  32. $feature_id = $node->feature->feature_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']['feature_id'] = array(
  44. '#type' => 'value',
  45. '#value' => $feature_id,
  46. '#required' => TRUE
  47. );
  48. // right now this defaults to the 'feature_property' CV
  49. // but in the future it should be more flexible
  50. $form['cv_name'] = array(
  51. '#type' => 'hidden',
  52. '#value' => 'feature_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_feature
  85. */
  86. function tripal_feature_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 feature
  90. if ( $form_state['values']['feature_id'] <= 0 ) {
  91. form_set_error('feature_id', 'There is no associated feature.');
  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_feature
  103. */
  104. function tripal_feature_add_ONE_property_form_submit($form, &$form_state) {
  105. $feature_id = $form_state['values']['feature_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_feature_insert_property($feature_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 features
  120. *
  121. * @ingroup tripal_feature
  122. */
  123. function tripal_feature_edit_ALL_properties_form($form_state, $node, $properties) {
  124. $form = array();
  125. $feature_id = $node->feature->feature_id;
  126. $form['nid'] = array(
  127. '#type' => 'hidden',
  128. '#value' => $node->nid
  129. );
  130. $form['add_properties']['feature_id'] = array(
  131. '#type' => 'value',
  132. '#value' => $feature_id,
  133. '#required' => TRUE
  134. );
  135. // right now this defaults to the 'feature_property' CV
  136. // but in the future it should be more flexible
  137. $form['cv_name'] = array(
  138. '#type' => 'hidden',
  139. '#value' => 'feature_property'
  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. $form["num-$i"] = array(
  157. '#type' => 'fieldset',
  158. '#value' => "Property $i"
  159. );
  160. $form["num-$i"]["id-$i"] = array(
  161. '#type' => 'hidden',
  162. '#value' => $property->featureprop_id
  163. );
  164. $default = array_search($property->type, $prop_type_options);
  165. $form["num-$i"]["type-$i"] = array(
  166. '#type' => 'select',
  167. '#options' => $prop_type_options,
  168. '#default_value' => $property->type_id->name
  169. );
  170. $form["num-$i"]["value-$i"] = array(
  171. '#type' => 'textfield',
  172. '#default_value' => $property->value
  173. );
  174. $form["num-$i"]["delete-$i"] = array(
  175. '#type' => 'submit',
  176. '#value' => t("Delete"),
  177. '#name' => "delete-$i",
  178. );
  179. } //end of foreach property
  180. $form['num_properties'] = array(
  181. '#type' => 'hidden',
  182. '#value' => $i
  183. );
  184. $form["submit-edits"] = array(
  185. '#type' => 'submit',
  186. '#value' => t('Update All Properties')
  187. );
  188. }
  189. return $form;
  190. }
  191. /**
  192. *
  193. *
  194. * @ingroup tripal_feature
  195. */
  196. function tripal_feature_edit_ALL_properties_form_submit($form, &$form_state) {
  197. $cv_name = $form_state['values']['cv_name'];
  198. $feature_id = $form_state['values']["feature_id"];
  199. $all_good = 1;
  200. // if the update button was clicked then do the update
  201. if ($form_state['clicked_button']['#value'] == t('Update All Properties') ) {
  202. // iterate through each of the properties and set each one
  203. for ($i=1; $i<=$form_state['values']['num_properties']; $i++) {
  204. $featureprop_id = $form_state['values']["id-$i"];
  205. $property = $form_state['values']["type-$i"];
  206. $value = $form_state['values']["value-$i"];
  207. $success = tripal_feature_update_property_by_id($featureprop_id, $property, $value, $cv_name);
  208. if (!$success) {
  209. drupal_set_message(t("Failed to Update Property: %property => %value", array('%property' => $property), array('%value' => $value)));
  210. $all_good = 0;
  211. }
  212. }
  213. if ($all_good) {
  214. drupal_set_message(t("Updated all Properties"));
  215. }
  216. drupal_goto('node/' . $form_state['values']['nid']);
  217. }
  218. // if the delete button was clicked then remove the property
  219. elseif (preg_match('/delete-(\d+)/', $form_state['clicked_button']['#name'], $matches) ) {
  220. $i = $matches[1];
  221. $featureprop_id = $form_state['values']["id-$i"];
  222. $property = $form_state['values']["type-$i"];
  223. $value = $form_state['values']["value-$i"];
  224. $success = tripal_feature_delete_property_by_id($featureprop_id);
  225. if ($success) {
  226. drupal_set_message(t("Deleted Property"));
  227. }
  228. else {
  229. drupal_set_message(t("Unable to Delete Property"));
  230. }
  231. }
  232. else {
  233. drupal_set_message(t("Unrecognized Button Pressed"), 'error');
  234. }
  235. }
  236. /**
  237. *
  238. *
  239. * @ingroup tripal_feature
  240. */
  241. function theme_tripal_feature_edit_ALL_properties_form($form) {
  242. $output = '';
  243. $output .= '<br /><fieldset>';
  244. $output .= '<legend>Edit Already Existing Properties<span class="form-optional" title="This field is optional">(optional)</span></legend>';
  245. $output .= '<p>Below is a list of already existing properties for this feature, one property per line. The type refers to the type of '
  246. .'property and the value is the value for that property. </p>';
  247. $output .= '<table>';
  248. $output .= '<tr><th>#</th><th>Type</th><th>Value</th><th></th></tr>';
  249. for ($i=0; $i<=$form['num_properties']['#value']; $i++) {
  250. if (isset($form["num-$i"])) {
  251. $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>';
  252. unset($form["num-$i"]);
  253. }
  254. }
  255. $output .= '</table><br />';
  256. $output .= drupal_render($form);
  257. $output .= '</fieldset>';
  258. return $output;
  259. }
  260. /**
  261. *
  262. *
  263. * @ingroup tripal_feature
  264. */
  265. function tripal_feature_list_properties_for_node($properties) {
  266. if (!empty($properties)) {
  267. $output = '<table>';
  268. $output .= '<tr><th>Type</th><th>Value</th></tr>';
  269. if (!empty($properties) ) {
  270. foreach ($properties as $p) {
  271. $output .= '<tr><td>' . $p->type . '</td><td>' . $p->value . '</td></tr>';
  272. } // end of foreach property
  273. }
  274. $output .= '</table>';
  275. }
  276. else {
  277. $output = 'No properties exist for the current feature';
  278. }
  279. return $output;
  280. }