mkoh.net beta!

Source

Source for: 'library/MkohZf/View/Helper/NiceLink.php'


<?php
class MkohZf_View_Helper_NiceLink extends Zend_View_Helper_Abstract
{
	private $_cache;
	
	public function setView(Zend_View_Interface $view)
	{
		$this->_prepareCache();
		parent::setView($view);
	}
	
	public function niceLink($href, $content, $showSnap = true, $title = null, $classNames = array())
	{
		if($showSnap) {
			$classNames[] = 'websnap';
		}
		if(!$title) {
			$cacheId = md5($href);
			if(!$title = $this->_cache->load($cacheId)) {
				
				$client = new Zend_Http_Client($href);
				$response = $client->request('GET');
				
				if($response->getStatus() == 200) {
					$title = '';
					$rHtml = $response->getBody();
					$matches = array();
					if(preg_match ('/<title>(.*?)<\/title>/', $rHtml, $matches)) {
						$title = $matches[1];
					}
				} else {
					$classNames[] = 'link-error';
					$title = 'This link seems to be invalid..';
				}
				
				$this->_cache->save($title, $cacheId);
			}		
		}
		$title = $this->view->escape($title);
		$classNames = implode(' ', $classNames);
		return "<a href=\"{$href}\" title=\"{$title}\" class=\"{$classNames}\">{$content}</a>";
	}
	
	private function _prepareCache() 
	{
		$this->_cache = Zend_Cache::factory
		(
			'Core', // frontend name
			'File', // backend name
			array // frontend options
			(
				'automatic_serialization' => false,
				'cache_id_prefix' => 'niceLink',
				'lifetime' => null
			),
			array // backend options
			(
				'cache_dir'=> Zend_Registry::get('config')->storage->filecache_dir,
			)  
		);
	}
}

Comments

No comments, why not write one?

Add a comment

  _  __   _    _      _____   __   __    ______     ___    
 | |/ // | || | ||   / ___//  \ \\/ //  /_____//   / _ \\  
 | ' //  | || | ||   \___ \\   \ ` //   `____ `   | / \ || 
 | . \\  | \\_/ ||   /    //    | ||    /___//    | \_/ || 
 |_|\_\\  \____//   /____//     |_||    `__ `      \___//  
 `-` --`   `---`   `-----`      `-`'    /_//       `---`   
                                        `-`