<?php
/**
 * @file
 * An example module for how to create a module tutorial.
 */
 
 function package_tour_block_info() {
  $blocks['package_tour'] = array('info' => t('Thai Happy Tour & Travel - Package Tour'));
  return $blocks;
}


/**
 * Implements hook_permission().
 */
function package_tour_permission() {
  return array(
    'administer package_tour' => array(
      'title' => t('Access content for the Tour module'),
    )
  );
}

/**
 * Implements hook_filter_tips().
 */
function _package_tour_filter_tips($delta, $format, $long = FALSE) {
  return t('More/Less script can be used in a content. (i.e. [more])');
}

/**
 * Implements hook_filter_info().
 */
function package_tour_filter_info() {
  $filters['package_tour'] = array(
    'title' => t('More (i.e. [more])'),
    'cache' => TRUE,
    'description' => t('Add more/less script on your content. (i.e. [more])'),
    'process callback' => '_package_tour_filter_insert_txt',
    'tips callback' => '_package_tour_filter_tips',
  );
  return $filters;
}

/**
 * Callback to connect to the main user hook to display a slideshow.
 */
function _package_tour_filter_insert_txt(
  $text, $filter, $format, $langcode, $cache, $cache_id
) {
  include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'package_tour') .
      '/package_tour.inc';
  return _package_tour_process_text($text);
}

/**
 * Implements hook_menu().
 */
function package_tour_menu() {
  $items = array();

  $items['admin/config/content/package_tour'] = array(
    'title'             => 'Package Tour Management',
    'description'       => 'Thai Happy Tour and Travel : Package tour management',
    'page callback'     => 'package_tour_admin_view',
    'page arguments'    => '',
    'access arguments'  => array('administer package_tour'),
    'file'              => 'package_tour.admin.inc',
    'type'              => MENU_NORMAL_ITEM,
  );
  
  $items['admin/config/content/package_tour/category'] = array(
    'title'             => 'Tour Category Management (Slide show)',
    'description'       => 'Setting package tour',
    'page callback'     => 'package_tour_admin_category_view',
    'page arguments'    => '',
    'access arguments'  => array('administer package_tour'),
    'file'              => 'package_tour.admin.inc',
    'type'              => MENU_NORMAL_ITEM,
  );

  $items['admin/config/content/package_tour/category/add'] = array(
    'title' => 'Add a new tour',
    'description' => 'Add a new tour',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_package_tour_admin_category_add_form', 'add'),
    'file' => 'package_tour.admin.inc',
    'access arguments' => array('administer package_tour'),
    'type' => MENU_CALLBACK,
  );
  
  $items['admin/config/content/package_tour/category/edit/%'] = array(
    'title' => 'Edit tour',
    'description' => 'Configure the package tour',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_package_tour_admin_category_edit_form', 6),
    'file' => 'package_tour.admin.inc',
    'access arguments' => array('administer package_tour'),
    'type' => MENU_CALLBACK,
  );
  
  $items['admin/config/content/package_tour/category/delete/%/%'] = array(
    'title' => 'Delete tour',
    'description' => 'Configure the package tour',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_package_tour_admin_category_delete_form', 6, 7),
    'file' => 'package_tour.admin.inc',
    'access arguments' => array('administer package_tour'),
    'type' => MENU_CALLBACK,
  );
  
  $items['admin/config/content/package_tour/tour'] = array(
    'title'             => 'Package Tour Management',
    'description'       => 'Setting package tour',
    'page callback'     => 'package_tour_admin_tour_view',
    'page arguments'    => '',
    'access arguments'  => array('administer package_tour'),
    'file'              => 'package_tour.admin.inc',
    'type'              => MENU_NORMAL_ITEM,
  );

  $items['admin/config/content/package_tour/tour/add'] = array(
    'title' => 'Add a new tour',
    'description' => 'Add a new tour',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_package_tour_admin_add_form', 'add'),
    'file' => 'package_tour.admin.inc',
    'access arguments' => array('administer package_tour'),
    'type' => MENU_CALLBACK,
  );
  
  $items['admin/config/content/package_tour/tour/edit/%'] = array(
    'title' => 'Edit tour',
    'description' => 'Configure the package tour',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_package_tour_admin_edit_form', 6),
    'file' => 'package_tour.admin.inc',
    'access arguments' => array('administer package_tour'),
    'type' => MENU_CALLBACK,
  );
  
  $items['admin/config/content/package_tour/tour/delete/%/%'] = array(
    'title' => 'Delete tour',
    'description' => 'Configure the package tour',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_package_tour_admin_delete_form', 6, 7),
    'file' => 'package_tour.admin.inc',
    'access arguments' => array('administer package_tour'),
    'type' => MENU_CALLBACK,
  );
  
  $items['admin/config/content/package_tour/tour/program/%'] = array(
    'title' => 'Program tour',
    'description' => 'Configure the package tour',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_package_tour_admin_program_list', 6),
    'file' => 'package_tour.admin.inc',
    'access arguments' => array('administer package_tour'),
    'type' => MENU_CALLBACK,
  );
  
  $items['admin/config/content/package_tour/tour/program/%/add'] = array(
    'title' => 'Program tour',
    'description' => 'Configure the package tour',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_package_tour_admin_program_form', 6),
    'file' => 'package_tour.admin.inc',
    'access arguments' => array('administer package_tour'),
    'type' => MENU_CALLBACK,
  );
  
  $items['admin/config/content/package_tour/tour/program/edit/%'] = array(
    'title' => 'Program tour',
    'description' => 'Configure the package tour',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_package_tour_admin_program_edit_form', 7),
    'file' => 'package_tour.admin.inc',
    'access arguments' => array('administer package_tour'),
    'type' => MENU_CALLBACK,
  );
  
  $items['admin/config/content/package_tour/tour/program/%/delete'] = array(
    'title' => 'Program tour',
    'description' => 'Configure the package tour',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_package_tour_admin_program_delete', 6),
    'file' => 'package_tour.admin.inc',
    'access arguments' => array('administer package_tour'),
    'type' => MENU_CALLBACK,
  );
  
  $items['tour/%'] = array(
    'title' => 'Tour',
    'page callback' => '_package_tour_list_item',
    'page arguments' => array(1),
    'file' => 'package_tour.inc',
    'access callback' => TRUE,
  );
  
  
  $items['admin/config/content/package_tour/transaction'] = array(
    'title' => 'Tour Transaction Order',
    'description' => 'View package tour transaction order by customer',
    'page callback'     => 'package_tour_admin_transaction',
    'page arguments'    => '',
    'access arguments'  => array('administer package_tour'),
    'type'              => MENU_NORMAL_ITEM,
    'file' => 'package_tour.admin.inc',
  );
  
  $items['admin/config/content/package_tour/transaction/detail/%'] = array(
    'title' => 'Tour Transaction Order Detail',
    'description' => 'View package tour transaction order by customer',
    'page callback'     =>  'package_tour_admin_transaction_detail',
    'page arguments'    => array(6),
    'access arguments'  => array('administer package_tour'),
    'type'              => MENU_NORMAL_ITEM,
    'file' => 'package_tour.admin.inc',
  );
  
  $items['admin/config/content/package_tour/transaction/detail/%/pdf'] = array(
    'title' => 'Tour Transaction Order Detail',
    'description' => 'View package tour transaction order by customer',
    'page callback'     =>  'package_tour_admin_transaction_detail_pdf',
    'page arguments'    => array(6),
    'access arguments'  => array('administer package_tour'),
    'type'              => MENU_NORMAL_ITEM,
    'file' => 'package_tour.admin.inc',
  );
  
  

  return $items;
}


function package_tour_admin_view() {
	$output = "";
	
  $result = db_select('thtt_m_tour','t')
    ->fields('t')
    ->execute();
  
  $header = array(t('Mangement Menu'), t('Operations'));
   $rows[] = array(
      array('data' => t('Tour Transaction Order'), 'valign' => 'top'),
      array(
        'data' =>
        l(t('View order'), 'admin/config/content/package_tour/transaction'),
        'valign' => 'top'
      )
    );
    
  $header = array(t('Mangement Menu'), t('Operations'));
   $rows[] = array(
      array('data' => t('Tour management'), 'valign' => 'top'),
      array(
        'data' =>
        l(t('List'), 'admin/config/content/package_tour/tour') . ' | '.
        l(t('Add new tour'), 'admin/config/content/package_tour/tour/add') ,
        'valign' => 'top'
      )
    );
    
    $rows[] = array(
      array('data' => t('Tour category management (Slide show)'), 'valign' => 'top'),
      array(
        'data' =>
        l(t('List'), 'admin/config/content/package_tour/category') . ' | '.
        l(t('Add new category'), 'admin/config/content/package_tour/category/add') ,
        'valign' => 'top'
      )
    );
  
  
  $output .= theme('table', array("header" => $header, "rows" => $rows));
  return $output;
}
  
function package_tour_admin_tour_view($ghi = 0, $jkl = '') {
	$output = "";
	
  $query = db_select('thtt_m_tour','t');
  $query->join('thtt_m_tour_category', 'u', 't.tour_category_id = u.tour_category_id');
  $result = $query
		  	->fields('t')
  			->fields('u', array('tour_category_name'))
  			->orderBy('t.tour_id')
		  	->execute();
  
  $output .= l(t('Add new tour'), 'admin/config/content/package_tour/tour/add');
  
  $header = array(t('Tour ID'), t('Category'), t('Tour name'),  t('Operations'));
  foreach($result as $item) { 
	
   $rows[] = array(
      array('data' => $item->tour_id, 'valign' => 'top'),
      array('data' => $item->tour_category_name, 'valign' => 'top'),
      array('data' => $item->tour_name, 'valign' => 'top'),
      array(
        'data' =>
        l(t('Programs'), 'admin/config/content/package_tour/tour/program/' . urlencode($item->tour_id)) . ' | ' . 
        l(t('edit'), 'admin/config/content/package_tour/tour/edit/' . urlencode($item->tour_id)) . ' | ' .
        l(t('delete'), 'admin/config/content/package_tour/tour/delete/' . urlencode($item->tour_id).'/'.urlencode($item->tour_name)),
        'valign' => 'top'
      )
    );
  }
  
  $output .= theme('table', array("header" => $header, "rows" => $rows));
  return $output;
}
  
function package_tour_admin_category_view($ghi = 0, $jkl = '') {
  $output = "";
  $result = db_select('thtt_m_tour_category','t')
		  	->fields('t')
		  	->execute();
  
  $output .= l(t('Add new category'), 'admin/config/content/package_tour/category/add');
  
  $header = array(t('Tour Category ID'), t('Name'), t('Display order'), t('Slideshow [Y=Yes | N=No]'), t('Operations'));
  foreach($result as $item) { 
  
   $rows[] = array(
      array('data' => $item->tour_category_id, 'valign' => 'top'),
      array('data' => $item->tour_category_name, 'valign' => 'top'),
      array('data' => $item->display_order, 'valign' => 'top'),
      array('data' => $item->slideshow_flg, 'valign' => 'top'),
      array(
        'data' =>
        l(t('edit'), 'admin/config/content/package_tour/category/edit/' . urlencode($item->tour_category_id)) . ' ' .
        l(t('delete'), 'admin/config/content/package_tour/category/delete/' . urlencode($item->tour_category_id).'/'.urlencode($item->tour_category_name)),
        'valign' => 'top'
      )
    );
  }
  
  $output .= theme('table', array("header" => $header, "rows" => $rows));
  return $output;
}
