12 require_once(
"DataUpdate.php");
13 require_once(
"DataEntry.php");
38 $this->Field = $field;
40 $this->LookupSql = $lookup_sql;
41 $this->Attributes = array();
44 function Set($value) {
45 $this->Value = $value;
61 $this->LookupSql = $lookup_sql;
79 function SetOptionList( $options, $current =
null, $parameters =
null) {
80 if ( gettype($options) ==
'array' ) {
81 $this->OptionList =
'';
83 if ( is_array($parameters) ) {
84 if ( isset($parameters[
'maxwidth']) ) $maxwidth = max(4,intval($parameters[
'maxwidth']));
85 if ( isset($parameters[
'translate']) ) $translate =
true;
88 foreach( $options AS $k => $v ) {
89 if (is_array($current)) {
90 $selected = ( ( in_array($k,$current,
true) || in_array($v,$current,
true)) ?
' selected="selected"' :
'' );
93 $selected = ( (
"$k" ==
"$current" ||
"$v" ==
"$current" ) ?
' selected="selected"' :
'' );
95 if ( isset($translate) ) $v = translate( $v );
96 if ( isset($maxwidth) ) $v = substr( $v, 0, $maxwidth);
97 $this->OptionList .=
"<option value=\"".htmlspecialchars($k).
"\"$selected>".htmlspecialchars($v).
"</option>";
101 $this->OptionList = $options;
105 function GetTarget() {
106 if ( $this->Sql ==
"" )
return $this->Field;
107 return "$this->Sql AS $this->Field";
117 $this->Attributes[$k] = $v;
129 if ( !isset($this->Attributes[
'_label']) || !isset($this->Attributes[
'id']))
return $wrapme;
130 $class = (isset($this->Attributes[
'class']) ? $this->Attributes[
'class'] :
'entry');
131 $title = (isset($this->Attributes[
'title']) ?
' title="'.str_replace(
'"',
''', $this->Attributes[
'title']) .
'"' :
'');
132 return( sprintf(
'<label for="%s" class="%s"%s>%s %s</label>',
133 $this->Attributes[
'id'], $class, $title, $wrapme, $this->Attributes[
'_label']) );
142 if ( count($this->Attributes) == 0 )
return $attributes;
143 foreach( $this->Attributes AS $k => $v ) {
144 if ( $k ==
'_label' )
continue;
145 $attributes .=
" $k=\"" . str_replace(
'"',
''', $v) .
'"';
174 var $RecordAvailable;
190 global $c, $session, $form_id_increment;
191 $this->
Title = $title;
194 $this->Template =
"";
195 $this->RecordAvailable =
false;
196 $this->SubmitName =
'submit';
197 $form_id_increment = (isset($form_id_increment)? ++$form_id_increment : 1);
198 $this->
Id =
'editor_'.$form_id_increment;
200 if ( isset($fields) ) {
201 if ( is_array($fields) ) {
202 foreach( $fields AS $k => $v ) {
206 else if ( is_string($fields) ) {
208 $this->BaseTable = $fields;
209 $field_list = awl_get_fields($fields);
210 foreach( $field_list AS $k => $v ) {
215 @dbg_error_log(
'editor',
'DBG: New editor called %s', $title);
226 function &
AddField( $field, $sql=
"", $lookup_sql=
"" ) {
227 $this->Fields[$field] =
new EditorField( $field, $sql, $lookup_sql );
228 $this->OrderedFields[] = $field;
229 return $this->Fields[$field];
238 $this->Fields[$field]->SetSql( $sql );
247 if (is_object($this->Fields[$field])) {
248 $this->Fields[$field]->SetLookup( $lookup_sql );
256 function Value( $value_field_name ) {
257 if ( !isset($this->Record->{$value_field_name}) )
return null;
258 return $this->Record->{$value_field_name};
266 function Assign( $value_field_name, $new_value ) {
267 if ( !isset($this->Record) ) $this->Record = (object) array();
268 $this->Record->{$value_field_name} = $new_value;
275 function Id( $id =
null ) {
276 if ( isset($id) ) $this->Id = preg_replace(
'#[^a-z0-9_+-]#',
'', $id);
289 function SetOptionList( $field, $options, $current =
null, $parameters =
null) {
290 $this->Fields[$field]->SetOptionList( $options, $current, $parameters );
300 $this->Fields[$field]->AddAttribute($k,$v);
309 $this->BaseTable = $base_table;
317 $this->Joins = $join_list;
327 function Title( $new_title =
null ) {
328 if ( isset($new_title) ) $this->Title = $new_title;
338 $this->SubmitName = $new_submit;
341 function IsSubmit() {
342 return isset($_POST[$this->SubmitName]);
350 $is_update = $this->Available();
351 if ( isset( $_POST[
'_editor_action']) && isset( $_POST[
'_editor_action'][$this->Id]) ) {
352 $is_update = ( $_POST[
'_editor_action'][$this->Id] ==
'update' );
353 @dbg_error_log(
'editor',
'Checking update: %s => %d', $_POST[
'_editor_action'][$this->Id], $is_update );
363 return ! $this->IsUpdate();
371 $this->Where = $where_clause;
379 $this->NewWhere = $where_clause;
388 if ( $this->Where ==
"" ) {
389 $this->Where = $more_where;
392 $this->Where =
"$this->Where $operator $more_where";
395 function AndWhere( $more_where ) {
396 $this->MoreWhere(
"AND",$more_where);
399 function OrWhere( $more_where ) {
400 $this->MoreWhere(
"OR",$more_where);
410 deprecated(
'Editor::SetTemplate');
411 $this->Template = $template;
421 if ( strstr( $template,
'##form##' ) ===
false && stristr( $template,
'<form' ) ===
false )
422 $template =
'##form##' . $template;
423 if ( stristr( $template,
'</form' ) ===
false ) $template .=
'</form>';
424 $this->Template = $template;
433 return $this->RecordAvailable;
443 $this->Record = $row;
444 $this->RecordAvailable = is_object($this->Record);
445 return $this->Record;
454 $this->RecordAvailable =
false;
455 if ( !isset($this->Record) ) $this->Record = (object) array();
456 foreach( $values AS $fname => $value ) {
457 $this->Record->{$fname} = $value;
467 foreach ( $this->Fields AS $fname => $fld ) {
468 @dbg_error_log(
'editor',
":PostToValues: %s => %s", $fname, $_POST[
"$prefix$fname"] );
469 if ( isset($_POST[$prefix.$fname]) ) {
470 $this->Record->{$fname} = $_POST[$prefix.$fname];
471 @dbg_error_log(
'editor',
":PostToValues: %s => %s", $fname, $_POST[
"$prefix$fname"] );
485 foreach( $this->Fields AS $k => $column ) {
486 if ( $target_fields !=
"" ) $target_fields .=
", ";
487 $target_fields .= $column->GetTarget();
489 if ( $where ==
"" ) $where = $this->Where;
490 $sql = sprintf(
"SELECT %s FROM %s %s WHERE %s %s %s",
491 $target_fields, $this->BaseTable, $this->Joins, $where, $this->Order, $this->Limit);
492 $this->Query =
new AwlQuery( $sql );
493 @dbg_error_log(
'editor',
"DBG: EditorGetQry: %s", $sql );
494 if ( $this->Query->Exec(
"Browse:$this->Title:DoQuery") ) {
495 $this->Record = $this->Query->Fetch();
496 $this->RecordAvailable = is_object($this->Record);
498 if ( !$this->RecordAvailable ) {
499 $this->Record = (object) array();
501 return $this->Record;
541 switch( $matches[0] ) {
543 return sprintf(
'<form method="POST" enctype="multipart/form-data" class="editor" id="form_%s">', $this->Id);
545 $action = ( $this->RecordAvailable ?
'update' :
'insert' );
546 $submittype = ($this->RecordAvailable ? translate(
'Apply Changes') : translate(
'Create'));
547 return sprintf(
'<input type="hidden" name="_editor_action[%s]" value="%s"><input type="submit" class="submit" name="%s" value="%s">',
548 $this->Id, $action, $this->SubmitName, $submittype );
553 $field_name = $matches[1];
554 $what_part = (isset($matches[3]) ? $matches[3] :
null);
555 $part3 = (isset($matches[5]) ? $matches[5] :
null);
557 $value_field_name = $field_name;
558 if ( substr($field_name,0,4) ==
'xxxx' ) {
562 $value_field_name = substr($field_name,4);
566 if ( isset($this->Fields[$field_name]) && is_object($this->Fields[$field_name]) ) {
567 $field = $this->Fields[$field_name];
568 $attributes = $field->RenderAttributes();
570 $field_value = (isset($this->Record->{$value_field_name}) ? $this->Record->{$value_field_name} :
null);
572 switch( $what_part ) {
575 if ( ! isset($currval) && isset($field_value) )
576 $currval = $field_value;
577 if ( isset($field->OptionList) && $field->OptionList !=
"" ) {
578 $option_list = $field->OptionList;
581 @dbg_error_log(
'editor',
"DBG: Current=%s, OptionQuery: %s", $currval, $field->LookupSql );
582 $opt_qry =
new AwlQuery( $field->LookupSql );
584 $field->OptionList = $option_list;
589 if ( ! isset($currval) && isset($field_value) )
590 $currval = $field_value;
591 if ( isset($field->OptionList) && $field->OptionList !=
"" ) {
592 $option_list = $field->OptionList;
595 @dbg_error_log(
'editor',
'DBG: Current=%s, OptionQuery: %s', $currval, $field->LookupSql );
596 $opt_qry =
new AwlQuery( $field->LookupSql );
598 $field->OptionList = $option_list;
600 return '<select class="entry" name="'.$field_name.
'"'.$attributes.
'>'.$option_list.
'</select>';
602 if ( !isset($field) ) {
603 @dbg_error_log(
"ERROR",
"Field '$field_name' is not defined.");
604 return "<p>Error: '$field_name' is not defined.</p>";
606 if ( $field_value ===
true ) {
607 $checked =
' CHECKED';
610 switch ( $field_value ) {
620 $checked =
' CHECKED';
623 return $field->RenderLabel(
'<input type="hidden" value="off" name="'.$field_name.
'"><input class="entry" type="checkbox" value="on" name="'.$field_name.
'"'.$checked.$attributes.
'>' );
625 $size = (isset($part3) ? $part3 : 6);
626 return "<input class=\"entry\" value=\"".htmlspecialchars($field_value).
"\" name=\"$field_name\" size=\"$size\"$attributes>";
628 $size = (isset($part3) ? $part3 : 30);
629 return "<input type=\"file\" class=\"entry\" value=\"".htmlspecialchars($field_value).
"\" name=\"$field_name\" size=\"$size\"$attributes>";
631 $size = (isset($part3) ? $part3 : 8);
632 return "<input class=\"money\" value=\"".htmlspecialchars(sprintf(
"%0.2lf",$field_value)).
"\" name=\"$field_name\" size=\"$size\"$attributes>";
634 $size = (isset($part3) ? $part3 : 10);
635 return "<input class=\"date\" value=\"".htmlspecialchars($field_value).
"\" name=\"$field_name\" size=\"$size\"$attributes>";
637 list( $cols, $rows ) = explode(
'x', $part3);
638 return "<textarea class=\"entry\" name=\"$field_name\" rows=\"$rows\" cols=\"$cols\"$attributes>".htmlspecialchars($field_value).
"</textarea>";
640 return sprintf(
"<input type=\"hidden\" value=\"%s\" name=\"$field_name\">", htmlspecialchars($field_value) );
642 return sprintf(
"<input type=\"password\" value=\"%s\" name=\"$field_name\" size=\"10\">", htmlspecialchars($part3) );
645 return htmlspecialchars($field_value);
647 $action = ( $this->RecordAvailable ?
'update' :
'insert' );
648 return sprintf(
'<input type="hidden" name="_editor_action[%s]" value="%s"><input type="submit" class="submit" name="%s" value="%s">',
649 $this->Id, $action, $this->SubmitName, $value_field_name );
651 return str_replace(
"\n",
"<br />", $field_value );
659 @dbg_error_log(
'editor',
"classEditor",
"Rendering editor $this->Title" );
660 if ( $this->Template ==
"" ) $this->DefaultTemplate();
662 $html = sprintf(
'<div class="editor" id="%s">', $this->Id);
663 if ( isset($this->Title) && $this->Title !=
"" ) {
664 if ( !isset($title_tag) ) $title_tag =
'h1';
665 $html =
"<$title_tag>$this->Title</$title_tag>\n";
669 $replaced = preg_replace_callback(
"/##([^#.]+)(\.([^#.]+))?(\.([^#.]+))?##/", array(&$this,
"ReplaceEditorPart"), $this->Template );
680 function Write( $is_update =
null ) {
681 global $c, $component;
683 @dbg_error_log(
'editor',
'DBG: Writing editor %s', $this->Title);
685 if ( !isset($is_update) ) {
686 if ( isset( $_POST[
'_editor_action']) && isset( $_POST[
'_editor_action'][$this->Id]) ) {
687 $is_update = ( $_POST[
'_editor_action'][$this->Id] ==
'update' );
695 $is_update = preg_match(
'/(save|update|apply)/i', $_POST[$this->SubmitName] );
696 dbg_error_log(
'WARN', $_SERVER[
'REQUEST_URI'].
" is using a deprecated method for controlling insert/update" );
699 $this->Action = ( $is_update ?
"update" :
"create" );
700 $qry =
new AwlQuery( sql_from_post( $this->Action, $this->BaseTable,
"WHERE ".$this->Where ) );
701 if ( !$qry->Exec(
"Editor::Write") ) {
702 $c->messages[] =
"ERROR: $qry->errorstring";
705 if ( $this->Action ==
"create" && isset($this->NewWhere) ) {
706 $this->GetRecord($this->NewWhere);
709 $this->GetRecord($this->Where);
711 return $this->Record;