5 * Defines the various handler objects to help build and display views.
9 * Instantiate and construct a new handler
11 function _views_create_handler($definition, $type = 'handler') {
12 // vpr('Instantiating handler ' . $definition['handler']);
13 if (empty($definition['handler'])) {
17 if (!class_exists($definition['handler']) && !views_include_handler($definition, $type)) {
21 $handler = new
$definition['handler'];
22 $handler->set_definition($definition);
23 // let the handler have something like a constructor.
24 $handler->construct();
30 * Attempt to find the include file for a given handler from its definition.
32 * This will also attempt to include all parents, though we're maxing the
33 * parent chain to 10 to prevent infinite loops.
35 function views_include_handler($definition, $type, $count = 0) {
36 // Do not proceed if the class already exists.
37 if (isset($definition['handler']) && class_exists($definition['handler'])) {
41 // simple infinite loop prevention.
43 vpr(t('Handler @handler include tried to loop infinitely!', array('@handler' => $definition['handler'])));
47 if (!isset($definition['path'])) {
48 if ($type == 'handler') {
49 $definition += views_fetch_handler_data($definition['handler']);
52 $definition += views_fetch_plugin_data($type, $definition['handler']);
56 if (!empty($definition['parent'])) {
57 if ($type == 'handler') {
58 $parent = views_fetch_handler_data($definition['parent']);
61 $parent = views_fetch_plugin_data($type, $definition['parent']);
65 $rc = views_include_handler($parent, $type, $count + 1);
66 // If the parent chain cannot be included, don't try; this will
67 // help alleviate problems with modules with cross dependencies.
74 if (isset($definition['path']) && $definition['file']) {
75 $filename = './' .
$definition['path'] .
'/' .
$definition['file'];
76 if (file_exists($filename)) {
77 require_once
$filename;
81 return class_exists($definition['handler']);
85 * Prepare a handler's data by checking defaults and such.
87 function _views_prepare_handler($definition, $data, $field) {
88 foreach (array('group', 'title', 'title short', 'help', 'real field') as
$key) {
89 if (!isset($definition[$key])) {
90 // First check the field level
91 if (!empty($data[$field][$key])) {
92 $definition[$key] = $data[$field][$key];
94 // Then if that doesn't work, check the table level
95 else if (!empty($data['table'][$key])) {
96 $definition[$key] = $data['table'][$key];
101 return _views_create_handler($definition);
105 * Fetch the handler data from cache.
107 function views_fetch_handler_data($handler = NULL
) {
108 static
$cache = NULL
;
109 if (!isset($cache)) {
110 $start = views_microtime();
112 $cache = views_discover_handlers();
114 vpr('Views handlers build time: ' .
(views_microtime() - $start) * 1000 .
' ms');
120 else if (isset($cache[$handler])) {
121 return $cache[$handler];
124 // Return an empty array if there is no match.
129 * Builds and return a list of all handlers available in the system.
131 * @return Nested array of handlers
133 function views_discover_handlers() {
135 // Get handlers from all modules.
136 foreach (module_implements('views_handlers') as
$module) {
137 $function = $module .
'_views_handlers';
138 $result = $function();
139 if (!is_array($result)) {
143 $module_dir = isset($result['info']['module']) ?
$result['info']['module'] : $module;
144 $path = isset($result['info']['path']) ?
$result['info']['path'] : drupal_get_path('module', $module_dir);
146 foreach ($result['handlers'] as
$handler => $def) {
147 if (!isset($def['module'])) {
148 $def['module'] = $module_dir;
150 if (!isset($def['path'])) {
151 $def['path'] = $path;
153 if (!isset($def['file'])) {
154 $def['file'] = "$handler.inc";
156 if (!isset($def['handler'])) {
157 $def['handler'] = $handler;
159 // merge the new data in
160 $cache[$handler] = $def;
167 * Fetch a handler to join one table to a primary table from the data cache
169 function views_get_table_join($table, $base_table) {
170 $data = views_fetch_data($table);
171 if (isset($data['table']['join'][$base_table])) {
172 $h = $data['table']['join'][$base_table];
173 if (!empty($h['handler']) && class_exists($h['handler'])) {
174 $handler = new
$h['handler'];
177 $handler = new
views_join();
180 // Fill in some easy defaults
181 $handler->definition
= $h;
182 if (empty($handler->definition
['table'])) {
183 $handler->definition
['table'] = $table;
185 // If this is empty, it's a direct link.
186 if (empty($handler->definition
['left_table'])) {
187 $handler->definition
['left_table'] = $base_table;
190 if (isset($h['arguments'])) {
191 call_user_func_array(array(&$handler, 'construct'), $h['arguments']);
194 $handler->construct();
199 // DEBUG -- identify missing handlers
200 vpr("Missing join: $table $base_table");
204 * Base handler, from which all the other handlers are derived.
205 * It creates a common interface to create consistency amongst
208 * This class would be abstract in PHP5, but PHP4 doesn't understand that.
211 * - table: The actual table this uses; only specify if different from
212 * the table this is attached to.
213 * - real field: The actual field this uses; only specify if different from
214 * the field this item is attached to.
215 * - group: A text string representing the 'group' this item is attached to,
216 * for display in the UI. Examples: "Node", "Taxonomy", "Comment",
217 * "User", etc. This may be inherited from the parent definition or
218 * the 'table' definition.
219 * - title: The title for this handler in the UI. This may be inherited from
220 * the parent definition or the 'table' definition.
221 * - help: A more informative string to give to the user to explain what this
222 * field/handler is or does.
223 * - access callback: If this field should have access control, this could
224 * be a function to use. 'user_access' is a common
225 * function to use here. If not specified, no access
226 * control is provided.
227 * - access arguments: An array of arguments for the access callback.
229 class views_handler
extends views_object
{
231 * init the handler with necessary data.
233 * The $view object this handler is attached to.
235 * The item from the database; the actual contents of this will vary
236 * based upon the type of handler.
238 function init(&$view, $options) {
239 $this->view
= &$view;
240 $this->unpack_options($this->options
, $options);
242 // This exist on most handlers, but not all. So they are still optional.
243 if (isset($options['table'])) {
244 $this->table
= $options['table'];
247 if (isset($this->definition
['real field'])) {
248 $this->real_field
= $this->definition
['real field'];
251 if (isset($this->definition
['field'])) {
252 $this->real_field
= $this->definition
['field'];
255 if (isset($options['field'])) {
256 $this->field
= $options['field'];
257 if (!isset($this->real_field
)) {
258 $this->real_field
= $options['field'];
262 $this->query
= &$view->query
;
266 * Return a string representing this handler's name in the UI.
268 function ui_name($short = FALSE
) {
269 $title = ($short && isset($this->definition
['title short'])) ?
$this->definition
['title short'] : $this->definition
['title'];
270 return t('!group: !title', array('!group' => $this->definition
['group'], '!title' => $title));
274 * Provide a form for setting options.
276 function options_form(&$form, &$form_state) { }
279 * Validate the options form.
281 function options_validate($form, &$form_state) { }
284 * Perform any necessary changes to the form values prior to storage.
285 * There is no need for this function to actually store the data.
287 function options_submit($form, &$form_state) { }
290 * If a handler has 'extra options' it will get a little settings widget and
291 * another form called extra_options.
293 function has_extra_options() { return FALSE
; }
296 * Provide defaults for the handler.
298 function extra_options(&$option) { }
301 * Provide a form for setting options.
303 function extra_options_form(&$form, &$form_state) { }
306 * Validate the options form.
308 function extra_options_validate($form, &$form_state) { }
311 * Perform any necessary changes to the form values prior to storage.
312 * There is no need for this function to actually store the data.
314 function extra_options_submit($form, &$form_state) { }
317 * Set new exposed option defaults when exposed setting is flipped
320 function expose_options() { }
322 * Render our chunk of the exposed filter form when selecting
324 function exposed_form(&$form, &$form_state) { }
327 * Validate the exposed filter form
329 function exposed_validate(&$form, &$form_state) { }
332 * Submit the exposed filter form
334 function exposed_submit(&$form, &$form_state) { }
337 * Get information about the exposed form for the form renderer.
340 * An array with the following keys:
341 * - operator: The $form key of the operator. Set to NULL if no operator.
342 * - value: The $form key of the value. Set to NULL if no value.
343 * - label: The label to use for this piece.
345 function exposed_info() { }
348 * Check whether current user has access to this handler.
353 if (isset($this->definition
['access callback']) && function_exists($this->definition
['access callback'])) {
354 if (isset($this->definition
['access arguments']) && is_array($this->definition
['access arguments'])) {
355 return call_user_func_array($this->definition
['access callback'], $this->definition
['access arguments']);
357 return $this->definition
['access callback']();
364 * Run before the view is built.
366 * This gives all the handlers some time to set up before any handler has
369 function pre_query() { }
372 * Called just prior to query(), this lets a handler set up any relationship
375 function set_relationship() {
376 // Ensure this gets set to something.
377 $this->relationship
= NULL
;
379 // Don't process non-existant relationships.
380 if (empty($this->options
['relationship']) || $this->options
['relationship'] == 'none') {
384 $relationship = $this->options
['relationship'];
386 // Ignore missing/broken relationships.
387 if (empty($this->view
->relationship
[$relationship])) {
391 // Check to see if the relationship has already processed. If not, then we
392 // cannot process it.
393 if (empty($this->view
->relationship
[$relationship]->alias
)) {
398 $this->relationship
= $this->view
->relationship
[$relationship]->alias
;
402 * Add this handler into the query.
404 * If we were using PHP5, this would be abstract.
409 * Ensure the main table for this handler is in the query. This is used
412 function ensure_my_table() {
413 if (!isset($this->table_alias
)) {
414 if (!method_exists($this->query
, 'ensure_table')) { vpr_trace(); exit; }
415 $this->table_alias
= $this->query
->ensure_table($this->table
, $this->relationship
);
417 return $this->table_alias
;
421 * Provide text for the administrative summary
423 function admin_summary() { }
426 * Determine if the argument needs a style plugin.
430 function needs_style_plugin() { return FALSE
; }
433 * Determine if this item is 'exposed', meaning it provides form elements
434 * to let users modify the view.
438 function is_exposed() {
439 return !empty($this->options
['exposed']);
443 * Take input from exposed filters and assign to this handler, if necessary.
445 function accept_exposed_input($input) { return TRUE
; }
448 * If set to remember exposed input in the session, store it there.
450 function store_exposed_input($input, $status) { return TRUE
; }
453 * Get the join object that should be used for this handler.
455 * This method isn't used a great deal, but it's very handy for easily
456 * getting the join if it is necessary to make some changes to it, such
457 * as adding an 'extra'.
459 function get_join() {
460 // get the join from this table that links back to the base table.
461 // Determine the primary table to seek
462 if (empty($this->query
->relationships
[$this->relationship
])) {
463 $base_table = $this->query
->base_table
;
466 $base_table = $this->query
->relationships
[$this->relationship
]['base'];
469 $join = views_get_table_join($this->table
, $base_table);
471 return drupal_clone($join);
476 * Validates the handler against the complete View.
478 * This is called when the complete View is being validated. For validating
479 * the handler options form use options_validate().
481 * @see views_handler::options_validate()
484 * Empty array if the handler is valid; an array of error strings if it is not.
486 function validate() { return array(); }
489 * Determine if the handler is considered 'broken', meaning it's a
490 * a placeholder used when a handler can't be found.
492 function broken() { }
496 * This many to one helper object is used on both arguments and filters.
498 * @todo This requires extensive documentation on how this class is to
499 * be used. For now, look at the arguments and filters that use it. Lots
500 * of stuff is just pass-through but there are definitely some interesting
501 * areas where they interact.
503 * Any handler that uses this can have the following possibly additional
505 * - numeric: If true, treat this field as numeric, using %d instead of %s in
509 class views_many_to_one_helper
{
510 function views_many_to_one_helper(&$handler) {
511 $this->handler
= &$handler;
514 function option_definition(&$options) {
515 $options['reduce_duplicates'] = array('default' => FALSE
);
518 function options_form(&$form, &$form_state) {
519 $form['reduce_duplicates'] = array(
520 '#type' => 'checkbox',
521 '#title' => t('Reduce duplicates'),
522 '#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.'),
523 '#default_value' => !empty($this->handler
->options
['reduce_duplicates']),
528 * Sometimes the handler might want us to use some kind of formula, so give
529 * it that option. If it wants us to do this, it must set $helper->formula = TRUE
530 * and implement handler->get_formula();
532 function get_field() {
533 if (!empty($this->formula
)) {
534 return $this->handler
->get_formula();
537 return $this->handler
->table_alias .
'.' .
$this->handler
->real_field
;
542 * Add a table to the query.
544 * This is an advanced concept; not only does it add a new instance of the table,
545 * but it follows the relationship path all the way down to the relationship
546 * link point and adds *that* as a new relationship and then adds the table to
547 * the relationship, if necessary.
549 function add_table($join = NULL
, $alias = NULL
) {
550 // This is used for lookups in the many_to_one table.
551 $field = $this->handler
->table .
'.' .
$this->handler
->field
;
554 $join = $this->get_join();
557 // See if there's a chain between us and the base relationship. If so, we need
558 // to create a new relationship to use.
559 $relationship = $this->handler
->relationship
;
561 // Determine the primary table to seek
562 if (empty($this->handler
->query
->relationships
[$relationship])) {
563 $base_table = $this->handler
->query
->base_table
;
566 $base_table = $this->handler
->query
->relationships
[$relationship]['base'];
569 // Cycle through the joins. This isn't as error-safe as the normal
570 // ensure_path logic. Perhaps it should be.
571 $r_join = drupal_clone($join);
572 while ($r_join->left_table
!= $base_table) {
573 $r_join = views_get_table_join($r_join->left_table
, $base_table);
575 // If we found that there are tables in between, add the relationship.
576 if ($r_join->table
!= $join->table
) {
577 $relationship = $this->handler
->query
->add_relationship($this->handler
->table .
'_' .
$r_join->table
, $r_join, $r_join->table
, $this->handler
->relationship
);
580 // And now add our table, using the new relationship if one was used.
581 $alias = $this->handler
->query
->add_table($this->handler
->table
, $relationship, $join, $alias);
583 // Store what values are used by this table chain so that other chains can
584 // automatically discard those values.
585 if (empty($this->handler
->view
->many_to_one_tables
[$field])) {
586 $this->handler
->view
->many_to_one_tables
[$field] = $this->handler
->value
;
589 $this->handler
->view
->many_to_one_tables
[$field] = array_merge($this->handler
->view
->many_to_one_tables
[$field], $this->handler
->value
);
595 function get_join() {
596 return $this->handler
->get_join();
600 * Provide the proper join for summary queries. This is important in part because
601 * it will cooperate with other arguments if possible.
603 function summary_join() {
604 $field = $this->handler
->table .
'.' .
$this->handler
->field
;
605 $join = $this->get_join();
608 $options = $this->handler
->options
;
609 $view = &$this->handler
->view
;
610 $query = &$this->handler
->query
;
612 if (!empty($options['require_value'])) {
613 $join->type
= 'INNER';
616 if (empty($options['add_table']) || empty($view->many_to_one_tables
[$field])) {
617 return $query->ensure_table($this->handler
->table
, $this->handler
->relationship
, $join);
620 if (!empty($view->many_to_one_tables
[$field])) {
621 foreach ($view->many_to_one_tables
[$field] as
$value) {
622 $join->extra
= array(
624 'field' => $this->handler
->real_field
,
627 'numeric' => !empty($this->definition
['numeric']),
632 return $this->add_table($join);
637 * Override ensure_my_table so we can control how this joins in.
638 * The operator actually has influence over joining.
640 function ensure_my_table() {
641 if (!isset($this->handler
->table_alias
)) {
642 // For 'or' if we're not reducing duplicates, we get the absolute simplest:
643 $field = $this->handler
->table .
'.' .
$this->handler
->field
;
644 if ($this->handler
->operator
== 'or' && empty($this->handler
->options
['reduce_duplicates'])) {
645 if (empty($this->handler
->options
['add_table']) && empty($this->handler
->view
->many_to_one_tables
[$field])) {
646 // query optimization, INNER joins are slightly faster, so use them
647 // when we know we can.
648 $join = $this->get_join();
649 $join->type
= 'INNER';
650 $this->handler
->table_alias
= $this->handler
->query
->ensure_table($this->handler
->table
, $this->handler
->relationship
, $join);
651 $this->handler
->view
->many_to_one_tables
[$field] = $this->handler
->value
;
654 $join = $this->get_join();
655 $join->type
= 'LEFT';
656 if (!empty($this->handler
->view
->many_to_one_tables
[$field])) {
657 foreach ($this->handler
->view
->many_to_one_tables
[$field] as
$value) {
658 $join->extra
= array(
660 'field' => $this->handler
->real_field
,
663 'numeric' => !empty($this->handler
->definition
['numeric']),
669 $this->handler
->table_alias
= $this->add_table($join);
672 return $this->handler
->table_alias
;
675 if ($this->handler
->operator
!= 'not') {
676 // If it's an and or an or, we do one join per selected value.
677 // Clone the join for each table:
678 $this->handler
->table_aliases
= array();
679 foreach ($this->handler
->value as
$value) {
680 $join = $this->get_join();
681 if ($this->handler
->operator
== 'and') {
682 $join->type
= 'INNER';
684 $join->extra
= array(
686 'field' => $this->handler
->real_field
,
688 'numeric' => !empty($this->handler
->definition
['numeric']),
692 // The table alias needs to be unique to this value across the
693 // multiple times the filter or argument is called by the view.
694 if (!isset($this->handler
->view
->many_to_one_aliases
[$field][$value])) {
695 if (!isset($this->handler
->view
->many_to_one_count
[$this->handler
->table
])) {
696 $this->handler
->view
->many_to_one_count
[$this->handler
->table
] = 0;
698 $this->handler
->view
->many_to_one_aliases
[$field][$value] = $this->handler
->table .
'_value_' .
($this->handler
->view
->many_to_one_count
[$this->handler
->table
]++);
700 $alias = $this->handler
->table_aliases
[$value] = $this->add_table($join, $this->handler
->view
->many_to_one_aliases
[$field][$value]);
702 // and set table_alias to the first of these.
703 if (empty($this->handler
->table_alias
)) {
704 $this->handler
->table_alias
= $alias;
709 // For not, we just do one join. We'll add a where clause during
710 // the query phase to ensure that $table.$field IS NULL.
711 $join = $this->get_join();
712 $join->type
= 'LEFT';
713 $join->extra
= array();
714 $join->extra_type
= 'OR';
715 foreach ($this->handler
->value as
$value) {
716 $join->extra
[] = array(
717 'field' => $this->handler
->real_field
,
719 'numeric' => !empty($this->handler
->definition
['numeric']),
723 $this->handler
->table_alias
= $this->add_table($join);
726 return $this->handler
->table_alias
;
729 function add_filter() {
730 if (empty($this->handler
->value
)) {
733 $this->handler
->ensure_my_table();
735 // Shorten some variables:
736 $field = $this->get_field();
737 $options = $this->handler
->options
;
738 $operator = $this->handler
->operator
;
739 if (empty($options['group'])) {
740 $options['group'] = 0;
743 $placeholder = !empty($this->handler
->definition
['numeric']) ?
'%d' : "'%s'";
745 if ($operator == 'not') {
746 $this->handler
->query
->add_where($options['group'], "$field IS NULL");
748 else if ($operator == 'or' && empty($options['reduce_duplicates'])) {
749 if (count($this->handler
->value
) > 1) {
750 $replace = array_fill(0, sizeof($this->handler
->value
), $placeholder);
751 $in = '(' .
implode(", ", $replace) .
')';
752 $this->handler
->query
->add_where($options['group'], "$field IN $in", $this->handler
->value
);
755 $this->handler
->query
->add_where($options['group'], "$field = $placeholder", $this->handler
->value
);
759 $field = $this->handler
->real_field
;
761 foreach ($this->handler
->table_aliases as
$value => $alias) {
762 $clauses[] = "$alias.$field = $placeholder";
765 $group = empty($options['group']) ?
0 : $options['group'];
767 // implode on either AND or OR.
768 $this->handler
->query
->add_where($group, implode(' ' .
strtoupper($operator) .
' ', $clauses), $this->handler
->value
);
774 * Break x,y,z and x+y+z into an array. Numeric only.
777 * The string to parse.
779 * The filter object to use as a base. If not specified one will
783 * The new filter object.
785 function views_break_phrase($str, $filter = NULL
) {
787 $filter = new
stdClass();
792 if (!isset($filter->value
)) {
793 $filter->value
= array();
796 if (!isset($filter->operator
)) {
797 $filter->operator
= 'or';
804 if (preg_match('/^([0-9]+[+ ])+[0-9]+$/', $str)) {
805 // The '+' character in a query string may be parsed as ' '.
806 $filter->operator
= 'or';
807 $filter->value
= preg_split('/[+ ]/', $str);
809 else if (preg_match('/^([0-9]+,)*[0-9]+$/', $str)) {
810 $filter->operator
= 'and';
811 $filter->value
= explode(',', $str);
814 // Keep an 'error' value if invalid strings were given.
815 if (!empty($str) && (empty($filter->value
) || !is_array($filter->value
))) {
816 $filter->value
= array(-1);
820 // Doubly ensure that all values are numeric only.
821 foreach ($filter->value as
$id => $value) {
822 $filter->value
[$id] = intval($value);
828 // --------------------------------------------------------------------------
829 // Date helper functions
832 * Figure out what timezone we're in; needed for some date manipulations.
834 function views_get_timezone() {
836 if (variable_get('configurable_timezones', 1) && $user->uid
&& strlen($user->timezone
)) {
837 $timezone = $user->timezone
;
840 $timezone = variable_get('date_default_timezone', 0);
843 // set up the database timezone
844 if (in_array($GLOBALS['db_type'], array('mysql', 'mysqli', 'pgsql'))) {
846 static
$already_set = false
;
848 if ($GLOBALS['db_type'] == 'pgsql') {
849 db_query("SET TIME ZONE INTERVAL '$offset' HOUR TO MINUTE");
851 elseif ($GLOBALS['db_type'] == 'mysqli' || version_compare(mysql_get_server_info(), '4.1.3', '>=')) {
852 db_query("SET @@session.time_zone = '$offset'");
863 * Helper function to create cross-database SQL dates.
866 * The real table and field name, like 'tablename.fieldname'.
868 * The type of date field, 'int' or 'datetime'.
870 * The name of a field that holds the timezone offset or a fixed timezone
871 * offset value. If not provided, the normal Drupal timezone handling
872 * will be used, i.e. $set_offset = 0 will make no timezone adjustment.
874 * An appropriate SQL string for the db type and field type.
876 function views_date_sql_field($field, $field_type = 'int', $set_offset = NULL
) {
877 $db_type = $GLOBALS['db_type'];
878 $offset = $set_offset !== NULL ?
$set_offset : views_get_timezone();
882 switch ($field_type) {
884 $field = "FROM_UNIXTIME($field)";
889 if (!empty($offset)) {
890 $field = "($field + INTERVAL $offset SECOND)";
894 switch ($field_type) {
896 $field = "$field::ABSTIME";
901 if (!empty($offset)) {
902 $field = "($field + INTERVAL '$offset SECONDS')";
909 * Helper function to create cross-database SQL date formatting.
912 * A format string for the result, like 'Y-m-d H:i:s'.
914 * The real table and field name, like 'tablename.fieldname'.
916 * The type of date field, 'int' or 'datetime'.
918 * The name of a field that holds the timezone offset or a fixed timezone
919 * offset value. If not provided, the normal Drupal timezone handling
920 * will be used, i.e. $set_offset = 0 will make no timezone adjustment.
922 * An appropriate SQL string for the db type and field type.
924 function views_date_sql_format($format, $field, $field_type = 'int', $set_offset = NULL
) {
925 $db_type = $GLOBALS['db_type'];
926 $field = views_date_sql_field($field, $field_type, $set_offset);
938 $format = strtr($format, $replace);
939 return "DATE_FORMAT($field, '$format')";
949 $format = strtr($format, $replace);
950 return "TO_CHAR($field, '$format')";
955 * Helper function to create cross-database SQL date extraction.
957 * @param $extract_type
958 * The type of value to extract from the date, like 'MONTH'.
960 * The real table and field name, like 'tablename.fieldname'.
962 * The type of date field, 'int' or 'datetime'.
964 * The name of a field that holds the timezone offset or a fixed timezone
965 * offset value. If not provided, the normal Drupal timezone handling
966 * will be used, i.e. $set_offset = 0 will make no timezone adjustment.
968 * An appropriate SQL string for the db type and field type.
970 function views_date_sql_extract($extract_type, $field, $field_type = 'int', $set_offset = NULL
) {
971 $db_type = $GLOBALS['db_type'];
972 $field = views_date_sql_field($field, $field_type, $set_offset);
974 // Note there is no space after FROM to avoid db_rewrite problems
975 // see http://drupal.org/node/79904.
976 switch ($extract_type) {
980 return "EXTRACT(YEAR FROM($field))";
982 return "EXTRACT(MONTH FROM($field))";
984 return "EXTRACT(DAY FROM($field))";
986 return "EXTRACT(HOUR FROM($field))";
988 return "EXTRACT(MINUTE FROM($field))";
990 return "EXTRACT(SECOND FROM($field))";
991 case('WEEK'): // ISO week number for date
995 // WEEK using arg 3 in mysql should return the same value as postgres EXTRACT
996 return "WEEK($field, 3)";
998 return "EXTRACT(WEEK FROM($field))";
1004 // mysql returns 1 for Sunday through 7 for Saturday
1005 // php date functions and postgres use 0 for Sunday and 6 for Saturday
1006 return "INTEGER(DAYOFWEEK($field) - 1)";
1008 return "EXTRACT(DOW FROM($field))";
1014 return "DAYOFYEAR($field)";
1016 return "EXTRACT(DOY FROM($field))";
1022 * Implementation of hook_views_handlers() to register all of the basic handlers
1025 function views_views_handlers() {
1028 'path' => drupal_get_path('module', 'views') .
'/handlers',
1030 'handlers' => array(
1031 // argument handlers
1032 'views_handler_argument' => array(
1033 'parent' => 'views_handler',
1035 'views_handler_argument_numeric' => array(
1036 'parent' => 'views_handler_argument',
1038 'views_handler_argument_formula' => array(
1039 'parent' => 'views_handler_argument',
1041 'views_handler_argument_date' => array(
1042 'parent' => 'views_handler_argument_formula',
1044 'views_handler_argument_string' => array(
1045 'parent' => 'views_handler_argument',
1047 'views_handler_argument_many_to_one' => array(
1048 'parent' => 'views_handler_argument',
1050 'views_handler_argument_null' => array(
1051 'parent' => 'views_handler_argument',
1055 'views_handler_field' => array(
1056 'parent' => 'views_handler',
1058 'views_handler_field_date' => array(
1059 'parent' => 'views_handler_field',
1061 'views_handler_field_boolean' => array(
1062 'parent' => 'views_handler_field',
1064 'views_handler_field_markup' => array(
1065 'parent' => 'views_handler_field',
1067 'views_handler_field_xss' => array(
1068 'parent' => 'views_handler_field',
1069 'file' => 'views_handler_field.inc',
1071 'views_handler_field_url' => array(
1072 'parent' => 'views_handler_field',
1074 'views_handler_field_file_size' => array(
1075 'parent' => 'views_handler_field',
1076 'file' => 'views_handler_field.inc',
1078 'views_handler_field_prerender_list' => array(
1079 'parent' => 'views_handler_field',
1081 'views_handler_field_numeric' => array(
1082 'parent' => 'views_handler_field',
1084 'views_handler_field_custom' => array(
1085 'parent' => 'views_handler_field',
1087 'views_handler_field_counter' => array(
1088 'parent' => 'views_handler_field',
1092 'views_handler_filter' => array(
1093 'parent' => 'views_handler',
1095 'views_handler_filter_equality' => array(
1096 'parent' => 'views_handler_filter',
1098 'views_handler_filter_string' => array(
1099 'parent' => 'views_handler_filter',
1101 'views_handler_filter_boolean_operator' => array(
1102 'parent' => 'views_handler_filter',
1104 'views_handler_filter_boolean_operator_string' => array(
1105 'parent' => 'views_handler_filter_boolean_operator',
1107 'views_handler_filter_in_operator' => array(
1108 'parent' => 'views_handler_filter',
1110 'views_handler_filter_numeric' => array(
1111 'parent' => 'views_handler_filter',
1113 'views_handler_filter_float' => array(
1114 'parent' => 'views_handler_filter_numeric',
1116 'views_handler_filter_date' => array(
1117 'parent' => 'views_handler_filter_numeric',
1119 'views_handler_filter_many_to_one' => array(
1120 'parent' => 'views_handler_filter_in_operator',
1123 // relationship handlers
1124 'views_handler_relationship' => array(
1125 'parent' => 'views_handler',
1130 'views_handler_sort' => array(
1131 'parent' => 'views_handler',
1133 'views_handler_sort_formula' => array(
1134 'parent' => 'views_handler_sort',
1136 'views_handler_sort_date' => array(
1137 'parent' => 'views_handler_sort',
1139 'views_handler_sort_menu_hierarchy' => array(
1140 'parent' => 'views_handler_sort',
1142 'views_handler_sort_random' => array(
1143 'parent' => 'views_handler_sort',
1155 * @defgroup views_join_handlers Views' join handlers
1157 * Handlers to tell Views how to join tables together.
1159 * Here is how you do complex joins:
1162 * class views_join_complex extends views_join {
1163 * // PHP 4 doesn't call constructors of the base class automatically from a
1164 * // constructor of a derived class. It is your responsibility to propagate
1165 * // the call to constructors upstream where appropriate.
1166 * function construct($table, $left_table, $left_field, $field, $extra = array(), $type = 'LEFT') {
1167 * parent::construct($table, $left_table, $left_field, $field, $extra, $type);
1170 * function join($table, &$query) {
1171 * $output = parent::join($table, $query);
1173 * $output .= "AND foo.bar = baz.boing";
1179 * A function class to represent a join and create the SQL necessary
1180 * to implement the join.
1182 * This is the Delegation pattern. If we had PHP5 exclusively, we would
1183 * declare this an interface.
1185 * Extensions of this class can be used to create more interesting joins.
1188 * - table: table to join (right table)
1189 * - field: field to join on (right field)
1190 * - left_table: The table we join to
1191 * - left_field: The field we join to
1192 * - type: either LEFT (default) or INNER
1193 * - extra: Either a string that's directly added, or an array of items:
1194 * - - table: if not set, current table; if NULL, no table. This field can't
1195 * be set in the cached definition because it can't know aliases; this field
1196 * can only be used by realtime joins.
1197 * - - field: Field or formula
1198 * - - operator: defaults to =
1199 * - - value: Must be set. If an array, operator will be defaulted to IN.
1200 * - - numeric: If true, the value will not be surrounded in quotes.
1201 * - extra type: How all the extras will be combined. Either AND or OR. Defaults to AND.
1205 * Construct the views_join object.
1207 function construct($table = NULL
, $left_table = NULL
, $left_field = NULL
, $field = NULL
, $extra = array(), $type = 'LEFT') {
1208 $this->extra_type
= 'AND';
1209 if (!empty($table)) {
1210 $this->table
= $table;
1211 $this->left_table
= $left_table;
1212 $this->left_field
= $left_field;
1213 $this->field
= $field;
1214 $this->extra
= $extra;
1215 $this->type
= strtoupper($type);
1217 else if (!empty($this->definition
)) {
1218 // if no arguments, construct from definition.
1219 // These four must exist or it will throw notices.
1220 $this->table
= $this->definition
['table'];
1221 $this->left_table
= $this->definition
['left_table'];
1222 $this->left_field
= $this->definition
['left_field'];
1223 $this->field
= $this->definition
['field'];
1224 if (!empty($this->definition
['extra'])) {
1225 $this->extra
= $this->definition
['extra'];
1227 if (!empty($this->definition
['extra type'])) {
1228 $this->extra_type
= strtoupper($this->definition
['extra type']);
1231 $this->type
= !empty($this->definition
['type']) ?
strtoupper($this->definition
['type']) : 'LEFT';
1236 * Build the SQL for the join this object represents.
1238 function join($table, &$query) {
1239 if (empty($this->definition
['table formula'])) {
1240 $right_table = "{" .
$this->table .
"}";
1243 $right_table = $this->definition
['table formula'];
1246 if ($this->left_table
) {
1247 $left = $query->get_table_info($this->left_table
);
1248 $left_field = "$left[alias].$this->left_field";
1251 // This can be used if left_field is a formula or something. It should be used only *very* rarely.
1252 $left_field = $this->left_field
;
1255 $output = " $this->type JOIN $right_table $table[alias] ON $left_field = $table[alias].$this->field";
1256 // Tack on the extra.
1257 if (isset($this->extra
)) {
1258 if (is_array($this->extra
)) {
1260 foreach ($this->extra as
$info) {
1262 // Figure out the table name. Remember, only use aliases provided
1263 // if at all possible.
1265 if (!array_key_exists('table', $info)) {
1266 $join_table = $table['alias'] .
'.';
1268 elseif (isset($info['table'])) {
1269 $join_table = $info['table'] .
'.';
1272 // And now deal with the value and the operator. Set $q to
1273 // a single-quote for non-numeric values and the
1274 // empty-string for numeric values, then wrap all values in $q.
1275 $raw_value = $this->db_safe($info['value']);
1276 $q = (empty($info['numeric']) ?
"'" : '');
1278 if (is_array($raw_value)) {
1279 $operator = !empty($info['operator']) ?
$info['operator'] : 'IN';
1280 // Transform from IN() notation to = notation if just one value.
1281 if (count($raw_value) == 1) {
1282 $value = $q .
array_shift($raw_value) .
$q;
1283 $operator = $operator == 'NOT IN' ?
'!=' : '=';
1286 $value = "($q" .
implode("$q, $q", $raw_value) .
"$q)";
1290 $operator = !empty($info['operator']) ?
$info['operator'] : '=';
1291 $value = "$q$raw_value$q";
1293 $extras[] = "$join_table$info[field] $operator $value";
1297 if (count($extras) == 1) {
1298 $output .
= ' AND ' .
array_shift($extras);
1301 $output .
= ' AND (' .
implode(' ' .
$this->extra_type .
' ', $extras) .
')';
1305 else if ($this->extra
&& is_string($this->extra
)) {
1306 $output .
= " AND ($this->extra)";
1313 * Ensure that input is db safe. We only check strings and ints tho
1314 * so something that needs floats in their joins needs to do their
1315 * own type checking.
1317 function db_safe($input) {
1318 if (is_array($input)) {
1320 foreach ($input as
$value) {
1321 if (empty($info['numeric'])) {
1322 $output[] = db_escape_string($value);
1325 $output[] = intval($value);
1329 else if (empty($info['numeric'])) {
1330 $output = db_escape_string($input);
1333 $output = intval($input);
1344 // Declare API compatibility on behalf of core modules:
1347 * Implementation of hook_views_api().
1349 * This one is used as the base to reduce errors when updating.
1351 function views_views_api() {
1354 'path' => drupal_get_path('module', 'views') .
'/modules',
1358 function aggregator_views_api() { return views_views_api(); }
1360 function book_views_api() { return views_views_api(); }
1362 function comment_views_api() { return views_views_api(); }
1364 function locale_views_api() { return views_views_api(); }
1366 function filter_views_api() { return views_views_api(); }
1368 function node_views_api() { return views_views_api(); }
1370 function poll_views_api() { return views_views_api(); }
1372 function profile_views_api() { return views_views_api(); }
1374 function search_views_api() { return views_views_api(); }
1376 function statistics_views_api() { return views_views_api(); }
1378 function system_views_api() { return views_views_api(); }
1380 function taxonomy_views_api() { return views_views_api(); }
1382 function translation_views_api() { return views_views_api(); }
1384 function upload_views_api() { return views_views_api(); }
1386 function user_views_api() { return views_views_api(); }
1388 function contact_views_api() { return views_views_api(); }