<?php
	$currDir = dirname(__FILE__);
	require("{$currDir}/incCommon.php");
	$GLOBALS['page_title'] = $Translation['view or rebuild fields'];
	include("{$currDir}/incHeader.php");

	/*
		$schema: [ tablename => [ fieldname => [ appgini => '...', 'db' => '...'], ... ], ... ]
	*/

	/* application schema as created in AppGini */
	$schema = array(
		'entete' => array(
			'identete' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'link_in' => array('appgini' => 'INT(4) unsigned zerofill null '),
			'nom' => array('appgini' => 'VARCHAR(80) null '),
			'raison_sociale' => array('appgini' => 'VARCHAR(255) null '),
			'description' => array('appgini' => 'VARCHAR(255) null '),
			'adresse' => array('appgini' => 'VARCHAR(80) null '),
			'telephone' => array('appgini' => 'VARCHAR(80) null '),
			'whatsapp' => array('appgini' => 'VARCHAR(80) null '),
			'email' => array('appgini' => 'VARCHAR(80) null '),
			'site_web' => array('appgini' => 'VARCHAR(80) null '),
			'bp' => array('appgini' => 'VARCHAR(80) null '),
			'footer' => array('appgini' => 'VARCHAR(255) null '),
			'footer_1' => array('appgini' => 'VARCHAR(255) null '),
			'footer_2' => array('appgini' => 'VARCHAR(255) null '),
			'footer_3' => array('appgini' => 'TEXT null '),
			'logo' => array('appgini' => 'VARCHAR(80) null '),
			'logop' => array('appgini' => 'VARCHAR(80) null '),
			'notice' => array('appgini' => 'VARCHAR(80) null '),
			'devise' => array('appgini' => 'VARCHAR(80) null '),
			'signataire' => array('appgini' => 'VARCHAR(80) null '),
			'observations' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'personnels' => array(
			'idpersonnel' => array('appgini' => 'INT(3) unsigned zerofill not null primary key auto_increment '),
			'matricule' => array('appgini' => 'VARCHAR(80) null '),
			'date_recrutement' => array('appgini' => 'DATE null '),
			'nom' => array('appgini' => 'VARCHAR(80) not null '),
			'prenom' => array('appgini' => 'VARCHAR(80) not null '),
			'nationalite' => array('appgini' => 'INT unsigned null '),
			'cin' => array('appgini' => 'VARCHAR(80) not null unique '),
			'adresse' => array('appgini' => 'VARCHAR(80) null '),
			'telephone' => array('appgini' => 'VARCHAR(80) not null '),
			'whatsapp' => array('appgini' => 'VARCHAR(80) null '),
			'email' => array('appgini' => 'VARCHAR(80) null '),
			'agence' => array('appgini' => 'INT unsigned null '),
			'fonction' => array('appgini' => 'INT unsigned null '),
			'service' => array('appgini' => 'INT unsigned null '),
			'poste' => array('appgini' => 'INT unsigned null '),
			'photo' => array('appgini' => 'VARCHAR(40) null '),
			'cv' => array('appgini' => 'VARCHAR(80) null '),
			'dossier' => array('appgini' => 'VARCHAR(80) null '),
			'observations' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'clients' => array(
			'idclient' => array('appgini' => 'INT(3) unsigned zerofill not null primary key auto_increment '),
			'matricule' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
			'date_en' => array('appgini' => 'DATETIME null '),
			'type_client' => array('appgini' => 'INT unsigned not null '),
			'raison_sociale' => array('appgini' => 'VARCHAR(255) null '),
			'personne_contact' => array('appgini' => 'VARCHAR(255) null '),
			'email_pc' => array('appgini' => 'VARCHAR(80) null '),
			'phone_pc' => array('appgini' => 'VARCHAR(80) null '),
			'ninea' => array('appgini' => 'VARCHAR(255) null unique '),
			'rc' => array('appgini' => 'VARCHAR(80) null unique '),
			'genre' => array('appgini' => 'VARCHAR(80) null '),
			'prenom' => array('appgini' => 'VARCHAR(80) null '),
			'nom' => array('appgini' => 'VARCHAR(80) null '),
			'cin' => array('appgini' => 'VARCHAR(80) null unique '),
			'agence' => array('appgini' => 'INT unsigned null '),
			'nationalite' => array('appgini' => 'INT unsigned null '),
			'categorie' => array('appgini' => 'INT unsigned null '),
			'fonction' => array('appgini' => 'INT unsigned null '),
			'activite' => array('appgini' => 'INT unsigned null '),
			'date_naissance' => array('appgini' => 'DATETIME null '),
			'age' => array('appgini' => 'INT null '),
			'passport' => array('appgini' => 'VARCHAR(80) not null unique '),
			'delivre_le' => array('appgini' => 'DATETIME not null '),
			'expire_le' => array('appgini' => 'DATETIME not null '),
			'lieu' => array('appgini' => 'INT unsigned not null '),
			'validite' => array('appgini' => 'VARCHAR(80) null '),
			'adresse' => array('appgini' => 'VARCHAR(80) not null '),
			'telephone' => array('appgini' => 'VARCHAR(80) not null '),
			'whatsapp' => array('appgini' => 'VARCHAR(80) not null '),
			'urgence' => array('appgini' => 'VARCHAR(80) not null '),
			'email' => array('appgini' => 'VARCHAR(80) not null '),
			'compte' => array('appgini' => 'VARCHAR(80) null '),
			'banque' => array('appgini' => 'INT unsigned null '),
			'carte_fidelite' => array('appgini' => 'INT unsigned null '),
			'num_carte' => array('appgini' => 'INT unsigned null unique '),
			'signature' => array('appgini' => 'VARCHAR(80) null '),
			'photo' => array('appgini' => 'VARCHAR(40) null '),
			'dossier' => array('appgini' => 'VARCHAR(80) null '),
			'observations' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
		),
		'suivi_client' => array(
			'idsuivi_client' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'num_dossier' => array('appgini' => 'VARCHAR(80) null unique '),
			'nom' => array('appgini' => 'INT(3) unsigned zerofill not null '),
			'prenom' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'raison_sociale' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'telephone' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'email' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'whatsapp' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'service_demande' => array('appgini' => 'INT unsigned not null '),
			'date_demande' => array('appgini' => 'DATE null '),
			'statut_service' => array('appgini' => 'VARCHAR(80) not null '),
			'num_visa' => array('appgini' => 'VARCHAR(80) null '),
			'date_debut_visa' => array('appgini' => 'DATE null '),
			'date_fin_visa' => array('appgini' => 'DATE null '),
			'type_de_visa' => array('appgini' => 'INT unsigned null '),
			'observations' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'rendez_vous' => array(
			'idrv' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'objet' => array('appgini' => 'VARCHAR(255) null '),
			'annee' => array('appgini' => 'INT unsigned null '),
			'mois' => array('appgini' => 'BLOB null '),
			'jour' => array('appgini' => 'BLOB null '),
			'heure' => array('appgini' => 'TIME null '),
			'rvs' => array('appgini' => 'TEXT null '),
			'observations' => array('appgini' => 'TEXT null '),
			'cree_le' => array('appgini' => 'VARCHAR(200) null '),
			'modifie_le' => array('appgini' => 'VARCHAR(200) null '),
			'statut' => array('appgini' => 'VARCHAR(80) not null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'partenaires' => array(
			'idpartenaires' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'date' => array('appgini' => 'DATE null '),
			'nom_prenom' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'fonction' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'adresse' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'telephone' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'email' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'observations' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'packages' => array(
			'idpackage' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'nom' => array('appgini' => 'VARCHAR(80) null '),
			'prix' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'observations' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'oumrah' => array(
			'idoumrah' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'Pelerinage' => array('appgini' => 'INT unsigned null '),
			'Prenom' => array('appgini' => 'VARCHAR(255) null '),
			'Nom' => array('appgini' => 'VARCHAR(255) null '),
			'Datedenaissance' => array('appgini' => 'DATE null '),
			'Lieudenaissance' => array('appgini' => 'VARCHAR(255) null '),
			'Adresse' => array('appgini' => 'VARCHAR(255) null '),
			'Sexe' => array('appgini' => 'VARCHAR(255) null '),
			'Numropasseport' => array('appgini' => 'VARCHAR(255) null unique '),
			'Remarque' => array('appgini' => 'VARCHAR(255) null '),
			'Tlphoneplerin' => array('appgini' => 'VARCHAR(255) null '),
			'Tlphonefamille' => array('appgini' => 'VARCHAR(255) null '),
			'Personnecontacter' => array('appgini' => 'VARCHAR(255) null '),
			'Squence' => array('appgini' => 'VARCHAR(255) null unique '),
			'Profession' => array('appgini' => 'VARCHAR(255) null '),
			'Ethnie' => array('appgini' => 'VARCHAR(255) null '),
			'Region' => array('appgini' => 'VARCHAR(255) null '),
			'total_entree' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'total_sortie' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'solde' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'hajj_oumrah' => array(
			'idhajjoumrah' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'Pelerinage' => array('appgini' => 'VARCHAR(80) null '),
			'Prenom' => array('appgini' => 'VARCHAR(255) null '),
			'Nom' => array('appgini' => 'VARCHAR(255) null '),
			'Datedenaissance' => array('appgini' => 'DATE null '),
			'Lieudenaissance' => array('appgini' => 'VARCHAR(255) null '),
			'Adresse' => array('appgini' => 'VARCHAR(255) null '),
			'Sexe' => array('appgini' => 'VARCHAR(255) null '),
			'Numropasseport' => array('appgini' => 'VARCHAR(255) null unique '),
			'Remarque' => array('appgini' => 'VARCHAR(255) null '),
			'Tlphoneplerin' => array('appgini' => 'VARCHAR(255) null '),
			'Tlphonefamille' => array('appgini' => 'VARCHAR(255) null '),
			'Personnecontacter' => array('appgini' => 'VARCHAR(255) null '),
			'Squence' => array('appgini' => 'VARCHAR(255) null unique '),
			'Profession' => array('appgini' => 'VARCHAR(255) null '),
			'Ethnie' => array('appgini' => 'VARCHAR(255) null '),
			'Region' => array('appgini' => 'VARCHAR(255) null '),
			'total_entree' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'total_sortie' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'solde' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'souscriptions' => array(
			'idsouscription' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'date_enr' => array('appgini' => 'DATE null '),
			'pelerin' => array('appgini' => 'INT unsigned null '),
			'oumrah' => array('appgini' => 'INT unsigned null '),
			'package' => array('appgini' => 'INT unsigned null '),
			'prix' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'remise' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'net_a_payer' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'observations' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'entrees' => array(
			'identrees' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'date_entree' => array('appgini' => 'DATE null '),
			'client' => array('appgini' => 'INT unsigned null '),
			'oumrah' => array('appgini' => 'INT unsigned null '),
			'description' => array('appgini' => 'INT unsigned null '),
			'montant' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'observations' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'sorties' => array(
			'idsorties' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'date_sortie' => array('appgini' => 'DATE null '),
			'client' => array('appgini' => 'INT unsigned null '),
			'oumrah' => array('appgini' => 'INT unsigned null '),
			'montant' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'observations' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'agences' => array(
			'idagence' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'agence' => array('appgini' => 'VARCHAR(80) null unique '),
			'adresse' => array('appgini' => 'VARCHAR(80) null '),
			'responsable' => array('appgini' => 'VARCHAR(80) not null '),
			'telephone' => array('appgini' => 'VARCHAR(80) not null '),
			'observations' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'gestion_transactions' => array(
			'idgtransaction' => array('appgini' => 'INT(3) unsigned zerofill not null primary key auto_increment '),
			'link_in' => array('appgini' => 'VARCHAR(80) null '),
			'agent_init' => array('appgini' => 'VARCHAR(2) not null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
			'matricule' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'numero' => array('appgini' => 'VARCHAR(80) null unique '),
			'date' => array('appgini' => 'DATE null '),
			'avec_entete' => array('appgini' => 'INT unsigned not null '),
			'rs' => array('appgini' => 'INT unsigned null '),
			'type' => array('appgini' => 'VARCHAR(80) not null '),
			'nom_prenom' => array('appgini' => 'INT(3) unsigned zerofill not null '),
			'adclient' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'telclient' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'emclient' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'raison_sociale' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'termes' => array('appgini' => 'TEXT null '),
			'fpdf' => array('appgini' => 'VARCHAR(80) null '),
			'fzip' => array('appgini' => 'VARCHAR(80) null '),
			'devise' => array('appgini' => 'INT unsigned null '),
			'logo' => array('appgini' => 'INT unsigned null '),
			'description' => array('appgini' => 'INT unsigned null '),
			'adresse' => array('appgini' => 'INT unsigned null '),
			'telephone' => array('appgini' => 'INT unsigned null '),
			'whatsapp' => array('appgini' => 'INT unsigned null '),
			'email' => array('appgini' => 'INT unsigned null '),
			'site_web' => array('appgini' => 'INT unsigned null '),
			'bp' => array('appgini' => 'INT unsigned null '),
			'footer_3' => array('appgini' => 'INT unsigned null '),
			'notice' => array('appgini' => 'INT unsigned null '),
			'signataire' => array('appgini' => 'INT unsigned null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
		),
		'billeteries' => array(
			'idbilleterie' => array('appgini' => 'INT(3) unsigned zerofill not null primary key auto_increment '),
			'link_out' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'facture' => array('appgini' => 'VARCHAR(80) null unique '),
			'date' => array('appgini' => 'DATE null '),
			'service' => array('appgini' => 'INT unsigned not null '),
			'passager' => array('appgini' => 'INT(3) unsigned zerofill not null '),
			'gds' => array('appgini' => 'INT unsigned not null '),
			'compagnie' => array('appgini' => 'INT unsigned not null '),
			'parcours' => array('appgini' => 'INT unsigned not null '),
			'nature_transaction' => array('appgini' => 'INT unsigned null '),
			'billet' => array('appgini' => 'VARCHAR(80) null '),
			'dossier' => array('appgini' => 'VARCHAR(80) null '),
			'prix_achat' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'prix_vente' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'qts' => array('appgini' => 'INT null default \'1\' '),
			'commission' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'frais_service' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'frais_assurance' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'mtva' => array('appgini' => 'FLOAT(10,2) null default \'0\' '),
			'tva' => array('appgini' => 'FLOAT(10,0) null default \'0\' '),
			'tht' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'net_compagnie' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'net_a_payer' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'account' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'reste_a_payer' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'observations' => array('appgini' => 'TEXT null '),
		),
		'gestion_changes' => array(
			'idgchange' => array('appgini' => 'INT(3) unsigned zerofill not null primary key auto_increment '),
			'link_in' => array('appgini' => 'VARCHAR(80) null '),
			'agent_init' => array('appgini' => 'VARCHAR(2) not null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
			'matricule' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'numero' => array('appgini' => 'VARCHAR(80) null unique '),
			'date' => array('appgini' => 'DATE null '),
			'avec_entete' => array('appgini' => 'INT unsigned not null '),
			'rs' => array('appgini' => 'INT unsigned null '),
			'type' => array('appgini' => 'VARCHAR(80) null '),
			'nom_prenom' => array('appgini' => 'INT(3) unsigned zerofill not null '),
			'nationalite' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'profession' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'adclient' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'telclient' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'emclient' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'raison_sociale' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'termes' => array('appgini' => 'TEXT null '),
			'fpdf' => array('appgini' => 'VARCHAR(80) null '),
			'fzip' => array('appgini' => 'VARCHAR(80) null '),
			'devise' => array('appgini' => 'INT unsigned null '),
			'logo' => array('appgini' => 'INT unsigned null '),
			'description' => array('appgini' => 'INT unsigned null '),
			'adresse' => array('appgini' => 'INT unsigned null '),
			'telephone' => array('appgini' => 'INT unsigned null '),
			'whatsapp' => array('appgini' => 'INT unsigned null '),
			'email' => array('appgini' => 'INT unsigned null '),
			'site_web' => array('appgini' => 'INT unsigned null '),
			'bp' => array('appgini' => 'INT unsigned null '),
			'footer_3' => array('appgini' => 'INT unsigned null '),
			'notice' => array('appgini' => 'INT unsigned null '),
			'signataire' => array('appgini' => 'INT unsigned null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
		),
		'changes' => array(
			'idchange' => array('appgini' => 'INT(3) unsigned zerofill not null primary key auto_increment '),
			'link_out' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'facture' => array('appgini' => 'VARCHAR(80) null unique '),
			'date' => array('appgini' => 'DATE null '),
			'nature_transaction' => array('appgini' => 'VARCHAR(255) null '),
			'billet' => array('appgini' => 'VARCHAR(80) null '),
			'dossier' => array('appgini' => 'VARCHAR(80) null '),
			'prix_achat' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'mtva' => array('appgini' => 'FLOAT(10,2) null default \'0\' '),
			'net_a_payer' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'net_compagnie' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'prix_vente' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'qts' => array('appgini' => 'INT null default \'1\' '),
			'commission' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'frais_service' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'frais_assurance' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'tva' => array('appgini' => 'FLOAT(10,0) null default \'0\' '),
			'tht' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'account' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'reste_a_payer' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'observations' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'partages' => array(
			'idpartage' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
			'date' => array('appgini' => 'DATE null '),
			'statut' => array('appgini' => 'INT unsigned null '),
			'client' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'image' => array('appgini' => 'VARCHAR(40) null '),
			'word' => array('appgini' => 'VARCHAR(80) null '),
			'document' => array('appgini' => 'VARCHAR(80) null '),
			'compresse' => array('appgini' => 'VARCHAR(80) null '),
			'commentaire' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
		),
		'type_compagnie' => array(
			'idtypecompagnie' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'type_compangie' => array('appgini' => 'VARCHAR(80) null unique '),
			'observations' => array('appgini' => 'TEXT null '),
		),
		'compagnies' => array(
			'idcompagnie' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'nom_compagnie' => array('appgini' => 'VARCHAR(80) null unique '),
			'adresse' => array('appgini' => 'VARCHAR(80) null '),
			'contact' => array('appgini' => 'VARCHAR(80) null '),
			'type' => array('appgini' => 'INT unsigned null '),
			'observations' => array('appgini' => 'TEXT null '),
		),
		'produits_services' => array(
			'idps' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'produit_service' => array('appgini' => 'VARCHAR(80) null unique '),
			'observations' => array('appgini' => 'TEXT null '),
		),
		'banques' => array(
			'idbanque' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'banque' => array('appgini' => 'VARCHAR(80) null unique '),
			'adresse' => array('appgini' => 'VARCHAR(80) null '),
			'telephone' => array('appgini' => 'VARCHAR(80) null '),
			'email' => array('appgini' => 'VARCHAR(80) null '),
			'contact' => array('appgini' => 'VARCHAR(80) null '),
			'observations' => array('appgini' => 'TEXT null '),
		),
		'pays' => array(
			'idpays' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'pays' => array('appgini' => 'VARCHAR(80) null unique '),
		),
		'villes' => array(
			'idville' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'ville' => array('appgini' => 'VARCHAR(80) null unique '),
		),
		'items' => array(
			'iditem' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'item' => array('appgini' => 'VARCHAR(80) null unique '),
			'observation' => array('appgini' => 'TEXT null '),
		),
		'type_client' => array(
			'idtype_client' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'type_client' => array('appgini' => 'VARCHAR(80) null unique '),
			'observation' => array('appgini' => 'TEXT null '),
		),
		'fonctions' => array(
			'idfonction' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'fonction' => array('appgini' => 'VARCHAR(80) null unique '),
			'observations' => array('appgini' => 'TEXT null '),
		),
		'activites' => array(
			'idactivite' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'activite' => array('appgini' => 'VARCHAR(80) null unique '),
			'observations' => array('appgini' => 'TEXT null '),
		),
		'postes' => array(
			'idposte' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'poste' => array('appgini' => 'VARCHAR(80) null unique '),
			'observations' => array('appgini' => 'TEXT null '),
		),
		'annees' => array(
			'idannee' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'annee' => array('appgini' => 'INT null unique '),
		),
		'gds' => array(
			'idgds' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'gds' => array('appgini' => 'VARCHAR(80) null unique '),
			'observations' => array('appgini' => 'TEXT null '),
		),
		'objets' => array(
			'idobjet' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'objet' => array('appgini' => 'VARCHAR(80) null '),
			'obs' => array('appgini' => 'TEXT null '),
		),
		'type_rapport' => array(
			'idtrpt' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'type_rapport' => array('appgini' => 'VARCHAR(80) null '),
			'obs' => array('appgini' => 'TEXT null '),
		),
		'fidelite' => array(
			'idfidelite' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'carte_fidelite' => array('appgini' => 'VARCHAR(80) null unique '),
			'observations' => array('appgini' => 'TEXT null '),
		),
		'pointages' => array(
			'idpointage' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'date' => array('appgini' => 'DATE null '),
			'nom_prenom' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'agence' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'fonction' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'arrivee' => array('appgini' => 'TIME null '),
			'depart' => array('appgini' => 'TIME null '),
			'obs' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'note_de_service' => array(
			'idnote_service' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'date' => array('appgini' => 'DATE null '),
			'objet' => array('appgini' => 'VARCHAR(80) not null '),
			'texte' => array('appgini' => 'TEXT not null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'redactions' => array(
			'idredaction' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'date' => array('appgini' => 'DATE null '),
			'objet' => array('appgini' => 'INT unsigned null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
			'redaction' => array('appgini' => 'LONGTEXT null '),
			'obs' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
		),
		'rapports_activite' => array(
			'idrapport' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'date' => array('appgini' => 'DATE null '),
			'nom_prenom' => array('appgini' => 'VARCHAR(40) null '),
			'objet' => array('appgini' => 'INT unsigned null '),
			'type_rapport' => array('appgini' => 'INT unsigned null '),
			'rapport' => array('appgini' => 'LONGTEXT null '),
			'obs' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'gestion_comptes' => array(
			'idgestioncompte' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'nom_compte' => array('appgini' => 'VARCHAR(255) null unique '),
			'num_compte' => array('appgini' => 'INT(4) null unique '),
			'banque' => array('appgini' => 'INT unsigned null '),
			'type_compte' => array('appgini' => 'INT unsigned null '),
			'observations' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'gestion_comptes_1' => array(
			'idgestioncompte' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'nom_compte' => array('appgini' => 'VARCHAR(255) null unique '),
			'num_compte' => array('appgini' => 'INT(4) null unique '),
			'banque' => array('appgini' => 'INT unsigned null '),
			'type_compte' => array('appgini' => 'INT unsigned null '),
			'observations' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'type_comptes' => array(
			'idtypecompte' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'type_compte' => array('appgini' => 'VARCHAR(80) null unique '),
			'observations' => array('appgini' => 'TEXT null '),
		),
		'journal' => array(
			'idjournal' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'date' => array('appgini' => 'DATE null '),
			'libelle' => array('appgini' => 'VARCHAR(80) null '),
			'compte_debit' => array('appgini' => 'INT unsigned null '),
			'compte_credit' => array('appgini' => 'INT unsigned null '),
			'tht' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'tva' => array('appgini' => 'FLOAT(10,2) null default \'0\' '),
			'tva2' => array('appgini' => 'FLOAT(10,0) null default \'0\' '),
			'ttc' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'remarques' => array('appgini' => 'VARCHAR(80) null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'liens' => array(
			'idlien' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'logo' => array('appgini' => 'VARCHAR(80) null '),
			'description' => array('appgini' => 'VARCHAR(255) null '),
			'lien' => array('appgini' => 'VARCHAR(80) null unique '),
			'observations' => array('appgini' => 'TEXT null '),
		),
		'courrier_entrant' => array(
			'idcentrant' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'date' => array('appgini' => 'DATE null '),
			'expediteur' => array('appgini' => 'VARCHAR(80) null '),
			'objet' => array('appgini' => 'VARCHAR(80) null '),
			'destinataire' => array('appgini' => 'VARCHAR(80) null '),
			'piece_jointe' => array('appgini' => 'VARCHAR(80) null '),
			'statut' => array('appgini' => 'VARCHAR(80) not null '),
			'remarques' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'courrier_sortant' => array(
			'idcsortant' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'date' => array('appgini' => 'DATE null '),
			'expediteur' => array('appgini' => 'VARCHAR(80) null '),
			'objet' => array('appgini' => 'VARCHAR(80) null '),
			'destinataire' => array('appgini' => 'VARCHAR(80) null '),
			'piece_jointe' => array('appgini' => 'VARCHAR(80) null '),
			'statut' => array('appgini' => 'VARCHAR(80) not null '),
			'remarques' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'vitae' => array(
			'idvitae' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'date' => array('appgini' => 'DATE null '),
			'candidat' => array('appgini' => 'VARCHAR(80) null '),
			'domaine' => array('appgini' => 'VARCHAR(80) null '),
			'dossier' => array('appgini' => 'VARCHAR(80) null '),
			'statut' => array('appgini' => 'VARCHAR(80) not null '),
			'photo' => array('appgini' => 'VARCHAR(40) null '),
			'observations' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'reunions' => array(
			'idreunion' => array('appgini' => 'INT(3) unsigned zerofill not null primary key auto_increment '),
			'link_in' => array('appgini' => 'VARCHAR(80) null '),
			'avec_entete' => array('appgini' => 'INT unsigned not null '),
			'ordre_jour' => array('appgini' => 'VARCHAR(255) null '),
			'date' => array('appgini' => 'DATE null '),
			'temps' => array('appgini' => 'VARCHAR(80) null '),
			'duree' => array('appgini' => 'VARCHAR(80) null '),
			'lieu' => array('appgini' => 'VARCHAR(80) null '),
			'objectif' => array('appgini' => 'TEXT null '),
			'fpdf' => array('appgini' => 'VARCHAR(80) null '),
			'fzip' => array('appgini' => 'VARCHAR(80) null '),
			'rs' => array('appgini' => 'INT unsigned null '),
			'devise' => array('appgini' => 'INT unsigned null '),
			'logo' => array('appgini' => 'INT unsigned null '),
			'description' => array('appgini' => 'INT unsigned null '),
			'adresse' => array('appgini' => 'INT unsigned null '),
			'telephone' => array('appgini' => 'INT unsigned null '),
			'whatsapp' => array('appgini' => 'INT unsigned null '),
			'email' => array('appgini' => 'INT unsigned null '),
			'site_web' => array('appgini' => 'INT unsigned null '),
			'bp' => array('appgini' => 'INT unsigned null '),
			'footer_3' => array('appgini' => 'INT unsigned null '),
			'notice' => array('appgini' => 'INT unsigned null '),
			'signataire' => array('appgini' => 'INT unsigned null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'item_reunions' => array(
			'iditemreunion' => array('appgini' => 'INT(3) unsigned zerofill not null primary key auto_increment '),
			'link_out' => array('appgini' => 'INT(3) unsigned zerofill null '),
			'activite' => array('appgini' => 'VARCHAR(80) null '),
			'temps' => array('appgini' => 'VARCHAR(80) null '),
			'responsable' => array('appgini' => 'VARCHAR(80) null '),
			'objectif' => array('appgini' => 'VARCHAR(80) null '),
			'observations' => array('appgini' => 'TEXT null '),
		),
		'statut_partage' => array(
			'idstatutpartage' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'statut' => array('appgini' => 'VARCHAR(80) null unique '),
			'observations' => array('appgini' => 'TEXT null '),
		),
		'chequiers' => array(
			'idchequier' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'date' => array('appgini' => 'DATE null '),
			'lot_num' => array('appgini' => 'VARCHAR(80) null unique '),
			'lot_qts' => array('appgini' => 'INT null '),
			'compte' => array('appgini' => 'INT unsigned null '),
			'banque' => array('appgini' => 'INT unsigned null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'cheques' => array(
			'idcheque' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'num' => array('appgini' => 'VARCHAR(80) not null unique '),
			'chequier' => array('appgini' => 'INT unsigned null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'gestion_banques' => array(
			'idgestion_banque' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'date' => array('appgini' => 'DATE null '),
			'mouvement' => array('appgini' => 'VARCHAR(80) not null '),
			'libelle' => array('appgini' => 'VARCHAR(80) null '),
			'compte_emetteur' => array('appgini' => 'INT unsigned null '),
			'numero' => array('appgini' => 'INT unsigned null '),
			'banque' => array('appgini' => 'INT unsigned null '),
			'compte_beneficiaire' => array('appgini' => 'INT unsigned null '),
			'numero_cb' => array('appgini' => 'INT unsigned null '),
			'banque_cb' => array('appgini' => 'INT unsigned null '),
			'cheque_mv' => array('appgini' => 'INT unsigned null '),
			'cheque_ce' => array('appgini' => 'VARCHAR(80) null unique '),
			'depense' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'recette' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'solde' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'cheque' => array('appgini' => 'VARCHAR(80) null '),
			'observations' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'suivi_banques' => array(
			'idsb' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'date' => array('appgini' => 'DATE null '),
			'mouvement' => array('appgini' => 'VARCHAR(80) null '),
			'description' => array('appgini' => 'INT unsigned null '),
			'reference' => array('appgini' => 'VARCHAR(80) null unique '),
			'debits' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'credits' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'balance' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'observations' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'charges_fixe' => array(
			'idcf' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'date' => array('appgini' => 'DATE null '),
			'libelle' => array('appgini' => 'INT unsigned null '),
			'mouvement' => array('appgini' => 'VARCHAR(80) null '),
			'piece' => array('appgini' => 'VARCHAR(80) null '),
			'beneficiaire' => array('appgini' => 'VARCHAR(80) null '),
			'montant' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'observations' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'caisse' => array(
			'idcaisse' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'date' => array('appgini' => 'DATE null '),
			'mouvement' => array('appgini' => 'VARCHAR(80) not null '),
			'libelle' => array('appgini' => 'VARCHAR(80) null '),
			'recette' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'depense' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'solde' => array('appgini' => 'DECIMAL(10,0) null default \'0\' '),
			'observations' => array('appgini' => 'TEXT null '),
			'groupe' => array('appgini' => 'VARCHAR(80) null '),
			'agent' => array('appgini' => 'VARCHAR(80) null '),
		),
		'type_visa' => array(
			'idtype_visa' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'type_visa' => array('appgini' => 'VARCHAR(80) null '),
		),
		'libelle_charges_fixe' => array(
			'idlcf' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'libelle' => array('appgini' => 'VARCHAR(80) null '),
		),
		'desc_suivi_banque' => array(
			'iddsb' => array('appgini' => 'INT unsigned not null primary key auto_increment '),
			'description' => array('appgini' => 'VARCHAR(255) null '),
		),
	);

	$table_captions = getTableList();

	/* function for preparing field definition for comparison */
	function prepare_def($def) {
		$def = strtolower($def);

		/* ignore 'null' */
		$def = preg_replace('/\s+not\s+null\s*/', '%%NOT_NULL%%', $def);
		$def = preg_replace('/\s+null\s*/', ' ', $def);
		$def = str_replace('%%NOT_NULL%%', ' not null ', $def);

		/* ignore length for int data types */
		$def = preg_replace('/int\s*\([0-9]+\)/', 'int', $def);

		/* make sure there is always a space before mysql words */
		$def = preg_replace('/(\S)(unsigned|not null|binary|zerofill|auto_increment|default)/', '$1 $2', $def);

		/* treat 0.000.. same as 0 */
		$def = preg_replace('/([0-9])*\.0+/', '$1', $def);

		/* treat unsigned zerofill same as zerofill */
		$def = str_ireplace('unsigned zerofill', 'zerofill', $def);

		/* ignore zero-padding for date data types */
		$def = preg_replace("/date\s*default\s*'([0-9]{4})-0?([1-9])-0?([1-9])'/", "date default '$1-$2-$3'", $def);

		return trim($def);
	}

	/**
	 *  @brief creates/fixes given field according to given schema
	 *  @return integer: 0 = error, 1 = field updated, 2 = field created
	 */
	function fix_field($fix_table, $fix_field, $schema, &$qry) {
		if(!isset($schema[$fix_table][$fix_field])) return 0;

		$def = $schema[$fix_table][$fix_field];
		$field_added = $field_updated = false;
		$eo['silentErrors'] = true;

		// field exists?
		$res = sql("show columns from `{$fix_table}` like '{$fix_field}'", $eo);
		if($row = db_fetch_assoc($res)) {
			// modify field
			$qry = "alter table `{$fix_table}` modify `{$fix_field}` {$def['appgini']}";
			sql($qry, $eo);

			// remove unique from db if necessary
			if($row['Key'] == 'UNI' && !stripos($def['appgini'], ' unique')) {
				// retrieve unique index name
				$res_unique = sql("show index from `{$fix_table}` where Column_name='{$fix_field}' and Non_unique=0", $eo);
				if($row_unique = db_fetch_assoc($res_unique)) {
					$qry_unique = "drop index `{$row_unique['Key_name']}` on `{$fix_table}`";
					sql($qry_unique, $eo);
					$qry .= ";\n{$qry_unique}";
				}
			}

			return 1;
		}

		// missing field is defined as PK and table has another PK field?
		$current_pk = getPKFieldName($fix_table);
		if(stripos($def['appgini'], 'primary key') !== false && $current_pk !== false) {
			// if current PK is not another AppGini-defined field, then rename it.
			if(!isset($schema[$fix_table][$current_pk])) {
				// no need to include 'primary key' in definition since it's already a PK field
				$redef = str_ireplace(' primary key', '', $def['appgini']);
				$qry = "alter table `{$fix_table}` change `{$current_pk}` `{$fix_field}` {$redef}";
				sql($qry, $eo);
				return 1;
			}

			// current PK field is another AppGini-defined field
			// this happens if table had a PK field in AppGini then it was unset as PK
			// and another field was created and set as PK
			// in that case, drop PK index from current PK
			// and also remove auto_increment from it if defined
			// then proceed to creating the missing PK field
			$pk_def = str_ireplace(' auto_increment', '', $schema[$fix_table][$current_pk]);
			sql("alter table `{$fix_table}` modify `{$current_pk}` {$pk_def}", $eo);
		}

		// create field
		$qry = "alter table `{$fix_table}` add column `{$fix_field}` {$def['appgini']}";
		sql($qry, $eo);
		return 2;
	}

	/* process requested fixes */
	$fix_table = (isset($_GET['t']) ? $_GET['t'] : false);
	$fix_field = (isset($_GET['f']) ? $_GET['f'] : false);
	$fix_all = (isset($_GET['all']) ? true : false);

	if($fix_field && $fix_table) $fix_status = fix_field($fix_table, $fix_field, $schema, $qry);

	/* retrieve actual db schema */
	foreach($table_captions as $tn => $tc) {
		$eo['silentErrors'] = true;
		$res = sql("show columns from `{$tn}`", $eo);
		if($res) {
			while($row = db_fetch_assoc($res)) {
				if(!isset($schema[$tn][$row['Field']]['appgini'])) continue;
				$field_description = strtoupper(str_replace(' ', '', $row['Type']));
				$field_description = str_ireplace('unsigned', ' unsigned', $field_description);
				$field_description = str_ireplace('zerofill', ' zerofill', $field_description);
				$field_description = str_ireplace('binary', ' binary', $field_description);
				$field_description .= ($row['Null'] == 'NO' ? ' not null' : '');
				$field_description .= ($row['Key'] == 'PRI' ? ' primary key' : '');
				$field_description .= ($row['Key'] == 'UNI' ? ' unique' : '');
				$field_description .= ($row['Default'] != '' ? " default '" . makeSafe($row['Default']) . "'" : '');
				$field_description .= ($row['Extra'] == 'auto_increment' ? ' auto_increment' : '');

				$schema[$tn][$row['Field']]['db'] = '';
				if(isset($schema[$tn][$row['Field']])) {
					$schema[$tn][$row['Field']]['db'] = $field_description;
				}
			}
		}
	}

	/* handle fix_all request */
	if($fix_all) {
		foreach($schema as $tn => $fields) {
			foreach($fields as $fn => $fd) {
				if(prepare_def($fd['appgini']) == prepare_def($fd['db'])) continue;
				fix_field($tn, $fn, $schema, $qry);
			}
		}

		redirect('admin/pageRebuildFields.php');
		exit;
	}
?>

<?php if($fix_status == 1 || $fix_status == 2) { ?>
	<div class="alert alert-info alert-dismissable">
		<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
		<i class="glyphicon glyphicon-info-sign"></i>
		<?php 
			$originalValues = array('<ACTION>', '<FIELD>', '<TABLE>', '<QUERY>');
			$action = ($fix_status == 2 ? 'create' : 'update');
			$replaceValues = array($action, $fix_field, $fix_table, $qry);
			echo str_replace($originalValues, $replaceValues, $Translation['create or update table']);
		?>
	</div>
<?php } ?>

<div class="page-header"><h1>
	<?php echo $Translation['view or rebuild fields'] ; ?>
	<button type="button" class="btn btn-default" id="show_deviations_only"><i class="glyphicon glyphicon-eye-close"></i> <?php echo $Translation['show deviations only'] ; ?></button>
	<button type="button" class="btn btn-default hidden" id="show_all_fields"><i class="glyphicon glyphicon-eye-open"></i> <?php echo $Translation['show all fields'] ; ?></button>
</h1></div>

<p class="lead"><?php echo $Translation['compare tables page'] ; ?></p>

<div class="alert summary"></div>
<table class="table table-responsive table-hover table-striped">
	<thead><tr>
		<th></th>
		<th><?php echo $Translation['field'] ; ?></th>
		<th><?php echo $Translation['AppGini definition'] ; ?></th>
		<th><?php echo $Translation['database definition'] ; ?></th>
		<th id="fix_all"></th>
	</tr></thead>

	<tbody>
	<?php foreach($schema as $tn => $fields) { ?>
		<tr class="text-info"><td colspan="5"><h4 data-placement="left" data-toggle="tooltip" title="<?php echo str_replace ( "<TABLENAME>" , $tn , $Translation['table name title']) ; ?>"><i class="glyphicon glyphicon-th-list"></i> <?php echo $table_captions[$tn]; ?></h4></td></tr>
		<?php foreach($fields as $fn => $fd) { ?>
			<?php $diff = ((prepare_def($fd['appgini']) == prepare_def($fd['db'])) ? false : true); ?>
			<?php $no_db = ($fd['db'] ? false : true); ?>
			<tr class="<?php echo ($diff ? 'warning' : 'field_ok'); ?>">
				<td><i class="glyphicon glyphicon-<?php echo ($diff ? 'remove text-danger' : 'ok text-success'); ?>"></i></td>
				<td><?php echo $fn; ?></td>
				<td class="<?php echo ($diff ? 'bold text-success' : ''); ?>"><?php echo $fd['appgini']; ?></td>
				<td class="<?php echo ($diff ? 'bold text-danger' : ''); ?>"><?php echo thisOr($fd['db'], $Translation['does not exist']); ?></td>
				<td>
					<?php if($diff && $no_db) { ?>
						<a href="pageRebuildFields.php?t=<?php echo $tn; ?>&f=<?php echo $fn; ?>" class="btn btn-success btn-xs btn_create" data-toggle="tooltip" data-placement="top" title="<?php echo $Translation['create field'] ; ?>"><i class="glyphicon glyphicon-plus"></i> <?php echo $Translation['create it'] ; ?></a>
					<?php }elseif($diff) { ?>
						<a href="pageRebuildFields.php?t=<?php echo $tn; ?>&f=<?php echo $fn; ?>" class="btn btn-warning btn-xs btn_update" data-toggle="tooltip" title="<?php echo $Translation['fix field'] ; ?>"><i class="glyphicon glyphicon-cog"></i> <?php echo $Translation['fix it'] ; ?></a>
					<?php } ?>
				</td>
			</tr>
		<?php } ?>
	<?php } ?>
	</tbody>
</table>
<div class="alert summary"></div>

<style>
	.bold{ font-weight: bold; }
	[data-toggle="tooltip"]{ display: block !important; }
</style>

<script>
	$j(function() {
		$j('[data-toggle="tooltip"]').tooltip();

		$j('#show_deviations_only').click(function() {
			$j(this).addClass('hidden');
			$j('#show_all_fields').removeClass('hidden');
			$j('.field_ok').hide();
		});

		$j('#show_all_fields').click(function() {
			$j(this).addClass('hidden');
			$j('#show_deviations_only').removeClass('hidden');
			$j('.field_ok').show();
		});

		$j('.btn_update, #fix_all').click(function() {
			return confirm("<?php echo $Translation['field update warning'] ; ?>");
		});

		var count_updates = $j('.btn_update').length;
		var count_creates = $j('.btn_create').length;
		if(!count_creates && !count_updates) {
			$j('.summary').addClass('alert-success').html("<?php echo $Translation['no deviations found'] ; ?>");
		}else{
			var fieldsCount = "<?php echo $Translation['error fields']; ?>";
			fieldsCount = fieldsCount.replace(/<CREATENUM>/, count_creates ).replace(/<UPDATENUM>/, count_updates);


			$j('.summary')
				.addClass('alert-warning')
				.html(
					fieldsCount + 
					'<br><br>' + 
					'<a href="pageBackupRestore.php" class="alert-link">' +
						'<b><?php echo addslashes($Translation['backup before fix']); ?></b>' +
					'</a>'
				);

			$j('<a href="pageRebuildFields.php?all=1" class="btn btn-danger btn-block"><i class="glyphicon glyphicon-cog"></i> <?php echo addslashes($Translation['fix all']); ?></a>').appendTo('#fix_all');
		}
	});
</script>

<?php
	include("{$currDir}/incFooter.php");
?>
