FieldsController.php 4.51 KB
<?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
        ]);
    }


}