Renamed classes/main/ to main/classes/ + added FuseFeature, an upcoming feature
[core.git] / inc / main / classes / criteria / dataset / class_DataSetCriteria.php
1 <?php
2 /**
3  * A set of data storeable in databases
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.org
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
25         /**
26          * Table name
27          */
28         private $tableName = '';
29
30         /**
31          * Unique key
32          */
33         private $uniqueKey = '';
34
35         /**
36          * Primary key
37          */
38         private $primaryKey = '';
39
40         /**
41          * Primary keys
42          */
43         private $primaryKeys = array();
44
45         /**
46          * Protected constructor
47          *
48          * @return      void
49          */
50         protected function __construct () {
51                 // Call parent constructor
52                 parent::__construct(__CLASS__);
53         }
54
55         /**
56          * Creates an instance of this criteria
57          *
58          * @param       $tableName                      Name of the table
59          * @return      $criteriaInstance       An instance of this criteria
60          */
61         public static final function createDataSetCriteria ($tableName) {
62                 // Get a new instance
63                 $criteriaInstance = new DataSetCriteria();
64
65                 // Set table name
66                 $criteriaInstance->setTableName($tableName);
67
68                 // Return the instance
69                 return $criteriaInstance;
70         }
71
72         /**
73          * Setter for table name
74          *
75          * @param       $tableName      Name of the table to set
76          * @return      void
77          */
78         public final function setTableName ($tableName) {
79                 $this->tableName = (string) $tableName;
80         }
81
82         /**
83          * Getter for table name
84          *
85          * @return      $tableName      Name of the table to set
86          */
87         public final function getTableName () {
88                 return $this->tableName;
89         }
90
91         /**
92          * Setter for unique key
93          *
94          * @param       $uniqueKey      Column to use as unique key
95          * @return      void
96          */
97         public final function setUniqueKey ($uniqueKey) {
98                 $this->uniqueKey = (string) $uniqueKey;
99         }
100
101         /**
102          * Getter for unique key
103          *
104          * @return      $uniqueKey      Column to use as unique key
105          */
106         public final function getUniqueKey () {
107                 return $this->uniqueKey;
108         }
109
110         /**
111          * Getter for unique key value
112          *
113          * @return      $uniqueValue    Value of the unique key
114          */
115         public final function getUniqueValue () {
116                 // Get primary key(s) first
117                 $primaryKey  = trim($this->getCriteriaElemnent($this->getPrimaryKey()));
118                 $primaryKeys = $this->getPrimaryKeys();
119
120                 // Debug message
121                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',primaryKey=' . $primaryKey . ',primaryKeys()=' . count($primaryKeys));
122
123                 /*
124                  * If this is not set, this could mean a badly written frontend as
125                  * tables should always have a primary key.
126                  */
127                 if (count($primaryKeys) > 0) {
128                         /*
129                          * Init return value, this can be put all together without any
130                          * separator as it only aids as a "unique value" for generating the
131                          * "row file name".
132                          */
133                         $return = '';
134
135                         // Combination set, so get all
136                         foreach ($primaryKeys as $primaryKey) {
137                                 // Add it
138                                 $return .= trim($this->getCriteriaElemnent($primaryKey));
139                         } // END - foreach
140
141                         // Debug message
142                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',return=' . $return . ' - EXIT!');
143
144                         // Return it
145                         return $return;
146                 } elseif (!empty($primaryKey)) {
147                         // Return primary key
148                         return $primaryKey;
149                 } else {
150                         // @TODO Issue a warning
151                         self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: Primary key not set for table ' . $this->getTableName() . ', please fix your table. Falling back to unique key ...');
152
153                         // Get unique key
154                         $uniqueKey = trim($this->getCriteriaElemnent($this->getUniqueKey()));
155
156                         // Debug message
157                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',uniqueKey=' . $uniqueKey);
158
159                         // Is it empty, too?
160                         if (empty($uniqueKey)) {
161                                 // Bad news, nothing is "unique" by design for this table
162                                 ApplicationEntryPoint::app_exit('Table ' . $this->getTableName() . ' has both no primary and unique key, but ' . __METHOD__ . ' was called. Please fix your table.');
163                         } else {
164                                 // Return unique key
165                                 return $uniqueKey;
166                         }
167                 }
168         }
169
170         /**
171          * Getter for primary key or unique key if not set
172          *
173          * @return      $primaryKey             Primary key or unique key if not set
174          */
175         public final function getPrimaryKey () {
176                 // Get primary key by default
177                 $primaryKey = $this->primaryKey;
178
179                 if (empty($primaryKey)) {
180                         // Get uniqueKey
181                         $primaryKey = $this->getUniqueKey();
182                 } // END - if
183
184                 // Return it
185                 return $primaryKey;
186         }
187
188         /**
189          * Setter for primary key
190          *
191          * @param       $primaryKey             Primary key to set
192          * @return      void
193          */
194         public final function setPrimaryKey ($primaryKey) {
195                 $this->primaryKey = (string) $primaryKey;
196         }
197
198         /**
199          * Setter for primary key array
200          *
201          * @param       $primaryKeys    Primary key array to set
202          * @return      void
203          */
204         public function setPrimaryKeyCombined (array $primaryKeys) {
205                 $this->primaryKeys = $primaryKeys;
206         }
207
208         /**
209          * Getter for primary keys
210          *
211          * @return      $primaryKeys    Primary key array
212          */
213         public final function getPrimaryKeys () {
214                 // Return it
215                 return $this->primaryKeys;
216         }
217 }
218
219 // [EOF]
220 ?>