Newer
Older
The Great Git Migration
committed
<?php
// $Id$
/**
* @file handlers.inc
* Defines the various handler objects to help build and display views.
*/
/**
* Instantiate and construct a new handler
*/
function _views_create_handler($definition, $type = 'handler') {
// debug('Instantiating handler ' . $definition['handler']);
The Great Git Migration
committed
if (empty($definition['handler'])) {
return;
}
// class_exists will automatically load the code file.
Daniel Wehner
committed
if (!empty($definition['override handler']) &&
!class_exists($definition['override handler'])) {
return;
}
Daniel Wehner
committed
if (!class_exists($definition['handler'])) {
The Great Git Migration
committed
return;
}
Daniel Wehner
committed
Daniel Wehner
committed
if (!empty($definition['override handler'])) {
$handler = new $definition['override handler'];
}
else {
$handler = new $definition['handler'];
}
The Great Git Migration
committed
$handler->set_definition($definition);
// let the handler have something like a constructor.
$handler->construct();
return $handler;
}
/**
* Prepare a handler's data by checking defaults and such.
*/
function _views_prepare_handler($definition, $data, $field) {
foreach (array('group', 'title', 'title short', 'help', 'real field') as $key) {
if (!isset($definition[$key])) {
// First check the field level
if (!empty($data[$field][$key])) {
$definition[$key] = $data[$field][$key];
}
// Then if that doesn't work, check the table level
elseif (!empty($data['table'][$key])) {
The Great Git Migration
committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
$definition[$key] = $data['table'][$key];
}
}
}
return _views_create_handler($definition);
}
/**
* Fetch a handler to join one table to a primary table from the data cache
*/
function views_get_table_join($table, $base_table) {
$data = views_fetch_data($table);
if (isset($data['table']['join'][$base_table])) {
$h = $data['table']['join'][$base_table];
if (!empty($h['handler']) && class_exists($h['handler'])) {
$handler = new $h['handler'];
}
else {
$handler = new views_join();
}
// Fill in some easy defaults
$handler->definition = $h;
if (empty($handler->definition['table'])) {
$handler->definition['table'] = $table;
}
// If this is empty, it's a direct link.
if (empty($handler->definition['left_table'])) {
$handler->definition['left_table'] = $base_table;
}
if (isset($h['arguments'])) {
call_user_func_array(array(&$handler, 'construct'), $h['arguments']);
}
else {
$handler->construct();
}
return $handler;
}
// DEBUG -- identify missing handlers
vpr("Missing join: $table $base_table");
The Great Git Migration
committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
}
/**
* Base handler, from which all the other handlers are derived.
* It creates a common interface to create consistency amongst
* handlers and data.
*
* This class would be abstract in PHP5, but PHP4 doesn't understand that.
*
* Definition terms:
* - table: The actual table this uses; only specify if different from
* the table this is attached to.
* - real field: The actual field this uses; only specify if different from
* the field this item is attached to.
* - group: A text string representing the 'group' this item is attached to,
* for display in the UI. Examples: "Node", "Taxonomy", "Comment",
* "User", etc. This may be inherited from the parent definition or
* the 'table' definition.
* - title: The title for this handler in the UI. This may be inherited from
* the parent definition or the 'table' definition.
* - help: A more informative string to give to the user to explain what this
* field/handler is or does.
* - access callback: If this field should have access control, this could
* be a function to use. 'user_access' is a common
* function to use here. If not specified, no access
* control is provided.
* - access arguments: An array of arguments for the access callback.
*/
class views_handler extends views_object {
/**
* init the handler with necessary data.
* @param $view
* The $view object this handler is attached to.
* @param $options
* The item from the database; the actual contents of this will vary
* based upon the type of handler.
*/
function init(&$view, &$options) {
The Great Git Migration
committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
$this->view = &$view;
$this->unpack_options($this->options, $options);
// This exist on most handlers, but not all. So they are still optional.
if (isset($options['table'])) {
$this->table = $options['table'];
}
if (isset($this->definition['real field'])) {
$this->real_field = $this->definition['real field'];
}
if (isset($this->definition['field'])) {
$this->real_field = $this->definition['field'];
}
if (isset($options['field'])) {
$this->field = $options['field'];
if (!isset($this->real_field)) {
$this->real_field = $options['field'];
}
}
$this->query = &$view->query;
}
Daniel Wehner
committed
function option_definition() {
$options = parent::option_definition();
$options['id'] = array('default' => '');
$options['table'] = array('default' => '');
$options['field'] = array('default' => '');
$options['relationship'] = array('default' => 'none');
Daniel Wehner
committed
$options['group_type'] = array('default' => 'group');
Earl Miles
committed
$options['ui_name'] = array('default' => '');
Daniel Wehner
committed
return $options;
}
The Great Git Migration
committed
/**
* Return a string representing this handler's name in the UI.
*/
function ui_name($short = FALSE) {
Earl Miles
committed
if (!empty($this->options['ui_name'])) {
$title = check_plain($this->options['ui_name']);
return $title;
}
The Great Git Migration
committed
$title = ($short && isset($this->definition['title short'])) ? $this->definition['title short'] : $this->definition['title'];
return t('!group: !title', array('!group' => $this->definition['group'], '!title' => $title));
}
/**
Daniel Wehner
committed
* Shortcut to get a handler's raw field value.
*
* This should be overridden for handlers with formulae or other
* non-standard fields. Because this takes an argument, fields
* overriding this can just call return parent::get_field($formula)
The Great Git Migration
committed
*/
Daniel Wehner
committed
function get_field($field = NULL) {
if (!isset($field)) {
if (!empty($this->formula)) {
$field = $this->get_formula();
}
else {
$field = $this->table_alias . '.' . $this->real_field;
}
}
// If grouping, check to see if the aggregation method needs to modify the field.
if ($this->view->display_handler->use_group_by()) {
$info = $this->query->get_aggregation_info();
if (!empty($info[$this->options['group_type']]['method']) && function_exists($info[$this->options['group_type']]['method'])) {
return $info[$this->options['group_type']]['method']($this->options['group_type'], $field);
}
}
return $field;
}
The Great Git Migration
committed
/**
* Validate the options form.
*/
function options_validate(&$form, &$form_state) { }
The Great Git Migration
committed
Earl Miles
committed
function options_form(&$form, &$form_state) {
$form['ui_name'] = array(
'#type' => 'textfield',
'#title' => t('Administrative Title'),
'#description' => t('This title will be displayed on the views edit page instead of the default one. This might be useful if you have the same item twice.'),
'#default_value' => $this->options['ui_name'],
);
}
The Great Git Migration
committed
/**
* Perform any necessary changes to the form values prior to storage.
* There is no need for this function to actually store the data.
*/
function options_submit(&$form, &$form_state) { }
The Great Git Migration
committed
/**
* Provide a form for groupby settings.
*/
function groupby_form(&$form, &$form_state) {
$view = &$form_state['view'];
$display_id = $form_state['display_id'];
$types = views_object_types();
$type = $form_state['type'];
$id = $form_state['id'];
$form['#title'] = check_plain($view->display[$display_id]->display_title) . ': ';
$form['#title'] .= t('Configure group settings for @type %item', array('@type' => $types[$type]['lstitle'], '%item' => $this->ui_name()));
$form['#section'] = $display_id . '-' . $type . '-' . $id;
$info = $view->query->get_aggregation_info();
foreach ($info as $id => $aggregate) {
$group_types[$id] = $aggregate['title'];
}
$form['group_type'] = array(
'#type' => 'select',
'#title' => t('Group type'),
'#default_value' => $this->options['group_type'],
Daniel Wehner
committed
'#description' => t('Select the grouping function to use on this field.'),
'#options' => $group_types,
);
}
/**
* Perform any necessary changes to the form values prior to storage.
* There is no need for this function to actually store the data.
*/
function groupby_form_submit(&$form, &$form_state) {
$item =& $form_state['handler']->options;
$item['group_type'] = $form_state['values']['group_type'];
}
The Great Git Migration
committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
* If a handler has 'extra options' it will get a little settings widget and
* another form called extra_options.
*/
function has_extra_options() { return FALSE; }
/**
* Provide defaults for the handler.
*/
function extra_options(&$option) { }
/**
* Provide a form for setting options.
*/
function extra_options_form(&$form, &$form_state) { }
/**
* Validate the options form.
*/
function extra_options_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.
*/
function extra_options_submit($form, &$form_state) { }
/**
* Determine if a handler can be exposed.
*/
function can_expose() { return FALSE; }
The Great Git Migration
committed
/**
* Set new exposed option defaults when exposed setting is flipped
* on.
*/
function expose_options() { }
/**
* Get information about the exposed form for the form renderer.
*/
function exposed_info() { }
The Great Git Migration
committed
/**
* Render our chunk of the exposed handler form when selecting
The Great Git Migration
committed
*/
function exposed_form(&$form, &$form_state) { }
/**
* Validate the exposed handler form
The Great Git Migration
committed
*/
function exposed_validate(&$form, &$form_state) { }
/**
* Submit the exposed handler form
The Great Git Migration
committed
*/
function exposed_submit(&$form, &$form_state) { }
/**
* Overridable form for exposed handler options.
The Great Git Migration
committed
*
* If overridden, it is best to call the parent or re-implement
* the stuff here.
*
* Many handlers will need to override this in order to provide options
* that are nicely tailored to the given filter.
The Great Git Migration
committed
*/
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
function expose_form(&$form, &$form_state) {
$form['expose']['start_left'] = array(
'#markup' => '<div class="views-left-50">',
);
$this->expose_form_left($form, $form_state);
$form['expose']['end_left'] = array(
'#markup' => '</div>',
);
$form['expose']['start_checkboxes'] = array(
'#markup' => '<div class="form-checkboxes views-left-40 clearfix">',
);
$this->expose_form_right($form, $form_state);
$form['expose']['end_checkboxes'] = array(
'#markup' => '</div>',
);
}
function expose_form_left(&$form, &$form_state) { }
function expose_form_right(&$form, &$form_state){ }
/**
* Validate the options form.
*/
function expose_validate($form, &$form_state) { }
/**
* Perform any necessary changes to the form exposes prior to storage.
* There is no need for this function to actually store the data.
*/
function expose_submit($form, &$form_state) { }
/**
* Shortcut to display the expose/hide button.
*/
function show_expose_button(&$form, &$form_state) {
$form['expose_button'] = array(
'#prefix' => '<div class="views-expose clearfix">',
'#suffix' => '</div>',
);
if (empty($this->options['exposed'])) {
$form['expose_button']['button'] = array(
'#type' => 'submit',
'#value' => t('Expose'),
'#submit' => array('views_ui_config_item_form_expose'),
);
$form['expose_button']['markup'] = array(
'#markup' => '<div class="description">' . t('This item is currently not exposed. If you <strong>expose</strong> it, users will be able to change the filter as they view it.') . '</div>',
);
}
else {
$form['expose_button']['button'] = array(
'#type' => 'submit',
'#value' => t('Hide'),
'#submit' => array('views_ui_config_item_form_expose'),
);
$form['expose_button']['markup'] = array(
'#markup' => '<div class="description">' . t('This item is currently exposed. If you <strong>hide</strong> it, users will not be able to change the filter as they view it.') . '</div>', );
}
}
/**
* Shortcut to display the exposed options form.
*/
function show_expose_form(&$form, &$form_state) {
if (empty($this->options['exposed'])) {
return;
}
$form['expose'] = array(
'#prefix' => '<div class="views-expose-options clearfix">',
'#suffix' => '</div>',
);
$this->expose_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_expose_options'])) {
foreach (element_children($form['expose']) as $id) {
if (isset($form['expose'][$id]['#default_value']) && !isset($form['expose'][$id]['#value'])) {
$form['expose'][$id]['#value'] = $form['expose'][$id]['#default_value'];
}
}
}
}
The Great Git Migration
committed
/**
* Check whether current user has access to this handler.
*
* @return boolean
*/
function access() {
if (isset($this->definition['access callback']) && function_exists($this->definition['access callback'])) {
if (isset($this->definition['access arguments']) && is_array($this->definition['access arguments'])) {
return call_user_func_array($this->definition['access callback'], $this->definition['access arguments']);
}
return $this->definition['access callback']();
}
return TRUE;
}
/**
* Run before the view is built.
*
* This gives all the handlers some time to set up before any handler has
* been fully run.
*/
function pre_query() { }
Daniel Wehner
committed
/**
* Provides a unique placeholders for handlers.
*/
function placeholder() {
return $this->query->placeholder($this->options['table'] . '_' . $this->options['field']);
}
The Great Git Migration
committed
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/**
* Called just prior to query(), this lets a handler set up any relationship
* it needs.
*/
function set_relationship() {
// Ensure this gets set to something.
$this->relationship = NULL;
// Don't process non-existant relationships.
if (empty($this->options['relationship']) || $this->options['relationship'] == 'none') {
return;
}
$relationship = $this->options['relationship'];
// Ignore missing/broken relationships.
if (empty($this->view->relationship[$relationship])) {
return;
}
// Check to see if the relationship has already processed. If not, then we
// cannot process it.
if (empty($this->view->relationship[$relationship]->alias)) {
return;
}
// Finally!
$this->relationship = $this->view->relationship[$relationship]->alias;
}
/**
* Ensure the main table for this handler is in the query. This is used
* a lot.
*/
function ensure_my_table() {
if (!isset($this->table_alias)) {
if (!method_exists($this->query, 'ensure_table')) { vpr_trace(); exit; }
$this->table_alias = $this->query->ensure_table($this->table, $this->relationship);
}
return $this->table_alias;
}
/**
* Provide text for the administrative summary
*/
function admin_summary() { }
/**
* Determine if the argument needs a style plugin.
*
* @return TRUE/FALSE
*/
function needs_style_plugin() { return FALSE; }
/**
* Determine if this item is 'exposed', meaning it provides form elements
* to let users modify the view.
*
* @return TRUE/FALSE
*/
function is_exposed() {
return !empty($this->options['exposed']);
}
/**
* Take input from exposed handlers and assign to this handler, if necessary.
The Great Git Migration
committed
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
*/
function accept_exposed_input($input) { return TRUE; }
/**
* If set to remember exposed input in the session, store it there.
*/
function store_exposed_input($input, $status) { return TRUE; }
/**
* Get the join object that should be used for this handler.
*
* This method isn't used a great deal, but it's very handy for easily
* getting the join if it is necessary to make some changes to it, such
* as adding an 'extra'.
*/
function get_join() {
// get the join from this table that links back to the base table.
// Determine the primary table to seek
if (empty($this->query->relationships[$this->relationship])) {
$base_table = $this->query->base_table;
}
else {
$base_table = $this->query->relationships[$this->relationship]['base'];
}
$join = views_get_table_join($this->table, $base_table);
if ($join) {
return clone $join;
The Great Git Migration
committed
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
}
}
/**
* Validates the handler against the complete View.
*
* This is called when the complete View is being validated. For validating
* the handler options form use options_validate().
*
* @see views_handler::options_validate()
*
* @return
* Empty array if the handler is valid; an array of error strings if it is not.
*/
function validate() { return array(); }
/**
* Determine if the handler is considered 'broken', meaning it's a
* a placeholder used when a handler can't be found.
*/
function broken() { }
}
/**
* This many to one helper object is used on both arguments and filters.
*
* @todo This requires extensive documentation on how this class is to
* be used. For now, look at the arguments and filters that use it. Lots
* of stuff is just pass-through but there are definitely some interesting
* areas where they interact.
*
* Any handler that uses this can have the following possibly additional
* definition terms:
* - numeric: If true, treat this field as numeric, using %d instead of %s in
* queries.
*
*/
class views_many_to_one_helper {
function views_many_to_one_helper(&$handler) {
$this->handler = &$handler;
}
Daniel Wehner
committed
function option_definition(&$options) {
The Great Git Migration
committed
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
$options['reduce_duplicates'] = array('default' => FALSE);
}
function options_form(&$form, &$form_state) {
$form['reduce_duplicates'] = array(
'#type' => 'checkbox',
'#title' => t('Reduce duplicates'),
'#description' => t('This filter can cause items that have more than one of the selected options to appear as duplicate results. If this filter causes duplicate results to occur, this checkbox can reduce those duplicates; however, the more terms it has to search for, the less performant the query will be, so use this with caution. Shouldn\'t be set on single-value fields, as it may cause values to disappear from display, if used on an incompatible field.'),
'#default_value' => !empty($this->handler->options['reduce_duplicates']),
);
}
/**
* Sometimes the handler might want us to use some kind of formula, so give
* it that option. If it wants us to do this, it must set $helper->formula = TRUE
* and implement handler->get_formula();
*/
function get_field() {
if (!empty($this->formula)) {
return $this->handler->get_formula();
}
else {
return $this->handler->table_alias . '.' . $this->handler->real_field;
}
}
/**
* Add a table to the query.
*
* This is an advanced concept; not only does it add a new instance of the table,
* but it follows the relationship path all the way down to the relationship
* link point and adds *that* as a new relationship and then adds the table to
* the relationship, if necessary.
*/
function add_table($join = NULL, $alias = NULL) {
// This is used for lookups in the many_to_one table.
$field = $this->handler->table . '.' . $this->handler->field;
if (empty($join)) {
$join = $this->get_join();
}
// See if there's a chain between us and the base relationship. If so, we need
// to create a new relationship to use.
$relationship = $this->handler->relationship;
// Determine the primary table to seek
if (empty($this->handler->query->relationships[$relationship])) {
$base_table = $this->handler->query->base_table;
}
else {
$base_table = $this->handler->query->relationships[$relationship]['base'];
}
// Cycle through the joins. This isn't as error-safe as the normal
// ensure_path logic. Perhaps it should be.
$r_join = clone $join;
The Great Git Migration
committed
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
while ($r_join->left_table != $base_table) {
$r_join = views_get_table_join($r_join->left_table, $base_table);
}
// If we found that there are tables in between, add the relationship.
if ($r_join->table != $join->table) {
$relationship = $this->handler->query->add_relationship($this->handler->table . '_' . $r_join->table, $r_join, $r_join->table, $this->handler->relationship);
}
// And now add our table, using the new relationship if one was used.
$alias = $this->handler->query->add_table($this->handler->table, $relationship, $join, $alias);
// Store what values are used by this table chain so that other chains can
// automatically discard those values.
if (empty($this->handler->view->many_to_one_tables[$field])) {
$this->handler->view->many_to_one_tables[$field] = $this->handler->value;
}
else {
$this->handler->view->many_to_one_tables[$field] = array_merge($this->handler->view->many_to_one_tables[$field], $this->handler->value);
}
return $alias;
}
function get_join() {
return $this->handler->get_join();
}
/**
* Provide the proper join for summary queries. This is important in part because
* it will cooperate with other arguments if possible.
*/
function summary_join() {
$field = $this->handler->table . '.' . $this->handler->field;
$join = $this->get_join();
// shortcuts
$options = $this->handler->options;
$view = &$this->handler->view;
$query = &$this->handler->query;
if (!empty($options['require_value'])) {
$join->type = 'INNER';
}
if (empty($options['add_table']) || empty($view->many_to_one_tables[$field])) {
return $query->ensure_table($this->handler->table, $this->handler->relationship, $join);
}
else {
if (!empty($view->many_to_one_tables[$field])) {
foreach ($view->many_to_one_tables[$field] as $value) {
$join->extra = array(
array(
'field' => $this->handler->real_field,
'operator' => '!=',
'value' => $value,
'numeric' => !empty($this->definition['numeric']),
),
);
}
}
return $this->add_table($join);
}
}
/**
* Override ensure_my_table so we can control how this joins in.
* The operator actually has influence over joining.
*/
function ensure_my_table() {
if (!isset($this->handler->table_alias)) {
// For 'or' if we're not reducing duplicates, we get the absolute simplest:
$field = $this->handler->table . '.' . $this->handler->field;
if ($this->handler->operator == 'or' && empty($this->handler->options['reduce_duplicates'])) {
if (empty($this->handler->options['add_table']) && empty($this->handler->view->many_to_one_tables[$field])) {
// query optimization, INNER joins are slightly faster, so use them
// when we know we can.
$join = $this->get_join();
$join->type = 'INNER';
$this->handler->table_alias = $this->handler->query->ensure_table($this->handler->table, $this->handler->relationship, $join);
$this->handler->view->many_to_one_tables[$field] = $this->handler->value;
}
else {
$join = $this->get_join();
$join->type = 'LEFT';
if (!empty($this->handler->view->many_to_one_tables[$field])) {
foreach ($this->handler->view->many_to_one_tables[$field] as $value) {
$join->extra = array(
array(
'field' => $this->handler->real_field,
'operator' => '!=',
'value' => $value,
'numeric' => !empty($this->handler->definition['numeric']),
),
);
}
}
$this->handler->table_alias = $this->add_table($join);
}
return $this->handler->table_alias;
}
if ($this->handler->operator != 'not') {
// If it's an and or an or, we do one join per selected value.
// Clone the join for each table:
$this->handler->table_aliases = array();
foreach ($this->handler->value as $value) {
$join = $this->get_join();
if ($this->handler->operator == 'and') {
$join->type = 'INNER';
}
$join->extra = array(
array(
'field' => $this->handler->real_field,
'value' => $value,
'numeric' => !empty($this->handler->definition['numeric']),
),
);
// The table alias needs to be unique to this value across the
// multiple times the filter or argument is called by the view.
if (!isset($this->handler->view->many_to_one_aliases[$field][$value])) {
if (!isset($this->handler->view->many_to_one_count[$this->handler->table])) {
$this->handler->view->many_to_one_count[$this->handler->table] = 0;
}
$this->handler->view->many_to_one_aliases[$field][$value] = $this->handler->table . '_value_' . ($this->handler->view->many_to_one_count[$this->handler->table]++);
}
$alias = $this->handler->table_aliases[$value] = $this->add_table($join, $this->handler->view->many_to_one_aliases[$field][$value]);
// and set table_alias to the first of these.
if (empty($this->handler->table_alias)) {
$this->handler->table_alias = $alias;
}
}
}
else {
// For not, we just do one join. We'll add a where clause during
// the query phase to ensure that $table.$field IS NULL.
$join = $this->get_join();
$join->type = 'LEFT';
$join->extra = array();
$join->extra_type = 'OR';
foreach ($this->handler->value as $value) {
$join->extra[] = array(
'field' => $this->handler->real_field,
'value' => $value,
'numeric' => !empty($this->handler->definition['numeric']),
);
}
$this->handler->table_alias = $this->add_table($join);
}
}
return $this->handler->table_alias;
}
/**
* Provides a unique placeholders for handlers.
*/
function placeholder() {
return $this->handler->query->placeholder($this->handler->options['table'] . '_' . $this->handler->options['field']);
}
The Great Git Migration
committed
function add_filter() {
if (empty($this->handler->value)) {
return;
}
$this->handler->ensure_my_table();
// Shorten some variables:
$field = $this->get_field();
$options = $this->handler->options;
$operator = $this->handler->operator;
$formula = !empty($this->formula);
Daniel Wehner
committed
$value = $this->handler->value;
The Great Git Migration
committed
if (empty($options['group'])) {
$options['group'] = 0;
}
$add_condition = TRUE;
The Great Git Migration
committed
if ($operator == 'not') {
$value = NULL;
$operator = 'IS NULL';
$add_condition = FALSE;
The Great Git Migration
committed
}
elseif ($operator == 'or' && empty($options['reduce_duplicates'])) {
The Great Git Migration
committed
if (count($this->handler->value) > 1) {
$value = $this->handler->value;
$operator = 'IN';
The Great Git Migration
committed
}
else {
$value = is_array($this->handler->value) ? array_pop($this->handler->value) : $this->handler->value;
$operator = '=';
The Great Git Migration
committed
}
$add_condition = FALSE;
}
Daniel Wehner
committed
if (!$add_condition) {
if ($formula) {
$placeholder = $this->placeholder();
if ($operator == 'IN') {
$operator = "$operator IN($placeholder)";
}
else {
$operator = "$operator $placeholder";
}
$placeholders = array(
$placeholder => $value,
) + $this->placeholders;
$this->handler->query->add_where_expression($options['group'], "$field $operator", $placeholders);
}
else {
Daniel Wehner
committed
$this->handler->query->add_where($options['group'], $field, $value, $operator);
}
}
if ($add_condition) {
The Great Git Migration
committed
$field = $this->handler->real_field;
$clause = $operator == 'or' ? db_or() : db_and();
The Great Git Migration
committed
foreach ($this->handler->table_aliases as $value => $alias) {
$clause->condition("$alias.$field", $this->handler->value);
The Great Git Migration
committed
}
// implode on either AND or OR.
$this->handler->query->add_where($options['group'], $clause);
The Great Git Migration
committed
}
}
}
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
/*
* Break x,y,z and x+y+z into an array. Numeric only.
*
* @param $str
* The string to parse.
* @param $handler
* The handler object to use as a base.
*
* @return $handler
* The new handler object.
*/
function views_break_phrase_string($str, &$handler) {
if (!$handler) {
$handler = new stdClass();
}
// Set up defaults:
if (!isset($handler->value)) {
$handler->value = array();
}
if (!isset($handler->operator)) {
$handler->operator = 'or';
}
if ($str == '') {
return $handler;
}
if (preg_match('/^(\w+[+ ])+\w+$/', $str)) {
// The '+' character in a query string may be parsed as ' '.
$handler->operator = 'or';
$handler->value = preg_split('/[+ ]/', $str);
}
else if (preg_match('/^(\w+,)*\w+$/', $str)) {
$handler->operator = 'and';
$handler->value = explode(',', $str);
}
// Keep an 'error' value if invalid strings were given.
if (!empty($str) && (empty($handler->value) || !is_array($handler->value))) {
$handler->value = array(-1);
return $handler;
}
// Doubly ensure that all values are strings only.
foreach ($handler->value as $id => $value) {
$handler->value[$id] = (string) $value;
}
return $handler;
}
The Great Git Migration
committed
/*
* Break x,y,z and x+y+z into an array. Numeric only.
*
* @param $str
* The string to parse.
* @param $filter
* The filter object to use as a base. If not specified one will
* be created.
*
* @return $filter
* The new filter object.
*/
function views_break_phrase($str, $handler = NULL) {
if (!$handler) {
$handler = new stdClass();
The Great Git Migration
committed
}
// Set up defaults:
if (!isset($handler->value)) {
$handler->value = array();
The Great Git Migration
committed
}
if (!isset($handler->operator)) {
$handler->operator = 'or';
The Great Git Migration
committed
}
if (empty($str)) {
return $handler;
The Great Git Migration
committed
}
if (preg_match('/^([0-9]+[+ ])+[0-9]+$/', $str)) {
// The '+' character in a query string may be parsed as ' '.
$handler->operator = 'or';
$handler->value = preg_split('/[+ ]/', $str);
The Great Git Migration
committed
}
elseif (preg_match('/^([0-9]+,)*[0-9]+$/', $str)) {
$handler->operator = 'and';
$handler->value = explode(',', $str);
The Great Git Migration
committed
}
// Keep an 'error' value if invalid strings were given.
if (!empty($str) && (empty($handler->value) || !is_array($handler->value))) {
$handler->value = array(-1);
return $handler;
The Great Git Migration
committed
}
// Doubly ensure that all values are numeric only.
foreach ($handler->value as $id => $value) {
$handler->value[$id] = intval($value);
The Great Git Migration
committed
}
return $handler;
The Great Git Migration
committed
}
// --------------------------------------------------------------------------
// Date helper functions
/**
* Figure out what timezone we're in; needed for some date manipulations.
*/
function views_get_timezone() {