00001 <?php
00011 class views_handler_sort extends views_handler {
00012
00016 function can_expose() { return TRUE; }
00017
00021 function query() {
00022 $this->ensure_my_table();
00023
00024 $this->query->add_orderby($this->table_alias, $this->real_field, $this->options['order']);
00025 }
00026
00027 function option_definition() {
00028 $options = parent::option_definition();
00029
00030 $options['order'] = array('default' => 'ASC');
00031 $options['exposed'] = array('default' => FALSE);
00032 $options['expose'] = array(
00033 'contains' => array(
00034 'label' => array('default' => '', 'translatable' => TRUE),
00035 ),
00036 );
00037 return $options;
00038 }
00039
00043 function admin_summary() {
00044 if (!empty($this->options['exposed'])) {
00045 return t('Exposed');
00046 }
00047 switch ($this->options['order']) {
00048 case 'ASC':
00049 case 'asc':
00050 default:
00051 return t('asc');
00052 break;
00053 case 'DESC';
00054 case 'desc';
00055 return t('desc');
00056 break;
00057 }
00058 }
00059
00063 function options_form(&$form, &$form_state) {
00064 parent::options_form($form, $form_state);
00065 if ($this->can_expose()) {
00066 $this->show_expose_button($form, $form_state);
00067 }
00068 $form['op_val_start'] = array('#value' => '<div class="clearfix">');
00069 $this->show_sort_form($form, $form_state);
00070 $form['op_val_end'] = array('#value' => '</div>');
00071 if ($this->can_expose()) {
00072 $this->show_expose_form($form, $form_state);
00073 }
00074 }
00075
00079 function show_expose_button(&$form, &$form_state) {
00080 $form['expose_button'] = array(
00081 '#prefix' => '<div class="views-expose clearfix">',
00082 '#suffix' => '</div>',
00083
00084 '#weight' => -1000,
00085 );
00086
00087
00088
00089 $form['expose_button']['checkbox'] = array(
00090 '#theme_wrappers' => array('container'),
00091 '#attributes' => array('class' => array('js-only')),
00092 );
00093 $form['expose_button']['checkbox']['checkbox'] = array(
00094 '#title' => t('Expose this sort to visitors, to allow them to change it'),
00095 '#type' => 'checkbox',
00096 );
00097
00098
00099 if (empty($this->options['exposed'])) {
00100 $form['expose_button']['markup'] = array(
00101 '#markup' => '<div class="description exposed-description" style="float: left; margin-right:10px">' . t('This sort is not exposed. Expose it to allow the users to change it.') . '</div>',
00102 );
00103 $form['expose_button']['button'] = array(
00104 '#limit_validation_errors' => array(),
00105 '#type' => 'submit',
00106 '#value' => t('Expose sort'),
00107 '#submit' => array('views_ui_config_item_form_expose'),
00108 );
00109 $form['expose_button']['checkbox']['checkbox']['#default_value'] = 0;
00110 }
00111 else {
00112 $form['expose_button']['markup'] = array(
00113 '#markup' => '<div class="description exposed-description">' . t('This sort is exposed. If you hide it, users will not be able to change it.') . '</div>',
00114 );
00115 $form['expose_button']['button'] = array(
00116 '#limit_validation_errors' => array(),
00117 '#type' => 'submit',
00118 '#value' => t('Hide sort'),
00119 '#submit' => array('views_ui_config_item_form_expose'),
00120 );
00121 $form['expose_button']['checkbox']['checkbox']['#default_value'] = 1;
00122 }
00123 }
00124
00128 function options_validate(&$form, &$form_state) {
00129 $this->sort_validate($form, $form_state);
00130 if (!empty($this->options['exposed'])) {
00131 $this->expose_validate($form, $form_state);
00132 }
00133
00134 }
00135
00139 function options_submit(&$form, &$form_state) {
00140 unset($form_state['values']['expose_button']);
00141 $this->sort_submit($form, $form_state);
00142 if (!empty($this->options['exposed'])) {
00143 $this->expose_submit($form, $form_state);
00144 }
00145 }
00146
00150 function show_sort_form(&$form, &$form_state) {
00151 $options = $this->sort_options();
00152 if (!empty($options)) {
00153 $form['order'] = array(
00154 '#type' => 'radios',
00155 '#options' => $options,
00156 '#default_value' => $this->options['order'],
00157 );
00158 }
00159 }
00160
00161 function sort_validate(&$form, &$form_state) { }
00162
00163 function sort_submit(&$form, &$form_state) { }
00164
00169 function sort_options() {
00170 return array(
00171 'ASC' => t('Sort ascending'),
00172 'DESC' => t('Sort descending'),
00173 );
00174 }
00175
00176 function expose_form(&$form, &$form_state) {
00177
00178
00179
00180
00181 array_unshift($form['#pre_render'], 'views_ui_pre_render_flatten_data');
00182 $form['expose']['#flatten'] = TRUE;
00183
00184 $form['expose']['label'] = array(
00185 '#type' => 'textfield',
00186 '#default_value' => $this->options['expose']['label'],
00187 '#title' => t('Label'),
00188 '#required' => TRUE,
00189 '#size' => 40,
00190 '#weight' => -1,
00191 );
00192 }
00193
00197 function expose_options() {
00198 $this->options['expose'] = array(
00199 'order' => $this->options['order'],
00200 'label' => $this->definition['title'],
00201 );
00202 }
00203 }
00204
00210 class views_handler_sort_broken extends views_handler_sort {
00211 function ui_name($short = FALSE) {
00212 return t('Broken/missing handler');
00213 }
00214
00215 function ensure_my_table() { }
00216 function query($group_by = FALSE) { }
00217 function options_form(&$form, &$form_state) {
00218 $form['markup'] = array(
00219 '#markup' => '<div class="form-item description">' . t('The handler for this item is broken or missing and cannot be used. If a module provided the handler and was disabled, re-enabling the module may restore it. Otherwise, you should probably delete this item.') . '</div>',
00220 );
00221 }
00222
00226 function broken() { return TRUE; }
00227 }
00228
00229