You are here

sendinblue.module in SendinBlue 7

Same filename and directory in other branches
  1. 8.2 sendinblue.module
  2. 8 sendinblue.module
  3. 7.2 sendinblue.module

The entry point of Sendinblue module.

File

sendinblue.module
View source
<?php

/**
 * @file
 * The entry point of Sendinblue module.
 */
define('SENDINBLUE_QUEUE_CRON', 'sendinblue');
define('SENDINBLUE_BATCH_QUEUE_CRON', 'sendinblue_batch');
define('SENDINBLUE_SIGNUP_BLOCK', 1);
define('SENDINBLUE_SIGNUP_PAGE', 2);
define('SENDINBLUE_SIGNUP_BOTH', 3);

/**
 * Implements hook_help().
 */
function sendinblue_help($path, $arg) {
  switch ($path) {
    case 'admin/help#sendinblue':
      return t('Sendinblue module provides integration with the SendinBlue email delivery service.');
  }
}

/**
 * Implements hook_menu().
 */
function sendinblue_menu() {
  $items = array();
  $items['admin/config/system/sendinblue'] = array(
    'title' => 'SendinBlue',
    'description' => 'Manage SendinBlue Settings.',
    'page callback' => 'sendinblue_home_page',
    'page arguments' => array(
      'sendinblue_home_page',
    ),
    'access arguments' => array(
      'administer sendinblue',
    ),
    'file' => 'includes/sendinblue_home.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/config/system/sendinblue/lists'] = array(
    'title' => 'Lists',
    'description' => 'Display SendinBlue Lists and Contacts.',
    'page callback' => 'sendinblue_list_page',
    'access callback' => 'sendinblue_signup_entity_access',
    'access arguments' => array(
      'administer sendinblue',
    ),
    'file' => 'includes/sendinblue_list.admin.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 8,
  );
  $items['admin/config/system/sendinblue/campaigns'] = array(
    'title' => 'Campaigns',
    'description' => 'Display SendinBlue Campaigns.',
    'page callback' => 'sendinblue_campaign_page',
    'access callback' => 'sendinblue_signup_entity_access',
    'access arguments' => array(
      'administer sendinblue',
    ),
    'file' => 'includes/sendinblue_campaign.admin.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 9,
  );
  $items['admin/config/system/sendinblue/statistics'] = array(
    'title' => 'Statistics',
    'description' => 'Display SendinBlue Statistics.',
    'page callback' => 'sendinblue_statistic_page',
    'access callback' => 'sendinblue_signup_entity_access',
    'access arguments' => array(
      'administer sendinblue',
    ),
    'file' => 'includes/sendinblue_statstic.admin.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 9,
  );
  $items['admin/config/system/sendinblue/global'] = array(
    'title' => 'Home',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/config/system/sendinblue/signup/%sendinblue_signup/edit'] = array(
    'title' => 'Edit a signup form',
    'description' => 'Edit a Signup form.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'sendinblue_signup_form',
      5,
    ),
    'load arguments' => array(
      5,
    ),
    'access arguments' => array(
      'administer sendinblue',
    ),
    'file' => 'includes/sendinblue_signup.admin.inc',
    'type' => MENU_CALLBACK,
  );
  $items['admin/config/system/sendinblue/signup/%sendinblue_signup/delete'] = array(
    'title' => 'Delete Signup form',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'sendinblue_signup_signup_delete_form',
      5,
    ),
    'access arguments' => array(
      'administer sendinblue',
    ),
    'file' => 'includes/sendinblue_signup.admin.inc',
  );
  $signups = sendinblue_signup_load_multiple();
  foreach ($signups as $signup) {
    if (intval($signup->mode) == SENDINBLUE_SIGNUP_PAGE || intval($signup->mode) == SENDINBLUE_SIGNUP_BOTH) {
      $items[$signup->settings['path']] = array(
        'title' => $signup->title,
        'page callback' => 'drupal_get_form',
        'page arguments' => array(
          'sendinblue_signup_subscribe_page_' . $signup
            ->identifier() . '_form',
          $signup,
          'sendinblue_signup_page',
        ),
        'access callback' => 'user_access',
        'access arguments' => array(
          'access sendinblue signup pages',
        ),
        'type' => MENU_SUGGESTED_ITEM,
      );
    }
  }
  return $items;
}

/**
 * Implements hook_permission().
 */
function sendinblue_permission() {
  return array(
    'administer sendinblue' => array(
      'title' => t('administer sendinblue'),
      'description' => t('Access the sendinblue configuration options.'),
    ),
    'access sendinblue signup pages' => array(
      'title' => t('Access all SendinBlue Signup Pages.'),
      'description' => t('This does not effect Signup Blocks: only Pages.'),
    ),
  );
}

/**
 * Implements hook_entity_info().
 */
function sendinblue_entity_info() {
  $entities = array(
    'sendinblue_signup' => array(
      'label' => t('SendinBlue Signup'),
      'plural label' => t('SendinBlue Signups'),
      'entity class' => 'SendinBlueSignup',
      'controller class' => 'EntityAPIControllerExportable',
      'base table' => 'sendinblue_signup',
      'uri callback' => 'entity_class_uri',
      'fieldable' => FALSE,
      'label callback' => 'entity_class_label',
      'module' => 'sendinblue',
      'entity keys' => array(
        'id' => 'mcsId',
        'name' => 'name',
      ),
      'bundles' => array(
        'sendinblue_signup' => array(
          'label' => t('SendinBlue Signup'),
        ),
      ),
      'view modes' => array(
        'full' => array(
          'label' => t('Full'),
          'custom settings' => FALSE,
        ),
      ),
      // Enable the entity API's admin UI.
      'admin ui' => array(
        'path' => 'admin/config/system/sendinblue/signup',
        'file' => 'includes/sendinblue_signup.admin.inc',
        'controller class' => 'SendinBlueSignupUIController',
      ),
      'access arguments' => array(
        'administer sendinblue',
      ),
      'access callback' => 'sendinblue_signup_entity_access',
    ),
  );
  return $entities;
}

/**
 * Implements hook_block_info().
 */
function sendinblue_block_info() {
  $blocks = array();
  $signups = sendinblue_signup_load_multiple();
  foreach ($signups as $signup) {
    if (intval($signup->mode) == SENDINBLUE_SIGNUP_BLOCK || intval($signup->mode) == SENDINBLUE_SIGNUP_BOTH) {
      $blocks[$signup
        ->identifier()] = array(
        'info' => t('SendinBlue Subscription Form: @name', array(
          '@name' => $signup
            ->label(),
        )),
        'cache' => DRUPAL_CACHE_PER_USER,
      );
    }
  }
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function sendinblue_block_view($delta = '') {
  $signup = sendinblue_signup_load($delta);
  if ($signup) {
    $block = array(
      'subject' => $signup->title,
      'content' => drupal_get_form('sendinblue_signup_subscribe_block_' . $signup->name . '_form', $signup, 'sendinblue_signup_block'),
    );
  }
  else {
    $block = FALSE;
  }
  return $block;
}

/**
 * Check whether it is possible to access or not.
 *
 * @return bool
 *   If current has logged_in, acces required, and is on SendInBlue loged-in status, return 1, else 0.
 */
function sendinblue_signup_entity_access($access) {
  return user_is_logged_in() && user_access($access) && SendinblueManager::isLoggedInState();
}

/**
 * Loads multiple signup forms.
 */
function sendinblue_signup_load_multiple($signup_ids = array(), $conditions = array(), $reset = FALSE) {
  if (empty($signup_ids)) {
    $signup_ids = FALSE;
  }
  return entity_load('sendinblue_signup', $signup_ids, $conditions, $reset);
}

/**
 * Wrapper around sendinblue_signup_load_multiple.
 */
function sendinblue_signup_load($signup_id) {
  $signups = sendinblue_signup_load_multiple(array(
    $signup_id,
  ));
  if ($signups) {
    return reset($signups);
  }
  else {
    return FALSE;
  }
}

/**
 * Implements hook_forms().
 */
function sendinblue_forms($form_id, $args) {
  $forms = array();
  if (strpos($form_id, 'sendinblue_') === 0 && isset($args[1])) {
    if ($args[1] == 'sendinblue_signup_page') {
      $forms['sendinblue_signup_subscribe_page_' . $args[0]->name . '_form'] = array(
        'callback' => 'sendinblue_signup_subscribe_form',
      );
    }
    if ($args[1] == 'sendinblue_signup_block') {
      $forms['sendinblue_signup_subscribe_block_' . $args[0]->name . '_form'] = array(
        'callback' => 'sendinblue_signup_subscribe_form',
      );
    }
  }
  return $forms;
}

/**
 * Returns a subscription form for sendinblue lists.
 */
function sendinblue_signup_subscribe_form($form, &$form_state, $signup, $type) {
  $form['#attributes'] = array(
    'class' => array(
      'sendinblue-signup-subscribe-form',
    ),
  );
  $form['description'] = array(
    '#markup' => check_plain($signup->settings['description']),
  );
  $form['fields'] = array(
    '#prefix' => '<div id="sendinblue-newsletter-' . check_plain($signup->settings['subscription']['settings']['list']) . '-mergefields" class="sendinblue-newsletter-mergefields">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
  );
  if (isset($signup->settings['fields']['mergefields'])) {
    $merge_fields = $signup->settings['fields']['mergefields'];
    $attributes = variable_get(SendinblueManager::ATTRIBUTE_LISTS, array());
    if (is_array($merge_fields)) {
      foreach ($merge_fields as $key => $value) {
        if ($key == 'EMAIL') {
          $form['fields'][$key] = array(
            '#type' => 'textfield',
            '#title' => check_plain($value['label']),
            '#attributes' => array(
              'style' => 'width:100%;box-sizing:border-box;',
            ),
            '#required' => TRUE,
          );
        }
        else {
          if (isset($value['check'])) {
            foreach ($attributes as $attribute) {
              if ($attribute['name'] == $key) {
                $type = $attribute['type'];
                if ($type == 'category') {
                  $enumerations = $attribute['enumeration'];
                }
                break;
              }
            }
            if ($type != 'category') {
              $form['fields'][$key] = array(
                '#type' => 'textfield',
                '#title' => check_plain($value['label']),
                '#attributes' => array(
                  'style' => 'width:100%;box-sizing:border-box;',
                ),
                '#required' => isset($value['required']) ? TRUE : FALSE,
              );
            }
            else {
              $options = array();
              foreach ($enumerations as $enumeration) {
                $options[$enumeration['value']] = $enumeration['label'];
              }
              $form['fields'][$key] = array(
                '#type' => 'select',
                '#title' => check_plain($value['label']),
                '#options' => $options,
                '#attributes' => array(
                  'style' => 'width:100%;box-sizing:border-box;',
                ),
                '#required' => isset($value['required']) ? TRUE : FALSE,
              );
            }
          }
        }
      }
    }
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#weight' => 10,
    '#value' => check_plain($signup->settings['fields']['submit_button']),
  );
  return $form;
}

/**
 * Validate handler to add users to lists on subscription form submission.
 */
function sendinblue_signup_subscribe_form_validate($form, &$form_state) {
  $signup = reset($form_state['build_info']['args']);
  $email = $form_state['values']['fields']['EMAIL'];
  if (!valid_email_address($email)) {
    form_set_error('email', check_plain($signup->settings['subscription']['messages']['invalid']));
    return;
  }
  $list_id = $signup->settings['subscription']['settings']['list'];
  $response = SendinblueManager::validationEmail($email, $list_id);
  if ($response['code'] == 'invalid') {
    form_set_error('email', check_plain($signup->settings['subscription']['messages']['invalid']));
    return;
  }
  if ($response['code'] == 'already_exist') {
    form_set_error('email', check_plain($signup->settings['subscription']['messages']['existing']));
    return;
  }
  $email_confirmation = $signup->settings['subscription']['settings']['email_confirmation'];
  if ($email_confirmation == '1') {
    $templage_id = $signup->settings['subscription']['settings']['template'];
  }
  $list_ids = $response['listid'];
  array_push($list_ids, $list_id);
  $info = array();
  $attributes = variable_get(SendinblueManager::ATTRIBUTE_LISTS, array());
  foreach ($attributes as $attribute) {
    if (isset($form_state['values']['fields'][$attribute['name']])) {
      $info[$attribute['name']] = $form_state['values']['fields'][$attribute['name']];
    }
  }
  $response_code = SendinblueManager::subscribeUser($email, $info, $list_ids);
  if ($response_code != 'success') {
    form_set_error('email', check_plain($signup->settings['subscription']['messages']['general']));
    return;
  }

  // Store db.
  $data = SendinblueManager::getSubscriberByEmail($email);
  if ($data == FALSE) {
    $uniqid = uniqid();
    $data = array(
      'email' => $email,
      'info' => serialize($info),
      'code' => $uniqid,
      'is_active' => 1,
    );
    SendinblueManager::addSubscriberTable($data);
  }
  else {
    $uniqid = $data['code'];
  }

  // Send confirm email.
  if ($email_confirmation == '1') {
    SendinblueManager::sendEmail('confirm', $email, $uniqid, $list_id, '-1', $templage_id);
  }
}

/**
 * Submit handler to add users to lists on subscription form submission.
 */
function sendinblue_signup_subscribe_form_submit($form, &$form_state) {
  $signup = reset($form_state['build_info']['args']);
  drupal_set_message(check_plain($signup->settings['subscription']['messages']['success']));
  if ($signup->settings['subscription']['settings']['redirect_url'] != '') {
    $form_state['redirect'] = $signup->settings['subscription']['settings']['redirect_url'];
  }
}

Functions

Namesort descending Description
sendinblue_block_info Implements hook_block_info().
sendinblue_block_view Implements hook_block_view().
sendinblue_entity_info Implements hook_entity_info().
sendinblue_forms Implements hook_forms().
sendinblue_help Implements hook_help().
sendinblue_menu Implements hook_menu().
sendinblue_permission Implements hook_permission().
sendinblue_signup_entity_access Check whether it is possible to access or not.
sendinblue_signup_load Wrapper around sendinblue_signup_load_multiple.
sendinblue_signup_load_multiple Loads multiple signup forms.
sendinblue_signup_subscribe_form Returns a subscription form for sendinblue lists.
sendinblue_signup_subscribe_form_submit Submit handler to add users to lists on subscription form submission.
sendinblue_signup_subscribe_form_validate Validate handler to add users to lists on subscription form submission.

Constants