Copyright updated, menu class added for 'home'
[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         /**
35          * Protected constructor
36          *
37          * @return      void
38          */
39         protected function __construct () {
40                 // Call parent constructor
41                 parent::__construct(__CLASS__);
42         }
43
44         /**
45          * Creates an instance of this wrapper class
46          *
47          * @param       $companyInstance        An instance of a generic company class or null if no specific
48          * @return      $wrapperInstance        An instance of this wrapper class
49          * @todo        Find an interface which is suitable for all companies
50          */
51         public final static function createCompanyDatabaseWrapper (ShippingCompany $companyInstance = null) {
52                 // Create a new instance
53                 $wrapperInstance = new CompanyDatabaseWrapper();
54
55                 // Set (primary!) table name
56                 $wrapperInstance->setTableName(self::DB_TABLE_COMPANY_DATA);
57
58                 // Set the company instance if not null
59                 if (!is_null($companyInstance)) {
60                         $wrapperInstance->setCompanyInstance($companyInstance);
61                 } // END - if
62
63                 // Return the instance
64                 return $wrapperInstance;
65         }
66
67         /**
68          * Checks wether the given user participates in a company
69          *
70          * @param       $userInstance   An instance of a user class
71          * @return      $participates   Wether the user participates at lease in one company
72          */
73         public function ifUserParticipatesInCompany (ManageableAccount $userInstance)  {
74                 // By default no user owns any company... ;)
75                 $participates = false;
76
77                 // Get a search criteria class
78                 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
79
80                 // Add the user primary key as a search criteria
81                 $criteriaInstance->addCriteria("participant_id", $userInstance->getPrimaryKey());
82                 $criteriaInstance->setLimit(1);
83
84                 // Set company->user table
85                 $this->setTableName(self::DB_TABLE_COMPANY_USER);
86
87                 // Get the result back
88                 $resultInstance = $this->doSelectByCriteria($criteriaInstance);
89
90                 // Is there a result?
91                 if ($resultInstance->next()) {
92                         // Then cache it
93                         $this->setResultInstance($resultInstance);
94
95                         // Entry found for further analysis/processing
96                         $participates = true;
97                 } // END - if
98
99                 // Return the result
100                 return $participates;
101         }
102
103         /**
104          * Setter for company instance
105          *
106          * @param       $companyInstance        An instance of a generic company
107          * @return      void
108          * @todo        Find an interface suitable for all types of companies
109          */
110         protected final function setCompanyInstance (ShippingCompany $companyInstance) {
111                 $this->companyInstance = $companyInstance;
112         }
113
114         /**
115          * Getter for company instance
116          *
117          * @return      $companyInstance        An instance of a generic company
118          * @todo        Find an interface suitable for all types of companies
119          */
120         public final function getCompanyInstance () {
121                 return $this->companyInstance;
122         }
123 }
124
125 // [EOF]
126 ?>