/**
 * services_edit_form_endpoint_resources_submit function.
 *
 * @param array $form
 * @param array $form_state
 * @return void
 */
function services_edit_form_endpoint_resources_submit($form, &$form_state)
{
    $endpoint = $form_state['values']['endpoint_object'];
    $existing_resources = _services_build_resources();
    // Apply the endpoint in a non-strict mode, so that the non-active resources
    // are preserved.
    _services_apply_endpoint($existing_resources, $endpoint, FALSE);
    $resources = $form_state['values'];
    $endpoint = $form_state['values']['endpoint_object'];
    foreach ($resources as $path => $state) {
        if (strpos($path, '-') === FALSE || empty($state)) {
            continue;
        }
        $split_path = explode('-', $path);
        $resource = $split_path[0];
        $method = $split_path[1];
        // If method is alias.
        if ($method == 'alias') {
            $final_resource[$resource]['alias'] = $state;
            continue;
        }
        // If it is action, relationship, or targeted action.
        if (isset($split_path[2])) {
            $final_resource[$resource][$split_path[2]][$method]['enabled'] = 1;
            continue;
        }
        // If it is operation.
        $final_resource[$resource]['operations'][$method]['enabled'] = 1;
    }
    $endpoint->resources = $final_resource;
    services_endpoint_save($endpoint);
    drupal_set_message(t('Resources have been saved'));
}
/**
 * services_edit_form_endpoint_resources function.
 *
 * @param array &$form_state
 * @param object $endpoint
 * @return Form
 */
function services_edit_form_endpoint_resources(&$form_state, $endpoint)
{
    module_load_include('resource_build.inc', 'services');
    module_load_include('runtime.inc', 'services');
    $form = array();
    drupal_add_js('misc/tableselect.js');
    drupal_add_js(drupal_get_path('module', 'services') . '/js/services.admin.js');
    drupal_add_css(drupal_get_path('module', 'services') . '/css/services.admin.css');
    $form['endpoint_object'] = array('#type' => 'value', '#value' => $endpoint);
    $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();
    // Apply the endpoint in a non-strict mode, so that the non-active resources
    // are preserved.
    _services_apply_endpoint($resources, $endpoint, FALSE);
    $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);
    // Collect authentication module info for later use and
    // append the default settings for authentication modules
    $auth_info = array();
    $class_names = services_operation_class_info();
    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, '#collapsible' => TRUE);
        $alias = '';
        if (isset($endpoint->resources[$resource_name]['alias'])) {
            $alias = $endpoint->resources[$resource_name]['alias'];
        } elseif (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', '#default_value' => $default_value);
                    $controller_settings = array();
                    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;
}