CAPTCHA support basicly finished (weak CAPTCHA!)
[shipsimu.git] / inc / classes / main / database / wrapper / class_NewsDatabaseWrapper.php
1 <?php
2 /**
3  * A database wrapper for news classes
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 NewsDatabaseWrapper extends BaseDatabaseWrapper {
26         /**
27          * Cache instance
28          */
29         private $cacheInstance = null;
30
31         // Constants for exceptions
32         const EXCEPTION_CLIENT_USERNAME_NOT_FOUND = 0x160;
33
34         // Constants for database columns
35
36         // Constants for database table names
37         const DB_TABLE_NEWS = "news";
38
39         /**
40          * Protected constructor
41          *
42          * @return      void
43          */
44         protected function __construct() {
45                 // Call parent constructor
46                 parent::__construct(__CLASS__);
47
48                 // Set part description
49                 $this->setObjectDescription("Database wrapper for user objects");
50
51                 // Create unique ID number
52                 $this->generateUniqueId();
53         }
54
55         /**
56          * Creates an instance of this database wrapper by a provided user class
57          *
58          * @return      $wrapperInstance        An instance of the created wrapper class
59          */
60         public final static function createNewsDatabaseWrapper () {
61                 // Get a new instance
62                 $wrapperInstance = new NewsDatabaseWrapper();
63
64                 // Initialize the cache instance
65                 $wrapperInstance->initCacheInstance();
66
67                 // Return the instance
68                 return $wrapperInstance;
69         }
70
71         /**
72          * Initializes the cache instance with a new object
73          *
74          * @return      void
75          */
76         protected function initCacheInstance () {
77                 // Set the new instance
78                 $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache();
79         }
80
81         /**
82          * Do a "select" query on the user table with the given search criteria and
83          * store it in cache for later usage
84          *
85          * @param       $criteriaInstance       An instance of a Criteria class
86          * @return      $resultInstance         An instance of a database result class
87          */
88         public function doSelectByCriteria (Criteria $criteriaInstance) {
89                 // First get a key suitable for our cache and extend it with this class name
90                 $cacheKey = sprintf("%s@%s",
91                         $this->__toString(),
92                         $criteriaInstance->getCacheKey()
93                 );
94
95                 // Does this key exists in cache?
96                 if ($this->cacheInstance->offsetExists($cacheKey)) {
97                         // Then use this result
98                         $result = $cacheInstance->offsetGet($cacheKey);
99                 } else {
100                         // Now it's time to ask the database layer for this select statement
101                         $result = $this->getDatabaseInstance()->doSelectByTableCriteria(self::DB_TABLE_NEWS, $criteriaInstance);
102
103                         // Cache the result if not null
104                         if (!is_null($result)) {
105                                 // A valid result has returned from the database layer
106                                 $this->cacheInstance->offsetSet($cacheKey, $result);
107                         } else {
108                                 // This invalid result must be wrapped
109                                 $result = array(
110                                         'status'                => "invalid",
111                                         'exception'             => $this->getDatabaseInstance()->getLastException()
112                                 );
113                         }
114                 }
115
116                 // Create an instance of a DatabaseResult class with the given result
117                 $resultInstance = DatabaseResult::createDatabaseResult($result);
118
119                 // And return the instance
120                 return $resultInstance;
121         }
122 }
123
124 // [EOF]
125 ?>