Skip to content

Commit

Permalink
Handle duplicate section numbers in downloads. Fixes #686
Browse files Browse the repository at this point in the history
  • Loading branch information
Bill Hunt committed Mar 21, 2017
1 parent a5315ae commit f333534
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 48 deletions.
49 changes: 22 additions & 27 deletions htdocs/law.php
Expand Up @@ -18,16 +18,6 @@

$edition = new Edition(array('db' => $db));

/*
* Create a new instance of Law.
*/
$law_object = new Law();

if (isset($args['edition_id']))
{
$law_object->edition_id = $args['edition_id'];
}

/*
* Allow multiple laws.
*/
Expand All @@ -43,7 +33,13 @@
{
foreach($args['relational_id'] as $relational_id)
{
$law_object = new Law();
if (isset($args['edition_id']))
{
$law_object->edition_id = $args['edition_id'];
}
$law_object->law_id = filter_var($relational_id, FILTER_SANITIZE_STRING);

$law = $law_object->get_law();
$laws[] = clone($law);
$titles[] = $law->catch_line;
Expand Down Expand Up @@ -282,30 +278,29 @@

//}

/*
* Display links to representational variants of the text of this law.
*/
$body .= '<section id="rep_variant">
<h2>Download</h2>
<ul>';
foreach ($law->formats as $format)
{
$body .= '<li class="file-download file-' . $format['format'] . '">
<a href="' . $format['url'] . '">' . $format['name'] . '</a></li>';
}
$body .= '
</ul>
</section>';


/*
* Indicate the conclusion of the "section" article, which is the container for the text of a
* section of the code.
*/
$body .= '</article>';
}

/*
* Display links to representational variants of the text of this law.
*/
$formats = array('doc' => 'Word doc', 'epub' => 'ePub', 'json' => 'JSON', 'pdf' => 'PDF',
'rtf' => 'Rich Text Format', 'txt' => 'Plain Text', 'xml' => 'XML');
$body .= '<section id="rep_variant">
<h2>Download</h2>
<ul>';
foreach ($laws[0]->formats as $format)
{
$body .= '<li class="file-download file-' . $format['format'] . '">
<a href="' . $format['url'] . '">' . $format['name'] . '</a></li>';
}
$body .= '
</ul>
</section>';

/*
* Establish the $sidebar variable, so that we can append to it in conditionals.
*/
Expand Down
4 changes: 2 additions & 2 deletions includes/class.Law.inc.php
Expand Up @@ -515,9 +515,9 @@ public function get_law()
* Get our edition.
*/
$edition_obj = new Edition();
$this->edition =$edition_obj->find_by_id($this->edition_id);
$this->edition = $edition_obj->find_by_id($this->edition_id);

$law = $this;
$law =& $this;
unset($law->config);

/*
Expand Down
10 changes: 4 additions & 6 deletions includes/class.ParserController.inc.php
Expand Up @@ -1242,17 +1242,15 @@ public function export_structure($parent_id = null)

if ($laws_result !== FALSE && $laws_statement->rowCount() > 0)
{

/*
* Create a new instance of the class that handles information about individual laws.
*/
$law_object = new Law();

/*
* Iterate through every section number, to pass to the Laws class.
*/
while ($section = $laws_statement->fetch(PDO::FETCH_OBJ))
{
/*
* Create a new instance of the class that handles information about individual laws.
*/
$law_object = new Law();

/*
* Instruct the Law class on what, specifically, it should retrieve.
Expand Down
51 changes: 40 additions & 11 deletions includes/class.State-sample.inc.php
Expand Up @@ -904,6 +904,12 @@ public function build_permalink_subsections($edition_id, $parent_id = null)

if(!defined('LAW_LONG_URLS') || LAW_LONG_URLS === FALSE)
{
$token = str_replace(
array(':', '/', '\\'),
array('_', '_', '_'),
$law['section_number']
);

/*
* Current-and-short is the most-preferred (shortest) url.
*/
Expand All @@ -914,8 +920,8 @@ public function build_permalink_subsections($edition_id, $parent_id = null)
':object_type' => 'law',
':relational_id' => $law['id'],
':identifier' => $law['section_number'],
':token' => $structure_token . '/' . $law['section_number'],
':url' => '/' . $law['section_number'] . '/',
':token' => $structure_token . '/' . $token,
':url' => '/' . $token . '/',
':edition_id' => $edition_id,
':permalink' => 0,
':preferred' => 1
Expand All @@ -932,8 +938,8 @@ public function build_permalink_subsections($edition_id, $parent_id = null)
':object_type' => 'law',
':relational_id' => $law['id'],
':identifier' => $law['section_number'],
':token' => $structure_token . '/' . $law['section_number'],
':url' => '/' . $edition->slug . '/' . $law['section_number'] . '/',
':token' => $structure_token . '/' . $token,
':url' => '/' . $edition->slug . '/' . $token . '/',
':edition_id' => $edition_id,
':permalink' => 0,
':preferred' => $preferred
Expand All @@ -952,8 +958,8 @@ public function build_permalink_subsections($edition_id, $parent_id = null)
':object_type' => 'law',
':relational_id' => $law['id'],
':identifier' => $law['section_number'],
':token' => $structure_token . '/' . $law['section_number'],
':url' => '/' . $structure_token . '/' . $law['section_number'] . '/',
':token' => $structure_token . '/' . $token,
':url' => '/' . $structure_token . '/' . $token . '/',
':edition_id' => $edition_id,
':permalink' => 0,
':preferred' => $preferred
Expand All @@ -970,15 +976,14 @@ public function build_permalink_subsections($edition_id, $parent_id = null)
':object_type' => 'law',
':relational_id' => $law['id'],
':identifier' => $law['section_number'],
':token' => $structure_token . '/' . $law['section_number'],
':url' => '/' . $edition->slug . '/' . $structure_token . '/' . $law['section_number'] . '/',
':token' => $structure_token . '/' . $token,
':url' => '/' . $edition->slug . '/' . $structure_token . '/' . $token . '/',
':edition_id' => $edition_id,
':permalink' => 1,
':preferred' => $preferred
);
$this->permalink_obj->create($insert_data);
}

$this->build_permalink_subsections($edition_id, $item['s1_id']);

}
Expand Down Expand Up @@ -1227,14 +1232,37 @@ public function store()
{
$query['history'] = $this->code->history;
}
$query['edition_id'] = $this->edition_id;

/*
* See if this is a duplicate law
*/

$dupe_query = 'SELECT COUNT(*) AS count FROM laws WHERE section = :section AND edition_id = :edition_id';
$dupe_args = array(':section' => $query['section'], ':edition_id' => $query['edition_id']);

$dupe_statement = $this->db->prepare($dupe_query);
$dupe_result = $dupe_statement->execute($dupe_args);

if($dupe_result)
{
$dupe = $dupe_statement->fetch();
if($dupe['count'] > 0) {
if(!$this->code->metadata)
{
$this->code->metadata = new stdClass();
}

$this->code->metadata->dupe_number = $dupe['count'];
}
}

/*
* Create the beginning of the insertion statement.
*/
$sql = 'INSERT INTO laws
SET date_created=now()';
$sql_args = array();
$query['edition_id'] = $this->edition_id;

/*
* Iterate through the array and turn it into SQL.
Expand Down Expand Up @@ -1302,7 +1330,8 @@ public function store()
SET law_id = :law_id,
meta_key = :meta_key,
meta_value = :meta_value,
edition_id = :edition_id';
edition_id = :edition_id,
date_created = NOW()';
$statement = $this->db->prepare($sql);

foreach ($this->code->metadata as $key => $value)
Expand Down
16 changes: 14 additions & 2 deletions includes/plugins/class.Export.inc.php
Expand Up @@ -105,7 +105,13 @@ public function getLawPaths($law, $dir)
$filebase = array_pop($tokens);

$path = join_paths($dir, 'code-' . $this->format, $tokens);
$filename = $filebase . $this->extension;
$filename = $filebase;
if($law->metadata->dupe_number)
{
$filename .= '_' . $law->metadata->dupe_number;
}

$filename .= $this->extension;

return array($path, $filename);
}
Expand Down Expand Up @@ -288,7 +294,13 @@ public function postGetLaw(&$law)
if(isset($law->url))
{
$url = '/downloads/' . $law->edition->slug . '/code-' . $this->format .
'/' . trim($law->url, '/') . $this->extension;
'/' . trim($law->url, '/');

if(isset($law->metadata) && isset($law->metadata->dupe_number))
{
$url .= '_' . $law->metadata->dupe_number;
}
$url .= $this->extension;

$law->formats[] = array(
'name' => $this->public_name,
Expand Down

0 comments on commit f333534

Please sign in to comment.