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