Welcome Guest, Not a member yet? Register   Sign In
ActiveRecord for CodeIgniter: Rails-style model interactions
#75

[eluser]beemr[/eluser]
Here's the code after adding 'fields_meta_' to the $watch array:
Code:
/**
     * _fields_meta
     *
     * Uses MySql's show full columns string to display a full array of
     * field metadata.  The optional parameter will return an array of
     * $input, $textarea, and $checkbox arrays ready to be used by the CI
     * form helper. $scope is switched by adding _public _private or _all.
     * _public is any field that is NOT NULL, default!==null, type doesn't
     * contain 'time', or doesn't have 'admin' in comments. _private is
     * the remainder of the fields. _all is all of the fields. 'Boolean'
     * is an assumed type for any tinyint(1) NOT NULL with a default value
     * of 0 or 1.
     *
     * For future: validation rules, POST catching
     *
     * @access    public
     * @param    string
     * @param    string
     * @return    object
     */

    function _fields_meta($scope,$form = FALSE)
    {
        $sql = "SHOW FULL COLUMNS FROM ". $this->_table;
        $query = $this->db->query($sql);
        foreach ($query->result() as $field)
        {
            /* Explanation of the ugly regex:
            **   match until first non '('
            **   then optionally match numbers '\d' inside brackets '\(', '\)
            */
            preg_match('/([^(]+)(\((\d+)\))?/', $field->Type, $matches);
            $type           = sizeof($matches) > 1 ? $matches[1] : null;
            $max_length     = sizeof($matches) > 3 ? $matches[3] : null;

            $F              = new stdClass();
            $F->name        = $field->Field;
            $F->type        = ($type == 'tinyint' && $max_length == 1 && $field->Default == 0|1) ? 'boolean' : $type;
            $F->default     = $field->Default;
            $F->max_length  = $max_length;
            $F->primary_key = ($field->Key == "PRI") ? 1 : 0;
            $F->comment     = $field->Comment;
            $F->collation   = $field->Collation;
            $F->extra       = $field->Extra;
            $F->unique        = ($field->Key == "UNI") ? 1 : 0;
            $F->required    = ($field->Null == "YES") ? 0 : 1;

            $retval[] = $F;
        }
        switch($scope)
        {
            case 'public':
                $public = array();
                foreach ($retval as $field)
                {
                    if ($field->name != 'id' && strpos($field->name,'_id')!=strlen($field->name)-3 && $field->default!==null && $field->required && strpos($field->comment,'admin') === FALSE && strpos($field->type,'time') === FALSE)
                    {
                        array_push($public, $field);
                    }
                }
                $retval = $public;
                break;
            case 'private':
                $private = array();
                foreach ($retval as $field)
                {
                    if (($field->name == 'id' || strpos($field->name,'_id')==strlen($field->name)-3 || (!$field->required && !$field->default) || strstr($field->type,'time')) || strstr($field->comment,'admin'))
                    {
                        array_push($private, $field);
                    }
                }
                $retval = $private;
                break;
            default:
        }
        switch($form)
        {
            case TRUE:
                $input = array();
                $textarea = array();
                $checkbox = array();
                foreach($retval as $field)
                {
                    switch($field->type)
                    {
                        case 'text':
                            array_push($textarea, array(
                                'name' => $field->name,
                                'id' => $field->name,
                                'maxlength' => $field->max_length,
                                'size' => 40
                            ));
                            break;
                        case 'boolean':
                            array_push($checkbox, array(
                                'name' => $field->name,
                                'id' => $field->name,
                                'checked' => ($field->default) ? TRUE : FALSE
                            ));
                            break;
                        default:
                            array_push($input, array(
                                'name' => $field->name,
                                'id' => $field->name,
                                'maxlength' => $field->max_length,
                                'size' => $field->max_length
                            ));
                    }
                }
                $retval = array(
                    'input' => $input,
                    'textarea' => $textarea,
                    'checkbox' => $checkbox
                );
                break;
            default:
        }
        return $retval;
    }
}
?>


Messages In This Thread
ActiveRecord for CodeIgniter: Rails-style model interactions - by El Forum - 01-25-2008, 05:23 PM



Theme © iAndrew 2016 - Forum software by © MyBB