You are here

clients_flickr.module in Web Service Clients 7

Same filename and directory in other branches
  1. 6 backends/clients_flickr/clients_flickr.module

Flickr plugin for Clients module

File

backends/clients_flickr/clients_flickr.module
View source
<?php

/**
 * @file
 * Flickr plugin for Clients module
 */

/**
 * Implementation of hook_help()
 * @param path which path of the site we're displaying help
 * @param arg array that holds the current path as would be returned from arg() function
 * @return help text for the path
 */
function clients_flickr_help($path, $arg) {
  $output = '';
  switch ($path) {
    case "admin/help#clients_flickr":
      $output = '<p>' . t("Clients - Flickr.") . '</p>';
      break;
  }
  return $output;
}

/**
 * Implementation of hook_clients_connection_type_info().
 *
 * Define the connection type we provide.
 */
function clients_flickr_clients_connection_type_info() {
  return array(
    // Key by machine name. Used as the base for hooks.
    'clients_flickr' => array(
      'label' => t('Flickr'),
    ),
  );
}

/**
 * Implementation of hook_perm()
 * @TODO
 * @return array An array of valid permissions for the clients_flickr module
 */
function clients_flickr_perm() {
  return array(
    'clients_flickr admin',
  );
}

/**
 * Implementation of hook_menu()
 */
function clients_flickr_menu() {
  $items = array();
  $items['admin/settings/clients/connections/flickr'] = array(
    'title' => 'Add Flickr connection',
    'description' => 'Add Flickr connection',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'clients_flickr_config',
      'add',
    ),
    'access arguments' => array(
      'clients_flickr admin',
    ),
    'weight' => 10,
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/settings/clients/connections/flickr/%/edit'] = array(
    'title' => 'Edit Flickr connection',
    'description' => 'Edit Flickr connection',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'clients_flickr_config',
      6,
      5,
    ),
    'access arguments' => array(
      'clients_flickr admin',
    ),
    'weight' => 10,
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * @return array Form
 */
function clients_flickr_config(&$form_state, $op, $cid = '') {
  $form = array();
  if ($cid) {
    $connection = clients_connection_load((int) $cid);
    $form['cid'] = array(
      '#type' => 'value',
      '#value' => $cid,
    );
  }
  $form['type'] = array(
    '#type' => 'value',
    '#value' => variable_get('clients_flickr_type', 'Flickr'),
  );
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Connection name'),
    '#default_value' => $cid ? $connection->name : '',
    '#size' => 50,
    '#maxlength' => 100,
    '#description' => t('Must be unique'),
    '#required' => TRUE,
  );
  $form['endpoint'] = array(
    '#type' => 'value',
    '#value' => 'http://api.flickr.com/services/rest/',
  );
  $form['configuration'] = array(
    '#type' => 'fieldset',
    '#title' => t('Configuration'),
    '#collapsible' => FALSE,
    '#tree' => TRUE,
  );
  $form['configuration']['api_key'] = array(
    '#type' => 'textfield',
    '#title' => t('API key'),
    '#default_value' => $cid ? $connection->configuration['api_key'] : '',
    '#size' => 50,
    '#maxlength' => 100,
    //    '#description' => t('Domain'),
    '#required' => TRUE,
  );
  $form['configuration']['secret'] = array(
    '#type' => 'textfield',
    '#title' => t('Secret'),
    '#default_value' => $cid ? $connection->configuration['secret'] : '',
    '#size' => 50,
    '#maxlength' => 40,
    '#attributes' => array(
      'autocomplete' => 'off',
    ),
    //    '#description' => t('Key'),
    '#required' => TRUE,
  );
  $form['configuration']['methods_enabled'] = array(
    '#type' => 'textarea',
    '#title' => t('Methods'),
    '#default_value' => $cid ? $connection->configuration['methods_enabled'] : '',
    '#description' => t('List of Flickr methods that you want to enable here. One per line.'),
  );
  if ($op == 'add') {
    $form['buttons']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Add connection'),
    );
    $form['#submit'] = array(
      'clients_flickr_add_submit_handler',
    );
  }
  elseif ($op == 'edit') {
    $form['buttons']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
    );
    $form['#submit'] = array(
      'clients_flickr_edit_submit_handler',
    );
  }
  return $form;
}

/**
 * Submit handler
 * @return array Form
 */
function clients_flickr_add_submit_handler($form, &$form_state) {
  clients_connection_save($form_state['values']);
}

/**
 * Submit handler
 * @return array Form
 */
function clients_flickr_edit_submit_handler($form, &$form_state) {

  // TODO: convert to class methods -- see Drupal clients for example.
  // Commented this call out in the meantime, as it's no longer the
  // same function.

  //clients_connection_edit($form_state['values']);
}

/**
 * Implementation of hook_validate()
 */
function clients_flickr_add_validate($form, &$form_state) {
  if (clients_connection_load($form['name']['#value'])) {
    form_set_error('name', 'A service by this name already exists!');
  }
  $connection = new stdClass();
  $connection->name = $form['name']['#value'];
  $connection->endpoint = $form['endpoint']['#value'];
  $connection->api_key = $form['configuration']['api_key']['#value'];
  $connection->secret = $form['configuration']['secret']['#value'];
  $testconnect = clients_connection_flickr::connect($connection);
  if (!is_array($testconnect) || !isset($testconnect['sessid'])) {
    form_set_error('endpoint', "Couldn't connect");
  }
  else {

    // test auth
  }
}

/**
 * Implementation of hook_clients_connection_edit(). Performs any post-processing of edit form before write to db. If we could get this working by reference we wouldn't need this wrapper here.
 */
function clients_flickr_clients_connection_edit($formvals) {
  if ($formvals['type'] == variable_get('clients_flickr_type', 'Flickr')) {
    return $formvals;
  }
}

/**
 * Implementation of hook_clients_service_options
 */
function clients_flickr_clients_service_options($connection, $wrapper, $wrapper_values, $resource) {
  if ($connection->type != variable_get('clients_flickr_type', 'Flickr')) {
    return;
  }
  $form = array();
  $connection = clients_connection_load((int) $connection->cid);
  $methods = array_map('trim', explode("\n", trim($connection->configuration['methods_enabled'])));
  $methods = array_combine($methods, $methods);
  $options_selected = isset($wrapper_values['options']) ? $wrapper_values['options'] : (isset($resource->rid) ? $resource->configuration['options'] : array());

  // if we change connection and method doesn't exist, reset
  if (!in_array($options_selected['method'], $methods)) {
    $options_selected['method'] = key($methods);
  }
  $form['method'] = array(
    '#type' => 'select',
    '#title' => t('Method'),
    '#default_value' => $options_selected['method'],
    '#options' => $methods,
    '#description' => t('Choose method'),
    '#required' => TRUE,
    '#ahah' => array(
      'path' => ahah_helper_path(array(
        $wrapper,
      )),
      'wrapper' => $wrapper,
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );
  if ($options_selected['method'] == 'flickr.photos.search') {
    $form['text'] = array(
      '#type' => 'textfield',
      '#title' => t('Search'),
      '#default_value' => $options_selected['text'],
      '#size' => 40,
      '#maxlength' => 200,
    );
    $form['per_page'] = array(
      '#type' => 'textfield',
      '#title' => t('Per page'),
      '#default_value' => $options_selected['per_page'],
      '#size' => 3,
      '#maxlength' => 3,
    );
    $form['page'] = array(
      '#type' => 'textfield',
      '#title' => t('Page'),
      '#default_value' => $options_selected['page'],
      '#size' => 3,
      '#maxlength' => 3,
    );
    $form['my_images'] = array(
      '#type' => 'checkbox',
      '#title' => t('My images only'),
      '#default_value' => $options_selected['my_images'],
    );

    // and more - http://www.flickr.com/services/api/flickr.photos.search.html
  }
  else {

    /**
     * @todo some 'method not supported' handling
     */
  }
  return $form;
}

/**
 * Implementation of hook_clients_connection_load
 */
function clients_flickr_clients_connection_load(&$connection) {
  if ($connection->type == variable_get('clients_flickr_type', 'Flickr')) {
    $connection->operations = l(t('Edit'), 'admin/settings/clients/connections/flickr/' . $connection->cid . '/edit') . ' | ' . l(t('Delete'), 'admin/settings/clients/connections/' . $connection->cid . '/delete');
  }
}

/**
 * Implementation of hook_clients_call
 */
function clients_flickr_clients_call($connection, $serviceOptions) {
  if ($connection->type == variable_get('clients_flickr_type', 'Flickr')) {
    $serviceConfig = clients_connection_load((int) $connection->cid);
    $response = clients_connection_flickr::call($serviceConfig, $serviceOptions);

    // process xml and turn into serializable array
    if ($response->data->code == '200') {
      $xml = new SimpleXMLElement($response->data->data);
      $result = new stdClass();
      $result->created = $response->created;
      $result->data = array();
      foreach ($xml->photos->photo as $photo) {
        $row = array();
        foreach ($photo
          ->attributes() as $name => $value) {
          $row[$name] = (string) $value;
        }
        $row['url'] = "http://farm{$row['farm']}.static.flickr.com/{$row['server']}/{$row['id']}_{$row['secret']}.jpg";
        $row['html'] = '<img src="' . $row['url'] . '" />';
        $row['status'] = 1;

        // published
        $result->data[] = $row;
      }
      return $result;
    }
  }
}

/**
 * Implementation of hook_clients_fields - refactor as inc file?
 * Allows us to get field for a specific resource. Default fields are set at the connection type level here and additional custom field will be defined per resource (@todo). This will allow adding of remote cck fields (for example) for a specific resource (e.g. a certain view). This function will need to aggregate these with the base ones. (Otherwise this function is used to return all available fields.)
 */
function clients_flickr_clients_fields($resource = NULL) {
  if ($resource->cid) {
    $connection = clients_connection_load((int) $resource->cid);
    if ($connection->type != variable_get('clients_flickr_type', 'Flickr')) {
      return;
    }
  }

  // etc - http://www.flickr.com/services/api/flickr.photos.search.html
  return array();
}

/**
 * Implementation of hook_clients_setparams
 */
function clients_flickr_clients_setparams(&$resource, $arg) {
  $connection = clients_connection_load((int) $resource->cid);

  // need to make static?
  if ($connection->type == variable_get('clients_flickr_type', 'Flickr')) {
    foreach ($arg as $key => $value) {
      if ($key == 'argument') {
        $resource->configuration['options']['text'] = str_replace(' ', '+', $value);
      }
      elseif ($key == 'limit') {
        $resource->configuration['options']['per_page'] = $value;
      }
      elseif ($key == 'offset') {
        $resource->configuration['options']['page'] = $value;
      }
      else {
        $resource->configuration['options'][$key] = str_replace(' ', '+', $value);
      }
    }
  }
}

/**
 * Implementation of hook_clients_arguments
 */
function clients_flickr_clients_arguments($connection) {
  if ($connection->type == variable_get('clients_flickr_type', 'Flickr')) {
    $arg1 = new stdClass();
    $arg1->name = 'argument';
    $arg1->title = t('Argument');
    $arg1->help = t('Argument to pass to resource');
    $args[] = $arg1;
    $arg2 = new stdClass();
    $arg2->name = 'tag';
    $arg2->title = t('Tag');
    $arg2->help = t('Tag to pass to resource');
    $args[] = $arg2;
    return $args;
  }
}

Functions

Namesort descending Description
clients_flickr_add_submit_handler Submit handler
clients_flickr_add_validate Implementation of hook_validate()
clients_flickr_clients_arguments Implementation of hook_clients_arguments
clients_flickr_clients_call Implementation of hook_clients_call
clients_flickr_clients_connection_edit Implementation of hook_clients_connection_edit(). Performs any post-processing of edit form before write to db. If we could get this working by reference we wouldn't need this wrapper here.
clients_flickr_clients_connection_load Implementation of hook_clients_connection_load
clients_flickr_clients_connection_type_info Implementation of hook_clients_connection_type_info().
clients_flickr_clients_fields Implementation of hook_clients_fields - refactor as inc file? Allows us to get field for a specific resource. Default fields are set at the connection type level here and additional custom field will be defined per resource (@todo). This will allow…
clients_flickr_clients_service_options Implementation of hook_clients_service_options
clients_flickr_clients_setparams Implementation of hook_clients_setparams
clients_flickr_config
clients_flickr_edit_submit_handler Submit handler
clients_flickr_help Implementation of hook_help()
clients_flickr_menu Implementation of hook_menu()
clients_flickr_perm Implementation of hook_perm() @TODO