Blame view

src/app/backend/controllers/ProjectsController.php 3.5 KB
ef60cd4d   Administrator   first commit
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
  <?php
  /**
   * Created by PhpStorm.
   * User: Vitaliy
   * Date: 12.06.14
   * Time: 12:02
   */
  
  namespace controllers;
  
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  
  class ProjectsController extends \Phalcon\Mvc\Controller
  {
      public function indexAction()
      {
          $data = \projects::find(array("order" => 'id'));
          $this->view->setVars([
              'data' => $data,
          ]);
      }
  
      public function addAction()
      {
          if( $this->request->isPost() )
          {
              $project['name']           = $this->request->getPost('name', 'string');
              $project['url']           = $this->request->getPost('url', 'string');
              $project['contacts']           = $this->request->getPost('contacts', 'string');
              $project['status']           = $this->request->getPost('status');
              if($this->request->getPost('id')) {
                 $id =  $this->request->getPost('id');
                 $model = \projects::findFirst("id = '$id'");
                 $model->save($project);
              } else {
                  $model = new \projects;
                  $model->save($project);
              }
              return $this->response->redirect('project_index');
          }
          $status = \projectsStatus::find();
          $this->view->setVars([
              'status' => $status
          ]);
      }
  
      public function deleteAction($id)
      {
  
          $model = \projects::findFirst("id = '$id'");
          if($model instanceof \projects) {
              $model->userToProjects->delete();
              $model->delete();
              return $this->response->redirect('project_index');
          } else {
              echo "Данный проект не найден";
          }
  
      }
  
      public function editAction($id)
      {
  
          $data = \projects::findFirst("id = '$id'");
          $status = \projectsStatus::find();
          $this->view->setVars([
              'data' => $data,
              'status' => $status
          ]);
      }
  
      public function sortAction()
      {
          $sort = $this->request->getPost('data', 'string', NULL );
          $data = \projects::find(array("order" => $sort));
          $this->view->disableLevel(\Phalcon\Mvc\View::LEVEL_MAIN_LAYOUT);
          $this->view->setVars([
              "data" =>$data
          ]);
      }
  
      public function checkAction() {
          $data = $this->request->getPost('data');
          $data = json_decode($data);
          if(isset($data->id)) {
              $model = \projects::findFirst(array("$data->name= '$data->value' AND id = '$data->id'"));
              if($model instanceof \projects) {
                  $result = array(
                      'errors' => false,
                      'message' => ""
                  );
                  $data = json_encode($result);
                  $this->view->disableLevel(\Phalcon\Mvc\View::LEVEL_MAIN_LAYOUT);
                  echo $data;
                  die();
              }
          }
  
          $model = \projects::findFirst("{$data->name}= '{$data->value}'");
  
          if($model instanceof \projects) {
              $result = array(
                  'errors' => true,
                  'message' => "$data->value уже занято"
              );
          } else {
              $result = array(
                  'errors' => false,
                  'message' => "$data->value свободно"
              );
          }
          $data = json_encode($result);
          $this->view->disableLevel(\Phalcon\Mvc\View::LEVEL_MAIN_LAYOUT);
          echo $data;
      }
  }