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 require_once 'Pman.php'; /// needed as we might not be included from pman..
15
16 class Pman_Cms_Preview extends Pman
17 {
18     // page header
19     var $title; 
20     
21     var $keywords;
22     
23     var $descriptions;
24     
25     // body
26     var $page; // the page that is being rendered...
27     
28     // blocks
29     var $block = array(); // the key/object blocks on the page.
30     
31     // home page 
32     var $defaultPage = false;
33     
34     
35     var $content = false; // cms_page object (content);
36     
37     function getAuth()
38     {
39         return false; // no access by default..
40         
41     }
42     
43     
44     function get($path= false)
45     {
46         die("implement me");
47         
48     }
49     function post($path=false) {
50         die("implement me");
51         
52     }
53     // now all the helper functions
54     
55     /**
56      * load page -- API has changed..
57      *
58      * $this->page= loadPage($name);
59      */
60     
61     function loadPage($prefix, $name, $default = false)
62     {
63         //var_dump($name, $default);
64         
65         
66         $oo = HTML_FlexyFramework::get()->Pman_Cms;
67         $template_prefix = isset($oo['template_prefix']) ? $oo['template_prefix'] : '';
68          
69         // used to load sub elements. of a site..
70         if ($default === false && preg_match('/\*$/', $name)) {
71             $x = DB_DataObject::factory("cms_page");
72             $x->whereAdd("page_link like '" . $x->escape(substr($name,0,-1)) ."%'");
73             foreach($x->fetchAll('page_link') as $pl) {
74                 $this->loadPage($pl, false);
75             }
76             return;
77             
78         }
79         
80         
81         if ($default != false) {
82             
83             $x = DB_DataObject::factory("cms_page");
84             $x->autoJoin();
85             $x->is_element = 0;
86             $x->is_draft = 0;
87             $x->language =  'en'; //$this->bootLoader->locale;
88             
89             if (!$x->get("page_link", $prefix .'/'. $default)) {
90                 die("default page '$default' does not exist");
91             }
92             // load the home page blocks..
93             
94             $blocks = $x->children(array(
95                 'type'=>'named_blocks',
96                 'language' => $this->bootLoader->locale,
97             ));
98             foreach($blocks as $k=>$b) {
99                 $this->block[$k] = $b;
100             }
101             
102             $this->defaultPage = $x->inLanguage($this->bootLoader->locale);
103             $this->defaultPage_en = $x;
104             
105             if ($this->defaultPage_en->id != $this->defaultPage->id) {
106                 $blocks =$this->defaultPage->children(array(
107                     'type'=>'named_blocks',
108                     'language' => $this->bootLoader->locale
109                 ));
110                 foreach($blocks as $k=>$b) {
111                     $this->block[$k] = $b;
112                 }
113                 
114             }
115              
116             
117             
118             
119             
120             
121             
122             
123         }
124         
125         
126         
127         if (empty($name)) {
128             $this->page = $this->defaultPage;
129             $this->page_en = $this->defaultPage_en;
130         } else {
131             $x = DB_DataObject::factory("cms_page");
132             $x->autoJoin();
133             $x->is_element = 0;
134             $x->is_draft = 0;
135             $x->language =  'en';
136             if (!$x->get("page_link", $prefix .'/'. $name)) {
137                 print_r('run');exit;
138                 $this->err(404, "$name not found");
139             }
140             
141             $blocks = $x->children(array(
142                 'type'=>'named_blocks',
143                 'language' => $this->bootLoader->locale,
144             ));
145             foreach($blocks as $k=>$b) {
146                 $this->block[$k] = $b;
147             }
148             $this->page_en = $x;
149             $this->page = $x->inLanguage($this->bootLoader->locale);
150             
151             if ($this->page_en->id != $this->page->id) {
152             
153                 $blocks = $this->page->children(array(
154                     'type'=>'named_blocks',
155                     'language' => $this->bootLoader->locale
156                 ));
157                 foreach($blocks as $k=>$b) {
158                     $this->block[$k] = $b;
159                 }
160                  
161                 
162             }
163              
164             
165         }
166         
167         // load all the blocks..
168         $this->assignMeta();
169         $this->template = $this->page_en->template()->template;
170         //var_dump($this->template);
171         
172         // menus????
173         
174          
175     }
176       
177     function assignMeta()
178     {   
179         foreach(array($this->page ,$this->defaultPage) as $argsItem) {
180             if(empty($this->title) && !empty($argsItem->content->title)){
181                 $this->title = ' - ' . $argsItem->title;
182             }
183             if(empty($this->keywords) && !empty($argsItem->keywords)){
184                 $this->keywords =   $argsItem->keywords;
185             }
186             if(empty($this->descriptions) && !empty($argsItem->descriptions)){
187                 $this->descriptions =   $argsItem->descriptions;
188             }
189         }
190     }
191     
192     /*
193     function herr( $error_name, $args = array(), $redirect=false )
194     {
195         
196         if($redirect !== false){
197             $args['errors'][$error_name] = 1;
198             return HTML_FlexyFramework::run($redirect, $args);
199         }
200         
201         $this->errors[$error_name] = 1;
202         
203         $this->elements =  HTML_Template_Flexy_Factory::fromArray($_REQUEST, $this->elements);
204         $this->addEvent('FRONTEND:'.$error_name,  (empty($args['error_on'])) ? false : $args['error_on'], $error_name);
205         
206         return true;
207     }
208     */
209     function err($n, $msg = false)
210     {
211         header("HTTP/1.0 $n Not Found");
212         $this->template = 'error.html';
213         $this->msg = $msg;
214         $this->output();
215         // should we log the referer!!
216         exit;
217     }
218     function errmsg($str)
219     {
220         $this->errors[$str] = 1;
221         return $this->get('');
222     }
223     
224     function redirect($loc)
225     {
226         header('Location: '. $this->baseURL . $loc);
227       
228         exit;
229     }
230     
231     
232     function ellipsis($input, $length) {
233          
234       
235         //no need to trim, already shorter than trim length
236         if (strlen($input) <= $length) {
237             return $input;
238         }
239       
240         //find last space within length
241         $last_space = strrpos(substr($input, 0, $length), ' ');
242         $trimmed_text = substr($input, 0, $last_space);
243       
244         //add ellipses (...)
245          
246          $trimmed_text .= '...';
247         
248       
249         return $trimmed_text;
250           
251     }
252     
253     function eq($a,$b){
254         return $a == $b;
255     }
256     
257     function checkIsAdmin()
258     {
259         if(!$this->authUser){
260             return false;
261         }
262         $x = DB_DataObject::factory('Companies');
263         if($x->get($this->authUser->company_id)){
264             if($x->comptype == 'OWNER'){
265                 return true;
266             }
267             return false;
268         }
269         return false;
270     }
271     function templateClass() {
272         return preg_replace('/[^a-z]+/', '-', preg_replace('/\.html$/', '', strtolower($this->template)));
273         
274     }
275 }
276
277
278