/** * Return law tag based on deputy vote * @return LawTag */ public function getLawTag() { $law = $this->law; $lawTagName = ''; if ($law->id === 'відвідуваність') { if ($this->vote >= 50) { $lawTagName = 'працює'; } else { $lawTagName = 'прогулює'; } } else { if (!$law->good && $this->vote === Law::VOTE_ABSENT) { $lawTagName = ''; } elseif ($this->vote === Law::VOTE_YES) { $lawTagName = $law->tagYes; } else { $lawTagName = $law->tagNo; } } return LawTag::findOne(['name' => $lawTagName]); }
/** * 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; }
/** * 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(); }
?> </tbody> </table> </td> </tr></tbody></table> </div> <div class="modal-body ng-scope"> <table class="table"> <tbody> <?php foreach ($deputy->lawTags as $lawTagName) { ?> <?php $tagLawIds = $deputy->lawTagsInfo[$lawTagName]['laws']; $lawTag = LawTag::findOne(['name' => $lawTagName]); $lawTagOpposite = LawTag::findOne(['opposite' => $lawTagName]); if (isset($deputy->lawTagsInfo[$lawTagOpposite->name])) { if ($lawTagOpposite->type === LawTag::TYPE_SUCCESS && $deputy->lawTagsInfo[$lawTagOpposite->name]['rate'] >= $deputy->lawTagsInfo[$lawTagName]['rate']) { continue; } elseif ($lawTagOpposite->type === LawTag::TYPE_DANGER && $deputy->lawTagsInfo[$lawTagOpposite->name]['rate'] > $deputy->lawTagsInfo[$lawTagName]['rate']) { continue; } $tagLawIds = \yii\helpers\ArrayHelper::merge($tagLawIds, $deputy->lawTagsInfo[$lawTagOpposite->name]['laws']); } ?> <tr> <td> <?php if (in_array($lawTagName, ['працює', 'прогулює'])) { ?> <p style="font-size: 14px; line-height: 18px;">