-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv.php
More file actions
71 lines (59 loc) · 1.54 KB
/
csv.php
File metadata and controls
71 lines (59 loc) · 1.54 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
<?php
class csv {
private $_file,
$_roles = array();
public $fields,
$data = array();
public function __construct($file, $fields) {
$this->_file = $file;
$this->fields = $fields;
$this->setFields();
$this->import();
}
private function import() {
// Get the posted data.
$csv = $this->_file['tmp_name'];
$header = true;
$handle = fopen($csv,"r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// Stores each csv line to an array
// Check if header line
if ( !$header ) {
$i=0;
$table = $this->getTable($heads[$i]);
for ($i=0; $i < count($data); $i++) {
$table = $this->getTable($heads[$i]);
$field = $heads[$i];
if ($table != '') {
if ($table == 'users-roles') {
$user[$table][$heads[$i]] = $data[$i] != 0 ? $this->getRoleID($heads[$i]) : $data[$i];
} else {
$user[$table][$heads[$i]] = $data[$i];
}
}
}
array_push($this->data, $user);
}
else {
$heads = $data;
$header = false;
}
}
}
private function getTable($field) {
foreach ($this->fields as $table => $fieldnames) {
if(in_array($field, $fieldnames)) return $table;
}
}
private function setFields() {
foreach ($this->fields['users-roles'] as $key=>$field) {
unset($this->fields['users-roles'][$key]);
$this->fields['users-roles'][] = $key;
$this->_roles[$key] = $field['ID'];
}
}
private function getRoleID($roleName) {
return $this->_roles[$roleName];
}
}
?>