You are here

sendgrid_integration.install in SendGrid Integration 7

Contains install and update functions for SendGrid Integration

File

sendgrid_integration.install
View source
<?php

/**
 * @file
 * Contains install and update functions for SendGrid Integration
 */
use SendGrid\Client;

/**
 * Implemenets hook_enable().
 */
function sendgrid_integration_enable() {
  variable_set('mail_system', [
    'default-system' => 'SendGridMailSystem',
  ]);
}

/**
 * Implements hook_disable().
 */
function sendgrid_integration_disable() {
  variable_set('mail_system', [
    'default-system' => 'DefaultMailSystem',
  ]);
}

/**
 * Implements hook_install().
 */
function sendgrid_integration_install() {
  $t = get_t();
  drupal_set_message($t('You can configure SendGrid settings under Administer > Configuration > !link', [
    '!link' => l($t('SendGrid settings'), 'admin/config/system/sendgrid'),
  ]));
}

/**
 * Implements hook_requirements().
 */
function sendgrid_integration_requirements($phase) {
  $requirements = [];
  $apikey = variable_get('sendgrid_integration_apikey', NULL);
  switch ($phase) {
    case 'install':
      $dir = drupal_get_path('module', 'sendgrid_integration') . '/vendor/composer';
      if (module_exists('xautoload') && file_exists($dir)) {
        xautoload()->adapter
          ->composerDir($dir);
      }
      if (!class_exists(Client::class)) {
        $requirements['sendgrid_integration'] = [
          'title' => t('SendGrid API Wrapper'),
          'value' => t('API Wrapper Library Missing'),
          'severity' => REQUIREMENT_ERROR,
          'description' => t('You need to install the SendGrid API wrapper via one of the two methods documented in the README.md of the module.'),
        ];
      }
      else {
        $requirements['sendgrid_integration'] = [
          'title' => t('SendGrid API Wrapper'),
          'severity' => REQUIREMENT_OK,
          'value' => t('API wrapper installed.'),
        ];
      }
      break;
    case 'runtime':
      if (empty($apikey)) {
        $requirements['sendgrid_integration'] = [
          'title' => t('SendGrid Integration Settings'),
          'value' => t('API Key Not Set'),
          'severity' => REQUIREMENT_ERROR,
          'description' => t('You need to <a href="@url">add a SendGrid API Secret Key</a> for Drupal to be able to deliver mail through SendGrid Integration.', [
            '@url' => url('admin/config/services/sendgrid'),
          ]),
        ];
      }
      else {
        $requirements['sendgrid_integration'] = [
          'title' => t('SendGrid Integration Settings'),
          'severity' => REQUIREMENT_OK,
          'value' => t('API Secret Key saved'),
        ];
      }
      break;
  }
  return $requirements;
}

/**
 * Implements hook_uninstall().
 */
function sendgrid_integration_uninstall() {
  variable_del('sendgrid_integration_apikey_id');
  variable_del('sendgrid_integration_apikey');
  variable_del('sendgrid_integration_test_defaults');
  variable_del('sendgrid_integration_maillog_log');
  variable_del('sendgrid_integration_maillog_devel');
}

/**
 * Remove variables no longer used.
 */
function sendgrid_integration_update_7001() {

  // Deleting unused variables.
  variable_del('sendgrid_integration_ssl');
  variable_del('sendgrid_integration_username');
}

/**
 * Remove variables no longer used.
 */
function sendgrid_integration_update_7002() {

  // Emptying old variables.
  variable_del('sendgrid_integration_apikey_id', '');
}