<?php

/**
 * Implements hook_enable().
 */
function groups_feeds_enable() {
  //clear the cache to display in Feeds as available plugin.
  cache_clear_all('plugins:feeds:plugins', 'cache');
}

/**
 * Implement hook_install()
 *
 */

function groups_feeds_install() {
  // Create markdown fetcher source
  $feed = 'static_page_import';
  $feedSource = feeds_source($feed);
  $config = $feedSource->getConfig();
  $config['feeds_fetcher_directory_fetcher']['source'] =
    variable_get('groups_feeds_markdown_directory', 'public://feeds');
  $config['feeds_fetcher_directory_fetcher']['reset'] = 0;
  $config['feeds_fetcher_directory_fetcher']['feed_files_fetched'] = "";
  $feedSource->setConfig($config);
  $feedSource->save();
  // disable groups.json import by default
  groups_feeds_update_7305();
}

/**
 * Implement hook_uninstall()
 *
 */

function groups_feeds_uninstall() {
  // TODO: remove static_page_import feeds source
}

/**
 * Enable commons_pages module as a dependency for
 * groups_feeds.
 */
function groups_feeds_update_7301() {
  if (!module_exists('commons_pages')) {
    module_enable(array('commons_pages'));
    drupal_flush_all_caches();
  }
}

/**
 * Rebuild feeds jobs including static page import and
 * meetup events import.
 */
function groups_feeds_update_7302() {
  groups_feeds_rebuild_jobs();
}

/**
 * Rebuild feeds jobs including static page import and
 * meetup events import.
 */
function groups_feeds_update_7303() {
  groups_feeds_update_7302();
}

/**
 * Enforce update of all events imported by ical_feed_importer.
 * Required to re-assign group ids to specific events.
 */
function groups_feeds_update_7304() {
  $result = db_update('feeds_item')->
    fields(array('hash' => '0'))
    ->condition('id', 'ical_feed_importer', '=')
    ->execute();
}

/**
 * Disable groups.json user group data import.
 */
function groups_feeds_update_7305() {
  elysia_cron_set_job_disabled('user_groups_import', true);
}
