00001 <?php
00002
00006 class views_plugin_exposed_form extends views_plugin {
00007
00016 function init(&$view, &$display, $options = array()) {
00017 $this->view = &$view;
00018 $this->display = &$display;
00019
00020 $this->unpack_options($this->options, $options);
00021 }
00022
00023 function option_definition() {
00024 $options = parent::option_definition();
00025 $options['submit_button'] = array('default' => t('Apply'), 'translatable' => TRUE);
00026 $options['reset_button'] = array('default' => FALSE, 'bool' => TRUE);
00027 $options['reset_button_label'] = array('default' => 'Reset', 'translatable' => TRUE);
00028 $options['exposed_sorts_label'] = array('default' => 'Sort by', 'translatable' => TRUE);
00029 $options['sort_asc_label'] = array('default' => 'Asc', 'translatable' => TRUE);
00030 $options['sort_desc_label'] = array('default' => 'Desc', 'translatable' => TRUE);
00031 $options['autosubmit'] = array('default' => FALSE);
00032 $options['autosubmit_hide'] = array('default' => TRUE);
00033 return $options;
00034 }
00035
00036 function options_form(&$form, &$form_state) {
00037 parent::options_form($form, $form_state);
00038 $form['submit_button'] = array(
00039 '#type' => 'textfield',
00040 '#title' => t('Submit button text'),
00041 '#description' => t('Text to display in the submit button of the exposed form.'),
00042 '#default_value' => $this->options['submit_button'],
00043 '#required' => TRUE,
00044 );
00045
00046 $form['reset_button'] = array (
00047 '#type' => 'checkbox',
00048 '#title' => t('Include reset button'),
00049 '#description' => t('If checked the exposed form will provide a button to reset all the applied exposed filters'),
00050 '#default_value' => $this->options['reset_button'],
00051 );
00052
00053 $form['reset_button_label'] = array(
00054 '#type' => 'textfield',
00055 '#title' => t('Reset button label'),
00056 '#description' => t('Text to display in the reset button of the exposed form.'),
00057 '#default_value' => $this->options['reset_button_label'],
00058 '#required' => TRUE,
00059 '#dependency' => array(
00060 'edit-exposed-form-options-reset-button' => array(1)
00061 ),
00062 );
00063
00064 $form['exposed_sorts_label'] = array(
00065 '#type' => 'textfield',
00066 '#title' => t('Exposed sorts label'),
00067 '#description' => t('Text to display as the label of the exposed sort select box.'),
00068 '#default_value' => $this->options['exposed_sorts_label'],
00069 '#required' => TRUE,
00070 );
00071
00072 $form['sort_asc_label'] = array(
00073 '#type' => 'textfield',
00074 '#title' => t('Ascending'),
00075 '#description' => t('Text to use when exposed sort is ordered ascending.'),
00076 '#default_value' => $this->options['sort_asc_label'],
00077 '#required' => TRUE,
00078 );
00079
00080 $form['sort_desc_label'] = array(
00081 '#type' => 'textfield',
00082 '#title' => t('Descending'),
00083 '#description' => t('Text to use when exposed sort is ordered descending.'),
00084 '#default_value' => $this->options['sort_desc_label'],
00085 '#required' => TRUE,
00086 );
00087
00088 $form['autosubmit'] = array(
00089 '#type' => 'checkbox',
00090 '#title' => t('Autosubmit'),
00091 '#description' => t('Automatically submit the form once an element is changed.'),
00092 '#default_value' => $this->options['autosubmit'],
00093 );
00094
00095 $form['autosubmit_hide'] = array(
00096 '#type' => 'checkbox',
00097 '#title' => t('Hide submit button'),
00098 '#description' => t('Hide submit button if javascript is enabled.'),
00099 '#default_value' => $this->options['autosubmit_hide'],
00100 '#dependency' => array(
00101 'edit-exposed-form-options-autosubmit' => array(1),
00102 ),
00103 );
00104 }
00105
00113 function render_exposed_form($block = FALSE) {
00114
00115 $form_state = array(
00116 'view' => &$this->view,
00117 'display' => &$this->display,
00118 'method' => 'get',
00119 'rerender' => TRUE,
00120 'no_redirect' => TRUE,
00121 'always_process' => TRUE,
00122 );
00123
00124
00125
00126
00127 if (!$this->view->display_handler->displays_exposed() || (!$block && $this->view->display_handler->get_option('exposed_block'))) {
00128 unset($form_state['rerender']);
00129 }
00130
00131 if (!empty($this->ajax)) {
00132 $form_state['ajax'] = TRUE;
00133 }
00134
00135 $form_state['exposed_form_plugin'] = $this;
00136 $form = drupal_build_form('views_exposed_form', $form_state);
00137 $output = drupal_render($form);
00138
00139 if (!$this->view->display_handler->displays_exposed() || (!$block && $this->view->display_handler->get_option('exposed_block'))) {
00140 return "";
00141 }
00142 else {
00143 return $output;
00144 }
00145 }
00146
00147 function query() {
00148 $view = $this->view;
00149 $exposed_data = isset($view->exposed_data) ? $view->exposed_data : array();
00150 $sort_by = isset($exposed_data['sort_by']) ? $exposed_data['sort_by'] : NULL;
00151 if (!empty($sort_by)) {
00152
00153
00154 if (isset($view->sort[$sort_by])) {
00155 $view->query->orderby = array();
00156 foreach ($view->sort as $key => $sort) {
00157 if (!$sort->is_exposed()) {
00158 $sort->query();
00159 }
00160 else if ($key == $sort_by) {
00161 if (isset($exposed_data['sort_order']) && in_array($exposed_data['sort_order'], array('ASC', 'DESC'))) {
00162 $sort->options['order'] = $exposed_data['sort_order'];
00163 }
00164 $sort->set_relationship();
00165 $sort->query();
00166 }
00167 }
00168 }
00169 }
00170 }
00171
00172 function pre_render($values) { }
00173
00174 function post_render(&$output) { }
00175
00176 function pre_execute() { }
00177
00178 function post_execute() { }
00179
00180 function exposed_form_alter(&$form, &$form_state) {
00181 if (!empty($this->options['reset_button'])) {
00182 $form['reset'] = array(
00183 '#value' => $this->options['reset_button_label'],
00184 '#type' => 'submit',
00185 );
00186 }
00187
00188 $form['submit']['#value'] = $this->options['submit_button'];
00189
00190 $exposed_sorts = array();
00191 foreach ($this->view->sort as $id => $handler) {
00192 if ($handler->can_expose() && $handler->is_exposed()) {
00193 $exposed_sorts[$id] = check_plain($handler->options['expose']['label']);
00194 }
00195 }
00196
00197 if (count($exposed_sorts)) {
00198 $form['sort_by'] = array(
00199 '#type' => 'select',
00200 '#options' => $exposed_sorts,
00201 '#title' => $this->options['exposed_sorts_label'],
00202 );
00203 $sort_order = array(
00204 'ASC' => $this->options['sort_asc_label'],
00205 'DESC' => $this->options['sort_desc_label'],
00206 );
00207 if (isset($form_state['input']['sort_by']) && isset($this->view->sort[$form_state['input']['sort_by']])) {
00208 $default_sort_order = $this->view->sort[$form_state['input']['sort_by']]->options['order'];
00209 } else {
00210 $first_sort = reset($this->view->sort);
00211 $default_sort_order = $first_sort->options['order'];
00212 }
00213
00214 if (!isset($form_state['input']['sort_by'])) {
00215 $keys = array_keys($exposed_sorts);
00216 $form_state['input']['sort_by'] = array_shift($keys);
00217 }
00218
00219 $form['sort_order'] = array(
00220 '#type' => 'select',
00221 '#options' => $sort_order,
00222 '#title' => t('Order'),
00223 '#default_value' => $default_sort_order,
00224 );
00225 $form['submit']['#weight'] = 10;
00226 if (isset($form['reset'])) {
00227 $form['reset']['#weight'] = 10;
00228 }
00229 }
00230
00231 $pager = $this->view->display_handler->get_plugin('pager');
00232 if ($pager) {
00233 $pager->exposed_form_alter($form, $form_state);
00234 $form_state['pager_plugin'] = $pager;
00235 }
00236
00237
00238
00239 if (!empty($this->options['autosubmit'])) {
00240 $form = array_merge_recursive($form, array('#attributes' => array('class' => array('ctools-auto-submit-full-form'))));
00241 $form['submit']['#attributes']['class'][] = 'ctools-use-ajax';
00242 $form['submit']['#attributes']['class'][] = 'ctools-auto-submit-click';
00243 $form['#attached']['js'][] = drupal_get_path('module', 'ctools') . '/js/auto-submit.js';
00244
00245 if (!empty($this->options['autosubmit_hide'])) {
00246 $form['submit']['#attributes']['class'][] = 'js-hide';
00247 }
00248 }
00249 }
00250
00251 function exposed_form_validate(&$form, &$form_state) {
00252 if (isset($form_state['pager_plugin'])) {
00253 $form_state['pager_plugin']->exposed_form_validate($form, $form_state);
00254 }
00255 }
00256
00268 function exposed_form_submit(&$form, &$form_state, &$exclude) {
00269 if (!empty($form_state['values']['op']) && $form_state['values']['op'] == $this->options['reset_button_label']) {
00270 $this->reset_form($form, $form_state);
00271 }
00272 if (isset($form_state['pager_plugin'])) {
00273 $form_state['pager_plugin']->exposed_form_submit($form, $form_state, $exclude);
00274 $exclude[] = 'pager_plugin';
00275 }
00276 }
00277
00278 function reset_form(&$form, &$form_state) {
00279
00280
00281
00282
00283
00284
00285 $display_id = ($this->view->display_handler->is_defaulted('filters')) ? 'default' : $this->view->current_display;
00286
00287 if (isset($_SESSION['views'][$this->view->name][$display_id])) {
00288 unset($_SESSION['views'][$this->view->name][$display_id]);
00289 }
00290
00291
00292 if (empty($this->view->live_preview)) {
00293 $form_state['no_redirect'] = FALSE;
00294 }
00295 else {
00296 $form_state['rebuild'] = TRUE;
00297 $this->view->exposed_data = array();
00298 }
00299
00300 $form_state['values'] = array();
00301 }
00302 }