FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
DataTablesUtility.php
1 <?php
2 /***********************************************************
3  * Copyright (C) 2014 Siemens AG
4  * Author: J.Najjar
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  ***********************************************************/
19 
20 namespace Fossology\Lib\Util;
21 
22 use Monolog\Logger;
23 
25 {
29  private $logger;
30 
31  function __construct()
32  {
33  $this->logger = new Logger(self::class);
34  }
35 
41  public function getSortingParametersFromArray($inputArray, $columNamesInDatabase, $defaultSearch = array())
42  {
43  if (array_key_exists('iSortingCols', $inputArray)) {
44  if ($inputArray['iSortingCols'] > count($columNamesInDatabase)) {
45  $this->logger->addWarning(
46  "did have enough columNames for " . $inputArray['iSortingCols'] .
47  " sort columns.");
48  return null;
49  }
50  return $this->getSortingParametersFromArrayImpl($inputArray,
51  $columNamesInDatabase, $defaultSearch);
52  } else {
53  $this->logger->addWarning("did not find iSortingCols in inputArray");
54  return null;
55  }
56  }
57 
58 
65  private function getSortingParametersFromArrayImpl($inputArray, $columNamesInDatabase, $defaultSearch = array())
66  {
67  $orderArray = array();
68  $sortedCols = array();
69  for ($i = 0; $i < $inputArray['iSortingCols']; $i ++) {
70  $whichCol = 'iSortCol_' . $i;
71  $colNumber = $inputArray[$whichCol];
72  $sortedCols[] = intval($colNumber);
73 
74  $isSortable = $inputArray['bSortable_' . $i];
75  if ($isSortable !== "true") {
76  continue;
77  }
78  $name = $columNamesInDatabase[$colNumber];
79 
80  $whichDir = 'sSortDir_' . $i;
81  $order = $inputArray[$whichDir];
82  $orderArray[] = $name . " " . $order;
83  }
84 
85  foreach ($defaultSearch as $search) {
86  $colNumber = $search[0];
87  $order = $search[1];
88  if (in_array($colNumber, $sortedCols)) {
89  continue;
90  }
91  $isSortable = $inputArray['bSortable_' . $colNumber];
92  if ($isSortable !== "true") {
93  continue;
94  }
95 
96  $name = $columNamesInDatabase[$colNumber];
97  $orderArray[] = $name . " " . $order;
98  }
99  return $orderArray;
100  }
101 
102 
109  public function getSortingString($inputArray, $columNamesInDatabase, $defaultSearch = array())
110  {
111  $orderArray = $this->getSortingParametersFromArray($inputArray, $columNamesInDatabase, $defaultSearch);
112  $orderString = empty($orderArray) ? "" : "ORDER BY " . implode(", ", $orderArray);
113  return $orderString;
114  }
115 }
getSortingString($inputArray, $columNamesInDatabase, $defaultSearch=array())
getSortingParametersFromArray($inputArray, $columNamesInDatabase, $defaultSearch=array())
getSortingParametersFromArrayImpl($inputArray, $columNamesInDatabase, $defaultSearch=array())