<?

/* ------------------------------------------------------------------- */
/*

*************************************************
*			UWAGA! SKRYPT NIE JEST DARMOWY.			*
*		DALSZE ROZPOWSZECHNIANIE BEZ ZGODY 			*
*			FIRMY ArtGlobal JEST ZABRONIONE.			*
*************************************************

*************************************************
*	WWW.ArtGlobal.PL		PRODUCENT OPROGRAMOWANIA	*
*	NAZWA SKRYPTU:		NIERUCHOMOSCI 6.0			*
*	WERSJA FINALNA:	04.10.2010						*
*	KONTAKT:				kontakt@artglobal.pl			*
*************************************************

Copyright (c) 2010 ArtGlobal. Wszelkie prawa zastrzeżone.

*/
/* ------------------------------------------------------------------- */

$smarty -> assign('ob', '');

$id = $_GET['id'];
$smarty -> assign('id', $id);

if ( isset( $_POST['edit'] ) )
{
        if ( !empty( $_POST['a'] ) )
        {
                $lpa = 0;
                foreach ( $_POST['a'] as $showA )
                {
                        $lpa++;
                        if ( $lpa == 1 )
                        {
                                $a = $showA;
                        }
                        else
                        {
                                $a .= '-' . $showA;
                        }
                }
        }
        else
        {
                $a = '';
        }
        if ( !empty( $_POST['b'] ) )
        {
                $lpb = 0;
                foreach ( $_POST['b'] as $showB )
                {
                        $lpb++;
                        if ( $lpb == 1 )
                        {
                                $b = $showB;
                        }
                        else
                        {
                                $b .= '-' . $showB;
                        }
                }
        }
        else
        {
                $b = '';
        }
        if ( !empty( $_POST['c'] ) )
        {
                $lpc = 0;
                foreach ( $_POST['c'] as $showC )
                {
                        $lpc++;
                        if ( $lpc == 1 )
                        {
                                $c = $showC;
                        }
                        else
                        {
                                $c .= '-' . $showC;
                        }
                }
        }
        else
        {
                $c = '';
        }
        
        if (  empty( $_POST['priceOn'] )  )
        {
                $smarty -> assign('error', 'Pola oznaczone \'*\' są obowiązkowe!');
        }
        else
        {
                $sql = "UPDATE `object` SET `obTypeId` = '".$_POST['type']."', `obStateId` = '".$_POST['state']."', `obName` = '".$_POST['name']."', `obAdress` = '".$_POST['adress']."', `obCity` = '".$_POST['city']."', `obTel` = '".$_POST['tel']."', `obEmail` = '".$_POST['email']."', `obWww` = '".str_replace(array('http://', 'www.'), '', $_POST['www'])."', `obPriceOn` = '".$_POST['priceOn']."', `obTeren` = '".$a."', `obRoom` = '".$b."', `obNearby` = '".$c."', `obRegion` = '".$_POST['region']."', `obDroga` = '".$_POST['droga']."', `obTxt` = '".$_POST['txt']."', `obBudulec` = '".$_POST['budulec']."', `obDach` = '".$_POST['dach']."',   `obDojazd` = '".$_POST['dojazd']."', `obKanalizacja` = '".$_POST['kanalizacja']."', `obKat` = '".$_POST['kategoria']."', `obOgrodzenie` = '".$_POST['ogrodzenie']."', `obStan` = '".$_POST['stan']."', `obOgrzewanie` = '".$_POST['ogrzewanie']."', `obOkna` = '".$_POST['okna']."',  `obRynek` = '".$_POST['rynek']."',  `obRodzaj` = '".$_POST['rodzaj']."',  `obRok` = '".$_POST['rok']."',   `obPowM` = '".$_POST['pom']."', `obPoC` = '".$_POST['poc']."', `obPietra` = '".$_POST['pietra']."', `obPietro` = '".$_POST['pietro']."', `obPokoje` = '".$_POST['pokoje']."', `obPowD` = '".$_POST['pow']."', `obDzielnica` = '".$_POST['dzielnica']."' WHERE obId = '".$id."'";



                mysql_query ( $sql );

                if ( !empty( $_POST['delete'] ) )
                {
                        foreach ( $_POST['delete'] as $photoId )
                        {
                                $sql = "SELECT * FROM photos WHERE photoId = '$photoId'";
                                $q = mysql_query( $sql );
                                while( $db = mysql_fetch_array( $q ) )
                                {
                       				    @unlink('../files/photo/mini/' . $db['photoUrl']);
						     @unlink('../files/photo/sre/' . $db['photoUrl']);
						    @unlink('../files/photo/duz/' . $db['photoUrl']);
                       		@unlink('../files/photo/big/' . $db['photoUrl']);
						    @unlink('../files/photo/top/' . $db['photoUrl']);
                                }
                                $sql = "DELETE FROM `photos` WHERE photoId = '$photoId'";
                                mysql_query( $sql );
                        }
                }

                $smarty -> assign('success', 'Poprawnie zapisano zmiany!');
        }
}
if ( isset( $_POST['new_photo'] ) )
{
        function cropImage ( $nw, $nh, $source, $stype, $dest )
        {
                $size = getimagesize( $source );
                $w = $size[0];
                $h = $size[1];
                switch( $stype )
                {
                        case 'gif':
                                $simg = imagecreatefromgif( $source );
                        break;
                        case 'GIF':
                                $simg = imagecreatefromgif( $source );
                        break;
                        case 'jpg':
                                $simg = imagecreatefromjpeg( $source );
                        break;
                        case 'JPG':
                                $simg = imagecreatefromjpeg( $source );
                        break;
                        case 'png':
                                $simg = imagecreatefrompng( $source );
                        break;
                        case 'PNG':
                                $simg = imagecreatefrompng( $source );
                        break;
                }
                $dimg = imagecreatetruecolor( $nw, $nh );
                $wm = $w / $nw;
                $hm = $h / $nh;
                $h_height = $nh / 2;
                $w_height = $nw / 2;
                if( $w > $h )
                {
                        $adjusted_width = $w / $hm;
                        $half_width = $adjusted_width / 2;
                        $int_width = $half_width - $w_height;
                        imagecopyresampled( $dimg, $simg, -$int_width, 0, 0, 0, $adjusted_width, $nh, $w, $h);
                }
                elseif( ( $w < $h ) || ( $w == $h ) )
                {
                        $adjusted_height = $h / $wm;
                        $half_height = $adjusted_height / 2;
                        $int_height = $half_height - $h_height;
                        imagecopyresampled( $dimg, $simg, 0, -$int_height, 0, 0, $nw, $adjusted_height, $w, $h);
                }
                else
                {
                        imagecopyresampled( $dimg, $simg, 0, 0, 0, 0, $nw, $nh, $w, $h);
                }
                imagejpeg( $dimg, $dest, 100 );
        }
        if ( $_FILES['photo']['tmp_name'] !== '' )
        {
                $name = $_FILES['photo']['name'];
                $tempName = $_FILES['photo']['tmp_name'];

                $roz = array_pop( explode('.', $name) );
                $new_name =  md5( substr( str_shuffle('1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'), 0, 25) ) . '.' . $roz;
                move_uploaded_file( $tempName, '../files/photo/tmp/' . $new_name);
                cropImage( 100, 80, '../files/photo/tmp/' . $new_name, $roz, '../files/photo/mini/' . $new_name );
				cropImage( 180, 115, '../files/photo/tmp/' . $new_name, $roz, '../files/photo/sre/' . $new_name );
				cropImage( 200, 150, '../files/photo/tmp/' . $new_name, $roz, '../files/photo/duz/' . $new_name );
                cropImage( 470, 350, '../files/photo/tmp/' . $new_name, $roz, '../files/photo/big/' . $new_name );
				cropImage( 108, 66, '../files/photo/tmp/' . $new_name, $roz, '../files/photo/top/' . $new_name );
				
                unlink('../files/photo/tmp/' . $new_name);
                $sql = "INSERT INTO `photos` (`photoObjectId`, `photoUrl`) VALUES ('$id', '$new_name')";
                mysql_query( $sql );
                $smarty -> assign('success', 'Poprawnie dodano nowe zdjęcie!');
        }
        else
        {
                $error = 'Nie wybrano pliku zdjęcia!';
        }
}

if ( isset( $_POST['ab1'] ) )
{
        if ( !empty( $_POST['day1'] ) )
        {
                $sql = "SELECT obDateEnd FROM object WHERE obId = '$id'";
                $q = mysql_query( $sql );
                $db = mysql_fetch_array( $q );
                
                if ( strtotime( $db[0] ) > time() )
                {
                        //nie wygaslo
                        $sql = "SELECT obDateEnd FROM object WHERE obId = '$id'";
                        $q = mysql_query( $sql );
                        $db = mysql_fetch_array( $q );
                        $date = $db[0];
                        $newDate = strtotime( $date ) + ( $_POST['day1'] * 86400 );
                        $dateEnd = date("Y-m-d H:i:s", $newDate);
                }
                else
                {
                        //wygaslo
                        $dateEnd = date("Y-m-d H:i:s", strtotime('+' . $_POST['day1'] . ' days'));
                }
                
                $sql = "UPDATE `object` SET `obDateEnd` = '$dateEnd' WHERE obId = '$id'";
                mysql_query( $sql );
                
                $smarty -> assign('success', 'Poprawnie zapisano zmiany!');
        }
}
if ( isset( $_POST['ab2'] ) )
{
        if ( !empty( $_POST['day2'] ) )
        {
                $sql = "SELECT obPromoDateEnd FROM object WHERE obId = '$id'";
                $q = mysql_query( $sql );
                $db = mysql_fetch_array( $q );
                
                if ( strtotime( $db[0] ) > time() )
                {
                        //nie wygaslo
                        $sql = "SELECT obPromoDateEnd FROM object WHERE obId = '$id'";
                        $q = mysql_query( $sql );
                        $db = mysql_fetch_array( $q );
                        $date = $db[0];
                        $newDate = strtotime( $date ) + ( $_POST['day2'] * 86400 );
                        $dateEnd = date("Y-m-d H:i:s", $newDate);
                }
                else
                {
                        //wygaslo
                        $dateEnd = date("Y-m-d H:i:s", strtotime('+' . $_POST['day2'] . ' days'));
                }
                
                $sql = "UPDATE `object` SET `obPromo` = '1', `obPromoDateEnd` = '$dateEnd' WHERE obId = '$id'";
                mysql_query( $sql );
                
                $smarty -> assign('success', 'Poprawnie zapisano zmiany!');
        }
}
if ( isset( $_POST['ab3'] ) )
{
        if ( !empty( $_POST['day3'] ) )
        {
                $sql = "SELECT obLastMinuteDateEnd FROM object WHERE obId = '$id'";
                $q = mysql_query( $sql );
                $db = mysql_fetch_array( $q );
                
                if ( strtotime( $db[0] ) > time() )
                {
                        //nie wygaslo
                        $sql = "SELECT obLastMinuteDateEnd FROM object WHERE obId = '$id'";
                        $q = mysql_query( $sql );
                        $db = mysql_fetch_array( $q );
                        $date = $db[0];
                        $newDate = strtotime( $date ) + ( $_POST['day3'] * 86400 );
                        $dateEnd = date("Y-m-d H:i:s", $newDate);
                }
                else
                {
                        //wygaslo
                        $dateEnd = date("Y-m-d H:i:s", strtotime('+' . $_POST['day3'] . ' days'));
                }
                
                $sql = "UPDATE `object` SET `obLastMinute` = '1', `obLastMinuteDateEnd` = '$dateEnd' WHERE obId = '$id'";
                mysql_query( $sql );
                
                $smarty -> assign('success', 'Poprawnie zapisano zmiany!');
        }
}
if ( isset( $_POST['ab4'] ) )
{
        if ( !empty( $_POST['day4'] ) )
        {
                $sql = "SELECT obWyrozDateEnd FROM object WHERE obId = '$id'";
                $q = mysql_query( $sql );
				// $q = mysql_query($sql) or die('Zapytanie: '.$sql.' --- błąd: '.mysql_error()); 

                $db = mysql_fetch_array( $q );
                
                if ( strtotime( $db[0] ) > time() )
                {
                        //nie wygaslo
                        $sql = "SELECT obWyrozDateEnd FROM object WHERE obId = '$id'";
                        $q = mysql_query( $sql );
                        $db = mysql_fetch_array( $q );
                        $date = $db[0];
                        $newDate = strtotime( $date ) + ( $_POST['day4'] * 86400 );
                        $dateEnd = date("Y-m-d H:i:s", $newDate);
                }
                else
                {
                        //wygaslo
                        $dateEnd = date("Y-m-d H:i:s", strtotime('+' . $_POST['day4'] . ' days'));
                }
                
                $sql = "UPDATE `object` SET `obWyroz` = '1', `obWyrozDateEnd` = '$dateEnd' WHERE obId = '$id'";
                mysql_query( $sql );
                
                $smarty -> assign('success', 'Poprawnie zapisano zmiany!');
        }
}
if ( isset( $_GET['ab'] ) )
{
        if ( $_GET['ab'] == 1 )
        {
                $sql = "UPDATE `object` SET `obPromoDateEnd` = '0000-00-00 00:00:00', `obPromo` = '0' WHERE obId = '$id'";
        }
        if ( $_GET['ab'] == 2 )
        {
                $sql = "UPDATE `object` SET `obLastMinuteDateEnd` = '0000-00-00 00:00:00', `obLastMinute` = '0' WHERE obId = '$id'";
        }
				        if ( $_GET['ab'] == 4 )
        {
                $sql = "UPDATE `object` SET `obWyrozDateEnd` = '0000-00-00 00:00:00', `obWyroz` = '0' WHERE obId = '$id'";
        }
        mysql_query( $sql );
        //header('Location: administrator.php?mod=edit&id=' . $id);
        $smarty -> assign('success', 'Poprawnie zapisano zmiany!');
}

$sql = "SELECT a.*, b.userName FROM object AS a, users AS b WHERE a.obUserId = b.userId AND obId = '$id'";
$q = mysql_query( $sql );
$edit = array();
while ( $db = mysql_fetch_array( $q ) )
{
        $edit[] = $db;
}
$smarty -> assign('edit', $edit);

$sql = "SELECT * FROM state ORDER by stateName ASC";
$q = mysql_query( $sql );
$state = array();
while ( $db = mysql_fetch_array( $q ) )
{
        $state[] = $db;
}
$smarty -> assign('showState', $state);

$sql = "SELECT * FROM type ORDER by typeName ASC";
$q = mysql_query( $sql );
$type = array();
while ( $db = mysql_fetch_array( $q ) )
{
        $type[] = $db;
}
$smarty -> assign('showType', $type);

$sql = "SELECT * FROM region ORDER by regionName ASC";
$q = mysql_query( $sql );
$region = array();
while ( $db = mysql_fetch_array( $q ) )
{
        $region[] = $db;
}
$smarty -> assign('showRegion', $region);


$sql = "SELECT * FROM droga ORDER by drogaName ASC";
$q = mysql_query( $sql );
$droga = array();
while ( $db = mysql_fetch_array( $q ) )
{
        $droga[] = $db;
}
$smarty -> assign('showDroga', $droga);


$sql = "SELECT * FROM photos WHERE photoObjectId = '$id' ORDER by photoId DESC";
$q = mysql_query( $sql );
$photos = array();
while ( $db = mysql_fetch_array( $q ) )
{
        $photos[] = $db;
}
$smarty -> assign('photos', $photos);


$sql = "SELECT * FROM kategoria  ORDER by katName ASC";
$q = mysql_query( $sql );
$kata = array();
while ( $db = mysql_fetch_array( $q ) )
{
        $kata[] = $db;
}
$smarty -> assign('showKategoria', $kata);



$sql = "SELECT * FROM rynek  ORDER by rynekName ASC";
$q = mysql_query( $sql );
$rynek  = array();
while ( $db = mysql_fetch_array( $q ) )
{
        $rynek[] = $db;
}
$smarty -> assign('showRynek', $rynek);



$sql = "SELECT * FROM okna  ORDER by oknaName ASC";
$q = mysql_query( $sql );;
$okna  = array();
while ( $db = mysql_fetch_array( $q ) )
{
        $okna[] = $db;
}
$smarty -> assign('showOkna', $okna);


$sql = "SELECT * FROM stan  ORDER by stanName ASC";
$q = mysql_query( $sql );
$stan = array();
while ( $db = mysql_fetch_array( $q ) )
{
        $stan[] = $db;
}
$smarty -> assign('showStan', $stan);


$sql = "SELECT * FROM rodzaj  ORDER by rodzajName ASC";
$q = mysql_query( $sql );
$rodzaj = array();
while ( $db = mysql_fetch_array( $q ) )
{
        $rodzaj[] = $db;
}
$smarty -> assign('showRodzaj', $rodzaj);



$sql = "SELECT * FROM ogrzewanie  ORDER by ogrzewanieName ASC";
$q = mysql_query( $sql );
$ogrzewanie  = array();
while ( $db = mysql_fetch_array( $q ) )
{
        $ogrzewanie[] = $db;
}
$smarty -> assign('showOgrzewanie', $ogrzewanie);



$sql = "SELECT * FROM ogrodzenie  ORDER by ogrodzenieName ASC";
$q = mysql_query( $sql );
$ogrodzenie  = array();
while ( $db = mysql_fetch_array( $q ) )
{
        $ogrodzenie[] = $db;
}
$smarty -> assign('showOgrodzenie', $ogrodzenie);

$sql = "SELECT * FROM kanalizacja   ORDER by kanalizacjaName ASC";
$q = mysql_query( $sql );
$kanalizacja   = array();
while ( $db = mysql_fetch_array( $q ) )
{
        $kanalizacja[] = $db;
}
$smarty -> assign('showKanalizacja', $kanalizacja);

$sql = "SELECT * FROM dojazd  ORDER by dojazdName ASC";
$q = mysql_query( $sql );
$dojazd  = array();
while ( $db = mysql_fetch_array( $q ) )
{
        $dojazd[] = $db;
}
$smarty -> assign('showDojazd', $dojazd);

$sql = "SELECT * FROM budulec ORDER by budulecName ASC";
$q = mysql_query( $sql );
$budulec = array();
while ( $db = mysql_fetch_array( $q ) )
{
        $budulec[] = $db;
}
$smarty -> assign('showBudulec', $budulec);

$sql = "SELECT * FROM dach ORDER by dachName ASC";
$q = mysql_query( $sql );
 $dach = array();
 while ( $db = mysql_fetch_array( $q ) )
{
        $dach[] = $db;
}
$smarty -> assign('showDach', $dach);
?>
