Merge branch 'master' of /home/git/platal into profile_edit
[platal.git] / modules / profile / addresses.inc.php
CommitLineData
0b14f91d
FB
1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2007 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22class ProfileAddress extends ProfileNoSave
23{
7568a515
FB
24 private function geolocAddress(&$address, &$success)
25 {
26 require_once 'geoloc.inc.php';
27 if (@$address['parsevalid'] || (@$address['text'] && @$address['changed']) || !@$address['cityid']) {
28 $address = array_merge($address, empty_address());
29 $new = get_address_infos(@$address['text']);
30 if (compare_addresses_text(@$adress['text'], $geotxt = get_address_text($new))
31 || @$address['parsevalid']) {
32 $address = array_merge($address, $new);
33 } else {
34 $success = false;
35 $address = array_merge($address, cut_address(@$address['text']));
36 $address['geoloc'] = $geotxt;
37 $address['geoloc_cityid'] = $new['cityid'];
38 }
39 }
40 $address['text'] = get_address_text($address);
41 unset($address['parsevalid']);
42 unset($address['changed']);
43 }
44
0b14f91d
FB
45 public function value(ProfilePage &$page, $field, $value, &$success)
46 {
7568a515 47 $init = false;
0b14f91d 48 if (is_null($value)) {
7568a515
FB
49 $value = $page->values['addresses'];
50 $init = true;
0b14f91d
FB
51 }
52 foreach ($value as $key=>&$adr) {
7568a515 53 if (@$adr['removed']) {
0b14f91d
FB
54 unset($value[$key]);
55 }
56 }
7568a515
FB
57 $success = true;
58 foreach ($value as $key=>&$adr) {
59 $this->geolocAddress($adr, $s);
60 if (!$init) {
61 $success = $success && $s;
62 }
63 }
0b14f91d
FB
64 return $value;
65 }
66}
67
68class ProfileAddresses extends ProfilePage
69{
70 protected $pg_template = 'profile/adresses.tpl';
71
72 public function __construct(PlWizard &$wiz)
73 {
74 parent::__construct($wiz);
75 $this->settings['addresses'] = new ProfileAddress();
76 }
77
78 protected function fetchData()
79 {
80 if (count($this->orig) > 0) {
81 $this->values = $this->orig;
82 return;
83 }
84 // Build the addresses tree
7568a515
FB
85 $res = XDB::query("SELECT a.adrid AS id, a.adr1, a.adr2, a.adr3,
86 a.postcode, a.city, a.cityid, a.region, a.regiontxt,
87 a.fax, a.glat, a.glng, a.datemaj, a.pub,
88 a.country, gp.pays AS countrytxt, gp.display,
89 FIND_IN_SET('res-secondaire', a.statut) AS secondaire,
90 FIND_IN_SET('courrier', a.statut) AS mail,
91 FIND_IN_SET('temporary', a.statut) AS temporary,
92 FIND_IN_SET('active', a.statut) AS current
93 FROM adresses AS a
94 INNER JOIN geoloc_pays AS gp ON(gp.a2 = a.country)
95 WHERE uid = {?} AND NOT FIND_IN_SET('pro', statut)
0b14f91d
FB
96 ORDER BY adrid",
97 S::i('uid'));
98 $this->values['addresses'] = $res->fetchAllAssoc();
99
7568a515
FB
100 $res = XDB::iterator("SELECT adrid, tel_type AS type, tel_pub AS pub, tel
101 FROM tels
102 WHERE uid = {?}
103 ORDER BY adrid",
104 S::i('uid'));
0b14f91d 105 $i = 0;
7568a515
FB
106 while ($tel = $res->next()) {
107 $adrid = $tel['adrid'];
108 unset($tel['adrid']);
0b14f91d
FB
109 while ($this->values['addresses'][$i]['id'] < $adrid) {
110 $i++;
111 }
112 $address =& $this->values['addresses'][$i];
113 if (!isset($address['tel'])) {
114 $address['tel'] = array();
115 }
116 if ($address['id'] == $adrid) {
7568a515 117 $address['tel'][] = $tel;
0b14f91d
FB
118 }
119 }
7568a515
FB
120 foreach ($this->values['addresses'] as $id=>&$address) {
121 unset($address['id']);
122 }
0b14f91d
FB
123 parent::fetchData();
124 }
125
126 protected function saveData()
127 {
128 parent::saveData();
129 }
130
131 public function prepare(PlatalPage &$page)
132 {
133 parent::prepare($page);
134 }
135}
136
137// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
138?>