6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
20 * Formatted text class to handle any text that can contain BBCodes, smilies, magic URLs or under word censor.
32 * Internal representation (first_pass data).
39 * Formatting options as bit flag.
48 * Compiled $text. For dispaly.
55 * Set to true if text, meta or flags have been changed, false otherwise.
66 private $update_table = '';
69 * Column in $update_table to update.
73 private $update_column = '';
76 * Where clause for auto update.
80 private $update_where = '';
83 * Creates a new instance.
89 public function __construct($text, $meta = '', $flags = 0)
93 $this->flags
= $flags;
98 $this->changed
= true;
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
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.
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
149 public function to_db($table, $column, $where = '1')
151 $this->changed
= false;
153 $sql = 'UPDATE ' . $table . ' SET ' . phpbb
::$db->sql_build_query('UPDATE', $this->to_db_data($column))
154 . ' WHERE ' . $where;
155 return (bool) phpbb
::$db->sql_query($sql);
159 * Returns an array containing $column, {$column}_meta and {$column}_flags
160 * indexes to be used with query generating functions.
162 * @param string $column
165 public function to_db_data($column)
168 return array($column => $this->text
, $column . '_meta' => $this->meta
, $column . '_flags' => $this->flags
);
172 * Enable automatic database update on
174 * @param string $table
175 * @param string $column
176 * @param string $where
178 public function set_auto_update($table, $column, $where = '1')
180 $this->update_table
= $table;
181 $this->update_column
= $column;
182 $this->update_where
= $where;
186 * Sets $meta if not set.
188 private function set_meta()
190 if (strlen($this->meta
))
195 $parser = new phpbb_bbcode_parser
;
196 $this->meta
= $parser->first_pass($this->text
);
200 * Sets $compiled if not set.
202 private function set_compiled()
206 if (strlen($this->compiled
))
211 $parser = new phpbb_bbcode_parser
;
212 $parser->set_flags($this->flags
);
213 $this->compiled
= $parser->second_pass($this->meta
);
219 * @param string $text
221 public function set_text($text)
223 if ($this->text
!= $text)
225 $this->text
= (string) $text;
227 $this->compiled
= '';
236 public function set_flags($flags)
238 $flags = (int) $flags;
239 if ($this->flags
!= $flags)
241 $this->flags
= $flags;
242 $this->compiled
= '';
247 * Returns the current text.
251 public function get_text()
257 * Returns the current(!!) metadata.
261 public function get_meta()
267 * Returns the current flags.
271 public function get_flags()
277 * Returns true if $this is equal to $other.
278 * Objects are only equal if $text and $flags are equal.
280 * @param formatted_text $other
283 public function eq(formatted_text
$other)
285 return $this->flags
== $other->get_flags() && $this->text
== $other->get_text();
289 * Cast to string. Object is represented by the formatted version of $text with respect to $flags.
293 public function __toString()
295 return $this->to_display();