34715c52a9b784d3c97f6d8f1c2f7ce37cabe4f2
[core.git] / inc / classes / main / criteria / class_DataSetCriteria.php
1 <?php
2 /**
3  * A set of data storeable in databases
4  *
5  * @see                 DatabaseFrontendInterface - An interface for database frontends (front-end to the application)
6  * @author              Roland Haeder <webmaster@ship-simu.org>
7  * @version             0.0.0
8  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, this is free software
9  * @license             GNU GPL 3.0 or any newer version
10  * @link                http://www.ship-simu.org
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  */
25 class DataSetCriteria extends BaseFrameworkSystem implements StoreableCriteria {
26         /**
27          * Table name
28          */
29         private $tableName = "";
30
31         /**
32          * Table columns (criteria) to store
33          */
34         private $tableColumns = array();
35
36         /**
37          * Unique key
38          */
39         private $uniqueKey = "";
40
41         /**
42          * Primary key
43          */
44         private $primaryKey = "";
45
46         /**
47          * Protected constructor
48          *
49          * @return      void
50          */
51         protected function __construct() {
52                 // Call parent constructor
53                 parent::__construct(__CLASS__);
54
55                 // Clean up a little
56                 $this->removeNumberFormaters();
57                 $this->removeSystemArray();
58         }
59
60         /**
61          * Creates an instance of this criteria
62          *
63          * @param       $tableName                      Name of the table
64          * @return      $criteriaInstance       An instance of this criteria
65          */
66         public final static function createDataSetCriteria ($tableName) {
67                 // Get a new instance
68                 $criteriaInstance = new DataSetCriteria();
69
70                 // Set table name
71                 $criteriaInstance->setTableName($tableName);
72
73                 // Return the instance
74                 return $criteriaInstance;
75         }
76
77         /**
78          * Add criteria
79          *
80          * @param       $criteriaKey    Criteria key
81          * @param       $criteriaValue  Criteria value
82          * @return      void
83          */
84         public function addCriteria ($criteriaKey, $criteriaValue) {
85                 $this->tableColumns[(string) $criteriaKey] = $criteriaValue;
86         }
87
88         /**
89          * Add configured criteria
90          *
91          * @param       $criteriaKey    Criteria key
92          * @param       $configEntry    Configuration entry
93          * @return      void
94          */
95         public function addConfiguredCriteria ($criteriaKey, $configEntry) {
96                 // Add configuration entry as criteria
97                 $value = $this->getConfigInstance()->readConfig($configEntry);
98                 $this->addCriteria($criteriaKey, $value);
99         }
100
101         /**
102          * Setter for table name
103          *
104          * @param       $tableName      Name of the table to set
105          * @return      void
106          */
107         public final function setTableName ($tableName) {
108                 $this->tableName = (string) $tableName;
109         }
110
111         /**
112          * Getter for table name
113          *
114          * @return      $tableName      Name of the table to set
115          */
116         public final function getTableName () {
117                 return $this->tableName;
118         }
119
120         /**
121          * Setter for unique key
122          *
123          * @param       $uniqueKey      Column to use as unique key
124          * @return      void
125          */
126         public final function setUniqueKey ($uniqueKey) {
127                 $this->uniqueKey = (string) $uniqueKey;
128         }
129
130         /**
131          * Getter for unique key
132          *
133          * @return      $uniqueKey      Column to use as unique key
134          */
135         public final function getUniqueKey () {
136                 return $this->uniqueKey;
137         }
138
139         /**
140          * Getter for unique key value
141          *
142          * @return      $uniqueValue    Value of the unique key
143          */
144         public final function getUniqueValue () {
145                 return $this->tableColumns[$this->getUniqueKey()];
146         }
147
148         /**
149          * Getter for criteria array
150          *
151          * @return      $tableColumns
152          */
153         public final function getCriteriaArray () {
154                 return $this->tableColumns;
155         }
156
157         /**
158          * Getter for primary key or unique key if not set
159          *
160          * @return      $primaryKey             Primary key or unique key if not set
161          */
162         public final function getPrimaryKey () {
163                 // Get primary key by default
164                 $primaryKey = $this->primaryKey;
165
166                 if (empty($primaryKey)) {
167                         // Get uniqueKey
168                         $primaryKey = $this->getUniqueKey();
169                 } // END - if
170
171                 // Return it
172                 return $primaryKey;
173         }
174
175         /**
176          * Setter for primary key
177          *
178          * @param       $primaryKey             Primary key to set
179          * @return      void
180          */
181         public final function setPrimaryKey ($primaryKey) {
182                 $this->primaryKey = (string) $primaryKey;
183         }
184 }
185
186 // [EOF]
187 ?>