<?php

class Intense_Content_Box extends Intense_Shortcode {

	function __construct() {
        parent::__construct();

		global $intense_visions_options;

		$this->title = __( 'Content Box', 'intense' );
		$this->description = __( 'The content box shortcode gives extra interest to what it is you’re wanting to say with the placement of an icon in one of many different locations. Icons can be placed in one of the four corners, in the center on the top or bottom, or inside of the box (next to the title). You also get the option to include a link, with whatever text you want.', 'intense' );
		$this->category = __( 'Layout', 'intense' );
		$this->show_preview = true;
		$this->preview_content = __( "Content Box Text", 'intense' );
		$this->is_container = true;
	}

	function shortcode( $atts, $content = null ) {
		global $intense_visions_options;
		extract( shortcode_atts( $this->get_shortcode_defaults( null, $atts ), $atts ) );

		if ( is_numeric( $border_size ) && $border_size != 0 ) $border_size .= "px";

		$border_color = intense_get_plugin_color( $border_color );

		if ( isset( $background ) ) {
			if ( in_array( $background, array( "warning", "error", "success", "info", "inverse", "muted", "primary" ) ) ) {
				$background_color_style = "background-color: " . intense_get_plugin_color( $background ) . " !important;";
				$backcolor = intense_get_plugin_color( $background );
			} else {
				$background_color_style = "background-color: " . $background . " !important;";
				$backcolor = $background;
			}
		} else {
			$background_color_style = "background-color: " . intense_get_plugin_color( "boxed" ) . " !important;";
		}

		if ( isset( $background_opacity ) && strlen( $backcolor ) > 0 ) {
			$hex = str_replace( "#", "", $backcolor );
			$a = $background_opacity / 100;

			if ( strlen( $hex ) == 3 ) {
				$r = hexdec( substr( $hex, 0, 1 ).substr( $hex, 0, 1 ) );
				$g = hexdec( substr( $hex, 1, 1 ).substr( $hex, 1, 1 ) );
				$b = hexdec( substr( $hex, 2, 1 ).substr( $hex, 2, 1 ) );
			} else {
				$r = hexdec( substr( $hex, 0, 2 ) );
				$g = hexdec( substr( $hex, 2, 2 ) );
				$b = hexdec( substr( $hex, 4, 2 ) );
			}

			$background_color_style = "background-color: rgba(" . $r . "," . $g . "," . $b . "," . $a . ") !important;";
		}

		$radius = '';
		if ( isset( $border_radius ) ) {
			$radius = 'border-radius:' . $border_radius . ';';
		}

		if ( is_numeric( $margin_top ) ) $margin_top .= "px";
		if ( is_numeric( $margin_bottom ) ) $margin_bottom .= "px";
		if ( is_numeric( $margin_left ) ) $margin_left .= "px";
		if ( is_numeric( $margin_right ) ) $margin_right .= "px";

		if ( is_numeric( $padding_top ) ) $padding_top .= "px";
		if ( is_numeric( $padding_bottom ) ) $padding_bottom .= "px";
		if ( is_numeric( $padding_left ) ) $padding_left .= "px";
		if ( is_numeric( $padding_right ) ) $padding_right .= "px";

		$padding = ( !empty( $padding_top ) ? ' padding-top: ' . $padding_top . '; ' : '' ) .
			( !empty( $padding_bottom ) ? ' padding-bottom: ' . $padding_bottom . '; ' : '' ) .
			( !empty( $padding_left ) ? ' padding-left: ' . $padding_left . '; ' : '' ) .
			( !empty( $padding_right ) ? ' padding-right: ' . $padding_right . '; ' : '' );

		$margin = ( !empty( $margin_top ) ? ' margin-top: ' . $margin_top . '; ' : '' ) .
			( !empty( $margin_bottom ) ? ' margin-bottom: ' . $margin_bottom . '; ' : '' ) .
			( !empty( $margin_left ) ? ' margin-left: ' . $margin_left . '; ' : '' ) .
			( !empty( $margin_right ) ? ' margin-right: ' . $margin_right . '; ' : '' );

		intense_add_script( 'intense.contentbox' );

		if ( !empty( $animation ) ) {
			intense_load_animation_CSS( $animation );			
			intense_add_script( 'intense.animated' );
		}

		if ( is_numeric( $custom_icon ) ) {
			$photo = Intense_Photo_Source::get_wordpress_photo( $custom_icon, $size );
			$custom_icon = $photo['url'];
		}

		$icon_shortcode = intense_run_shortcode( 'intense_icon', array( 'source' => $icon_source, 'type' => $icon, 'stack_source' => $icon_stack_source, 'stack_type' => $icon_stack_type, 'color' => $icon_color, 'stack_color' => $icon_stack_color, 'size' => $size ) );

		$id = ( !empty ( $id ) ) ? ' id="' . $id . '"' : '';

		$shadow_style = '';
		$output = '';
		$iconwidth = '';
		$output .= '<div' . $id . ' class="intense content-box ' . $position . ' ' . ( !$boxed ? "unboxed " : "boxed " ) . ( is_rtl() ? 'rtl ' : '' ) . ( !empty( $class ) ? $class . ' ' : '' ) .  '" ' . ( isset( $animation ) ? "data-animation='$animation'" : "" ) . ' style="' . ( !empty( $width ) ? 'width:' . $width . '; ' : 'width:100%; ' ) . $margin . $padding . '">';
		
		if ( $size == '2' || $size == '3' ) {
			$iconwidth = 'width: 80px;';
		}

		if ( $size == '4' ) {
			$iconwidth = 'width: 130px;';
		}

		if ( $size == '5' ) {
			$iconwidth = 'width: 150px;';
		}


		if ( in_array( $position, array( "topcenter", "topleft", "topright" ) ) ) {
			$output .= '  <div class="content-box-icon" style="' . $iconwidth . '">';

			if ( isset( $custom_icon ) ) {
				$output .= "<img style='width:100%;' src='" . $custom_icon . "' alt='' />";
			} else {
				$output .= $icon_shortcode;
			}

			$output .= '  </div>';
		}

		if ( $boxed ) {
			$style = $background_color_style . ' border:' . $border_size . ' ' . $border_style . ' ' . $border_color . '; ' . $radius . ';';
		} else {
			$style = '';
		}

		$output .= "<div class='content-box-text-wrapper'>";

		if ( isset( $shadow ) && $shadow != 0 && !in_array( $position, array( "bottomcenter", "bottomleft", "bottomright" ) ) ) {
			$output .= "<div style='$shadow_style'>";
		}

		$output .= '  <div class="content-box-text" style="' . $style . '">';

		if ( $position == "insidetop" ) {
			$output .= "<div class='content-box-icon' style='" . $iconwidth . "'>";

			if ( isset( $custom_icon ) ) {
				$output .= "<img style='width:100%;' src='" . $custom_icon . "' alt='' />";
			} else {
				$output .= $icon_shortcode;
			}

			$output .= "</div>";
		}

		if ( isset( $title ) ) {
			if ( isset( $custom_icon ) ) {
				$icon = "<img style='width:50px;' src='" . $custom_icon . "' alt='' />";
			} else {
				$icon = $icon_shortcode;
			}
			$output .= '    <div class="content-box-title"><h2>' . ( $position == "inside" ? $icon . " <span class='content-box-title-text'>" : "" ) . $title . ( $position == "inside" ? " </span>" : "" ) . '</h2></div>';
		}

		$output .= '    <div class="content-box-excerpt">' . do_shortcode( $content ) . '</div>';

		if ( !empty( $link ) ) {
			if ( !empty( $link_rel ) ) $link_rel = ' rel="' . $link_rel . '"';

			if ( is_numeric( $link ) ) {
				$link = get_permalink( $link );
			}
			
			$output .= '    <div class="pull-right content-box-link"><a' . $link_rel . ' href="' . $link . '" target="' . $link_target . '">' . $link_text . '</a></div><div class="clearfix"></div>';
		}

		if ( $position == "insidebottom" ) {
			$output .= "<div class='content-box-icon' style='" . $iconwidth . "'>";

			if ( isset( $custom_icon ) ) {
				$output .= "<img style='width:100%;' src='" . $custom_icon . "' alt='' />";
			} else {
				$output .= $icon_shortcode;
			}

			$output .= "</div>";
		}

		$output .= '  </div>';

		if ( isset( $shadow ) && $shadow != 0 && !in_array( $position, array( "bottomcenter", "bottomleft", "bottomright" ) ) ) {
			$output .= "</div><img src='" . INTENSE_PLUGIN_URL . "/assets/img/shadow{$shadow}.png' class='intense shadow' style='vertical-align: top; border:0px; border-radius: 0px; box-shadow: 0 0 0;' />";
		}

		$output .= "</div>";

		if ( in_array( $position, array( "bottomcenter", "bottomleft", "bottomright" ) ) ) {
			$output .= '  <div class="content-box-icon" style="' . $iconwidth . '">';

			if ( isset( $custom_icon ) ) {
				$output .= "<img style='width:100%;' src='" . $custom_icon . "' alt='' />";
			} else {
				$output .= $icon_shortcode;
			}

			$output .= '  </div>';
		}

		$output .= '</div>';

		return $output;
	}	
}
