HomeHelpTrac

Changeset 15929 for trunk


Ignore:
Timestamp:
01/25/12 23:54:54 (4 months ago)
Author:
Alexander Trofimov
Message:

Grid - search in translatable fields

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/inc/classes/BxDolGrid.php

    r15903 r15929  
    6161 * - paginate_get_start: GET variable name for 'start'. 
    6262 * - paginate_get_per_page: GET variable name for 'per_page'. 
    63  * - filter_fields: comma separated field names, to search in. 
     63 * - filter_fields: comma separated list of field names to search in; if field contains language key then it is better to specify it in 'filter_fields_translatable'. 
     64 * - filter_fields_translatable: comma separated list of field names to search in its translations; enter field name here if field contains language key. 
    6465 * - filter_mode: search mode: 
    6566 *      - like: use SQL LIKE expression for search, if 'source_type' is not 'Sql' it doesn't matter. 
     
    308309         
    309310        // apply filter 
    310         if ($sFilter && !empty($this->_aOptions['filter_fields'])) { 
     311        if ($sFilter && (!empty($this->_aOptions['filter_fields']) || !empty($this->_aOptions['filter_fields_translatable']))) { 
    311312            $aSource = array(); 
    312313            foreach ($this->_aOptions['source'] as $aRow) { 
    313                 foreach ($this->_aOptions['filter_fields'] as $sField) { 
    314                     if (empty($aRow[$sField]) || false === strpos($aRow[$sField], $sFilter)) 
    315                         continue; 
    316                     $aSource[] = $aRow; 
    317                     break; 
     314                $bFound = false; 
     315                if (!empty($this->_aOptions['filter_fields'])) { 
     316                    foreach ($this->_aOptions['filter_fields'] as $sField) { 
     317                        if (empty($aRow[$sField]) || false === stripos($aRow[$sField], $sFilter)) 
     318                            continue; 
     319                        $aSource[] = $aRow; 
     320                        $bFound = true; 
     321                        break; 
     322                    } 
     323                } 
     324                if (!$bFound && !empty($this->_aOptions['filter_fields_translatable'])) { 
     325                    foreach ($this->_aOptions['filter_fields_translatable'] as $sField) { 
     326                        if (empty($aRow[$sField]) || false === stripos(_t($aRow[$sField]), $sFilter)) 
     327                            continue; 
     328                        $aSource[] = $aRow; 
     329                        $bFound = true; 
     330                        break; 
     331                    } 
    318332                } 
    319333            } 
     
    353367        // add filter condition 
    354368        $sOrderByFilter = ''; 
    355         if ($sFilter && !empty($this->_aOptions['filter_fields'])) {             
     369        if ($sFilter && (!empty($this->_aOptions['filter_fields']) || !empty($this->_aOptions['filter_fields_translatable']))) { 
    356370 
    357371            $sMode = $this->_aOptions['filter_mode']; 
     
    360374 
    361375            $sCond = ''; 
    362             if ('like' == $sMode) { 
    363                 foreach ($this->_aOptions['filter_fields'] as $sField) 
    364                     $sCond .= $oDb->prepare("`{$sField}` LIKE ? OR ", '%' . $sFilter . '%'); 
     376            if ('like' == $sMode) { // LIKE search 
     377 
     378                // condition for regular fields 
     379                if (!empty($this->_aOptions['filter_fields'])) 
     380                    foreach ($this->_aOptions['filter_fields'] as $sField) 
     381                        $sCond .= $oDb->prepare("`{$sField}` LIKE ? OR ", '%' . $sFilter . '%'); 
     382 
     383                // condition for translatable fields 
     384                if (!empty($this->_aOptions['filter_fields_translatable'])) { 
     385                    $sCondFields = ''; 
     386                    foreach ($this->_aOptions['filter_fields_translatable'] as $sField) 
     387                        $sCondFields .= "`k`.`Key` = `{$sField}` OR ";  
     388 
     389                    $sCondFields = rtrim($sCondFields, ' OR '); 
     390 
     391                    if ($sCondFields) 
     392                        $sCond .= $oDb->prepare("(SELECT 1 FROM `sys_localization_strings` AS `s` INNER JOIN `sys_localization_keys` AS `k` ON (`k`.`ID` = `s`.`IDKey`) WHERE `s`.`string` LIKE ? AND ($sCondFields) LIMIT 1) OR ", '%' . $sFilter . '%'); 
     393                } 
     394 
    365395                $sCond = rtrim($sCond, ' OR '); 
    366             } else { 
    367                 foreach ($this->_aOptions['filter_fields'] as $sField) 
    368                     $sCond .= "`{$sField}`,";  
    369                 $sCond = rtrim($sCond, ','); 
    370                 if ($sCond) { 
    371                     $sCond = $oDb->prepare(" MATCH ($sCond) AGAINST (?) ", $sFilter); 
    372                     $sOrderByFilter = $sCond; 
    373                     $sCond .= ' > 1 '; 
     396 
     397            } else { // FULLTEXT search 
     398 
     399                // condition for regular fields 
     400                if (!empty($this->_aOptions['filter_fields'])) { 
     401 
     402                    $sCondFields = ''; 
     403                    foreach ($this->_aOptions['filter_fields'] as $sField) 
     404                        $sCondFields .= "`{$sField}`,";  
     405 
     406                    $sCondFields = rtrim($sCondFields, ','); 
     407 
     408                    if ($sCondFields) { 
     409                        $sCond = $oDb->prepare(" MATCH ($sCondFields) AGAINST (?) ", $sFilter); 
     410                        $sOrderByFilter = $sCond; 
     411                        $sCond .= ' > 1 OR '; 
     412                    } 
    374413                } 
    375             }             
     414 
     415                // condition for translatable fields 
     416                if (!empty($this->_aOptions['filter_fields_translatable'])) { 
     417 
     418                    $sCondFields = ''; 
     419                    foreach ($this->_aOptions['filter_fields_translatable'] as $sField) 
     420                        $sCondFields .= "`k`.`Key` = `{$sField}` OR "; 
     421 
     422                    $sCondFields = rtrim($sCondFields, ' OR '); 
     423 
     424                    if ($sCondFields) 
     425                        $sCond .= $oDb->prepare("(SELECT 1 FROM `sys_localization_strings` AS `s` INNER JOIN `sys_localization_keys` AS `k` ON (`k`.`ID` = `s`.`IDKey`) WHERE MATCH (`s`.`string`) AGAINST (?) AND ($sCondFields) LIMIT 1) OR ", $sFilter); 
     426                } 
     427 
     428                $sCond = rtrim($sCond, ' OR '); 
     429            } 
    376430 
    377431            if ($sCond) 
     
    418472    protected function _isVisibleGrid ($a) { 
    419473        bx_import('BxDolAcl'); 
     474        if (!isset($a['visible_for_levels'])) 
     475            return true; 
    420476        return BxDolAcl::getInstance()->isMemberLevelInSet($a['visible_for_levels']); 
    421477    } 
  • trunk/inc/classes/BxDolGridQuery.php

    r15774 r15929  
    3636        if ($aObject['filter_fields']) 
    3737            $aObject['filter_fields'] = array_map('trim', explode(',', $aObject['filter_fields'])); 
     38 
     39        if ($aObject['filter_fields_translatable']) 
     40            $aObject['filter_fields_translatable'] = array_map('trim', explode(',', $aObject['filter_fields_translatable'])); 
    3841 
    3942        // sorting 
  • trunk/install/sql/v70.sql

    r15924 r15929  
    46034603  `paginate_get_per_page` varchar(255) NOT NULL, 
    46044604  `filter_fields` text NOT NULL, 
     4605  `filter_fields_translatable` text NOT NULL, 
    46054606  `filter_mode` enum('like','fulltext','auto') NOT NULL DEFAULT 'auto', 
    46064607  `sorting_fields` text NOT NULL, 
  • trunk/templates/base/scripts/BxBaseGrid.php

    r15887 r15929  
    209209                    ), 
    210210                    'bx_if:filter' => array ( 
    211                         'condition' => !empty($this->_aOptions['filter_fields']), 
     211                        'condition' => !empty($this->_aOptions['filter_fields']) || !empty($this->_aOptions['filter_fields_translatable']), 
    212212                        'content' => array( 
    213213                            'controls' => $this->_getFilterControls(), 
Note: See TracChangeset for help on using the changeset viewer.