/**
 * Endpoint authentication configuration form.
 */
function services_edit_form_endpoint_authentication($form_state)
{
    $endpoint = services_endpoint_load(arg(4));
    // Loading runtime include as needed by services_authentication_info().
    module_load_include('runtime.inc', 'services');
    $form = array();
    $auth_modules = module_implements('services_authentication_info');
    $form['endpoint_object'] = array('#type' => 'value', '#value' => $endpoint);
    if (empty($auth_modules)) {
        $form['message'] = array('#type' => 'item', '#title' => t('Authentication'), '#description' => t('No authentication modules are installed, all requests will be anonymous.'));
        return $form;
    }
    if (empty($endpoint->authentication)) {
        $form['message'] = array('#type' => 'item', '#title' => t('Authentication'), '#description' => t('No authentication modules are enabled, all requests will be anonymous.'));
        return $form;
    }
    // Add configuration fieldsets for the authentication modules
    foreach ($endpoint->authentication as $module => $settings) {
        $info = services_authentication_info($module);
        if (empty($info)) {
            continue;
        }
        $form[$module] = array('#type' => 'fieldset', '#title' => isset($info['title']) ? $info['title'] : $module, '#tree' => TRUE);
        $module_settings_form = services_auth_invoke($module, 'security_settings', $settings);
        if (!empty($module_settings_form) && $module_settings_form !== TRUE && $settings == $module || is_array($settings)) {
            $form[$module] += $module_settings_form;
        } else {
            $form[$module]['message'] = array('#type' => 'item', '#value' => t('@module has no settings available.', array('@module' => drupal_ucfirst($module))));
        }
    }
    $form['submit'] = array('#type' => 'submit', '#value' => 'Save');
    return $form;
}
/**
 * services_edit_form_endpoint_resources function.
 *
 * @param array &$form_state
 * @param object $endpoint
 * @return Form
 */
function services_edit_form_endpoint_resources($form, &$form_state, $endpoint)
{
    module_load_include('resource_build.inc', 'services');
    module_load_include('runtime.inc', 'services');
    $form = array();
    $form['endpoint_object'] = array('#type' => 'value', '#value' => $endpoint);
    $form['#attached']['js'] = array('misc/tableselect.js', drupal_get_path('module', 'services') . '/js/services.admin.js');
    $form['#attached']['css'] = array(drupal_get_path('module', 'services') . '/css/services.admin.css');
    $ops = array('create' => t('Create'), 'retrieve' => t('Retrieve'), 'update' => t('Update'), 'delete' => t('Delete'), 'index' => t('Index'));
    // Call _services_build_resources() directly instead of
    // services_get_resources to bypass caching.
    $resources = _services_build_resources($endpoint->name);
    $form['instructions'] = array('#type' => 'item', '#title' => t('Resources'), '#description' => t('Select the resource(s) or methods you would like to enable, and click <em>Save</em>.'));
    $form['resources'] = array('#theme' => 'services_resource_table', '#tree' => TRUE);
    $class_names = services_operation_class_info();
    // Collect authentication module info for later use and
    // append the default settings for authentication modules
    $auth_info = array();
    foreach ($endpoint->authentication as $module => $settings) {
        $auth_info[$module] = services_authentication_info($module);
        // Append the default settings for the authentication module.
        $default_settings = services_auth_invoke($module, 'default_security_settings');
        if (is_array($default_settings) && is_array($settings)) {
            $settings += $default_settings;
        }
        $endpoint->authentication[$module] = $settings;
    }
    // Generate the list of methods arranged by resource.
    foreach ($resources as $resource_name => $resource) {
        $resource_conf = array();
        if (isset($endpoint->resources[$resource_name])) {
            $resource_conf = $endpoint->resources[$resource_name];
        }
        $res_item = array('#collapsed' => TRUE);
        $alias = '';
        if (isset($form_state['input'][$resource_name]['alias'])) {
            $alias = $form_state['input'][$resource_name]['alias'];
        } else {
            if (isset($resource_conf['alias'])) {
                $alias = $resource_conf['alias'];
            }
        }
        $res_item['alias'] = array('#type' => 'textfield', '#default_value' => $alias, '#size' => 20);
        foreach ($class_names as $class => $info) {
            if (!empty($resource[$class])) {
                $res_item[$class] = array('#type' => 'item', '#title' => $info['title']);
                foreach ($resource[$class] as $op_name => $op) {
                    $description = isset($op['help']) ? $op['help'] : t('No description is available');
                    $default_value = 0;
                    if (isset($resource_conf[$class][$op_name]['enabled'])) {
                        $default_value = $resource_conf[$class][$op_name]['enabled'];
                    }
                    $res_item[$class][$op_name] = array('#type' => 'item', '#title' => $op_name, '#description' => $description);
                    $res_item[$class][$op_name]['enabled'] = array('#type' => 'checkbox', '#title' => t('Enabled'), '#default_value' => $default_value);
                    $controller_settings = array();
                    // Let modules add their own settings.
                    drupal_alter('controller_settings', $controller_settings);
                    // Get service update versions.
                    $update_versions = services_get_update_versions($resource_name, $op_name);
                    $options = array('1.0' => '1.0');
                    $options = array_merge($options, $update_versions);
                    $default_api_value = 0;
                    if (isset($endpoint->resources[$resource_name][$class][$op_name]['settings']['services'])) {
                        $default_api_value = $endpoint->resources[$resource_name][$class][$op_name]['settings']['services'];
                    }
                    // Add the version information.
                    $controller_settings['services'] = array('#title' => 'Services', '#type' => 'item', 'resource_api_version' => array('#type' => 'select', '#options' => $options, '#default_value' => $default_api_value, '#title' => 'Resource API Version', '#disabled' => count($options) == 1 ? TRUE : FALSE));
                    foreach ($endpoint->authentication as $module => $settings) {
                        $auth_settings = services_auth_invoke($module, 'controller_settings', $settings, $op, $endpoint->authentication[$module], $class, $op_name);
                        if (is_array($auth_settings)) {
                            $auth_settings = array('#title' => $auth_info[$module]['title'], '#type' => 'item') + $auth_settings;
                            $controller_settings[$module] = $auth_settings;
                        }
                    }
                    $res_item[$class][$op_name]['settings'] = $controller_settings;
                }
            }
        }
        $form['resources'][$resource_name] = $res_item;
    }
    $form['save'] = array('#type' => 'submit', '#value' => t('Save'));
    return $form;
}