You are here

function sendinblue_signup_subscribe_form_validate in SendinBlue 7

Same name and namespace in other branches
  1. 7.2 sendinblue.module \sendinblue_signup_subscribe_form_validate()

Validate handler to add users to lists on subscription form submission.

File

./sendinblue.module, line 334
The entry point of Sendinblue module.

Code

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);
  }
}