UIModelGrid.php 4.89 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 37 38 39 40 41 42 43
<?php
/**
 *    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.
 *
 */

namespace OPNsense\Base;

/**
 * Class UIModelGrid Grid control support functions
 * @package OPNsense\Base
 */
class UIModelGrid
{
    /**
     * @var null|FieldTypes\ArrayField Data provider to link Grid support functions to.
     */
    private $DataField = null;

    /**
44
     * construct a new UIModelGrid
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
     * @param FieldTypes\ArrayField $DataField
     */
    public function __construct($DataField)
    {
        $this->DataField = $DataField;
    }

    /**
     * Fetch data from Array type field (Base\FieldTypes\ArrayField), sorted by specified fields and optionally filtered
     * @param array $fields select fieldnames
     * @param int $itemsPerPage number of items per page
     * @param int $currentPage current selected page
     * @param array $sortBy sort by fieldnames
     * @param bool $sortDescending sort in descending order
     * @param string $searchPhrase search phrase to use
     * @return array
     */
62 63 64 65 66 67 68 69
    public function fetch(
        $fields,
        $itemsPerPage,
        $currentPage,
        $sortBy = array(),
        $sortDescending = false,
        $searchPhrase = ''
    ) {
70 71 72 73
        $result = array('rows'=>array());

        $recordIndex = 0;
        foreach ($this->DataField->sortedBy($sortBy, $sortDescending) as $record) {
74 75 76 77 78 79
            if (array_key_exists("uuid", $record->getAttributes())) {
                // parse rows, because we may need to convert some (list) items we need to know the actual content
                // before searching.
                $row =  array();
                $row['uuid'] = $record->getAttributes()['uuid'];
                foreach ($fields as $fieldname) {
80 81
                    if ($record->$fieldname != null) {
                        $row[$fieldname] = $record->$fieldname->getNodeData();
82
                        if (is_array($row[$fieldname])) {
83 84 85 86 87 88 89 90
                            foreach ($row[$fieldname] as $fieldKey => $fieldValue) {
                                if ($fieldValue['selected'] == 1) {
                                    $row[$fieldname] = $fieldValue['value'];
                                }
                            }
                            if (is_array($row[$fieldname])) {
                                $row[$fieldname] = "##Unlinked";
                            }
91
                        }
92
                    }
93 94
                }

95 96 97 98 99 100 101 102
                // if a search phrase is provided, use it to search in all requested fields
                if ($searchPhrase != '') {
                    $searchFound = false;
                    foreach ($fields as $fieldname) {
                        if (strpos(strtolower($row[$fieldname]), strtolower($searchPhrase)) !== false) {
                            $searchFound = true;
                            break;
                        }
103
                    }
104 105
                } else {
                    $searchFound = true;
106 107
                }

108 109 110 111 112 113 114 115 116
                // if result is relevant, count total and add (max number of) items to result.
                // $itemsPerPage = -1 is used as wildcard for "all results"
                if ($searchFound) {
                    if ((count($result['rows']) < $itemsPerPage &&
                        $recordIndex >= ($itemsPerPage*($currentPage-1)) || $itemsPerPage == -1)
                    ) {
                        $result['rows'][] = $row;
                    }
                    $recordIndex++;
117 118 119 120 121 122 123 124 125 126
                }
            }
        }

        $result['rowCount'] = count($result['rows']);
        $result['total'] = $recordIndex;
        $result['current'] = (int)$currentPage;

        return $result;
    }
127
}