id = $id; $this->members = array(); $this->get_data(); } // si id = 0; création d'une instance vide return; } // récupération des données du groupe function get_data() { global $dbh; $requete = "SELECT * FROM groupe"; $requete .= " WHERE id_groupe='".$this->id."' "; $res = mysql_query($requete, $dbh); if(mysql_num_rows($res)) { $row = mysql_fetch_object($res); $this->libelle = $row->libelle_groupe; $this->lettre_rappel=$row->lettre_rappel; $this->mail_rappel=$row->mail_rappel; $this->lettre_rappel_show_nomgroup=$row->lettre_rappel_show_nomgroup; // récupération id et libelle du responsable if($row->resp_groupe) { $this->id_resp = $row->resp_groupe; $requete = "SELECT empr_nom, empr_prenom, empr_cb, empr_mail FROM empr"; $requete .= " WHERE id_empr=".$this->id_resp." LIMIT 1"; $res = mysql_query($requete, $dbh); if(mysql_num_rows($res)) { $row = mysql_fetch_object($res); $this->libelle_resp = $row->empr_nom; if($row->empr_prenom) $this->libelle_resp .= ', '.$row->empr_prenom; $this->libelle_resp .= ' ('.$row->empr_cb.')'; $this->cb_resp = $row->empr_cb; $this->mail_resp = $row->empr_mail; } } $this->get_members(); } return; } // génération du form de group function form() { global $group_form; global $msg; global $charset; if($this->id) $titre = $msg[912]; // modification else $titre = $msg[910]; // création $group_form = str_replace('!!titre!!', $titre, $group_form); if ($this->lettre_rappel) $group_form = str_replace('!!lettre_rappel!!', "checked", $group_form); else $group_form = str_replace('!!lettre_rappel!!', "", $group_form); if ($this->mail_rappel) $group_form = str_replace('!!mail_rappel!!', "checked", $group_form); else $group_form = str_replace('!!mail_rappel!!', "", $group_form); if ($this->lettre_rappel_show_nomgroup) $group_form = str_replace('!!lettre_rappel_show_nomgroup!!', "checked", $group_form); else $group_form = str_replace('!!lettre_rappel_show_nomgroup!!', "", $group_form); $group_form = str_replace('!!group_name!!', htmlentities($this->libelle,ENT_QUOTES, $charset), $group_form); $group_form = str_replace('!!nom_resp!!', $this->libelle_resp, $group_form); $group_form = str_replace('!!groupID!!', $this->id, $group_form); $group_form = str_replace('!!respID!!', $this->id_resp, $group_form); if($this->id) { $link_annul = './circ.php?categ=groups&action=showgroup&groupID='.$this->id; $link_suppr = ""; } else { $link_annul = './circ.php?categ=groups'; $link_suppr = ""; } $group_form = str_replace('!!link_annul!!', $link_annul, $group_form); $group_form = str_replace('', $link_suppr, $group_form); return $group_form; } // affectation de nouvelles valeurs function set($group_name, $respID=0, $lettre_rappel=0, $mail_rappel=0, $lettre_rappel_show_nomgroup=0) { if ($group_name) $this->libelle = $group_name; $this->id_resp = $respID; $this->lettre_rappel=$lettre_rappel; $this->mail_rappel=$mail_rappel; $this->lettre_rappel_show_nomgroup=$lettre_rappel_show_nomgroup; return; } // récupération des membres du groupe (feed : array members) function get_members() { if(!$this->id) return; global $dbh; $requete = "select EMPR.id_empr AS id, EMPR.empr_nom AS nom , EMPR.empr_prenom AS prenom, EMPR.empr_cb AS cb"; $requete .= " FROM empr EMPR, empr_groupe MEMBERS"; $requete .= " WHERE MEMBERS.empr_id=EMPR.id_empr"; $requete .= " AND MEMBERS.groupe_id=".$this->id; $requete .= " ORDER BY EMPR.empr_nom, EMPR.empr_prenom"; $result = mysql_query($requete, $dbh); $this->nb_members = mysql_num_rows($result); if($this->nb_members) { while($mb = mysql_fetch_object($result)) { $this->members[] = array( 'nom' => $mb->nom, 'prenom' => $mb->prenom, 'cb' => $mb->cb, 'id' => $mb->id); } } $this->nb_members = sizeof($this->members); return; } // ajout d'un membre function add_member($member) { global $dbh; if(!$member) return 0; // checke si ce membre n'est pas déjà dans le groupe $requete = "SELECT count(1) FROM empr_groupe"; $requete .= " WHERE empr_id=$member AND groupe_id=".$this->id; $res = mysql_query($requete, $dbh); if(mysql_result($res, 0, 0)) return $member; // OK. insertion 'pour de vrai' $requete = "INSERT INTO empr_groupe"; $requete .= " SET empr_id='$member', groupe_id='".$this->id."'"; $res = mysql_query($requete, $dbh); if($res) return $member; else return 0; } // suppression du groupe function delete() { global $dbh; $requete = "DELETE FROM groupe WHERE id_groupe=".$this->id; $res = mysql_query($requete, $dbh); $nb = mysql_affected_rows($dbh); $requete = "DELETE FROM empr_groupe WHERE groupe_id=".$this->id; $res = mysql_query($requete, $dbh); return $nb; } // suppression d'un membre function del_member($member) { global $dbh; if(!$member) return 0; $requete = "DELETE FROM empr_groupe"; $requete .= " WHERE empr_id=$member AND groupe_id=".$this->id; $res = mysql_query($requete, $dbh); return $res; } // mise à jour dans la table function update() { global $dbh; global $msg; if($this->id) { // mise à jour $requete = "UPDATE groupe"; $requete .= " SET libelle_groupe='".$this->libelle."'"; $requete .= ", resp_groupe='".$this->id_resp."'"; $requete .= ", lettre_rappel='".$this->lettre_rappel."'"; $requete .= ", mail_rappel='".$this->mail_rappel."'"; $requete .= ", lettre_rappel_show_nomgroup='".$this->lettre_rappel_show_nomgroup."'"; $requete .= " WHERE id_groupe=".$this->id." LIMIT 1"; $res = mysql_query($requete, $dbh); } else { // on voit si ça n'existe pas if($this->exists($this->libelle)) return $this->id; // création $requete = "INSERT INTO groupe SET id_groupe=''"; $requete .= ", libelle_groupe='".$this->libelle."'"; $requete .= ", resp_groupe='".$this->id_resp."'"; $requete .= ", lettre_rappel='".$this->lettre_rappel."'"; $requete .= ", mail_rappel='".$this->mail_rappel."'"; $requete .= ", lettre_rappel_show_nomgroup='".$this->lettre_rappel_show_nomgroup."'"; $result = mysql_query($requete, $dbh); $this->id = mysql_insert_id(); } return $this->id; } function exists($name) { global $dbh; if(!$name) return; $requete = "SELECT count(1) FROM groupe"; $requete .= " WHERE libelle_groupe='$name'"; $result = mysql_query($requete, $dbh); return mysql_result($result, 0, 0); } }