Dotclear

source: inc/admin/class.dc.admincontext.php @ 1128:9e868a6c21c3

Revision 1128:9e868a6c21c3, 6.0 KB checked in by JcDenis, 12 years ago (diff)

Yet another more flexible method to create template environment and context.

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2011 Olivier Meunier & Association Dotclear
7# Licensed under the GPL version 2.0 license.
8# See LICENSE file or
9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10#
11# -- END LICENSE BLOCK -----------------------------------------
12if (!defined('DC_RC_PATH')) { return; }
13
14/**
15 * @ingroup DC_CORE
16 * @brief Template extension for admin context.
17 *
18 * This extends template environment with tools required in admin context.
19 */
20class dcAdminContext extends dcContext
21{
22     public function __construct($core)
23     {
24          parent::__construct($core);
25         
26          $this->protected_globals = array_merge($this->protected_globals,array(
27               'page_title'   => array(),
28               'page_global'  => false,
29               
30               'admin_url'    => DC_ADMIN_URL,
31               'theme_url'    => DC_ADMIN_URL.'index.php?tf=',
32               'plugin_url'   => DC_ADMIN_URL.'index.php?pf=',
33          ));
34     }
35     
36     /**
37      * Returns a list of global variables to add to the existing list.
38      *
39      * This merges overloaded variables with defined variables.
40      *
41      * @return array An array of global variables
42      */
43     public function getGlobals()
44     {
45          $this->getBlogs();
46          $this->getCurrentBlog();
47          $this->getCurrentUser();
48          $this->getMenus();
49         
50          # Additional globals
51          $p = path::info($_SERVER['REQUEST_URI']);
52          $this->protected_globals['current_page'] = $p['base'];
53          $this->protected_globals['blog_count'] = $this->core->auth->blog_count;
54          $this->protected_globals['rtl'] = l10n::getTextDirection(
55               $this->protected_globals['current_user']['lang']) == 'rtl';
56          $this->protected_globals['session'] = array(
57               'id' => session_id(),
58               'uid' => isset($_SESSION['sess_browser_uid']) ? $_SESSION['sess_browser_uid'] : '',
59               'nonce' => $this->core->getNonce()
60          );
61         
62          # Keep protected globals safe
63          return array_merge($this->globals,$this->protected_globals);
64     }
65     
66     /**
67      * Fill the page title.
68      *
69      * $title can be:
70      * - a string for page title part or
71      * - TRUE to add blog name at the begining of title or
72      * - NULL to empty/reset title
73      *
74      * @param mixed $title A title part
75      * @param boolean $url Link of the title part
76      * @return object self
77      */
78     public function fillPageTitle($title,$url='')
79     {
80          if (is_bool($title)) {
81               $this->protected_globals['page_global'] = $title;
82          }
83          elseif (null === $title) {
84               $this->protected_globals['page_global'] = false;
85               $this->protected_globals['page_title'] = array();
86          }
87          else {
88               $this->protected_globals['page_title'][] = array(
89                    'title' => $title,
90                    'link' => $url
91               );
92          }
93          return $this;
94     }
95     
96     /**
97      * Check if a page title is set
98      */
99     public function hasPageTitle()
100     {
101          return !empty($this->protected_globals['page_title']);
102     }
103     
104     /**
105      * Get list of blogs
106      */
107     protected function getBlogs()
108     {
109          $blog_id = '';
110         
111          # Blogs list
112          $blogs = array();
113          if ($this->core->auth->blog_count > 1 && $this->core->auth->blog_count < 20) {
114               $blog_id = $this->core->blog->id;
115               $rs_blogs = $this->core->getBlogs(array('order'=>'LOWER(blog_name)','limit'=>20));
116               while ($rs_blogs->fetch()) {
117                    $blogs[$rs_blogs->blog_id] = $rs_blogs->blog_name.' - '.$rs_blogs->blog_url;
118                    $this->protected_globals['blogs'][$rs_blogs->blog_id] = array(
119                         'id'      => $rs_blogs->blog_id,
120                         'name'    => $rs_blogs->blog_name,
121                         'desc'    => $rs_blogs->blog_desc,
122                         'url'     => $rs_blogs->blog_url,
123                         'creadt'  => $rs_blogs->blog_creadt,
124                         'upddt'   => $rs_blogs->blog_upddt
125                    );
126               }
127          }
128         
129          # Switch blog form
130          $form = new dcForm($this->core,'switchblog_menu','index.php');
131          $form
132               ->addField(
133                    new dcFieldCombo('switchblog',$blog_id,$blogs,array(
134                    'label' => __('Blogs:'))))
135               ->addField(
136                    new dcFieldSubmit('switchblog_submit',__('ok'),array(
137                    'action' => 'switchblog')))
138               ->setup();
139     }
140     
141     /**
142      * Get current blog information
143      */
144     protected function getCurrentBlog()
145     {
146          $this->protected_globals['current_blog'] = $this->core->auth->blog_count ?
147               array(
148                    'id'      => $this->core->blog->id,
149                    'name'    => $this->core->blog->name,
150                    'desc'    => $this->core->blog->desc,
151                    'url'     => $this->core->blog->url,
152                    'host'    => $this->core->blog->host,
153                    'creadt'  => $this->core->blog->creadt,
154                    'upddt'   => $this->core->blog->upddt
155               ) : array(
156                    'id'      => '',
157                    'name'    => '',
158                    'desc'    => '',
159                    'url'     => '',
160                    'host'    => '',
161                    'creadt'  => '',
162                    'upddt'   => ''
163               );
164     }
165     
166     /**
167      * Get current user information
168      */
169     protected function getCurrentUser()
170     {
171          $infos = array(
172               'pwd','name','firstname','displayname',
173               'email','url','default_blog','lang','tz',
174               'post_status','creadt','upddt','cn'
175          );
176         
177          $user = array(
178               'id' => '',
179               'super' => false,
180               'lang' => 'en',
181               'options' => $this->core->userDefaults(),
182               'prefs' => array(),
183               'rights' => array(
184                    'media' => false
185               )
186          );
187         
188          foreach($infos as $i) {
189               $user[$i] = '';
190          }
191         
192          if ($this->core->auth->userID()) {
193         
194               $user = array(
195                    'id' => $this->core->auth->userID(),
196                    'super' => $this->core->auth->isSuperAdmin(),
197                    'options' => $this->core->auth->getOptions(),
198                    'rights' => array(
199                         'media' => $this->core->auth->check('media,media_admin',$this->core->blog->id)
200                    )
201               );
202               
203               foreach($infos as $i) {
204                    $user[$i] = $this->core->auth->getInfo('user_'.$i);
205               }
206               
207               foreach($this->core->auth->user_prefs->dumpWorkspaces() as $ws => $prefs) {
208                    $user['prefs'][$ws] = $prefs->dumpPrefs();
209               }
210          }
211         
212          $this->protected_globals['current_user'] = $user;
213     }
214     
215     /**
216      * Get sidebar menus
217      */
218     protected function getMenus()
219     {
220          global $_menu;
221         
222          $this->protected_globals['menus'] = array();
223         
224          if (!isset($_menu)) {
225               return;
226          }
227         
228          foreach($_menu as $m) {
229               $this->protected_globals['menus'][] = array(
230                    'id'           => $m->getID(),
231                    'title'        => $m->getTitle(),
232                    'separator'    => $m->getSeparator(),
233                    'items'        => $m->getItems()
234               );
235          }
236     }
237}
238?>
Note: See TracBrowser for help on using the repository browser.

Sites map