Back to index

awl  0.53
Public Member Functions | Public Attributes
Editor Class Reference

List of all members.

Public Member Functions

 __construct ($title="", $fields=null)
AddField ($field, $sql="", $lookup_sql="")
 SetSql ($field, $sql)
 SetLookup ($field, $lookup_sql)
 Value ($value_field_name)
 Gets the value of a field in the record currently assigned to this editor.
 Assign ($value_field_name, $new_value)
 Assigns the value of a field in the record currently associated with this editor.
 Id ($id=null)
 Sets or returns the form ID used for differentiating this form from others in the page.
 SetOptionList ($field, $options, $current=null, $parameters=null)
 AddAttribute ($field, $k, $v)
 SetBaseTable ($base_table)
 SetJoins ($join_list)
 Title ($new_title=null)
 Accessor for the Title for the browse, which could set the title also.
 SetSubmitName ($new_submit)
 IsSubmit ()
 IsUpdate ()
 IsCreate ()
 SetWhere ($where_clause)
 WhereNewRecord ($where_clause)
 MoreWhere ($operator, $more_where)
 AndWhere ($more_where)
 OrWhere ($more_where)
 SetTemplate ($template)
 Layout ($template)
 Available ()
 SetRecord ($row)
 Initialise ($values)
 Set some particular values to the ones from the array.
 PostToValues ($prefix= '')
 This will assign $_POST values to the internal Values object for each field that exists in the Fields array.
 GetRecord ($where="")
 ReplaceEditorPart ($matches)
 Replace parts into the form template.
 Render ($title_tag=null)
 Render the templated component.
 Write ($is_update=null)
 Write the record.

Public Attributes

 $Title
 $Action
 $Fields
 $OrderedFields
 $BaseTable
 $Joins
 $Where
 $NewWhere
 $Order
 $Limit
 $Query
 $Template
 $RecordAvailable
 $Record
 $SubmitName
 $Id

Detailed Description

Definition at line 111 of file classEditor.php.


Constructor & Destructor Documentation

Editor::__construct ( title = "",
fields = null 
)

Definition at line 130 of file classEditor.php.

                                                      {
    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( 'editor', 'DBG: New editor called %s', $title);
  }

Here is the call graph for this function:


Member Function Documentation

Editor::AddAttribute ( field,
k,
v 
)

Definition at line 207 of file classEditor.php.

                                          {
    $this->Fields[$field]->AddAttribute($k,$v);

  }
& Editor::AddField ( field,
sql = "",
lookup_sql = "" 
)

Definition at line 159 of file classEditor.php.

                                                        {
    $this->Fields[$field] = new EditorField( $field, $sql, $lookup_sql );
    $this->OrderedFields[] = $field;
    return $this->Fields[$field];
  }

Here is the caller graph for this function:

Editor::AndWhere ( more_where)

Definition at line 270 of file classEditor.php.

                                   {
    $this->MoreWhere("AND",$more_where);
  }
Editor::Assign ( value_field_name,
new_value 
)

Assigns the value of a field in the record currently associated with this editor.

Parameters:
string$value_field_name
string$new_value

Definition at line 189 of file classEditor.php.

                                                   {
    if ( !isset($this->Record) ) $this->Record = (object) array();
    $this->Record->{$value_field_name} = $new_value;
  }

Definition at line 288 of file classEditor.php.

Editor::GetRecord ( where = "")

Definition at line 326 of file classEditor.php.

                                    {
    global $session;
    $target_fields = "";
    foreach( $this->Fields AS $k => $column ) {
      if ( $target_fields != "" ) $target_fields .= ", ";
      $target_fields .= $column->GetTarget();
    }
    if ( $where == "" ) $where = $this->Where;
    $sql = sprintf( "SELECT %s FROM %s %s WHERE %s %s %s",
             $target_fields, $this->BaseTable, $this->Joins, $where, $this->Order, $this->Limit);
    $this->Query = new AwlQuery( $sql );
    @dbg_error_log( 'editor', "DBG: EditorGetQry: %s", $sql );
    if ( $this->Query->Exec("Browse:$this->Title:DoQuery") ) {
      $this->Record = $this->Query->Fetch();
      $this->RecordAvailable = is_object($this->Record);
    }
    if ( !$this->RecordAvailable ) {
      $this->Record = (object) array();
    }
    return $this->Record;
  }
Editor::Id ( id = null)

Sets or returns the form ID used for differentiating this form from others in the page.

Parameters:
string$id

Definition at line 198 of file classEditor.php.

                            {
    if ( isset($id) ) $this->Id = preg_replace( '#[^a-z0-9_+-]#', '', $id);
    return $this->Id;
  }

Here is the caller graph for this function:

Editor::Initialise ( values)

Set some particular values to the ones from the array.

Parameters:
array$valuesAn array of fieldname / value pairs

Definition at line 303 of file classEditor.php.

                                 {
    $this->RecordAvailable = false;
    if ( !isset($this->Record) ) $this->Record = (object) array();
    foreach( $values AS $fname => $value ) {
      $this->Record->{$fname} = $value;
    }
  }

Definition at line 250 of file classEditor.php.

                      {
    return ! $this->IsUpdate();
  }

Definition at line 237 of file classEditor.php.

                      {
    return isset($_POST[$this->SubmitName]);
  }

Definition at line 241 of file classEditor.php.

                      {
    $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( 'editor', 'Checking update: %s => %d', $_POST['_editor_action'][$this->Id], $is_update );
    }
    return $is_update;
  }
Editor::Layout ( template)

Definition at line 282 of file classEditor.php.

                               {
    if ( strstr( $template, '##form##' ) === false && stristr( $template, '<form' ) === false ) $template = '##form##' . $template;
    if ( stristr( $template, '</form' ) === false ) $template .= '</form>';
    $this->Template = $template;
  }
Editor::MoreWhere ( operator,
more_where 
)

Definition at line 262 of file classEditor.php.

                                               {
    if ( $this->Where == "" ) {
      $this->Where = $more_where;
      return;
    }
    $this->Where = "$this->Where $operator $more_where";
  }
Editor::OrWhere ( more_where)

Definition at line 274 of file classEditor.php.

                                  {
    $this->MoreWhere("OR",$more_where);
  }
Editor::PostToValues ( prefix = '')

This will assign $_POST values to the internal Values object for each field that exists in the Fields array.

Definition at line 316 of file classEditor.php.

                                        {
    foreach ( $this->Fields AS $fname => $fld ) {
      @dbg_error_log( 'editor', ":PostToValues: %s => %s", $fname, $_POST["$prefix$fname"] );
      if ( isset($_POST[$prefix.$fname]) ) {
        $this->Record->{$fname} = $_POST[$prefix.$fname];
        @dbg_error_log( 'editor', ":PostToValues: %s => %s", $fname, $_POST["$prefix$fname"] );
      }
    }
  }
Editor::Render ( title_tag = null)

Render the templated component.

The heavy lifting is done by the callback...

Definition at line 472 of file classEditor.php.

                                       {
    @dbg_error_log( 'editor', "classEditor", "Rendering editor $this->Title" );
    if ( $this->Template == "" ) $this->DefaultTemplate();

    $html = sprintf('<div class="editor" id="%s">', $this->Id);
    if ( isset($this->Title) && $this->Title != "" ) {
      if ( !isset($title_tag) ) $title_tag = 'h1';
      $html = "<$title_tag>$this->Title</$title_tag>\n";
    }

    // Stuff like "##fieldname.part## gets converted to the appropriate value
    $replaced = preg_replace_callback("/##([^#.]+)(\.([^#.]+))?(\.([^#.]+))?##/", array(&$this, "ReplaceEditorPart"), $this->Template );
    $html .= $replaced;

    $html .= '</div>';
    return $html;
  }
Editor::ReplaceEditorPart ( matches)

Replace parts into the form template.

Parameters:
array$matchesThe matches found which preg_replace_callback is calling us for.
Returns:
string What we want to replace this match with.
Todo:
It might be nice to construct a form ID

Definition at line 354 of file classEditor.php.

  {
    global $session;

    // $matches[0] is the complete match
    switch( $matches[0] ) {
      case "##form##": 
        return sprintf('<form method="POST" enctype="multipart/form-data" class="editor" id="%s">', $this->Id);
      case "##submit##":
        $action =  ( $this->RecordAvailable ? 'update' : 'insert' );
        $submittype = ($this->RecordAvailable ? translate('Apply Changes') : translate('Create'));
        return sprintf('<input type="hidden" name="_editor_action[%s]" value="%s"><input type="submit" class="submit" name="%s" value="%s">',
                                                              $this->Id, $action,                           $this->SubmitName, $submittype );
    }

    // $matches[1] the match for the first subpattern
    // enclosed in '(...)' and so on
    $field_name = $matches[1];
    $what_part = $matches[3];
    $part3 = (isset($matches[5]) ? $matches[5] : null);

    $value_field_name = $field_name;
    if ( substr($field_name,0,4) == 'xxxx' ) {
        // Sometimes we will prepend 'xxxx' to the field name so that the field
        // name differs from the column name in the database.  We also remove it
        // when it's submitted.
        $value_field_name = substr($field_name,4);
    }

    $attributes = "";
    if ( isset($this->Fields[$field_name]) && is_object($this->Fields[$field_name]) ) {
      $field = $this->Fields[$field_name];
      $attributes = $field->RenderAttributes();
    }
    $field_value = (isset($this->Record->{$value_field_name}) ? $this->Record->{$value_field_name} : null);

    switch( $what_part ) {
      case "options":
        $currval = $part3;
        if ( ! isset($currval) && isset($field_value) )
          $currval = $field_value;
        if ( isset($field->OptionList) && $field->OptionList != "" ) {
          $option_list = $field->OptionList;
        }
        else {
          @dbg_error_log( 'editor', "DBG: Current=%s, OptionQuery: %s", $currval, $field->LookupSql );
          $opt_qry = new AwlQuery( $field->LookupSql );
          $option_list = EntryField::BuildOptionList($opt_qry, $currval, "FieldOptions: $field_name" );
          $field->OptionList = $option_list;
        }
        return $option_list;
      case "select":
        $currval = $part3;
        if ( ! isset($currval) && isset($field_value) )
          $currval = $field_value;
        if ( isset($field->OptionList) && $field->OptionList != "" ) {
          $option_list = $field->OptionList;
        }
        else {
          @dbg_error_log( 'editor', 'DBG: Current=%s, OptionQuery: %s', $currval, $field->LookupSql );
          $opt_qry = new AwlQuery( $field->LookupSql );
          $option_list = EntryField::BuildOptionList($opt_qry, $currval, 'FieldOptions: '.$field_name );
          $field->OptionList = $option_list;
        }
        return '<select class="entry" name="'.$field_name.'"'.$attributes.'>'.$option_list.'</select>';
      case "checkbox":
        if ( $field_value === true ) {
          $checked = ' CHECKED';
        }
        else {
          switch ( $field_value ) {
            case 'f':
            case 'off':
            case 'false':
            case '':
            case '0':
              $checked = "";
              break;
  
            default:
              $checked = ' CHECKED';
          }
        }
        return $field->RenderLabel('<input type="hidden" value="off" name="'.$field_name.'"><input class="entry" type="checkbox" value="on" name="'.$field_name.'"'.$checked.$attributes.'>' );
      case "input":
        $size = (isset($part3) ? $part3 : 6);
        return "<input class=\"entry\" value=\"".htmlspecialchars($field_value)."\" name=\"$field_name\" size=\"$size\"$attributes>";
      case "file":
        $size = (isset($part3) ? $part3 : 30);
        return "<input type=\"file\" class=\"entry\" value=\"".htmlspecialchars($field_value)."\" name=\"$field_name\" size=\"$size\"$attributes>";
      case "money":
        $size = (isset($part3) ? $part3 : 8);
        return "<input class=\"money\" value=\"".htmlspecialchars(sprintf("%0.2lf",$field_value))."\" name=\"$field_name\" size=\"$size\"$attributes>";
      case "date":
        $size = (isset($part3) ? $part3 : 10);
        return "<input class=\"date\" value=\"".htmlspecialchars($field_value)."\" name=\"$field_name\" size=\"$size\"$attributes>";
      case "textarea":
        list( $cols, $rows ) = explode( 'x', $part3);
        return "<textarea class=\"entry\" name=\"$field_name\" rows=\"$rows\" cols=\"$cols\"$attributes>".htmlspecialchars($field_value)."</textarea>";
      case "hidden":
        return sprintf( "<input type=\"hidden\" value=\"%s\" name=\"$field_name\">", htmlspecialchars($field_value) );
      case "password":
        return sprintf( "<input type=\"password\" value=\"%s\" name=\"$field_name\" size=\"10\">", htmlspecialchars($part3) );
      case "encval":
      case "enc":
        return htmlspecialchars($field_value);
      case "submit":
        $action =  ( $this->RecordAvailable ? 'update' : 'insert' );
        return sprintf('<input type="hidden" name="_editor_action[%s]" value="%s"><input type="submit" class="submit" name="%s" value="%s">',
                                                              $this->Id, $action,                           $this->SubmitName, $value_field_name );
      default:
        return str_replace( "\n", "<br />", $field_value );
    }
  }
Editor::SetBaseTable ( base_table)

Definition at line 212 of file classEditor.php.

                                       {
    $this->BaseTable = $base_table;
  }
Editor::SetJoins ( join_list)

Definition at line 216 of file classEditor.php.

                                  {
    $this->Joins = $join_list;
  }
Editor::SetLookup ( field,
lookup_sql 
)

Definition at line 169 of file classEditor.php.

                                            {
    if (is_object($this->Fields[$field])) {
      $this->Fields[$field]->SetLookup( $lookup_sql );
    }
  }
Editor::SetOptionList ( field,
options,
current = null,
parameters = null 
)

Definition at line 203 of file classEditor.php.

                                                                                 {
    $this->Fields[$field]->SetOptionList( $options, $current, $parameters );
  }
Editor::SetRecord ( row)

Definition at line 292 of file classEditor.php.

                             {
    $this->Record = $row;
    $this->RecordAvailable = is_object($this->Record);
    return $this->Record;
  }
Editor::SetSql ( field,
sql 
)

Definition at line 165 of file classEditor.php.

                                  {
    $this->Fields[$field]->SetSql( $sql );
  }
Editor::SetSubmitName ( new_submit)

Definition at line 233 of file classEditor.php.

                                        {
    $this->SubmitName = $new_submit;
  }
Editor::SetTemplate ( template)

Definition at line 278 of file classEditor.php.

                                    {
    $this->Template = $template;
  }
Editor::SetWhere ( where_clause)

Definition at line 254 of file classEditor.php.

                                     {
    $this->Where = $where_clause;
  }
Editor::Title ( new_title = null)

Accessor for the Title for the browse, which could set the title also.

Parameters:
string$new_titleThe new title for the browser
Returns:
string The current title for the browser

Definition at line 227 of file classEditor.php.

                                      {
    if ( isset($new_title) ) $this->Title = $new_title;
    return $this->Title;
  }

Here is the caller graph for this function:

Editor::Value ( value_field_name)

Gets the value of a field in the record currently assigned to this editor.

Parameters:
string$value_field_name

Definition at line 179 of file classEditor.php.

                                      {
    if ( !isset($this->Record->{$value_field_name}) ) return null;
    return $this->Record->{$value_field_name};
  }
Editor::WhereNewRecord ( where_clause)

Definition at line 258 of file classEditor.php.

                                           {
    $this->NewWhere = $where_clause;
  }
Editor::Write ( is_update = null)

Write the record.

Parameters:
boolean$is_updateTell the write whether it's an update or insert. Hopefully it should be able to figure it out though.
Todo:
Our old approach will not work for translation. We need to have a hidden field containing the submittype. Probably we should add placeholders like ##form##, ##script## etc. which the editor can use for internal purposes.

Definition at line 495 of file classEditor.php.

                                      {
    global $c, $component;

    @dbg_error_log( 'editor', 'DBG: Writing editor %s', $this->Title);

    if ( !isset($is_update) ) {
      if ( isset( $_POST['_editor_action']) && isset( $_POST['_editor_action'][$this->Id]) ) {
        $is_update = ( $_POST['_editor_action'][$this->Id] == 'update' );
      }
      else {
        // Then we dvine the action by looking at the submit button value...
        $is_update = preg_match( '/(save|update|apply)/i', $_POST[$this->SubmitName] );
        dbg_error_log('WARN', $_SERVER['REQUEST_URI']. " is using a deprecated method for controlling insert/update" );
      }
    }
    $this->Action = ( $is_update ? "update" : "create" );
    $qry = new AwlQuery( sql_from_post( $this->Action, $this->BaseTable, "WHERE ".$this->Where ) );
    if ( !$qry->Exec("Editor::Write") ) {
      $c->messages[] = "ERROR: $qry->errorstring";
      return 0;
    }
    if ( $this->Action == "create" && isset($this->NewWhere) ) {
      $this->GetRecord($this->NewWhere);
    }
    else {
      $this->GetRecord($this->Where);
    }
    return $this->Record;
  }

Here is the call graph for this function:


Member Data Documentation

Definition at line 114 of file classEditor.php.

Definition at line 117 of file classEditor.php.

Definition at line 115 of file classEditor.php.

Definition at line 128 of file classEditor.php.

Definition at line 118 of file classEditor.php.

Definition at line 122 of file classEditor.php.

Definition at line 120 of file classEditor.php.

Definition at line 121 of file classEditor.php.

Definition at line 116 of file classEditor.php.

Definition at line 123 of file classEditor.php.

Definition at line 126 of file classEditor.php.

Definition at line 125 of file classEditor.php.

Definition at line 127 of file classEditor.php.

Definition at line 124 of file classEditor.php.

Definition at line 113 of file classEditor.php.

Definition at line 119 of file classEditor.php.


The documentation for this class was generated from the following file: