Moving error handling stuff into Loader and Functions
[isso.git] / printer_css.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
6 || #
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.
10 || #
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
14 || # more details.
15 || #
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 \*=====================================================================*/
21
22 /**
23 * Printer - CSS Blocks
24 * printer_css.php
25 *
26 * @package ISSO
27 */
28
29 /**
30 * Printer - CSS Blocks
31 *
32 * This framework works in conjunction with ISSO.Printer to create a unique
33 * CSS editor enviornment.
34 *
35 * Hooks:
36 * $this->fetch_modified_link_hook - Required hook that is run when
37 * preparing revert links for properties
38 *
39 * @author Blue Static
40 * @copyright Copyright ©2002 - [#]year[#], Blue Static
41 * @version $Revision$
42 * @package ISSO
43 *
44 */
45 class Printer_CSS
46 {
47 /**
48 * Framework registry object
49 * @var object
50 */
51 private $registry = null;
52
53 /**
54 * CSS block list
55 * @var array
56 */
57 private $descriptors = array();
58
59 /**
60 * Master data set
61 * @var array
62 */
63 private $masterdata = array();
64
65 /**
66 * Cutomized data set
67 * @var array
68 */
69 private $customdata = array();
70
71 /**
72 * Valid properties that can be used in CSS
73 * @var array
74 */
75 private $properties = array(
76 'background' => 'background',
77 'color' => 'color',
78 'font-style' => 'font_style',
79 'font-size' => 'font_size',
80 'font-family' => 'font_family',
81 'text-decoration' => 'text_decoration'
82 );
83
84 /**
85 * Hook ran in fetch_modified_link; takes three params: $descriptor, $property, $name
86 * @var string
87 */
88 private $fetch_modified_link_hook = ':=NO METHOD=:';
89
90 /**
91 * Module fields
92 * @var array
93 */
94 private $fields = array(
95 'fetch_modified_link_hook' => array(REQ_YES, null, false)
96 );
97
98 // ###################################################################
99 /**
100 * Constructor
101 */
102 public function __construct(&$registry)
103 {
104 $this->registry =& $registry;
105 }
106
107 // ###################################################################
108 /**
109 * Sets an ISSO field
110 *
111 * @param string Field name
112 * @param mixed Value of the field
113 */
114 public function set($name, $value)
115 {
116 $this->registry->do_set($name, $value, 'printer_css');
117 }
118
119 // ###################################################################
120 /**
121 * Gets an ISSO field
122 *
123 * @param string Field name
124 *
125 * @return mixed Value of the field
126 */
127 public function get($fieldname)
128 {
129 return $this->registry->do_get($fieldname, 'printer_css');
130 }
131
132 // ###################################################################
133 /**
134 * Adds a CSS information block to the array for later use
135 *
136 * @param string Block title to display in thead
137 * @param string CSS class/descriptor/element name
138 * @param bool Show the link CSS information
139 */
140 public function add_block($title, $descriptor, $dolink)
141 {
142 if (isset($this->descriptors["$descriptor"]))
143 {
144 trigger_error('The descriptor `' . $descriptor . '` already exists', E_USER_WARNING);
145 return;
146 }
147
148 $this->descriptors["$descriptor"] = array(
149 'title' => $title,
150 'descriptor' => $descriptor,
151 'dolink' => $dolink
152 );
153 }
154
155 // ###################################################################
156 /**
157 * Sets a master data key for a given descriptor and property
158 *
159 * @param string Descriptor
160 * @param string Property
161 * @param string Value
162 */
163 public function set_master_data($descriptor, $property, $value)
164 {
165 $this->masterdata["$descriptor"]["$property"] = $value;
166 }
167
168 // ###################################################################
169 /**
170 * Sets a custom data key for a given descriptor and property
171 *
172 * @param string Descriptor
173 * @param string Property
174 * @param string Value
175 */
176 public function set_custom_data($descriptor, $property, $value)
177 {
178 $this->customdata["$descriptor"]["$property"] = $value;
179 }
180
181 // ###################################################################
182 /**
183 * Generates the HTML needed to output the CSS editing blocks; this is
184 * done in the form of using ISSO.Printer
185 */
186 public function generate_blocks()
187 {
188 $print =& $this->registry->modules['printer'];
189
190 $lang = array(
191 'standard_css_attributes' => $this->registry->modules['localize']->string('Standard CSS Attributes'),
192 'extra_css_attributes' => $this->registry->modules['localize']->string('Extra CSS Attributes'),
193
194 'links_normal' => $this->registry->modules['localize']->string('Normal CSS Links'),
195 'links_visited' => $this->registry->modules['localize']->string('Visited CSS Links'),
196 'links_hover' => $this->registry->modules['localize']->string('Hover CSS Links'),
197
198 'background' => $this->registry->modules['localize']->string('Background'),
199 'font_color' => $this->registry->modules['localize']->string('Font Color'),
200 'font_style' => $this->registry->modules['localize']->string('Font Style'),
201 'font_size' => $this->registry->modules['localize']->string('Font Size'),
202 'font_family' => $this->registry->modules['localize']->string('Font Family'),
203
204 'text_decoration' => $this->registry->modules['localize']->string('Text Decoration'),
205
206 'css_selector' => $this->registry->modules['localize']->string('CSS Selector'),
207 'save_css' => $this->registry->modules['localize']->string('Save CSS')
208 );
209
210 foreach ($this->descriptors AS $descriptor)
211 {
212 $value = array();
213 $status = array();
214
215 $desc = $descriptor['descriptor'];
216
217 $print->table_start();
218 $print->table_head($descriptor['title']);
219
220 foreach ($this->properties AS $prop => $name)
221 {
222 $value["$name"] = $this->fetch_value($descriptor['descriptor'], $prop);
223 $status["$name"] = $this->fetch_modified_status($descriptor['descriptor'], $prop);
224 }
225
226 $value['extra'] = $this->fetch_value($descriptor['descriptor'], 'extra');
227
228 $html = "<table cellspacing=\"0\" cellpadding=\"4\" border=\"0\" width=\"100%\">
229 <tr valign=\"top\">
230 <td width=\"50%\">
231 <fieldset>
232 <legend><strong>$lang[standard_css_attributes]</strong></legend>
233
234 <table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" width=\"100%\">
235 <tr>
236 <td width=\"45%\">" . $this->fetch_modified_link($desc, 'background', $lang['background']) . "</td>
237 <td><input name=\"css[$descriptor[descriptor]][background]\" class=\"input\" style=\"width: 100%\" value=\"$value[background]\" /></td>
238 </tr>
239 <tr>
240 <td width=\"45%\">" . $this->fetch_modified_link($desc, 'color', $lang['font_color']) . "</td>
241 <td><input name=\"css[$descriptor[descriptor]][color]\" class=\"input\" style=\"width: 100%\" value=\"$value[color]\" /></td>
242 </tr>
243 <tr>
244 <td width=\"45%\">" . $this->fetch_modified_link($desc, 'font-style', $lang['font_style']) . "</td>
245 <td><input name=\"css[$descriptor[descriptor]][font-style]\" class=\"input\" style=\"width: 100%\" value=\"$value[font_style]\" /></td>
246 </tr>
247 <tr>
248 <td width=\"45%\">" . $this->fetch_modified_link($desc, 'font-size', $lang['font_size']) . "</td>
249 <td><input name=\"css[$descriptor[descriptor]][font-size]\" class=\"input\" style=\"width: 100%\" value=\"$value[font_size]\" /></td>
250 </tr>
251 <tr>
252 <td width=\"45%\">" . $this->fetch_modified_link($desc, 'font-family', $lang['font_family']) . "</td>
253 <td><input name=\"css[$descriptor[descriptor]][font-family]\" class=\"input\" style=\"width: 100%\" value=\"$value[font_family]\" /></td>
254 </tr>
255 </table>
256 </fieldset>
257 </td>
258
259 <td>
260 <fieldset style=\"height: 115px\">
261 <legend><strong>" . $this->fetch_modified_link($desc, 'extra', $lang['extra_css_attributes']) . "</strong></legend>
262 <textarea name=\"css[$descriptor[descriptor]][extra]\" style=\"width: 100%; height: 90%\">$value[extra]</textarea>
263 </fieldset>
264 </td>
265 </tr>
266 </table>";
267 if ($descriptor['dolink'])
268 {
269 foreach (array('a:link' => 'a_link', 'a:visited' => 'a_visited', 'a:hover' => 'a_hover') AS $sel => $selname)
270 {
271 foreach (array('background' => 'background', 'color' => 'color', 'text-decoration' => 'text_decoration') AS $prop => $name)
272 {
273 $value["{$selname}_{$name}"] = $this->fetch_value($descriptor['descriptor'] . ' ' . $sel, $prop);
274 $status["{$selname}_{$name}"] = $this->fetch_modified_status($descriptor['descriptor'] . ' ' . $sel, $prop);
275 }
276 }
277
278 $html .= "
279
280 <table cellspacing=\"0\" cellpadding=\"4\" border=\"0\" width=\"100%\">
281 <tr valign=\"top\">
282 <td width=\"33%\">
283 <fieldset>
284 <legend><strong>$lang[links_normal]</strong></legend>
285
286 <table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" width=\"100%\">
287 <tr>
288 <td width=\"45%\">" . $this->fetch_modified_link($desc . ' a:link', 'background', $lang['background']) . "</td>
289 <td><input name=\"css[$descriptor[descriptor] a:link][background]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_link_background]\" /></td>
290 </tr>
291 <tr>
292 <td width=\"45%\">" . $this->fetch_modified_link($desc . ' a:link', 'color', $lang['font_color']) . "</td>
293 <td><input name=\"css[$descriptor[descriptor] a:link][color]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_link_color]\" /></td>
294 </tr>
295 <tr>
296 <td width=\"45%\">" . $this->fetch_modified_link($desc . ' a:link', 'text-decoration', $lang['text_decoration']) . "</td>
297 <td><input name=\"css[$descriptor[descriptor] a:link][text-decoration]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_link_text_decoration]\" /></td>
298 </tr>
299 </table>
300 </fieldset>
301 </td>
302
303 <td width=\"33%\">
304 <fieldset>
305 <legend><strong>$lang[links_visited]</strong></legend>
306
307 <table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" width=\"100%\">
308 <tr>
309 <td width=\"45%\">" . $this->fetch_modified_link($desc . ' a:visited', 'background', $lang['background']) . "</td>
310 <td><input name=\"css[$descriptor[descriptor] a:visited][background]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_visited_background]\" /></td>
311 </tr>
312 <tr>
313 <td width=\"45%\">" . $this->fetch_modified_link($desc . ' a:visited', 'color', $lang['font_color']) . "</td>
314 <td><input name=\"css[$descriptor[descriptor] a:visited][color]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_visited_color]\" /></td>
315 </tr>
316 <tr>
317 <td width=\"45%\">" . $this->fetch_modified_link($desc . ' a:visited', 'text-decoration', $lang['text_decoration']) . "</td>
318 <td><input name=\"css[$descriptor[descriptor] a:visited][text-decoration]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_visited_text_decoration]\" /></td>
319 </tr>
320 </table>
321 </fieldset>
322 </td>
323
324 <td width=\"33%\">
325 <fieldset>
326 <legend><strong>$lang[links_hover]</strong></legend>
327
328 <table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" width=\"100%\">
329 <tr>
330 <td width=\"45%\">" . $this->fetch_modified_link($desc . ' a:hover', 'background', $lang['background']) . "</td>
331 <td><input name=\"css[$descriptor[descriptor] a:hover][background]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_hover_background]\" /></td>
332 </tr>
333 <tr>
334 <td width=\"45%\">" . $this->fetch_modified_link($desc . ' a:hover', 'color', $lang['font_color']) . "</td>
335 <td><input name=\"css[$descriptor[descriptor] a:hover][color]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_hover_color]\" /></td>
336 </tr>
337 <tr>
338 <td width=\"45%\">" . $this->fetch_modified_link($desc . ' a:hover', 'text-decoration', $lang['text_decoration']) . "</td>
339 <td><input name=\"css[$descriptor[descriptor] a:hover][text-decoration]\" class=\"input\" style=\"width: 100%\" value=\"$value[a_hover_text_decoration]\" /></td>
340 </tr>
341 </table>
342 </fieldset>
343 </td>
344 </tr>
345 </table>";
346 }
347
348 $print->row_span($html, 'alt2', 'left', 1);
349
350 $print->row_span('
351 <div class="alt1" style="border: inset 1px; padding: 2px 5px 2px 5px; float: left">' . $lang['css_selector'] . ': <code>' . $descriptor['descriptor'] . '</code></div>
352 <input type="submit" name="submit" value="' . $lang['save_css'] . '" class="button" />', 'tfoot', 'right', 1);
353
354 $print->table_end();
355 }
356 }
357
358 // ###################################################################
359 /**
360 * Returns the value of a given descriptor and property by comparing
361 * the mater set and custom set then returning the right one
362 *
363 * @param string Descriptor
364 * @param string Property
365 *
366 * @return string Value of the given property
367 */
368 private function fetch_value($descriptor, $property)
369 {
370 if (!isset($this->customdata["$descriptor"]["$property"]))
371 {
372 return $this->masterdata["$descriptor"]["$property"];
373 }
374 else
375 {
376 return $this->customdata["$descriptor"]["$property"];
377 }
378 }
379
380 // ###################################################################
381 /**
382 * Returns the state modified state (false for untouched and true
383 * for modified) from the descriptor-property value between the master
384 * set and the custom set of data
385 *
386 * @param string Descriptor
387 * @param string Property
388 *
389 * @return bool Modified from the master value?
390 */
391 private function fetch_modified_status($descriptor, $property)
392 {
393 if ($this->masterdata["$descriptor"]["$property"] != $this->customdata["$descriptor"]["$property"] AND isset($this->customdata["$descriptor"]["$property"]))
394 {
395 return true;
396 }
397
398 return false;
399 }
400
401 // ###################################################################
402 /**
403 * Fetches a link that shows a revert link for a given property
404 * that uses AJAX to revert when clicked
405 *
406 * @param string Descriptor
407 * @param string Property
408 * @param string Nominalized text
409 *
410 * @return string Output HTML
411 */
412 private function fetch_modified_link($descriptor, $property, $name)
413 {
414 $status = $this->fetch_modified_status($descriptor, $property);
415
416 if ($status)
417 {
418 if (is_callable($this->fetch_modified_link_hook))
419 {
420 return call_user_func($this->fetch_modified_link_hook, $descriptor, $property, $name);
421 }
422 else
423 {
424 trigger_error('Printer_CSS::fetch_modified_link() needs to have the fetch_modified_link_hook( $descriptor , $property , $name ) defined', E_USER_ERROR);
425 }
426 }
427 else
428 {
429 return $name;
430 }
431 }
432
433 // ###################################################################
434 /**
435 * Generates an array of queries that should be run on your database to
436 * update CSS changes. All of the queries have sprintf() markers that
437 * need to be evaluated:
438 *
439 * %1$s - Database table
440 * %2$s - styleid field
441 * %3$s - descriptor field
442 * %4$s - property field
443 * %5$s - value field
444 * %6$d - Styleid value
445 *
446 * @param array Array of user-inputted information to be transformed into queries
447 *
448 * @return array Queries that need to be evaluated then ran
449 */
450 public function generate_change_query($data)
451 {
452 $queries[0] = '--- RESERVED FOR LATER USE ---';
453
454 $deletes = array();
455
456 foreach ($this->descriptors AS $descriptor => $opts)
457 {
458 $dolink = $opts['dolink'];
459
460 if ($dolink)
461 {
462 $loops = array('', ' a:link', ' a:visited', ' a:hover');
463 }
464 else
465 {
466 $loops = array('');
467 }
468
469 foreach ($loops AS $sel)
470 {
471 foreach ($data["$descriptor$sel"] AS $prop => $value)
472 {
473 // the given value matches the master -- no change
474 if ($this->masterdata["$descriptor$sel"]["$prop"] == $value)
475 {
476 continue;
477 }
478 // the given matches the custom -- no change
479 else if (isset($this->customdata["$descriptor$sel"]["$prop"]) AND $this->customdata["$descriptor$sel"]["$prop"] == $value)
480 {
481 continue;
482 }
483 // no matching, it's new
484 else
485 {
486 $value = str_replace('%', '%%', $value);
487 $deletes[] = "%3\$s = '" . $this->escape($descriptor . $sel) . "' AND %4\$s = '" . $this->escape($prop) . "'";
488 $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) . "')";
489 }
490 }
491 }
492 }
493
494 if (sizeof($deletes) < 1)
495 {
496 $queries[0] = '##';
497 }
498 else
499 {
500 $queries[0] = "DELETE FROM %1\$s WHERE styleid = %6\$d AND ((" . implode(') OR (', $deletes) . "))";
501 }
502
503 return $queries;
504 }
505
506 // ###################################################################
507 /**
508 * Wrapper for $this->registry->modules[ISSO_DB_LAYER]->escape_string()
509 *
510 * @param string Unprotected string
511 *
512 * @return string Sanitized string
513 */
514 private function escape($string)
515 {
516 return $this->registry->modules[ISSO_DB_LAYER]->escape_string($string);
517 }
518
519 // ###################################################################
520 /**
521 * Generates a linkable/usable CSS stylehseet content file; this can
522 * be outputted to the browser
523 *
524 * @return string CSS output
525 */
526 public function generate_css_output()
527 {
528 $data = array();
529
530 foreach ($this->descriptors AS $descriptor => $opts)
531 {
532 $dolink = $opts['dolink'];
533
534 if ($dolink)
535 {
536 $loops = array('', ' a:link', ' a:visited', ' a:hover');
537 }
538 else
539 {
540 $loops = array('');
541 }
542
543 foreach ($loops AS $sel)
544 {
545 foreach ($this->masterdata["$descriptor$sel"] AS $prop => $value)
546 {
547 $data["$descriptor$sel"]["$prop"] = $value;
548 }
549 if (is_array($this->customdata["$descriptor$sel"]))
550 {
551 foreach ($this->customdata["$descriptor$sel"] AS $prop => $value)
552 {
553 $data["$descriptor$sel"]["$prop"] = $value;
554 }
555 }
556 }
557 }
558
559 $output = '/* CSS Style Sheet (generated by ISSO.Printer.CSS $Revision$) */';
560
561 foreach ($data AS $selector => $props)
562 {
563 $output .= "\n\n$selector\n{";
564 foreach ($props AS $name => $value)
565 {
566 if ($name != 'extra' AND $value != '')
567 {
568 $output .= str_replace('&quot;', '"', "\n\t$name: $value;");
569 }
570 }
571
572 if ($props['extra'])
573 {
574 $extra = explode("\n", $this->registry->modules['functions']->convert_line_breaks($props['extra']));
575
576 foreach ($extra AS $prop)
577 {
578 $output .= "\n\t$prop";
579 }
580 }
581
582 $output .= "\n}";
583 }
584
585 return $output;
586 }
587 }
588
589 /*=====================================================================*\
590 || ###################################################################
591 || # $HeadURL$
592 || # $Id$
593 || ###################################################################
594 \*=====================================================================*/
595 ?>