2016-06-13 18:40:44 +02:00
< ? php
if ( ! defined ( '_PS_VERSION_' ))
exit ;
class BlockLayeredOverride extends BlockLayered
{
2016-07-20 12:07:50 +02:00
public function hookHeader ( $params )
{
global $smarty , $cookie ;
// No filters => module disable
if ( $filter_block = $this -> getFilterBlock ( $this -> getSelectedFilters ()))
if ( $filter_block [ 'nbr_filterBlocks' ] == 0 )
return false ;
if ( Tools :: getValue ( 'id_category' , Tools :: getValue ( 'id_category_layered' , Configuration :: get ( 'PS_HOME_CATEGORY' ))) == Configuration :: get ( 'PS_HOME_CATEGORY' ))
return ;
$id_lang = ( int ) $cookie -> id_lang ;
$category = new Category (( int ) Tools :: getValue ( 'id_category' ));
// Generate meta title and meta description
$category_title = ( empty ( $category -> meta_title [ $id_lang ]) ? $category -> name [ $id_lang ] : $category -> meta_title [ $id_lang ]);
$category_metas = Meta :: getMetaTags ( $id_lang , 'category' );
$title = '' ;
$keywords = '' ;
if ( is_array ( $filter_block [ 'title_values' ]))
foreach ( $filter_block [ 'title_values' ] as $key => $val )
{
$title .= ' > ' . $key . ' ' . implode ( '/' , $val );
$keywords .= $key . ' ' . implode ( '/' , $val ) . ', ' ;
}
$title = $category_title . $title ;
if ( ! empty ( $title ))
$smarty -> assign ( 'meta_title' , $title . ' - ' . Configuration :: get ( 'PS_SHOP_NAME' ));
else
$smarty -> assign ( 'meta_title' , $category_metas [ 'meta_title' ]);
$smarty -> assign ( 'meta_description' , $category_metas [ 'meta_description' ]);
$keywords = substr ( strtolower ( $keywords ), 0 , 1000 );
if ( ! empty ( $keywords ))
$smarty -> assign ( 'meta_keywords' , rtrim ( $category_title . ', ' . $keywords . ', ' . $category_metas [ 'meta_keywords' ], ', ' ));
$this -> context -> controller -> addJS (( $this -> _path ) . 'blocklayered.js' );
$this -> context -> controller -> addJS ( _PS_JS_DIR_ . 'jquery/jquery-ui-1.8.10.custom.min.js' );
$this -> context -> controller -> addJQueryUI ( 'ui.slider' );
$this -> context -> controller -> addJS ( _THEME_JS_DIR_ . 'tools/touch-punch.js' );
$this -> context -> controller -> addCSS ( _PS_CSS_DIR_ . 'jquery-ui-1.8.10.custom.css' );
if ( version_compare ( _PS_VERSION_ , '1.6.0' , '>=' ) === true )
$this -> context -> controller -> addCSS (( $this -> _path ) . 'blocklayered.css' , 'all' );
else
$this -> context -> controller -> addCSS (( $this -> _path ) . 'blocklayered-15.css' , 'all' );
$this -> context -> controller -> addJQueryPlugin ( 'scrollTo' );
$filters = $this -> getSelectedFilters ();
// Get non indexable attributes
$attribute_group_list = Db :: getInstance () -> executeS ( 'SELECT id_attribute_group FROM ' . _DB_PREFIX_ . 'layered_indexable_attribute_group WHERE indexable = 0' );
// Get non indexable features
$feature_list = Db :: getInstance () -> executeS ( 'SELECT id_feature FROM ' . _DB_PREFIX_ . 'layered_indexable_feature WHERE indexable = 0' );
2016-06-13 18:40:44 +02:00
2016-07-20 12:07:50 +02:00
$attributes = array ();
$features = array ();
$blacklist = array ( 'weight' , 'price' );
if ( ! Configuration :: get ( 'PS_LAYERED_FILTER_INDEX_CDT' ))
$blacklist [] = 'condition' ;
if ( ! Configuration :: get ( 'PS_LAYERED_FILTER_INDEX_QTY' ))
$blacklist [] = 'quantity' ;
if ( ! Configuration :: get ( 'PS_LAYERED_FILTER_INDEX_MNF' ))
$blacklist [] = 'manufacturer' ;
if ( ! Configuration :: get ( 'PS_LAYERED_FILTER_INDEX_CAT' ))
$blacklist [] = 'category' ;
foreach ( $filters as $type => $val )
{
switch ( $type )
{
case 'id_attribute_group' :
foreach ( $val as $attr )
{
$attr_id = preg_replace ( '/_\d+$/' , '' , $attr );
if ( in_array ( $attr_id , $attributes ) || in_array ( array ( 'id_attribute_group' => $attr_id ), $attribute_group_list ))
{
$smarty -> assign ( 'nobots' , true );
$smarty -> assign ( 'nofollow' , true );
return ;
}
$attributes [] = $attr_id ;
}
break ;
case 'id_feature' :
foreach ( $val as $feat )
{
$feat_id = preg_replace ( '/_\d+$/' , '' , $feat );
if ( in_array ( $feat_id , $features ) || in_array ( array ( 'id_feature' => $feat_id ), $feature_list ))
{
$smarty -> assign ( 'nobots' , true );
$smarty -> assign ( 'nofollow' , true );
return ;
}
$features [] = $feat_id ;
}
break ;
default :
if ( in_array ( $type , $blacklist ))
{
if ( count ( $val ))
{
$smarty -> assign ( 'nobots' , true );
$smarty -> assign ( 'nofollow' , true );
return ;
}
}
elseif ( count ( $val ) > 1 )
{
$smarty -> assign ( 'nobots' , true );
$smarty -> assign ( 'nofollow' , true );
return ;
}
break ;
}
}
}
private function getSelectedFilters ()
{
$home_category = Configuration :: get ( 'PS_HOME_CATEGORY' );
$id_parent = ( int ) Tools :: getValue ( 'id_category' , Tools :: getValue ( 'id_category_layered' , $home_category ));
if ( $id_parent == $home_category )
return ;
// Force attributes selection (by url '.../2-mycategory/color-blue' or by get parameter 'selected_filters')
if ( strpos ( $_SERVER [ 'SCRIPT_FILENAME' ], 'blocklayered-ajax.php' ) === false || Tools :: getValue ( 'selected_filters' ) !== false )
{
if ( Tools :: getValue ( 'selected_filters' ))
$url = Tools :: getValue ( 'selected_filters' );
else
$url = preg_replace ( '/\/(?:\w*)\/(?:[0-9]+[-\w]*)([^\?]*)\??.*/' , '$1' , Tools :: safeOutput ( $_SERVER [ 'REQUEST_URI' ], true ));
$url_attributes = explode ( '/' , ltrim ( $url , '/' ));
$selected_filters = array ( 'category' => array ());
if ( ! empty ( $url_attributes ))
{
foreach ( $url_attributes as $url_attribute )
{
/* Pagination uses - as separator, can be different from $this->getAnchor()*/
if ( strpos ( $url_attribute , 'page-' ) === 0 )
$url_attribute = str_replace ( '-' , $this -> getAnchor (), $url_attribute );
$url_parameters = explode ( $this -> getAnchor (), $url_attribute );
$attribute_name = array_shift ( $url_parameters );
if ( $attribute_name == 'page' )
$this -> page = ( int ) $url_parameters [ 0 ];
else if ( in_array ( $attribute_name , array ( 'price' , 'weight' )))
$selected_filters [ $attribute_name ] = array ( $this -> filterVar ( $url_parameters [ 0 ]), $this -> filterVar ( $url_parameters [ 1 ]));
else
{
foreach ( $url_parameters as $url_parameter )
{
$data = Db :: getInstance () -> getValue ( 'SELECT data FROM `' . _DB_PREFIX_ . 'layered_friendly_url` WHERE `url_key` = \'' . md5 ( '/' . $attribute_name . $this -> getAnchor () . $url_parameter ) . '\'' );
if ( $data )
foreach ( Tools :: unSerialize ( $data ) as $key_params => $params )
{
if ( ! isset ( $selected_filters [ $key_params ]))
$selected_filters [ $key_params ] = array ();
foreach ( $params as $key_param => $param )
{
if ( ! isset ( $selected_filters [ $key_params ][ $key_param ]))
$selected_filters [ $key_params ][ $key_param ] = array ();
$selected_filters [ $key_params ][ $key_param ] = $this -> filterVar ( $param );
}
}
}
}
}
return $selected_filters ;
}
}
/* Analyze all the filters selected by the user and store them into a tab */
$selected_filters = array ( 'category' => array (), 'manufacturer' => array (), 'quantity' => array (), 'condition' => array ());
foreach ( $_GET as $key => $value )
if ( substr ( $key , 0 , 8 ) == 'layered_' )
{
preg_match ( '/^(.*)_([0-9]+|new|used|refurbished|slider)$/' , substr ( $key , 8 , strlen ( $key ) - 8 ), $res );
if ( isset ( $res [ 1 ]))
{
$tmp_tab = explode ( '_' , $this -> filterVar ( $value ));
$value = $this -> filterVar ( $tmp_tab [ 0 ]);
$id_key = false ;
if ( isset ( $tmp_tab [ 1 ]))
$id_key = $tmp_tab [ 1 ];
if ( $res [ 1 ] == 'condition' && in_array ( $value , array ( 'new' , 'used' , 'refurbished' )))
$selected_filters [ 'condition' ][] = $value ;
else if ( $res [ 1 ] == 'quantity' && ( ! $value || $value == 1 ))
$selected_filters [ 'quantity' ][] = $value ;
else if ( in_array ( $res [ 1 ], array ( 'category' , 'manufacturer' )))
{
if ( ! isset ( $selected_filters [ $res [ 1 ] . ( $id_key ? '_' . $id_key : '' )]))
$selected_filters [ $res [ 1 ] . ( $id_key ? '_' . $id_key : '' )] = array ();
$selected_filters [ $res [ 1 ] . ( $id_key ? '_' . $id_key : '' )][] = ( int ) $value ;
}
else if ( in_array ( $res [ 1 ], array ( 'id_attribute_group' , 'id_feature' )))
{
if ( ! isset ( $selected_filters [ $res [ 1 ]]))
$selected_filters [ $res [ 1 ]] = array ();
$selected_filters [ $res [ 1 ]][( int ) $value ] = $id_key . '_' . ( int ) $value ;
}
else if ( $res [ 1 ] == 'weight' )
$selected_filters [ $res [ 1 ]] = $tmp_tab ;
else if ( $res [ 1 ] == 'price' )
$selected_filters [ $res [ 1 ]] = $tmp_tab ;
}
}
return $selected_filters ;
}
public function generateFiltersBlock ( $selected_filters )
{
global $smarty ;
if ( $filter_block = $this -> getFilterBlock ( $selected_filters ))
{
if ( $filter_block [ 'nbr_filterBlocks' ] == 0 )
return false ;
$translate = array ();
$translate [ 'price' ] = mb_strtolower ( $this -> l ( 'price' ));
$translate [ 'weight' ] = mb_strtolower ( $this -> l ( 'weight' ));
$smarty -> assign ( $filter_block );
$smarty -> assign ( array (
'hide_0_values' => Configuration :: get ( 'PS_LAYERED_HIDE_0_VALUES' ),
'blocklayeredSliderName' => $translate ,
'col_img_dir' => _PS_COL_IMG_DIR_
));
return $this -> display ( __FILE__ , 'blocklayered.tpl' );
}
else
return false ;
}
2016-06-13 18:40:44 +02:00
public function hookDisplayFilters ( $params )
{
return $this -> hookLeftColumn ( $params );
}
}