Application->IsAdmin()) { $user_id = $this->Application->RecallVar('user_id'); if ($event->Name == 'OnDelete') { $id = $this->getPassedID($event); $address_dummy =& $this->Application->recallObject($event->Prefix.'.-item', null, Array('skip_autoload' => true)); $address_dummy->Load($id); if ($address_dummy->GetDBField('PortalUserId') == $user_id) { // user is allowed to delete it's own address, but not other user address return true; } } $items_info = $this->Application->GetVar($event->getPrefixSpecial(true)); foreach ($items_info as $id => $field_values) { if ($field_values['PortalUserId'] != $user_id) { // user is trying to perform event on other user address record } if ($event->Name == 'OnCreate' || $event->Name == 'OnUpdate') { // all checks were ok -> it's user's order -> allow to modify return true; } } } return parent::CheckPermission($event); } /** * Get's special of main item for linking with subitem * * @param kEvent $event * @return string */ function getMainSpecial(&$event) { return ''; } /** * Enter description here... * * @param kEvent $event */ function SetCustomQuery(&$event) { $types = $event->getEventParam('types'); if($types == 'myaddresses') { $object =& $event->getObject(); $user_id = $this->Application->RecallVar('user_id'); $object->addFilter('myitems_user','%1$s.PortalUserId = '.$user_id); } } /** * Set currently logged in user as owner of address * * @param kEvent $event */ function OnNew(&$event) { parent::OnNew($event); $object =& $event->getObject(); $object->SetDBField('PortalUserId', $this->Application->RecallVar('user_id') ); } /** * Update LastUsed statuses for address * * @param kEvent $event */ function setLastUsed(&$event) { $object =& $event->getObject(Array('skip_autoload' => true)); $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); if ($items_info) { list($id, $field_values) = each($items_info); $this->processLastUsed($field_values, 'Shipping'); $this->processLastUsed($field_values, 'Billing'); $items_info[$id] = $field_values; $this->Application->SetVar( $event->getPrefixSpecial(true), $items_info ); } } function processLastUsed(&$field_values, $type) { $is_last = $field_values['LastUsedAs'.$type]; if ($is_last) { $sql = 'UPDATE '.$this->Application->getUnitOption($this->Prefix, 'TableName').' SET LastUsedAs'.$type.' = 0 WHERE PortalUserId = '.$field_values['PortalUserId']; $this->Conn->Query($sql); } $field_values['LastUsedAs'.$type] = (int)$is_last; } function OnUpdate(&$event) { $cs_helper =& $this->Application->recallObject('CountryStatesHelper'); $cs_helper->CheckStateField($event, 'State', 'Country'); $this->setLastUsed($event); parent::OnUpdate($event); $this->setNextTemplate($event); } /** * Creates new user * * @param kEvent $event */ function OnCreate(&$event) { $cs_helper =& $this->Application->recallObject('CountryStatesHelper'); $cs_helper->CheckStateField($event, 'State', 'Country'); $this->setLastUsed($event); parent::OnCreate($event); $this->setNextTemplate($event); } /** * Enter description here... * * @param kEvent $event */ function setNextTemplate(&$event) { if( !$this->Application->IsAdmin() ) { $event->redirect_params['opener'] = 's'; $next_template = $this->Application->GetVar('next_template'); if($next_template) $event->redirect = $next_template; } } /** * [HOOK] Prefill states dropdown with correct values * * @param kEvent $event * @access public */ function OnPrepareStates(&$event) { $cs_helper =& $this->Application->recallObject('CountryStatesHelper'); $cs_helper->PopulateStates($event, 'State', 'Country'); $object =& $event->MasterEvent->getObject(); if( $object->isRequired('Country') && $cs_helper->CountryHasStates( $object->GetDBField('Country') ) ) $object->setRequired('State', true); } /** * [HOOK] Update PortalUser table when address marked as ProfileAddress is changed via addr prefix object * * @param kEvent $event */ function OnUpdateProfileAddress(&$event) { $user =& $this->Application->recallObject('u.current'); 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 (!$address_id) { return true; } $address =& $event->getObject(Array('skip_autoload' => true)); $address->Load($address_id); if (!$address->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, ); $user->setName( $address->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)); } return $user->Update(); } /** * [HOOK] Create user profile address based on PortalUser table data * * @param kEvent $event */ function OnUpdateUserProfile(&$event) { $user =& $event->MasterEvent->getObject(); $load_keys = Array('PortalUserId' => $user->GetID(), 'IsProfileAddress' => 1); $address =& $this->Application->recallObject($event->Prefix.'.-item', null, Array('skip_autoload' => true)); $address->Load($load_keys); $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')); $address->SetDBField('To', $full_name); $address->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)); } $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(); $address_status = $this->Conn->GetRow($sql); if (!$address_status['HasBilling']) { $address->SetDBField('LastUsedAsBilling', 1); } if (!$address_status['HasShipping']) { $address->SetDBField('LastUsedAsShipping', 1); } return $address->isLoaded() ? $address->Update() : $address->Create(); } } ?>