-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.php
More file actions
240 lines (210 loc) · 10.1 KB
/
Copy pathindex.php
File metadata and controls
240 lines (210 loc) · 10.1 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
ini_set('memory_limit', '-1');
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
$server = "localhost";
$username = "root";
$password = "";
$dbname = "datathon";
// Try and connect to the database
$connection = new mysqli('127.0.0.1', $username, $password, $dbname);
$connection->set_charset("utf8");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
header("content-type: text/html;charset=utf-8");
require_once "PHPExcel/Classes/PHPExcel.php";
echo "<br>Hello Open Data Comp! <br>";
//1 is mikron 2 is megalon 3 is online
$storeType = "2";//set store type before
//readXMLFile("amochostos_paratiritirio_megalon_peragoron_01-12-2015.xls"); //test 1 file
$megalonFiles = array('amochostos_paratiritirio_megalon_peragoron_01-12-2015.xls',
'amochostos_paratiritirio_megalon_peragoron_02-03-2016.xls',
'amochostos_paratiritirio_megalon_peragoron_25-07-2016.xls',
'amochostos_paratiritirio_megalon_peragoron_25-08-2015.xls',
'larnaka_paratiritirio_megalon_peragoron_01-12-2015.xls',
'larnaka_paratiritirio_megalon_peragoron_02-03-2016.xls',
'larnaka_paratiritirio_megalon_peragoron_25-07-2016.xls',
'larnaka_paratiritirio_megalon_peragoron_25-08-2015.xls',
'lefkosia_paratiritirio_megalon_iperagoron_24-08-2015.xls',
'lefkosia_paratiritirio_megalon_peragoron_01-12-2015.xls',
'lefkosia_paratiritirio_megalon_peragoron_02-03-2016.xls',
'lefkosia_paratiritirio_megalon_peragoron_25-07-2016.xls',
'lemesos_paratiritirio_megalon_peragoron_01-12-2015.xls',
'lemesos_paratiritirio_megalon_peragoron_02-03-2016.xls',
'lemesos_paratiritirio_megalon_peragoron_25-07-2016.xls',
'lemesos_paratiritirio_megalon_peragoron_25-08-2015.xls',
'pafos_paratiritirio_megalon_peragoron_01-12-2015.xls',
'pafos_paratiritirio_megalon_peragoron_02-03-2016.xlsx',
'pafos_paratiritirio_megalon_peragoron_25-07-2016.xls',
'pafos_paratiritirio_megalon_peragoron_25-08-2015.xlsx');
foreach ($megalonFiles as $key => $value) {
readXMLFile($value, $storeType, $connection);
}
function checkExists($connection, $name, $tableName, $secPar = '')
{
//remember stores also have their city => check a and b
$result = 0;
$extraClause = '';
if ($secPar != '') {
$extraClause = " AND city=$secPar";
}
$query = "SELECT id from $tableName WHERE name='$name'" . $extraClause;
$run_query = $connection->query($query);
if ($row = $run_query->fetch_assoc()) {
$result = $row['id'];
}
return $result;
}
function insertMergedTable($connection, $fk_product_id, $fk_store_id, $fk_product_name, $fk_city_name, $fk_category_id, $price, $on_offer, $date)
{
echo "inserting the big data..<br>";
$query = "INSERT INTO products_stores (id, fk_product_id,fk_store_id,fk_product_name,fk_city_name,fk_category_id,price,on_offer,entry_date)
VALUES (DEFAULT, '$fk_product_id','$fk_store_id', '$fk_product_name', '$fk_city_name', '$fk_category_id', '$price', '$on_offer', '$date');";
$connection->query($query);
}
//insert category if does not exist
function insertRow($connection, $name, $tableName, $secPar = '')
{
echo "insert $name in $tableName #$secPar#<br>";
$extraClause = '';
//check if entry exists
$row_id = $exsists = checkExists($connection, $name, $tableName, $secPar);
//if it does not exist insert, otherwise do nothing!!!
if (!$exsists) {
if ($secPar != '') {
$extraClause = ",'$secPar'";
}
//does not exist, insert row!
$query = "INSERT INTO $tableName VALUES (DEFAULT,'$name'$extraClause);";
$connection->query($query);
$row_id = $connection->insert_id;
}
return $row_id;
} //calls checkExist
function readXMLFile($tempFileName, $storeType, $connection)
{
$cities_array = array("ΛΕΥΚΩΣΙΑΣ", "ΛΕΜΕΣΟΥ", "ΛΑΡΝΑΚΑΣ", "ΑΜΜΟΧΩΣΤΟΥ", "ΠΑΦΟΥ");
$tmpfname = "govdata/" . $tempFileName;
$excelReader = PHPExcel_IOFactory::createReaderForFile($tmpfname);
$excelObj = $excelReader->load($tmpfname);
$data_date = '';
#$workSheet = $excelObj->getActiveSheet();
foreach ($excelObj->getWorksheetIterator() as $workSheet) {
$lastRow = $workSheet->getHighestDataRow();
$lastColumn = $workSheet->getHighestDataColumn();
//city
$data_city_string = $workSheet->getCell("A3");
foreach ($cities_array as $key => $value) {
if (strpos($data_city_string, $value) !== false) {
$data_city = $value;
}
}
//date
$date_formula = $workSheet->getCell("A4"); //some sheets use formula and get the date from the first spreadsheet
$date_formula .= "";
if ($date_formula[0] != "=") {
$data_date = explode(" ", $date_formula);
$data_date = str_replace("/", "-", $data_date[1]);
}
///////////////// start /////////////////
$dataArray = array();
$storesArray = array();
// stores go from B7 and go up to N7
$storesColumn = "B";
$storesLastColumn = "N";
foreach (range($storesColumn, $storesLastColumn) as $key => $value) {
$lookingAt = $workSheet->getCell($value . "7")->getValue();
if ($lookingAt != '') {
$storesArray[$value . "7"]['storeName_7'] = $lookingAt;
}
}
// echo "<pre>";
// print_r($storesArray);
// echo "</pre>";
// die;
// products table row starts at B11
$productsColumn = "C";
$productsLastColumn = "N";
$category = '';
for ($row = 11; $row <= $lastRow; $row++) {
$storeBeingProcessed = 0;
foreach (range($storesColumn, $storesLastColumn) as $key => $value) {
$lookingAt = $workSheet->getCell($value . $row)->getValue();
if (array_key_exists($value . "7", $storesArray)) {
$storeBeingProcessed = $storesArray[$value . "7"]['storeName_7'];
}
$checkCategoryIndex = $workSheet->getCell('A' . $row)->getValue();
if ($checkCategoryIndex == '') {
//continue if category line
$category = $lookingAt;
break;
}
//B is product name, after is price in each store
$checkProductNameIndex = $workSheet->getCell('B' . $row)->getValue();
if ($checkProductNameIndex == "ΟΝΟΜΑΣΙΑ ΚΑΙ ΕΙΔΟΣ ΠΡΟΙΟΝΤΟΣ") {
break;
}
if ($lookingAt != '') {
#$dataArray[$row][$data_being_stored] = $lookingAt;
if ($storeBeingProcessed) {
$dataArray[$row][$storeBeingProcessed]['City'] = $data_city;
$dataArray[$row][$storeBeingProcessed]['Date'] = $data_date;
$dataArray[$row][$storeBeingProcessed]['Store'] = $storeBeingProcessed;
$dataArray[$row][$storeBeingProcessed]['ProductName'] = $checkProductNameIndex;
if ($lookingAt == '*') {
//get next colummn to check if offer
$dataArray[$row][$storeBeingProcessed]['offer'] = 1;
} else {
$dataArray[$row][$storeBeingProcessed]['price'] = $lookingAt;
$dataArray[$row][$storeBeingProcessed]['offer'] = 0;
}
if ($category == '') {
//if category is empty, the spreadsheet MIGHT not have a category placed..
//$category = 'General';
$category = $workSheet->getTitle();
}
$dataArray[$row][$storeBeingProcessed]["category"] = $category;
#$dataArray[$row]["store"] = $storeBeingProcessed;
}
}
}
}
insertWorkSheetToDatabase($dataArray, $connection);
}
}
//echo checkExists($connection,'Megalon','store_types'); //seems to work
//echo insertRow($connection,'testtest','store_types'); //seems to work, returns insered data row
//function insertMergedTable($connection, $fk_product_id,$fk_store_id, $fk_product_name, $fk_city_name, $fk_category_id, $price,on_offer, $date) // not tested!!!
function insertWorkSheetToDatabase($dataArray, $connection)
{
// echo "<pre>";
// print_r($dataArray); //array is carried over;
// echo "</pre>";
echo "inserting rows... <br>";
foreach ($dataArray as $key => $stores) {
foreach ($stores as $key => $actualValues) {
//must be first
//category
$category = addslashes($actualValues['category']);
$catId = addslashes(insertRow($connection, $category, 'categories'));
//city
$city = addslashes($actualValues['City']);
$cityId = addslashes(insertRow($connection, $city, 'cities'));
//product
$productName = addslashes($actualValues['ProductName']);
$productNameId = addslashes(insertRow($connection, $productName, 'products'));
//store
$store = addslashes($actualValues['Store']);
$storeId = addslashes(insertRow($connection, $store, 'stores', $cityId));
//insert after -- data for joined table
$date = addslashes($actualValues['Date']);
$price = addslashes($actualValues['price']);
$isOffer = addslashes($actualValues['offer']);
//insertMergedTable($connection, $fk_product_id,$fk_store_id, $fk_product_name, $fk_city_name, $fk_category_id, $price, $on_offer, $date);
insertMergedTable($connection, $productNameId, $storeId, $productName, $city, $catId, $price, $isOffer, $date);
}
}
}
?>