Adds the possibility to forge an url linking to a profile page from networking addresses
[platal.git] / upgrade / fusionax-0.0.1 / phones.php
CommitLineData
de15699b
GB
1#!/usr/bin/php5
2<?php
3require_once 'connect.db.inc.php';
4
5//next two are required to include 'profil.func.inc.php'
6require_once 'xorg.inc.php';
7$page = new XorgPage(null);
8
9require_once 'profil.func.inc.php';
10
11$globals->debug = 0; //do not store backtraces
12
13$warnings = 0;
14
15// Import from auth_user_quick
16echo "\nImporting mobile phone numbers from auth_user_quick...\n";
17$phones = XDB::iterRow("SELECT user_id, profile_mobile_pub, profile_mobile FROM auth_user_quick WHERE profile_mobile <> ''");
18while (list($uid, $pub, $phone) = $phones->next()) {
19 $fmt_phone = format_phone_number($phone);
20 if($fmt_phone != '')
21 {
22 $display = format_display_number($fmt_phone, $error);
23 if (!XDB::execute("INSERT INTO telephone (uid, link_type, link_id, tel_id, tel_type, search_tel, display_tel, pub)
24 VALUES ({?}, 'user', 0, 0, 'mobile', {?}, {?}, {?})", $uid, $fmt_phone, $display, $pub)) {
25 echo "WARNING: insert of profile mobile phone number failed for user $uid.\n";
26 $warnings++;
27 }
28 }
29}
30
31
32// Import from entreprises
33echo "\nImporting professional phone numbers from entreprises...\n";
34$phones = XDB::iterator("SELECT uid, entrid, tel, fax, mobile, tel_pub FROM entreprises ORDER BY uid");
35while ($row = $phones->next()) {
36 $request = "INSERT INTO telephone (uid, link_type, link_id, tel_id, tel_type, search_tel, display_tel, pub)
37 VALUES ({?}, 'pro', {?}, {?}, {?}, {?}, {?}, {?})";
38 $fmt_fixed = format_phone_number($row['tel']);
39 $fmt_mobile = format_phone_number($row['mobile']);
40 $fmt_fax = format_phone_number($row['fax']);
41 if ($fmt_fixed != '')
42 {
43 $disp_fixed = format_display_number($fmt_fixed, $error);
44 if (!XDB::execute($request, $row['uid'], $row['entrid'], 0, 'fixed', $fmt_fixed, $disp_fixed, $row['tel_pub'])) {
45 echo 'WARNING: insert of professional fixed phone number failed for user ' . $row['uid'] . ' and entreprise ' . $row['entrid'] . ".\n";
46 $warnings++;
47 }
48 }
49 if ($fmt_mobile != '')
50 {
51 $disp_mobile = format_display_number($fmt_mobile, $error);
52 if (!XDB::execute($request, $row['uid'], $row['entrid'], 1, 'mobile', $fmt_mobile, $disp_mobile, $row['tel_pub'])) {
53 echo 'WARNING: insert of professional mobile number failed for user ' . $row['uid'] . ' and entreprise ' . $row['entrid'] . ".\n";
54 $warnings++;
55 }
56 }
57 if ($fmt_fax != '')
58 {
59 $disp_fax = format_display_number($fmt_fax, $error);
60 if (!XDB::execute($request, $row['uid'], $row['entrid'], 2, 'fax', $fmt_fax, $disp_fax, $row['tel_pub'])) {
61 echo 'WARNING: insert of professional fax number failed for user ' . $row['uid'] . ' and entreprise ' . $row['entrid'] . ".\n";
62 $warnings++;
63 }
64 }
65}
66
67
68//import from tels
69echo "\nImporting personnal phone numbers from tels...\n";
70$phones = XDB::iterator("SELECT uid, adrid, telid, tel_type, tel_pub, tel FROM tels");
71$conversions = array();
72$autre_count = 0;
73while ($row = $phones->next()) {
74 $fmt_phone = format_phone_number($row['tel']);
75 if ($fmt_phone != '') {
76 $display = format_display_number($fmt_phone, $error);
77 $guess_type = guess_phone_type($row['tel_type'], $fmt_phone);
78
79 switch ($guess_type) {
80 case 'fixed':
81 case 'fax':
82 case 'mobile':
83 if (!XDB::execute("INSERT INTO telephone (uid, link_type, link_id, tel_id, tel_type, search_tel, display_tel, pub)
84 VALUES ({?}, 'address', {?}, {?}, {?}, {?}, {?}, {?})",
85 $row['uid'], $row['adrid'], $row['telid'], $guess_type, $fmt_phone, $display, $row['tel_pub'])) {
86 echo 'WARNING: insert of address phone number failed for user ' . $row['uid'] . ', address ' . $row['adrid']
87 . ' and telephone id ' . $row['telid'] . ".\n";
88 $warnings++;
89 } else {
90 if ($row['tel_type'] == 'Autre') {
91 $autre_count++;
92 } else if (!isset($conversions[$row['tel_type']])) {
93 $conversions[$row['tel_type']] = $guess_type;
94 }
95 }
96 break;
97 case 'conflict':
98 echo 'WARNING: conflict for user ' . $row['uid'] . ', address ' . $row['adrid']
99 . ' and telephone id ' . $row['telid'] . ': type = "' . $row['tel_type']
100 . '", number = "' .$fmt_phone . "\"\n";
101 $warnings++;
102 break;
103 case 'unknown':
104 default:
105 echo 'WARNING: unknown phone type (' . $row['tel_type'] . ') for user ' . $row['uid'] . ', address ' . $row['adrid']
106 . ' and telephone id ' . $row['telid'] . "\n";
107 $warnings++;
108 }
109 }
110}
111
112echo "\nSummary of automatic phone type conversion\n";
113foreach ($conversions as $old => $new) {
114 echo "* $old => $new\n";
115}
116echo "There was also $autre_count conversions from old type 'Autre' to a new one determined by the phone number.\n";
117
118
119
120//end of import
121if ($warnings) {
122 echo "\n----------------------------------------------------------------------\n"
123 . " There is $warnings phone numbers that couldn't be imported.\n"
124 . " They need to be manually inserted.\n";
125}
126echo "\nAfter solving any import problem and checking automatic conversions,\n"
127 . "you can drop useless columns and tables by these requests:\n"
128 . "DROP TABLE IF EXISTS `tels`;\n"
129 . "ALTER TABLE `auth_user_quick` DROP COLUMN `profile_mobile`;\n"
130 . "ALTER TABLE `auth_user_quick` DROP COLUMN `profile_mobile_pub`;\n"
131 . "ALTER TABLE `entreprises` DROP COLUMN `tel`;\n"
132 . "ALTER TABLE `entreprises` DROP COLUMN `fax`;\n"
133 . "ALTER TABLE `entreprises` DROP COLUMN `mobile`;\n"
134 . "ALTER TABLE `entreprises` DROP COLUMN `tel_pub`;\n";
135
136
137// auxilliary functions
138
139function guess_phone_type($str_type, $phone)
140{
141 $str_type = strtolower(trim($str_type));
142
143 // special case for phone type 'autre', guessing by phone number
144 if ($str_type == 'autre') {
145 if (substr($phone, 3) == '336') {
146 return 'mobile';
147 } else {
148 return 'fixed';
149 }
150 }
151
152 if ((strpos($str_type, 'mob') !== false) || (strpos($str_type, 'cell') !== false) || (strpos($str_type, 'port') !== false)) {
153 if (substr($phone, 3) == '336' || substr($phone, 2) != '33') {
154 return 'mobile'; //for France check if number is a mobile one
155 } else {
156 return 'conflict';
157 }
158 }
159 if (strpos($str_type, 'fax') !== false) {
160 if(substr($phone, 3) == '336') {
161 return 'conflict';
162 } else {
163 return 'fax';
164 }
165 }
166 if ((strpos($str_type, 'fixe') !== false) || (strpos($str_type, 'tél') !== false) || (strpos($str_type, 'tel') !== false)) {
167 if(substr($phone, 3) == '336') {
168 return 'conflict';
169 } else {
170 return 'fixed';
171 }
172 }
173
174 return 'unknown';
175}
176
177/* vim:set et sw=4 sts=4 ts=4: */
178?>