<?php

/**
 * Implementation of THEMENAME_alpha_preprocess_HOOK().
 */
function diy_alpha_preprocess_node(&$vars) {
  global $user;

  if($vars['type'] == 'news' && $vars['view_mode'] == 'full') {
    if(isset($vars['field_sponsored'][0]['value']) && $vars['field_sponsored'][0]['value'] == 1) {
      $vars['sponsored_text'] = t('Sponsored');

      if(is_array($vars['field_article_steps'])) {
        foreach($vars['field_article_steps'] as $key => $value) {
          if(is_array($vars['content']['field_article_steps'][$key]['entity']['field_collection_item'])) {
            foreach($vars['content']['field_article_steps'][$key]['entity']['field_collection_item'] as $key2 => $item) {
              if(isset($vars['content']['field_article_steps'][$key]['entity']['field_collection_item'][$key2]['field_article_steps_content'][0]['#markup'])) {
                $c = $vars['content']['field_article_steps'][$key]['entity']['field_collection_item'][$key2]['field_article_steps_content'][0]['#markup'];
                $vars['content']['field_article_steps'][$key]['entity']['field_collection_item'][$key2]['field_article_steps_content'][0]['#markup'] = check_markup($c, 'full_html_nofollow');
              }
            }
          }
        }
      }
    }
  }

  if($vars['type'] == 'video' && $vars['view_mode'] == 'full') {
    if($article = diy_structure_video_get_article($vars['node']->nid)) {
      $products = diy_tools_get_article_products($article);
      if(!empty($products)) {
        $items = array();

        foreach($products as $product) {
          $nv = node_view(node_load($product), 'embed_tools');
          $items[] = render($nv);
        }
        $vars['product_list'] = theme_item_list(array(
          'title' => t('Used products'),
          'items' => $items,
          'type' => 'ul',
          'attributes' => array()
        ));
      }
    }
  }

  if($vars['type'] == 'article' && $vars['view_mode'] == 'full') {
    $products = diy_tools_get_article_products($vars['node']);
    if(!empty($products)) {
      $items = array();

      foreach($products as $product) {
        $nv = node_view(node_load($product), 'embed_tools');
        $items[] = render($nv);
      }
      $vars['product_list'] = theme_item_list(array(
        'title' => t('Used products'),
        'items' => $items,
        'type' => 'ul',
        'attributes' => array()
      ));
    }

    $products_partner = diy_tools_get_article_products_partner($vars['node']);

    if(!empty($products_partner)) {
      $items = array();

      foreach($products_partner as $product) {
        $nv = node_view(node_load($product), 'embed_tools');
        $items[] = render($nv);
      }
      $vars['product_list_partner'] = theme_item_list(array(
        'title' => t('Products of our partners'),
        'items' => $items,
        'type' => 'ul',
        'attributes' => array()
      ));
    }

  }

  if(($vars['type'] == 'ct_werkzeug' || $vars['type'] == 'material') && $vars['view_mode'] == 'full') {
    $products = diy_tools_tm_get_products(array($vars['node']->nid));

    if(!empty($products)) {
      $items = array();

      foreach($products as $product) {
        $nv = node_view(node_load($product), 'embed_product');
        $items[] = render($nv);
      }
      $vars['product_list'] = theme_item_list(array(
        'title' => NULL,
        'items' => $items,
        'type' => 'ul',
        'attributes' => array()
      ));
    }

    $products_promoted = diy_tools_tm_get_products(array($vars['node']->nid), true);

    if(!empty($products_promoted)) {
      $items = array();

      foreach($products_promoted as $product) {
        $nv = node_view(node_load($product), 'embed_product');
        $items[] = render($nv);
      }

      $vars['product_list_promoted'] = theme_item_list(array(
        'title' => t('Recommended products'),
        'items' => $items,
        'type' => 'ul',
        'attributes' => array()
      ));
    }
  }

  if($vars['type'] == 'product' && $vars['view_mode'] == 'full') {
    if($price_items = diy_product_get_price_items($vars['node'])) {

      foreach($price_items as $item) {
        $delivery = $item->delivery_time;

        $shop = node_load($item->shop_nid);
        $logo = l(diy_product_get_logo_for_shop($item->shop_nid), $item->deeplink, array(
          'absolute' => true,
          'html' => true,
          'attributes' => array(
            'target' => '_blank',
            'class' => 'shop-logo',
            'rel' => 'nofollow',
            'onclick' => 'Drupal.behaviors.trackOutboundLink(\'outbound-shop\', \''. $shop->title .'\',\''. $item->deeplink .'\', '. $vars['nid'] .');return false;'
          )
        ));


        $price_informations = diy_product_get_price_information($item);
        $price = theme('diy_product_priceitem', array('data' => $price_informations));

        $rows[] = array(
          'data' => array(
            $logo,
            $delivery,
            $price
          )
        );
      }

      $vars['price_items'] = theme_table(array(
        'header' => array(
          array(
            'data' => t('Shop'),
            'class' => 'shop-column'
          ),
          array(
            'data' => t('Delivery time'),
            'class' => 'delivery-column'
          ),
          array(
            'data' => t('Price and Delivery'),
            'class' => 'price-column'
          )
        ),
        'attributes' => array(
          'class' => array(
            'plain-table',
            'price-items-table'
          )
        ),
        'caption' => NULL,
        'colgroups' => NULL,
        'sticky' => NULL,
        'empty' => NULL,
        'rows' => $rows
      ));
    }

  }
}