<?php
require_once 'rover-templates.php';

class Rover_IDX_ADMINSOCIAL
	{
	private Rover_IDX $rover_idx;

	public function __construct(Rover_IDX $rover_idx)
		{
		$this->rover_idx						= $rover_idx;
		}

	public function roveridx_social_panel_form() {

		global									$wpdb;

		$posts									= $wpdb->get_results( "SELECT * FROM $wpdb->posts AS p
														LEFT JOIN $wpdb->term_relationships as r ON p.ID = r.object_ID
														LEFT JOIN $wpdb->term_taxonomy as tax ON r.term_taxonomy_id = tax.term_taxonomy_id
														LEFT JOIN $wpdb->terms as terms ON tax.term_id = terms.term_id
														WHERE	p.post_type = 'post'
														AND	p.post_status = 'publish'
														AND	p.ID = r.object_id
														AND terms.name = 'Rover IDX Property'" );

		$settings								= get_option(roverOPTIONS::SOCIAL);

		$post_to_wp_comments					= (isset($settings['post_to_wp_comments']))
														? $settings['post_to_wp_comments']
														: false;

		$email_on_error							= (isset($settings['email_on_error']))
														? $settings['email_on_error']
														: false;

		$email_on_post							= (isset($settings['email_on_post']))
														? $settings['email_on_post']
														: false;

		# Get the ID of a given category
		$category_id							= get_cat_ID( 'Rover IDX Property' );

		# Get the URL of this category
		$category_link							= get_category_link( $category_id );

		$settings['control_post_to_wp_as_user']	= $this->rover_wp_user_select($settings, 'post_to_wp_as_user');
		$settings['wp_post_count']				= count($posts);
		$settings['wp_posts_url']				= $category_link;

		$the_content							= $this->rover_idx->CONTENT->rover_content(
																	'ROVER_COMPONENT_SOCIAL_PANEL',
																	array(
																		'region'	=> $this->rover_idx->get_first_region(),
																		'regions'	=> implode(',', array_keys($this->rover_idx->all_selected_regions)),
																		'settings'	=> $settings
																		)
																	);
		$TEMPLATES								= new Rover_IDX_ADMINTEMPLATES($this->rover_idx);
		?>

		<div id="wp_defaults" class="wrap" data-page="rover-panel-social">

		<?php echo $TEMPLATES->roveridx_panel_header();	?>

			<div id="rover-social-panel">

				<?php
				echo $the_content['the_html'];
				?>

				<p class="submit">
					<span id="jq_msg"></span>
				</p>

			</div>

		<?php echo $TEMPLATES->roveridx_panel_footer($panel = 'social');	?>

		</div>

	<?php

		}


	private function rover_wp_user_select($settings, $key)
		{
		global 				$wpdb;

		$the_html		= array();

		$authors		= $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users ORDER BY display_name");
		foreach($authors as $author)
			{
			$selected	= (count($authors) == 1)
								? 'selected=selected'
								: $this->rover_idx->COMMON->settings_val_is_selected($settings, $key, $author->ID);

			$the_html[]	=	"<option value='".$author->ID."' ".$selected."> ".$author->user_nicename."</option>";
			}

		return implode('', $the_html);
		}
	}


?>