Adjust some files to support new methods...
[phpbb.git] / phpBB / includes / formatted_text.php
blob0e7c1d7ea8d5671cd1ac733dc8a4b6a0d53d5477
1 <?php
2 /**
4 * @package phpBB3
5 * @version $Id$
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * @ignore
14 if (!defined('IN_PHPBB'))
16 exit;
19 /**
20 * Formatted text class to handle any text that can contain BBCodes, smilies, magic URLs or under word censor.
22 class formatted_text
24 /**
25 * Unformated text.
27 * @var string
29 private $text;
31 /**
32 * Internal representation (first_pass data).
34 * @var string
36 private $meta;
38 /**
39 * Formatting options as bit flag.
41 * @see bbcode_parser
43 * @var int
45 private $flags;
47 /**
48 * Compiled $text. For dispaly.
50 * @var string
52 private $compiled;
54 /**
55 * Set to true if text, meta or flags have been changed, false otherwise.
57 * @var bool
59 private $changed;
61 /**
62 * DB table to update
64 * @var string
66 private $update_table = '';
68 /**
69 * Column in $update_table to update.
71 * @var string
73 private $update_column = '';
75 /**
76 * Where clause for auto update.
78 * @var string
80 private $update_where = '';
82 /**
83 * Creates a new instance.
85 * @param string $text
86 * @param string $meta
87 * @param int $flags
89 public function __construct($text, $meta = '', $flags = 0)
91 $this->text = $text;
92 $this->meta = $meta;
93 $this->flags = $flags;
94 $this->compiled = '';
96 if ($meta == '')
98 $this->changed = true;
100 else
102 $this->changed = false;
106 public function __destruct()
108 if ($this->changed && $this->update_table)
110 $this->to_db($this->update_table, $this->update_column, $this->update_where);
115 * Convieniently initialize a formatted_text object from
116 * a database result set. The array must contain the following indexes:
117 * $column, {$column}_meta and {$column}_flags
120 * @param array $data
121 * @param string $column
122 * @return formatted_text
124 public static function from_db_data(array $data, $column)
126 return new formatted_text($data[$column], $data[$column . '_meta'], (int) $data[$column . '_flags']);
130 * Returns the $text formatted, ready to be displayed on a webpage.
132 * @return string
134 public function to_display()
136 $this->set_compiled();
137 return $this->compiled;
141 * Updates $table, sets $column, {$column}_meta and {$column}_flags.
142 * All 3 columns must exist.
144 * @param string $table
145 * @param string $column
146 * @param string $where
147 * @return bool
149 public function to_db($table, $column, $where = '1')
151 global $db;
152 $this->changed = false;
154 $sql = 'UPDATE ' . $table . ' SET ' . $db->sql_build_query('UPDATE', $this->to_db_data($column))
155 . ' WHERE ' . $where;
156 return (bool) $db->sql_query($sql);
160 * Returns an array containing $column, {$column}_meta and {$column}_flags
161 * indexes to be used with query generating functions.
163 * @param string $column
164 * @return array
166 public function to_db_data($column)
168 $this->set_meta();
169 return array($column => $this->text, $column . '_meta' => $this->meta, $column . '_flags' => $this->flags);
173 * Enable automatic database update on
175 * @param string $table
176 * @param string $column
177 * @param string $where
179 public function set_auto_update($table, $column, $where = '1')
181 $this->update_table = $table;
182 $this->update_column = $column;
183 $this->update_where = $where;
187 * Sets $meta if not set.
189 private function set_meta()
191 if (strlen($this->meta))
193 return;
196 $parser = new phpbb_bbcode_parser;
197 $this->meta = $parser->first_pass($this->text);
201 * Sets $compiled if not set.
203 private function set_compiled()
205 $this->set_meta();
207 if (strlen($this->compiled))
209 return;
212 $parser = new phpbb_bbcode_parser;
213 $parser->set_flags($this->flags);
214 $this->compiled = $parser->second_pass($this->meta);
218 * Sets $text.
220 * @param string $text
222 public function set_text($text)
224 if ($this->text != $text)
226 $this->text = (string) $text;
227 $this->meta = '';
228 $this->compiled = '';
233 * Sets $flags.
235 * @param int $flags
237 public function set_flags($flags)
239 $flags = (int) $flags;
240 if ($this->flags != $flags)
242 $this->flags = $flags;
243 $this->compiled = '';
248 * Returns the current text.
250 * @return string
252 public function get_text()
254 return $this->text;
258 * Returns the current(!!) metadata.
260 * @return string
262 public function get_meta()
264 return $this->meta;
268 * Returns the current flags.
270 * @return int
272 public function get_flags()
274 return $this->flags;
278 * Returns true if $this is equal to $other.
279 * Objects are only equal if $text and $flags are equal.
281 * @param formatted_text $other
282 * @return bool
284 public function eq(formatted_text $other)
286 return $this->flags == $other->get_flags() && $this->text == $other->get_text();
290 * Cast to string. Object is represented by the formatted version of $text with respect to $flags.
292 * @return string
294 public function __toString()
296 return $this->to_display();