Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialSenateLookup
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 3
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 1
42
 loadSenators
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace MediaWiki\Extension\CongressLookup;
4
5use Html;
6use TemplateParser;
7use UnlistedSpecialPage;
8
9/**
10 * This class uses the state to look up information about the user's senate representatives
11 * and presents that information to the user.
12 */
13class SpecialSenateLookup extends UnlistedSpecialPage {
14
15    /** @var null */
16    protected $state = null;
17
18    /** @var array */
19    protected $senators = [];
20
21    public function __construct() {
22        // Register special page
23        parent::__construct( 'SenateLookup' );
24    }
25
26    /**
27     * Shows the page to the user.
28     * @param string $sub The subpage string argument (if any).
29     */
30    public function execute( $sub ) {
31        $out = $this->getOutput();
32        $out->addModuleStyles( 'ext.congresslookup.styles' );
33        $out->setPageTitle( $this->msg( 'congresslookup-senator-contact' ) );
34
35        // Pull in query string parameters
36        $state = $this->getRequest()->getVal( 'state', '' );
37
38        if ( strlen( $state ) !== 2 ) {
39            $out->addHTML(
40                Html::element(
41                    'div',
42                    [ 'class' => 'error' ],
43                    $this->msg( 'congresslookup-state-error' )->text()
44                )
45            );
46            return;
47        }
48
49        $this->state = $state;
50        // Load the contact information
51        $this->loadSenators();
52        $myContactArray = [];
53        foreach ( $this->senators as $senator ) {
54            if ( isset( $senator['state'] ) && $senator['state'] === $state ) {
55                $myContactArray['contacts'][] = $senator;
56            }
57        }
58
59        if ( !$myContactArray ) {
60            $out->addHTML(
61                Html::element(
62                    'div',
63                    [ 'class' => 'error' ],
64                    $this->msg( 'congresslookup-senator-error' )->text()
65                )
66            );
67            return;
68        }
69
70        // Build the HTML table
71        $templateParser = new TemplateParser( dirname( __DIR__ ) . '/templates' );
72        $contactsHtml = $templateParser->processTemplate( 'contacts', $myContactArray );
73        $out->addHTML( $contactsHtml );
74        // Show sidebar content
75        $parsedSidebarMessage = $this->msg( 'net-neutrality-sidebar' )->parse();
76        $out->addHTML(
77            Html::rawElement(
78                'div',
79                [ 'class' => 'plainlinks', 'id' => 'instructions' ],
80                $parsedSidebarMessage
81            )
82        );
83    }
84
85    /**
86     * Load senators from a json file.
87     */
88    protected function loadSenators() {
89        $contents = file_get_contents( dirname( __DIR__ ) . '/senators.json' );
90        $contents = json_decode( $contents, true );
91        if ( $contents ) {
92            $this->senators = $contents['senators'];
93        }
94    }
95}