3 * A wrapper for database access to shipping company data
5 * @author Roland Haeder <webmaster@shipsimu.org>
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.shipsimu.org
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.
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.
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/>.
24 class CompanyDatabaseWrapper extends BaseDatabaseWrapper {
28 private $companyInstance = null;
30 // Constants for database tables
31 const DB_TABLE_COMPANY_DATA = 'company';
32 const DB_TABLE_COMPANY_USER = 'company_user';
34 // Constants for database columns
35 const DB_COLUMN_PARTICIPANT_ID = 'participant_id';
38 * Protected constructor
42 protected function __construct () {
43 // Call parent constructor
44 parent::__construct(__CLASS__);
48 * Creates an instance of this wrapper class
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
54 public static final function createCompanyDatabaseWrapper (ShippingCompany $companyInstance = null) {
55 // Create a new instance
56 $wrapperInstance = new CompanyDatabaseWrapper();
58 // Set (primary!) table name
59 $wrapperInstance->setTableName(self::DB_TABLE_COMPANY_DATA);
61 // Set the company instance if not null
62 if (!is_null($companyInstance)) {
63 $wrapperInstance->setCompanyInstance($companyInstance);
66 // Return the instance
67 return $wrapperInstance;
71 * Checks wether the given user participates in a company
73 * @param $userInstance An instance of a user class
74 * @return $participates Wether the user participates at lease in one company
76 public function ifUserParticipatesInCompany (ManageableAccount $userInstance) {
77 // By default no user owns any company... ;)
78 $participates = false;
80 // Get a search criteria class
81 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
83 // Add the user primary key as a search criteria
84 $searchInstance->addCriteria(self::DB_COLUMN_PARTICIPANT_ID, $userInstance->getPrimaryKey());
85 $searchInstance->setLimit(1);
87 // Set company->user table
88 $this->setTableName(self::DB_TABLE_COMPANY_USER);
90 // Get the result back
91 $resultInstance = $this->doSelectByCriteria($searchInstance);
94 if ($resultInstance->next()) {
96 $this->setResultInstance($resultInstance);
98 // Entry found for further analysis/processing
103 return $participates;
107 * Setter for company instance
109 * @param $companyInstance An instance of a generic company
111 * @todo Find an interface suitable for all types of companies
113 protected final function setCompanyInstance (ShippingCompany $companyInstance) {
114 $this->companyInstance = $companyInstance;
118 * Getter for company instance
120 * @return $companyInstance An instance of a generic company
121 * @todo Find an interface suitable for all types of companies
123 public final function getCompanyInstance () {
124 return $this->companyInstance;