123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- <?php
- /**
- * Provide form to store information of other Tripal sites
- *
- * @param unknown $form
- * @param unknown $form_state
- */
- function tripal_ws_tripal_sites_form($form, &$form_state) {
- $form = array();
- $values = key_exists('values', $form_state) ? $form_state['values'] : NULL;
- $tripal_site = $values ? $values['tripal_site'] : 0;
- $results =
- db_select('tripal_sites', 'ts')
- ->fields('ts')
- ->execute();
- $headers = array('Name', 'URL', 'Version', 'Description', 'Action');
- $rows = array();
- while ($site = $results->fetchObject()) {
- $rows[] = array(
- $site->name,
- $site->url,
- $site->version,
- $site->description,
- array(
- 'data' => l('Edit', '/admin/tripal/storage/ws/tripal_sites/edit/' . $site->id) . ' | ' .
- l('Remove', '/admin/tripal/storage/ws/tripal_sites/remove/' . $site->id),
- 'nowrap' => TRUE,
- ),
- );
- }
- if (count($rows) == 0) {
- $rows[] = array('No configured Tripal site.', array('colspan' => 5));
- }
- $table = array(
- 'header' => $headers,
- 'rows' => $rows,
- 'attributes' => array(),
- 'sticky' => FALSE,
- 'caption' => '',
- 'colgroups' => array(),
- 'empty' => '',
- );
- $output = theme_table($table);
- $form['instructions'] = array(
- '#type' => 'item',
- '#markup' => t('The following is a list of remote Tripal sites with which
- this site can communicate. You may add and remove site as needed.')
- );
- $form['table'] = array(
- '#markup' => $output
- );
- return $form;
- }
- /**
- * Provide form to add/edit a Tripal site
- *
- * @param unknown $form
- * @param unknown $form_state
- */
- function tripal_ws_tripal_sites_edit_form($form, &$form_state, $tripal_site_id = NULL) {
- $id = NULL;
- $name = '';
- $url = '';
- $version = '';
- $description = '';
- if ($tripal_site_id) {
- $site = db_select('tripal_sites', 'ts')
- ->fields('ts')
- ->condition('id', $tripal_site_id)
- ->execute()
- ->fetchObject();
- if (is_object($site)) {
- $id = $site->id;
- $name = $site->name;
- $url = $site->url;
- $version = $site->version;
- $description = $site->description;
- }
- }
- $form = array();
- $form['tripal_site_info']['id'] = array(
- '#type' => 'hidden',
- '#value' => $id,
- );
- $form['tripal_site_info']['name'] = array(
- '#title' => t('Name'),
- '#type' => 'textfield',
- '#description' => t('Full name of the Tripal site.'),
- '#default_value' => $name,
- '#required' => TRUE
- );
- $form['tripal_site_info']['url'] = array(
- '#title' => t('URL'),
- '#type' => 'textfield',
- '#description' => t('The URL of the Tripal site.'),
- '#default_value' => $url,
- '#required' => TRUE
- );
- $form['tripal_site_info']['version'] = array(
- '#title' => t('Version'),
- '#type' => 'textfield',
- '#description' => t('Web services version used by the Tripal site.'),
- '#default_value' => $version,
- );
- $form['tripal_site_info']['description'] = array(
- '#title' => t('Description'),
- '#type' => 'textarea',
- '#default_value' => $description,
- );
- $form['submit_button'] = array(
- '#type' => 'submit',
- '#value' => t('Save'),
- '#name' => 'save'
- );
- $form['cancel_button'] = array(
- '#type' => 'button',
- '#value' => t('Cancel'),
- '#name' => 'cancel_button',
- '#limit_validation_errors' => array()
- );
- return $form;
- }
- /**
- * Implements hook_validate()
- *
- * @param unknown $form
- * @param unknown $form_state
- */
- function tripal_ws_tripal_sites_edit_form_validate($form, &$form_state) {
- if (array_key_exists('clicked_button', $form_state)) {
- if ($form_state['clicked_button']['#name'] =='cancel_button') {
- drupal_goto('/admin/tripal/storage/ws/tripal_sites');
- }
- // Make sure URL does not already exist
- else if ($form_state['clicked_button']['#name'] =='save') {
- $url = $form_state['values']['url'];
- $version = $form_state['values']['version'];
- $name = $form_state['values']['name'];
- $check_url = NULL;
- $check_name = NULL;
- // When updating a record
- if ($form_state['values']['id']) {
- $check_url =
- db_select('tripal_sites', 'ts')
- ->fields('ts', array('id'))
- ->condition('url', $url)
- ->condition('version', $version)
- ->condition('id', $form_state['values']['id'], '<>')
- ->execute()
- ->fetchField();
- $check_name =
- db_select('tripal_sites', 'ts')
- ->fields('ts', array('id'))
- ->condition('name', $name)
- ->condition('id', $form_state['values']['id'], '<>')
- ->execute()
- ->fetchField();
- }
- // When inserting a record
- else {
- $check_url =
- db_select('tripal_sites', 'ts')
- ->fields('ts', array('id'))
- ->condition('url', $url)
- ->condition('version', $version)
- ->execute()
- ->fetchField();
- $check_name =
- db_select('tripal_sites', 'ts')
- ->fields('ts', array('id'))
- ->condition('name', $name)
- ->execute()
- ->fetchField();
- }
- if ($check_url) {
- form_set_error('url', t('The URL and version is used by another site.'));
- form_set_error('version');
- }
- if ($check_name) {
- form_set_error('name', t('The name is used by another site.'));
- }
- }
- }
- }
- /**
- * Implements hook_submit()
- *
- * @param unknown $form
- * @param unknown $form_state
- */
- function tripal_ws_tripal_sites_edit_form_submit($form, &$form_state) {
- $id = $form_state['values']['id'];
- $name = $form_state['values']['name'];
- $url = $form_state['values']['url'];
- $version = $form_state['values']['version'];
- $description = $form_state['values']['description'];
- // If there is an 'id' do an update, otherwise do an insert
- if ($id) {
- db_update('tripal_sites')
- ->fields(array(
- 'name' => $name,
- 'url' => $url,
- 'version' => $version,
- 'description' => $description
- ))
- ->condition('id', $id)
- ->execute();
- drupal_set_message(t('Tripal site \'' . $name . '\' has been updated.'));
- }
- else {
- db_insert('tripal_sites')
- ->fields(array(
- 'name' => $name,
- 'url' => $url,
- 'version' => $version,
- 'description' => $description
- ))
- ->execute();
- drupal_set_message(t('Tripal site \'' . $name . '\' has been added.'));
- }
- drupal_goto('/admin/tripal/storage/ws/tripal_sites');
- }
- /**
- * Implements hook_form()
- * Reset term used by semantic web
- *
- * @param $form
- * @param $form_state
- * @param $table
- * @param $column
- * @return $form
- */
- function tripal_ws_tripal_sites_remove_form($form, &$form_state, $id = NULL) {
- $name = '';
- $record_id = '';
- if ($id) {
- $site = db_select('tripal_sites', 'ts')
- ->fields('ts')
- ->condition('id', $id)
- ->execute()
- ->fetchObject();
- if (is_object($site)) {
- $record_id = $site->id;
- $name = $site->name;
- }
- }
- $form['confirmation'] = array(
- '#markup' => 'Really remove the \'' . $name . '\' Tripal site? ',
- );
- $form['tripal_site_id'] = array(
- '#type' => 'value',
- '#value' => $record_id
- );
- $form['tripal_site_name'] = array(
- '#type' => 'value',
- '#value' => $name
- );
- $form['submit_button'] = array(
- '#type' => 'submit',
- '#value' => t('Remove'),
- '#name' => 'remove'
- );
- $form['cancel_button'] = array(
- '#type' => 'button',
- '#value' => t('Cancel'),
- '#name' => 'cancel_button',
- '#limit_validation_errors' => array()
- );
- return $form;
- }
- /**
- * Implements hook_form_validate()
- *
- * Validate function for resetting the semantic web term
- *
- * @param unknown $form
- * @param unknown $form_state
- */
- function tripal_ws_tripal_sites_remove_form_validate($form, &$form_state) {
- if (array_key_exists('clicked_button', $form_state)) {
- if ($form_state['clicked_button']['#name'] =='cancel_button') {
- drupal_goto('/admin/tripal/storage/ws/tripal_sites');
- }
- else if (!$form_state['values']['tripal_site_id']) {
- drupal_set_message(t('Invalid Tripal site id'), 'error');
- }
- }
- }
- /**
- * Implements hook_form_submit()
- *
- * Submit function for editing the semantic web term
- *
- * @param unknown $form
- * @param unknown $form_state
- */
- function tripal_ws_tripal_sites_remove_form_submit($form, &$form_state) {
- $id = $form_state['values']['tripal_site_id'];
- $name = $form_state['values']['tripal_site_name'];
- if ($id) {
- db_delete('tripal_sites')
- ->condition('id', $id)
- ->execute();
- drupal_set_message('The Tripal site \'' .$name . '\' has been removed.');
- }
- drupal_goto('/admin/tripal/storage/ws/tripal_sites');
- }
|