12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565 |
- <?php
- /**
- * @file
- * Definitions of views_handler_filter and views_handler_filter_broken.
- */
- /**
- * @defgroup views_filter_handlers Views filter handlers
- * @{
- * Handlers to tell Views how to filter queries.
- *
- * Definition items:
- * - allow empty: If true, the 'IS NULL' and 'IS NOT NULL' operators become
- * available as standard operators.
- *
- * Object flags:
- * It's possible to set specific behavior using the following flags on the
- * custom class:
- * - always_multiple:
- * Disable the possibility to force a single value.
- * - no_operator:
- * Disable the possibility to use operators.
- * - always_required:
- * Disable the possibility to allow a exposed input to be optional.
- */
- /**
- * Base class for filters.
- *
- * @ingroup views_filter_handlers
- */
- class views_handler_filter extends views_handler {
- /**
- * Contains the actual value of the field.
- *
- * This will be either configured in the views UI or entered in the exposed
- * filters.
- *
- * @var mixed
- */
- public $value = NULL;
- /**
- * Contains the operator which is used on the query.
- *
- * @var string
- */
- public $operator = '=';
- /**
- * Contains the information of the selected item in a gruped filter.
- *
- * @var array|null
- */
- public $group_info = NULL;
- /**
- * Disable the possibility to force a single value.
- *
- * @var bool
- */
- public $always_multiple = FALSE;
- /**
- * Disable the possibility to use operators.
- *
- * @var bool
- */
- public $no_operator = FALSE;
- /**
- * Disable the possibility to allow a exposed input to be optional.
- *
- * @var bool
- */
- public $always_required = FALSE;
- /**
- * Provide some extra help to get the operator/value easier to use.
- *
- * This likely has to be overridden by filters which are more complex
- * than simple operator/value.
- */
- public function init(&$view, &$options) {
- parent::init($view, $options);
- $this->operator = $this->options['operator'];
- $this->value = $this->options['value'];
- $this->group_info = $this->options['group_info']['default_group'];
- // Compatibility: The new UI changed several settings.
- if (!empty($options['exposed']) && !empty($options['expose']['optional']) && !isset($options['expose']['required'])) {
- $this->options['expose']['required'] = !$options['expose']['optional'];
- }
- if (!empty($options['exposed']) && !empty($options['expose']['single']) && !isset($options['expose']['multiple'])) {
- $this->options['expose']['multiple'] = !$options['expose']['single'];
- }
- if (!empty($options['exposed']) && !empty($options['expose']['operator']) && !isset($options['expose']['operator_id'])) {
- $this->options['expose']['operator_id'] = $options['expose']['operator_id'] = $options['expose']['operator'];
- }
- if ($this->multiple_exposed_input()) {
- $this->group_info = NULL;
- if (!empty($options['group_info']['default_group_multiple'])) {
- $this->group_info = array_filter($options['group_info']['default_group_multiple']);
- }
- $this->options['expose']['multiple'] = TRUE;
- }
- // If there are relationships in the view, allow empty should be true
- // so that we can do IS NULL checks on items. Not all filters respect
- // allow empty, but string and numeric do and that covers enough.
- if ($this->view->display_handler->get_option('relationships')) {
- $this->definition['allow empty'] = TRUE;
- }
- }
- /**
- * {@inheritdoc}
- */
- public function option_definition() {
- $options = parent::option_definition();
- $options['operator'] = array('default' => '=');
- $options['value'] = array('default' => '');
- $options['group'] = array('default' => '1');
- $options['exposed'] = array('default' => FALSE, 'bool' => TRUE);
- $options['expose'] = array(
- 'contains' => array(
- 'operator_id' => array('default' => FALSE),
- 'label' => array('default' => '', 'translatable' => TRUE),
- 'description' => array('default' => '', 'translatable' => TRUE),
- 'use_operator' => array('default' => FALSE, 'bool' => TRUE),
- 'operator_label' => array('default' => '', 'translatable' => TRUE),
- 'operator' => array('default' => ''),
- 'limit_operators' => array('default' => FALSE, 'bool' => TRUE),
- 'available_operators' => array('default' => array()),
- 'identifier' => array('default' => ''),
- 'required' => array('default' => FALSE, 'bool' => TRUE),
- 'remember' => array('default' => FALSE, 'bool' => TRUE),
- 'multiple' => array('default' => FALSE, 'bool' => TRUE),
- 'remember_roles' => array(
- 'default' => array(
- DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID,
- ),
- ),
- ),
- );
- // A group is a combination of a filter, an operator and a value
- // operating like a single filter.
- // Users can choose from a select box which group they want to apply.
- // Views will filter the view according to the defined values.
- // Because it acts as a standard filter, we have to define
- // an identifier and other settings like the widget and the label.
- // This settings are saved in another array to allow users to switch
- // between a normal filter and a group of filters with a single click.
- $options['is_grouped'] = array('default' => FALSE, 'bool' => TRUE);
- $options['group_info'] = array(
- 'contains' => array(
- 'label' => array('default' => '', 'translatable' => TRUE),
- 'description' => array('default' => '', 'translatable' => TRUE),
- 'identifier' => array('default' => ''),
- 'optional' => array('default' => TRUE, 'bool' => TRUE),
- 'widget' => array('default' => 'select'),
- 'multiple' => array('default' => FALSE, 'bool' => TRUE),
- 'remember' => array('default' => 0),
- 'default_group' => array('default' => 'All'),
- 'default_group_multiple' => array('default' => array()),
- 'group_items' => array('default' => array()),
- ),
- );
- return $options;
- }
- /**
- * Display the filter on the administrative summary.
- */
- public function admin_summary() {
- return check_plain((string) $this->operator) . ' ' . check_plain((string) $this->value);
- }
- /**
- * Determine if a filter can be exposed.
- */
- public function can_expose() {
- return TRUE;
- }
- /**
- * Determine if a filter can be converted into a group.
- *
- * Only exposed filters with operators available can be converted into groups.
- */
- public function can_build_group() {
- return $this->is_exposed() && (count($this->operator_options()) > 0);
- }
- /**
- * Returns TRUE if the exposed filter works like a grouped filter.
- */
- public function is_a_group() {
- return $this->is_exposed() && !empty($this->options['is_grouped']);
- }
- /**
- * Provide the basic form which calls through to subforms.
- *
- * If overridden, it is best to call through to the parent, or to at least
- * make sure all of the functions in this form are called.
- */
- public function options_form(&$form, &$form_state) {
- parent::options_form($form, $form_state);
- if ($this->can_expose()) {
- $this->show_expose_button($form, $form_state);
- }
- if ($this->can_build_group()) {
- $this->show_build_group_button($form, $form_state);
- }
- $form['clear_markup_start'] = array(
- '#markup' => '<div class="clearfix">',
- );
- if ($this->is_a_group()) {
- if ($this->can_build_group()) {
- $form['clear_markup_start'] = array(
- '#markup' => '<div class="clearfix">',
- );
- // Render the build group form.
- $this->show_build_group_form($form, $form_state);
- $form['clear_markup_end'] = array(
- '#markup' => '</div>',
- );
- }
- }
- else {
- // Add the subform from operator_form().
- $this->show_operator_form($form, $form_state);
- // Add the subform from value_form().
- $this->show_value_form($form, $form_state);
- $form['clear_markup_end'] = array(
- '#markup' => '</div>',
- );
- if ($this->can_expose()) {
- // Add the subform from expose_form().
- $this->show_expose_form($form, $form_state);
- }
- }
- }
- /**
- * Simple validate handler.
- */
- public function options_validate(&$form, &$form_state) {
- $this->operator_validate($form, $form_state);
- $this->value_validate($form, $form_state);
- if (!empty($this->options['exposed']) && !$this->is_a_group()) {
- $this->expose_validate($form, $form_state);
- }
- if ($this->is_a_group()) {
- $this->build_group_validate($form, $form_state);
- }
- }
- /**
- * Simple submit handler.
- */
- public function options_submit(&$form, &$form_state) {
- // Don't store these.
- unset($form_state['values']['expose_button']);
- unset($form_state['values']['group_button']);
- if (!$this->is_a_group()) {
- $this->operator_submit($form, $form_state);
- $this->value_submit($form, $form_state);
- }
- if (!empty($this->options['exposed'])) {
- $options = &$form_state['values']['options']['expose'];
- $options['available_operators'] = (!empty($options['use_operator']) && !empty($options['limit_operators'])) ? array_filter($options['available_operators']) : array();
- $this->expose_submit($form, $form_state);
- }
- if ($this->is_a_group()) {
- $this->build_group_submit($form, $form_state);
- }
- }
- /**
- * Shortcut to display the operator form.
- */
- public function show_operator_form(&$form, &$form_state) {
- $this->operator_form($form, $form_state);
- $form['operator']['#prefix'] = '<div class="views-group-box views-left-30">';
- $form['operator']['#suffix'] = '</div>';
- }
- /**
- * Options form subform for setting the operator.
- *
- * This may be overridden by child classes, and it must define
- * $form['operator'].
- *
- * @see options_form()
- */
- public function operator_form(&$form, &$form_state) {
- $options = $this->operator_options();
- if (!empty($options)) {
- $available = $this->options['expose']['available_operators'];
- if ($this->options['expose']['limit_operators'] && count($available)) {
- foreach ($options as $key => $value) {
- if (!isset($available[$key])) {
- unset($options[$key]);
- }
- }
- // Make sure we have a valid default value if the current one is
- // excluded.
- if (!isset($options[$this->operator])) {
- // Just choose the first.
- $this->operator = key($options);
- }
- }
- $form['operator'] = array(
- '#type' => count($options) < 10 ? 'radios' : 'select',
- '#title' => t('Operator'),
- '#default_value' => $this->operator,
- '#options' => $options,
- );
- }
- }
- /**
- * Provide a list of options for the default operator form.
- *
- * Should be overridden by classes that don't override operator_form.
- */
- public function operator_options() {
- return array();
- }
- /**
- * Validate the operator form.
- */
- public function operator_validate($form, &$form_state) {
- }
- /**
- * Perform any necessary changes to the form values prior to storage.
- *
- * There is no need for this function to actually store the data.
- */
- public function operator_submit($form, &$form_state) {
- }
- /**
- * Shortcut to display the value form.
- */
- public function show_value_form(&$form, &$form_state) {
- $this->value_form($form, $form_state);
- if (empty($this->no_operator)) {
- $form['value']['#prefix'] = '<div class="views-group-box views-right-70">' . (isset($form['value']['#prefix']) ? $form['value']['#prefix'] : '');
- $form['value']['#suffix'] = (isset($form['value']['#suffix']) ? $form['value']['#suffix'] : '') . '</div>';
- }
- }
- /**
- * Options form subform for setting options.
- *
- * This should be overridden by all child classes and it must define
- * $form['value'].
- *
- * @see options_form()
- */
- public function value_form(&$form, &$form_state) {
- $form['value'] = array();
- }
- /**
- * Validate the options form.
- */
- public function value_validate($form, &$form_state) {
- }
- /**
- * Perform any necessary changes to the form values prior to storage.
- *
- * There is no need for this function to actually store the data.
- */
- public function value_submit($form, &$form_state) {
- }
- /**
- * Shortcut to display the exposed options form.
- */
- public function show_build_group_form(&$form, &$form_state) {
- if (empty($this->options['is_grouped'])) {
- return;
- }
- $this->build_group_form($form, $form_state);
- // When we click the expose button, we add new gadgets to the form but they
- // have no data in $_POST so their defaults get wiped out. This prevents
- // these defaults from getting wiped out. This setting will only be TRUE
- // during a 2nd pass rerender.
- if (!empty($form_state['force_build_group_options'])) {
- foreach (element_children($form['group_info']) as $id) {
- if (isset($form['group_info'][$id]['#default_value']) && !isset($form['group_info'][$id]['#value'])) {
- $form['group_info'][$id]['#value'] = $form['group_info'][$id]['#default_value'];
- }
- }
- }
- }
- /**
- * Shortcut to display the build_group/hide button.
- */
- public function show_build_group_button(&$form, &$form_state) {
- $form['group_button'] = array(
- '#prefix' => '<div class="views-grouped clearfix">',
- '#suffix' => '</div>',
- // Should always come after the description and the relationship.
- '#weight' => -190,
- );
- $grouped_description = t('Grouped filters allow a choice between predefined operator|value pairs.');
- $form['group_button']['radios'] = array(
- '#theme_wrappers' => array('container'),
- '#attributes' => array('class' => array('js-only')),
- );
- $form['group_button']['radios']['radios'] = array(
- '#title' => t('Filter type to expose'),
- '#description' => $grouped_description,
- '#type' => 'radios',
- '#options' => array(
- t('Single filter'),
- t('Grouped filters'),
- ),
- );
- if (empty($this->options['is_grouped'])) {
- $form['group_button']['markup'] = array(
- '#markup' => '<div class="description grouped-description">' . $grouped_description . '</div>',
- );
- $form['group_button']['button'] = array(
- '#limit_validation_errors' => array(),
- '#type' => 'submit',
- '#value' => t('Grouped filters'),
- '#submit' => array('views_ui_config_item_form_build_group'),
- );
- $form['group_button']['radios']['radios']['#default_value'] = 0;
- }
- else {
- $form['group_button']['button'] = array(
- '#limit_validation_errors' => array(),
- '#type' => 'submit',
- '#value' => t('Single filter'),
- '#submit' => array('views_ui_config_item_form_build_group'),
- );
- $form['group_button']['radios']['radios']['#default_value'] = 1;
- }
- }
- /**
- * Shortcut to display the expose/hide button.
- */
- public function show_expose_button(&$form, &$form_state) {
- $form['expose_button'] = array(
- '#prefix' => '<div class="views-expose clearfix">',
- '#suffix' => '</div>',
- // Should always come after the description and the relationship.
- '#weight' => -200,
- );
- // Add a checkbox for JS users, which will have behavior attached to it so
- // it can replace the button.
- $form['expose_button']['checkbox'] = array(
- '#theme_wrappers' => array('container'),
- '#attributes' => array('class' => array('js-only')),
- );
- $form['expose_button']['checkbox']['checkbox'] = array(
- '#title' => t('Expose this filter to visitors, to allow them to change it'),
- '#type' => 'checkbox',
- );
- // Then add the button itself.
- if (empty($this->options['exposed'])) {
- $form['expose_button']['markup'] = array(
- '#markup' => '<div class="description exposed-description">' . t('This filter is not exposed. Expose it to allow the users to change it.') . '</div>',
- );
- $form['expose_button']['button'] = array(
- '#limit_validation_errors' => array(),
- '#type' => 'submit',
- '#value' => t('Expose filter'),
- '#submit' => array('views_ui_config_item_form_expose'),
- );
- $form['expose_button']['checkbox']['checkbox']['#default_value'] = 0;
- }
- else {
- $form['expose_button']['markup'] = array(
- '#markup' => '<div class="description exposed-description">' . t('This filter is exposed. If you hide it, users will not be able to change it.') . '</div>',
- );
- $form['expose_button']['button'] = array(
- '#limit_validation_errors' => array(),
- '#type' => 'submit',
- '#value' => t('Hide filter'),
- '#submit' => array('views_ui_config_item_form_expose'),
- );
- $form['expose_button']['checkbox']['checkbox']['#default_value'] = 1;
- }
- }
- /**
- * Options form subform for exposed filter options.
- *
- * @see options_form()
- */
- public function expose_form(&$form, &$form_state) {
- $form['#theme'] = 'views_ui_expose_filter_form';
- // #flatten will move everything from $form['expose'][$key] to $form[$key]
- // prior to rendering. That's why the pre_render for it needs to run first,
- // so that when the next pre_render (the one for fieldsets) runs, it gets
- // the flattened data.
- array_unshift($form['#pre_render'], 'views_ui_pre_render_flatten_data');
- $form['expose']['#flatten'] = TRUE;
- if (empty($this->always_required)) {
- $form['expose']['required'] = array(
- '#type' => 'checkbox',
- '#title' => t('Required'),
- '#default_value' => $this->options['expose']['required'],
- );
- $operator_options = $this->operator_options();
- if (count($operator_options)) {
- $form['expose']['limit_operators'] = array(
- '#type' => 'checkbox',
- '#title' => t('Limit operators'),
- '#description' => t('When checked, the operator will be exposed to the user'),
- '#default_value' => !empty($this->options['expose']['limit_operators']),
- '#dependency' => array(
- 'edit-options-expose-use-operator' => array(1),
- ),
- '#description' => t('Restrict which operators will be available to select in the exposed operator form.'),
- );
- $form['expose']['available_operators'] = array(
- '#type' => 'checkboxes',
- '#title' => t('Limit the exposed operators'),
- '#default_value' => $this->options['expose']['available_operators'],
- '#prefix' => '<div id="edit-options-expose-available-operators-wrapper"><div id="edit-options-expose-available-operators">',
- '#suffix' => '</div></div>',
- '#description' => t('Select which operators will be available to select in the exposed operator form. If none are selected, all the operators listed here will be used.'),
- '#options' => $operator_options,
- '#dependency' => array(
- 'edit-options-expose-limit-operators' => array(1),
- ),
- );
- }
- }
- else {
- $form['expose']['required'] = array(
- '#type' => 'value',
- '#value' => TRUE,
- );
- }
- $form['expose']['label'] = array(
- '#type' => 'textfield',
- '#default_value' => $this->options['expose']['label'],
- '#title' => t('Label'),
- '#size' => 40,
- );
- $form['expose']['description'] = array(
- '#type' => 'textfield',
- '#default_value' => $this->options['expose']['description'],
- '#title' => t('Description'),
- '#size' => 60,
- );
- if (!empty($form['operator']['#type'])) {
- // Increase the width of the left (operator) column.
- $form['operator']['#prefix'] = '<div class="views-group-box views-left-40">';
- $form['operator']['#suffix'] = '</div>';
- $form['value']['#prefix'] = '<div class="views-group-box views-right-60">';
- $form['value']['#suffix'] = '</div>';
- $form['expose']['use_operator'] = array(
- '#type' => 'checkbox',
- '#title' => t('Expose operator'),
- '#description' => t('Allow the user to choose the operator.'),
- '#default_value' => !empty($this->options['expose']['use_operator']),
- );
- $form['expose']['operator_label'] = array(
- '#type' => 'textfield',
- '#default_value' => $this->options['expose']['operator_label'],
- '#title' => t('Operator label'),
- '#size' => 40,
- '#description' => t('This will appear before your operator select field.'),
- '#dependency' => array(
- 'edit-options-expose-use-operator' => array(1),
- ),
- );
- $form['expose']['operator_id'] = array(
- '#type' => 'textfield',
- '#default_value' => $this->options['expose']['operator_id'],
- '#title' => t('Operator identifier'),
- '#size' => 40,
- '#description' => t('This will appear in the URL after the ? to identify this operator.'),
- '#dependency' => array(
- 'edit-options-expose-use-operator' => array(1),
- ),
- '#fieldset' => 'more',
- );
- }
- else {
- $form['expose']['operator_id'] = array(
- '#type' => 'value',
- '#value' => '',
- );
- }
- if (empty($this->always_multiple)) {
- $form['expose']['multiple'] = array(
- '#type' => 'checkbox',
- '#title' => t('Allow multiple selections'),
- '#description' => t('Enable to allow users to select multiple items.'),
- '#default_value' => $this->options['expose']['multiple'],
- );
- }
- $form['expose']['remember'] = array(
- '#type' => 'checkbox',
- '#title' => t('Remember the last selection'),
- '#description' => t('Enable to remember the last selection made by the user.'),
- '#default_value' => $this->options['expose']['remember'],
- );
- $role_options = array_map('check_plain', user_roles());
- $form['expose']['remember_roles'] = array(
- '#type' => 'checkboxes',
- '#title' => t('User roles'),
- '#description' => t('Remember exposed selection only for the selected user role(s). If you select no roles, the exposed data will never be stored.'),
- '#default_value' => $this->options['expose']['remember_roles'],
- '#options' => $role_options,
- '#dependency' => array(
- 'edit-options-expose-remember' => array(1),
- ),
- );
- $form['expose']['identifier'] = array(
- '#type' => 'textfield',
- '#default_value' => $this->options['expose']['identifier'],
- '#title' => t('Filter identifier'),
- '#size' => 40,
- '#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
- '#fieldset' => 'more',
- );
- }
- /**
- * Validate the options form.
- */
- public function expose_validate($form, &$form_state) {
- if (empty($form_state['values']['options']['expose']['identifier'])) {
- form_error($form['expose']['identifier'], t('The identifier is required if the filter is exposed.'));
- }
- if (!empty($form_state['values']['options']['expose']['identifier'])) {
- $illegal_identifiers = array('value', 'q');
- if (in_array($form_state['values']['options']['expose']['identifier'], $illegal_identifiers)) {
- form_error($form['expose']['identifier'], t('This identifier is not allowed.'));
- }
- }
- if (!$this->view->display_handler->is_identifier_unique($form_state['id'], $form_state['values']['options']['expose']['identifier'])) {
- form_error($form['expose']['identifier'], t('This identifier is used by another handler.'));
- }
- // Filter out roles which weren't selected, so that they aren't exported.
- // This is purely cosmetic.
- if (!empty($form_state['values']['options']['expose']['remember_roles'])) {
- $form_state['values']['options']['expose']['remember_roles'] = array_filter($form_state['values']['options']['expose']['remember_roles']);
- }
- }
- /**
- * Validate the build group options form.
- */
- public function build_group_validate($form, &$form_state) {
- if (!empty($form_state['values']['options']['group_info'])) {
- if (empty($form_state['values']['options']['group_info']['identifier'])) {
- form_error($form['group_info']['identifier'], t('The identifier is required if the filter is exposed.'));
- }
- if (!empty($form_state['values']['options']['group_info']['identifier'])) {
- $illegal_identifiers = array('value', 'q');
- if (in_array($form_state['values']['options']['group_info']['identifier'], $illegal_identifiers)) {
- form_error($form['group_info']['identifier'], t('This identifier is not allowed.'));
- }
- }
- if (!$this->view->display_handler->is_identifier_unique($form_state['id'], $form_state['values']['options']['group_info']['identifier'])) {
- form_error($form['group_info']['identifier'], t('This identifier is used by another handler.'));
- }
- }
- if (!empty($form_state['values']['options']['group_info']['group_items'])) {
- foreach ($form_state['values']['options']['group_info']['group_items'] as $id => $group) {
- if (empty($group['remove'])) {
- // Check if the title is defined but value wasn't defined.
- if (!empty($group['title'])) {
- // No value is needed for 'empty' and 'not empty' operator.
- if (!in_array($group['operator'], array('empty', 'not empty'))) {
- if ((!is_array($group['value']) && trim($group['value']) == "") ||
- (is_array($group['value']) && count(array_filter($group['value'], '_views_array_filter_zero')) == 0)) {
- form_error($form['group_info']['group_items'][$id]['value'],
- t('The value is required if title for this item is defined.'));
- }
- }
- }
- // Check if the value is defined but title wasn't defined.
- if ((!is_array($group['value']) && trim($group['value']) != "") ||
- (is_array($group['value']) && count(array_filter($group['value'], '_views_array_filter_zero')) > 0)) {
- if (empty($group['title'])) {
- form_error($form['group_info']['group_items'][$id]['title'],
- t('The title is required if value for this item is defined.'));
- }
- }
- }
- }
- }
- }
- /**
- * Save new group items, re-enumerates and remove groups marked to delete.
- */
- public function build_group_submit($form, &$form_state) {
- $groups = array();
- uasort($form_state['values']['options']['group_info']['group_items'], 'drupal_sort_weight');
- // Filter out removed items.
- // Start from 1 to avoid problems with #default_value in the widget.
- $new_id = 1;
- $new_default = 'All';
- foreach ($form_state['values']['options']['group_info']['group_items'] as $id => $group) {
- if (empty($group['remove'])) {
- // Don't store this.
- unset($group['remove']);
- unset($group['weight']);
- $groups[$new_id] = $group;
- if ($form_state['values']['options']['group_info']['default_group'] === $id) {
- $new_default = $new_id;
- }
- }
- $new_id++;
- }
- if ($new_default != 'All') {
- $form_state['values']['options']['group_info']['default_group'] = $new_default;
- }
- $filter_default_multiple = array_filter($form_state['values']['options']['group_info']['default_group_multiple']);
- $form_state['values']['options']['group_info']['default_group_multiple'] = $filter_default_multiple;
- $form_state['values']['options']['group_info']['group_items'] = $groups;
- }
- /**
- * Provide default options for exposed filters.
- */
- public function expose_options() {
- $this->options['expose'] = array(
- 'use_operator' => FALSE,
- 'operator' => $this->options['id'] . '_op',
- 'identifier' => $this->options['id'],
- 'label' => $this->definition['title'],
- 'description' => NULL,
- 'remember' => FALSE,
- 'multiple' => FALSE,
- 'required' => FALSE,
- );
- }
- /**
- * Provide default options for exposed filters.
- */
- public function build_group_options() {
- $this->options['group_info'] = array(
- 'label' => $this->definition['title'],
- 'description' => NULL,
- 'identifier' => $this->options['id'],
- 'optional' => TRUE,
- 'widget' => 'select',
- 'multiple' => FALSE,
- 'remember' => FALSE,
- 'default_group' => 'All',
- 'default_group_multiple' => array(),
- 'group_items' => array(),
- );
- }
- /**
- * Build a form with a group of operator | values to apply as a single filter.
- */
- public function group_form(&$form, &$form_state) {
- if (!empty($this->options['group_info']['optional']) && !$this->multiple_exposed_input()) {
- $old_any = $this->options['group_info']['widget'] == 'select' ? '<Any>' : '<Any>';
- $any_label = variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? $old_any : t('- Any -');
- $groups = array('All' => $any_label);
- }
- foreach ($this->options['group_info']['group_items'] as $id => $group) {
- if (!empty($group['title'])) {
- $groups[$id] = $id != 'All' ? t($group['title']) : $group['title'];
- }
- }
- if (count($groups)) {
- $value = $this->options['group_info']['identifier'];
- $form[$value] = array(
- '#type' => $this->options['group_info']['widget'],
- '#default_value' => $this->group_info,
- '#options' => $groups,
- );
- if (!empty($this->options['group_info']['multiple'])) {
- if (count($groups) < 5) {
- $form[$value]['#type'] = 'checkboxes';
- }
- else {
- $form[$value]['#type'] = 'select';
- $form[$value]['#size'] = 5;
- $form[$value]['#multiple'] = TRUE;
- }
- unset($form[$value]['#default_value']);
- if (empty($form_state['input'][$value])) {
- $form_state['input'][$value] = $this->group_info;
- }
- }
- $this->options['expose']['label'] = '';
- }
- }
- /**
- * Render our chunk of the exposed filter form when selecting.
- *
- * You can override this if it doesn't do what you expect.
- */
- public function exposed_form(&$form, &$form_state) {
- if (empty($this->options['exposed'])) {
- return;
- }
- // Build the exposed form, when its based on an operator.
- if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id'])) {
- $operator = $this->options['expose']['operator_id'];
- $this->operator_form($form, $form_state);
- $form[$operator] = $form['operator'];
- $form[$operator]['#title'] = $this->options['expose']['operator_label'];
- $form[$operator]['#title_display'] = 'invisible';
- $this->exposed_translate($form[$operator], 'operator');
- unset($form['operator']);
- }
- // Build the form and set the value based on the identifier.
- if (!empty($this->options['expose']['identifier'])) {
- $value = $this->options['expose']['identifier'];
- if ($this->operator == 'empty' || $this->operator == 'not empty') {
- $boolean = new views_handler_filter_boolean_operator();
- $boolean->value = $this->value = 'All';
- $boolean->value_value = $this->value_value = '';
- $boolean->value_options = $this->value_options = array(
- 1 => t('Yes'),
- 0 => t('No'),
- );
- $boolean->value_form($form, $form_state);
- }
- else {
- $this->value_form($form, $form_state);
- }
- $form[$value] = $form['value'];
- if (isset($form[$value]['#title']) && !empty($form[$value]['#type']) && $form[$value]['#type'] != 'checkbox') {
- unset($form[$value]['#title']);
- }
- $this->exposed_translate($form[$value], 'value');
- if (!empty($form['#type']) && ($form['#type'] == 'checkboxes' || ($form['#type'] == 'select' && !empty($form['#multiple'])))) {
- unset($form[$value]['#default_value']);
- }
- if (!empty($form['#type']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
- $form[$value]['#default_value'] = 'All';
- }
- if ($value != 'value') {
- unset($form['value']);
- }
- }
- }
- /**
- * Build the form to let users create the group of exposed filters.
- *
- * This form is displayed when users click on button 'Build group'
- */
- public function build_group_form(&$form, &$form_state) {
- if (empty($this->options['exposed']) || empty($this->options['is_grouped'])) {
- return;
- }
- $form['#theme'] = 'views_ui_build_group_filter_form';
- // #flatten will move everything from $form['group_info'][$key] to
- // $form[$key] prior to rendering. That's why the pre_render for it needs
- // to run first, so that when the next pre_render (the one for fieldsets)
- // runs, it gets the flattened data.
- array_unshift($form['#pre_render'], 'views_ui_pre_render_flatten_data');
- $form['group_info']['#flatten'] = TRUE;
- if (!empty($this->options['group_info']['identifier'])) {
- $identifier = $this->options['group_info']['identifier'];
- }
- else {
- $identifier = 'group_' . $this->options['expose']['identifier'];
- }
- $form['group_info']['identifier'] = array(
- '#type' => 'textfield',
- '#default_value' => $identifier,
- '#title' => t('Filter identifier'),
- '#size' => 40,
- '#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
- '#fieldset' => 'more',
- );
- $form['group_info']['label'] = array(
- '#type' => 'textfield',
- '#default_value' => $this->options['group_info']['label'],
- '#title' => t('Label'),
- '#size' => 40,
- );
- $form['group_info']['optional'] = array(
- '#type' => 'checkbox',
- '#title' => t('Optional'),
- '#description' => t('This exposed filter is optional and will have added options to allow it not to be set.'),
- '#default_value' => $this->options['group_info']['optional'],
- );
- $form['group_info']['multiple'] = array(
- '#type' => 'checkbox',
- '#title' => t('Allow multiple selections'),
- '#description' => t('Enable to allow users to select multiple items.'),
- '#default_value' => $this->options['group_info']['multiple'],
- );
- $form['group_info']['widget'] = array(
- '#type' => 'radios',
- '#default_value' => $this->options['group_info']['widget'],
- '#title' => t('Widget type'),
- '#options' => array(
- 'radios' => t('Radios'),
- 'select' => t('Select'),
- ),
- '#description' => t('Select which kind of widget will be used to render the group of filters'),
- );
- $form['group_info']['remember'] = array(
- '#type' => 'checkbox',
- '#title' => t('Remember'),
- '#description' => t('Remember the last setting the user gave this filter.'),
- '#default_value' => $this->options['group_info']['remember'],
- );
- if (!empty($this->options['group_info']['identifier'])) {
- $identifier = $this->options['group_info']['identifier'];
- }
- else {
- $identifier = 'group_' . $this->options['expose']['identifier'];
- }
- $form['group_info']['identifier'] = array(
- '#type' => 'textfield',
- '#default_value' => $identifier,
- '#title' => t('Filter identifier'),
- '#size' => 40,
- '#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
- '#fieldset' => 'more',
- );
- $form['group_info']['label'] = array(
- '#type' => 'textfield',
- '#default_value' => $this->options['group_info']['label'],
- '#title' => t('Label'),
- '#size' => 40,
- );
- $form['group_info']['description'] = array(
- '#type' => 'textfield',
- '#default_value' => $this->options['group_info']['description'],
- '#title' => t('Description'),
- '#size' => 60,
- );
- $form['group_info']['optional'] = array(
- '#type' => 'checkbox',
- '#title' => t('Optional'),
- '#description' => t('This exposed filter is optional and will have added options to allow it not to be set.'),
- '#default_value' => $this->options['group_info']['optional'],
- );
- $form['group_info']['widget'] = array(
- '#type' => 'radios',
- '#default_value' => $this->options['group_info']['widget'],
- '#title' => t('Widget type'),
- '#options' => array(
- 'radios' => t('Radios'),
- 'select' => t('Select'),
- ),
- '#description' => t('Select which kind of widget will be used to render the group of filters'),
- );
- $form['group_info']['remember'] = array(
- '#type' => 'checkbox',
- '#title' => t('Remember'),
- '#description' => t('Remember the last setting the user gave this filter.'),
- '#default_value' => $this->options['group_info']['remember'],
- );
- // The string '- Any -' will not be rendered.
- // @see theme_views_ui_build_group_filter_form()
- $groups = array('All' => '- Any -');
- // Provide 3 options to start when we are in a new group.
- if (count($this->options['group_info']['group_items']) == 0) {
- $this->options['group_info']['group_items'] = array_fill(1, 3, array());
- }
- // After the general settings, comes a table with all the existent groups.
- $default_weight = 0;
- foreach ($this->options['group_info']['group_items'] as $item_id => $item) {
- if (!empty($form_state['values']['options']['group_info']['group_items'][$item_id]['remove'])) {
- continue;
- }
- // Each rows contains three widgets:
- // a) The title, where users define how they identify a pair of operator
- // | value.
- // b) The operator.
- // c) The value (or values) to use in the filter with the selected
- // operator.
- // In each row, we have to display the operator form and the value from
- // $row acts as a fake form to render each widget in a row.
- $row = array();
- $groups[$item_id] = '';
- $this->operator_form($row, $form_state);
- // Force the operator form to be a select box. Some handlers uses radios
- // and they occupy a lot of space in a table row.
- $row['operator']['#type'] = 'select';
- $row['operator']['#title'] = '';
- $this->value_form($row, $form_state);
- // Fix the dependencies to update value forms when operators changes.
- // This is needed because forms are inside a new form and their IDs
- // changes. Dependencies are used when operator changes from to
- // 'Between', 'Not Between', etc, and two or more widgets are displayed.
- $without_children = TRUE;
- foreach (element_children($row['value']) as $children) {
- if (isset($row['value'][$children]['#dependency']['edit-options-operator'])) {
- $row['value'][$children]['#dependency']["edit-options-group-info-group-items-$item_id-operator"] = $row['value'][$children]['#dependency']['edit-options-operator'];
- unset($row['value'][$children]['#dependency']['edit-options-operator']);
- $row['value'][$children]['#title'] = '';
- if (!empty($this->options['group_info']['group_items'][$item_id]['value'][$children])) {
- $row['value'][$children]['#default_value'] = $this->options['group_info']['group_items'][$item_id]['value'][$children];
- }
- }
- $without_children = FALSE;
- }
- if ($without_children) {
- if (!empty($this->options['group_info']['group_items'][$item_id]['value'])) {
- $row['value']['#default_value'] = $this->options['group_info']['group_items'][$item_id]['value'];
- }
- }
- if (!empty($this->options['group_info']['group_items'][$item_id]['operator'])) {
- $row['operator']['#default_value'] = $this->options['group_info']['group_items'][$item_id]['operator'];
- }
- $default_title = '';
- if (!empty($this->options['group_info']['group_items'][$item_id]['title'])) {
- $default_title = $this->options['group_info']['group_items'][$item_id]['title'];
- }
- // Per item group, we have a title that identifies it.
- $form['group_info']['group_items'][$item_id] = array(
- 'title' => array(
- '#type' => 'textfield',
- '#size' => 20,
- '#default_value' => $default_title,
- ),
- 'operator' => $row['operator'],
- 'value' => $row['value'],
- 'remove' => array(
- '#type' => 'checkbox',
- '#id' => 'views-removed-' . $item_id,
- '#attributes' => array('class' => array('views-remove-checkbox')),
- '#default_value' => 0,
- ),
- 'weight' => array(
- '#type' => 'weight',
- '#delta' => count($this->options['group_info']['group_items']),
- '#default_value' => $default_weight++,
- '#attributes' => array('class' => array('weight')),
- ),
- );
- }
- // From all groups, let chose which is the default.
- $form['group_info']['default_group'] = array(
- '#type' => 'radios',
- '#options' => $groups,
- '#default_value' => $this->options['group_info']['default_group'],
- '#required' => TRUE,
- '#attributes' => array(
- 'class' => array('default-radios'),
- ),
- );
- // From all groups, let chose which is the default.
- $form['group_info']['default_group_multiple'] = array(
- '#type' => 'checkboxes',
- '#options' => $groups,
- '#default_value' => $this->options['group_info']['default_group_multiple'],
- '#attributes' => array(
- 'class' => array('default-checkboxes'),
- ),
- );
- $form['group_info']['add_group'] = array(
- '#prefix' => '<div class="views-build-group clear-block">',
- '#suffix' => '</div>',
- '#type' => 'submit',
- '#value' => t('Add another item'),
- '#submit' => array('views_ui_config_item_form_add_group'),
- );
- $js = array();
- $js['tableDrag']['views-filter-groups']['weight'][0] = array(
- 'target' => 'weight',
- 'source' => NULL,
- 'relationship' => 'sibling',
- 'action' => 'order',
- 'hidden' => TRUE,
- 'limit' => 0,
- );
- if (!empty($form_state['js settings']) && is_array($js)) {
- $form_state['js settings'] = array_merge($form_state['js settings'], $js);
- }
- else {
- $form_state['js settings'] = $js;
- }
- }
- /**
- * Make some translations to a form item to make it more suitable to exposing.
- */
- public function exposed_translate(&$form, $type) {
- if (!isset($form['#type'])) {
- return;
- }
- if ($form['#type'] == 'radios') {
- $form['#type'] = 'select';
- }
- // Checkboxes don't work so well in exposed forms due to GET conversions.
- if ($form['#type'] == 'checkboxes') {
- if (empty($form['#no_convert']) || empty($this->options['expose']['multiple'])) {
- $form['#type'] = 'select';
- }
- if (!empty($this->options['expose']['multiple'])) {
- $form['#multiple'] = TRUE;
- }
- }
- if (empty($this->options['expose']['multiple']) && isset($form['#multiple'])) {
- unset($form['#multiple']);
- $form['#size'] = NULL;
- }
- // Cleanup in case the translated element's (radios or checkboxes) display
- // value contains html.
- if ($form['#type'] == 'select') {
- $this->prepare_filter_select_options($form['#options']);
- }
- if ($type == 'value' && empty($this->always_required) && empty($this->options['expose']['required']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
- $any_label = variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? t('<Any>') : t('- Any -');
- $form['#options'] = array('All' => $any_label) + $form['#options'];
- $form['#default_value'] = 'All';
- }
- if (!empty($this->options['expose']['required'])) {
- $form['#required'] = TRUE;
- }
- }
- /**
- * Sanitizes the HTML select element's options.
- *
- * The function is recursive to support optgroups.
- */
- public function prepare_filter_select_options(&$options) {
- foreach ($options as $value => $label) {
- // Recurse for optgroups.
- if (is_array($label)) {
- $this->prepare_filter_select_options($options[$value]);
- }
- // FAPI has some special value to allow hierarchy.
- // @see _form_options_flatten()
- elseif (is_object($label)) {
- $this->prepare_filter_select_options($options[$value]->option);
- }
- else {
- $options[$value] = strip_tags(decode_entities($label));
- }
- }
- }
- /**
- * Tell the renderer about our exposed form.
- *
- * This only needs to be overridden for particularly complex forms. And maybe
- * not even then.
- *
- * @return array|null
- * For standard exposed filters. An array with the following keys:
- * - operator: The $form key of the operator. Set to NULL if no operator.
- * - value: The $form key of the value. Set to NULL if no value.
- * - label: The label to use for this piece.
- * For grouped exposed filters. An array with the following keys:
- * - value: The $form key of the value. Set to NULL if no value.
- * - label: The label to use for this piece.
- */
- public function exposed_info() {
- if (empty($this->options['exposed'])) {
- return;
- }
- if ($this->is_a_group()) {
- return array(
- 'value' => $this->options['group_info']['identifier'],
- 'label' => $this->options['group_info']['label'],
- 'description' => $this->options['group_info']['description'],
- );
- }
- return array(
- 'operator' => $this->options['expose']['operator_id'],
- 'value' => $this->options['expose']['identifier'],
- 'label' => $this->options['expose']['label'],
- 'description' => $this->options['expose']['description'],
- );
- }
- /**
- * Transform the input from a grouped filter into a standard filter.
- *
- * When a filter is a group, find the set of operator and values that the
- * choosen item represents, and inform views that a normal filter was
- * submitted by telling the operator and the value selected.
- *
- * The param $selected_group_id is only passed when the filter uses the
- * checkboxes widget, and this function will be called for each item choosen
- * in the checkboxes.
- */
- public function convert_exposed_input(&$input, $selected_group_id = NULL) {
- if ($this->is_a_group()) {
- // If it is already defined the selected group, use it. Only valid when
- // the filter uses checkboxes for widget.
- if (!empty($selected_group_id)) {
- $selected_group = $selected_group_id;
- }
- else {
- $selected_group = $input[$this->options['group_info']['identifier']];
- }
- if ($selected_group == 'All' && !empty($this->options['group_info']['optional'])) {
- return NULL;
- }
- if ($selected_group != 'All' && empty($this->options['group_info']['group_items'][$selected_group])) {
- return FALSE;
- }
- if (isset($selected_group) && isset($this->options['group_info']['group_items'][$selected_group])) {
- $input[$this->options['expose']['operator']] = $this->options['group_info']['group_items'][$selected_group]['operator'];
- // Value can be optional, For example for 'empty' and 'not empty'
- // filters.
- if (!empty($this->options['group_info']['group_items'][$selected_group]['value'])) {
- $input[$this->options['expose']['identifier']] = $this->options['group_info']['group_items'][$selected_group]['value'];
- }
- $this->options['expose']['use_operator'] = TRUE;
- $this->group_info = $input[$this->options['group_info']['identifier']];
- return TRUE;
- }
- else {
- return FALSE;
- }
- }
- }
- /**
- * Options available for a grouped filter which uses checkboxes.
- *
- * Note: has to be applied several times, one per item selected.
- *
- * @return array
- * The options available for a grouped filter.
- */
- public function group_multiple_exposed_input(&$input) {
- if (!empty($input[$this->options['group_info']['identifier']])) {
- return array_filter($input[$this->options['group_info']['identifier']]);
- }
- return array();
- }
- /**
- * Indicate whether users can select multiple group items.
- *
- * @return bool
- * TRUE if users can select multiple groups items of a grouped exposed
- * filter.
- */
- public function multiple_exposed_input() {
- return $this->is_a_group() && !empty($this->options['group_info']['multiple']);
- }
- /**
- * If set to remember exposed input in the session, store it there.
- *
- * This function is similar to store_exposed_input but modified to work
- * properly when the filter is a group.
- */
- public function store_group_input($input, $status) {
- if (!$this->is_a_group() || empty($this->options['group_info']['identifier'])) {
- return TRUE;
- }
- if (empty($this->options['group_info']['remember'])) {
- return;
- }
- // Figure out which display id is responsible for the filters, so we know
- // where to look for session stored values.
- $display_id = ($this->view->display_handler->is_defaulted('filters')) ? 'default' : $this->view->current_display;
- // False means that we got a setting that means to recuse ourselves, so we
- // should erase whatever happened to be there.
- if ($status === FALSE && isset($_SESSION['views'][$this->view->name][$display_id])) {
- $session = &$_SESSION['views'][$this->view->name][$display_id];
- if (isset($session[$this->options['group_info']['identifier']])) {
- unset($session[$this->options['group_info']['identifier']]);
- }
- }
- if ($status !== FALSE) {
- if (!isset($_SESSION['views'][$this->view->name][$display_id])) {
- $_SESSION['views'][$this->view->name][$display_id] = array();
- }
- $session = &$_SESSION['views'][$this->view->name][$display_id];
- $session[$this->options['group_info']['identifier']] = $input[$this->options['group_info']['identifier']];
- }
- }
- /**
- * Check to see if input from the exposed filters should change the behavior.
- */
- public function accept_exposed_input($input) {
- if (empty($this->options['exposed'])) {
- return TRUE;
- }
- if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']) && isset($input[$this->options['expose']['operator_id']])) {
- $this->operator = $input[$this->options['expose']['operator_id']];
- }
- if (!empty($this->options['expose']['identifier'])) {
- $value = $input[$this->options['expose']['identifier']];
- // Various ways to check for the absence of non-required input.
- if (empty($this->options['expose']['required'])) {
- if ($this->operator == 'empty' || $this->operator == 'not empty') {
- $value = is_array($value) ? $value['value'] : $value;
- $this->operator = ($this->operator == 'empty' && empty($value)) || ($this->operator == 'not empty' && !empty($value)) ? 'not empty' : 'empty';
- }
- if ($value == 'All' || $value === array()) {
- return FALSE;
- }
- if (!empty($this->always_multiple) && $value === '') {
- return FALSE;
- }
- }
- if (isset($value)) {
- $this->value = $value;
- if (empty($this->always_multiple) && empty($this->options['expose']['multiple'])) {
- $this->value = array($value);
- }
- }
- else {
- return FALSE;
- }
- }
- return TRUE;
- }
- /**
- * Store the exposed input for processing later.
- */
- public function store_exposed_input($input, $status) {
- if (empty($this->options['exposed']) || empty($this->options['expose']['identifier'])) {
- return TRUE;
- }
- if (empty($this->options['expose']['remember'])) {
- return;
- }
- // Check if we store exposed value for current user.
- global $user;
- $allowed_rids = empty($this->options['expose']['remember_roles']) ? array() : array_filter($this->options['expose']['remember_roles']);
- $intersect_rids = array_intersect_key($allowed_rids, $user->roles);
- if (empty($intersect_rids)) {
- return;
- }
- // Figure out which display id is responsible for the filters, so we know
- // where to look for session stored values.
- $display_id = ($this->view->display_handler->is_defaulted('filters')) ? 'default' : $this->view->current_display;
- // Shortcut test.
- $operator = !empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']);
- // False means that we got a setting that means to recuse ourselves, so we
- // should erase whatever happened to be there.
- if (!$status && isset($_SESSION['views'][$this->view->name][$display_id])) {
- $session = &$_SESSION['views'][$this->view->name][$display_id];
- if ($operator && isset($session[$this->options['expose']['operator_id']])) {
- unset($session[$this->options['expose']['operator_id']]);
- }
- if (isset($session[$this->options['expose']['identifier']])) {
- unset($session[$this->options['expose']['identifier']]);
- }
- }
- if ($status) {
- if (!isset($_SESSION['views'][$this->view->name][$display_id])) {
- $_SESSION['views'][$this->view->name][$display_id] = array();
- }
- $session = &$_SESSION['views'][$this->view->name][$display_id];
- if ($operator && isset($input[$this->options['expose']['operator_id']])) {
- $session[$this->options['expose']['operator_id']] = $input[$this->options['expose']['operator_id']];
- }
- if (isset($input[$this->options['expose']['identifier']])) {
- $session[$this->options['expose']['identifier']] = $input[$this->options['expose']['identifier']];
- }
- }
- }
- /**
- * Add this filter to the query.
- *
- * Due to the nature of fapi, the value and the operator have an unintended
- * level of indirection. You will find them in $this->operator and
- * $this->value respectively.
- */
- public function query() {
- $this->ensure_my_table();
- $this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field", $this->value, $this->operator);
- }
- /**
- * Can this filter be used in OR groups?
- *
- * Some filters have complicated where clauses that cannot be easily used with
- * OR groups. Some filters must also use HAVING which also makes them not
- * groupable. These filters will end up in a special group if OR grouping is
- * in use.
- *
- * @return bool
- * Whether the filter can be used in OR groups.
- */
- public function can_group() {
- return TRUE;
- }
- }
- /**
- * A special handler to take the place of missing or broken handlers.
- *
- * @ingroup views_filter_handlers
- */
- class views_handler_filter_broken extends views_handler_filter {
- /**
- * {@inheritdoc}
- */
- public function ui_name($short = FALSE) {
- return t('Broken/missing handler');
- }
- /**
- * {@inheritdoc}
- */
- public function ensure_my_table() {
- // No table to ensure!
- }
- /**
- * {@inheritdoc}
- */
- public function query($group_by = FALSE) {
- // No query to run.
- }
- /**
- * {@inheritdoc}
- */
- public function options_form(&$form, &$form_state) {
- $form['markup'] = array(
- '#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>',
- );
- }
- /**
- * {@inheritdoc}
- */
- public function broken() {
- return TRUE;
- }
- }
- /**
- * Filter by no empty values, though allow to use "0".
- *
- * @param string $var
- * The string to check.
- *
- * @return bool
- * Indicates if the argument is an empty string.
- */
- function _views_array_filter_zero($var) {
- return trim($var) != '';
- }
- /**
- * @}
- */
|