psf
 All Classes Functions Variables
container.php
1 <?php
2 
3 //Part of simple php framework (spf)
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 Petr Bena 2015
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  protected $Items = array();
29  protected $cIndent = 4;
30 
31  function __construct($_parent = NULL)
32  {
33  parent::__construct($_parent);
34  }
35 
36  protected function ReplaceControl($text)
37  {
38  return str_replace("\n", "<br>", $text);
39  }
40 
42  public function AppendHtmlLine($html, $indent = -1)
43  {
44  $value = "";
45  while ($indent-- > 0)
46  {
47  $value .= " ";
48  }
49  $this->AppendObject(new HtmlPrimitiveObject($value . $html));
50  }
51 
52  public function AppendHtml($html, $indent = -1)
53  {
54  $lines = explode("\n", $html);
55  foreach ($lines as $l)
56  $this->AppendHtmlLine($l, $indent);
57  }
58 
59  public function AppendParagraph($text)
60  {
61  $this->AppendHtmlLine($this->html_p($this->ReplaceControl(htmlspecialchars($text))));
62  }
63 
64  public function AppendPreformatted($text)
65  {
66  $pre = new HtmlPrimitiveObject("<pre>\n" . $text . "\n</pre>");
67  // we have to disable indenting here, because it simply is not desired
68  $pre->Indent = false;
69  $this->AppendObject($pre);
70  }
71 
72  public function AppendPre($text)
73  {
74  $this->AppendPreformatted($text);
75  }
76 
77  public function AppendLine()
78  {
79  $this->AppendHtmlLine("<hr>");
80  }
81 
82  public function AppendObject($object, $indent = -1)
83  {
84  $object->Parent = $this;
85  array_push($this->Items, $object);
86  }
87 
88  public function _gen_html_tag($name, $value, $param = "")
89  {
90  if (strlen($param) == 0)
91  return "<$name>" . $value . "</$name>";
92  else
93  return "<$name $param>" . $value . "</$name>";
94  }
95 
96  public function html_b($text) { return $this->_gen_html_tag("b", $text); }
97  public function html_div($text) { return $this->_gen_html_tag("div", $text); }
98  public function html_font($text, $param = "") { return $this->_gen_html_tag("font", $text, $param); }
99  public function html_p($text) { return $this->_gen_html_tag("p", $text); }
100 
101  public function InsertFileToBody($f)
102  {
103  $tx = file_get_contents($f);
104  if ($tx === FALSE)
105  throw new Exception("File couldn't be read: " . $f);
106  $this->AppendObject(new HtmlPrimitiveObject($tx));
107  }
108 
109  public function ToHtml()
110  {
111  $indent = 4;
112  $_b = "";
113  foreach ($this->Items as $html)
114  {
115  // Convert the object to html
116  if ($this->Indent && $html->Indent)
117  $_b .= psf_indent_text($html->ToHtml(), $indent);
118  else
119  $_b .= $html->ToHtml() . "\n";
120  }
121  return $_b;
122  }
123 }
124