You are here

public function FormAssemblyRequest::getForms in FormAssembly 7

Get data about available forms. If admin index is in use, get a single page of data.

Parameters

string $endpoint: The api endpoint to make the request against.

boolean $is_admin: Flag indicating if $endpoint is an admin index

Return value

array An array of objects representing FormAssembly forms.

File

includes/FormAssemblyRequest.php, line 211
Authorizes the current site and handles API requests to FormAssembly.

Class

FormAssemblyRequest
@file Authorizes the current site and handles API requests to FormAssembly.

Code

public function getForms($endpoint = 'api_v1/forms/index.json', $is_admin = FALSE) {

  //prepare a processing array for forms
  $finished = false;
  if ($is_admin) {
    $request_uri = $this->apiHost . '/' . $endpoint . '?access_token=' . urlencode($this->token) . '&show=50&page=' . $this->page;
    $response = drupal_http_request($request_uri);

    // If there's an error message...
    if (isset($response->error)) {
      throw new Exception($response->error);
    }

    // If the response was not an HTTP 200 Success...
    if ($response->code != 200) {
      throw new Exception($response->status_message);
    }

    // If we don't get JSON on the first try there is an error
    // on subsequent pages FormAssembly indicates that the last page could be empty
    if ($this->page == 1 && !$this
      ->isJson($response->data)) {
      throw new Exception($response->data);
    }

    //increment flags
    if (!empty($this->current_hash)) {
      $this->last_hash = $this->current_hash;
    }

    // hash the request so we can easily see if it has changed
    $this->current_hash = md5($response->data);

    //We store the hashes in the object to keep all the properties of the request together

    // but the boolean expression below is easier to read without also checking for empty object properties.
    $last_hash = $this->last_hash;
    $current_hash = $this->current_hash;
    if (!(empty($response->data) || $last_hash === $current_hash)) {

      // observed behavior from formassembly is if n pages are needed to iterate the admin index
      // page n+1 returns the same response as page n
      // but formassembly advises this could change to page n+1 returns empty. Neither of these responses should
      // be processed and we are finished.
      // Otherwise, process response
      $this->responses[] = drupal_json_decode($response->data);

      // increment page
      ++$this->page;
    }
    else {
      $finished = TRUE;
    }
  }
  else {
    $request_uri = $this->apiHost . '/' . $endpoint . '?access_token=' . urlencode($this->token);
    $response = drupal_http_request($request_uri);

    // If there's an error message...
    if (isset($response->error)) {
      throw new Exception($response->error);
    }

    // If the response was not an HTTP 200 Success...
    if ($response->code != 200) {
      throw new Exception($response->status_message);
    }
    if (!$this
      ->isJson($response->data)) {
      throw new Exception($response->data);
    }
    $this->responses[] = drupal_json_decode($response->data);
    $finished = TRUE;
  }
  return $finished;
}