<?php
// +-----------------------------------------------------------------------+
// | Piwigo - a PHP based picture gallery                                  |
// | Autosize                                                              |
// +-----------------------------------------------------------------------+
// | Copyright(C) 2010      cljosse                                        |
// +-----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify  |
// | it under the terms of the GNU General Public License as published by  |
// | the Free Software Foundation                                          |
// |                                                                       |
// | This program is distributed in the hope that it will be useful, but   |
// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
// | General Public License for more details.                              |
// |                                                                       |
// | You should have received a copy of the GNU General Public License     |
// | along with this program; if not, write to the Free Software           |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA.                                                                  |
// +-----------------------------------------------------------------------+
if (!defined('AUTOSIZE_PATH')) 
define(
  'AUTOSIZE_PATH',
   PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/'
);

class autosize_controler {
	/*
	*
	*/
	static public function cl_autosize_affiche()
	{
		global $user, $picture, $template,$page;
		global $content, $element_info;   
		global $infos_message,$erreur_message;
		global  $conf,$lang ,$user,$userdata;

		load_language('lang', AUTOSIZE_PATH);
		$my_path = dirname(__FILE__).'/';

		if (isset( $page['body_id'])  ) {
		if ( $page['body_id']=='thePicturePage'  ) {
			if ( isset($picture['current'])){
				
				$autosize_parametres = cl_autosize_Get_Options();
				include (AUTOSIZE_PATH."include/affiche.php"); 	
				$debug = isset($conf['debug']) ? $conf['debug'] : false ; 
				$cl_visible=isset($conf['cl_auto']) ? $conf['cl_auto'] : 'true' ; 
				
				if($autosize_parametres->check_icon_v == 'on'){
				 
				$template->assign('cl_autosize_button', 
				 array( 
						'cl_autosize_info' => 'cl_autosize_info' ,
						'cl_autosize_info_2' => 'cl_autosize_info_2' ,    
						'URL' => $_SERVER['REQUEST_URI'] ,
						'ICON2' => AUTOSIZE_PATH . 'icons/button-maximize.png',
						'ICON' => AUTOSIZE_PATH . 'icons/button-minimize.png'
					)

					);
 
				$template->set_filenames(array('cl_bp' => $my_path. 'template/picture.tpl'));
				$template->concat('PLUGIN_PICTURE_ACTIONS', $template->parse('cl_bp', true));
				}
				 

				$template->set_filenames(
						array('autosize_content'=> $my_path.'template/autosize.tpl')
					);
                   $template->assign(
  						array('DEBUG' => $debug   ,
						'cl_visible' =>  $cl_visible
						)
						 );		
						 			
				return $template->parse( 'autosize_content', false);
			}
      
		}
		}
	} //public function cl_autosize_affiche
	/*
	*
	*/
	static public function cl_autosize_admin($menu)
		{
		global  $lang ;
		array_push($menu, array('NAME' => 'Autosize',
		'URL' => get_admin_plugin_menu_link(AUTOSIZE_PATH . 'admin/admin_autosize.php')));
		return $menu;
	} //function cl_autosize_admin
	/*
	*
	*/
	static public function cl_autosize_aff_infos_plus()
		{
		 global $template,$infos_message,$erreur_message, $user ;
		global  $conf,$lang ;

		 //==============================================================
		  if (isset($erreur_message))
				{	
				if ($erreur_message <> "")
					{
						$erreur_message=str_replace("\n",'<br />',$erreur_message) ;
   						$template->assign('errors',$erreur_message);
						$erreur_message="";
					}
  				}
		  if (isset($infos_message))
				{	
		
				if ($infos_message <> "")
					{
						$infos_message=str_replace("\n",'<br />',$infos_message) ;
   						$template->assign('infos',$infos_message);
						$infos_message="";
					}
  				}
				//=============================================================
		return;		 
		
		} // function cl_autosize_aff_infos_plus



 /*
 * conflit =============================================
 */
	static public function cl_autosize_script()
	{
	   global  $template,$user,$conf ;
 
		 

	   	$my_path =  basename(dirname(__FILE__)).'/' ;
	$my_path = dirname(__FILE__).'/';
		$template->set_filenames(
		     array('autosize_init'=>$my_path. "template/conflit.tpl"  
			 )
		      );		 
  		$template->assign(
  						array('AUTOSIZE_ROOT' => AUTOSIZE_PATH)
						 );

		   return $template->parse( 'autosize_init', false);

       
	}// function cl_autosize_script
	/*
	*
	*/
	static public function cl_ajuste_data($content, $image){
 
	   global  $template,$user,$conf ;
	   		$my_path = realpath(AUTOSIZE_PATH .'/');
			
			$template->set_filenames(
		     array('autosize_cl_init'=> $my_path.'template/conflit.tpl')
		      );
 /*
	 $userdata=$user;
	 $fields = array( 'maxwidth', 'maxheight' );
	 $ThePicture = $template->get_template_vars('current'); 

			$data = array();
			$data['maxheight'] = $ThePicture['scaled_height'];
			$data['maxwidth'] = $ThePicture['scaled_width'];
			$data['user_id'] = $userdata['id'];
	   foreach ($fields as $field)
      {
        if (isset($_POST[$field]))
        {
          $data[$field] = $_POST[$field];
        }
      }
	
	if($data['maxwidth'] != "" )
{
        
       mass_updates(USER_INFOS_TABLE,
                   array('primary' => array('user_id'), 'update' => $fields),
                   array($data));
	
}

   */

 $template->assign(
  						array(
						'AUTOSIZE_PATH' => $my_path,

											)
						 );
 
		$template->parse( 'autosize_cl_init', false);
	return $content;


										 
	 }

} // class


	/*
	*
	*/
 function cl_autosize_Get_Options()
  {
		global $conf,$autosize_parametres; 
  		$autosize_parametres =  unserialize($conf['cl_autosize']);
		$autosize_parametres =  cl_autosize_Set_Options();
		return $autosize_parametres;
  }
	/*
	*
	*/
function cl_autosize_Set_Options()
  {

  global $autosize_parametres;
//=============================================================================
$autosize_parametres->webmaster_height =(isset($_POST['webmaster_height'])) ? $_POST['webmaster_height'] : ((isset($autosize_parametres->webmaster_height )) ? $autosize_parametres->webmaster_height : '100%' ) ;
$autosize_parametres->webmaster_width=(isset($_POST['webmaster_width']))? $_POST['webmaster_width']: ((isset($autosize_parametres->webmaster_width)) ? $autosize_parametres->webmaster_width:  '90%') ;

$autosize_parametres->admin_height =(isset($_POST['admin_height'])) ? $_POST['admin_height'] : ((isset($autosize_parametres->admin_height )) ? $autosize_parametres->admin_height : '90%' ) ;
$autosize_parametres->admin_width=(isset($_POST['admin_width']))? $_POST['admin_width']: ((isset($autosize_parametres->admin_width)) ? $autosize_parametres->admin_width:  '90%') ;

$autosize_parametres->generic_height =(isset($_POST['generic_height'])) ? $_POST['generic_height'] : ((isset($autosize_parametres->generic_height )) ? $autosize_parametres->generic_height : '100%' ) ;
$autosize_parametres->generic_width=(isset($_POST['generic_width']))? $_POST['generic_width']: ((isset($autosize_parametres->generic_width)) ? $autosize_parametres->generic_width:  '90%') ;

$autosize_parametres->guest_height =(isset($_POST['guest_height'])) ? $_POST['guest_height'] : ((isset($autosize_parametres->guest_height )) ? $autosize_parametres->guest_height : '100%' ) ;
$autosize_parametres->guest_width=(isset($_POST['guest_width']))? $_POST['guest_width']: ((isset($autosize_parametres->guest_width)) ? $autosize_parametres->guest_width:  '90%') ;

$autosize_parametres->normal_height =(isset($_POST['normal_height'])) ? $_POST['normal_height'] : ((isset($autosize_parametres->normal_height )) ? $autosize_parametres->normal_height : '100%' ) ;
$autosize_parametres->normal_width=(isset($_POST['normal_width']))? $_POST['normal_width']: ((isset($autosize_parametres->normal_width)) ? $autosize_parametres->normal_width:  '90%') ;


$autosize_parametres->mini_height =(isset($_POST['mini_height'])) ? $_POST['mini_height'] : ((isset($autosize_parametres->mini_height )) ? $autosize_parametres->mini_height : '150' ) ;
$autosize_parametres->mini_width=(isset($_POST['mini_width']))? $_POST['mini_width']: ((isset($autosize_parametres->mini_width)) ? $autosize_parametres->mini_width:  '300') ;

$autosize_parametres->echelle_max=(isset($_POST['echelle_max']))? $_POST['echelle_max'] : ((isset($autosize_parametres->echelle_max)) ? $autosize_parametres->echelle_max :  '1.2'); 
$autosize_parametres->marge_basse=(isset($_POST['marge_basse']))? $_POST['marge_basse']: ((isset($autosize_parametres->marge_basse)) ? $autosize_parametres->marge_basse :  '50');
//===============================================================================	

if ( isset($_POST['submit'] ) && $_POST['submit'] == l10n('cl_autosize_save') )  {  
		$autosize_parametres->check_desc_v = isset($_POST['check_desc_v']) ? $_POST['check_desc_v'] : "off" ;
		$autosize_parametres->check_icon_v = isset($_POST['check_icon_v']) ? $_POST['check_icon_v'] : "off" ;
 		$autosize_parametres->check_auto_w = isset($_POST['check_auto_w']) ? $_POST['check_auto_w'] : "off" ;

		$autosize_parametres->webmaster_enabled = isset($_POST['webmaster_enabled']) ? $_POST['webmaster_enabled'] : "off" ;
		$autosize_parametres->admin_enabled = isset($_POST['admin_enabled']) ? $_POST['admin_enabled'] : "off" ;
		$autosize_parametres->generic_enabled = isset($_POST['generic_enabled']) ? $_POST['generic_enabled'] : "off" ;
		$autosize_parametres->guest_enabled = isset($_POST['guest_enabled']) ? $_POST['guest_enabled'] : "off" ;
		$autosize_parametres->normal_enabled = isset($_POST['normal_enabled']) ? $_POST['normal_enabled'] : "off" ;

	 }else{
		$autosize_parametres->check_desc_v = isset($_POST['check_desc_v']) ? $_POST['check_desc_v'] : ( ( isset($autosize_parametres->check_desc_v) ) ? $autosize_parametres->check_desc_v :   'off') ;  
		$autosize_parametres->check_icon_v = isset($_POST['check_icon_v']) ? $_POST['check_icon_v'] : ( ( isset($autosize_parametres->check_icon_v) ) ? $autosize_parametres->check_icon_v :   'off') ;  
		$autosize_parametres->check_auto_w = isset($_POST['check_auto_w']) ? $_POST['check_auto_w'] : ( ( isset($autosize_parametres->check_auto_w) ) ? $autosize_parametres->check_auto_w :   'off') ;  


$autosize_parametres->webmaster_enabled = isset($_POST['webmaster_enabled']) ? $_POST['webmaster_enabled'] : ( ( isset($autosize_parametres->webmaster_enabled) ) ? $autosize_parametres->webmaster_enabled :   'on') ;  
$autosize_parametres->admin_enabled  = isset($_POST['admin_enabled ']) ? $_POST['admin_enabled '] : ( ( isset($autosize_parametres->admin_enabled ) ) ? $autosize_parametres->admin_enabled :   'on') ;  
$autosize_parametres->generic_enabled = isset($_POST['generic_enabled']) ? $_POST['generic_enabled'] : ( ( isset($autosize_parametres->generic_enabled) ) ? $autosize_parametres->generic_enabled :   'on') ;  
$autosize_parametres->guest_enabled = isset($_POST['guest_enabled']) ? $_POST['guest_enabled'] : ( ( isset($autosize_parametres->guest_enabled) ) ? $autosize_parametres->guest_enabled :   'on') ;
$autosize_parametres->normal_enabled = isset($_POST['normal_enabled']) ? $_POST['normal_enabled'] : ( ( isset($autosize_parametres->normal_enabled) ) ? $autosize_parametres->normal_enabled :   'on') ;  


   }

return $autosize_parametres;
}
	/*
	*
	*/
function cl_autosize_sauve_options_inf()
{
global $options,$infos_message,$conf,$autosize_parametres  ;
$infos_message .=l10n("cl_autosize_save_config")."<br>";
 $autosize_parametres=cl_autosize_Set_Options();

 if ( isset($autosize_parametres) )
 	{
 
		$query = '
    		UPDATE '.CONFIG_TABLE.'
    		SET value="'.addslashes(serialize($autosize_parametres)).'"
    		WHERE param = "cl_autosize"
    		LIMIT 1';
 			pwg_query($query);
	  }
	 
 }



?>