]>
src.bluestatic.org Git - isso.git/blob - PrinterCss.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright ©2002-[#]year[#] 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 [#]gpl[#] 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 ©2002 - [#]year[#], Blue Static
52 private $descriptors = array();
58 private $masterdata = array();
64 private $customdata = array();
67 * Valid properties that can be used in CSS
70 private $properties = array(
71 'background' => 'background',
73 'font-style' => 'font_style',
74 'font-size' => 'font_size',
75 'font-family' => 'font_family',
76 'text-decoration' => 'text_decoration'
80 * Hook ran in _fetchModifiedLink; takes three params: $descriptor, $property, $name
83 private $modifiedLinkHook = ':undefined:';
85 // ###################################################################
89 public function __construct()
91 BSApp
::RequiredModules(array('Db', 'Printer'));
94 // ###################################################################
96 * Sets the function callback that creates a link for modified properties
97 * in the CSS blocks display. It should have the signature:
98 * public string _M(string $descriptor, string $property, string $name)
100 * @param string Callback function
102 public function setModifiedLinkHook($callback)
104 $this->modifiedLinkHook
= $callback;
107 // ###################################################################
109 * Adds a CSS information block to the array for later use
111 * @param string Block title to display in thead
112 * @param string CSS class/descriptor/element name
113 * @param bool Show the link CSS information
115 public function addBlock($title, $descriptor, $dolink)
117 if (isset($this->descriptors
["$descriptor"]))
119 trigger_error('The descriptor "' . $descriptor . '" already exists');
123 $this->descriptors["$descriptor"] = array(
125 'descriptor' => $descriptor,
130 // ###################################################################
132 * Sets a master data key for a given descriptor and property
134 * @param string Descriptor
135 * @param string Property
136 * @param string Value
138 public function setMasterData($descriptor, $property, $value)
140 $this->masterdata
["$descriptor"]["$property"] = $value;
143 // ###################################################################
145 * Sets a custom data key for a given descriptor and property
147 * @param string Descriptor
148 * @param string Property
149 * @param string Value
151 public function setCustomData($descriptor, $property, $value)
153 $this->customdata
["$descriptor"]["$property"] = $value;
156 // ###################################################################
158 * Generates the HTML needed to output the CSS editing blocks; this is
159 * done in the form of using BSPrinter
161 public function printEditor()
164 'standard_css_attributes' => _('Standard CSS Attributes'),
165 'extra_css_attributes' => _('Extra CSS Attributes'),
167 'links_normal' => _('Normal CSS Links'),
168 'links_visited' => _('Visited CSS Links'),
169 'links_hover' => _('Hover CSS Links'),
171 'background' => _('Background'),
172 'font_color' => _('Font Color'),
173 'font_style' => _('Font Style'),
174 'font_size' => _('Font Size'),
175 'font_family' => _('Font Family'),
177 'text_decoration' => _('Text Decoration'),
179 'css_selector' => _('CSS Selector'),
180 'save_css' => _('Save CSS')
183 foreach ($this->descriptors
AS $descriptor)
188 $desc = $descriptor['descriptor'];
190 $table = new BSPrinterRootElementTable();
191 $head = new BSPrinterTableElement(new BSPrinterLabelElement($descriptor['title']));
192 $head->setCssClass('tcat');
193 $table->addHeadingChild($head);
195 foreach ($this->properties
AS $prop => $name)
197 $value["$name"] = $this->_fetchValue($descriptor['descriptor'], $prop);
198 $status["$name"] = $this->_fetchModifiedStatus($descriptor['descriptor'], $prop);
201 $value['extra'] = $this->_fetchValue($descriptor['descriptor'], 'extra');
203 $html = "<table cellspacing=\"0\" cellpadding=\"4\" border=\"0\" width=\"100%\">
207 <legend><strong>$lang[standard_css_attributes]</strong></legend>
209 <table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" width=\"100%\">
211 <td width=\"45%\">" . $this->_fetchModifiedLink($desc, 'background', $lang['background']) . "</td>
212 <td><input name=\"css[$descriptor[descriptor]][background]\" class=\"input\" style=\"width: 100%\" value=\"$value[background]\" /></td>
215 <td width=\"45%\">" . $this->_fetchModifiedLink($desc, 'color', $lang['font_color']) . "</td>
216 <td><input name=\"css[$descriptor[descriptor]][color]\" class=\"input\" style=\"width: 100%\" value=\"$value[color]\" /></td>
219 <td width=\"45%\">" . $this->_fetchModifiedLink($desc, 'font-style', $lang['font_style']) . "</td>
220 <td><input name=\"css[$descriptor[descriptor]][font-style]\" class=\"input\" style=\"width: 100%\" value=\"$value[font_style]\" /></td>
223 <td width=\"45%\">" . $this->_fetchModifiedLink($desc, 'font-size', $lang['font_size']) . "</td>
224 <td><input name=\"css[$descriptor[descriptor]][font-size]\" class=\"input\" style=\"width: 100%\" value=\"$value[font_size]\" /></td>
227 <td width=\"45%\">" . $this->_fetchModifiedLink($desc, 'font-family', $lang['font_family']) . "</td>
228 <td><input name=\"css[$descriptor[descriptor]][font-family]\" class=\"input\" style=\"width: 100%\" value=\"$value[font_family]\" /></td>
235 <fieldset style=\"height: 115px\">
236 <legend><strong>" . $this->_fetchModifiedLink($desc, 'extra', $lang['extra_css_attributes']) . "</strong></legend>
237 <textarea name=\"css[$descriptor[descriptor]][extra]\" style=\"width: 100%; height: 90%\">$value[extra]</textarea>
242 if ($descriptor['dolink'])
244 foreach (array('a:link' => 'a_link', 'a:visited' => 'a_visited', 'a:hover' => 'a_hover') AS $sel => $selname)
246 foreach (array('background' => 'background', 'color' => 'color', 'text-decoration' => 'text_decoration') AS $prop => $name)
248 $value["{$selname}_{$name}"] = $this->_fetchValue($descriptor['descriptor'] . ' ' . $sel, $prop);
249 $status["{$selname}_{$name}"] = $this->_fetchModifiedStatus($descriptor['descriptor'] . ' ' . $sel, $prop);
255 <table cellspacing=\"0\" cellpadding=\"4\" border=\"0\" width=\"100%\">
259 <legend><strong>$lang[links_normal]</strong></legend>
261 <table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" width=\"100%\">
263 <td width=\"45%\">" . $this->_fetchModifiedLink($desc . ' a:link', 'background', $lang['background']) . "</td>
264 <td><input name=\"css[$descriptor[descriptor] a:link][background]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_link_background]\" /></td>
267 <td width=\"45%\">" . $this->_fetchModifiedLink($desc . ' a:link', 'color', $lang['font_color']) . "</td>
268 <td><input name=\"css[$descriptor[descriptor] a:link][color]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_link_color]\" /></td>
271 <td width=\"45%\">" . $this->_fetchModifiedLink($desc . ' a:link', 'text-decoration', $lang['text_decoration']) . "</td>
272 <td><input name=\"css[$descriptor[descriptor] a:link][text-decoration]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_link_text_decoration]\" /></td>
280 <legend><strong>$lang[links_visited]</strong></legend>
282 <table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" width=\"100%\">
284 <td width=\"45%\">" . $this->_fetchModifiedLink($desc . ' a:visited', 'background', $lang['background']) . "</td>
285 <td><input name=\"css[$descriptor[descriptor] a:visited][background]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_visited_background]\" /></td>
288 <td width=\"45%\">" . $this->_fetchModifiedLink($desc . ' a:visited', 'color', $lang['font_color']) . "</td>
289 <td><input name=\"css[$descriptor[descriptor] a:visited][color]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_visited_color]\" /></td>
292 <td width=\"45%\">" . $this->_fetchModifiedLink($desc . ' a:visited', 'text-decoration', $lang['text_decoration']) . "</td>
293 <td><input name=\"css[$descriptor[descriptor] a:visited][text-decoration]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_visited_text_decoration]\" /></td>
301 <legend><strong>$lang[links_hover]</strong></legend>
303 <table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" width=\"100%\">
305 <td width=\"45%\">" . $this->_fetchModifiedLink($desc . ' a:hover', 'background', $lang['background']) . "</td>
306 <td><input name=\"css[$descriptor[descriptor] a:hover][background]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_hover_background]\" /></td>
309 <td width=\"45%\">" . $this->_fetchModifiedLink($desc . ' a:hover', 'color', $lang['font_color']) . "</td>
310 <td><input name=\"css[$descriptor[descriptor] a:hover][color]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_hover_color]\" /></td>
313 <td width=\"45%\">" . $this->_fetchModifiedLink($desc . ' a:hover', 'text-decoration', $lang['text_decoration']) . "</td>
314 <td><input name=\"css[$descriptor[descriptor] a:hover][text-decoration]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_hover_text_decoration]\" /></td>
323 $row = new BSPrinterTableElement(new BSPrinterLabelElement($html));
324 $row->setCssClass('alt2');
325 $table->addChild($row);
327 $row = new BSPrinterTableElement(new BSPrinterLabelElement('
328 <div class="alt1" style="border: inset 1px; padding: 2px 5px 2px 5px; float: left">' . $lang['css_selector'] . ': <code>' . $descriptor['descriptor'] . '</code></div>
329 <input type="submit" name="submit" value="' . $lang['save_css'] . '" class="button" />'));
330 $row->setCssClass('tfoot');
331 $row->setStyle(array('text-align' => 'right'));
332 $table->addChild($row)
334 echo $table->paint();
338 // ###################################################################
340 * Returns the value of a given descriptor and property by comparing
341 * the mater set and custom set then returning the right one
343 * @param string Descriptor
344 * @param string Property
346 * @return string Value of the given property
348 private function _fetchValue($descriptor, $property)
350 if (!isset($this->customdata
["$descriptor"]["$property"]))
352 return $this->masterdata
["$descriptor"]["$property"];
356 return $this->customdata
["$descriptor"]["$property"];
360 // ###################################################################
362 * Returns the state modified state (false for untouched and true
363 * for modified) from the descriptor-property value between the master
364 * set and the custom set of data
366 * @param string Descriptor
367 * @param string Property
369 * @return bool Modified from the master value?
371 private function _fetchModifiedStatus($descriptor, $property)
373 return ($this->masterdata
["$descriptor"]["$property"] != $this->customdata
["$descriptor"]["$property"] AND isset($this->customdata
["$descriptor"]["$property"]));
376 // ###################################################################
378 * Fetches a link that shows a revert link for a given property
379 * that uses AJAX to revert when clicked
381 * @param string Descriptor
382 * @param string Property
383 * @param string Nominalized text
385 * @return string Output HTML
387 private function _fetchModifiedLink($descriptor, $property, $name)
389 $status = $this->_fetchModifiedStatus($descriptor, $property);
393 if (is_callable($this->modifiedLinkHook
))
395 return call_user_func($this->modifiedLinkHook
, $descriptor, $property, $name);
399 trigger_error('BSPrinterCss::_fetchModifiedLink() needs to have the fetchModifiedLinkHook( $descriptor , $property , $name ) defined');
408 // ###################################################################
410 * Generates an array of queries that should be run on your database to
411 * update CSS changes. All of the queries have sprintf() markers that
412 * need to be evaluated:
414 * %1$s - Database table
415 * %2$s - styleid field
416 * %3$s - descriptor field
417 * %4$s - property field
419 * %6$d - Styleid value
421 * @param array Array of user-inputted information to be transformed into queries
423 * @return array Queries that need to be evaluated then ran
425 public function fetchChangeQuery($data)
427 $queries[0] = '--- RESERVED FOR LATER USE ---';
431 foreach ($this->descriptors
AS $descriptor => $opts)
433 $dolink = $opts['dolink'];
437 $loops = array('', ' a:link', ' a:visited', ' a:hover');
444 foreach ($loops AS $sel)
446 foreach ($data["$descriptor$sel"] AS $prop => $value)
448 // the given value matches the master -- no change
449 if ($this->masterdata["$descriptor$sel"]["$prop"] == $value)
453 // the given matches the custom -- no change
454 else if (isset($this->customdata["$descriptor$sel"]["$prop"]) AND $this->customdata["$descriptor$sel"]["$prop"] == $value)
458 // no matching, it's new
461 $value = str_replace('%', '%%', $value);
462 $deletes[] = "%
3\$s
= '" . $this->_escape($descriptor . $sel) . "' AND %
4\$s
= '" . $this->_escape($prop) . "'";
463 $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) . "')";
469 if (sizeof($deletes) < 1)
475 $queries[0] = "DELETE FROM %
1\$s WHERE styleid
= %
6\$d
AND ((" . implode(') OR (', $deletes) . "))";
481 // ###################################################################
483 * Wrapper for BSDb->escapeString()
485 * @param string Unprotected string
487 * @return string Sanitized string
489 private function _escape($string)
491 return BSApp::GetType('Db')->escapeString($string);
494 // ###################################################################
496 * Generates a linkable/usable CSS stylehseet content file; this can
497 * be outputted to the browser
499 * @return string CSS output
501 public function fetchCssOutput()
505 foreach ($this->descriptors AS $descriptor => $opts)
507 $dolink = $opts['dolink'];
511 $loops = array('', ' a:link', ' a:visited', ' a:hover');
518 foreach ($loops AS $sel)
520 foreach ($this->masterdata["$descriptor$sel"] AS $prop => $value)
522 $data["$descriptor$sel"]["$prop"] = $value;
524 if (is_array($this->customdata
["$descriptor$sel"]))
526 foreach ($this->customdata["$descriptor$sel"] AS $prop => $value)
528 $data["$descriptor$sel"]["$prop"] = $value;
534 $output = '/* CSS Style Sheet (generated by BSPrinterCss $Revision$) */';
536 foreach ($data AS $selector => $props)
538 $output .= "\n\n$selector\n{";
539 foreach ($props AS $name => $value)
541 if ($name != 'extra' AND $value != '')
543 $output .= str_replace('"', '"', "\n\t$name: $value;");
549 $extra = explode("\n", BSFunctions
::ConvertLineBreaks($props['extra']));
551 foreach ($extra AS $prop)
553 $output .= "\n\t$prop";
564 /*=====================================================================*\
565 || ###################################################################
568 || ###################################################################
569 \*=====================================================================*/