Development

/branches/1.3/lib/helper/TagHelper.php

You must first sign up to be able to contribute.

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

Revision 23810, 4.3 kB (checked in by Kris.Wallsmith, 5 years ago)

[1.3] set svn:eol-style property to native and svn:keywords property to Id on all .php files

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
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  * Wraps the content in conditional comments.
57  *
58  * @param  string $condition
59  * @param  string $content
60  *
61  * @return string
62  *
63  * @see http://msdn.microsoft.com/en-us/library/ms537512(VS.85).aspx
64  */
65 function comment_as_conditional($condition, $content)
66 {
67   return "<!--[if $condition]>$content<![endif]-->";
68 }
69
70 /**
71  * Escape carrier returns and single and double quotes for Javascript segments.
72  */
73 function escape_javascript($javascript = '')
74 {
75   $javascript = preg_replace('/\r\n|\n|\r/', "\\n", $javascript);
76   $javascript = preg_replace('/(["\'])/', '\\\\\1', $javascript);
77
78   return $javascript;
79 }
80
81 /**
82  * Escapes an HTML string.
83  *
84  * @param  string $html HTML string to escape
85  * @return string escaped string
86  */
87 function escape_once($html)
88 {
89   return fix_double_escape(htmlspecialchars($html, ENT_COMPAT, sfConfig::get('sf_charset')));
90 }
91
92 /**
93  * Fixes double escaped strings.
94  *
95  * @param  string $escaped HTML string to fix
96  * @return string fixed escaped string
97  */
98 function fix_double_escape($escaped)
99 {
100   return preg_replace('/&amp;([a-z]+|(#\d+)|(#x[\da-f]+));/i', '&$1;', $escaped);
101 }
102
103 function _tag_options($options = array())
104 {
105   $options = _parse_attributes($options);
106
107   $html = '';
108   foreach ($options as $key => $value)
109   {
110     $html .= ' '.$key.'="'.escape_once($value).'"';
111   }
112
113   return $html;
114 }
115
116 function _parse_attributes($string)
117 {
118   return is_array($string) ? $string : sfToolkit::stringToArray($string);
119 }
120
121 function _get_option(&$options, $name, $default = null)
122 {
123   if (array_key_exists($name, $options))
124   {
125     $value = $options[$name];
126     unset($options[$name]);
127   }
128   else
129   {
130     $value = $default;
131   }
132
133   return $value;
134 }
135
136 /**
137  * Returns a formatted ID based on the <i>$name</i> parameter and optionally the <i>$value</i> parameter.
138  *
139  * This function determines the proper form field ID name based on the parameters. If a form field has an
140  * array value as a name we need to convert them to proper and unique IDs like so:
141  * <samp>
142  *  name[] => name (if value == null)
143  *  name[] => name_value (if value != null)
144  *  name[bob] => name_bob
145  *  name[item][total] => name_item_total
146  * </samp>
147  *
148  * <b>Examples:</b>
149  * <code>
150  *  echo get_id_from_name('status[]', '1');
151  * </code>
152  *
153  * @param  string $name   field name
154  * @param  string $value  field value
155  *
156  * @return string <select> tag populated with all the languages in the world.
157  */
158 function get_id_from_name($name, $value = null)
159 {
160   // check to see if we have an array variable for a field name
161   if (strstr($name, '['))
162   {
163     $name = str_replace(array('[]', '][', '[', ']'), array((($value != null) ? '_'.$value : ''), '_', '_', ''), $name);
164   }
165
166   return $name;
167 }
168
169 /**
170  * Converts specific <i>$options</i> to their correct HTML format
171  *
172  * @param  array $options
173  * @return array returns properly formatted options
174  */
175 function _convert_options($options)
176 {
177   $options = _parse_attributes($options);
178
179   foreach (array('disabled', 'readonly', 'multiple') as $attribute)
180   {
181     if (array_key_exists($attribute, $options))
182     {
183       if ($options[$attribute])
184       {
185         $options[$attribute] = $attribute;
186       }
187       else
188       {
189         unset($options[$attribute]);
190       }
191     }
192   }
193
194   return $options;
195 }
196
Note: See TracBrowser for help on using the browser.