| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Http\Rest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use \Psr\Http\Message\ServerRequestInterface as Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use \Psr\Http\Message\ResponseInterface as Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | require 'vendor/autoload.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class DataTableAPI extends RestAPI | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     protected $dataSetName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     protected $dataTableName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     protected $primaryKeyName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     public function __construct($dataSetName, $dataTableName, $primaryKeyName = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         $this->dataSetName    = $dataSetName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         $this->dataTableName  = $dataTableName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $this->primaryKeyName = $primaryKeyName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public function setup($app) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $app->get('[/]', array($this, 'readEntries')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $app->post('[/]', array($this, 'createEntry')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         if($this->primaryKeyName !== false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             $app->get('/{name}[/]', array($this, 'readEntry')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             $app->patch('/{name}[/]', array($this, 'updateEntry')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             $app->delete('/{name}[/]', array($this, 'deleteEntry')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected function getDataTable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         return \DataSetFactory::getDataTableByNames($this->dataSetName, $this->dataTableName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected function canRead($request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->validateLoggedIn($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         //validateLoggedIn is fatal if not logged in... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     protected function canCreate($request) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         //Must be overriden in a child class to allow create | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     protected function canUpdate($request, $entity) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         //Must be overriden in a child class to allow update | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     protected function canDelete($request, $entity) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         //Must be overriden in a child class to allow update | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     protected function getFilterForPrimaryKey($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return new \Data\Filter($this->primaryKeyName." eq '$value'"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     protected function manipulateParameters($request, &$odata) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     protected function validateCreate(&$obj, $request) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     protected function validateUpdate(&$newObj, $request, $oldObj) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     public function readEntries($request, $response, $args) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         if($this->canRead($request) === false) | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |             return $response->withStatus(401); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         $dataTable = $this->getDataTable(); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         $odata = $request->getAttribute('odata', new \ODataParams(array())); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         $params = $this->manipulateParameters($request, $odata); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         $areas = $dataTable->read($odata->filter, $odata->select, $odata->top, | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |                                   $odata->skip, $odata->orderby, $params); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         if($areas === false) | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |             $areas = array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         if(method_exists($this, 'processEntry')) | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |             $count = count($areas); | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |             for($i = 0; $i < $count; $i++) | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |             { | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |                 $areas[$i] = $this->processEntry($areas[$i], $request); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |         return $response->withJson($areas); | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     public function createEntry($request, $response, $args) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         if($this->canCreate($request) === false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             return $response->withStatus(401); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $dataTable = $this->getDataTable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $obj = $request->getParsedBody(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         if($obj == NULL) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             $obj = json_decode($request->getBody()->getContents(), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         if($this->validateCreate($obj, $request) === false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             return $response->withStatus(400); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $ret = $dataTable->create($obj); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         return $response->withJson($ret); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     public function readEntry($request, $response, $args) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         if($this->canRead($request) === false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             return $response->withStatus(401); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $dataTable = $this->getDataTable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $odata = $request->getAttribute('odata', new \ODataParams(array())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $filter = $this->getFilterForPrimaryKey($args['name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $areas = $dataTable->read($filter, $odata->select, $odata->top, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                                   $odata->skip, $odata->orderby); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         if(empty($areas)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             return $response->withStatus(404); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         if(method_exists($this, 'processEntry')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             $areas[0] = $this->processEntry($areas[0], $request); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         return $response->withJson($areas[0]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     public function updateEntry($request, $response, $args) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         if($this->canRead($request) === false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             return $response->withStatus(401); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         $filter = $this->getFilterForPrimaryKey($args['name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         $dataTable = $this->getDataTable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         $entry = $dataTable->read($filter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         if(empty($entry)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |             return $response->withStatus(404); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         if(count($entry) === 1 && isset($entry[0])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             $entry = $entry[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         if($this->canUpdate($request, $entry) === false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             return $response->withStatus(401); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         $obj = $request->getParsedBody(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         if($this->validateUpdate($obj, $request, $entry) === false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |             return $response->withStatus(400); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         $ret = $dataTable->update($filter, $obj); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         return $response->withJson($ret); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     public function deleteEntry($request, $response, $args) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         if($this->canRead($request) === false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |             return $response->withStatus(401); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |         $filter = $this->getFilterForPrimaryKey($args['name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         $dataTable = $this->getDataTable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         $entry = $dataTable->read($filter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         if(empty($entry)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |             return $response->withStatus(404); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |         if($this->canDelete($request, $entry) === false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |             return $response->withStatus(401); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |         $ret = $dataTable->delete($filter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         return $response->withJson($ret); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 202 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 203 |  |  |  | 
            
                        
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.