<?php
/***************************************************************************
- * Copyright (C) 2003-2007 Polytechnique.org *
+ * Copyright (C) 2003-2010 Polytechnique.org *
* http://opensource.polytechnique.org/ *
* *
* This program is free software; you can redistribute it and/or modify *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
***************************************************************************/
-class ProfileAddress extends ProfileNoSave
+class ProfileSettingAddress extends ProfileSettingGeocoding
{
+ private $bool;
+ private $pub;
+
+ public function __construct()
+ {
+ $this->bool = new ProfileSettingBool();
+ $this->pub = new ProfileSettingPub();
+ }
+
+ private function cleanAddress(ProfilePage &$page, $addrid, array &$address)
+ {
+ if (!isset($address['tel'])) {
+ $address['tel'] = array();
+ }
+ $profiletel = new ProfileSettingPhones('address', $addrid);
+ $address['tel'] = $profiletel->value($page, 'tel', $address['tel'], $s);
+ $address['current'] = $this->bool->value($page, 'current', $address['current'], $s);
+ $address['temporary'] = $this->bool->value($page, 'temporary', $address['temporary'], $s);
+ $address['secondary'] = $this->bool->value($page, 'secondary', $address['secondary'], $s);
+ $address['mail'] = $this->bool->value($page, 'mail', $address['mail'], $s);
+ $address['pub'] = $this->pub->value($page, 'pub', $address['pub'], $s);
+ }
+
public function value(ProfilePage &$page, $field, $value, &$success)
{
- $success = true;
+ $init = false;
if (is_null($value)) {
- return $page->values['addresses'];
+ $value = $page->values['addresses'];
+ $init = true;
}
- foreach ($value as $key=>&$adr) {
- if ($adr['removed']) {
+ foreach ($value as $key => &$address) {
+ if (isset($address['removed']) && $address['removed']) {
unset($value[$key]);
}
}
+ $current = 0;
+ $success = true;
+ foreach ($value as $key => &$address) {
+ if (isset($address['current']) && $address['current']) {
+ $current++;
+ }
+ }
+ if ($current == 0 && count($value) > 0) {
+ foreach ($value as &$address) {
+ $address['current'] = true;
+ break;
+ }
+ } elseif ($current > 1) {
+ $success = false;
+ }
+ foreach ($value as $key => &$address) {
+ if (!trim($address['text'])) {
+ unset($value[$key]);
+ } elseif (!$init) {
+ $this->geocodeAddress($address, $s);
+ $success = $success && $s;
+ }
+ $this->cleanAddress($page, $key, $address);
+ }
return $value;
}
+
+ public function saveAddress($pid, $addrid, array &$address, $type)
+ {
+ require_once 'geocoding.inc.php';
+
+ $flags = new PlFlagSet();
+ $flags->addFlag('current', $address['current']);
+ $flags->addFlag('temporary', $address['temporary']);
+ $flags->addFlag('secondary', $address['secondary']);
+ $flags->addFlag('mail', $address['mail']);
+ $flags->addFlag('cedex', $address['cedex'] =
+ (strpos(strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"),
+ array("", "\n"), $address['text'])), 'CEDEX')) !== false);
+ Geocoder::getAreaId($address, "administrativeArea");
+ Geocoder::getAreaId($address, "subAdministrativeArea");
+ Geocoder::getAreaId($address, "locality");
+ XDB::execute("INSERT INTO profile_addresses (pid, type, id, flags, accuracy,
+ text, postalText, postalCode, localityId,
+ subAdministrativeAreaId, administrativeAreaId,
+ countryId, latitude, longitude, updateTime, pub, comment,
+ north, south, east, west)
+ VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?},
+ {?}, {?}, FROM_UNIXTIME({?}), {?}, {?}, {?}, {?}, {?}, {?})",
+ $pid, $type, $addrid, $flags, $address['accuracy'],
+ $address['text'], $address['postalText'], $address['postalCode'], $address['localityId'],
+ $address['subAdministrativeAreaId'], $address['administrativeAreaId'],
+ $address['countryId'], $address['latitude'], $address['longitude'],
+ $address['updateTime'], $address['pub'], $address['comment'],
+ $address['north'], $address['south'], $address['east'], $address['west']);
+ }
+
+ public function save(ProfilePage &$page, $field, $value)
+ {
+ XDB::execute("DELETE FROM profile_addresses
+ WHERE pid = {?} AND type = 'home'",
+ $page->pid());
+ XDB::execute("DELETE FROM profile_phones
+ WHERE pid = {?} AND link_type = 'address'",
+ $page->pid());
+ foreach ($value as $addrid => &$address) {
+ $this->saveAddress($page->pid(), $addrid, $address, 'home');
+ $profiletel = new ProfileSettingPhones('address', $addrid);
+ $profiletel->saveTels($page->pid(), 'tel', $address['tel']);
+ }
+ }
}
-class ProfileAddresses extends ProfilePage
+class ProfileSettingAddresses extends ProfilePage
{
protected $pg_template = 'profile/adresses.tpl';
public function __construct(PlWizard &$wiz)
{
parent::__construct($wiz);
- $this->settings['addresses'] = new ProfileAddress();
+ $this->settings['addresses'] = new ProfileSettingAddress();
+ $this->watched['addresses'] = true;
}
- protected function fetchData()
+ protected function _fetchData()
{
- if (count($this->orig) > 0) {
- $this->values = $this->orig;
- return;
+ $res = XDB::query("SELECT id, accuracy, text, postalText,
+ postalCode, localityId, subAdministrativeAreaId, administrativeAreaId,
+ countryId, latitude, longitude, pub, comment, UNIX_TIMESTAMP(updateTime) AS updateTime,
+ north, south, east, west,
+ FIND_IN_SET('current', flags) AS current,
+ FIND_IN_SET('temporary', flags) AS temporary,
+ FIND_IN_SET('secondary', flags) AS secondary,
+ FIND_IN_SET('mail', flags) AS mail,
+ FIND_IN_SET('cedex', flags) AS cedex
+ FROM profile_addresses
+ WHERE pid = {?} AND type = 'home'
+ ORDER BY id",
+ $this->pid());
+ if ($res->numRows() == 0) {
+ $this->values['addresses'] = array();
+ } else {
+ $this->values['addresses'] = $res->fetchAllAssoc();
}
- // Build the addresses tree
- $res = XDB::query("SELECT adrid AS id, adr1, adr2, adr3,
- postcode, city, cityid, region, regiontxt,
- fax, glat, glng, datemaj, pub,
- FIND_IN_SET('res-secondaire', statut) AS secondaire,
- FIND_IN_SET('courrier', statut) AS mail,
- FIND_IN_SET('temporary', statut) AS temporary,
- FIND_IN_SET('active', statut) AS current,
- FIND_IN_SET('coord-checked', statut) AS checked,
- FIND_IN_SET('coord-valid', statut) AS valid
- FROM adresses
- WHERE uid = {?}
- ORDER BY adrid",
- S::i('uid'));
- $this->values['addresses'] = $res->fetchAllAssoc();
- $res = XDB::iterRow("SELECT adrid, telid, tel_type, tel_pub, tel
- FROM tels
- WHERE uid = {?}
- ORDER BY adrid",
- S::i('uid'));
+ $res = XDB::iterator("SELECT link_id AS addrid, tel_type AS type, pub, display_tel AS tel, comment
+ FROM profile_phones
+ WHERE pid = {?} AND link_type = 'address'
+ ORDER BY link_id",
+ $this->pid());
$i = 0;
- while (list($adrid, $telid, $type, $pub, $tel) = $res->next()) {
- while ($this->values['addresses'][$i]['id'] < $adrid) {
+ $adrNb = count($this->values['addresses']);
+ while ($tel = $res->next()) {
+ $addrid = $tel['addrid'];
+ unset($tel['addrid']);
+ while ($i < $adrNb && $this->values['addresses'][$i]['id'] < $addrid) {
$i++;
}
+ if ($i >= $adrNb) {
+ break;
+ }
$address =& $this->values['addresses'][$i];
if (!isset($address['tel'])) {
$address['tel'] = array();
}
- if ($address['id'] == $adrid) {
- $address['tel'][] = array('id' => $telid,
- 'type' => $type,
- 'pub' => $pub,
- 'tel' => $tel);
+ if ($address['id'] == $addrid) {
+ $address['tel'][] = $tel;
}
}
- parent::fetchData();
- }
-
- protected function saveData()
- {
- parent::saveData();
- }
-
- public function prepare(PlatalPage &$page)
- {
- parent::prepare($page);
+ foreach ($this->values['addresses'] as $id => &$address) {
+ if (!isset($address['tel'])) {
+ $address['tel'] = array(
+ 0 => array(
+ 'type' => 'fixed',
+ 'tel' => '',
+ 'pub' => 'private',
+ 'comment' => '',
+ )
+ );
+ }
+ unset($address['id']);
+ $address['changed'] = 0;
+ $address['removed'] = 0;
+ }
}
}