<?php 
defined('ABSPATH') or die('No direct access permitted');
$collectionBlock["export"] = "exportCollectionBlock"; 

class exportCollectionBlock extends collectionBlock 
{

	protected $blockname = "export"; 
	protected $fields = array( ); 

	protected $socialdata = array(); 
	

	function export() 
	{
		$collection_id = $this->collection->getID(); 
		$socialdata = $this->collection->getBlock("social");
		$this->socialdata = $socialdata->get(); // PHP 5.3 fix
		$picker = $this->collection->getBlock('picker'); 
		$buttons = $picker->get();  // PHP 5.3 fix
		$buttons = $buttons["selection"]; 
		
		$bbasic = $this->collection->getBlock('basic'); 
		
		$cname = $bbasic->get(); // PHP 5.3 fix
		$cname = $cname["name"];
		
		add_filter("maxbutton_export_button", array($this,'export_button'),10,2); 
		$packs = MB()->getClass('pack'); 
 		$args = array(
 			"pack_name" => $cname,
 			"buttons" =>  $buttons,
 			"output_type" => 'ziplocation', 
			);
 		$ziplocation =  $packs->export_pack($args); 
 		$result = array(
 			"error" => false,
 			"result" => true, 
 			"data" => array("location" => $ziplocation), 
 		); 
		return $result; 
		
	}
	
	function export_button($xml, $button_id) 
	{
		$data = $this->socialdata; 
		//print_R($this->socialdata);
		$add_xml = ''; 
		
		//if (isset($data["social-option"][$button_id])) 
		foreach($data["social-option"] as $index => $data)
		{
			foreach($data as $this_button_id => $button_data)
			{
				if ($this_button_id == $button_id)
				{
					$add_xml .= "<collection>"; 
						$add_xml .= "<social>"; 
							foreach($button_data as $field => $value) 
							{
								$add_xml .= "<$field>$value</$field>"; 
			
							}
						$add_xml .= "</social>"; 
					$add_xml .= "</collection>"; 
				}	
			}
		}
		
		$xml .= $add_xml; 
		return $xml; 
	
	}


	function admin_fields()
	{
		//if (! defined('MAXBUTTONS_DEBUG') || ! MAXBUTTONS_DEBUG)
		if (! isset($_GET["mbexport"])) 
	 		return; //off 
?>
	
	<div class="mb_tab option-container export-block"> 
		<div class="title">
						<button type="button" id="exportCollection" class="button"><?php _e("Export","maxbuttons"); ?></button>
			<span class="dashicons dashicons-admin-appearance"></span>		
			<span class="title"><?php _e("Export","maxbuttons"); ?></span>
 
		</div>
			
 
	
	</div>
	
	
<?php 	
	
	}


}?> 
