<?php

class czUtils {
    public static function frenchDateFormat($timestamp, $format = '%B %Y') {
        return utf8_encode(ucwords(strftime($format, $timestamp)));
    }

    public static function frenchDateFormatToTimestamp($date_format) {
        if (!preg_match('/^\d{2}\/\d{2}\/\d{4}$/', $date_format)) {
            throw new Exception(sprintf('Le format de date suivant n\'est pas valide : "%s" (requis : JJ/MM/AAAA)', $date_format));
            return false;
        }
        list($d, $m, $y) = explode('/', $date_format);
        if (false === $timestamp = strtotime("$y-$m-$d")) {
            throw new Exception(sprintf('La date suivante n\'est pas valide : "%s"', $date_format));
            return false;
        }
        return $timestamp;
    }

	public static function getLatitudeAndLongitude($address) {
$result = false;
        $xml = simplexml_load_string(utf8_encode(file_get_contents(sprintf('https://maps.google.com/maps/geo?q=%s&output=xml&key=%s', urlencode($address), sfConfig::get('app_gmap')))));
        $code = (int)$xml->Response->Status->code;
		//printf("[%d] %s\n", $code, $address);
        if (200 == $code) {
            // Adresse correcte
            $coordinates = $xml->Response->Placemark->Point->coordinates;
            list($lng, $lat, $altitude) = explode(',', $coordinates);

            $result = array($lat, $lng);
        } elseif (601 == $code) {
            // Adresse manquante
            if ('' != $type) {
                // print_r(sprintf("\n" . '%s: Adresse "%s" complète inconnue pour "%s"', $type, $address, $name));
            }
            $result =  false;
        } elseif (602 == $code) {
            // // Adresse invalide
            // set_time_limit(60);
            // $address = trim($object->getVille());
            // $xml = simplexml_load_string(utf8_encode(file_get_contents(sprintf('https://maps.google.com/maps/geo?q=%s&output=xml&key=%s', urlencode($address), sfConfig::get('app_sfGMaps_key')))));
            // $code = (int)$xml->Response->Status->code;
            // if (200 == $code) {
            // // Adresse correcte
            // $coordinates = $xml->Response->Placemark->Point->coordinates;
            // list($lng, $lat, $altitude) = explode(',', $coordinates);
            // $object->setLatitude($lat);
            // $object->setLongitude($lng);
            // $object->save();
            // } elseif (601 == $code) {
            // // Adresse manquante
            // if ('' != $type) {
            // print_r(sprintf("\n" . '%s: Ville "%s" inconnue pour "%s"', $type, $address, $name));
            // }
            // } elseif (602 == $code) {
            // // Adresse invalide
            // if ('' != $type) {
            // print_r(sprintf("\n" . '%s: Ville "%s" invalide pour "%s"', $type, $address, $name));
            // }
            // }
        }
        return $result;
    }

	public static function getLatestNodes($class, $count = -1, $dateMethod = 'getPublishedAt'){
		$nodesIterator = new FilteredContentTreeNodeIterator(ContentTree::instance(), array($class));

		$nodes = array();
		while ($node = $nodesIterator->next()) {
			$page = $node->getPage();
			$publishedAt = $page->$dateMethod(null);
			while (isset($nodes[$publishedAt])) {
				$publishedAt++;
			}
			$nodes[$publishedAt] = array('node' => $node, 'page' => $page);
		}

		krsort($nodes);
		if($count>0){
			for($i = count($nodes) - $count; $i >0; $i--){
				array_pop($nodes);
			}
		}
		return $nodes;

	}
}