<?php

// init
add_action( 'admin_init', 'aitDirCreateRoles');

// admin customization
add_filter( 'pre_get_posts', 'aitDirShowOnlyMyItems');
add_filter( 'views_edit-ait-dir-item', 'aitDirItemsTableViews');
add_filter( 'admin_notices', 'aitDirShowNotices' );
add_action( 'add_meta_boxes', 'aitDirItemDetailedCapabilities');
add_action( 'init', 'aitDirItemRemoveFeatures');
add_action( 'pre_get_posts', 'aitDirShowOnlyOwnAttachments');
add_filter( 'add_menu_classes', 'aitDirRemoveMediaAdminButton');
add_filter( 'manage_users_columns', 'aitDirUsersShowDetails');
add_filter( 'manage_users_custom_column', 'aitDirShowDetailsData', 10, 3);

// tour operator functions
add_action( 'pre_get_posts', 'aitTourOffersRestrictions');
add_filter( 'views_edit-ait-dir-item-offer', 'aitTourOffersTableViews');

add_action( 'pre_get_posts', 'aitTourReservationsRestrictions');
add_filter( 'views_edit-ait-tour-reservation', 'aitTourReservationsTableViews');

// registrations functionality
require_once dirname(__FILE__) . '/class-paypal.php';
require_once dirname(__FILE__) . '/accounts-reg.php';

function aitDirCreateRoles() {

	global $current_user, $aitThemeOptions;

	// directory items capability
	$capabilityType = 'ait-dir-item';
	$capabilityTypeOffer = 'ait-dir-item-offer';
	$capabilityTypeRes = 'ait-tour-reservation';
	$capabilitiesAdmin = array(
		"edit_{$capabilityType}" => true,
		"read_{$capabilityType}" => true,
		"delete_{$capabilityType}" => true,
		"edit_{$capabilityType}s" => true,
		"edit_others_{$capabilityType}s" => true,
		"publish_{$capabilityType}s" => true,
		"read_private_{$capabilityType}s" => true,
		"delete_{$capabilityType}s" => true,
		"delete_private_{$capabilityType}s" => true,
		"delete_published_{$capabilityType}s" => true,
		"delete_others_{$capabilityType}s" => true,
		"edit_private_{$capabilityType}s" => true,
		"edit_published_{$capabilityType}s" => true,
		// item offers
		"edit_{$capabilityTypeOffer}" => true,
		"read_{$capabilityTypeOffer}" => true,
		"delete_{$capabilityTypeOffer}" => true,
		"edit_{$capabilityTypeOffer}s" => true,
		"edit_others_{$capabilityTypeOffer}s" => true,
		"publish_{$capabilityTypeOffer}s" => true,
		"read_private_{$capabilityTypeOffer}s" => true,
		"delete_{$capabilityTypeOffer}s" => true,
		"delete_private_{$capabilityTypeOffer}s" => true,
		"delete_published_{$capabilityTypeOffer}s" => true,
		"delete_others_{$capabilityTypeOffer}s" => true,
		"edit_private_{$capabilityTypeOffer}s" => true,
		"edit_published_{$capabilityTypeOffer}s" => true,
		// reservations
		"edit_{$capabilityTypeRes}" => true,
		"read_{$capabilityTypeRes}" => true,
		"delete_{$capabilityTypeRes}" => true,
		"edit_{$capabilityTypeRes}s" => true,
		"edit_others_{$capabilityTypeRes}s" => true,
		"publish_{$capabilityTypeRes}s" => true,
		"read_private_{$capabilityTypeRes}s" => true,
		"delete_{$capabilityTypeRes}s" => true,
		"delete_private_{$capabilityTypeRes}s" => true,
		"delete_published_{$capabilityTypeRes}s" => true,
		"delete_others_{$capabilityTypeRes}s" => true,
		"edit_private_{$capabilityTypeRes}s" => true,
		"edit_published_{$capabilityTypeRes}s" => true,
		// categories
		"assign_dir_category" => true,
		"assign_dir_location" => true,
		"assign_dir_amenities" => true
	);

	// set admin capability
	$adminRole = get_role( 'administrator' );
	foreach ($capabilitiesAdmin as $key => $value) {
		$adminRole->add_cap( $key );
	}

	$subscriberRole = get_role( 'subscriber' );
	$subscriberRole->add_cap( 'directory_account_update' );

	// update user roles from admin
	if(isset($_POST['action']) && $_POST['action'] == 'update' && strpos($_SERVER['PHP_SELF'],'options.php') !== false){
		$optionName = $_POST['option_page'];
		if(isset($_POST[$optionName]) && isset($_POST[$optionName]['members'])){

			$prefixName = '';
			$options = $_POST[$optionName]['members'];
			$roles = $GLOBALS['wp_roles']->role_names;

			$capabilitiesDirectory = array(
				"edit_{$capabilityType}s" => true,
				"read_private_{$capabilityType}s" => false,
				"edit_published_{$capabilityType}s" => true,
				"delete_{$capabilityType}s" => true,
				"delete_published_{$capabilityType}s" => true,
				// item offers
				"edit_{$capabilityTypeOffer}s" => true,
				"read_private_{$capabilityTypeOffer}s" => false,
				"edit_published_{$capabilityTypeOffer}s" => true,
				"delete_{$capabilityTypeOffer}s" => true,
				"delete_published_{$capabilityTypeOffer}s" => true,
				"publish_{$capabilityTypeOffer}s" => true,
				// reservations
				"edit_{$capabilityTypeRes}s" => true,
				"read_private_{$capabilityTypeRes}s" => false,
				"edit_published_{$capabilityTypeRes}s" => true,
				"delete_{$capabilityTypeRes}s" => true,
				"delete_published_{$capabilityTypeRes}s" => true,
				"publish_{$capabilityTypeRes}s" => true,
				// categories
				"assign_dir_category" => true,
				"assign_dir_location" => true,
				"assign_dir_amenities" => true,
				// others capabilities
				"read" => true,
				"upload_files" => true,
				"directory_account_update" => true
			);

			if(isset($options['role1Enable'])){
				remove_role( 'directory_1' );
				$caps = $capabilitiesDirectory;
				if(!isset($options['role1Approve'])){
					$caps["publish_{$capabilityType}s"] = true;
				}
				add_role( 'directory_1', $prefixName . $options['role1Name'], $caps);
			} else {
				remove_role( 'directory_1' );
			}

			if(isset($options['role2Enable'])){
				remove_role( 'directory_2' );
				$caps = $capabilitiesDirectory;
				if(!isset($options['role2Approve'])){
					$caps["publish_{$capabilityType}s"] = true;
				}
				add_role( 'directory_2', $prefixName . $options['role2Name'], $caps);
			} else {
				remove_role( 'directory_2' );
			}

			if(isset($options['role3Enable'])){
				remove_role( 'directory_3' );
				$caps = $capabilitiesDirectory;
				if(!isset($options['role3Approve'])){
					$caps["publish_{$capabilityType}s"] = true;
				}
				add_role( 'directory_3', $prefixName . $options['role3Name'], $caps);
			} else {
				remove_role( 'directory_3' );
			}

			if(isset($options['role4Enable'])){
				remove_role( 'directory_4' );
				$caps = $capabilitiesDirectory;
				if(!isset($options['role4Approve'])){
					$caps["publish_{$capabilityType}s"] = true;
				}
				add_role( 'directory_4', $prefixName . $options['role4Name'], $caps);
			} else {
				remove_role( 'directory_4' );
			}

			if(isset($options['role5Enable'])){
				remove_role( 'directory_5' );
				$caps = $capabilitiesDirectory;
				if(!isset($options['role5Approve'])){
					$caps["publish_{$capabilityType}s"] = true;
				}
				add_role( 'directory_5', $prefixName . $options['role5Name'], $caps);
			} else {
				remove_role( 'directory_5' );
			}

		}
	}

	// check number of posts for directory users
	$usrRoles = $current_user->roles;
	if (isDirectoryUser()) {
		if((strpos($_SERVER['PHP_SELF'],'post-new.php') !== false) && isset($_GET['post_type']) && ($_GET['post_type'] == 'ait-dir-item')){

			$params = array(
				'post_type'			=> 'ait-dir-item',
				'nopaging'			=> true,
				'author'			=> $current_user->ID
			);
			$itemsQuery = new WP_Query();
			$items = $itemsQuery->query($params);

			$backUrl = admin_url('edit.php?post_type=ait-dir-item&dir_notification=1');

			if(in_array('directory_1', $usrRoles)){
				if(count($items) >= intval($aitThemeOptions->members->role1Items)){
					header('Location: ' . $backUrl);
				}
			} elseif (in_array('directory_2', $usrRoles)) {
				if(count($items) >= intval($aitThemeOptions->members->role2Items)){
					header('Location: ' . $backUrl);
				}
			} elseif (in_array('directory_3', $usrRoles)) {
				if(count($items) >= intval($aitThemeOptions->members->role3Items)){
					header('Location: ' . $backUrl);
				}
			} elseif (in_array('directory_4', $usrRoles)) {
				if(count($items) >= intval($aitThemeOptions->members->role4Items)){
					header('Location: ' . $backUrl);
				}
			} elseif (in_array('directory_5', $usrRoles)) {
				if(count($items) >= intval($aitThemeOptions->members->role5Items)){
					header('Location: ' . $backUrl);
				}
			}
		}
	}
}

/**
 * Don't show others items for directory roles
 */
function aitDirShowOnlyMyItems($query) {
	global $current_screen, $current_user;
	if (isDirectoryUser()) {
		if( isset($current_screen) && ($current_screen->base == 'edit') && ($current_screen->post_type == 'ait-dir-item') ){
			$query->set('author',$current_user->ID);
		}
	}
	return $query;
}

/**
 * Correct number of items for directory users
 */
function aitDirItemsTableViews($views) {
	if (isDirectoryUser()) {
		global $wpdb;

		$user = wp_get_current_user();
		$type = 'ait-dir-item';

		//$cache_key = $type;

		$query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s AND post_author = %d";
		$query .= ' GROUP BY post_status';

		//$count = wp_cache_get($cache_key, 'counts');
		//if ( false !== $count )
		//	return $count;

		$count = $wpdb->get_results( $wpdb->prepare( $query, $type, $user->ID ), ARRAY_A );

		$stats = array();
		foreach ( get_post_stati() as $state )
			$stats[$state] = 0;

		foreach ( (array) $count as $row )
			$stats[$row['post_status']] = $row['num_posts'];

		$stats = (object) $stats;
		//wp_cache_set($cache_key, $stats, 'counts');

		global $locked_post_status, $avail_post_stati;

		$post_type = $type;

		if ( !empty($locked_post_status) )
			return array();

		$status_links = array();
		$num_posts = $stats;
		$class = '';
		$allposts = '';

		$current_user_id = get_current_user_id();

		$total_posts = array_sum( (array) $num_posts );

		// Subtract post types that are not included in the admin all list.
		foreach ( get_post_stati( array('show_in_admin_all_list' => false) ) as $state )
			$total_posts -= $num_posts->$state;

		$class = empty( $class ) && empty( $_REQUEST['post_status'] ) && empty( $_REQUEST['show_sticky'] ) ? ' class="current"' : '';
		$status_links['all'] = "<a href='edit.php?post_type=$post_type{$allposts}'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_posts, 'posts' , 'ait'), number_format_i18n( $total_posts ) ) . '</a>';

		foreach ( get_post_stati(array('show_in_admin_status_list' => true), 'objects') as $status ) {
			$class = '';

			$status_name = $status->name;

			if ( !in_array( $status_name, $avail_post_stati ) )
				continue;

			if ( empty( $num_posts->$status_name ) )
				continue;

			if ( isset($_REQUEST['post_status']) && $status_name == $_REQUEST['post_status'] )
				$class = ' class="current"';

			$status_links[$status_name] = "<a href='edit.php?post_status=$status_name&amp;post_type=$post_type'$class>" . sprintf( translate_nooped_plural( $status->label_count, $num_posts->$status_name ), number_format_i18n( $num_posts->$status_name ) ) . '</a>';
		}
		return $status_links;
	} else {
		return $views;
	}
}

/**
 * Show error notice if max items was exceeded or registration messages
 */
function aitDirShowNotices() {
	global $registerMessages;

	if(isset($_GET['dir_notification']) && $_GET['dir_notification'] == '1'){
		echo '<div class="error"><p>'.__('Sorry but you have maximum items for your account type. Upgrade your account to insert new items!','ait').'</p></div>';
	}
	if(isset($registerMessages)){
		echo '<div class="updated"><p>'.$registerMessages.'</p></div>';
	}
}

function aitDirItemDetailedCapabilities($data) {
	
	global $aitThemeOptions, $wp_meta_boxes, $current_user;
	$usrRoles = $current_user->roles;

	// remove featured options for non administrators
	if (isset($wp_meta_boxes['ait-dir-item']) && (!in_array('administrator', $usrRoles))) {
		foreach ($wp_meta_boxes['ait-dir-item'] as $contextName => $context) {
			foreach ($context as $boxesName => $boxes) {
				foreach ($boxes as $boxName => $box) {
					if (isset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['featured'])) {
						unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['featured']);
					}
				}
			}
		}
	}

	if (isDirectoryUser()) {
		$usrRoles = $current_user->roles;
		$roleNumber = substr(array_shift(array_values($usrRoles)), 10);

		$nameAddress = 'role'.$roleNumber.'Address';
		$nameStreetview = 'role'.$roleNumber.'Streetview';
		$nameTelephone = 'role'.$roleNumber.'Telephone';
		$nameEmail = 'role'.$roleNumber.'Email';
		$nameWeb = 'role'.$roleNumber.'Web';
		$nameHours = 'role'.$roleNumber.'Hours';
		$nameAlternativeContent = 'role'.$roleNumber.'AlternativeContent';
		$nameGallery = 'role'.$roleNumber.'Gallery';
		$nameTabs = 'role'.$roleNumber.'Tabs';

		if(isset($wp_meta_boxes['ait-dir-item'])){
			foreach ($wp_meta_boxes['ait-dir-item'] as $contextName => $context) {
				foreach ($context as $boxesName => $boxes) {
					foreach ($boxes as $boxName => $box) {
						// item options
						if($boxName == '_ait-dir-item_metabox'){
							//unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['address']);
							if(!isset($aitThemeOptions->members->$nameAddress)){
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['address']);
							}
							if(!isset($aitThemeOptions->members->$nameStreetview)){
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['showStreetview']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['streetViewHeading']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['streetViewLatitude']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['streetViewLongitude']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['streetViewPitch']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['streetViewZoom']);
							}
							if(!isset($aitThemeOptions->members->$nameTelephone)){
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['telephone']);
							}
							if(!isset($aitThemeOptions->members->$nameEmail)){
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['email']);
							}
							if(!isset($aitThemeOptions->members->$nameWeb)){
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['web']);
							}
							if(!isset($aitThemeOptions->members->$nameHours)){
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['Opening Hours']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['hoursMonday']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['hoursTuesday']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['hoursWednesday']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['hoursThursday']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['hoursFriday']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['hoursSaturday']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['hoursSunday']);
							}
							if(!isset($aitThemeOptions->members->$nameAlternativeContent)){
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['Alternative Content']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['alternativeContent']);
							}
							if(!isset($aitThemeOptions->members->$nameGallery)){
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['Gallery']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['galleryEnable']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['gallery1']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['gallery2']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['gallery3']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['gallery4']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['gallery5']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['gallery6']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['gallery7']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['gallery8']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['gallery9']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['gallery10']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['gallery11']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['gallery12']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['gallery13']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['gallery14']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['gallery15']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['gallery16']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['gallery17']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['gallery18']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['gallery19']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['gallery20']);
							}
							if(!isset($aitThemeOptions->members->$nameTabs)){
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['1. Tab']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['tab1']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['tab1Title']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['tab1Content']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['2. Tab']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['tab2']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['tab2Title']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['tab2Content']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['3. Tab']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['tab3']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['tab3Title']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['tab3Content']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['4. Tab']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['tab4']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['tab4Title']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['tab4Content']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['5. Tab']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['tab5']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['tab5Title']);
								unset($wp_meta_boxes['ait-dir-item'][$contextName][$boxesName][$boxName]['callback'][0]->configData['tab5Content']);
							}
						}
					}
				}
			}
		}
	}
}

function aitDirItemRemoveFeatures() {
	global $aitThemeOptions, $current_user;

	if (isDirectoryUser()) {
		$usrRoles = $current_user->roles;
		$roleNumber = substr(array_shift(array_values($usrRoles)), 10);

		$nameEditor = 'role'.$roleNumber.'Content';
		$nameImage = 'role'.$roleNumber.'Image';
		$nameExcerpt = 'role'.$roleNumber.'Excerpt';
		$nameReviews = 'role'.$roleNumber.'Reviews';

		// attributes
		remove_post_type_support( 'ait-dir-item', 'page-attributes' );
		// editor - content
		if(!isset($aitThemeOptions->members->$nameEditor)){
			remove_post_type_support( 'ait-dir-item', 'editor' );
		}
		// image - thumbnail
		if(!isset($aitThemeOptions->members->$nameImage)){
			remove_post_type_support( 'ait-dir-item', 'thumbnail' );
		}
		// excerpt
		if(!isset($aitThemeOptions->members->$nameExcerpt)){
			remove_post_type_support( 'ait-dir-item', 'excerpt' );
		}
		// reviews - comments
		if(!isset($aitThemeOptions->members->$nameReviews)){
			remove_post_type_support( 'ait-dir-item', 'comments' );
		}

	}
}

function aitDirShowOnlyOwnAttachments( $query ) {
	global $current_user, $pagenow;
	if (isDirectoryUser()) {
		if( 'upload.php' == $pagenow || 'admin-ajax.php' == $pagenow || 'media-upload.php' == $pagenow) {
			$query->set('author', $current_user->ID );
		}
	}
	return $query;
}

function aitDirRemoveMediaAdminButton($menu) {
	global $current_user;
	if (isDirectoryUser()) {
		foreach ($menu as $key => $item) {
			if($item[1] == 'upload_files'){
				unset($menu[$key]);
			}
		}
	}
	return $menu;
}

function aitDirUsersShowDetails($columns) {
	global $aitThemeOptions;
	$columns['items'] = __('Items','ait');
	if (isset($aitThemeOptions->members->paypalPaymentType) && ($aitThemeOptions->members->paypalPaymentType == 'recurring')) {
		$columns['recurring_profile_id'] = __('PayPal recurring payments profile ID','ait');
	} else {
		$columns['activation_time'] = __('Activation time','ait');
		$columns['days_before_expiration'] = __('Days left before expiration','ait');
		$columns['transaction_id'] = __('Last PayPal transaction ID','ait');
	}
	return $columns;
}

function aitDirShowDetailsData($empty='', $column_name, $id) {
	if( $column_name == 'items' ) {
		return getAuthorItemsCount($id);
	}
	if( $column_name == 'activation_time' ) {
		$data = get_user_meta( $id, 'dir_activation_time', true );
		if ($data) {
			$dateFormat = get_option( 'date_format', 'm/d/Y' );
			return date($dateFormat,$data['time']);
		}
	}
	if( $column_name == 'days_before_expiration' ) {
		$user = new WP_User($id);
		if (isDirectoryUser($user)) {
			return aitDirGetDaysLeft($user->ID);
		} else {
			return '';
		}
	}
	if( $column_name == 'transaction_id' ) {
		$data = get_user_meta( $id, 'dir_paypal_transaction_id', true );
		if ($data) {
			return $data;
		}
	}
	if( $column_name == 'recurring_profile_id' ) {
		$data = get_user_meta( $id, 'dir_paypal_recurring_profile_id', true );
		if ($data) {
			return $data;
		}
	}
}

function getAuthorItemsCount($id) {
	global $wpdb;

	$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'ait-dir-item' AND post_status = 'publish' AND post_author = ".$id );
	return $count;
}

// ==================================================
// TourOperator functions
// --------------------------------------------------

function aitTourOffersRestrictions( $query ) {
	global $current_screen, $current_user;
	if (isDirectoryUser()) {
		// show only my items
		if( isset($current_screen) && ($current_screen->base == 'post') && ($current_screen->post_type == 'ait-dir-item-offer') && isset($query->query['post_type']) && ($query->query['post_type'] == 'ait-dir-item') ) {
			$query->set('author', $current_user->ID );
		}
		// offers table show only my offers
		if( isset($current_screen) && ($current_screen->base == 'edit') && ($current_screen->post_type == 'ait-dir-item-offer') ){
			$query->set('author',$current_user->ID);
		}
	}
	return $query;
}

function aitTourOffersTableViews($views) {
	if (isDirectoryUser()) {
		global $wpdb;

		$user = wp_get_current_user();
		$type = 'ait-dir-item-offer';

		//$cache_key = $type;

		$query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s AND post_author = %d";
		$query .= ' GROUP BY post_status';

		//$count = wp_cache_get($cache_key, 'counts');
		//if ( false !== $count )
		//	return $count;

		$count = $wpdb->get_results( $wpdb->prepare( $query, $type, $user->ID ), ARRAY_A );

		$stats = array();
		foreach ( get_post_stati() as $state )
			$stats[$state] = 0;

		foreach ( (array) $count as $row )
			$stats[$row['post_status']] = $row['num_posts'];

		$stats = (object) $stats;
		//wp_cache_set($cache_key, $stats, 'counts');

		global $locked_post_status, $avail_post_stati;

		$post_type = $type;

		if ( !empty($locked_post_status) )
			return array();

		$status_links = array();
		$num_posts = $stats;
		$class = '';
		$allposts = '';

		$current_user_id = get_current_user_id();

		$total_posts = array_sum( (array) $num_posts );

		// Subtract post types that are not included in the admin all list.
		foreach ( get_post_stati( array('show_in_admin_all_list' => false) ) as $state )
			$total_posts -= $num_posts->$state;

		$class = empty( $class ) && empty( $_REQUEST['post_status'] ) && empty( $_REQUEST['show_sticky'] ) ? ' class="current"' : '';
		$status_links['all'] = "<a href='edit.php?post_type=$post_type{$allposts}'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_posts, 'posts' , 'ait'), number_format_i18n( $total_posts ) ) . '</a>';

		foreach ( get_post_stati(array('show_in_admin_status_list' => true), 'objects') as $status ) {
			$class = '';

			$status_name = $status->name;

			if ( !in_array( $status_name, $avail_post_stati ) )
				continue;

			if ( empty( $num_posts->$status_name ) )
				continue;

			if ( isset($_REQUEST['post_status']) && $status_name == $_REQUEST['post_status'] )
				$class = ' class="current"';

			$status_links[$status_name] = "<a href='edit.php?post_status=$status_name&amp;post_type=$post_type'$class>" . sprintf( translate_nooped_plural( $status->label_count, $num_posts->$status_name ), number_format_i18n( $num_posts->$status_name ) ) . '</a>';
		}
		return $status_links;
	} else {
		return $views;
	}
}

function aitTourReservationsRestrictions( $query ) {
	global $current_screen, $current_user;
	if (isDirectoryUser()) {
		// reservations table show only my reservations
		if( isset($current_screen) && ($current_screen->base == 'edit') && ($current_screen->post_type == 'ait-tour-reservation') ){
			$query->set('author',$current_user->ID);
		}
	}
	return $query;
}

function aitTourReservationsTableViews($views) {
	if (isDirectoryUser()) {
		global $wpdb;

		$user = wp_get_current_user();
		$type = 'ait-tour-reservation';

		//$cache_key = $type;

		$query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s AND post_author = %d";
		$query .= ' GROUP BY post_status';

		//$count = wp_cache_get($cache_key, 'counts');
		//if ( false !== $count )
		//	return $count;

		$count = $wpdb->get_results( $wpdb->prepare( $query, $type, $user->ID ), ARRAY_A );

		$stats = array();
		foreach ( get_post_stati() as $state )
			$stats[$state] = 0;

		foreach ( (array) $count as $row )
			$stats[$row['post_status']] = $row['num_posts'];

		$stats = (object) $stats;
		//wp_cache_set($cache_key, $stats, 'counts');

		global $locked_post_status, $avail_post_stati;

		$post_type = $type;

		if ( !empty($locked_post_status) )
			return array();

		$status_links = array();
		$num_posts = $stats;
		$class = '';
		$allposts = '';

		$current_user_id = get_current_user_id();

		$total_posts = array_sum( (array) $num_posts );

		// Subtract post types that are not included in the admin all list.
		foreach ( get_post_stati( array('show_in_admin_all_list' => false) ) as $state )
			$total_posts -= $num_posts->$state;

		$class = empty( $class ) && empty( $_REQUEST['post_status'] ) && empty( $_REQUEST['show_sticky'] ) ? ' class="current"' : '';
		$status_links['all'] = "<a href='edit.php?post_type=$post_type{$allposts}'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_posts, 'posts' , 'ait'), number_format_i18n( $total_posts ) ) . '</a>';

		foreach ( get_post_stati(array('show_in_admin_status_list' => true), 'objects') as $status ) {
			$class = '';

			$status_name = $status->name;

			if ( !in_array( $status_name, $avail_post_stati ) )
				continue;

			if ( empty( $num_posts->$status_name ) )
				continue;

			if ( isset($_REQUEST['post_status']) && $status_name == $_REQUEST['post_status'] )
				$class = ' class="current"';

			$status_links[$status_name] = "<a href='edit.php?post_status=$status_name&amp;post_type=$post_type'$class>" . sprintf( translate_nooped_plural( $status->label_count, $num_posts->$status_name ), number_format_i18n( $num_posts->$status_name ) ) . '</a>';
		}
		return $status_links;
	} else {
		return $views;
	}
}