Rev 61 | Rev 64 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
<?php
class Camp {
protected static $dbStructure;
//public static $myName;
//protected static $viewName = 'view_device_location_owner_type';
/**
* Builds an instance and loads data from database
*
* #parameter int $id The id of the record to load
*/
public function __construct( $id = 0 ) {
if ( $id )
$this->loadFromDB( $id );
} // __construct
public function loadFromDB( $id ) {
global $dbConnection;
$this->data = array();
$fields = array();
foreach ( static::$dbStructure['table']['fields'] as $key => $data ) {
if ( $data['type'] != 'calculated' ) { // we have some which are calculated
$fields[] = $data['fieldname'];
}
}
$query = sprintf( "select %s from %s where %s = %s",
implode( ',', $fields ),
static::$dbStructure['table']['tableName'],
static::$dbStructure['table']['primaryKey'],
$id
);
//print "<pre> Constructor using query\n$query</pre>";
//print "<pre>Constructor finding dbstructure of\n" . print_r( static::$dbStructure, true) . '</pre>';
if ( ( $this->data = $dbConnection->getOneRow( $query ) ) === false )
print DBQuery::error2String();
// figure out which fields have calculated set so we can
// do them next
$calculatedFields = array();
foreach ( static::$dbStructure['table']['fields'] as $field => $record ) {
if ( $record['type'] == 'calculated' || $record['type'] == 'link') {
$calculatedFields[] = $field;
}
}
//print "<pre>" . print_r( $calculatedFields, true ) . "</pre>"; die;
//print "<pre>" . print_r( $this->data, true ) . "</pre>"; die;
if ( count( $calculatedFields ) ) {
$query = sprintf( 'select distinct %s from %s %s',
implode( ',', $calculatedFields ),
static::$dbStructure['view']['viewName'],
static::makeWhereClause( array(
static::$dbStructure['table']['fields']['id']['fieldname'] .
' = ' .
$this->data[static::$dbStructure['table']['primaryKey']] )
)
);
//print "<pre>$query</pre>"; die;
$result = $dbConnection->getOneRow( $query );
foreach ( $calculatedFields as $field ) {
$this->data[$field] = empty( $result[$field] ) ? '' : $result[$field];
} // foreach
}
} // loadFromDB
/**
* gets the contents of $fieldname
*
* @parameter string $fieldname name of the database field to retrieve
*
* @return string The value of $fieldname stored in structure
*/
public function __get( $fieldname ) {
return
isset( $this->data[static::$dbStructure['table']['fields'][$fieldname]['fieldname']] ) ?
$this->data[static::$dbStructure['table']['fields'][$fieldname]['fieldname']] :
null;
} // __get
/**
* Gets stats for an extended class.
*
* Gets number of active and inactive rows for a class, using the
* view ($viewName)
*
* @return int[] array of integers which is a count
*/
public static function getStats () {
global $dbConnection;
global $activeOnly;
$return = array();
$restrictions = array();
$trueClass = get_called_class();
$saveActive = $activeOnly;
$activeOnly = true;
$active = sprintf(
'select count(distinct %s) from %s %s',
static::$dbStructure['table']['primaryKey'],
static::$dbStructure['view']['viewName'],
self::makeWhereClause()
);
$activeOnly = false;
$inactive = sprintf(
'select count(distinct %s) from %s %s',
static::$dbStructure['table']['primaryKey'],
static::$dbStructure['view']['viewName'],
self::makeWhereClause( array( strtolower( $trueClass ) . "_removed is not null" ) )
);
$activeOnly = $saveActive;
//print "<pre>Active\n$active</pre><pre>Inactive\n$inactive</pre>"; die;
$return['active'] = $dbConnection->getOneDBValue( $active );
$return['inactive'] = $dbConnection->getOneDBValue( $inactive );
return $return;
}
/**
* Creates a where clause
*
* Merges $restrictions with any restrictions which may be in place
* for the current user. Also sets 'class_removed is null' if
* $activeOnly (global) is set
*
* @parameter string[] $restrictions array of conditionals
*
* @return string all restricions ANDED together
*/
protected static function makeWhereClause( $restrictions = array() ) {
global $activeOnly;
//print "<pre>" . print_r( $restrictions, true ) . "</pre>";
$trueClass = get_called_class();
foreach ( $_SESSION['restrictions'] as $class => $restriction ) {
$restrictions[] = $_SESSION['restrictions'][$class];
}
if ( $activeOnly ) {
$restrictions[] = strtolower( $trueClass ) . '_removed is null';
}
return count( $restrictions ) ? 'where ' . implode( ' and ', $restrictions ) : '';
} //
/**
* Creates a list of all records for a particular class and returns
* a UL with all LI's set as links (A's) to displaying a record.
*
* @parameter string[] $filter array of conditionals to limit if $list not set
* @parameter string[] $list An optional list of data to be displayed
*
* @return string A UL with each LI containing a link to an entry
*/
public static function showSelectionList( $filter = array(), $list = array() ) {
global $url;
$return = array();
if ( empty( $list ) ) {
$list = self::getAll( $filter );
}
$module = get_called_class();
foreach ( $list as $id => $name ) {
$return[] = "<a href='$url?module=$module&id=$id'>$name</a>";
}
return "<ul>\n<li>" . implode( "</li>\n<li>", $return ) . "</li>\n</ul>";
}
/**
* Gets all matching rows
*
* Does an SQL call to retrieve ID and Name for all rows matching
* $filter and $_REQUEST['to_find'].
*
* @parameter string[] $filter Optional list of conditionals for where clause
*
* @return array[] An array of indexes and names matching query
*/
public static function getAll( $filter = array() ) {
global $activeOnly;
global $dbConnection;
//print "<pre>" . print_r( $filter, true ) . "</pre>";
if ( isset( $_REQUEST['to_find'] ) ) {
$filter[] = sprintf( " %s like '%%%s%%'", static::$dbStructure['view']['selectionDisplay'], $_REQUEST['to_find']) ;
}
$return = array();
$query = sprintf( 'select distinct %s id, %s name from %s %s',
static::$dbStructure['view']['primaryKey'],
static::$dbStructure['view']['selectionDisplay'],
static::$dbStructure['view']['viewName'],
self::makeWhereClause( $filter )
);
$query .= " order by " . static::$dbStructure['view']['selectionDisplay'];
//print "<pre>$query</pre>\n";
$result = $dbConnection->doSQL( $query );
foreach ( $result['returnData'] as $row ) {
$return[$row['id']] = $row['name'];
}
return $return;
}
/**
* Returns the "name" of an instance
*
* Normally, this is $this->name, but some classes may want to put
* in additional data
*
* @return string Display name of this instance
*/
public function __toString() {
return $this->name;
}
/**
* Returns an HTML structure which can display a record
*
* Note that if the instance has children, they will be displayed
* also. Additionally, if it has fields marked as type 'calculated'
* it will perform the limited calculations.
*
* @return string HTML to display record
*/
public function display() {
global $url;
$return = array();
$save = '';
/* get the records for this, including from other tables */
foreach ( static::$dbStructure['table']['fields'] as $key => $record ) {
if ( isset( $record['display'] ) && $record['display'] === false ) // we don't try to show links
continue;
if ( isset( $record['link'] ) ) { // this one is supposed to be set up as a link
$return[] = sprintf(
"<td>%s</td><td><a href='$url?module=%s&id=%s'>%s</a></td>",
$record['displayName'],
static::$dbStructure['table']['fields'][$record['link']]['class'],
$this->data[static::$dbStructure['table']['fields'][$record['link']]['fieldname']],
$this->data[$record['fieldname']]
);
} else { // just pulling data from the table itself
$return[] = '<td>' . $record['displayName'] . "</td>\n<td>" . $this->data[$record['fieldname']] . '</td>';
}
}
$return[] = sprintf( "<td colspan='2'><a href='$url?module=%s&id=%s&action=edit'>Edit</a></td>",
get_called_class(),
$this->data[static::$dbStructure['table']['primaryKey']]
);
$return = "<div class='show_record'>\n<table class='show_record'>\n<tr>" . implode("</tr>\n<tr>", $return ) . "</tr>\n</table>\n</div>";
/* Now, get the children records */
if ( isset( $_REQUEST['to_find'] ) ) {
$save = $_REQUEST['to_find'];
unset( $_REQUEST['to_find'] );
}
foreach ( static::$dbStructure['children'] as $class ) {
$return .= "<div class='show_record'>\n<h3>$class</h3>\n" . $class::showSelectionList( array( static::$dbStructure['table']['primaryKey'] . ' = ' . $this->data[static::$dbStructure['table']['primaryKey']] ) ) . '</div>';
}
if ( $save !== null ) {
$_REQUEST['to_find'] = $save;
}
return $return;
}
/**
* Function is basically a placeholder in case a extended class
* needs to do some additional processing when editing
*
* In some cases, the edit function can not handle additional fields.
* See the Device class, where device_type is a table with many-to-many
* linkages to device. Since this type of thing is very rare, I decided
* to just allow edit and post to call an additional function in
* those cases.
*
* Based on the action requested, will either return an HTML string
* which will be placed in the edit screen (for edit), or an array
* of SQL to be executed to update/add rows (for post).
*
* If the action is edit, there should be a <td></td> around the
* whole thing
*/
protected function editAdditionalFields() {
if ( $_REQUEST['action'] == 'edit' ) {
return '';
} elseif ($_REQUEST['action'] == 'post' ) {
return array();
}
}
protected function edit() {
global $dbConnection;
// save everything we have right now
$this->beforeEdit = $this->data;
foreach ( static::$dbStructure['table']['fields'] as $key => $record ) {
if ( isset( $record['display'] ) && $record['display'] === false ) // we don't things which aren't supposed to be shown
continue;
if ( isset( $record['link'] ) ) { // this one is supposed to be set up as a link
// get a list of all options
$list = static::$dbStructure['table']['fields'][$record['link']]['class']::getAll();
//print '<pre>' . print_r( $record, true ) . "\n----\n" . print_r( $this->data, true ) . '</pre>'; die;
$selectedRow = $this->data[static::$dbStructure['table']['fields'][$record['link']]['fieldname']];
$select = $dbConnection->htmlSelect( array (
'data' => $list,
'name' => $record['fieldname'],
'nullok' => true,
'selected' => isset( $selectedRow ) ? $selectedRow : -1,
//'class' =>
)
);
$return[] = sprintf(
"<td>%s</td><td>%s</td>",
$record['displayName'],
$select
);
} else { // just pulling data from the table itself
if ( isset( $record['canEdit'] ) && $record['canEdit'] == false ) {
$return[] = sprintf(
"<td>%s</td><td>%s</td>",
$record['displayName'],
$this->data[$record['fieldname']]
);
} else {
$return[] = sprintf(
"<td>%s</td><td><input type='text' name='%s' value='%s'></td>",
$record['displayName'],
$record['fieldname'],
$this->data[$record['fieldname']]
);
}
}
}
$return[] = $this->editAdditionalFields();
$return[] = "<td colspan='2'><input type='submit' name='Save' value='Save'></td>";
$hiddens = sprintf( "<input type='hidden' name='module' value='%s'>\n", get_called_class() ) .
sprintf( "<input type='hidden' name='id' value='%s'>\n", $this->data[static::$dbStructure['table']['primaryKey']] ) .
"<input type='hidden' name='action' value='post'>\n";
return "<div class='show_record'>\n<form><table class='show_record'>\n<tr>" . implode("</tr>\n<tr>", $return ) . "</tr>\n</table>\n$hiddens</form></div>";
} // edit
protected function post() {
global $dbConnection;
$queries = array();
$fields = array();
foreach ( static::$dbStructure['table']['fields'] as $key => $record ) {
if ( isset( $record['display'] ) && $record['display'] === false ) // we don't try to show links
continue;
if ( isset( $record['canEdit'] ) && ! $record['canEdit'] )
continue;
if ( isset( $record['link'] ) ) { // this one is supposed to be set up as a link
} elseif ( $_REQUEST[$record['fieldname']] != $this->data[$record['fieldname']] ) {
// just a regular field, and it has changed, so add it to fields
$fields[$record['fieldname']] = $_REQUEST[$record['fieldname']];
}
} // foreach
//print "<pre>Fields\n" . print_r( $this->data, true ) . "</pre>"; die;
if ( count( $fields ) ) {
if ( isset( $this->data[static::$dbStructure['table']['primaryKey']] ) ) { // this is an update
$queries[] = $dbConnection->updateQuery(
static::$dbStructure['table']['tableName'],
array( static::$dbStructure['table']['primaryKey'] => $this->data[static::$dbStructure['table']['primaryKey']] ),
$fields );
} else { // this is an insert
$queries[] = $dbConnection->insertQuery(
static::$dbStructure['table']['tableName'],
$fields );
}
} // if there are fields
//print "<pre>Queries" . print_r( $queries, true ) . "</pre>"; die;
$dbConnection->runSQLScript( $queries );
$this->loadFromDB( $this->data[static::$dbStructure['table']['primaryKey']] );
} // post
/**
* Simple controller. Will determine what action is going on, then
* call the correct method, returning the HTML required
*/
public function run() {
if ( isset( $_REQUEST['action'] ) ) {
switch ( $_REQUEST['action'] ) {
case 'edit' : $return = $this->edit();
break;
case 'post' : $this->post();
default: $return = $this->display();
} // switch
} else {
$return = $this->display();
}
return $return;
}
} // abstract class Camp
?>