ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjStyleSheet.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 
5 require_once "./classes/class.ilObject.php";
6 
16 {
17  var $style;
18 
19  public static $num_unit = array("px", "em", "ex", "%", "pt", "pc", "in", "mm", "cm");
20  public static $num_unit_no_perc = array("px", "em", "ex", "pt", "pc", "in", "mm", "cm");
21 
22  // css parameters and their attribute values, input type and group
23  public static $parameter = array(
24  "font-size" => array(
25  "values" => array("xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large", "smaller", "larger"),
26  "input" => "fontsize",
27  "group" => "text"),
28  "font-family" => array(
29  "values" => array(),
30  "input" => "text",
31  "group" => "text"),
32  "font-style" => array(
33  "values" => array("italic", "oblique", "normal"),
34  "input" => "select",
35  "group" => "text"),
36  "font-weight" => array(
37  "values" => array("bold", "normal", "bolder", "lighter"),
38  "input" => "select",
39  "group" => "text"),
40  "font-variant" => array(
41  "values" => array("small-caps", "normal"),
42  "input" => "select",
43  "group" => "text"),
44  "word-spacing" => array(
45  "values" => array(),
46  "input" => "numeric_no_perc",
47  "group" => "text"),
48  "letter-spacing" => array(
49  "values" => array(),
50  "input" => "numeric_no_perc",
51  "group" => "text"),
52  "text-decoration" => array(
53  "values" => array("underline", "overline", "line-through", "blink", "none"),
54  "input" => "select",
55  "group" => "text"),
56  "text-transform" => array(
57  "values" => array("capitalize", "uppercase", "lowercase", "none"),
58  "input" => "select",
59  "group" => "text"),
60  "color" => array(
61  "values" => array(),
62  "input" => "color",
63  "group" => "text"),
64  "text-indent" => array(
65  "values" => array(),
66  "input" => "numeric",
67  "group" => "text"),
68  "line-height" => array(
69  "values" => array(),
70  "input" => "numeric",
71  "group" => "text"),
72  "vertical-align" => array(
73  "values" => array("top", "middle", "bottom", "baseline", "sub", "super",
74  "text-top", "text-bottom"),
75  "input" => "select",
76  "group" => "text"),
77  "text-align" => array(
78  "values" => array("left", "center", "right", "justify"),
79  "input" => "select",
80  "group" => "text"),
81  "white-space" => array(
82  "values" => array("normal", "pre", "nowrap"),
83  "input" => "select",
84  "group" => "text"),
85  "margin" => array(
86  "values" => array(),
87  "input" => "trbl_numeric",
88  "subpar" => array("margin", "margin-top", "margin-right",
89  "margin-bottom", "margin-left"),
90  "group" => "margin_and_padding"),
91  "padding" => array(
92  "values" => array(),
93  "input" => "trbl_numeric",
94  "subpar" => array("padding", "padding-top", "padding-right",
95  "padding-bottom", "padding-left"),
96  "group" => "margin_and_padding"),
97  "border-width" => array(
98  "values" => array("thin", "medium", "thick"),
99  "input" => "border_width",
100  "subpar" => array("border-width", "border-top-width", "border-right-width",
101  "border-bottom-width", "border-left-width"),
102  "group" => "border"),
103  "border-color" => array(
104  "values" => array(),
105  "input" => "trbl_color",
106  "subpar" => array("border-color", "border-top-color", "border-right-color",
107  "border-bottom-color", "border-left-color"),
108  "group" => "border"),
109  "border-style" => array(
110  "values" => array("none", "hidden", "dotted", "dashed", "solid", "double",
111  "groove", "ridge", "inset", "outset"),
112  "input" => "border_style",
113  "subpar" => array("border-style", "border-top-style", "border-right-style",
114  "border-bottom-style", "border-left-style"),
115  "group" => "border"),
116 
117  "background-color" => array(
118  "values" => array(),
119  "input" => "color",
120  "group" => "background"),
121  "background-image" => array(
122  "values" => array(),
123  "input" => "background_image",
124  "group" => "background"),
125  "background-repeat" => array(
126  "values" => array("repeat", "repeat-x", "repeat-y", "no-repeat"),
127  "input" => "select",
128  "group" => "background"),
129  "background-attachment" => array(
130  "values" => array("fixed", "scroll"),
131  "input" => "select",
132  "group" => "background"),
133  "background-position" => array(
134  "values" => array("horizontal" => array("left", "center", "right"),
135  "vertical" => array("top", "center", "bottom")),
136  "input" => "background_position",
137  "group" => "background"),
138 
139  "position" => array(
140  "values" => array("absolute", "fixed", "relative", "static"),
141  "input" => "select",
142  "group" => "positioning"),
143  "top" => array(
144  "values" => array(),
145  "input" => "numeric",
146  "group" => "positioning"),
147  "bottom" => array(
148  "values" => array(),
149  "input" => "numeric",
150  "group" => "positioning"),
151  "left" => array(
152  "values" => array(),
153  "input" => "numeric",
154  "group" => "positioning"),
155  "right" => array(
156  "values" => array(),
157  "input" => "numeric",
158  "group" => "positioning"),
159  "width" => array(
160  "values" => array(),
161  "input" => "numeric",
162  "group" => "positioning"),
163  "height" => array(
164  "values" => array(),
165  "input" => "numeric",
166  "group" => "positioning"),
167  "min-height" => array(
168  "values" => array(),
169  "input" => "numeric",
170  "group" => "positioning"),
171  "float" => array(
172  "values" => array("left", "right", "none"),
173  "input" => "select",
174  "group" => "positioning"),
175  "overflow" => array(
176  "values" => array("visible", "hidden", "scroll", "auto"),
177  "input" => "select",
178  "group" => "positioning"),
179 
180  "opacity" => array(
181  "values" => array(),
182  "input" => "percentage",
183  "group" => "special"),
184  "cursor" => array(
185  "values" => array("auto", "default", "crosshair", "pointer", "move",
186  "n-resize", "ne-resize", "e-resize", "se-resize", "s-resize", "sw-resize",
187  "w-resize", "nw-resize", "text", "wait", "help"),
188  "input" => "select",
189  "group" => "special"),
190  "clear" => array(
191  "values" => array ("both","left","right","none"),
192  "input" => "select",
193  "group" => "special"),
194 
195  "list-style-type.ol" => array(
196  "values" => array ("decimal","lower-roman","upper-roman",
197  "lower-alpha", "upper-alpha", "lower-greek", "hebrew",
198  "decimal-leading-zero", "cjk-ideographic", "hiragana",
199  "katakana", "hiragana-iroha", "katakana-iroha", "none"),
200  "input" => "select",
201  "group" => "ol"),
202  "list-style-type.ul" => array(
203  "values" => array ("disc","circle","square",
204  "none"),
205  "input" => "select",
206  "group" => "ul"),
207  "list-style-image.ul" => array(
208  "values" => array(),
209  "input" => "background_image",
210  "group" => "ul"),
211  "list-style-position.ol" => array(
212  "values" => array ("inside","outside"),
213  "input" => "select",
214  "group" => "ol"),
215  "list-style-position.ul" => array(
216  "values" => array ("inside","outside"),
217  "input" => "select",
218  "group" => "ul"
219  ),
220  "border-collapse" => array(
221  "values" => array ("collapse","separate"),
222  "input" => "select",
223  "group" => "table"
224  ),
225  "caption-side" => array(
226  "values" => array ("top","bottom","left","right"),
227  "input" => "select",
228  "group" => "table"
229  )
230  );
231 
232  // filter groups of properties that should only be
233  // displayed with matching tag (group -> tags)
234  public static $filtered_groups =
235  array("ol" => array("ol"), "ul" => array("ul"),
236  "table" => array("table"), "positioning" => array("h1", "h2", "h3", "div", "img", "table"));
237 
238  // style types and their super type
239  public static $style_super_types = array(
240  "text_block" => array("text_block", "heading1", "heading2", "heading3"),
241  "text_inline" => array("text_inline"),
242  "section" => array("section"),
243  "link" => array("link"),
244  "table" => array("table", "table_cell", "table_caption"),
245  "list" => array("list_o", "list_u", "list_item"),
246  "flist" => array("flist_cont", "flist_head", "flist", "flist_li", "flist_a"),
247  "media" => array("media_cont", "media_caption"),
248  "tabs" => array("va_cntr", "va_icntr", "va_ihead", "va_icont",
249  "ha_cntr", "ha_icntr", "ha_ihead", "ha_icont"),
250  "question" => array("question", "qtitle", "qanswer", "qinput", "qlinput", "qsubmit", "qfeedr", "qfeedw"),
251  "page" => array("page_frame", "page_cont", "page_title", "page_fn",
252  "page_tnav", "page_bnav", "page_lnav", "page_rnav", "page_lnavlink", "page_rnavlink",
253  "page_lnavimage", "page_rnavimage"),
254  "sco" => array("sco_title", "sco_keyw", "sco_desc", "sco_desct", "sco_obj", "sco_objt")
255  );
256 
257  // these types are expandable, i.e. the user can define new style classes
258  public static $expandable_types = array (
259  "text_block", "section", "media_cont", "table", "table_cell", "flist_li", "table_caption",
260  "list_o", "list_u",
261  "va_cntr", "va_icntr", "va_ihead", "va_icont",
262  "ha_cntr", "ha_icntr", "ha_ihead", "ha_icont"
263  );
264 
265  // these types can be hidden in the content editor
266  public static $hideable_types = array (
267  "table", "table_cell"
268  );
269 
270  // tag that are used by style types
271  public static $assigned_tags = array (
272  "text_block" => "div",
273  "heading1" => "h1",
274  "heading2" => "h2",
275  "heading3" => "h3",
276  "text_inline" => "span",
277  "section" => "div",
278  "link" => "a",
279  "table" => "table",
280  "table_cell" => "td",
281  "table_caption" => "caption",
282  "media_cont" => "table",
283  "media_caption" => "div",
284  "sco_title" => "div",
285  "sco_keyw" => "div",
286  "sco_desc" => "div",
287  "sco_obj" => "div",
288  "sco_desct" => "div",
289  "sco_objt" => "div",
290  "list_o" => "ol",
291  "list_u" => "ul",
292  "list_item" => "li",
293  "flist_cont" => "div",
294  "flist_head" => "div",
295  "flist" => "ul",
296  "flist_li" => "li",
297  "flist_a" => "a",
298  "question" => "div",
299  "qtitle" => "div",
300  "qanswer" => "div",
301  "qinput" => "input",
302  "qlinput" => "textarea",
303  "qsubmit" => "input",
304  "qfeedr" => "div",
305  "qfeedw" => "div",
306  "page_frame" => "table",
307  "page_cont" => "table",
308  "page_fn" => "div",
309  "page_tnav" => "div",
310  "page_bnav" => "div",
311  "page_lnav" => "div",
312  "page_rnav" => "div",
313  "page_lnavlink" => "a",
314  "page_rnavlink" => "a",
315  "page_lnavimage" => "img",
316  "page_rnavimage" => "img",
317  "page_title" => "h1",
318  "va_cntr" => "div",
319  "va_icntr" => "div",
320  "va_icont" => "div",
321  "va_ihead" => "div",
322  "ha_cntr" => "div",
323  "ha_icntr" => "div",
324  "ha_icont" => "div",
325  "ha_ihead" => "div"
326  );
327 
328  // pseudo classes
329  public static $pseudo_classes =
330  array ("a" => array("hover"), "div" => array("hover"), "img" => array("hover"));
331 
332  // core styles these styles MUST exists -> see also basic_style/style.xml
333  public static $core_styles = array(
334  array("type" => "text_block", "class" => "Standard"),
335  array("type" => "text_block", "class" => "List"),
336  array("type" => "text_block", "class" => "TableContent"),
337  array("type" => "heading1", "class" => "Headline1"),
338  array("type" => "heading2", "class" => "Headline2"),
339  array("type" => "heading3", "class" => "Headline3"),
340  array("type" => "text_inline", "class" => "Comment"),
341  array("type" => "text_inline", "class" => "Emph"),
342  array("type" => "text_inline", "class" => "Quotation"),
343  array("type" => "text_inline", "class" => "Strong"),
344  array("type" => "text_inline", "class" => "Accent"),
345  array("type" => "text_inline", "class" => "Important"),
346  array("type" => "link", "class" => "IntLink"),
347  array("type" => "link", "class" => "ExtLink"),
348  array("type" => "link", "class" => "FootnoteLink"),
349  array("type" => "media_cont", "class" => "MediaContainer"),
350  array("type" => "table", "class" => "StandardTable"),
351  array("type" => "media_caption", "class" => "MediaCaption"),
352  array("type" => "page_frame", "class" => "PageFrame"),
353  array("type" => "page_cont", "class" => "PageContainer"),
354  array("type" => "page_tnav", "class" => "TopNavigation"),
355  array("type" => "page_bnav", "class" => "BottomNavigation"),
356  array("type" => "page_lnav", "class" => "LeftNavigation"),
357  array("type" => "page_rnav", "class" => "RightNavigation"),
358  array("type" => "page_lnavlink", "class" => "LeftNavigationLink"),
359  array("type" => "page_rnavlink", "class" => "RightNavigationLink"),
360  array("type" => "page_lnavimage", "class" => "LeftNavigationImage"),
361  array("type" => "page_rnavimage", "class" => "RightNavigationImage"),
362  array("type" => "page_fn", "class" => "Footnote"),
363  array("type" => "page_title", "class" => "PageTitle"),
364  array("type" => "sco_title", "class" => "Title"),
365  array("type" => "sco_desc", "class" => "Description"),
366  array("type" => "sco_desct", "class" => "DescriptionTop"),
367  array("type" => "sco_keyw", "class" => "Keywords"),
368  array("type" => "sco_obj", "class" => "Objective"),
369  array("type" => "sco_objt", "class" => "ObjectiveTop"),
370  array("type" => "list_o", "class" => "NumberedList"),
371  array("type" => "list_u", "class" => "BulletedList"),
372  array("type" => "list_item", "class" => "StandardListItem"),
373  array("type" => "question", "class" => "Standard"),
374  array("type" => "question", "class" => "SingleChoice"),
375  array("type" => "question", "class" => "MultipleChoice"),
376  array("type" => "question", "class" => "TextQuestion"),
377  array("type" => "question", "class" => "OrderingQuestion"),
378  array("type" => "question", "class" => "MatchingQuestion"),
379  array("type" => "question", "class" => "ImagemapQuestion"),
380  array("type" => "question", "class" => "ClozeTest"),
381  array("type" => "qtitle", "class" => "Title"),
382  array("type" => "qanswer", "class" => "Answer"),
383  array("type" => "qinput", "class" => "TextInput"),
384  array("type" => "qlinput", "class" => "LongTextInput"),
385  array("type" => "qsubmit", "class" => "Submit"),
386  array("type" => "qfeedr", "class" => "FeedbackRight"),
387  array("type" => "qfeedw", "class" => "FeedbackWrong"),
388  array("type" => "flist_cont", "class" => "FileListContainer"),
389  array("type" => "flist_head", "class" => "FileListHeading"),
390  array("type" => "flist", "class" => "FileList"),
391  array("type" => "flist_li", "class" => "FileListItem"),
392  array("type" => "flist_a", "class" => "FileListItemLink")
393  );
394 
395  public static $templates = array(
396  "table" => array(
397  "table" => "table",
398  "caption" => "table_caption",
399  "row_head" => "table_cell",
400  "row_foot" => "table_cell",
401  "col_head" => "table_cell",
402  "col_foot" => "table_cell",
403  "odd_row" => "table_cell",
404  "even_row" => "table_cell",
405  "odd_col" => "table_cell",
406  "even_col" => "table_cell"),
407  "vaccordion" => array(
408  "va_cntr" => "va_cntr",
409  "va_icntr" => "va_icntr",
410  "va_ihead" => "va_ihead",
411  "va_icont" => "va_icont"
412  ),
413  "haccordion" => array(
414  "ha_cntr" => "ha_cntr",
415  "ha_icntr" => "ha_icntr",
416  "ha_ihead" => "ha_ihead",
417  "ha_icont" => "ha_icont"
418  )
419  );
420 
421  // basic style xml file, image directory and dom
422  protected static $basic_style_file = "./Services/Style/basic_style/style.xml";
423  protected static $basic_style_image_dir = "./Services/Style/basic_style/images";
424  protected static $basic_style_dom;
425 
432  function ilObjStyleSheet($a_id = 0, $a_call_by_reference = false)
433  {
434  $this->type = "sty";
435  $this->style = array();
436  if($a_call_by_reference)
437  {
438  $this->ilias->raiseError("Can't instantiate style object via reference id.",$this->ilias->error_obj->FATAL);
439  }
440 
441  parent::ilObject($a_id, false);
442  }
443 
447  function setRefId()
448  {
449  $this->ilias->raiseError("Operation ilObjStyleSheet::setRefId() not allowed.",$this->ilias->error_obj->FATAL);
450  }
451 
455  function getRefId()
456  {
457  return "";
458  //$this->ilias->raiseError("Operation ilObjStyleSheet::getRefId() not allowed.",$this->ilias->error_obj->FATAL);
459  }
460 
464  function putInTree()
465  {
466  $this->ilias->raiseError("Operation ilObjStyleSheet::putInTree() not allowed.",$this->ilias->error_obj->FATAL);
467  }
468 
472  function createReference()
473  {
474  $this->ilias->raiseError("Operation ilObjStyleSheet::createReference() not allowed.",$this->ilias->error_obj->FATAL);
475  }
476 
480  function setUpToDate($a_up_to_date = true)
481  {
482  $this->up_to_date = $a_up_to_date;
483  }
484 
488  function getUpToDate()
489  {
490  return $this->up_to_date;
491  }
492 
496  function setScope($a_scope)
497  {
498  $this->scope = $a_scope;
499  }
500 
504  function getScope()
505  {
506  return $this->scope;
507  }
508 
512  function _writeUpToDate($a_id, $a_up_to_date)
513  {
514  global $ilDB;
515 
516  $q = "UPDATE style_data SET uptodate = ".
517  $ilDB->quote((int) $a_up_to_date, "integer").
518  " WHERE id = ".$ilDB->quote($a_id, "integer");
519  $ilDB->manipulate($q);
520  }
521 
525  function _lookupUpToDate($a_id)
526  {
527  global $ilDB;
528 
529  $q = "SELECT uptodate FROM style_data ".
530  " WHERE id = ".$ilDB->quote($a_id, "integer");
531  $res = $ilDB->query($q);
532  $sty = $ilDB->fetchAssoc($res);
533 
534  return (boolean) $sty["uptodate"];
535  }
536 
540  function _writeStandard($a_id, $a_std)
541  {
542  global $ilDB;
543 
544  $q = "UPDATE style_data SET standard = ".
545  $ilDB->quote((int) $a_std, "integer").
546  " WHERE id = ".$ilDB->quote($a_id, "integer");
547  $ilDB->manipulate($q);
548  }
549 
553  function _writeScope($a_id, $a_scope)
554  {
555  global $ilDB;
556 
557  $q = "UPDATE style_data SET category = ".
558  $ilDB->quote((int) $a_scope, "integer").
559  " WHERE id = ".$ilDB->quote($a_id, "integer");
560  $ilDB->manipulate($q);
561  }
562 
566  function _lookupStandard($a_id)
567  {
568  global $ilDB;
569 
570  $q = "SELECT * FROM style_data ".
571  " WHERE id = ".$ilDB->quote($a_id, "integer");
572  $res = $ilDB->query($q);
573  $sty = $ilDB->fetchAssoc($res);
574 
575  return (boolean) $sty["standard"];
576  }
577 
581  function _writeActive($a_id, $a_active)
582  {
583  global $ilDB;
584 
585  $q = "UPDATE style_data SET active = ".
586  $ilDB->quote((int) $a_active, "integer").
587  " WHERE id = ".$ilDB->quote($a_id, "integer");
588  $ilDB->manipulate($q);
589  }
590 
594  function _lookupActive($a_id)
595  {
596  global $ilDB;
597 
598  $q = "SELECT * FROM style_data ".
599  " WHERE id = ".$ilDB->quote($a_id, "integer");
600  $res = $ilDB->query($q);
601  $sty = $ilDB->fetchAssoc($res);
602 
603  return (boolean) $sty["active"];
604  }
605 
609  function _getStandardStyles($a_exclude_default_style = false,
610  $a_include_deactivated = false, $a_scope = 0)
611  {
612  global $ilDB, $ilias, $tree;
613 
614  $default_style = $ilias->getSetting("default_content_style_id");
615 
616  $and_str = "";
617  if (!$a_include_deactivated)
618  {
619  $and_str = " AND active = 1";
620  }
621 
622  $q = "SELECT * FROM style_data ".
623  " WHERE standard = 1".$and_str;
624  $res = $ilDB->query($q);
625  $styles = array();
626  while($sty = $ilDB->fetchAssoc($res))
627  {
628  if (!$a_exclude_default_style || $default_style != $sty["id"])
629  {
630  // check scope
631  if ($a_scope > 0 && $sty["category"] > 0)
632  {
633  if ($tree->isInTree($sty["category"]) &&
634  $tree->isInTree($a_scope))
635  {
636  $path = $tree->getPathId($a_scope);
637  if (!in_array($sty["category"], $path))
638  {
639  continue;
640  }
641  }
642  }
643  $styles[$sty["id"]] = ilObject::_lookupTitle($sty["id"]);
644  }
645  }
646 
647  return $styles;
648  }
649 
650 
656  {
657  global $ilAccess, $ilDB;
658 
659  $clonable_styles = array();
660 
661  $q = "SELECT * FROM style_data";
662  $style_set = $ilDB->query($q);
663  while($style_rec = $ilDB->fetchAssoc($style_set))
664  {
665  $clonable = false;
666  if ($style_rec["standard"] == 1)
667  {
668  if ($style_rec["active"] == 1)
669  {
670  $clonable = true;
671  }
672  }
673  else
674  {
675  include_once("./Modules/LearningModule/classes/class.ilObjContentObject.php");
676  $obj_ids = ilObjContentObject::_lookupContObjIdByStyleId($style_rec["id"]);
677  foreach($obj_ids as $id)
678  {
679  $ref = ilObject::_getAllReferences($id);
680  foreach($ref as $ref_id)
681  {
682  if ($ilAccess->checkAccess("write", "", $ref_id))
683  {
684  $clonable = true;
685  }
686  }
687  }
688  }
689  if ($clonable)
690  {
691  $clonable_styles[$style_rec["id"]] =
692  ilObject::_lookupTitle($style_rec["id"]);
693  }
694  }
695  return $clonable_styles;
696  }
697 
701  function assignMetaData(&$a_meta_data)
702  {
703  $this->meta_data =& $a_meta_data;
704  }
705 
709  static function _getBasicStyleDom()
710  {
711  global $ilBench;
712 
713  if (!is_object(self::$basic_style_dom))
714  {
715  self::$basic_style_dom = new DOMDocument();
716  self::$basic_style_dom->load(self::$basic_style_file);
717  }
718 
719  return self::$basic_style_dom;
720  }
721 
725  function &getMetaData()
726  {
727  return $this->meta_data;
728  }
729 
733  function create($a_from_style = 0, $a_import_mode = false)
734  {
735  global $ilDB;
736 
737  parent::create();
738 
739  if ($a_from_style == 0)
740  {
741  if (!$a_import_mode)
742  {
743  // copy styles from basic style
744  $this->createFromXMLFile(self::$basic_style_file, true);
745 
746  // copy images from basic style
747  $this->createImagesDirectory();
748  ilUtil::rCopy(self::$basic_style_image_dir,
749  $this->getImagesDirectory());
750  }
751  }
752  else
753  {
754  // get style parameter records
755  $def = array();
756  $q = "SELECT * FROM style_parameter WHERE style_id = ".
757  $ilDB->quote($a_from_style, "integer");
758  $par_set = $ilDB->query($q);
759  while($par_rec = $ilDB->fetchAssoc($par_set))
760  {
761  $def[] = array("tag" => $par_rec["tag"], "class" => $par_rec["class"],
762  "parameter" => $par_rec["parameter"], "value" => $par_rec["value"],
763  "type" => $par_rec["type"]);
764  }
765 
766  // get style characteristics records
767  $chars = array();
768  $q = "SELECT * FROM style_char WHERE style_id = ".
769  $ilDB->quote($a_from_style, "integer");
770  $par_set = $ilDB->query($q);
771  while($par_rec = $ilDB->fetchAssoc($par_set))
772  {
773  $chars[] = array("type" => $par_rec["type"], "characteristic" => $par_rec["characteristic"]);
774  }
775 
776  // default style settings
777  foreach ($def as $sty)
778  {
779  $id = $ilDB->nextId("style_parameter");
780  $q = "INSERT INTO style_parameter (id, style_id, tag, class, parameter, value, type) VALUES ".
781  "(".
782  $ilDB->quote($id, "integer").",".
783  $ilDB->quote($this->getId(), "integer").",".
784  $ilDB->quote($sty["tag"], "text").",".
785  $ilDB->quote($sty["class"], "text").",".
786  $ilDB->quote($sty["parameter"], "text").",".
787  $ilDB->quote($sty["value"], "text").",".
788  $ilDB->quote($sty["type"], "text").")";
789  $ilDB->manipulate($q);
790  }
791 
792  // insert style characteristics
793  foreach ($chars as $char)
794  {
795  $q = "INSERT INTO style_char (style_id, type, characteristic) VALUES ".
796  "(".$ilDB->quote($this->getId(), "integer").",".
797  $ilDB->quote($char["type"], "text").",".
798  $ilDB->quote($char["characteristic"], "text").")";
799  $ilDB->manipulate($q);
800  }
801 
802  // add style_data record
803  $q = "INSERT INTO style_data (id, uptodate, category) VALUES ".
804  "(".$ilDB->quote($this->getId(), "integer").", 0,".
805  $ilDB->quote((int) $this->getScope(), "integer").")";
806  $ilDB->manipulate($q);
807 
808  // copy images
809  $from_style = new ilObjStyleSheet($a_from_style);
810  $this->createImagesDirectory();
811  ilUtil::rCopy($from_style->getImagesDirectory(),
812  $this->getImagesDirectory());
813 
814  // copy colors
815  $colors = $from_style->getColors();
816  foreach ($colors as $c)
817  {
818  $this->addColor($c["name"], $c["code"]);
819  }
820 
821  // copy templates
823  foreach ($tcts as $tct => $v)
824  {
825  $templates = $from_style->getTemplates($tct);
826  foreach ($templates as $t)
827  {
828  $this->addTemplate($tct, $t["name"], $t["classes"]);
829  }
830  }
831 
832  }
833 
834  $this->read();
835  if (!$a_import_mode)
836  {
837  $this->writeCSSFile();
838  }
839  }
840 
844  function deleteCharacteristic($a_type, $a_tag, $a_class)
845  {
846  global $ilDB;
847 
848  // check, if characteristic is not a core style
850  if (empty($core_styles[$a_type.".".$a_tag.".".$a_class]))
851  {
852  // delete characteristic record
853  $st = $ilDB->manipulateF(
854  "DELETE FROM style_char WHERE style_id = %s AND type = %s AND characteristic = %s",
855  array("integer", "text", "text"),
856  array($this->getId(), $a_type, $a_class));
857 
858  // delete parameter records
859  $st = $ilDB->manipulateF("DELETE FROM style_parameter WHERE style_id = %s AND tag = %s AND type = %s AND class = %s",
860  array("integer", "text", "text", "text"),
861  array($this->getId(), $a_tag, $a_type, $a_class));
862  }
863 
864  $this->setUpToDate(false);
865  $this->_writeUpToDate($this->getId(), false);
866  }
867 
871  function characteristicExists($a_char, $a_style_type)
872  {
873  global $ilDB;
874 
875  $set = $ilDB->queryF(
876  "SELECT style_id FROM style_char WHERE style_id = %s AND characteristic = %s AND type = %s",
877  array("integer", "text", "text"),
878  array($this->getId(), $a_char, $a_style_type));
879  if ($rec = $ilDB->fetchAssoc($set))
880  {
881  return true;
882  }
883  return false;
884  }
885 
889  function addCharacteristic($a_type, $a_char, $a_hidden = false)
890  {
891  global $ilDB;
892 
893  // delete characteristic record
894  $ilDB->manipulateF("INSERT INTO style_char (style_id, type, characteristic, hide)".
895  " VALUES (%s,%s,%s,%s) ",
896  array("integer", "text", "text", "integer"),
897  array($this->getId(), $a_type, $a_char, $a_hidden));
898 
899  $this->setUpToDate(false);
900  $this->_writeUpToDate($this->getId(), false);
901  }
902 
906  function getCharacteristics($a_type = "", $a_no_hidden = false)
907  {
908  $chars = array();
909 
910  if ($a_type == "")
911  {
912  $chars = $this->chars;
913  }
914  if (is_array($this->chars_by_type[$a_type]))
915  {
916  $chars = $this->chars_by_type[$a_type];
917  }
918 
919  if ($a_no_hidden)
920  {
921  foreach ($chars as $k => $char)
922  {
923  if ($a_type == "" && $this->hidden_chars[$char["type"].":".$char["class"]])
924  {
925  unset($chars[$k]);
926  }
927  else if ($this->hidden_chars[$a_type.":".$char])
928  {
929  unset($chars[$k]);
930  }
931  }
932  }
933 
934  return $chars;
935  }
936 
940  function setCharacteristics($a_chars)
941  {
942  $this->chars = $a_chars;
943  // $this->chars_by_type[$a_type];
944  }
945 
949  function saveHideStatus($a_type, $a_char, $a_hide)
950  {
951  global $ilDB;
952 
953  $ilDB->manipulate("UPDATE style_char SET ".
954  " hide = ".$ilDB->quote((int) $a_hide, "integer").
955  " WHERE style_id = ".$ilDB->quote($this->getId(), "integer")." AND ".
956  " type = ".$ilDB->quote($a_type, "text")." AND ".
957  " characteristic = ".$ilDB->quote($a_char, "text")
958  );
959  }
960 
964  function getHideStatus($a_type, $a_char)
965  {
966  global $ilDB;
967 
968  $set = $ilDB->query("SELECT hide FROM style_char ".
969  " WHERE style_id = ".$ilDB->quote($this->getId(), "integer")." AND ".
970  " type = ".$ilDB->quote($a_type, "text")." AND ".
971  " characteristic = ".$ilDB->quote($a_char, "text")
972  );
973  $rec = $ilDB->fetchAssoc($set);
974 
975  return $rec["hide"];
976  }
977 
984  function ilClone()
985  {
986  global $log, $lng;
987 
988  $new_obj = new ilObjStyleSheet();
989  $new_obj->setTitle($this->getTitle()." (".$lng->txt("sty_acopy").")");
990  $new_obj->setType($this->getType());
991  $new_obj->setDescription($this->getDescription());
992  $new_obj->create($this->getId());
993 
994  return $new_obj->getId();
995  }
996 
1000  function copyImagesToDir($a_target)
1001  {
1002  ilUtil::rCopy($this->getImagesDirectory(), $a_target);
1003  }
1004 
1012  function addParameter($a_tag, $a_par, $a_type)
1013  {
1014  global $ilDB;
1015 
1016  $avail_params = $this->getAvailableParameters();
1017  $tag = explode(".", $a_tag);
1018  $value = $avail_params[$a_par][0];
1019  $id = $ilDB->nextId("style_parameter");
1020  $q = "INSERT INTO style_parameter (id,style_id, type, tag, class, parameter, value) VALUES ".
1021  "(".
1022  $ilDB->quote($id, "integer").",".
1023  $ilDB->quote($this->getId(), "integer").",".
1024  $ilDB->quote($a_type, "text").",".
1025  $ilDB->quote($tag[0], "text").",".
1026  $ilDB->quote($tag[1], "text").",".
1027  $ilDB->quote($a_par, "text").",".
1028  $ilDB->quote($value, "text").")";
1029  $ilDB->manipulate($q);
1030  $this->read();
1031  $this->writeCSSFile();
1032  }
1033 
1039  {
1041  }
1042 
1047  static function _createImagesDirectory($a_style_id)
1048  {
1049  global $ilErr;
1050 
1051  $sty_data_dir = ilUtil::getWebspaceDir()."/sty";
1052  ilUtil::makeDir($sty_data_dir);
1053  if(!is_writable($sty_data_dir))
1054  {
1055  $ilErr->raiseError("Style data directory (".$sty_data_dir
1056  .") not writeable.", $ilErr->FATAL);
1057  }
1058 
1059  $style_dir = $sty_data_dir."/sty_".$a_style_id;
1060  ilUtil::makeDir($style_dir);
1061  if(!@is_dir($style_dir))
1062  {
1063  $ilErr->raiseError("Creation of style directory failed (".
1064  $style_dir.").",$ilErr->FATAL);
1065  }
1066 
1067  // create images subdirectory
1068  $im_dir = $style_dir."/images";
1069  ilUtil::makeDir($im_dir);
1070  if(!@is_dir($im_dir))
1071  {
1072  $ilErr->raiseError("Creation of Import Directory failed (".
1073  $im_dir.").", $ilErr->FATAL);
1074  }
1075 
1076  // create thumbnails directory
1077  $thumb_dir = $style_dir."/images/thumbnails";
1078  ilUtil::makeDir($thumb_dir);
1079  if(!@is_dir($thumb_dir))
1080  {
1081  $ilErr->raiseError("Creation of Import Directory failed (".
1082  $thumb_dir.").", $ilErr->FATAL);
1083  }
1084  }
1085 
1090  {
1092  }
1093 
1097  static function _getImagesDirectory($a_style_id)
1098  {
1099  return ilUtil::getWebspaceDir()."/sty/sty_".$a_style_id.
1100  "/images";
1101  }
1102 
1107  {
1108  return $this->getImagesDirectory().
1109  "/thumbnails";
1110  }
1111 
1115  function getImages()
1116  {
1117  $dir = $this->getImagesDirectory();
1118  $images = array();
1119  if (is_dir($dir))
1120  {
1121  $entries = ilUtil::getDir($dir);
1122  foreach($entries as $entry)
1123  {
1124  if (substr($entry["entry"],0,1) == ".")
1125  {
1126  continue;
1127  }
1128  if ($entry["type"] != "dir")
1129  {
1130  $images[] = $entry;
1131  }
1132  }
1133  }
1134 
1135  return $images;
1136  }
1137 
1141  function uploadImage($a_file)
1142  {
1143  $this->createImagesDirectory();
1144  @ilUtil::moveUploadedFile($a_file["tmp_name"], $a_file["name"],
1145  $this->getImagesDirectory()."/".$a_file["name"]);
1146  @ilUtil::resizeImage($this->getImagesDirectory()."/".$a_file["name"],
1147  $this->getThumbnailsDirectory()."/".$a_file["name"], 75, 75);
1148  }
1149 
1153  function deleteImage($a_file)
1154  {
1155  if (is_file($this->getImagesDirectory()."/".$a_file))
1156  {
1157  unlink($this->getImagesDirectory()."/".$a_file);
1158  }
1159  if (is_file($this->getThumbnailsDirectory()."/".$a_file))
1160  {
1161  unlink($this->getThumbnailsDirectory()."/".$a_file);
1162  }
1163  }
1164 
1170  function deleteParameter($a_id)
1171  {
1172  global $ilDB;
1173 
1174  $q = "DELETE FROM style_parameter WHERE id = ".
1175  $ilDB->quote($a_id, "integer");
1176  $ilDB->query($q);
1177  }
1178 
1183  function deleteStylePar($a_tag, $a_class, $a_par, $a_type)
1184  {
1185  global $ilDB;
1186 
1187  $q = "DELETE FROM style_parameter WHERE ".
1188  " style_id = ".$ilDB->quote($this->getId(), "integer")." AND ".
1189  " tag = ".$ilDB->quote($a_tag, "text")." AND ".
1190  " class = ".$ilDB->quote($a_class, "text")." AND ".
1191  " ".$ilDB->equals("type", $a_type, "text", true)." AND ".
1192  " parameter = ".$ilDB->quote($a_par, "text");
1193 
1194  $ilDB->manipulate($q);
1195  }
1196 
1200  function delete()
1201  {
1202  global $ilDB;
1203 
1204  // delete object
1205  parent::delete();
1206 
1207  // check whether this style is global default
1208  $def_style = $this->ilias->getSetting("default_content_style_id");
1209  if ($def_style == $this->getId())
1210  {
1211  $this->ilias->deleteSetting("default_content_style_id");
1212  }
1213 
1214  // check whether this style is global fixed
1215  $fixed_style = $this->ilias->getSetting("fixed_content_style_id");
1216  if ($fixed_style == $this->getId())
1217  {
1218  $this->ilias->deleteSetting("fixed_content_style_id");
1219  }
1220 
1221  // delete style parameter
1222  $q = "DELETE FROM style_parameter WHERE style_id = ".
1223  $ilDB->quote($this->getId(), "integer");
1224  $ilDB->manipulate($q);
1225 
1226  // delete style file
1227  $css_file_name = ilUtil::getWebspaceDir()."/css/style_".$this->getId().".css";
1228  if (is_file($css_file_name))
1229  {
1230  unlink($css_file_name);
1231  }
1232 
1233  // delete entries in learning modules
1234  include_once("./Modules/LearningModule/classes/class.ilObjContentObject.php");
1236 
1237  // delete style data record
1238  $q = "DELETE FROM style_data WHERE id = ".
1239  $ilDB->quote($this->getId(), "integer");
1240  $ilDB->manipulate($q);
1241 
1242  }
1243 
1244 
1248  function read()
1249  {
1250  global $ilDB;
1251 
1252  parent::read();
1253 
1254  $q = "SELECT * FROM style_parameter WHERE style_id = ".
1255  $ilDB->quote($this->getId(), "integer")." ORDER BY tag, class, type ";
1256  $style_set = $ilDB->query($q);
1257  $ctag = "";
1258  $cclass = "";
1259  $ctype = "";
1260  $this->style = array();
1261  while($style_rec = $ilDB->fetchAssoc($style_set))
1262  {
1263  if ($style_rec["tag"] != $ctag || $style_rec["class"] != $cclass
1264  || $style_rec["type"] != $ctype)
1265  {
1266  // add current tag array to style array
1267  if(is_array($tag))
1268  {
1269  $this->style[] = $tag;
1270  }
1271  $tag = array();
1272  }
1273  $ctag = $style_rec["tag"];
1274  $cclass = $style_rec["class"];
1275  $ctype = $style_rec["type"];
1276  $tag[] = $style_rec;
1277  }
1278  if(is_array($tag))
1279  {
1280  $this->style[] = $tag;
1281  }
1282 
1283  $q = "SELECT * FROM style_data WHERE id = ".
1284  $ilDB->quote($this->getId(), "integer");
1285  $res = $ilDB->query($q);
1286  $sty = $ilDB->fetchAssoc($res);
1287  $this->setUpToDate((boolean) $sty["uptodate"]);
1288  $this->setScope($sty["category"]);
1289 
1290  // get style characteristics records
1291  $this->chars = array();
1292  $this->chars_by_type = array();
1293  $q = "SELECT * FROM style_char WHERE style_id = ".
1294  $ilDB->quote($this->getId(), "integer").
1295  " ORDER BY type ASC, characteristic ASC";
1296  $par_set = $ilDB->query($q);
1297  while($par_rec = $ilDB->fetchAssoc($par_set))
1298  {
1299  $this->chars[] = array("type" => $par_rec["type"], "class" => $par_rec["characteristic"], "hide" => $par_rec["hide"]);
1300  $this->chars_by_type[$par_rec["type"]][] = $par_rec["characteristic"];
1301  if ($par_rec["hide"])
1302  {
1303  $this->hidden_chars[$par_rec["type"].":".$par_rec["characteristic"]] = true;
1304  }
1305  }
1306  }
1307 
1311  function writeCSSFile($a_target_file = "", $a_image_dir = "")
1312  {
1313  $style = $this->getStyle();
1314 
1315  if ($a_target_file == "")
1316  {
1317  $css_file_name = ilUtil::getWebspaceDir()."/css/style_".$this->getId().".css";
1318  }
1319  else
1320  {
1321  $css_file_name = $a_target_file;
1322  }
1323  $css_file = fopen($css_file_name, "w");
1324 
1325  $page_background = "";
1326 
1327  foreach ($style as $tag)
1328  {
1329  fwrite ($css_file, $tag[0]["tag"].".ilc_".$tag[0]["type"]."_".$tag[0]["class"]."\n");
1330  if ($tag[0]["tag"] = "td")
1331  {
1332  fwrite ($css_file, ",th".".ilc_".$tag[0]["type"]."_".$tag[0]["class"]."\n");
1333  }
1334  fwrite ($css_file, "{\n");
1335 
1336  // collect table border attributes
1337  $t_border = array();
1338 
1339  foreach($tag as $par)
1340  {
1341  $cur_par = $par["parameter"];
1342  $cur_val = $par["value"];
1343 
1344  // replace named colors
1345  if (is_int(strpos($cur_par, "color")) && substr(trim($cur_val), 0, 1) == "!")
1346  {
1347  $cur_val = $this->getColorCodeForName(substr($cur_val, 1));
1348  }
1349 
1350  if ($tag[0]["type"] == "table" && is_int(strpos($par["parameter"], "border")))
1351  {
1352  $t_border[$cur_par] = $cur_val;
1353  }
1354 
1355  if (in_array($cur_par, array("background-image", "list-style-image")))
1356  {
1357  if (is_int(strpos($cur_val, "/"))) // external
1358  {
1359  $cur_val = "url(".$cur_val.")";
1360  }
1361  else // internal
1362  {
1363  if ($a_image_dir == "")
1364  {
1365  $cur_val = "url(../sty/sty_".$this->getId()."/images/".$cur_val.")";
1366  }
1367  else
1368  {
1369  $cur_val = "url(".$a_image_dir."/".$cur_val.")";
1370  }
1371  }
1372  }
1373 
1374  if ($cur_par == "opacity")
1375  {
1376  $cur_val = ((int) $cur_val) / 100;
1377  }
1378 
1379  fwrite ($css_file, "\t".$cur_par.": ".$cur_val.";\n");
1380 
1381  // IE6 fix for minimum height
1382  if ($cur_par == "min-height")
1383  {
1384  fwrite ($css_file, "\t"."height".": "."auto !important".";\n");
1385  fwrite ($css_file, "\t"."height".": ".$cur_val.";\n");
1386  }
1387 
1388  // opacity fix
1389  if ($cur_par == "opacity")
1390  {
1391  fwrite ($css_file, "\t".'-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity='.($cur_val * 100).')"'.";\n");
1392  fwrite ($css_file, "\t".'filter: alpha(opacity='.($cur_val * 100).')'.";\n");
1393  fwrite ($css_file, "\t".'-moz-opacity: '.$cur_val.";\n");
1394  }
1395 
1396  // save page background
1397  if ($tag[0]["tag"] == "div" && $tag[0]["class"] == "Page"
1398  && $cur_par == "background-color")
1399  {
1400  $page_background = $cur_val;
1401  }
1402  }
1403  fwrite ($css_file, "}\n");
1404  fwrite ($css_file, "\n");
1405 
1406  // use table border attributes for th td as well
1407 /* if ($tag[0]["type"] == "table")
1408  {
1409  if (count($t_border) > 0)
1410  {
1411  fwrite ($css_file, $tag[0]["tag"].".ilc_".$tag[0]["type"]."_".$tag[0]["class"]." th,".
1412  $tag[0]["tag"].".ilc_".$tag[0]["type"]."_".$tag[0]["class"]." td\n");
1413  fwrite ($css_file, "{\n");
1414  foreach ($t_border as $p => $v)
1415  {
1416 // fwrite ($css_file, "\t".$p.": ".$v.";\n");
1417  }
1418  fwrite ($css_file, "}\n");
1419  fwrite ($css_file, "\n");
1420  }
1421  }*/
1422  }
1423 
1424  if ($page_background != "")
1425  {
1426  fwrite ($css_file, "td.ilc_Page\n");
1427  fwrite ($css_file, "{\n");
1428  fwrite ($css_file, "\t"."background-color: ".$page_background.";\n");
1429  fwrite ($css_file, "}\n");
1430  }
1431  fclose($css_file);
1432 
1433  $this->setUpToDate(true);
1434  $this->_writeUpToDate($this->getId(), true);
1435  }
1436 
1443  static function getEffectiveContentStyleId($a_style_id, $a_type = "")
1444  {
1445  global $ilSetting;
1446 
1447  // check global fixed content style
1448  $fixed_style = $ilSetting->get("fixed_content_style_id");
1449  if ($fixed_style > 0)
1450  {
1451  $a_style_id = $fixed_style;
1452  }
1453 
1454  // check global default style
1455  if ($a_style_id <= 0)
1456  {
1457  $a_style_id = $ilSetting->get("default_content_style_id");
1458  }
1459 
1460  if ($a_style_id > 0 && ilObject::_lookupType($a_style_id) == "sty")
1461  {
1462  return $a_style_id;
1463  }
1464 
1465  return 0;
1466  }
1467 
1473  function getContentStylePath($a_style_id)
1474  {
1475  global $ilias;
1476 
1477  $rand = rand(1,999999);
1478 
1479 
1480  // check global fixed content style
1481  $fixed_style = $ilias->getSetting("fixed_content_style_id");
1482  if ($fixed_style > 0)
1483  {
1484  $a_style_id = $fixed_style;
1485  }
1486 
1487  // check global default style
1488  if ($a_style_id <= 0)
1489  {
1490  $a_style_id = $ilias->getSetting("default_content_style_id");
1491  }
1492 
1493  if ($a_style_id > 0 && ilObject::_exists($a_style_id))
1494  {
1495  // check whether file is up to date
1496  if (!ilObjStyleSheet::_lookupUpToDate($a_style_id))
1497  {
1498  $style = new ilObjStyleSheet($a_style_id);
1499  $style->writeCSSFile();
1500  }
1501 
1502  return ilUtil::getWebspaceDir("output").
1503  "/css/style_".$a_style_id.".css?dummy=$rand";
1504  }
1505  else // todo: work this out
1506  {
1507  return "./Services/COPage/css/content.css";
1508  }
1509  }
1510 
1517  {
1518  return "./Services/COPage/css/print_content.css";
1519  }
1520 
1527  {
1528  return "./Services/COPage/css/syntaxhighlight.css";
1529  }
1530 
1537  {
1538  return "./Services/COPage/css/placeholder.css";
1539  }
1540 
1541  function update()
1542  {
1543  global $ilDB;
1544 
1545  parent::update();
1546  $this->read(); // this could be done better
1547  $this->writeCSSFile();
1548 
1549  $q = "UPDATE style_data ".
1550  "SET category = ".$ilDB->quote((int) $this->getScope(), "integer").
1551  " WHERE id = ".$ilDB->quote($this->getId(), "integer");
1552  $ilDB->manipulate($q);
1553  }
1554 
1561  function updateStyleParameter($a_id, $a_value)
1562  {
1563  global $ilDB;
1564 
1565  $q = "UPDATE style_parameter SET VALUE = ".
1566  $ilDB->quote($a_value, "text")." WHERE id = ".
1567  $ilDB->quote($a_id, "integer");
1568  $style_set = $ilDB->manipulate($q);
1569  }
1570 
1575  function replaceStylePar($a_tag, $a_class, $a_par, $a_val, $a_type)
1576  {
1577  ilObjStyleSheet::_replaceStylePar($this->getId(), $a_tag, $a_class, $a_par, $a_val, $a_type);
1578  }
1579 
1580  function _replaceStylePar($style_id, $a_tag, $a_class, $a_par, $a_val, $a_type)
1581  {
1582  global $ilDB;
1583 
1584  $q = "SELECT * FROM style_parameter WHERE ".
1585  " style_id = ".$ilDB->quote($style_id, "integer")." AND ".
1586  " tag = ".$ilDB->quote($a_tag, "text")." AND ".
1587  " class = ".$ilDB->quote($a_class, "text")." AND ".
1588  " ".$ilDB->equals("type", $a_type, "text", true)." AND ".
1589  " parameter = ".$ilDB->quote($a_par, "text");
1590 
1591  $set = $ilDB->query($q);
1592 
1593  if ($rec = $set->fetchRow())
1594  {
1595  $q = "UPDATE style_parameter SET ".
1596  " value = ".$ilDB->quote($a_val, "text")." WHERE ".
1597  " style_id = ".$ilDB->quote($style_id, "integer")." AND ".
1598  " tag = ".$ilDB->quote($a_tag, "text")." AND ".
1599  " class = ".$ilDB->quote($a_class, "text")." AND ".
1600  " ".$ilDB->equals("type", $a_type, "text", true)." AND ".
1601  " parameter = ".$ilDB->quote($a_par, "text");
1602 
1603  $ilDB->manipulate($q);
1604  }
1605  else
1606  {
1607  $id = $ilDB->nextId("style_parameter");
1608  $q = "INSERT INTO style_parameter (id, value, style_id, tag, class, type, parameter) VALUES ".
1609  " (".
1610  $ilDB->quote($id, "integer").",".
1611  $ilDB->quote($a_val, "text").",".
1612  " ".$ilDB->quote($this->getId(), "integer").",".
1613  " ".$ilDB->quote($a_tag, "text").",".
1614  " ".$ilDB->quote($a_class, "text").",".
1615  " ".$ilDB->quote($a_type, "text").",".
1616  " ".$ilDB->quote($a_par, "text").")";
1617 
1618  $ilDB->manipulate($q);
1619  }
1620  }
1621 
1622 
1626  function getStyle()
1627  {
1628  return $this->style;
1629  }
1630 
1634  function setStyle($a_style)
1635  {
1636  $this->style = $a_style;
1637  }
1638 
1639 
1646  function handleXmlString($a_str)
1647  {
1648  return str_replace("&", "&amp;", $a_str);
1649  }
1650 
1654  function getXML()
1655  {
1656  $xml.= "<StyleSheet>\n";
1657 
1658  // title and description
1659  $xml.= "<Title>".$this->handleXmlString($this->getTitle())."</Title>";
1660  $xml.= "<Description>".$this->handleXmlString($this->getDescription())."</Description>\n";
1661 
1662  // style classes
1663  foreach($this->chars as $char)
1664  {
1665  $xml.= "<Style Tag=\"".ilObjStyleSheet::_determineTag($char["type"]).
1666  "\" Type=\"".$char["type"]."\" Class=\"".$char["class"]."\">\n";
1667  foreach($this->style as $style)
1668  {
1669  if ($style[0]["type"] == $char["type"] && $style[0]["class"] == $char["class"])
1670  {
1671  foreach($style as $tag)
1672  {
1673  $xml.="<StyleParameter Name=\"".$tag["parameter"]."\" Value=\"".$tag["value"]."\"/>\n";
1674  }
1675  }
1676  }
1677  $xml.= "</Style>\n";
1678  }
1679 
1680  // colors
1681  foreach($this->getColors() as $color)
1682  {
1683  $xml.="<StyleColor Name=\"".$color["name"]."\" Code=\"".$color["code"]."\"/>\n";
1684  }
1685 
1686  // templates
1688  foreach ($tcts as $tct => $v)
1689  {
1690  $ts = $this->getTemplates($tct);
1691 
1692  foreach ($ts as $t)
1693  {
1694  $xml.="<StyleTemplate Type=\"".$tct."\" Name=\"".$t["name"]."\">\n";
1695  foreach ($t["classes"] as $ct => $c)
1696  {
1697  if ($c != "")
1698  {
1699  $xml.="<StyleTemplateClass ClassType=\"".$ct."\" Class=\"".$c."\"/>\n";
1700  }
1701  }
1702  $xml.="</StyleTemplate>\n";
1703  }
1704  }
1705 
1706 
1707  $xml.= "</StyleSheet>";
1708 //echo "<pre>".htmlentities($xml)."</pre>"; exit;
1709  return $xml;
1710  }
1711 
1712 
1717  {
1718  $sty_data_dir = ilUtil::getDataDir()."/sty";
1719  ilUtil::makeDir($sty_data_dir);
1720  if(!is_writable($sty_data_dir))
1721  {
1722  $this->ilias->raiseError("Style data directory (".$sty_data_dir
1723  .") not writeable.",$this->ilias->error_obj->FATAL);
1724  }
1725 
1726  $style_dir = $sty_data_dir."/sty_".$this->getId();
1727  ilUtil::makeDir($style_dir);
1728  if(!@is_dir($style_dir))
1729  {
1730  $this->ilias->raiseError("Creation of style directory failed (".
1731  $style_dir.").",$this->ilias->error_obj->FATAL);
1732  }
1733 
1734  // create export subdirectory
1735  $ex_dir = $style_dir."/export";
1736  ilUtil::makeDir($ex_dir);
1737  if(!@is_dir($ex_dir))
1738  {
1739  $this->ilias->raiseError("Creation of Import Directory failed (".
1740  $ex_dir.").",$this->ilias->error_obj->FATAL);
1741  }
1742 
1743  return $ex_dir;
1744  }
1745 
1750  {
1751  $sty_data_dir = ilUtil::getDataDir()."/sty";
1752  $style_dir = $sty_data_dir."/sty_".$this->getId();
1753  // create export subdirectory
1754  $ex_dir = $style_dir."/export";
1755 
1756  if (is_dir($ex_dir))
1757  {
1758  ilUtil::delDir($ex_dir, true);
1759  }
1760  }
1761 
1762 
1767  {
1768  $ex_dir = $this->createExportDirectory();
1769  $ex_sub_dir = $ex_dir."/".$this->getExportSubDir();
1770  ilUtil::makeDir($ex_sub_dir);
1771  if(!is_writable($ex_sub_dir))
1772  {
1773  $this->ilias->raiseError("Style data directory (".$ex_sub_dir
1774  .") not writeable.",$this->ilias->error_obj->FATAL);
1775  }
1776  $ex_sub_images_dir = $ex_sub_dir."/images";
1777  ilUtil::makeDir($ex_sub_images_dir);
1778  if(!is_writable($ex_sub_images_dir))
1779  {
1780  $this->ilias->raiseError("Style data directory (".$ex_sub_images_dir
1781  .") not writeable.",$this->ilias->error_obj->FATAL);
1782  }
1783  }
1784 
1788  function setExportSubDir($a_dir)
1789  {
1790  $this->export_sub_dir = $a_dir;
1791  }
1792 
1796  function getExportSubDir()
1797  {
1798  if ($this->export_sub_dir == "")
1799  {
1800  return "sty_".$this->getId();
1801  }
1802  else
1803  {
1804  return $this->export_sub_dir;
1805  }
1806  }
1807 
1813  function export()
1814  {
1815  $this->cleanExportDirectory();
1816  $ex_dir = $this->createExportDirectory();
1817  $this->createExportSubDirectory();
1818  $this->exportXML($ex_dir."/".$this->getExportSubDir());
1819 //echo "-".$this->getImagesDirectory()."-".$ex_dir."/".$this->getExportSubDir()."/images"."-";
1821  $ex_dir."/".$this->getExportSubDir()."/images");
1822  if (is_file($ex_dir."/".$this->getExportSubDir().".zip"))
1823  {
1824  unlink($ex_dir."/".$this->getExportSubDir().".zip");
1825  }
1826  ilUtil::zip($ex_dir."/".$this->getExportSubDir(),
1827  $ex_dir."/".$this->getExportSubDir().".zip");
1828 
1829  return $ex_dir."/".$this->getExportSubDir().".zip";
1830  }
1831 
1835  function exportXML($a_dir)
1836  {
1837  $file = $a_dir."/style.xml";
1838 
1839  // open file
1840  if (!($fp = @fopen($file,"w")))
1841  {
1842  die ("<b>Error</b>: Could not open \"".$file."\" for writing".
1843  " in <b>".__FILE__."</b> on line <b>".__LINE__."</b><br />");
1844  }
1845 
1846  // set file permissions
1847  chmod($file, 0770);
1848 
1849  // write xml data into the file
1850  fwrite($fp, $this->getXML());
1851 
1852  // close file
1853  fclose($fp);
1854 
1855  }
1856 
1861  {
1862  $sty_data_dir = ilUtil::getDataDir()."/sty";
1863  ilUtil::makeDir($sty_data_dir);
1864  if(!is_writable($sty_data_dir))
1865  {
1866  $this->ilias->raiseError("Style data directory (".$sty_data_dir
1867  .") not writeable.",$this->ilias->error_obj->FATAL);
1868  }
1869 
1870  $style_dir = $sty_data_dir."/sty_".$this->getId();
1871  ilUtil::makeDir($style_dir);
1872  if(!@is_dir($style_dir))
1873  {
1874  $this->ilias->raiseError("Creation of style directory failed (".
1875  $style_dir.").",$this->ilias->error_obj->FATAL);
1876  }
1877 
1878  // create import subdirectory
1879  $im_dir = $style_dir."/import";
1880  ilUtil::makeDir($im_dir);
1881  if(!@is_dir($im_dir))
1882  {
1883  $this->ilias->raiseError("Creation of Import Directory failed (".
1884  $im_dir.").",$this->ilias->error_obj->FATAL);
1885  }
1886 
1887  return $im_dir;
1888  }
1889 
1893  function import($a_file)
1894  {
1895  parent::create();
1896 
1897  $im_dir = $this->createImportDirectory();
1898 
1899  // handle uploaded files
1900  if (is_array($a_file))
1901  {
1902  ilUtil::moveUploadedFile($a_file["tmp_name"],
1903  $a_file["name"], $im_dir."/".$a_file["name"]);
1904  $file_name = $a_file["name"];
1905  }
1906  else // handle not directly uploaded files
1907  {
1908  $pi = pathinfo($a_file);
1909  $file_name = $pi["basename"];
1910  copy($a_file, $im_dir."/".$file_name);
1911  }
1912  $file = pathinfo($file_name);
1913 
1914  // unzip file
1915  if (strtolower($file["extension"] == "zip"))
1916  {
1917  ilUtil::unzip($im_dir."/".$file_name);
1918  $subdir = basename($file["basename"],".".$file["extension"]);
1919  if (!is_dir($im_dir."/".$subdir))
1920  {
1921  $subdir = "style"; // check style subdir
1922  }
1923  $xml_file = $im_dir."/".$subdir."/style.xml";
1924  }
1925  else // handle xml file directly (old style)
1926  {
1927  $xml_file = $im_dir."/".$file_name;
1928  }
1929 
1930  // load information from xml file
1931 //echo "-$xml_file-";
1932  $this->createFromXMLFile($xml_file, true);
1933 
1934  // copy images
1935  $this->createImagesDirectory();
1936  if (is_dir($im_dir."/".$subdir."/images"))
1937  {
1938  ilUtil::rCopy($im_dir."/".$subdir."/images",
1939  $this->getImagesDirectory());
1940  }
1941 
1943  $this->read();
1944  $this->writeCSSFile();
1945  }
1946 
1950  function createFromXMLFile($a_file, $a_skip_parent_create = false)
1951  {
1952  global $ilDB;
1953 
1954  $this->is_3_10_skin = false;
1955 
1956  if (!$a_skip_parent_create)
1957  {
1958  parent::create();
1959  }
1960  include_once("./Services/Style/classes/class.ilStyleImportParser.php");
1961  $importParser = new ilStyleImportParser($a_file, $this);
1962  $importParser->startParsing();
1963 
1964  // store style parameter
1965  foreach ($this->style as $style)
1966  {
1967  foreach($style as $tag)
1968  {
1969  $id = $ilDB->nextId("style_parameter");
1970  $q = "INSERT INTO style_parameter (id,style_id, tag, class, parameter, type, value) VALUES ".
1971  "(".
1972  $ilDB->quote($id, "integer").",".
1973  $ilDB->quote($this->getId(), "integer").",".
1974  $ilDB->quote($tag["tag"], "text").",".
1975  $ilDB->quote($tag["class"], "text").",".
1976  $ilDB->quote($tag["parameter"], "text").",".
1977  $ilDB->quote($tag["type"], "text").",".
1978  $ilDB->quote($tag["value"], "text").")";
1979 //echo "<br>-$q";
1980  $ilDB->manipulate($q);
1981  }
1982  }
1983 
1984  // store characteristics
1985  $this->is_3_10_skin = true;
1986  if (is_array($this->chars))
1987  {
1988  foreach ($this->chars as $char)
1989  {
1990  if ($char["type"] != "")
1991  {
1992  $q = "INSERT INTO style_char (style_id, type, characteristic) VALUES ".
1993  "(".$ilDB->quote($this->getId(), "integer").",".
1994  $ilDB->quote($char["type"], "text").",".
1995  $ilDB->quote($char["class"], "text").")";
1996  $ilDB->manipulate($q);
1997  $this->is_3_10_skin = false;
1998  }
1999  }
2000  }
2001 
2002  // add style_data record
2003  $q = "INSERT INTO style_data (id, uptodate) VALUES ".
2004  "(".$ilDB->quote($this->getId(), "integer").", 0)";
2005  $ilDB->manipulate($q);
2006 
2007  $this->update();
2008  $this->read();
2009 
2010  if ($this->is_3_10_skin)
2011  {
2012  $this->do_3_10_Migration();
2013  }
2014  //$this->writeCSSFile();
2015  }
2016 
2021  {
2022  $groups = array();
2023 
2024  foreach (self::$parameter as $parameter => $props)
2025  {
2026  $groups[$props["group"]][] = $parameter;
2027  }
2028  return $groups;
2029  }
2030 
2031  static function _getStyleParameterInputType($par)
2032  {
2033  $input = self::$parameter[$par]["input"];
2034  return $input;
2035  }
2036 
2037  static function _getStyleParameterSubPar($par)
2038  {
2039  $subpar = self::$parameter[$par]["subpar"];
2040  return $subpar;
2041  }
2042 
2043  static function _getStyleParameters($a_tag = "")
2044  {
2045  if ($a_tag == "")
2046  {
2047  return self::$parameter;
2048  }
2049  $par = array();
2050  foreach (self::$parameter as $k => $v)
2051  {
2052  if (is_array(self::$filtered_groups[$v["group"]]) &&
2053  !in_array($a_tag, self::$filtered_groups[$v["group"]]))
2054  {
2055  continue;
2056  }
2057  $par[$k] = $v;
2058  }
2059  return $par;
2060  }
2061 
2062  static function _getFilteredGroups()
2063  {
2064  return self::$filtered_groups;
2065  }
2066 
2067  static function _getStyleParameterNumericUnits($a_no_percentage = false)
2068  {
2069  if ($a_no_percentage)
2070  {
2071  return self::$num_unit_no_perc;
2072  }
2073  return self::$num_unit;
2074  }
2075 
2076  static function _getStyleParameterValues($par)
2077  {
2078  return self::$parameter[$par]["values"];
2079  }
2080 
2081  /*static function _getStyleTypes()
2082  {
2083  return self::$style_types;
2084  }*/
2085 
2086  static function _getStyleSuperTypes()
2087  {
2088  return self::$style_super_types;
2089  }
2090 
2091  static function _isExpandable($a_type)
2092  {
2093  return in_array($a_type, self::$expandable_types);
2094  }
2095 
2096  static function _isHideable($a_type)
2097  {
2098  return in_array($a_type, self::$hideable_types);
2099  }
2100 
2101  static function _getStyleSuperTypeForType($a_type)
2102  {
2103  foreach (self::$style_super_types as $s => $t)
2104  {
2105  if (in_array($a_type, $t))
2106  {
2107  return $s;
2108  }
2109  if ($a_type == $s)
2110  {
2111  return $s;
2112  }
2113  }
2114  }
2115 
2119  static function _getCoreStyles()
2120  {
2121  $c_styles = array();
2122  foreach (self::$core_styles as $cstyle)
2123  {
2124  $c_styles[$cstyle["type"].".".ilObjStyleSheet::_determineTag($cstyle["type"]).".".$cstyle["class"]]
2125  = array("type" => $cstyle["type"],
2126  "tag" => ilObjStyleSheet::_determineTag($cstyle["type"]),
2127  "class" => $cstyle["class"]);
2128  }
2129  return $c_styles;
2130  }
2131 
2135  static function _getTemplateClassTypes($a_template_type = "")
2136  {
2137  if ($a_template_type == "")
2138  {
2139  return self::$templates;
2140  }
2141 
2142  return self::$templates[$a_template_type];
2143  }
2144 
2145 
2146  function _getPseudoClasses($tag)
2147  {
2148  return self::$pseudo_classes[$tag];
2149  }
2150 
2152  {
2153  return self::$templates[$t][$k];
2154  }
2155 
2156  static function _determineTag($a_type)
2157  {
2158  return self::$assigned_tags[$a_type];
2159  }
2160 
2164  static function getAvailableParameters()
2165  {
2166  $pars = array();
2167  foreach(self::$parameter as $p => $v)
2168  {
2169  $pars[$p] = $v["values"];
2170  }
2171 
2172  return $pars;
2173  }
2174 
2175 
2179  static function _addMissingStyleClassesToStyle($a_id)
2180  {
2181  $styles = array(array("id" => $a_id));
2183  }
2184 
2188  static function _addMissingStyleClassesToAllStyles($a_styles = "")
2189  {
2190  global $ilDB;
2191 
2192  if ($a_styles == "")
2193  {
2194  $styles = ilObject::_getObjectsDataForType("sty");
2195  }
2196  else
2197  {
2198  $styles = $a_styles;
2199  }
2202 
2203  // get all core image files
2204  $core_images = array();
2205  $core_dir = self::$basic_style_image_dir;
2206  if (is_dir($core_dir))
2207  {
2208  $dir = opendir($core_dir);
2209  while($file = readdir($dir))
2210  {
2211  if (substr($file, 0, 1) != "." && is_file($core_dir."/".$file))
2212  {
2213  $core_images[] = $file;
2214  }
2215  }
2216  }
2217 
2218  foreach ($styles as $style)
2219  {
2220  $id = $style["id"];
2221 
2222  foreach($core_styles as $cs)
2223  {
2224  // check, whether core style class exists
2225  $set = $ilDB->queryF("SELECT * FROM style_char WHERE style_id = %s ".
2226  "AND type = %s AND characteristic = %s",
2227  array("integer", "text", "text"),
2228  array($id, $cs["type"], $cs["class"]));
2229 
2230  // if not, add core style class
2231  if (!($rec = $ilDB->fetchAssoc($set)))
2232  {
2233  $ilDB->manipulateF(
2234  "INSERT INTO style_char (style_id, type, characteristic) ".
2235  " VALUES (%s,%s,%s) ",
2236  array("integer", "text", "text"),
2237  array($id, $cs["type"], $cs["class"]));
2238 
2239  $xpath = new DOMXPath($bdom);
2240  $par_nodes = $xpath->query("/StyleSheet/Style[@Tag = '".$cs["tag"]."' and @Type='".
2241  $cs["type"]."' and @Class='".$cs["class"]."']/StyleParameter");
2242  foreach ($par_nodes as $par_node)
2243  {
2244  // check whether style parameter exists
2245  $set = $ilDB->queryF("SELECT * FROM style_parameter WHERE style_id = %s ".
2246  "AND type = %s AND class = %s AND tag = %s AND parameter = %s",
2247  array("integer", "text", "text", "text", "text"),
2248  array($id, $cs["type"], $cs["class"],
2249  $cs["tag"], $par_node->getAttribute("Name")));
2250 
2251  // if not, create style parameter
2252  if (!($rec = $ilDB->fetchAssoc($set)))
2253  {
2254  $spid = $ilDB->nextId("style_parameter");
2255  $st = $ilDB->manipulateF("INSERT INTO style_parameter (id, style_id, type, class, tag, parameter, value) ".
2256  " VALUES (%s,%s,%s,%s,%s,%s,%s)",
2257  array("integer", "integer", "text", "text", "text", "text", "text"),
2258  array($spid, $id, $cs["type"], $cs["class"], $cs["tag"],
2259  $par_node->getAttribute("Name"), $par_node->getAttribute("Value")));
2260  }
2261  }
2262  }
2263  }
2264 
2265  // now check, whether some core image files are missing
2268  reset($core_images);
2269  foreach($core_images as $cim)
2270  {
2271  if (!is_file($imdir."/".$cim))
2272  {
2273  copy($core_dir."/".$cim, $imdir."/".$cim);
2274  }
2275  }
2276  }
2277  }
2278 
2279  //
2280  // Color management
2281  //
2282 
2287  {
2288  global $ilDB;
2289 
2290  $this->do_3_9_Migration($this->getId());
2291 
2292  //include_once("./Services/Migration/DBUpdate_1385/classes/class.ilStyleMigration.php");
2293  //ilStyleMigration::addMissingStyleCharacteristics($this->getId());
2294 
2295  $this->do_3_10_CharMigration($this->getId());
2296 
2297  // style_char: type for characteristic
2298  $st = $ilDB->prepareManip("UPDATE style_char SET type = ? WHERE characteristic = ?".
2299  " AND style_id = ? ", array("text", "text", "integer"));
2300  $ilDB->execute($st, array("media_cont", "Media", $this->getId()));
2301  $ilDB->execute($st, array("media_caption", "MediaCaption", $this->getId()));
2302  $ilDB->execute($st, array("page_fn", "Footnote", $this->getId()));
2303  $ilDB->execute($st, array("page_nav", "LMNavigation", $this->getId()));
2304  $ilDB->execute($st, array("page_title", "PageTitle", $this->getId()));
2305  $ilDB->execute($st, array("page_cont", "Page", $this->getId()));
2306 
2307  // style_parameter: type for class
2308  $st = $ilDB->prepareManip("UPDATE style_parameter SET type = ? WHERE class = ?".
2309  " AND style_id = ? ", array("text", "text", "integer"));
2310  $ilDB->execute($st, array("media_cont", "Media", $this->getId()));
2311  $ilDB->execute($st, array("media_caption", "MediaCaption", $this->getId()));
2312  $ilDB->execute($st, array("page_fn", "Footnote", $this->getId()));
2313  $ilDB->execute($st, array("page_nav", "LMNavigation", $this->getId()));
2314  $ilDB->execute($st, array("page_title", "PageTitle", $this->getId()));
2315  $ilDB->execute($st, array("table", "Page", $this->getId()));
2316 
2317  $st = $ilDB->prepareManip("UPDATE style_parameter SET tag = ? WHERE class = ?".
2318  " AND style_id = ? ", array("text", "text", "integer"));
2319  $ilDB->execute($st, array("div", "MediaCaption", $this->getId()));
2320 
2321  // style_char: characteristic for characteristic
2322  $st = $ilDB->prepareManip("UPDATE style_char SET characteristic = ? WHERE characteristic = ?".
2323  " AND style_id = ? ", array("text", "text", "integer"));
2324  $ilDB->execute($st, array("MediaContainer", "Media", $this->getId()));
2325  $ilDB->execute($st, array("PageContainer", "Page", $this->getId()));
2326 
2327  // style_parameter: class for class
2328  $st = $ilDB->prepareManip("UPDATE style_parameter SET class = ? WHERE class = ?".
2329  " AND style_id = ? ", array("text", "text", "integer"));
2330  $ilDB->execute($st, array("MediaContainer", "Media", $this->getId()));
2331  $ilDB->execute($st, array("PageContainer", "Page", $this->getId()));
2332 
2333  // force rewriting of container style
2334  $st = $ilDB->prepareManip("DELETE FROM style_char WHERE type = ?".
2335  " AND style_id = ? ", array("text", "integer"));
2336  $ilDB->execute($st, array("page_cont", $this->getId()));
2337  $st = $ilDB->prepareManip("DELETE FROM style_parameter WHERE type = ?".
2338  " AND style_id = ? ", array("text", "integer"));
2339  $ilDB->execute($st, array("page_cont", $this->getId()));
2340 
2341  }
2342 
2349  function do_3_10_CharMigration($a_id = "")
2350  {
2351  global $ilDB;
2352 
2353  $add_str = "";
2354  if ($a_id != "")
2355  {
2356  $add_str = " AND style_id = ".$ilDB->quote($a_id, "integer");
2357  }
2358 
2359  $set = $ilDB->query($q = "SELECT DISTINCT style_id, tag, class FROM style_parameter WHERE ".
2360  $ilDB->equals("type", "", "text", true)." ".$add_str);
2361 
2362  while ($rec = $ilDB->fetchAssoc($set))
2363  {
2364  // derive types from tag
2365  $types = array();
2366  switch ($rec["tag"])
2367  {
2368  case "div":
2369  case "p":
2370  if (in_array($rec["class"], array("Headline3", "Headline1",
2371  "Headline2", "TableContent", "List", "Standard", "Remark",
2372  "Additional", "Mnemonic", "Citation", "Example")))
2373  {
2374  $types[] = "text_block";
2375  }
2376  if (in_array($rec["class"], array("Block", "Remark",
2377  "Additional", "Mnemonic", "Example", "Excursus", "Special")))
2378  {
2379  $types[] = "section";
2380  }
2381  if (in_array($rec["class"], array("Page", "Footnote", "PageTitle", "LMNavigation")))
2382  {
2383  $types[] = "page";
2384  }
2385  break;
2386 
2387  case "td":
2388  $types[] = "table_cell";
2389  break;
2390 
2391  case "a":
2392  if (in_array($rec["class"], array("ExtLink", "IntLink", "FootnoteLink")))
2393  {
2394  $types[] = "link";
2395  }
2396  break;
2397 
2398  case "span":
2399  $types[] = "text_inline";
2400  break;
2401 
2402  case "table":
2403  $types[] = "table";
2404  break;
2405  }
2406 
2407  // check if style_char set exists
2408  foreach ($types as $t)
2409  {
2410  // check if second type already exists
2411  $set4 = $ilDB->queryF("SELECT * FROM style_char ".
2412  " WHERE style_id = %s AND type = %s AND characteristic = %s",
2413  array("integer", "text", "text"),
2414  array($rec["style_id"], $t, $rec["class"]));
2415  if ($rec4 = $ilDB->fetchAssoc($set4))
2416  {
2417  // ok
2418  }
2419  else
2420  {
2421 //echo "<br>1-".$rec["style_id"]."-".$t."-".$rec["class"]."-";
2422  $ilDB->manipulateF("INSERT INTO style_char ".
2423  " (style_id, type, characteristic) VALUES ".
2424  " (%s,%s,%s) ",
2425  array("integer", "text", "text"),
2426  array($rec["style_id"], $t, $rec["class"]));
2427  }
2428  }
2429 
2430  // update types
2431  if ($rec["type"] == "")
2432  {
2433  if (count($types) > 0)
2434  {
2435  $ilDB->manipulateF("UPDATE style_parameter SET type = %s ".
2436  " WHERE style_id = %s AND class = %s AND ".$ilDB->equals("type", "", "text", true),
2437  array("text", "integer", "text"),
2438  array($types[0], $rec["style_id"], $rec["class"]));
2439 //echo "<br>3-".$types[0]."-".$rec["style_id"]."-".$rec["class"]."-";
2440 
2441  // links extra handling
2442  if ($types[0] == "link")
2443  {
2444  $ilDB->manipulateF("UPDATE style_parameter SET type = %s ".
2445  " WHERE style_id = %s AND (class = %s OR class = %s) AND ".$ilDB->equals("type", "", "text", true),
2446  array("text", "integer", "text", "text"),
2447  array($types[0], $rec["style_id"], $rec["class"].":visited",
2448  $rec["class"].":hover"));
2449  }
2450  }
2451 
2452  if (count($types) == 2)
2453  {
2454  // select all records of first type and add second type
2455  // records if necessary.
2456  $set2 = $ilDB->queryF("SELECT * FROM style_parameter ".
2457  " WHERE style_id = %s AND class = %s AND type = %s",
2458  array("integer", "text", "text"),
2459  array($rec["style_id"], $rec["class"], $types[0]));
2460  while ($rec2 = $ilDB->fetchAssoc($set2))
2461  {
2462  // check if second type already exists
2463  $set3 = $ilDB->queryF("SELECT * FROM style_parameter ".
2464  " WHERE style_id = %s AND tag = %s AND class = %s AND type = %s AND parameter = %s",
2465  array("integer", "text", "text", "text", "text"),
2466  array($rec["style_id"], $rec["tag"], $rec["class"], $types[1], $rec["parameter"]));
2467  if ($rec3 = $ilDB->fetchAssoc($set3))
2468  {
2469  // ok
2470  }
2471  else
2472  {
2473  $nid = $ilDB->nextId("style_parameter");
2474  $ilDB->manipulateF("INSERT INTO style_parameter ".
2475  " (id, style_id, tag, class, parameter, value, type) VALUES ".
2476  " (%s, %s,%s,%s,%s,%s,%s) ",
2477  array("integer", "integer", "text", "text", "text", "text", "text"),
2478  array($nid, $rec2["style_id"], $rec2["tag"], $rec2["class"],
2479  $rec2["parameter"], $rec2["value"], $types[1]));
2480  }
2481  }
2482  }
2483  }
2484  }
2485  }
2486 
2490  function do_3_9_Migration($a_id)
2491  {
2492  global $ilDB;
2493 
2494  $classes = array("Example", "Additional", "Citation", "Mnemonic", "Remark");
2495  $pars = array("margin-top", "margin-bottom");
2496 
2497  foreach ($classes as $curr_class)
2498  {
2499  foreach ($pars as $curr_par)
2500  {
2501  $res2 = $ilDB->queryF("SELECT id FROM style_parameter WHERE style_id = %s".
2502  " AND tag = %s AND class= %s AND parameter = %s",
2503  array("integer", "text", "text", "text"),
2504  array($a_id, "p", $curr_class, $curr_par));
2505  if ($row2 = $ilDB->fetchAssoc($res2))
2506  {
2507  $ilDB->manipulateF("UPDATE style_parameter SET value= %s WHERE id = %s",
2508  array("text", "integer"),
2509  array("10px", $row2["id"]));
2510  }
2511  else
2512  {
2513  $nid = $ilDB->nextId("style_parameter");
2514  $ilDB->manipulateF("INSERT INTO style_parameter ".
2515  "(id, style_id, tag, class, parameter,value) VALUES (%s,%s,%s,%s,%s,%s)",
2516  array("integer", "integer", "text", "text", "text", "text"),
2517  array($nid, $a_id, "div", $curr_class, $curr_par, "10px"));
2518  }
2519  }
2520  }
2521 
2522  $ilDB->manipulateF("UPDATE style_parameter SET tag = %s WHERE tag = %s and style_id = %s",
2523  array("text", "text", "integer"),
2524  array("div", "p", $a_id));
2525 
2526  }
2527 
2528 
2532  function getColors()
2533  {
2534  global $ilDB;
2535 
2536  $set = $ilDB->query("SELECT * FROM style_color WHERE ".
2537  "style_id = ".$ilDB->quote($this->getId(), "integer")." ".
2538  "ORDER BY color_name");
2539 
2540  $colors = array();
2541  while ($rec = $ilDB->fetchAssoc($set))
2542  {
2543  $colors[] = array(
2544  "name" => $rec["color_name"],
2545  "code" => $rec["color_code"]
2546  );
2547  }
2548 
2549  return $colors;
2550  }
2551 
2555  function addColor($a_name, $a_code)
2556  {
2557  global $ilDB;
2558 
2559  $ilDB->manipulate("INSERT INTO style_color (style_id, color_name, color_code)".
2560  " VALUES (".
2561  $ilDB->quote($this->getId(), "integer").",".
2562  $ilDB->quote($a_name, "text").",".
2563  $ilDB->quote($a_code, "text").
2564  ")");
2565  }
2566 
2570  function updateColor($a_name, $a_new_name, $a_code)
2571  {
2572  global $ilDB;
2573 
2574  // todo: update names in parameters as well
2575 
2576  $ilDB->manipulate("UPDATE style_color SET ".
2577  "color_name = ".$ilDB->quote($a_new_name, "text").", ".
2578  "color_code = ".$ilDB->quote($a_code, "text").
2579  " WHERE style_id = ".$ilDB->quote($this->getId(), "integer").
2580  " AND color_name = ".$ilDB->quote($a_name, "text"));
2581  ilObjStyleSheet::_writeUpToDate($this->getId(), false);
2582 
2583  // rename also the name in the style parameter values
2584  if ($a_name != $a_new_name)
2585  {
2586  $set = $ilDB->query("SELECT * FROM style_parameter ".
2587  " WHERE style_id = ".$ilDB->quote($this->getId(), "integer").
2588  " AND (".
2589  " parameter = ".$ilDB->quote("background-color", "text"). " OR ".
2590  " parameter = ".$ilDB->quote("color", "text"). " OR ".
2591  " parameter = ".$ilDB->quote("border-color", "text"). " OR ".
2592  " parameter = ".$ilDB->quote("border-top-color", "text"). " OR ".
2593  " parameter = ".$ilDB->quote("border-bottom-color", "text"). " OR ".
2594  " parameter = ".$ilDB->quote("border-left-color", "text"). " OR ".
2595  " parameter = ".$ilDB->quote("border-right-color", "text").
2596  ")");
2597  while ($rec = $ilDB->fetchAssoc($set))
2598  {
2599  if ($rec["value"] == "!".$a_name ||
2600  is_int(strpos($rec["value"], "!".$a_name."(")))
2601  {
2602  // parameter is based on color -> rename it
2603  $this->replaceStylePar($rec["tag"], $rec["class"],
2604  $rec["parameter"], str_replace($a_name, $a_new_name, $rec["value"]), $rec["type"]);
2605  }
2606  }
2607  }
2608  }
2609 
2613  function removeColor($a_name)
2614  {
2615  global $ilDB;
2616 
2617  $ilDB->manipulate("DELETE FROM style_color WHERE ".
2618  " style_id = ".$ilDB->quote($this->getId(), "integer")." AND ".
2619  " color_name = ".$ilDB->quote($a_name, "text"));
2620  }
2621 
2625  function colorExists($a_color_name)
2626  {
2627  global $ilDB;
2628 
2629  $set = $ilDB->query("SELECT * FROM style_color WHERE ".
2630  "style_id = ".$ilDB->quote($this->getId(), "integer")." AND ".
2631  "color_name = ".$ilDB->quote($a_color_name, "text"));
2632  if ($rec = $ilDB->fetchAssoc($set))
2633  {
2634  return true;
2635  }
2636  return false;
2637  }
2638 
2642  function getColorCodeForName($a_name)
2643  {
2644  global $ilDB;
2645 
2646  $pos = strpos($a_name, "(");
2647  if ($pos > 0)
2648  {
2649  $a_i = substr($a_name, $pos + 1);
2650  $a_i = str_replace(")", "", $a_i);
2651  $a_name = substr($a_name, 0, $pos);
2652  }
2653 
2654  $set = $ilDB->query("SELECT color_code FROM style_color WHERE ".
2655  " style_id = ".$ilDB->quote($this->getId(), "integer")." AND ".
2656  " color_name = ".$ilDB->quote($a_name, "text"));
2657  if ($rec = $ilDB->fetchAssoc($set))
2658  {
2659  if ($a_i == "")
2660  {
2661  return "#".$rec["color_code"];
2662  }
2663  else
2664  {
2665  return "#".ilObjStyleSheet::_getColorFlavor($rec["color_code"],
2666  (int) $a_i);
2667  }
2668  }
2669  }
2670 
2674  static function _getColorFlavor($a_rgb, $a_i)
2675  {
2676  $rgb = ilObjStyleSheet::_explodeRGB($a_rgb, true);
2677  $hls = ilObjStyleSheet::_RGBToHLS($rgb);
2678 
2679  if ($a_i > 0)
2680  {
2681  $hls["l"] = $hls["l"] + ((255 - $hls["l"]) * ($a_i / 100));
2682  }
2683  if ($a_i < 0)
2684  {
2685  $hls["l"] = $hls["l"] - (($hls["l"]) * (-$a_i / 100));
2686  }
2687 
2688  $rgb = ilObjStyleSheet::_HLSToRGB($hls);
2689 
2690  foreach ($rgb as $k => $v)
2691  {
2692  $rgb[$k] = str_pad(dechex($v), 2, "0", STR_PAD_LEFT);
2693  }
2694 
2695  return $rgb["r"].$rgb["g"].$rgb["b"];
2696  }
2697 
2701  static function _explodeRGB($a_rgb, $as_dec = false)
2702  {
2703  $r["r"] = substr($a_rgb, 0, 2);
2704  $r["g"] = substr($a_rgb, 2, 2);
2705  $r["b"] = substr($a_rgb, 4, 2);
2706 
2707  if ($as_dec)
2708  {
2709  $r["r"] = (int) hexdec($r["r"]);
2710  $r["g"] = (int) hexdec($r["g"]);
2711  $r["b"] = (int) hexdec($r["b"]);
2712  }
2713 
2714  return $r;
2715  }
2716 
2720  static function _RGBToHLS($a_rgb)
2721  {
2722  $r = $a_rgb["r"] / 255;
2723  $g = $a_rgb["g"] / 255;
2724  $b = $a_rgb["b"] / 255;
2725 
2726  // max / min
2727  $max = max($r,$g,$b);
2728  $min = min($r,$g,$b);
2729 
2730  //lightness
2731  $l = ($max + $min) / 2;
2732 
2733  if ($max == $min)
2734  {
2735  $s = 0;
2736  $h = 0;
2737  }
2738  else
2739  {
2740  if ($l < 0.5)
2741  {
2742  $s = ($max - $min) / ($max + $min);
2743  }
2744  else
2745  {
2746  $s = ($max - $min) / (2.0 - $max - $min);
2747  }
2748 
2749  if ($r == $max)
2750  {
2751  $h = ($g - $b) / ($max - $min);
2752  }
2753  else if ($g == $max)
2754  {
2755  $h = 2.0 + ($b - $r) / ($max - $min);
2756  }
2757  else if ($b == $max)
2758  {
2759  $h = 4.0 + ($r - $g) / ($max - $min);
2760  }
2761  }
2762 
2763  $hls["h"] = round(($h / 6) * 255);
2764  $hls["l"] = round($l * 255);
2765  $hls["s"] = round($s * 255);
2766 
2767  return $hls;
2768  }
2769 
2773  static function _HLSToRGB($a_hls)
2774  {
2775  $h = $a_hls["h"] / 255;
2776  $l = $a_hls["l"] / 255;
2777  $s = $a_hls["s"] / 255;
2778 
2779  $rgb["r"] = $rgb["g"] = $rgb["b"] = 0;
2780 
2781  // If S=0, define R, G, and B all to L
2782  if ($s == 0)
2783  {
2784  $rgb["r"] = $rgb["g"] = $rgb["b"] = $l;
2785  }
2786  else
2787  {
2788 
2789  if ($l < 0.5)
2790  {
2791  $temp2 = $l * (1.0 + $s);
2792  }
2793  else
2794  {
2795  $temp2 = $l + $s - $l * $s;
2796  }
2797 
2798  $temp1 = 2.0 * $l - $temp2;
2799 
2800 
2801  # For each of R, G, B, compute another temporary value, temp3, as follows:
2802  foreach ($rgb as $k => $v)
2803  {
2804  switch ($k)
2805  {
2806  case "r":
2807  $temp3 = $h + 1.0 / 3.0;
2808  break;
2809 
2810  case "g":
2811  $temp3 = $h;
2812  break;
2813 
2814  case "b":
2815  $temp3 = $h - 1.0/3.0;
2816  break;
2817  }
2818  if ($temp3 < 0)
2819  {
2820  $temp3 = $temp3 + 1.0;
2821  }
2822  if ($temp3 > 1)
2823  {
2824  $temp3 = $temp3 - 1.0;
2825  }
2826 
2827  if (6.0 * $temp3 < 1)
2828  {
2829  $rgb[$k] = $temp1 + ($temp2 - $temp1) * 6.0 * $temp3;
2830  }
2831  else if (2.0 * $temp3 < 1)
2832  {
2833  $rgb[$k] = $temp2;
2834  }
2835  else if (3.0 * $temp3 < 2)
2836  {
2837  $rgb[$k] = $temp1 + ($temp2 - $temp1) * ((2.0/3.0) - $temp3) * 6.0;
2838  }
2839  else
2840  {
2841  $rgb[$k] = $temp1;
2842  }
2843  }
2844  }
2845 
2846  $rgb["r"] = round($rgb["r"] * 255);
2847  $rgb["g"] = round($rgb["g"] * 255);
2848  $rgb["b"] = round($rgb["b"] * 255);
2849 
2850  return $rgb;
2851  }
2852 
2853  //
2854  // Table template management
2855  //
2856 
2860  function getTemplates($a_type)
2861  {
2862  global $ilDB;
2863 
2864  $set = $ilDB->query("SELECT * FROM style_template WHERE ".
2865  "style_id = ".$ilDB->quote($this->getId(), "integer")." AND ".
2866  "temp_type = ".$ilDB->quote($a_type, "text")." ".
2867  "ORDER BY name");
2868 
2869  $templates = array();
2870  while ($rec = $ilDB->fetchAssoc($set))
2871  {
2872  $rec["classes"] = $this->getTemplateClasses($rec["id"]);
2873  $templates[] = $rec;
2874  }
2875 
2876  return $templates;
2877  }
2878 
2882  function getTemplateClasses($a_tid)
2883  {
2884  global $ilDB;
2885  $set = $ilDB->query("SELECT * FROM style_template_class WHERE ".
2886  "template_id = ".$ilDB->quote($a_tid, "integer"));
2887 
2888  $class = array();
2889  while ($rec = $ilDB->fetchAssoc($set))
2890  {
2891  $key = $rec["class_type"];
2892  $class[$key] = $rec["class"];
2893  }
2894 
2895  return $class;
2896  }
2897 
2898 
2902  function addTemplate($a_type, $a_name, $a_classes)
2903  {
2904  global $ilDB;
2905 
2906  $tid = $ilDB->nextId("style_template");
2907  $ilDB->manipulate($q = "INSERT INTO style_template ".
2908  "(id, style_id, name, temp_type)".
2909  " VALUES (".
2910  $ilDB->quote($tid, "integer").",".
2911  $ilDB->quote($this->getId(), "integer").",".
2912  $ilDB->quote($a_name, "text").",".
2913  $ilDB->quote($a_type, "text").
2914  ")");
2915 
2916  foreach ($a_classes as $t => $c)
2917  {
2918  $ilDB->manipulate($q = "INSERT INTO style_template_class ".
2919  "(template_id, class_type, class)".
2920  " VALUES (".
2921  $ilDB->quote($tid, "integer").",".
2922  $ilDB->quote($t, "text").",".
2923  $ilDB->quote($c, "text").
2924  ")");
2925  }
2926 
2927  include_once("./Services/Style/classes/class.ilObjStyleSheetGUI.php");
2928  $this->writeTemplatePreview($tid,
2929  ilObjStyleSheetGUI::_getTemplatePreview($this, $a_type, $tid, true));
2930 
2931  return $tid;
2932  }
2933 
2937  function updateTemplate($a_t_id, $a_name, $a_classes)
2938  {
2939  global $ilDB;
2940 
2941  $ilDB->manipulate("UPDATE style_template SET ".
2942  "name = ".$ilDB->quote($a_name, "text").
2943  " WHERE id = ".$ilDB->quote($a_t_id, "integer"));
2944 
2945  $ilDB->manipulate("DELETE FROM style_template_class WHERE ".
2946  "template_id = ".$ilDB->quote($a_t_id, "integer")
2947  );
2948  foreach ($a_classes as $t => $c)
2949  {
2950  $ilDB->manipulate($q = "INSERT INTO style_template_class ".
2951  "(template_id, class_type, class)".
2952  " VALUES (".
2953  $ilDB->quote($a_t_id, "integer").",".
2954  $ilDB->quote($t, "text").",".
2955  $ilDB->quote($c, "text").
2956  ")");
2957  }
2958 
2959  }
2960 
2964  function templateExists($a_template_name)
2965  {
2966  global $ilDB;
2967 
2968  $set = $ilDB->query("SELECT * FROM style_template WHERE ".
2969  "style_id = ".$ilDB->quote($this->getId(), "integer")." AND ".
2970  "name = ".$ilDB->quote($a_template_name, "text"));
2971  if ($rec = $ilDB->fetchAssoc($set))
2972  {
2973  return true;
2974  }
2975  return false;
2976  }
2977 
2981  function getTemplate($a_t_id)
2982  {
2983  global $ilDB;
2984 
2985  $set = $ilDB->query("SELECT * FROM style_template WHERE ".
2986  "style_id = ".$ilDB->quote($this->getId(), "integer")." ".
2987  " AND id = ".$ilDB->quote($a_t_id, "integer"));
2988 
2989  if ($rec = $ilDB->fetchAssoc($set))
2990  {
2991  $rec["classes"] = $this->getTemplateClasses($rec["id"]);
2992 
2993  $template = $rec;
2994  return $template;
2995  }
2996  return array();
2997  }
2998 
3002  function lookupTemplateName($a_t_id)
3003  {
3004  global $ilDB;
3005 
3006  $set = $ilDB->query("SELECT name FROM style_template WHERE ".
3007  " id = ".$ilDB->quote($a_t_id, "integer"));
3008 
3009  if ($rec = $ilDB->fetchAssoc($set))
3010  {
3011  return $rec["name"];
3012  }
3013 
3014  return false;
3015  }
3016 
3020  function getTemplateXML()
3021  {
3022  global $ilDB;
3023 
3024  $tag = "<StyleTemplates>";
3025 
3026  $ttypes = array("table", "vaccordion", "haccordion");
3027 
3028  foreach ($ttypes as $ttype)
3029  {
3030  $ts = $this->getTemplates($ttype);
3031 
3032  foreach($ts as $t)
3033  {
3035  /*$atts = array("table" => "TableClass",
3036  "caption" => "CaptionClass",
3037  "row_head" => "RowHeadClass",
3038  "row_foot" => "RowFootClass",
3039  "col_head" => "ColHeadClass",
3040  "col_foot" => "ColFootClass",
3041  "odd_row" => "OddRowClass",
3042  "even_row" => "EvenRowClass",
3043  "odd_col" => "OddColClass",
3044  "even_col" => "EvenColClass");*/
3045  $c = $t["classes"];
3046 
3047  $tag.= '<StyleTemplate Name="'.$t["name"].'">';
3048 
3049  foreach ($atts as $type => $t)
3050  {
3051  if ($c[$type] != "")
3052  {
3053  $tag.= '<StyleClass Type="'.$type.'" Value="'.$c[$type].'" />';
3054  }
3055  }
3056 
3057  $tag.= "</StyleTemplate>";
3058  }
3059  }
3060 
3061  $tag.= "</StyleTemplates>";
3062 
3063 //echo htmlentities($tag);
3064  return $tag;
3065  }
3066 
3070  function writeTemplatePreview($a_t_id, $a_preview_html)
3071  {
3072  global $ilDB;
3073  $a_preview_html = str_replace(' width=""', "", $a_preview_html);
3074  $a_preview_html = str_replace(' valign="top"', "", $a_preview_html);
3075  $a_preview_html = str_replace('<div class="ilc_text_block_TableContent">', "<div>", $a_preview_html);
3076 //echo "1-".strlen($a_preview_html)."-";
3077 //echo htmlentities($a_preview_html);
3078  if (strlen($a_preview_html) > 4000)
3079  {
3080 //echo "2";
3081  $a_preview_html = "";
3082  }
3083  $ilDB->manipulate("UPDATE style_template SET ".
3084  "preview = ".$ilDB->quote($a_preview_html, "text").
3085  " WHERE id = ".$ilDB->quote($a_t_id, "integer"));
3086  }
3087 
3091  function lookupTemplatePreview($a_t_id)
3092  {
3093  global $ilDB;
3094 
3095  $set = $ilDB->query("SELECT preview FROM style_template ".
3096  " WHERE id = ".$ilDB->quote($a_t_id, "integer"));
3097  if ($rec = $ilDB->fetchAssoc($set))
3098  {
3099  return $rec["preview"];
3100  }
3101 
3102  return "";
3103  }
3104 
3108  static function _lookupTemplateIdByName($a_style_id, $a_name)
3109  {
3110  global $ilDB;
3111 
3112  $set = $ilDB->query("SELECT id FROM style_template ".
3113  " WHERE style_id = ".$ilDB->quote($a_style_id, "integer").
3114  " AND name = ".$ilDB->quote($a_name, "text"));
3115  if ($rec = $ilDB->fetchAssoc($set))
3116  {
3117  return $rec["id"];
3118  }
3119 
3120  return false;
3121  }
3122 
3126  function removeTemplate($a_t_id)
3127  {
3128  global $ilDB;
3129 
3130  $ilDB->manipulate("DELETE FROM style_template WHERE ".
3131  " style_id = ".$ilDB->quote($this->getId(), "integer")." AND ".
3132  " id = ".$ilDB->quote($a_t_id, "integer"));
3133 
3134  $ilDB->manipulate("DELETE FROM style_template_class WHERE ".
3135  "template_id = ".$ilDB->quote($a_t_id, "integer")
3136  );
3137 
3138  }
3139 
3143  function writeStyleSetting($a_name, $a_value)
3144  {
3145  global $ilDB;
3146 
3147  $ilDB->manipulate("DELETE FROM style_setting WHERE ".
3148  " style_id = ".$ilDB->quote($this->getId(), "integer").
3149  " AND name = ".$ilDB->quote($a_name, "text")
3150  );
3151 
3152  $ilDB->manipulate("INSERT INTO style_setting ".
3153  "(style_id, name, value) VALUES (".
3154  $ilDB->quote($this->getId(), "integer").",".
3155  $ilDB->quote($a_name, "text").",".
3156  $ilDB->quote($a_value, "text").
3157  ")");
3158  }
3159 
3163  function lookupStyleSetting($a_name)
3164  {
3165  global $ilDB;
3166 
3167  $set = $ilDB->query("SELECT value FROM style_setting ".
3168  " WHERE style_id = ".$ilDB->quote($this->getId(), "integer").
3169  " AND name = ".$ilDB->quote($a_name, "text")
3170  );
3171  $rec = $ilDB->fetchAssoc($set);
3172 
3173  return $rec["value"];
3174  }
3175 
3179  static function writeStyleUsage($a_obj_id, $a_style_id)
3180  {
3181  global $ilDB;
3182 
3183  $ilDB->replace("style_usage", array(
3184  "obj_id" => array("integer", (int) $a_obj_id)),
3185  array(
3186  "style_id" => array("integer", (int) $a_style_id))
3187  );
3188  }
3189 
3193  static function lookupObjectStyle($a_obj_id)
3194  {
3195  global $ilDB;
3196 
3197  $set = $ilDB->query("SELECT style_id FROM style_usage ".
3198  " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer")
3199  );
3200  $rec = $ilDB->fetchAssoc($set);
3201 
3202  return (int) $rec["style_id"];
3203  }
3204 
3205 
3206 }
3207 ?>