Skip to content

Commit

Permalink
move topic definitions into database
Browse files Browse the repository at this point in the history
Store the, name, description,  list of policy sets and debates that
are associated with a topic in the database.
  • Loading branch information
struan committed May 12, 2017
1 parent 48bce07 commit f3c14cf
Show file tree
Hide file tree
Showing 10 changed files with 369 additions and 165 deletions.
153 changes: 153 additions & 0 deletions classes/Topic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php
/**
* Topic
*
* @package TheyWorkForYou
*/

namespace MySociety\TheyWorkForYou;

class Topic {

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

private $raw;
private $id;
private $title;
private $slug;
private $description;

/**
* Constructor
*
*/

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

$this->raw = $data;
$this->id = $data['id'];
$this->title = $data['title'];
$this->slug = $data['slug'];
$this->description = $data['description'];

}


function data() {
return $this->raw;
}

function title() {
return $this->title;
}

function set_title($title) {
$this->title = $title;
}

function slug() {
return $this->slug;
}

function url() {
$url = new \URL('topic');
return $url->generate() . $this->slug;
}

function image() {
$image_name = preg_replace('/-/', '', $this->slug);
return "/images/topic" . $image_name . ".jpg";
}

function description() {
return $this->description;
}

function set_description($description) {
$this->description = $description;
}

function getContent() {
$q = $this->db->query(
"SELECT body, gid FROM epobject ep JOIN hansard h on ep.epobject_id = h.epobject_id
WHERE ep.epobject_id in (
SELECT epobject_id from topic_epobjects WHERE topic_key = :topic_key
)",
array(
':topic_key' => $this->id
)
);

$content = array();
$rows = $q->rows;
for ($i = 0; $i < $rows; $i++) {
$content[] = array(
'title' => $q->field($i, 'body'),
'href' => Utility\Hansard::gid_to_url($q->field($i, 'gid')),
);
}

return $content;
}

function addContent($gid) {
$q = $this->db->query(
"SELECT epobject_id FROM hansard WHERE gid = :gid",
array(
":gid" => $gid
)
);

if (!$q->success() || $q->rows == 0) {
return false;
}

$epobject_id = $q->field(0, 'epobject_id');

$q = $this->db->query(
"INSERT INTO topic_epobjects (topic_key, epobject_id) VALUES (:topic, :ep_id)",
array(
":topic" => $this->id,
":ep_id" => $epobject_id
)
);

return $q->success();
}

function getPolicySets() {
$q = $this->db->query(
"SELECT policyset FROM topic_policysets WHERE topic_key = :key",
array(
':key' => $this->id
)
);

$sets = array();
$count = $q->rows;
for ($i = 0; $i < $count; $i++) {
$sets[] = $q->field($i, 'policyset');
}

return $sets;
}

function save() {
$q = $this->db->query(
"REPLACE INTO topics (id, title, slug, description) VALUES(:id, :title, :slug, :description)",
array(
':id' => $this->id,
':slug' => $this->slug(),
':title' => $this->title(),
':description' => $this->description()
)
);

return $q->success();
}
}
51 changes: 51 additions & 0 deletions classes/Topics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Topics
*
* @package TheyWorkForYou
*/

namespace MySociety\TheyWorkForYou;

class Topics {

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

/**
* Constructor
*
*/

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

public function getTopics() {
$q = $this->db->query("SELECT id, slug, title, description FROM topics");

$topics = array();
$count = $q->rows();

for ($i = 0; $i < $count; $i++ ) {
$topic = $q->row($i);
$topics[$topic['slug']] = new Topic($topic);
}
return $topics;
}

public function getTopic($topic_name) {
$q = $this->db->query(
"SELECT id, slug, title, description FROM topics WHERE slug = :slug",
array(':slug' => $topic_name)
);
if ($q->rows) {
return new Topic($q->row(0));
}

return NULL;
}
}
45 changes: 45 additions & 0 deletions classes/Utility/Hansard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace MySociety\TheyWorkForYou\Utility;

/**
* Hansard Utilities
*
* Utility functions related to content
*/

class Hansard
{
public static function get_gid_from_url($url) {
$gid = NULL;
$parts = parse_url($url);
parse_str($parts['query'], $query);

if ( $query['id'] ) {
if (strpos($parts['path'], 'lords') !== false) {
$gid = 'uk.org.publicwhip/lords/';
} elseif (strpos($parts['path'], 'whall') !== false) {
$gid = 'uk.org.publicwhip/westminhall/';
} else {
$gid = 'uk.org.publicwhip/debate/';
}
$gid .= $query['id'];
}
return $gid;
}


public static function gid_to_url($gid) {
if ( !$gid ) {
return '';
}
global $hansardmajors;
$db = new \ParlDB();

$q = $db->query("SELECT major FROM hansard WHERE gid = :gid", array( ':gid' => $gid ));
$url_gid = fix_gid_from_db($gid);
$url = new \URL($hansardmajors[$q->field(0, 'major')]['page']);
$url->insert(array('id' => $url_gid));
return $url->generate();
}
}
27 changes: 27 additions & 0 deletions db/0011-add-topics-tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
CREATE TABLE `topics` (
`id` int(11) NOT NULL auto_increment,
`slug` varchar(100) NOT NULL,
`title` text NOT NULL,
`description` text,
PRIMARY KEY (`id`),
UNIQUE KEY `slug` (`slug`)
);

CREATE TABLE `topic_policysets` (
`topic_key` int(11) NOT NULL,
`policyset` varchar(30) NOT NULL,
UNIQUE KEY `topic_policyset` (`topic_key`, `policyset`)
);

CREATE TABLE `topic_policies` (
`topic_key` int(11) NOT NULL,
`policy_id` int(11) NOT NULL,
UNIQUE KEY `topic_policy` (`topic_key`, `policy_id`)
);

CREATE TABLE `topic_epobjects` (
`topic_key` int(11) NOT NULL,
`epobject_id` int(11) NOT NULL,
UNIQUE KEY `topic_object` (`topic_key`, `epobject_id`)
);

28 changes: 28 additions & 0 deletions db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,31 @@ CREATE TABLE `editorial` (
PRIMARY KEY `item` (`item`)
);


CREATE TABLE `topics` (
`id` int(11) NOT NULL auto_increment,
`slug` varchar(100) NOT NULL,
`title` text NOT NULL,
`description` text,
PRIMARY KEY (`id`),
UNIQUE KEY `slug` (`slug`)
);

CREATE TABLE `topic_policysets` (
`topic_key` int(11) NOT NULL,
`policyset` varchar(30) NOT NULL,
UNIQUE KEY `topic_policyset` (`topic_key`, `policyset`)
);

CREATE TABLE `topic_policies` (
`topic_key` int(11) NOT NULL,
`policy_id` int(11) NOT NULL,
UNIQUE KEY `topic_policy` (`topic_key`, `policy_id`)
);

CREATE TABLE `topic_epobjects` (
`topic_key` int(11) NOT NULL,
`epobject_id` int(11) NOT NULL,
UNIQUE KEY `topic_object` (`topic_key`, `epobject_id`)
);

9 changes: 4 additions & 5 deletions www/docs/topic/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@

// Disable the old PAGE class.
$new_style_template = TRUE;
global $this_page;
$this_page = 'topics';

// Include all the things this page needs.
include_once '../../includes/easyparliament/init.php';

// Array of topic page names (must exist in metadata.php) and titles to display.
$data['topics'] = array(
'topicbenefits' => 'Benefits',
'topiccrimestats' => 'Crime Statistics',
'topicnhs' => 'NHS'
);
$topics = new Topics();
$data['topics'] = $topics->getTopics();

// Send for rendering!
Renderer::output('topic/list', $data);
Loading

0 comments on commit f3c14cf

Please sign in to comment.