Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homepage class tidy and tweaks #1834

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions classes/Divisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public function getRecentDebatesWithDivisions($number = 20, $majors = null) {
'url' => $url->generate() . $anchor,
'title' => "$debate[section_body] : $debate[subsection_body]",
'date' => $debate['hdate'],
'major' => $debate['major'],
];
}

Expand Down
79 changes: 79 additions & 0 deletions classes/Homepage/Base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace MySociety\TheyWorkForYou\Homepage;

abstract class Base {
public function display() {
global $this_page;
$this_page = $this->page;

$data = [];

$common = new \MySociety\TheyWorkForYou\Common();

$data['debates'] = $this->getDebatesData();

$user = new \MySociety\TheyWorkForYou\User();
$data['mp_data'] = $user->getRep($this->cons_type, $this->mp_house);

$data['regional'] = $this->getRegionalList();
$data['popular_searches'] = $common->getPopularSearches();
$data['featured'] = $this->getEditorialContent($data);
$data['divisions'] = $this->getRecentDivisions();
$data['search_box'] = $this->getSearchBox($data);

return $data;
}

abstract protected function getSearchBox(array $data): \MySociety\TheyWorkForYou\Search\SearchBox;
abstract protected function getEditorialContent(array &$data);

protected function getRegionalList() {
return null;
}

private function getRecentDivisions() {
$divisions = new \MySociety\TheyWorkForYou\Divisions();
return $divisions->getRecentDebatesWithDivisions(5, $this->houses);
}

protected function getDebatesData() {
$debates = []; // holds the most recent data there is data for, indexed by type

$recent_content = [];

foreach ($this->recent_types as $class => $recent) {
$class = "\\$class";
$instance = new $class();
$more_url = new \MySociety\TheyWorkForYou\Url($recent[1]);
if ($recent[0] == 'recent_pbc_debates') {
$content = [ 'data' => $instance->display($recent[0], ['num' => 5], 'none') ];
} elseif ($recent[1] == 'senedddebatesfront' || $recent[1] == 'nidebatesfront') {
$content = $instance->display($recent[0], ['days' => 30, 'num' => 6], 'none');
# XXX Bit hacky, for now
foreach ($content['data'] as $d) {
dracos marked this conversation as resolved.
Show resolved Hide resolved
$d['more_url'] = $more_url->generate();
$d['desc'] = '';
$recent_content[] = $d;
}
$content = [];
} else {
$content = $instance->display($recent[0], ['days' => 7, 'num' => 1], 'none');
if (isset($content['data']) && count($content['data'])) {
$content = $content['data'][0];
} else {
$content = [];
}
}
if ($content) {
$content['more_url'] = $more_url->generate();
$content['desc'] = $recent[2];
$recent_content[] = $content;
}
}

$debates['recent'] = $recent_content;

return $debates;
}
}
72 changes: 72 additions & 0 deletions classes/Homepage/NI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace MySociety\TheyWorkForYou\Homepage;

class NI extends Base {
protected $mp_house = 3;
protected $cons_type = 'NIE';
protected $page = 'nioverview';
protected $houses = [5];
protected $recent_types = [
'NILIST' => ['recent_debates', 'nidebatesfront', 'Northern Ireland Assembly debates'],
];

public function display() {
$data = parent::display();
$data['popular_searches'] = null;
$data['template'] = 'ni/index';
return $data;
}

protected function getEditorialContent(&$data) {
$featured = [];
if (count($data['debates']['recent'])) {
$MOREURL = new \MySociety\TheyWorkForYou\Url('nidebatesfront');
$MOREURL->insert([ 'more' => 1 ]);
$featured = array_shift($data['debates']['recent']);
$featured['more_url'] = $MOREURL->generate();
$featured['desc'] = 'Northern Ireland Assembly debate';
$featured['related'] = [];
$featured['featured'] = false;
}
return $featured;
}

protected function getSearchBox(array $data): \MySociety\TheyWorkForYou\Search\SearchBox {

global $THEUSER;

if ($THEUSER->isloggedin() && $THEUSER->postcode() != '' || $THEUSER->postcode_is_set()) {
$postcode = $THEUSER->postcode();
} else {
$postcode = null;
}

$search_box = new \MySociety\TheyWorkForYou\Search\SearchBox();
$search_box->homepage_panel_class = "panel--homepage--niassembly";
$search_box->homepage_subhead = "Northern Ireland Assembly";
$search_box->homepage_desc = "";
$search_box->search_section = "ni";
$search_box->quick_links = [];
if (count($data["regional"])) {
$constituency = $data["regional"][0]["constituency"];
$search_box->add_quick_link('Find out more about your MLAs for ' . $constituency, '/postcode/?pc=' . $postcode, 'torso');
}
$search_box->add_quick_link('Create and manage email alerts', '/alert/', 'megaphone');
$search_box->add_quick_link(gettext('Subscribe to our newsletter'), '/about/#about-mysociety', 'mail');
$search_box->add_quick_link('Donate to support our work', '/support-us/', 'heart');
$search_box->add_quick_link('Learn more about TheyWorkForYou', '/about/', 'magnifying-glass');
return $search_box;
}

protected function getRegionalList() {
global $THEUSER;

$mreg = [];
if ($THEUSER->isloggedin() && $THEUSER->postcode() != '' || $THEUSER->postcode_is_set()) {
return \MySociety\TheyWorkForYou\Member::getRegionalList($THEUSER->postcode, 3, 'NIE');
}

return $mreg;
}
}
49 changes: 15 additions & 34 deletions classes/SPHomepage.php → classes/Homepage/Scotland.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php

namespace MySociety\TheyWorkForYou;
namespace MySociety\TheyWorkForYou\Homepage;

class SPHomepage extends Homepage {
class Scotland extends Base {
protected $mp_house = 4;
protected $cons_type = 'SPC';
protected $mp_url = 'yourmsp';
protected $page = 'spoverview';
protected $houses = [7];

Expand All @@ -14,8 +13,8 @@ class SPHomepage extends Homepage {
'SPWRANSLIST' => ['recent_wrans', 'spwransfront', 'Written answers'],
];

protected function getSearchBox(array $data): Search\SearchBox {
$search_box = new Search\SearchBox();
protected function getSearchBox(array $data): \MySociety\TheyWorkForYou\Search\SearchBox {
$search_box = new \MySociety\TheyWorkForYou\Search\SearchBox();
$search_box->homepage_panel_class = "panel--homepage--scotland";
$search_box->homepage_subhead = "Scottish Parliament";
$search_box->homepage_desc = "";
Expand All @@ -32,43 +31,25 @@ protected function getSearchBox(array $data): Search\SearchBox {
return $search_box;
}

protected function getEditorialContent() {
protected function getEditorialContent(&$data) {
$debatelist = new \SPLIST();
$item = $debatelist->display('recent_debates', ['days' => 7, 'num' => 1], 'none');

$item = $item['data'][0];
$more_url = new Url('spdebatesfront');
$item['more_url'] = $more_url->generate();
$item['desc'] = 'Scottish Parliament debate';
$item['related'] = [];
$item['featured'] = false;

if (count($item['data'])) {
$item = $item['data'][0];
$more_url = new \MySociety\TheyWorkForYou\Url('spdebatesfront');
$item['more_url'] = $more_url->generate();
$item['desc'] = 'Scottish Parliament debate';
$item['related'] = [];
$item['featured'] = false;
}
return $item;
}

protected function getURLs() {
$urls = [];

$regional = new Url('msp');
$urls['regional'] = $regional->generate();

return $urls;
}

protected function getCalendarData() {
return null;
}


protected function getRegionalList() {
global $THEUSER;

$mreg = [];

if ($THEUSER->isloggedin() && $THEUSER->postcode() != '' || $THEUSER->postcode_is_set()) {
return Member::getRegionalList($THEUSER->postcode, 4, 'SPE');
return \MySociety\TheyWorkForYou\Member::getRegionalList($THEUSER->postcode, 4, 'SPE');
}

return $mreg;
return [];
}
}
98 changes: 13 additions & 85 deletions classes/Homepage.php → classes/Homepage/UK.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<?php

namespace MySociety\TheyWorkForYou;

class Homepage {
private $db;
namespace MySociety\TheyWorkForYou\Homepage;

class UK extends Base {
protected $mp_house = 1;
protected $cons_type = 'WMC';
protected $mp_url = 'yourmp';
protected $page = 'overview';
protected $houses = [1, 101];
private $mp_url = 'yourmp';

protected $recent_types = [
'DEBATELIST' => ['recent_debates', 'debatesfront', 'Commons debates'],
Expand All @@ -20,39 +18,15 @@ class Homepage {
'StandingCommittee' => ['recent_pbc_debates', 'pbcfront', 'Public Bill committees'],
];

public function __construct() {
$this->db = new \ParlDB();
}

public function display() {
global $this_page;
$this_page = $this->page;

$data = [];

$common = new Common();
$dissolution = Dissolution::dates();

$data['debates'] = $this->getDebatesData();

$user = new User();
$data['mp_data'] = $user->getRep($this->cons_type, $this->mp_house);
$data["commons_dissolved"] = isset($dissolution[1]);

$data['regional'] = $this->getRegionalList();
$data['popular_searches'] = $common->getPopularSearches();
$data['urls'] = $this->getURLs();
$data = parent::display();
$data['calendar'] = $this->getCalendarData();
$data['featured'] = $this->getEditorialContent();
$data['topics'] = $this->getFrontPageTopics();
$data['divisions'] = $this->getRecentDivisions();
$data['search_box'] = $this->getSearchBox($data);

return $data;
}

protected function getSearchBox(array $data): Search\SearchBox {
$search_box = new Search\SearchBox();
protected function getSearchBox(array $data): \MySociety\TheyWorkForYou\Search\SearchBox {
$search_box = new \MySociety\TheyWorkForYou\Search\SearchBox();
$search_box->homepage_panel_class = "panel--homepage--overall";
$search_box->homepage_subhead = "";
$search_box->homepage_desc = "Understand who represents you, across the UK’s Parliaments.";
Expand All @@ -68,15 +42,11 @@ protected function getSearchBox(array $data): Search\SearchBox {
return $search_box;
}

protected function getRegionalList() {
return null;
}

protected function getEditorialContent() {
protected function getEditorialContent(&$data) {
$debatelist = new \DEBATELIST();
$featured = new Model\Featured();
$featured = new \MySociety\TheyWorkForYou\Model\Featured();
$gid = $featured->get_gid();
$gidCheck = new Gid($gid);
$gidCheck = new \MySociety\TheyWorkForYou\Gid($gid);
$gid = $gidCheck->checkForRedirect();
if ($gid) {
$title = $featured->get_title();
Expand All @@ -85,9 +55,9 @@ protected function getEditorialContent() {
$item = $this->getFeaturedDebate($gid, $title, $context, $related);
} else {
$item = $debatelist->display('recent_debates', ['days' => 7, 'num' => 1], 'none');
if (isset($item['data']) && count($item['data'])) {
if (count($item['data'])) {
$item = $item['data'][0];
$more_url = new Url('debates');
$more_url = new \MySociety\TheyWorkForYou\Url('debates');
$item['more_url'] = $more_url->generate();
$item['desc'] = 'Commons Debates';
$item['related'] = [];
Expand Down Expand Up @@ -127,54 +97,12 @@ public function getFeaturedDebate($gid, $title, $context, $related) {
}

protected function getFrontPageTopics() {
$topics = new Topics();
$topics = new \MySociety\TheyWorkForYou\Topics();
return $topics->getFrontPageTopics();
}

private function getRecentDivisions() {
$divisions = new Divisions();
return $divisions->getRecentDebatesWithDivisions(5, $this->houses);
}

protected function getURLs() {
$urls = [];

return $urls;
}

protected function getDebatesData() {
$debates = []; // holds the most recent data there is data for, indexed by type

$recent_content = [];

foreach ($this->recent_types as $class => $recent) {
$class = "\\$class";
$instance = new $class();
$more_url = new Url($recent[1]);
if ($recent[0] == 'recent_pbc_debates') {
$content = [ 'data' => $instance->display($recent[0], ['num' => 5], 'none') ];
} else {
$content = $instance->display($recent[0], ['days' => 7, 'num' => 1], 'none');
if (isset($content['data']) && count($content['data'])) {
$content = $content['data'][0];
} else {
$content = [];
}
}
if ($content) {
$content['more_url'] = $more_url->generate();
$content['desc'] = $recent[2];
$recent_content[] = $content;
}
}

$debates['recent'] = $recent_content;

return $debates;
}

private function getCalendarData() {
return Utility\Calendar::fetchFuture();
return \MySociety\TheyWorkForYou\Utility\Calendar::fetchFuture();
}

}
Loading
Loading