~eda-qa/brainbrain/main

1
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php

/**
 * A standard entry viewing module for the ems-php-app framework.
 *
 * Assumes Request has a function: getNoteLink which takes a BrainEntry as parameter
 */
class ModuleBrainEntry extends Module
{
	/**
	 * Obtains the brain object to retrieve documents. By default it just uses
	 * the global brain object.
	 */
	protected function getBrain()
	{
		global $brain;
		return $brain;
	}
	
	protected $pathOffset = 1; //may be overriden in derived if this is not the first offset in path for id/name
	protected $allowNS = array(); //which namespaces are allowed, empty is always allowed
	protected $be;
	protected $showTitle = true; //show's the title if it doesn't contain a namespace, use <h1> tags
	protected $inNS;
	
	public function process()
	{
		$title = $this->request->getPath( $this->pathOffset );
		if( is_numeric( $title ) )
		{
			$this->be = $this->getBrain()->getEntryWithID( $title );
			$title = $this->request->getPath( $this->pathOffset + 1 );
		}
		else
			$this->be = $this->getBrain()->getEntry( $title );
			
		if( $this->be === false )
			$this->request->abortNotFound();
			
		//only items in allowed namespaces can be viewed
		$ns = explode( ':', $this->be->title, 2 );
		if( count( $ns ) > 1 )
		{
			if( array_search( $ns[0], $this->allowNS ) === false )
				$this->request->addNoticeAndAbort( Request::LEVEL_ERROR, REQUEST_TPLPRE . 'InvalidNote' );
			$this->inNS = true;
		}
		else
			$this->inNS = false;
		
		//if the title in the URL doesn't match the document title redirect -- this normalizes the URLs
		//and prevents mischief with odd URLs
		if( strcmp( dt_prettytag( $title ), dt_prettytag( $this->be->title ) ) != 0 )
			$this->request->redirectExtern( $this->request->getNoteLink( $this->be ) );
			
		//render now so we can get at the abstract
		$this->text = $this->getBrain()->getDesc( $this->be );
		list( $this->request->metaDescription ) = $this->be->getBriefDesc( 400, true/*text*/ );
	}
	
	public function getTitle()
	{
		$useTitle = split( ':', $be->title );
		$useTitle = join( ' - ', $useTitle );
		return $useTitle;
	}
	
	public function display()
	{
		if( $this->showTitle && !$this->inNS )
			print( '<h1>' . xml( $this->be->title ) . '</h1>' );
			
		print( $this->text );
	}
}

?>