<?php  
class MB {
           
  	static function init(){  
        if (isset($_POST['Mb_reset'])){ Mb::initOptions(true); }
        $aOptions = MB::initOptions();

        if(isset($_POST['Mb_txt_accueil'])){
          $aOptions = Mb::initOptions();
          $aOptions['home-title'] = stripslashes($_POST['home-title']);
          $aOptions['home-desc'] = stripslashes($_POST['home-desc']);
	        update_option('Mb_theme', $aOptions);
          header('Location:'.get_bloginfo('url').'/wp-admin/themes.php?page=control.php');    
        } 

        if(isset($_POST['Mb_color'])){
          $aOptions = Mb::initOptions();
          $aOptions['color'] = stripslashes($_POST['color']);
          update_option('Mb_theme', $aOptions);
          header('Location:'.get_bloginfo('url').'/wp-admin/themes.php?page=control.php');    
        } 
                                    
    		if(isset($_POST['Mb_upload'])){
          
          $whitelist = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/ico', 'image/x-png');
			
    			if ($_FILES['attachment_image']['type']!='') { 
    				$up_file = 'image'; 
    			}
			    
            $filetype = $_FILES['attachment_' . $up_file]['type'];

            if (in_array($filetype, $whitelist)) {            
              $upload=wp_handle_upload($_FILES['attachment_'.$up_file], array('test_form' => false));
      			  $aOptions[$up_file] = stripslashes($upload['url']);  
              
      			  update_option('Mb_theme', $aOptions);   
            }   
            header('Location:'.get_bloginfo('url').'/wp-admin/themes.php?page=control.php');       
	     	}
  		add_theme_page("The mobility options", 'Options du thème', 'edit_themes', 'control.php', array('MB','displayOptions'));
  	}
    static function initOptions($reset=false){
        $aOptions = get_option('Mb_theme');
        	if ((!is_array($aOptions))||($reset==true)){
        		$aOptions['image']=get_template_directory_uri().'/images/themobility/image-banniere.jpg';  
            $aOptions['color']='#d1174c';
            
            $aOptions['home-title'] ="mobility";	
            $aOptions['home-desc'] ="<p>Et quoniam mirari posse quosdam peregrinos existimo haec lecturos forsitan, si contigerit, quamobrem cum oratio ad ea monstranda deflexerit quae Romae gererentur, nihil praeter seditiones narratur et tabernas et vilitates harum similis alias, summatim causas perstringam nusquam a veritate sponte propria digressurus.</p>
            <br />
            <p>Sed fruatur sane hoc solacio atque hanc insignem ignominiam, quoniam uni praeter se inusta sit, putet esse leviorem, dum modo, cuius exemplo se consolatur, eius exitum expectet, praesertim cum in Albucio nec Pisonis libidines nec audacia Gabini fuerit ac tamen hac una plaga conciderit, ignominia senatus.</p>";	
            $aOptions['home-image'] = get_template_directory_uri().'/images/themobility/welcome.jpg'; 
                                    
        	  update_option('Mb_theme', $aOptions);	
        	}
    return $aOptions;
    }                  
    static function displayOptions(){
          $aOptions = MB::initOptions();
    ?>
       		<div class="wrap">
            <h2>Options du thème</h2>
            <form action="#" method="post" enctype="multipart/form-data">
               <input type="submit" class="button"  name="Mb_reset" value="Réinitialisation" />
			         <br />
               
                <div class="bloc">                   		
                <h3>Image bannière</h3>
                   <h4>Aperçu :</h4> 
                   <div id="apercuimage">  
                      <img src="<?php echo($aOptions['image']); ?>" /> 
                   </div>                                                             
                   <h4>Téléchargez une image bannière de 728X90 pixels :</h4>
                      <div id='imgupload'>
                        <?php echo MB::get_upload_field('image'); ?>
              		    </div>
                </div> 
                
                <div class="bloc">    
                <h3>Couleur du pied de page et de la ligne de l'entête :</h3>
                    <h4>Aperçu :</h4>
                    <div id="apercucolor" style="background:<?php echo($aOptions['color']); ?>"></div>
                    <h4>Choisissez votre couleur :</h4>
                    <input type="text" name="color" id='color-picker' value="<?php echo($aOptions['color']); ?>" />
                
                <span class="submit"><input name="Mb_color" type="submit" value="Enregistrer" class="button panel-upload-save" /></span>
                </div> 
                
                   <div class="bloc2">                     
                    <h3>Ajouter une présentation :</h3>
                    <div id="homeleft">      
                      <h4>titre :</h4>           
                      <input  type="text" style="width:80%" name="hometitle" id="hometitle" value="<?php echo($aOptions['home-title']); ?>"/>
                      <h4>texte :</h4>                   
                  		<div class="txtmce">
                          <?php    
                          $settings=array(
                              'media_buttons' => false,
                              'textarea_rows' => '10',
                              'tinymce' => array(
                                  'toolbar1' => 'bold, italic, underline, strikethrough, forecolor,bullist, numlist,link, unlink',
                                  'toolbar2' => ''
                                  ),
                              'quicktags' => array('buttons' => 'strong,em,link,block,del,ins,ul,ol,li,code,close')
                              );
                           wp_editor(stripslashes($aOptions['home-desc']),'home-desc', $settings);    
                           ?>
                      </div><br />                
                      <span class="submit"><input name="Mb_txt_accueil" type="submit" value="Enregistrer" class="button panel-upload-save" /></span>
                    </div>
                    <div id="homeright"> 
                        <h4>Aperçu :</h4>                                
                        <div id="apercuhomeimage"> 
                        <img src="<?php echo($aOptions['home-image']); ?>" />
                        </div>                               
                        <h4>Téléchargez une image de 440x320 pixels :</h4>
                        <div id='imgupload'>
                          <?php echo MB::get_upload_field('home-image'); ?>
                      </div> 
                    </div>                 
                  </div> 
                       
            </form>	       
          </div>
    <?php
    }
    static function addStyleColor(){
      $aOptions = MB::initOptions();
      $output="
              <style type=\"text/css\">  
                #site-footer{background: ".$aOptions['color'].";}
                #site-header{border-bottom: 1px solid ".$aOptions['color'].";}
              </style>                
              ";

      echo $output;   
    }   
    static function addAdminHeader(){ 
                   
        wp_register_style('Mb_css',get_template_directory_uri().'/inc/css/control.css',array());               
        wp_enqueue_style('Mb_css');
                         
        wp_register_script('Mb_js',get_template_directory_uri().'/inc/js/control.js',array());               
        wp_enqueue_script('Mb_js'); 
        
        wp_enqueue_style('iris');
        wp_enqueue_script('iris');                 
    }
    static function get_upload_field($id) {
        $field = '<input id="'.$id.'" type="file" name="attachment_'.$id.'" />
                  <span class="submit"><input name="Mb_upload" type="submit" value="Envoyer" class="button panel-upload-save" /></span>';
        return $field;
    }     
}//Fin de la classe MB  
   
 if(class_exists("MB")){        
    $inst_MB = new MB();   
 }  
    
 if(isset($inst_MB)){
        
   if((isset($_GET['page']))&&($_GET['page']=='control.php')){           
      add_action('admin_init',array($inst_MB,'addAdminHeader'));     
   }
   
   add_action( 'wp_head', array($inst_MB,'addStyleColor'));        
   add_action('admin_menu', array($inst_MB,'init')); 
   
 }   
?>