You are here

function opening_hours_crud_api_page in Opening hours 7

Same name and namespace in other branches
  1. 6 includes/opening_hours.pages.inc \opening_hours_crud_api_page()

The CRUD API for communication with Backbone.

1 string reference to 'opening_hours_crud_api_page'
opening_hours_menu in ./opening_hours.module
Implements hook_menu().

File

includes/opening_hours.pages.inc, line 29
Page callbacks for the opening hours module.

Code

function opening_hours_crud_api_page() {
  $output = array();
  switch ($_SERVER['REQUEST_METHOD']) {
    case 'POST':
      if (!user_access('edit opening hours for content')) {
        header('HTTP/1.1 403 Forbidden');
        exit('Client does not have permissions to edit opening hours.');
      }
      $instance = _opening_hours_get_instance_from_request();

      // We can hardly save an instance that doesn't have the required data.
      if (!$instance) {
        header('HTTP/1.1 400 Bad Request');
        exit('Required fields were missing or invalid.');
      }
      module_invoke_all('opening_hours_presave', $instance, NULL);
      if (drupal_write_record('opening_hours', $instance) === SAVED_NEW) {
        header('HTTP/1.1 201 Created');
        header('Location: ' . url('opening_hours/instances/' . $instance->instance_id));
        $output[] = $instance;
        if (!empty($instance->repeat_rule)) {
          opening_hours_repeat_instance_propagate($instance);
        }

        // Clear the cached array storing which nodes have opening hours.
        cache_clear_all('opening_hours_present_on_node', 'cache');
      }
      break;
    case 'GET':

      // The nid parameter might be an array. If it's a single value, it
      // should still pass these filters with no trouble.
      // In any case, we want to elimitate bogus values, to see if
      // there's anything left to fetch.
      $nids = explode(',', $_REQUEST['nid']);
      array_filter($nids, 'is_numeric');
      array_filter($nids);
      if (!empty($_REQUEST['from_date']) && !empty($_REQUEST['to_date']) && !empty($nids)) {
        $output = opening_hours_instance_load_multiple($nids, $_REQUEST['from_date'], $_REQUEST['to_date']);
      }

      // Filter out instances on blocked days.
      // array_values is necessary, since filtered values causes holes
      // in the key order, which in turns causes json_encode to return a
      // JavaScript object instead of an array in some cases.
      $output = array_values(array_filter($output, '_opening_hours_exclude_blocked'));
      break;
  }
  return drupal_json_output($output);
}