Development

/branches/1.1/lib/helper/TagHelper.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/helper/TagHelper.php

Revision 9967, 4.0 kB (checked in by fabien, 6 years ago)

moved get_id_from_name() function to TagHelper? as FormHelper? is not always loaded (when sf_compat is off for example)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  * (c) 2004 David Heinemeier Hansson
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 /**
13  * TagHelper defines some base helpers to construct html tags.
14  *
15  * @package    symfony
16  * @subpackage helper
17  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
18  * @author     David Heinemeier Hansson
19  * @version    SVN: $Id$
20  */
21
22 /**
23  * Constructs an html tag.
24  *
25  * @param  string $name     tag name
26  * @param  array  $options  tag options
27  * @param  bool   $open     true to leave tag open
28  * @return string
29  */
30 function tag($name, $options = array(), $open = false)
31 {
32   if (!$name)
33   {
34     return '';
35   }
36
37   return '<'.$name._tag_options($options).(($open) ? '>' : ' />');
38 }
39
40 function content_tag($name, $content = '', $options = array())
41 {
42   if (!$name)
43   {
44     return '';
45   }
46
47   return '<'.$name._tag_options($options).'>'.$content.'</'.$name.'>';
48 }
49
50 function cdata_section($content)
51 {
52   return "<![CDATA[$content]]>";
53 }
54
55 /**
56  * Escape carrier returns and single and double quotes for Javascript segments.
57  */
58 function escape_javascript($javascript = '')
59 {
60   $javascript = preg_replace('/\r\n|\n|\r/', "\\n", $javascript);
61   $javascript = preg_replace('/(["\'])/', '\\\\\1', $javascript);
62
63   return $javascript;
64 }
65
66 /**
67  * Escapes an HTML string.
68  *
69  * @param  string $html HTML string to escape
70  * @return string escaped string
71  */
72 function escape_once($html)
73 {
74   return fix_double_escape(htmlspecialchars($html, ENT_COMPAT, sfConfig::get('sf_charset')));
75 }
76
77 /**
78  * Fixes double escaped strings.
79  *
80  * @param  string $escaped HTML string to fix
81  * @return string fixed escaped string
82  */
83 function fix_double_escape($escaped)
84 {
85   return preg_replace('/&amp;([a-z]+|(#\d+)|(#x[\da-f]+));/i', '&$1;', $escaped);
86 }
87
88 function _tag_options($options = array())
89 {
90   $options = _parse_attributes($options);
91
92   $html = '';
93   foreach ($options as $key => $value)
94   {
95     $html .= ' '.$key.'="'.escape_once($value).'"';
96   }
97
98   return $html;
99 }
100
101 function _parse_attributes($string)
102 {
103   return is_array($string) ? $string : sfToolkit::stringToArray($string);
104 }
105
106 function _get_option(&$options, $name, $default = null)
107 {
108   if (array_key_exists($name, $options))
109   {
110     $value = $options[$name];
111     unset($options[$name]);
112   }
113   else
114   {
115     $value = $default;
116   }
117
118   return $value;
119 }
120
121 /**
122  * Returns a formatted ID based on the <i>$name</i> parameter and optionally the <i>$value</i> parameter.
123  *
124  * This function determines the proper form field ID name based on the parameters. If a form field has an
125  * array value as a name we need to convert them to proper and unique IDs like so:
126  * <samp>
127  *  name[] => name (if value == null)
128  *  name[] => name_value (if value != null)
129  *  name[bob] => name_bob
130  *  name[item][total] => name_item_total
131  * </samp>
132  *
133  * <b>Examples:</b>
134  * <code>
135  *  echo get_id_from_name('status[]', '1');
136  * </code>
137  *
138  * @param  string $name   field name
139  * @param  string $value  field value
140  *
141  * @return string <select> tag populated with all the languages in the world.
142  */
143 function get_id_from_name($name, $value = null)
144 {
145   // check to see if we have an array variable for a field name
146   if (strstr($name, '['))
147   {
148     $name = str_replace(array('[]', '][', '[', ']'), array((($value != null) ? '_'.$value : ''), '_', '_', ''), $name);
149   }
150
151   return $name;
152 }
153
154 /**
155  * Converts specific <i>$options</i> to their correct HTML format
156  *
157  * @param  array $options
158  * @return array returns properly formatted options
159  */
160 function _convert_options($options)
161 {
162   $options = _parse_attributes($options);
163
164   foreach (array('disabled', 'readonly', 'multiple') as $attribute)
165   {
166     if (array_key_exists($attribute, $options))
167     {
168       if ($options[$attribute])
169       {
170         $options[$attribute] = $attribute;
171       }
172       else
173       {
174         unset($options[$attribute]);
175       }
176     }
177   }
178
179   return $options;
180 }
181
Note: See TracBrowser for help on using the browser.