Ejemplo n.º 1
0
 static function mergeAll(Entity $e1, Entity $e2)
 {
     $db = Doctrine_Manager::connection();
     try {
         $db->beginTransaction();
         //MERGE ENTITIES AND EXTENSIONS
         $e1 = self::mergeBasic($e1, $e2);
         //MERGE CONTACT INFO
         //phones are moved to e1
         foreach ($e2->Phone as $phone) {
             if ($e1->id) {
                 $q = LsDoctrineQuery::create()->from('Phone p')->where('p.entity_id = ? AND p.number = ?', array($e1->id, $phone->number));
                 //not if e1 already has a phone with same number
                 if ($q->count()) {
                     continue;
                 }
             }
             $phone->entity_id = $e1['id'];
             $phone->setMerge(true);
             $phone->save();
         }
         //emails are moved to e1
         foreach ($e2->Email as $email) {
             if ($e1->id) {
                 $q = LsDoctrineQuery::create()->from('Email e')->where('e.entity_id = ? AND e.address = ?', array($e1->id, $email->address));
                 //not if e1 already has the same email
                 if ($q->count()) {
                     continue;
                 }
             }
             $email->entity_id = $e1['id'];
             $email->setMerge(true);
             $email->save();
         }
         //addresses
         foreach ($e2->Address as $address) {
             if ($e1->id) {
                 if ($address->latitude && $address->longitude) {
                     //not if e1 already has an address with the same coords
                     if (AddressTable::getByCoordsQuery($address->longitude, $address->latitude, $e1)->count()) {
                         continue;
                     }
                 }
             }
             $address->entity_id = $e1['id'];
             $address->setMerge(true);
             $address->save();
         }
         //RELATIONSHIPS
         if ($e2->id) {
             //where e2 is first
             foreach ($e2->getRelationshipsQuery(1)->execute() as $rel) {
                 //don't try to merge relationships, there isn't a good way to do this automatically
                 $rel->entity1_id = $e1['id'];
                 $rel->setMerge(true);
                 $rel->save();
             }
             //where e2 is second
             foreach ($e2->getRelationshipsQuery(2)->execute() as $rel) {
                 //don't try to merge relationships, there isn't a good way to do this automatically
                 $rel->entity2_id = $e1['id'];
                 $rel->setMerge(true);
                 $rel->save();
             }
         }
         //LISTS
         //lists are moved to e1
         foreach ($e2->LsListEntity as $listEntity) {
             if ($e1->id) {
                 $q = LsQuery::getByModelAndFieldsQuery('LsListEntity', array('entity_id' => $e1->id, 'list_id' => $listEntity->list_id));
                 //not if e1 is already on the list
                 if ($q->count()) {
                     continue;
                 }
             }
             $listEntity->entity_id = $e1;
             $listEntity->setMerge(true);
             $listEntity->save();
         }
         //IMAGES
         //images are moved to e1
         foreach ($e2->Image as $image) {
             $image->entity_id = $e1;
             $image->setMerge(true);
             $image->save();
         }
         //ALIASES
         //aliases are moved to e1
         foreach ($e2->Alias as $alias) {
             //not if e1 already has that alias
             if ($e1->id && $e1->hasName($alias->name, $matchContext = true, $alias->context)) {
                 continue;
             }
             $alias->entity_id = $e1;
             $alias->is_primary = false;
             $alias->setMerge(true);
             $alias->save();
         }
         //save now so that template merges work
         $e1->last_user_id = LsVersionableListener::getUserId();
         $e1->updated_at = LsDate::getCurrentDateTime();
         $e1->setMerge(true);
         $e1->save();
         //set merged_id for e2
         $e2->merged_id = $e1['id'];
         //TEMPLATES
         foreach ($e2->_table->getTemplates() as $template) {
             //templates must have mergeFrom method
             if (method_exists($template, 'mergeFrom')) {
                 $template->setInvoker($e1);
                 $template->mergeFrom($e2);
             }
         }
         //OTHER RELATIONS
         foreach ($e2->Child as $child) {
             $child->parent_id = $e1->id;
             $child->setMerge(true);
             $child->save();
         }
         foreach ($e2->PartyMember as $member) {
             $member->party_id = $e1->id;
             $member->setMerge(true);
             $member->save();
         }
         foreach ($e2->BusinessIndustry as $bi) {
             $q = LsDoctrineQuery::create()->from('BusinessIndustry bi')->where('bi.business_id = ? AND bi.industry_id = ?', array($e1->id, $bi->industry_id));
             if (!$q->count()) {
                 $bi->business_id = $e1->id;
                 $bi->save();
             }
         }
         foreach ($e2->OverseenPosition as $position) {
             $position->boss_id = $e1->id;
             $position->save();
         }
         foreach ($e2->BundledDonation as $donation) {
             $donation->bundler_id = $e1->id;
             $donation->save();
         }
         foreach ($e2->LobbyFilingLobbyist as $lfl) {
             $lfl->lobbyist_id = $e1->id;
             $lfl->save();
         }
         foreach ($e2->Contact1Transaction as $t) {
             $t->contact1_id = $e1->id;
             $t->save();
         }
         foreach ($e2->Contact2Transaction as $t) {
             $t->contact2_id = $e1->id;
             $t->save();
         }
         //NOTES
         sfLoader::loadHelpers('Url');
         $notes = LsDoctrineQuery::create()->from('Note n')->where('n.entity_ids LIKE ?', '%,' . $e2->id . ',%')->execute();
         foreach ($notes as $note) {
             $note->body = preg_replace('#/(person|org)/' . $e2->id . '/([^"]+)#i', '/\\1/' . $e1->id . '/' . LsSlug::convertNameToSlug($e1->name), $note->body);
             $note->body_raw = preg_replace('#@entity:' . $e2->id . '(\\[([^\\]]+)\\])?#', '@entity:' . $e1->id . '\\1', $note->body_raw);
             $note->entity_ids = str_replace(',' . $e2->id . ',', ',' . $e1->id . ',', $note->entity_ids);
             $note->save();
         }
         //DONATION MATCHES
         //get matches for e2
         if ($e2->id != '') {
             $matches2 = Doctrine::getTable('OsEntityTransaction')->findByEntityId($e2->id);
             //get matches for e1
             $matches1 = Doctrine::getTable('OsEntityTransaction')->findByEntityId($e1->id);
             $matches1Sorted = array();
             //sort matches for e1
             foreach ($matches1 as $match1) {
                 $matches1Sorted[$match1['cycle']][$match1['transaction_id']] = $match1;
             }
             //loop through e2 matches
             foreach ($matches2 as $match2) {
                 //if e1 matched exists, update as necessary
                 if ($match1 = @$matches1Sorted[$match2['cycle']][$match2['transaction_id']]) {
                     $save = false;
                     if (!$match1['is_verified'] && $match2['is_verified']) {
                         $match1->is_verified = true;
                         $save = true;
                     }
                     if (!$match1['is_processed'] && $match2['is_processed']) {
                         $match1->is_processed = true;
                         $save = true;
                     }
                     if (!$match1['is_synced'] && $matche2['is_synced']) {
                         $match1->is_synced = true;
                         $save = true;
                     }
                     if ($save) {
                         $match1->save();
                     }
                     $match2->delete();
                 } else {
                     //if e1 match doesn't exist, update the entity_id of the e2 match
                     $match2->entity_id = $e1->id;
                     $match2->save();
                 }
             }
             //DONATION MATCHING PREPROCESS LOG
             $cycles2 = Doctrine::getTable('OsEntityPreprocess')->findByEntityId($e2->id);
             $cycles1 = OsEntityPreprocessTable::getCyclesByEntityId($e1->id);
             foreach ($cycles2 as $cycle2) {
                 //if e1 already preprocessed for this cycle, delete
                 if (in_array($cycle2['cycle'], $cycles1)) {
                     $cycle2->delete();
                 } else {
                     //if not, update entity_id
                     $cycle2->entity_id = $e1->id;
                     $cycle2->save();
                 }
             }
         }
         //EXTERNAL KEYS
         foreach ($e2->ExternalKey as $key) {
             $q = LsDoctrineQuery::create()->from('ExternalKey k')->where('k.entity_id = ?', $e1->id)->andWhere('k.domain_id = ?', $key['domain_id']);
             if (!($existingKey = $q->fetchOne())) {
                 $key->entity_id = $e1->id;
                 $key->save();
             }
         }
         $db->commit();
     } catch (Exception $e) {
         $db->rollback();
         throw $e;
     }
     $e2->clearRelated();
     return $e1;
 }
Ejemplo n.º 2
0
 public function addAddress($address)
 {
     if (!($address = AddressTable::standardize($address))) {
         return false;
     }
     if ($this->exists()) {
         if ($address->latitude && $address->longitude && AddressTable::getByCoordsQuery($address->longitude, $address->latitude, $this)->count()) {
             return false;
         } else {
             if (!$address->latitude && AddressTable::retrieveByAddress($address, false, $this)) {
                 return false;
             }
         }
     }
     $this->Address[] = $address;
     return $address;
 }
Ejemplo n.º 3
0
 public function executeEditAddress($request)
 {
     $this->address = Doctrine::getTable('Address')->find($request->getParameter('id'));
     $this->forward404Unless($this->address);
     $this->entity = $this->address->Entity;
     $this->forward404Unless($this->entity);
     $this->address_form = new AddressForm($this->address);
     $this->reference_form = new ReferenceForm();
     $this->reference_form->setSelectObject($this->entity, false, true);
     if ($request->isMethod('post')) {
         $params = $request->getParameter('address');
         $refParams = $request->getParameter('reference');
         $this->address_form->bind($params);
         $this->reference_form->bind($refParams);
         if ($this->address_form->isValid() && $this->reference_form->isValid()) {
             $this->address_form->updateObject();
             $address = $this->address_form->getObject();
             //standardize
             $address = AddressTable::standardize($address);
             //make sure edited address isn't a duplicate
             if ($address->latitude && $address->longitude) {
                 $q = AddressTable::getByCoordsQuery($address->longitude, $address->latitude, $this->entity);
                 $q->addWhere('a.entity_id <> ?', $this->entity->id);
                 if (!$q->count()) {
                     //save address and reference
                     $address->saveWithRequiredReference($refParams);
                     $this->clearCache($this->entity);
                     $this->redirect('entity/address?id=' . $address->id);
                 } else {
                     $validator = new sfValidatorString(array(), array('invalid' => 'This ' . strtolower($this->entity->getPrimaryExtension()) . ' already has an address with the same coordinates'));
                     $this->address_form->getErrorSchema()->addError(new sfValidatorError($validator, 'invalid'));
                 }
             } else {
                 $address->save();
                 $this->clearCache($this->entity);
                 $this->redirect('entity/address?id=' . $address->id);
             }
         }
     }
 }