public function updateReprStorageProfile(RepresentativeStorage $representative)
 {
     $openStateId = $this->getRepresentativeByName($representative->getFirstName(), $representative->getLastName());
     if ($openStateId) {
         $representative->setOpenstateId($openStateId);
     }
     return $representative;
 }
 public function updateReprStorageProfile(RepresentativeStorage $representative)
 {
     $response = $this->getRepresentativeByName($representative->getFirstName(), $representative->getLastName());
     foreach ($response as $objField => $objValue) {
         $fieldName = ucfirst($objField);
         $representativeSetMethod = 'set' . $fieldName;
         $representativeGetMethod = 'get' . $fieldName;
         if (method_exists($representative, $representativeSetMethod) && method_exists($representative, $representativeGetMethod)) {
             $currentPropertyValue = $representative->{$representativeGetMethod}();
             if (empty($currentPropertyValue)) {
                 $representative->{$representativeSetMethod}($objValue);
             }
         }
     }
     return $representative;
 }
 public function load(ObjectManager $manager)
 {
     $stRepresentative = new RepresentativeStorage();
     $stRepresentative->setStorageId(44926);
     $stRepresentative->setFirstName('Joseph');
     $stRepresentative->setLastName('Biden');
     $stRepresentative->setOfficialTitle('Vice President');
     $stRepresentative->setDistrict($this->getReference('district_us'));
     $stRepresentative->setAvatarSrc('http://google.com/');
     $stRepresentative->setUpdatedAt(new \DateTime('2010-01-01'));
     $this->addReference('vice_president', $stRepresentative);
     $manager->persist($stRepresentative);
     $manager->flush();
 }
 public function load(ObjectManager $manager)
 {
     $representative = new RepresentativeStorage();
     $representative->setStorageId(52990);
     $representative->setFirstName('Joseph');
     $representative->setLastName('Biden');
     $representative->setOfficialTitle('Vice President');
     $representative->setOpenstateId('DC000000');
     $representative->setAvatarSrc('http://google.com');
     $this->addReference('representativeStorage', $representative);
     $manager->persist($representative);
     $manager->flush();
 }
 /**
  * Synchronize $storageRepresentative with Cicero representative.
  * 
  * @param \Civix\CoreBundle\Entity\RepresentativeStorage $storageRepresentative
  * @return boolean
  */
 public function synchronizeRepresentative(RepresentativeStorage $storageRepresentative)
 {
     $ciceroRepresentative = $this->ciceroService->findRepresentativeByNameAndId($storageRepresentative->getFirstName(), $storageRepresentative->getLastName(), $storageRepresentative->getStorageId());
     $civixRepresentative = $storageRepresentative->getRepresentative();
     if ($ciceroRepresentative) {
         //update current data of representative from cicero
         $this->ciceroStorageService->fillStorRepresentativeByApiObj($storageRepresentative, $ciceroRepresentative);
         $this->entityManager->persist($storageRepresentative);
         //update representative in civix
         if ($civixRepresentative instanceof Representative) {
             $civixRepresentative->setOfficialTitle($storageRepresentative->getOfficialTitle());
             $civixRepresentative->setDistrict($storageRepresentative->getDistrict());
             $this->entityManager->persist($civixRepresentative);
             $this->entityManager->flush($civixRepresentative);
         }
         $this->entityManager->flush($storageRepresentative);
     } else {
         //unlink district and storage
         if ($civixRepresentative instanceof Representative) {
             $civixRepresentative->setDistrict(null);
             $civixRepresentative->setStorageId(null);
             $this->entityManager->persist($civixRepresentative);
             $this->entityManager->flush($civixRepresentative);
         }
         $this->entityManager->remove($storageRepresentative);
         $this->entityManager->flush($storageRepresentative);
         return false;
     }
     return true;
 }
 /**
  * Change Representative Storage object according to object which was getten from Cicero Api
  * 
  * @param \Civix\CoreBundle\Entity\RepresentativeStorage $storeObj
  * @param Object $repr Cicero Api object
  * 
  * @return \Civix\CoreBundle\Entity\RepresentativeStorage
  */
 public function fillStorRepresentativeByApiObj(RepresentativeStorage $storeObj, $repr)
 {
     $storeObj->setStorageId($repr->id);
     $storeObj->setFirstName(trim($repr->first_name));
     $storeObj->setLastName(trim($repr->last_name));
     $storeObj->setOfficialTitle(trim($repr->office->title));
     $storeObj->setAvatarSrc($repr->photo_origin_url);
     //create district
     $representativeDistrict = $this->createDistrict($repr->office->district);
     $storeObj->setDistrict($representativeDistrict);
     $storeObj->setContactEmail($repr->office->chamber->contact_email);
     $storeObj->setWebsite($repr->office->chamber->url);
     $storeObj->setCountry($repr->office->district->country);
     if (isset($repr->addresses[0])) {
         $storeObj->setPhone($repr->addresses[0]->phone_1);
         $storeObj->setFax($repr->addresses[0]->fax_1);
         $state = $this->entityManager->getRepository('CivixCoreBundle:State')->findOneByCode($repr->addresses[0]->state);
         $storeObj->setState($state);
         $storeObj->setCity($repr->addresses[0]->city);
         $storeObj->setAddressLine1($repr->addresses[0]->address_1);
         $storeObj->setAddressLine2($repr->addresses[0]->address_2);
         $storeObj->setAddressLine3($repr->addresses[0]->address_3);
     }
     //extended profile
     $storeObj->setParty($repr->party);
     if (isset($repr->notes[1]) && \DateTime::createFromFormat('Y-m-d', $repr->notes[1]) !== false) {
         $storeObj->setBirthday(new \DateTime($repr->notes[1]));
     }
     if (isset($repr->current_term_start_date)) {
         $storeObj->setStartTerm(new \DateTime($repr->current_term_start_date));
     }
     if (isset($repr->term_end_date)) {
         $storeObj->setEndTerm(new \DateTime($repr->term_end_date));
     }
     //social networks
     foreach ($repr->identifiers as $identificator) {
         $socialType = strtolower(isset($identificator->identifier_type) ? $identificator->identifier_type : '');
         $socialMethod = 'set' . ucfirst($socialType);
         if (method_exists($storeObj, $socialMethod)) {
             $storeObj->{$socialMethod}($identificator->identifier_value);
         }
     }
     //save photo
     if (!empty($repr->photo_origin_url)) {
         $fileInfo = explode('.', basename($repr->photo_origin_url));
         $fileExt = array_pop($fileInfo);
         $storeObj->setAvatarFileName(uniqid() . '.' . $fileExt);
         if (false !== ($header = $this->checkLink($repr->photo_origin_url))) {
             if (strpos($header, 'image') !== false) {
                 //square avatars
                 try {
                     $temp_file = tempnam(sys_get_temp_dir(), 'avatar') . '.' . $fileExt;
                     $this->saveImageFromUrl($storeObj->getAvatarSrc(), $temp_file);
                     $this->cropImageService->rebuildImage($temp_file, $temp_file);
                     $fileUpload = new UploadedFile($temp_file, $storeObj->getAvatarFileName());
                     $storeObj->setAvatar($fileUpload);
                 } catch (\Exception $exc) {
                     $this->logger->addError('Image ' . $storeObj->getAvatarSrc() . '. ' . $exc->getMessage());
                     $storeObj->setAvatarFileName(null);
                 }
             }
         }
     }
     //update profile from congress api
     $this->congressService->updateReprStorageProfile($storeObj);
     //update profile from openstate api
     $this->openstatesService->updateReprStorageProfile($storeObj);
     return $storeObj;
 }
 public function getStateCode()
 {
     $this->__load();
     return parent::getStateCode();
 }
 /**
  * @Route(
  *      "/info/sponsored-bills/{storage_id}",
  *      name="api_representative_sponsored_bills",
  *      requirements={
  *          "storage_id" = "\d+"
  *      }
  * )
  * @ParamConverter(
  *      "representative", 
  *      class="CivixCoreBundle:RepresentativeStorage",
  *      options={"id" = "storage_id"}
  * )
  * @Method("GET")
  *
  * @ApiDoc(
  *     resource=true,
  *     description="Get sponsored bills by representative",
  *     statusCodes={
  *         200="Get ponsored bills by representative",
  *         401="Authorization required",
  *         405="Method Not Allowed"
  *     }
  * )
  */
 public function getSponsoredBills(RepresentativeStorage $representative)
 {
     $responseBody = array();
     $openStateId = $representative->getOpenstateId();
     if ($openStateId) {
         $responseBody = $this->get('civix_core.openstates_api')->getBillsBySponsorId($openStateId);
     }
     $response = new Response();
     $response->setContent($this->jmsSerialization($responseBody, 'api-bills'));
     $response->headers->set('Content-Type', 'application/json');
     return $response;
 }