-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move topic definitions into database
Store the, name, description, list of policy sets and debates that are associated with a topic in the database.
- Loading branch information
Showing
10 changed files
with
369 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) | ||
); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.