Prepares database for job terms.
[platal.git] / include / profil.func.inc.php
index 7b418d7..e8ecb59 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2009 Polytechnique.org                              *
+ *  Copyright (C) 2003-2010 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -20,7 +20,6 @@
  ***************************************************************************/
 
 
-require_once('applis.func.inc.php');
 
 function replace_ifset(&$var,$req) {
     if (Env::has($req)){
@@ -62,16 +61,12 @@ function diff_user_details(&$a, &$b, $view = 'private') { // compute $c = $a - $
                 switch ($val) {
                     case 'adr' : if (!($c['adr'] = diff_user_addresses($a[$val], $bvar, $view))) unset($c['adr']); break;
                     case 'adr_pro' : if (!($c['adr_pro'] = diff_user_pros($a[$val], $bvar, $view))) unset($c['adr_pro']); break;
-                    case 'mobile' : if (same_tel($a[$val], $bvar)) unset($c['mobile']); break;
+                    case 'tels' : if (!($c['tels'] = diff_user_tels($a[$val], $bvar, $view))) unset($c['tels']); break;
                 }
             }
         }
     }
-    // don't modify mobile if you don't have the right
-    if (isset($b['mobile_pub']) && !has_user_right($b['mobile_pub'], $view) && isset($c['mobile']))
-        unset($c['mobile']);
-    if (isset($b['web_pub']) && !has_user_right($b['web_pub'], $view) && isset($c['web']))
-        unset($c['web']);
+    // don't modify freetext if you don't have the right
     if (isset($b['freetext_pub']) && !has_user_right($b['freetext_pub'], $view) && isset($c['freetext']))
         unset($c['freetext']);
     if (!count($c))
@@ -80,15 +75,15 @@ function diff_user_details(&$a, &$b, $view = 'private') { // compute $c = $a - $
 }
 
 function same_tel(&$a, &$b) {
-    $numbera = preg_replace('/[^0-9]/', '', (string) $a);
-    $numberb = preg_replace('/[^0-9]/', '', (string) $b);
+    $numbera = format_phone_number((string) $a);
+    $numberb = format_phone_number((string) $b);
     return $numbera === $numberb;
 }
 function same_address(&$a, &$b) {
     return
         (same_field($a['adr1'],$b['adr1'])) &&
-        (same_field($a['adr1'],$b['adr1'])) &&
-        (same_field($a['adr1'],$b['adr1'])) &&
+        (same_field($a['adr2'],$b['adr2'])) &&
+        (same_field($a['adr3'],$b['adr3'])) &&
         (same_field($a['postcode'],$b['postcode'])) &&
         (same_field($a['city'],$b['city'])) &&
         (same_field($a['countrytxt'],$b['countrytxt'])) &&
@@ -108,7 +103,7 @@ function same_field(&$a, &$b) {
             if (!isset($b[$val]) || !same_field($avar, $b[$val])) return false;
         return true;
     } elseif (is_string($a))
-        return (strtoupper($a) == strtoupper($b));
+        return (mb_strtoupper($a) == mb_strtoupper($b));
 }
 function diff_user_tel(&$a, &$b) {
     $c = $a;
@@ -126,49 +121,57 @@ function diff_user_tel(&$a, &$b) {
     return $c;
 }
 
-function diff_user_address($a, $b) {
-    if (isset($b['pub']) && isset($a['pub']) && has_user_right($b['pub'], $a['pub']))
-        $a['pub'] = $b['pub'];
-    if (isset($b['tels'])) {
-        $bvar = $b['tels'];
-
-        $telids_b = array();
-        foreach ($bvar as $i => $telb) $telids_b[$telb['telid']] = $i;
+function diff_user_tels(&$a, &$b)
+{
+    $c = $a;
+    $telids_b = array();
+    foreach ($b as $i => $telb) $telids_b[$telb['telid']] = $i;
 
-        if (isset($a['tels']))
-            $avar = $a['tels'];
-        else
-            $avar = array();
-        $ctels = $avar;
-        foreach ($avar as $j => $tela) {
-            if (isset($tela['telid'])) {
-                // if b has a tel with the same telid, compute diff
-                if (isset($telids_b[$tela['telid']])) {
-                    if (!($ctels[$j] = diff_user_tel($tela, $varb[$telids_b[$tela['adrid']]])))
-                        unset($ctels[$j]);
-                    unset($telids_b[$tela['telid']]);
+    foreach ($a as $j => $tela) {
+        if (isset($tela['telid'])) {
+            // if b has a tel with the same telid, compute diff
+            if (isset($telids_b[$tela['telid']])) {
+                if (!($c[$j] = diff_user_tel($tela, $b[$telids_b[$tela['adrid']]]))) {
+                    unset($c[$j]);
                 }
-            } else {
-                // try to find a match in b
-                foreach ($bvar as $i => $telb) {
-                    if (same_tel($tela['tel'], $telb['tel'])) {
-                        $tela['telid'] = $telb['telid'];
-                        if (!($ctels[$j] = diff_user_tel($tela, $telb)))
-                            unset($ctels[$j]);
-                        unset($telids_b[$tela['telid']]);
-                        break;
+                unset($telids_b[$tela['telid']]);
+            }
+        } else {
+            // try to find a match in b
+            foreach ($b as $i => $telb) {
+                if (same_tel($tela['tel'], $telb['tel'])) {
+                    $tela['telid'] = $telb['telid'];
+                    if (!($c[$j] = diff_user_tel($tela, $telb))) {
+                        unset($c[$j]);
                     }
+                    unset($telids_b[$tela['telid']]);
+                    break;
                 }
             }
         }
+    }
 
-        foreach ($telids_b as $telidb => $i)
-            $ctels[] = array('telid' => $telidb, 'remove' => 1);
+    foreach ($telids_b as $telidb => $i)
+        $c[] = array('telid' => $telidb, 'remove' => 1);
+    return $c;
+}
+
+function diff_user_address($a, $b) {
+    if (isset($b['pub']) && isset($a['pub']) && has_user_right($b['pub'], $a['pub']))
+        $a['pub'] = $b['pub'];
+    if (isset($b['tels'])) {
+        if (isset($a['tels'])) {
+            $avar = $a['tels'];
+        } else {
+            $avar = array();
+        }
+        $ctels = diff_user_tels($avar, $b['tels']);
 
         if (!count($ctels)) {
             $b['tels'] = $avar;
-        } else
+        } else {
             $a['tels'] = $ctels;
+        }
     }
 
     foreach ($a as $val => $avar) {
@@ -229,13 +232,18 @@ function diff_user_pro($a, &$b, $view = 'private') {
     }
     if (isset($b['adr_pub']) && isset($a['adr_pub']) && has_user_right($b['adr_pub'], $a['adr_pub']))
         $a['adr_pub'] = $b['adr_pub'];
-    if (isset($b['tel_pub']) && !has_user_right($b['tel_pub'], $view)) {
-        unset($a['tel']);
-        unset($a['fax']);
-        unset($a['mobile']);
+    if (isset($b['tels'])) {
+        if (isset($a['tels']))
+            $avar = $a['tels'];
+        else
+            $avar = array();
+        $ctels = diff_user_tels($avar, $b['tels']);
+
+        if (!count($ctels)) {
+            $b['tels'] = $avar;
+        } else
+            $a['tels'] = $ctels;
     }
-    if (isset($b['tel_pub']) && isset($a['tel_pub']) && has_user_right($b['tel_pub'], $a['tel_pub']))
-        $a['tel_pub'] = $b['tel_pub'];
     if (isset($b['email_pub']) && !has_user_right($b['email_pub'], $view))
         unset($a['email']);
     if (isset($b['email_pub']) && isset($a['email_pub']) && has_user_right($b['email_pub'], $a['email_pub']))
@@ -284,5 +292,88 @@ function diff_user_pros(&$a, &$b, $view = 'private') {
     return $c;
 }
 
+function format_phone_number($tel)
+{
+    $tel = trim($tel);
+    if (substr($tel, 0, 3) === '(0)') {
+        $tel = '33' . $tel;
+    }
+    $tel = preg_replace('/\(0\)/',  '', $tel);
+    $tel = preg_replace('/[^0-9]/', '', $tel);
+    if (substr($tel, 0, 2) === '00') {
+        $tel = substr($tel, 2);
+    } else if(substr($tel, 0, 1) === '0') {
+        $tel = '33' . substr($tel, 1);
+    }
+    return $tel;
+}
+
+function format_display_number($tel, &$error, $format = array('format'=>'','phoneprf'=>''))
+{
+    $error = false;
+    $ret = '';
+    $tel_length = strlen($tel);
+    if((!isset($format['phoneprf'])) || ($format['phoneprf'] == '')) {
+        $res = XDB::query("SELECT phonePrefix AS phoneprf, phoneFormat AS format
+                             FROM geoloc_countries
+                            WHERE phonePrefix = {?} OR phonePrefix = {?} OR phonePrefix = {?}
+                            LIMIT 1",
+                          substr($tel, 0, 1), substr($tel, 0, 2), substr($tel, 0, 3));
+        if ($res->numRows() == 0) {
+            $error = true;
+            return '+' . $tel;
+        }
+        $format = $res->fetchOneAssoc();
+    }
+    if ($format['format'] == '') {
+        $format['format'] = '+p';
+    }
+    $j = 0;
+    $i = strlen($format['phoneprf']);
+    $length_format = strlen($format['format']);
+    while (($i < $tel_length) && ($j < $length_format)){
+        if ($format['format'][$j] == '#'){
+            $ret .= $tel[$i];
+            $i++;
+        } else if ($format['format'][$j] == 'p') {
+            $ret .= $format['phoneprf'];
+        } else {
+            $ret .= $format['format'][$j];
+        }
+        $j++;
+    }
+    for (; $i < $tel_length - 1; $i += 2) {
+        $ret .= ' ' . substr($tel, $i, 2);
+    }
+    //appends last alone number to the last block
+    if ($i < $tel_length) {
+        $ret .= substr($tel, $i);
+    }
+    return $ret;
+}
+
+/**
+ * Extract search token from term
+ * @param $term a utf-8 string that can contain any char
+ * @param an array of elementary tokens
+ */
+function tokenize_job_term($term)
+{
+    $term = mb_strtoupper(replace_accent($term));
+    $term = str_replace(array('/', ',', '(', ')', '"', '&', '»', '«'), ' ', $term);
+    $tokens = explode(' ', $term);
+    static $not_tokens = array('ET','AND','DE','DES','DU','D\'','OU','L\'','LA','LE','LES','PAR','AU','AUX','EN','SUR','UN','UNE','IN');
+    foreach ($tokens as &$t) {
+        if (substr($t, 1, 1) == '\'' && in_array(substr($t, 0, 2), $not_tokens)) {
+            $t = substr($t, 2);
+        }
+        if (strlen($t) == 1 || in_array($t, $not_tokens)) {
+            $t = false;
+            continue;
+        }
+    }
+    return array_filter($tokens);
+}
+
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>