Example #1
0
 /**
  * JSON => DB
  */
 public function actionImport()
 {
     error_reporting(E_ALL & ~E_NOTICE);
     // Get data
     $data = json_decode(file_get_contents(\yii::getAlias('@frontend') . '/data/data.json'));
     // Deputies
     Deputy::deleteAll();
     foreach ($data->deputies as $deputyData) {
         // Data
         $deputyData->partyName = $deputyData->party;
         $deputyData->facebook = $deputyData->facebook ?: null;
         $deputyData->phones = $deputyData->phones ? implode(',', $deputyData->phones) : null;
         $deputyData->dateAuthorityStart = date('Y-m-d', $deputyData->dateAuthorityStart);
         $deputyData->dateAuthorityStop = $deputyData->dateAuthorityStop ? date('Y-m-d', $deputyData->dateAuthorityStop) : null;
         $deputyData->districtId = $deputyData->district->id ?: null;
         $deputy = new Deputy();
         $deputy->setAttributes((array) $deputyData, false);
         $deputy->save();
         // Votes
         DeputyLaw::deleteAll(['deputyId' => $deputy->id]);
         foreach ($deputyData->laws as $deputyLawId => $deputyLawVote) {
             if ($deputyLawId === 'відвідуваність') {
                 continue;
             }
             $deputyLaw = new DeputyLaw();
             $deputyLaw->setAttributes(['deputyId' => $deputy->id, 'lawId' => $deputyLawId, 'vote' => $deputyLawVote], false);
             $deputyLaw->save();
         }
     }
     // Laws
     Law::deleteAll();
     foreach ($data->laws as $lawData) {
         $law = new Law();
         $law->setAttributes((array) $lawData, false);
         $law->dateVoting = $lawData->date ? date('Y-m-d H:i:s', $lawData->date) : null;
         $law->save();
     }
     // Law tags
     LawTag::deleteAll();
     foreach ($data->lawTags as $lawTagData) {
         $lawTag = new LawTag();
         $lawTag->setAttributes((array) $lawTagData, false);
         $lawTag->save();
     }
     // Parites
     Party::deleteAll();
     foreach ($data->parties as $partyData) {
         $party = new Party();
         $party->setAttributes((array) $partyData, false);
         $party->save();
     }
 }
Example #2
0
 /**
  * Import deputies' votes
  */
 public function actionVotes($url = '')
 {
     foreach (Law::find()->all() as $law) {
         // Skip
         if (!$law->urlVoting) {
             continue;
         }
         if ($url && $url !== $law->urlVoting) {
             continue;
         }
         // Get votes
         echo "{$law->id}\n";
         $content = file_get_contents($law->urlVoting);
         $content = iconv('windows-1251', 'utf-8', $content);
         $html = $this->parser->str_get_html($content);
         // Parse date
         $lawInfo = $html->find('.head_gol', 0)->plaintext;
         preg_match('/\\d\\d\\.\\d\\d\\.\\d\\d\\d\\d \\d\\d:\\d\\d/', $lawInfo, $matches);
         $law->dateVoting = date('Y-m-d H:i:s', strtotime($matches[0]));
         $law->save();
         // Parse votes
         $votes = $html->find('.golos');
         $deputyNo = 0;
         foreach (Deputy::find()->orderBy('name')->all() as $deputy) {
             // Delete voting
             DeputyLaw::deleteAll(['deputyId' => $deputy->id, 'lawId' => $law->id]);
             // Check if law voting date is in authority date range
             if ($law->dateVoting) {
                 if ($deputy->dateAuthorityStart > $law->dateVoting || $deputy->dateAuthorityStop && $deputy->dateAuthorityStop < $law->dateVoting) {
                     continue;
                 }
             }
             // Get vote
             $voteStr = strip_tags($votes[$deputyNo]->plaintext);
             if ($law->urlVoting === 'http://w1.c1.rada.gov.ua/pls/radan_gs09/ns_golos?g_id=3049' && in_array($deputy->name, array('Кулініч Олег Іванович', 'Мельничук Сергій Петрович', 'Рудик Сергій Ярославович'))) {
                 $vote = DeputyLaw::VOTE_YES;
             } else {
                 switch ($voteStr) {
                     case 'За':
                     case 'За*':
                         $vote = DeputyLaw::VOTE_YES;
                         break;
                     default:
                         $registration = Registration::find()->where(['deputyId' => $deputy->id, 'date' => date('Y-m-d', strtotime($law->dateVoting))])->andWhere(['<=', 'time', date('H:i:s', strtotime($law->dateVoting))])->orderBy('time DESC')->one();
                         if ($registration->isPresent) {
                             $vote = DeputyLaw::VOTE_NO;
                         } else {
                             $vote = DeputyLaw::VOTE_ABSENT;
                         }
                         break;
                 }
             }
             // Save law vote
             $deputyLaw = new DeputyLaw();
             $deputyLaw->setAttributes(['deputyId' => $deputy->id, 'lawId' => $law->id, 'vote' => $vote], false);
             $deputyLaw->save();
             // Next deputy index
             $deputyNo++;
         }
     }
 }