-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeme.php
262 lines (236 loc) · 10.6 KB
/
theme.php
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
namespace Habari;
if ( !defined( 'HABARI_PATH' ) ) { die( 'No direct access' ); }
class Charcoal extends Theme
{
var $defaults = array(
'show_title_image' => false,
'home_label' => 'Blog',
'show_entry_paperclip' => true,
'show_page_paperclip' => false,
'show_powered' => true,
'display_login' => true,
'tags_in_multiple' => false,
'show_post_nav' => true,
'tags_count' => 40,
);
/**
* On theme activation, set the default options and activate a default menu
*/
public function action_theme_activated()
{
$opts = Options::get_group( __CLASS__ );
if ( empty( $opts ) ) {
Options::set_group( __CLASS__, $this->defaults );
}
$blocks = $this->get_blocks( 'nav', 0, $this );
if( count( $blocks ) == 0 ) {
$block = new Block( array(
'title' => _t( 'Charcoal Menu' ),
'type' => 'charcoal_menu',
) );
$block->add_to_area( 'nav' );
Session::notice( _t( 'Added Charcoal Menu block to Nav area.' ) );
}
}
/**
* Configuration form for the Charcoal theme
**/
public function action_theme_ui( $theme )
{
$ui = new FormUI( __CLASS__ );
// This is a fudge as I only need to add a little bit of styling to make things look nice.
$ui->append( FormControlStatic::create('style')->set_static('<style type="text/css">#charcoal .formcontrol { line-height: 2.2em; }</style>'));
$ui->append( FormControlCheckbox::create('show_title_image', __CLASS__.'__show_title_image')->set_helptext(_t( 'Check to show the title image, uncheck to display the title text.' ))->label(_t( 'Show Title Image:')) );
$ui->append( FormControlText::create('home_label', __CLASS__.'__home_label')->set_helptext(_t( 'Set to whatever you want your first tab text to be.' ))->label(_t( 'Home label:' ) ));
$ui->append( FormControlCheckbox::create('show_entry_paperclip', __CLASS__.'__show_entry_paperclip')->set_helptext(_t( 'Check to show the paperclip graphic in posts, uncheck to hide it.' ))->label( _t( 'Show Entry Paperclip:' ) ) );
$ui->append( FormControlCheckbox::create('show_page_paperclip', __CLASS__.'__show_page_paperclip')->set_helptext(_t( 'Check to show the paperclip graphic in pages, uncheck to hide it.' ))->label( _t( 'Show Page Paperclip:' ) ) );
$ui->append( FormControlCheckbox::create('show_powered', __CLASS__.'__show_powered')->set_helptext(_t( 'Check to show the "powered by Habari" graphic in the sidebar, uncheck to hide it.' ))->label( _t( 'Show Powered By:' ) ) );
$ui->append( FormControlCheckbox::create('display_login', __CLASS__.'__display_login')->set_helptext(_t( 'Check to show the Login/Logout link in the navigation bar, uncheck to hide it.' ))->label( _t( 'Display Login:' ) ) );
$ui->append( FormControlCheckbox::create('tags_in_multiple', __CLASS__.'__tags_in_multiple')->set_helptext(_t( 'Check to show the post tags in the multiple posts pages (search, tags, archives), uncheck to hide them.' ))->label(_t( 'Tags in Multiple Posts Page:') ) );
$ui->append( FormControlCheckbox::create('show_post_nav', __CLASS__.'__show_post_nav')->set_helptext(_t( 'Set to true to show single post navigation links, false to hide them.' ))->label( _t( 'Show Post Navigation:' ) ) );
$ui->append( FormControlText::create('tags_count', __CLASS__.'__tags_count')->set_helptext(_t( 'Set to the number of tags to display on the default "cloud".' ))->label(_t( 'Tag Cloud Count:' ) ) );
// Save
$ui->append( FormControlSubmit::create('save')->set_caption( _t( 'Save' ) ) );
$ui->set_settings( array('success_message' => _t( 'Options saved' )) );
$ui->out();
}
/**
* Execute on theme init to apply these filters to output
*/
public function action_init_theme()
{
// Apply Format::autop() to comment content...
Format::apply( 'autop', 'comment_content_out' );
// Truncate content excerpt at "more" or 56 characters...
Format::apply( 'autop', 'post_content_excerpt' );
Format::apply_with_hook_params( 'more', 'post_content_excerpt', '', 56, 1 );
// Add FormUI template placing the input before the label
$this->add_template( 'charcoal_text', dirname( __FILE__ ) . '/formcontrol_text.php' );
}
/**
* Add some variables to the template output
*/
public function add_template_vars()
{
parent::add_template_vars();
// Use theme options to set values that can be used directly in the templates
$opts = Options::get_group( __CLASS__ );
$this->assign( 'show_title_image', $opts['show_title_image'] );
$this->assign( 'home_label', $opts['home_label'] );
$this->assign( 'show_powered', $opts['show_powered'] );
$this->assign( 'display_login', $opts['display_login'] );
$this->assign( 'tags_in_multiple', $opts['tags_in_multiple'] );
$this->assign( 'post_class', 'post' . ( ! $opts['show_entry_paperclip'] ? ' alt' : '' ) );
$this->assign( 'page_class', 'post' . ( ! $opts['show_page_paperclip'] ? ' alt' : '' ) );
$this->assign( 'show_post_nav', $opts['show_post_nav'] );
$this->assign( 'loggedin', User::identify()->loggedin );
$locale = Options::get( 'locale' );
if ( $this->get_url($locale . '.css') ) {
$this->assign( 'localized_css', $locale . '.css' );
}
else {
$this->assign( 'localized_css', false );
}
if ( $opts['show_title_image'] ) {
if ( $this->get_url( 'images.' . $locale . '/title-image.png' ) ) {
$this->assign( 'title_image', 'images.' . $locale . '/title-image.png' );
}
else if ( $this->get_url( 'images/title-image.png' ) ) {
$this->assign( 'title_image', 'images/title-image.png' );
} else {
$this->assign( 'title_image', 'images/sample-title.png' );
}
}
if ( !$this->template_engine->assigned( 'pages' ) ) {
$this->assign( 'pages', Posts::get( 'page_list' ) );
}
$this->assign( 'post_id', ( isset( $this->post ) && $this->post->content_type == Post::type( 'page' ) ) ? $this->post->id : 0 );
if ( is_object($this->request) && $this->request->display_entries_by_tag ) {
if ( count( $this->include_tag ) && count( $this->exclude_tag ) == 0 ) {
$this->tags_msg = _t( 'Displaying posts tagged: %s', array( Format::tag_and_list( $this->include_tag ) ) );
}
else if ( count( $this->exclude_tag ) && count( $this->include_tag ) == 0 ) {
$this->tags_msg = _t( 'Displaying posts not tagged: %s', array( Format::tag_and_list( $this->exclude_tag ) ) );
}
else {
$this->tags_msg = _t( 'Displaying posts tagged: %s and not %s', array( Format::tag_and_list( $this->include_tag ), Format::tag_and_list( $this->exclude_tag ) ) );
}
}
}
/**
* Convert a post's tags array into a usable list of links
*
* @param array $array The tags array from a Post object
* @return string The HTML of the linked tags
*/
public function filter_post_tags_out( $array )
{
$fn = function($a) {return "<a href=\"" . URL::get("display_entries_by_tag", array( "tag" => $a->term) ) . "\" rel=\"tag\">" . $a->term_display . "</a>";};
$array = array_map( $fn, (array)$array );
$out = implode( ' ', $array );
return $out;
}
public function theme_post_comments_link( $theme, $post )
{
$c = $post->comments->approved->count;
return 0 == $c ? _t( 'No Comments' ) : sprintf( _n( '%1$d Comment', '%1$d Comments', $c ), $c );
}
public function filter_post_content_excerpt( $return )
{
return strip_tags( $return );
}
public function theme_search_prompt( $theme, $criteria, $has_results )
{
$out =array();
$keywords = explode( ' ', trim( $criteria ) );
foreach ( $keywords as $keyword ) {
$out[]= '<a href="' . Site::get_url( 'habari', true ) .'search?criteria=' . $keyword . '" title="' . _t( 'Search for ' ) . $keyword . '">' . $keyword . '</a>';
}
if ( sizeof( $keywords ) > 1 ) {
if ( $has_results ) {
return _t( 'Search results for \'%s\'', array( implode( ' ', $out ) ) );
exit;
}
return _t( 'No results found for your search \'%s\'', array( $criteria ) ) . '<br>'. _t( 'You can try searching for \'%s\'', array( implode( '\' or \'', $out ) ) );
}
else {
return _t( 'Search results for \'%s\'', array( $criteria ) );
exit;
}
return _t( 'No results found for your search \'%s\'', array( $criteria ) );
}
public function theme_search_form( $theme )
{
return $theme->fetch( 'searchform' );
}
/**
* Returns an unordered list of all used Tags
*/
public function theme_show_tags ( $theme )
{
$limit = Options::get( __CLASS__ . '__tags_count' );
$sql ="
SELECT t.term AS slug, t.term_display AS text, count(tp.object_id) as ttl
FROM {terms} t
INNER JOIN {object_terms} tp
ON t.id=tp.term_id
INNER JOIN {posts} p
ON p.id=tp.object_id AND p.status = ?
WHERE t.vocabulary_id = ? AND tp.object_type_id = ?
GROUP BY t.term, t.term_display
ORDER BY t.term_display
LIMIT {$limit}
";
$tags = DB::get_results( $sql, array( Post::status( 'published' ), Tags::vocabulary()->id, Vocabulary::object_type_id( 'post' ) ) );
foreach ( $tags as $index => $tag ) {
$tags[$index]->url = URL::get( 'display_entries_by_tag', array( 'tag' => $tag->slug ) );
}
$theme->taglist = $tags;
return $theme->fetch( 'taglist' );
}
/**
* Customize comment form layout. Needs thorough commenting.
*/
public function action_form_comment( $form ) {
$form->cf_commenter->caption = '<strong>' . _t( 'Name' ) . '</strong> <span class="required">' . ( Options::get( 'comments_require_id' ) == 1 ? _t( '(Required)' ) : '' ) . '</span>';
$form->cf_commenter->template = 'charcoal_text';
$form->cf_email->caption = '<strong>' . _t( 'Mail' ) . '</strong> ' . _t( '(will not be published' ) .' <span class="required">' . ( Options::get( 'comments_require_id' ) == 1 ? _t( '- Required)' ) : ')' ) . '</span>';
$form->cf_email->template = 'charcoal_text';
$form->cf_url->caption = '<strong>' . _t( 'Website' ) . '</strong>';
$form->cf_url->template = 'charcoal_text';
$form->cf_content->caption = '';
$form->cf_submit->caption = _t( 'Submit' );
}
/**
* Add a charcoal_menu block to the list of available blocks
*/
public function filter_block_list($block_list)
{
$block_list['charcoal_menu'] = _t('Charcoal Menu');
return $block_list;
}
/**
* Produce a menu for the Charcoal menu block from all of the available pages
*/
public function action_block_content_charcoal_menu($block, $theme)
{
$menus = array('home' => array(
'link' => Site::get_url( 'habari' ),
'title' => Options::get( 'title' ),
'caption' => Options::get( 'Charcoal__home_label', _t( 'Blog' ) ),
'cssclass' => $theme->request->display_home ? 'current_page_item' : '',
));
$pages = Posts::get( 'page_list' );
foreach($pages as $page) {
$menus[] = array(
'link' => $page->permalink,
'title' => $page->title,
'caption' => $page->title,
'cssclass' => (isset($theme->post) && $theme->post->id == $page->id) ? 'current_page_item' : '',
);
}
$block->menus = $menus;
}
}
?>