Skip to content

Commit

Permalink
New front page design
Browse files Browse the repository at this point in the history
Lots of improvements along with new design.

* Mobile friendly.
* Space for featured commons debates and editing interface for this.
* Scottish and NI front pages now show list of regional MLAs/MSPs
  if we know the user's postcode.
* Displays upcoming westminster events on front page
* Moves the front page to use the new style template system.

Fixes #589
  • Loading branch information
wrightmartin authored and struan committed May 26, 2015
1 parent 5903270 commit 3f36fe7
Show file tree
Hide file tree
Showing 31 changed files with 2,029 additions and 373 deletions.
215 changes: 215 additions & 0 deletions classes/Homepage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
<?php

namespace MySociety\TheyWorkForYou;

class Homepage {

private $db;

protected $mp_house = 1;
protected $cons_type = 'WMC';
protected $mp_url = 'yourmp';
protected $page = 'overview';

protected $recent_types = array(
'DEBATELIST' => array('recent_debates', 'debatesfront', 'Commons debates'),
'LORDSDEBATELIST' => array('recent_debates', 'lordsdebatesfront', 'Lords debates'),
'WHALLLIST' => array('recent_debates', 'whallfront', 'Westminster Hall debates'),
'WMSLIST' => array('recent_wms', 'wmsfront', 'Written ministerial statements'),
'WRANSLIST' => array('recent_wrans', 'wransfront', 'Written answers'),
'StandingCommittee' => array('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 = array();

$data['debates'] = $this->getDebatesData();
$data['mp_data'] = $this->getMP();
$data['regional'] = $this->getRegionalList();
$data['popular_searches'] = $this->getPopularSearches();
$data['urls'] = $this->getURLs();
$data['calendar'] = $this->getCalendarData();
$data['featured'] = $this->getEditorialContent();

return $data;
}

private function getPostCodeChangeURL() {
global $THEUSER;
$CHANGEURL = new \URL('userchangepc');
if ($THEUSER->isloggedin()) {
$CHANGEURL = new \URL('useredit');
}

return $CHANGEURL->generate();
}

protected function getMP() {
$mp_url = new \URL($this->mp_url);
$mp_data = array();
global $THEUSER;

if ($THEUSER->has_postcode()) {
// User is logged in and has a postcode, or not logged in with a cookied postcode.

// (We don't allow the user to search for a postcode if they
// already have one set in their prefs.)

// this is for people who have e.g. an English postcode looking at the
// Scottish homepage
try {
$constituencies = postcode_to_constituencies($THEUSER->postcode());
if ( isset($constituencies[$this->cons_type]) ) {
$constituency = $constituencies[$this->cons_type];
$MEMBER = new Member(array('constituency'=>$constituency, 'house'=> $this->mp_house));
}
} catch ( MemberException $e ) {
return $mp_data;
}

if (isset($MEMBER) && $MEMBER->valid) {
$mp_data['name'] = $MEMBER->full_name();
$left_house = $MEMBER->left_house();
$mp_data['former'] = '';
if ($left_house[$this->mp_house]['date'] != '9999-12-31') {
$mp_data['former'] = 'former';
}
$mp_data['postcode'] = $THEUSER->postcode();
$mp_data['mp_url'] = $mp_url->generate();
$mp_data['change_url'] = $this->getPostCodeChangeURL();
}
}

return $mp_data;
}

protected function getRegionalList() {
return NULL;
}

protected function getEditorialContent() {
$debatelist = new \DEBATELIST;
$featured = new Model\Featured;
$gid = $featured->get_gid();
if ( $gid ) {
$title = $featured->get_title();
$context = $featured->get_context();
$related = $featured->get_related();
$item = $this->getFeaturedDebate($gid, $title, $context, $related);
} else {
$item = $debatelist->display('recent_debates', array('days' => 7, 'num' => 1), 'none');
if ( isset($item['data']) && count($item['data']) ) {
$item = $item['data'][0];
$more_url = new \URL('debates');
$item['more_url'] = $more_url->generate();
$item['desc'] = 'Commons Debates';
$item['related'] = array();
$item['featured'] = false;
} else {
$item = array();
}
}

return $item;
}

public function getFeaturedDebate($gid, $title, $context, $related) {
$debatelist = new \DEBATELIST;

$item = $debatelist->display('featured_gid', array('gid' => $gid), 'none');
$item = $item['data'];
$item['headline'] = $title;
$item['context'] = $context;
$item['featured'] = true;

$related_debates = array();
foreach ( $related as $related_gid ) {
if ( $related_gid ) {
$related_item = $debatelist->display('featured_gid', array('gid' => $related_gid), 'none');
$related_debates[] = $related_item['data'];
}
}
$item['related'] = $related_debates;
return $item;
}

protected function getURLs() {
$urls = array();

$search = new \URL('search');
$urls['search'] = $search->generate();

$alert = new \URL('alert');
$urls['alert'] = $alert->generate();

return $urls;
}

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

$recent_content = array();

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 = array( 'data' => $instance->display($recent[0], array('num' => 5), 'none') );
} else {
$content = $instance->display($recent[0], array('days' => 7, 'num' => 1), 'none');
if ( isset($content['data']) && count($content['data']) ) {
$content = $content['data'][0];
} else {
$content = array();
}
}
if ( $content ) {
$content['more_url'] = $more_url->generate();
$content['desc'] = $recent[2];
$recent_content[] = $content;
}
}

$debates['recent'] = $recent_content;

return $debates;
}

protected function getPopularSearches() {
global $SEARCHLOG;
$popular_searches = $SEARCHLOG->popular_recent(10);

return $popular_searches;
}

private function getCalendarData() {
$date = date('Y-m-d');
$q = $this->db->query("SELECT * FROM future
LEFT JOIN future_people ON future.id = future_people.calendar_id AND witness = 0
WHERE event_date >= :date
AND deleted = 0
ORDER BY event_date, chamber, pos",
array( ':date' => $date )
);

if (!$q->rows()) {
return array();
}

$data = array();
foreach ($q->data as $row) {
$data[$row['event_date']][$row['chamber']][] = $row;
}

return $data;
}

}
59 changes: 59 additions & 0 deletions classes/Member.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,63 @@ public function getEnterLeaveStrings() {
return $output;
}

public static function getRegionalList($postcode, $house, $type) {
$db = new \ParlDB;

$dissolution_dates = array(
3 => '2011-03-24',
4 => '2011-03-23'
);
$mreg = array();
$constituencies = postcode_to_constituencies($postcode);
if ( isset($constituencies[$type]) ) {
$cons_name = $constituencies[$type];
$q = $db->query("SELECT member.person_id, title, lordofname, given_name, family_name, constituency, house
FROM member, person_names
WHERE
member.person_id = person_names.person_id
AND constituency = :cons_name
AND entered_house >= start_date
AND left_house <= end_date
AND left_reason = 'still_in_office' AND house = :house",
array(
':house' => $house,
':cons_name' => $cons_name
)
);
if ( !$q->rows() ) {
$q = $db->query("SELECT member.person_id, title, lordofname, given_name, family_name, constituency, house
FROM member, person_names
WHERE
member.person_id = person_names.person_id
AND constituency = :cons_name
AND house = :house
AND entered_house >= start_date
AND left_house <= end_date
AND left_house = :dissolution_date",
array(
':house' => $house,
':cons_name' => $cons_name,
':dissolution_date' => $dissolution_dates[$house]
)
);
}

for ($i = 0; $i < $q->rows; $i++) {
$name = member_full_name($house, $q->field($i, 'title'),
$q->field($i, 'given_name'), $q->field($i, 'family_name'),
$q->field($i, 'lordofname'));

$mreg[] = array(
'person_id' => $q->field($i, 'person_id'),
'name' => $name,
'house' => $q->field($i, 'house'),
'constituency' => $q->field($i, 'constituency')
);
}
}

return $mreg;
}

}
106 changes: 106 additions & 0 deletions classes/Model/Featured.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* Banner Model
*
* @package TheyWorkForYou
*/

namespace MySociety\TheyWorkForYou\Model;

class Featured {

/**
* DB handle
*/
private $db;

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

public function get_title() {
return $this->_get('featured_title');
}

public function set_title($title) {
return $this->_set('featured_title', $title);
}

public function get_context() {
return $this->_get('featured_context');
}

public function set_context($context) {
return $this->_set('featured_context', $context);
}

public function get_gid() {
return $this->_get('featured_gid');
}

public function set_gid($gid) {
return $this->_set('featured_gid', $gid);
}

public function get_related() {
$related = $this->_get('featured_related');
return explode(',', $related);
}

public function set_related($related) {
$related = implode(',', $related);
$this->_set('featured_related', $related);
}

private function _get($key) {
$text = NULL;

$q = $this->db->query(
"SELECT value FROM editorial WHERE item = :key",
array(
':key' => $key
)
);

if ($q->rows) {
$text = $q->field(0, 'value');
if ( trim($text) == '' ) {
$text = NULL;
}
}

return $text;
}

private function _set($key, $value) {
if ( trim($value) == '' ) {
$value = NULL;
}
$check_q = $this->db->query(
"SELECT value FROM editorial WHERE item = :key",
array(
':key' => $key
)
);
if ( $check_q->rows ) {
$set_q = $this->db->query("UPDATE editorial set value = :value WHERE item = :key",
array(
':key' => $key,
':value' => $value
)
);
} else {
$set_q = $this->db->query("INSERT INTO editorial (item, value ) VALUES (:key, :value)",
array(
':key' => $key,
':value' => $value
)
);
}

if ( $set_q->success() ) {
return true;
}
return false;
}
}
Loading

0 comments on commit 3f36fe7

Please sign in to comment.