ApiMutableTableModelControllerBase.php 8.42 KB
Newer Older
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
<?php
/**
 *    Copyright (C) 2016 IT-assistans Sverige AB
 *    Copyright (C) 2016 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.
 *
 */
namespace OPNsense\Base;

/**
 * Class ApiMutableTableModelControllerBase, inherit this class to implement
 * an API that exposes a model for tables.
 * @package OPNsense\Base
 */
37
abstract class ApiMutableTableModelControllerBase extends ApiMutableModelControllerBase
38 39
{
    static protected $modelPathPrefix = '';
40 41
    static protected $gridFields = array();
    static protected $gridDefaultSort = null;
Franco Fichtner's avatar
Franco Fichtner committed
42 43
    private function getNodes()
    {
44
        $ref = static::$modelPathPrefix . static::$internalModelName;
45 46
        return $this->getModel()->getNodeByReference($ref);
    }
Franco Fichtner's avatar
Franco Fichtner committed
47 48
    private function getNodeByUUID($uuid)
    {
49 50
        $nodes = $this->getNodes();
        return !empty($nodes) ? $nodes->$uuid : null;
51
    }
52

pv2b's avatar
pv2b committed
53
    /**
54 55 56 57
     * retrieve item or return defaults
     * @param $uuid item unique id
     * @return array
     */
pv2b's avatar
pv2b committed
58 59
    public function getItemAction($uuid = null)
    {
60 61
        $mdl = $this->getModel();
        if ($uuid != null) {
62
            $node = $this->getNodeByUUID($uuid);
63 64 65 66 67 68
            if ($node != null) {
                // return node
                return array(static::$internalModelName => $node->getNodes());
            }
        } else {
            // generate new node, but don't save to disc
69
            $node = $this->getNodes()->add();
70 71 72 73
            return array(static::$internalModelName => $node->getNodes());
        }
        return array();
    }
pv2b's avatar
pv2b committed
74

75 76 77 78 79 80 81 82
    /**
     * hook to be overridden if the controller is to take an action when
     * setItemAction is called. This hook is called after a model has been
     * constructed and validated but before it serialized to the configuration
     * and written to disk
     * @param $mdl The validated model containing the new state of the model
     * @return Error message on error, or null/void on success
     */
Franco Fichtner's avatar
Franco Fichtner committed
83 84 85
    protected function setItemActionHook($mdl)
    {
    }
86

87 88 89 90 91
    /**
     * update item with given properties
     * @param $uuid item unique id
     * @return array
     */
pv2b's avatar
pv2b committed
92 93
    public function setItemAction($uuid)
    {
94 95 96 97 98 99
        if ($this->request->isPost() && $this->request->hasPost(static::$internalModelName)) {
            $mdl = $this->getModel();
            if ($uuid != null) {
                $node = $this->getNodeByUUID($uuid);
                if ($node != null) {
                    $node->setNodes($this->request->getPost(static::$internalModelName));
100
                    return $this->save($mdl, $node, $this->setItemActionHook);
101 102 103 104
                }
            }
        }
        return array("result"=>"failed");
105
    }
106

107 108 109 110 111 112 113 114
    /**
     * hook to be overridden if the controller is to take an action when
     * addItemAction is called. This hook is called after a model has been
     * constructed and validated but before it serialized to the configuration
     * and written to disk
     * @param $mdl The validated model containing the state of the new model
     * @return Error message on error, or null/void on success
     */
Franco Fichtner's avatar
Franco Fichtner committed
115 116 117
    protected function addItemActionHook($mdl)
    {
    }
118

119 120 121 122
    /**
     * add new item and set with attributes from post
     * @return array
     */
pv2b's avatar
pv2b committed
123 124
    public function addItemAction()
    {
125
        $result = array("result"=>"failed");
126
        if ($this->request->isPost() && $this->request->hasPost(static::$internalModelName)) {
127
            $mdl = $this->getModel();
pv2b's avatar
pv2b committed
128
            $node = $this->getNodes()->add();
129
            $node->setNodes($this->request->getPost(static::$internalModelName));
130
            return $this->save($mdl, $node, $this->addItemActionHook);
131 132
        }
        return $result;
133
    }
pv2b's avatar
pv2b committed
134

135 136 137 138 139 140 141 142
    /**
     * hook to be overridden if the controller is to take an action when
     * delItemAction is called. This hook is called after a model has been
     * constructed and validated but before it serialized to the configuration
     * and written to disk
     * @param $uuid The UUID of the item to be deleted
     * @return Error message on error, or null/void on succes s
     */
Franco Fichtner's avatar
Franco Fichtner committed
143 144 145
    protected function delItemActionHook($uuid)
    {
    }
146

147 148 149 150 151
    /**
     * delete item by uuid
     * @param $uuid item unique id
     * @return array status
     */
pv2b's avatar
pv2b committed
152 153
    public function delItemAction($uuid)
    {
154 155 156 157
        $result = array("result"=>"failed");
        if ($this->request->isPost()) {
            $mdl = $this->getModel();
            if ($uuid != null) {
158 159 160
                $errorMessage = delItemActionHook($uuid);
                if ($errorMessage) {
                    $result['error'] = $errorMessage;
Franco Fichtner's avatar
Franco Fichtner committed
161
                } elseif (getNodes()->del($uuid)) {
162
                    // if item is removed, serialize to config and save
pv2b's avatar
pv2b committed
163
                    $mdl->serializeToConfig();
164 165 166 167 168 169 170 171
                    Config::getInstance()->save();
                    $result['result'] = 'deleted';
                } else {
                    $result['result'] = 'not found';
                }
            }
        }
        return $result;
172
    }
pv2b's avatar
pv2b committed
173

174 175 176 177 178 179 180 181 182
    /**
     * hook to be overridden if the controller is to take an action when
     * toggleItemAction is called. This hook is called after a model has been
     * constructed and validated but before it serialized to the configuration
     * and written to disk
     * @param $uuid The UUID of the item to be toggled
     * @param $enabled desired state enabled(1)/disabled(1), leave empty for toggle
     * @return Error message on error, or null/void on succes s
     */
Franco Fichtner's avatar
Franco Fichtner committed
183 184 185
    protected function toggleItemActionHook($uuid, $enabled)
    {
    }
186

187 188 189 190 191 192
    /**
     * toggle item by uuid (enable/disable)
     * @param $uuid item unique id
     * @param $enabled desired state enabled(1)/disabled(1), leave empty for toggle
     * @return array status
     */
pv2b's avatar
pv2b committed
193 194
    public function toggleItemAction($uuid, $enabled = null)
    {
195 196 197 198 199 200
        $result = array("result" => "failed");
        if ($this->request->isPost()) {
            $mdl = $this->getModel();
            if ($uuid != null) {
                $node = $mdl->getNodeByUUID($uuid);
                if ($node != null) {
201 202 203
                    $errorMessage = toggleItemActionHook($uuid, $enabled);
                    if ($errorMessage) {
                        $result['error'] = $errorMessage;
204
                    } else {
205 206 207 208 209 210 211 212 213 214 215
                        if ($enabled == "0" || $enabled == "1") {
                            $node->enabled = (string)$enabled;
                        } elseif ($node->enabled->__toString() == "1") {
                            $node->enabled = "0";
                        } else {
                            $node->enabled = "1";
                        }
                        $result['result'] = $node->enabled;
                        // if item has toggled, serialize to config and save
                        $mdl->serializeToConfig();
                        Config::getInstance()->save();
216 217 218 219 220
                    }
                }
            }
        }
        return $result;
221
    }
pv2b's avatar
pv2b committed
222

223 224 225 226
    /**
     * search items
     * @return array
     */
pv2b's avatar
pv2b committed
227 228
    public function searchItemsAction()
    {
229 230 231
        $this->sessionClose();
        $mdl = $this->getModel();
        $grid = new UIModelGrid($this->getNodes());
232
        return $grid->fetchBindRequest($this->request, static::$gridFields, static::$gridDefaultSort);
233 234
    }
}