psf
container.php
1 <?php
2 
3 // Part of php simple framework (psf)
4 
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 
15 // Copyright (c) Petr Bena <petr@bena.rocks> 2015 - 2018
16 
17 if (!defined("PSF_ENTRY_POINT"))
18  die("Not a valid psf entry point");
19 
20 require_once (dirname(__FILE__) . "/../object.php");
21 require_once (dirname(__FILE__) . "/../../default_config.php");
22 require_once (dirname(__FILE__) . "/../../functions.php");
23 require_once (dirname(__FILE__) . "/primitive_object.php");
24 
27 {
28  public $AutoInsertChilds = false;
29  protected $Items = array();
30  protected $cIndent = 4;
31 
32  function __construct($_parent = NULL)
33  {
34  global $psf_containers_auto_insert_child;
35  $this->AutoInsertChilds = $psf_containers_auto_insert_child;
36  parent::__construct($_parent);
37  }
38 
39  protected function ReplaceControl($text)
40  {
41  return str_replace("\n", "<br>", $text);
42  }
43 
45  public function AppendHtmlLine($html, $indent = -1)
46  {
47  $value = "";
48  while ($indent-- > 0)
49  {
50  $value .= " ";
51  }
52  $this->AppendObject(new HtmlPrimitiveObject($value . $html), -1, true);
53  }
54 
56  public function AppendHeader($text, $level = 1)
57  {
58  $this->AppendHtmlLine("<h$level>" . htmlspecialchars($text) . "</h$level>");
59  }
60 
61  public function AppendHtml($html, $indent = -1)
62  {
63  $lines = explode("\n", $html);
64  foreach ($lines as $l)
65  $this->AppendHtmlLine($l, $indent);
66  }
67 
68  public function AppendParagraph($text, $class = NULL)
69  {
70  $this->AppendHtmlLine($this->html_p($this->ReplaceControl(htmlspecialchars($text)), $class));
71  }
72 
73  public function AppendPreformatted($text)
74  {
75  $pre = new HtmlPrimitiveObject("<pre>\n" . $text . "\n</pre>");
76  // we have to disable indenting here, because it simply is not desired
77  $pre->Indent = false;
78  $this->AppendObject($pre, -1, true);
79  }
80 
81  public function AppendPre($text)
82  {
83  $this->AppendPreformatted($text);
84  }
85 
86  public function AppendLine()
87  {
88  $this->AppendHtmlLine("<hr>");
89  }
90 
91  public function AppendLineBreak()
92  {
93  $this->AppendHtmlLine("<br>");
94  }
95 
96  public function AppendObject($object, $indent = -1, $force = false)
97  {
98  if ($object === NULL)
99  return;
100  $object->Parent = $this;
101  if ($force || !in_array($object, $this->Items))
102  array_push($this->Items, $object);
103  }
104 
105  public function AddChild($_child)
106  {
107  if ($_child === NULL)
108  return;
109  if ($this->AutoInsertChilds)
110  {
111  $this->AppendObject($_child);
112  }
113  }
114 
115  public function _gen_html_tag($name, $value, $param = "")
116  {
117  if (strlen($param) == 0)
118  return "<$name>" . $value . "</$name>";
119  else
120  return "<$name $param>" . $value . "</$name>";
121  }
122 
123  public function html_b($text) { return $this->_gen_html_tag("b", $text); }
124  public function html_div($text) { return $this->_gen_html_tag("div", $text); }
125  public function html_font($text, $param = "") { return $this->_gen_html_tag("font", $text, $param); }
126  public function html_p($text, $class = NULL)
127  {
128  if ($class === NULL)
129  return $this->_gen_html_tag("p", $text);
130  else
131  return $this->_gen_html_tag("p", $text, 'class="' . $class . '"');
132  }
133 
134  public function InsertFileToBody($f)
135  {
136  $tx = file_get_contents($f);
137  if ($tx === FALSE)
138  throw new Exception("File couldn't be read: " . $f);
139  $this->AppendObject(new HtmlPrimitiveObject($tx));
140  }
141 
142  public function ToHtml()
143  {
144  $indent = 4;
145  $_b = "";
146  foreach ($this->Items as $html)
147  {
148  // Convert the object to html
149  if ($this->Indent && $html->Indent)
150  $_b .= psf_indent_text($html->ToHtml(), $indent);
151  else
152  $_b .= $html->ToHtml() . "\n";
153  }
154  return $_b;
155  }
156 }
157 
AppendHtmlLine($html, $indent=-1)
Insert a line of html into body of a page (to bottom of the body). If $indent contains anything else ...
Definition: container.php:45
Represent a single Html container, usually used by htmlpage or htmltable or any other element that is...
Definition: container.php:26
AppendHeader($text, $level=1)
Insert a header on bottom of current body of the page.
Definition: container.php:56