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