Overview

Namespaces

  • Simpletools
    • Autoload
    • Config
    • Db
      • Mysql
    • Event
    • Http
    • Mvc
    • Page
    • Store

Classes

  • Simpletools\Autoload\Loader
  • Simpletools\Config\Ini
  • Simpletools\Db\Mysql\Client
  • Simpletools\Db\Mysql\Iterator
  • Simpletools\Db\Mysql\Model
  • Simpletools\Db\Mysql\QueryBuilder
  • Simpletools\Db\Mysql\Result
  • Simpletools\Db\Mysql\Sql
  • Simpletools\Event\Event
  • Simpletools\Http\Ssl
  • Simpletools\Mvc\Common
  • Simpletools\Mvc\Controller
  • Simpletools\Mvc\Model
  • Simpletools\Mvc\Router
  • Simpletools\Mvc\RoutingHook
  • Simpletools\Mvc\View
  • Simpletools\Page\Layout
  • Simpletools\Store\Cookie
  • Simpletools\Store\Flash
  • Simpletools\Store\Session
  • Overview
  • Namespace
  • Class
  1: <?php
  2: /*
  3:  * Simpletools Framework.
  4:  * Copyright (c) 2009, Marcin Rosinski. (http://www.getsimpletools.com)
  5:  * All rights reserved.
  6:  *
  7:  * LICENCE
  8:  *
  9:  * Redistribution and use in source and binary forms, with or without modification, 
 10:  * are permitted provided that the following conditions are met:
 11:  *
 12:  * -    Redistributions of source code must retain the above copyright notice, 
 13:  *      this list of conditions and the following disclaimer.
 14:  *
 15:  * -    Redistributions in binary form must reproduce the above copyright notice, 
 16:  *      this list of conditions and the following disclaimer in the documentation and/or other 
 17:  *      materials provided with the distribution.
 18:  *
 19:  * -    Neither the name of the Simpletools nor the names of its contributors may be used to 
 20:  *      endorse or promote products derived from this software without specific prior written permission.
 21:  *
 22:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
 23:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 24:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
 25:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 26:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 27:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
 28:  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
 29:  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 30:  *
 31:  * @framework       Simpletools
 32:  * @packages        Layout
 33:  * @description     Simple Layout solution
 34:  * @copyright       Copyright (c) 2009 Marcin Rosinski. (http://www.getsimpletools.com)
 35:  * @license         http://www.opensource.org/licenses/bsd-license.php - BSD
 36:  * @version         Ver: 2.0.15 2014-12-30 23: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 &registerLayouts(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:                     //echo preg_replace('/<!--(.*)-->/Uis', '', $html);
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(); //to prevent memory exhaust in case of big buffers
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: ?>
API documentation generated by ApiGen