1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38:
39:
40: namespace Simpletools\Page;
41:
42: use \Simpletools\Page\Layout;
43:
44: class Layout
45: {
46: private $_layout = '';
47: private $_layoutDir = '';
48: private $_render = false;
49: private $_css = null;
50: private $_js = null;
51: private $_internalJs = null;
52: public $content = '';
53: private $_style = null;
54: private $_head_link = null;
55: private $_meta_tags = array();
56:
57: public $description = null;
58: public $title = null;
59:
60: private $_settings = null;
61:
62: protected $_layouts = array();
63: protected $_currentLayout = 'default';
64:
65: private static $_instance;
66:
67: public function __construct(array $settings)
68: {
69: $this->_settings = $settings;
70: $this->checkSettings();
71:
72: if(isset($settings['path_to_layout']))
73: {
74: $this->setLayout($settings['path_to_layout']);
75: }
76: elseif(isset($settings['path']))
77: {
78: $this->setLayout($settings['path']);
79: }
80: elseif(isset($settings['layouts']))
81: {
82: $this->registerLayouts($settings['layouts']);
83: }
84:
85: if (empty(self::$_instance))
86: {
87: self::$_instance = &$this;
88: }
89: }
90:
91: public static function &settings(array $settings)
92: {
93: if (empty(self::$_instance))
94: {
95: new Layout($settings);
96: }
97: else
98: {
99: $this->_settings = $settings;
100: $this->checkSettings();
101: }
102:
103: return self::getInstance();
104: }
105:
106: private function checkSettings()
107: {
108: if(!isset($this->_settings['charset']))
109: $this->_settings['charset'] = 'UTF-8';
110: }
111:
112: public static function &getInstance($settings=null)
113: {
114: if (empty(self::$_instance))
115: {
116: if(!$settings OR !is_array($settings))
117: {
118: $settings = array();
119: }
120:
121: self::$_instance = new Layout($settings);
122: }
123:
124: return self::$_instance;
125: }
126:
127: public function &setLayout($layDir)
128: {
129: $this->_layoutDir = $layDir;
130: $this->_render = true;
131:
132: $this->_layouts['default'] = $layDir;
133:
134: return $this;
135: }
136:
137: public function ®isterLayouts(array $layouts)
138: {
139: foreach($layouts as $name => $path)
140: {
141: $this->_layouts[$name] = $path;
142: }
143:
144: $this->_render = true;
145:
146: return $this;
147: }
148:
149: public function &start()
150: {
151: return $this->startBuffer();
152: }
153:
154: public function &startBuffer()
155: {
156: ob_start();
157:
158: return $this;
159: }
160:
161: public function clearBuffer()
162: {
163: ob_end_clean();
164: }
165:
166: public function render($minify=false)
167: {
168: if($this->_render)
169: {
170: $this->layout()->content = ob_get_contents();
171: ob_end_clean();
172:
173: ob_start();
174:
175: if($this->_render === true)
176: {
177: require($this->_layouts[$this->_currentLayout]);
178: $c = ob_get_contents();
179: }
180: else
181: $c = $this->layout()->content;
182:
183: ob_end_clean();
184:
185: if($minify)
186: {
187: echo preg_replace('/\s\s+/', ' ',str_replace(array("\t","\n","\r"),' ',$c));
188:
189: }
190: else
191: echo $c;
192: }
193:
194:
195: }
196:
197: public function &layout()
198: {
199: return $this;
200: }
201:
202: public function displayContent()
203: {
204: echo $this->layout()->content;
205: }
206:
207: public function &disable()
208: {
209: ob_end_clean();
210: $this->_render = false;
211:
212: return $this;
213: }
214:
215: public function &set($layout='default')
216: {
217: if($layout && isset($this->_layouts[$layout]))
218: {
219: $this->_currentLayout = $layout;
220: }
221:
222: return $this;
223: }
224:
225: public function &enable($layout='default')
226: {
227: $this->_render = true;
228:
229: if($layout && isset($this->_layouts[$layout]))
230: {
231: $this->_currentLayout = $layout;
232: }
233:
234: return $this;
235: }
236:
237: private function htmlentities($str)
238: {
239: return htmlentities(
240: html_entity_decode($str,ENT_COMPAT,$this->_settings['charset'])
241: ,ENT_COMPAT,$this->_settings['charset']);
242: }
243:
244: public function &setTitle($title)
245: {
246: $this->layout()->title = $this->htmlentities($title);
247: return $this;
248: }
249:
250: public function &setDefaultLayoutTitle($title)
251: {
252: $this->layout()->title = $title;
253: return $this;
254: }
255:
256: public function displayTitle()
257: {
258: echo $this->layout()->title;
259: }
260:
261: public function &setDescription($description)
262: {
263: $this->layout()->description = $this->htmlentities($description);
264: return $this;
265: }
266:
267: public function displayDescription()
268: {
269: echo '<meta name="description" content="'.$this->layout()->description.'" />'."\r\n";
270: }
271:
272: public function &addInternalCss($style)
273: {
274: $this->_style .= ' '.$style;
275: return $this;
276: }
277:
278: public function displayInternalCss()
279: {
280: if($this->_style != null)
281: {
282: echo '
283: <style type="text/css">
284: '.trim($this->_style,' ').'
285: </style>
286: ';
287: }
288: }
289:
290: public function &clearInternalCss()
291: {
292: $this->_style = null;
293: return $this;
294: }
295:
296: public function &addExternalCss($href)
297: {
298: $args = func_get_args();
299:
300: foreach($args as $href)
301: {
302: $this->_css .= '<link href="'.$href.'" rel="stylesheet" type="text/css" media="screen" />';
303: }
304:
305: return $this;
306: }
307:
308: public function &addExternalCss_($href,$media='screen',$rel='stylesheet')
309: {
310: $this->_css .= '<link href="'.$href.'" rel="'.$rel.'" type="text/css" media="'.$media.'" />';
311: return $this;
312: }
313:
314: public function &clearExternalCss()
315: {
316: $this->_css = null;
317: return $this;
318: }
319:
320: public function displayExternalCss()
321: {
322: if($this->_css) echo $this->_css;
323: }
324:
325: public function &addMetaTag($name,$content)
326: {
327: $this->_meta_tags[$name] = $content;
328:
329: return $this;
330: }
331:
332: public function displayMetaTags()
333: {
334: if(count($this->_meta_tags)) {
335:
336: foreach($this->_meta_tags as $tag => $content)
337: {
338: if($content)
339: {
340: echo '<meta name="'.$tag.'" content="'.$content.'" />';
341: }
342: }
343: }
344: }
345:
346: public function &addHeadLink(array $options)
347: {
348: $rel = isset($options['rel']) ? 'rel="'.$options['rel'].'"' : null;
349: $title = isset($options['title']) ? 'title="'.$options['title'].'"' : null;
350: $type = isset($options['type']) ? 'type="'.$options['type'].'"' : null;
351: $href = isset($options['href']) ? 'href="'.$options['href'].'"' : null;
352:
353: $this->_head_link .= '
354: <link '.$rel.' '.$title.' '.$type.' '.$href.' />' ;
355:
356: return $this;
357: }
358:
359: public function displayHeadLinks()
360: {
361: if($this->_head_link) echo $this->_head_link;
362: }
363:
364: public function &addExternalJs($href)
365: {
366: $args = func_get_args();
367:
368: foreach($args as $href)
369: {
370: $this->_js .= '<script src="'.$href.'" type="text/javascript"></script>';
371: }
372:
373: return $this;
374: }
375:
376: public function &clearExternalJs()
377: {
378: $this->_js = null;
379: return $this;
380: }
381:
382: public function displayExternalJs()
383: {
384: if($this->_js) echo $this->_js;
385: }
386:
387: public function &addInternalJs($source)
388: {
389: $this->_internalJs .= ' '.$source;
390: return $this;
391: }
392:
393: public function &clearInternalJs()
394: {
395: $this->_internalJs = null;
396: return $this;
397: }
398:
399: public function displayInternalJs()
400: {
401: if($this->_internalJs != null)
402: {
403: echo '
404: <script type="text/javascript">
405: /* <![CDATA[ */
406: '.trim($this->_internalJs,' ').'
407: /* ]]> */
408: </script>';
409: }
410:
411: }
412:
413: }
414:
415: ?>