You are here

entity_share_ui_client.config.admin.inc in Entity Share 7

Entity Share UI Client Admin Configuration file.

File

modules/entity_share_ui/modules/entity_share_ui_client/entity_share_ui_client.config.admin.inc
View source
<?php

/**
 * @file
 * Entity Share UI Client Admin Configuration file.
 */
define('ENDPOINT_MAIN_PATH', 'admin/config/entity_share/client');

/**
 * Node list table.
 *
 * @return array
 *   The render array of the endpoint list.
 */
function entity_share_ui_endpoint_list() {
  drupal_set_title(t('Entity Share Client - Endpoints'));

  // Table header.
  $header = array(
    'title' => array(
      'data' => t('Title'),
    ),
    'description' => array(
      'data' => t('Description'),
    ),
    'url' => array(
      'data' => t('URL'),
    ),
    'enable' => array(
      'data' => t('Enable'),
    ),
    'debug' => array(
      'data' => t('Debug'),
    ),
    'actions' => array(
      'data' => t('Action'),
    ),
  );
  $endpoints = EntityShareUiClientEndpoint::loadAll();
  $rows = array();
  if ($endpoints) {
    foreach ($endpoints as $endpoint) {
      $rows[$endpoint['eid']] = array(
        'title' => array(
          'data' => array(
            '#type' => 'link',
            '#title' => $endpoint['title'],
            '#href' => 'admin/entity_share/endpoint/' . $endpoint['name'] . '/edit',
          ),
        ),
        'description' => check_plain($endpoint['description']),
        'url' => $endpoint['url'],
        'enable' => $endpoint['enabled'] ? t('Enabled') : t('Disabled'),
        'debug' => $endpoint['debug'] ? t('Enabled') : t('Disabled'),
      );
      $rows[$endpoint['eid']]['actions'] = array(
        'data' => array(
          '#theme' => 'links',
          '#attributes' => array(
            'class' => array(
              'links',
              'inline',
            ),
          ),
          '#links' => array(
            'edit' => array(
              'title' => t('edit'),
              'href' => 'admin/entity_share/endpoint/' . $endpoint['name'] . '/edit',
            ),
            'delete' => array(
              'title' => t('delete'),
              'href' => 'admin/entity_share/endpoint/' . $endpoint['name'] . '/delete',
            ),
          ),
        ),
      );
    }
  }

  // Create a render array ($build) which will be themed as a table.
  $build = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => t('There is no endpoint...'),
  );
  return $build;
}

/**
 * Endpoint edit or create form.
 *
 * @param array $form
 *   The form array.
 * @param array $form_state
 *   The form state.
 * @param string $name
 *   (Optional) The endpoint name.
 *
 * @return array
 *   The form render array.
 */
function entity_share_ui_endpoint_edit_form(array $form, array &$form_state, $name = NULL) {

  // Load endpoint data to reinject it if $name exists.
  if (!empty($name)) {
    $endpoint = EntityShareUiClientEndpoint::load($name);
    $button_name = t('Update');
    $action = 'update';
    drupal_set_title(t('Edit the endpoint @endpoint', array(
      '@endpoint' => !empty($endpoint['title']) ? $endpoint['title'] : $name,
    )));
  }
  else {
    $button_name = t('Create');
    $action = 'create';
    drupal_set_title(t('Create a new endpoint'));
  }

  // Form avec champ de type machine_name.
  $form = array();
  $form['name'] = array(
    '#type' => 'machine_name',
    '#machine_name' => array(
      'exists' => '_entity_share_ui_endpoint_name_exists',
    ),
    '#default_value' => isset($endpoint['name']) ? $endpoint['name'] : '',
    '#disabled' => !empty($endpoint['name']),
  );
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#maxlength' => 128,
    '#required' => TRUE,
    '#default_value' => isset($endpoint['title']) ? $endpoint['title'] : '',
  );
  $form['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#default_value' => isset($endpoint['description']) ? $endpoint['description'] : '',
  );
  $form['url'] = array(
    '#type' => 'textfield',
    '#title' => t('Url'),
    '#maxlength' => 255,
    '#required' => TRUE,
    '#default_value' => isset($endpoint['url']) ? $endpoint['url'] : '',
  );

  // Authentication.
  $form['authentication'] = array(
    '#type' => 'fieldset',
    '#title' => t('Authentication'),
  );
  $form['authentication']['login'] = array(
    '#type' => 'textfield',
    '#title' => t('Login'),
    '#description' => t('User login to access the endpoint on the server side'),
    '#maxlength' => 128,
    '#required' => TRUE,
    '#default_value' => isset($endpoint['login']) ? $endpoint['login'] : '',
  );
  $form['authentication']['password'] = array(
    '#type' => 'password',
    '#title' => t('Password'),
    '#description' => t('User password to access the endpoint on the server side'),
    '#maxlength' => 128,
    '#required' => $action == 'create' ? TRUE : FALSE,
    '#default_value' => isset($endpoint['password']) ? $endpoint['password'] : '',
  );
  $form['enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable endpoint'),
    '#default_value' => isset($endpoint['enabled']) ? $endpoint['enabled'] : 1,
  );
  $form['debug'] = array(
    '#type' => 'checkbox',
    '#title' => t('Debug mode'),
    '#default_value' => isset($endpoint['debug']) ? $endpoint['debug'] : 0,
  );
  $form['eid'] = array(
    '#type' => 'hidden',
    '#value' => isset($endpoint['eid']) ? $endpoint['eid'] : '',
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => $button_name,
    '#submit' => array(
      'entity_share_ui_endpoint_edit_form_submit',
    ),
  );
  $form['cancel'] = array(
    '#type' => 'link',
    '#title' => t('Cancel'),
    '#href' => ENDPOINT_MAIN_PATH,
  );
  return $form;
}

/**
 * Submit of the endpoint form.
 *
 * @param array $form
 *   The form array.
 * @param array $form_state
 *   The form state.
 */
function entity_share_ui_endpoint_edit_form_submit(array $form, array &$form_state) {
  $endpoint = array(
    'name' => $form_state['values']['name'],
    'title' => $form_state['values']['title'],
    'description' => $form_state['values']['description'],
    'url' => $form_state['values']['url'],
    'login' => $form_state['values']['login'],
    'password' => $form_state['values']['password'],
    'enabled' => $form_state['values']['enabled'],
    'debug' => $form_state['values']['debug'],
  );
  if (!empty($form_state['values']['eid'])) {
    $endpoint['eid'] = $form_state['values']['eid'];
  }
  $result = EntityShareUiClientEndpoint::save($endpoint);
  if ($result) {
    drupal_set_message(t('Endpoint @endpoint was successfully saved', array(
      '@endpoint' => $endpoint['title'],
    )));
  }
  else {
    drupal_set_message(t('An error occurred while saving endpoint @endpoint.', array(
      '@endpoint' => $endpoint['title'],
    )), 'error');
  }
  $form_state['redirect'] = ENDPOINT_MAIN_PATH;
}

/**
 * Check if the endpoint machine_name already exists.
 *
 * @param string $name
 *   Name of the endpoint.
 *
 * @return bool
 *   TRUE if endpoint name exists, FALSE otherwise.
 */
function _entity_share_ui_endpoint_name_exists($name) {
  return is_array(EntityShareUiClientEndpoint::load($name)) ? TRUE : FALSE;
}

/**
 * Endpoint deletion form.
 *
 * @param array $form
 *   The form array.
 * @param array $form_state
 *   The form state.
 * @param string $name
 *   Name of the endpoint.
 *
 * @return array
 *   The confirm form.
 */
function entity_share_ui_endpoint_delete_form(array $form, array &$form_state, $name) {
  $endpoint = EntityShareUiClientEndpoint::load($name);
  if (!$endpoint) {
    drupal_set_message(t('Invalid endpoint @endpoint', array(
      '@endpoint' => $name,
    )), 'error');
    drupal_goto('admin/config/entity_share/client');
  }
  $form = array();
  $form['endpoint'] = array(
    '#type' => 'hidden',
    '#value' => $name,
  );
  $form['#submit'] = array(
    'entity_share_ui_endpoint_delete_form_submit',
  );
  return confirm_form($form, t('Do you want to delete the endpoint @endpoint ?', array(
    '@endpoint' => $endpoint['title'],
  )), 'admin/config/entity_share/client', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
}

/**
 * Form submission handler for entity_share_ui_endpoint_delete_form().
 *
 * @param array $form
 *   The form array.
 * @param array $form_state
 *   The form state.
 */
function entity_share_ui_endpoint_delete_form_submit(array $form, array &$form_state) {
  $endpoint = $form_state['values']['endpoint'];
  if ($form_state['values']['confirm']) {
    if (EntityShareUiClientEndpoint::delete($endpoint)) {
      drupal_set_message(t('Endpoint @endpoint successfully deleted', array(
        '@endpoint' => $endpoint,
      )));
    }
    else {
      drupal_set_message(t('An error occurred while deleting endpoint @endpoint', array(
        '@endpoint' => $endpoint,
      )), 'error');
    }
  }
  $form_state['redirect'] = ENDPOINT_MAIN_PATH;
}

Functions

Namesort descending Description
entity_share_ui_endpoint_delete_form Endpoint deletion form.
entity_share_ui_endpoint_delete_form_submit Form submission handler for entity_share_ui_endpoint_delete_form().
entity_share_ui_endpoint_edit_form Endpoint edit or create form.
entity_share_ui_endpoint_edit_form_submit Submit of the endpoint form.
entity_share_ui_endpoint_list Node list table.
_entity_share_ui_endpoint_name_exists Check if the endpoint machine_name already exists.

Constants

Namesort descending Description
ENDPOINT_MAIN_PATH @file Entity Share UI Client Admin Configuration file.