Merge branch 'platal-0.10.2'
[platal.git] / include / validations / entreprises.inc.php
index 2e1cd9a..6becf7a 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2008 Polytechnique.org                              *
+ *  Copyright (C) 2003-2010 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -36,22 +36,45 @@ class EntrReq extends Validate
 
     public $tel;
     public $fax;
+    public $address;
 
-    //TODO: addresses
+    public $suggestions;
 
     // }}}
     // {{{ constructor
 
-    public function __construct(User &$_user, $_id, $_name, $_acronym, $_url, $_email, $_tel, $_fax, $_stamp = 0)
+    public function __construct(User &$_user, $_id, $_name, $_acronym, $_url, $_email, $_tel, $_fax, $_address, $_stamp = 0)
     {
-        parent::__construct($_user, true, 'entreprise', $_stamp);
-        $this->id        = $_id;
-        $this->name      = $_name;
-        $this->acronym   = $_acronym;
-        $this->url       = $_url;
-        $this->email     = $_email;
-        $this->tel       = $_tel;
-        $this->fax       = $_fax;
+        parent::__construct($_user, false, 'entreprise', $_stamp);
+        $this->id       = $_id;
+        $this->name     = $_name;
+        $this->acronym  = $_acronym;
+        $this->url      = $_url;
+        $this->email    = $_email;
+        $this->tel      = $_tel;
+        $this->fax      = $_fax;
+        $this->address  = $_address;
+
+        $_name       = preg_replace('/[^0-9a-z]/i', ' ', strtolower(replace_accent($_name)));
+        $name        = explode(" ", $_name);
+        $name_array  = array_map("trim", $name);
+        $length      = count($name_array);
+        $where       = "";
+        for ($i = 0; $i < $length; $i++) {
+            if (strlen($name_array[$i]) > 2) {
+                if ($where !== "") {
+                    $where .= " OR ";
+                }
+                $where .= "name LIKE '%" . $name_array[$i] . "%'";
+            }
+        }
+        $res = XDB::iterator('SELECT  name
+                                FROM  profile_job_enum
+                               WHERE  ' . $where);
+        $this->suggestions = "| ";
+        while ($sug = $res->next()) {
+            $this->suggestions .= $sug['name'] . " | ";
+        }
     }
 
     // }}}
@@ -123,34 +146,57 @@ class EntrReq extends Validate
 
     public function commit()
     {
+        // TODO: use address and phone classes to update profile_job_enum and profile_phones once they are done.
+
         $res = XDB::query('SELECT  id
                              FROM  profile_job_enum
                             WHERE  name = {?}',
                           $this->name);
         if ($res->numRows() != 1) {
-            require_once("profil.func.inc.php");
+            require_once 'profil.func.inc.php';
+            require_once 'geocoding.inc.php';
 
             XDB::execute('INSERT INTO  profile_job_enum (name, acronym, url, email, holdingid, NAF_code, AX_code)
                                VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?})',
                          $this->name, $this->acronym, $this->url, $this->email,
                          $this->holdingid, $this->NAF_code, $this->AX_code);
+
             $jobid = XDB::insertId();
             $display_tel = format_display_number($this->tel, $error_tel);
-            $display_fax =format_display_number($this->fax, $error_fax);
-            XDB::execute('INSERT INTO  profile_phones (uid, link_type, link_id, tel_id, tel_type,
+            $display_fax = format_display_number($this->fax, $error_fax);
+            XDB::execute("INSERT INTO  profile_phones (pid, link_type, link_id, tel_id, tel_type,
                                        search_tel, display_tel, pub)
-                               VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}),
-                                       ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
-                         $jobid, 'hq', $this->id, 0, 'fixed', format_phone_number($this->tel), $display_tel, 'public', 
-                         $jobid, 'hq', $this->id, 1, 'fax', format_phone_number($this->fax), $display_fax, 'public');
+                               VALUES  ({?}, 'hq', 0, 0, 'fixed', {?}, {?}, 'public'),
+                                       ({?}, 'hq', 0, 1, 'fax', {?}, {?}, 'public')",
+                         $jobid, format_phone_number($this->tel), $display_tel,
+                         $jobid, format_phone_number($this->fax), $display_fax);
+
+            $gmapsGeocoder = new GMapsGeocoder();
+            $address = $gmapsGeocoder->getGeocodedAddress($this->address);
+            Geocoder::getAreaId($address, 'administrativeArea');
+            Geocoder::getAreaId($address, 'subAdministrativeArea');
+            Geocoder::getAreaId($address, 'locality');
+            XDB::execute("INSERT INTO  profile_addresses (jobid, type, id, accuracy,
+                                                          text, postalText, postalCode, localityId,
+                                                          subAdministrativeAreaId, administrativeAreaId,
+                                                          countryId, latitude, longitude, updateTime,
+                                                          north, south, east, west)
+                               VALUES  ({?}, 'hq', 0, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?},
+                                        {?}, {?}, FROM_UNIXTIME({?}), {?}, {?}, {?}, {?})",
+                         $jobid, $this->address['accuracy'], $this->address['text'], $this->address['postalText'],
+                         $this->address['postalCode'], $this->address['localityId'],
+                         $this->address['subAdministrativeAreaId'], $this->address['administrativeAreaId'],
+                         $this->address['countryId'], $this->address['latitude'], $this->address['longitude'],
+                         $this->address['updateTime'], $this->address['north'], $this->address['south'],
+                         $this->address['east'], $this->address['west']);
         } else {
             $jobid = $res->fetchOneCell();
             $success = true;
         }
         return XDB::execute('UPDATE  profile_job
                                 SET  jobid = {?}
-                              WHERE  uid = {?} AND id = {?}',
-                            $jobid, $this->user->id(), $this->id);
+                              WHERE  pid = {?} AND id = {?}',
+                            $jobid, $this->user->profile()->id(), $this->id);
     }
 
     // }}}