Exemplo n.º 1
0
 /**
  * DB => JSON
  */
 public function actionExport()
 {
     echo "Export to data.json...";
     $data = [];
     // Parties
     $data['parties'] = [];
     foreach (Party::find()->all() as $party) {
         $data['parties'][] = ['name' => $party->name, 'deputies' => $party->deputyCount, 'lawTagsInfo' => $party->lawTagsInfo];
     }
     usort($data['parties'], function ($a, $b) {
         return $b['deputies'] - $a['deputies'];
     });
     // Laws
     $data['laws'] = [];
     foreach (Law::find()->orderBy('(dateVoting IS NOT NULL), dateVoting DESC')->all() as $law) {
         $lawData = ['id' => $law->id, 'no' => $law->no, 'name' => $law->name, 'url' => $law->url, 'urlVoting' => $law->urlVoting, 'good' => $law->good, 'tagYes' => $law->tagYes, 'descYes' => $law->descYes, 'tagNo' => $law->tagNo, 'descNo' => $law->descNo, 'date' => strtotime($law->dateVoting)];
         if (!$law->urlVoting) {
             unset($lawData['urlVoting']);
         }
         if (!$law->dateVoting) {
             unset($lawData['date']);
         }
         $data['laws'][] = $lawData;
     }
     // Law tags
     $data['lawTags'] = [];
     foreach (LawTag::find()->orderBy('order')->all() as $lawTag) {
         $data['lawTags'][] = ['name' => $lawTag->name, 'desc' => $lawTag->desc, 'type' => $lawTag->type, 'opposite' => $lawTag->opposite, 'order' => $lawTag->order, 'laws' => $lawTag->lawCount];
     }
     // Deputies
     $data['deputies'] = [];
     foreach (Deputy::find()->orderBy('name')->all() as $deputy) {
         $deputyData = ['id' => $deputy->id, 'name' => $deputy->name, 'party' => $deputy->partyName, 'phones' => $deputy->phones ? explode(',', $deputy->phones) : [], 'residence' => $deputy->residence, 'dateAuthorityStart' => strtotime($deputy->dateAuthorityStart)];
         if ($deputy->dateAuthorityStop) {
             $deputyData['dateAuthorityStop'] = strtotime($deputy->dateAuthorityStop);
         }
         if ($deputy->facebook) {
             $deputyData['facebook'] = $deputy->facebook;
         }
         if ($deputy->district) {
             $deputyData['district'] = ['id' => $deputy->district->id, 'region' => $deputy->district->region, 'text' => $deputy->district->text];
         }
         // Deputy laws
         $deputyData['laws'] = [];
         $deputyData['laws']['відвідуваність'] = $deputy->registrationRate;
         foreach ($deputy->laws as $deputyLaw) {
             $deputyData['laws'][$deputyLaw->lawId] = $deputyLaw->vote;
         }
         // Deputy law tags and info
         $deputyData['lawTags'] = $deputy->lawTags;
         $deputyData['lawTagsInfo'] = $deputy->lawTagsInfo;
         $data['deputies'][] = $deputyData;
     }
     // Search suggestions
     $tags = array();
     $tagsDeputyName = array();
     $tagsDeputyDistrict = array();
     foreach (LawTag::find()->orderBy('name')->all() as $lawTag) {
         $tags[] = array('name' => $lawTag->name, 'type' => 'law-tag', 'typeOrder' => 1, 'lawTagType' => $lawTag->type);
     }
     foreach (Party::find()->orderBy('name')->all() as $party) {
         $tags[] = array('name' => $party->name, 'type' => 'party', 'typeOrder' => 2);
     }
     foreach (Deputy::find()->orderBy('name')->all() as $deputy) {
         $tagsDeputyName[] = array('name' => $deputy->name, 'type' => 'deputy-name', 'typeOrder' => 3, 'deputyId' => $deputy->id, 'dateAuthorityStop' => $deputy->dateAuthorityStop ? $deputy->dateAuthorityStop : '');
     }
     foreach (District::find()->orderBy('id')->all() as $district) {
         $tagsDeputyDistrict[] = array('name' => "Виборчий округ №{$district->id} ({$district->region})", 'type' => 'district', 'typeOrder' => 4, 'districtId' => (int) $district->id);
     }
     $data['searchSuggestions'] = array_merge($tags, $tagsDeputyName, $tagsDeputyDistrict);
     // Array data to json
     $json = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
     // Save data
     chdir(\yii::getAlias('@frontend'));
     file_put_contents('data/data.json', $json);
     file_put_contents('js/data.js', "VVKP_DATA = {$json};");
     $this->actionStatic();
     $this->actionGzip();
 }
Exemplo n.º 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++;
         }
     }
 }