[3959b2a] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * @file |
---|
| 4 | * Provides a generic but powerful API for web services. |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | /** |
---|
| 8 | * Implementation of hook_views_api(). |
---|
| 9 | */ |
---|
| 10 | function services_views_views_api() { |
---|
| 11 | return array( |
---|
| 12 | 'api' => 2.0, |
---|
| 13 | ); |
---|
| 14 | } |
---|
| 15 | |
---|
| 16 | /** |
---|
| 17 | * Implementation of hook_services_resources(). |
---|
| 18 | */ |
---|
| 19 | function services_views_services_resources() { |
---|
| 20 | $resources['views'] = array(); |
---|
| 21 | |
---|
| 22 | $resources['views']['retrieve'] = array( |
---|
| 23 | 'help' => 'Retrieves a view.', |
---|
| 24 | 'file' => array( |
---|
| 25 | 'type' => 'inc', |
---|
| 26 | 'module' => 'services_views', |
---|
| 27 | 'name' => 'services_views.resource', |
---|
| 28 | ), |
---|
| 29 | 'callback' => 'services_views_retrieve', |
---|
| 30 | 'access callback' => 'services_views_access', |
---|
| 31 | 'access arguments' => array('view'), |
---|
| 32 | 'access arguments append' => TRUE, |
---|
| 33 | 'args' => array( |
---|
| 34 | 'view_name' => array( |
---|
| 35 | 'name' => 'view_name', |
---|
| 36 | 'type' => 'string', |
---|
| 37 | 'description' => 'The name of the view to get.', |
---|
| 38 | 'source' => array('path' => '0'), |
---|
| 39 | 'optional' => FALSE, |
---|
| 40 | ), |
---|
| 41 | 'display_id' => array( |
---|
| 42 | 'name' => 'display_id', |
---|
| 43 | 'type' => 'string', |
---|
| 44 | 'description' => 'The display ID of the view to get.', |
---|
| 45 | 'source' => array('param' => 'display_id'), |
---|
| 46 | 'optional' => TRUE, |
---|
| 47 | 'default value' => 'default', |
---|
| 48 | ), |
---|
| 49 | 'args' => array( |
---|
| 50 | 'name' => 'args', |
---|
| 51 | 'type' => 'array', |
---|
| 52 | 'description' => 'A list of arguments to pass to the view.', |
---|
| 53 | 'source' => array('param' => 'args'), |
---|
| 54 | 'optional' => TRUE, |
---|
| 55 | 'default value' => array(), |
---|
| 56 | ), |
---|
| 57 | 'offset' => array( |
---|
| 58 | 'name' => 'offset', |
---|
| 59 | 'type' => 'int', |
---|
| 60 | 'description' => 'The number of the entry for the page begin with.', |
---|
| 61 | 'source' => array('param' => 'offset'), |
---|
| 62 | 'optional' => TRUE, |
---|
| 63 | 'default value' => 0, |
---|
| 64 | ), |
---|
| 65 | 'limit' => array( |
---|
| 66 | 'name' => 'limit', |
---|
| 67 | 'type' => 'int', |
---|
| 68 | 'description' => 'The total number of entries to list.', |
---|
| 69 | 'source' => array('param' => 'limit'), |
---|
| 70 | 'optional' => TRUE, |
---|
| 71 | 'default value' => 10, |
---|
| 72 | ), |
---|
| 73 | 'theme_output' => array( |
---|
| 74 | 'name' => 'theme_output', |
---|
| 75 | 'type' => 'bool', |
---|
| 76 | 'description' => 'Whether to return the raw data results or style the results.', |
---|
| 77 | 'source' => array('param' => 'theme_output'), |
---|
| 78 | 'optional' => TRUE, |
---|
| 79 | 'default value' => FALSE, |
---|
| 80 | ), |
---|
| 81 | 'filters' => array( |
---|
| 82 | 'name' => 'filters', |
---|
| 83 | 'type' => 'array', |
---|
| 84 | 'description' => 'A list of filters to pass to the view. These are defined by the exposed filters on your view. Example call: <code>/views/your_view?filters[nid]=12345</code>', |
---|
| 85 | 'source' => array('param' => 'filters'), |
---|
| 86 | 'optional' => TRUE, |
---|
| 87 | 'default value' => array(), |
---|
| 88 | ), |
---|
| 89 | ), |
---|
| 90 | ); |
---|
| 91 | |
---|
| 92 | return $resources; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | /** |
---|
| 96 | * Check the access permission to a given views. |
---|
| 97 | * |
---|
| 98 | * @param $op |
---|
| 99 | * String. The operation that's going to be performed. |
---|
| 100 | * @param $args |
---|
| 101 | * Array. The arguments that will be passed to the callback. |
---|
| 102 | * @return |
---|
| 103 | * Boolean. TRUE if the user is allowed to load the given view. |
---|
| 104 | */ |
---|
| 105 | function services_views_access($op = 'view', $args = array()) { |
---|
| 106 | //watchdog('services_views_access', 'Argumentos @detail.', array('@detail' => print_r($args, 1)), WATCHDOG_ERROR); |
---|
| 107 | //return TRUE; |
---|
| 108 | switch ($op) { |
---|
| 109 | case 'view': |
---|
| 110 | $view = views_get_view($args[0]); |
---|
| 111 | if (empty($view)) { |
---|
| 112 | return services_error(t('View @view1 1could not be found', array('@view' => $args[0])), 404); |
---|
| 113 | } |
---|
| 114 | $args[1] = !(isset($args[1]) || $args[1]) ? 'default' : $args[1]; |
---|
| 115 | if (!isset($view->display[$args[1]])) { |
---|
| 116 | return services_error(t('Display @display on view @view could not be found', array( |
---|
| 117 | '@display' => $args[1], |
---|
| 118 | '@view' => $args[0], |
---|
| 119 | )), 404); |
---|
| 120 | } |
---|
| 121 | return $view->access($args[1]); |
---|
| 122 | } |
---|
| 123 | } |
---|