2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2002-2007 Blue Static
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version 2 of the License.
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
23 * Printer: CSS Editor (PrinterCss.php)
28 require_once('ISSO/Functions.php');
33 * This framework works in conjunction with BSPrinter to create a
34 * CSS editor enviornment.
37 * $this->_fetchModifiedLinkHook - Required hook that is run when
38 * preparing revert links for properties
41 * @copyright Copyright (c)2002 - 2007, Blue Static
51 private $descriptors = array();
57 private $masterdata = array();
63 private $customdata = array();
66 * Valid properties that can be used in CSS
69 private $properties = array(
70 'background' => 'background',
72 'font-style' => 'font_style',
73 'font-size' => 'font_size',
74 'font-family' => 'font_family',
75 'text-decoration' => 'text_decoration'
79 * Hook ran in _fetchModifiedLink; takes three params: $descriptor, $property, $name
82 private $modifiedLinkHook = ':undefined:';
84 // ###################################################################
88 public function __construct()
90 BSApp
::RequiredModules(array('Db', 'Printer'));
93 // ###################################################################
95 * Sets the function callback that creates a link for modified properties
96 * in the CSS blocks display. It should have the signature:
97 * public string _M(string $descriptor, string $property, string $name)
99 * @param string Callback function
101 public function setModifiedLinkHook($callback)
103 $this->modifiedLinkHook
= $callback;
106 // ###################################################################
108 * Adds a CSS information block to the array for later use
110 * @param string Block title to display in thead
111 * @param string CSS class/descriptor/element name
112 * @param bool Show the link CSS information
114 public function addBlock($title, $descriptor, $dolink)
116 if (isset($this->descriptors
["$descriptor"]))
118 trigger_error('The descriptor "' . $descriptor . '" already exists');
122 $this->descriptors
["$descriptor"] = array(
124 'descriptor' => $descriptor,
129 // ###################################################################
131 * Sets a master data key for a given descriptor and property
133 * @param string Descriptor
134 * @param string Property
135 * @param string Value
137 public function setMasterData($descriptor, $property, $value)
139 $this->masterdata
["$descriptor"]["$property"] = $value;
142 // ###################################################################
144 * Sets a custom data key for a given descriptor and property
146 * @param string Descriptor
147 * @param string Property
148 * @param string Value
150 public function setCustomData($descriptor, $property, $value)
152 $this->customdata
["$descriptor"]["$property"] = $value;
155 // ###################################################################
157 * Generates the HTML needed to output the CSS editing blocks; this is
158 * done in the form of using BSPrinter
160 public function printEditor()
163 'standard_css_attributes' => _('Standard CSS Attributes'),
164 'extra_css_attributes' => _('Extra CSS Attributes'),
166 'links_normal' => _('Normal CSS Links'),
167 'links_visited' => _('Visited CSS Links'),
168 'links_hover' => _('Hover CSS Links'),
170 'background' => _('Background'),
171 'font_color' => _('Font Color'),
172 'font_style' => _('Font Style'),
173 'font_size' => _('Font Size'),
174 'font_family' => _('Font Family'),
176 'text_decoration' => _('Text Decoration'),
178 'css_selector' => _('CSS Selector'),
179 'save_css' => _('Save CSS')
182 foreach ($this->descriptors
AS $descriptor)
187 $desc = $descriptor['descriptor'];
189 $table = new BSPrinterRootElementTable();
190 $head = new BSPrinterTableElement(new BSPrinterLabelElement($descriptor['title']));
191 $head->setCssClass('tcat');
192 $table->addHeadingChild($head);
194 foreach ($this->properties
AS $prop => $name)
196 $value["$name"] = $this->_fetchValue($descriptor['descriptor'], $prop);
197 $status["$name"] = $this->_fetchModifiedStatus($descriptor['descriptor'], $prop);
200 $value['extra'] = $this->_fetchValue($descriptor['descriptor'], 'extra');
202 $html = "<table cellspacing=\"0\" cellpadding=\"4\" border=\"0\" width=\"100%\">
206 <legend><strong>$lang[standard_css_attributes]</strong></legend>
208 <table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" width=\"100%\">
210 <td width=\"45%\">" . $this->_fetchModifiedLink($desc, 'background', $lang['background']) . "</td>
211 <td><input name=\"css[$descriptor[descriptor]][background]\" class=\"input\" style=\"width: 100%\" value=\"$value[background]\" /></td>
214 <td width=\"45%\">" . $this->_fetchModifiedLink($desc, 'color', $lang['font_color']) . "</td>
215 <td><input name=\"css[$descriptor[descriptor]][color]\" class=\"input\" style=\"width: 100%\" value=\"$value[color]\" /></td>
218 <td width=\"45%\">" . $this->_fetchModifiedLink($desc, 'font-style', $lang['font_style']) . "</td>
219 <td><input name=\"css[$descriptor[descriptor]][font-style]\" class=\"input\" style=\"width: 100%\" value=\"$value[font_style]\" /></td>
222 <td width=\"45%\">" . $this->_fetchModifiedLink($desc, 'font-size', $lang['font_size']) . "</td>
223 <td><input name=\"css[$descriptor[descriptor]][font-size]\" class=\"input\" style=\"width: 100%\" value=\"$value[font_size]\" /></td>
226 <td width=\"45%\">" . $this->_fetchModifiedLink($desc, 'font-family', $lang['font_family']) . "</td>
227 <td><input name=\"css[$descriptor[descriptor]][font-family]\" class=\"input\" style=\"width: 100%\" value=\"$value[font_family]\" /></td>
234 <fieldset style=\"height: 115px\">
235 <legend><strong>" . $this->_fetchModifiedLink($desc, 'extra', $lang['extra_css_attributes']) . "</strong></legend>
236 <textarea name=\"css[$descriptor[descriptor]][extra]\" style=\"width: 100%; height: 90%\">$value[extra]</textarea>
241 if ($descriptor['dolink'])
243 foreach (array('a:link' => 'a_link', 'a:visited' => 'a_visited', 'a:hover' => 'a_hover') AS $sel => $selname)
245 foreach (array('background' => 'background', 'color' => 'color', 'text-decoration' => 'text_decoration') AS $prop => $name)
247 $value["{$selname}_{$name}"] = $this->_fetchValue($descriptor['descriptor'] . ' ' . $sel, $prop);
248 $status["{$selname}_{$name}"] = $this->_fetchModifiedStatus($descriptor['descriptor'] . ' ' . $sel, $prop);
254 <table cellspacing=\"0\" cellpadding=\"4\" border=\"0\" width=\"100%\">
258 <legend><strong>$lang[links_normal]</strong></legend>
260 <table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" width=\"100%\">
262 <td width=\"45%\">" . $this->_fetchModifiedLink($desc . ' a:link', 'background', $lang['background']) . "</td>
263 <td><input name=\"css[$descriptor[descriptor] a:link][background]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_link_background]\" /></td>
266 <td width=\"45%\">" . $this->_fetchModifiedLink($desc . ' a:link', 'color', $lang['font_color']) . "</td>
267 <td><input name=\"css[$descriptor[descriptor] a:link][color]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_link_color]\" /></td>
270 <td width=\"45%\">" . $this->_fetchModifiedLink($desc . ' a:link', 'text-decoration', $lang['text_decoration']) . "</td>
271 <td><input name=\"css[$descriptor[descriptor] a:link][text-decoration]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_link_text_decoration]\" /></td>
279 <legend><strong>$lang[links_visited]</strong></legend>
281 <table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" width=\"100%\">
283 <td width=\"45%\">" . $this->_fetchModifiedLink($desc . ' a:visited', 'background', $lang['background']) . "</td>
284 <td><input name=\"css[$descriptor[descriptor] a:visited][background]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_visited_background]\" /></td>
287 <td width=\"45%\">" . $this->_fetchModifiedLink($desc . ' a:visited', 'color', $lang['font_color']) . "</td>
288 <td><input name=\"css[$descriptor[descriptor] a:visited][color]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_visited_color]\" /></td>
291 <td width=\"45%\">" . $this->_fetchModifiedLink($desc . ' a:visited', 'text-decoration', $lang['text_decoration']) . "</td>
292 <td><input name=\"css[$descriptor[descriptor] a:visited][text-decoration]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_visited_text_decoration]\" /></td>
300 <legend><strong>$lang[links_hover]</strong></legend>
302 <table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" width=\"100%\">
304 <td width=\"45%\">" . $this->_fetchModifiedLink($desc . ' a:hover', 'background', $lang['background']) . "</td>
305 <td><input name=\"css[$descriptor[descriptor] a:hover][background]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_hover_background]\" /></td>
308 <td width=\"45%\">" . $this->_fetchModifiedLink($desc . ' a:hover', 'color', $lang['font_color']) . "</td>
309 <td><input name=\"css[$descriptor[descriptor] a:hover][color]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_hover_color]\" /></td>
312 <td width=\"45%\">" . $this->_fetchModifiedLink($desc . ' a:hover', 'text-decoration', $lang['text_decoration']) . "</td>
313 <td><input name=\"css[$descriptor[descriptor] a:hover][text-decoration]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_hover_text_decoration]\" /></td>
322 $row = new BSPrinterTableElement(new BSPrinterLabelElement($html));
323 $row->setCssClass('alt2');
324 $table->addChild($row);
326 $row = new BSPrinterTableElement(new BSPrinterLabelElement('
327 <div class="alt1" style="border: inset 1px; padding: 2px 5px 2px 5px; float: left">' . $lang['css_selector'] . ': <code>' . $descriptor['descriptor'] . '</code></div>
328 <input type="submit" name="submit" value="' . $lang['save_css'] . '" class="button" />'));
329 $row->setCssClass('tfoot');
330 $row->setStyle(array('text-align' => 'right'));
331 $table->addChild($row)
333 echo $table->paint();
337 // ###################################################################
339 * Returns the value of a given descriptor and property by comparing
340 * the mater set and custom set then returning the right one
342 * @param string Descriptor
343 * @param string Property
345 * @return string Value of the given property
347 private function _fetchValue($descriptor, $property)
349 if (!isset($this->customdata
["$descriptor"]["$property"]))
351 return $this->masterdata
["$descriptor"]["$property"];
355 return $this->customdata
["$descriptor"]["$property"];
359 // ###################################################################
361 * Returns the state modified state (false for untouched and true
362 * for modified) from the descriptor-property value between the master
363 * set and the custom set of data
365 * @param string Descriptor
366 * @param string Property
368 * @return bool Modified from the master value?
370 private function _fetchModifiedStatus($descriptor, $property)
372 return ($this->masterdata
["$descriptor"]["$property"] != $this->customdata
["$descriptor"]["$property"] AND isset($this->customdata
["$descriptor"]["$property"]));
375 // ###################################################################
377 * Fetches a link that shows a revert link for a given property
378 * that uses AJAX to revert when clicked
380 * @param string Descriptor
381 * @param string Property
382 * @param string Nominalized text
384 * @return string Output HTML
386 private function _fetchModifiedLink($descriptor, $property, $name)
388 $status = $this->_fetchModifiedStatus($descriptor, $property);
392 if (is_callable($this->modifiedLinkHook
))
394 return call_user_func($this->modifiedLinkHook
, $descriptor, $property, $name);
398 trigger_error('BSPrinterCss::_fetchModifiedLink() needs to have the fetchModifiedLinkHook( $descriptor , $property , $name ) defined');
407 // ###################################################################
409 * Generates an array of queries that should be run on your database to
410 * update CSS changes. All of the queries have sprintf() markers that
411 * need to be evaluated:
413 * %1$s - Database table
414 * %2$s - styleid field
415 * %3$s - descriptor field
416 * %4$s - property field
418 * %6$d - Styleid value
420 * @param array Array of user-inputted information to be transformed into queries
422 * @return array Queries that need to be evaluated then ran
424 public function fetchChangeQuery($data)
426 $queries[0] = '--- RESERVED FOR LATER USE ---';
430 foreach ($this->descriptors
AS $descriptor => $opts)
432 $dolink = $opts['dolink'];
436 $loops = array('', ' a:link', ' a:visited', ' a:hover');
443 foreach ($loops AS $sel)
445 foreach ($data["$descriptor$sel"] AS $prop => $value)
447 // the given value matches the master -- no change
448 if ($this->masterdata
["$descriptor$sel"]["$prop"] == $value)
452 // the given matches the custom -- no change
453 else if (isset($this->customdata
["$descriptor$sel"]["$prop"]) AND $this->customdata
["$descriptor$sel"]["$prop"] == $value)
457 // no matching, it's new
460 $value = str_replace('%', '%%', $value);
461 $deletes[] = "%3\$s = '" . $this->_escape($descriptor . $sel) . "' AND %4\$s = '" . $this->_escape($prop) . "'";
462 $queries[] = "INSERT INTO %1\$s (%2\$s, %3\$s, %4\$s, %5\$s) VALUES (%6\$d, '" . $this->_escape($descriptor . $sel) . "', '" . $this->_escape($prop) . "', '" . $this->_escape($value) . "')";
468 if (sizeof($deletes) < 1)
474 $queries[0] = "DELETE FROM %1\$s WHERE styleid = %6\$d AND ((" . implode(') OR (', $deletes) . "))";
480 // ###################################################################
482 * Wrapper for BSDb->escapeString()
484 * @param string Unprotected string
486 * @return string Sanitized string
488 private function _escape($string)
490 return BSApp
::GetType('Db')->escapeString($string);
493 // ###################################################################
495 * Generates a linkable/usable CSS stylehseet content file; this can
496 * be outputted to the browser
498 * @return string CSS output
500 public function fetchCssOutput()
504 foreach ($this->descriptors
AS $descriptor => $opts)
506 $dolink = $opts['dolink'];
510 $loops = array('', ' a:link', ' a:visited', ' a:hover');
517 foreach ($loops AS $sel)
519 foreach ($this->masterdata
["$descriptor$sel"] AS $prop => $value)
521 $data["$descriptor$sel"]["$prop"] = $value;
523 if (is_array($this->customdata
["$descriptor$sel"]))
525 foreach ($this->customdata
["$descriptor$sel"] AS $prop => $value)
527 $data["$descriptor$sel"]["$prop"] = $value;
533 $output = '/* CSS Style Sheet (generated by BSPrinterCss $Revision$) */';
535 foreach ($data AS $selector => $props)
537 $output .= "\n\n$selector\n{";
538 foreach ($props AS $name => $value)
540 if ($name != 'extra' AND $value != '')
542 $output .= str_replace('"', '"', "\n\t$name: $value;");
548 $extra = explode("\n", BSFunctions
::ConvertLineBreaks($props['extra']));
550 foreach ($extra AS $prop)
552 $output .= "\n\t$prop";