<?php
/**
 * Your Inspiration Themes
 * 
 * @package WordPress
 * @subpackage Your Inspiration Themes
 * @author Your Inspiration Themes Team <info@yithemes.com>
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE (GPL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-3.0.txt
 */

/**
 * Extends classic WP_Nav_Menu_Edit
 * 
 * @since 1.0.0
 */

class YIT_Walker_Nav_Menu extends Walker_Nav_Menu
{
	protected $_customFields = array(); 
	
	public function __construct() {
		$this->_customFields = yit_get_model('nav_menu')->fields;
	}
	
    function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output )
    {
        $id_field = $this->db_fields['id'];
        if ( is_object( $args[0] ) ) {
            $args[0]->children_number = !empty( $children_elements[$element->$id_field] ) ? count($children_elements[$element->$id_field]) : 0;
        }
        return parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
    }
	
	function start_el(&$output, $item, $depth = 0, $args = array(), $current_object_id = 0)
    {
    	global $wp_query;
        $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

		$this->_loadCustomFields( $item );

        $class_names = $value = '';
		$children_number = isset($args->children_number) ? $args->children_number : '0';

        $classes = empty( $item->classes ) ? array() : (array) $item->classes;

        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
		$class_names .= ' menu-item-children-' . $children_number;
		if($depth == 1 && $this->_isCustomItem( $item ) ) {
			$class_names .= ' menu-item-custom-content';
		}
        $class_names = ' class="'. esc_attr( $class_names ) . '"';

        $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';

        $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
        $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
        $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
        $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';

        $prepend = '';
        $append = '';
        $description  = ''; //! empty( $item->description ) ? '<span>'.esc_attr( $item->description ).'</span>' : '';

        $item_output = $args->before;
		
        if($depth == 1 && $this->_isCustomItem( $item ) ) {

            $item_out = "";

            $item_out .= '<a'. $attributes .'>';
            $item_out .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
            $item_out .= $args->link_after;
            $item_out .= '</a>';

            $item_output .= apply_filters( 'yit_walker_nav_menu_title' , $item_out);

			foreach( $this->_customFields as $id=>$field ) {
				if( !empty( $item->{$id} ) ) {
					$item_output .= $this->getOutput( $item, $id, $field );
				}
			}

		} else {
	        $item_output .= '<a'. $attributes .'>';
	        $item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
	        $item_output .= $args->link_after;
	        $item_output .= '</a>';			
		}

        $item_output .= $args->after;

        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
	}

	protected function _loadCustomFields( &$item ) {
		foreach( $this->_customFields as $field=>$data ) {
			$item->{$field} = get_post_meta( $item->ID, '_menu_item_' . $field, true ) ? get_post_meta( $item->ID, '_menu_item_' . $field, true ) : '';
		}
	}

	protected function _isCustomItem( $item ) {
		foreach( $this->_customFields as $field=>$data ) {
			if( !empty( $item->{$field} ) ) {
				return true;
			}
		}

		return false;
	}
	
	protected function _parseString( $string ) {
		$string = str_replace( array( '[', ']' ), array( '<span class="highlight">', '</span>' ), $string );
		return nl2br($string);
	}
	
	public function getOutput( $item, $fieldId, $field ) {
		if( $field['type'] == 'text' ) {
			return "<span class='custom-item-{$item->ID} custom-item-{$fieldId} custom-item-text'>" . $item->{$fieldId} . '</span>';
		} elseif( $field['type'] == 'textarea' ) {
			return "<p class='custom-item-{$item->ID} custom-item-{$fieldId} custom-item-textarea'>" . $this->_parseString($item->{$fieldId}) . '</p>';
		} elseif( $field['type'] == 'upload' ) {
			
			$img_url = $item->{$fieldId};
			
			$img_array = yit_getimagesize( $img_url );
			$img_size = ( $img_array != NULL ) ? $img_array[3] : "";
			
			return "<a class='custom-item-{$item->ID} custom-item-{$fieldId} custom-item-image' href='". esc_attr( $item->url ) ."'><img src='". $img_url ."' alt='". apply_filters( 'the_title', $item->title, $item->ID ) ."' " . $img_size . " /></a>";
		}
	}
}
