Exemplo n.º 1
0
 public function execute()
 {
     if (!$this->safeToRun('fortune1000')) {
         $this->printDebug('script already running');
         die;
     }
     $this->setList();
     switch ($this->year) {
         case 2008:
             $this->getCompanyList2008();
             echo "list imported\n";
     }
     while ($company = current($this->companies)) {
         try {
             $this->db->beginTransaction();
             $company['name'] = OrgTable::stripNamePunctuation($company['name']);
             $rank = $company['rank'];
             $existing = Doctrine_Query::create()->from('Entity e')->where('name = ?', $company['name']);
             if ($existing->count() == 0) {
                 switch ($this->year) {
                     case 2008:
                         $corp = $this->getCompany2008($fortune_id = $company['fortune_id'], $name = $company['name'], $revenue = $company['revenue']);
                 }
             } else {
                 //echo "corp already exists\n";
                 $corp = $existing->fetchOne();
             }
             if ($corp) {
                 //two corps can have the same rank, so searches for duplicate entity_id and rank
                 $rank_existing = Doctrine_Query::create()->from('LsListEntity L')->where('list_id = ? and rank = ? and entity_id = ?', array($this->list->id, $rank, $corp->id))->count();
                 if ($rank_existing == 0) {
                     $listentity = new LsListEntity();
                     $listentity->entity_id = $corp->id;
                     $listentity->list_id = $this->list->id;
                     $listentity->rank = $rank;
                     $listentity->save();
                     echo "{$rank} {$corp->name} (saved)\n";
                 } else {
                     echo "{$rank} {$corp->name} (already saved)\n";
                 }
             }
             unset($corp);
             if (!$this->testMode) {
                 $this->db->commit();
             }
         } catch (Exception $e) {
             $this->db->rollback();
             throw $e;
         }
         next($this->companies);
     }
 }
Exemplo n.º 2
0
 static function addByListIdAndEntityId($listId, $entityId)
 {
     $le = LsDoctrineQuery::create()->from('LsListEntity le')->where('le.list_id = ?', $listId)->andWhere('le.entity_id = ?', $entityId)->fetchOne();
     if ($le) {
         if ($le['is_deleted']) {
             $le->is_deleted = 0;
             $le->save();
         }
     } else {
         $le = new LsListEntity();
         $le->list_id = $listId;
         $le->entity_id = $entityId;
         $le->save();
     }
     return $le;
 }
Exemplo n.º 3
0
 protected function execute($arguments = array(), $options = array())
 {
     $configuration = ProjectConfiguration::getApplicationConfiguration($arguments['application'], $options['env'], true);
     $databaseManager = new sfDatabaseManager($configuration);
     $databaseManager->initialize($configuration);
     $db = Doctrine_Manager::connection();
     $list_id = $options['list_id'];
     if ($options['direct_entity_ids']) {
         $entity_ids_for_list = explode(",", $options['direct_entity_ids']);
     } else {
         $entity_ids = "(" . $options['related_entity_ids'] . ")";
         $category_ids = "(" . $options['category_ids'] . ")";
         $sql = 'SELECT l.entity2_id FROM link l LEFT JOIN relationship r ON r.id = l.relationship_id WHERE l.entity1_id in ' . $entity_ids . ' and l.category_id in ' . $category_ids;
         if ($options['description']) {
             $sql .= " and (r.description1 like '" . $options['description'] . "' or r.description2 like '" . $options['description'] . "')";
         }
         if ($options['is_current']) {
             $sql .= " and r.is_current = " . $options['is_current'];
         }
         $stmt = $db->execute($sql);
         $entity_ids_for_list = $stmt->fetchAll(PDO::FETCH_COLUMN);
     }
     try {
         $db->beginTransaction();
         foreach ($entity_ids_for_list as $entity_id) {
             $sql = 'SELECT count(entity_id) FROM ls_list_entity WHERE entity_id = ? and list_id = ?';
             $stmt = $db->execute($sql, array($entity_id, $list_id));
             $ct = $stmt->fetch(PDO::FETCH_COLUMN);
             if (!$ct) {
                 $list_entity = new LsListEntity();
                 $list_entity->entity_id = $entity_id;
                 $list_entity->list_id = $list_id;
                 $list_entity->save();
             }
         }
         $db->commit();
     } catch (Exception $e) {
         $db->rollback();
     }
 }
Exemplo n.º 4
0
 public function saveEntityList($con = null)
 {
     if (!$this->isValid()) {
         throw $this->getErrorSchema();
     }
     if (!isset($this->widgetSchema['entity_list'])) {
         // somebody has unset this widget
         return;
     }
     if (is_null($con)) {
         $con = $this->getConnection();
     }
     $q = Doctrine_Query::create()->delete()->from('LsListEntity r')->where('r.list_id = ?', current($this->object->identifier()))->execute();
     $values = $this->getValue('entity_list');
     if (is_array($values)) {
         foreach ($values as $value) {
             $obj = new LsListEntity();
             $obj->list_id = current($this->object->identifier());
             $obj->entity_id = $value;
             $obj->save();
         }
     }
 }
 public function addListMember($member)
 {
     $q = LsDoctrineQuery::create()->from('LsListEntity le')->where('le.list_id = ? AND le.entity_id = ?', array($this->_sessionListId, $member->id));
     if (!$q->count()) {
         $le = new LsListEntity();
         $le->entity_id = $member->id;
         $le->list_id = $this->_sessionListId;
         $le->save();
     }
 }
Exemplo n.º 6
0
 /**
  * Saves Entity and (by default) adds/removes/updates extensions
  *
  * @param   Doctrine_Connection   $conn             Optional connection instance
  * @param   boolean               $saveExtensions   Whether to save extensions & their data; true by default
  * @return  Entity                                  The saved Entity
  *
  * @see Doctrine_record
  */
 public function save(Doctrine_Connection $conn = null, $saveExtensions = true, array $networkIds = null)
 {
     if (!$this->exists()) {
         $this->_isFirstSave = true;
     }
     if ($conn === null) {
         $conn = Doctrine_Manager::connection();
     }
     //make sure extensions are loaded
     $this->_loadExtensions();
     try {
         $conn->beginTransaction();
         if ($saveExtensions) {
             //run extensions' onEntitySave methods
             foreach ($this->_extensionObjects as $name => $object) {
                 if (method_exists($object, 'onEntitySave')) {
                     $object->onEntitySave($this);
                 }
             }
         }
         //set primary extension
         if (!$this->primary_ext) {
             $this->primary_ext = $this->getPrimaryExtension();
         }
         if ($this->primary_ext == 'Person') {
             $this->name = PersonTable::nameizePersonName($this->name);
         }
         //set delta field for sphinx indexing (in rails)
         $this->delta = true;
         //save entity
         $ret = parent::save($conn);
         //add primary alias if needed
         if (!LsDoctrineQuery::create()->from('Alias a')->where('a.entity_id = ?', $this->id)->count()) {
             $a = new Alias();
             $a->entity_id = $this->id;
             $a->name = $this->rawGet('name');
             $a->is_primary = true;
             $a->save(null, false);
         }
         if ($saveExtensions) {
             //save extensions
             foreach ($this->_extensionObjects as $name => $object) {
                 //set entity_id in case it isn't set
                 $object->entity_id = $this->id;
                 $object->save($conn);
             }
             //save new extensions
             foreach (array_keys($this->_extensionsAdded) as $name) {
                 $def = $this->_extensionDefinitions[$name];
                 //record shouldn't already exist
                 if ($this->getExtensionRecordForDefinition($def)) {
                     throw new Exception("Can't add record for extension " . $name . "; alrady exists");
                 }
                 //create record
                 $record = new ExtensionRecord();
                 $record->Entity = $this;
                 $record->Definition = $def;
                 $record->save($conn);
             }
             $this->_extensionsAdded = array();
             //remove extensions
             foreach ($this->_extensionsRemoved as $name => $extAry) {
                 $def = $extAry['definition'];
                 if ($def->has_fields) {
                     $object = $extAry['object'];
                     $object->delete($conn);
                 }
                 if (!($record = $this->getExtensionRecordForDefinition($def))) {
                     throw new Exception("Can't remove record for extension " . $name . "; doesn't exist");
                 }
                 $record->delete($conn);
             }
         }
         //if creating, add entity to specified networks, otherwise, default network
         if ($this->_isFirstSave) {
             if ($networkIds) {
                 foreach ($networkIds as $networkId) {
                     $le = new LsListEntity();
                     $le->list_id = $networkId;
                     $le->entity_id = $this->id;
                     $le->save(null, true, false);
                     //prevents it from trying to update the entity updated_at and last_user_id fields
                 }
             } else {
                 if (sfContext::hasInstance() && ($user = sfContext::getInstance()->getUser()->getGuardUser())) {
                     $networkId = $user->Profile->home_network_id;
                 } else {
                     $networkId = LsListTable::US_NETWORK_ID;
                 }
                 $le = new LsListEntity();
                 $le->list_id = $networkId;
                 $le->entity_id = $this->id;
                 $le->save(null, true, false);
                 //prevents it from trying to update the entity updated_at and last_user_id fields
             }
         }
         $conn->commit();
     } catch (Exception $e) {
         $conn->rollback();
         throw $e;
     }
     $this->_isFirstSave = false;
     return $ret;
 }
Exemplo n.º 7
0
 protected function addToList($list)
 {
     try {
         $this->db->beginTransaction();
         foreach ($this->edits as &$edit) {
             $entity_id = $edit['New Person'] ? $edit['New Person'] : $edit['Existing Person'];
             $entity = Doctrine::getTable('Entity')->find($entity_id);
             $q = LsDoctrineQuery::create()->from('LsListEntity l')->where('l.entity_id = ? and l.list_id = ?', array($entity->id, $list->id))->fetchOne();
             if ($q) {
                 $this->printDebug('List Entity already saved');
             } else {
                 $le = new LsListEntity();
                 $le->LsList = $list;
                 $le->Entity = $entity;
                 if (isset($edit['rank'])) {
                     $le->rank = $edit['rank'];
                     unset($edit['rank']);
                 }
                 $le->save();
                 //$edit['lists'] = $le;
             }
         }
         $this->db->commit();
     } catch (Exception $e) {
         $this->db->rollback();
         throw $e;
     }
 }
Exemplo n.º 8
0
 public function executeAddBulk($request)
 {
     $this->checkList($request, false, false);
     $this->reference_form = new ReferenceForm();
     $this->reference_form->setSelectObject($this->list);
     $this->csv_form = new CsvUploadForm();
     if ($request->isMethod('post')) {
         $commit = $request->getParameter('commit');
         if ($commit == 'Cancel') {
             $this->redirect(LsListTable::getInternalUrl($this->list));
         }
         // IF REFERENCE INFO AND FILE HAVE BEEN SUBMITTED, LOAD DATA IN
         if ($request->hasParameter('reference') && $request->hasParameter('csv')) {
             $csvParams = $request->getParameter('csv');
             $filePath = $request->getFilePath('csv[file]');
             $this->csv_form->bind($csvParams, $request->getFiles('csv'));
             $refParams = $request->getParameter('reference');
             $this->reference_form->bind($refParams);
             if ($this->reference_form->isValid()) {
                 if ($spreadsheetArr = LsSpreadsheet::parse($filePath)) {
                     $names = $spreadsheetArr['rows'];
                     if (!in_array('name', $spreadsheetArr['headers'])) {
                         $request->setError('csv', 'The file you uploaded could not be parsed properly because there is no "name" column.');
                         return;
                     }
                 } else {
                     $request->setError('csv', 'The file you uploaded could not be parsed properly.');
                     return;
                 }
                 if ($this->ref_id = $refParams['existing_source']) {
                     $ref = Doctrine::getTable('Reference')->find($this->ref_id);
                     $url = $ref->source;
                 } else {
                     $ref = new Reference();
                     $ref->object_model = 'LsList';
                     $ref->object_id = $this->list->id;
                     $ref->source = $refParams['source'];
                     $ref->name = $refParams['name'];
                     $ref->source_detail = $refParams['source_detail'];
                     $ref->publication_date = $refParams['publication_date'];
                     $ref->save();
                     $this->ref_id = $ref->id;
                 }
                 $this->default_type = $request->getParameter('default_type');
                 if (!$this->default_type) {
                     $request->setError('csv', 'You need to choose a default type.');
                     return;
                 }
                 $this->extensions = ExtensionDefinitionTable::getByTier(2, $this->default_type);
                 $extensions_arr = array();
                 foreach ($this->extensions as $ext) {
                     $extensions_arr[] = $ext->name;
                 }
                 $this->matches = array();
                 if (isset($names) && count($names) > 0) {
                     for ($i = 0; $i < count($names); $i++) {
                         if (isset($names[$i]['name']) && trim($names[$i]['name']) != '') {
                             $name = $names[$i]['name'];
                             $name_terms = $name;
                             if ($this->default_type == 'Person') {
                                 $name_parts = preg_split('/\\s+/', $name);
                                 if (count($name_parts) > 1) {
                                     $name_terms = PersonTable::nameSearch($name, true);
                                 }
                                 $terms = $name_terms;
                                 $primary_ext = "Person";
                             } else {
                                 if ($this->default_type == 'Org') {
                                     $name_terms = OrgTable::nameSearch($name);
                                     $terms = $name_terms;
                                     $primary_ext = "Org";
                                 } else {
                                     $terms = $name_terms;
                                     $primary_ext = null;
                                 }
                             }
                             $pager = EntityTable::getSphinxPager($terms, $page = 1, $num = 20, $listIds = null, $aliases = true, $primary_ext);
                             $match = array('name' => $name);
                             $match['search_results'] = $pager->execute();
                             $match['blurb'] = isset($names[$i]['blurb']) ? $names[$i]['blurb'] : null;
                             $match['rank'] = isset($names[$i]['rank']) ? $names[$i]['rank'] : null;
                             $match['types'] = array();
                             if (isset($names[$i]['types'])) {
                                 $types = explode(',', $names[$i]['types']);
                                 $types = array_map('trim', $types);
                                 foreach ($types as $type) {
                                     if (in_array($type, $extensions_arr)) {
                                         $match['types'][] = $type;
                                     }
                                 }
                             }
                             $this->matches[] = $match;
                         }
                     }
                 }
             }
         } else {
             if ($request->hasParameter('ref_id')) {
                 $this->ref_id = $this->getRequestParameter('ref_id');
                 $entity_ids = array();
                 $default_type = $this->getRequestParameter('default_type');
                 for ($i = 0; $i < $this->getRequestParameter('count'); $i++) {
                     if ($entity_id = $request->getParameter('entity_' . $i)) {
                         $selected_entity_id = null;
                         if ($entity_id == 'new') {
                             $name = $request->getParameter('new_name_' . $i);
                             if ($default_type == 'Person') {
                                 $new_entity = PersonTable::parseFlatName($name);
                             } else {
                                 $new_entity = new Entity();
                                 $new_entity->addExtension('Org');
                                 $new_entity->name = trim($name);
                             }
                             if ($types = $request->getParameter('new_extensions_' . $i)) {
                                 foreach ($types as $type) {
                                     $new_entity->addExtension($type);
                                 }
                             }
                             $new_entity->save();
                             $new_entity->blurb = $request->getParameter('new_blurb_' . $i);
                             $ref = Doctrine::getTable('Reference')->find($request->getParameter('ref_id'));
                             $new_entity->addReference($ref->source, null, null, $ref->name);
                             $new_entity->save();
                             $selected_entity_id = $new_entity->id;
                         } else {
                             if ($entity_id > 0) {
                                 $selected_entity_id = $entity_id;
                             }
                         }
                         if ($selected_entity_id) {
                             $q = LsDoctrineQuery::create()->from('LsListEntity le')->where('le.list_id = ? AND le.entity_id = ?', array($this->list['id'], $selected_entity_id));
                             if (!$q->count()) {
                                 $ls_list_entity = new LsListEntity();
                                 $ls_list_entity->list_id = $this->list->id;
                                 $ls_list_entity->entity_id = $selected_entity_id;
                                 $ls_list_entity->rank = $request->getParameter('entity_' . $i . '_rank');
                                 $ls_list_entity->save();
                                 LsCache::clearEntityCacheById($selected_entity_id);
                             }
                         }
                     }
                 }
                 $this->clearCache($this->list);
                 $this->clearRailsCache($this->list->id);
                 $this->redirect($this->list->getInternalUrl());
             } else {
                 $request->setError('name', 'The name you entered is invalid');
             }
         }
     }
 }
Exemplo n.º 9
0
 public function parseResults($match)
 {
     if (isset($match['bio'])) {
         $bio_dirty = LsHtml::replaceEntities(LsString::spacesToSpace(LsHtml::stripTags($match['bio'], "; ")));
         $bio_dirty = preg_replace('/(\\;\\s)+/is', '; ', $bio_dirty);
     }
     foreach ($match as $k => &$m) {
         $m = LsHtml::replaceEntities(LsString::spacesToSpace(LsHtml::stripTags($m, " ")));
     }
     if (isset($match['name'])) {
         $name = $match['name'];
         $bio = '';
         if (isset($match['bio'])) {
             $bio = $match['bio'];
         }
     } else {
         return;
     }
     $this->printDebug("_________________________\n\nname: " . $name . "\n");
     $this->printDebug("bio: " . $bio . "\n");
     $accept = strtolower($this->readline('Process this entity? (n to skip) '));
     if ($accept == 'n' || $accept == 'no') {
         return false;
     }
     if (!$this->org_org) {
         if ($this->last_first) {
             $entity = PersonTable::parseCommaName($name);
         } else {
             $entity = PersonTable::parseFlatName($name);
         }
         $similar_entities = PersonTable::getSimilarQuery2($entity)->execute();
     } else {
         $entity = new Entity();
         $entity->addExtension('Org');
         foreach ($this->org_extensions as $ext) {
             $entity->addExtension($ext);
         }
         $entity->setEntityField('name', $name);
         $name = trim($name);
         $name = str_replace('.', '', $name);
         $similar_entities = OrgTable::getSimilarQuery($entity)->execute();
     }
     $matched = false;
     foreach ($similar_entities as $similar_entity) {
         if ($similar_entity['primary_ext'] == 'Person') {
             $this->printDebug('  POSSIBLE MATCH: ' . $similar_entity->name . ' (Orgs :: ' . $similar_entity->getRelatedOrgsSummary() . "  Bio :: {$similar_entity->summary})");
         } else {
             $this->printDebug('  POSSIBLE MATCH: ' . $similar_entity->name . ' (Summary :: ' . $similar_entity->summary . ')');
         }
         $accept = $this->readline('  Is this the same entity? (y or n)');
         $attempts = 1;
         while ($accept != 'y' && $accept != 'n' && $attempts < 5) {
             $accept = $this->readline('  Is this the same entity? (y or n) ');
             $attempts++;
         }
         if ($accept == 'y') {
             $entity = $similar_entity;
             $matched = true;
             $this->printDebug('             [accepted]');
             //sleep(1);
             break;
         } else {
             if ($accept == 'break') {
                 break;
             }
         }
     }
     $created = false;
     if (!$matched) {
         if ($entity->getPrimaryExtension() == 'Person') {
             $this->printDebug('  New person: ' . $entity->name_first . ' ' . $entity->name_last);
         } else {
             $this->printDebug('  New org: ' . $entity->name);
         }
         $accept = $this->readline('    create this new entity? (y or n) ');
         $attempts = 1;
         while ($accept != 'y' && $accept != 'n' && $attempts < 5) {
             $accept = $this->readline('    create this new entity? (y or n) ');
             $attempts++;
         }
         if ($accept == 'y') {
             if ($entity->getPrimaryExtension() == 'Person') {
                 $this->printDebug("\n  Bio: {$bio} \n");
                 $accept = $this->readline('    Add this bio? (y or n) ');
                 $attempts = 1;
                 while ($accept != 'y' && $accept != 'n' && $attempts < 5) {
                     $accept = $this->readline('    add this bio? (y or n) ');
                     $attempts++;
                 }
                 if ($accept == 'y') {
                     $entity->summary = $bio;
                 }
             }
             $entity->save();
             $entity->addReference($this->url, null, null, $this->url_name);
             $created = true;
             $this->printDebug(' ' . $entity->name . ' saved');
             //sleep(1);
         }
     }
     if (($matched || $created) && $entity->getPrimaryExtension() == 'Person') {
         $accept = $this->readline("Parse above bio for possible relationships? (y or n) ");
         $attempts = 1;
         while ($accept != 'y' && $accept != 'n' && $attempts < 5) {
             $accept = $this->readline("Parse above bio for possible relationships? (y or n) ");
             $attempts++;
         }
         if ($accept == 'y') {
             $names = $entity->parseBio($bio_dirty);
             $this->printDebug(" Orgs that {$entity} has a position at?");
             foreach ($names as $name) {
                 $exists = false;
                 $name = trim($name);
                 $accept = $this->readline(" > {$name} ::  an org? (y or n or b to break) ");
                 $attempts = 1;
                 $accept = strtolower($accept);
                 while ($accept != 'y' && $accept != 'n' && $accept != 'b' && $attempts < 5) {
                     $accept = $this->readline("  {$name} ::  an org? (y or n or b to break) ");
                     $accept = strtolower($accept);
                     $attempts++;
                 }
                 if ($accept == 'b') {
                     break;
                 } else {
                     if ($accept == 'y') {
                         $this->printDebug(' .....looking for names.....');
                         $orgs = EntityTable::getByExtensionAndNameQuery('Org', $name)->limit(10)->execute();
                         $related_org = null;
                         foreach ($orgs as $org) {
                             $q = LsDoctrineQuery::create()->from('Relationship r')->where('entity1_id = ? and entity2_id = ?', array($entity->id, $org->id))->fetchOne();
                             if ($q) {
                                 $this->printDebug('  Position already exists, skipping...');
                                 $exists = true;
                                 break;
                             }
                             $accept = $this->readline("    Create a position relationship between {$entity->name} and {$org->name}? (y or n) ");
                             $attempts = 1;
                             while ($accept != 'y' && $accept != 'n' && $attempts < 5) {
                                 $accept = $this->readline("    Create a position relationship between {$entity->name} and {$org->name}? (y or n) ");
                                 $attempts++;
                             }
                             if ($accept == 'y') {
                                 $related_org = $org;
                                 break;
                             }
                         }
                         if (!$related_org && !$exists) {
                             $accept = $this->readline(" couldn't find org, should this one be created: {$name} (y or n) ");
                             while ($accept != 'y' && $accept != 'n' && $attempts < 5) {
                                 $accept = $this->readline(" couldn't find org, should this one be created: {$name} (y or n) ");
                                 $attempts++;
                             }
                             if ($accept == 'y') {
                                 $related_org = new Entity();
                                 $related_org->addExtension('Org');
                                 $related_org->name = preg_replace('/\\.(?!com)/i', '', $name);
                                 $extensions = $this->readline("  what extensions should this org get? (eg 'Business, LobbyingFirm, LawFirm') ");
                                 $extensions = preg_split('/\\,\\s*/isu', $extensions, -1, PREG_SPLIT_NO_EMPTY);
                                 try {
                                     foreach ($extensions as $extension) {
                                         $related_org->addExtension($extension);
                                     }
                                     $related_org->save();
                                     $related_org->addReference($this->url, null, null, $this->url_name);
                                 } catch (Exception $e) {
                                     $this->printDebug('   !!! problems with org creation, skipping');
                                     $related_org = null;
                                 }
                             }
                         }
                         if ($related_org) {
                             $q = LsDoctrineQuery::create()->from('Relationship r')->where('r.entity1_id = ? and r.entity2_id = ? and r.category_id = ?', array($entity->id, $related_org->id, 1))->fetchOne();
                             if ($q) {
                                 $this->printDebug('   (relationship already found, skipping...)');
                                 continue;
                             }
                             $relationship = new Relationship();
                             $relationship->Entity1 = $entity;
                             $relationship->Entity2 = $related_org;
                             $relationship->setCategory('Position');
                             $title = $this->readline("     Title for this position relationship? (<enter> to skip) ");
                             if (strlen($title) > 2) {
                                 $relationship->description1 = $title;
                             }
                             $current = strtolower($this->readline("      Is the relationship current? (y or n or <enter> to skip) "));
                             if (in_array($current, array('y', 'yes'))) {
                                 $relationship->is_current = 1;
                             } else {
                                 if (in_array($current, array('n', 'no'))) {
                                     $relationship->is_current = 0;
                                 }
                             }
                             $board = strtolower($this->readline("      Is the relationship a board position? (y or n or <enter> to skip) "));
                             if (in_array($board, array('y', 'yes'))) {
                                 $relationship->is_board = 1;
                             } else {
                                 if (in_array($board, array('n', 'no'))) {
                                     $relationship->is_board = 0;
                                 }
                             }
                             $relationship->save();
                             $relationship->addReference($this->url, null, null, $this->url_name);
                             $this->printDebug("     Relationship saved: {$relationship}");
                         }
                     }
                 }
             }
         }
     }
     if ($matched || $created) {
         if ($this->list) {
             $q = LsDoctrineQuery::create()->from('LsListEntity l')->where('l.entity_id = ? and l.list_id = ?', array($entity->id, $this->list->id))->fetchOne();
             if (!$q) {
                 $le = new LsListEntity();
                 $le->Entity = $entity;
                 $le->LsList = $this->list;
                 if (isset($match['rank'])) {
                     if (preg_match('/(\\d+)/isu', $match['rank'], $m)) {
                         $le->rank = $m[1];
                     }
                 }
                 $le->save();
                 $this->printDebug('List membership saved');
             }
         }
         if ($this->org) {
             $q = LsDoctrineQuery::create()->from('Relationship r')->where('r.entity1_id = ? and r.entity2_id = ? and r.category_id = ?', array($entity->id, $this->org->id, 1))->fetchOne();
             if ($q) {
                 $this->printDebug('   (relationship already found, skipping...)');
                 return;
             }
             $relationship = new Relationship();
             $relationship->Entity1 = $entity;
             $relationship->Entity2 = $this->org;
             $relationship->setCategory($this->relationship_category);
             if ($this->description1) {
                 $relationship->description1 = $this->description1;
             } else {
                 $description = $this->readline("       what description to give this relationship ({$relationship}) ? (less than 3 chars will skip)");
                 if (strlen($description) > 2) {
                     $relationship->description1 = $description;
                 }
             }
             if ($this->relationship_category == 'Position') {
                 $relationship->is_board = $this->is_board;
             } else {
                 if ($this->relationship_category == 'Donation') {
                     if ($this->amount) {
                         $relationship->amount = $this->amount;
                     } else {
                         $amount = $this->readline("  what amount ({$relationship}) ? (less than 3 chars will skip)");
                         if (strlen($amount) > 1) {
                             $relationship->amount = $amount;
                         }
                     }
                 }
             }
             $relationship->save();
             $relationship->addReference($this->url, null, null, $this->url_name);
             $this->printDebug(" Relationship saved: {$relationship}");
         }
     }
     //dump history
     if (isset($match['affiliation1'])) {
         $affiliation = $match['affiliation'];
         //$this->printDebug($affiliation);
     }
 }
Exemplo n.º 10
0
 public function processRow($row)
 {
     if (isset($row['url']) && $row['url'] != '' && isset($row['url_name']) && $row['url_name'] != '') {
         $url = $row['url'];
         $url_name = $row['url_name'];
     } else {
         $url = $this->url;
         $url_name = $this->url_name;
     }
     foreach ($row as &$r) {
         trim($r);
     }
     unset($r);
     if ($this->entity) {
         $required = array('entity_name', 'primary_type', 'relationship_category');
     } else {
         $required = array('entity_name', 'primary_type');
     }
     foreach ($required as $req) {
         if (!isset($row[$req]) || $row[$req] == '') {
             $this->printDebug('!!! > skipping row, ' . $req . ' not set');
             return;
         }
     }
     if ($row['primary_type'] != 'Person' && $row['primary_type'] != 'Org') {
         $this->printDebug('!!! > primary type not properly set, skipping row...');
         return;
     }
     if ($this->entity) {
         $relationship_category = trim($row['relationship_category']);
         $relationship_category_id = array_search($relationship_category, RelationshipCategoryTable::$categoryNames);
         if (!$relationship_category_id) {
             $this->printDebug('!!! > relationship type not properly set, skipping row...');
             return;
         }
     }
     $this->printDebug("processing: " . $row['entity_name'] . '......');
     if ($row['primary_type'] == 'Person') {
         $entity2 = PersonTable::parseFlatName($row['entity_name']);
         $similar_entities = PersonTable::getSimilarQuery2($entity2)->execute();
     } else {
         $entity2 = new Entity();
         $entity2->addExtension('Org');
         $entity2->setEntityField('name', $row['entity_name']);
         $similar_entities = OrgTable::getOrgsWithSimilarNames($entity2->name);
     }
     $matched = false;
     foreach ($similar_entities as $similar_entity) {
         if ($similar_entity['primary_ext'] == 'Person') {
             $this->printDebug('  POSSIBLE MATCH: ' . $similar_entity->name . ' (Orgs :: ' . $similar_entity->getRelatedOrgsSummary() . "  Bio :: {$similar_entity->summary})");
         } else {
             $this->printDebug('  POSSIBLE MATCH: ' . $similar_entity->name . ' (Summary :: ' . $similar_entity->summary . ')');
         }
         $accept = $this->readline('  Is this the same entity? (y or n or b to break)');
         if ($accept == 'y') {
             $entity2 = $similar_entity;
             $matched = true;
             $this->printDebug('             [accepted]');
             break;
         } else {
             if ($accept == 'b') {
                 break;
             }
         }
     }
     $created = false;
     if (!$matched) {
         if ($entity2->getPrimaryExtension() == 'Person') {
             $this->printDebug('  New person: ' . $entity2->name_first . ' ' . $entity2->name_last);
         } else {
             $this->printDebug('  New org: ' . $entity2->name);
         }
         $accept = $this->readline('    create this new entity? (y or n) ');
         if ($accept == 'y') {
             try {
                 $extensions = LsString::split($row['entity_extensions'], '\\s*\\,\\s*');
                 foreach ($extensions as $extension) {
                     $entity2->addExtension($extension);
                 }
                 $entity2->save();
                 $entity2->addReference($url, null, null, $url_name);
             } catch (Exception $e) {
                 $this->printDebug('   !!! problems with extensions for this row');
             }
             $fields = array('summary', 'blurb', 'website');
             foreach ($fields as $field) {
                 if (isset($row[$field])) {
                     $entity2[$field] = $row[$field];
                 }
             }
             $entity2->save();
             $entity2->addReference($url, null, null, $url_name);
             $created = true;
             $this->printDebug(' ' . $entity2->name . ' saved');
             //sleep(1);
         } else {
             $entity2 = null;
         }
     }
     // create relationship
     if ($entity2) {
         if ($this->entity) {
             $relationship = new Relationship();
             if (isset($row['relationship_order']) && $row['relationship_order'] != '') {
                 if ($row['relationship_order'] == '1') {
                     $relationship->Entity1 = $this->entity;
                     $relationship->Entity2 = $entity2;
                 } else {
                     $relationship->Entity2 = $this->entity;
                     $relationship->Entity1 = $entity2;
                 }
             } else {
                 if ($relationship_category == 'Position' || $relationship_category == 'Education') {
                     if ($row['primary_type'] == 'Org') {
                         $relationship->Entity1 = $this->entity;
                         $relationship->Entity2 = $entity2;
                     } else {
                         $relationship->Entity1 = $entity2;
                         $relationship->Entity2 = $this->entity;
                     }
                 } else {
                     $relationship->Entity1 = $this->entity;
                     $relationship->Entity2 = $entity2;
                 }
             }
             $relationship->setCategory($relationship_category);
             $cols = array('description1', 'description2', 'start_date', 'end_date', 'goods', 'amount', 'is_board', 'is_executive', 'is_employee');
             foreach ($cols as $col) {
                 if (isset($row[$col]) && $row[$col] != '') {
                     try {
                         $relationship[$col] = $row[$col];
                     } catch (Exception $e) {
                         $this->printDebug("   could not set {$col} for relationship, skipping");
                     }
                 }
             }
             $q = LsDoctrineQuery::create()->from('Relationship r')->where('r.entity1_id = ? and r.entity2_id = ? and r.category_id = ? and r.id <> ?', array($relationship->entity1_id, $relationship->entity2_id, $relationship->category_id, $relationship->id))->fetchOne();
             if ($q) {
                 $this->printDebug('   (relationship already found, skipping...)');
                 return;
             }
             $relationship->save();
             $relationship->addReference($url, null, null, $url_name);
             $this->printDebug(" Relationship saved: {$relationship}\n");
         } else {
             if ($this->list) {
                 $q = LsDoctrineQuery::create()->from('LsListEntity le')->where('le.entity_id = ? and le.list_id = ?', array($entity2->id, $this->list->id))->fetchOne();
                 if ($q) {
                     $this->printDebug('   (already on list, skipping...)');
                     return;
                 }
                 $le = new LsListEntity();
                 $le->LsList = $this->list;
                 $le->Entity = $entity2;
                 var_dump($row);
                 if (isset($row['rank'])) {
                     echo $row['rank'];
                     $le->rank = $row['rank'];
                 }
                 $le->save();
             }
         }
     }
 }
Exemplo n.º 11
0
 protected function saveToList($company, $rank)
 {
     $list = Doctrine_Query::create()->from('LsListEntity L')->where('L.list_id = ? AND L.entity_id = ?', array($this->list->id, $company->id))->fetchOne();
     if ($list) {
         $this->printDebug($this->list->name . " (already saved)");
     } else {
         if ($company->id && $this->list->id) {
             $listentity = new LsListEntity();
             $listentity->entity_id = $company->id;
             $listentity->list_id = $this->list->id;
             $listentity->rank = $rank;
             $listentity->save();
             $this->printDebug($this->list->name . " (saved)");
         } else {
             if ($this->list->id) {
                 $this->printDebug("List ID not set");
             } else {
                 $this->printDebug("Company ID not set");
             }
         }
     }
 }
Exemplo n.º 12
0
 public function executeToolbarCreate($request)
 {
     $this->checkToolbarCredentials();
     if (!$request->isMethod('post')) {
         $this->forward404();
     }
     $name = $request->getParameter('name');
     $ext = $request->getParameter('ext');
     $blurb = $request->getParameter('blurb');
     $position = $request->getParameter('position');
     $listId = $request->getParameter("list_id");
     //save list_id to session for further use
     //$this->getUser()->setAttribute('list' . $position . '_id', $listId);
     if (!$name || !$ext) {
         $this->forward404();
     }
     if ($ext == 'Person') {
         $entity = PersonTable::parseFlatName($name);
         $entity->name = $name;
         if (!$entity->name_last) {
             $this->forward404();
         }
     } else {
         $entity = new Entity();
         $entity->addExtension('Org');
         $entity->name = $name;
     }
     $entity->blurb = $blurb;
     $entity->save();
     if ($listId) {
         $db = Doctrine_Manager::connection();
         $sql = "SELECT COUNT(*) FROM ls_list WHERE id = ? AND is_network = 0 AND is_deleted = 0";
         if (!$this->getUser()->hasCredential('admin')) {
             $sql .= " AND is_admin = 0";
         }
         $stmt = $db->execute($sql, array($listId));
         $count = $stmt->fetch(PDO::FETCH_COLUMN);
         if ($count == "1") {
             $sql = "SELECT COUNT(*) FROM ls_list_entity WHERE list_id = ? AND entity_id = ? AND is_deleted = 0";
             $stmt = $db->execute($sql, array($listId, $entity->id));
             $count = $stmt->fetch(PDO::FETCH_COLUMN);
             if ($count == "0") {
                 $le = new LsListEntity();
                 $le->list_id = $listId;
                 $le->entity_id = $entity->id;
                 $le->save();
             }
         } else {
             //save list_id to session for further use
             //$this->getUser()->setAttribute('list' . $position . '_id', null);
         }
     }
     $this->entity = $entity;
     $this->getResponse()->setHttpHeader('Content-Type', 'application/json; charset=utf-8');
 }
Exemplo n.º 13
0
 public function executeAddList($request)
 {
     $this->checkEntity($request, false, false);
     if ($request->hasParameter('add_list_terms')) {
         $terms = explode(' ', $request->getParameter('add_list_terms'));
         $page = $request->getParameter('page', 1);
         $num = $request->getParameter('num', 10);
         $q = LsDoctrineQuery::create()->from('LsList l')->where('l.is_network = 0 AND NOT EXISTS(SELECT le.id FROM ls_list_entity le WHERE le.list_id = l.id AND le.entity_id = ? AND le.is_deleted = 0)', $this->entity->id);
         foreach ($terms as $term) {
             $q->addWhere('l.name LIKE \'%' . $term . '%\' OR l.description LIKE \'%' . $term . '%\'');
         }
         if (!$this->getUser()->hasCredential('admin')) {
             $q->addWhere('l.is_admin = ?', 0);
         }
         $this->results_pager = new Doctrine_Pager($q, $page, $num);
     }
     if ($request->isMethod('post')) {
         $list = Doctrine::getTable('LsList')->find($request->getParameter('list_id'));
         $this->forward404Unless($list);
         //check that entity isn't already on this list
         $q = LsDoctrineQuery::create()->from('LsListEntity le')->where('le.list_id = ? AND le.entity_id = ? AND le.is_deleted IS NOT NULL', array($list->id, $this->entity->id));
         if ($listEntity = $q->fetchOne()) {
             if ($listEntity->is_deleted) {
                 $listEntity->is_deleted = 0;
                 $listEntity->save();
             }
         } else {
             $listEntity = new LsListEntity();
             $listEntity->list_id = $list->id;
             $listEntity->entity_id = $this->entity->id;
             $listEntity->save();
         }
         $this->clearCache($this->entity);
         LsCache::clearListCacheById($list->id);
         $this->redirect($this->entity->getInternalUrl());
     }
     //also show popular lists
     $q = LsDoctrineQuery::create()->select('l.*, COUNT(le.id) num')->from('LsList l')->where('l.is_network = 0')->leftJoin('l.LsListEntity le')->groupBy('l.id')->orderBy('num DESC')->limit(10);
     if (!$this->getUser()->hasCredential('admin')) {
         $q->addWhere('l.is_admin = ?', 0);
     }
     $this->popular_lists = $q->execute();
 }