<?php
/**
 * @var int $AUTH_USER_ID
 * @var int $catalogue
 * @class nc_netshop
 * @var nc_netshop $netshop
 * @var array $current_catalogue
 * @var array $current_cc
 *
 **/

if ($AUTH_USER_ID) {

  $nc_core = nc_core::get_object();


  if (class_exists('nc_netshop') and !$netshop)
    $netshop = nc_netshop::get_instance($catalogue);


  $dashamail = new DashaMail();
  $dashamail_vars = $nc_core->modules->get_module_vars();
  $dashamail_vars = $dashamail_vars['dashamail'];


  // Посетил сайт
  if (!$_SESSION['dashamail_online']) {
    $_SESSION['dashamail_online'] = 1;
    $dashamail->event_visit();
  }


  // Начал оформление заказа
  if ($dashamail->order_class_id == $current_cc['Class_ID'] and $action == 'add') {
    $dashamail->event_purchase_start();
  }


  $online_user_id = $nc_core->db->get_var("SELECT `DashaMail_Online_ID` FROM `Module_DashaMail_Online` WHERE `User_ID` = " . $AUTH_USER_ID . " LIMIT 1");

  $dashamail_online = new dashamail_online();
  if ($online_user_id) {
    $dashamail_online->load($online_user_id);
  }else {
    $dashamail_online->set('user_id', $AUTH_USER_ID);
  }
  $dashamail_online->set('time_end', time() + 60 * 3);
  $dashamail_online->set('online', 1);

  // Если есть интернет магазин то записывать данные корзины
  if ($netshop) {
    if (is_numeric($dashamail_vars['CART_ROUTE'])) $cart_url = nc_folder_url($dashamail_vars['CART_ROUTE']);
    else $cart_url = $dashamail_vars['CART_ROUTE'];

    $cart = array(
      'products' => array(),
      'cart_url' => $cart_url,
    );

    if ($netshop->cart->get_item_count()) {
      foreach ($netshop->cart->get_items() as $item) {

        // Определить тип поля
        $sql = "SELECT `TypeOfData_ID` FROM `Field` WHERE `Class_ID` = " . $item['Class_ID'] . " AND `Field_Name` = '" . $dashamail_vars['CART_ITEM_IMAGE'] . "'";
        $field_TypeOfData_ID = $nc_core->db->get_var($sql);


        $image = '';
        $is_image = false;

        // Выбрать изображение согласно типу поля
        switch ($field_TypeOfData_ID) {
          case 6:

            $is_preview = file_exists($DOCUMENT_ROOT . $item[$dashamail_vars['CART_ITEM_IMAGE'] . '_preview']);
            if ($is_preview) {
              $image = $item[$dashamail_vars['CART_ITEM_IMAGE'] . '_preview'];
            }else {
              $image = $item[$dashamail_vars['CART_ITEM_IMAGE']];
            }
            if ($image) $is_image = true;
            break;

          case 11:
            $is_preview = file_exists($DOCUMENT_ROOT . $item[$dashamail_vars['CART_ITEM_IMAGE']]->records[0]['Preview']);
            if ($is_preview) {
              $image = $item[$dashamail_vars['CART_ITEM_IMAGE']]->records[0]['Preview'];
            }else {
              $image = $item[$dashamail_vars['CART_ITEM_IMAGE']]->records[0]['Path'];
            }
            if ($image) $is_image = true;
            break;

          default:
            $image = $item[$dashamail_vars['CART_ITEM_IMAGE']];
            break;
        }

        if ($is_image) $image = nc_get_scheme() . '://' . $current_catalogue['Domain'] . $image;

        // Данные корзины
        $cart['products'][] = array(
          'title' => $nc_core->db->escape($item['FullName']),
          'price' => $item['ItemPriceF'],
          'quantity' => $item['Qty'],
          'total' => $item['TotalPriceF'],
          'url' => nc_get_scheme() . '://' . $current_catalogue['Domain'] . nc_message_link($item['Message_ID'], $item['Class_ID']),
          'image' => $image,
        );
      }
    }
    if ($_GET['test']) var_dump($cart, $field_TypeOfData_ID, $sql);

    $dashamail_online->set('cart', $netshop->cart->get_item_count() ? serialize($cart) : 0);
  }


  // Сохранить
  $dashamail_online->save();
}
