Preview.php
[Pman.Cms] / Preview.php
1 <?php
2
3
4 /**
5  * Possibly a base page for general CMS usage..
6  *
7  * -- it's Currently used by the Page Preview code
8  *
9  *
10  *  in theory a general base page would extend this..
11  *  
12  * 
13  */
14
15 class Pman_Cms_Preview extends Pman
16 {
17     // page header
18     var $title; 
19     
20     var $keywords;
21     
22     var $descriptions;
23     
24     // body
25     var $page; // the page that is being rendered...
26     
27     // blocks
28     var $block = array(); // the key/object blocks on the page.
29     
30     // home page 
31     var $defaultPage = false;
32     
33     
34     var $content = false; // cms_page object (content);
35     
36     function getAuth()
37     {
38         return false; // no access by default..
39         
40     }
41     
42     
43     function get($path= false)
44     {
45         die("implement me");
46         
47     }
48     function post($path=false) {
49         die("implement me");
50         
51     }
52     // now all the helper functions
53     
54     /**
55      * load page -- API has changed..
56      *
57      * $this->page= loadPage($name);
58      */
59     
60     function loadPage($name, $default = false)
61     {
62         // used to load sub elements. of a site..
63         if ($default == false && preg_match('/\*$/', $name)) {
64             $x = DB_DataObject::factory("cms_page");
65             $x->whereAdd("page_link like '" . $x->escape(substr($name,0,-1)) ."%'");
66             foreach($x->fetchAll('page_link') as $pl) {
67                 $this->loadPage($pl, false);
68             }
69             return;
70             
71         }
72         
73         
74         if ($default != false) {
75             
76             $x = DB_DataObject::factory("cms_page");
77             $x->autoJoin();
78             $x->is_element = 0;
79             $x->is_draft = 0;
80             if (!$x->get("page_link", $default)) {
81                 die("default page '$default' does not exist");
82             }
83             // load the home page blocks..
84             
85             $blocks = $x->children(array(
86                 'type'=>'named_blocks'
87             ));
88             foreach($blocks as $k=>$b) {
89                 $this->block[$k] = $b;
90             }
91             
92             
93             $this->defaultPage = $x;
94             
95         }
96         
97         
98         
99         if (empty($name)) {
100             $this->page = $this->defaultPage;
101             
102         } else {
103             $x = DB_DataObject::factory("cms_page");
104             $x->autoJoin();
105             $x->is_element = 0;
106             $x->is_draft = 0;
107             if (!$x->get("page_link", $name)) {
108                 $this->herr(404);
109             }
110             $blocks = $x->children(array(
111                 'type'=>'named_blocks'
112             ));
113             foreach($blocks as $k=>$b) {
114                 $this->block[$k] = $b;
115             }
116             $this->page = $x;
117         }
118         
119         // load all the blocks..
120         $this->assignMeta();
121         
122         // menus????
123         
124          
125     }
126       
127     function assignMeta()
128     {   
129         foreach(array($this->content,$this->defaultPage) as $argsItem) {
130             if(empty($this->title) && !empty($argsItem->content->title)){
131                 $this->title = ' - ' . $argsItem->title;
132             }
133             if(empty($this->keywords) && !empty($argsItem->keywords)){
134                 $this->keywords =   $argsItem->keywords;
135             }
136             if(empty($this->descriptions) && !empty($argsItem->descriptions)){
137                 $this->descriptions =   $argsItem->descriptions;
138             }
139         }
140     }
141     
142     
143     function herr( $error_name, $args = array(), $redirect=false )
144     {
145         
146         if($redirect !== false){
147             $args['errors'][$error_name] = 1;
148             return HTML_FlexyFramework::run($redirect, $args);
149         }
150         
151         $this->errors[$error_name] = 1;
152         
153         $this->elements =  HTML_Template_Flexy_Factory::fromArray($_REQUEST, $this->elements);
154         $this->addEvent('FRONTEND:'.$error_name,  (empty($args['error_on'])) ? false : $args['error_on'], $error_name);
155         
156         return true;
157     }
158     
159     function err($n, $msg = false)
160     {
161         header("HTTP/1.0 $n Not Found");
162         $this->template = 'error.html';
163         $this->msg = $msg;
164         $this->output();
165         // should we log the referer!!
166         exit;
167     }
168     function errmsg($str)
169     {
170         $this->errors[$str] = 1;
171         return $this->get('');
172     }
173     
174     function redirect($loc)
175     {
176         header('Location: '. $this->baseURL . $loc);
177       
178         exit;
179     }
180     
181     
182     function ellipsis($input, $length) {
183          
184       
185         //no need to trim, already shorter than trim length
186         if (strlen($input) <= $length) {
187             return $input;
188         }
189       
190         //find last space within length
191         $last_space = strrpos(substr($input, 0, $length), ' ');
192         $trimmed_text = substr($input, 0, $last_space);
193       
194         //add ellipses (...)
195          
196          $trimmed_text .= '...';
197         
198       
199         return $trimmed_text;
200             
201         
202     }
203     
204     function eq($a,$b){
205         return $a == $b;
206     }
207     
208     function checkIsAdmin()
209     {
210         if(!$this->authUser){
211             return false;
212         }
213         $x = DB_DataObject::factory('Companies');
214         if($x->get($this->authUser->company_id)){
215             if($x->comptype == 'OWNER'){
216                 return true;
217             }
218             return false;
219         }
220         return false;
221     }
222     function templateClass() {
223         return preg_replace('/[^a-z]+/', '-', preg_replace('/\.html$/', '', strtolower($this->template)));
224         
225     }
226 }
227
228
229