You are here

drupagram_actions.module in Drupagram 7

Exposes Drupal actions for sending Instagram messages.

File

drupagram_actions/drupagram_actions.module
View source
<?php

/**
 * @file
 * Exposes Drupal actions for sending Instagram messages.
 */

/**
 * Implements hook_action_info().
 */
function drupagram_actions_action_info() {
  return array(
    'drupagram_actions_set_status_action' => array(
      'type' => 'system',
      'label' => t('Post a message to Instagram'),
      'configurable' => TRUE,
      'triggers' => array(
        'node_view',
        'node_insert',
        'node_update',
        'node_delete',
        'comment_view',
        'comment_insert',
        'comment_update',
        'comment_delete',
        'user_view',
        'user_insert',
        'user_update',
        'user_delete',
        'user_login',
        'cron',
      ),
    ),
  );
}

/**
 * Returns a form definition so the Instagram action can be configured.
 *
 * @param $context
 *   Default values (if we are editing an existing action instance).
 * @return
 *   Form definition.
 */
function drupagram_actions_set_status_action_form($context) {
  $options = array();
  $results = db_query("SELECT username FROM {drupagram_account}");
  foreach ($results as $result) {
    $options[$result->username] = $result->username;
  }

  // Set default values for form.
  $form['username'] = array(
    '#type' => 'select',
    '#title' => t('Instagram account name'),
    '#options' => $options,
    '#default_value' => isset($context['username']) ? $context['username'] : '',
    '#required' => TRUE,
  );
  if (!count($options)) {
    $form['username']['#description'] = t('You first need to add a Instagram account to one of ' . 'your users with rights for posting to Instagram.');
  }
  $form['message'] = array(
    '#type' => 'textarea',
    '#title' => t('Message'),
    '#default_value' => isset($context['message']) ? $context['message'] : '',
    '#cols' => '80',
    '#rows' => '3',
    '#description' => t('The message that should be sent. You may include the following variables: ' . '%site_name, %username, %node_url, %node_type, %title, %summary, %body, ' . '%tinyurl. Not all variables will be available in all contexts.'),
    '#required' => TRUE,
  );
  return $form;
}

/**
 * Verifies if Oauth module has been setup and also checks the Instagram
 * authentication against the provided screen name.
 */
function drupagram_actions_set_status_action_validate($form, $form_state) {
  if (!_drupagram_use_oauth()) {
    form_set_error('username', t('Oauth has not been setup yet. Please go to !link and follow steps.', array(
      '!link' => l(t('Instagram settings'), 'admin/settings/drupagram'),
    )));
  }
}

/**
 * Submits the form and sets the drupagram account pulling the data from the
 * drupagram_account table.
 */
function drupagram_actions_set_status_action_submit($form, $form_state) {
  $form_values = $form_state['values'];
  $drupagram_id = db_query("SELECT drupagram_id FROM {drupagram_account} WHERE username = :username", array(
    ':username' => $form_values['username'],
  ))
    ->fetchField();

  // Process the HTML form to store configuration. The keyed array that
  // we return will be serialized to the database.
  $params = array(
    'drupagram_uid' => $drupagram_uid,
    'username' => $form_values['username'],
    'images' => $form_values['images'],
    'caption' => $form_values['caption'],
  );
  return $params;
}

/**
 * Implementation of a configurable Instagram actions.
 * @todo Implementation for language negotiation for the body and sumary. Also
 * need implementation for bodies with multiple values. Right now it is hard
 * coded and it will only get body and summary for 'und' language and only
 * the first value of the body field.
 * If the final message is over 140 chars, there is no feedback to the user.
 */
function drupagram_actions_set_status_action($object, $context) {
  global $user;
  $variables['%site_name'] = variable_get('site_name', 'Drupal');

  // Seting variables array depending on action's group
  switch ($context['group']) {
    case 'node':
      $node = $context['node'];
      if (isset($node)) {
        $variables = array_merge($variables, array(
          '%uid' => $node->uid,
          '%username' => $node->name,
          '%node_url' => url('node/' . $node->nid, array(
            'absolute' => TRUE,
          )),
          '%node_type' => node_type_get_name($node),
          '%title' => $node->title,
          '%summary' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['summary'] : '',
          '%body' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['value'] : '',
        ));
      }
      break;
    case 'comment':
      $node = node_load($context['comment']->nid);
      if (isset($node)) {
        $variables = array_merge($variables, array(
          '%uid' => $context['comment']->uid,
          '%username' => $context['comment']->name,
          '%node_url' => url('node/' . $node->nid, array(
            'absolute' => TRUE,
          )),
          '%node_type' => node_type_get_name($node),
          '%title' => $node->title,
          '%summary' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['summary'] : '',
          '%body' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['value'] : '',
        ));
      }
      break;
    case 'user':
      $variables['%username'] = $context['user']->name;
      break;
    case 'cron':
      break;
    default:

      // We are being called directly.
      $node = $object;
      if (isset($node) && is_object($node)) {
        $variables = array_merge($variables, array(
          '%uid' => $node->uid,
          '%username' => $node->name,
          '%node_url' => url('node/' . $node->nid, array(
            'absolute' => TRUE,
          )),
          '%node_type' => node_type_get_name($node),
          '%title' => $node->title,
          '%summary' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['summary'] : '',
          '%body' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['value'] : '',
        ));
      }
  }

  // Only make a tinyurl if it was asked for.
  if (strstr($context['message'], '%tinyurl') !== FALSE) {
    $variables = array_merge($variables, array(
      '%tinyurl' => drupagram_shorten_url(url('node/' . $node->nid, array(
        'absolute' => TRUE,
      ))),
    ));
  }
  $message = strtr($context['message'], $variables);
  module_load_include('inc', 'drupagram');
  $drupagram_account = drupagram_account_load($context['drupagram_id']);
  drupagram_set_status($drupagram_account, $message);
}

Functions

Namesort descending Description
drupagram_actions_action_info Implements hook_action_info().
drupagram_actions_set_status_action Implementation of a configurable Instagram actions. @todo Implementation for language negotiation for the body and sumary. Also need implementation for bodies with multiple values. Right now it is hard coded and it will only get body and summary for…
drupagram_actions_set_status_action_form Returns a form definition so the Instagram action can be configured.
drupagram_actions_set_status_action_submit Submits the form and sets the drupagram account pulling the data from the drupagram_account table.
drupagram_actions_set_status_action_validate Verifies if Oauth module has been setup and also checks the Instagram authentication against the provided screen name.