FieldsController.php
4.51 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
<?php
namespace controllers;
class FieldsController extends \Phalcon\Mvc\Controller
{
function indexAction($type)
{
$result = array();
$data = \fields::find(array("type='$type'","order" => 'weight'))->toArray();
$count = count($data);
for($i = 0; $i < $count; $i++){
if($data[$i]['parent_id'] == 0){
$result[$i] = $data[$i];
$result[$i]['children'] = array();
foreach($data as $subrow){
if($subrow['parent_id'] == $data[$i]['id']){
$result[$i]['children'][] = $subrow;
}
}
}
}
$this->view->setVars([
'data' => $result,
'type' => $type
]);
}
function setAction()
{
/*if($this->request->getPost()){
$model = new \fields();
$model->email = $this->request->getPost('email', 'email');
$model->request = $this->request->getPost('text', 'string');
$model->save();
} else {
echo "doesn't have any request ";
}
die();*/
$result = $this->getModelCars();
header('Cache-Control: no-cache, must-revalidate');
header('content-type:application/json');
echo(json_encode($result));
die();
}
function addAction($type)
{
if($this->request->getPost()){
$post = $this->request->getPost();
$model= new \fields();
$model->field_value = $post['field_value'];
$model->type = $type;
$model->comments = $post['comments'];
$model->parent_id = $post['parent_id'];
$model->name = $post['name'];
$model->weight = $post['weight'];
$model->class_name = $post['class_name'];
$model->required = $post['required'];
if($model->save()){
$this->flash->success( 'Сохранение прошло успешно' );
return $this->response->redirect([ 'for' => 'index_fields', 'type' =>$type ]);
}
else
{
$this->flash->error( 'Произошла ошибка во время добавления.' );
}
}
$data = \fields::find(array("type='$type'","order" => 'id'));
$this->view->pick( 'fields/addEdit' );
$this->view->setVars([
'fields' => $data
]);
}
function deleteAction($id, $type){
$model = \fields::findFirst("id = {$id}");
if($model instanceof \fields) {
$children = \fields::find("parent_id = {$model->id}");
foreach($children as $child){
$child->parent_id = 0;
$child->save();
}
if($model->delete()){
$this->flash->error( 'Данные успешно удалены' );
return $this->response->redirect([ 'for' => 'index_fields', 'type'=>$type ]);
} else {
$this->flash->error( 'Произошла ошибка при попытке удаления' );
return $this->response->redirect([ 'for' => 'index_fields', 'type'=>$type ]);
}
}
}
function updateAction($id, $type)
{
if($this->request->getPost()){
$post = $this->request->getPost();
$model = \fields::findFirst("id = $id");
$model->field_value = $post['field_value'];
$model->type = $type;
$model->comments = $post['comments'];
$model->parent_id = $post['parent_id'];
$model->name = $post['name'];
$model->weight = $post['weight'];
$model->class_name = $post['class_name'];
$model->required = $post['required'];
if($model->save()){
$this->flash->success( 'Сохранение прошло успешно' );
return $this->response->redirect([ 'for' => 'index_fields', 'type' =>$type ]);
}
else
{
$this->flash->error( 'Произошла ошибка во время добавления.' );
}
}
$data = \fields::find(array("type='$type'","order" => 'id'));
$model = \fields::findFirst("id = $id");
$this->view->pick( 'fields/addEdit' );
$this->view->setVars([
'data' => $model,
'fields' => $data
]);
}
}