Anthem Class

Here is the Anthem class. It may run out to the right a bit.


<?php
/*
##################################################
#
# Filename..........: $RCSfile: Anthem.php,v $
# Original Author...: Anthony L. Awtrey
# Version...........: $Revision: 0.1 $
# Last Modified By..: $Author: aawtrey $
# Last Modified.....: $Date: 2006/09/21 18:15:56 $
#
# Copyright 2006 Anthony Awtrey
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
*/

/*
 * This class provides the mechanisms to manage the data in the account
 * database.
 */
class Anthem {

  var 
$page;
  var 
$data;
  var 
$error_string;

  
/*
   * Class initialization
   */
  
function __construct() {
    global 
$_SERVER;
    global 
$_SESSION;
    global 
$_REQUEST;
    global 
$_POST;
    global 
$_GET;
    
$this->page              = new Page();
    
$this->page->title       "Anthem Library";
    
$this->page->description "This is the anthem library.";
    
$this->data              = new Data("anthem");
  }

  function 
__destruct()
  {
  }

  function 
generate_alpha_links($current_link) {
    
$alpha = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
    
$result '<p class="center">['."n";
    foreach ( 
$alpha as $letter ) {
      if ( 
$letter == $current_link ) {
        
$result .= strtoupper($letter)." n";
      } else {
        
$result .= '<a href=index.php?alpha='.$letter.'>'.strtoupper($letter)."</a> n";
      }
    }
    
$result .= ']</p>'."n";
    return(
$result);
  }

  function 
generate_anthem_table($anthems) {
    
$result  "<table>n";
    
$result .= "  <thead>n";
    
$result .= "    <tr>n";
    
$result .= "      <th>Title</th>n";
    
$result .= "      <th>Composer</th>n";
    
$result .= "      <th>Author</th>n";
    
$result .= "    </tr>n";
    
$result .= "  </thead>n";
    
$result .= "  <tbody>n";
    
$count 1;
    foreach (
$anthems as $key => $val) {
      
$result .= "  <tr>n";
      
$result .= '    <td><a href="index.php?id=' $val['id'] . '">' $val['title'] . "</a></td>n";
      
$result .= '    <td>' $val['composer'] . "</td>n";
      
$result .= '    <td>' $val['author'] . "</td>n";
      
$result .= "  </tr>n";
      
$count++;
    }
    
$result .= "  </tbody>n";
    
$result .= "</table>n";
    return(
$result);
  }

  
/*
   * This method lists all the anthems with a given starting character in the title
   */
  
function print_anthems() {
    if ( !empty(
$_GET['search']) ) {
      
$search $_GET['search'];
      if ( !empty(
$_GET['type']) && $_GET['type'] == 'author' ) {
        
$sql 'SELECT * FROM anthem WHERE author LIKE "%'.$search.'%" ORDER BY title';
        
$achecked 'checked';
      } elseif ( !empty(
$_GET['type']) && $_GET['type'] == 'composer' ) {
        
$sql 'SELECT * FROM anthem WHERE composer LIKE "%'.$search.'%" ORDER BY title';
        
$cchecked 'checked';
      } else {
        
$sql 'SELECT * FROM anthem WHERE lyrics LIKE "%'.$search.'%" OR title LIKE "%'.$search.'%" ORDER BY title';
        
$tchecked 'checked';
      }
    } elseif ( !empty(
$_GET['alpha']) ) {
      
$alpha $_GET['alpha'];
      
$sql 'SELECT * FROM anthem WHERE title LIKE "'.$alpha.'%" ORDER BY title';
      
$tchecked 'checked';
    } else {
      
$alpha 'a';
      
$sql 'SELECT * FROM anthem WHERE title LIKE "'.$alpha.'%" ORDER BY title';
      
$tchecked 'checked';
    }
    
$anthems $this->data->return_array($sql);
    
$anthem_count $this->data->count_records();
    
$this->page->content .= "<p>n";
    
$this->page->content .= '  <div class="left">Here are '.count($anthems).' of '.$anthem_count.' anthems.</div>'."n";
    
$this->page->content .= '  <div class="right"><form action="index.php" method="GET">';
    
$this->page->content .= '<input type="text" size="30" maxlength="30" name="search" id="search" value="'.$search.'">';
    
$this->page->content .= '<input type="submit" value="Search"><br />';
    
$this->page->content .= '<input type="radio" name="type" value="lyrics" '.$tchecked.' /> Title and Lyrics ';
    
$this->page->content .= '<input type="radio" name="type" value="composer" '.$cchecked.' /> Composer ';
    
$this->page->content .= '<input type="radio" name="type" value="author" '.$achecked.' /> Author';
    
$this->page->content .= '</form></div>'."n";
    
$this->page->content .= "</p>n";
    if ( 
$_SESSION['login'] ) {
      
$this->page->content .= "<p>n";
      
$this->page->content .= "<a href="index.php?action=add">Add an anthem</a>n";
      
$this->page->content .= "</p>n";
    }
    
$this->page->content .= $this->generate_alpha_links($alpha);
    
$this->page->content .= $this->generate_anthem_table($anthems);
    
$this->page->content .= $this->generate_alpha_links($alpha);
    
$this->page->display();
  }

  function 
print_anthem() {
    
$anthem $this->data->get_record_by_id($_GET['id']);
    
$this->page->title       $anthem['title'];
    
$this->page->description $anthem['title'];
    
$this->page->content .= "<p>n";
    if ( !empty(
$anthem['composer']) )      { $this->page->content .= 'Composer: ' .           $anthem['composer']      . "<br />n"; }
    if ( !empty(
$anthem['author']) )        { $this->page->content .= 'Author: ' .             $anthem['author']        . "<br />n"; }
    if ( !empty(
$anthem['arranger']) )      { $this->page->content .= 'Arranger: ' .           $anthem['arranger']      . "<br />n"; }
    if ( !empty(
$anthem['publisher']) )     { $this->page->content .= 'Publisher: ' .          $anthem['publisher']     . "<br />n"; }
    if ( !empty(
$anthem['scripture']) )     { $this->page->content .= 'Scripture Allusion: <a href="../bible/?verse='.urlencode($anthem['scripture']).'">' $anthem['scripture'] . "</a><br />n"; }
    if ( !empty(
$anthem['voicing']) )       { $this->page->content .= 'Voicing: ' .            $anthem['voicing']       . "<br />n"; }
    if ( !empty(
$anthem['pages']) )         { $this->page->content .= 'Pages: ' .              $anthem['pages']         . "<br />n"; }
    if ( !empty(
$anthem['length']) )        { $this->page->content .= 'Length: ' .             $anthem['length']        . "<br />n"; }
    if ( !empty(
$anthem['copies']) )        { $this->page->content .= 'Copies: ' .             $anthem['copies']        . "<br />n"; }
    if ( !empty(
$anthem['publish_date']) )  { $this->page->content .= 'Publish Date:  ' .      $anthem['publish_date']  . "<br />n"; }
    if ( !empty(
$anthem['purchase_date']) ) { $this->page->content .= 'Purchase Date: ' .      $anthem['purchase_date'] . "<br />n"; }
    if ( !empty(
$anthem['lyrics']) ) { 
      
$this->page->content .= "</p>n";
      
$this->page->content .= "<h2>Lyrics</h2>n";
      
$this->page->content .= "<p>n";
      
$this->page->content .= nl2br($anthem['lyrics']) . "<br />n";
      
$this->page->content .= "</p>n";
    }
    if ( !empty(
$anthem['notes']) ) { 
      
$this->page->content .= "<h2>Notes</h2>n";
      
$this->page->content .= "<p>n";
      
$this->page->content .= nl2br($anthem['notes']) . "<br />n";
      
$this->page->content .= "</p>n";
    }
    if ( 
$_SESSION['login'] ) {
      
$this->page->content .= "<p>n";
      
$this->page->content .= "[<a href="index.php?id=" $anthem['id'] . "&action=edit">Edit this anthem</a>]n";
      
$this->page->content .= "[<a href="index.php?id=" $anthem['id'] . "&action=delete">Delete this anthem</a>]n";
      
$this->page->content .= "</p>n";
    }
    
$this->page->display();
  }

  
/*
   * This method prints the add/edit confirmation form
   */
  
function print_anthem_form() {
    if ( 
$anthem['action'] == 'add'  || $_GET['action'] == 'add' ) {
      
$action 'add';
    } elseif ( 
$anthem['action'] == 'edit' || $_GET['action'] == 'edit' ) {
      list( 
$anthem['inId'],
            
$anthem['inAccount'],
            
$anthem['inTitle'],
            
$anthem['inComposer'],
            
$anthem['inAuthor'],
            
$anthem['inArranger'],
            
$anthem['inPublisher'],
            
$anthem['inScripture'],
            
$anthem['inVoicing'],
            
$anthem['inPages'],
            
$anthem['inLength'],
            
$anthem['inCopies'],
            
$anthem['inPublish_date'],
            
$anthem['inPurchase_date'],
            
$anthem['inLyrics'],
            
$anthem['inNotes']
          ) = 
array_values($this->data->get_record_by_id($_GET['id']));
      
$action 'edit';
    } else {
      
redirect('./'.$_POST['backto']);
    }
    
$this->page->title       ucwords($action) . " Anthem";
    
$this->page->description ucwords($action) . "ing Anthem.";
    if (!
$_SESSION['login'] ) { redirect($anthem['inId']); }
    if (!
$anthem['inAccount'])       { $anthem['inAccount']       = $_SESSION['login']; }
    if (
$this->error_string)         { $this->page->content .= "<p class="error">" $this->error_string "</p>n"; }
    require_once(
'Form.php');
    
$form = new Form('index.php','post');
    
$form->hidden('action',$action);
    
$form->hidden('inId',$anthem['inId']);
    
$form->hidden('inAccount',$anthem['inAccount']);
    
$form->text('Title','inTitle',$anthem['inTitle'],60,200);
    
$form->text('Composer','inComposer',$anthem['inComposer'],60,200);
    
$form->text('Author','inAuthor',$anthem['inAuthor'],60,200);
    
$form->text('Arranger','inArranger',$anthem['inArranger'],60,200);
    
$form->text('Publisher','inPublisher',$anthem['inPublisher'],60,200);
    
$form->text('Scripture','inScripture',$anthem['inScripture'],60,200);
    
$form->text('Voicing','inVoicing',$anthem['inVoicing'],60,200);
    
$form->text('Length in Pages','inPages',$anthem['inPages'],60,6);
    
$form->text('Length in Seconds','inLength',$anthem['inLength'],60,6);
    
$form->text('Copies','inCopies',$anthem['inCopies'],60,6);
    
$form->text('Published Date','inPublish_date',$anthem['inPublish_date'],60,32);
    
$form->text('Purchase Date','inPurchase_date',$anthem['inPurchase_date'],60,32);
    
$form->textarea('Lyrics','inLyrics',$anthem['inLyrics'],10,80);
    
$form->textarea('Notes','inNotes',$anthem['inNotes'],10,80);
    
$form->submit('submit',ucwords($action));
    
$form->submit('submit','Cancel');
    
$this->page->content .= $form->render();
    
$this->page->display();
  }

  
/*
   * This method prints a delete confirmation form
   */
  
function print_anthem_delete_confirmation($id) {
    
$anthem $this->data->get_record_by_id($id);
    
$this->page->title       "Delete Anthem";
    
$this->page->description "Anthem deletion confirmation";
    
$this->page->content     .= "<p class="error">n";
    
$this->page->content     .= "<b>Warning!</b> You are about to delete the anthem entitled:<br /><br />n";
    
$this->page->content     .= """ $anthem['title'] . ""<br /><br />n";
    
$this->page->content     .= "Click 'Delete' again if you are sure.n";
    
$this->page->content     .= "</p>n";
    require_once(
'Form.php');
    
$form = new Form('index.php','post');
    
$form->hidden('action','delete');
    
$form->hidden('inId',$anthem['id']);
    
$form->submit('submit','Delete');
    
$form->submit('submit','Cancel');
    
$this->page->content     .= $form->render();
    
$this->page->display();
  }

  
/*
   * This is the method that processing POST/GET events for this object.
   */
  
function run() {
    if ( 
$_SERVER["REQUEST_METHOD"] == 'POST' ) {
      if ( ! 
$_SESSION['login'] ) { redirect($_POST['inId']); }
      if ( 
$_POST['submit'] == 'Cancel' ) {
        
redirect('./'.$_POST['backto']);
      }
      if       ( 
$_POST['action'] == 'add' || $_POST['action'] == 'edit' ) {
        if ( 
$_POST['inAccount'] && $_POST['inTitle'] ) {
          
$this->data->set_attribute('id',            $_POST['inId']);
          
$this->data->set_attribute('account',       $_POST['inAccount']);
          
$this->data->set_attribute('title',         $_POST['inTitle']);
          
$this->data->set_attribute('composer',      $_POST['inComposer']);
          
$this->data->set_attribute('author',        $_POST['inAuthor']);
          
$this->data->set_attribute('arranger',      $_POST['inArranger']);
          
$this->data->set_attribute('publisher',     $_POST['inPublisher']);
          
$this->data->set_attribute('scripture',     $_POST['inScripture']);
          
$this->data->set_attribute('voicing',       $_POST['inVoicing']);
          
$this->data->set_attribute('pages',         $_POST['inPages']);
          
$this->data->set_attribute('length',        $_POST['inLength']);
          
$this->data->set_attribute('copies',        $_POST['inCopies']);
          
$this->data->set_attribute('publish_date',  $_POST['inPublish_date']);
          
$this->data->set_attribute('purchase_date'$_POST['inPurchase_date']);
          
$this->data->set_attribute('lyrics',        $_POST['inLyrics']);
          
$this->data->set_attribute('notes',         $_POST['inNotes']);
          if ( 
$this->data->save() ) {
            
$url '';
            if ( !empty(
$_POST['inId']) ) {
              
$url '?id='.$_POST['inId'];
            } else {
              
$url '?alpha='.strtolower($_POST['inTitle'][0]);
            }
            
redirect("./index.php".$url);
          } else {
            
$this->page->title   "An Error Occurred!";
            
$this->page->title   "An error occurred when trying to save a comment.";
            
$this->page->content "Unable to save anthem!";
            
$this->page->display();
          }
        } else {
          
$this->error_string "You must provide input for each field!n";
          
$this->print_anthem_form($_POST);
        }
      } elseif ( 
$_POST['action'] == 'delete' ) {
        if ( 
$this->data->delete_record_by_id($_POST['inId']) ) {
          
redirect('./');
        } else {
          
$this->page->title   "An Error Occurred!";
          
$this->page->title   "An error occurred when trying to delete an anthem.";
          
$this->page->content "Unable to delete anthem!";
          
$this->page->display();
        }
      }
    } elseif ( 
$_GET['action'] == 'add'    && $_SESSION['login'] ) {
      
$this->print_anthem_form();
    } elseif ( 
$_GET['action'] == 'edit'   && $_SESSION['login'] && isset($_GET['id']) ) {
      
$this->print_anthem_form();
    } elseif ( 
$_GET['action'] == 'delete' && $_SESSION['login'] && isset($_GET['id']) ) {
      
$this->print_anthem_delete_confirmation($_GET['id']);
    } elseif ( isset(
$_GET['id']) ) {
      
$this->print_anthem();
    } else {
      
$this->print_anthems($_GET['alpha']);
    }
  }

}

?>