From 506d7603f642ebd732e17b1b13c38ecaeef5d74f Mon Sep 17 00:00:00 2001 From: inghamn Date: Mon, 4 Jan 2010 15:47:15 +0000 Subject: [PATCH] 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 --- access_control.inc | 1 + blocks/html/cemeteries/cemeteryInfo.inc | 53 ++++++ blocks/html/cemeteries/cemeteryList.inc | 6 +- blocks/html/interments/findForm.inc | 6 +- blocks/html/sections/addSectionForm.inc | 31 ++++ blocks/html/sections/updateSectionForm.inc | 33 ++++ classes/Cemetery.php | 2 +- classes/Interment.php | 18 -- classes/Section.php | 199 +++++++++++++++++++++ classes/SectionList.php | 86 +++++++++ html/cemeteries/viewCemetery.php | 2 +- html/sections/addSection.php | 35 ++++ html/sections/updateSection.php | 31 ++++ scripts/migration/import.sql | 28 ++- scripts/mysql.sql | 11 +- 15 files changed, 516 insertions(+), 26 deletions(-) create mode 100644 blocks/html/cemeteries/cemeteryInfo.inc create mode 100644 blocks/html/sections/addSectionForm.inc create mode 100644 blocks/html/sections/updateSectionForm.inc create mode 100644 classes/Section.php create mode 100644 classes/SectionList.php create mode 100644 html/sections/addSection.php create mode 100644 html/sections/updateSection.php diff --git a/access_control.inc b/access_control.inc index 1c69b2a..687dbe3 100644 --- a/access_control.inc +++ b/access_control.inc @@ -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')); /** diff --git a/blocks/html/cemeteries/cemeteryInfo.inc b/blocks/html/cemeteries/cemeteryInfo.inc new file mode 100644 index 0000000..e8eb964 --- /dev/null +++ b/blocks/html/cemeteries/cemeteryInfo.inc @@ -0,0 +1,53 @@ + + * @param Cemetery $this->cemetery + */ +$addButton = ''; +if (userIsAllowed('Sections')) { + $url = BASE_URL.'/sections/addSection.php?cemetery_id='.$this->cemetery->getId(); + $addButton = " + + "; +} + +$name = View::escape($this->cemetery->getName()); +echo " +

$name

+

$addButton Sections

+ + + + + + + + +"; + foreach ($this->cemetery->getSections() as $section) { + $editButton = ''; + if (userIsAllowed('Sections')) { + $url = BASE_URL.'/sections/updateSection.php?section_id='.$section->getId(); + $editButton = " + + "; + } + $code = View::escape($section->getCode()); + $name = View::escape($section->getName()); + echo " + + + + + "; + } +echo " + +
CodeName
$editButton$code$name
+"; diff --git a/blocks/html/cemeteries/cemeteryList.inc b/blocks/html/cemeteries/cemeteryList.inc index bd3a9d6..7fd334d 100644 --- a/blocks/html/cemeteries/cemeteryList.inc +++ b/blocks/html/cemeteries/cemeteryList.inc @@ -31,7 +31,11 @@ "; } $name = View::escape($cemetery->getName()); - echo "
  • $editButton $name
  • "; + echo " +
  • $editButton + getURL()}\">$name +
  • + "; } ?> diff --git a/blocks/html/interments/findForm.inc b/blocks/html/interments/findForm.inc index eb15d1e..57761f0 100644 --- a/blocks/html/interments/findForm.inc +++ b/blocks/html/interments/findForm.inc @@ -30,11 +30,11 @@ "; 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 ""; + $name = View::escape($section); + echo ""; } echo " diff --git a/blocks/html/sections/addSectionForm.inc b/blocks/html/sections/addSectionForm.inc new file mode 100644 index 0000000..b64109f --- /dev/null +++ b/blocks/html/sections/addSectionForm.inc @@ -0,0 +1,31 @@ + + * @param Cemetery $this->cemetery + */ +?> +

    Add Section

    +
    +
    Section Info + + + + + + + + + + + +
    + + + +
    +
    \ No newline at end of file diff --git a/blocks/html/sections/updateSectionForm.inc b/blocks/html/sections/updateSectionForm.inc new file mode 100644 index 0000000..0e0c361 --- /dev/null +++ b/blocks/html/sections/updateSectionForm.inc @@ -0,0 +1,33 @@ + + * @param Section $this->section + */ +?> +

    Update Section

    +
    +
    Section Info + + + + + + + + + + + +
    +
    +
    + + + +
    +
    \ No newline at end of file diff --git a/classes/Cemetery.php b/classes/Cemetery.php index ecb1fb1..1970bc0 100644 --- a/classes/Cemetery.php +++ b/classes/Cemetery.php @@ -162,7 +162,7 @@ class Cemetery */ public function getSections() { - return Interment::getSections($this); + return new SectionList(array('cemetery_id'=>$this->id)); } /** diff --git a/classes/Interment.php b/classes/Interment.php index d6f56e4..fc5213d 100644 --- a/classes/Interment.php +++ b/classes/Interment.php @@ -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 */ diff --git a/classes/Section.php b/classes/Section.php new file mode 100644 index 0000000..88bac07 --- /dev/null +++ b/classes/Section.php @@ -0,0 +1,199 @@ + + */ +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; + } +} diff --git a/classes/SectionList.php b/classes/SectionList.php new file mode 100644 index 0000000..a7525d0 --- /dev/null +++ b/classes/SectionList.php @@ -0,0 +1,86 @@ + + */ +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]); + } +} diff --git a/html/cemeteries/viewCemetery.php b/html/cemeteries/viewCemetery.php index c0ee56a..2624f07 100644 --- a/html/cemeteries/viewCemetery.php +++ b/html/cemeteries/viewCemetery.php @@ -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; diff --git a/html/sections/addSection.php b/html/sections/addSection.php new file mode 100644 index 0000000..b5906f4 --- /dev/null +++ b/html/sections/addSection.php @@ -0,0 +1,35 @@ + + * @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(); \ No newline at end of file diff --git a/html/sections/updateSection.php b/html/sections/updateSection.php new file mode 100644 index 0000000..cb9a109 --- /dev/null +++ b/html/sections/updateSection.php @@ -0,0 +1,31 @@ + + * @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(); \ No newline at end of file diff --git a/scripts/migration/import.sql b/scripts/migration/import.sql index 200c476..c1c42ee 100644 --- a/scripts/migration/import.sql +++ b/scripts/migration/import.sql @@ -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, @@ -18,4 +23,25 @@ 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'; \ No newline at end of file +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; \ No newline at end of file diff --git a/scripts/mysql.sql b/scripts/mysql.sql index aa79e64..b9d8a10 100644 --- a/scripts/mysql.sql +++ b/scripts/mysql.sql @@ -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;