protected function attachImage(Entity $entity, $url, $title = '', $caption = '', $is_featured = 1, $is_free = 0) { if (preg_match('/^https/', $url)) { $this->printDebug("HTTPS protocol"); return false; } try { $filename = ImageTable::createFiles($url, basename($url)); } catch (Exception $e) { $this->printDebug($e); return false; } if (!$filename) { $this->printDebug("File could not be created"); return false; } $this->printDebug("Creating file: " . $filename); //insert image row $image = new Image(); $image->Entity = $entity; $image->filename = $filename; $image->url = $url; $image->title = $title; $image->caption = $caption; $image->is_featured = $is_featured; $image->is_free = $is_free; $image->save(); $image->addReference($url, null, array('filename')); $this->printDebug("Imported image with ID: " . $image->getId()); return true; }
private function getLobbyistInfo($org) { $people = $org->getRelatedEntitiesQuery('Person', RelationshipTable::POSITION_CATEGORY, null, null, null, false)->addWhere('summary is NULL or summary = ?', '')->execute(); $google_scraper = new LsGoogle(); $ct = 0; foreach ($people as $person) { if ($ct > 30) { return null; } $this->printDebug("\n******************\n"); $bio = null; $image = null; $query = 'site:' . $org->website . ' ' . $person->name; $this->printDebug('Query: ' . $query); $google_scraper->setQuery(trim($query)); $google_scraper->execute(); if ($google_scraper->getNumResults()) { $results = $google_scraper->getResults(); $match_sets = array(); $this->stopTimer(); $last = $this->timer->getElapsedTime(); $this->beginTimer(); foreach ($results as $result) { $this->stopTimer(); $now = $this->timer->getElapsedTime(); $diff = $now - $last; $this->printDebug($diff); $last = $now; $this->beginTimer(); if ($diff > 30) { try { $this->db->beginTransaction(); $this->saveMeta($org->id, 'timeout', 1); $this->printDebug('TIMEOUT======================================='); if (!$this->testMode) { $this->db->commit(); } else { $this->db->rollback(); } } catch (Exception $e) { $this->db->rollback(); } return null; } if (0) { $url = $result->cacheUrl; } else { $url = $result->unescapedUrl; } $this->printDebug($url); if (preg_match('/\\.pdf$/is', $url)) { $this->printDebug("PDF, skipping ({$url})\n----------------"); continue; } try { $error = $this->browser->get($url)->responseIsError(); } catch (Exception $e) { continue; } if (!$error) { $this->printDebug('checking: ' . $url); $page = $this->browser->getResponseText(); $page = LsHtml::replaceEntities($page); if (!$bio) { if ($bio = $this->findPersonBio($page, $person, $org)) { try { $this->db->beginTransaction(); $person->summary = $bio; $person->save(); $person->addReference($url, null, array('summary'), $org->name . ' website'); $this->printDebug("\nBIO FOUND & SAVED: " . $bio . "\n"); $ct = 0; if (!$this->testMode) { $this->db->commit(); } else { $this->db->rollback(); } } catch (Exception $e) { $this->db->rollback(); throw $e; } } else { $this->printDebug('no bio'); } } if (!$image) { if ($image = $this->findPersonImage($page, $person, $org)) { $this->printDebug('IMAGE FOUND: ' . $image['url']); preg_match('/(http\\:\\/\\/[^\\/]+)\\//is', $url, $match); $root_url = $match[1]; $image_url = null; if (preg_match('/^http/is', $image['url'])) { $image_url = $image['url']; } else { $pos = strrpos($url, '/'); if ($pos > 8) { $trimmed_url = substr($url, 0, $pos); } else { $trimmed_url = $url; } if (preg_match('/^\\//is', $image['url'])) { $image_url = $root_url . $image['url']; } else { if (preg_match('/^((\\.\\.\\/)+)(.+)/is', $image['url'], $match)) { $num_steps = strlen($match[1]) / 3; for ($i = 0; $i < $num_steps; $i++) { $trimmed_url = substr($trimmed_url, 0, strrpos($trimmed_url, '/')); } $image_url = $trimmed_url . '/' . $match[3]; } else { $image_url = $trimmed_url . '/' . $image['url']; } } } if ($image_url) { $this->printDebug($image_url); if ($fileName = ImageTable::createFiles($image_url, $person->name)) { //insert image record try { $this->db->beginTransaction(); $image = new Image(); $image->filename = $fileName; $image->entity_id = $person->id; $image->title = $person->name; $image->caption = 'From ' . $org->name . '\'s website.'; $image->is_featured = true; $image->is_free = false; $image->url = $image_url; $q = LsDoctrineQuery::create()->from('Image i')->where('i.entity_id = ?', $person->id)->addWhere('i.title =?', $person->name)->addWhere('i.caption =?', $image->caption); if (count($q->execute()) == 0) { $image->save(); $image->addReference($image_url, null, array('filename'), $org->name . ' website'); if (!$bio) { $person->addReference($url, null, null, $org->name . ' website'); } $this->printDebug("Imported image: " . $image->filename); $ct = 0; } if (!$this->testMode) { $this->db->commit(); } else { $this->db->rollback(); } } catch (Exception $e) { $this->db->rollback(); throw $e; } } } } else { $this->printDebug('no image'); } } if ($bio && $image) { break; } } else { $this->printDebug('response is error: ' . $url); } $this->printDebug('-------------'); } if (count($match_sets)) { //var_dump($match_sets); } } else { $this->printDebug("No results found \n"); } if (!$image && !$bio) { $ct++; } } }
protected function importGovernor($row) { $url = $this->_baseUrl . $row['url']; if (!$this->browser->get($url)->responseIsError()) { $text = $this->browser->getResponseText(); $text = LsHtml::replaceEntities($text); //preg_match('/>Family\:<\/b>([^<]*)<br/is',$text,$family_arr); $name = trim(str_ireplace('Gov.', '', $row['name'])); $this->printDebug(''); $this->printDebug($name . ':'); $governor = PersonTable::parseFlatName($name); $governor->addExtension('PoliticalCandidate'); $governor->addExtension('ElectedRepresentative'); $governor->is_state = 1; $similar = $governor->getSimilarEntitiesQuery(true)->execute(); foreach ($similar as $s) { $sim_re = LsString::escapeStringForRegex($s->name_first); $search_re = LsString::escapeStringForRegex($governor->name_first); if (preg_match('/^' . $sim_re . '/su', $governor->name_first) == 0 && preg_match('/^' . $search_re . '/su', $s->name_first) == 0) { continue; } $bio = $s->getExtendedBio(); if (preg_match('/\\bgovernor(ship)?\\b/isu', $bio)) { $governor = $s; $this->printDebug(' Found existing governor: ' . $s->name . ' ' . $s->id); break; } } $governor->save(); $this->printDebug($governor->id); if (!$governor->start_date && preg_match('/>Born\\:<\\/b>([^<]*)<br/is', $text, $birth_arr)) { $this->printDebug(' Birthdate: ' . $birth_arr[1]); $governor->start_date = trim($birth_arr[1]); } if (!$governor->birthplace && preg_match('/>Birth State\\:<\\/b>([^<]*)<br/is', $text, $birth_state_arr)) { $this->printDebug(' Birthplace: ' . trim($birth_state_arr[1])); $governor->birthplace = trim($birth_state_arr[1]); } //PARTY MEMBERSHIP if (preg_match('/>Party\\:<\\/b>([^<]*)<br/is', $text, $party_arr)) { $party_str = $party_arr[1]; $this->printDebug(' Party: ' . $party_str); if (stristr($party_str, 'Democrat')) { $party = EntityTable::getByExtensionQuery('PoliticalParty')->addWhere('name = ?', 'Democratic Party')->fetchOne(); } if (stristr($party_str, 'Republican')) { $party = EntityTable::getByExtensionQuery('PoliticalParty')->addWhere('name = ?', 'Republican Party')->fetchOne(); } if (isset($party) && $party && !$governor->party_id) { $governor->Party = $party; $governor->is_independent = false; $this->printDebug(' Added membership in ' . $party); } else { if (stristr($party_str, 'Independent')) { $governor->is_independent = true; } } } if (!$governor->summary && preg_match_all('/>([^<]{240,})/isu', $text, $bio_match)) { $str = ''; foreach ($bio_match[1] as $b) { if (!stristr($b, 'Javascript')) { $str .= "\n\n" . $b; } } $str = trim($str); if (strlen($str)) { $governor->summary = $str; } } $governor->save(); $governor->addReference($url, null, $governor->getAllModifiedFields(), 'Governors Association'); //SCHOOLS if (preg_match('/>School\\(s\\)\\:<\\/b>([^<]*)<br/is', $text, $school_arr)) { $school_names = explode(';', trim($school_arr[1])); if (count($school_names) == 1) { $school_names = explode(',', $school_names[0]); } foreach ($school_names as $school_name) { $school_name = trim($school_name); if (!($school = EntityTable::getByExtensionQuery('School')->leftJoin('e.Alias a')->addWhere('e.name = ? or a.name = ?', array($school_name, $school_name))->fetchOne())) { $school = new Entity(); $school->addExtension('Org'); $school->addExtension('School'); $school->name = $school_name; $school->save(); $this->printDebug(' Added School: ' . $school_name); } $q = RelationshipTable::getByCategoryQuery('Education')->addWhere('entity1_id = ? and entity2_id = ?', array($governor->id, $school->id))->fetchOne(); if (!$q) { $relationship = new Relationship(); $relationship->setCategory('Education'); $relationship->Entity1 = $governor; $relationship->Entity2 = $school; $relationship->is_current = 0; $relationship->save(); $relationship->addReference($url, null, $relationship->getAllModifiedFields(), 'Governors Association'); $this->printDebug(' Added education: ' . $relationship->name); } } } //GOVERNOR OFFICE AND POSITION $office_name = 'Office of the Governor of ' . $row['state']; if (!($office = EntityTable::getByExtensionQuery('GovernmentBody')->addWhere('name = ?', $office_name)->fetchOne())) { $office = new Entity(); $office->name = $office_name; $office->addExtension('Org'); $office->addExtension('GovernmentBody'); $state = Doctrine::getTable('AddressState')->findOneByName($row['state']); if ($state) { $office->state_id = $state->id; } $office->save(); $office->addReference($url, null, $office->getAllModifiedFields(), 'Governors Association'); $this->printDebug(' Added office: ' . $office->name); } $q = RelationshipTable::getByCategoryQuery('Position')->addWhere('entity1_id = ? and entity2_id = ? and description1 = ?', array($governor->id, $office->id, 'Governor'))->fetchOne(); if (!$q) { sort($row['years']); $i = 0; while ($i < count($row['years'])) { $governorship = new Relationship(); $governorship->setCategory('Position'); $governorship->Entity1 = $governor; $governorship->Entity2 = $office; $governorship->description1 = 'Governor'; $governorship->start_date = $row['years'][$i]; $i++; if (isset($row['years'][$i])) { $governorship->end_date = $row['years'][$i]; $governorship->is_current = 0; if (!$governor->blurb && !isset($row['years'][$i + 1])) { $governor->blurb = 'Former Governor of ' . $row['state']; } } else { $governorship->is_current = 1; if (!$governor->blurb) { $governor->blurb = 'Governor of ' . $row['state']; } } $governor->save(); $i++; $governorship->save(); $governorship->addReference($url, null, $governorship->getAllModifiedFields(), 'Governors Association'); $this->printDebug(' Added governorship: ' . $governorship->name); } } //SPOUSE if (preg_match('/>Spouse\\:<\\/b>(.*?)<br/is', $text, $spouse_arr)) { $spouse = trim(LsHtml::stripTags($spouse_arr[1])); $q = RelationshipTable::getByCategoryQuery('Family')->addWhere('entity1_id = ? or entity2_id = ?', array($governor->id, $governor->id))->fetchOne(); if (!$q && strlen($spouse)) { $spouse = PersonTable::parseFlatName($spouse); $spouse->save(); $this->printDebug(' Added spouse: ' . $spouse->name); $relationship = new Relationship(); $relationship->setCategory('Family'); $relationship->Entity1 = $spouse; $relationship->Entity2 = $governor; $relationship->description1 = 'Spouse'; $relationship->description2 = 'Spouse'; $relationship->save(); $relationship->addReference($url, null, $relationship->getAllModifiedFields(), 'Governors Association'); $this->printDebug(' Added spouse relationship: ' . $relationship->name); } } //ADDRESS --not working, malformed addresses /* if (preg_match('/>Address\:\s*<\/b>(.*?)<b>/is',$text,$address_arr)) { $address = trim(str_replace('<br/>',', ',$address_arr[1])); $this->printDebug($address); if ($governor->Address->count() == 0 && $a = $governor->addAddress($address)) { $this->printDebug(' Address: ' . $a); $governor->save(); } }*/ //PHONE NUMBER if (preg_match('/>Phone\\(s\\)\\:<\\/b>([^<]*)<br/is', $text, $phone_arr)) { $phone_number = trim($phone_arr[1]); if (!$governor->Phone->count()) { $phone = $governor->addPhone($phone_number); $this->printDebug(' Phone: ' . $phone); } } if (!$governor->Image->count() && preg_match('/<img .*?class\\="display" src\\="([^"]*)"/is', $text, $img_arr)) { $url = $img_arr[1]; try { $fileName = ImageTable::createFiles($url, $governor->name_first); } catch (Exception $e) { $fileName = null; } if ($fileName) { //insert image record $image = new Image(); $image->filename = $fileName; $image->entity_id = $governor->id; $image->title = $governor->name; $image->caption = 'From Governors Association website'; $image->is_featured = true; $image->is_free = false; $image->url = $url; $image->save(); $this->printDebug("Imported image: " . $image->filename); } } } }
public function execute() { if (!$this->safeToRun('uk-mp-candidates')) { $this->printDebug('Script already running'); die; } // Get (or create) the UK local Network $uk = Doctrine::getTable('LsList')->findOneByName('United Kingdom'); if (!$uk) { $uk = new LsList(); $uk->name = 'United Kingdom'; $uk->is_network = 1; $uk->description = 'People and organizations with significant influence on the policies of the United Kingdom'; $uk->display_name = 'uk'; $uk->save(); } // Get the MP list $raw = $this->getMPs(); // Add new MPs to the list foreach ($raw as $mp) { $this->printDebug(sprintf('Processing %s', $mp['name'])); // Split name $entity = PersonTable::parseFlatName($mp['name']); $entity->blurb = 'Prospective Parliamentary Candidate for ' . $mp['constituency']; $q = TagTable::getByTripleQuery('yournextmp', 'url', $mp['url']); $r = $q->count(); if ($r) { $this->printDebug('Already processed, skipping.'); continue; } // Get political party $q = EntityTable::getByExtensionQuery('PoliticalParty')->addWhere('e.name = ?', $mp['party']); if (!($partyEntity = $q->fetchOne())) { $partyEntity = new Entity(); $partyEntity->addExtension('Org'); $partyEntity->addExtension('PoliticalParty'); $partyEntity->name = $mp['party']; $partyEntity->blurb = 'UK Political Party'; $partyEntity->save(null, true, array($uk->id)); $this->printDebug("Created new political party: " . $mp['party']); } // Save entity to UK Network $entity->party_id = $partyEntity->id; $entity->save(null, true, array($uk->id)); // Add party relationship $r = new Relationship(); $r->entity1_id = $entity->id; $r->entity2_id = $partyEntity->id; $r->setCategory('Membership'); $r->description1 = 'Prospective parliamentary candidate'; $r->is_current = true; // $r->start_date = // Don't know where we can get this, and "now" seems kind of wrong $r->save(); // Add YourNextMP triple $entity->addTagByTriple('yournextmp', 'url', $mp['url']); // Add references $ref = new Reference(); $ref->addFields(array('name_first', 'name_last', 'name_middle')); // Don't need this $ref->source = $mp['url']; $ref->name = 'YourNextMP.com - ' . $entity['name']; $ref->object_model = 'Entity'; $ref->object_id = $entity->getId(); $ref->save(); unset($ref); $ref = new Reference(); $ref->addFields(array('name')); $ref->source = $mp['party_url']; $ref->name = 'YourNextMP.com - ' . $partyEntity['name']; $ref->object_model = 'Entity'; $ref->object_id = $partyEntity->getId(); $ref->save(); unset($ref); $ref = new Reference(); $ref->addFields(array('name')); $ref->source = $mp['url']; $ref->name = 'YourNextMP.com - ' . $entity['name']; $ref->object_model = 'Relationship'; $ref->object_id = $r->getId(); $ref->save(); unset($ref); $r->free(true); unset($r); // Add image? if ($mp['image']) { if ($fileName = ImageTable::createFiles($mp['image'])) { //insert image record $image = new Image(); $image->filename = $fileName; $image->title = $entity['name']; $image->caption = 'From YourNextMP under CC-BY-SA license.'; $image->is_featured = true; $image->is_free = true; $image->url = $mp['image']; $this->printDebug("Imported image: " . $image->filename); } $image->Entity = $entity; $image->save(); if ($mp['image']) { //save image source $image->addReference($mp['image']); $this->printDebug("Saved image reference"); } unset($image); } // Add party image? if ($mp['party_image']) { if ($fileName = ImageTable::createFiles($mp['party_image'])) { //insert image record $partyImage = new Image(); $partyImage->filename = $fileName; $partyImage->title = $partyEntity['name']; $partyImage->caption = 'From YourNextMP under CC-BY-SA license.'; $partyImage->is_featured = true; $partyImage->is_free = true; $partyImage->url = $mp['party_image']; $this->printDebug("Imported image: " . $partyImage->filename); } $partyImage->Entity = $partyEntity; $partyImage->save(); if ($mp['party_image']) { //save image source $partyImage->addReference($mp['party_image']); $this->printDebug("Saved image reference"); } unset($partyImage); } unset($entity); unset($partyEntity); } }
public function getProfileData($member) { //generate URL for member's profile $url = $this->_profileUrlBase . $member->bioguide_id; $this->_references['bioguide']->source = $url; $this->_references['bioguide']->name = 'Congressional Biographical Directory'; if (!$this->browser->get($url)->responseIsError()) { $this->printDebug("Fetched member's profile page"); $this->_bioPageText = $text = LsString::newlinesToSpaces($this->browser->getResponseText()); //get bio if (preg_match('/, <\\/FONT>([^<]+)<\\/(TD|P)>/', $text, $bio)) { $bio = preg_replace('/\\n/', ' ', $bio[1]); $bio = ucfirst(trim(preg_replace('/\\s{2,}/', ' ', $bio))); $bio = LsHtml::replaceEntities($bio); $member->summary = $bio; $this->printDebug("Bio: " . $bio); if (preg_match('/\\b(a(\\s+\\p{L}+){2,8})\\;/isu', $bio, $match)) { $blurb = 'US ' . preg_replace('/a\\s+/isu', '', $match[1]); $member->blurb = $blurb; $this->printDebug("Blurb: " . $blurb); } } //get senate term, if any if (preg_match('/Service:<\\/B><\\/FONT>([^<]+)<BR>/', $text, $term)) { $terms = preg_split('/,;/', $term[1]); foreach ($terms as $term) { if (!($term = trim($term))) { continue; } //create relationship $rel = new Relationship(); $rel->Entity1 = $member; $rel->entity2_id = $this->_senateEntityId; $rel->setCategory('Membership'); $rel->description1 = 'Senator'; //break term into start and end $years = explode('-', $term); $start = trim($years[0]); $rel->start_date = $start . '-00-00'; $this->printDebug("Senate term start: " . $start); if (count($years) > 1 && trim($years[1])) { $end = trim($years[1]); $rel->end_date = $end . '-00-00'; $this->printDebug("Senate term end: " . $end); } $this->_senateRelationships[] = $rel; $this->printDebug("Created relationship to US Senate"); } } //get house terms preg_match_all('/\\((\\w+\\s+\\d{1,2},\\s+\\d{4})-(present|(\\w+\\s+\\d{1,2},\\s+\\d{4}))\\)/ismU', $text, $matches, PREG_SET_ORDER); foreach ($matches as $match) { if ($time = strtotime($match[1])) { //create relationship $rel = new Relationship(); $rel->Entity1 = $member; $rel->entity2_id = $this->_houseEntityId; $rel->setCategory('Membership'); $rel->start_date = date('Y-m-d', $time); $rel->description1 = 'Representative'; $this->printDebug("Created relationship to US House of Reps"); $this->printDebug("House term start: " . $rel->start_date); if ($match[2] != 'present' && ($time = strtotime($match[2]))) { $rel->end_date = date('Y-m-d', $time); $this->printDebug("House term end: " . $rel->end_date); } $this->_houseRelationships[] = $rel; } } //get photo url & name if (preg_match('/bioguide\\/photo\\/[A-Z]\\/([^"]+)/', $text, $photo)) { if ($photoUrl = $photo[0]) { $this->_photoUrl = 'http://bioguide.congress.gov/' . $photoUrl; $photoName = $photo[1]; $this->printDebug("Photo URL: " . $this->_photoUrl); //get photo credit if (preg_match('/<I>([^<]+)<\\/photo\\-credit>/', $text, $credit)) { $credit = trim($credit[1]); $this->printDebug("Photo credit: " . $credit); } else { $credit = null; } if ($fileName = ImageTable::createFiles($this->_photoUrl, $photoName)) { //insert image record $image = new Image(); $image->filename = $fileName; $image->title = 'Congress Photo'; $image->caption = $credit ? $credit : 'From the Biographical Directory of the United States Congress'; $image->is_featured = true; $image->is_free = true; $image->url = $this->_photoUrl; //save for later $this->_image = $image; $this->printDebug("Imported image: " . $image->filename); } } } } else { //Error response (eg. 404, 500, etc) throw new Exception("Couldn't get " . $url); } }
private function importDirectorInfo($info, $age_match) { $id = $age_match['name_match']['id']; $person = $age_match['name_match']['person']; $this->printDebug($person->name); $category = Doctrine::getTable('RelationshipCategory')->findOneByName('Position'); $relationship = LsDoctrineQuery::create()->from('Relationship r')->where('r.entity1_id = ?', $id)->addWhere('r.entity2_id = ?', $this->corp->id)->addWhere('r.category_id = ?', $category->id)->addWhere('r.description1 = ?', 'Director')->fetchOne(); if ($info['since']) { $relationship->start_date = $info['since'] . '-00-00'; $relationship->save(); $this->printDebug('Director since: ' . $relationship->start_date); $relationship->addReference($this->url, null, array('start_date'), $this->corp->name . ' ' . $this->year . ' Proxy', 'pg ' . $age_match['name_match']['page_number']); } if ($info['blurb'] != '') { $info['blurb'] = $this->cleanSummary($info['blurb']); if (strlen($info['blurb']) > 3000) { $info['blurb'] = substr($info['blurb'], 0, 3000); } if ($person->summary == null) { $person->summary = $info['blurb']; $person->save(); $person->addReference($this->url, $info['blurb'], array('summary'), $this->corp->name . ' ' . $this->year . ' Proxy', 'pg ' . $age_match['name_match']['page_number']); $this->printDebug('Summary: ' . $person->summary); } else { $person->addReference($this->url, $info['blurb'], array('summary'), $this->corp->name . ' ' . $this->year . ' Proxy', 'pg ' . $age_match['name_match']['page_number']); } } else { $this->printDebug('no blurb'); } if ($person->start_date == null) { $person->start_date = $this->year - $age_match['age'] . '-00-00'; $person->save(); $person->addReference($this->url, $info['blurb'], array('start_date'), $this->corp->name . ' ' . $this->year . ' Proxy', 'pg ' . $age_match['name_match']['page_number']); $this->printDebug('Birthdate: ' . $person->start_date); } if (isset($info['img'])) { $url = substr($this->url, 0, strrpos($this->url, '/') + 1) . $info['img']; if ($fileName = ImageTable::createFiles($url, $info['img'])) { //insert image record $image = new Image(); $image->filename = $fileName; $image->entity_id = $person->id; $image->title = $person->name; $image->caption = 'From ' . $this->corp->name . '\'s proxy filing.'; $image->is_featured = true; $image->is_free = false; $image->url = $url; $q = LsDoctrineQuery::create()->from('Image i')->where('i.entity_id = ?', $person->id)->addWhere('i.title =?', $person->name)->addWhere('i.caption =?', $image->caption); if (count($q->execute()) == 0) { $image->save(); $image->addReference($this->url, null, array('filename'), $this->corp->name . ' ' . $this->year . ' Proxy', 'pg ' . $age_match['name_match']['page_number']); $this->printDebug("Imported image: " . $image->filename); } } } }
public function executeUploadImage($request) { $this->checkUser(); $this->user = $this->getUser(); $this->profile = $this->getUser()->getProfile(); $this->upload_form = new ImageUploadForm(); $params = $request->getParameter('image'); if ($request->isMethod('post')) { $db = Doctrine_Manager::connection(); //$this->upload_form->bind($params, $request->getFiles('image')); try { $db->beginTransaction(); $files = $request->getFiles('image'); //set filename and path based on upload type if (isset($files['file']['size']) && $files['file']['size']) { $path = $request->getFilePath('image'); $path = $path['file']; $originalFilename = $request->getFileName('image'); $originalFilename = $originalFilename['file']; } else { $path = $params['url']; $pathParts = explode('?', basename($path)); $originalFilename = $pathParts[0]; } //if image files can't be created, assume remote url was bad if (!($filename = ImageTable::createFiles($path, $originalFilename))) { $validatorSchema = $this->upload_form->getValidatorSchema(); $this->upload_form->getErrorSchema()->addError(new sfValidatorError($validatorSchema['url'], 'invalid')); return sfView::SUCCESS; } $this->profile->filename = $filename; $this->profile->save(); //create reference //if featured, unfeature any other images if (isset($params['featured']) && ($profileImage = $entity->getProfileImage())) { $profileImage->is_featured = false; $profileImage->save(); } $db->commit(); } catch (Exception $e) { $db->rollback(); throw $e; } $this->clearUserCache(); $this->redirect($request->getParameter('referer', 'home/notes')); } }
protected function attachImage(Entity $entity, $url) { $filename = ImageTable::createFiles(preg_replace('/^https/i', 'http', $url), basename($url)); if ($filename) { $this->printDebug("Filename saved as " . $filename); //insert image row $image = new Image(); $image->Entity = $entity; $image->filename = $filename; $image->url = $url; $image->title = $entity->name; $image->is_featured = 1; $image->is_free = 0; $image->save(); $image->addReference($url, null, array('filename'), 'Forbes.com'); $this->printDebug("Imported image ID: " . $image->getId()); return true; } else { $this->printDebug("File could not be created"); return false; } }
public function executeUploadImage($request) { $this->checkEntity($request); $params = $request->getParameter('image'); $this->upload_form = new ImageUploadForm(); $this->has_image = EntityTable::hasProfileImage($this->entity); if ($request->isMethod('post')) { $db = Doctrine_Manager::connection(); $this->upload_form->bind($params, $request->getFiles('image')); if ($this->upload_form->isValid()) { try { $db->beginTransaction(); $files = $request->getFiles('image'); //set filename and path based on upload type if (isset($files['file']['size']) && $files['file']['size']) { $path = $request->getFilePath('image'); $path = $path['file']; $originalFilename = $request->getFileName('image'); $originalFilename = $originalFilename['file']; } else { $path = $params['url']; $pathParts = explode('?', basename($path)); $originalFilename = $pathParts[0]; } //if image files can't be created, assume remote url was bad if (!($filename = ImageTable::createFiles($path, $originalFilename))) { $validatorSchema = $this->upload_form->getValidatorSchema(); $this->upload_form->getErrorSchema()->addError(new sfValidatorError($validatorSchema['url'], 'invalid')); return sfView::SUCCESS; } //create image $image = new Image(); $image->entity_id = $this->entity['id']; $image->filename = $filename; $image->title = $params['title']; $image->caption = $params['caption']; $image->url = $params['url']; if (!$this->has_image) { $image->is_featured = true; } elseif (isset($params['is_featured'])) { $db = Doctrine_Manager::connection(); $sql = 'UPDATE image SET is_featured = 0 WHERE entity_id = ?'; $stmt = $db->execute($sql, array($this->entity['id'])); $image->is_featured = true; } else { $image->is_featured = 0; } $image->is_free = isset($params['is_free']) ? true : null; $image->save(); //if featured, unfeature any other images if (isset($params['featured']) && ($profileImage = EntityTable::getProfileImageById($entity))) { $profileImage->is_featured = false; $profileImage->save(); } $db->commit(); } catch (Exception $e) { $db->rollback(); throw $e; } $this->clearCache($this->entity); $this->redirect($image->url ? EntityTable::getInternalUrl($this->entity, 'images') : 'entity/image?id=' . $image->id); } } }