00001 <?php
00012 class views_plugin_display_block extends views_plugin_display {
00013 function option_definition() {
00014 $options = parent::option_definition();
00015
00016 $options['block_description'] = array('default' => '', 'translatable' => TRUE);
00017 $options['block_caching'] = array('default' => DRUPAL_NO_CACHE);
00018
00019 return $options;
00020 }
00021
00027 function execute_hook_block_list($delta = 0, $edit = array()) {
00028 $delta = $this->view->name . '-' . $this->display->id;
00029 $desc = $this->get_option('block_description');
00030
00031 if (empty($desc)) {
00032 if ($this->display->display_title == $this->definition['title']) {
00033 $desc = t('View: @view', array('@view' => $this->view->get_human_name()));
00034 }
00035 else {
00036 $desc = t('View: @view: @display', array('@view' => $this->view->get_human_name(), '@display' => $this->display->display_title));
00037 }
00038 }
00039 return array(
00040 $delta => array(
00041 'info' => $desc,
00042 'cache' => $this->get_cache_type()
00043 ),
00044 );
00045 }
00046
00050 function execute() {
00051
00052
00053 $info['content'] = $this->view->render();
00054 $info['subject'] = filter_xss_admin($this->view->get_title());
00055 if (!empty($this->view->result) || $this->get_option('empty') || !empty($this->view->style_plugin->definition['even empty'])) {
00056 return $info;
00057 }
00058 }
00059
00065 function options_summary(&$categories, &$options) {
00066
00067 parent::options_summary($categories, $options);
00068
00069 $categories['block'] = array(
00070 'title' => t('Block settings'),
00071 'column' => 'second',
00072 'build' => array(
00073 '#weight' => -10,
00074 ),
00075 );
00076
00077 $block_description = strip_tags($this->get_option('block_description'));
00078 if (empty($block_description)) {
00079 $block_description = t('None');
00080 }
00081
00082 $options['block_description'] = array(
00083 'category' => 'block',
00084 'title' => t('Block name'),
00085 'value' => views_ui_truncate($block_description, 24),
00086 );
00087
00088 $cache_type = $this->get_option('block_caching');
00089 if (empty($cache_type)) {
00090 $cache_type = DRUPAL_NO_CACHE;
00091 }
00092
00093 $types = $this->block_caching_modes();
00094 $options['block_caching'] = array(
00095 'category' => 'other',
00096 'title' => t('Block caching'),
00097 'value' => $types[$this->get_cache_type()],
00098 );
00099 }
00100
00104 function block_caching_modes() {
00105 return array(
00106 DRUPAL_NO_CACHE => t('Do not cache'),
00107 DRUPAL_CACHE_GLOBAL => t('Cache once for everything (global)'),
00108 DRUPAL_CACHE_PER_PAGE => t('Per page'),
00109 DRUPAL_CACHE_PER_ROLE => t('Per role'),
00110 DRUPAL_CACHE_PER_ROLE | DRUPAL_CACHE_PER_PAGE => t('Per role per page'),
00111 DRUPAL_CACHE_PER_USER => t('Per user'),
00112 DRUPAL_CACHE_PER_USER | DRUPAL_CACHE_PER_PAGE => t('Per user per page'),
00113 );
00114 }
00115
00120 function get_cache_type() {
00121 $cache_type = $this->get_option('block_caching');
00122 if (empty($cache_type)) {
00123 $cache_type = DRUPAL_NO_CACHE;
00124 }
00125 return $cache_type;
00126 }
00127
00131 function options_form(&$form, &$form_state) {
00132 parent::options_form($form, $form_state);
00133
00134 parent::options_form($form, $form_state);
00135
00136 switch ($form_state['section']) {
00137 case 'block_description':
00138 $form['#title'] .= t('Block admin description');
00139 $form['block_description'] = array(
00140 '#type' => 'textfield',
00141 '#description' => t('This will appear as the name of this block in administer >> structure >> blocks.'),
00142 '#default_value' => $this->get_option('block_description'),
00143 );
00144 break;
00145 case 'block_caching':
00146 $form['#title'] .= t('Block caching type');
00147
00148 $form['block_caching'] = array(
00149 '#type' => 'radios',
00150 '#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."),
00151 '#options' => $this->block_caching_modes(),
00152 '#default_value' => $this->get_cache_type(),
00153 );
00154 break;
00155 }
00156 }
00157
00162 function options_submit(&$form, &$form_state) {
00163
00164 parent::options_submit($form, $form_state);
00165 switch ($form_state['section']) {
00166 case 'display_id':
00167 $this->update_block_bid($form_state['view']->name, $this->display->id, $this->display->new_id);
00168 break;
00169 case 'block_description':
00170 $this->set_option('block_description', $form_state['values']['block_description']);
00171 break;
00172 case 'block_caching':
00173 $this->set_option('block_caching', $form_state['values']['block_caching']);
00174 $this->save_block_cache($form_state['view']->name . '-'. $form_state['display_id'], $form_state['values']['block_caching']);
00175 break;
00176 }
00177 }
00178
00182 function uses_exposed() {
00183 if ($this->use_ajax()) {
00184 return parent::uses_exposed();
00185 }
00186
00187 return FALSE;
00188 }
00189
00193 function update_block_bid($name, $old_delta, $delta) {
00194 $old_hashes = $hashes = variable_get('views_block_hashes', array());
00195
00196 $old_delta = $name . '-' . $old_delta;
00197 $delta = $name . '-' . $delta;
00198 if (strlen($old_delta) >= 32) {
00199 $old_delta = md5($old_delta);
00200 unset($hashes[$old_delta]);
00201 }
00202 if (strlen($delta) >= 32) {
00203 $md5_delta = md5($delta);
00204 $hashes[$md5_delta] = $delta;
00205 $delta = $md5_delta;
00206 }
00207 db_update('block')
00208 ->fields(array('delta' => $delta))
00209 ->condition('delta', $old_delta)
00210 ->execute();
00211
00212
00213 if ($hashes != $old_hashes) {
00214 variable_set('views_block_hashes', $hashes);
00215 }
00216 }
00221 function save_block_cache($delta, $cache_setting) {
00222 if (strlen($delta) >= 32) {
00223 $delta = md5($delta);
00224 }
00225 if (db_table_exists('block') && $bid = db_query("SELECT bid FROM {block} WHERE module = 'views' AND delta = :delta", array(
00226 ':delta' => $delta))->fetchField()) {
00227 db_update('block')
00228 ->fields(array(
00229 'cache' => $cache_setting,
00230 ))
00231 ->condition('module','views')
00232 ->condition('delta', $delta)
00233 ->execute();
00234 }
00235 }
00236 }