Sections now have their own table, so we can give them custom names

git-svn-id: https://rosehill.googlecode.com/svn/branches/php@17 100bd78a-fc82-11de-b5bc-ffd2847a4b57
This commit is contained in:
inghamn 2010-01-04 15:47:15 +00:00
parent 7dfa607734
commit 506d7603f6
15 changed files with 516 additions and 26 deletions

View File

@ -21,6 +21,7 @@ $ZEND_ACL->add(new Zend_Acl_Resource('Users'));
$ZEND_ACL->add(new Zend_Acl_Resource('Deeds'));
$ZEND_ACL->add(new Zend_Acl_Resource('Interments'));
$ZEND_ACL->add(new Zend_Acl_Resource('Cemeteries'));
$ZEND_ACL->add(new Zend_Acl_Resource('Sections'));
/**

View File

@ -0,0 +1,53 @@
<?php
/**
* @copyright 2010 City of Bloomington, Indiana
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* @author Cliff Ingham <inghamn@bloomington.in.gov>
* @param Cemetery $this->cemetery
*/
$addButton = '';
if (userIsAllowed('Sections')) {
$url = BASE_URL.'/sections/addSection.php?cemetery_id='.$this->cemetery->getId();
$addButton = "
<button type=\"button\" class=\"add\" onclick=\"document.location.href='$url';\">
Add a section
</button>
";
}
$name = View::escape($this->cemetery->getName());
echo "
<h1>$name</h1>
<h2>$addButton Sections</h2>
<table>
<thead>
<tr><th></th>
<th>Code</th>
<th>Name</th>
</tr>
</thead>
<tbody>
";
foreach ($this->cemetery->getSections() as $section) {
$editButton = '';
if (userIsAllowed('Sections')) {
$url = BASE_URL.'/sections/updateSection.php?section_id='.$section->getId();
$editButton = "
<button type=\"button\" class=\"edit\" onclick=\"document.location.href='$url';\">
Edit
</button>
";
}
$code = View::escape($section->getCode());
$name = View::escape($section->getName());
echo "
<tr><td>$editButton</td>
<td>$code</td>
<td>$name</td>
</tr>
";
}
echo "
</tbody>
</table>
";

View File

@ -31,7 +31,11 @@
";
}
$name = View::escape($cemetery->getName());
echo "<li>$editButton $name</li>";
echo "
<li>$editButton
<a href=\"{$cemetery->getURL()}\">$name</a>
</li>
";
}
?>
</ul>

View File

@ -30,11 +30,11 @@
<option value=\"\">All</option>
";
foreach ($cemetery->getSections() as $section) {
$section = View::escape($section);
$selected = (isset($_GET[$fieldname]) && $_GET[$fieldname]==$section)
$selected = (isset($_GET[$fieldname]) && $_GET[$fieldname]==$section->getId())
? 'selected="selected"'
: '';
echo "<option $selected>$section</option>";
$name = View::escape($section);
echo "<option value=\"{$section->getId()}\" $selected>$name</option>";
}
echo "
</select>

View File

@ -0,0 +1,31 @@
<?php
/**
* @copyright 2010 City of Bloomington, Indiana
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* @author Cliff Ingham <inghamn@bloomington.in.gov>
* @param Cemetery $this->cemetery
*/
?>
<h1>Add Section</h1>
<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<fieldset><legend>Section Info</legend>
<input type="hidden" name="cemetery_id" value="<?php echo $this->cemetery->getId(); ?>" />
<table>
<tr><td><label for="code" class="required">Code</label></td>
<td><input name="code" id="code" size="5" maxlength="5" /></td>
</tr>
<tr><td><label for="name">Name</label></td>
<td><input name="name" id="name" /></td>
</tr>
</table>
<button type="submit" class="submit">Submit</button>
<button type="button" class="cancel"
onclick="document.location.href='<?php echo $this->cemetery->getURL(); ?>';">
Cancel
</button>
</fieldset>
</form>

View File

@ -0,0 +1,33 @@
<?php
/**
* @copyright 2009 City of Bloomington, Indiana
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* @author Cliff Ingham <inghamn@bloomington.in.gov>
* @param Section $this->section
*/
?>
<h1>Update Section</h1>
<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<fieldset><legend>Section Info</legend>
<input name="section_id" type="hidden" value="<?php echo $this->section->getId(); ?>" />
<table>
<tr><td><label for="code" class="required">Code</label></td>
<td><input name="code" id="code" value="<?php echo View::escape($this->section->getCode()); ?>" />
</td>
</tr>
<tr><td><label for="name">Name</label></td>
<td><input name="name" id="name" value="<?php echo View::escape($this->section->getName()); ?>" />
</td>
</tr>
</table>
<button type="submit" class="submit">Submit</button>
<button type="button" class="cancel"
onclick="document.location.href='<?php echo $this->section->getCemetery()->getURL(); ?>';">
Cancel
</button>
</fieldset>
</form>

View File

@ -162,7 +162,7 @@ class Cemetery
*/
public function getSections()
{
return Interment::getSections($this);
return new SectionList(array('cemetery_id'=>$this->id));
}
/**

View File

@ -440,24 +440,6 @@ class Interment
return BASE_URL.'/interments/viewInterment.php?interment_id='.$this->id;
}
/**
* Returns the list of available sections
*
* @return array
*/
public static function getSections(Cemetery $cemetery=null)
{
$zend_db = Database::getConnection();
$select = new Zend_Db_Select($zend_db);
$select->distinct()->from('interments','section');
$select->where('section is not null');
if ($cemetery) {
$select->where('cemetery_id=?',$cemetery->getId());
}
$select->order(array('section'));
return $zend_db->fetchCol($select);
}
/**
* @return string
*/

199
classes/Section.php Normal file
View File

@ -0,0 +1,199 @@
<?php
/**
* @copyright 2009 City of Bloomington, Indiana
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* @author Cliff Ingham <inghamn@bloomington.in.gov>
*/
class Section
{
private $id;
private $cemetery_id;
private $code;
private $name;
private $cemetery;
/**
* Populates the object with data
*
* Passing in an associative array of data will populate this object without
* hitting the database.
*
* Passing in a scalar will load the data from the database.
* This will load all fields in the table as properties of this class.
* You may want to replace this with, or add your own extra, custom loading
*
* @param int|array $id
*/
public function __construct($id=null)
{
if ($id) {
if (is_array($id)) {
$result = $id;
}
else {
$zend_db = Database::getConnection();
$sql = 'select * from sections where id=?';
$result = $zend_db->fetchRow($sql,array($id));
}
if ($result) {
foreach ($result as $field=>$value) {
if ($value) {
$this->$field = $value;
}
}
}
else {
throw new Exception('sections/unknownSection');
}
}
else {
// This is where the code goes to generate a new, empty instance.
// Set any default values for properties that need it here
}
}
/**
* Throws an exception if anything's wrong
* @throws Exception $e
*/
public function validate()
{
// Check for required fields here. Throw an exception if anything is missing.
if (!$this->cemetery_id || !$this->code) {
throw new Excepction('missingRequiredFields');
}
}
/**
* Saves this record back to the database
*/
public function save()
{
$this->validate();
$data = array();
$data['cemetery_id'] = $this->cemetery_id;
$data['code'] = $this->code;
$data['name'] = $this->name ? $this->name : null;
if ($this->id) {
$this->update($data);
}
else {
$this->insert($data);
}
}
private function update($data)
{
$zend_db = Database::getConnection();
$zend_db->update('sections',$data,"id='{$this->id}'");
}
private function insert($data)
{
$zend_db = Database::getConnection();
$zend_db->insert('sections',$data);
$this->id = $zend_db->lastInsertId('sections','id');
}
//----------------------------------------------------------------
// Generic Getters
//----------------------------------------------------------------
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return int
*/
public function getCemetery_id()
{
return $this->cemetery_id;
}
/**
* @return string
*/
public function getCode()
{
return $this->code;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return Cemetery
*/
public function getCemetery()
{
if ($this->cemetery_id) {
if (!$this->cemetery) {
$this->cemetery = new Cemetery($this->cemetery_id);
}
return $this->cemetery;
}
return null;
}
//----------------------------------------------------------------
// Generic Setters
//----------------------------------------------------------------
/**
* @param int $int
*/
public function setCemetery_id($int)
{
$this->cemetery = new Cemetery($int);
$this->cemetery_id = $int;
}
/**
* @param string $string
*/
public function setCode($string)
{
$this->code = trim($string);
}
/**
* @param string $string
*/
public function setName($string)
{
$this->name = trim($string);
}
/**
* @param Cemetery $cemetery
*/
public function setCemetery($cemetery)
{
$this->cemetery_id = $cemetery->getId();
$this->cemetery = $cemetery;
}
//----------------------------------------------------------------
// Custom Functions
// We recommend adding all your custom code down here at the bottom
//----------------------------------------------------------------
public function __toString()
{
return $this->name ? $this->name : $this->code;
}
}

86
classes/SectionList.php Normal file
View File

@ -0,0 +1,86 @@
<?php
/**
* A collection class for Section objects
*
* This class creates a zend_db select statement.
* ZendDbResultIterator handles iterating and paginating those results.
* As the results are iterated over, ZendDbResultIterator will pass each desired
* row back to this class's loadResult() which will be responsible for hydrating
* each Section object
*
* Beyond the basic $fields handled, you will need to write your own handling
* of whatever extra $fields you need
*
* @copyright 2010 City of Bloomington, Indiana
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* @author Cliff Ingham <inghamn@bloomington.in.gov>
*/
class SectionList extends ZendDbResultIterator
{
/**
* Creates a basic select statement for the collection.
*
* Populates the collection if you pass in $fields
* Setting itemsPerPage turns on pagination mode
* In pagination mode, this will only load the results for one page
*
* @param array $fields
* @param int $itemsPerPage Turns on Pagination
* @param int $currentPage
*/
public function __construct($fields=null,$itemsPerPage=null,$currentPage=null)
{
parent::__construct($itemsPerPage,$currentPage);
if (is_array($fields)) {
$this->find($fields);
}
}
/**
* Populates the collection
*
* @param array $fields
* @param string|array $order Multi-column sort should be given as an array
* @param int $limit
* @param string|array $groupBy Multi-column group by should be given as an array
*/
public function find($fields=null,$order='code',$limit=null,$groupBy=null)
{
$this->select->from('sections');
// Finding on fields from the sections table is handled here
if (count($fields)) {
foreach ($fields as $key=>$value) {
$this->select->where("$key=?",$value);
}
}
// Finding on fields from other tables requires joining those tables.
// You can handle fields from other tables by adding the joins here
// If you add more joins you probably want to make sure that the
// above foreach only handles fields from the sections table.
$this->select->order($order);
if ($limit) {
$this->select->limit($limit);
}
if ($groupBy) {
$this->select->group($groupBy);
}
$this->populateList();
}
/**
* Hydrates all the Section objects from a database result set
*
* This is a callback function, called from ZendDbResultIterator. It is
* called once per row of the result.
*
* @param int $key The index of the result row to load
* @return Section
*/
protected function loadResult($key)
{
return new Section($this->result[$key]);
}
}

View File

@ -10,7 +10,7 @@ try {
if (!isset($_GET['cemetery_id']) || !$_GET['cemetery_id']) {
throw new Exception('cemeteries/unknownCemetery');
}
$cemetery = new Cemeter($_GET['cemetery_id']);
$cemetery = new Cemetery($_GET['cemetery_id']);
}
catch (Exception $e) {
$_SESSION['errorMessages'][] = $e;

View File

@ -0,0 +1,35 @@
<?php
/**
* @copyright 2009 City of Bloomington, Indiana
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* @author Cliff Ingham <inghamn@bloomington.in.gov>
* @param REQUEST cemetery_id
*/
if (!userIsAllowed('Sections')) {
$_SESSION['errorMessages'][] = new Exception('noAccessAllowed');
header('Location: '.BASE_URL.'/sections');
exit();
}
$cemetery = new Cemetery($_REQUEST['cemetery_id']);
if (isset($_POST['section'])) {
$section = new Section();
$section->setCemetery($cemetery);
$section->setCode($_POST['code']);
$section->setName($_POST['name']);
try {
$section->save();
header('Location: '.$cemetery->getURL());
exit();
}
catch (Exception $e) {
$_SESSION['errorMessages'][] = $e;
}
}
$template = new Template();
$template->blocks[] = new Block('sections/addSectionForm.inc',array('cemetery'=>$cemetery));
echo $template->render();

View File

@ -0,0 +1,31 @@
<?php
/**
* @copyright 2010 City of Bloomington, Indiana
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* @author Cliff Ingham <inghamn@bloomington.in.gov>
* @param GET section_id
*/
if (!userIsAllowed('Sections')) {
$_SESSION['errorMessages'][] = new Exception('noAccessAllowed');
header('Location: '.BASE_URL.'/sections');
exit();
}
$section = new Section($_REQUEST['section_id']);
if (isset($_POST['section'])) {
$section->setCode($_POST['code']);
$section->setName($_POST['name']);
try {
$section->save();
header('Location: '.$section->getCemetery()->getURL());
exit();
}
catch (Exception $e) {
$_SESSION['errorMessages'][] = $e;
}
}
$template = new Template();
$template->blocks[] = new Block('sections/updateSectionForm.inc',array('section'=>$section));
echo $template->render();

View File

@ -1,3 +1,8 @@
insert cemeteries set id=1,name='White Oak';
insert cemeteries set id=2,name='Rose Hill';
insert deeds (id,section,lot,lastname1,firstname1,middleInitial1,
lastname2,firstname2,middleInitial2,issueDate,notes,lot2,cemetery_id)
select r.ID,r.SEC,r.LOT,r.LNAME1,r.FNAME1,r.MI1,
@ -19,3 +24,24 @@ left join cemeteries c on r.whiteoak=substr(c.name,1,1);
-- A little bit of cleanup on the data
update interments set section='P.G.' where section='P.G';
update interments set section=null where section='0';
insert sections (code,cemetery_id)
select distinct section,1 from interments
where cemetery_id=1 and section is not null;
insert sections (code,cemetery_id)
select distinct section,2 from interments
where cemetery_id=2 and section is not null;
alter table interments add section_id int unsigned after section;
alter table interments add foreign key (section_id) references sections(id);
update interments,sections
set section_id=sections.id
where interments.section=sections.code
and interments.cemetery_id=sections.cemetery_id;
alter table interments drop section;

View File

@ -38,6 +38,14 @@ create table cemeteries (
googleMapURL varchar(255)
) engine=InnoDB;
create table sections (
id int unsigned not null primary key auto_increment,
cemetery_id int unsigned not null,
code varchar(5) not null,
name varchar(128),
foreign key (cemetery_id) references cemeteries(id)
) engine=InnoDB;
create table deeds (
id int unsigned not null primary key auto_increment,
section varchar(5),
@ -57,7 +65,7 @@ create table deeds (
create table interments (
id int(11) unsigned not null primary key auto_increment,
section varchar(5),
section_id int unsigned,
lot varchar(5),
book varchar(4),
pageNumber varchar(5),
@ -72,5 +80,6 @@ create table interments (
cemetery_id int unsigned,
notes text,
lot2 varchar(5),
foreign key (section_id) references sections(id),
foreign key (cemetery_id) references cemeteries(id)
) engine=InnoDB;