Stripping CVS keywords
[project/views.git] / plugins / views_plugin_display_block.inc
1 <?php
2 /**
3 * @file
4 * Contains the block display plugin.
5 */
6
7 /**
8 * The plugin that handles a block.
9 *
10 * @ingroup views_display_plugins
11 */
12 class views_plugin_display_block extends views_plugin_display {
13 function option_definition() {
14 $options = parent::option_definition();
15
16 $options['block_description'] = array('default' => '', 'translatable' => TRUE);
17 $options['block_caching'] = array('default' => BLOCK_NO_CACHE);
18
19 return $options;
20 }
21
22 /**
23 * The default block handler doesn't support configurable items,
24 * but extended block handlers might be able to do interesting
25 * stuff with it.
26 */
27 function execute_hook_block($op = 'list', $delta = 0, $edit = array()) {
28 if ($op == 'list') {
29 $delta = $this->view->name . '-' . $this->display->id;
30 $desc = $this->get_option('block_description');
31
32 if (empty($desc)) {
33 $desc = t('@view: @display', array('@view' => $this->view->name, '@display' => $this->display->display_title));
34 }
35 return array(
36 $delta => array(
37 'info' => $desc,
38 'cache' => $this->get_cache_type()
39 )
40 );
41 }
42 }
43
44 /**
45 * The display block handler returns the structure necessary for a block.
46 */
47 function execute() {
48 // Prior to this being called, the $view should already be set to this
49 // display, and arguments should be set on the view.
50 $info['content'] = $this->view->render();
51 $info['subject'] = filter_xss_admin($this->view->get_title());
52 if (!empty($this->view->result) || $this->get_option('empty') || !empty($this->view->style_plugin->definition['even empty'])) {
53 return $info;
54 }
55 }
56
57 /**
58 * Provide the summary for page options in the views UI.
59 *
60 * This output is returned as an array.
61 */
62 function options_summary(&$categories, &$options) {
63 // It is very important to call the parent function here:
64 parent::options_summary($categories, $options);
65
66 $categories['block'] = array(
67 'title' => t('Block settings'),
68 );
69
70 $block_description = strip_tags($this->get_option('block_description'));
71 if (empty($block_description)) {
72 $block_description = t('None');
73 }
74
75 if (strlen($block_description) > 16) {
76 $block_description = substr($block_description, 0, 16) . '...';
77 }
78
79 $options['block_description'] = array(
80 'category' => 'block',
81 'title' => t('Admin'),
82 'value' => $block_description,
83 );
84
85 $cache_type = $this->get_option('block_caching');
86 if (empty($cache_type)) {
87 $cache_type = BLOCK_NO_CACHE;
88 }
89
90 $types = $this->block_caching_modes();
91 $options['block_caching'] = array(
92 'category' => 'block',
93 'title' => t('Caching'),
94 'value' => $types[$this->get_cache_type()],
95 );
96 }
97
98 /**
99 * Provide a list of core's block caching modes.
100 */
101 function block_caching_modes() {
102 return array(
103 BLOCK_NO_CACHE => t('Do not cache'),
104 BLOCK_CACHE_GLOBAL => t('Cache once for everything (global)'),
105 BLOCK_CACHE_PER_PAGE => t('Per page'),
106 BLOCK_CACHE_PER_ROLE => t('Per role'),
107 BLOCK_CACHE_PER_ROLE | BLOCK_CACHE_PER_PAGE => t('Per role per page'),
108 BLOCK_CACHE_PER_USER => t('Per user'),
109 BLOCK_CACHE_PER_USER | BLOCK_CACHE_PER_PAGE => t('Per user per page'),
110 );
111 }
112
113 /**
114 * Provide a single method to figure caching type, keeping a sensible default
115 * for when it's unset.
116 */
117 function get_cache_type() {
118 $cache_type = $this->get_option('block_caching');
119 if (empty($cache_type)) {
120 $cache_type = BLOCK_NO_CACHE;
121 }
122 return $cache_type;
123 }
124
125 /**
126 * Provide the default form for setting options.
127 */
128 function options_form(&$form, &$form_state) {
129 // It is very important to call the parent function here:
130 parent::options_form($form, $form_state);
131
132 switch ($form_state['section']) {
133 case 'block_description':
134 $form['#title'] .= t('Block admin description');
135 $form['block_description'] = array(
136 '#type' => 'textfield',
137 '#description' => t('This will appear as the name of this block in administer >> site building >> blocks.'),
138 '#default_value' => $this->get_option('block_description'),
139 );
140 break;
141 case 'block_caching':
142 $form['#title'] .= t('Block caching type');
143
144 $form['block_caching'] = array(
145 '#type' => 'radios',
146 '#description' => t("This sets the default status for Drupal's built-in block caching method; this requires that caching be turned on in block administration, and be careful because you have little control over when this cache is flushed."),
147 '#options' => $this->block_caching_modes(),
148 '#default_value' => $this->get_cache_type(),
149 );
150 break;
151 }
152 }
153
154 /**
155 * Perform any necessary changes to the form values prior to storage.
156 * There is no need for this function to actually store the data.
157 */
158 function options_submit(&$form, &$form_state) {
159 // It is very important to call the parent function here:
160 parent::options_submit($form, $form_state);
161 switch ($form_state['section']) {
162 case 'block_description':
163 $this->set_option('block_description', $form_state['values']['block_description']);
164 break;
165 case 'block_caching':
166 $this->set_option('block_caching', $form_state['values']['block_caching']);
167 $this->save_block_cache($form_state['view']->name.'-'.$form_state['display_id'], $form_state['values']['block_caching']);
168 break;
169 }
170 }
171
172 /**
173 * Block views use exposed widgets only if AJAX is set.
174 */
175 function uses_exposed() {
176 if ($this->use_ajax()) {
177 return parent::uses_exposed();
178 }
179
180 return FALSE;
181 }
182
183 /**
184 * Save the block cache setting in the blocks table if this block allready
185 * exists in the blocks table. Dirty fix untill http://drupal.org/node/235673 gets in.
186 */
187 function save_block_cache($delta, $cache_setting) {
188 if ($bid = db_fetch_object(db_query("SELECT bid, cache FROM {blocks} WHERE module = 'views' AND delta = '%s'", $delta))) {
189 db_query("UPDATE {blocks} set cache = %d WHERE module = 'views' AND delta = '%s'", $cache_setting, $delta);
190 }
191 }
192 }