PageRenderTime 14ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/apps/wiki/domain/Entry.php

http://zoop.googlecode.com/
PHP | 140 lines | 121 code | 17 blank | 2 comment | 13 complexity | fcc7d0798d45be3525523bd162c2d74f MD5 | raw file
  1<?php
  2class Entry extends DbObject
  3{
  4	public function init()
  5	{
  6		$this->keyAssignedBy = parent::keyAssignedBy_dev;
  7	}
  8	
  9	public function getDateNameUrl()
 10	{
 11		$datePart = substr($this->published_date, 0, strpos($this->published_date, ' '));
 12		$datePart = str_replace('-', '/', $datePart);
 13		return $datePart . '/' . $this->name;
 14	}
 15	
 16	public function getBasePath()
 17	{
 18		$id = str_pad($this->id, 4, '0', STR_PAD_LEFT);
 19		return Config::get('app.blog.contentDir') . "/{$id}_{$this->name}";
 20	}
 21	
 22	public function getContentPath()
 23	{
 24		return $this->getBasePath() . '/content.md'; 
 25	}
 26	
 27	public function getCacheDir()
 28	{
 29		return app_dir . "/tmp/content/{$this->id}";
 30	}
 31	
 32	public function streamAsset($name)
 33	{
 34		$info = pathinfo($name);
 35		// if($info['extension'] == 'png')
 36		// 	header('Content-Type: image/png');
 37		readfile($this->getCacheDir() . '/' . $name);
 38	}
 39	
 40	public function getHeaders()
 41	{
 42		$pre = file($this->getContentPath());
 43		$headers = array();
 44		foreach($pre as $line)
 45		{
 46			$line = trim($line);
 47			if(!$line)
 48				break;
 49			
 50			$pos = strpos($line, ':');
 51			$key = strtolower(trim(substr($line, 0, $pos)));
 52			$value = trim(substr($line, $pos + 1));
 53			$headers[$key] = $value;
 54		}
 55		
 56		return $headers;
 57	}
 58	
 59	public function assignHeaders()
 60	{
 61		$headers = $this->getHeaders();
 62		echo_r($headers);
 63		if(isset($headers['publish-date']))
 64			$this->published_date = $headers['publish-date'];
 65		if(isset($headers['title']))
 66			$this->title = $headers['title'];
 67		if(isset($headers['link']))
 68			$this->link = $headers['link'];
 69		if(isset($headers['link-text']))
 70			$this->link_text = $headers['link-text'];
 71		echo_r($this);
 72	}
 73	
 74	public function getContent($cacheResults = false)
 75	{
 76		$pre = file($this->getContentPath());
 77		$headers = array();
 78		$inHeaders = true;
 79		$content = '';
 80		foreach($pre as $line)
 81		{
 82			if($inHeaders && !trim($line))
 83			{
 84				$inHeaders = false;
 85				continue;
 86			}
 87			
 88			if($inHeaders)
 89			{
 90				$pos = strpos($line, ':');
 91				$key = strtolower(trim(substr($line, 0, $pos)));
 92				$value = trim(substr($line, $pos + 1));
 93				$headers[$key] = $value;
 94			}
 95			else
 96				$content .= $line;
 97		}
 98		
 99		if($cacheResults)
100		{
101			$cacheDir = $this->getCacheDir();
102			if(!is_dir($cacheDir))
103				mkdir($cacheDir, 0770, true);
104		}
105		
106		if(isset($headers['filters']))
107		{
108			include_once app_dir . '/domain/filters/ContentFilter.php';
109			$filterList = explode(',', $headers['filters']);
110			foreach($filterList as $filterName)
111			{
112				$filterName = trim($filterName);
113				include_once app_dir . "/domain/filters/$filterName.php";
114				$className = ucfirst($filterName) . 'Filter';
115				$filter = new $className();
116				$filter->assetBaseUrl = 'index.php/asset/' . $this->id;
117				
118				$filter->assetSrcDir = $this->getBasePath();
119				if($cacheResults)
120				{
121					$filter->assetCacheDir = $cacheDir;
122				}
123				$content = $filter->filter($content, $cacheResults);
124			}
125		}
126		
127		if($cacheResults)
128		{
129			$filename = "$cacheDir/{$this->name}.html";
130			file_put_contents($filename, $this->getContent());
131		}
132		
133		return $content;
134	}
135	
136	public function cacheContent()
137	{
138		$this->getContent(true);
139	}
140}