pluf2

pluf2 Git Source Tree


Root/src/Pluf/Form/BoundField.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */
 
/**
 * A class to store field, widget and data.
 *
 * Used when rendering a form.
 */
class Pluf_Form_BoundField
{
    public $form = null;
    public $field = null;
    public $name = null;
    public $html_name = null;
    public $label = null;
    public $help_text = null;
    public $errors = array();
 
    public function __construct($form, $field, $name)
    {
        $this->form = $form;
        $this->field = $field;
        $this->name = $name;
        $this->html_name = $this->form->addPrefix($name);
        if ($this->field->label == '') {
            $this->label = mb_ereg_replace('/\_/', '/ /', mb_ucfirst($name));
        } else {
            $this->label = $this->field->label;
        }
        $this->help_text = ($this->field->help_text) ? $this->field->help_text : '';
        if (isset($this->form->errors[$name])) {
            $this->errors = $this->form->errors[$name];
        }
    }
 
    public function render_w($widget=null, $attrs=array())
    {
        if ($widget === null) {
            $widget = $this->field->widget;
        }
        $id = $this->autoId();
        if ($id and !array_key_exists('id', $attrs)
            and !array_key_exists('id', $widget->attrs)) {
            $attrs['id'] = $id;
        }
        if (!$this->form->is_bound) {
            $data = $this->form->initial($this->name);
        } else {
            $data = $this->field->widget->valueFromFormData($this->html_name, $this->form->data);
        }
        return $widget->render($this->html_name, $data, $attrs);
    }
 
    /**
     * Returns the HTML of the label tag.  Wraps the given contents in
     * a <label>, if the field has an ID attribute. Does not
     * HTML-escape the contents. If contents aren't given, uses the
     * field's HTML-escaped label. If attrs are given, they're used as
     * HTML attributes on the <label> tag.
     *
     * @param string Content of the label, will not be escaped (null).
     * @param array Extra attributes.
     * @return string HTML of the label.
     */
    public function labelTag($contents=null, $attrs=array())
    {
        $contents = ($contents) ? $contents : htmlspecialchars($this->label);
        $widget = $this->field->widget;
        $id = (isset($widget->attrs['id'])) ? $widget->attrs['id'] : $this->autoId();
        $_tmp = array();
        foreach ($attrs as $attr=>$val) {
            $_tmp[] = $attr.'="'.$val.'"';
        }
        if (count($_tmp)) {
            $attrs = ' '.implode(' ', $_tmp);
        } else {
            $attrs = '';
        }
        return new Pluf_Template_SafeString(sprintf('<label for="%s"%s>%s</label>',
                                                    $widget->idForLabel($id), $attrs, $contents), true);
    }
 
 
    /**
     * Calculates and returns the ID attribute for this BoundField, if
     * the associated Form has specified auto_id. Returns an empty
     * string otherwise.
     *
     * @return string Id or empty string if no auto id defined.
     */
    public function autoId()
    {
        $id_fields = $this->form->id_fields;
        if (false !== strpos($id_fields, '%s')) {
            return sprintf($id_fields, $this->html_name);
        } elseif ($id_fields) {
            return $this->html_name;
        }
        return '';
    }
 
    /**
     * Return HTML to display the errors.
     */
    public function fieldErrors()
    {
        Pluf::loadFunction('Pluf_Form_renderErrorsAsHTML');
        return new Pluf_Template_SafeString(Pluf_Form_renderErrorsAsHTML($this->errors), true);
    }
 
    /**
     * Overloading of the property access.
     */
    public function __get($prop)
    {
        if (!in_array($prop, array('labelTag', 'fieldErrors', 'render_w'))) {
            return $this->$prop;
        }
        return $this->$prop();
    }
 
 
    /**
     * Render as string.
     */
    public function __toString()
    {
        return (string)$this->render_w();
    }
}
 
if (!function_exists('mb_ucfirst')) {
    function mb_ucfirst($str) {
        return mb_strtoupper(mb_substr($str, 0, 1)).mb_substr($str, 1);
    }
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.08640s using 11 queries.