* @copyright Catalyst IT Ltd, Morphoss Ltd
* @license http://gnu.org/copyleft/gpl.html GNU GPL v2
*/
require_once("DataUpdate.php");
require_once("DataEntry.php");
/**
* A class for the fields in the editor
* @package apms
*/
class EditorField
{
var $Field;
var $Sql;
var $Value;
var $Attributes;
var $LookupSql;
var $OptionList;
function __construct( $field, $sql="", $lookup_sql="" ) {
global $session;
$this->Field = $field;
$this->Sql = $sql;
$this->LookupSql = $lookup_sql;
$this->Attributes = array();
$session->Log("DBG: New field '%s' SQL='%s', Lookup='%s'", $field, $sql, $lookup_sql );
}
function Set($value) {
$this->Value = $value;
}
function SetSql( $sql ) {
$this->Sql = $sql;
}
function SetLookup( $lookup_sql ) {
$this->LookupSql = $lookup_sql;
}
function SetOptionList( $options, $current = null, $parameters = null) {
if ( gettype($options) == 'array' ) {
$this->OptionList = '';
if ( is_array($parameters) ) {
if ( isset($parameters['maxwidth']) ) $maxwidth = max(4,intval($parameters['maxwidth']));
if ( isset($parameters['translate']) ) $translate = true;
}
foreach( $options AS $k => $v ) {
if (is_array($current)) {
$selected = ( ( in_array($k,$current,true) || in_array($v,$current,true)) ? ' selected="selected"' : '' );
}
else {
$selected = ( ( "$k" == "$current" || "$v" == "$current" ) ? ' selected="selected"' : '' );
}
if ( isset($translate) ) $v = translate( $v );
if ( isset($maxwidth) ) $v = substr( $v, 0, $maxwidth);
$this->OptionList .= "";
}
}
else {
$this->OptionList = $options;
}
}
function GetTarget() {
if ( $this->Sql == "" ) return $this->Field;
return "$this->Sql AS $this->Field";
}
function AddAttribute( $k, $v ) {
$this->Attributes[$k] = $v;
}
function RenderAttributes() {
$attributes = "";
if ( count($this->Attributes) == 0 ) return $attributes;
foreach( $this->Attributes AS $k => $v ) {
$attributes .= " $k=\"" . str_replace('"', '\\"', $v) . '"';
}
return $attributes;
}
}
/**
* The class for the Editor form in full
* @package apms
*/
class Editor
{
var $Title;
var $Action;
var $Fields;
var $OrderedFields;
var $BaseTable;
var $Joins;
var $Where;
var $NewWhere;
var $Order;
var $Limit;
var $Query;
var $Template;
var $RecordAvailable;
var $Record;
var $SubmitName;
var $Id;
function __construct( $title = "", $fields = null ) {
global $c, $session, $form_id_increment;
$this->Title = $title;
$this->Order = "";
$this->Limit = "";
$this->Template = "";
$this->RecordAvailable = false;
$this->SubmitName = 'submit';
$form_id_increment = (isset($form_id_increment)? ++$form_id_increment : 1);
$this->Id = 'editor_'.$form_id_increment;
if ( isset($fields) ) {
if ( is_array($fields) ) {
foreach( $fields AS $k => $v ) {
$this->AddField($v);
}
}
else if ( is_string($fields) ) {
// We've been given a table name, so get all fields for it.
$this->BaseTable = $fields;
$field_list = get_fields($fields);
foreach( $field_list AS $k => $v ) {
$this->AddField($k);
}
}
}
dbg_error_log("classEditor", "DBG: New editor called $title");
}
function &AddField( $field, $sql="", $lookup_sql="" ) {
$this->Fields[$field] = new EditorField( $field, $sql, $lookup_sql );
$this->OrderedFields[] = $field;
return $this->Fields[$field];
}
function SetSql( $field, $sql ) {
$this->Fields[$field]->SetSql( $sql );
}
function SetLookup( $field, $lookup_sql ) {
if (is_object($this->Fields[$field])) {
$this->Fields[$field]->SetLookup( $lookup_sql );
}
}
function Value( $value_field_name ) {
if ( !isset($this->Record->{$value_field_name}) ) return null;
return $this->Record->{$value_field_name};
}
function Assign( $value_field_name, $new_value ) {
$this->Record->{$value_field_name} = $new_value;
}
function Id( $id = null ) {
if ( isset($id) ) $this->Id = preg_replace( '#[^a-z0-9_+-]#', '', $id);
return $this->Id;
}
function SetOptionList( $field, $options, $current = null, $parameters = null) {
$this->Fields[$field]->SetOptionList( $options, $current, $parameters );
}
function AddAttribute( $field, $k, $v ) {
$this->Fields[$field]->AddAttribute($k,$v);
}
function SetBaseTable( $base_table ) {
$this->BaseTable = $base_table;
}
function SetJoins( $join_list ) {
$this->Joins = $join_list;
}
/**
* Accessor for the Title for the browse, which could set the title also.
*
* @param string $new_title The new title for the browser
* @return string The current title for the browser
*/
function Title( $new_title = null ) {
if ( isset($new_title) ) $this->Title = $new_title;
return $this->Title;
}
function SetSubmitName( $new_submit ) {
$this->SubmitName = $new_submit;
}
function IsSubmit() {
return isset($_POST[$this->SubmitName]);
}
function IsUpdate() {
$is_update = $this->Available();
if ( isset( $_POST['_editor_action']) && isset( $_POST['_editor_action'][$this->Id]) ) {
$is_update = ( $_POST['_editor_action'][$this->Id] == 'update' );
dbg_error_log("ERROR", "Checking update: %s => %d", $_POST['_editor_action'][$this->Id], $is_update );
}
return $is_update;
}
function SetWhere( $where_clause ) {
$this->Where = $where_clause;
}
function WhereNewRecord( $where_clause ) {
$this->NewWhere = $where_clause;
}
function MoreWhere( $operator, $more_where ) {
if ( $this->Where == "" ) {
$this->Where = $more_where;
return;
}
$this->Where = "$this->Where $operator $more_where";
}
function AndWhere( $more_where ) {
$this->MoreWhere("AND",$more_where);
}
function OrWhere( $more_where ) {
$this->MoreWhere("OR",$more_where);
}
function SetTemplate( $template ) {
$this->Template = $template;
}
function Layout( $template ) {
if ( strstr( $template, '##form##' ) === false && stristr( $template, '