示例#1
0
 function scenarios_get()
 {
     // Set a limit, could be expanded later on
     $limit = 15;
     $scenarios = new Scenario();
     $i = 0;
     while ($i < 2) {
         if ($this->get('scenarios') || $this->get('modules') || $this->get('aps') || $this->get('quests')) {
             $scenarios->group_start();
         }
         // Filter: Scenarios
         if ($this->get('scenarios')) {
             $scenarios->or_where('type', 'scenario');
         }
         // Filter: Modules
         if ($this->get('modules')) {
             $scenarios->or_where('type', 'mod');
         }
         // Filter: Adventure paths
         if ($this->get('aps')) {
             $scenarios->or_where('type', 'ap');
         }
         // Filter: Quests
         if ($this->get('quests')) {
             $scenarios->or_where('type', 'quest');
         }
         if ($this->get('scenarios') || $this->get('modules') || $this->get('aps') || $this->get('quests')) {
             $scenarios->group_end();
         }
         if (!$this->get('scenarios') && !$this->get('modules') && !$this->get('aps') && !$this->get('quests')) {
             // Dirty, but works
             $scenarios->where('type', 'nonexistent');
         }
         // Filter: Season
         if ($this->get('season')) {
             $scenarios->where_in('season', $this->get('season'));
         }
         // Filter: Retired
         if (!$this->get('retired')) {
             $scenarios->where('archived IS NULL', NULL);
         }
         // Filter: Specials
         if (!$this->get('specials')) {
             $scenarios->not_like('name', '%Special: %');
         }
         // Filter: Search
         if ($this->get('search')) {
             $scenarios->like('name', $this->get('search'));
         }
         // Filter: Author
         if ($this->get('author')) {
             $scenarios->like_related_authors('name', $this->get('author'))->distinct();
         }
         // Filter: Level range
         if ($this->get('levels')) {
             $levels = $this->get('levels');
             foreach ($levels as $level) {
                 $scenarios->like('levelrange', $level);
             }
         }
         if ($this->get('levelRangeMin') && $this->get('levelRangeMax')) {
             $scenarios->group_start();
             $levelrange = range($this->get('levelRangeMin'), $this->get('levelRangeMax'));
             foreach ($levelrange as $key => $value) {
                 $value = str_pad($value, 2, '0', STR_PAD_LEFT);
                 $scenarios->or_like('levelrange', $value);
             }
             $scenarios->group_end();
         }
         // Filter: Evergreen
         if ($this->get('evergreen')) {
             $scenarios->where('evergreen', 1);
         }
         // Filter: Players
         if ($this->get('player') && $this->get('campaign')) {
             $pfsnumbers = array();
             $subq_players = new Scenario();
             $subq_players->select('id')->where_join_field('players', $this->get('campaign') . ' IS NOT NULL', null)->where_in_related_players('pfsnumber', $this->get('player'));
             $scenarios->where_not_in_subquery('id', $subq_players);
         }
         // Sorting
         if ($this->get('sorting')) {
             if ($this->get('sorting') == 'name_asc') {
                 $scenarios->order_by('name', 'asc');
             } elseif ($this->get('sorting') == 'name_desc') {
                 $scenarios->order_by('name', 'desc');
             } elseif ($this->get('sorting') == 'season_asc') {
                 $scenarios->order_by('season', 'asc');
                 $scenarios->order_by('cast(number as unsigned)', 'asc');
                 $scenarios->order_by('name', 'asc');
             } elseif ($this->get('sorting') == 'season_desc') {
                 $scenarios->order_by('season', 'desc');
                 $scenarios->order_by('cast(number as unsigned)', 'desc');
                 $scenarios->order_by('name', 'desc');
             }
         }
         // Pagination
         if ($i == 0) {
             $scenarios->get();
             $response['count'] = $scenarios->result_count();
         } else {
             // First time through, just count
             if ($this->get('currentPage')) {
                 $page = $limit * ($this->get('currentPage') - 1);
                 $scenarios->limit($limit, $page);
             } else {
                 // Just limit it to X scenarios
                 $scenarios->limit($limit);
             }
             $scenarios->get();
             //$scenarios->check_last_query();
         }
         $i++;
     }
     if ($scenarios->exists()) {
         $scenarios_array = $scenarios->all_to_array();
         foreach ($scenarios_array as $key => $value) {
             foreach ($scenarios as $scenario) {
                 if ($scenario->id == $value['id']) {
                     $scenarios_array[$key]['authors'] = $scenario->authors->all_to_single_array('name');
                     //$scenarios_array[$key]['subtiers'] = $scenario->subtiers->all_to_single_array('name');
                 }
             }
         }
         $response['scenarios'] = $scenarios_array;
         $this->response($response, 200);
         // 200 being the HTTP response code
     } else {
         $response['scenarios'] = array();
         $response['total'] = 0;
         $this->response($response, 200);
     }
 }