<?php
/**
 * Font Icon
 * 
 * Shortcode which displays an icon with optional hover effect
 */
// Don't load directly
if ( ! defined( 'ABSPATH' ) ) { die('-1'); }

if ( ! class_exists( 'av_font_icon' ) )
{
    class av_font_icon extends aviaShortcodeTemplate
    {
        /**
         * Create the config array for the shortcode button
         */
        function shortcode_insert_button()
        {
			$this->config['version']		= '1.0';
			$this->config['self_closing']	= 'no';
			
            $this->config['name']       = __( 'Icon', 'avia_framework' );
			$this->config['tab']		= __( 'Content Elements', 'avia_framework' );
			$this->config['icon']		= AviaBuilder::$path['imagesURL'] . 'sc-icon.png';
            $this->config['order']      = 90;
            $this->config['shortcode']  = 'av_font_icon';
            $this->config['tooltip'] 	    = __( 'Display an icon with optional hover effect', 'avia_framework' );
			$this->config['target']		= 'avia-target-insert';
            //$this->config['inline']   = true;
            $this->config['tinyMCE']    = array( 'tiny_always' => true );
			$this->config['preview'] 	= 1;
			$this->config['disabling_allowed'] = true;
			$this->config['id_name']	= 'id';
			$this->config['id_show']	= 'yes';
        }
        
        function extra_assets()
		{
			wp_enqueue_style( 'avia-module-icon' , AviaBuilder::$path['pluginUrlRoot'] . 'avia-shortcodes/icon/icon.css' , array('avia-layout'), false );
		}

        /**
         * Popup Elements
         *
         * If this function is defined in a child class the element automatically gets an edit button, that, when pressed
         * opens a modal window that allows to edit the element properties
         *
         * @return void
         */
        function popup_elements()
        {

            $this->elements = array(
				
				array(
						'type' 	=> 'tab_container', 
						'nodescription' => true
					),
						
				array(
						'type' 	=> 'tab',
						'name'  => __( 'Content', 'avia_framework' ),
						'nodescription' => true
					),
				
					array(
							'type'			=> 'template',
							'template_id'	=> $this->popup_key( 'content_iconfont' )
						),
				
					array(
							'type'			=> 'template',
							'template_id'	=> $this->popup_key( 'content_tooltip' )
						),
				
				array(
						'type' 	=> 'tab_close',
						'nodescription' => true
					),
				
				array(
						'type' 	=> 'tab',
						'name'  => __( 'Styling', 'avia_framework' ),
						'nodescription' => true
					),
				
					array(
							'type'			=> 'template',
							'template_id'	=> 'toggle_container',
							'templates_include'	=> array( 
													$this->popup_key( 'styling_nettings' ),
													$this->popup_key( 'styling_color' ),
												),
							'nodescription' => true
						),
				
				array(
						'type' 	=> 'tab_close',
						'nodescription' => true
					),
				
				array(
						'type' 	=> 'tab',
						'name'  => __( 'Advanced', 'avia_framework' ),
						'nodescription' => true
					),
				
					array(
							'type' 	=> 'toggle_container',
							'nodescription' => true
						),
				
						array(
								'type'			=> 'template',
								'template_id'	=> $this->popup_key( 'advanced_link' ),
								'nodescription' => true
							),
				
						array(
								'type'			=> 'template',
								'template_id'	=> 'toggle',
								'title'			=> __( 'Animation' , 'avia_framework' ),
								'content'		=> $this->popup_templates['advanced_animation'],
								'nodescription' => true
							),
				
						array(	
								'type'			=> 'template',
								'template_id'	=> 'screen_options_toggle'
							),
				
						array(	
								'type'			=> 'template',
								'template_id'	=> 'developer_options_toggle',
								'args'			=> array( 'sc' => $this )
							),
				
					array(
							'type' 	=> 'toggle_container_close',
							'nodescription' => true
						),
				
				array(
						'type' 	=> 'tab_close',
						'nodescription' => true
					),

				array(
						'type' 	=> 'tab_container_close',
						'nodescription' => true
					)
					
				);
        }
		
		/**
		 * Create and register templates for easier maintainance
		 * 
		 * @since 4.6.4
		 */
		protected function register_dynamic_templates()
		{
			/**
			 * Content Tab
			 * ===========
			 */
			
			$c = array(
						array(
							'name'  => __( 'Font Icon', 'avia_framework' ),
							'desc'  => __( 'Select an Icon below', 'avia_framework' ),
							'id'    => 'icon',
							'type'  => 'iconfont',
							'std'   => ''
						),
				);
			
			AviaPopupTemplates()->register_dynamic_template( $this->popup_key( 'content_iconfont' ), $c );
			
			
			$c = array(
						array(
							'name' 	=> __( 'Optional Tooltip', 'avia_framework' ),
							'desc' 	=> __( 'Add a tooltip for this Icon. The tooltip will appear on mouse over', 'avia_framework' )
											. '<br/><small>' 
											. __( 'Please note: Images within the tooltip are currently not supported', 'avia_framework' ) 
											. '</small>',
							'id' 	=> 'content',
							'type' 	=> 'textarea',
							'std' 	=> ''
						)
				);
			
			AviaPopupTemplates()->register_dynamic_template( $this->popup_key( 'content_tooltip' ), $c );
			
			
			/**
			 * Styling Tab
			 * ===========
			 */
			
			$c = array(
						array(
							'name'	=> __( 'Icon Style', 'avia_framework' ),
							'desc'	=> __( 'Here you can set the style of the icon. Either display it inline as part of some text or let it stand alone with border and optional caption', 'avia_framework' ),
							'id'	=> 'style',
							'type'	=> 'select',
							'std'	=> '',
							'subtype'	=> array(
										__( 'Default inline style', 'avia_framework' )	=> '',
										__( 'Standalone Icon with border and optional caption', 'avia_framework' )	=> 'border',
									)
						),	
				
						array(
							'name'  => __( 'Icon Caption', 'avia_framework' ),
							'desc'  => __( 'A small caption below the icon', 'avia_framework' ),
							'id'    => 'caption',
							'type' 	=> 'input',
							'std' 	=> '',
							'required' 	=> array( 'style', 'not', '' )
						),	
				
						array(
							'name'  => __( 'Icon Size', 'avia_framework' ),
							'desc'  => __( 'Enter the font size in px, em or &percnt;', 'avia_framework' ),
							'id'    => 'size',
							'type'  => 'input',
							'std'	=> '40px'
						),
                    
						array(	
							'name' 	=> __( 'Icon Position', 'avia_framework' ),
							'desc' 	=> __( 'Choose the alignment of your icon here', 'avia_framework' ),
							'id' 	=> 'position',
							'type' 	=> 'select',
							'std' 	=> 'left',
							'subtype' => array(
											__( 'Align Left', 'avia_framework' )	=> 'left',
											__( 'Align Center', 'avia_framework' )	=> 'center',
											__( 'Align Right', 'avia_framework' )	=> 'right',
										)
						),
				);
			
			$template = array(
							array(	
								'type'			=> 'template',
								'template_id'	=> 'toggle',
								'title'			=> __( 'General Styling', 'avia_framework' ),
								'content'		=> $c 
							),
					);
			
			AviaPopupTemplates()->register_dynamic_template( $this->popup_key( 'styling_nettings' ), $template );
			
			
			$c = array(
						array(
							'name'  => __( 'Icon Color', 'avia_framework' ),
							'desc'  => __( 'Here you can set the  color of the icon. Enter no value if you want to use the standard font color.', 'avia_framework' ),
							'id'    => 'color',
							'rgba' 	=> true,
							'type'  => 'colorpicker'
						),	
				);
			
			$template = array(
							array(	
								'type'			=> 'template',
								'template_id'	=> 'toggle',
								'title'			=> __( 'Colors', 'avia_framework' ),
								'content'		=> $c 
							),
					);
			
			AviaPopupTemplates()->register_dynamic_template( $this->popup_key( 'styling_color' ), $template );
			
			/**
			 * Advanced Tab
			 * ============
			 */
			
			$c = array(
						array(
							'name' 	=> __( 'Animation', 'avia_framework' ),
							'desc' 	=> __( 'Should the icons appear in an animated way?', 'avia_framework' ),
							'id' 	=> 'animation',
							'type' 	=> 'select',
							'std' 	=> '',
							'subtype' => array(
											__( 'Animation activated', 'avia_framework' )	=> '',
											__( 'Animation deactivated', 'avia_framework' )	=> 'deactivated',
										)
						),
				);
			
			$this->popup_templates['advanced_animation'] = $c;
			
			$c = array(
						array(	
							'type'			=> 'template',
							'template_id'	=> 'linkpicker_toggle',
							'name'			=> __( 'Title Link?', 'avia_framework' ),
							'desc'			=> __( 'Where should your title link to?', 'avia_framework' ),
							'subtypes'		=> array( 'no', 'manually', 'single', 'taxonomy' ),
						)
				
				);
			
			
			AviaPopupTemplates()->register_dynamic_template( $this->popup_key( 'advanced_link' ), $c );

		}


		/**
			 * Editor Element - this function defines the visual appearance of an element on the AviaBuilder Canvas
			 * Most common usage is to define some markup in the $params['innerHtml'] which is then inserted into the drag and drop container
			 * Less often used: $params['data'] to add data attributes, $params['class'] to modify the className
			 *
			 *
			 * @param array $params this array holds the default values for $content and $args.
			 * @return $params the return array usually holds an innerHtml key that holds item specific markup.
			 */
			function editor_element( $params )
			{
				extract( av_backend_icon( $params ) ); // creates $font and $display_char if the icon was passed as param 'icon' and the font as 'font' 

				extract( shortcode_atts( array(
								'color'		=> '',
								'size'		=> '',
								'style'		=> '',
								'caption'	=> '',
								'use_link'	=> 'no',
								'position'	=> 'left',
								'link'		=>'',
								'linktarget'	=> 'no',
								'custom_class'	=> '',
							), $params['args'], $this->config['shortcode'] ) );
				
			
				$inner  = "<div class='avia_icon_element avia_textblock avia_textblock_style'>";
				$inner .=		'<div ' . $this->class_by_arguments( 'position', $params['args'] ) . '>';
				$inner .=			'<div ' . $this->class_by_arguments( 'style', $params['args'] ) . '>';
				$inner .=				'<span ' . $this->class_by_arguments( 'font', $font ) . '>';
				$inner .=					"<span data-update_with='icon_fakeArg' class='avia_icon_char'>{$display_char}</span>";
				$inner .=				'</span>';
				$inner .=				"<div class='avia_icon_content_wrap'>";
				$inner .=					"<h4  class='av_icon_caption' data-update_with='caption'>" . html_entity_decode( $caption ) . "</h4>";
				$inner .=				'</div>';
				$inner .=			'</div>';
				$inner .=		'</div>';
				$inner .= '</div>';

				$params['innerHtml'] = $inner;
				$params['class'] = '';

				return $params;
			}


        /**
         * Frontend Shortcode Handler
         *
         * @param array $atts array of attributes
         * @param string $content text within enclosing form of shortcode element
         * @param string $shortcodename the shortcode found, when == callback name
         * @return string $output returns the modified html string
         */
        function shortcode_handler( $atts, $content = '', $shortcodename = '', $meta = '' )
        {
            //this is a fix that solves the false paragraph removal by wordpress if the dropcaps shortcode is used at the beginning of the content of single posts/pages
            global $post, $avia_add_p;

            $add_p = '';
            $custom_class = !empty($meta['custom_class']) ? $meta['custom_class'] : '';
            if(isset($post->post_content) && strpos($post->post_content, '[av_font_icon') === 0 && $avia_add_p == false && is_singular())
            {
                $add_p = '<p>';
                $avia_add_p = true;
            }
            
            extract( AviaHelper::av_mobile_sizes( $atts ) ); //return $av_font_classes, $av_title_font_classes and $av_display_classes 

            extract( shortcode_atts( array(
						'icon'			=> '',
						'font'			=> '',
						'color'			=> '',
						'size'			=> '',
						'style'			=> '',
						'caption'		=> '',
						'use_link'		=> 'no',
						'position'		=> 'left',
						'animation'		=> '',
						'link'			=> '',
						'linktarget'	=> 'no',
						'font'			=> ''
					), $atts, $this->config['shortcode'] ) );

            $char = av_icon( $icon, $font );

            $color = ! empty( $color ) ? "color:{$color}; border-color:{$color};" : '';
			
			if( empty( $color ) ) 
			{
				$custom_class .= ' av-no-color';
			}
			
            if( ! empty( $size ) && is_numeric( $size ) ) 
			{
				$size .= 'px';
			}
			
            $size_string = ! empty( $size ) ? "font-size:{$size};line-height:{$size};" : '';
			
			if( ! empty( $style ) )
			{
				$size_string   .= "width:{$size};";
				if(!empty($caption)) $caption = "<span class='av_icon_caption av-special-font'>{$caption}</span>";
			}
			else
			{
				$caption = '';
			}

            // animation
            $animation_class = '';
            if( $animation == '' )
			{
                $animation_class = ' avia-icon-animate ';
            }

            $blank = (strpos($linktarget, '_blank') !== false || $linktarget == 'yes') ? ' target="_blank" ' : '';
            $blank .= strpos($linktarget, 'nofollow') !== false ? ' rel="nofollow" ' : '';
           
            $link = aviaHelper::get_url($link);
            
            $tags = !empty($link) ? array("a href='{$link}' {$blank} ",'a') : array('span','span');
            
            $tooltip = empty($content) ? '' : 'data-avia-icon-tooltip="'.htmlspecialchars(do_shortcode($content)).'"';
            
            $display_char = "<{$tags[0]} class='av-icon-char' style='{$size_string}' {$char} {$tooltip}></{$tags[1]}>";
            
            $output = '<span ' . $meta['custom_el_id'] . ' class="'.$shortcodename.' avia_animate_when_visible'.$animation_class.$av_display_classes.' av-icon-style-'.$style.' '.$custom_class.' avia-icon-pos-'.$position.' " style="'.$color.'">'.$display_char.$caption.'</span>';

            return $output;
        }

    }
}
