<?php
/**
 * @version    $Id$
 * @package    IT_Theme_Framework
 * @author     InnoThemes Team <support@innothemes.com>
 * @copyright  Copyright (C) 2012 InnoThemes.com. All Rights Reserved.
 * @license    GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
 *
 * Websites: http://www.innothemes.com
 */

/**
 * Social channels field renderer.
 *
 * @package  IT_Theme_Framework
 * @since    1.0.0
 */
class IT_Form_Field_Social_Channels extends IT_Form_Field {
	/**
	 * Field type.
	 *
	 * @var  string
	 */
	protected $type = 'social-channels';

	/**
	 * Typography options.
	 *
	 * @var  array
	 */
	protected $choices = array(
		'facebook',
		'twitter',
		'youtube',
		'googleplus',
		'linkedin',
		'pinterest',
		'myspace',
		'flickr',
		'dribble',
		'stumbleupon',
		'yahoo',
		'skype',
		'rss',
	);

	/**
	 * Supported social channels.
	 *
	 * @var  array
	 */
	protected $social_channels = array(
		'facebook' => array(
			'label'       => 'Facebook',
			'placeholder' => 'http://www.facebook.com/your_user_name',
			'link'        => '',
		),
		'twitter' => array(
			'label'       => 'Twitter',
			'placeholder' => 'http://www.twitter.com/your_user_name',
			'link'        => '',
		),
		'youtube' => array(
			'label'       => 'YouTube',
			'placeholder' => 'http://www.youtube.com/your_user_name',
			'link'        => '',
		),
		'googleplus' => array(
			'label'       => 'Google+',
			'placeholder' => 'http://plus.google.com/your_user_id',
			'link'        => '',
		),
		'linkedin' => array(
			'label'       => 'LinkedIn',
			'placeholder' => 'http://www.linkedin.com/in/your_name',
			'link'        => '',
		),
		'pinterest' => array(
			'label'       => 'Pinterest',
			'placeholder' => 'http://www.pinterest.com/your_user_name',
			'link'        => '',
		),
		'myspace' => array(
			'label'       => 'MySpace',
			'placeholder' => 'http://www.myspace.com/your_user_name',
			'link'        => '',
		),
		'flickr' => array(
			'label'       => 'Flickr',
			'placeholder' => 'http://www.flickr.com/people/your_user_name',
			'link'        => '',
		),
		'dribble' => array(
			'label'       => 'Dribbble',
			'placeholder' => 'http://www.dribbble.com/your_user_name',
			'link'        => '',
		),
		'stumbleupon' => array(
			'label'       => 'StumbleUpon',
			'placeholder' => 'http://your_user_name.stumbleupon.com',
			'link'        => '',
		),
		'yahoo' => array(
			'label'       => 'Yahoo',
			'placeholder' => 'ymsgr:sendim?your_user_name',
			'link'        => '',
		),
		'skype' => array(
			'label'       => 'Skype',
			'placeholder' => 'skype:your_user_name?call',
			'link'        => '',
		),
		'rss' => array(
			'label'       => 'RSS',
			'placeholder' => 'http://your_joomla_site_url/index.php?format=feed&amp;type=rss',
			'link'        => '',
		)
	);

	/**
	 * Constructor.
	 *
	 * @param   array  $config  Field declaration.
	 * @param   array  $merge   Array of property should be merged.
	 *
	 * @return  void
	 */
	public function __construct( $config, $merge = array( 'attributes', 'social_channels' ) ) {
		parent::__construct( $config, $merge );
	}

	/**
	 * Get selected social icons.
	 *
	 * @param   array    &$configured_social_channels  Configured social channels.
	 * @param   integer  &$num_configured              The number of configured social channels.
	 *
	 * @return  void
	 */
	protected function configure_social_channels( &$configured_social_channels, &$num_configured ) {
		// Get saved social channels configuration
		$saved_social_channels = get_theme_mod( 'it_social_channels', array() );

		// Filter available choices
		$this->choices = apply_filters( 'it_form_field_choices_support', $this->choices );

		// Filter supported social channels
		$this->social_channels = apply_filters( 'it_form_field_social_channels_support', $this->social_channels );

		// Prepare configured social channels
		$configured_social_channels = array();

		if ( ! empty( $this->value ) ) {
			foreach ( $this->value as $name => $status ) {
				if ( count( $saved_social_channels ) && isset( $saved_social_channels[$name] ) && ! empty( $saved_social_channels[$name] ) ) {
					$configured_social_channels[ $name ] = $saved_social_channels[ $name ];
				} elseif ( isset( $this->social_channels[ $name ]['link'] ) && ! empty( $this->social_channels[ $name ]['link'] ) ) {
					$configured_social_channels[ $name ] = $this->social_channels[ $name ]['link'];
				}
			}
		}

		foreach ( $this->choices as $name ) {
			$data = isset( $this->social_channels[ $name ] ) ? $this->social_channels[ $name ] : array();

			if ( ! isset( $this->value[ $name ] ) ) {
				if ( count( $saved_social_channels ) && isset( $saved_social_channels[ $name ] ) && ! empty( $saved_social_channels[$name] ) ) {
					$configured_social_channels[ $name ] = $saved_social_channels[ $name ];
				} elseif ( ! empty( $data['link'] ) ) {
					$configured_social_channels[ $name ] = $data['link'];
				}
			}
		}

		$num_configured = count( $configured_social_channels );
	}
}
