notice_tpl=$pmb_compare_notice_template;
$this->notice_nb=$pmb_compare_notice_nb;
$this->facette_compare = static::get_compare_checked_session();
$this->facette_groupby = static::get_groupby_checked_session();
}
public function get_display_parameters(){
global $tpl_display_compare,$msg,$charset;
$tpl_display_compare = str_replace('!!notice_nb_libelle!!', htmlentities($this->notice_nb,ENT_QUOTES,$charset), $tpl_display_compare);
if($this->notice_tpl) {
$tpl = notice_tpl_gen::get_instance($this->notice_tpl);
$tpl_display_compare = str_replace('!!notice_tpl_libelle!!',htmlentities($tpl->name,ENT_QUOTES,$charset) , $tpl_display_compare);
} else {
$tpl_display_compare = str_replace('!!notice_tpl_libelle!!',htmlentities($msg['notice_tpl_list_default'],ENT_QUOTES,$charset) , $tpl_display_compare);
}
return $tpl_display_compare;
}
public function get_form(){
global $tpl_form_compare;
$interface_form = new interface_admin_form('form_compare');
$sel_notice_tpl=notice_tpl_gen::gen_tpl_select("notice_tpl",$this->notice_tpl,'');
$content_form=$tpl_form_compare;
$content_form = str_replace('!!notice_nb!!', $this->notice_nb, $content_form);
$content_form = str_replace('!!sel_notice_tpl!!', $sel_notice_tpl, $content_form);
$interface_form->set_content_form($content_form);
return $interface_form->get_display_parameters();
}
public function save_form(){
global $pmb_compare_notice_template,$pmb_compare_notice_nb;
global $notice_tpl;
global $notice_nb;
$this->notice_tpl=$notice_tpl*1;
$this->notice_nb=$notice_nb*1;
$query="UPDATE parametres SET valeur_param='".$this->notice_tpl."' WHERE type_param='pmb' AND sstype_param='compare_notice_template'";
pmb_mysql_query($query);
$query="UPDATE parametres SET valeur_param='".$this->notice_nb."' WHERE type_param='pmb' AND sstype_param='compare_notice_nb'";
pmb_mysql_query($query);
$pmb_compare_notice_template=$this->notice_tpl;
$pmb_compare_notice_nb=$this->notice_nb;
return true;
}
/**
* Génére un nom de table temporaire
* @param string $prefix
*/
public static function gen_temporary_table_name($prefix='compare_table'){
static::$temporary_table_name = $prefix.md5(microtime(true));
}
/**
* Tableau d'identifiants d'objets
*/
protected function get_objects_compare($facette_compare) {
$objects_ids = array();
$query="SELECT DISTINCT(notice_id) as object_id FROM notices_fields_global_index
JOIN ".static::$temporary_table_name." ON notices_fields_global_index.id_notice=".static::$temporary_table_name.".notice_id
WHERE notices_fields_global_index.code_champ='$facette_compare[2]'
AND notices_fields_global_index.code_ss_champ='$facette_compare[3]'
AND notices_fields_global_index.value='".addslashes($facette_compare[1])."'";
$result=pmb_mysql_query($query);
while($row=pmb_mysql_fetch_object($result)){
$objects_ids[]=$row->object_id;
}
return $objects_ids;
}
protected function get_query_groupby($facette_groupby, $tmpArray) {
global $lang;
$query = "SELECT value,id_notice FROM notices_fields_global_index
WHERE id_notice IN (".implode(",", $tmpArray).")
AND notices_fields_global_index.code_champ='$facette_groupby[1]'
AND notices_fields_global_index.code_ss_champ='$facette_groupby[2]'
AND (notices_fields_global_index.lang='$lang' OR notices_fields_global_index.lang='')";
return $query;
}
protected function add_result($group_label, $value, $pos, $key, $groupby_key, $tmpArray=array()) {
$this->result[$group_label][$pos] = array(
'count' => count($tmpArray),
'value' => $value,
'pos' => $pos,
'key' => $key,
'groupby_key' => $groupby_key,
'notices_ids' => implode(",", $tmpArray)
);
}
protected function build_result() {
global $msg;
$pos=0;
foreach($this->facette_compare as $key=>$facette_compare){
//on remonte les ID de notices, dans la liste des ID de notices déjà prente dans le recherche et qui ont une correspondance avec la valeur de la facette
$tmpArray = $this->get_objects_compare($facette_compare);
//on construit les entete du tableau
if(!in_array($facette_compare[1], $this->headers)){
$pos++;
$this->headers[$pos]=$facette_compare[1];
}
if (!empty($tmpArray)) {
//et on organise par critère de regroupement.
if (!empty($this->facette_groupby)) {
foreach ($this->facette_groupby as $key_groupby => $facette_groupby) {
// je regroupe sous les valeurs de la facette choisie pour le regroupement
$query = $this->get_query_groupby($facette_groupby, $tmpArray);
$result=pmb_mysql_query($query);
$tmpArray=array_flip($tmpArray);
while($line=pmb_mysql_fetch_object($result)){
if(!$this->result[$line->value][$pos]['count']){
$this->add_result($line->value, $facette_compare[1], $pos, $key, $key_groupby);
$this->result[$line->value][$pos]['count']=1;
} else {
$this->result[$line->value][$pos]['count']++;
}
if($this->result[$line->value][$pos]['notices_ids']){
$this->result[$line->value][$pos]['notices_ids'].=",";
}
$this->result[$line->value][$pos]['notices_ids'].=$line->id_notice;
unset($tmpArray[$line->id_notice]);
}
if (!empty($tmpArray)) {
$tmpArray = array_flip($tmpArray);
//pas de valeur de regroupement, je regroupe sous le message facettes_not_grouped
$this->add_result($msg['facettes_not_grouped'], $facette_compare[1], $pos, $key, $key_groupby, $tmpArray);
}
}
} else {
//pas de valeur de regroupement, je regroupe sous le message facettes_not_grouped
$this->add_result($msg['facettes_not_grouped'], $facette_compare[1], $pos, $key, 0, $tmpArray);
}
}
//le tri du résultat
if (!empty($this->result)) {
self::sort_compare($this->result);
}
}
}
/**
* On lance la comparaison à partir d'un résultat de recherche
* Rempli la variables result
* @param object searcher $searcher
* @return true si succès message d'erreur sinon
*/
public function compare($searcher){
self::session_facette_compare($this);
if (!empty($this->facette_compare)) {
$listeNotices = "0";
if($searcher->get_nb_results()){
$listeNotices = $searcher->get_result();
}
//on insert les notices du searcher en table memoire
self::gen_temporary_table_name();
$query = "CREATE TEMPORARY TABLE ".static::$temporary_table_name." engine=memory SELECT notice_id FROM notices WHERE notice_id IN (".$listeNotices.")";
pmb_mysql_query($query);
$query = "ALTER TABLE ".static::$temporary_table_name." engine=memory ADD INDEX notice_id_index BTREE (notice_id)";
pmb_mysql_query($query);
//pour toutes les facettes choisies en comparaison
$this->build_result();
//Si trop de résultat, la génération du tableau html sera trop longue = on coupe.
if ((count($this->result) * count($this->facette_compare)) > $this->max_display) {
return 'facette_compare_too_more_result';
}
return true;
}else{
//pas de résultat
return 'facette_compare_no_result';
}
}
/**
* La fonction d'affichage du comparateur de notices
* @return string affichage en mode comparateur
*/
public function display_compare(){
global $base_path,$charset,$msg;
global $facette_search_compare_wrapper;
global $facette_search_compare_header;
global $facette_search_compare_line;
global $facette_search_compare_element;
global $facette_search_compare_hidden_line;
global $facette_search_compare_hidden_element;
//script
$compare_wrapper_script="";
$facette_search_compare_wrapper=str_replace("!!compare_wrapper_script!!", $compare_wrapper_script, $facette_search_compare_wrapper);
$body="";
$header="";
if (!empty($this->result)) {
//Les entetes
foreach ($this->headers as $compareHeader) {
$header.= $facette_search_compare_header;
$header = str_replace("!!compare_hearder_libelle!!", $compareHeader, $header);
}
//les tailles CSS
$facette_search_compare_wrapper = str_replace("!!first_collumn_size!!", $this->first_collumn_size, $facette_search_compare_wrapper);
$cullumn_size = round((100 - $this->first_collumn_size) / count($this->headers));
$facette_search_compare_wrapper = str_replace("!!cullumn_size!!", $cullumn_size, $facette_search_compare_wrapper);
//les lignes
$even_odd='even';
foreach($this->result as $groupedby=>$comparedElements){
//une ligne
$line=$facette_search_compare_line;
$line=str_replace("!!even_odd!!",$even_odd , $line);
if($even_odd=='even'){
$even_odd='odd';
}else{
$even_odd='even';
}
$line=str_replace("!!groupedby_libelle!!",$groupedby , $line);
$line=str_replace("!!compare_line_onclick!!",'toggle_hidden_line(this,"compare_hidden_line_'.$groupedby.'")' , $line);
//et la ligne cachée
$hidden_line=$facette_search_compare_hidden_line;
$hidden_line=str_replace("!!compare_hidden_line_id!!", 'compare_hidden_line_'.$groupedby, $hidden_line);
//chacun des elements et elements cachés d'une ligne
$elements="";
$hidden_elements="";
$nb_headers = count($this->headers);
for ($i = 1; $i < $nb_headers + 1; $i++) {
//un element d'une ligne
$element = $facette_search_compare_element;
$hidden_element = $facette_search_compare_hidden_element;
if($comparedElements[$i]['notices_ids']){
$element=str_replace("!!compare_element_libelle!!", $comparedElements[$i]['count'], $element);
$notices=static::call_notice_display($comparedElements[$i]['notices_ids'],$this->notice_nb,$this->notice_tpl);
$hidden_element=str_replace("!!compare_hidden_element_libelle!!", $notices, $hidden_element);
}else{
$element=str_replace("!!compare_element_libelle!!", '', $element);
$hidden_element=str_replace("!!compare_hidden_element_libelle!!", '', $hidden_element);
}
//on renseigne le boutton "..." si besoin
if($comparedElements[$i]['notices_ids']){
$hidden_element=str_replace("!!compare_hidden_line_see_more!!", self::get_compare_see_more($comparedElements[$i]['notices_ids']), $hidden_element);
}else{
$hidden_element=str_replace("!!compare_hidden_line_see_more!!", '', $hidden_element);
}
$elements.=$element;
$hidden_elements.=$hidden_element;
}
$hidden_line=str_replace("!!compare_hidden_line_elements!!", $hidden_elements, $hidden_line);
$line=str_replace("!!compare_line_elements!!", $elements, $line);
//on ajoute la ligne visible et invisible
$body.=$line.$hidden_line;
}
}
//et on injecte le HTML
$facette_search_compare_wrapper=str_replace("!!compare_header!!", $header, $facette_search_compare_wrapper);
$facette_search_compare_wrapper=str_replace("!!compare_body!!", $body, $facette_search_compare_wrapper);
//construction du lien AJAX
if(static::class == 'facettes_external_search_compare') {
$facette_search_compare_wrapper=str_replace("!!categ!!", "facettes_external", $facette_search_compare_wrapper);
} else {
$facette_search_compare_wrapper=str_replace("!!categ!!", "facettes", $facette_search_compare_wrapper);
}
return $facette_search_compare_wrapper;
}
/**
* On créé le tableau des éléments à comparer, au dessus du menu des facettes
* @return string le tableau de selection des valeurs de comparaisons
*/
public function gen_table_compare() {
global $charset,$msg;
$table_compare='';
if(is_array($this->facette_compare)) {
foreach($this->facette_compare as $key=>$facette_compare){
if(!$facette_compare['available']){
$balise_start="";
$balise_stop="";
}else{
$balise_start="
"; $balise_stop="
"; } $table_compare.='"; $balise_stop="
"; } $table_groupby.='