'public static final' is correct
[shipsimu.git] / application / ship-simu / main / wrapper / class_CompanyDatabaseWrapper.php
1 <?php
2 /**
3  * A wrapper for database access to shipping company data
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 Ship-Simu Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.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 CompanyDatabaseWrapper extends BaseDatabaseWrapper {
25         /**
26          * Company instance
27          */
28         private $companyInstance = null;
29
30         // Constants for database tables
31         const DB_TABLE_COMPANY_DATA = 'company';
32         const DB_TABLE_COMPANY_USER = 'company_user';
33
34         // Constants for database columns
35         const DB_COLUMN_PARTICIPANT_ID = 'participant_id';
36
37         /**
38          * Protected constructor
39          *
40          * @return      void
41          */
42         protected function __construct () {
43                 // Call parent constructor
44                 parent::__construct(__CLASS__);
45         }
46
47         /**
48          * Creates an instance of this wrapper class
49          *
50          * @param       $companyInstance        An instance of a generic company class or null if no specific
51          * @return      $wrapperInstance        An instance of this wrapper class
52          * @todo        Find an interface which is suitable for all companies
53          */
54         public static final function createCompanyDatabaseWrapper (ShippingCompany $companyInstance = null) {
55                 // Create a new instance
56                 $wrapperInstance = new CompanyDatabaseWrapper();
57
58                 // Set (primary!) table name
59                 $wrapperInstance->setTableName(self::DB_TABLE_COMPANY_DATA);
60
61                 // Set the company instance if not null
62                 if (!is_null($companyInstance)) {
63                         $wrapperInstance->setCompanyInstance($companyInstance);
64                 } // END - if
65
66                 // Return the instance
67                 return $wrapperInstance;
68         }
69
70         /**
71          * Checks wether the given user participates in a company
72          *
73          * @param       $userInstance   An instance of a user class
74          * @return      $participates   Wether the user participates at lease in one company
75          */
76         public function ifUserParticipatesInCompany (ManageableAccount $userInstance)  {
77                 // By default no user owns any company... ;)
78                 $participates = false;
79
80                 // Get a search criteria class
81                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
82
83                 // Add the user primary key as a search criteria
84                 $searchInstance->addCriteria(self::DB_COLUMN_PARTICIPANT_ID, $userInstance->getPrimaryKey());
85                 $searchInstance->setLimit(1);
86
87                 // Set company->user table
88                 $this->setTableName(self::DB_TABLE_COMPANY_USER);
89
90                 // Get the result back
91                 $resultInstance = $this->doSelectByCriteria($searchInstance);
92
93                 // Is there a result?
94                 if ($resultInstance->next()) {
95                         // Then cache it
96                         $this->setResultInstance($resultInstance);
97
98                         // Entry found for further analysis/processing
99                         $participates = true;
100                 } // END - if
101
102                 // Return the result
103                 return $participates;
104         }
105
106         /**
107          * Setter for company instance
108          *
109          * @param       $companyInstance        An instance of a generic company
110          * @return      void
111          * @todo        Find an interface suitable for all types of companies
112          */
113         protected final function setCompanyInstance (ShippingCompany $companyInstance) {
114                 $this->companyInstance = $companyInstance;
115         }
116
117         /**
118          * Getter for company instance
119          *
120          * @return      $companyInstance        An instance of a generic company
121          * @todo        Find an interface suitable for all types of companies
122          */
123         public final function getCompanyInstance () {
124                 return $this->companyInstance;
125         }
126 }
127
128 // [EOF]
129 ?>