-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.php
130 lines (105 loc) · 3.55 KB
/
model.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
<?php
require_once('db.php');
class Job
{
private $db;
public function __construct()
{
$this->db = Database::getInstance();
}
public function job_save(array $data)
{
try {
foreach ($data as $key => $value) {
if (!empty($key)) {
$topic_id = $this->_return_create_topic($key);
$results = $this->_save_options($value, $topic_id);
} else {
throw new Exception('Empty section.');
}
}
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
}
private function _save_options($array, $t_id)
{
$options = array();
foreach ($array as $option) {
$options[] = array('topic_id' => $t_id,
'name' => $option[0],
'value' => $option[1]);
}
return $this->create_options($options);
}
public function create_options($array)
{
return $this->db->multi_insert("INSERT INTO options (topic_id, name, value ) VALUE (:topic_id, :name, :value)", $array);
}
public function create_topic($title)
{
$this->db->query("INSERT INTO topics (name) VALUE (?)", $title);
return $this->db->getLastId();
}
public function get_topic_id($title)
{
$statement = $this->db->query("SELECT id FROM topics WHERE name = ?", $title);
$result = $statement->fetch(PDO::FETCH_NUM);
return $result[0];
}
private function _return_create_topic($title)
{
$topic_id = $this->get_topic_id($title);
if ($topic_id) {
return $topic_id;
} else {
return $this->create_topic($title);
}
}
private function _extract_values($arr)
{
$result = array();
$result['options'] = array();
for ($i = 0; $i < sizeof($arr); $i++) {
//get predefined params
if ($arr[$i][1] == 'Должность') {
$result['position'] = $arr[$i][2];
unset($arr[$i]);
} elseif ($arr[$i][1] == 'Дата регистрации вакансии') {
$result['date'] = $arr[$i][2];
unset($arr[$i]);
} else {
//changed array structure to make one topic as parent for options
$result['sections'][$arr[$i][3]][] = array($arr[$i][1], $arr[$i][2], $arr[$i][0]);
}
}
return $result;
}
public function get_job()
{
$statement = $this->db->query("SELECT options.id, options.name, options.value, topics.name FROM options
LEFT JOIN topics ON topics.id = options.topic_id;");
$result = $statement->fetchAll(PDO::FETCH_NUM);
$extracted = $this->_extract_values($result);
return $extracted;
}
private function _prepare_arry($arr)
{
$new_arr = array();
foreach ($arr as $key => $value) {
preg_match_all('/^([^\d]+)(\d+)/', $key, $match);
//$text = $match[1][0];
//$num = $match[2][0];
//id, name, value
$new_arr[$match[2][0]]['id'] = $match[2][0];
$new_arr[$match[2][0]][$match[1][0]] = $value;
}
return $new_arr;
}
public function save_job()
{
//TODO: add validation
$prepared = $this->_prepare_arry($_POST);
return $this->db->multi_insert("UPDATE options SET name = :name, value = :value WHERE id = :id", $prepared);
}
}