object_id = $object_id;
$this->object_type = $object_type;
$this->set_object_string_type($object_type);
$this->fetch_data();
}
public function get_object_string_type() {
return $this->object_string_type;
}
public function set_object_string_type($type) {
$this->object_string_type = '';
switch ($type) {
case AVIS_RECORDS :
$this->object_string_type = 'notice';
break;
case AVIS_ARTICLES :
$this->object_string_type = 'article';
break;
case AVIS_SECTIONS :
$this->object_string_type = 'section';
break;
}
}
protected function fetch_data() {
global $msg;
$this->avis = array();
$query = "select avg(note) as average, count(id_avis) as number from avis where valide = 1";
if(isset($_SESSION['id_empr_session']) && $_SESSION['id_empr_session']) {
$query .= "
and (
avis_private = 0
or (avis_private = 1 and num_empr='".$_SESSION['id_empr_session']."')
or (avis_private = 1 and avis_num_liste_lecture <> 0
and (
avis_num_liste_lecture in (
select num_liste from abo_liste_lecture
where abo_liste_lecture.num_empr='".$_SESSION['id_empr_session']."' and abo_liste_lecture.etat=2
) or
avis_num_liste_lecture in (select id_liste from opac_liste_lecture where opac_liste_lecture.num_empr = '".$_SESSION['id_empr_session']."')
)
)
)";
} else {
$query .= " and avis_private = 0";
}
$query .= " and num_notice = ".$this->object_id." and type_object = ".$this->get_object_type()."
group by num_notice, type_object";
$result = pmb_mysql_query($query);
if ($result && pmb_mysql_num_rows($result)) {
$row = pmb_mysql_fetch_object($result);
$this->average = number_format($row->average, 1, ',', '');
$this->number = $row->number;
if ($this->number) {
$this->avis = array();
$query = "select id_avis, note, commentaire, sujet, DATE_FORMAT(dateajout,'".$msg['format_date']."') as create_date, avis_private, avis_num_liste_lecture, num_empr, empr_login, empr_nom, empr_prenom
from avis
left join empr on id_empr=num_empr
where num_notice='$this->object_id' and type_object = ".$this->get_object_type()." and valide=1";
if($_SESSION['id_empr_session']) {
$query .= "
and (
avis_private = 0
or (avis_private = 1 and num_empr='".$_SESSION['id_empr_session']."')
or (avis_private = 1 and avis_num_liste_lecture <> 0
and (
avis_num_liste_lecture in (
select num_liste from abo_liste_lecture
where abo_liste_lecture.num_empr='".$_SESSION['id_empr_session']."' and abo_liste_lecture.etat=2
) or
avis_num_liste_lecture in (select id_liste from opac_liste_lecture where opac_liste_lecture.num_empr = '".$_SESSION['id_empr_session']."')
)
)
)";
} else {
$query .= " and avis_private = 0";
}
$query .= " order by avis_private desc, avis_num_liste_lecture, dateajout desc";
$result = pmb_mysql_query($query);
if ($result && pmb_mysql_num_rows($result)) {
$notes_sum = 0;
$this->avis['public'] = array();
$this->avis['private'] = array();
while ($avis = pmb_mysql_fetch_object($result)) {
if($avis->avis_private) {
$this->avis['private'][$avis->avis_num_liste_lecture][] = $avis;
} else {
$this->avis['public'][] = $avis;
$notes_sum = $notes_sum + $avis->note;
}
if (!isset($this->nb_by_note[$avis->note])) {
$this->nb_by_note[$avis->note] = 0;
}
$this->nb_by_note[$avis->note]++;
}
if(count($this->avis['public'])){
$this->public_average = number_format($notes_sum / count($this->avis['public']), 1, ',', '');
}
}
}
}
}
/**
* Sélecteur des listes de lecture privées
*/
public function gen_selector_private_reading_lists($id_avis = 0, $selected = 0) {
global $msg;
$display = '';
$query = "select id_liste from opac_liste_lecture
join empr on empr.id_empr = opac_liste_lecture.num_empr
where opac_liste_lecture.num_empr = '".$_SESSION['id_empr_session']."'
or id_liste in (select num_liste from abo_liste_lecture where num_empr = '".$_SESSION['id_empr_session']."' and etat=2)
";
$listes = array();
$result = pmb_mysql_query($query);
if ($result && pmb_mysql_num_rows($result)) {
while ($row = pmb_mysql_fetch_object($result)) {
$liste = new liste_lecture($row->id_liste);
$notices = $liste->notices;
// Pour ne sélectionner que les listes de lecture qui intégrent cette notice
if (in_array($this->object_id, $notices)) {
$listes[] = $row->id_liste;
}
}
}
if (count($listes)) {
$filter = implode(',', $listes);
} else {
$filter = 0;
}
$query = "select id_liste, nom_liste from opac_liste_lecture where id_liste in ( ".$filter." ) order by nom_liste ";
$display = gen_liste($query,'id_liste','nom_liste', 'avis_'.$id_avis.'_listes_lecture_notice_'.$this->object_id, '', $selected, 0, $msg['avis_liste_lecture_default_value'], 0, $msg['avis_liste_lecture_default_value']);
return $display;
}
/**
* Retourne l'affichage des étoiles
*/
protected function get_stars() {
$stars="";
if (!$this->public_average) {
for ($i = 0; $i < 5; $i++) {
$stars .= " ";
}
} else {
$cpt_star = 0;
for ($i = 1; $i <= $this->public_average; $i++) {
$stars.=" ";
$cpt_star++;
}
if (substr($this->public_average,2,2) > 75) {
$stars.=" ";
$cpt_star++;
} elseif (substr($this->public_average,2,2) > 25) {
$stars .= " ";
$cpt_star++;
}
for ($cpt_star;$cpt_star < 5 ; $cpt_star++) {
$stars .= " ";
}
}
return $stars;
}
/**
* Affichage des avis
*/
public function get_display() {
global $msg;
$display = '';
//Affichage des Etoiles et nombre d'avis
if ($this->number > 0) {
$display = "get_object_string_type().'_title_avis']."\" onclick=\"w=window.open('avis.php?todo=liste&".$this->get_object_string_type()."id=".$this->object_id."','avis','width=600,height=290,scrollbars=yes,resizable=yes'); w.focus(); return false;\">".$this->number." ".$msg[$this->get_object_string_type().'_bt_avis']." ";
$stars = $this->get_stars();
$display .= "get_object_string_type().'_title_avis']."\" onclick=\"w=window.open('avis.php?todo=liste&".$this->get_object_string_type()."id=".$this->object_id."','avis','width=600,height=290,scrollbars=yes,resizable=yes'); w.focus(); return false;\">".$stars." ";
} else {
$display = "get_object_string_type().'_title_avis']."\" onclick=\"w=window.open('avis.php?todo=liste&".$this->get_object_string_type()."id=".$this->object_id."','avis','width=600,height=290,scrollbars=yes,resizable=yes'); w.focus(); return false;\">".$msg['avis_aucun']." ";
}
return $display;
}
protected function get_note_display_stars($note) {
global $msg;
$stars = "";
$cpt_star = 4;
for ($i = 1; $i <= $note; $i++) {
$stars .= " ";
}
for ($i = round($note); $i <= $cpt_star; $i++) {
$stars .= " ";
}
return $stars;
}
/**
* Template des notes
* @param number $id_avis
*/
protected function get_note_form($id_avis = 0, $note_avis = 3) {
global $msg;
global $opac_avis_note_display_mode;
$note_form = "";
if($opac_avis_note_display_mode) {
$note_form .= "
".$msg["avis_appreciation"]." ";
switch($opac_avis_note_display_mode) {
case 2 :
$note_form .= "";
for($note = 0; $note <= 5; $note++) {
$note_form .= "".$msg["avis_detail_note_".$note]." ";
}
$note_form .= " ";
break;
case 4 :
$note_form .= "
".$msg["avis_note_1"]."
";
for($note = 1; $note <= 5; $note++) {
$note_form .= " ";
}
$note_form .= "
".$msg["avis_note_5"]."
";
break;
case 5 :
$note_form .= "
";
for($note = 1; $note <= 5; $note++) {
$note_form .= " object_id.", '".$this->get_object_string_type()."');\" /> ";
}
$note_form .= "
".$msg["avis_detail_note_3"]."
";
break;
case 1 :
case 3 :
$note_form .= "
".$msg['avis_note_1'];
for($note = 1; $note <= 5; $note++) {
$note_form .= " ";
}
$note_form .= $msg['avis_note_5']."
";
break;
}
$note_form .= "
";
} else {
$note_form .= " ";
}
return $note_form;
}
/**
* Formulaire d'édition d'un avis
*/
public function get_form($id_avis = 0) {
global $msg;
global $avis_tpl_form;
global $id_liste;
global $opac_avis_default_private;
$form = $avis_tpl_form;
$id_avis += 0;
if($id_avis) {
$query = "select num_empr, note, sujet, commentaire, avis_private, avis_num_liste_lecture from avis where id_avis = ".$id_avis;
$result = pmb_mysql_query($query);
$row = pmb_mysql_fetch_object($result);
$form = str_replace("!!note!!", $this->get_note_form($id_avis, $row->note), $form);
$form = str_replace("!!sujet!!", $row->sujet, $form);
$form = str_replace("!!commentaire!!", $row->commentaire, $form);
$form = str_replace("!!private!!", ($row->avis_private ? "checked='checked'" : ''), $form);
$form = str_replace("!!listes_lecture!!", $this->gen_selector_private_reading_lists($id_avis, $row->avis_num_liste_lecture), $form);
$form = str_replace("!!button_send!!", "", $form);
if(static::is_editable($row->avis_private, $row->num_empr)) {
$form = str_replace("!!button_save!!", " object_id.", '".$this->get_object_string_type()."'); return false; \" value='".$msg["avis_bt_save"]."'>", $form);
$form = str_replace("!!button_delete!!", " object_id.", '".$this->get_object_string_type()."');} return false; \" value='".$msg["avis_bt_delete"]."'>", $form);
} else {
$form = str_replace("!!button_save!!", "", $form);
$form = str_replace("!!button_delete!!", "", $form);
}
} else {
$form = str_replace("!!note!!", $this->get_note_form(), $form);
$form = str_replace("!!sujet!!", "", $form);
$form = str_replace("!!commentaire!!", "", $form);
$form = str_replace("!!private!!", ($opac_avis_default_private ? "checked='checked'" : ''), $form);
$selected_reading_list = ($id_liste ? $id_liste : 0);
$form = str_replace("!!listes_lecture!!", $this->gen_selector_private_reading_lists(0, $selected_reading_list), $form);
$form = str_replace("!!button_send!!", " get_object_string_type()."'); return false; \" value='".$msg["avis_bt_envoyer"]."'>", $form);
$form = str_replace("!!button_save!!", "", $form);
$form = str_replace("!!button_delete!!", "", $form);
}
$form = str_replace("!!id!!", $id_avis, $form);
$form = str_replace("!!object_id!!", $this->object_id, $form);
$form = str_replace("!!object_type!!", $this->get_object_string_type(), $form);
return $form;
}
/**
* Affichage de l'entête de liste
*/
protected function get_display_header_detail($node_id, $label) {
global $msg;
$display = "
";
return $display;
}
/**
* Affichage du détail d'un avis
*/
protected function get_display_line_detail($data, $node_id, $order) {
global $msg, $charset;
global $opac_avis_note_display_mode;
global $opac_avis_show_writer;
if ($order % 2 == 1) $pair_impair="even"; else $pair_impair="odd";
$display = "
";
if(static::is_editable($data->avis_private, $data->num_empr)) {
$display .= $this->get_form($data->id_avis);
}
return $display;
}
/**
* Affichage du détail des avis
*/
public function get_display_detail() {
global $msg, $charset;
global $action; // pour gérer l'affichage des avis en impression de panier
global $allow_avis_ajout;
global $opac_avis_allow;
global $opac_avis_note_display_mode;
$display = '';
if($this->number) {
if ($action=="print" || ($opac_avis_allow==1 && !$_SESSION["user_code"] )) {
$display .= "".$msg['avis_detail']." :
".str_replace("!!nb_avis!!",$this->number,$msg['avis_detail_nb_auth_ajt'])."
";
} else {
$display .= "";
$display .= $this->get_form();
}
} else {
if ($action=="print" || ($opac_avis_allow==1 && !$_SESSION["user_code"] )) {
$display .= "".$msg['avis_detail_aucun_auth_ajt']." ";
} else {
$display .= "";
$display .= $this->get_form();
}
}
if(isset($this->avis['private']) && is_array($this->avis['private']) && count($this->avis['private'])) {
foreach ($this->avis['private'] as $id_liste_lecture=>$group) {
if($id_liste_lecture) {
$liste_lecture = new liste_lecture($id_liste_lecture);
$label_liste = $liste_lecture->nom_liste;
} else {
$label_liste = $msg['avis_private_list'];
}
$node_id = 'avis_private_'.$id_liste_lecture.'_'.$this->get_object_string_type().'_'.$this->object_id;
$display .= $this->get_display_header_detail($node_id, $label_liste);
$display .= "";
foreach ($group as $order => $data) {
$display .= $this->get_display_line_detail($data, $node_id, $order);
}
$display .= "
";
}
}
if(isset($this->avis['public']) && is_array($this->avis['public']) && count($this->avis['public'])) {
$node_id = 'avis_public_'.$this->get_object_string_type().'_'.$this->object_id;
$display .= $this->get_display_header_detail($node_id, $msg['avis_public']);
$display .= "";
foreach ($this->avis['public'] as $order => $data) {
$display .= $this->get_display_line_detail($data, $node_id, $order);
}
$display .= "
";
}
$display = "
".$display."
";
return $display;
}
/**
* Affichage des étoiles uniquement
* @return string
*/
public function get_display_only_stars() {
global $msg;
return "get_object_string_type().'_title_avis']."\" onclick=\"w=window.open('avis.php?todo=liste&".$this->get_object_string_type()."id=".$this->object_id."','avis','width=600,height=290,scrollbars=yes,resizable=yes'); w.focus(); return false;\">".$this->get_stars()." ";
}
public static function save_avis($id_avis, $object_id, $object_type) {
global $charset;
global $allow_avis, $opac_avis_allow;
global $id_empr, $note, $sujet, $commentaire, $private, $num_liste_lecture;
if(($opac_avis_allow==3) || ($_SESSION["user_code"] && ($opac_avis_allow ==1 || $opac_avis_allow ==2) && $allow_avis)) {
if (!$note) $note="NULL";
$masque="@<[\/\!]*?[^<>]*?>@si";
$commentaire = preg_replace($masque,'',$commentaire);
if($charset != "utf-8") $commentaire=cp1252Toiso88591($commentaire);
if($private) {
$valide = 1;
} else {
$valide = 0;
$num_liste_lecture = 0; // un avis public ne sera pas associé à une liste de lecture
}
$id_avis += 0;
if($id_avis) {
$query = "select avis_private, num_empr from avis where id_avis = '".$id_avis."'";
$result = pmb_mysql_query($query);
if($result) {
$row = pmb_mysql_fetch_object($result);
if(static::is_editable($row->avis_private, $row->num_empr)) {
$query = "update avis set
note = '".$note."', sujet = '".$sujet."', commentaire = '".$commentaire."', valide = '".$valide."',
avis_private = '".$private."', avis_num_liste_lecture = '".$num_liste_lecture."'
where id_avis = '".$id_avis."'";
pmb_mysql_query($query);
return true;
}
}
return false;
} else {
$query = "insert into avis (num_empr,num_notice,type_object,note,sujet,commentaire,valide,avis_private,avis_num_liste_lecture) values ('".$id_empr."','".$object_id."','".$object_type."','".$note."','".$sujet."','".$commentaire."','".$valide."','".$private."','".$num_liste_lecture."')";
pmb_mysql_query($query);
return true;
}
} else {
return false;
}
}
public static function delete_avis($id_avis) {
$id_avis += 0;
if($id_avis) {
$query = "select avis_private, num_empr from avis where id_avis = '".$id_avis."'";
$result = pmb_mysql_query($query);
if($result) {
$row = pmb_mysql_fetch_object($result);
if(static::is_editable($row->avis_private, $row->num_empr)) {
$query = "delete from avis where id_avis = '".$id_avis."'";
pmb_mysql_query($query);
return true;
}
}
}
return false;
}
public static function is_editable($avis_private = 0, $num_empr = 0) {
if($avis_private && ($num_empr == $_SESSION['id_empr_session'])) {
return true;
} else {
return false;
}
}
public function get_object_type() {
return $this->object_type;
}
public function get_average() {
return $this->average;
}
public function get_public_average() {
return $this->public_average;
}
public function get_number() {
return $this->number;
}
public function get_avis() {
return $this->avis;
}
public function get_nb_by_note() {
return $this->nb_by_note;
}
public function set_average($average) {
$this->average = $average;
}
public function set_public_average($public_average) {
$this->public_average = $public_average;
}
public function set_number($number) {
$this->number = $number;
}
public function set_avis($avis) {
$this->avis = $avis;
}
public function set_nb_by_note($nb_by_note) {
$this->nb_by_note = $nb_by_note;
}
public function get_data() {
global $msg, $charset;
$memo_avis = array();
$query = "SELECT id_avis,note,sujet,commentaire,DATE_FORMAT(dateajout,'".$msg['format_date']."') as ladate,empr_login,empr_nom, empr_prenom, valide
from avis left join empr on id_empr=num_empr
where num_notice='".$this->object_id."' and type_object='".$this->object_type."' and valide=1 order by avis_rank, dateajout desc";
$result = pmb_mysql_query($query);
if ($result) {
while ($avis = pmb_mysql_fetch_object($result)) {
$avis->note_textuelle = $msg['avis_detail_note_'.$avis->note];
if($charset != "utf-8") $avis->commentaire=cp1252Toiso88591($avis->commentaire);
$avis->commentaire = do_bbcode($avis->commentaire);
$memo_avis[]=$avis;
}
}
return $memo_avis;
}
public function get_notes_avg() {
$query = "SELECT avg(note) as moyenne, count(*) as combien FROM avis WHERE avis_private = 0 and num_notice = '".$this->object_id."' and type_object = '".$this->object_type."'";
$result = pmb_mysql_query($query);
$row=pmb_mysql_fetch_object($result);
return $row->combien."|".$row->moyenne;
}
}