tripal_natural_diversity.nd_geolocation.inc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * Implementation of hook_form().
  4. *
  5. * This form takes the ND Geolocation Title information and description from the user.
  6. *
  7. * @parm $node
  8. * The initialized node
  9. *
  10. * @parm $form_state
  11. * The state of the form, that has the user entered information that is neccessary for adding
  12. * information to the nd_geolocation
  13. *
  14. * @return $form
  15. * An array as described by the Drupal Form API
  16. *
  17. *
  18. * @ingroup tripal_nd_geolocation
  19. */
  20. function chado_nd_geolocation_form(&$node, $form_state) {
  21. $form = array();
  22. $nd_geolocation = $node->nd_geolocation;
  23. $nd_geolocation_latitude = $nd_geolocation->latitude;
  24. $nd_geolocation_longitude = $nd_geolocation->longitude;
  25. $nd_geolocation_altitude = $nd_geolocation->altitude;
  26. $nd_geolocation_geodetic_datum = $nd_geolocation->geodetic_datum;
  27. // keep track of the nd_geolocation id if we have. If we do have one then
  28. // this is an update as opposed to an insert.
  29. $form['nd_geolocation_id'] = array(
  30. '#type' => 'value',
  31. '#value' => $nd_geolocation->nd_geolocation_id,
  32. );
  33. // use nd_geolocation.description as the node title
  34. $form['title']= array(
  35. '#type' => 'textfield',
  36. '#title' => t('ND Geolocation Description'),
  37. '#description' => t('A brief description of the nd_geolocation.'),
  38. '#required' => TRUE,
  39. '#default_value' => $node->title,
  40. '#weight' => 1
  41. );
  42. $form['nd_geolocation_latitude']= array(
  43. '#type' => 'textfield',
  44. '#title' => t('ND Geolocation Latitude'),
  45. '#description' => t('Latitude of the nd_geolocation'),
  46. '#required' => FALSE,
  47. '#default_value' => $nd_geolocation_latitude,
  48. '#weight' => 2
  49. );
  50. $form['nd_geolocation_longitude']= array(
  51. '#type' => 'textfield',
  52. '#title' => t('ND Geolocation Longitude'),
  53. '#description' => t('Longitude of the nd_geolocation'),
  54. '#required' => FALSE,
  55. '#default_value' => $nd_geolocation_longitude,
  56. '#weight' => 3
  57. );
  58. $form['nd_geolocation_altitude']= array(
  59. '#type' => 'textfield',
  60. '#title' => t('ND Geolocation Altitude'),
  61. '#description' => t('Altitude of the nd_geolocation'),
  62. '#required' => FALSE,
  63. '#default_value' => $nd_geolocation_altitude,
  64. '#weight' => 4
  65. );
  66. $form['nd_geolocation_geodetic_datum']= array(
  67. '#type' => 'textfield',
  68. '#title' => t('ND Geolocation Geodetic Datum'),
  69. '#description' => t('Geodetic_datum of the nd_geolocation'),
  70. '#required' => FALSE,
  71. '#default_value' => $nd_geolocation_geodetic_datum,
  72. '#weight' => 5
  73. );
  74. return $form;
  75. }
  76. /**
  77. * validates submission of form when adding or updating a nd_geolocation node
  78. *
  79. * @ingroup tripal_nd_geolocation
  80. */
  81. function chado_nd_geolocation_validate($node) {
  82. // check to make sure latitude, longitude, and altitude are numbers or null
  83. $latitude = trim($node->nd_geolocation_latitude);
  84. $longitude = trim($node->nd_geolocation_longitude);
  85. $altitude = trim($node->nd_geolocation_altitude);
  86. if ($latitude && !is_numeric($latitude)) {
  87. form_set_error('nd_geolocation_latitude', t('Latitude shoulbe a number'));
  88. }
  89. if ($longitude && !is_numeric($longitude)) {
  90. form_set_error('nd_geolocation_longitude', t('Longitude shoulbe a number'));
  91. }
  92. if ($altitude && !is_numeric($altitude)) {
  93. form_set_error('nd_geolocation_altitude', t('Altitude shoulbe a number'));
  94. }
  95. }
  96. /**
  97. * Implementation of hook_insert().
  98. *
  99. * @parm $node
  100. * Then node that has the information stored within, accessed given the nid
  101. *
  102. *
  103. * @ingroup tripal_nd_geolocation
  104. */
  105. function chado_nd_geolocation_insert($node) {
  106. if ($node->nd_geolocation_id) {
  107. $nd_geolocation['nd_geolocation_id'] = $node->nd_geolocation_id;
  108. }
  109. else {
  110. $description = trim($node->title);
  111. $latitude = trim($node->nd_geolocation_latitude);
  112. $longitude = trim($node->nd_geolocation_longitude);
  113. $altitude = trim($node->nd_geolocation_altitude);
  114. $geodetic_datum = trim($node->nd_geolocation_geodetic_datum);
  115. $values = array(
  116. 'description' => $description,
  117. 'latitude' => $latitude ? $latitude : 'NULL',
  118. 'longitude' => $longitude ? $longitude : 'NULL',
  119. 'altitude' => $altitude ? $altitude : 'NULL',
  120. 'geodetic_datum' => $geodetic_datum ? $geodetic_datum : '',
  121. );
  122. $nd_geolocation = tripal_core_chado_insert('nd_geolocation', $values);
  123. }
  124. if ($nd_geolocation) {
  125. // make sure the entry for this feature doesn't already exist in the chado_nd_geolocation table
  126. // if it doesn't exist then we want to add it.
  127. $nd_geolocation_id = chado_get_id_for_node('nd_geolocation', $node) ;
  128. if (!$nd_geolocation_id) {
  129. // next add the item to the drupal table
  130. $sql = "INSERT INTO {chado_nd_geolocation} (nid, vid, nd_geolocation_id) ".
  131. "VALUES (%d, %d, %d)";
  132. db_query($sql, $node->nid, $node->vid, $nd_geolocation['nd_geolocation_id']);
  133. }
  134. }
  135. else {
  136. drupal_set_message(t('Unable to add nd_geolocation.', 'warning'));
  137. watchdog('tripal_nd_geolocation', 'Insert nd_geolocation: Unable to create nd_geolocation where values: %values',
  138. array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  139. }
  140. }
  141. /**
  142. *
  143. * Implementation of hook_delete().
  144. *
  145. * @param $node
  146. * The node which is to be deleted, only chado nd_geolocation and chado_nd_geolocation need to be dealt with
  147. * since the drupal node is deleted automagically
  148. *
  149. *
  150. * @ingroup tripal_nd_geolocation
  151. */
  152. function chado_nd_geolocation_delete($node) {
  153. $nd_geolocation_id = chado_get_id_for_node('nd_geolocation', $node);
  154. // if we don't have a nd_geolocation id for this node then this isn't a node of
  155. // type chado_nd_geolocation or the entry in the chado_nd_geolocation table was lost.
  156. if (!$nd_geolocation_id) {
  157. return;
  158. }
  159. // Remove data from {chado_nd_geolocation}, {node} and {node_revisions} tables of
  160. // drupal database
  161. $sql_del = "DELETE FROM {chado_nd_geolocation} ".
  162. "WHERE nid = %d ".
  163. "AND vid = %d";
  164. db_query($sql_del, $node->nid, $node->vid);
  165. $sql_del = "DELETE FROM {node_revisions} ".
  166. "WHERE nid = %d ".
  167. "AND vid = %d";
  168. db_query($sql_del, $node->nid, $node->vid);
  169. $sql_del = "DELETE FROM {node} ".
  170. "WHERE nid = %d ".
  171. "AND vid = %d";
  172. db_query($sql_del, $node->nid, $node->vid);
  173. // Remove data from nd_geolocation and nd_geolocationprop tables of chado database as well
  174. chado_query("DELETE FROM {nd_geolocationprop} WHERE nd_geolocation_id = %d", $eimage_id);
  175. chado_query("DELETE FROM {nd_geolocation} WHERE nd_geolocation_id = %d", $nd_geolocation_id);
  176. }
  177. /**
  178. * Implements hook_update().
  179. *
  180. * @param $node
  181. * The node which is to have its containing information updated when the user modifies information
  182. * pertaining to the specific nd_geolocation
  183. *
  184. *
  185. * @ingroup tripal_nd_geolocation
  186. */
  187. function chado_nd_geolocation_update($node) {
  188. if ($node->revision) {
  189. // there is no way to handle revisions in Chado but leave
  190. // this here just to make not we've addressed it.
  191. }
  192. // update the nd_geolocation and the description
  193. $nd_geolocation_id = chado_get_id_for_node('nd_geolocation', $node) ;
  194. $match = array(
  195. 'nd_geolocation_id' => $nd_geolocation_id,
  196. );
  197. $description = trim($node->title);
  198. $latitude = trim($node->nd_geolocation_latitude);
  199. $longitude = trim($node->nd_geolocation_longitude);
  200. $altitude = trim($node->nd_geolocation_altitude);
  201. $geodetic_datum = trim($node->nd_geolocation_geodetic_datum);
  202. $values = array(
  203. 'description' => $description,
  204. 'latitude' => $latitude ? $latitude : 'NULL',
  205. 'longitude' => $longitude ? $longitude : 'NULL',
  206. 'altitude' => $altitude ? $altitude : 'NULL',
  207. 'geodetic_datum' => $geodetic_datum ? $geodetic_datum : '',
  208. );
  209. $status = tripal_core_chado_update('nd_geolocation', $match, $values);
  210. }
  211. /**
  212. * Implementation of node_load().
  213. *
  214. * @param $node
  215. * The node that is to have its containing information loaded
  216. *
  217. * @return $node
  218. * The node, containing the loaded nd_geolocation with the current nid
  219. *
  220. *
  221. * @ingroup tripal_nd_geolocation
  222. */
  223. function chado_nd_geolocation_load($node) {
  224. // get the feature details from chado
  225. $nd_geolocation_id = chado_get_id_for_node('nd_geolocation', $node);
  226. $values = array('nd_geolocation_id' => $nd_geolocation_id);
  227. $nd_geolocation = tripal_core_generate_chado_var('nd_geolocation', $values);
  228. $additions = new stdClass();
  229. $additions->nd_geolocation = $nd_geolocation;
  230. return $additions;
  231. }