PHP Classes

File: villes/php4/ocity.cls.php

Recommend this page to a friend!
  Classes of Johan Barbier   Cities and zip codes   villes/php4/ocity.cls.php   Download  
File: villes/php4/ocity.cls.php
Role: Class source
Content type: text/plain
Description: PHP4 ocity class
Class: Cities and zip codes
Lookup for cities and zip codes using AJAX
Author: By
Last change: Removed PHP4 errors
Date: 17 years ago
Size: 6,471 bytes
 

Contents

Class file image Download
<?php
function array_intersect_key($arr1, $arr2) {
  
$res = array();
   foreach(
$arr1 as $key=>$value) {
      
$push = true;
       for (
$i = 1; $i < func_num_args(); $i++) {
          
$actArray = func_get_arg($i);
           if (
gettype($actArray) != 'array') return false;
           if (!
array_key_exists($key, $actArray)) $push = false;
       }
       if (
$push) $res[$key] = $arr1[$key];
   }
   return
$res;
}

function
array_combine($a, $b) {
  
$c = array();
   if (
is_array($a) && is_array($b))
       while (list(,
$va) = each($a))
           if (list(,
$vb) = each($b))
              
$c[$va] = $vb;
           else
               break
1;
   return
$c;
}

class
ocity {
   
/**
    * private array aNomVilles, aCpVilles, aSoundexVilles, aPhonexVilles : data arrays
    */
   
var $aNomVilles = array ();
    var
$aCpVilles = array ();
    var
$aSoundexVilles = array ();
    var
$aPhonexVilles = array ();
   
/**
    * public static string sResult : string used to store the result of the query
    */
   
var $sResult = '';

   
/**
    * public static string sSearch : string used to store the query
    */
   
var $sSearch = '';
    var
$_post = '';

    var
$oSoundex;
    var
$oPhonex;
   
/**
    * public function __construct
    * constructor
    * @Param string dataFile : data filename.
    */
   
function ocity ($dataFile = 'data.dat', $soundex, $phonex) {
       
$this -> oSoundex = $soundex;
       
$this -> oPhonex = $phonex;
       
$aLines = file ($dataFile);
        foreach (
$aLines as $line) {
           
$aWord = explode (';', $line);
           
$this -> aCpVilles[] = $aWord[0];
           
$this -> aNomVilles[] = $aWord[1];
           
$this -> aSoundexVilles[] = $aWord[2];
           
$this -> aPhonexVilles[] = $aWord[3];
        }
    }

   
/**
    * callback public static function getFirst
    * returns an array with the results of the query
    * @Returns array
    */
   
function getFirst ($val) {
        return (
substr ($val, 0, strlen ($this -> sSearch)) === $this -> sSearch);
    }

    function
mapLev ($val) {
        return
levenshtein ($this -> _post, $val);
    }

   
/**
    * public function getSearch
    * returns the string result, called by the xmlhttp method
    * @Returns string sResult
    */
   
public function getSearch () {
        if (isset (
$_POST['data']) && '' !== trim ($_POST['data'])) {
           
$this -> sSearch = strtolower ($_POST['data']);
            if (
$_POST['type'] === '0') {
                if (
$this -> sSearch === '*') {
                   
$aTmpCP = $this -> aCpVilles;
                   
$aTmpVilles = $this -> aNomVilles;
                    if (isset (
$_POST['sort']) && in_array ($_POST['sort'], array ('0', '2'))) {
                       
array_multisort ($aTmpVilles, $aTmpCP);
                    } else {
                       
array_multisort ($aTmpCP, $aTmpVilles);
                    }
                }
                elseif (
is_numeric ($this -> sSearch)) {
                   
$aTmpCP = array_filter ($this -> aCpVilles, array ($this, 'getFirst'));
                   
$aTmpVilles = array_intersect_key ($this -> aNomVilles, $aTmpCP);
                    if (isset (
$_POST['sort']) && in_array ($_POST['sort'], array ('0', '1'))) {
                       
array_multisort ($aTmpCP, $aTmpVilles);
                    } else {
                       
array_multisort ($aTmpVilles, $aTmpCP);
                    }
                } else {
                   
$aTmpVilles = array_filter ($this -> aNomVilles, array ($this, 'getFirst'));
                   
$aTmpCP = array_intersect_key ($this -> aCpVilles, $aTmpVilles);
                    if (isset (
$_POST['sort']) && in_array ($_POST['sort'], array ('0', '2'))) {
                       
array_multisort ($aTmpVilles, $aTmpCP);
                    } else {
                       
array_multisort ($aTmpCP, $aTmpVilles);
                    }
                }
            } elseif (
$_POST['type'] === '1') {
                if (!
is_numeric ($this -> sSearch)) {
                   
$this -> oSoundex -> build ($this -> sSearch);
                   
$this -> sSearch = $this -> oSoundex -> sString;
                   
$this -> _post = strtolower ($_POST['data']);
                   
$aDump = array_filter ($this -> aSoundexVilles, array ($this, 'getFirst'));
                   
$aTmpVilles = array_intersect_key ($this -> aNomVilles, $aDump);
                   
$aTmpCP = array_intersect_key ($this -> aCpVilles, $aTmpVilles);
                    if (isset (
$_POST['sort']) && $_POST['sort'] === '2') {
                       
array_multisort ($aTmpVilles, $aTmpCP);
                    } elseif (isset (
$_POST['sort']) && $_POST['sort'] === '1') {
                       
array_multisort ($aTmpCP, $aTmpVilles);
                    } else {
                       
$aLev = array_map (array ($this, 'mapLev'), $aTmpVilles);
                        if (!empty (
$aLev) && !empty ($aTmpVilles)) {
                           
array_multisort ($aLev, $aTmpVilles);
                           
$aTmpCP = array_intersect_key ($this -> aCpVilles, $aTmpVilles);
                        } else {
                           
$aTmpCP = array ();
                        }
                    }
                } else {
                   
$aTmpCP = array ();
                }
            } else {
                if (!
is_numeric ($this -> sSearch)) {
                   
$this -> oPhonex -> build ($this -> sSearch);
                   
$this -> sSearch = $this -> oPhonex -> sString;
                   
$this -> _post = strtolower ($_POST['data']);
                   
$aDump = array_filter ($this -> aPhonexVilles, array ($this, 'getFirst'));
                   
$aTmpVilles = array_intersect_key ($this -> aNomVilles, $aDump);
                   
$aTmpCP = array_intersect_key ($this -> aCpVilles, $aTmpVilles);
                    if (isset (
$_POST['sort']) && $_POST['sort'] === '2') {
                       
array_multisort ($aTmpVilles, $aTmpCP);
                    } elseif (isset (
$_POST['sort']) && $_POST['sort'] === '1') {
                       
array_multisort ($aTmpCP, $aTmpVilles);
                    } else {
                       
$aLev = array_map (array ($this, 'mapLev'), $aTmpVilles);
                        if (!empty (
$aLev) && !empty ($aTmpVilles)) {
                           
array_multisort ($aLev, $aTmpVilles);
                           
$aTmpCP = array_intersect_key ($this -> aCpVilles, $aTmpVilles);
                        } else {
                           
$aTmpCP = array ();
                        }
                    }
                } else {
                   
$aTmpCP = array ();
                }
            }
           
$iCpt = 0;
           
$this -> sResult .= '<div style="border: 1px solid #000000;width: 250px;background-color: #ffffff;"><span title="Trier par code postal" onclick="search (\''.$_POST['data'].'\',1, '.$_POST['type'].');" style="cursor: pointer; margin: 5px;font-weight: bold;text-align: left;width: 100px;">Code </span><span title="Trier par ville" onclick="search (\''.$_POST['data'].'\',2, '.$_POST['type'].');" style="width: 150px;cursor: pointer; margin: 5px;font-weight: bold; text-align: right;">Ville</span></div>';
            foreach (
$aTmpCP as $clef => $cp) {
               
$sColor = ($iCpt%2 === 0)?'background-color: #cccccc;':'background-color: #ffffff;';
               
$this -> sResult .= '<div style="border: 1px solid #000000;width: 250px;'.$sColor.'"><span onclick="document.getElementById(\'mySearch\').value = this.innerHTML;" style="cursor: pointer; margin: 5px;'.$sColor.'">'.$cp.'</span><span onclick="document.getElementById(\'mySearch\').value = this.innerHTML;" style="cursor: pointer; margin: 5px;'.$sColor.'">'.$aTmpVilles[$clef].'</span></div>';
               
$iCpt ++;
            }
            echo
$this -> sResult;
        } else {
            return
false;
        }
    }
}
?>