/**
     * Recurse through all subsections to build permalink data.
     */
    public function build_permalink_subsections($edition_id, $parent_id = null)
    {
        $edition_obj = new Edition(array('db' => $this->db));
        $edition = $edition_obj->find_by_id($edition_id);
        /*
         * If we don't have a parent, set the base url.
         * We only want to do this once.
         */
        if (!isset($parent_id)) {
            /*
             * By default, the actual permalink is preferred.
             */
            $preferred = 1;
            /*
             * If this is the current edition, add links to the urls
             * without the edition slug.  This becomes the preferred
             * link url.
             */
            if ($edition->current) {
                $insert_data = array(':object_type' => 'structure', ':relational_id' => '', ':identifier' => '', ':token' => '', ':url' => '/browse/', ':edition_id' => $edition_id, ':preferred' => $preferred, ':permalink' => 0);
                $this->permalink_obj->create($insert_data);
                $preferred = 0;
            }
            $insert_data = array(':object_type' => 'structure', ':relational_id' => '', ':identifier' => '', ':token' => '', ':url' => '/' . $edition->slug . '/', ':edition_id' => $edition_id, ':preferred' => $preferred, ':permalink' => 1);
            $this->permalink_obj->create($insert_data);
        }
        $structure_sql = 'SELECT structure_unified.*
			FROM structure
			LEFT JOIN structure_unified
				ON structure.id = structure_unified.s1_id
			WHERE structure.edition_id = :edition_id';
        /*
         * We use prepared statements for efficiency.  As a result,
         * we need to keep an array of our arguments rather than
         * hardcoding them in the SQL.
         */
        $structure_args = array(':edition_id' => $edition_id);
        if (isset($parent_id)) {
            $structure_sql .= ' AND parent_id = :parent_id';
            $structure_args[':parent_id'] = $parent_id;
        } else {
            $structure_sql .= ' AND parent_id IS NULL';
        }
        $structure_statement = $this->db->prepare($structure_sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
        $structure_statement->execute($structure_args);
        /*
         * Get results as an array to save memory
         */
        while ($item = $structure_statement->fetch(PDO::FETCH_ASSOC)) {
            /*
             * Figure out the URL for this structural unit by iterating through the "identifier"
             * columns in this row.
             */
            $identifier_parts = array();
            foreach ($item as $key => $value) {
                if (preg_match('/s[0-9]_identifier/', $key) == 1) {
                    /*
                     * Higher-level structural elements (e.g., titles) will have blank columns in
                     * structure_unified, so we want to omit any blank values. Because a valid
                     * structural unit identifier is "0" (Virginia does this), we check the string
                     * length, rather than using empty().
                     */
                    if (strlen($value) > 0) {
                        $identifier_parts[] = urlencode($value);
                    }
                }
            }
            $identifier_parts = array_reverse($identifier_parts);
            $structure_token = implode('/', $identifier_parts);
            /*
             * Insert the structure
             */
            /*
             * By default, the actual permalink is preferred.
             */
            $preferred = 1;
            /*
             * If this is the current edition, add links to the urls
             * without the edition slug.  This becomes the preferred
             * link url.
             */
            if ($edition->current) {
                $insert_data = array(':object_type' => 'structure', ':relational_id' => $item['s1_id'], ':identifier' => $item['s1_identifier'], ':token' => $structure_token, ':url' => '/' . $structure_token . '/', ':edition_id' => $edition_id, ':preferred' => 1, ':permalink' => 0);
                $this->permalink_obj->create($insert_data);
                $preferred = 0;
            }
            /*
             * Insert actual permalinks.
             */
            $insert_data = array(':object_type' => 'structure', ':relational_id' => $item['s1_id'], ':identifier' => $item['s1_identifier'], ':token' => $structure_token, ':url' => '/' . $edition->slug . '/' . $structure_token . '/', ':edition_id' => $edition_id, ':preferred' => $preferred, ':permalink' => 1);
            $this->permalink_obj->create($insert_data);
            /*
             * Now we can use our data to build the child law identifiers
             */
            if (INCLUDES_REPEALED !== TRUE) {
                $laws_sql = '	SELECT id, structure_id, section AS section_number, catch_line
								FROM laws
								WHERE structure_id = :s_id
								AND laws.edition_id = :edition_id
								ORDER BY order_by, section';
            } else {
                $laws_sql = '	SELECT laws.id, laws.structure_id, laws.section AS section_number,
								laws.catch_line
								FROM laws
								LEFT OUTER JOIN laws_meta
									ON laws_meta.law_id = laws.id AND laws_meta.meta_key = "repealed"
								WHERE structure_id = :s_id
								AND (laws_meta.meta_value = "n" OR laws_meta.meta_value IS NULL)
								AND laws.edition_id = :edition_id
								ORDER BY order_by, section';
            }
            $laws_statement = $this->db->prepare($laws_sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
            $laws_sql_args = array(':s_id' => $item['s1_id'], ':edition_id' => $edition_id);
            $laws_statement->execute($laws_sql_args);
            while ($law = $laws_statement->fetch(PDO::FETCH_ASSOC)) {
                /*
                 * Note that we descend from our most-preferred url option
                 * to our least, depending on what flags have been set.
                 */
                $preferred = 1;
                if (!defined('LAW_LONG_URLS') || LAW_LONG_URLS === FALSE) {
                    /*
                     * Current-and-short is the most-preferred (shortest) url.
                     */
                    if ($edition->current) {
                        $insert_data = array(':object_type' => 'law', ':relational_id' => $law['id'], ':identifier' => $law['section_number'], ':token' => $structure_token . '/' . $law['section_number'], ':url' => '/' . $law['section_number'] . '/', ':edition_id' => $edition_id, ':permalink' => 0, ':preferred' => 1);
                        $this->permalink_obj->create($insert_data);
                        $preferred = 0;
                    }
                    /*
                     * If this is not-current, then short is the most-preferred.
                     */
                    $insert_data = array(':object_type' => 'law', ':relational_id' => $law['id'], ':identifier' => $law['section_number'], ':token' => $structure_token . '/' . $law['section_number'], ':url' => '/' . $edition->slug . '/' . $law['section_number'] . '/', ':edition_id' => $edition_id, ':permalink' => 0, ':preferred' => $preferred);
                    $this->permalink_obj->create($insert_data);
                    $preferred = 0;
                }
                /*
                 * Long and current is our third choice.
                 */
                if ($edition->current) {
                    $insert_data = array(':object_type' => 'law', ':relational_id' => $law['id'], ':identifier' => $law['section_number'], ':token' => $structure_token . '/' . $law['section_number'], ':url' => '/' . $structure_token . '/' . $law['section_number'] . '/', ':edition_id' => $edition_id, ':permalink' => 0, ':preferred' => $preferred);
                    $this->permalink_obj->create($insert_data);
                    $preferred = 0;
                }
                /*
                 * Failing everything else, use the super-long url.
                 */
                $insert_data = array(':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'] . '/', ':edition_id' => $edition_id, ':permalink' => 1, ':preferred' => $preferred);
                $this->permalink_obj->create($insert_data);
            }
            $this->build_permalink_subsections($edition_id, $item['s1_id']);
        }
    }
Exemple #2
0
				<h1>Cite As</h1>
				<ul>';
    foreach ($law->citation as $citation) {
        $sidebar .= '<li>' . $citation->label . ': <span class="' . strtolower($citation->label) . '">' . $citation->text . '</span></li>';
    }
    $sidebar .= '</ul>
			</section>';
}
/*
 * End Masonry.js wrapper
 */
$sidebar .= '</section>';
/*
 * Show edition info.
 */
$edition_data = $edition->find_by_id($law->edition_id);
$edition_list = $edition->all();
if ($edition_data && count($edition_list) > 1) {
    $content->set('edition', '<p class="edition">This is the <strong>' . $edition_data->name . '</strong> edition of the code.  ');
    if ($edition_data->current) {
        $content->append('edition', 'This is the current edition.  ');
    } else {
        $content->append('edition', 'There is <strong>not</strong> the current edition.  ');
    }
    if ($edition_data->last_import) {
        $content->append('edition', 'It was last updated ' . date('M d, Y', strtotime($edition_data->last_import)) . '.  ');
    }
    $content->append('edition', '<a href="/editions/?from=' . $_SERVER['REQUEST_URI'] . '" class="edition-link">Browse all editions.</a></p>');
}
$content->set('current_edition', $law->edition_id);
/*
 public function handle_editions($post_data)
 {
     $previous_edition = $this->get_current_edition();
     if ($previous_edition->id !== $this->edition_id) {
         $this->previous_edition_id = $previous_edition->id;
     }
     $errors = array();
     $create_data = array();
     if (!empty($post_data['make_current'])) {
         if ($current = filter_var($post_data['make_current'], FILTER_VALIDATE_INT)) {
             $create_data['current'] = (int) $current;
         } else {
             $errors[] = 'Unexpected value for “make this edition current.”';
         }
     } else {
         $create_data['current'] = 0;
     }
     if ($post_data['edition_option'] == 'new') {
         if ($name = filter_var($post_data['new_edition_name'], FILTER_SANITIZE_STRING)) {
             $create_data['name'] = $name;
         } else {
             $errors[] = 'Please enter a valid edition name.';
         }
         if ($slug = filter_var($post_data['new_edition_slug'], FILTER_SANITIZE_STRING)) {
             $create_data['slug'] = $slug;
         } else {
             $errors[] = 'Please enter a valid edition URL.';
         }
         if (count($errors) === 0) {
             $edition_id = $this->create_edition($create_data);
             if ($edition_id) {
                 $this->edition_id = $edition_id;
             } else {
                 $errors[] = 'Unable to create edition.';
             }
         }
     } elseif ($post_data['edition_option'] == 'existing') {
         if ($edition_id = filter_var($post_data['edition'], FILTER_VALIDATE_INT)) {
             $this->edition_id = $edition_id;
             if ($create_data['current'] > 0) {
                 $create_data['id'] = $this->edition_id;
                 $this->update_edition($create_data);
             }
         } else {
             $errors[] = 'Please select an edition to update.';
         }
     } else {
         $errors[] = 'Please select if you would like to create a new edition or ' . 'update an existing one.';
     }
     if (isset($this->edition_id)) {
         /*
          * Get the edition from the database and store a copy locally.
          */
         $edition = new Edition();
         $edition_result = $edition->find_by_id($this->edition_id);
         if ($edition_result !== FALSE) {
             $this->edition = $edition_result;
             // Write the EDITION_ID
             if ($this->edition->current) {
                 $edition->unset_current($this->edition_id);
                 $this->export_edition_id($this->edition_id);
             }
         } else {
             $errors[] = 'The edition could not be found.';
         }
     }
     return $errors;
 }
 * If this isn't the canonical page, show a canonical meta tag.
 */
$permalink_obj = new Permalink(array('db' => $db));
$permalink = $permalink_obj->get_permalink($struct->structure_id, 'structure', $struct->edition_id);
if ($args['url'] !== $permalink->url) {
    $content->append('meta_tags', '<link rel="canonical" href="' . $permalink->url . '" />');
}
/*
 * Put the shorthand $body variable into its proper place.
 */
$content->set('body', $body);
unset($body);
/*
 * Show edition info.
 */
$edition_data = $edition->find_by_id($struct->edition_id);
$edition_list = $edition->all();
if ($edition_data && count($edition_list) > 1) {
    $content->set('edition', '<p class="edition">This is the <strong>' . $edition_data->name . '</strong> edition of the code.  ');
    if ($edition_data->current) {
        $content->append('edition', 'This is the current edition.  ');
    } else {
        $content->append('edition', 'There is <strong>not</strong> the current edition.  ');
    }
    if ($edition_data->last_import) {
        $content->append('edition', 'It was last updated ' . date('M d, Y', strtotime($edition_data->last_import)) . '.  ');
    }
    $content->append('edition', '<a href="/editions/?from=' . $_SERVER['REQUEST_URI'] . '" class="edition-link">Browse all editions.</a></p>');
}
$content->set('current_edition', $struct->edition_id);
/*
     $body .= $result->catch_line;
 } else {
     $body .= $law->catch_line;
 }
 $body .= ' (' . SECTION_SYMBOL . '&nbsp;';
 if (strlen($result->section_number)) {
     $body .= $result->section_number;
 } else {
     $body .= $law->section_number;
 }
 $body .= ')</a></h1>';
 /*
  * If we're searching all editions, show what edition this law is from.
  */
 if (!strlen($edition_id)) {
     $law_edition = $edition->find_by_id($law->edition_id);
     $body .= '<div class="edition_heading edition">' . $law_edition->name . '</div>';
 }
 /*
  * Display this law's structural ancestry as a breadcrumb trail.
  */
 $body .= '<div class="breadcrumbs"><ul>';
 foreach (array_reverse((array) $law->ancestry) as $structure) {
     $body .= '<li><a href="' . $structure->url . '">' . $structure->identifier . ' ' . $structure->name . '</a></li>';
 }
 $body .= '</ul></div>';
 /*
  * Attempt to display a snippet of the indexed law, highlighting the use of the search
  * terms within that text.
  */
 if (isset($result->highlight) && !empty($result->highlight)) {