PageController.php 6.3 KB
Newer Older
1
<?php
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
/**
 *    Copyright (C) 2015 Deciso B.V.
 *
 *    All rights reserved.
 *
 *    Redistribution and use in source and binary forms, with or without
 *    modification, are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 *    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 *    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 *    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 *    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 *    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *    POSSIBILITY OF SUCH DAMAGE.
 *
 */
30

31 32 33 34
namespace OPNsense\Sample;

use Phalcon\Mvc\Controller;
use \OPNsense\Base\ControllerBase;
35
use \OPNsense\Core\Config;
36

37 38 39 40
/**
 * Class PageController
 * @package OPNsense\Sample
 */
41 42
class PageController extends ControllerBase
{
43 44 45 46 47 48 49 50 51 52 53 54 55 56
    /**
     * update model with data from our request
     * @param BaseModel &$mdlSample model to update
     */
    private function updateModelWithPost(&$mdlSample)
    {
        // Access POST data and save parts to model
        foreach ($this->request->getPost() as $key => $value) {
            $refparts = explode("_", $key);
            if (array_shift($refparts) == "sample") {
                // this post item belongs to the Sample model (see prefix in view)
                $node_found = $mdlSample->setNodeByReference(implode(".", $refparts), $value);
                // new node in the post which is not on disc, create a new child node
                // we need to create new nodes in memory for Array types
57
                if ($node_found == false && strpos($key, 'childnodes_section_') !== false) {
58 59 60 61 62 63 64 65 66
                    // because all the array items are numbered in order, we know that any item not found
                    // must be a new one.
                    $mdlSample->childnodes->section->add();
                    $mdlSample->setNodeByReference(implode(".", $refparts), $value);
                }
            }
        }
    }

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
    /**
     * perform validation, forward to index on failure.
     * @param $mdlSample Sample model
     * @return bool validation status
     */
    private function performFormValidation($mdlSample)
    {
        $validationOutput = $mdlSample->performValidation();
        if ($validationOutput->count() > 0) {
            // forward to index including errors
            $error_msgs = array();
            foreach ($validationOutput as $msg) {
                $error_msgs[] = array("field" => $msg-> getField(), "msg" => $msg->getMessage());
            }

            // redirect to index
            $this->dispatcher->forward(array(
                "action" => "index",
                "params" => array($error_msgs)
            ));
            return false;
        }

        return true;
    }

93 94
    /**
     * controller for sample index page, defaults to http://<host>/sample/page
95
     * @param array $error_msg error messages
96
     */
97
    public function indexAction($error_msg = array())
98
    {
99 100 101 102
        // load model and send to view, this model is automatically filled with data from the config.xml
        $mdlSample = new Sample();
        $this->view->sample = $mdlSample;

103 104 105 106 107 108 109 110
        // got forwarded with errors, load form data from post and update on our model
        if ($this->request->isPost() == true && count($error_msg) >0) {
            $this->updateModelWithPost($mdlSample);
        }

        // send error messages to view
        $this->view->error_messages = $error_msg;

111 112
        // set title and pick a template
        $this->view->title = "test page";
113 114 115
        $this->view->pick('OPNsense/Sample/page');
    }

116 117 118 119
    /**
     * Example save action
     * @throws \Phalcon\Validation\Exception
     */
120 121
    public function saveAction()
    {
122
        // save action should be a post
123 124 125 126
        if ($this->request->isPost() == true) {
            // create model(s)
            $mdlSample = new Sample();

127 128 129 130
            // update model with request data
            $this->updateModelWithPost($mdlSample);

            if ($this->request->getPost("form_action") == "add") {
131
                // implement addRow, append new model row and serialize to config if form is valid
132
                $mdlSample->childnodes->section->add();
133 134 135
                if ($this->performFormValidation($mdlSample) == false) {
                    return false;
                }
136 137 138 139 140 141 142 143 144 145 146 147 148 149
                $mdlSample->serializeToConfig();
            } elseif ($this->request->getPost("form_action") == "save") {
                // implement save, possible removing
                if ($this->request->hasPost("delete")) {
                    // delete selected Rows, cannot combine with add because of the index numbering
                    foreach ($this->request->getPost("delete") as $node_ref => $option_key) {
                        $refparts = explode(".", $node_ref);
                        $delete_key = array_pop($refparts);
                        $parentNode = $mdlSample->getNodeByReference(implode(".", $refparts));
                        if ($parentNode != null) {
                            $parentNode->del($delete_key);
                        }

                    }
150
                }
151

152 153
                // form validation
                if ($this->performFormValidation($mdlSample) == false) {
154 155 156
                    return false;
                }

157
                // save data to config
158 159 160
                $mdlSample->serializeToConfig();
                $cnf = Config::getInstance();
                $cnf->save();
161
            }
162
        }
163

164 165 166 167
        // redirect to index
        $this->dispatcher->forward(array(
            "action" => "index"
        ));
168

169
        return true;
170
    }
171
}