You are here

drupagram.module in Drupagram 6

Same filename and directory in other branches
  1. 7 drupagram.module

File

drupagram.module
View source
<?php

/**
 * @file
 * Provides API integration with the Instagram microblogging service.
 */
if (!defined('REQUEST_TIME')) {
  define('REQUEST_TIME', time());
}

/**
 * Implementation hook_menu().
 */
function drupagram_menu() {
  $items = array();
  $items['instagram/oauth'] = array(
    'title' => 'Instagram',
    'access callback' => TRUE,
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'drupagram_oauth_callback',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'drupagram.pages.inc',
  );
  $items['admin/settings/services/instagram'] = array(
    'title' => 'Instagram',
    'description' => 'Configure integration with Instagram (and compatible) API services.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'drupagram_admin_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'drupagram.pages.inc',
  );
  $items['admin/settings/services/instagram/default'] = array(
    'title' => 'Instagram',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['user/%user/edit/drupagram'] = array(
    'title' => 'Instagram accounts',
    'page callback' => 'drupagram_user_settings',
    'page arguments' => array(
      1,
    ),
    'access callback' => 'drupagram_edit_access',
    'access arguments' => array(
      1,
    ),
    'load arguments' => array(
      '%map',
      '%index',
    ),
    'weight' => 10,
    'file' => 'drupagram.pages.inc',
    'type' => MENU_LOCAL_TASK,
  );
  $items['user/%user/edit/drupagram/global/%'] = array(
    'title' => 'Instagram accounts',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'drupagram_user_make_global',
      1,
      5,
    ),
    'access arguments' => array(
      'make drupagram accounts global',
    ),
    'file' => 'drupagram.pages.inc',
  );
  return $items;
}
function drupagram_edit_access($account) {
  return user_edit_access($account) && user_access('Add Instagram accounts');
}

/**
 * Implementation of hook_perm().
 */
function drupagram_perm() {
  return array(
    t('Add Instagram accounts'),
    t('Use the site global Instagram account'),
    t('Assign a Instagram account as the site global account'),
    t('Import own media to the site'),
  );
}

/**
 * Detect whether we should use oauth.
 */
function _drupagram_use_oauth() {
  return module_exists('oauth_common') && variable_get('drupagram_client_id', '') && variable_get('drupagram_client_secret', '');
}

/**
 * Implementation hook_user().
 */
function drupagram_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case "load":
      $account->drupagram_accounts = module_invoke_all("drupagram_accounts", $account);
      break;
  }
}

/**
 * An implementation of hook_drupagram_accounts. We want to move this into a
 * separate module eventually, but sticking the code here and using a hook
 * lets other modules solve the 'what accounts can a user post with' problem
 * in cleaner ways.
 *
 * @return
 *   array with Instagram accounts
 */
function drupagram_drupagram_accounts($account) {
  module_load_include('inc', 'drupagram');
  $query = "SELECT da.drupagram_id FROM {drupagram_account} da";
  $condition = " WHERE da.uid = %d";
  if (user_access('use global drupagram account', $account)) {
    $condition .= " OR da.is_global=1";
  }
  $query = $query . $condition;
  $drupagram_accounts = db_query($query, $account->uid);
  $drupagram_accounts_list = array();
  while ($drupagram_account = db_fetch_object($drupagram_accounts)) {
    $drupagram_accounts_list[] = drupagram_account_load($drupagram_account->drupagram_id);
  }
  return $drupagram_accounts_list;
}

/**
 * Implementation hook_theme().
 */
function drupagram_theme() {
  return array(
    'drupagram_account_list_form' => array(
      'arguments' => array(
        'form' => NULL,
      ),
    ),
    'drupagram_likes' => array(
      'arguments' => array(
        'count' => NULL,
        'data' => array(),
      ),
      'file' => 'drupagram.theme.inc',
    ),
    'drupagram_likes_data' => array(
      'arguments' => array(
        'data' => array(),
      ),
      'file' => 'drupagram.theme.inc',
    ),
    'drupagram_likes_data_item' => array(
      'arguments' => array(
        'id' => NULL,
        'username' => NULL,
        'full_name' => NULL,
        'profile_picture' => NULL,
      ),
      'file' => 'drupagram.theme.inc',
    ),
  );
}

/**
 * Implementation hook_cron().
 *
 * Imports new Instagram statuses for site users, and deletes expired media.
 */
function drupagram_cron() {
  if (!variable_get('drupagram_import', TRUE)) {
    return;
  }
  module_load_include('inc', 'drupagram');
  $result = db_query_range("SELECT drupagram_id FROM {drupagram_account} WHERE import = %d ORDER BY last_refresh ASC", 1, 0, 20);
  while ($account = db_fetch_object($result)) {
    if ($account->drupagram_id) {

      //drupagram_fetch_user_feed($account->drupagram_id);
      drupagram_fetch_recent_items($account->drupagram_id);
    }
  }
  if ($age = variable_get('drupagram_expire', 0)) {
    db_query("DELETE FROM {drupagram} WHERE created_time=%d", REQUEST_TIME - $age);
  }
}

/**
 * Implementation hook_views_api().
 */
function drupagram_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'drupagram') . '/views',
  );
}

Functions

Namesort descending Description
drupagram_cron Implementation hook_cron().
drupagram_drupagram_accounts An implementation of hook_drupagram_accounts. We want to move this into a separate module eventually, but sticking the code here and using a hook lets other modules solve the 'what accounts can a user post with' problem in cleaner ways.
drupagram_edit_access
drupagram_menu Implementation hook_menu().
drupagram_perm Implementation of hook_perm().
drupagram_theme Implementation hook_theme().
drupagram_user Implementation hook_user().
drupagram_views_api Implementation hook_views_api().
_drupagram_use_oauth Detect whether we should use oauth.