You are here

social_content.module in Social Content 7.2

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

Social Content module.

File

social_content.module
View source
<?php

/**
 * @file
 * Social Content module.
 */

/**
 * Implements hook_menu().
 */
function social_content_menu() {
  $info = array();
  $info['admin/config/services/social-content/global'] = array(
    'title' => 'Social Content',
    'description' => 'Social Content Import instances and Settings',
    'page callback' => 'social_content_global_overview',
    'access arguments' => array(
      'administer social content',
    ),
    'file' => 'social_content.admin.inc',
  );
  $info['admin/config/services/social-content/global/%social_content/edit'] = array(
    'title' => 'Edit Social Content type',
    'title callback' => 'social_content_page_title',
    'title arguments' => array(
      4,
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'social_content_form',
      4,
      5,
    ),
    'access arguments' => array(
      'administer social content',
    ),
    'file' => 'social_content.admin.inc',
  );
  $info['admin/config/services/social-content/global/%social_content/instances'] = array(
    'title' => 'Social Content',
    'page callback' => 'social_content_instances_overview',
    'page arguments' => array(
      5,
    ),
    'access arguments' => array(
      'administer social content',
    ),
    'file' => 'social_content.admin.inc',
  );
  $info['admin/config/services/social-content/instance/%social_content/add'] = array(
    'title' => 'Edit Social Content type',
    'title callback' => 'social_content_page_title',
    'title arguments' => array(
      5,
      6,
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'social_content_form',
      4,
      5,
      6,
    ),
    'access arguments' => array(
      'administer social content',
    ),
    'file' => 'social_content.admin.inc',
  );
  $info['admin/config/services/social-content/instance/%social_content/edit'] = array(
    'title' => 'Edit Social Content type',
    'title callback' => 'social_content_page_title',
    'title arguments' => array(
      5,
      6,
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'social_content_form',
      4,
      5,
    ),
    'access arguments' => array(
      'administer social content',
    ),
    'file' => 'social_content.admin.inc',
  );
  $info['admin/config/services/social-content/instance/%social_content/delete'] = array(
    'title' => 'Edit Social Content type',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'social_content_delete_instance_form',
      5,
    ),
    'access arguments' => array(
      'administer social content',
    ),
    'file' => 'social_content.admin.inc',
  );
  $info['admin/config/services/social-content/instance/%social_content/run'] = array(
    'title' => 'Run Social Content import',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'social_content_import_run_form',
      5,
    ),
    'access arguments' => array(
      'administer social content',
    ),
    'file' => 'social_content.admin.inc',
  );
  return $info;
}

/**
 * Implements hook_load().
 */
function social_content_load($id) {
  $classes = social_content_get_classes();
  if (is_string($id) && !empty($classes[$id])) {
    return new $classes[$id]();
  }
  elseif (is_numeric($id)) {
    return SocialContent::getObjectFromInstance($classes, $id);
  }
}

/**
 * Implements hook_permission().
 */
function social_content_permission() {
  return array(
    'administer social content' => array(
      'title' => t('Administer social content'),
      'description' => t('Administer social content.'),
    ),
  );
}

/**
 * Implements hook_delete().
 *
 * Delete this node from history log so it can be imported again.
 */
function social_content_node_delete($node) {
  if (variable_get('social_content_auto_delete_history', TRUE)) {
    SocialContent::deleteHistory($node->nid);
  }
}

/**
 * Implements hook_cron().
 *
 * Run through the social content types and import the posts.
 * Delete old nodes.
 */
function social_content_cron() {
  set_time_limit(0);

  // Get all enabled instances.
  $instances = array_keys(SocialContent::getAllInstances(array(), TRUE));

  // Import.
  foreach ($instances as $id) {
    $instance = social_content_load($id);
    social_content_run_import($instance);
  }

  // Delete old nodes.
  social_content_delete_old_nodes();
}

/**
 * Deletes old nodes of all configured instances.
 *
 * @return int
 *  The number of deleted nodes.
 */
function social_content_delete_old_nodes() {
  $nids_to_delete = array();
  $instances = SocialContent::getAllInstances();
  foreach ($instances as $instance) {
    if (isset($instance->settings['auto_delete']) && is_numeric($instance->settings['auto_delete']) && $instance->settings['auto_delete'] > 0) {

      // Get all ids and timestamps of nodes imported by this instance.
      $nodes = db_query('SELECT internal_id, stamp FROM {social_content_history} WHERE instance = :instance', array(
        ':instance' => $instance->id,
      ))
        ->fetchAllKeyed(0, 1);
      $delete_date_seconds = $instance->settings['auto_delete'] * 24 * 60 * 60;
      foreach ($nodes as $nid => $created) {
        if (REQUEST_TIME - $created > $delete_date_seconds) {
          $nids_to_delete[] = $nid;
        }
      }
    }
  }
  $count = count($nids_to_delete);
  if ($count) {

    // Do not delete from history, so they are not imported again.
    $auto_delete_current = variable_get('social_content_auto_delete_history', TRUE);
    variable_set('social_content_auto_delete_history', FALSE);
    node_delete_multiple($nids_to_delete);

    // Restore custom value.
    variable_set('social_content_auto_delete_history', $auto_delete_current);
    watchdog('social_content', '%count nodes have been deleted for being too old.', array(
      '%count' => $count,
    ));
  }
  return $count;
}

/**
 * Implements hook_cronapi().
 *
 * Elysia Cron / Ultimate Cron hook.
 * Split all social content imports into their own jobs.
 * Stagger the time by five minutes for each run.
 */
function social_content_cronapi($op, $job = NULL) {

  // $classes = social_content_get_classes();
  // Get all enabled instances.
  $instances = array_keys(SocialContent::getAllInstances(array(), TRUE));
  $items = array();
  $mins = 0;
  foreach ($instances as $id) {
    $instance = social_content_load($id);
    $mins += 5;
    if ($mins > 60) {
      $mins -= 60;
    }
    $items['social_content_' . $instance
      ->getMachineName() . '_' . $id] = array(
      'description' => t('Social content import for !title', array(
        '!title' => $instance
          ->getInstanceTitle(),
      )),
      'rule' => $mins . ' */2 * * *',
      'callback' => 'social_content_run_import',
      'arguments' => array(
        $instance,
      ),
    );
  }
  $items['social_content_purge'] = array(
    'description' => t('Delete old nodes imported by Social Content.'),
    'rule' => '0 0 * * *',
    'callback' => 'social_content_delete_old_nodes',
  );
  return $items;
}

/**
 * Run an import for a particular instance.
 *
 *
 * @param array $instance
 *   The full loaded instance
 * @param array $settings_override
 *   (Optional) Pass through settings in a key => value array to override
 *   instance and global settings.
 *
 * @return mixed
 *   Whatever is returned by the import process
 */
function social_content_run_import($instance, $settings_override = array()) {
  return $instance
    ->import($settings_override);
}

/**
 * Implements hook_cron_alter().
 *
 * If using Elysia Cron or Ultimate Cron, remove default cron.
 * We don't want it running multiple times.
 */
function social_content_cron_alter(&$data) {
  if (isset($data['social_content_cron']) && (module_exists('elysia_cron') || module_exists('ultimate_cron'))) {
    unset($data['social_content_cron']);
  }
}

/**
 * Implements hook_views_api().
 */
function social_content_views_api() {
  return array(
    'api' => 3,
  );
}

/**
 * Get all classes which integrating modules declare.
 *
 * @return array
 *   An array of classes.
 */
function social_content_get_classes() {
  $classes = module_invoke_all('social_content_class_info');
  drupal_alter('social_content_class_info', $classes);
  return $classes;
}

/**
 * Implements hook_modules_uninstalled().
 */
function social_content_modules_uninstalled($modules) {
  foreach ($modules as $module) {
    require_once DRUPAL_ROOT . '/' . drupal_get_path('module', $module) . '/' . $module . '.module';
    $function = $module . '_social_content_class_info';

    // If this module implements hook_social_content_class_info(), we should
    // remove all instances and history.
    if (function_exists($function)) {
      module_load_include('inc', $module, $module . '.class');
      $info = $function();
      $name = key($info);
      $instances = SocialContent::getAllInstances($name);
      foreach ($instances as $i) {
        $instance = new $info[$name]($i->settings);
        $instance
          ->deleteInstance();
      }
    }
  }
}

/**
 * Form element validation handler to check for valid usernames.
 *
 * This is used on the author field, on the global/instance settings form.
 */
function social_content_element_validate_user_name($element, &$form_state) {
  if (!empty($element['#value']) && !($account = user_load_by_name($element['#value']))) {
    form_error($element, t('The username %name does not exist.', array(
      '%name' => $element['#value'],
    )));
  }
}

Functions

Namesort descending Description
social_content_cron Implements hook_cron().
social_content_cronapi Implements hook_cronapi().
social_content_cron_alter Implements hook_cron_alter().
social_content_delete_old_nodes Deletes old nodes of all configured instances.
social_content_element_validate_user_name Form element validation handler to check for valid usernames.
social_content_get_classes Get all classes which integrating modules declare.
social_content_load Implements hook_load().
social_content_menu Implements hook_menu().
social_content_modules_uninstalled Implements hook_modules_uninstalled().
social_content_node_delete Implements hook_delete().
social_content_permission Implements hook_permission().
social_content_run_import Run an import for a particular instance.
social_content_views_api Implements hook_views_api().