3 * A database wrapper for city sections
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2015 City Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class CitySectionsDatabaseWrapper extends BaseDatabaseWrapper implements CitySectionsWrapper, Registerable {
25 // Constants for database table names
26 const DB_TABLE_CITY_SECTIONS = 'city_sections';
28 // Section id, an referenced city id and lot id
29 const DB_COLUMN_SECTION_ID = 'city_section_id';
30 const DB_COLUMN_CITY_ID = 'city_id';
31 const DB_COLUMN_LOT_ID = 'lot_id';
33 // Section and sub type (e.g. residential, hut)
34 const DB_COLUMN_SECTION_TYPE = 'section_type';
35 const DB_COLUMN_SECTION_SUB_TYPE = 'section_sub_type';
38 const DB_COLUMN_SECTION_POSITION_X = 'section_position_x';
39 const DB_COLUMN_SECTION_POSITION_Y = 'section_position_y';
40 const DB_COLUMN_SECTION_POSITION_Z = 'section_position_z';
42 // Connected neigbouring sections
43 const DB_COLUMN_SECTION_NEIGHBOUR_WEST_ID = 'section_neighbour_west_id';
44 const DB_COLUMN_SECTION_NEIGHBOUR_EAST_ID = 'section_neighbour_east_id';
45 const DB_COLUMN_SECTION_NEIGHBOUR_NORTH_ID = 'section_neighbour_north_id';
46 const DB_COLUMN_SECTION_NEIGHBOUR_SOUTH_ID = 'section_neighbour_south_id';
47 const DB_COLUMN_SECTION_NEIGHBOUR_UP_ID = 'section_neighbour_up_id';
48 const DB_COLUMN_SECTION_NEIGHBOUR_DOWN_ID = 'section_neighbour_down_id';
51 // Reserved section (see documentation)
52 const DB_COLUMN_SECTION_RESERVED = 'section_reserved';
55 * Protected constructor
59 protected function __construct () {
60 // Call parent constructor
61 parent::__construct(__CLASS__);
65 * Creates an instance of this database wrapper by a provided user class
67 * @return $wrapperInstance An instance of the created wrapper class
69 public static final function createCitySectionsDatabaseWrapper () {
71 $wrapperInstance = new CitySectionsDatabaseWrapper();
73 // Set (primary!) table name
74 $wrapperInstance->setTableName(self::DB_TABLE_CITY_SECTIONS);
76 // Return the instance
77 return $wrapperInstance;
81 * Checks if the given city id is found in sections table
83 * @param $cityId City id to check
84 * @return $isFound Whether the city id is found
86 public function ifCityHasSections ($cityId) {
87 // Get search instance
88 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
90 // Search for 'city_id'
91 $searchInstance->addCriteria(self::DB_COLUMN_CITY_ID, $cityId);
94 * Only one entry is enough to find, else this query could run very\
97 $searchInstance->setLimit(1);
99 // Execute it on database instance
100 $resultInstance = $this->doSelectByCriteria($searchInstance);
102 // Check if there is one entry
103 $isFound = ($resultInstance->next() == 1);
110 * Expands the sections table with initial data for given city id
112 * @param $cityId City id to check
113 * @return $ids Sections ids from initial expansion
115 public function doInitialCityExpansion ($cityId) {
116 // Make sure this city has no sections
117 assert(!$this->ifCityHasSections($cityId));
120 $this->partialStub('cityId=' . $cityId . ' - UNFINISHED!');