Exemple #1
0
 public function GET($args)
 {
     global $database;
     $sel = new \Selector('Player');
     $sort = false;
     $sortColumn = '';
     $sortOrder = '';
     $limit = 10;
     foreach ($_GET as $column => $value) {
         switch ($column) {
             case 'id':
                 $sel->filter([['Player.id', '=', $value]]);
                 break;
             case 'firstname':
             case 'lastname':
                 if (strlen($value) < 3) {
                     $this->players = [];
                     return;
                 }
                 $value = str_replace('%', '\\%', $value);
                 $value = str_replace('[', '\\[', $value);
                 $value = str_replace(']', '\\]', $value);
                 $value = str_replace('_', '\\_', $value);
                 $value = '%' . $value . '%';
                 $sel->filter([['Player.' . $column, 'LIKE', $value]]);
                 break;
             case 'sort':
                 if ($value == 'match') {
                     $sort = true;
                     $sortColumn = 'playedMatches';
                     $sel->join('PlaysMatchInTeam', 'id', 'playerId');
                     $sel->group('Player.id');
                     $sel->select(['Player.*', 'COUNT(*) as playedMatches']);
                 } elseif ($value == 'goal') {
                     $sort = true;
                     $sortColumn = 'goals';
                     $sel->join('Goal', 'id', 'playerId');
                     $sel->group('Player.id');
                     $sel->select(['Player.*', 'COUNT(*) as goals']);
                 } elseif ($value == 'firstname') {
                     $sort = true;
                     $sortColumn = 'firstName';
                 }
                 break;
             case 'order':
                 $sortOrder = $value;
                 break;
             case 'search':
                 $value = str_replace('%', '\\%', $value);
                 $value = str_replace('[', '\\[', $value);
                 $value = str_replace(']', '\\]', $value);
                 $value = str_replace('_', '\\_', $value);
                 $value = explode(' ', $value);
                 $filters = [];
                 foreach ($value as $term) {
                     $filters[] = ['Player.firstname', 'LIKE ', $term];
                     $filters[] = ['Player.lastname', 'LIKE ', $term];
                 }
                 $sel->filter($filters);
                 break;
             case 'limit':
                 $limit = $value;
                 break;
         }
     }
     if ($sort) {
         if ($sortOrder === '') {
             $sel->order($sortColumn, 'DESC');
         } else {
             $sel->order($sortColumn, $sortOrder);
         }
     }
     $sel->limit(0, $limit);
     $result = $database->select($sel);
     $this->result = $database->resultToPlayers($result);
 }