tripal_ws.admin.inc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 = [];
  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 = ['Name', 'URL', 'Version', 'Description', 'Action'];
  17. $rows = [];
  18. while ($site = $results->fetchObject()) {
  19. $rows[] = [
  20. $site->name,
  21. $site->url,
  22. $site->version,
  23. $site->description,
  24. [
  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[] = ['No configured Tripal site.', ['colspan' => 5]];
  33. }
  34. $table = [
  35. 'header' => $headers,
  36. 'rows' => $rows,
  37. 'attributes' => [],
  38. 'sticky' => FALSE,
  39. 'caption' => '',
  40. 'colgroups' => [],
  41. 'empty' => '',
  42. ];
  43. $output = theme_table($table);
  44. $form['instructions'] = [
  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'] = [
  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 = [];
  81. $form['tripal_site_info']['id'] = [
  82. '#type' => 'hidden',
  83. '#value' => $id,
  84. ];
  85. $form['tripal_site_info']['name'] = [
  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'] = [
  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'] = [
  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'] = [
  106. '#title' => t('Description'),
  107. '#type' => 'textarea',
  108. '#default_value' => $description,
  109. ];
  110. $form['submit_button'] = [
  111. '#type' => 'submit',
  112. '#value' => t('Save'),
  113. '#name' => 'save',
  114. ];
  115. $form['cancel_button'] = [
  116. '#type' => 'button',
  117. '#value' => t('Cancel'),
  118. '#name' => 'cancel_button',
  119. '#limit_validation_errors' => [],
  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 {
  136. if ($form_state['clicked_button']['#name'] == 'save') {
  137. $url = $form_state['values']['url'];
  138. $version = $form_state['values']['version'];
  139. $name = $form_state['values']['name'];
  140. $check_url = NULL;
  141. $check_name = NULL;
  142. // When updating a record
  143. if ($form_state['values']['id']) {
  144. $check_url =
  145. db_select('tripal_sites', 'ts')
  146. ->fields('ts', ['id'])
  147. ->condition('url', $url)
  148. ->condition('version', $version)
  149. ->condition('id', $form_state['values']['id'], '<>')
  150. ->execute()
  151. ->fetchField();
  152. $check_name =
  153. db_select('tripal_sites', 'ts')
  154. ->fields('ts', ['id'])
  155. ->condition('name', $name)
  156. ->condition('id', $form_state['values']['id'], '<>')
  157. ->execute()
  158. ->fetchField();
  159. }
  160. // When inserting a record
  161. else {
  162. $check_url =
  163. db_select('tripal_sites', 'ts')
  164. ->fields('ts', ['id'])
  165. ->condition('url', $url)
  166. ->condition('version', $version)
  167. ->execute()
  168. ->fetchField();
  169. $check_name =
  170. db_select('tripal_sites', 'ts')
  171. ->fields('ts', ['id'])
  172. ->condition('name', $name)
  173. ->execute()
  174. ->fetchField();
  175. }
  176. if ($check_url) {
  177. form_set_error('url', t('The URL and version is used by another site.'));
  178. form_set_error('version');
  179. }
  180. if ($check_name) {
  181. form_set_error('name', t('The name is used by another site.'));
  182. }
  183. }
  184. }
  185. }
  186. }
  187. /**
  188. * Implements hook_submit()
  189. *
  190. * @param unknown $form
  191. * @param unknown $form_state
  192. */
  193. function tripal_ws_tripal_sites_edit_form_submit($form, &$form_state) {
  194. $id = $form_state['values']['id'];
  195. $name = $form_state['values']['name'];
  196. $url = $form_state['values']['url'];
  197. $version = $form_state['values']['version'];
  198. $description = $form_state['values']['description'];
  199. // If there is an 'id' do an update, otherwise do an insert
  200. if ($id) {
  201. db_update('tripal_sites')
  202. ->fields([
  203. 'name' => $name,
  204. 'url' => $url,
  205. 'version' => $version,
  206. 'description' => $description,
  207. ])
  208. ->condition('id', $id)
  209. ->execute();
  210. drupal_set_message(t('Tripal site \'' . $name . '\' has been updated.'));
  211. }
  212. else {
  213. db_insert('tripal_sites')
  214. ->fields([
  215. 'name' => $name,
  216. 'url' => $url,
  217. 'version' => $version,
  218. 'description' => $description,
  219. ])
  220. ->execute();
  221. drupal_set_message(t('Tripal site \'' . $name . '\' has been added.'));
  222. }
  223. drupal_goto('/admin/tripal/storage/ws/tripal_sites');
  224. }
  225. /**
  226. * Implements hook_form()
  227. * Reset term used by semantic web
  228. *
  229. * @param $form
  230. * @param $form_state
  231. * @param $table
  232. * @param $column
  233. *
  234. * @return $form
  235. */
  236. function tripal_ws_tripal_sites_remove_form($form, &$form_state, $id = NULL) {
  237. $name = '';
  238. $record_id = '';
  239. if ($id) {
  240. $site = db_select('tripal_sites', 'ts')
  241. ->fields('ts')
  242. ->condition('id', $id)
  243. ->execute()
  244. ->fetchObject();
  245. if (is_object($site)) {
  246. $record_id = $site->id;
  247. $name = $site->name;
  248. }
  249. }
  250. $form['confirmation'] = [
  251. '#markup' => 'Really remove the \'' . $name . '\' Tripal site? ',
  252. ];
  253. $form['tripal_site_id'] = [
  254. '#type' => 'value',
  255. '#value' => $record_id,
  256. ];
  257. $form['tripal_site_name'] = [
  258. '#type' => 'value',
  259. '#value' => $name,
  260. ];
  261. $form['submit_button'] = [
  262. '#type' => 'submit',
  263. '#value' => t('Remove'),
  264. '#name' => 'remove',
  265. ];
  266. $form['cancel_button'] = [
  267. '#type' => 'button',
  268. '#value' => t('Cancel'),
  269. '#name' => 'cancel_button',
  270. '#limit_validation_errors' => [],
  271. ];
  272. return $form;
  273. }
  274. /**
  275. * Implements hook_form_validate()
  276. *
  277. * Validate function for resetting the semantic web term
  278. *
  279. * @param unknown $form
  280. * @param unknown $form_state
  281. */
  282. function tripal_ws_tripal_sites_remove_form_validate($form, &$form_state) {
  283. if (array_key_exists('clicked_button', $form_state)) {
  284. if ($form_state['clicked_button']['#name'] == 'cancel_button') {
  285. drupal_goto('/admin/tripal/storage/ws/tripal_sites');
  286. }
  287. else {
  288. if (!$form_state['values']['tripal_site_id']) {
  289. drupal_set_message(t('Invalid Tripal site id'), 'error');
  290. }
  291. }
  292. }
  293. }
  294. /**
  295. * Implements hook_form_submit()
  296. *
  297. * Submit function for editing the semantic web term
  298. *
  299. * @param unknown $form
  300. * @param unknown $form_state
  301. */
  302. function tripal_ws_tripal_sites_remove_form_submit($form, &$form_state) {
  303. $id = $form_state['values']['tripal_site_id'];
  304. $name = $form_state['values']['tripal_site_name'];
  305. if ($id) {
  306. db_delete('tripal_sites')
  307. ->condition('id', $id)
  308. ->execute();
  309. drupal_set_message('The Tripal site \'' . $name . '\' has been removed.');
  310. }
  311. drupal_goto('/admin/tripal/storage/ws/tripal_sites');
  312. }