Troisième Etape : La page d'administrationAvant de commencer, j'insère les différents messages que j'utiliserai dans la tables d'internationalisation. Notez que le nom du module (dans les ordres SQL) correspond au nom de votre fichier (sans l'extension)
Code :
DELETE FROM I18NM_messages WHERE module='ganalytics';
INSERT INTO `I18NM_messages` (`id`, `module`, `timestamp`, `fr`, `en`) VALUES (1, 'ganalytics', NOW(), 'GAnalytics', 'GAnalytics');
INSERT INTO `I18NM_messages` (`id`, `module`, `timestamp`, `fr`, `en`) VALUES (2, 'ganalytics', NOW(), 'Script GAnalytics (la variable %id_site% sera remplacé par le code du site)', 'Script GAnalytics (la variable %id_site% sera remplacé par le code du site)');
INSERT INTO `I18NM_messages` (`id`, `module`, `timestamp`, `fr`, `en`) VALUES (3, 'ganalytics', NOW(), 'Code GAnalytics', 'Code GAnalytics');
INSERT INTO `I18NM_messages` (`id`, `module`, `timestamp`, `fr`, `en`) VALUES (4, 'ganalytics', NOW(), 'Paramètres', 'Parameters');
INSERT INTO `I18NM_messages` (`id`, `module`, `timestamp`, `fr`, `en`) VALUES (5, 'ganalytics', NOW(), 'Sauvegarder', 'Save');
INSERT INTO `I18NM_messages` (`id`, `module`, `timestamp`, `fr`, `en`) VALUES (6, 'ganalytics', NOW(), 'Régénérer toutes les pages', 'Regenerate all pages');
INSERT INTO `I18NM_messages` (`id`, `module`, `timestamp`, `fr`, `en`) VALUES (7, 'ganalytics', NOW(), 'Libellé', 'Label');
INSERT INTO `I18NM_messages` (`id`, `module`, `timestamp`, `fr`, `en`) VALUES (8, 'ganalytics', NOW(), 'Valeur', 'Value');
INSERT INTO `I18NM_messages` (`id`, `module`, `timestamp`, `fr`, `en`) VALUES (9, 'ganalytics', NOW(), 'Sauvegarde effectuée', 'Sauvegarde effectuée');
INSERT INTO `I18NM_messages` (`id`, `module`, `timestamp`, `fr`, `en`) VALUES (10, 'ganalytics', NOW(), '%id_site% doit apparaitre dans le Script GAnalytics', '%id_site% doit apparaitre dans le Script GAnalytics');
INSERT INTO `I18NM_messages` (`id`, `module`, `timestamp`, `fr`, `en`) VALUES (11, 'ganalytics', NOW(), 'Page non tagguée', 'Page non tagguée');
INSERT INTO `I18NM_messages` (`id`, `module`, `timestamp`, `fr`, `en`) VALUES (12, 'ganalytics', NOW(), 'Supprimer', 'Supprimer');
INSERT INTO `I18NM_messages` (`id`, `module`, `timestamp`, `fr`, `en`) VALUES (13, 'ganalytics', NOW(), 'Ne pas écrire le code GAnalytics', 'Ne pas écrire le code GAnalytics');
Afin de gérer dans Automne les éléments, je crée une page index.php dans /automne/admin/modules/ganalytics/
Le nom du dernier répertoire est le même que le nom du fichier vu précédemment, le nom de votre module.
Code :
require_once($_SERVER["DOCUMENT_ROOT"]."/cms_rc_admin.php");
require_once(PATH_ADMIN_SPECIAL_SESSION_CHECK_FS);
define("MESSAGE_PAGE_TITLE", 249);
define("MESSAGE_PAGE_TITLE_MODULE", 248);
define("MESSAGE_PAGE_CLEARANCE_ERROR", 65);
define("MESSAGE_PAGE_ACTION_EDIT", 261);
define("MESSAGE_PAGE_ACTION_NEW", 262);
define("MESSAGE_PAGE_FIELD_ACTIONS", 259);
define("MESSAGE_PAGE_ACTION_DELETE", 252);
define("MESSAGE_PAGE_EMPTY_SET", 265);
define("MESSAGE_PAGE_ACTION_VIEW", 1006);
define("MESSAGE_PAGE_UNKNOWN_PAGE", 66);
//CHECKS
if (!$cms_user->hasModuleClearance(MOD_GANALYTICS_CODENAME, CLEARANCE_MODULE_EDIT)) {
header("Location: ".PATH_ADMIN_SPECIAL_ENTRY_WR."?cms_message_id=".MESSAGE_PAGE_CLEARANCE_ERROR."&".session_name()."=".session_id());
exit;
}
$cms_module = CMS_modulesCatalog::getByCodename(MOD_GANALYTICS_CODENAME);
//render initialisation
$dialog = new CMS_dialog();
$content = '';
$dialog->setTitle($cms_language->getMessage(MESSAGE_PAGE_TITLE_MODULE, array($cms_module->getLabel($cms_language)))." :: ".$cms_language->getMessage(MESSAGE_PAGE_TITLE));
if ($cms_message) {
$dialog->setActionMessage($cms_message);
}
//module specific
define("MESSAGE_CODE_GANALYTICS", 3);
define("MESSAGE_PARAMETRE", 4);
define("MESSAGE_SAUVEGARDER", 5);
define("MESSAGE_REGENERER", 6);
define("MESSAGE_LIBELLE", 7);
define("MESSAGE_VALEUR", 8);
define("MESSAGE_CONFIRMATION_SAUVEGARDE", 9);
define("MESSAGE_ERREUR_PARAM_SCRIPT", 10);
define("MESSAGE_PAGE_EXCEPT", 11);
define("MESSAGE_SUPPRIMER", 12);
// ****************************************************************
// ** ACTIONS MANAGEMENT **
// ****************************************************************
$onglet_selectionne = 0;
// traitement code google
if(isset($_POST["action"]) && $_POST["action"] == "code_google"){
$onglet_selectionne = 0;
unset($_POST["action"]);
$regenerate_all = isset($_POST["regenerate_all"]) ? true : false;
unset($_POST["regenerate_all"]);
$datas = $_POST;
$sql = "";
$enregistrement = array();
foreach($datas as $hostname => $code){
if($code != ""){
$enregistrement[] = "('".$hostname."','".$code."')";
}
}
// on enleve toutes les valeurs de la table ganalytics
$cms_module->sql_delete_all_ganalytics();
$cms_module->sql_insert_ganalytics($enregistrement);
$cms_message = $cms_language->getMessage(MESSAGE_CONFIRMATION_SAUVEGARDE, false, MOD_GANALYTICS_CODENAME);
if($regenerate_all){
@set_time_limit(1000);
CMS_tree::regenerateAllPages(true);
}
}
// traitement parametre
if(isset($_POST["action"]) && $_POST["action"] == "parametre"){
$onglet_selectionne = 2;
unset($_POST["action"]);
$regenerate_all = isset($_POST["regenerate_all"]) ? true : false;
unset($_POST["regenerate_all"]);
$datas = $_POST;
$cms_message = '';
$error = false;
// verification sur les regles des champs
foreach($datas as $param => $value){
switch($param){
case "2" :
$pos = strpos($value, "%id_site%");
if($pos === false){
$error = true;
$cms_message .= $cms_language->getMessage(MESSAGE_ERREUR_PARAM_SCRIPT, false, MOD_GANALYTICS_CODENAME);
}
break;
}
}
// si il n'y a pas d'erreur => enregistrement
if(!$error){
$cms_module->sql_update_ganalytics_parameters($datas);
$cms_message = $cms_language->getMessage(MESSAGE_CONFIRMATION_SAUVEGARDE, false, MOD_GANALYTICS_CODENAME);
if($regenerate_all){
@set_time_limit(1000);
CMS_tree::regenerateAllPages(true);
}
}
}
$dialog->addOnglet();
if ($cms_message) {
$dialog->setActionMessage($cms_message);
}
// RENDU
$message_code_ganalytics = $cms_language->getMessage(MESSAGE_CODE_GANALYTICS, false, MOD_GANALYTICS_CODENAME);
$message_page_except = $cms_language->getMessage(MESSAGE_PAGE_EXCEPT, false, MOD_GANALYTICS_CODENAME);
$content .='
<script type="text/javascript">
<!-- Definir les Styles des onglets -->
ongletstyle();
<!-- Creation de l\'objet Onglet -->
var monOnglet = new Onglet("monOnglet", "100%", "100%", "110", "30", '.$onglet_selectionne.');
monOnglet.add(new OngletItem("'.$message_code_ganalytics.'", "'.$message_code_ganalytics.'"));
monOnglet.add(new OngletItem("'.$message_page_except.'", "'.$message_page_except.'"));
</script>
$content .='
<table width="750" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<script type="text/javascript">monOnglet.displayHeader();</script>
';
// code google
// recuperation des hostnames-code google dans la variable site
$sites = array();
$sql = "SELECT * FROM ganalytics";
$q = new CMS_query($sql);
while ($data = $q->getArray()) {
$sites[$data["hostname"]] = $data["id_ganalytics"];
}
$content .='<div id="og_monOnglet0" style="DISPLAY: none;top:0px;left:0px;width:100%;">
<form action="index.php" method="post">
<input type="hidden" name="action" value="code_google"/>
<table width="100%" border="0" cellpadding="3" cellspacing="0" class="admin_onglet">';
$content .='<tr><td class="admin_onglet_head">Hostname</td><td class="admin_onglet_head">id site</td></tr>';
$websites = CMS_websitesCatalog::getAll('order');
$code_ganalytics = array();
// listing des hostnames presents sur automne $code_google["$hostname"] = "";
foreach($websites as $website){
if(!isset($code_ganalytics[$website->getUrl()])) $code_ganalytics[$website->getUrl()] = "";
}
// en fonction des hostnames, proposition d'association d'un code google
foreach($code_ganalytics as $index => $co){
$v = isset($sites[$index]) ? $sites[$index] : "";
$style = $v == "" ? ' style="font-weight:bold;"' : '';
$content .='<tr><td class="admin_onglet_body"'.$style.'>'.$index.'</td><td class="admin_onglet_body"><input name="'.$index.'" type="text" value="'.$v.'"/></td></tr>';
}
$content .='<tr><td class="admin_onglet_head" colspan="2"><input type="submit" class="admin_input_submit" value="'.$cms_language->getMessage(MESSAGE_SAUVEGARDER, false, MOD_GANALYTICS_CODENAME).'"/><input id="regenerate_all_code_google" type="checkbox" name="regenerate_all" value="true" /><label for="regenerate_all_code_google">'.$cms_language->getMessage(MESSAGE_REGENERER, false, MOD_GANALYTICS_CODENAME).'</label></td></tr>';
$content .= '</table></form></div>';
// parametre
$content .='<div id="og_monOnglet2" style="DISPLAY: none;top:0px;left:0px;width:100%;">
<form action="index.php" method="post">
<input type="hidden" name="action" value="parametre"/>
<table width="100%" border="0" cellpadding="3" cellspacing="0" class="admin_onglet">';
$content .='<tr><td class="admin_onglet_head">'.$cms_language->getMessage(MESSAGE_LIBELLE, false, MOD_GANALYTICS_CODENAME).'</td><td class="admin_onglet_head">'.$cms_language->getMessage(MESSAGE_VALEUR, false, MOD_GANALYTICS_CODENAME).'</td></tr>';
$q = $cms_module->sql_select_all_ganalytics_parameters();
while ($data = $q->getArray()) {
$champ = "";
switch($data["type"]){
case "textarea" :
$champ = '<textarea name="'.$data["id"].'" style="width:400px;height:100px;">'.$data["value"].'</textarea>';
break;
case "text" :
$champ = '<input type="text" name="'.$data["id"].'" style="width:400px;" value="'.stripslashes(htmlentities($data["value"])).'"/>';
break;
}
$content .='<tr><td class="admin_onglet_body">'.$cms_language->getMessage($data["id"], false, MOD_GANALYTICS_CODENAME).'</td><td class="admin_onglet_body"><textarea name="'.$data["id"].'" style="width:400px;height:100px;">'.$data["value"].'</textarea></td></tr>';
}
$content .='<tr><td class="admin_onglet_head" colspan="2"><input type="submit" class="admin_input_submit" value="'.$cms_language->getMessage(MESSAGE_SAUVEGARDER, false, MOD_GANALYTICS_CODENAME).'"/><input id="regenerate_all_parametre" type="checkbox" name="regenerate_all" value="true" /><label for="regenerate_all_parametre">'.$cms_language->getMessage(MESSAGE_REGENERER, false, MOD_GANALYTICS_CODENAME).'</label></td></tr>';
$content .= '</table></form></div>';
$content .= '</td></tr><script type="text/javascript">monOnglet.displayFooter();</script>
</table>';
$dialog->setContent($content);
$dialog->show();
?>
Le code semble long mais ne contient aucune difficulté, noter qu'il est possible de lancer la régénération totale du site.
J'utilise la classe CMS_websitesCatalog pour obtenir la liste des sites mais n'en récupère qu'une liste des noms de domaines uniques (vous pouvez avoir différents sites (Automne) avec le même nom de domaine, on aura alors la même clé pour les deux sites.