Index: branches/5.2.x/units/addresses/addresses_event_handler.php =================================================================== diff -u -N -r14849 -r14953 --- branches/5.2.x/units/addresses/addresses_event_handler.php (.../addresses_event_handler.php) (revision 14849) +++ branches/5.2.x/units/addresses/addresses_event_handler.php (.../addresses_event_handler.php) (revision 14953) @@ -1,6 +1,6 @@ Array('subitem' => true), - 'OnUpdate' => Array('subitem' => true), - 'OnCreate' => Array('subitem' => true), - 'OnDelete' => Array('subitem' => true), + 'OnItemBuild' => Array ('subitem' => true), + 'OnUpdate' => Array ('subitem' => true), + 'OnCreate' => Array ('subitem' => true), + 'OnDelete' => Array ('subitem' => true), ); $this->permMapping = array_merge($this->permMapping, $permissions); @@ -49,11 +50,13 @@ */ protected function SetCustomQuery(&$event) { - if ($this->Application->isAdminUser) { - return ; + if ( $this->Application->isAdminUser ) { + return; } $object =& $event->getObject(); + /* @var $object kDBList */ + $user_id = $this->Application->RecallVar('user_id'); $object->addFilter('myitems_user','%1$s.PortalUserId = '.$user_id); } @@ -64,14 +67,14 @@ * @param kDBItem $object * @param string $type */ - function processLastUsed(&$object, $type) + protected function processLastUsed(&$object, $type) { - $is_last = $object->GetDBField('LastUsedAs'.$type); - if ($is_last) { - $fields_hash = Array ( - 'LastUsedAs'.$type => 0, - ); - $this->Conn->doUpdate($fields_hash, $object->TableName, 'PortalUserId = '.$object->GetDBField('PortalUserId')); + $is_last = $object->GetDBField('LastUsedAs' . $type); + + if ( $is_last ) { + $fields_hash = Array ('LastUsedAs' . $type => 0); + + $this->Conn->doUpdate($fields_hash, $object->TableName, 'PortalUserId = ' . $object->GetDBField('PortalUserId')); } } @@ -105,7 +108,14 @@ $this->processLastUsed($object, 'Billing'); } - function OnUpdate(&$event) + /** + * Updates kDBItem + * + * @param kEvent $event + * @return void + * @access protected + */ + protected function OnUpdate(kEvent &$event) { parent::OnUpdate($event); @@ -131,12 +141,13 @@ */ function setNextTemplate(&$event) { - if ($this->Application->isAdminUser) { - return ; + if ( $this->Application->isAdminUser ) { + return; } $event->SetRedirectParam('opener', 's'); $next_template = $this->Application->GetVar('next_template'); + if ($next_template) { $event->redirect = $next_template; } @@ -163,103 +174,136 @@ * [HOOK] Update PortalUser table when address marked as ProfileAddress is changed via addr prefix object * * @param kEvent $event + * @return bool + * @access protected */ - function OnUpdateProfileAddress(&$event) + protected function OnUpdateProfileAddress(kEvent &$event) { - $user =& $this->Application->recallObject('u.current'); + $object =& $event->getObject(Array ('skip_autoload' => true)); + /* @var $object kDBItem */ - if ($this->Application->GetVar('billing_address_id') > 0) { - $address_id = $this->Application->GetVar('billing_address_id'); - } - elseif ($this->Application->GetVar('shipping_address_id') > 0) { - $address_id = $this->Application->GetVar('shipping_address_id'); - } - else { - $address_id = false; - } + if ( !$this->Application->isAdmin ) { + $address_id = $this->getOrderAddressId(); - if (!$address_id) { - return true; + if ( !$address_id ) { + return true; + } + + $object->Load($address_id); } - $address =& $event->getObject(Array('skip_autoload' => true)); - $address->Load($address_id); - if (!$address->GetDBField('IsProfileAddress')) { + if ( !$object->GetDBField('IsProfileAddress') ) { return true; } - $field_map = Array( 'Company' => 1, - 'Phone' => 1, - 'Fax' => 1, - 'Email' => 1, - 'Address1' => 'Street', - 'Address2' => 'Street2', - 'City' => 1, - 'State' => 1, - 'Zip' => 1, - 'Country' => 1, - ); + $field_map = Array ( + 'Company' => 1, + 'Phone' => 1, + 'Fax' => 1, + 'Email' => 1, + 'Address1' => 'Street', + 'Address2' => 'Street2', + 'City' => 1, + 'State' => 1, + 'Zip' => 1, + 'Country' => 1, + ); - $user->setName( $address->GetDBField('To') ); + $user =& $this->Application->recallObject($this->Application->isAdmin ? 'u' : 'u.current'); + /* @var $user UsersItem */ + $user->setName( $object->GetDBField('To') ); + foreach ($field_map as $src_field => $dst_field) { - if ($dst_field == 1) $dst_field = $src_field; - $user->SetDBField($dst_field, $address->GetDBField($src_field)); + if ( $dst_field == 1 ) { + $dst_field = $src_field; + } + + $user->SetDBField($dst_field, $object->GetDBField($src_field)); } return $user->Update(); } /** + * Returns ID of address, that is selected in address dropdown on shipping/billing step of checkout + * + * @return int + * @access protected + */ + protected function getOrderAddressId() + { + $ret = false; + + if ( $this->Application->GetVar('billing_address_id') > 0 ) { + $ret = $this->Application->GetVar('billing_address_id'); + } + elseif ( $this->Application->GetVar('shipping_address_id') > 0 ) { + $ret = $this->Application->GetVar('shipping_address_id'); + } + + return $ret; + } + + /** * [HOOK] Create user profile address based on PortalUser table data * * @param kEvent $event + * @return bool + * @access protected */ - function OnUpdateUserProfile(&$event) + protected function OnUpdateUserProfile(kEvent &$event) { $user =& $event->MasterEvent->getObject(); + /* @var $user UsersItem */ - $load_keys = Array('PortalUserId' => $user->GetID(), 'IsProfileAddress' => 1); + $load_keys = Array ('PortalUserId' => $user->GetID(), 'IsProfileAddress' => 1); - $address =& $this->Application->recallObject($event->Prefix.'.-item', null, Array('skip_autoload' => true)); - $address->Load($load_keys); + $object =& $this->Application->recallObject($event->Prefix . '.-item', null, Array ('skip_autoload' => true)); + /* @var $object kDBItem */ - $field_map = Array( 'PortalUserId' => 1, - 'Company' => 1, - 'Phone' => 1, - 'Fax' => 1, - 'Email' => 1, - 'Address1' => 'Street', - 'Address2' => 'Street2', - 'City' => 1, - 'State' => 1, - 'Zip' => 1, - 'Country' => 1, - ); + $object->Load($load_keys); - $full_name = trim($user->GetDBField('FirstName').' '.$user->GetDBField('LastName')); - $address->SetDBField('To', $full_name); - $address->SetDBField('IsProfileAddress', 1); + $field_map = Array ( + 'PortalUserId' => 1, + 'Company' => 1, + 'Phone' => 1, + 'Fax' => 1, + 'Email' => 1, + 'Address1' => 'Street', + 'Address2' => 'Street2', + 'City' => 1, + 'State' => 1, + 'Zip' => 1, + 'Country' => 1, + ); + $full_name = trim($user->GetDBField('FirstName') . ' ' . $user->GetDBField('LastName')); + $object->SetDBField('To', $full_name); + $object->SetDBField('IsProfileAddress', 1); + foreach ($field_map as $dst_field => $src_field) { - if ($src_field == 1) $src_field = $dst_field; - $address->SetDBField($dst_field, $user->GetDBField($src_field)); + if ( $src_field == 1 ) { + $src_field = $dst_field; + } + + $object->SetDBField($dst_field, $user->GetDBField($src_field)); } $sql = 'SELECT SUM(IF(LastUsedAsBilling = 1, 1, 0 )) AS HasBilling, SUM(IF(LastUsedAsShipping = 1, 1, 0)) AS HasShipping - FROM '.$address->TableName.' - WHERE PortalUserId = '.$user->GetID(); + FROM ' . $object->TableName . ' + WHERE PortalUserId = ' . $user->GetID(); $address_status = $this->Conn->GetRow($sql); - if (!$address_status['HasBilling']) { - $address->SetDBField('LastUsedAsBilling', 1); + if ( !$address_status['HasBilling'] ) { + $object->SetDBField('LastUsedAsBilling', 1); } - if (!$address_status['HasShipping']) { - $address->SetDBField('LastUsedAsShipping', 1); + if ( !$address_status['HasShipping'] ) { + $object->SetDBField('LastUsedAsShipping', 1); } - return $address->isLoaded() ? $address->Update() : $address->Create(); + return $object->isLoaded() ? $object->Update() : $object->Create(); } /** @@ -271,16 +315,18 @@ */ function checkItemStatus(&$event) { - if ($this->Application->isAdminUser) { + if ( $this->Application->isAdminUser ) { return true; } - if (!$this->Application->LoggedIn()) { + if ( !$this->Application->LoggedIn() ) { return false; } $object =& $event->getObject(); - if (!$object->isLoaded()) { + /* @var $object kDBItem */ + + if ( !$object->isLoaded() ) { return true; } @@ -308,7 +354,9 @@ $object =& $event->getObject(); /* @var $object kDBItem */ - $object->SetDBField('PortalUserId', $this->Application->RecallVar('user_id')); + if ( !$this->Application->isAdmin ) { + $object->SetDBField('PortalUserId', $this->Application->RecallVar('user_id')); + } $cs_helper =& $this->Application->recallObject('CountryStatesHelper'); /* @var $cs_helper kCountryStatesHelper */ @@ -320,15 +368,22 @@ $this->processLastUsed($object, 'Billing'); } + /** + * Occurs before deleting item, id of item being + * deleted is stored as 'id' event param + * + * @param kEvent $event + * @access public + */ function OnBeforeItemDelete(&$event) { $object =& $event->getObject(); /* @var $object kDBItem */ - if (!$object->isLoaded() || !$this->checkItemStatus($event)) { + if ( !$object->isLoaded() || !$this->checkItemStatus($event) ) { // not trivially loaded object OR not current user address $event->status = kEvent::erPERM_FAIL; - return ; + return; } } @@ -350,4 +405,36 @@ $fields['Country']['default'] = $site_helper->getDefaultCountry('Shipping'); $this->Application->setUnitOption($event->Prefix, 'Fields', $fields); } + + /** + * [HOOK] Creates user addresses editing tab + * + * @param kEvent $event + */ + function OnModifyUsersConfig(&$event) + { + $title_presets = $this->Application->getUnitOption($event->MasterEvent->Prefix, 'TitlePresets'); + + $title_presets['user_edit_addresses'] = Array ( + 'prefixes' => Array ('u', $event->Prefix . '_List'), + 'format' => "#u_status# '#u_titlefield#' - !la_title_Addresses! (#" . $event->Prefix . "_recordcount#)" + ); + + $title_presets['user_address_edit'] = Array ( + 'prefixes' => Array ('u', $event->Prefix), + 'new_status_labels' => Array ($event->Prefix => '!la_title_AddingAddress!'), + 'edit_status_labels' => Array ($event->Prefix => '!la_title_EditingAddress!'), + 'new_titlefield' => Array ($event->Prefix => '!la_title_NewAddress!'), + 'format' => "#u_status# '#u_titlefield#' - #{$event->Prefix}_status#" + ); + $this->Application->setUnitOption($event->MasterEvent->Prefix, 'TitlePresets', $title_presets); + + $edit_tab_presets = $this->Application->getUnitOption($event->MasterEvent->Prefix, 'EditTabPresets'); + + $edit_tab_presets['Default'][] = Array ( + 'title' => 'la_tab_Addresses', 't' => 'in-commerce/users/user_edit_addresses', 'priority' => 6 + ); + + $this->Application->setUnitOption($event->MasterEvent->Prefix, 'EditTabPresets', $edit_tab_presets); + } } \ No newline at end of file