function wpv_admin_archive_listing_name( $views_pre_query_data, $current_post_status ) {

	global $WP_Views, $WPV_settings, $WPV_view_archive_loop;

	// array of URL modifiers
	$mod_url = array(
		'orderby' => '',
		'order' => '',
		's' => '',
		'items_per_page' => '',
		'paged' => '',
		'status' => $current_post_status
	);
	
	// array of WP_Query parameters
	$wpv_args = array(
		'post_type' => 'view',
		'post__in' => $views_pre_query_data[ 'post__in' ],
		'posts_per_page' => WPV_ITEMS_PER_PAGE,
		'order' => 'ASC',
		'orderby' => 'title',
		'post_status' => $current_post_status
	);

	
	$search_string = wpv_getget( 's', '' );
	$is_search = ( '' != $search_string );
	if ( $is_search ) {
		// perform the search in WPA titles and decriptions and add post__in argument to $wpv_args.
		$wpv_args = wpv_modify_wpquery_for_search( $search_string, $wpv_args );
		
		$mod_url['s'] = sanitize_text_field( $search_string );
	}
	$search_term = urldecode( sanitize_text_field( $search_string ) );

	$items_per_page = (int) wpv_getget( 'items_per_page', 0 ); // 0 means "not set"
	if ( 
		$items_per_page > 0 
		|| $items_per_page == -1
	) {
		$wpv_args['posts_per_page'] = $items_per_page;
		$mod_url['items_per_page'] = $items_per_page;
	}

	$orderby = sanitize_text_field( wpv_getget( 'orderby' ) );
	$order = sanitize_text_field( wpv_getget( 'order' ) );
	if ( '' != $orderby ) {
		$wpv_args['orderby'] = $orderby;
		$mod_url['orderby'] = $orderby;
		if ( '' != $order ) {
			$wpv_args['order'] = $order;
			$mod_url['order'] = $order;
		}
	}

	$paged = (int) wpv_getget( 'paged', 0 );
	if ( $paged > 0 ) {
		$wpv_args['paged'] = $paged;
		$mod_url['paged'] = $paged;
	}

	$wpv_query = new WP_Query( $wpv_args );

	// The number of WPAs being displayed.
	$wpv_count_posts = $wpv_query->post_count;

	// Total number of WPAs matching query parameters.
	$wpv_found_posts = $wpv_query->found_posts;

	?>
	
	<!-- links to lists WPA in different statuses -->
	<ul class="subsubsub">
		<li>
			<?php
				// Show link to published WPA templates.

				// We show this link as current only if there is no search.
				$is_plain_publish_current_status = ( 'publish' == $current_post_status && ! $is_search );

				printf(
						'<a href="%s" %s >%s</a> (%s) |',
						esc_url( add_query_arg( array( 'page' => 'view-archives', 'status' => 'publish' ), admin_url( 'admin.php' ) ) ),
						$is_plain_publish_current_status ? 'class="current"' : '',
						__( 'Published', 'wpv-views' ),
						$views_pre_query_data['published_count'] );

			?>
		</li>
		<li>
			<?php
				// Show link to trashed WPA templates.

				// We show this link as current only if there is no search.
				$is_plain_trash_current_status = ( 'trash' == $current_post_status && ! $is_search );

				printf(
						'<a href="%s" %s >%s</a> (%s)',
						esc_url( add_query_arg( array( 'page' => 'view-archives', 'status' => 'trash' ), admin_url( 'admin.php' ) ) ),
						$is_plain_trash_current_status ? 'class="current"' : '',
						__( 'Trash', 'wpv-views' ),
						$views_pre_query_data['trashed_count'] );
			?>
		</li>
	</ul>
	<div style="clear:both;"></div>
	
	<?php

	if ( !$WPV_view_archive_loop->check_archive_loops_exists() ) {
		?>
			<p id="js-wpv-no-archive" class="toolset-alert toolset-alert-success">
				<?php _e('All loops have a WordPress Archive assigned','wpv-views'); ?>
			</p>
		<?php
	}

	if ( $wpv_count_posts > 0 ) {

		// A nonce for WPA action - used for individual as well as for bulk actions
		$wpa_action_nonce = wp_create_nonce( 'wpv_view_listing_actions_nonce' );

		// === Render "tablenav" section (Bulk actions and Search box) ===
		echo '<div class="tablenav top">';

		// Prepare ender bulk actions dropdown.
		if( 'publish' == $current_post_status ) {
			$bulk_actions = array( 'trash' => __( 'Move to trash', 'wpv-views' ) );
		} else {
			$bulk_actions = array(
				'restore-from-trash' => __( 'Restore from trash', 'wpv-views' ),
				'delete' => __( 'Delete permanently', 'wpv-views' ) );
		}

		$bulk_actions_args = array( 'data-viewactionnonce' => $wpa_action_nonce );
		$bulk_actions_class = 'js-wpv-wpa-listing-bulk-action';
		
		echo wpv_admin_table_bulk_actions( $bulk_actions, $bulk_actions_class, $bulk_actions_args, 'top' );

		// Show search box
		?>
			<div class="alignright">
				<form id="posts-filter" action="" method="get">
					<p class="search-box">
						<label class="screen-reader-text" for="post-search-input"><?php _e('Search WordPress Archives','wpv-views'); ?>:</label>
						<input type="search" id="post-search-input" name="s" value="<?php echo $search_term; ?>" />
						<input type="submit" name="" id="search-submit" class="button"
								value="<?php echo htmlentities( __( 'Search WordPress Archives', 'wpv-views' ), ENT_QUOTES ); ?>" />
						<input type="hidden" name="paged" value="1" />
					</p>
				</form>
			</div>
		<?php

		echo '</div>'; // End of tablenav section

		?>
		<table id="wpv_view_list" class="js-wpv-views-listing wpv-views-listing wpv-views-listing-by-name widefat">
			<thead>
				<?php
					/* To avoid code duplication, table header is stored in output buffer and echoed twice - within
					 * thead and tfoot tags. */
					ob_start();
				?>
				<tr>
					<th class="wpv-admin-listing-col-bulkactions check-column">
						<input type="checkbox" />
					</th>

					<?php
						$column_active = '';
						$column_sort_to = 'ASC';
						$column_sort_now = 'ASC';
						if ( $wpv_args['orderby'] === 'title' ) {
							$column_active = ' views-list-sort-active';
							$column_sort_to = ( $wpv_args['order'] === 'ASC' ) ? 'DESC' : 'ASC';
							$column_sort_now = $wpv_args['order'];
						}
					?>
					<th class="wpv-admin-listing-col-title">
						<?php
							// "sort by title" link
							printf(
									'<a href="%s" class="%s", data-orderby="title">%s <i class="%s"></i></a>',
									wpv_maybe_add_query_arg(
											array(
													'page' => 'view-archives',
													'orderby' => 'title',
													'order' => $column_sort_to,
													's' => $mod_url['s'],
													'items_per_page' => $mod_url['items_per_page'],
													'paged' => $mod_url['paged'],
													'status' => $mod_url['status'] ),
											admin_url( 'admin.php' ) ),
									'js-views-list-sort views-list-sort' . $column_active,
									__( 'Title', 'wpv-views' ),
									( 'DESC' === $column_sort_now ) ? 'icon-sort-by-alphabet-alt' : 'icon-sort-by-alphabet' );
						?>
					</th>
					<th class="wpv-admin-listing-col-usage"><?php _e('Archive usage','wpv-views') ?></th>

					<?php
						$column_active = '';
						$column_sort_to = 'DESC';
						$column_sort_now = 'DESC';
						if ( $wpv_args['orderby'] === 'date' ) {
							$column_active = ' views-list-sort-active';
							$column_sort_to = ( $wpv_args['order'] === 'ASC' ) ? 'DESC' : 'ASC';
							$column_sort_now = $wpv_args['order'];
						}
					?>
					<th class="wpv-admin-listing-col-date">
						<?php
							// "sort by date" link
							printf(
									'<a href="%s" class="%s" data-orderby="date">%s <i class="%s"></i></a>',
									wpv_maybe_add_query_arg(
											array(
													'page' => 'view-archives',
													'orderby' => 'date',
													'order' => $column_sort_to,
													's' => $mod_url['s'],
													'items_per_page' => $mod_url['items_per_page'],
													'paged' => $mod_url['paged'],
													'status' => $mod_url['status'] ),
											admin_url( 'admin.php' ) ),
											'js-views-list-sort views-list-sort ' . $column_active,
											__( 'Date', 'wpv-views' ),
											( 'DESC' === $column_sort_now ) ? 'icon-sort-by-attributes-alt' : 'icon-sort-by-attributes' );
						?>
					</th>
				</tr>
				<?php
					// Get table header from output buffer and stop buffering
					$table_header = ob_get_contents();
					ob_end_clean();

					echo $table_header;
				?>
			</thead>
			<tfoot>
				<?php
					echo $table_header;
				?>
			</tfoot>

			<tbody class="js-wpv-views-listing-body">
				<?php
					$loops = $WPV_view_archive_loop->_get_post_type_loops();
					$builtin_loops = array(
							'home-blog-page' => __('Home/Blog', 'wpv-views'),
							'search-page' => __('Search results', 'wpv-views'),
							'author-page' => __('Author archives', 'wpv-views'),
							'year-page' => __('Year archives', 'wpv-views'),
							'month-page' => __('Month archives', 'wpv-views'),
							'day-page' => __('Day archives', 'wpv-views') );
					$taxonomies = get_taxonomies('', 'objects');
					$exclude_tax_slugs = array();
					$exclude_tax_slugs = apply_filters( 'wpv_admin_exclude_tax_slugs', $exclude_tax_slugs );
					$alternate = '';

					while ( $wpv_query->have_posts() ) :
						$wpv_query->the_post();
						$post_id = get_the_id();
						$post = get_post($post_id);
						$view_settings = $WP_Views->get_view_settings( $post_id );
						$view_description = get_post_meta($post_id, '_wpv_description', true);
						$alternate = ' alternate' == $alternate ? '' : ' alternate';
						?>
						<tr id="wpv_view_list_row_<?php echo $post->ID; ?>" class="js-wpv-view-list-row <?php echo $alternate; ?>" >
							<th class="wpv-admin-listing-col-bulkactions check-column">
								<?php
									printf( '<input type="checkbox" value="%s" name="wpa[]" />', $post->ID );
								?>
							</th>
							<td  class="wpv-admin-listing-col-title">
								<span class="row-title">
									<?php
										if ( 'trash' == $current_post_status ) {
											echo esc_html( trim( $post->post_title ) );
										} else {
											// Title + edit link
											printf(
												'<a href="%s">%s</a>',
												esc_url( add_query_arg(
														array( 'page' => 'view-archives-editor', 'view_id' => $post->ID ),
														admin_url( 'admin.php' ) ) ),
												esc_html( trim( $post->post_title ) ) 
											);
										}
									?>
								</span>
								<?php
									// Show the description if there is any.
									if ( isset( $view_description ) && '' != $view_description ) {
										?>
										<p class="desc">
											<?php echo nl2br( $view_description ); ?>
										</p>
										<?php
									}

									/* Generate and show row actions.
									 * Note that we want to add also 'simple' action names to the action list because
									 * they get echoed as a class of the span tag and get styled by WordPress core css
									 * accordingly (e.g. trash in different colour than the rest) */
									$row_actions = array();

									if ( 'publish' == $current_post_status ) {
										$row_actions['edit'] = sprintf(
												'<a href="%s">%s</a>',
												esc_url( add_query_arg(
														array( 'page' => 'view-archives-editor', 'view_id' => $post->ID ),
														admin_url( 'admin.php' ) ) ),
												__( 'Edit', 'wpv-views' ) );
										/* Note that hash in <a href="#"> is present so the link behaves like a link.
										 * <a href=""> causes problems with colorbox and with mere <a> the mouse cursor
										 * doesn't change when hovering over the link. */
										if ( $view_settings['view-query-mode'] == 'archive' ) {
											$row_actions['change js-list-views-action-change'] = sprintf(
													'<a href="#">%s</a>',
													__( 'Change archive usage', 'wpv-views' ) );
										}
										$row_actions['trash js-list-views-action-trash'] = sprintf(
												'<a href="#">%s</a>',
												__( 'Move to trash', 'wpv-views' ) );
									} else if ( 'trash' == $current_post_status ) {
										$row_actions['restore-from-trash js-list-views-action-restore-from-trash'] = sprintf(
												'<a href="#">%s</a>',
												__( 'Restore from trash', 'wpv-views' ) );
										$row_actions['delete js-list-views-action-delete'] = sprintf( '<a href="#">%s</a>', __( 'Delete', 'wpv-views' ) );
									}

									echo wpv_admin_table_row_actions( $row_actions,	array(
											"data-view-id" => $post->ID,
											"data-viewactionnonce" => $wpa_action_nonce ) );
								?>
							</td>
							<td class="wpv-admin-listing-col-usage">
								<?php
								if ( $view_settings['view-query-mode'] == 'archive' ) {
									$selected = array();
									foreach ( $loops as $loop => $loop_name ) {
										if ( isset( $WPV_settings[ 'view_' . $loop ] ) && $WPV_settings[ 'view_' . $loop ] == $post->ID ) {
											$not_built_in = '';
											if ( !isset( $builtin_loops[ $loop ] ) ) {
												$not_built_in = __(' (post type archive)', 'wpv-views' );
											}
											$selected[] = '<li>' . $loop_name . $not_built_in . '</li>';
										}
									}

									foreach ( $taxonomies as $category_slug => $category ) {
										if ( in_array( $category_slug, $exclude_tax_slugs ) ) {
											continue;
										}
										// Only show taxonomies with show_ui set to TRUE
										if ( !$category->show_ui ) {
											continue;
										}
										$name = $category->name;
										if ( isset ( $WPV_settings[ 'view_taxonomy_loop_' . $name ] )
											&& $WPV_settings[ 'view_taxonomy_loop_' . $name ] == $post->ID )
										{
											$selected[] = '<li>' . $category->labels->name . __(' (taxonomy archive)', 'wpv-views') . '</li>';
										}
									}

									if ( !empty( $selected ) ) {
										?>
										<ul class="wpv-taglike-list js-list-views-loops">
											<?php
												echo implode( $selected );
											?>
										</ul>
										<?php
									} else {
										_e( 'This WordPress Archive isn\'t being used for any loops.', 'wpv-views' );
									}
								} else if ( $view_settings['view-query-mode'] == 'layouts-loop' ) {
									_e( 'This WordPress Archive is part of a Layout, so it will display the archive(s) to which the Layout is assigned.', 'wpv-views' );
								}
								?>
							</td>
							<td class="wpv-admin-listing-col-date">
								<?php echo get_the_time( get_option( 'date_format' ), $post->ID ); ?>
							</td>
						</tr>
					<?php
				endwhile;
			?>
		</tbody>
	</table>
	<div class="tablenav bottom">
		<?php
			echo wpv_admin_table_bulk_actions( $bulk_actions, $bulk_actions_class, $bulk_actions_args, 'bottom' );
		?>
	</div>

		<p class="add-new-view js-add-new-view">
			<a class="button js-wpv-views-archive-add-new wpv-views-archive-add-new" href="#">
				<i class="icon-plus"></i><?php _e('Add new WordPress Archive','wpv-views') ?>
			</a>
		</p>
		<?php

		wpv_admin_listing_pagination( 'view-archives', $wpv_found_posts, $wpv_args["posts_per_page"], $mod_url );

	} else {
		// No WordPress Archives matches the criteria
		?>
		<div class="wpv-views-listing views-empty-list">
			<?php
				if ( 'trash' == $current_post_status && $is_search ) {
					printf(
							'<p>%s</p><p><a class="button-secondary" href="%s">%s</a></p>',
							__( 'No WordPress Archives in trash matched your criteria.', 'wpv-views' ),
							wpv_maybe_add_query_arg(
									array(
											'page' => 'view-archives',
											'orderby' => $mod_url['orderby'],
											'order' => $mod_url['order'],
											'items_per_page' => $mod_url['items_per_page'],
											'paged' => '1',
											'status' => 'trash' ),
									admin_url( 'admin.php' ) ),
							__( 'Return', 'wpv-views' ) );
				} else if ( 'trash' == $current_post_status ) {
					printf(
							'<p>%s</p><p><a class="button-secondary" href="%s">%s</a></p>',
							__( 'No WordPress Archives in trash.', 'wpv-views' ),
							wpv_maybe_add_query_arg(
									array(
											'page' => 'view-archives',
											'orderby' => $mod_url['orderby'],
											'order' => $mod_url['order'],
											'items_per_page' => $mod_url['items_per_page'],
											'paged' => '1' ),
									admin_url( 'admin.php' ) ),
							__( 'Return', 'wpv-views' ) );
				} else if ( $is_search ) {
					printf(
							'<p>%s</p><p><a class="button-secondary" href="%s">%s</a></p>',
							__( 'No WordPress Archives matched your criteria.', 'wpv-views' ),
							wpv_maybe_add_query_arg(
									array(
											'page' => 'view-archives',
											'orderby' => $mod_url['orderby'],
											'order' => $mod_url['order'],
											'items_per_page' => $mod_url['items_per_page'],
											'paged' => '1' ),
									admin_url( 'admin.php' ) ),
							__( 'Return', 'wpv-views' ) );
				} else {
					?>
					<p><?php _e( 'WordPress Archives let you customize the output of standard Archive pages.' );?></p>
					<p>
						<?php
							// "Create your first archive" link
							printf(
									'<a href="#" class="button js-wpv-views-archive-create-new"><i class="icon-plus"></i> %s</a>',
									__( 'Create your first WordPress Archive', 'wpv-views' ) );
						?>
					</p>
					<?php
				}
			?>
		</div>
		<?php
	}
}
function wpv_admin_content_template_listing_name() {

	$mod_url = array( // array of URL modifiers
		'orderby' => '',
		'order' => '',
		's' => '',
		'items_per_page' => '',
		'paged' => '',
		'status' => ''
	);

	$wpv_args = array(
		'post_type' => 'view-template',
		'posts_per_page' => WPV_ITEMS_PER_PAGE,
		'order' => 'ASC',
		'orderby' => 'title',
		'post_status' => 'publish'
	);

	// Apply post_status coming from the URL parameters.
    $post_status = wpv_getget( 'status', 'publish', array( 'publish', 'trash' ) );
	$wpv_args['post_status'] = $post_status;
	$mod_url['status'] = $post_status;
    $the_other_post_status = ( 'publish' == $post_status ) ? 'trash' : 'publish';

	if ( isset( $_GET["s"] ) && '' != $_GET["s"] ) {
		$wpv_args = wpv_modify_wpquery_for_search( $_GET["s"], $wpv_args );
		$mod_url['s'] = sanitize_text_field( $_GET["s"] );
	}

	if ( isset( $_GET["items_per_page"] ) && '' != $_GET["items_per_page"] ) {
		$wpv_args['posts_per_page'] = (int) $_GET["items_per_page"];
		$mod_url['items_per_page'] = (int) $_GET["items_per_page"];
	}

	if ( isset( $_GET["orderby"] ) && '' != $_GET["orderby"] ) {
		$wpv_args['orderby'] = sanitize_text_field($_GET["orderby"]);
		$mod_url['orderby'] = sanitize_text_field($_GET["orderby"]);
		if ( isset( $_GET["order"] ) && '' != $_GET["order"] ) {
			$wpv_args['order'] = sanitize_text_field($_GET["order"]);
			$mod_url['order'] = sanitize_text_field($_GET["order"]);
		}
	}

	if ( isset( $_GET["paged"] ) && '' != $_GET["paged"]) {
		$wpv_args['paged'] = (int) $_GET["paged"];
		$mod_url['paged'] = (int) $_GET["paged"];
	}

    // Build a query for the other post status. We're interested only in post count
    $other_post_status_args = $wpv_args;
    $other_post_status_args['post_status'] = $the_other_post_status;
    $other_post_status_args['fields'] = 'ids';

    // All querying must be done between those two switch_lang() calls otherwise CT translations
    // will be also (wrongly) included.
    global $sitepress;

    $default_language = '';
    if( isset( $sitepress ) ) {
        //changes to the default language
        $default_language = $sitepress->get_default_language();
        $sitepress->switch_lang( $default_language );
    }

	$query = new WP_Query( $wpv_args );

    $other_post_status_query = new WP_Query( $other_post_status_args );

    if( isset( $sitepress ) ) {
        //changes to the current language
        $sitepress->switch_lang( ICL_LANGUAGE_CODE );
    }

    // Number of posts that are being displayed.
    $wpv_count_posts = $query->post_count;

    // Total number of posts matching the query.
    $wpv_found_posts = $query->found_posts;

    // to hold the number of Views in each status
    $ct_counts_by_post_status = array(
        $post_status => $wpv_found_posts,
        $the_other_post_status => $other_post_status_query->found_posts
    );

    // True if some content templates (even those not matching current query) exist.
    $some_posts_exist = ( $ct_counts_by_post_status['publish'] > 0 || $ct_counts_by_post_status['trash'] > 0 );

	$active_nondefault_languages = array();
	$add_translation_icon = '';
	$edit_translation_icon = '';

	$are_cts_translatable = WPV_Content_Template_Embedded::is_translatable();
    if( $are_cts_translatable ) {
        $active_languages = apply_filters( 'wpml_active_languages', array() );

        // just remove the default language
        $active_nondefault_languages = $active_languages;
        unset( $active_nondefault_languages[ $default_language ] );

        // store urls to add/edit translaton icons
        if( defined( 'ICL_PLUGIN_URL' ) ) {
            $add_translation_icon = ICL_PLUGIN_URL . '/res/img/add_translation.png';
            $edit_translation_icon = ICL_PLUGIN_URL . '/res/img/edit_translation.png';
        }
    }

	?>

	<?php
		if ( $some_posts_exist ) {
			?>
			<ul class="subsubsub" style="clear:both"><!-- links to lists WPA in different statuses -->
				<li>
					<?php
						$is_plain_publish_current_status = ( $wpv_args['post_status'] == 'publish' && !isset( $_GET["s"] ) );
						printf(
								'<a href="%s" %s>%s</a> (%s) | ',
								esc_url( add_query_arg(
										array( 'page' => 'view-templates', 'status' => 'publish' ),
										admin_url( 'admin.php' ) ) ),
								$is_plain_publish_current_status ?  ' class="current" ' : '',
								__( 'Published', 'wpv-views' ),
								$ct_counts_by_post_status['publish'] );

					?>
				</li>
				<li>
					<?php
						$is_plain_trash_current_status = ( $wpv_args['post_status'] == 'trash' && !isset( $_GET["s"] ) );
						printf(
								'<a href="%s" %s>%s</a> (%s)',
								esc_url( add_query_arg(
										array( 'page' => 'view-templates', 'status' => 'trash' ),
										admin_url( 'admin.php' ) ) ),
								$is_plain_trash_current_status ?  ' class="current" ' : '',
								__( 'Trash', 'wpv-views' ),
								$ct_counts_by_post_status['trash'] );
					?>
				</li>
			</ul>

			<?php
		} else {
			// No post exist at all
			?>
			<p class="wpv-view-not-exist">
			<?php _e('Content Templates let you design single pages.','wpv-views'); ?>
			</p>
			<p class="add-new-view">
				<button class="button js-add-new-content-template"
				data-target="<?php echo esc_url( add_query_arg( array( 'action' => 'wpv_ct_create_new' ), admin_url( 'admin-ajax.php' ) ) ); ?>">
					<i class="icon-plus"></i><?php _e('Add new Content Template','wpv-views') ?>
				</button>
			</p><?php
		}

        // A nonce for CT action - used for individual as well as for bulk actions.
        // It will have a value only if some posts exist.
        $ct_action_nonce = '';

		if( $some_posts_exist ) {

			$ct_action_nonce = wp_create_nonce( 'wpv_view_listing_actions_nonce' );
			
			// === Render "tablenav" section (Bulk actions and Search box) ===
			echo '<div class="tablenav top">';

			if( $wpv_count_posts > 0 ) {
		
				// Prepare to render bulk actions dropdown.
				if( 'publish' == $wpv_args['post_status'] ) {
					$bulk_actions = array( 'trash' => __( 'Move to trash', 'wpv-views' ) );
				} else {
					$bulk_actions = array(
							'restore-from-trash' => __( 'Restore from trash', 'wpv-views' ),
							'delete' => __( 'Delete permanently', 'wpv-views' ) );
				}

				$bulk_actions_args = array( 'data-viewactionnonce' => $ct_action_nonce );
				$bulk_actions_class = 'js-wpv-ct-listing-bulk-action';

				echo wpv_admin_table_bulk_actions( $bulk_actions, $bulk_actions_class, $bulk_actions_args, 'top' );
			}

			// Show search box
			if ( $wpv_found_posts > 0 ) {
				?>
				<div class="alignright">
					<form id="posts-filter" action="" method="get">
						<p class="search-box">
							<label class="screen-reader-text" for="post-search-input"><?php _e('Search Views:', 'wpv-views') ?></label>
							<?php $search_term = isset( $_GET["s"] ) ? urldecode( sanitize_text_field($_GET["s"]) ) : ''; ?>
							<input type="search" id="ct-post-search-input" name="s" value="<?php echo $search_term; ?>">
							<input type="submit" name="" id="ct-search-submit" class="button" value="<?php echo htmlentities( __('Search Content Templates', 'wpv-views'), ENT_QUOTES ); ?>">
							<input type="hidden" name="paged" value="1" />
						</p>
					</form>
				</div>
				<?php
			}
			
			echo '</div>'; // End of tablenav section
			
		}

		if ( $wpv_count_posts == 0 && $some_posts_exist ) {
			// No posts are displayed, but some exist
			if ( isset( $_GET["s"] ) && '' != $_GET["s"] ) {
				if ( $wpv_args['post_status'] == 'trash' ) {
					// Searching in trash
					?>
					<div class="wpv-views-listing views-empty-list">
						<p>
							<?php
								printf(
										'<p>%s</p><p><a class="button-secondary" href="%s">%s</a></p>',
										__( 'No Content Templates in trash matched your criteria.', 'wpv-views' ),
										wpv_maybe_add_query_arg(
												array(
														'page' => 'view-templates',
														'orderby' => $mod_url['orderby'],
														'order' => $mod_url['order'],
														'items_per_page' => $mod_url['items_per_page'],
														'paged' => '1',
														'status' => 'trash' ),
												admin_url( 'admin.php' ) ),
										__( 'Return', 'wpv-views' ) );
							?>
						</p>
					</div>
					<?php
				} else {
					// Normal search
					?>
					<div class="wpv-views-listing views-empty-list">
						<p>
							<?php
								printf(
										'<p>%s</p><p><a class="button-secondary" href="%s">%s</a></p>',
										__( 'No Content Templates matched your criteria.', 'wpv-views' ),
										wpv_maybe_add_query_arg(
												array(
														'page' => 'view-templates',
														'orderby' => $mod_url['orderby'],
														'order' => $mod_url['order'],
														'items_per_page' => $mod_url['items_per_page'],
														'paged' => '1' ),
												admin_url( 'admin.php' ) ),
										__( 'Return', 'wpv-views' ) );
							?>
						</p>
					</div>
					<?php
				}
			} else {
				if ( $wpv_args['post_status'] == 'trash' ) {
					// No items in trash
					?>
					<div class="wpv-views-listing views-empty-list">
						<p>
							<?php
								printf(
										'<p>%s</p><p><a class="button-secondary" href="%s">%s</a></p>',
										__( 'No Content Templates in trash.', 'wpv-views' ),
										wpv_maybe_add_query_arg(
												array(
														'page' => 'view-templates',
														'orderby' => $mod_url['orderby'],
														'order' => $mod_url['order'],
														'items_per_page' => $mod_url['items_per_page'],
														'paged' => '1' ),
												admin_url( 'admin.php' ) ),
										__( 'Return', 'wpv-views' ) );
							?>
						</p>
					</div>
					<?php
				} else {
					?>
					<p class="wpv-view-not-exist">
						<?php _e('Content Templates let you design single pages.','wpv-views'); ?>
					</p>
					<p class="add-new-view">
						<button class="button js-add-new-content-template"
								data-target="<?php echo esc_url( add_query_arg( array( 'action' => 'wpv_ct_create_new' ), admin_url( 'admin-ajax.php' ) ) ); ?>">
							<i class="icon-plus"></i><?php _e( 'Add new Content Template', 'wpv-views') ?>
						</button>
					</p>
					<?php
				}
			}
			
		} else if ( $wpv_count_posts != 0 ) {
			// We have some results to display.
			
			global $wpdb;

			?>
			
			<table class="wpv-views-listing widefat">

			<!-- section for: sort by name -->
				<thead>
					<?php
						/* To avoid code duplication, table header is stored in output buffer and echoed twice - within
						 * thead and tfoot tags. */
						ob_start();
					?>
					<tr>
						<th class="wpv-admin-listing-col-bulkactions check-column">
							<input type="checkbox" />
						</th>
						<?php
							$column_active = '';
							$column_sort_to = 'ASC';
							$column_sort_now = 'ASC';
							$status = '';
							if ( $wpv_args['orderby'] === 'title' ) {
								$column_active = ' views-list-sort-active';
								$column_sort_to = ( $wpv_args['order'] === 'ASC' ) ? 'DESC' : 'ASC';
								$column_sort_now = $wpv_args['order'];
							}
							if ( isset($_GET['status']) && $_GET['status'] == 'trash' ){
								$status = 'trash';
							}
						?>
						<th class="wpv-admin-listing-col-title">
							<?php
								printf(
										'<a href="%s" class="%s" data-orderby="title">%s <i class="%s"></i></a>',
										wpv_maybe_add_query_arg(
												array(
														'page' => 'view-templates',
														'status' => $status,
														'orderby' => 'title',
														'order' => $column_sort_to,
														's' => $mod_url['s'],
														'items_per_page' => $mod_url['items_per_page'],
														'paged' => $mod_url['paged'] ),
												admin_url( 'admin.php' ) ),
										'js-views-list-sort views-list-sort ' . $column_active,
										__( 'Title', 'wpv-views' ),
										( 'DESC'  === $column_sort_now ) ? 'icon-sort-by-alphabet-alt' : 'icon-sort-by-alphabet' );
							?>
						</th>
                        <?php
                            if( $are_cts_translatable ) {
                                $flag_images = array();

                                foreach( $active_nondefault_languages as $language_info ) {
                                    $flag_images[] = sprintf(
                                        '<img style="padding: 2px;" src="%s" title="%s" alt="%s" />',
                                        $language_info['country_flag_url'],
                                        $language_info['translated_name'],
                                        $language_info['code']
                                    );
                                }
                                if( empty( $flag_images ) ) {
                                    $translation_column_header = __( 'Translations', 'wpv-views' );
                                } else {
                                    $translation_column_header = implode( '', $flag_images );
                                }

                                printf( '<th>%s</th>', $translation_column_header );
                            }
                        ?>
						<th class="wpv-admin-listing-col-usage js-wpv-col-two"><?php _e('Used on','wpv-views') ?></th>
						<?php
							$column_active = '';
							$column_sort_to = 'DESC';
							$column_sort_now = 'DESC';
							if ( $wpv_args['orderby'] === 'date' ) {
								$column_active = ' views-list-sort-active';
								$column_sort_to = ( $wpv_args['order'] === 'ASC' ) ? 'DESC' : 'ASC';
								$column_sort_now = $wpv_args['order'];
							}
						?>
						<th class="wpv-admin-listing-col-date">
							<?php
								printf(
										'<a href="%s" class="%s" data-orderby="date">%s <i class="%s"></i></a>',
										wpv_maybe_add_query_arg(
												array(
														'page' => 'view-templates',
														'status' => $status,
														'orderby' => 'date',
														'order' => $column_sort_to,
														's' => $mod_url['s'],
														'items_per_page' => $mod_url['items_per_page'],
														'paged' => $mod_url['paged'] ),
												admin_url( 'admin.php' ) ),
										'js-views-list-sort views-list-sort ' . $column_active,
										__( 'Date', 'wpv-views' ),
										( 'DESC'  === $column_sort_now ) ? 'icon-sort-by-attributes-alt' : 'icon-sort-by-attributes' );
							?>
						</th>
					</tr>
					<?php
						// Get table header from output buffer and stop buffering
						$table_header = ob_get_contents();
						ob_end_clean();

						echo $table_header;
					?>
				</thead>
				<tfoot>
					<?php
						echo $table_header;
					?>
				</tfoot>

				<tbody class="js-wpv-views-listing-body">
					<?php
					$alternate = '';
					while ( $query->have_posts() ) :
						$query->the_post();
						$post = get_post( get_the_id() );
						$template_id = $post->ID;
                        $ct = WPV_Content_Template::get_instance( $template_id );
						$wpv_content_template_decription  = get_post_meta( $template_id, '_wpv-content-template-decription', true );
						$layout_loop_template_for_view_id = get_post_meta( $template_id, '_view_loop_id', true );
						$alternate = ( ' alternate' == $alternate ) ? '' : ' alternate';
						?>
						<tr id="wpv_ct_list_row_<?php echo $template_id; ?>" class="js-wpv-ct-list-row<?php echo $alternate; ?>">
							<th class="wpv-admin-listing-col-bulkactions check-column">
								<?php
									if ( empty( $layout_loop_template_for_view_id ) ) {
										printf( '<input type="checkbox" value="%s" name="view[]" />', $template_id );
									}
								?>
							</th>
							<td class="wpv-admin-listing-col-title post-title page-title column-title">
								<span class="row-title">
									<?php
										if ( $wpv_args['post_status'] == 'trash' ) {
											echo esc_html( $post->post_title );
										} else {
                                            wpv_ct_editor_render_link( $template_id, esc_html( $post->post_title ) );
										}
									?>
								</span>
								<?php
									if ( ! empty( $wpv_content_template_decription ) ) {
										?>
										<p class="desc">
											<?php echo nl2br( $wpv_content_template_decription )?>
										</p>
										<?php
									}
									/* Generate and show row actions.
									 * Note that we want to add also 'simple' action names to the action list because
									 * they get echoed as a class of the span tag and get styled from WordPress core css
									 * accordingly (e.g. trash in different colour than the rest) */
									$row_actions = array();
									$asigned_count = $wpdb->get_var(
										$wpdb->prepare(
											"SELECT COUNT(post_id) FROM {$wpdb->postmeta} JOIN {$wpdb->posts} p 
											WHERE meta_key = '_views_template' 
											AND meta_value = %s
											AND post_id = p.ID 
											AND p.post_status NOT IN ('auto-draft') 
											AND p.post_type != 'revision'",
											$template_id
										)
									);
									if ( 'publish' == $wpv_args['post_status'] ) {
										$row_actions['edit'] = sprintf(
												'<a href="%s">%s</a>',
												esc_url( add_query_arg(
														array( 'page' => WPV_CT_EDITOR_PAGE_NAME, 'ct_id' => $template_id ),
														admin_url( 'admin.php' ) ) ),
												__( 'Edit', 'wpv-views' ) );
										if ( empty( $layout_loop_template_for_view_id ) ) {
											$row_actions['change js-wpv-ct-change-usage-popup'] = sprintf( '<a href="#">%s</a>', __( 'Change template usage', 'wpv-views' ) );
										}
										$row_actions['duplicate js-list-ct-action-duplicate'] = sprintf( '<a href="#">%s</a>', __( 'Duplicate', 'wpv-views' ) );
										if ( empty( $layout_loop_template_for_view_id ) ) {
											$row_actions['trash js-wpv-ct-action-trash'] = sprintf( '<a href="#">%s</a>', __( 'Move to trash', 'wpv-views' ) );
										}
									} else if ( 'trash' == $wpv_args['post_status'] ) {
										$row_actions['restore-from-trash js-wpv-ct-action-restore-from-trash'] = sprintf( '<a href="#">%s</a>', __( 'Restore from trash', 'wpv-views' ) );
										$row_actions['delete js-list-ct-action-delete'] = sprintf( '<a href="#">%s</a>', __( 'Delete', 'wpv-views' ) );
									}

									echo wpv_admin_table_row_actions( $row_actions,	array(
												"data-ct-id" => $template_id,
												"data-postcount" => $asigned_count,
												"data-ct-name" => htmlentities( $post->post_title, ENT_QUOTES ),
												"data-viewactionnonce" => $ct_action_nonce,
												// Used by the "duplicate" action
												"data-msg" => htmlentities( __( 'Enter new title','wpv-views'), ENT_QUOTES ) 
											)
										);
								?>
							</td>
                            <?php
                                if( $are_cts_translatable ) {
                                    echo '<td>';
                                    $ct_translations = $ct->wpml_translations;

                                    foreach( $active_nondefault_languages as $language_info ) {
                                        $translation = wpv_getarr( $ct_translations, $language_info['code'], null );

                                        if( null == $translation ) {
											$translation_text = __( 'Add translation', 'wpv-views' );
											$translation_icon = $add_translation_icon;
                                        } else {
											$translation_text = __( 'Edit translation', 'wpv-views' );
											$translation_icon = $edit_translation_icon;
                                        }

										$translation_editor_link = $ct->get_wpml_tm_link( $language_info['code'] );
										if( null != $translation_editor_link ) {
											printf(
												'<a style="padding: 2px;" href="%s"><img alt="%s" src="%s" title="%s" /></a>',
												$translation_editor_link,
												$language_info['code'],
												$translation_icon,
												$translation_text
											);
										} else {
											/** @noinspection CssInvalidFunction */
											/** @noinspection CssUnknownProperty */
											printf(
												'<span style="padding: 2px">
													<img alt="%s" src="%s" title="%s" style="-webkit-filter: grayscale(100%%); filter: grayscale(100%%)"/>
												</span>',
												$language_info['code'],
												$translation_icon,
												__( 'WPML Translation Management must be active for this link to work.', 'wpv-view' )
											);
										}
                                    }

                                    echo '</td>';
                                }
                            ?>
							<td class="wpv-admin-listing-col-usage">
								<?php echo wpv_content_template_used_for_list( $template_id ); ?>
							</td>
							<td class="wpv-admin-listing-col-date">
								<?php echo get_the_time( get_option( 'date_format' ), $template_id ); ?>
							</td>
						</tr>
						<?php
					endwhile;
					?>
				</tbody>
			</table>

			<div class="tablenav bottom">
				<?php
					echo wpv_admin_table_bulk_actions( $bulk_actions, $bulk_actions_class, $bulk_actions_args, 'bottom' );
				?>
			</div>


			<p class="add-new-view">
				<button class="button js-add-new-content-template"
						data-target="<?php echo esc_url( add_query_arg( array( 'action' => 'wpv_ct_create_new' ), admin_url( 'admin-ajax.php' ) ) ); ?>">
					<i class="icon-plus"></i><?php _e( 'Add new Content Template','wpv-views' ) ?>
				</button>
			</p>

			<?php
		}

		wpv_admin_listing_pagination( 'view-templates', $wpv_found_posts, $wpv_args["posts_per_page"], $mod_url );

		// Render dialog templates.
		wpv_render_ct_listing_dialog_templates_arrangeby_name();
}
/**
 * wpv_admin_view_listing_table
 *
 * Displays the content of the Views admin listing page: status, table and pagination.
 *
 * @param array $views_pre_query_data Array with IDs of possible results and counts per post status.
 *     See wpv_prepare_view_listing_query() for details.
 * @param string $current_post_status Status of posts to display. Can be 'publish' or 'trash'.
 *
 * @since unknown
 */
function wpv_admin_view_listing_table($views_pre_query_data, $current_post_status)
{
    // array of URL modifiers
    $mod_url = array('orderby' => '', 'order' => '', 's' => '', 'items_per_page' => '', 'paged' => '', 'status' => $current_post_status);
    // array of WP_Query parameters
    $wpv_args = array('post_type' => 'view', 'post__in' => $views_pre_query_data['post__in'], 'posts_per_page' => WPV_ITEMS_PER_PAGE, 'order' => 'ASC', 'orderby' => 'title', 'post_status' => $current_post_status);
    // perform the search in Views titles and decriptions and update post__in argument to $wpv_args.
    if (isset($_GET["s"]) && '' != $_GET["s"]) {
        $wpv_args = wpv_modify_wpquery_for_search($_GET["s"], $wpv_args);
        $mod_url['s'] = sanitize_text_field($_GET["s"]);
    }
    // apply posts_per_page coming from the URL parameters
    if (isset($_GET["items_per_page"]) && '' != $_GET["items_per_page"]) {
        $wpv_args['posts_per_page'] = (int) $_GET["items_per_page"];
        $mod_url['items_per_page'] = (int) $_GET["items_per_page"];
    }
    // apply orderby coming from the URL parameters
    if (isset($_GET["orderby"]) && '' != $_GET["orderby"]) {
        $wpv_args['orderby'] = sanitize_text_field($_GET["orderby"]);
        $mod_url['orderby'] = sanitize_text_field($_GET["orderby"]);
        // apply order coming from the URL parameters
        if (isset($_GET["order"]) && '' != $_GET["order"]) {
            $wpv_args['order'] = sanitize_text_field($_GET["order"]);
            $mod_url['order'] = sanitize_text_field($_GET["order"]);
        }
    }
    // apply paged coming from the URL parameters
    if (isset($_GET["paged"]) && '' != $_GET["paged"]) {
        $wpv_args['paged'] = (int) $_GET["paged"];
        $mod_url['paged'] = (int) $_GET["paged"];
    }
    $wpv_query = new WP_Query($wpv_args);
    // The number of Views being displayed.
    $wpv_count_posts = $wpv_query->post_count;
    // Total number of Views matching query parameters.
    $wpv_found_posts = $wpv_query->found_posts;
    ?>
		<ul class="subsubsub"><!-- links to lists Views in different statuses -->
			<?php 
    // "publish" status
    $is_current = 'publish' == $current_post_status && !isset($_GET["s"]);
    printf('<li><a href="%s" %s >%s</a> (%s) | </li>', esc_url(add_query_arg(array('page' => 'views', 'status' => 'publish'), admin_url('admin.php'))), $is_current ? ' class="current" ' : '', __('Published', 'wpv-views'), $views_pre_query_data['published_count']);
    // "trash" status
    $is_current = 'trash' == $current_post_status && !isset($_GET["s"]);
    printf('<li><a href="%s" %s >%s</a> (%s)</li>', esc_url(add_query_arg(array('page' => 'views', 'status' => 'trash'), admin_url('admin.php'))), $is_current ? ' class="current" ' : '', __('Trash', 'wpv-views'), $views_pre_query_data['trashed_count']);
    ?>
		</ul>
	<?php 
    // A nonce for view action - used for individual as well as for bulk actions
    $view_action_nonce = wp_create_nonce('wpv_view_listing_actions_nonce');
    // === Render "tablenav" section (Bulk actions and Search box) ===
    echo '<div class="tablenav top">';
    // If there is one or more Views in this query, show search box
    if ($wpv_found_posts > 0) {
        ?>
		<div class="alignright">
			<form id="posts-filter" action="" method="get">
				<p class="search-box">
					<label class="screen-reader-text" for="post-search-input"><?php 
        _e('Search Views', 'wpv-views');
        ?>
:</label>
					<?php 
        $search_term = isset($_GET["s"]) ? urldecode(sanitize_text_field($_GET["s"])) : '';
        ?>
					<input type="search" id="post-search-input" name="s" value="<?php 
        echo $search_term;
        ?>
" />
					<input type="submit" name="" id="search-submit" class="button" value="<?php 
        echo htmlentities(__('Search Views', 'wpv-views'), ENT_QUOTES);
        ?>
" />
					<input type="hidden" name="paged" value="1" />
				</p>
			</form>
		</div>
		<?php 
    }
    // If this page has one or more Views, show Bulk actions controls
    if ($wpv_count_posts > 0) {
        // Prepare ender bulk actions dropdown.
        if ('publish' == $current_post_status) {
            $bulk_actions = array('trash' => __('Move to trash', 'wpv-views'));
        } else {
            $bulk_actions = array('restore-from-trash' => __('Restore from trash', 'wpv-views'), 'delete' => __('Delete permanently', 'wpv-views'));
        }
        $bulk_actions_args = array('data-viewactionnonce' => $view_action_nonce);
        $bulk_actions_class = 'js-wpv-views-listing-bulk-action';
        echo wpv_admin_table_bulk_actions($bulk_actions, $bulk_actions_class, $bulk_actions_args, 'top');
    }
    echo '</div>';
    // End of tablenav section
    // If this page has one or more Views, show the table
    if ($wpv_count_posts > 0) {
        ?>
		<table class="wpv-views-listing js-wpv-views-listing widefat">
			<thead>
				<?php 
        /* To avoid code duplication, table header is stored in output buffer and echoed twice - within
         * thead and tfoot tags. */
        ob_start();
        ?>
				<tr>
					<th class="wpv-admin-listing-col-bulkactions check-column">
						<input type="checkbox" />
					</th>
					<?php 
        $column_active = '';
        $column_sort_to = 'ASC';
        $column_sort_now = 'ASC';
        if ($wpv_args['orderby'] === 'title') {
            $column_active = ' views-list-sort-active';
            $column_sort_to = $wpv_args['order'] === 'ASC' ? 'DESC' : 'ASC';
            $column_sort_now = $wpv_args['order'];
        }
        ?>
					<th class="wpv-admin-listing-col-title">
						<?php 
        // "sort by title" link
        printf('<a href="%s" class="%s" data-orderby="title">%s <i class="%s"></i></a>', wpv_maybe_add_query_arg(array('page' => 'views', 'orderby' => 'title', 'order' => $column_sort_to, 's' => $mod_url['s'], 'items_per_page' => $mod_url['items_per_page'], 'paged' => $mod_url['paged'], 'status' => $mod_url['status']), admin_url('admin.php')), 'js-views-list-sort views-list-sort ' . $column_active, __('Title', 'wpv-views'), 'DESC' === $column_sort_now ? 'icon-sort-by-alphabet-alt' : 'icon-sort-by-alphabet');
        ?>
					</th>

					<th class="wpv-admin-listing-col-summary js-wpv-col-two"><?php 
        _e('Content to load', 'wpv-views');
        ?>
</th>
					<th class="wpv-admin-listing-col-scan"><?php 
        _e('Used on', 'wpv-views');
        ?>
</th>

					<?php 
        $column_active = '';
        $column_sort_to = 'DESC';
        $column_sort_now = 'DESC';
        if ($wpv_args['orderby'] === 'date') {
            $column_active = ' views-list-sort-active';
            $column_sort_to = $wpv_args['order'] === 'ASC' ? 'DESC' : 'ASC';
            $column_sort_now = $wpv_args['order'];
        }
        ?>
					<th class="wpv-admin-listing-col-date">
						<?php 
        // "sort by date" link
        printf('<a href="%s" class="%s" data-orderby="date">%s <i class="%s"></i></a>', wpv_maybe_add_query_arg(array('page' => 'views', 'orderby' => 'date', 'order' => $column_sort_to, 's' => $mod_url['s'], 'items_per_page' => $mod_url['items_per_page'], 'paged' => $mod_url['paged'], 'status' => $mod_url['status']), admin_url('admin.php')), 'js-views-list-sort views-list-sort ' . $column_active, __('Date', 'wpv-views'), 'DESC' === $column_sort_now ? 'icon-sort-by-attributes-alt' : 'icon-sort-by-attributes');
        ?>
					</th>
				</tr>
				<?php 
        // Get table header from output buffer and stop buffering
        $table_header = ob_get_contents();
        ob_end_clean();
        echo $table_header;
        ?>
			</thead>
			<tfoot>
				<?php 
        echo $table_header;
        ?>
			</tfoot>

			<tbody class="js-wpv-views-listing-body">
				<?php 
        $alternate = '';
        while ($wpv_query->have_posts()) {
            $wpv_query->the_post();
            $post_id = get_the_id();
            $post = get_post($post_id);
            $meta = get_post_meta($post_id, '_wpv_settings');
            $view_description = get_post_meta($post_id, '_wpv_description', true);
            $alternate = ' alternate' == $alternate ? '' : ' alternate';
            ?>
						<tr id="wpv_view_list_row_<?php 
            echo $post->ID;
            ?>
" class="js-wpv-view-list-row<?php 
            echo $alternate;
            ?>
">
							<th class="wpv-admin-listing-col-bulkactions check-column">
								<?php 
            printf('<input type="checkbox" value="%s" name="view[]" />', $post->ID);
            ?>
							</th>
							<td class="wpv-admin-listing-col-title">
								<span class="row-title">
								<?php 
            if ('trash' == $current_post_status) {
                echo $post->post_title;
            } else {
                printf('<a href="%s">%s</a>', esc_url(add_query_arg(array('page' => 'views-editor', 'view_id' => $post->ID), admin_url('admin.php'))), $post->post_title);
            }
            ?>
								</span>
								<?php 
            if (isset($view_description) && '' != $view_description) {
                ?>
										<p class="desc">
											<?php 
                echo nl2br($view_description);
                ?>
										</p>
										<?php 
            }
            /* Generate and show row actions.
             * Note that we want to add also 'simple' action names to the action list because
             * they get echoed as a class of the span tag and get styled from WordPress core css
             * accordingly (e.g. trash in different colour than the rest) */
            $row_actions = array();
            if ('publish' == $current_post_status) {
                $row_actions['edit'] = sprintf('<a href="%s">%s</a>', esc_url(add_query_arg(array('page' => 'views-editor', 'view_id' => $post->ID), admin_url('admin.php'))), __('Edit', 'wpv-views'));
                $row_actions['duplicate js-views-actions-duplicate'] = sprintf('<a href="#">%s</a>', __('Duplicate', 'wpv-views'));
                $row_actions['trash js-views-actions-trash'] = sprintf('<a href="#">%s</a>', __('Move to trash', 'wpv-views'));
            } else {
                if ('trash' == $current_post_status) {
                    $row_actions['restore-from-trash js-views-actions-restore-from-trash'] = sprintf('<a href="#">%s</a>', __('Restore from trash', 'wpv-views'));
                    $row_actions['delete js-views-actions-delete'] = sprintf('<a href="#">%s</a>', __('Delete', 'wpv-views'));
                }
            }
            echo wpv_admin_table_row_actions($row_actions, array("data-view-id" => $post->ID, "data-view-title" => $post->post_title, "data-viewactionnonce" => $view_action_nonce));
            ?>
							</td>
							<td class="wpv-admin-listing-col-summary">
								<?php 
            echo wpv_create_content_summary_for_listing($post->ID);
            ?>
							</td>
							<td class="wpv-admin-listing-col-scan">
								<button class="button js-scan-button" data-view-id="<?php 
            echo $post->ID;
            ?>
">
									<?php 
            _e('Scan', 'wp-views');
            ?>
								</button>
								<span class="js-nothing-message hidden"><?php 
            _e('Nothing found', 'wpv-views');
            ?>
</span>
							</td>
							<td class="wpv-admin-listing-col-date">
								<?php 
            echo get_the_time(get_option('date_format'), $post->ID);
            ?>
							</td>
						</tr>
					<?php 
        }
        ?>
			</tbody>
		</table>

		<div class="tablenav bottom">
			<?php 
        echo wpv_admin_table_bulk_actions($bulk_actions, $bulk_actions_class, $bulk_actions_args, 'bottom');
        ?>
		</div>

		<p class="add-new-view" >
			<a class="button js-wpv-views-add-new" href="#">
				<i class="icon-plus"></i><?php 
        _e('Add new View', 'wpv-views');
        ?>
			</a>
		</p>

		<?php 
        wpv_admin_listing_pagination('views', $wpv_found_posts, $wpv_args["posts_per_page"], $mod_url);
        ?>
		<?php 
    } else {
        // No Views matches the criteria
        ?>
		<div class="wpv-views-listing views-empty-list">
			<?php 
        if (isset($_GET["status"]) && $_GET["status"] == 'trash' && isset($_GET["s"]) && $_GET["s"] != '') {
            printf('<p>%s</p><p><a class="button-secondary" href="%s">%s</a></p>', __('No Views in trash matched your criteria.', 'wpv-views'), wpv_maybe_add_query_arg(array('page' => 'views', 'orderby' => $mod_url['orderby'], 'order' => $mod_url['order'], 'items_per_page' => $mod_url['items_per_page'], 'paged' => '1', 'status' => 'trash'), admin_url('admin.php')), __('Return', 'wpv-views'));
        } else {
            if (isset($_GET["status"]) && $_GET["status"] == 'trash') {
                printf('<p>%s</p><p><a class="button-secondary" href="%s">%s</a></p>', __('No Views in trash.', 'wpv-views'), wpv_maybe_add_query_arg(array('page' => 'views', 'orderby' => $mod_url['orderby'], 'order' => $mod_url['order'], 'items_per_page' => $mod_url['items_per_page'], 'paged' => '1'), admin_url('admin.php')), __('Return', 'wpv-views'));
            } else {
                if (isset($_GET["s"]) && $_GET["s"] != '') {
                    printf('<p>%s</p><p><a class="button-secondary" href="%s">%s</a></p>', __('No Views matched your criteria.', 'wpv-views'), wpv_maybe_add_query_arg(array('page' => 'views', 'orderby' => $mod_url['orderby'], 'order' => $mod_url['order'], 'items_per_page' => $mod_url['items_per_page'], 'paged' => '1'), admin_url('admin.php')), __('Return', 'wpv-views'));
                } else {
                    ?>
					<div class="wpv-view-not-exist js-wpv-view-not-exist">
						<p><?php 
                    _e('Views load content from the database and display on the site.');
                    ?>
</p>
						<p><a class="button js-wpv-views-add-first" href="#"><i class="icon-plus"></i><?php 
                    _e('Create your first View', 'wpv-views');
                    ?>
</a></p>
					</div>
					<?php 
                }
            }
        }
        ?>
		</div>
		<?php 
    }
}
function wpv_admin_content_template_listing_name()
{
    $mod_url = array('orderby' => '', 'order' => '', 's' => '', 'items_per_page' => '', 'paged' => '', 'status' => '');
    $wpv_args = array('post_type' => 'view-template', 'posts_per_page' => WPV_ITEMS_PER_PAGE, 'order' => 'ASC', 'orderby' => 'title', 'post_status' => 'publish');
    // Apply post_status coming from the URL parameters.
    if (isset($_GET["status"]) && '' != $_GET["status"]) {
        $wpv_args['post_status'] = sanitize_text_field($_GET["status"]);
        $mod_url['status'] = sanitize_text_field($_GET["status"]);
    }
    if (isset($_GET["s"]) && '' != $_GET["s"]) {
        $wpv_args = wpv_modify_wpquery_for_search($_GET["s"], $wpv_args);
        $mod_url['s'] = sanitize_text_field($_GET["s"]);
    }
    if (isset($_GET["items_per_page"]) && '' != $_GET["items_per_page"]) {
        $wpv_args['posts_per_page'] = (int) $_GET["items_per_page"];
        $mod_url['items_per_page'] = (int) $_GET["items_per_page"];
    }
    if (isset($_GET["orderby"]) && '' != $_GET["orderby"]) {
        $wpv_args['orderby'] = sanitize_text_field($_GET["orderby"]);
        $mod_url['orderby'] = sanitize_text_field($_GET["orderby"]);
        if (isset($_GET["order"]) && '' != $_GET["order"]) {
            $wpv_args['order'] = sanitize_text_field($_GET["order"]);
            $mod_url['order'] = sanitize_text_field($_GET["order"]);
        }
    }
    if (isset($_GET["paged"]) && '' != $_GET["paged"]) {
        $wpv_args['paged'] = (int) $_GET["paged"];
        $mod_url['paged'] = (int) $_GET["paged"];
    }
    $query = new WP_Query($wpv_args);
    // Number of posts that are being displayed.
    $wpv_count_posts = $query->post_count;
    // Total number of posts matching the query.
    $wpv_found_posts = $query->found_posts;
    $all_posts = wp_count_posts('view-template');
    $wpv_views_status = array();
    // to hold the number of Views in each status
    $wpv_views_status['publish'] = $all_posts->publish;
    $wpv_views_status['trash'] = $all_posts->trash;
    // True if some content templates (even those not matching current query) exist.
    $some_posts_exist = $wpv_views_status['publish'] > 0 || $wpv_views_status['trash'] > 0;
    ?>

	<?php 
    if ($some_posts_exist) {
        ?>
			<ul class="subsubsub" style="clear:both"><!-- links to lists WPA in different statuses -->
				<li>
					<?php 
        $is_plain_publish_current_status = $wpv_args['post_status'] == 'publish' && !isset($_GET["s"]);
        printf('<a href="%s" %s>%s</a> (%s) | ', esc_url(add_query_arg(array('page' => 'view-templates', 'status' => 'publish'), admin_url('admin.php'))), $is_plain_publish_current_status ? ' class="current" ' : '', __('Published', 'wpv-views'), $wpv_views_status['publish']);
        ?>
				</li>
				<li>
					<?php 
        $is_plain_trash_current_status = $wpv_args['post_status'] == 'trash' && !isset($_GET["s"]);
        printf('<a href="%s" %s>%s</a> (%s)', esc_url(add_query_arg(array('page' => 'view-templates', 'status' => 'trash'), admin_url('admin.php'))), $is_plain_trash_current_status ? ' class="current" ' : '', __('Trash', 'wpv-views'), $wpv_views_status['trash']);
        ?>
				</li>
			</ul>

			<?php 
    } else {
        // No post exist at all
        ?>
			<p class="wpv-view-not-exist">
			<?php 
        _e('Content Templates let you design single pages.', 'wpv-views');
        ?>
			</p>
			<p class="add-new-view">
				<button class="button js-add-new-content-template"
				data-target="<?php 
        echo esc_url(add_query_arg(array('action' => 'wpv_ct_create_new'), admin_url('admin-ajax.php')));
        ?>
">
					<i class="icon-plus"></i><?php 
        _e('Add new Content Template', 'wpv-views');
        ?>
				</button>
			</p><?php 
    }
    if ($some_posts_exist) {
        // A nonce for CT action - used for individual as well as for bulk actions
        $ct_action_nonce = wp_create_nonce('wpv_view_listing_actions_nonce');
        // === Render "tablenav" section (Bulk actions and Search box) ===
        echo '<div class="tablenav top">';
        if ($wpv_count_posts > 0) {
            // Prepare to render bulk actions dropdown.
            if ('publish' == $wpv_args['post_status']) {
                $bulk_actions = array('trash' => __('Move to trash', 'wpv-views'));
            } else {
                $bulk_actions = array('restore-from-trash' => __('Restore from trash', 'wpv-views'), 'delete' => __('Delete permanently', 'wpv-views'));
            }
            $bulk_actions_args = array('data-viewactionnonce' => $ct_action_nonce);
            $bulk_actions_class = 'js-wpv-ct-listing-bulk-action';
            echo wpv_admin_table_bulk_actions($bulk_actions, $bulk_actions_class, $bulk_actions_args, 'top');
        }
        // Show search box
        if ($wpv_found_posts > 0) {
            ?>
				<div class="alignright">
					<form id="posts-filter" action="" method="get">
						<p class="search-box">
							<label class="screen-reader-text" for="post-search-input"><?php 
            _e('Search Views:', 'wpv-views');
            ?>
</label>
							<?php 
            $search_term = isset($_GET["s"]) ? urldecode(sanitize_text_field($_GET["s"])) : '';
            ?>
							<input type="search" id="ct-post-search-input" name="s" value="<?php 
            echo $search_term;
            ?>
">
							<input type="submit" name="" id="ct-search-submit" class="button" value="<?php 
            echo htmlentities(__('Search Content Templates', 'wpv-views'), ENT_QUOTES);
            ?>
">
							<input type="hidden" name="paged" value="1" />
						</p>
					</form>
				</div>
				<?php 
        }
        echo '</div>';
        // End of tablenav section
    }
    if ($wpv_count_posts == 0 && $some_posts_exist) {
        // No posts are displayed, but some exist
        if (isset($_GET["s"]) && '' != $_GET["s"]) {
            if ($wpv_args['post_status'] == 'trash') {
                // Searching in trash
                ?>
					<div class="wpv-views-listing views-empty-list">
						<p>
							<?php 
                printf('<p>%s</p><p><a class="button-secondary" href="%s">%s</a></p>', __('No Content Templates in trash matched your criteria.', 'wpv-views'), wpv_maybe_add_query_arg(array('page' => 'view-templates', 'orderby' => $mod_url['orderby'], 'order' => $mod_url['order'], 'items_per_page' => $mod_url['items_per_page'], 'paged' => '1', 'status' => 'trash'), admin_url('admin.php')), __('Return', 'wpv-views'));
                ?>
						</p>
					</div>
					<?php 
            } else {
                // Normal search
                ?>
					<div class="wpv-views-listing views-empty-list">
						<p>
							<?php 
                printf('<p>%s</p><p><a class="button-secondary" href="%s">%s</a></p>', __('No Content Templates matched your criteria.', 'wpv-views'), wpv_maybe_add_query_arg(array('page' => 'view-templates', 'orderby' => $mod_url['orderby'], 'order' => $mod_url['order'], 'items_per_page' => $mod_url['items_per_page'], 'paged' => '1'), admin_url('admin.php')), __('Return', 'wpv-views'));
                ?>
						</p>
					</div>
					<?php 
            }
        } else {
            if ($wpv_args['post_status'] == 'trash') {
                // No items in trash
                ?>
					<div class="wpv-views-listing views-empty-list">
						<p>
							<?php 
                printf('<p>%s</p><p><a class="button-secondary" href="%s">%s</a></p>', __('No Content Templates in trash.', 'wpv-views'), wpv_maybe_add_query_arg(array('page' => 'view-templates', 'orderby' => $mod_url['orderby'], 'order' => $mod_url['order'], 'items_per_page' => $mod_url['items_per_page'], 'paged' => '1'), admin_url('admin.php')), __('Return', 'wpv-views'));
                ?>
						</p>
					</div>
					<?php 
            } else {
                ?>
					<p class="wpv-view-not-exist">
						<?php 
                _e('Content Templates let you design single pages.', 'wpv-views');
                ?>
					</p>
					<p class="add-new-view">
						<button class="button js-add-new-content-template"
								data-target="<?php 
                echo esc_url(add_query_arg(array('action' => 'wpv_ct_create_new'), admin_url('admin-ajax.php')));
                ?>
">
							<i class="icon-plus"></i><?php 
                _e('Add new Content Template', 'wpv-views');
                ?>
						</button>
					</p>
					<?php 
            }
        }
    } else {
        if ($wpv_count_posts != 0) {
            // We have some results to display.
            global $wpdb;
            ?>
			
			<table class="wpv-views-listing widefat">

			<!-- section for: sort by name -->
				<thead>
					<?php 
            /* To avoid code duplication, table header is stored in output buffer and echoed twice - within
             * thead and tfoot tags. */
            ob_start();
            ?>
					<tr>
						<th class="wpv-admin-listing-col-bulkactions check-column">
							<input type="checkbox" />
						</th>
						<?php 
            $column_active = '';
            $column_sort_to = 'ASC';
            $column_sort_now = 'ASC';
            $status = '';
            if ($wpv_args['orderby'] === 'title') {
                $column_active = ' views-list-sort-active';
                $column_sort_to = $wpv_args['order'] === 'ASC' ? 'DESC' : 'ASC';
                $column_sort_now = $wpv_args['order'];
            }
            if (isset($_GET['status']) && $_GET['status'] == 'trash') {
                $status = 'trash';
            }
            ?>
						<th class="wpv-admin-listing-col-title">
							<?php 
            printf('<a href="%s" class="%s" data-orderby="title">%s <i class="%s"></i></a>', wpv_maybe_add_query_arg(array('page' => 'view-templates', 'status' => $status, 'orderby' => 'title', 'order' => $column_sort_to, 's' => $mod_url['s'], 'items_per_page' => $mod_url['items_per_page'], 'paged' => $mod_url['paged']), admin_url('admin.php')), 'js-views-list-sort views-list-sort ' . $column_active, __('Title', 'wpv-views'), 'DESC' === $column_sort_now ? 'icon-sort-by-alphabet-alt' : 'icon-sort-by-alphabet');
            ?>
						</th>
						<th class="wpv-admin-listing-col-usage js-wpv-col-two"><?php 
            _e('Used on', 'wpv-views');
            ?>
</th>
						<?php 
            $column_active = '';
            $column_sort_to = 'DESC';
            $column_sort_now = 'DESC';
            if ($wpv_args['orderby'] === 'date') {
                $column_active = ' views-list-sort-active';
                $column_sort_to = $wpv_args['order'] === 'ASC' ? 'DESC' : 'ASC';
                $column_sort_now = $wpv_args['order'];
            }
            ?>
						<th class="wpv-admin-listing-col-date">
							<?php 
            printf('<a href="%s" class="%s" data-orderby="date">%s <i class="%s"></i></a>', wpv_maybe_add_query_arg(array('page' => 'view-templates', 'status' => $status, 'orderby' => 'date', 'order' => $column_sort_to, 's' => $mod_url['s'], 'items_per_page' => $mod_url['items_per_page'], 'paged' => $mod_url['paged']), admin_url('admin.php')), 'js-views-list-sort views-list-sort ' . $column_active, __('Date', 'wpv-views'), 'DESC' === $column_sort_now ? 'icon-sort-by-attributes-alt' : 'icon-sort-by-attributes');
            ?>
						</th>
					</tr>
					<?php 
            // Get table header from output buffer and stop buffering
            $table_header = ob_get_contents();
            ob_end_clean();
            echo $table_header;
            ?>
				</thead>
				<tfoot>
					<?php 
            echo $table_header;
            ?>
				</tfoot>

				<tbody class="js-wpv-views-listing-body">
					<?php 
            $alternate = '';
            while ($query->have_posts()) {
                $query->the_post();
                $post = get_post(get_the_id());
                $template_id = $post->ID;
                $wpv_content_template_decription = get_post_meta($template_id, '_wpv-content-template-decription', true);
                $layout_loop_template_for_view_id = get_post_meta($template_id, '_view_loop_id', true);
                $alternate = ' alternate' == $alternate ? '' : ' alternate';
                ?>
						<tr id="wpv_ct_list_row_<?php 
                echo $template_id;
                ?>
" class="js-wpv-ct-list-row<?php 
                echo $alternate;
                ?>
">
							<th class="wpv-admin-listing-col-bulkactions check-column">
								<?php 
                if (empty($layout_loop_template_for_view_id)) {
                    printf('<input type="checkbox" value="%s" name="view[]" />', $template_id);
                }
                ?>
							</th>
							<td class="wpv-admin-listing-col-title post-title page-title column-title">
								<span class="row-title">
									<?php 
                if ($wpv_args['post_status'] == 'trash') {
                    echo $post->post_title;
                } else {
                    printf('<a href="%s">%s</a>', esc_url(add_query_arg(array('action' => 'edit', 'post' => $template_id), admin_url('post.php'))), $post->post_title);
                }
                ?>
								</span>
								<?php 
                if (!empty($wpv_content_template_decription)) {
                    ?>
										<p class="desc">
											<?php 
                    echo nl2br($wpv_content_template_decription);
                    ?>
										</p>
										<?php 
                }
                /* Generate and show row actions.
                 * Note that we want to add also 'simple' action names to the action list because
                 * they get echoed as a class of the span tag and get styled from WordPress core css
                 * accordingly (e.g. trash in different colour than the rest) */
                $row_actions = array();
                $asigned_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(post_id) FROM {$wpdb->postmeta} JOIN {$wpdb->posts} p \n\t\t\t\t\t\t\t\t\t\t\tWHERE meta_key = '_views_template' \n\t\t\t\t\t\t\t\t\t\t\tAND meta_value = %s\n\t\t\t\t\t\t\t\t\t\t\tAND post_id = p.ID \n\t\t\t\t\t\t\t\t\t\t\tAND p.post_status NOT IN ('auto-draft') \n\t\t\t\t\t\t\t\t\t\t\tAND p.post_type != 'revision'", $template_id));
                if ('publish' == $wpv_args['post_status']) {
                    $row_actions['edit'] = sprintf('<a href="%s">%s</a>', esc_url(add_query_arg(array('action' => 'edit', 'post' => $template_id), admin_url('post.php'))), __('Edit', 'wpv-views'));
                    /* Note that hash in <a href="#"> is present so the link behaves like a link.
                     * <a href=""> causes problems with colorbox and with mere <a> the mouse cursor
                     * doesn't change when hovering over the link. */
                    if (empty($layout_loop_template_for_view_id)) {
                        $row_actions['change js-list-ct-action-change'] = sprintf('<a href="#">%s</a>', __('Change template usage', 'wpv-views'));
                    }
                    $row_actions['duplicate js-list-ct-action-duplicate'] = sprintf('<a href="#">%s</a>', __('Duplicate', 'wpv-views'));
                    if (empty($layout_loop_template_for_view_id)) {
                        $row_actions['trash js-list-ct-action-trash'] = sprintf('<a href="#">%s</a>', __('Move to trash', 'wpv-views'));
                    }
                } else {
                    if ('trash' == $wpv_args['post_status']) {
                        $row_actions['restore-from-trash js-list-ct-action-restore-from-trash'] = sprintf('<a href="#">%s</a>', __('Restore from trash', 'wpv-views'));
                        $row_actions['delete js-list-ct-action-delete'] = sprintf('<a href="#">%s</a>', __('Delete', 'wpv-views'));
                    }
                }
                echo wpv_admin_table_row_actions($row_actions, array("data-ct-id" => $template_id, "data-postcount" => $asigned_count, "data-ct-name" => htmlentities($post->post_title, ENT_QUOTES), "data-viewactionnonce" => $ct_action_nonce, "data-msg" => htmlentities(__('Enter new title', 'wpv-views'), ENT_QUOTES)));
                ?>
							</td>
							<td class="wpv-admin-listing-col-usage">
								<?php 
                echo wpv_content_template_used_for_list($template_id);
                ?>
							</td>
							<td class="wpv-admin-listing-col-date">
								<?php 
                echo get_the_time(get_option('date_format'), $template_id);
                ?>
							</td>
						</tr>
						<?php 
            }
            ?>
				</tbody>
			</table>

			<div class="tablenav bottom">
				<?php 
            echo wpv_admin_table_bulk_actions($bulk_actions, $bulk_actions_class, $bulk_actions_args, 'bottom');
            ?>
			</div>


			<p class="add-new-view">
				<button class="button js-add-new-content-template"
						data-target="<?php 
            echo esc_url(add_query_arg(array('action' => 'wpv_ct_create_new'), admin_url('admin-ajax.php')));
            ?>
">
					<i class="icon-plus"></i><?php 
            _e('Add new Content Template', 'wpv-views');
            ?>
				</button>
			</p>

			<?php 
        }
    }
    wpv_admin_listing_pagination('view-templates', $wpv_found_posts, $wpv_args["posts_per_page"], $mod_url);
    // Render dialog templates.
    wpv_render_ct_listing_dialog_templates_arrangeby_name();
}