Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 1609

Přidáno uživatelem David Raška před více než 12 roky(ů)

Upravy:
- pridan vylucny pristup k instalaci a upgradu databaze, nemuze tak dojit k chybe upgradu pri vicenasobnem pristupu

Zobrazit rozdíly:

freenetis/branches/testing/application/controllers/installation.php
if ($form->validate())
{
$form_data = $form->as_array();
// this executes all queries that upgrade database structure
try
// try to open mutex file
if (($f = @fopen(server::base_dir().'/upload/mutex', 'w')) === FALSE)
{
Version::make_db_up_to_date();
// directory is not writeable
self::error(WRITABLE, server::base_dir().'/upload/');
}
catch (Exception $e)
// acquire an exclusive access to file
// wait while database is being updated
if (flock($f, LOCK_EX))
{
throw new Exception(
__('Database upgrade failed') . ': ' .
$e->getMessage(), 0, $e
);
}
// data
$town_model = new Town_Model();
$street_model = new Street_Model();
try
{
$town_model->transaction_start();
// first access - update db
// other access - skip
if (!Version::is_db_up_to_date())
{
// this executes all queries that upgrade database structure
try
{
Version::make_db_up_to_date();
}
catch (Exception $e)
{
throw new Exception(
__('Database upgrade failed') . ': ' .
$e->getMessage(), 0, $e
);
}
// first member is special, it represents association
$member = new Member_Model();
$member->registration = NULL;
$member->name = $form_data['name'];
// data
$town_model = new Town_Model();
$street_model = new Street_Model();
// address
$town = $town_model->get_town(
$form_data['zip_code'],
$form_data['town'],
$form_data['quarter']
);
try
{
$town_model->transaction_start();
$street = $street_model->get_street($form_data['street'], $town->id);
// first member is special, it represents association
$member = new Member_Model();
$member->registration = NULL;
$member->name = $form_data['name'];
$address_point_model = new Address_point_Model();
// address
$town = $town_model->get_town(
$form_data['zip_code'],
$form_data['town'],
$form_data['quarter']
);
$address_point = $address_point_model->get_address_point(
$form_data['default_country'],
$town->id, $street->id,
$form_data['street_number']
);
$street = $street_model->get_street($form_data['street'], $town->id);
// address point doesn't exist exist, create it
if (!$address_point->id)
{
$address_point->save_throwable();
}
$address_point_model = new Address_point_Model();
$member->address_point_id = $address_point->id;
$address_point = $address_point_model->get_address_point(
$form_data['default_country'],
$town->id, $street->id,
$form_data['street_number']
);
$enum_type = new Enum_type_Model();
$member->type = $enum_type->get_type_id('Honorary member');
$member->entrance_date = date('Y-m-d', $form_data['foundation']);
$member->save_throwable();
// address point doesn't exist exist, create it
if (!$address_point->id)
{
$address_point->save_throwable();
}
// every member has its one primary user of type "member"
$user = new User_Model();
$user->member_id = $member->id;
$user->name = $form_data['login'];
$user->surname = $form_data['login'];
$user->birthday = date('Y-m-d', $form_data['foundation']);
$user->login = $form_data['login'];
$user->password = sha1($form_data['password']);
$user->type = User_Model::MAIN_USER;
$user->application_password = security::generate_password();
$user->save_throwable();
$member->address_point_id = $address_point->id;
// add user to access list as admin
$groups_aro_map = new Groups_aro_map_Model();
$groups_aro_map->aro_id = $user->id;
$groups_aro_map->group_id = Aro_group_Model::ADMINS;
$groups_aro_map->save_throwable();
$enum_type = new Enum_type_Model();
$member->type = $enum_type->get_type_id('Honorary member');
$member->entrance_date = date('Y-m-d', $form_data['foundation']);
$member->save_throwable();
// users telephone
$contact = new Contact_Model();
$contact->type = Contact_Model::TYPE_PHONE;
$contact->value = $form_data['phone'];
$contact->save_throwable();
// every member has its one primary user of type "member"
$user = new User_Model();
$user->member_id = $member->id;
$user->name = $form_data['login'];
$user->surname = $form_data['login'];
$user->birthday = date('Y-m-d', $form_data['foundation']);
$user->login = $form_data['login'];
$user->password = sha1($form_data['password']);
$user->type = User_Model::MAIN_USER;
$user->application_password = security::generate_password();
$user->save_throwable();
$contact->add($user);
$contact->save_throwable();
// add user to access list as admin
$groups_aro_map = new Groups_aro_map_Model();
$groups_aro_map->aro_id = $user->id;
$groups_aro_map->group_id = Aro_group_Model::ADMINS;
$groups_aro_map->save_throwable();
$phone_country = new Country_Model($form_data['default_country']);
$contact->add($phone_country);
$contact->save_throwable();
$contact->clear();
// users telephone
$contact = new Contact_Model();
$contact->type = Contact_Model::TYPE_PHONE;
$contact->value = $form_data['phone'];
$contact->save_throwable();
// users email
if (! empty($form_data['email']))
{
$contact->type = Contact_Model::TYPE_EMAIL;
$contact->value = $form_data['email'];
$contact->save_throwable();
$contact->add($user);
$contact->save_throwable();
}
$contact->add($user);
$contact->save_throwable();
// association has at least one real bank account
$bank_account = new Bank_account_Model();
$bank_account->name = $form_data['account_name'];
$bank_account->member_id = $member->id;
$bank_account->account_nr = $form_data['account_nr'];
$bank_account->bank_nr = $form_data['bank_nr'];
$bank_account->IBAN = $form_data['IBAN'];
$bank_account->SWIFT = $form_data['SWIFT'];
$bank_account->save_throwable();
$phone_country = new Country_Model($form_data['default_country']);
$contact->add($phone_country);
$contact->save_throwable();
$contact->clear();
// these three double-entry accounts are related to one bank account through relation table
// double-entry bank account
$doubleentry_bank_account = new Account_Model();
$doubleentry_bank_account->member_id = $member->id;
$doubleentry_bank_account->name = $form_data['account_name'];
$doubleentry_bank_account->account_attribute_id = Account_attribute_Model::BANK;
$doubleentry_bank_account->comment = __('Bank accounts');
$doubleentry_bank_account->add($bank_account);
$doubleentry_bank_account->save_throwable();
// users email
if (! empty($form_data['email']))
{
$contact->type = Contact_Model::TYPE_EMAIL;
$contact->value = $form_data['email'];
$contact->save_throwable();
$contact->add($user);
$contact->save_throwable();
}
// double-entry account of bank fees
$bank_fees_account = new Account_Model();
$bank_fees_account->member_id = $member->id;
$bank_fees_account->name = $form_data['account_name'].' - '.__('Bank fees');
$bank_fees_account->account_attribute_id = Account_attribute_Model::BANK_FEES;
$bank_fees_account->comment = __('Bank fees');
$bank_fees_account->add($bank_account);
$bank_fees_account->save_throwable();
// association has at least one real bank account
$bank_account = new Bank_account_Model();
$bank_account->name = $form_data['account_name'];
$bank_account->member_id = $member->id;
$bank_account->account_nr = $form_data['account_nr'];
$bank_account->bank_nr = $form_data['bank_nr'];
$bank_account->IBAN = $form_data['IBAN'];
$bank_account->SWIFT = $form_data['SWIFT'];
$bank_account->save_throwable();
// double-entry account of bank interests
$bank_interests_account = new Account_Model();
$bank_interests_account->member_id = $member->id;
$bank_interests_account->name = $form_data['account_name'].' - '.__('Bank interests');
$bank_interests_account->account_attribute_id = Account_attribute_Model::BANK_INTERESTS;
$bank_interests_account->comment = __('Bank interests');
$bank_interests_account->add($bank_account);
$bank_interests_account->save_throwable();
// these three double-entry accounts are related to one bank account through relation table
// double-entry bank account
$doubleentry_bank_account = new Account_Model();
$doubleentry_bank_account->member_id = $member->id;
$doubleentry_bank_account->name = $form_data['account_name'];
$doubleentry_bank_account->account_attribute_id = Account_attribute_Model::BANK;
$doubleentry_bank_account->comment = __('Bank accounts');
$doubleentry_bank_account->add($bank_account);
$doubleentry_bank_account->save_throwable();
// other double entry accounts independent of bank account
// double-entry cash account
$cash_account = new Account_Model();
$cash_account->member_id = $member->id;
$cash_account->name = __('Cash');
$cash_account->account_attribute_id = Account_attribute_Model::CASH;
$cash_account->comment = __('Cash');
$cash_account->save_throwable();
// double-entry account of bank fees
$bank_fees_account = new Account_Model();
$bank_fees_account->member_id = $member->id;
$bank_fees_account->name = $form_data['account_name'].' - '.__('Bank fees');
$bank_fees_account->account_attribute_id = Account_attribute_Model::BANK_FEES;
$bank_fees_account->comment = __('Bank fees');
$bank_fees_account->add($bank_account);
$bank_fees_account->save_throwable();
// double-entry operating account
$operating_account = new Account_Model();
$operating_account->member_id = $member->id;
$operating_account->name = __('Operating account');
$operating_account->account_attribute_id = Account_attribute_Model::OPERATING;
$operating_account->comment = __('Operating account');
$operating_account->save_throwable();
// double-entry account of bank interests
$bank_interests_account = new Account_Model();
$bank_interests_account->member_id = $member->id;
$bank_interests_account->name = $form_data['account_name'].' - '.__('Bank interests');
$bank_interests_account->account_attribute_id = Account_attribute_Model::BANK_INTERESTS;
$bank_interests_account->comment = __('Bank interests');
$bank_interests_account->add($bank_account);
$bank_interests_account->save_throwable();
// double-entry infrastructure account
$infrastructure_account = new Account_Model();
$infrastructure_account->member_id = $member->id;
$infrastructure_account->name = __('Infrastructure account');
$infrastructure_account->account_attribute_id = Account_attribute_Model::INFRASTRUCTURE;
$infrastructure_account->comment = __('Infrastructure account');
$infrastructure_account->save_throwable();
// other double entry accounts independent of bank account
// double-entry cash account
$cash_account = new Account_Model();
$cash_account->member_id = $member->id;
$cash_account->name = __('Cash');
$cash_account->account_attribute_id = Account_attribute_Model::CASH;
$cash_account->comment = __('Cash');
$cash_account->save_throwable();
// double-entry account of purchasers
$purchasers_account = new Account_Model();
$purchasers_account->member_id = $member->id;
$purchasers_account->name = __('Purchasers account');
$purchasers_account->account_attribute_id = Account_attribute_Model::PURCHASERS;
$purchasers_account->comment = __('Purchasers account');
$purchasers_account->save_throwable();
// double-entry operating account
$operating_account = new Account_Model();
$operating_account->member_id = $member->id;
$operating_account->name = __('Operating account');
$operating_account->account_attribute_id = Account_attribute_Model::OPERATING;
$operating_account->comment = __('Operating account');
$operating_account->save_throwable();
// double-entry account of suppliers
$suppliers_account = new Account_Model();
$suppliers_account->member_id = $member->id;
$suppliers_account->name = __('Suppliers account');
$suppliers_account->account_attribute_id = Account_attribute_Model::SUPPLIERS;
$suppliers_account->comment = __('Suppliers account');
$suppliers_account->save_throwable();
// double-entry infrastructure account
$infrastructure_account = new Account_Model();
$infrastructure_account->member_id = $member->id;
$infrastructure_account->name = __('Infrastructure account');
$infrastructure_account->account_attribute_id = Account_attribute_Model::INFRASTRUCTURE;
$infrastructure_account->comment = __('Infrastructure account');
$infrastructure_account->save_throwable();
// double-entry account of received member fees
$fees_account = new Account_Model();
$fees_account->member_id = $member->id;
$fees_account->name = __('Received member fees');
$fees_account->account_attribute_id = Account_attribute_Model::MEMBER_FEES;
$fees_account->comment = __('Received member fees');
$fees_account->save_throwable();
// double-entry account of purchasers
$purchasers_account = new Account_Model();
$purchasers_account->member_id = $member->id;
$purchasers_account->name = __('Purchasers account');
$purchasers_account->account_attribute_id = Account_attribute_Model::PURCHASERS;
$purchasers_account->comment = __('Purchasers account');
$purchasers_account->save_throwable();
// interval of fee availability
$from = date('Y-m-d', $form_data['foundation']);
$to = '9999-12-31 23:59:59';
// double-entry account of suppliers
$suppliers_account = new Account_Model();
$suppliers_account->member_id = $member->id;
$suppliers_account->name = __('Suppliers account');
$suppliers_account->account_attribute_id = Account_attribute_Model::SUPPLIERS;
$suppliers_account->comment = __('Suppliers account');
$suppliers_account->save_throwable();
// entrance fee
$entrance_fee = new Fee_Model();
$entrance_fee->fee = $form_data['entrance_fee'];
$entrance_fee->from = $from;
$entrance_fee->to = $to;
$entrance_fee->type_id = $enum_type->get_type_id('entrance fee');
$entrance_fee->save_throwable();
// double-entry account of received member fees
$fees_account = new Account_Model();
$fees_account->member_id = $member->id;
$fees_account->name = __('Received member fees');
$fees_account->account_attribute_id = Account_attribute_Model::MEMBER_FEES;
$fees_account->comment = __('Received member fees');
$fees_account->save_throwable();
// default entrance fee
$default_entrance_fee = new Members_fee_Model();
$default_entrance_fee->fee_id = $entrance_fee->id;
$default_entrance_fee->member_id = $member->id;
$default_entrance_fee->activation_date = $from;
$default_entrance_fee->deactivation_date = $to;
$default_entrance_fee->priority = 1;
$default_entrance_fee->save_throwable();
// interval of fee availability
$from = date('Y-m-d', $form_data['foundation']);
$to = '9999-12-31 23:59:59';
// regular member fee
$regular_member_fee = new Fee_Model();
$regular_member_fee->fee = $form_data['regular_member_fee'];
$regular_member_fee->from = $from;
$regular_member_fee->to = $to;
$regular_member_fee->type_id = $enum_type->get_type_id('regular member fee');
$regular_member_fee->save_throwable();
// entrance fee
$entrance_fee = new Fee_Model();
$entrance_fee->fee = $form_data['entrance_fee'];
$entrance_fee->from = $from;
$entrance_fee->to = $to;
$entrance_fee->type_id = $enum_type->get_type_id('entrance fee');
$entrance_fee->save_throwable();
// default regular member fee
$default_regular_member_fee = new Members_fee_Model();
$default_regular_member_fee->fee_id = $regular_member_fee->id;
$default_regular_member_fee->member_id = $member->id;
$default_regular_member_fee->activation_date = $from;
$default_regular_member_fee->deactivation_date = $to;
$default_regular_member_fee->priority = 1;
$default_regular_member_fee->save_throwable();
// default entrance fee
$default_entrance_fee = new Members_fee_Model();
$default_entrance_fee->fee_id = $entrance_fee->id;
$default_entrance_fee->member_id = $member->id;
$default_entrance_fee->activation_date = $from;
$default_entrance_fee->deactivation_date = $to;
$default_entrance_fee->priority = 1;
$default_entrance_fee->save_throwable();
// transfer fee
$transfer_fee = new Fee_Model();
$transfer_fee->fee = $form_data['transfer_fee'];
$transfer_fee->from = $from;
$transfer_fee->to = $to;
$transfer_fee->type_id = $enum_type->get_type_id('transfer fee');
$transfer_fee->save_throwable();
// regular member fee
$regular_member_fee = new Fee_Model();
$regular_member_fee->fee = $form_data['regular_member_fee'];
$regular_member_fee->from = $from;
$regular_member_fee->to = $to;
$regular_member_fee->type_id = $enum_type->get_type_id('regular member fee');
$regular_member_fee->save_throwable();
// default transfer fee
$default_transfer_fee = new Members_fee_Model();
$default_transfer_fee->fee_id = $transfer_fee->id;
$default_transfer_fee->member_id = $member->id;
$default_transfer_fee->activation_date = $from;
$default_transfer_fee->deactivation_date = $to;
$default_transfer_fee->priority = 1;
$default_transfer_fee->save_throwable();
// default regular member fee
$default_regular_member_fee = new Members_fee_Model();
$default_regular_member_fee->fee_id = $regular_member_fee->id;
$default_regular_member_fee->member_id = $member->id;
$default_regular_member_fee->activation_date = $from;
$default_regular_member_fee->deactivation_date = $to;
$default_regular_member_fee->priority = 1;
$default_regular_member_fee->save_throwable();
// penalty
$penalty = new Fee_Model();
$penalty->fee = $form_data['penalty'];
$penalty->from = $from;
$penalty->to = $to;
$penalty->type_id = $enum_type->get_type_id('penalty');
$penalty->save_throwable();
// transfer fee
$transfer_fee = new Fee_Model();
$transfer_fee->fee = $form_data['transfer_fee'];
$transfer_fee->from = $from;
$transfer_fee->to = $to;
$transfer_fee->type_id = $enum_type->get_type_id('transfer fee');
$transfer_fee->save_throwable();
// default transfer fee
$default_penalty = new Members_fee_Model();
$default_penalty->fee_id = $penalty->id;
$default_penalty->member_id = $member->id;
$default_penalty->activation_date = $from;
$default_penalty->deactivation_date = $to;
$default_penalty->priority = 1;
$default_penalty->save_throwable();
// default transfer fee
$default_transfer_fee = new Members_fee_Model();
$default_transfer_fee->fee_id = $transfer_fee->id;
$default_transfer_fee->member_id = $member->id;
$default_transfer_fee->activation_date = $from;
$default_transfer_fee->deactivation_date = $to;
$default_transfer_fee->priority = 1;
$default_transfer_fee->save_throwable();
// system settings
Settings::set('title', $form_data['title']);
Settings::set('currency', $form_data['currency']);
Settings::set('default_country', $form_data['default_country']);
// penalty
$penalty = new Fee_Model();
$penalty->fee = $form_data['penalty'];
$penalty->from = $from;
$penalty->to = $to;
$penalty->type_id = $enum_type->get_type_id('penalty');
$penalty->save_throwable();
// saves special (read-only) types of fee
$fee_model = new Fee_Model();
$special_types = array
(
1 => 'Membership interrupt',
2 => 'Fee-free regular member',
3 => 'Non-member',
4 => 'Honorary member'
);
// default transfer fee
$default_penalty = new Members_fee_Model();
$default_penalty->fee_id = $penalty->id;
$default_penalty->member_id = $member->id;
$default_penalty->activation_date = $from;
$default_penalty->deactivation_date = $to;
$default_penalty->priority = 1;
$default_penalty->save_throwable();
foreach ($special_types as $id => $name)
{
$fee_model->clear();
$fee_model->readonly = 1;
$fee_model->fee = 0;
$fee_model->from = $from;
$fee_model->to = $to;
$fee_model->type_id = $enum_type->get_type_id('regular member fee');
$fee_model->name = $name;
$fee_model->special_type_id = $id;
$fee_model->save_throwable();
}
// commit changes
$town_model->transaction_commit();
}
catch (Exception $e)
{
$town_model->transaction_rollback();
throw new Exception(__('Installation has failed'), $e);
}
// system settings
Settings::set('title', $form_data['title']);
Settings::set('currency', $form_data['currency']);
Settings::set('default_country', $form_data['default_country']);
// array for store error
$errors = array();
// saves special (read-only) types of fee
$fee_model = new Fee_Model();
$special_types = array
(
1 => 'Membership interrupt',
2 => 'Fee-free regular member',
3 => 'Non-member',
4 => 'Honorary member'
);
// set base domain
Settings::set('domain', server::http_host());
foreach ($special_types as $id => $name)
{
$fee_model->clear();
$fee_model->readonly = 1;
$fee_model->fee = 0;
$fee_model->from = $from;
$fee_model->to = $to;
$fee_model->type_id = $enum_type->get_type_id('regular member fee');
$fee_model->name = $name;
$fee_model->special_type_id = $id;
$fee_model->save_throwable();
}
// set subdirectory
$suffix = substr(server::script_name(),0,-9);
Settings::set('suffix', $suffix);
// send registration data
if ($form_data['register_install'])
{
@file_get_contents(
REGISTER_URL .
'?name=' . urlencode($form_data['name']) .
'&email=' . urlencode($form_data['email']) .
'&address=' . urlencode($address_point->__toString())
);
}
// commit changes
$town_model->transaction_commit();
}
catch (Exception $e)
{
$town_model->transaction_rollback();
$view = new View('installation/done');
throw new Exception(__('Installation has failed'), $e);
}
// remove index.php from urls
Settings::set('index_page', 0);
$view->errors = $errors;
$view->render(TRUE);
// array for store error
$errors = array();
// set base domain
Settings::set('domain', server::http_host());
// set subdirectory
$suffix = substr(server::script_name(),0,-9);
Settings::set('suffix', $suffix);
// send registration data
if ($form_data['register_install'])
{
@file_get_contents(
REGISTER_URL .
'?name=' . urlencode($form_data['name']) .
'&email=' . urlencode($form_data['email']) .
'&address=' . urlencode($address_point->__toString())
);
}
$view = new View('installation/done');
// remove index.php from urls
Settings::set('index_page', 0);
$view->errors = $errors;
$view->render(TRUE);
}
// unlock mutex file
flock($f, LOCK_UN);
}
// close mutex file
fclose($f);
}
else
{
freenetis/branches/testing/application/libraries/Version.php
public static function is_db_up_to_date()
{
// old type of DB upgrades? => always false
if (is_numeric(self::get_db_version()))
if (is_numeric(self::get_db_version(FALSE)))
{
return false;
}
freenetis/branches/testing/application/libraries/MY_Controller.php
// db schema is not up to date => we must run upgrade
else if (!Version::is_db_up_to_date())
{
try
// try to open mutex file
if (($f = @fopen(server::base_dir().'/upload/mutex', 'w')) === FALSE)
{
Version::make_db_up_to_date();
// directory is not writeable
self::error(WRITABLE, server::base_dir().'/upload/');
}
catch (Old_Mechanism_Exception $ome)
// acquire an exclusive access to file
// wait while database is being updated
if (flock($f, LOCK_EX))
{
self::error(DATABASE_OLD_MECHANISM);
// first access - update db
// other access - skip
if (!Version::is_db_up_to_date())
{
try
{
Version::make_db_up_to_date();
}
catch (Old_Mechanism_Exception $ome)
{
self::error(DATABASE_OLD_MECHANISM);
}
catch (Database_Downgrate_Exception $dde)
{
self::error(DATABASE_DOWNGRATE);
}
catch (Exception $e)
{
throw new Exception(
__('Database upgrade failed') . ': ' .
$e->getMessage(), 0, $e
);
}
}
// unlock mutex file
flock($f, LOCK_UN);
}
catch (Database_Downgrate_Exception $dde)
{
self::error(DATABASE_DOWNGRATE);
}
catch (Exception $e)
{
throw new Exception(
__('Database upgrade failed') . ': ' .
$e->getMessage(), 0, $e
);
}
// close mutex file
fclose($f);
}
// load these variables only for logged user

Také k dispozici: Unified diff