<?php

/**
 * Implements HOOK_views_data().
 */
function diy_product_views_data() {
  $data['diy_product']['table']['group'] = t('DIY products');

  $data['diy_product']['table']['base'] = array(
    'field' => 'pid', // This is the identifier field for the view.
    'title' => t('DIY products'),
    'help' => t('Contains imported products.'),
    'weight' => -10,
  );

  $data['diy_product']['shop_nid'] = array(
    'title' => t('Shop nid'),
    'field' => array(
      'handler' => 'views_handler_field_numeric',
      'click sortable' => TRUE,
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'relationship' => array(
      'base' => 'node',
      'base field' => 'nid',
      'handler' => 'views_handler_relationship',
      'label' => t('Node relationship'),
      'title' => t('Node relationship')
    )
  );

  $numeric_fields = array(
    'external_id',
    'price',
    'delivery_costs'
  );

  $text_fields = array(
    'source',
    'title',
    'article_number',
    'description',
    'deeplink',
    'producer',
    'ean',
    'delivery_time',
    'image_url'
  );

  $date_fields = array(
    'created',
    'updated'
  );

  foreach($text_fields as $field) {
    $data['diy_product'][$field] = array(
      'title' => t($field),
      'help' => t($field),
      'field' => array(
        'handler' => 'views_handler_field',
        'click sortable' => TRUE,
      ),
      'sort' => array(
        'handler' => 'views_handler_sort',
      ),
      'filter' => array(
        'handler' => 'views_handler_filter_string',
      ),
      'argument' => array(
        'handler' => 'views_handler_argument_string',
      ),
    );
  }

  foreach($numeric_fields as $field) {
    $data['diy_product'][$field] = array(
      'title' => t($field),
      'help' => t($field),
      'field' => array(
        'handler' => 'views_handler_field_numeric',
        'click sortable' => TRUE,
      ),
      'filter' => array(
        'handler' => 'views_handler_filter_numeric',
      ),
      'sort' => array(
        'handler' => 'views_handler_sort',
      ),
    );
  }

  foreach($date_fields as $field) {
    $data['diy_product'][$field] = array(
      'title' => t($field),
      'help' => t($field),
      'field' => array(
        'handler' => 'views_handler_field_date',
      ),
      'filter' => array(
        'handler' => 'views_handler_filter_date',
      ),
      'sort' => array(
        'handler' => 'views_handler_sort_date',
      ),
    );
  }

  $data['diy_product_price']['table']['group'] = t('DIY product prices');
  $data['diy_product_price']['table']['base'] = array(
    'field' => 'pid', // This is the identifier field for the view.
    'title' => t('DIY product prices'),
    'help' => t('Imported product prices.'),
    'weight' => -10,
  );

  $data['diy_product_price']['pid'] = array(
    'title' => t('Price id'),
    'help' => t('Price id'),
    'field' => array(
      'handler' => 'views_handler_field_numeric',
      'click sortable' => TRUE,
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );

  $data['diy_product_price']['shop_nid'] = array(
    'title' => t('Shop nid'),
    'help' => t('Shop nid'),
    'field' => array(
      'handler' => 'views_handler_field_numeric',
      'click sortable' => TRUE,
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'relationship' => array(
      'base' => 'node',
      'base field' => 'nid',
      'handler' => 'views_handler_relationship',
      'label' => t('Node relationship (shop)'),
      'title' => t('Node relationship (shop)')
    )
  );

  $data['diy_product_price']['product_nid'] = array(
    'title' => t('Product nid'),
    'help' => t('Product nid'),
    'field' => array(
      'handler' => 'views_handler_field_numeric',
      'click sortable' => TRUE,
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'relationship' => array(
      'base' => 'node',
      'base field' => 'nid',
      'handler' => 'views_handler_relationship',
      'label' => t('Node relationship (product)'),
      'title' => t('Node relationship (product)')
    )
  );
  $data['diy_product_price']['source'] = array(
    'title' => t('Source'),
    'help' => t('Source'),
    'field' => array(
      'handler' => 'views_handler_field',
      'click sortable' => TRUE,
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );
  $data['diy_product_price']['delivery_time'] = array(
    'title' => t('Delivery time'),
    'help' => t('Delivery time'),
    'field' => array(
      'handler' => 'views_handler_field',
      'click sortable' => TRUE,
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );
  $data['diy_product_price']['deeplink'] = array(
    'title' => t('Deeplink'),
    'help' => t('Deeplink'),
    'field' => array(
      'handler' => 'views_handler_field',
      'click sortable' => TRUE,
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );
  $data['diy_product_price']['price'] = array(
    'title' => t('Price'),
    'help' => t('Price'),
    'field' => array(
      'handler' => 'views_handler_field_numeric',
      'click sortable' => TRUE,
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );
  $data['diy_product_price']['delivery_costs'] = array(
    'title' => t('Delivery costs'),
    'help' => t('Delivery costs'),
    'field' => array(
      'handler' => 'views_handler_field_numeric',
      'click sortable' => TRUE,
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );
  $data['diy_product_price']['status'] = array(
    'title' => t('Status'),
    'help' => t('Status'),
    'field' => array(
      'handler' => 'views_handler_field_numeric',
      'click sortable' => TRUE,
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );
  $data['diy_product_price']['created'] = array(
    'title' => t('Created'),
    'help' => t('Created'),
    'field' => array(
      'handler' => 'views_handler_field_date',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_date',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort_date',
    ),
  );
  $data['diy_product_price']['updated'] = array(
    'title' => t('Updated'),
    'help' => t('Updated'),
    'field' => array(
      'handler' => 'views_handler_field_date',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_date',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort_date',
    ),
  );

  return $data;
}