|
Tripal 0.3b
|
00001 <?php 00002 00003 /** 00004 * Allows stocks to be filtered by proeprty values 00005 * 00006 * @ingroup tripal_stock 00007 * @ingroup views_filter_handlers 00008 */ 00009 class views_handler_filter_stockprop_id extends views_handler_filter { 00010 00011 function init(&$view, $options) { 00012 parent::init($view, $options); 00013 $this->type = $this->options['type']; 00014 } 00015 00016 function options_form(&$form, &$form_state) { 00017 if ($this->can_expose()) { 00018 $this->show_expose_button($form, $form_state); 00019 } 00020 00021 $form['op_val_start'] = array('#value' => '<div class="clear-block">'); 00022 00023 $this->types_form($form, $form_state); 00024 00025 $this->show_operator_form($form, $form_state); 00026 $form['operator']['#prefix'] = '<div class="views-right-70">'; 00027 $this->show_value_form($form, $form_state); 00028 $form['op_val_end'] = array('#value' => '</div>'); 00029 00030 if ($this->can_expose()) { 00031 $this->show_expose_form($form, $form_state); 00032 } 00033 00034 } 00035 00036 function options_validate(&$form, &$form_state) { 00037 parent::options_validate($form, $form_state); 00038 00039 if (preg_match('/NULL/',$form_state['values']['options']['operator'])) { 00040 $value = $form_state['values']['options']['value']; 00041 if (!empty($value)) { 00042 drupal_set_message('The Value ('.$value.') will be IGNORED when the Operator is set to "Is Present" or Is Absent".', 'warning'); 00043 } 00044 } else { 00045 $value = $form_state['values']['options']['value']; 00046 if (empty($value)) { 00047 form_error($form['value'], t('Value required. The value will be used in conjunction with the operator. For example, if the ' 00048 .'operator="Is equal to" and the value="2010" then only properties with a value of 2010 and the type specified will be displayed.')); 00049 } 00050 } 00051 00052 if (empty($form_state['values']['options']['type'])) { 00053 drupal_set_message('No Property Type was choosen. As such, any property type whose value ' 00054 .$form_state['values']['options']['operator'].' '.$form_state['values']['options']['value'].' will be displayed', 'warning'); 00055 } 00056 } 00057 00058 function query () { 00059 if (preg_match('/IS NOT NULL/', $this->options['operator'])) { 00060 $new_where_sql = "stock.stock_id IN (SELECT stockprop.stock_id FROM stockprop WHERE stockprop.type_id=".$this->type.")"; 00061 } elseif (preg_match('/IS NULL/', $this->options['operator'])) { 00062 $new_where_sql = "stock.stock_id NOT IN (SELECT stockprop.stock_id FROM stockprop WHERE stockprop.type_id=".$this->type.")"; 00063 } else { 00064 $new_where_sql = "stock.stock_id IN (SELECT stockprop.stock_id FROM stockprop WHERE stockprop.type_id=".$this->type." AND stockprop.value".$this->operator."'".$this->value."')"; 00065 } 00066 $this->query->add_where($this->options['group'], $new_where_sql); 00067 } 00068 00069 function types_form(&$form, &$form_state) { 00070 $previous_db = tripal_db_set_active('chado'); 00071 $result = db_query("SELECT cvt.cvterm_id as type_id, cvt.name FROM cvterm cvt WHERE cvt.cvterm_id IN (SELECT type_id FROM stockprop)"); 00072 tripal_db_set_active($previous_db); 00073 00074 $types = array(); 00075 while ($r = db_fetch_object($result)) { $types[$r->type_id] = $r->name; } 00076 00077 $form['type'] = array( 00078 '#type' => count($options) < 10 ? 'radios' : 'select', 00079 '#title' => t('Property Types'), 00080 '#options' => $types, 00081 '#default_value' => $this->type, 00082 '#prefix' => '<div class="views-left-30">', 00083 '#suffix' => '</div>', 00084 ); 00085 00086 } 00087 00088 function value_form(&$form, &$form_state) { 00089 parent::value_form($form, $form_state); 00090 00091 if ($this->options['expose']['display_type'] == 'select') { 00092 00093 // Get options 00094 $previous_db = tripal_db_set_active('chado'); 00095 $resource = db_query("SELECT value FROM stockprop WHERE type_id=".$this->type." ORDER BY value"); 00096 tripal_db_set_active($previous_db); 00097 while ($r = db_fetch_object($resource)) { 00098 $options[$r->value] = $r->value; 00099 } 00100 $form['value'] = array( 00101 '#type' => 'select', 00102 '#title' => $this->options['label'], 00103 '#options' => $options, 00104 '#default_value' => $this->value, 00105 ); 00106 } else { 00107 $form['value'] = array( 00108 '#type' => 'textfield', 00109 '#title' => t('Value'), 00110 '#default_value' => $this->value, 00111 ); 00112 } 00113 } 00114 00115 function operator_options() { 00116 return array( 00117 '=' => t('Is equal to'), 00118 '!=' => t('Is not equal to'), 00119 '~' => t('Contains'), 00120 '!~' => t('Does not contain'), 00121 'IS NOT NULL' => t('Is Present (Not Empty)'), 00122 'IS NULL' => t('Is Absent (Empty)'), 00123 ); 00124 } 00125 00126 /** 00127 * Render our chunk of the exposed filter form when selecting 00128 */ 00129 function exposed_form(&$form, &$form_state) { 00130 if (empty($this->options['exposed'])) { 00131 return; 00132 } 00133 00134 if (!empty($this->options['expose']['use_type']) && !empty($this->options['expose']['type'])) { 00135 $type = $this->options['expose']['type']; 00136 $form[$type] = array( 00137 '#type' => 'select', 00138 '#title' => t('Property Types'), 00139 '#options' => $this->type_options(), 00140 '#default_value' => $this->type, 00141 ); 00142 00143 if (isset($form[$type]['#title'])) { 00144 unset($form[$type]['#title']); 00145 } 00146 } 00147 00148 if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator'])) { 00149 $operator = $this->options['expose']['operator']; 00150 $this->operator_form($form, $form_state); 00151 $form[$operator] = $form['operator']; 00152 00153 if (isset($form[$operator]['#title'])) { 00154 unset($form[$operator]['#title']); 00155 } 00156 00157 $this->exposed_translate($form[$operator], 'operator'); 00158 00159 unset($form['operator']); 00160 } 00161 00162 if (!empty($this->options['expose']['identifier'])) { 00163 $value = $this->options['expose']['identifier']; 00164 $this->value_form($form, $form_state); 00165 $form[$value] = $form['value']; 00166 00167 if (isset($form[$value]['#title']) && !empty($form[$value]['#type']) && $form[$value]['#type'] != 'checkbox') { 00168 unset($form[$value]['#title']); 00169 } 00170 00171 $this->exposed_translate($form[$value], 'value'); 00172 00173 if (!empty($form['#type']) && ($form['#type'] == 'checkboxes' || ($form['#type'] == 'select' && !empty($form['#multiple'])))) { 00174 unset($form[$value]['#default_value']); 00175 } 00176 00177 if (!empty($form['#type']) && $form['#type'] == 'select' && empty($form['#multiple'])) { 00178 $form[$value]['#default_value'] = 'All'; 00179 } 00180 00181 if ($value != 'value') { 00182 unset($form['value']); 00183 } 00184 } 00185 } 00186 00187 function expose_form_left(&$form, &$form_state) { 00188 $form['expose']['label'] = array( 00189 '#type' => 'textfield', 00190 '#default_value' => $this->options['expose']['label'], 00191 '#title' => t('Label'), 00192 '#size' => 40, 00193 ); 00194 00195 $form['expose']['identifier'] = array( 00196 '#type' => 'textfield', 00197 '#default_value' => $this->options['expose']['identifier'], 00198 '#title' => t('Filter identifier'), 00199 '#size' => 40, 00200 '#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'), 00201 ); 00202 00203 $form['expose']['display_type'] = array( 00204 '#type' => 'radios', 00205 '#default_value' => $this->options['expose']['display_type'], 00206 '#title' => t('Display Type'), 00207 '#description' => t('This will change the form item type of the exposed value form. ie: it can be used to let the user select the property value from a select box rather than a textfield.'), 00208 '#options' => array( 00209 'textfield' => 'Text Field', 00210 'select' => 'Drop Down', 00211 ), 00212 ); 00213 } 00214 00215 function expose_form_right(&$form, &$form_state) { 00216 if (!empty($form['type']['#type'])) { 00217 $form['expose']['use_type'] = array( 00218 '#type' => 'checkbox', 00219 '#title' => t('Unlock Property Type'), 00220 '#description' => t('When checked, the property type will be exposed to the user'), 00221 '#default_value' => !empty($this->options['expose']['use_type']), 00222 ); 00223 $form['expose']['type'] = array( 00224 '#type' => 'textfield', 00225 '#default_value' => $this->options['expose']['type'], 00226 '#title' => t('Property Type identifier'), 00227 '#size' => 40, 00228 '#description' => t('This will appear in the URL after the ? to identify this property type.'), 00229 '#process' => array('views_process_dependency'), 00230 '#dependency' => array( 00231 'edit-options-expose-use-type' => array(1) 00232 ), 00233 ); 00234 } 00235 else { 00236 $form['expose']['type'] = array( 00237 '#type' => 'value', 00238 '#value' => '', 00239 ); 00240 } 00241 00242 $form['expose']['identifier'] = array( 00243 '#type' => 'textfield', 00244 '#default_value' => $this->options['expose']['identifier'], 00245 '#title' => t('Filter identifier'), 00246 '#size' => 40, 00247 '#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'), 00248 ); 00249 00250 if (!empty($form['operator']['#type'])) { 00251 $form['expose']['use_operator'] = array( 00252 '#type' => 'checkbox', 00253 '#title' => t('Unlock operator'), 00254 '#description' => t('When checked, the operator will be exposed to the user'), 00255 '#default_value' => !empty($this->options['expose']['use_operator']), 00256 ); 00257 $form['expose']['operator'] = array( 00258 '#type' => 'textfield', 00259 '#default_value' => $this->options['expose']['operator'], 00260 '#title' => t('Operator identifier'), 00261 '#size' => 40, 00262 '#description' => t('This will appear in the URL after the ? to identify this operator.'), 00263 '#process' => array('views_process_dependency'), 00264 '#dependency' => array( 00265 'edit-options-expose-use-operator' => array(1) 00266 ), 00267 ); 00268 } 00269 else { 00270 $form['expose']['operator'] = array( 00271 '#type' => 'value', 00272 '#value' => '', 00273 ); 00274 } 00275 00276 $form['expose']['optional'] = array( 00277 '#type' => 'checkbox', 00278 '#title' => t('Optional'), 00279 '#description' => t('This exposed filter is optional and will have added options to allow it not to be set.'), 00280 '#default_value' => $this->options['expose']['optional'], 00281 ); 00282 } 00283 00284 00285 }