/core/filter_api.php
PHP | 4032 lines | 3307 code | 282 blank | 443 comment | 413 complexity | f5aa88adf8f0e19613695b08e5d9c349 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
Large files files are truncated, but you can click here to view the full file
- <?php
- # MantisBT - A PHP based bugtracking system
- # MantisBT is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 2 of the License, or
- # (at your option) any later version.
- #
- # MantisBT is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
- /**
- * Filter API
- *
- * @package CoreAPI
- * @subpackage FilterAPI
- * @copyright Copyright 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
- * @copyright Copyright 2002 MantisBT Team - mantisbt-dev@lists.sourceforge.net
- * @link http://www.mantisbt.org
- *
- * @uses access_api.php
- * @uses authentication_api.php
- * @uses bug_api.php
- * @uses collapse_api.php
- * @uses columns_api.php
- * @uses config_api.php
- * @uses constant_inc.php
- * @uses current_user_api.php
- * @uses custom_field_api.php
- * @uses database_api.php
- * @uses date_api.php
- * @uses error_api.php
- * @uses event_api.php
- * @uses filter_constants_inc.php
- * @uses gpc_api.php
- * @uses helper_api.php
- * @uses lang_api.php
- * @uses logging_api.php
- * @uses print_api.php
- * @uses profile_api.php
- * @uses project_api.php
- * @uses relationship_api.php
- * @uses session_api.php
- * @uses string_api.php
- * @uses tag_api.php
- * @uses user_api.php
- * @uses utility_api.php
- * @uses version_api.php
- * @uses filter_form_api.php
- */
- require_api( 'access_api.php' );
- require_api( 'authentication_api.php' );
- require_api( 'bug_api.php' );
- require_api( 'collapse_api.php' );
- require_api( 'columns_api.php' );
- require_api( 'config_api.php' );
- require_api( 'constant_inc.php' );
- require_api( 'current_user_api.php' );
- require_api( 'custom_field_api.php' );
- require_api( 'database_api.php' );
- require_api( 'date_api.php' );
- require_api( 'error_api.php' );
- require_api( 'event_api.php' );
- require_api( 'filter_constants_inc.php' );
- require_api( 'gpc_api.php' );
- require_api( 'helper_api.php' );
- require_api( 'lang_api.php' );
- require_api( 'logging_api.php' );
- require_api( 'print_api.php' );
- require_api( 'profile_api.php' );
- require_api( 'project_api.php' );
- require_api( 'relationship_api.php' );
- require_api( 'session_api.php' );
- require_api( 'string_api.php' );
- require_api( 'tag_api.php' );
- require_api( 'user_api.php' );
- require_api( 'utility_api.php' );
- require_api( 'version_api.php' );
- require_api( 'filter_form_api.php' );
- # @global array $g_filter Filter array for the filter in use through view_all_bug_page
- # This gets initialized on filter load
- # @TODO cproensa We should move towards not relying on this variable, as we reuse filter logic
- # to allow operating on other filter different that the one in use for view_all_bug_page.
- # For example: manage and edit stored filters.
- $g_filter = null;
- # ==========================================================================
- # CACHING
- # ==========================================================================
- # We cache filter requests to reduce the number of SQL queries
- # @global array $g_cache_filter_db_rows
- # indexed by filter_id, contains the filter rows as read from db table
- $g_cache_filter_db_rows = array();
- /**
- * Initialize the filter API with the current filter.
- * @param array $p_filter The filter to set as the current filter.
- */
- function filter_init( $p_filter ) {
- global $g_filter;
- $g_filter = $p_filter;
- }
- /**
- * Allow plugins to define a set of class-based filters, and register/load
- * them here to be used by the rest of filter_api.
- * @return array Mapping of field name to filter object
- */
- function filter_get_plugin_filters() {
- static $s_field_array = null;
- if( is_null( $s_field_array ) ) {
- $s_field_array = array();
- $t_all_plugin_filters = event_signal( 'EVENT_FILTER_FIELDS' );
- foreach( $t_all_plugin_filters as $t_plugin => $t_plugin_filters ) {
- foreach( $t_plugin_filters as $t_callback => $t_plugin_filter_array ) {
- if( is_array( $t_plugin_filter_array ) ) {
- foreach( $t_plugin_filter_array as $t_filter_item ) {
- if( is_object( $t_filter_item ) && $t_filter_item instanceof MantisFilter ) {
- $t_filter_object = $t_filter_item;
- } elseif( class_exists( $t_filter_item ) && is_subclass_of( $t_filter_item, 'MantisFilter' ) ) {
- $t_filter_object = new $t_filter_item();
- } else {
- continue;
- }
- $t_filter_name = mb_strtolower( $t_plugin . '_' . $t_filter_object->field );
- $s_field_array[$t_filter_name] = $t_filter_object;
- }
- }
- }
- }
- }
- return $s_field_array;
- }
- /**
- * Get a permanent link for the current active filter. The results of using these fields by other users
- * can be inconsistent with the original results due to fields like "Myself", "Current Project",
- * and due to access level.
- * @param array $p_custom_filter Array containing a custom filter definition.
- * @return string the search.php?xxxx or an empty string if no criteria applied.
- */
- function filter_get_url( array $p_custom_filter ) {
- $t_query = array();
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_PROJECT_ID] ) ) {
- $t_project_id = $p_custom_filter[FILTER_PROPERTY_PROJECT_ID];
- if( count( $t_project_id ) == 1 && $t_project_id[0] == META_FILTER_CURRENT ) {
- $t_project_id = array(
- helper_get_current_project(),
- );
- }
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_PROJECT_ID, $t_project_id );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_SEARCH] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_SEARCH, $p_custom_filter[FILTER_PROPERTY_SEARCH] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_CATEGORY_ID] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_CATEGORY_ID, $p_custom_filter[FILTER_PROPERTY_CATEGORY_ID] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_REPORTER_ID] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_REPORTER_ID, $p_custom_filter[FILTER_PROPERTY_REPORTER_ID] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_STATUS] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_STATUS, $p_custom_filter[FILTER_PROPERTY_STATUS] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_MONITOR_USER_ID] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_MONITOR_USER_ID, $p_custom_filter[FILTER_PROPERTY_MONITOR_USER_ID] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_HANDLER_ID] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_HANDLER_ID, $p_custom_filter[FILTER_PROPERTY_HANDLER_ID] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_NOTE_USER_ID] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_NOTE_USER_ID, $p_custom_filter[FILTER_PROPERTY_NOTE_USER_ID] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_SEVERITY] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_SEVERITY, $p_custom_filter[FILTER_PROPERTY_SEVERITY] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_RESOLUTION] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_RESOLUTION, $p_custom_filter[FILTER_PROPERTY_RESOLUTION] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_PRIORITY] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_PRIORITY, $p_custom_filter[FILTER_PROPERTY_PRIORITY] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_VIEW_STATE] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_VIEW_STATE, $p_custom_filter[FILTER_PROPERTY_VIEW_STATE] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_STICKY] ) ) {
- $t_query[] = filter_encode_field_and_value(
- FILTER_PROPERTY_STICKY,
- $p_custom_filter[FILTER_PROPERTY_STICKY] ? 'on' : 'off' );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_VERSION] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_VERSION, $p_custom_filter[FILTER_PROPERTY_VERSION] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_BUILD] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_BUILD, $p_custom_filter[FILTER_PROPERTY_BUILD] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_FIXED_IN_VERSION] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_FIXED_IN_VERSION, $p_custom_filter[FILTER_PROPERTY_FIXED_IN_VERSION] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_TARGET_VERSION] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_TARGET_VERSION, $p_custom_filter[FILTER_PROPERTY_TARGET_VERSION] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_SORT_FIELD_NAME] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_SORT_FIELD_NAME, $p_custom_filter[FILTER_PROPERTY_SORT_FIELD_NAME] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_SORT_DIRECTION] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_SORT_DIRECTION, $p_custom_filter[FILTER_PROPERTY_SORT_DIRECTION] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_ISSUES_PER_PAGE] ) ) {
- if( $p_custom_filter[FILTER_PROPERTY_ISSUES_PER_PAGE] != config_get( 'default_limit_view' ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_ISSUES_PER_PAGE, $p_custom_filter[FILTER_PROPERTY_ISSUES_PER_PAGE] );
- }
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_HIGHLIGHT_CHANGED] ) ) {
- if( $p_custom_filter[FILTER_PROPERTY_HIGHLIGHT_CHANGED] != config_get( 'default_show_changed' ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_HIGHLIGHT_CHANGED, $p_custom_filter[FILTER_PROPERTY_HIGHLIGHT_CHANGED] );
- }
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_HIDE_STATUS] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_HIDE_STATUS, $p_custom_filter[FILTER_PROPERTY_HIDE_STATUS] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_FILTER_BY_DATE_SUBMITTED] ) ) {
- $t_query[] = filter_encode_field_and_value(
- FILTER_PROPERTY_FILTER_BY_DATE_SUBMITTED,
- $p_custom_filter[FILTER_PROPERTY_FILTER_BY_DATE_SUBMITTED] ? 'on' : 'off' );
- # The start and end dates are only applicable if filter by date is set.
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_DATE_SUBMITTED_START_DAY] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_DATE_SUBMITTED_START_DAY, $p_custom_filter[FILTER_PROPERTY_DATE_SUBMITTED_START_DAY] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_DATE_SUBMITTED_END_DAY] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_DATE_SUBMITTED_END_DAY, $p_custom_filter[FILTER_PROPERTY_DATE_SUBMITTED_END_DAY] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_DATE_SUBMITTED_START_MONTH] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_DATE_SUBMITTED_START_MONTH, $p_custom_filter[FILTER_PROPERTY_DATE_SUBMITTED_START_MONTH] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_DATE_SUBMITTED_END_MONTH] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_DATE_SUBMITTED_END_MONTH, $p_custom_filter[FILTER_PROPERTY_DATE_SUBMITTED_END_MONTH] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_DATE_SUBMITTED_START_YEAR] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_DATE_SUBMITTED_START_YEAR, $p_custom_filter[FILTER_PROPERTY_DATE_SUBMITTED_START_YEAR] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_DATE_SUBMITTED_END_YEAR] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_DATE_SUBMITTED_END_YEAR, $p_custom_filter[FILTER_PROPERTY_DATE_SUBMITTED_END_YEAR] );
- }
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_FILTER_BY_LAST_UPDATED_DATE] ) ) {
- $t_query[] = filter_encode_field_and_value(
- FILTER_PROPERTY_FILTER_BY_LAST_UPDATED_DATE,
- $p_custom_filter[FILTER_PROPERTY_FILTER_BY_LAST_UPDATED_DATE] ? 'on' : 'off' );
- # The start and end dates are only applicable if filter by date is set.
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_LAST_UPDATED_START_DAY] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_LAST_UPDATED_START_DAY, $p_custom_filter[FILTER_PROPERTY_LAST_UPDATED_START_DAY] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_LAST_UPDATED_END_DAY] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_LAST_UPDATED_END_DAY, $p_custom_filter[FILTER_PROPERTY_LAST_UPDATED_END_DAY] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_LAST_UPDATED_START_MONTH] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_LAST_UPDATED_START_MONTH, $p_custom_filter[FILTER_PROPERTY_LAST_UPDATED_START_MONTH] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_LAST_UPDATED_END_MONTH] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_LAST_UPDATED_END_MONTH, $p_custom_filter[FILTER_PROPERTY_LAST_UPDATED_END_MONTH] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_LAST_UPDATED_START_YEAR] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_LAST_UPDATED_START_YEAR, $p_custom_filter[FILTER_PROPERTY_LAST_UPDATED_START_YEAR] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_LAST_UPDATED_END_YEAR] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_LAST_UPDATED_END_YEAR, $p_custom_filter[FILTER_PROPERTY_LAST_UPDATED_END_YEAR] );
- }
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_RELATIONSHIP_TYPE] ) ) {
- if( $p_custom_filter[FILTER_PROPERTY_RELATIONSHIP_TYPE] != -1 ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_RELATIONSHIP_TYPE, $p_custom_filter[FILTER_PROPERTY_RELATIONSHIP_TYPE] );
- }
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_RELATIONSHIP_BUG] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_RELATIONSHIP_BUG, $p_custom_filter[FILTER_PROPERTY_RELATIONSHIP_BUG] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_PLATFORM] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_PLATFORM, $p_custom_filter[FILTER_PROPERTY_PLATFORM] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_OS] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_OS, $p_custom_filter[FILTER_PROPERTY_OS] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_OS_BUILD] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_OS_BUILD, $p_custom_filter[FILTER_PROPERTY_OS_BUILD] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_TAG_STRING] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_TAG_STRING, $p_custom_filter[FILTER_PROPERTY_TAG_STRING] );
- }
- if( !filter_field_is_any( $p_custom_filter[FILTER_PROPERTY_TAG_SELECT] ) ) {
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_TAG_SELECT, $p_custom_filter[FILTER_PROPERTY_TAG_SELECT] );
- }
- $t_query[] = filter_encode_field_and_value( FILTER_PROPERTY_MATCH_TYPE, $p_custom_filter[FILTER_PROPERTY_MATCH_TYPE] );
- if( isset( $p_custom_filter['custom_fields'] ) ) {
- foreach( $p_custom_filter['custom_fields'] as $t_custom_field_id => $t_custom_field_values ) {
- if( !filter_field_is_any( $t_custom_field_values ) ) {
- $t_query[] = filter_encode_field_and_value( 'custom_field_' . $t_custom_field_id, $t_custom_field_values );
- }
- }
- }
- # Allow plugins to add filter fields
- $t_plugin_filter_array = filter_get_plugin_filters();
- foreach( $t_plugin_filter_array as $t_field_name => $t_filter_object ) {
- if( !filter_field_is_any( $p_custom_filter[$t_field_name] ) ) {
- $t_query[] = filter_encode_field_and_value( $t_field_name, $p_custom_filter[$t_field_name], $t_filter_object->type );
- }
- }
- if( count( $t_query ) > 0 ) {
- $t_query_str = implode( $t_query, '&' );
- $t_url = config_get_global( 'path' ) . 'search.php?' . $t_query_str;
- } else {
- $t_url = '';
- }
- return $t_url;
- }
- /**
- * Encodes a field and it's value for the filter URL. This handles the URL encoding and arrays.
- * @param string $p_field_name The field name.
- * @param string $p_field_value The field value (can be an array).
- * @param integer $p_field_type Field Type e.g. FILTER_TYPE_MULTI_STRING.
- * @return string url encoded string
- */
- function filter_encode_field_and_value( $p_field_name, $p_field_value, $p_field_type = null ) {
- $t_query_array = array();
- if( is_array( $p_field_value ) ) {
- $t_count = count( $p_field_value );
- if( $t_count > 1 || $p_field_type == FILTER_TYPE_MULTI_STRING || $p_field_type == FILTER_TYPE_MULTI_INT ) {
- foreach( $p_field_value as $t_value ) {
- $t_query_array[] = urlencode( $p_field_name . '[]' ) . '=' . urlencode( $t_value );
- }
- } else if( $t_count == 1 ) {
- $t_query_array[] = urlencode( $p_field_name ) . '=' . urlencode( $p_field_value[0] );
- }
- } else {
- $t_query_array[] = urlencode( $p_field_name ) . '=' . urlencode( $p_field_value );
- }
- return implode( $t_query_array, '&' );
- }
- /**
- * Checks the supplied value to see if it is an ANY value.
- * @param string $p_field_value The value to check.
- * @return boolean true for "ANY" values and false for others. "ANY" means filter criteria not active.
- */
- function filter_field_is_any( $p_field_value ) {
- if( is_array( $p_field_value ) ) {
- if( count( $p_field_value ) == 0 ) {
- return true;
- }
- foreach( $p_field_value as $t_value ) {
- if( ( META_FILTER_ANY == $t_value ) && ( is_numeric( $t_value ) ) ) {
- return true;
- }
- }
- } else {
- if( is_string( $p_field_value ) && is_blank( $p_field_value ) ) {
- return true;
- }
- if( is_bool( $p_field_value ) && !$p_field_value ) {
- return true;
- }
- if( ( META_FILTER_ANY == $p_field_value ) && ( is_numeric( $p_field_value ) ) ) {
- return true;
- }
- }
- return false;
- }
- /**
- * Checks the supplied value to see if it is a NONE value.
- * @param string $p_field_value The value to check.
- * @return boolean true for "NONE" values and false for others.
- * @todo is a check for these necessary? if( ( $t_filter_value === 'none' ) || ( $t_filter_value === '[none]' ) )
- */
- function filter_field_is_none( $p_field_value ) {
- if( is_array( $p_field_value ) ) {
- foreach( $p_field_value as $t_value ) {
- if( ( META_FILTER_NONE == $t_value ) && ( is_numeric( $t_value ) ) ) {
- return true;
- }
- }
- } else {
- if( is_string( $p_field_value ) && is_blank( $p_field_value ) ) {
- return false;
- }
- if( ( META_FILTER_NONE == $p_field_value ) && ( is_numeric( $p_field_value ) ) ) {
- return true;
- }
- }
- return false;
- }
- /**
- * Checks the supplied value to see if it is a MYSELF value.
- * @param string $p_field_value The value to check.
- * @return boolean true for "MYSELF" values and false for others.
- */
- function filter_field_is_myself( $p_field_value ) {
- return( META_FILTER_MYSELF == $p_field_value ? true : false );
- }
- /**
- * Filter per page
- * @param array $p_filter Filter.
- * @param integer $p_count Count.
- * @param integer $p_per_page Per page.
- * @return integer
- */
- function filter_per_page( array $p_filter, $p_count, $p_per_page ) {
- $p_per_page = (( null == $p_per_page ) ? (int)$p_filter[FILTER_PROPERTY_ISSUES_PER_PAGE] : $p_per_page );
- $p_per_page = (( 0 == $p_per_page || -1 == $p_per_page ) ? $p_count : $p_per_page );
- return (int)abs( $p_per_page );
- }
- /**
- * Use $p_count and $p_per_page to determine how many pages to split this list up into.
- * For the sake of consistency have at least one page, even if it is empty.
- * @param integer $p_count Count.
- * @param integer $p_per_page Per page.
- * @return integer page count
- */
- function filter_page_count( $p_count, $p_per_page ) {
- $t_page_count = ceil( $p_count / $p_per_page );
- if( $t_page_count < 1 ) {
- $t_page_count = 1;
- }
- return $t_page_count;
- }
- /**
- * Checks to make sure $p_page_number isn't past the last page.
- * and that $p_page_number isn't before the first page
- * @param integer $p_page_number Page number.
- * @param integer $p_page_count Page count.
- * @return integer
- */
- function filter_valid_page_number( $p_page_number, $p_page_count ) {
- if( $p_page_number > $p_page_count ) {
- $p_page_number = $p_page_count;
- }
- if( $p_page_number < 1 ) {
- $p_page_number = 1;
- }
- return $p_page_number;
- }
- /**
- * Figure out the offset into the db query, offset is which record to start querying from
- * @param integer $p_page_number Page number.
- * @param integer $p_per_page Per page.
- * @return integer
- */
- function filter_offset( $p_page_number, $p_per_page ) {
- return(( (int)$p_page_number -1 ) * (int)$p_per_page );
- }
- /**
- * Make sure the filter array contains all the fields. If any field is missing,
- * create it with a default value.
- * @param array $p_filter_arr Input filter array
- * @return array Processed filter array
- */
- function filter_ensure_fields( array $p_filter_arr ) {
- # Fill missing filter properties with defaults
- if( isset( $p_filter_arr['_view_type'] ) ) {
- $t_filter_default = filter_get_default_array( $p_filter_arr['_view_type'] );
- } else {
- $t_filter_default = filter_get_default_array();
- }
- foreach( $t_filter_default as $t_key => $t_default_value ) {
- if( !isset( $p_filter_arr[$t_key] ) ) {
- $p_filter_arr[$t_key] = $t_default_value;
- }
- }
- # Veryfy custom fields
- foreach( $t_filter_default['custom_fields'] as $t_cfid => $t_cf_data ) {
- if( !isset( $p_filter_arr['custom_fields'][$t_cfid] ) ) {
- $p_filter_arr['custom_fields'][$t_cfid] = $t_cf_data;
- }
- }
- return $p_filter_arr;
- }
- /**
- * A wrapper to compare filter version syntax
- * Note: Currently, filter versions have this syntax: "vN", * where N is an integer number.
- * @param string $p_version1 First version number
- * @param string $p_version2 Second version number
- * @param string $p_operator Comparison test, if provided. As expected by version_compare()
- * @return mixed As returned by version_compare()
- */
- function filter_version_compare( $p_version1, $p_version2, $p_operator = null ) {
- return version_compare( $p_version1, $p_version2, $p_operator );
- }
- /**
- * Upgrade a filter array to the current filter structure, by converting properties
- * that have changed from previous filter versions
- * @param array $p_filter Filter array to upgrade
- * @return array Updgraded filter array
- */
- function filter_version_upgrade( array $p_filter ) {
- # This is a stub for future version upgrades
- # After conversions are made, update filter value to current version
- $p_filter['_version'] = FILTER_VERSION;
- return $p_filter;
- }
- /**
- * Make sure that our filters are entirely correct and complete (it is possible that they are not).
- * We need to do this to cover cases where we don't have complete control over the filters given.
- * @param array $p_filter_arr A filter array
- * @return array Validated filter array
- */
- function filter_ensure_valid_filter( array $p_filter_arr ) {
- if( !isset( $p_filter_arr['_version'] ) ) {
- $p_filter_arr['_version'] = FILTER_VERSION;
- }
- if( filter_version_compare( $p_filter_arr['_version'], FILTER_VERSION, '<' ) ) {
- $p_filter_arr = filter_version_upgrade( $p_filter_arr );
- }
- $p_filter_arr = filter_ensure_fields( $p_filter_arr );
- $t_config_view_filters = config_get( 'view_filters' );
- $t_view_type = $p_filter_arr['_view_type'];
- if( ADVANCED_ONLY == $t_config_view_filters ) {
- $t_view_type = FILTER_VIEW_TYPE_ADVANCED;
- }
- if( SIMPLE_ONLY == $t_config_view_filters ) {
- $t_view_type = FILTER_VIEW_TYPE_SIMPLE;
- }
- if( !in_array( $t_view_type, array( FILTER_VIEW_TYPE_SIMPLE, FILTER_VIEW_TYPE_ADVANCED ) ) ) {
- $t_view_type = filter_get_default_view_type();
- }
- $p_filter_arr['_view_type'] = $t_view_type;
- $t_sort_fields = explode( ',', $p_filter_arr[FILTER_PROPERTY_SORT_FIELD_NAME] );
- $t_dir_fields = explode( ',', $p_filter_arr[FILTER_PROPERTY_SORT_DIRECTION] );
- # both arrays should be equal length, just in case
- $t_sort_fields_count = min( count( $t_sort_fields ), count( $t_dir_fields ) );
- # clean up sort fields, remove invalid columns
- $t_new_sort_array = array();
- $t_new_dir_array = array();
- $t_all_columns = columns_get_all_active_columns();
- for( $ix = 0; $ix < $t_sort_fields_count; $ix++ ) {
- if( isset( $t_sort_fields[$ix] ) ) {
- $t_column = $t_sort_fields[$ix];
- # check that the column name exist
- if( !in_array( $t_column, $t_all_columns ) ) {
- continue;
- }
- # check that it has not been already used
- if( in_array( $t_column, $t_new_sort_array ) ) {
- continue;
- }
- # check that it is sortable
- if( !column_is_sortable( $t_column ) ) {
- continue;
- }
- $t_new_sort_array[] = $t_column;
- # if there is no dir field, set a dummy value
- if( isset( $t_dir_fields[$ix] ) ) {
- $t_dir = $t_dir_fields[$ix];
- } else {
- $t_dir = '';
- }
- # normalize sort_dir value
- $t_dir = ( $t_dir == 'ASC' ) ? 'ASC' : 'DESC';
- $t_new_dir_array[] = $t_dir;
- }
- }
- if( count( $t_new_sort_array ) > 0 ) {
- $p_filter_arr[FILTER_PROPERTY_SORT_FIELD_NAME] = implode( ',', $t_new_sort_array );
- $p_filter_arr[FILTER_PROPERTY_SORT_DIRECTION] = implode( ',', $t_new_dir_array );
- } else {
- $p_filter_arr[FILTER_PROPERTY_SORT_FIELD_NAME] = filter_get_default_property( FILTER_PROPERTY_SORT_FIELD_NAME, $t_view_type );
- $p_filter_arr[FILTER_PROPERTY_SORT_DIRECTION] = filter_get_default_property( FILTER_PROPERTY_SORT_DIRECTION, $t_view_type );
- }
- # Validate types for values.
- # helper function to validate types
- $t_function_validate_type = function( $p_value, $p_type ) {
- $t_value = stripslashes( $p_value );
- if( ( $t_value === 'any' ) || ( $t_value === '[any]' ) ) {
- $t_value = META_FILTER_ANY;
- }
- if( ( $t_value === 'none' ) || ( $t_value === '[none]' ) ) {
- $t_value = META_FILTER_NONE;
- }
- # Ensure the filter property has the right type - see #20087
- switch( $p_type ) {
- case 'string' :
- case 'int' :
- settype( $t_value, $p_type );
- break;
- }
- return $t_value;
- };
- # Validate properties that must not be arrays
- $t_single_value_list = array(
- FILTER_PROPERTY_VIEW_STATE => 'int',
- FILTER_PROPERTY_RELATIONSHIP_TYPE => 'int',
- FILTER_PROPERTY_RELATIONSHIP_BUG => 'int',
- );
- foreach( $t_single_value_list as $t_field_name => $t_field_type ) {
- $t_value = $p_filter_arr[$t_field_name];
- if( is_array( $t_value ) ) {
- if( count( $t_value ) > 0 ) {
- $p_filter_arr[$t_field_name] = reset( $t_value );
- } else {
- $p_filter_arr[$t_field_name] = filter_get_default_property( $t_field_name, $t_view_type );
- }
- }
- $p_filter_arr[$t_field_name] = $t_function_validate_type( $p_filter_arr[$t_field_name], $t_field_type );
- }
- # Validate properties that must be arrays, and the type of its elements
- $t_array_values_list = array(
- FILTER_PROPERTY_CATEGORY_ID => 'string',
- FILTER_PROPERTY_SEVERITY => 'int',
- FILTER_PROPERTY_STATUS => 'int',
- FILTER_PROPERTY_REPORTER_ID => 'int',
- FILTER_PROPERTY_HANDLER_ID => 'int',
- FILTER_PROPERTY_NOTE_USER_ID => 'int',
- FILTER_PROPERTY_RESOLUTION => 'int',
- FILTER_PROPERTY_PRIORITY => 'int',
- FILTER_PROPERTY_BUILD => 'string',
- FILTER_PROPERTY_VERSION => 'string',
- FILTER_PROPERTY_HIDE_STATUS => 'int',
- FILTER_PROPERTY_FIXED_IN_VERSION => 'string',
- FILTER_PROPERTY_TARGET_VERSION => 'string',
- FILTER_PROPERTY_MONITOR_USER_ID => 'int',
- FILTER_PROPERTY_PROFILE_ID => 'int',
- FILTER_PROPERTY_PLATFORM => 'string',
- FILTER_PROPERTY_OS => 'string',
- FILTER_PROPERTY_OS_BUILD => 'string',
- FILTER_PROPERTY_PROJECT_ID => 'int'
- );
- foreach( $t_array_values_list as $t_multi_field_name => $t_multi_field_type ) {
- if( !is_array( $p_filter_arr[$t_multi_field_name] ) ) {
- $p_filter_arr[$t_multi_field_name] = array(
- $p_filter_arr[$t_multi_field_name],
- );
- }
- $t_checked_array = array();
- foreach( $p_filter_arr[$t_multi_field_name] as $t_filter_value ) {
- $t_checked_array[] = $t_function_validate_type( $t_filter_value, $t_multi_field_type );
- }
- $p_filter_arr[$t_multi_field_name] = $t_checked_array;
- }
- $t_custom_fields = custom_field_get_ids();
- if( is_array( $t_custom_fields ) && ( count( $t_custom_fields ) > 0 ) ) {
- foreach( $t_custom_fields as $t_cfid ) {
- if( isset( $p_filter_arr['custom_fields'][$t_cfid]) ) {
- if( !is_array( $p_filter_arr['custom_fields'][$t_cfid] ) ) {
- $p_filter_arr['custom_fields'][$t_cfid] = array(
- $p_filter_arr['custom_fields'][$t_cfid],
- );
- }
- $t_checked_array = array();
- foreach( $p_filter_arr['custom_fields'][$t_cfid] as $t_filter_value ) {
- $t_filter_value = stripslashes( $t_filter_value );
- if( ( $t_filter_value === 'any' ) || ( $t_filter_value === '[any]' ) ) {
- $t_filter_value = META_FILTER_ANY;
- }
- $t_checked_array[] = $t_filter_value;
- }
- $p_filter_arr['custom_fields'][$t_cfid] = $t_checked_array;
- }
- }
- }
- # If view_type is advanced, and hide_status is present, modify status array
- # to remove hidden status. This may happen after switching from simple to advanced.
- # Then, remove hide_status property, as it does not apply to advanced filter
- if( $p_filter_arr['_view_type'] == FILTER_VIEW_TYPE_ADVANCED
- && !filter_field_is_none( $p_filter_arr[FILTER_PROPERTY_HIDE_STATUS] ) ) {
- if( filter_field_is_any( $p_filter_arr[FILTER_PROPERTY_STATUS] ) ) {
- $t_selected_status_array = MantisEnum::getValues( config_get( 'status_enum_string' ) );
- } else {
- $t_selected_status_array = $p_filter_arr[FILTER_PROPERTY_STATUS];
- }
- $t_hide_status = $p_filter_arr[FILTER_PROPERTY_HIDE_STATUS][0];
- $t_new_status_array = array();
- foreach( $t_selected_status_array as $t_status ) {
- if( $t_status < $t_hide_status ) {
- $t_new_status_array[] = $t_status;
- }
- }
- # If there is no status left, reset the status property to "any"
- if( empty( $t_new_status_array ) ) {
- $t_new_status_array[] = META_FILTER_ANY;
- }
- $p_filter_arr[FILTER_PROPERTY_STATUS] = $t_new_status_array;
- $p_filter_arr[FILTER_PROPERTY_HIDE_STATUS] = META_FILTER_NONE;
- }
- #If view_type is simple, resolve conflicts between show_status and hide_status
- if( $p_filter_arr['_view_type'] == FILTER_VIEW_TYPE_SIMPLE
- && !filter_field_is_none( $p_filter_arr[FILTER_PROPERTY_HIDE_STATUS] ) ) {
- # get array of hidden status ids
- $t_all_status = MantisEnum::getValues( config_get( 'status_enum_string' ) );
- $t_hidden_status = $p_filter_arr[FILTER_PROPERTY_HIDE_STATUS][0];
- $t_hidden_status_array = array();
- foreach( $t_all_status as $t_status ) {
- if( $t_status >= $t_hidden_status ) {
- $t_hidden_status_array[] = $t_status;
- }
- }
- # remove hidden status from show_status property array
- # note that this will keep the "any" meta value, if present
- $t_show_status_array = array_diff( $p_filter_arr[FILTER_PROPERTY_STATUS], $t_hidden_status_array );
- # If there is no status left, reset the status property previous values, and remove hide_status
- if( empty( $t_show_status_array ) ) {
- $t_show_status_array = $p_filter_arr[FILTER_PROPERTY_STATUS];
- $p_filter_arr[FILTER_PROPERTY_HIDE_STATUS] = META_FILTER_NONE;
- }
- $p_filter_arr[FILTER_PROPERTY_STATUS] = $t_show_status_array;
- }
- # validate relationship fields
- if( !(
- $p_filter_arr[FILTER_PROPERTY_RELATIONSHIP_BUG] > 0
- || $p_filter_arr[FILTER_PROPERTY_RELATIONSHIP_BUG] == META_FILTER_ANY
- || $p_filter_arr[FILTER_PROPERTY_RELATIONSHIP_BUG] == META_FILTER_NONE
- ) ) {
- $p_filter_arr[FILTER_PROPERTY_RELATIONSHIP_BUG] = filter_get_default_property( FILTER_PROPERTY_RELATIONSHIP_BUG, $t_view_type );
- }
- # all of our filter values are now guaranteed to be there, and correct.
- return $p_filter_arr;
- }
- /**
- * Get a filter array with default values
- * Optional view type parameter is used to initialize some fields properly,
- * as some may differ in the default content.
- * @param string $p_view_type FILTER_VIEW_TYPE_SIMPLE or FILTER_VIEW_TYPE_ADVANCED
- * @return array Filter array with default values
- */
- function filter_get_default_array( $p_view_type = null ) {
- static $t_cache_default_array = array();
- $t_default_view_type = filter_get_default_view_type();
- if( !in_array( $p_view_type, array( FILTER_VIEW_TYPE_SIMPLE, FILTER_VIEW_TYPE_ADVANCED ) ) ) {
- $p_view_type = $t_default_view_type;
- }
- # this function is called multiple times from filter api so return a cached value if possible
- if( isset( $t_cache_default_array[$p_view_type] ) ) {
- return $t_cache_default_array[$p_view_type];
- }
- $t_default_show_changed = config_get( 'default_show_changed' );
- $t_meta_filter_any_array = array( META_FILTER_ANY );
- $t_config_view_filters = config_get( 'view_filters' );
- if( ADVANCED_ONLY == $t_config_view_filters ) {
- $t_view_type = FILTER_VIEW_TYPE_ADVANCED;
- } elseif( SIMPLE_ONLY == $t_config_view_filters ) {
- $t_view_type = FILTER_VIEW_TYPE_SIMPLE;
- } else {
- $t_view_type = $p_view_type;
- }
- if( $t_view_type == FILTER_VIEW_TYPE_SIMPLE ) {
- $t_hide_status_default = config_get( 'hide_status_default' );
- } else {
- $t_hide_status_default = META_FILTER_NONE;
- }
- $t_filter = array(
- '_version' => FILTER_VERSION,
- '_view_type' => $t_view_type,
- FILTER_PROPERTY_CATEGORY_ID => $t_meta_filter_any_array,
- FILTER_PROPERTY_SEVERITY => $t_meta_filter_any_array,
- FILTER_PROPERTY_STATUS => $t_meta_filter_any_array,
- FILTER_PROPERTY_HIGHLIGHT_CHANGED => $t_default_show_changed,
- FILTER_PROPERTY_REPORTER_ID => $t_meta_filter_any_array,
- FILTER_PROPERTY_HANDLER_ID => $t_meta_filter_any_array,
- FILTER_PROPERTY_PROJECT_ID => array( META_FILTER_CURRENT ),
- FILTER_PROPERTY_RESOLUTION => $t_meta_filter_any_array,
- FILTER_PROPERTY_BUILD => $t_meta_filter_any_array,
- FILTER_PROPERTY_VERSION => $t_meta_filter_any_array,
- FILTER_PROPERTY_HIDE_STATUS => array( $t_hide_status_default ),
- FILTER_PROPERTY_MONITOR_USER_ID => $t_meta_filter_any_array,
- FILTER_PROPERTY_SORT_FIELD_NAME => 'last_updated',
- FILTER_PROPERTY_SORT_DIRECTION => 'DESC',
- FILTER_PROPERTY_ISSUES_PER_PAGE => config_get( 'default_limit_view' ),
- FILTER_PROPERTY_MATCH_TYPE => FILTER_MATCH_ALL,
- FILTER_PROPERTY_PLATFORM => $t_meta_filter_any_array,
- FILTER_PROPERTY_OS => $t_meta_filter_any_array,
- FILTER_PROPERTY_OS_BUILD => $t_meta_filter_any_array,
- FILTER_PROPERTY_FIXED_IN_VERSION => $t_meta_filter_any_array,
- FILTER_PROPERTY_TARGET_VERSION => $t_meta_filter_any_array,
- FILTER_PROPERTY_PROFILE_ID => $t_meta_filter_any_array,
- FILTER_PROPERTY_PRIORITY => $t_meta_filter_any_array,
- FILTER_PROPERTY_NOTE_USER_ID => $t_meta_filter_any_array,
- FILTER_PROPERTY_STICKY => gpc_string_to_bool( config_get( 'show_sticky_issues' ) ),
- FILTER_PROPERTY_FILTER_BY_DATE_SUBMITTED => false,
- FILTER_PROPERTY_DATE_SUBMITTED_START_MONTH => date( 'm' ),
- FILTER_PROPERTY_DATE_SUBMITTED_END_MONTH => date( 'm' ),
- FILTER_PROPERTY_DATE_SUBMITTED_START_DAY => 1,
- FILTER_PROPERTY_DATE_SUBMITTED_END_DAY => date( 'd' ),
- FILTER_PROPERTY_DATE_SUBMITTED_START_YEAR => date( 'Y' ),
- FILTER_PROPERTY_DATE_SUBMITTED_END_YEAR => date( 'Y' ),
- FILTER_PROPERTY_FILTER_BY_LAST_UPDATED_DATE => false,
- FILTER_PROPERTY_LAST_UPDATED_START_MONTH => date( 'm' ),
- FILTER_PROPERTY_LAST_UPDATED_END_MONTH => date( 'm' ),
- FILTER_PROPERTY_LAST_UPDATED_START_DAY => 1,
- FILTER_PROPERTY_LAST_UPDATED_END_DAY => date( 'd' ),
- FILTER_PROPERTY_LAST_UPDATED_START_YEAR => date( 'Y' ),
- FILTER_PROPERTY_LAST_UPDATED_END_YEAR => date( 'Y' ),
- FILTER_PROPERTY_SEARCH => '',
- FILTER_PROPERTY_VIEW_STATE => META_FILTER_ANY,
- FILTER_PROPERTY_TAG_STRING => '',
- FILTER_PROPERTY_TAG_SELECT => 0,
- FILTER_PROPERTY_RELATIONSHIP_TYPE => BUG_REL_ANY,
- FILTER_PROPERTY_RELATIONSHIP_BUG => META_FILTER_ANY,
- );
- # initialize plugin filters
- $t_plugin_filters = filter_get_plugin_filters();
- foreach( $t_plugin_filters as $t_field_name => $t_filter_object ) {
- switch( $t_filter_object->type ) {
- case FILTER_TYPE_STRING:
- $t_filter[$t_field_name] = $t_filter_object->default;
- break;
- case FILTER_TYPE_INT:
- $t_filter[$t_field_name] = (int)$t_filter_object->default;
- break;
- case FILTER_TYPE_BOOLEAN:
- $t_filter[$t_field_name] = (bool)$t_filter_object->default;
- break;
- case FILTER_TYPE_MULTI_STRING:
- $t_filter[$t_field_name] = array( (string)META_FILTER_ANY );
- break;
- case FILTER_TYPE_MULTI_INT:
- $t_filter[$t_field_name] = array( META_FILTER_ANY );
- break;
- default:
- $t_filter[$t_field_name] = (string)META_FILTER_ANY;
- }
- if( !$t_filter_object->validate( $t_filter[$t_field_name] ) ) {
- $t_filter[$t_field_name] = $t_filter_object->default;
- }
- }
- $t_custom_fields = custom_field_get_ids();
- # @@@ (thraxisp) This should really be the linked ids, but we don't know the project
- $f_custom_fields_data = array();
- if( is_array( $t_custom_fields ) && ( count( $t_custom_fields ) > 0 ) ) {
- foreach( $t_custom_fields as $t_cfid ) {
- $f_custom_fields_data[$t_cfid] = array( (string)META_FILTER_ANY );
- }
- }
- $t_filter['custom_fields'] = $f_custom_fields_data;
- $t_cache_default_array[$p_view_type] = $t_filter;
- return $t_filter;
- }
- /**
- * Returns the default view type for filters
- * @return string Default view type
- */
- function filter_get_default_view_type() {
- if( ADVANCED_DEFAULT == config_get( 'view_filters' ) ) {
- return FILTER_VIEW_TYPE_ADVANCED;
- } else {
- return FILTER_VIEW_TYPE_SIMPLE;
- }
- }
- /**
- * Returns the default value for a filter property.
- * Relies on filter_get_default_array() to get a defaulted filter.
- * @param string $p_filter_property The requested filter property name
- * @param string $p_view_type Optional, view type for the defaulted filter (simple/advanced)
- * @return mixed The property default value, or null if it doesn't exist
- */
- function filter_get_default_property( $p_filter_property, $p_view_type = null ) {
- $t_default_array = filter_get_default_array( $p_view_type );
- if( isset( $t_default_array[$p_filter_property] ) ) {
- return $t_default_array[$p_filter_property];
- } else {
- return null;
- }
- }
- /**
- * Get the standard filter that is to be used when no filter was previously saved.
- * When creating specific filters, this can be used as a basis for the filter, where
- * specific entries can be overridden.
- * @return mixed
- */
- function filter_get_default() {
- # Create empty array, validation will fill it with defaults
- $t_filter = array();
- return filter_ensure_valid_filter( $t_filter );
- }
- /**
- * Deserialize filter string
- * Expected strings have this format: "<version>#<json string>" where:
- * - <version> is the versio number of the filter structure used. See constant FILTER_VERSION
- * - # is a separator
- * - <json string> is the json encoded filter array.
- * @param string $p_serialized_filter Serialized filter string.
- * @return mixed $t_filter array
- * @see filter_ensure_valid_filter
- */
- function filter_deserialize( $p_serialized_filter ) {
- if( is_blank( $p_serialized_filter ) ) {
- return false;
- }
- #@TODO cproensa, we could accept a pure json array, without version prefix
- # in this case, the filter version field inside the array is to be used
- # and if not present, set the current filter version
- # check filter version mark
- $t_setting_arr = explode( '#', $p_serialized_filter, 2 );
- $t_version_string = $t_setting_arr[0];
- if( in_array( $t_version_string, array( 'v1', 'v2', 'v3', 'v4' ) ) ) {
- # these versions can't be salvaged, they are too old to update
- return false;
- } elseif( in_array( $t_version_string, array( 'v5', 'v6', 'v7', 'v8' ) ) ) {
- # filters from v5 onwards should cope with changing filter indices dynamically
- $t_filter_array = unserialize( $t_setting_arr[1] );
- } else {
- # filters from v9 onwards are stored as json
- $t_filter_array = json_decode( $t_setting_arr[1], /* assoc array */ true );
- }
- # If the unserialez data is not an array, the some error happened, eg, invalid format
- if( !is_array( $t_filter_array ) ) {
- return false;
- }
- # Set the filter version that was loaded in the array
- $t_filter_array['_version'] = $t_setting_arr[0];
- # If upgrade in filter content is needed, it will be done in filter_ensure_valid_filter()
- return filter_ensure_valid_filter( $t_filter_array );
- }
- /**
- * Creates a serialized filter with the correct format
- * @param array $p_filter_array Filter array to be serialized
- * @return string Serialized filter string
- */
- function filter_serialize( $p_filter_array ) {
- $t_cookie_version = FILTER_VERSION;
- $p_filter_array = filter_clean_runtime_properties( $p_filter_array );
- $t_settings_serialized = json_encode( $p_filter_array );
- $t_settings_string = $t_cookie_version . '#' . $t_settings_serialized;
- return $t_settings_string;
- }
- /**
- * Get the filter db row $p_filter_id
- * using the cached row if it's available
- * @global array $g_cache_filter_db_rows
- * @param integer $p_filter_id A filter identifier to look up in the database.
- * @return array|boolean The row of filter data as stored in db table, or false if does not exist
- */
- function filter_get_row( $p_filter_id ) {
- global $g_cache_filter_db_rows;
- if( !isset( $g_cache_filter_db_rows[$p_filter_id] ) ) {
- filter_cache_rows( array($p_filter_id) );
- }
- $t_row = $g_cache_filter_db_rows[$p_filter_id];
- return $t_row;
- }
- /**
- * Get the value of the filter field specified by filter id and field name
- * @param integer $p_filter_id A filter identifier to look up in the database.
- * @param string $p_field_name Name of the filter field to retrieve.
- * @return string
- */
- function filter_get_field( $p_filter_id, $p_field_name ) {
- $t_row = filter_get_row( $p_filter_id );
- if( isset( $t_row[$p_field_name] ) ) {
- return $t_row[$p_field_name];
- } else {
- error_parameters( $p_field_name );
- trigger_error( ERROR_DB_FIELD_NOT_FOUND, WARNING );
- return '';
- }
- }
- /**
- * Add sort parameters to the query clauses
- * @param array &$p_filter Filter to sort.
- * @param boolean $p_show_sticky Whether to show sticky items.
- * @param array $p_query_clauses Array of query clauses.
- * @return array $p_query_clauses
- *
- * @deprecated Use BugFilterQuery class
- */
- function filter_get_query_sort_data( array &$p_filter, $p_show_sticky, array $p_query_clauses ) {
- error_parameters( __FUNCTION__ . '()', 'BugFilterQuery class' );
- trigger_error( ERROR_DEPRECATED_SUPERSEDED, DEPRECATED );
- $p_query_clauses['order'] = array();
- # Get only the visible, and sortable, column properties
- # @TODO cproensa: this defaults to COLUMNS_TARGET_VIEW_PAGE
- # are we sure that filters are only used with the column set for view page?
- $p_sort_properties = filter_get_visible_sort_properties_array( $p_filter );
- $t_sort_fields = $p_sort_properties[FILTER_PROPERTY_SORT_FIELD_NAME];
- $t_dir_fields = $p_sort_properties[FILTER_PROPERTY_SORT_DIRECTION];
- if( gpc_string_to_bool( $p_filter[FILTER_PROPERTY_STICKY] ) && ( null !== $p_show_sticky ) ) {
- $p_query_clauses['order'][] = '{bug}.sticky DESC';
- }
- $t_included_project_ids = $p_query_clauses['metadata']['included_projects'];
- $t_user_id = $p_query_clauses['metadata']['user_id'];
- $t_count = count( $t_sort_fields );
- for( $i = 0; $i < $t_count; $i++ ) {
- $c_sort = $t_sort_fields[$i];
- $c_dir = 'DESC' == $t_dir_fields[$i] ? 'DESC' : 'ASC';
- # if sorting by a custom field
- if( column_is_custom_field( $c_sort ) ) {
- $t_custom_field = column_get_custom_field_name( $c_sort );
- $t_custom_field_id = custom_field_get_id_from_name( $t_custom_field );
- $t_def = custom_field_get_definition( $t_custom_field_id );
- $t_value_field = ( $t_def['type'] == CUSTOM_FIELD_TYPE_TEXTAREA ? 'text' : 'value' );
- $t_table_name = '';
- # if the custom field was filtered, there is already a calculated join, so reuse that table alias
- # otherwise, a new join must be calculated
- if( isset( $p_query_clauses['metadata']['cf_alias'][$t_custom_field_id] ) ) {
- $t_table_name = $p_query_clauses['metadata']['cf_alias'][$t_custom_field_id];
- } else {
- # @TODO This code for CF visibility is the same as filter_get_bug_rows_query_clauses()
- # It should be encapsulated and reused
- $t_searchable_projects = array_intersect( $t_included_project_ids, custom_field_get_project_ids( $t_custom_field_id ) );
- $t_projects_can_view_field = access_project_array_filter( (int)$t_def['access_level_r'], $t_searchable_projects, $t_user_id );
- if( empty( $t_projects_can_view_field ) ) {
- continue;
- }
- $t_table_name = 'cf_sort_' . $t_custom_field_id;
- $t_cf_join_clause = 'LEFT OUTER JOIN {custom_field_string} ' . $t_table_name . ' ON {bug}.id = ' . $t_table_name . '.bug_id AND ' . $t_table_name . '.field_id = ' . $t_custom_field_id;
- # This diff will contain those included projects that can't view this custom field
- $t_diff = array_diff( $t_included_project_ids, $t_projects_can_view_field );
- # If not empty, it means there are some projects that can't view the field values,
- # so a project filter must be used to not include values from those projects
- if( !empty( $t_diff ) ) {
- $t_cf_join_clause .= ' AND {bug}.project_id IN (' . implode( ',', $t_projects_can_view_field ) . ')';
- }
- $p_query_clauses['metadata']['cf_alias'][$t_custom_field_id] = $t_table_name;
- $p_query_clauses['join'][] = $t_cf_join_clause;
- }
- # if no join can be used (eg, no view access), skip this field from the order clause
- if( empty( $t_table_name ) ) {
- continue;
- }
- $t_field_alias = 'cf_sortfield_' . $t_custom_field_id;
- $t_sort_col = $t_table_name . '.' . $t_value_field;
- # which types need special type cast
- switch( $t_def['type'] ) {
- case CUSTOM_FIELD_TYPE_FLOAT:
- # mysql can't cast to float, use alternative syntax
- $t_sort_expr = db_is_mysql() ? $t_sort_col . '+0.0' : 'CAST(NULLIF(' . $t_sort_col . ',\'\') AS FLOAT)';
- break;
- case CUSTOM_FIELD_TYPE_DATE:
- case CUSTOM_FIELD_TYPE_NUMERIC:
- $t_sort_expr = 'CAST(NULLIF(' . $t_sort_col . ',\'\') AS DECIMAL)';
- break;
- default: # no cast needed
- $t_sort_expr = $t_sort_col;
- }
- # which types need special treatment for null sorting
- switch( $t_def['type'] ) {
- case CUSTOM_FIELD_TYPE_DATE:
- case CUSTOM_FIELD_TYPE_NUMERIC:
- case CUSTOM_FIELD_TYPE_FLOAT:
- $t_null_last = true;
- break;
- default:
- $t_null_last = false;
- }
- if( $t_null_last ) {
- $t_null_expr = 'CASE WHEN NULLIF(' . $t_sort_col . ', \'\') IS NULL THEN 1 ELSE 0 END';
- $t_clause_for_select = $t_null_expr . ' AS ' . $t_field_alias . '_null';
- $t_clause_for_select .= ', ' . $t_sort_expr . ' AS ' . $t_field_alias;
- $t_clause_for_order = $t_field_alias . '_null ASC, ' . $t_field_alias . ' ' . $c_dir;
- } else {
- $t_clause_for_select = $t_sort_expr . ' AS ' . $t_field_alias;
- $t_clause_for_order = $t_field_alias . ' ' . $c_dir;
- }
- # Note: pgsql needs the sort expression to appear as member of the "select distinct"
- $p_query_clauses['select'][] = $t_clause_for_select;
- $p_query_clauses['order'][] = $t_clause_for_order;
- # if sorting by plugin columns
- } else if( column_is_plugin_column( $c_sort ) ) {
- $t_plugin_columns = columns_get_plugin_columns();
- $t_column_object = $t_plugin_columns[$c_sort];
- $t_clauses = $t_column_object->sortquery( $c_dir );
- if( is_array( $t_clauses ) ) {
- if( isset( $t_clauses['join'] ) ) {
- $p_query_clauses['join'][] = $t_clauses['join'];
- }
- if( isset( $t_clauses['order'] ) ) {
- $p_query_clauses['order'][] = $t_clauses['order'];
- }
- }
- # standard column
- } else {
- $t_sort_col = '{bug}.' . $c_sort;
- # When sorting by due_date, always display undefined dates last.
- # Undefined date is defaulted as "1" in database, so add a special
- # sort clause to group and sort by this.
- if( 'due_date' == $c_sort && 'ASC' == $c_dir ) {
- $t_null_expr = 'CASE ' . $t_sort_col . ' WHEN 1 THEN 1 ELSE 0 END';
- $p_query_clauses['select'][] = $t_null_expr . ' AS due_date_sort_null';
- $p_query_clauses['order'][] = 'due_date_sort_null ASC';
- }
- # main sort clause for due date
- $p_query_clauses['order'][] = $t_sort_col . ' ' .$c_dir;
- }
- }
- # add basic sorting if necessary
- if( !in_array( 'last_updated', $t_sort_fields ) ) {
- $p_query_clauses['order'][] = '{bug}.last_updated DESC';
- }
- if( !in_array( 'date_submitted', $t_sort_fields ) ) {
- $p_query_clauses['order'][] = '{bug}.date_submitted DESC';
- }
- return $p_query_clauses;
- }
- /**
- * Remove any duplicate values in certain elements of query_clauses
- * Do not loop over query clauses as some keys may contain valid duplicate values.
- * We basically want unique values for just the base query elements select, from, and join
- * 'where' and 'where_values' key should not have duplicates as that is handled earlier and applying
- * array_unique here could cause problems with the query.
- * @param array $p_query_clauses Array of query clauses.
- * @return array
- *
- * @deprecated Use BugFilterQuery class
- */
- function filter_unique_query_clauses( array $p_query_clauses ) {
- error_parameters( __FUNCTION__ . '()', 'BugFilterQuery class' );
- trigger_error( ERROR_DEPRECATED_SUPERSEDED, DEPRECATED );
- $p_query_clauses['select'] = array_unique( $p_query_clauses['select'] );
- $p_query_clauses['from'] = array_unique( $p_query_clauses['from'] );
- $p_query_clauses['join'] = array_unique( $p_query_clauses['join'] );
- return $p_query_clauses;
- }
- /**
- * Build a query with the query clauses array, query for bug count and return the result
- *
- * Note: The parameter $p_pop_param can be used as 'false' to keep db_params in the stack,
- * if the same query clauses object is reused for several queries. In that case a db_param_pop()
- * should be used manually when required.
- * This is the case when "filter_get_bug_count" is used followed by "filter_get_bug_rows_result"
- * @param array $p_query_clauses Array of query cla…
Large files files are truncated, but you can click here to view the full file