The abstract class is something like this:
abstract class Control
{
abstract protected function amendRecord();
abstract protected function createRecord();
abstract protected function deleteRecord($id);
abstract protected function deleteSelected(array $id_array);
abstract protected function loadForm($sub_op);
protected $date_fields;
protected $retrieve_order;
protected $form; /* The data for HTML form to update/create the object */
public $table; /* The full data from the table */
public $errors; /* An array of strings detailing any issues. */
public $table_name; /* This will hold the name of the table for SQL queries */
public $records; /* Will hold the total number of records in the table */
public $paginate; /* Paginate the records with a limit flag */
public $records_per_page;
public $current_page;
/** Constructor
* Sets members up as arrays.
*
* @return
*/
public function __construct($order = 'id DESC', $paginate = 'no', $records_per_page = 10)
{
$this->form = array();
$this->table = array();
$this->errors = array();
$this->date_fields = array('entered', 'posted', 'taken', 'on_date');
$this->time_fields = array('at_time');
$this->retrieve_order = $order;
$this->paginate = $paginate;
$this->records_per_page = $records_per_page;
$this->current_page = 0;
$this->getPageLimits();
$this->retrieveAll();
}
/** updateFromForm
* This function reads through the form array member, which has
* previously been filled by the parseForm method, and builds
* an update query by examining types. It is therefore generic.
*
* @return $result Array
*/
protected function updateFromForm()
{
$query_string = 'UPDATE '.$this->table_name.' SET ';
$values = array();
foreach($this->form as $key => $value)
{
if($key == 'id')
{
$query_append = " WHERE id = %d";
$values_append = $value;
}
else if (in_array($key, $this->date_fields))
{
$value = convert_date($value, "YYYY-MM-DD");
$query_string .= "`".$key."`=%s, ";
array_push($values, $value);
}
else if (in_array($key, $this->time_fields))
{
$value = convert_time($value, "hh:mm:ss");
$query_string .= "`".$key."`=%s, ";
array_push($values, $value);
}
else if ($key == 'password')
{
/* Do nothing */
}
else
{
if (is_float($value))
$query_string .= "`".$key."`=%f, ";
else if (is_numeric($value))
$query_string .= "`".$key."`=%d, ";
else
$query_string .= "`".$key."`=%s, ";
array_push($values, $value);
}
}
$query_string = rtrim($query_string, " ,");
$query_string .= $query_append;
array_push($values, $values_append);
return run_query($query_string, $values);
}
/** insertFromForm
* This function reads through the form array member, which has
* previously been filled by the parseForm method, and builds
* an update query by examining types. It is therefore generic.
*
* @return $result Array
*/
protected function insertFromForm()
{
$query_string = 'INSERT INTO '.$this->table_name.' VALUES(';
$values = array();
foreach($this->form as $key => $value)
{
if($key == 'id')
{
$query_string .= "NULL, ";
}
else if (in_array($key, $this->date_fields))
{
$value = convert_date($value, "YYYY-MM-DD");
$query_string .= "%s, ";
array_push($values, $value);
}
else if (in_array($key, $this->time_fields))
{
$value = convert_time($value, "hh:mm:ss");
$query_string .= "%s, ";
array_push($values, $value);
}
else if ($key == 'password')
{
$query_string .= "PASSWORD(%s), ";
array_push($values, $value);
}
else
{
if (is_float($value))
$query_string .= "%f, ";
else if (is_numeric($value))
$query_string .= "%d, ";
else
$query_string .= "%s, ";
array_push($values, $value);
}
}
$query_string = rtrim($query_string, " ,");
$query_string .= ")";
return run_query($query_string, $values);
}
/** deleteMultipleEntries
* Cycles through an array of ids deleting them all.
*
* @return
* @param $id_array Object
*/
protected function deleteMultipleEntries($id_array)
{
if(count($id_array) == 0)
return 0;
for($i = 0; $i<count($id_array); $i++)
run_query('DELETE FROM '.$this->table_name.' WHERE id = %d', $id_array[$i]);
return count($id_array);
}
/** parseForm
* The parseForm function will determine if we have submitted a form
* and if so it will load the form data into the form member of the
* class. This data will be used by Smarty.
*
* @return Nothing
*/
public function parseForm()
{
if ((isset($_POST['sub_op'])) && ($_POST['sub_op']))
$sub_op = strtolower($_POST['sub_op']);
else
$sub_op = '';
/* Load form should clear the form if there is no $sub_op */
if ($this->loadForm($sub_op))
{
switch ($sub_op)
{
case 'amend': $this->amendRecord();
break;
case 'create': $this->createRecord();
break;
case 'delete': $this->deleteRecord();
break;
case 'delete selected': $this->deleteSelected($_POST['del_array']);
break;
default: /* Do nothing as there has been no request made */
}
/* Clear the form */
$this->loadForm('');
}
$this->retrieveAll();
}
/** formToScreen
* Move the form fields to a Smarty Object for display on screen.
*
* @return
* @param $SmartyObject Object
*/
public function formToScreen(&$SmartyObject)
{
foreach($this->form as $key => $value)
{
$SmartyObject->assign("$key", $value);
}
}
/** retrieveRecord
* This function provides two duties:
* 1. Sets up the form if an 'amend' hyperlink has been
* selected, retrieving the record to the form and
* converting the date from DB form to screen.
* 2. Maintain current versions of the form data in the
* case of an Amend update error.
*
* @return Text for the form button.
*/
public function retrieveRecord()
{
if (isset($_GET['link_op']) && ($_GET['link_op'] == 'amend'))
{
$result = run_query('SELECT * FROM '.$this->table_name.' WHERE id = %d', $_GET['id']);
if(!$result)
{
$err_string = ucfirst($this->table_name)." ".$id." not found.";
array_push($this->errors, $err_string);
}
else
{
foreach($result[0] as $key => $value)
{
if (in_array($key, $this->date_fields))
{
$value = convert_date($value, "DD/MM/YY");
}
elseif (in_array($key, $this->time_fields))
{
$value = convert_time($value);
}
$this->form[$key] = $value;
}
return 'Amend';
}
}
/* We allow for an error in the amend, e.g. blank fields that shouldn't be */
if (isset($_POST['sub_op']) && (strtolower($_POST['sub_op']) == 'amend') && (count($this->errors)>0))
return 'Amend';
return '';
}
/** displayRecord
* Puts a record into an array for display purposes
* and returning it. This will parse every value for
* output with line breaks and fix the dates.
*
* @return Array Object of the record
*/
public function displayRecord()
{
if (isset($_GET['link_op']) && ($_GET['link_op'] == 'display'))
{
$result = run_query('SELECT * FROM '.$this->table_name.' WHERE id = %d', $_GET['id']);
if(!$result)
{
$err_string = ucfirst($this->table_name)." ".$id." not found.";
array_push($this->errors, $err_string);
return false;
}
else
{
$output = array();
for($i=0; $i<count($result); $i++)
{
$output[$i] = parse_record($result[$i], $this->date_fields);
}
return $output;
}
}
return '';
}
/** retrieveOrdered
* Retrieve all records in an order
*
* @return
*/
public function retrieveOrdered($where_condition, $order_condition)
{
$result = run_query('SELECT * FROM '.$this->table_name.' WHERE '
.$where_condition.' ORDER BY '.$order_condition);
if(!($result)) return 0;
for ($i = 0; $i<count($result); $i++)
{
foreach($result[$i] as $key => $value)
{
if (in_array($key, $this->date_fields))
{
$value = convert_date($value, "dd month year");
$result[$i][$key] = $value;
}
elseif (in_array($key, $this->time_fields))
{
$value = convert_time($value);
$result[$i][$key] = $value;
}
}
}
return $result;
}
}