This repository has been archived by the owner on Oct 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManager.php
197 lines (154 loc) · 4.85 KB
/
Manager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
class Import_Manager
{
private $rawData;
public $people;
public $cities;
private $view;
// loads the contents of the file
public function Import_Manager($view) {
$this->view = $view;
if (file_exists(ROOTDIR . '/configuration/socialGraph.php')) {
$this->rawData = @include(ROOTDIR . '/configuration/socialGraph.php');
} else {
throw new SocialException(SocialException::IMPORTER_DATASET_ABSENT);
}
}
// generate slugs, filter people having no names, validate age and gender
public function sanitizeInputData() {
// final curated array of people
$output = array();
// array of slugs, used to prevent different people getting the same slug
$slugs = array();
// array grouping cities by name
$this->cities = array();
if (!is_array($this->rawData)) {
throw new SocialException(SocialException::IMPORTER_CHECK_ERROR);
return false;
}
foreach ($this->rawData as $person) {
// compose the full name
$name = $person['firstName'] . ' ' . $person['surname'];
// generate the slug (helps hiding the db id's, also good for SEO)
$slug = $this->view->slug($name);
// stop and skip the current user. He/She has no name.
if ($slug == '') continue;
// make sure there's no slug collision
$uniqueSlug = $slug;
$i=0;
while (in_array($uniqueSlug, $slugs)) {
$i++;
$uniqueSlug = $slug . '-' . $i;
}
$slugs[] = $uniqueSlug;
$age = $this->view->sanitizeAge($person['age']);
$gender = $this->view->sanitizeGender($person['gender']);
$sanitizedPerson = array(
'id' => $person['id'],
'first_name' => $person['firstName'],
'last_name' => $person['surname'],
'slug' => $uniqueSlug,
'age' => $age,
'gender' => $gender,
'connections' => $person['connections'],
'cities' => $person['cities']
);
$output[] = $sanitizedPerson;
}
unset($this->rawData);
if (!count($output)) {
throw new SocialException(SocialException::IMPORTER_CHECK_ERROR);
return false;
}
$this->people = $output;
return true;
}
public function insertDataIntoDatabase() {
if (!is_array($this->people)) {
throw new SocialException(SocialException::IMPORTER_DATASET_EMPTY);
return false;
}
if (!count($this->people)) {
throw new SocialException(SocialException::IMPORTER_DATASET_EMPTY);
return false;
}
$idToDBIdRelation = array();
$accountsTable = new Accounts_Table();
// empty the table
$accountsTable->delete('1');
foreach ($this->people as $person) {
$statement = array(
'first_name' =>$person['first_name'],
'last_name' =>$person['last_name'],
'age' =>$person['age'],
'gender' =>$person['gender'],
'slug' =>$person['slug']
);
$dbID = $accountsTable->insert($statement);
$importedID = $person['id'];
// basically not trusting the ID from the data source file. Regenerating it.
if (is_numeric($importedID) && !array_key_exists($importedID, $idToDBIdRelation)) {
$idToDBIdRelation[$importedID] = $dbID;
}
foreach ($person['connections'] as $importedFriendID) {
$connection = array(
'account_id' => $dbID,
'friend' => $importedFriendID
);
$connections[] = $connection;
}
foreach ($person['cities'] as $city => $rating) {
if (!array_key_exists($city, $this->cities)) {
$this->cities[$city] = array();
$this->cities[$city]['sum_all_ratings'] = 0;
$this->cities[$city]['ratings'] = array();
}
$newRating = array();
$newRating['account_id'] = $dbID;
$newRating['rating'] = $rating;
$this->cities[$city]['ratings'][] = $newRating;
$this->cities[$city]['sum_all_ratings'] += $rating;
}
}
$connectionsTable = new Accounts_Connections_Table();
// empty the table
$connectionsTable->delete('1');
foreach ($connections as $connection) {
$friendID = $connection['friend'];
$dbFriendID = $idToDBIdRelation[$friendID];
if (is_numeric($dbFriendID)) {
unset($connection['friend']);
$connection['friend_id'] = $dbFriendID;
$connectionsTable->insert($connection);
}
}
$this->insertCityRatings();
return true;
}
public function insertCityRatings() {
$citiesTable = new Cities_Table();
$citiesTable->delete('1');
$citiesRatingsTable = new Cities_Ratings_Table();
$citiesRatingsTable->delete('1');
foreach ($this->cities as $cityName=>$cityData) {
if (count($cityData['ratings']) == 0) {
$cityRank = 0;
} else {
$cityRank = $cityData['sum_all_ratings'] / count($cityData['ratings']);
}
$statement = array(
'city_name' => $cityName,
'rating' => $cityRank
);
$cityDbID = $citiesTable->insert($statement);
foreach ($cityData['ratings'] as $rating) {
$statement = array(
'account_id' =>$rating['account_id'],
'city_id' =>$cityDbID,
'rating' =>$rating['rating']
);
$citiesRatingsTable->insert($statement);
}
}
}
}