[cvs] [Wiki] changed: NewFieldType

Wiki Guest wiki at wiki.horde.org
Sun Aug 21 20:07:37 PDT 2005


guest [65.19.150.231]  Sun, 21 Aug 2005 20:07:37 -0700

Modified page: http://wiki.horde.org/NewFieldType
New Revision:  15.0
Change log:  Revert

@@ -1,11 +1,10 @@
 + Proposal for Implementation of New Package to Replace Horde_Form_Type:: and Horde_Form_Variable::
 
 ++ Notes
 
-+++ IRC Log From 2004-02-13
 <code>
-<oo> trying to figure out the best way to enable editing of images
+oo> trying to figure out the best way to enable editing of images
                 using horde_form image type
 <oo> because at the moment form data being edited and containing an
                 image field type, drops the image
 <oo> the idea i'm following right now would be that the image data
@@ -89,459 +88,4 @@
 <oo_> plus don't know what i missed but enum/radio are only different in how
           they are rendered.. in the horde_form_type classes one inherits from
           the other
 </code>
-
-+++ Jay's Field:: Class from Cadre Framework
-
-This is an example from the Cadre framework.  I'm certainly not suggesting just taking this and using it, but this class has weathered almost four years of service, so I'm posting it here for discussion in this context.
-
-<php>
-<?php // vi:set sts=4 sw=4 ai:
-/* CADRE - Cronosys web application framework.
- * Copyright (C) 2002 Cronosys, LLC.
- *
- * $Id: class.Field.php,v 1.23 2004/01/22 13:52:39 jasonf Exp $
- * 
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2, or (at your option)
- * any later version.
- * 
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- * 
- * You should have received a copy of the GNU General Public License
- * along with this software; see the file COPYING.  If not, write to
- * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307 USA */
-
-/**
- * The field class is an abstract base class which encapsulates a
- * particular type of field.
- */
-class Field {
-    //{{{ properties
-
-    var $name;
-
-    /**
-     * Array of information about this field, typically fetched from the
-     * <database>fw_fields</database> table.
-     */
-    var $info;
-
-    /**
-     * This is a name => value array of field options.  This is parsed from
-     * field_type or optionally passed in the constructor.
-     * @var array $options
-     */
-    var $options = array();
-
-    //}}}
-    //{{{ constructor
-
-    /**
-     * Construct a new field object.
-     *
-     * @param array $info           This is a key => value array of items
-     *                              describing the field.
-     * @param array $addtl_options  These are additional options which override
-     *                              any options found in $info['field_type'].
-     */
-    Function Field ($info, $addtl_options = array())
-    {
-	$this->info = $info;
-	$this->name = $this->info["field_name"];
-
-        if (isset($info['field_type']))
-            {
-            $work = $info['field_type'];
-            while (strlen($work) > 0)
-                {
-                if (preg_match("/^([a-zA-Z0-9_]+)(?:(=)(\"(?:[^\"\\\\]|\\\\.)*\"|[^,]*))?,?/", $work, $m))
-                    {
-                    if (isset($m[2]) && $m[2] == '=')
-                        {
-                        if (substr($m[3],0,1) == '"' && substr($m[3],-1) == '"')
-                            {
-                            $value = preg_replace('/\\\\(.)/', '\\1',
-                                                  substr($m[3],1,
-                                                         strlen($m[3])-2));
-                            }
-                        else
-                            $value = $m[3];
-                        }
-                    else
-                        $value = true;
-                    $this->options[$m[1]] = $value;
-                    }
-                else
-                    break;
-                $work = substr($work, strlen($m[0]));
-                }
-            }
-
-        $this->options = array_merge($this->options, $addtl_options);
-    }
-
-    //}}}
-    //{{{ Display_Name()
-
-    /**
-     * This method returns a name for the field as it would appear to a
-     * user on a report or an entry screen.  For example, for the database
-     * field <database>user_name</database>, it might return "User Name".
-     *
-     * @returns the field's display name.
-     */
-    Function Display_Name ()
-    {
-	return $this->info["field_display_name"];
-    }
-
-    //}}}
-    //{{{ Has_Field_Type_Flag()
-
-    /**
-     * Determines if <varname/$flag/ is one of the comma-separated values in
-     * this field's <database/field_type/ field.
-     *
-     * @param $flag the flag to check for.
-     * @returns true if the flag is present, false otherwise.
-     */
-    Function Has_Field_Type_Flag ($flag)
-    {
-        return isset($this->options[$flag]) && $this->options[$flag] !== false;
-    }
-
-    //}}}
-    //{{{ Get_Option()
-
-    /**
-     * Retrieves the value of an option as supplied in the `field_type' flag
-     * from the database.
-     *
-     * @param string $name          This is the option's name.
-     * @param mixed $default        This is the deafult value of the option.
-     * @return mixed the option's value or null if not present.
-     */
-    Function Get_Option ($name, $default = null)
-    {
-        return isset($this->options[$name]) ? $this->options[$name] : $default;
-    }
-
-    //}}}
-    //{{{ Get_Db_Value()
-
-    /**
-     * This method converts a representation of the field's value as it
-     * would be received from an HTML form to a form acceptable for the
-     * database.
-     *
-     * For example, a boolean field might appear on an edit form as a
-     * checkbox and return the value "On" if the box is checked.  For a 
-     * boolean field type, this method would convert "On" to "t", which is
-     * the way the PostgreSQL database likes to receive its boolean true.
-     *
-     * This method is not responsible for quoting or escaping the value.
-     *
-     * @param value the value received from the HTML form.
-     * @returns the value you would send to the database.
-     */
-    Function Get_Db_Value ($value)
-    {
-	if (isBlank($value) && $this->Has_Field_Type_Flag('nullable'))
-	    return null;
-	return $value;
-    }
-
-    //}}}
-    //{{{ Get_Filter_Value()
-
-    /**
-     * This method converts the HTTP post data created by the HTML from 
-     * <function/Field::Get_Filter_Html()/ to a "database" value suitable
-     * for passing around and/or sending back to <function/Get_Filter_Html()/.
-     *
-     * @param value the value received from the HTML form.
-     * @returns the value you would pass around.
-     */
-    Function Get_Filter_Value ($value)
-    {
-        return $this->Get_Db_Value ($value);
-    }
-
-    //}}}
-    //{{{ Get_Filter_SQL()
-
-    /**
-     * This method converts a filter value to SQL code to filter a query. The
-     * value here is not the post value, but the value as might be returned
-     * by <function/Field::Get_Filter_Value()/.
-     *
-     * By default, we work like a text field with a keyword search.
-     *
-     * @param $value the filter value to convert.
-     * @returns an expression useful for an SQL "WHERE" clause.
-     */
-    Function Get_Filter_SQL ($column, $value, &$error_message)
-    {
-        if (is_null($value))
-            return "TRUE";
-	$ret = dbKeywordClause($column, $value, $error_message);
-	if (!isBlank ($error_message))
-	    $error_message .= ' in "'.$this->Display_Name() .'"';
-	return $ret;
-    }
-
-    //}}}
-    //{{{ Get_Display_Text()
-
-    /**
-     * This method translates the value into a textual representation that
-     * you might display to a user.  This method does not use HTML or any
-     * other sort of markup; for that, see the 
-     * <function>Get_Display_Html</function> method.
-     *
-     * @param value the value retreived from the database.
-     * @returns a human-readable, plaintext representation of the value.
-     */
-    Function Get_Display_Text ($value)
-    {
-	return $value;
-    }
-
-    //}}}
-    //{{{ Get_Display_Html()
-
-    /**
-     * This method translates a database value into an HTML representation
-     * that you might display to a user.  This method uses HTML markup for
-     * formatting if necessary.
-     *
-     * Implementors of this method in derived classes should not that any
-     * HTML special characters should be properly escaped with 
-     * <function>htmlspecialchars</function>().
-     *
-     * @param value the value retreived from the database.
-     * @returns a human-readable, marked-up representation of the value.
-     */
-    Function Get_Display_Html ($value)
-    {
-	$value = $this->Get_Display_Text ($value);
-	if ( IsBlank ($value) )
-	    return "&nbsp;";
-	else
-	    return htmlspecialchars ($value);
-    }
-
-    //}}}
-    //{{{ Get_Display_TeX()
-
-    /**
-     * This method translates a database value into an TeX representation
-     * that you might display to a user.  This method uses TeX markup for
-     * formatting if necessary.
-     *
-     * Implementors of this method in derived classes should ensure that the
-     * returned string is properly escaped for TeX output or call this base
-     * implementation.
-     *
-     * @param value the value retreived from the database.
-     * @returns a human-readable, marked-up representation of the value.
-     */
-    Function Get_Display_TeX ($value)
-    {
-	$ret = '';
-
-	$value = $this->Get_Display_Text ($value);
-	return texspecialchars($value);
-    }
-
-    //}}}
-    //{{{ Get_Browse_Display_TeX()
-
-    Function Get_Browse_Display_TeX ($value)
-    {
-	return $this->Get_Display_TeX ($value);
-    }
-
-    //}}}
-    //{{{ Get_Browse_Display_Html()
-
-    /**
-     * This method translates a database value into an HTML representation
-     * in much the same way that <function>Get_Display_Html</function>() does;
-     * however, this method may truncate the data or otherwise abbreviate it.
-     * This is designed to use from <classname>Browser</classname> tables.
-     *
-     * @param value the value retreived from the database.
-     * @returns a human-readable, marked up representation of the value.
-     */
-    Function Get_Browse_Display_Html ($value)
-    {
-	return $this->Get_Display_Html ($value);
-    } // Function Get_Browse_Display_Html ()
-
-    //}}}
-    //{{{ Get_Edit_Value()
-
-    /**
-     * This method retreives the most appropriate value for the field.  Note
-     * that if validation failed and the <varname/$base/ doesn't start with
-     * `post' or `view', it won't get the proper value.  That's okay, because
-     * everything should be under `post' or `view' anyway.
-     */
-    Function Get_Edit_Value ($value, $base = 'post[data]')
-    {
-	global $post, $view, $store;
-
-	$postvar = $base."[".$this->info[field_name]."]";
-	if ( $post[validation_failed] )
-	    eval("\$value = \$$postvar;");
-	elseif ( IsSet($store[$postvar]) )
-	    $value = $store[$postvar];
-
-	return ($value);
-    }
-
-    //}}}
-    //{{{ Get_Edit_Html()
-
-    /**
-     * This method translates a database value into an editable HTML
-     * representation.  It generates the necessary form control or controls
-     * and populates them with <parameter>$value</parameter>.
-     *
-     * @param value the current value of the field.
-     * @param base a hack to allow more than one field with the same name
-     *	    on one form.
-     * @returns the HTML for the edit controls.
-     */
-    Function Get_Edit_Html ($value, $base = 'post[data]')
-    {
-	$postvar = $base."[".$this->info[field_name]."]";
-
-	$value = $this->Get_Edit_Value($value, $base);
-
-	return "<INPUT TYPE=\"TEXT\" NAME=\"$postvar\" VALUE=\"" .
-	    htmlspecialchars ($value) . "\" SIZE=\"" .
-	    $this->info["field_width"] . "\"" . ( $this->Has_Field_Type_Flag("small") ? " class=\"smalledit\"" : "" ) . ">";
-    }
-
-    //}}}
-    //{{{ Get_Filter_Html()
-
-    /**
-     * This method translates a database value into editable HTML control(s)
-     * of the sort you would use to search based on that field as opposed
-     * to the sort you would use to edit a value in that field.  This allows
-     * us to search on a date range for a date field, for example.
-     *
-     * @param $value the default filter value.
-     * @param $base the variable's base.
-     * @returns the HTML for the edit control(s).
-     */
-    Function Get_Filter_Html ($value, $base = 'view[filter]')
-    {
-	return $this->Get_Edit_Html ($value, $base);
-    }
-
-    //}}}
-    //{{{ Validate()
-
-    /**
-     * This method is called before processing post data to determine if the
-     * HTML form value is valid.  It is meant to be overridden by date types,
-     * for example, to check that the date format is valid before passing
-     * the information to the database.
-     *
-     * @param value the HTML form value to validate.
-     * @returns an empty string if the input is valid; otherwise, a friendly
-     *	    error message.
-     */
-    Function Validate ($value)
-    {
-	return "";
-    }
-
-    //}}}
-    //{{{ Database_Interface()
-
-    /**
-     * This returns the interface that we use for dispatching field types.
-     */
-    Function Database_Interface ()
-    {
-?>
-    <table name="fw_field">
-	<field name="field_name" type="text" allowNulls="false"/>
-	<field name="field_display_name" type="text"/>
-	<field name="field_edit_type" type="text" allowNulls="false"/>
-	<field name="field_width" type="integer"/>
-	<field name="field_options" type="text"/>
-	<index type="unique">
-	    <indexField name="field_name"/>
-	</index>
-    </table>
-<?php
-    }
-
-    //}}}
-}
-
-//{{{ Get_Field()
-
-/**
- * Finds a field in the fields table, determines it's class, and instantiates
- * the appropriate class for that field.  It is assumed that the class name
- * in the database is the named of a class derived from the Field class.  The
- * class must already exist or be in a file named 
- * <filename>class.<replaceable/classname/.php</filename>.
- *
- * If the field does not exist in the database, some reasonable guesses are
- * made - a bare Field class is implemented, the field's caption is set to
- * $name.
- *
- * The return value is cached, so calling this multiple times for the same
- * field isn't a performance issue.
- *
- * @param string $name          the name of the field.
- * @param array $field_options  allows a screen to send custom info to a
- *                              field object.
- * @returns an instance of a field type class.
- */
-Function Get_Field ($name, $field_options = array())
-{
-    global $db;
-    global $field_cache;
-    if ( !IsBlank ($field_cache[$name]) )
-	return $field_cache[$name];
-    $r = $db->Query ("SELECT * FROM fw_field
-	WHERE field_name = '" . addslashes ($name) . "';");
-    if ( !$r || $r->Row_Count () <= 0 )
-	{
-	$info = array (
-	    "field_name" => $name,
-	    "field_display_name" => $name,
-	    "field_displayable" => "t",
-	    "field_edit_type" => "Field",
-	    "field_width" => 20
-	    );
-	}
-    else
-	$info = $r->Fetch_Row (0);
-    if ( $r )
-	$r->Free ();
-    if ( IsBlank ($info["field_edit_type"]) )
-	$info["field_edit_type"] = 'Field';
-    include_once "class.$info[field_edit_type].php";
-    $field_cache[$name] = new $info['field_edit_type'] ($info, $field_options);
-    return $field_cache[$name];
-}
-
-//}}}
-
-</php>


More information about the cvs mailing list