You are here

birthdays_defaults.install in Birthdays 7

Create a default field and action.

File

defaults/birthdays_defaults.install
View source
<?php

/**
 * @file
 * Create a default field and action.
 */

/**
 * Implements hook_install().
 */
function birthdays_defaults_install() {

  // Clear the field cache to be sure the new field type is available.
  field_cache_clear();

  // Create the default field.
  if (!field_info_field('birthdays')) {
    field_create_field(array(
      'field_name' => 'birthdays',
      'type' => 'birthdays_date',
      'cardinality' => 1,
      'translatable' => FALSE,
      'entity_types' => array(
        'user',
      ),
    ));
  }

  // Create the deault field instance.
  if (!field_info_instance('user', 'birthdays', 'user')) {
    field_create_instance(array(
      'field_name' => 'birthdays',
      'entity_type' => 'user',
      'bundle' => 'user',
      'label' => st('Birthday'),
    ));
  }

  // Explain that.
  if (module_exists('field_ui')) {
    $field_ui = st('You can adjust the birthday !field_settings, !manage_account_fields or !manage_the_display under Administration &raquo; Configuration &raquo; People.', array(
      '!field_settings' => l(st('field settings'), 'admin/config/people/accounts/fields/birthdays'),
      '!manage_account_fields' => l(st('manage account fields'), 'admin/config/people/accounts/fields'),
      '!manage_the_display' => l(st('manage the display'), 'admin/config/people/accounts/display'),
    ));
  }
  else {
    $field_ui = st('Enable the Field UI module to adjust the birthday field settings, manage account fields or manage the display under Administration &raquo; Configuration &raquo; People.');
  }
  drupal_set_message(st('A default field has been attached to the user profiles.') . ' ' . $field_ui);

  // Create a default birthday mail action.
  if (!variable_get('birthdays_defaults_action')) {
    $aid = actions_save('system_send_email_action', 'system', _birthdays_defaults_action_parameters(), st('Happy birthday mail'));
    variable_set('birthdays_defaults_action', $aid);

    // Explain that.
    if (module_exists('trigger')) {
      drupal_set_message(st('A <em>Happy birthday mail</em> action has been created. You can !assign that action so that users recieve an email on their birthday.', array(
        '!assign' => l(st('assign'), 'admin/structure/trigger/birthdays'),
      )));
    }
    else {
      drupal_set_message(st('Enable the Trigger module and assign the newly created <em>Happy birthday mail</em> action to send users emails on their birthdays.'));
    }
  }

  // Explain views support.
  if (module_exists('views_ui')) {
    drupal_set_message(st('Birthdays defaults provides a default page and block with birthdays. You can !use_the_views_module to enable or customize them.', array(
      '!use_the_views_module' => l(st('use the views module'), 'admin/structure/views'),
    )));
  }
  else {
    drupal_set_message(st('You can use the the Views module to lists of birthdays as blocks or pages.'));
  }
}

/**
 * Replaces the non existing token [site:uri-brief] with [site:url-brief] in the
 * default happy birthday message.
 *
 * Implements hook_update_N().
 */
function birthdays_defaults_update_7003() {
  $aid = variable_get('birthdays_defaults_action');
  if ($aid && ($action = actions_load($aid))) {
    $params = unserialize($action->parameters);
    $params['message'] = str_replace('[site:uri-brief]', '[site:url-brief]', $params['message'], $count);
    if ($count) {
      actions_save($action->callback, $action->type, $params, $action->label, $aid);
      return t('[site:uri-brief] has been replaced with [site:url-brief] in the default happy birthday mail action.');
    }
  }
}

/**
 * Get the default action settings.
 *
 * @return
 *   An associative array with recipient, subject and message keys set.
 */
function _birthdays_defaults_action_parameters() {
  return array(
    'recipient' => "[user:name] <[user:mail]>",
    'subject' => t("Happy Birthday, [user:name]!"),
    'message' => t("Hey [user:name],\n\nHappy birthday!\nWe hope you have a great day.\n\nThe [site:name]-team\n[site:url-brief]"),
  );
}

/**
 * Implements hook_disable().
 */
function birthdays_defaults_disable() {
  if (field_info_instance('user', 'birthdays', 'user')) {
    drupal_set_message(t('Note that the <em>birthdays</em> field will be deleted, when you uninstall Birthdays defaults.'), 'warning');
  }
}

/**
 * Implements hook_uninstall().
 */
function birthdays_defaults_uninstall() {

  // Delete the default action unless it has been overridden.
  if ($action = actions_load(variable_get('birthdays_defaults_action', 0))) {
    if (unserialize($action->parameters) == _birthdays_defaults_action_parameters()) {
      actions_delete($action->aid);
    }
  }
  variable_del('birthdays_defaults_action');

  // Delete the default field.
  if (field_info_field('birthdays')) {
    field_delete_field('birthdays');
    field_purge_batch(5);
  }
}

Functions

Namesort descending Description
birthdays_defaults_disable Implements hook_disable().
birthdays_defaults_install Implements hook_install().
birthdays_defaults_uninstall Implements hook_uninstall().
birthdays_defaults_update_7003 Replaces the non existing token [site:uri-brief] with [site:url-brief] in the default happy birthday message.
_birthdays_defaults_action_parameters Get the default action settings.