<?php
/**
 * Post Grid Widget
 *
 * @package Hootkit
 */

// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
	die;
}

/**
 * Class HootKit_Post_Grid_Widget
 */
class HootKit_Post_Grid_Widget extends HK_Widget {

	function __construct() {

		$id = 'post-grid';
		$settings['id'] = "hootkit-posts-grid";
		$settings['name'] = hootkit()->get_string( $id );
		$settings['widget_options'] = array(
			'description'	=> __( 'Display Posts in a Grid', 'hootkit' ),
		);
		$settings['control_options'] = array();
		$settings['form_options'] = array(
			'description' => array(
				// 'name'		=> __( '', 'hootkit' ),
				'type'		=> __( "<strong>Only posts which have a 'Featured Image' will be displayed</strong>", 'hootkit' ),
			),
			'title' => array(
				'name'		=> __( 'Title (Optional)', 'hootkit' ),
				'type'		=> 'text',
			),
			'subtitle' => array(
				'name'		=> __( 'Sub Title (optional)', 'hootkit' ),
				'type'		=> 'text',
			),
			'columns' => array(
				'name'		=> __( 'Number Of Columns', 'hootkit' ),
				'type'		=> 'smallselect',
				'std'		=> '4',
				'options'	=> array(
					'1'  => __( '1', 'hootkit' ),
					'2'  => __( '2', 'hootkit' ),
					'3'  => __( '3', 'hootkit' ),
					'4'  => __( '4', 'hootkit' ),
					'5'  => __( '5', 'hootkit' ),
					'6'  => __( '6', 'hootkit' ),
					'7'  => __( '7', 'hootkit' ),
					'8'  => __( '8', 'hootkit' ),
					'9'  => __( '9', 'hootkit' ),
					'10' => __( '10', 'hootkit' ),
					'11' => __( '11', 'hootkit' ),
					'12' => __( '12', 'hootkit' ),
				),
			),
			'rows' => array(
				'name'		=> __( 'Number of Rows', 'hootkit' ),
				'type'		=> 'smallselect',
				'std'		=> '3',
				'options'	=> array(
					'1'  => __( '1', 'hootkit' ),
					'2'  => __( '2', 'hootkit' ),
					'3'  => __( '3', 'hootkit' ),
					'4'  => __( '4', 'hootkit' ),
					'5'  => __( '5', 'hootkit' ),
					'6'  => __( '6', 'hootkit' ),
					'7'  => __( '7', 'hootkit' ),
					'8'  => __( '8', 'hootkit' ),
					'9'  => __( '9', 'hootkit' ),
					'10' => __( '10', 'hootkit' ),
					'11' => __( '11', 'hootkit' ),
					'12' => __( '12', 'hootkit' ),
					'13' => __( '13', 'hootkit' ),
					'14' => __( '14', 'hootkit' ),
					'15' => __( '15', 'hootkit' ),
					'16' => __( '16', 'hootkit' ),
					'17' => __( '17', 'hootkit' ),
					'18' => __( '18', 'hootkit' ),
					'19' => __( '19', 'hootkit' ),
					'20' => __( '20', 'hootkit' ),
					'21' => __( '21', 'hootkit' ),
					'22' => __( '22', 'hootkit' ),
					'23' => __( '23', 'hootkit' ),
					'24' => __( '24', 'hootkit' ),
					'25' => __( '25', 'hootkit' ),
					'26' => __( '26', 'hootkit' ),
					'27' => __( '27', 'hootkit' ),
					'28' => __( '28', 'hootkit' ),
					'29' => __( '29', 'hootkit' ),
					'30' => __( '30', 'hootkit' ),
				),
			),
			'offset' => array(
				'name'		=> __( 'Offset', 'hootkit' ),
				'desc'		=> __( 'Number of posts to skip from the start. Leave empty to start from the latest post.', 'hootkit' ),
				'type'		=> 'text',
				'settings'	=> array( 'size' => 3, ),
				'sanitize'	=> 'absint',
			),
			'category' => array(
				'name'		=> __( 'Category (Optional)', 'hootkit' ),
				'desc'		=> __( 'Only include posts from these categories. Leave empty to display posts from ALL categories.', 'hootkit' ),
				'type'		=> 'multiselect',
				'options'	=> (array)Hoot_List::categories(0),
			),
			'exccategory' => array(
				'name'		=> __( 'Exclude Category (Optional)', 'hootkit' ),
				'desc'		=> __( 'Exclude posts from these categories.', 'hootkit' ),
				'type'		=> 'multiselect',
				'options'	=> (array)Hoot_List::categories(0),
			),
			'viewall' => array(
				'name'		=> __( "'View All Posts' link", 'hootkit' ),
				'desc'		=> __( 'Links to your Blog page. If you have a Category selected below, then this will link to the Category Archive page.', 'hootkit' ),
				'type'		=> 'select',
				'std'		=> 'none',
				'options'	=> array(
					'none'		=> __( 'Do not display', 'hootkit' ),
					'top'		=> __( 'Show at Top', 'hootkit' ),
					'bottom'	=> __( 'Show at Bottom', 'hootkit' ),
				),
			),
			'unitheight' => array(
				'name'		=> __( 'Grid Unit (Post Image) Size', 'hootkit' ),
				'desc'		=> __( 'Default: 215 (in pixels)', 'hootkit' ),
				'type'		=> 'text',
				'settings'	=> array( 'size' => 3, ),
				'sanitize'	=> 'absint',
			),
			'content_bg' => array(
				'name'		=> __( 'Text Styling', 'hootkit' ),
				'type'		=> 'select',
				'std'		=> 'light',
				'options'	=> array(
					'dark'			=> __( 'Dark Font', 'hootkit' ),
					'light'			=> __( 'Light Font', 'hootkit' ),
					'dark-on-light'	=> __( 'Dark Font / Light Background', 'hootkit' ),
					'light-on-dark'	=> __( 'Light Font / Dark Background', 'hootkit' ),
				),
			),
			'show_title' => array(
				'name'		=> __( 'Display Post Titles', 'hootkit' ),
				'type'		=> 'checkbox',
				'std'		=> 1,
			),
			'seppost' => array(
				'name'		=> __( 'Individual Posts:', 'hootkit' ),
				// 'desc'		=> __( 'INDIVIDUAL POSTS', 'hootkit' ),
				'type'		=> 'separator',
			),
			'show_author' => array(
				'name'		=> __( 'Show Author', 'hootkit' ),
				'type'		=> 'checkbox',
				'boxdivi'	=> 'div-ve0',
			),
			'show_date' => array(
				'name'		=> __( 'Show Post Date', 'hootkit' ),
				'type'		=> 'checkbox',
				'boxdivi'	=> 'div-ve0',
			),
			'show_comments' => array(
				'name'		=> __( 'Show number of comments', 'hootkit' ),
				'type'		=> 'checkbox',
				'boxdivi'	=> 'div-ve0',
			),
			'show_cats' => array(
				'name'		=> __( 'Show Categories', 'hootkit' ),
				'type'		=> 'checkbox',
				'boxdivi'	=> 'div-ve0',
			),
			'show_tags' => array(
				'name'		=> __( 'Show Tags', 'hootkit' ),
				'type'		=> 'checkbox',
				'boxdivi'	=> 'div-ve0',
			),
			'firstpost' => array(
				'name'		=> __( 'Big Grid Unit', 'hootkit' ),
				'type'		=> 'collapse',
				'settings'	=> array( 'state' => 'open' ),
				'fields'	=> array(
					'rowsize' => array(
						'mergefieldbelow' => true,
						'name'		=> __( 'Big Grid Unit Size', 'hootkit' ),
						'type'		=> 'smallselect',
						'smallselectlabel' => __( 'Rows', 'hootkit' ),
						'std'		=> '2',
						'options'	=> array(
							'1'	=> __( '1', 'hootkit' ),
							'2'	=> __( '2', 'hootkit' ),
							'3'	=> __( '3', 'hootkit' ),
							'4'	=> __( '4', 'hootkit' ),
							'5'	=> __( '5', 'hootkit' ),
							'6'	=> __( '6', 'hootkit' ),
							'7'	=> __( '7', 'hootkit' ),
						),
					),
					'colsize' => array(
						'mergefieldtop'   => true,
						'type'		=> 'smallselect',
						'smallselectlabel' => __( 'Columns', 'hootkit' ),
						'desc'		=> __( 'Set this to 1x1 to make it the same size as rest of the grid units.', 'hootkit' ),
						'std'		=> '2',
						'options'	=> array(
							'1'	=> __( '1', 'hootkit' ),
							'2'	=> __( '2', 'hootkit' ),
							'3'	=> __( '3', 'hootkit' ),
							'4'	=> __( '4', 'hootkit' ),
							'5'	=> __( '5', 'hootkit' ),
							'6'	=> __( '6', 'hootkit' ),
							'7'	=> __( '7', 'hootkit' ),
						),
					),
					'align' => array(
						'name'		=> __( 'Big Unit Location', 'hootkit' ),
						'type'		=> 'smallselect',
						'std'		=> 'left',
						'options'	=> array(
							'left'		=> __( 'Left', 'hootkit' ),
							'center'	=> __( 'Center', 'hootkit' ),
							'right'		=> __( 'Right', 'hootkit' ),
						),
					),
					'author' => array(
						'name'		=> __( 'Show Author', 'hootkit' ),
						'type'		=> 'checkbox',
						'std'		=> 1,
						'boxdivi'	=> 'div-ve0',
					),
					'date' => array(
						'name'		=> __( 'Show Post Date', 'hootkit' ),
						'type'		=> 'checkbox',
						'std'		=> 1,
						'boxdivi'	=> 'div-ve0',
					),
					'comments' => array(
						'name'		=> __( 'Show number of comments', 'hootkit' ),
						'type'		=> 'checkbox',
						'boxdivi'	=> 'div-ve0',
					),
					'cats' => array(
						'name'		=> __( 'Show Categories', 'hootkit' ),
						'type'		=> 'checkbox',
						'boxdivi'	=> 'div-ve0',
					),
					'tags' => array(
						'name'		=> __( 'Show Tags', 'hootkit' ),
						'type'		=> 'checkbox',
						'boxdivi'	=> 'div-ve0',
					),
					'count' => array(
						'name'		=> __( 'Number of Posts', 'hootkit' ),
						'desc'		=> __( 'Selecting more than 1 post will <strong>convert the first grid into a SLIDER</strong>', 'hootkit' ),
						'type'		=> 'smallselect',
						'std'		=> '1',
						'options'	=> array(
							'1'	=> __( '1', 'hootkit' ),
							'2'	=> __( '2', 'hootkit' ),
							'3'	=> __( '3', 'hootkit' ),
							'4'	=> __( '4', 'hootkit' ),
							'5'	=> __( '5', 'hootkit' ),
							'6'	=> __( '6', 'hootkit' ),
							'7'	=> __( '7', 'hootkit' ),
							'8'	=> __( '8', 'hootkit' ),
							'9'	=> __( '9', 'hootkit' ),
							'10'=> __( '10', 'hootkit' ),
						),
					),
					'fix' => array(
						'type'		=> '<input type="hidden" name="%name%" id="%id%" value="na" class="%class%">',
						// Bugfix: This field is added since all the fields in collapsible are checkboxes. So when all checkbox are unchecked, value for "widget-hoot-post-grid-widget[N][firstpost]" in the instance is returned as false by the browsers instead of an array with all emements = 0 (empty string value is ok, but we still add a dummy value)
					),
				),
			),
			'sepcss' => array(
				'type'		=> 'separator',
			),
			'customcss' => array(
				'name'		=> __( 'Widget Options', 'hootkit' ),
				'type'		=> 'collapse',
				'fields'	=> array(
					'class' => array(
						'name'		=> __( 'Custom CSS Class', 'hootkit' ),
						'desc'		=> __( 'Give this widget a custom css classname', 'hootkit' ),
						'type'		=> 'text',
					),
					'mt' => array(
						'name'		=> __( 'Margin Top', 'hootkit' ),
						'desc'		=> __( '(in pixels) Leave empty to load default margins.<br>Hint: You can use negative numbers also.', 'hootkit' ),
						'type'		=> 'text',
						'settings'	=> array( 'size' => 3 ),
						'sanitize'	=> 'integer',
					),
					'mb' => array(
						'name'		=> __( 'Margin Bottom', 'hootkit' ),
						'desc'		=> __( '(in pixels) Leave empty to load default margins.<br>Hint: You can use negative numbers also.', 'hootkit' ),
						'type'		=> 'text',
						'settings'	=> array( 'size' => 3 ),
						'sanitize'	=> 'integer',
					),
				),
			),
		);

		if ( ! hootkit()->supports( 'widget-subtitle' ) ) {
			unset( $settings['form_options']['subtitle'] );
		}

		$settings = apply_filters( 'hootkit_post_grid_widget_settings', $settings );

		parent::__construct( $settings['id'], $settings['name'], $settings['widget_options'], $settings['control_options'], $settings['form_options'] );

	}

	/**
	 * Display the widget content
	 */
	function display_widget( $instance, $before_title = '', $title = '', $after_title = '' ) {
		// Allow theme/child-themes to use their own template
		$widget_template = hoot_get_widget( 'post-grid', false );
		// Use Hootkit template if theme does not have one
		$widget_template = apply_filters( 'hootkit_widget_template', $widget_template, 'post-grid' );
		$default = hootkit()->dir . 'widgets-v3/post-grid/view.php';
		$widget_template = ( $widget_template ) ? $widget_template : $default;
		// Option to overwrite variables to keep html tags in title later sanitized during display => skips 'widget_title' filter (esc_html hooked) action on title; (Possibly redundant as html is sanitized in title during save)
		if ( apply_filters( 'hootkit_display_widget_extract_overwrite', false, 'post-grid' ) ) extract( $instance, EXTR_OVERWRITE ); else extract( $instance, EXTR_SKIP );
		// Fire up the template
		if ( is_string( $widget_template ) && file_exists( $widget_template ) ) include ( $widget_template );
	}

}

/**
 * Register Widget
 */
function hootkit_post_grid_widget_register(){
	register_widget( 'HootKit_Post_Grid_Widget' );
}
add_action( 'widgets_init', 'hootkit_post_grid_widget_register' );