Exemplo n.º 1
0
 public function execute()
 {
     if (!$this->safeToRun('address')) {
         $this->printDebug('script already running');
         die;
     }
     if (!$this->_addresses) {
         $this->setAddresses();
     }
     if (!count($this->_addresses)) {
         echo 'no more addresses';
         die;
     }
     foreach ($this->_addresses as $address) {
         try {
             $this->db->beginTransaction();
             $this->printDebug('standardizing ' . $address->getOneLiner() . ' entity: ' . $address->entity_id);
             $address = AddressTable::standardize($address);
             if ($address) {
                 $address->save();
                 $this->printDebug('address saved');
                 $this->printDebug('saved as ' . $address->getOneLiner());
             }
             $this->saveMeta('standardization', 'last_processed', $address->id);
             $this->db->commit();
         } catch (Exception $e) {
             $this->db->rollback();
             throw $e;
         }
     }
 }
Exemplo n.º 2
0
<tr>
  <td>
    <?php 
echo sprintf("%01.1f", AddressTable::getDistanceBetweenCoords($object->latitude, $object->longitude, $base_object->latitude, $base_object->longitude));
?>
  </td>
  <td><?php 
echo entity_link($object->Entity, null);
?>
</td>
  <td>
    <span class="text_small"><?php 
echo $object->getOneLiner();
?>
</span>
  </td>
</tr>
Exemplo n.º 3
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;
 }
 public function importAddresses($id)
 {
     $entity = Doctrine::getTable('Entity')->find($id);
     if (!$entity) {
         return false;
     }
     $this->printDebug("\nImporting addresses for entity " . $id . ": " . $entity->name);
     $addresses = array();
     //get all the transactions
     $sql = 'SELECT cycle, transaction_id FROM os_entity_transaction WHERE entity_id = ? AND is_verified = 1 AND cycle > ?';
     $stmt = $this->db->execute($sql, array($id, $this->after_cycle));
     $trans = $stmt->fetchAll(PDO::FETCH_ASSOC);
     $this->printDebug("Found " . count($trans) . " transactions...");
     $unique_address_parts = array();
     $unique_addresses = array();
     foreach ($trans as $tran) {
         $sql = 'SELECT street, city, state, zip, fec_id, date FROM os_donation WHERE cycle = ? AND row_id = ? ' . 'AND street IS NOT NULL AND zip IS NOT NULL AND state IS NOT NULL GROUP BY street, city, state, zip';
         $stmt = $this->rawDb->execute($sql, array($tran['cycle'], $tran['transaction_id']));
         if (!($address = $stmt->fetch(PDO::FETCH_ASSOC))) {
             $this->printDebug("Couldn't find complete address for donation " . $tran['cycle'] . "-" . $tran['transaction_id'] . "; skipping...");
             continue;
         }
         $addr2 = array_map(function ($a) {
             return strtolower($a);
         }, array_slice($address, 0, 4));
         if (preg_match("/\\d+ +(.*?) +/is", $addr2['street'], $str)) {
             $addr2['street'] = $str[0];
         }
         if (!in_array($addr2, $unique_addresses2)) {
             $unique_addresses2[] = $addr2;
             $unique_addresses[] = $address;
         }
     }
     foreach ($unique_addresses as $address) {
         //get state id
         $sql = 'SELECT id FROM address_state WHERE abbreviation = ?';
         $stmt = $this->db->execute($sql, array($address['state']));
         if (!($stateId = $stmt->fetch(PDO::FETCH_COLUMN))) {
             $this->printDebug("Couldn't parse address: " . $str . "; skipping...");
             continue;
         }
         $str = $address['street'] . ', ' . $address['city'] . ', ' . $address['state'] . ' ' . $address['zip'];
         $a = AddressTable::parseV3($str);
         $this->ct++;
         $a->entity_id = $id;
         //only save if zips match
         if ($a->postal && trim($a->postal) != '' && $a->postal != $address['zip']) {
             $this->printDebug("Zips don't match, " . $a->postal . " / " . $address['zip'] . ", {$str} ; skipping...");
             continue;
         }
         //only save if longitude and latitude and street and state are set
         if ($a->longitude && $a->latitude && $a->street1 && $a->state_id) {
             //make sure it's not a duplicate
             $sql = 'SELECT COUNT(id) FROM address WHERE entity_id = ? AND ((longitude = ? AND latitude = ?) OR street1 = ?) AND is_deleted = 0';
             $stmt = $this->db->execute($sql, array($id, $a->longitude, $a->latitude, $a->street1));
             if ($stmt->fetch(PDO::FETCH_COLUMN)) {
                 $this->printDebug("Duplicate address: " . $a->getOneLiner() . "; skipping...");
                 continue;
             } else {
                 $a->save();
                 $this->printDebug("+ Imported address: " . $a->getOneLiner());
                 if ($address['fec_id']) {
                     $ref = new Reference();
                     $ref->object_model = 'Address';
                     $ref->object_id = $a->id;
                     $ref->source = 'http://images.nictusa.com/cgi-bin/fecimg/?' . $address['fec_id'];
                     $ref->name = 'FEC Filing ' . $address['fec_id'];
                     if ($address['date']) {
                         $ref->publication_date = $address['date'];
                     }
                     $ref->save();
                     $this->printDebug("  (with reference)");
                 }
             }
         } else {
             $this->printDebug("\tCouldn't parse address: " . $str . "; skipping...(" . $a->longitude . " " . $a->latitude . " " . $a->street1 . " " . $a->state_id . ")");
             continue;
         }
     }
     if (count($trans)) {
         $this->saveMeta($id, 'matched');
     } else {
         $this->saveMeta($id, 'no matches');
     }
 }
Exemplo n.º 5
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;
 }
Exemplo n.º 6
0
 public function executeAddress($request)
 {
     if ($str = $request->getParameter('address_search')) {
         if ($this->address = AddressTable::parse($str)) {
             $page = $request->getParameter('page', 1);
             $num = $request->getParameter('num', 20);
             $q = $this->address->getNearbyAddressesQuery(0.2)->groupBy('e.id');
             $this->results_pager = new Doctrine_Pager($q, $page, $num);
         } else {
             $request->setError('address_search_terms', 'The address you entered seems to be invalid');
         }
     }
 }
Exemplo n.º 7
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);
             }
         }
     }
 }