tripal_ws.admin.inc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. /**
  3. * Provide form to store information of other Tripal sites
  4. *
  5. * @param unknown $form
  6. * @param unknown $form_state
  7. */
  8. function tripal_ws_tripal_sites_form($form, &$form_state) {
  9. $form = array();
  10. $values = key_exists('values', $form_state) ? $form_state['values'] : NULL;
  11. $tripal_site = $values ? $values['tripal_site'] : 0;
  12. $results =
  13. db_select('tripal_sites', 'ts')
  14. ->fields('ts')
  15. ->execute();
  16. $headers = array('Name', 'URL', 'Version', 'Description', 'Action');
  17. $rows = array();
  18. while ($site = $results->fetchObject()) {
  19. $rows[] = array(
  20. $site->name,
  21. $site->url,
  22. $site->version,
  23. $site->description,
  24. array(
  25. 'data' => l('Edit', '/admin/tripal/storage/ws/tripal_sites/edit/' . $site->id) . ' | ' .
  26. l('Remove', '/admin/tripal/storage/ws/tripal_sites/remove/' . $site->id),
  27. 'nowrap' => TRUE,
  28. ),
  29. );
  30. }
  31. if (count($rows) == 0) {
  32. $rows[] = array('No configured Tripal site.', array('colspan' => 5));
  33. }
  34. $table = array(
  35. 'header' => $headers,
  36. 'rows' => $rows,
  37. 'attributes' => array(),
  38. 'sticky' => FALSE,
  39. 'caption' => '',
  40. 'colgroups' => array(),
  41. 'empty' => '',
  42. );
  43. $output = theme_table($table);
  44. $form['instructions'] = array(
  45. '#type' => 'item',
  46. '#markup' => t('The following is a list of remote Tripal sites with which
  47. this site can communicate. You may add and remove site as needed.')
  48. );
  49. $form['table'] = array(
  50. '#markup' => $output
  51. );
  52. return $form;
  53. }
  54. /**
  55. * Provide form to add/edit a Tripal site
  56. *
  57. * @param unknown $form
  58. * @param unknown $form_state
  59. */
  60. function tripal_ws_tripal_sites_edit_form($form, &$form_state, $tripal_site_id = NULL) {
  61. $id = NULL;
  62. $name = '';
  63. $url = '';
  64. $version = '';
  65. $description = '';
  66. if ($tripal_site_id) {
  67. $site = db_select('tripal_sites', 'ts')
  68. ->fields('ts')
  69. ->condition('id', $tripal_site_id)
  70. ->execute()
  71. ->fetchObject();
  72. if (is_object($site)) {
  73. $id = $site->id;
  74. $name = $site->name;
  75. $url = $site->url;
  76. $version = $site->version;
  77. $description = $site->description;
  78. }
  79. }
  80. $form = array();
  81. $form['tripal_site_info']['id'] = array(
  82. '#type' => 'hidden',
  83. '#value' => $id,
  84. );
  85. $form['tripal_site_info']['name'] = array(
  86. '#title' => t('Name'),
  87. '#type' => 'textfield',
  88. '#description' => t('Full name of the Tripal site.'),
  89. '#default_value' => $name,
  90. '#required' => TRUE
  91. );
  92. $form['tripal_site_info']['url'] = array(
  93. '#title' => t('URL'),
  94. '#type' => 'textfield',
  95. '#description' => t('The URL of the Tripal site, including the "http://" or "https://" followed by the address for the site\'s home page.'),
  96. '#default_value' => $url,
  97. '#required' => TRUE
  98. );
  99. $form['tripal_site_info']['version'] = array(
  100. '#title' => t('Version'),
  101. '#type' => 'textfield',
  102. '#description' => t('Web services version used by the Tripal site (example: v0.1)'),
  103. '#default_value' => $version,
  104. );
  105. $form['tripal_site_info']['description'] = array(
  106. '#title' => t('Description'),
  107. '#type' => 'textarea',
  108. '#default_value' => $description,
  109. );
  110. $form['submit_button'] = array(
  111. '#type' => 'submit',
  112. '#value' => t('Save'),
  113. '#name' => 'save'
  114. );
  115. $form['cancel_button'] = array(
  116. '#type' => 'button',
  117. '#value' => t('Cancel'),
  118. '#name' => 'cancel_button',
  119. '#limit_validation_errors' => array()
  120. );
  121. return $form;
  122. }
  123. /**
  124. * Implements hook_validate()
  125. *
  126. * @param unknown $form
  127. * @param unknown $form_state
  128. */
  129. function tripal_ws_tripal_sites_edit_form_validate($form, &$form_state) {
  130. if (array_key_exists('clicked_button', $form_state)) {
  131. if ($form_state['clicked_button']['#name'] =='cancel_button') {
  132. drupal_goto('/admin/tripal/storage/ws/tripal_sites');
  133. }
  134. // Make sure URL does not already exist
  135. else if ($form_state['clicked_button']['#name'] =='save') {
  136. $url = $form_state['values']['url'];
  137. $version = $form_state['values']['version'];
  138. $name = $form_state['values']['name'];
  139. $check_url = NULL;
  140. $check_name = NULL;
  141. // When updating a record
  142. if ($form_state['values']['id']) {
  143. $check_url =
  144. db_select('tripal_sites', 'ts')
  145. ->fields('ts', array('id'))
  146. ->condition('url', $url)
  147. ->condition('version', $version)
  148. ->condition('id', $form_state['values']['id'], '<>')
  149. ->execute()
  150. ->fetchField();
  151. $check_name =
  152. db_select('tripal_sites', 'ts')
  153. ->fields('ts', array('id'))
  154. ->condition('name', $name)
  155. ->condition('id', $form_state['values']['id'], '<>')
  156. ->execute()
  157. ->fetchField();
  158. }
  159. // When inserting a record
  160. else {
  161. $check_url =
  162. db_select('tripal_sites', 'ts')
  163. ->fields('ts', array('id'))
  164. ->condition('url', $url)
  165. ->condition('version', $version)
  166. ->execute()
  167. ->fetchField();
  168. $check_name =
  169. db_select('tripal_sites', 'ts')
  170. ->fields('ts', array('id'))
  171. ->condition('name', $name)
  172. ->execute()
  173. ->fetchField();
  174. }
  175. if ($check_url) {
  176. form_set_error('url', t('The URL and version is used by another site.'));
  177. form_set_error('version');
  178. }
  179. if ($check_name) {
  180. form_set_error('name', t('The name is used by another site.'));
  181. }
  182. }
  183. }
  184. }
  185. /**
  186. * Implements hook_submit()
  187. *
  188. * @param unknown $form
  189. * @param unknown $form_state
  190. */
  191. function tripal_ws_tripal_sites_edit_form_submit($form, &$form_state) {
  192. $id = $form_state['values']['id'];
  193. $name = $form_state['values']['name'];
  194. $url = $form_state['values']['url'];
  195. $version = $form_state['values']['version'];
  196. $description = $form_state['values']['description'];
  197. // If there is an 'id' do an update, otherwise do an insert
  198. if ($id) {
  199. db_update('tripal_sites')
  200. ->fields(array(
  201. 'name' => $name,
  202. 'url' => $url,
  203. 'version' => $version,
  204. 'description' => $description
  205. ))
  206. ->condition('id', $id)
  207. ->execute();
  208. drupal_set_message(t('Tripal site \'' . $name . '\' has been updated.'));
  209. }
  210. else {
  211. db_insert('tripal_sites')
  212. ->fields(array(
  213. 'name' => $name,
  214. 'url' => $url,
  215. 'version' => $version,
  216. 'description' => $description
  217. ))
  218. ->execute();
  219. drupal_set_message(t('Tripal site \'' . $name . '\' has been added.'));
  220. }
  221. drupal_goto('/admin/tripal/storage/ws/tripal_sites');
  222. }
  223. /**
  224. * Implements hook_form()
  225. * Reset term used by semantic web
  226. *
  227. * @param $form
  228. * @param $form_state
  229. * @param $table
  230. * @param $column
  231. * @return $form
  232. */
  233. function tripal_ws_tripal_sites_remove_form($form, &$form_state, $id = NULL) {
  234. $name = '';
  235. $record_id = '';
  236. if ($id) {
  237. $site = db_select('tripal_sites', 'ts')
  238. ->fields('ts')
  239. ->condition('id', $id)
  240. ->execute()
  241. ->fetchObject();
  242. if (is_object($site)) {
  243. $record_id = $site->id;
  244. $name = $site->name;
  245. }
  246. }
  247. $form['confirmation'] = array(
  248. '#markup' => 'Really remove the \'' . $name . '\' Tripal site? ',
  249. );
  250. $form['tripal_site_id'] = array(
  251. '#type' => 'value',
  252. '#value' => $record_id
  253. );
  254. $form['tripal_site_name'] = array(
  255. '#type' => 'value',
  256. '#value' => $name
  257. );
  258. $form['submit_button'] = array(
  259. '#type' => 'submit',
  260. '#value' => t('Remove'),
  261. '#name' => 'remove'
  262. );
  263. $form['cancel_button'] = array(
  264. '#type' => 'button',
  265. '#value' => t('Cancel'),
  266. '#name' => 'cancel_button',
  267. '#limit_validation_errors' => array()
  268. );
  269. return $form;
  270. }
  271. /**
  272. * Implements hook_form_validate()
  273. *
  274. * Validate function for resetting the semantic web term
  275. *
  276. * @param unknown $form
  277. * @param unknown $form_state
  278. */
  279. function tripal_ws_tripal_sites_remove_form_validate($form, &$form_state) {
  280. if (array_key_exists('clicked_button', $form_state)) {
  281. if ($form_state['clicked_button']['#name'] =='cancel_button') {
  282. drupal_goto('/admin/tripal/storage/ws/tripal_sites');
  283. }
  284. else if (!$form_state['values']['tripal_site_id']) {
  285. drupal_set_message(t('Invalid Tripal site id'), 'error');
  286. }
  287. }
  288. }
  289. /**
  290. * Implements hook_form_submit()
  291. *
  292. * Submit function for editing the semantic web term
  293. *
  294. * @param unknown $form
  295. * @param unknown $form_state
  296. */
  297. function tripal_ws_tripal_sites_remove_form_submit($form, &$form_state) {
  298. $id = $form_state['values']['tripal_site_id'];
  299. $name = $form_state['values']['tripal_site_name'];
  300. if ($id) {
  301. db_delete('tripal_sites')
  302. ->condition('id', $id)
  303. ->execute();
  304. drupal_set_message('The Tripal site \'' .$name . '\' has been removed.');
  305. }
  306. drupal_goto('/admin/tripal/storage/ws/tripal_sites');
  307. }