PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/export_ui/services_ctools_export_ui.class.php

https://bitbucket.org/luksak/services
PHP | 399 lines | 292 code | 45 blank | 62 comment | 44 complexity | 3835e22361a05457e84a03b7f267e8fa MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Export-ui handler for the Services module.
  5. */
  6. class services_ctools_export_ui extends ctools_export_ui {
  7. /**
  8. * Page callback for the resources page.
  9. */
  10. function resources_page($js, $input, $item) {
  11. drupal_set_title($this->get_page_title('resources', $item));
  12. return drupal_get_form('services_edit_form_endpoint_resources', $item);
  13. }
  14. /**
  15. * Page callback for the server page.
  16. */
  17. function server_page($js, $input, $item) {
  18. drupal_set_title($this->get_page_title('server', $item));
  19. return drupal_get_form('services_edit_form_endpoint_server', $item);
  20. }
  21. /**
  22. * Page callback for the authentication page.
  23. */
  24. function authentication_page($js, $input, $item) {
  25. drupal_set_title($this->get_page_title('authentication', $item));
  26. return drupal_get_form('services_edit_form_endpoint_authentication', $item);
  27. }
  28. // Avoid standard submit of edit form by ctools.
  29. function edit_save_form($form_state) { }
  30. function set_item_state($state, $js, $input, $item) {
  31. ctools_export_set_object_status($item, $state);
  32. menu_rebuild();
  33. if (!$js) {
  34. drupal_goto(ctools_export_ui_plugin_base_path($this->plugin));
  35. }
  36. else {
  37. return $this->list_page($js, $input);
  38. }
  39. }
  40. }
  41. /**
  42. * Endpoint authentication configuration form.
  43. */
  44. function services_edit_form_endpoint_authentication($form, &$form_state) {
  45. list($endpoint) = $form_state['build_info']['args'];
  46. // Loading runtime include as needed by services_authentication_info().
  47. module_load_include('runtime.inc', 'services');
  48. $auth_modules = module_implements('services_authentication_info');
  49. $form['endpoint_object'] = array(
  50. '#type' => 'value',
  51. '#value' => $endpoint,
  52. );
  53. if (empty($auth_modules)) {
  54. $form['message'] = array(
  55. '#type' => 'item',
  56. '#title' => t('Authentication'),
  57. '#description' => t('No authentication modules are installed, all requests will be anonymous.'),
  58. );
  59. return $form;
  60. }
  61. if (empty($endpoint->authentication)) {
  62. $form['message'] = array(
  63. '#type' => 'item',
  64. '#title' => t('Authentication'),
  65. '#description' => t('No authentication modules are enabled, all requests will be anonymous.'),
  66. );
  67. return $form;
  68. }
  69. // Add configuration fieldsets for the authentication modules
  70. foreach ($endpoint->authentication as $module => $settings) {
  71. $info = services_authentication_info($module);
  72. if (empty($info)) {
  73. continue;
  74. }
  75. $form[$module] = array(
  76. '#type' => 'fieldset',
  77. '#title' => isset($info['title']) ? $info['title'] : $module,
  78. '#tree' => TRUE,
  79. );
  80. $module_settings_form = services_auth_invoke($module, 'security_settings', $settings);
  81. if (!empty($module_settings_form) && $module_settings_form !== TRUE && $settings == $module || is_array($settings)) {
  82. $form[$module] += $module_settings_form;
  83. }
  84. else {
  85. $form[$module]['message'] = array(
  86. '#type' => 'item',
  87. '#markup' => t('@module has no settings available.', array('@module' => drupal_ucfirst($module))),
  88. );
  89. }
  90. }
  91. $form['submit'] = array(
  92. '#type' => 'submit',
  93. '#value' => 'Save',
  94. );
  95. return $form;
  96. }
  97. function services_edit_form_endpoint_authentication_submit($form, $form_state) {
  98. $endpoint = $form_state['values']['endpoint_object'];
  99. foreach (array_keys($endpoint->authentication) as $module) {
  100. if (isset($form_state['values'][$module])) {
  101. $endpoint->authentication[$module] = $form_state['values'][$module];
  102. }
  103. }
  104. drupal_set_message(t('Your authentication options have been saved.'));
  105. services_endpoint_save($endpoint);
  106. }
  107. function services_edit_form_endpoint_server($form, &$form_state) {
  108. list($endpoint) = $form_state['build_info']['args'];
  109. $servers = services_get_servers();
  110. $server = !empty($servers[$endpoint->server]) ? $servers[$endpoint->server] : FALSE;
  111. $form['endpoint_object'] = array(
  112. '#type' => 'value',
  113. '#value' => $endpoint,
  114. );
  115. if (!$server) {
  116. $form['message'] = array(
  117. '#type' => 'item',
  118. '#title' => t('Unknown server @name', array('@name' => $endpoint->server)),
  119. '#description' => t('No server matching the one used in the endpoint.'),
  120. );
  121. }
  122. else if (empty($server['settings'])) {
  123. $form['message'] = array(
  124. '#type' => 'item',
  125. '#title' => t('@name has no settings', array('@name' => $endpoint->server)),
  126. '#description' => t("The server doesn't have any settings that needs to be configured."),
  127. );
  128. }
  129. else {
  130. $definition = $server['settings'];
  131. $settings = isset($endpoint->server_settings[$endpoint->server]) ? $endpoint->server_settings[$endpoint->server] : array();
  132. if (!empty($definition['file'])) {
  133. call_user_func_array('module_load_include', $definition['file']);
  134. }
  135. $form[$endpoint->server] = array(
  136. '#type' => 'fieldset',
  137. '#title' => $server['name'],
  138. '#tree' => TRUE,
  139. );
  140. call_user_func_array($definition['form'], array(&$form[$endpoint->server], $endpoint, $settings));
  141. $form['submit'] = array(
  142. '#type' => 'submit',
  143. '#value' => 'Save',
  144. );
  145. }
  146. return $form;
  147. }
  148. function services_edit_form_endpoint_server_submit($form, $form_state) {
  149. $endpoint = $form_state['values']['endpoint_object'];
  150. $servers = services_get_servers();
  151. $definition = $servers[$endpoint->server]['settings'];
  152. $values = $form_state['values'][$endpoint->server];
  153. // Allow the server to alter the submitted values before they're stored
  154. // as settings.
  155. if (!empty($definition['submit'])) {
  156. if (!empty($definition['file'])) {
  157. call_user_func_array('module_load_include', $definition['file']);
  158. }
  159. $values = call_user_func_array($definition['submit'], array($endpoint, &$values));
  160. }
  161. // Store the settings in the endpoint
  162. $endpoint->server_settings[$endpoint->server] = $values;
  163. services_endpoint_save($endpoint);
  164. drupal_set_message(t('Your server settings have been saved.'));
  165. }
  166. /**
  167. * services_edit_endpoint_resources function.
  168. *
  169. * Edit Resources endpoint form
  170. * @param object $endpoint
  171. * @return string The form to be displayed
  172. */
  173. function services_edit_endpoint_resources($endpoint) {
  174. if (!is_object($endpoint)) {
  175. $endpoint = services_endpoint_load($endpoint);
  176. }
  177. if ($endpoint && !empty($endpoint->title)) {
  178. drupal_set_title($endpoint->title);
  179. }
  180. return drupal_get_form('services_edit_form_endpoint_resources', $endpoint);
  181. }
  182. /**
  183. * services_edit_form_endpoint_resources function.
  184. *
  185. * @param array &$form_state
  186. * @param object $endpoint
  187. * @return Form
  188. */
  189. function services_edit_form_endpoint_resources($form, &$form_state, $endpoint) {
  190. module_load_include('resource_build.inc', 'services');
  191. $form = array();
  192. $form['endpoint_object'] = array(
  193. '#type' => 'value',
  194. '#value' => $endpoint,
  195. );
  196. $form['#attached']['js'] = array(
  197. 'misc/tableselect.js',
  198. drupal_get_path('module', 'services') . '/js/services.admin.js',
  199. );
  200. $form['#attached']['css'] = array(
  201. drupal_get_path('module', 'services') . '/css/services.admin.css',
  202. );
  203. $ops = array(
  204. 'create' => t('Create'),
  205. 'retrieve' => t('Retrieve'),
  206. 'update' => t('Update'),
  207. 'delete' => t('Delete'),
  208. 'index' => t('Index'),
  209. );
  210. // Call _services_build_resources() directly instead of
  211. // services_get_resources to bypass caching.
  212. $resources = _services_build_resources();
  213. // Apply the endpoint in a non-strict mode, so that the non-active resources
  214. // are preserved.
  215. _services_apply_endpoint($resources, $endpoint, FALSE);
  216. $form['resources'] = array(
  217. '#type' => 'fieldset',
  218. '#title' => t('Resources'),
  219. '#description' => t('Select the resource(s) or resource group(s) you would like to enable, and click <em>Save</em>.'),
  220. );
  221. $form['resources']['table'] = array(
  222. '#theme' => 'services_resource_table',
  223. );
  224. $ignoreArray = array('actions', 'relationships', 'endpoint', 'name', 'file', 'targeted_actions');
  225. // Generate the list of methods arranged by resource.
  226. foreach ($resources as $resource => $methods) {
  227. $form['resources']['table'][$resource] = array(
  228. '#collapsed' => TRUE,
  229. );
  230. $alias = '';
  231. if (isset($form_state['build_info']['args'][0]->resources[$resource]['alias'])) {
  232. $alias = $form_state['build_info']['args'][0]->resources[$resource]['alias'];
  233. }
  234. elseif (isset($form_state['input'][$resource . '/alias'])) {
  235. $alias = $form_state['input'][$resource . '/alias'];
  236. }
  237. $form['resources']['table'][$resource]['alias'] = array(
  238. '#type' => 'textfield',
  239. '#default_value' => $alias,
  240. '#name' => $resource .'/alias',
  241. '#size' => 20,
  242. );
  243. foreach ($methods as $class => $info) {
  244. if (!in_array($class, $ignoreArray)) {
  245. if (!isset($info['help'])) {
  246. $description = t('No description is available');
  247. } else {
  248. $description = $info['help'];
  249. }
  250. if (isset($form_state['build_info']['args'][0]->resources[$resource]['operations'][$class])) {
  251. $default_value = $form_state['build_info']['args'][0]->resources[$resource]['operations'][$class]['enabled'];
  252. }
  253. else {
  254. $default_value = 0;
  255. }
  256. $form['resources']['table'][$resource][$resource .'/'. $class] = array(
  257. '#type' => 'checkbox',
  258. '#title' => $class,
  259. '#description' => $description,
  260. '#default_value' => $default_value,
  261. );
  262. }
  263. elseif($class == 'actions' || $class == 'relationships' || $class == 'targeted_actions') {
  264. foreach($info as $key => $action) {
  265. if (!isset($action['help'])) {
  266. $description = t('No description is available');
  267. }
  268. else {
  269. $description = $action['help'];
  270. }
  271. if (isset($form_state['build_info']['args'][0]->resources[$resource][$class][$key])) {
  272. $default_value = $form_state['build_info']['args'][0]->resources[$resource][$class][$key]['enabled'];
  273. }
  274. else {
  275. $default_value = 0;
  276. }
  277. $form['resources']['table'][$resource][$resource .'/'. $key .'/'. $class] = array(
  278. '#type' => 'checkbox',
  279. '#title' => $key,
  280. '#description' => $description,
  281. '#default_value' => $default_value,
  282. );
  283. }
  284. }
  285. }
  286. }
  287. $form['save'] = array(
  288. '#type' => 'submit',
  289. '#value' => t('Save'),
  290. );
  291. return $form;
  292. }
  293. /**
  294. * services_edit_form_endpoint_resources_validate function.
  295. *
  296. * @param array $form
  297. * @param array $form_state
  298. * @return void
  299. */
  300. function services_edit_form_endpoint_resources_validate($form, $form_state) {
  301. $input = $form_state['values']['endpoint_object'];
  302. // Validate aliases.
  303. foreach ($input as $key => $value) {
  304. if (strpos($key, '/alias') !== FALSE && !empty($value) && !preg_match('/^[a-z-]+$/', $value)) {
  305. list($resource_name,) = explode('/', $key);
  306. // Still this doesn't highlight needed form element.
  307. form_set_error("resources][table][$resource_name][alias", t("The alias for the !name resource may only contain lower case a-z and dashes.", array(
  308. '!name' => $resource_name,
  309. )));
  310. }
  311. }
  312. }
  313. /**
  314. * Resources form submit function.
  315. *
  316. * @param array $form
  317. * @param array $form_state
  318. * @return void
  319. */
  320. function services_edit_form_endpoint_resources_submit($form, $form_state) {
  321. $endpoint = $form_state['values']['endpoint_object'];
  322. $existing_resources = _services_build_resources();
  323. // Apply the endpoint in a non-strict mode, so that the non-active resources
  324. // are preserved.
  325. _services_apply_endpoint($existing_resources, $endpoint, FALSE);
  326. $resources = $form_state['input'];
  327. $endpoint = $form_state['build_info']['args'][0];
  328. foreach ($resources as $path => $state) {
  329. if (strpos($path, '/') === FALSE || empty($state)) {
  330. continue;
  331. }
  332. $split_path = explode('/', $path);
  333. $resource = $split_path[0];
  334. $method = $split_path[1];
  335. // If method is alias.
  336. if ($method == 'alias') {
  337. $final_resource[$resource]['alias'] = $state;
  338. continue;
  339. }
  340. // If it is action, relationship, or targeted action.
  341. if (isset($split_path[2])) {
  342. $final_resource[$resource][$split_path[2]][$method]['enabled'] = 1;
  343. continue;
  344. }
  345. // If it is operation.
  346. $final_resource[$resource]['operations'][$method]['enabled'] = 1;
  347. }
  348. $endpoint->resources = $final_resource;
  349. services_endpoint_save($endpoint);
  350. drupal_set_message('Resources have been saved');
  351. }