Beispiel #1
0
 /**
  * Returns party's law tags info
  * @return array
  */
 public function getLawTagsInfo()
 {
     $info = [];
     foreach (LawTag::find()->orderBy('order')->all() as $lawTag) {
         $lawTagGood = $lawTag->type === LawTag::TYPE_SUCCESS ? $lawTag->name : $lawTag->opposite;
         $lawTagBad = $lawTag->type === LawTag::TYPE_SUCCESS ? $lawTag->opposite : $lawTag->name;
         $info[$lawTagGood] = ['good' => $lawTagGood, 'bad' => $lawTagBad, 'rates' => []];
     }
     foreach ($this->deputies as $deputy) {
         $lawTagsInfo = $deputy->lawTagsInfo;
         foreach ($lawTagsInfo as $lawTagName => $lawTagInfo) {
             $lawTag = LawTag::findOne(['name' => $lawTagName]);
             $lawTagGood = $lawTag->type === LawTag::TYPE_SUCCESS ? $lawTag->name : $lawTag->opposite;
             $lawTagBad = $lawTag->type === LawTag::TYPE_SUCCESS ? $lawTag->opposite : $lawTag->name;
             if ($lawTag->type === LawTag::TYPE_SUCCESS) {
                 $info[$lawTagGood]['rates'][] = $lawTagInfo['rate'];
                 if (isset($lawTagsInfo[$lawTag->opposite])) {
                     $info[$lawTagGood]['rates'][] = 100 - $lawTagsInfo[$lawTag->opposite]['rate'];
                 }
             } else {
                 $info[$lawTagGood]['rates'][] = 100 - $lawTagInfo['rate'];
                 if (isset($lawTagsInfo[$lawTag->opposite])) {
                     $info[$lawTagGood]['rates'][] = $lawTagsInfo[$lawTag->opposite]['rate'];
                 }
             }
         }
     }
     foreach ($info as $lawTagName => $lawTagInfo) {
         if (count($lawTagInfo['rates'])) {
             $info[$lawTagName]['rate'] = round(array_sum($lawTagInfo['rates']) / count($lawTagInfo['rates']));
         } else {
             $info[$lawTagName]['rate'] = 0;
         }
         unset($info[$lawTagName]['rates']);
     }
     return $info;
 }
Beispiel #2
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();
 }