<?

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

*************************************************
*			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.

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


class obj
{

				       function showStateLicz( $mysql )
        {
               
							$sql = "SELECT a.obStateId , b.stateId, b.stateName,  COUNT( obStateId  ) 
							FROM object AS a, 
							state AS b
							WHERE a.obStateId  = b.stateId AND a.obDateEnd > NOW()
							GROUP BY obStateId ";

								          
                $q = $mysql -> q( $sql );
                $woje = array();
								$count = 0;
                while ( $db = mysql_fetch_array( $q) )
                {
							$woje[$count] = $db;
							//$marka[$count]['ikona'] = $db[3];
							$woje[$count]['licz'] = $db[3];

							$count++;
                }
                return $woje;
				
        }

				
				
				function showPromoB( $mysql ) { 
								$sql = "SELECT a.*, b.*, c.*, d.*, e.*,  g.*, h.* 
								FROM object AS a, type AS b, state AS c , region AS d, kategoria AS e,  rodzaj AS g, stan AS h 
								WHERE a.obTypeId = b.typeId AND a.obStateId = c.stateId AND a.obRegion = d.regionId AND a.obKat = e.katId  AND a.obRodzaj = g.rodzajId AND a.obStan = h.stanId AND a.obPromo = '1' AND a.obConfirm = '1' AND a.obDateEnd > NOW() AND a.obPromoDateEnd > NOW() ORDER by a.obId DESC LIMIT 7";
				$q = $mysql -> q( $sql );
								$promosB = array();
								$count = 0;
								while ( $db = mysql_fetch_array( $q ) )
								{
												$promosB[$count] = $db;
												$sql2 = "SELECT photoUrl FROM photos WHERE photoObjectId = '".$db['obId']."' ORDER by photoId ASC LIMIT 1";
												$q2 = $mysql -> q( $sql2 );
												$db2 = mysql_fetch_array( $q2 );
												$promosB[$count]['photo'] = $db2[0];
												$count++;
								}

									return $promosB;
						}
						

				function showPromoM( $mysql ) { 
								$sql = "SELECT a.*, b.*, c.*, d.*, e.*,  g.*, h.* 
								FROM object AS a, type AS b, state AS c , region AS d, kategoria AS e,  rodzaj AS g, stan AS h 
								WHERE a.obTypeId = b.typeId AND a.obStateId = c.stateId AND a.obRegion = d.regionId AND a.obKat = e.katId  AND a.obRodzaj = g.rodzajId AND a.obStan = h.stanId  AND a.obConfirm = '1' AND a.obDateEnd > NOW() AND a.obWyrozDateEnd  > NOW() ORDER BY RAND() LIMIT 5";
				$q = $mysql -> q( $sql );
								$promosB = array();
								$count = 0;
								while ( $db = mysql_fetch_array( $q ) )
								{
												$promosB[$count] = $db;
												$sql2 = "SELECT photoUrl FROM photos WHERE photoObjectId = '".$db['obId']."' ORDER by photoId ASC LIMIT 1";
												$q2 = $mysql -> q( $sql2 );
												$db2 = mysql_fetch_array( $q2 );
												$promosB[$count]['photo'] = $db2[0];
												$count++;
								}

									return $promosB;
						}
						
						

					function showPromoA( $mysql ) {
								$sql = "SELECT a.*, b.*, c.*, d.*, e.*,  g.*, h.* 
								FROM object AS a, type AS b, state AS c , region AS d, kategoria AS e,  rodzaj AS g, stan AS h 
								WHERE a.obTypeId = b.typeId AND a.obStateId = c.stateId AND a.obRegion = d.regionId AND a.obKat = e.katId  AND a.obRodzaj = g.rodzajId AND a.obStan = h.stanId AND a.obPromo = '1' AND a.obConfirm = '1' AND a.obDateEnd > NOW() AND a.obPromoDateEnd > NOW() ORDER by  RAND() LIMIT 5";
								$q = $mysql -> q( $sql );
								$promos = array();
								$count = 0;
								while ( $db = mysql_fetch_array( $q ) )
								{
												$promos[$count] = $db;
												$sql2 = "SELECT photoUrl FROM photos WHERE photoObjectId = '".$db['obId']."' ORDER by photoId ASC LIMIT 1";
												$q2 = $mysql -> q( $sql2 );
												$db2 = mysql_fetch_array( $q2 );
												$promos[$count]['photo'] = $db2[0];
												$count++;
								}

									return $promos;
					}
					
					function showReklamaA( $mysql )
					{
							$sqlR = mysql_query('SELECT * FROM reklama ORDER BY RAND() LIMIT 0,1');
							$rek= array();
							$count = 0;
							while (  $dbR =  mysql_fetch_array( $sqlR ) )
							{
							$rek[$count] = $dbR;
							$rek[$count]['id'] = $dbR[0];
							$count++;
							}
							return $rek;
					}

					function showReklamaB( $mysql )
					{
							$sqlRB = mysql_query('SELECT * FROM reklamaB ORDER BY RAND() LIMIT 0,1');
							$rekB= array();
							$count = 0;
							while (  $dbRB =  mysql_fetch_array( $sqlRB ) )
							{
							$rekB[$count] = $dbRB;
							$rekB[$count]['id'] = $dbRB[0];
							$count++;
							}
							return $rekB;
					}
					
					function showReklamaC( $mysql )
					{
							$sqlRC = mysql_query('SELECT * FROM reklamaB ORDER BY RAND() LIMIT 0,1');
							$rekC= array();
							$count = 0;
							while (  $dbRC =  mysql_fetch_array( $sqlRC ) )
							{
							$rekC[$count] = $dbRC;
							$rekC[$count]['id'] = $dbRC[0];
							$count++;
							}
							return $rekC;
					}

										function showReklamaD( $mysql )
					{
							$sqlRD = mysql_query('SELECT * FROM reklamaC ORDER BY RAND() LIMIT 0,1');
							$rekD= array();
							$count = 0;
							while (  $dbRD =  mysql_fetch_array( $sqlRD ) )
							{
							$rekD[$count] = $dbRD;
							$rekD[$count]['id'] = $dbRD[0];
							$count++;
							}
							return $rekD;
					}
					
										function showReklamaD2( $mysql )
					{
							$sqlRD2 = mysql_query('SELECT * FROM reklamaD ORDER BY RAND() LIMIT 0,1');
							$rekD2= array();
							$count = 0;
							while (  $dbRD2 =  mysql_fetch_array( $sqlRD2 ) )
							{
							$rekD2[$count] = $dbRD2;
							$rekD2[$count]['id'] = $dbRD2[0];
							$count++;
							}
							return $rekD2;
					}
					
					
					function showReklamaS( $mysql )
					{
							$sqlS = mysql_query('SELECT * FROM reklamaS ORDER BY RAND() LIMIT 0,1');
							$rekS= array();
							$count = 0;
							while (  $dbS =  mysql_fetch_array( $sqlS ) )
							{
							$rekS[$count] = $dbS;
							$rekS[$count]['id'] = $dbS[0];
							$count++;
							}
							return $rekS;
					}

				       function showTypeLicz( $mysql )
        {
               
$sql = "SELECT a.obTypeId, b.typeId, b.typeName, COUNT( obTypeId ) 
FROM object AS a, 
type AS b
WHERE a.obTypeId = b.typeId
GROUP BY obTypeId";

							 // $sql = "SELECT obTypeId, COUNT(obTypeId) FROM object GROUP BY obTypeId";
								          
                $q = $mysql -> q( $sql );
                $type = array();
								$count = 0;
                while ( $db = mysql_fetch_array( $q) )
                {
							$type[$count] = $db;
							$type[$count]['licz'] = $db[3];
							$count++;
                }
                return $type;
				
        }
				
				       function showKategriaLicz( $mysql )
        {
               
$sql = "SELECT a.obKat, b.katId, b.katName, COUNT( obKat ) 
FROM object AS a, 
kategoria AS b
WHERE a.obKat  = b.katId
GROUP BY obKat";

							 // $sql = "SELECT obTypeId, COUNT(obTypeId) FROM object GROUP BY obTypeId";
								          
                $q = $mysql -> q( $sql );
                $kat = array();
								$count = 0;
                while ( $db = mysql_fetch_array( $q) )
                {
							$kat[$count] = $db;
							$kat[$count]['licz'] = $db[3];
							$count++;
                }
                return $kat;
				
        }
				
				
	       function showDiler( $mysql )
        {
            $sql = "SELECT * FROM diler    ORDER by   RAND() LIMIT 2";

                $q = $mysql -> q( $sql );
				
               $dil = array();
                while ( $db = mysql_fetch_array( $q ) )
                {
                        $dil[] = $db;
                }
                return $dil;
			
        }
				
					       function showOgl( $mysql )
        {
            $sql = "SELECT * FROM aktualnosci  ORDER by aktId DESC LIMIT 2";
			//$q = mysql_query($sql) or die('Zapytanie: '.$sql.' --- błąd: '.mysql_error()); 

                $q = $mysql -> q( $sql );
				
               $ogl2 = array();
                while ( $db = mysql_fetch_array( $q ) )
                {
                        $ogl2[] = $db;
                }
                return $ogl2;
			
        }

				function showOglBB( $mysql )
        {
				$sql = "SELECT  * FROM ogloszenie  ORDER by ogloszenieId ";
			//$q = mysql_query($sql) or die('Zapytanie: '.$sql.' --- błąd: '.mysql_error()); 

                $q = $mysql -> q( $sql );
				
               $ogl4 = array();
                while ( $db2 = mysql_fetch_array( $q ) )
                {
                        $ogl4[] = $db2;
                }
                return $ogl4;
			
        }
				
				
				function showOglB( $mysql )
        {
				$sql = "SELECT  * FROM ogloszenie  ORDER by ogloszenieId DESC LIMIT 4";
			//$q = mysql_query($sql) or die('Zapytanie: '.$sql.' --- błąd: '.mysql_error()); 

                $q = $mysql -> q( $sql );
				
               $ogl3 = array();
                while ( $db = mysql_fetch_array( $q ) )
                {
                        $ogl3[] = $db;
                }
                return $ogl3;
			
        }
				
				

	       function showRynek( $mysql )
        {
            $sql = "SELECT * FROM rynek  ORDER by rynekName ASC";
                $q = $mysql -> q( $sql );
                $rynek  = array();
                while ( $db = mysql_fetch_array( $q ) )
                {
                        $rynek[] = $db;
                }
                return $rynek;
        }
		
	       function showOkna( $mysql )
        {
            $sql = "SELECT * FROM okna  ORDER by oknaName ASC";
                $q = $mysql -> q( $sql );
                $okna  = array();
                while ( $db = mysql_fetch_array( $q ) )
                {
                        $okna[] = $db;
                }
                return $okna;
        }
		
	       function showOgrzewanie( $mysql )
        {
            $sql = "SELECT * FROM ogrzewanie  ORDER by ogrzewanieName ASC";
                $q = $mysql -> q( $sql );
                $ogrzewanie  = array();
                while ( $db = mysql_fetch_array( $q ) )
                {
                        $ogrzewanie[] = $db;
                }
                return $ogrzewanie;
        }
		
	       function showOgrodzenie( $mysql )
        {
            $sql = "SELECT * FROM ogrodzenie  ORDER by ogrodzenieName ASC";
                $q = $mysql -> q( $sql );
                $ogrodzenie  = array();
                while ( $db = mysql_fetch_array( $q ) )
                {
                        $ogrodzenie[] = $db;
                }
                return $ogrodzenie;
        }
       function showKanalizacja( $mysql )
        {
            $sql = "SELECT * FROM kanalizacja   ORDER by kanalizacjaName ASC";
                $q = $mysql -> q( $sql );
                $kanalizacja   = array();
                while ( $db = mysql_fetch_array( $q ) )
                {
                        $kanalizacja[] = $db;
                }
                return $kanalizacja  ;
        }
		
		function showDojazd( $mysql )
        {
            $sql = "SELECT * FROM dojazd  ORDER by dojazdName ASC";
                $q = $mysql -> q( $sql );
                $dojazd  = array();
                while ( $db = mysql_fetch_array( $q ) )
                {
                        $dojazd[] = $db;
                }
                return $dojazd ;
        }
		
		
       function showStan( $mysql )
        {
            $sql = "SELECT * FROM stan ORDER by stanName ASC";
                $q = $mysql -> q( $sql );
                $stan = array();
                while ( $db = mysql_fetch_array( $q ) )
                {
                        $stan[] = $db;
                }
                return $stan;
        }

		
       function showKategoria( $mysql )
        {
            $sql = "SELECT * FROM kategoria ORDER by katName ASC";
                $q = $mysql -> q( $sql );
                $kategoria = array();
                while ( $db = mysql_fetch_array( $q ) )
                {
                        $kategoria[] = $db;
                }
                return $kategoria;
        }
		
       function showBudulec( $mysql )
        {
            $sql = "SELECT * FROM budulec ORDER by budulecName ASC";
                $q = $mysql -> q( $sql );
                $budulec = array();
                while ( $db = mysql_fetch_array( $q ) )
                {
                        $budulec[] = $db;
                }
                return $budulec;
        }

       function showRodzaj( $mysql )
        {
            $sql = "SELECT * FROM rodzaj ORDER by rodzajName ASC";
                $q = $mysql -> q( $sql );
                $rodzaj = array();
                while ( $db = mysql_fetch_array( $q ) )
                {
                        $rodzaj[] = $db;
                }
                return $rodzaj;
        }
		
       function showDach( $mysql )
        {
            $sql = "SELECT * FROM dach ORDER by dachName ASC";
                $q = $mysql -> q( $sql );
                $dach = array();
                while ( $db = mysql_fetch_array( $q ) )
                {
                        $dach[] = $db;
                }
                return $dach;
        }
		
       function showMiasto( $mysql )
        {
                $sql = "SELECT DISTINCT obCity FROM object ORDER by obCity ASC";
                $q = $mysql -> q( $sql );
                $miasto = array();
                while ( $db = mysql_fetch_array( $q ) )
                {
                        $miasto[] = $db;
                }
                return $miasto;
        }
		
        function showPages( $mysql )
        {
                $sql = "SELECT * FROM pages WHERE pagesId > 4";
                $q = $mysql -> q( $sql );
                $strona = array();
                while ( $db = mysql_fetch_array( $q ) )
                {
                        $strona[] = $db;
                }
                return $strona;
        }

        function showType( $mysql )
        {
                $sql = "SELECT * FROM type ORDER by typeName ASC";
                $q = $mysql -> q( $sql );
                $type = array();
                while ( $db = mysql_fetch_array( $q ) )
                {
                        $type[] = $db;
                }
                return $type;
        }
		function showRegion( $mysql )
        {
                $sql = "SELECT * FROM region ORDER by regionName ASC";
                $q = $mysql -> q( $sql );
                $region = array();
                while ( $db = mysql_fetch_array( $q ) )
                {
                        $region[] = $db;
                }
                return $region;
        }
		function showDroga( $mysql )
        {
                $sql = "SELECT * FROM droga ORDER by drogaName ASC";
                $q = $mysql -> q( $sql );
                $droga = array();
                while ( $db = mysql_fetch_array( $q ) )
                {
                        $droga[] = $db;
                }
                return $droga;
        }
        function showState( $mysql )
        {
                $sql = "SELECT * FROM state ORDER by stateId ASC";
                $q = $mysql -> q( $sql );
                $state = array();
                while ( $db = mysql_fetch_array( $q ) )
                {
                        $state[] = $db;
                }
                return $state;
        }

 function addOB ( $obName,  $obImie,  $obCity,  $obTel, $obEmail, $obTxt, $freeDay, $mysql )
        {
                $dateEnd = date("Y-m-d H:i:s", strtotime('+'.$freeDay.' days'));
				
       			 $sql = "INSERT INTO `ogloszenie` (`ogloszenieId`,  `ogloszenieTitle`,  `ogloszenieImie`,  `ogloszenieMiasto`,  `ogloszenieEmail`, `ogloszenieTelefon`,   `ogloszenieTxt`, `ogloszenieData`)  VALUES ('".$_SESSION['userId']."',  '$obName',  '$obImie', '$obCity', '$obEmail', '$obTel', '$obTxt', NOW())";

                $mysql -> q( $sql );
        }
		
        function editOB ( $obName,  $obImie,  $obCity,  $obTel, $obEmail, $obTxt, $freeDay, $objectId, $mysql )
        {
		
                $sql = "UPDATE `ogloszenie` SET `ogloszenieTitle` = '$obName',  `ogloszenieImie` = '$obImie', `ogloszenieMiasto` = '$obCity',  `ogloszenieEmail` = '$obEmail',    `ogloszenieTelefon` = '$obTel', `ogloszenieTxt` = '$obTxt', WHERE ogloszenieId = '$objectId'";
                $mysql -> q( $sql );
        }


        function add ( $obName,  $obKat,  $obType, $obAdress, $obCity, $obState, $obRegion, $obDroga, $obTel, $obEmail, $obWww, $obRok, $obRodzaj, $obRynek, $obPowM, $obPoC, $obPietra,  $obPietro, $obPokoje, $obPowD, $obBudulec, $obDach, $obDojazd, $obOgrodzenie, $obOgrzewanie,  $obKanalizacja,  $obOkna,  $obStan,   $obPriceOn,  $obTeren, $obRoom, $obNearby, $obTxt, $obDzielnica, $freeDay, $mysql )
        {
                $dateEnd = date("Y-m-d H:i:s", strtotime('+'.$freeDay.' days'));
				
       			 $sql = "INSERT INTO `object` (`obUserId`, `obTypeId`, `obStateId`, `obName`, `obAdress`, `obCity`, `obTel`, `obEmail`, `obWww`,  `obPriceOn`,  `obTeren`, `obRoom`, `obNearby`, `obRegion`, `obDroga`,  `obTxt`, `obDate`, `obDateEnd`, `obConfirm`, `obBudulec`, `obDach`, `obDojazd`, `obKanalizacja`, `obKat`, `obOgrodzenie`, `obStan`, `obOgrzewanie`, `obOkna`, `obRynek`, `obRodzaj`, `obRok`, `obPowM`, `obPoC`, `obPietra`, `obPietro`, `obPokoje`, `obPowD`, `obDzielnica`)  VALUES ('".$_SESSION['userId']."', '$obType', '$obState', '$obName', '$obAdress', '$obCity', '$obTel', '$obEmail', '".str_replace(array('http://', 'www.'), '', $obWww)."', '$obPriceOn', '$obTeren', '$obRoom', '$obNearby', '$obRegion', '$obDroga', '$obTxt', NOW(), '$dateEnd', '0', '$obBudulec',  '$obDach', '$obDojazd', '$obKanalizacja', '$obKat', '$obOgrodzenie', '$obStan', '$obOgrzewanie', '$obOkna', '$obRynek', '$obRodzaj', '$obRok', '$obPowM', '$obPoC', '$obPietra', '$obPietro', '$obPokoje', '$obPowD', '$obDzielnica')";

                $mysql -> q( $sql );
        }
		


        function edit ( $obType, $obState, $obName, $obAdress, $obCity, $obTel, $obEmail, $obWww, $obPriceOn,  $obTeren, $obRoom, $obNearby, $obRegion, $obDroga, $obTxt, $freeDay, $obBudulec,  $obDach, $obDojazd, $obKanalizacja, $obKat, $obOgrodzenie, $obStan, $obOgrzewanie, $obOkna, $obRynek, $obRodzaj, $obRok, $obPowM, $obPoC, $obPietra, $obPietro, $obPokoje,  $obPowD, $obDzielnica, $objectId, $mysql )
        {
		
                $sql = "UPDATE `object` SET `obTypeId` = '$obType', `obStateId` = '$obState', `obName` = '$obName', `obAdress` = '$obAdress', `obCity` = '$obCity', `obTel` = '$obTel', `obEmail` = '$obEmail', `obWww` = '".str_replace(array('http://', 'www.'), '', $obWww)."',  `obPriceOn` = '$obPriceOn',  `obTeren` = '$obTeren', `obRoom` = '$obRoom', `obNearby` = '$obNearby', `obRegion` = '$obRegion', `obDroga` = '$obDroga', `obTxt` = '$obTxt', `obBudulec` = '$obBudulec',  `obDach` = '$obDach', `obDojazd` = '$obDojazd', `obKanalizacja` = '$obKanalizacja', `obKat` = '$obKat', `obOgrodzenie` = '$obOgrodzenie', `obStan` = '$obStan', `obOgrzewanie` = '$obOgrzewanie', `obOkna` = '$obOkna', `obRynek` = '$obRynek', `obRodzaj` = '$obRodzaj', `obRok` = '$obRok', `obPowM` = '$obPowM', `obPoC` = '$obPoC', `obPietra` = '$obPietra', , `obPietro` = '$obPietro', `obPokoje` = '$obPokoje', `obPowD` = '$obPowD', `obDzielnica` = '$obDzielnica' WHERE obId = '$objectId'";
                $mysql -> q( $sql );
        }
		
		
		
		
function addDiler ( $dilerNazwa, $dilerOfirmie, $dilerUlica, $dilerNrulicy, $dilerPoczta, $dilerMiasto, $dilerWWW, $dilerTel, $dilerFax, $dilerEmail, $dilerNr, $mysql )
        {
       			 $sql = "INSERT INTO `diler` ( `dilerNazwa`, `uzytkownik`, `dilerOfirmie`, `dilerUlica`, `dilerNrulicy`, `dilerPoczta`, `dilerMiasto`, `dilerWWW`, `dilerTel`, `dilerFax`, `dilerEmail`, `dilerfoto` )  VALUES ( '$dilerNazwa',  '".$_SESSION['userId']."', '$dilerOfirmie', '$dilerUlica', '$dilerNrulicy', '$dilerPoczta', '$dilerMiasto', '$dilerWWW', '$dilerTel', '$dilerFax', '$dilerEmail', '$dilerfoto' )";
						 

                $mysql -> q( $sql );
        }
				
function eddDiler ( $dilerNazwa, $dilerOfirmie, $dilerUlica, $dilerNrulicy, $dilerPoczta, $dilerMiasto, $dilerWWW, $dilerTel, $dilerFax, $dilerEmail, $dilerNr, $id_diler, $uzytkownik, $mysql )
        {
       			 $sql = "UPDATE `diler` SET  
						 `dilerNazwa` = '$dilerNazwa',
						 `dilerOfirmie` = '$dilerOfirmie',
						 `dilerUlica` = '$dilerUlica',
						 `dilerNrulicy` = '$dilerNrulicy',
						 `dilerPoczta` = '$dilerPoczta',
						 `dilerMiasto` = '$dilerMiasto',
						 `dilerWWW` = '$dilerWWW',
						 `dilerTel` = '$dilerTel',
						 `dilerFax` = '$dilerFax',
						 `dilerEmail` = '$dilerEmail',
						 `dilerNr` = '$dilerNr'
						 WHERE uzytkownik = '$uzytkownik' AND  id_diler = '$id_diler'";
													 
													 
                $mysql -> q( $sql );
        }
				
				
				
				
				
				
        function walidPhoto ( $name )
        {
                if ( !empty( $name ) )
                {
                        $roz = array_pop( explode('.', $name) );
                
                        if ( $roz != 'jpg' && $roz != 'jpeg' && $roz != 'JPG' && $roz != 'JPEG' && $roz != 'gif' && $roz != 'GIF' && $roz != 'png' && $roz != 'PNG' )
                        {
                                return false;
                        }
                        else
                        {
                                return true;
                        }
                }
                else
                {
                        return true;
                }
        }
        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 );
        }
        function uploadPhoto( $name, $tempName, $objectId, $mysql )
        {
                $roz = array_pop( explode('.', $name) );
                $new_name =  md5( substr( str_shuffle('1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'), 0, 25) ) . '.' . $roz;
                move_uploaded_file( $tempName, 'files/photo/tmp/' . $new_name);
                $this -> cropImage( 100, 80, 'files/photo/tmp/' . $new_name, $roz, 'files/photo/mini/' . $new_name );
				$this -> cropImage( 180, 115, 'files/photo/tmp/' . $new_name, $roz, 'files/photo/sre/' . $new_name );
				$this -> cropImage( 200, 150, 'files/photo/tmp/' . $new_name, $roz, 'files/photo/duz/' . $new_name );
                $this -> cropImage( 430, 360, 'files/photo/tmp/' . $new_name, $roz, 'files/photo/big/' . $new_name );
                $this -> 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 ('$objectId', '$new_name')";
                $mysql -> q( $sql );
        }
				
				function uploadPhotoDiler( $name, $tempName, $objectId, $mysql )
        {
                $roz = array_pop( explode('.', $name) );
                $new_name =  md5( substr( str_shuffle('1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'), 0, 25) ) . '.' . $roz;
                move_uploaded_file( $tempName, 'diler/tmp/' . $new_name);
                $this -> cropImage( 200, 140, 'diler/tmp/' . $new_name, $roz, 'diler/duze/' . $new_name );
								$this -> cropImage( 100, 60, 'diler/tmp/' . $new_name, $roz, 'diler/male/' . $new_name );
                unlink('diler/tmp/' . $new_name);
							

                $sql = "UPDATE `diler` SET  `dilerfoto`= '$new_name' WHERE uzytkownik = '".$_SESSION['userId']."' ";
                $mysql -> q( $sql );
        }
				
				
        function checkDelete ( $objectId, $userId, $mysql )
        {
                $sql = "SELECT obUserId FROM object WHERE obId = '$objectId'";
                $q = $mysql -> q( $sql );
                $db = mysql_fetch_array( $q );
                
                if ( $db[0] == $userId )
                {
                        return true;
                }
                else
                {
                        return false;
                }
        }
        function delete ( $objectId, $mysql )
        {
                $sql = "SELECT * FROM photos WHERE photoObjectId = '$objectId'";
                $q = $mysql -> q( $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 `object` WHERE obId = '$objectId'";
                $mysql -> q( $sql );
                $sql = "DELETE FROM `photos` WHERE photoObjectId = '$objectId'";
                $mysql -> q( $sql );
        }
        function numPtohos ( $objectId, $mysql )
        {
                $sql = "SELECT COUNT(*) as num_photos FROM photos WHERE photoObjectId = '$objectId'";
                $q = $mysql -> q( $sql );
                $db = mysql_fetch_array( $q );
                return $db['num_photos'];
        }
        function deletePhoto ( $photoId, $mysql )
        {
                $sql = "SELECT * FROM photos WHERE photoId = '$photoId'";
                $q = $mysql -> q( $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 -> q( $sql );
        }
        function end ( $objectId, $mysql )
        {
                $now = time();
                $sql = "SELECT obDateEnd FROM object WHERE obId = '$objectId'";
                $q = $mysql -> q( $sql );
                $db = mysql_fetch_array( $q );
                $dateEnd = strtotime( $db[0] );
                $end = $dateEnd - $now;
                $end = $end / 60;
                $end = $end / 60;
                $end = $end / 24;
                $end = substr( $end, 0, 3 );
                $odc = explode('.', $end);
                if ( $odc[1] > 5 )
                {
                        $end = ceil( $end );
                }
                else
                {
                        $end = floor( $end );
                }
                return $end;
        }
		
		

		
}