You are here

basic_cart.module in Basic cart 8

File

basic_cart.module
View source
<?php

use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\basic_cart\OrderConnectStorage;
use Drupal\basic_cart\Utility;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\views\ViewExecutable;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;
function basic_cart_theme($existing, $type, $theme, $path) {
  return array(
    'basic_cart_cart_template' => array(
      'variables' => array(
        'basic_cart' => NULL,
      ),
    ),
    'total_price_markup' => array(
      'variables' => array(
        'price' => NULL,
      ),
    ),
    'cart_form' => array(
      'render element' => 'form',
    ),
    'basic_cart_quantity_prefix' => array(
      'variables' => array(
        'basic_cart' => NULL,
      ),
    ),
    'basic_cart_quantity_suffix' => array(
      'variables' => array(
        'basic_cart' => NULL,
      ),
    ),
    'basic_cart_thank_you' => array(
      'variables' => array(
        'basic_cart' => NULL,
      ),
    ),
  );
}

/**
 * Implements hook_ENTITY_TYPE_view() for node entities.
 */
function basic_cart_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {

  //die;

  /*print $entity->hasfield('add_to_cart'); die;
    if($entity->getEntityTypeId() == "test1") {
     die;
    } */
}

/**
* Implements hook_page_attachments().
*/
function basic_cart_page_attachments(array &$page) {
  $page['#attached']['library'][] = 'basic_cart/basic_cart';
}

/**
* Implements hook_entity_insert().
*/
function basic_cart_entity_insert(EntityInterface $node) {
  $utility = new Utility();
  if (Utility::isBasicCartOrder($node
    ->bundle())) {
    $get_cart = $utility::getCart();
    $cart = $get_cart['cart'];
    $nid = $node
      ->id();
    basic_cart_order_send_notifications($node);
    $order = new OrderConnectStorage();
    foreach ($cart as $key => $value) {
      $params['oid'] = $nid;
      $params['id'] = $key;
      $params['entitytype'] = $value
        ->getEntityTypeId();
      $params['quantity'] = $get_cart['cart_quantity'][$key];
      $order
        ->insert($params);
    }
    $utility::emptyCart();
  }
}

/**
* Implements hook_form_alter().
*/
function basic_cart_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form_ids = array(
    'node_basic_cart_order_edit_form',
    'node_basic_cart_order_form',
  );
  if (in_array($form_id, $form_ids)) {
    $form['title']['widget'][0]['value']['#title'] = t('Name');
    unset($form['basic_cart_vat']);
    unset($form['basic_cart_total_price']);
    $form['actions']['submit']['#value'] = t('Place Order');
    foreach (array_keys($form['actions']) as $action) {
      if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
        $form['actions'][$action]['#submit'][] = 'basic_cart_order_submit';
      }
    }
  }
}

/**
* Implements hook_entity_presave().
*/
function basic_cart_entity_presave(EntityInterface $node) {
  if (Utility::isBasicCartOrder($node
    ->bundle())) {
    $get_price = Utility::getTotalPrice();
    $bundle = $node
      ->bundle();
    if (empty($node
      ->id())) {
      $node
        ->set('basic_cart_vat', $get_price->vat);
      $node
        ->set('basic_cart_total_price', $get_price->total);
    }
  }
}

/**
* Implements hook_entity_delete().
*/
function basic_cart_entity_delete(EntityInterface $node) {
  $utility = new Utility();
  if (Utility::isBasicCartOrder($node
    ->bundle())) {
    $entitytype = $node
      ->getEntityTypeId();
    $id = $node
      ->id();
    $bundle = $node
      ->bundle();
    if (isset($entitytype) && isset($id)) {
      $params['oid'] = $id;
      $params['entitytype'] = $entitytype;
      OrderConnectStorage::orderDelete($params);
    }
  }
}

/**
* Implements hook_mail().
*/
function basic_cart_mail($key, &$message, $params) {
  $utility = new Utility();
  $config = $utility
    ->checkoutSettings();
  $options = array(
    'langcode' => $message['langcode'],
  );
  $message['from'] = \Drupal::config('system.site')
    ->get('mail');
  switch ($key) {
    case 'admin_mail':
      $message['subject'] = SafeMarkup::checkPlain($config
        ->get('admin')['subject']);
      $message['body'][] = Xss::filter($params['admin_message']);
      break;
    case 'user_mail':
      $message['subject'] = SafeMarkup::checkPlain($config
        ->get('user')['subject']);
      $message['body'][] = Xss::filter($params['user_message']);
      break;
  }
}

/**
 * Sending emails.
 */
function basic_cart_order_send_notifications($order) {
  $mailManager = \Drupal::service('plugin.manager.mail');

  // Admin message.
  $utility = new Utility();
  $token = \Drupal::token();
  $config = $utility
    ->checkoutSettings();
  $message_html = $config
    ->get('admin')['body'];

  // Token data.
  $data = array(
    'node' => $order,
  );
  $message_html = $token
    ->replace($message_html, $data, array(
    'callback' => 'basic_cart_order_tokens_format',
  ));

  // Admin mail.
  $params['admin_message'] = $message_html;
  $site_mail = \Drupal::config('system.site')
    ->get('mail');
  $send = true;
  $admin_emails = $config
    ->get('admin_emails');
  $langcode = \Drupal::currentUser()
    ->getPreferredLangcode();
  if (empty($admin_emails)) {

    // Sending mail to admin.
    $message = $mailManager
      ->mail('basic_cart', 'admin_mail', $site_mail, $langcode, $params, NULL, $send);
    $mails_sent = 0;
    if ($message['result']) {
      $mails_sent++;
    }
  }
  else {
    $admin_emails = explode("\n", $admin_emails);
    if (is_array($admin_emails) && !empty($admin_emails)) {
      $ok = FALSE;
      foreach ($admin_emails as $admin_email) {

        // Sending mail to each admin.
        $message = $mailManager
          ->mail('basic_cart', 'admin_mail', $admin_email, $langcode, $params, NULL, $send);

        // Verifing that the mail was sent for at least one email address.
        if ($message['result']) {
          $ok = TRUE;
        }
      }
      $mails_sent = 0;
      if ($ok) {
        $mails_sent++;
      }
    }
  }

  // User email.
  $send_user_mail = $config
    ->get('send_emailto_user');
  if ($send_user_mail) {
    $message_html = $config
      ->get('user')['body'];

    // Token data.
    $data = array(
      'node' => $order,
    );
    $message_html = $token
      ->replace($message_html, $data, array(
      'callback' => 'basic_cart_order_tokens_format',
    ));
    $params['user_message'] = $message_html;

    // Sending mail.
    $email = $order
      ->getTranslation($langcode)
      ->get('basic_cart_email')
      ->getValue();
    $email = $email[0]['value'];
    $message = $mailManager
      ->mail('basic_cart', 'user_mail', $email, $langcode, $params, NULL, $send);
    if ($message['result']) {
      $mails_sent++;
    }
  }
  return $mails_sent;
}

/**
 * Callback function for formatting the order tokens.
 * 
 * @param array $tokens
 *   The tockens list.
 * 
 * @return array $tokens.
 *   The formatted list of tokens
 */
function basic_cart_order_tokens_format(&$tokens) {
  $utility = new Utility();
  if (is_array($tokens) && count($tokens) > 0) {
    foreach ($tokens as $token => $value) {
      switch ($token) {
        case '[basic_cart_order:basic_cart_total_price]':
          $tokens[$token] = $utility::formatPrice($value
            ->__toString());
          break;
        case '[basic_cart_order:basic_cart_vat]':
          $tokens[$token] = $utility::formatPrice($value
            ->__toString());
          break;
      }
    }
  }
  return $tokens;
}

/**
 * Implements hook_tokens().
 */
function basic_cart_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = array();
  $utility = new Utility();
  $order = isset($data["node"]) ? $data["node"] : array();

  // The first thing that we're going to check for is the type of token - node,
  // user etc...
  if ($type == 'basic_cart_order') {

    // Loop through each of the available tokens.
    foreach ($tokens as $name => $original) {

      // Find the desired token by name
      switch ($name) {
        case 'products':
          $new = '';
          $get_cart = $utility::getCart();
          $cart = $get_cart['cart'];

          // Building the order details.
          $i = 0;
          $products = '';
          foreach ($cart as $nid => $node) {
            $langcode = $node
              ->language()
              ->getId();
            $price_value = $node
              ->getTranslation($langcode)
              ->get('add_to_cart_price')
              ->getValue();
            $title = $node
              ->getTranslation($langcode)
              ->get('title')
              ->getValue();
            $unit_price = $utility::formatPrice($price_value[0]['value']);
            $new .= ++$i . '. ' . $title[0]['value'] . "\t" . $get_cart['cart_quantity'][$nid] . ' x ' . $unit_price . "\n";
          }

          // Add the new value into the replacements array.
          $replacements[$original] = $new;
          break;
        case 'basic_cart_total_price':
          $total = Utility::getTotalPrice();
          $langcode = $order
            ->language()
            ->getId();
          $title = $order
            ->getTranslation($langcode)
            ->get('title')
            ->getValue();
          $replacements[$original] = !empty($total) && isset($total->total) ? $total->total : 0;
          break;
        case 'basic_cart_email':
          $langcode = $order
            ->language()
            ->getId();
          $email = $order
            ->getTranslation($langcode)
            ->get('basic_cart_email')
            ->getValue();
          $replacements[$original] = isset($email[0]['value']) ? $email[0]['value'] : "";
          break;
        case 'basic_cart_vat':
          $order = $data["node"];
          $total = Utility::getTotalPrice();
          $langcode = $order
            ->language()
            ->getId();
          $vat = $order
            ->getTranslation($langcode)
            ->get('basic_cart_vat')
            ->getValue();
          $replacements[$original] = isset($vat[0]['value']) ? $vat[0]['value'] : 0;
          break;
      }
    }
  }

  // Return the replacements.
  return $replacements;
}

/**
 * Implements hook_token_info().
 */
function basic_cart_token_info() {
  $info = array();

  // Add any new tokens.
  $info['tokens']['basic_cart_order']['products'] = 'Listing of ordered products.';
  $info['tokens']['basic_cart_order']['basic_cart_total_price'] = 'Total Price of the Order';
  $info['tokens']['basic_cart_order']['basic_cart_vat'] = 'VAT value of the order.';
  $info['tokens']['basic_cart_order']['basic_cart_email'] = 'Email defined with the order.';

  // Return them.
  return $info;
}
function basic_cart_user_login($account) {
  $utility = new Utility();
  $utility
    ->loggedInActionCart();
}
function basic_cart_order_submit(&$form, FormStateInterface $form_state, $form_id) {
  $utility = new Utility();
  $config = $utility
    ->checkoutSettings();
  $location = trim($config
    ->get('thankyou')['custom_page']);
  if ($location) {
    $redirect = \Drupal::pathValidator()
      ->getUrlIfValid($location);
    $form_state
      ->setRedirectUrl($redirect);
  }
  else {
    $url = Url::fromRoute('basic_cart.thankyou');
    $form_state
      ->setRedirectUrl($url);
  }
}

/*
function basic_cart_views_pre_render(ViewExecutable $view) {
 // Scramble the order of the rows shown on this result page.
 // Note that this could be done earlier, but not later in the view execution
 // process.
 print_r($view->field['add_to_cart']); die;
 foreach ($view->result as $value) {
   # code...
   print_r($value);
 }
 die;
 //print_r($view->result); die;
 shuffle($view->result);
}
*/