<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Admin extends Controller_Applicationadmin {
public $template = 'templateadmin';	


public function action_index()
{
     // Load the user information 	
		$user = Auth::instance()->get_user();

		// if a user is not logged in, redirect to login page 		
		if (!$user)
		{
			Request::current()->redirect('user/login');
		}
		
$content = View::factory('admin/panel')
->bind('nom_site', $nom_site);

$this->template->content = $content;
}

/////////// Gestion d'authentification Application mobile //////////////////////////	
public function action_loginuser()
{

if($_POST)
{
$username= $_POST['username'];
$password= $_POST['password'];

$user = Model::factory('User');
$status = $user->_login($username,$password);

if ($status){
$content='';
$user = Model::factory('User');
$users = $user->select_user_password($username,$password);

foreach ($users as $user1) :
  $content=$content.$user1['id'].'<p/>'.$user1['username'];
endforeach; 

$this->template = View::factory('templaterequest');
$this->template->content = $content;
}else
{
$content = '';
$this->template = View::factory('templaterequest');
$this->template->content = $content;
}
}
}


public function action_listeadmins()
{
$content = View::factory('admin/listeadmins')
->bind('nom_site', $nom_site);

$content->bind_global('users', $users);
$users = Model::factory('User');
$users = $users->get_all_admin();

$this->template->content = $content;
}

public function action_ajouteradmin()
{

if($_POST)
{

$this->request->redirect("admin/listeadmins");

}else
{
$content = View::factory('admin/formadmin')
->bind('nom_site', $nom_site);


$this->template->content = $content;

}
}

public function action_modifieradmin()
{
if($_POST)
{


$this->request->redirect("admin/listeadmins");

}else
{

$content = View::factory('admin/editformadmin')
		    ->bind('nom_site', $nom_site)
			->bind('errors', $errors)
			->bind('username', $username)
			->bind('email', $email)
			->bind('isuser', $isuser)
			->bind('id', $id)
			->bind('message', $message);

$content->bind_global('user', $user);
$user = Model::factory('User');
$id= (string) $this->request->param('id');

$user = ORM::factory('user')
                  ->select(array($id));


                 $username=$user[0]['username'];
			     $email=$user[0]['email'];
			     $isuser=$user[0]['isuser'];
			     $id=$user[0]['id'];

$this->template->content = $content;
}
}

public function action_supprimeradmin()
{


$content = View::factory('admin/listeadmins')
->bind('nom_site', $nom_site);

$user = Model::factory('User');
$id= (string) $this->request->param('id');

 $user = ORM::factory('user')
                  ->where('id', '=', $id)
                  ->find()->delete();


$this->request->redirect("admin/listeadmins");

}

public function action_listeusers()
{
$content = View::factory('admin/listeusers')
->bind('nom_site', $nom_site);

$content->bind_global('users', $users);
$users = Model::factory('User');
$users = $users->get_all();

$this->template->content = $content;
}

public function action_ajouteruser()
{

if($_POST)
{
$email= $_POST['email'];
$username= $_POST['username'];
$raison_sociale= $_POST['raison_sociale'];
$telephone= $_POST['telephone'];
$nom_contact= $_POST['nom_contact'];
$adresse= $_POST['adresse'];
$password= $_POST['password'];

$sliders = Model::factory('User');
$sliders = $sliders->add($email,$username,$raison_sociale,$telephone,$nom_contact,$adresse,$password);

$this->request->redirect("admin/listeusers");

}else
{
$content = View::factory('admin/formuser')
->bind('nom_site', $nom_site);


$this->template->content = $content;

}
}

public function action_modifieruser()
{

if($_POST)
{


$this->request->redirect("admin/listeusers");

}else
{

$content = View::factory('admin/editformuser')
		    ->bind('nom_site', $nom_site)
			->bind('errors', $errors)
			->bind('username', $username)
			->bind('email', $email)
			->bind('isuser', $isuser)
			->bind('raison_sociale', $raison_sociale)
			->bind('telephone', $telephone)
			->bind('nom_contact', $nom_contact)
			->bind('adresse', $adresse)
			->bind('id', $id)
			->bind('message', $message);

$content->bind_global('user', $user);
$user = Model::factory('User');
$id= (string) $this->request->param('id');

$user = ORM::factory('user')
                  ->select(array($id));


                 $username=$user[0]['username'];
			     $email=$user[0]['email'];
			     $isuser=$user[0]['isuser'];
			     $raison_sociale=$user[0]['raison_sociale'];
			     $telephone=$user[0]['telephone'];
			     $nom_contact=$user[0]['nom_contact'];
			     $adresse=$user[0]['adresse'];
			     $id=$user[0]['id'];

$this->template->content = $content;


}
}

public function action_supprimeruser()
{


$content = View::factory('admin/listeusers')
->bind('nom_site', $nom_site);

$user = Model::factory('User');
$id= (string) $this->request->param('id');

 $user = ORM::factory('user')
                  ->where('id', '=', $id)
                  ->find()->delete();


$this->request->redirect("admin/listeusers");

}

//////////////////Fin gestion des Commandes ///////////////////:

/////////// Gestion des slides //////////////////////////	

public function action_listecommandes()
{
$content = View::factory('admin/listecommandes')
->bind('nom_site', $nom_site) 
->bind('cmdcount', $cmdcount); 

$content->bind_global('commandes', $commandes);
$content->bind_global('cmdcount', $cmdcount);

$commandes = Model::factory('Commande');
$cmdcount = Model::factory('Commande')->count_all_non_valide();
$commandes = $commandes->get_all();

$this->template->content = $content;
}

public function action_ajoutercommande()
{

if($_POST)
{
$unite=$_POST['unite'];
$id_client=$_POST['id_client'];
$client=$_POST['client'];
$id_product=$_POST['id_product'];
$product=$_POST['product'];
$quantite=$_POST['quantite'];
$date_commande=getdate();
$date_commande=date('Y-m-d H:i:s');

$user =  Model::factory('user');
$user=$user->select($id_client); 
$telephone=$user['telephone'];

$commandes = Model::factory('Commande');
$commandes = $commandes->add($unite,$id_client,$client,$telephone, $id_product,$product,$quantite,$date_commande);

$content="";
$this->template = View::factory('templaterequest');
$this->template->content = $content;

}else
{
$content = View::factory('admin/formslider')
->bind('nom_site', $nom_site);

$content->bind_global('sliders', $sliders);
$sliders = Model::factory('Slider');
$sliders = $sliders->get_all();

$this->template->content = $content;

}
}

public function action_modifiercommande()
{

if(isset($_POST['libelle']))
{
$id= $_POST['id'];
$nom= '';//str_replace('\application', '', realpath(APPPATH)).'\media\images\slider\\'.basename($_FILES['fileInput']['name']);
$sliders = Model::factory('Slider');
$sliders = $sliders->edit($id,$nom,$_POST['libelle']);

if(isset($_FILES['fileInput']))
{ 
   

     $dossier =  str_replace('application', '', realpath(APPPATH))."media/images/slider/";
     $fichier = basename($_FILES['fileInput']['name']);
     if(move_uploaded_file($_FILES['fileInput']['tmp_name'], $dossier.'slide-'.$id.'.jpg')) //Si la fonction renvoie TRUE, c'est que �a a fonctionn�...
     {
          echo 'Chargement effectu� avec succ�s !';
     }
     else //Sinon (la fonction renvoie FALSE).
     {
          echo 'Echec de chargement !';
     }
}

$this->request->redirect("admin/listeslider");

}else
{

$content = View::factory('admin/editformslider')
->bind('nom_site', $nom_site);

$content->bind_global('slide', $slide);
$slide = Model::factory('Slider');
$id= (string) $this->request->param('id');

$slide = $slide->select($id);

$this->template->content = $content;


}
}

public function action_supprimercommande()
{


$content = View::factory('admin/listecommandes')
->bind('nom_site', $nom_site);

$commande = Model::factory('Commande');
$id= (string) $this->request->param('id');

$commande = $commande->delete($id);

$this->request->redirect("admin/listecommandes");

}

public function action_validercommande()
{


$content = View::factory('admin/listecommandes')
->bind('nom_site', $nom_site);

$commande = Model::factory('Commande');
$id= (string) $this->request->param('id');

$commande = $commande->valide($id);

$this->request->redirect("admin/listecommandes");

}

public function action_devalidercommande()
{


$content = View::factory('admin/listecommandes')
->bind('nom_site', $nom_site);

$commande = Model::factory('Commande');
$id= (string) $this->request->param('id');

$commande = $commande->devalide($id);

$this->request->redirect("admin/listecommandes");

}

//////////////////Fin gestion des Commandes ///////////////////:

/////////// Gestion des slides //////////////////////////	

public function action_listeslider()
{
$content = View::factory('admin/listeslider')
->bind('nom_site', $nom_site);

$content->bind_global('sliders', $sliders);
$sliders = Model::factory('Slider');
$sliders = $sliders->get_all();

$this->template->content = $content;
}

public function action_ajouterslider()
{

if($_POST)
{

$nom= $_POST['libelle'];
$sliders = Model::factory('Slider');
$sliders = $sliders->add('',$nom);

if(isset($_FILES['fileInput']))
{ 
   

     $dossier =  str_replace('application', '', realpath(APPPATH))."media/images/slider/";
     $fichier = basename($_FILES['fileInput']['name']);
     if(move_uploaded_file($_FILES['fileInput']['tmp_name'], $dossier.'slide-'.$sliders[0].'.jpg')) //Si la fonction renvoie TRUE, c'est que �a a fonctionn�...
     {
          echo $sliders[0];
     }
     else //Sinon (la fonction renvoie FALSE).
     {
          echo 'Echec de chargement !';
     }
}
$content = View::factory('admin/listeslider')
->bind('nom_site', $nom_site);

$content->bind_global('sliders', $sliders);
$sliders = Model::factory('Slider');
$sliders = $sliders->get_all();

$this->template->content = $content;
//$this->request->redirect("admin/listeslider");

}else
{
$content = View::factory('admin/formslider')
->bind('nom_site', $nom_site);

$content->bind_global('sliders', $sliders);
$sliders = Model::factory('Slider');
$sliders = $sliders->get_all();

$this->template->content = $content;

}
}

public function action_modifierslider()
{

if(isset($_POST['libelle']))
{
$id= $_POST['id'];
$nom= '';//str_replace('\application', '', realpath(APPPATH)).'\media\images\slider\\'.basename($_FILES['fileInput']['name']);
$sliders = Model::factory('Slider');
$sliders = $sliders->edit($id,$nom,$_POST['libelle']);

if(isset($_FILES['fileInput']))
{ 
   

     $dossier =  str_replace('application', '', realpath(APPPATH))."media/images/slider/";
     $fichier = basename($_FILES['fileInput']['name']);
     if(move_uploaded_file($_FILES['fileInput']['tmp_name'], $dossier.'slide-'.$id.'.jpg')) //Si la fonction renvoie TRUE, c'est que �a a fonctionn�...
     {
          echo 'Chargement effectu� avec succ�s !';
     }
     else //Sinon (la fonction renvoie FALSE).
     {
          echo 'Echec de chargement !';
     }
}

$this->request->redirect("admin/listeslider");

}else
{

$content = View::factory('admin/editformslider')
->bind('nom_site', $nom_site);

$content->bind_global('slide', $slide);
$slide = Model::factory('Slider');
$id= (string) $this->request->param('id');

$slide = $slide->select($id);

$this->template->content = $content;


}
}

public function action_supprimerslider()
{


$content = View::factory('admin/listeslider')
->bind('nom_site', $nom_site);

$slide = Model::factory('Slider');
$id= (string) $this->request->param('id');

$slide = $slide->delete($id);

$this->request->redirect("admin/listeslider");

}

//////////////////Fin gestion des slides ///////////////////:

///////////////////// Gestion des articles /////////////////////

public function action_listearticles()
{
$content = View::factory('admin/listearticles')
->bind('nom_site', $nom_site);

$content->bind_global('articles', $articles);
$articles = Model::factory('Article');
$articles = $articles->get_all();

$this->template->content = $content;
}

public function action_ajouterarticle()
{

if($_POST)
{

$nom= $_POST['nom'];
$texte= $_POST['texte'];
$article = Model::factory('Article');
$article = $article->add($nom,$texte);

$this->request->redirect("admin/listearticles");

}else
{
$content = View::factory('admin/formarticle')
->bind('nom_site', $nom_site);

$this->template->content = $content;

}
}


public function action_modifierarticle()
{

if($_POST)
{

$id= $_POST['id'];
$nom= $_POST['nom'];
$texte= $_POST['texte'];

$texte=substr($texte,45);
$texte=str_replace("</body>","",$texte);
$texte=str_replace("</html>","",$texte);

$articles = Model::factory('Article');
$articles = $articles->edit($id,$nom,$texte);

$this->request->redirect("admin/listearticles");

}else
{

$content = View::factory('admin/editformarticle')
->bind('nom_site', $nom_site);

$content->bind_global('article', $article);
$article = Model::factory('Article');
$id= (string) $this->request->param('id');

$article = $article->select($id);

$this->template->content = $content;


}
}


public function action_listeproduits()
{
$content = View::factory('admin/listeproduits')
->bind('nom_site', $nom_site);

$content->bind_global('produits', $produits);
$produits = Model::factory('Produit');
$produits = $produits->get_all_admin('');

$this->template->content = $content;
}

public function action_ajouterproduit()
{


if($_POST)
{

$id= $_POST['id'];
$libelle= $_POST['libelle'];
$unite = explode("_",$_POST['unite']);
$unite_id = $unite[0];
//$categorie= $_POST['categorie'];
$description= $_POST['description'];
//$IsService= $_POST['isservice'];

$description=substr($description,45);
$description=str_replace("</body>","",$description);
$description=str_replace("</html>","",$description);

$produit = Model::factory('Produit');
$produit = $produit->add($libelle,$description,'Disponible',$unite_id);

if(isset($_FILES['fileInput']))
{ 
   

     $dossier =  str_replace('application', '', realpath(APPPATH))."media/images/produits/";
     $fichier = basename($_FILES['fileInput']['name']);
     if(move_uploaded_file($_FILES['fileInput']['tmp_name'], $dossier.$produit[0].'.png')) //Si la fonction renvoie TRUE, c'est que �a a fonctionn�...
     {
          echo 'Chargement effectu� avec succ�s !';
     }
     else //Sinon (la fonction renvoie FALSE).
     {
          echo 'Echec de chargement !';
     }
}

$this->request->redirect("admin/listeproduits");
}else
{
$id= (string) $this->request->param('id');

$content = View::factory('admin/formproduit')
->bind('nom_site', $nom_site);

$content->bind_global('produit', $produits);
$produits = Model::factory('Produit');

$produits = $produits->select($id);

$this->template->content = $content;
}
}



public function action_modifierproduit()
{

if($_POST)
{

$id= $_POST['id'];
$libelle= $_POST['libelle'];
//$unite= $_POST['unite'];

$unite = explode("_",$_POST['unite']);
$unite_id = $unite[0];
//$unite_name = $unite[1];

//$categorie= $_POST['categorie'];
$description= $_POST['description'];
//$IsService= $_POST['isservice'];

$description=substr($description,45);
$description=str_replace("</body>","",$description);
$description=str_replace("</html>","",$description);

$produit = Model::factory('Produit');
$produit = $produit->edit($id,$libelle,'',$description,0,$unite_id);

if(isset($_FILES['fileInput']))
{ 
   

     $dossier =  str_replace('application', '', realpath(APPPATH))."media/images/produits/";
     $fichier = basename($_FILES['fileInput']['name']);
     if(move_uploaded_file($_FILES['fileInput']['tmp_name'], $dossier.$id.'.png')) //Si la fonction renvoie TRUE, c'est que �a a fonctionn�...
     {
          echo 'Chargement effectu� avec succ�s !';
     }
     else //Sinon (la fonction renvoie FALSE).
     {
          echo 'Echec de chargement !';
     }
}

$this->request->redirect("admin/listeproduits");
}else
{
$id= (string) $this->request->param('id');

$content = View::factory('admin/editformproduit')
->bind('nom_site', $nom_site);

$content->bind_global('produit', $produits);
$produits = Model::factory('Produit');

$produits = $produits->select($id);

$this->template->content = $content;
}
}


  
public function action_supprimerproduit(){
   $content = View::factory('admin/listeproduits')
->bind('nom_site', $nom_site);

$produit = Model::factory('Produit');
$id= (string) $this->request->param('id');

$produit = $produit->delete($id);

$this->request->redirect('admin/listeproduits');
}

public function action_listeproduitsagregal()
{

if($_POST)
{
$username= $_POST['username'];
$password= $_POST['password'];
$unite= $_POST['unite'];


$produits = Model::factory('Produit');
$produits = $produits->get_all_products_unite($unite);
$content = '';
foreach ($produits as $produit) :
  $content=$content.$produit['id'].'<p/>'.$produit['libelle'].'<br/>';
endforeach; 
$this->template = View::factory('templaterequest');
$this->template->content = $content;

}
}
 
 // Gestion des partenaires ###
 
 public function action_listepromotions()
{
$content = View::factory('admin/listepromotions')
->bind('nom_site', $nom_site);

$content->bind_global('promotions', $promotions);
$promotions = Model::factory('Promotion');
$promotions = $promotions->get_all('');

$this->template->content = $content;
}

public function action_ajouterpromotion()
{


if($_POST)
{

$date_debut= $_POST['date_debut'];
$date_fin= $_POST['date_fin'];
$libelle= $_POST['libelle'];

$promotion = Model::factory('Promotion');
$promotion = $promotion->add($libelle,$date_debut,$date_fin);


$this->request->redirect("admin/listepromotions");
}else
{
$id= (string) $this->request->param('id');

$content = View::factory('admin/formpromotion')
->bind('nom_site', $nom_site);



$this->template->content = $content;
}
}



public function action_modifierpromotion()
{

if($_POST)
{

$id= $_POST['id'];
$date_debut= $_POST['date_debut'];
$date_fin= $_POST['date_fin'];
$libelle= $_POST['libelle'];


$promotion = Model::factory('Promotion');
$promotion = $promotion->edit($id,$libelle,$date_debut,$date_fin);


$this->request->redirect("admin/listepromotions");
}else
{
$id= (string) $this->request->param('id');

$content = View::factory('admin/editformpromotion')
->bind('nom_site', $nom_site);

$content->bind_global('promotion', $promotion);
$promotion = Model::factory('Promotion');

$promotion = $promotion->select($id);

$this->template->content = $content;
}
}


  
public function action_supprimerpromotion(){
   $content = View::factory('admin/listepromotions')
->bind('nom_site', $nom_site);

$partenaire = Model::factory('Promotion');
$id= (string) $this->request->param('id');

$partenaire = $partenaire->delete($id);

$this->request->redirect('admin/listepromotions');
}

 
 
 
 
 // Fin gestion des promotions
 
 
 // Gestion des partenaires ###
 
 public function action_listepartenaires()
{
$content = View::factory('admin/listepartenaires')
->bind('nom_site', $nom_site);

$content->bind_global('partenaires', $partenaires);
$partenaires = Model::factory('Partenaire');
$partenaires = $partenaires->get_all('');

$this->template->content = $content;
}

public function action_ajouterpartenaire()
{


if($_POST)
{

$id= $_POST['id'];
$intitule= $_POST['intitule'];

$partenaire = Model::factory('Partenaire');
$partenaire = $partenaire->add($intitule);

if(isset($_FILES['fileInput']))
{ 
   

     $dossier =  str_replace('application', '', realpath(APPPATH))."media/images/partenaires/";
     $fichier = basename($_FILES['fileInput']['name']);
     if(move_uploaded_file($_FILES['fileInput']['tmp_name'], $dossier.$partenaire[0].'.png')) //Si la fonction renvoie TRUE, c'est que �a a fonctionn�...
     {
          echo $partenaire[0];
     }
     else //Sinon (la fonction renvoie FALSE).
     {
          echo 'Echec de chargement !';
     }
}


$this->request->redirect("admin/listepartenaires");
}else
{
$id= (string) $this->request->param('id');

$content = View::factory('admin/formpartenaire')
->bind('nom_site', $nom_site);

$content->bind_global('partenaire', $partenaire);
$partenaire = Model::factory('Partenaire');

$partenaire = $partenaire->select($id);

$this->template->content = $content;
}
}



public function action_modifierpartenaire()
{

if($_POST)
{

$id= $_POST['id'];
$intitule= $_POST['intitule'];


$partenaire = Model::factory('Partenaire');
$partenaire = $partenaire->edit($id,$intitule);

if(isset($_FILES['fileInput']))
{ 
   

     $dossier =  str_replace('application', '', realpath(APPPATH))."media/images/partenaires/";
     $fichier = basename($_FILES['fileInput']['name']);
     if(move_uploaded_file($_FILES['fileInput']['tmp_name'], $dossier.$id.'.png')) //Si la fonction renvoie TRUE, c'est que �a a fonctionn�...
     {
          echo $dossier;
     }
     else //Sinon (la fonction renvoie FALSE).
     {
          echo 'Echec de chargement !';
     }
}

$this->request->redirect("admin/listepartenaires");
}else
{
$id= (string) $this->request->param('id');

$content = View::factory('admin/editformpartenaire')
->bind('nom_site', $nom_site);

$content->bind_global('partenaire', $partenaire);
$partenaire = Model::factory('Partenaire');

$partenaire = $partenaire->select($id);

$this->template->content = $content;
}
}


  
public function action_supprimerpartenaire(){
   $content = View::factory('admin/listepartenaires')
->bind('nom_site', $nom_site);

$partenaire = Model::factory('Partenaire');
$id= (string) $this->request->param('id');

$partenaire = $partenaire->delete($id);

$this->request->redirect('admin/listepartenaires');
}

 
 
 
 
 // Fin gestion des partenaires
 
 // Gestion d'authentification pour la partie administration
 
 
/*Controlleur du LOGIN*/
public function action_login() 
 {
		// if (!empty($_SESSION['nom_utilisateur']))
	//	{
		    
	//	  	$this->request->redirect('admin');
	//	}
		
		// Definition des constantes et variables
  $errorMessage = '';
 
  // Test de l'envoi du formulaire
  if (isset($_post['username']) && isset($_post['password'])) 
  {
  
		    $user =  Model::factory( 'user');
			$status = $user->_login($_POST['username'],$_POST['password']);
			if ($status)
			{
				$user=$user->select_user_password($_POST['username'],$_POST['password']);       
						 
			if ($user[0]['isuser']==0)
			{	$this->request->redirect('admin/login');}
			
			else{
			
			 // On ouvre la session
            // session_start();
             // On enregistre le login en session
            //$_SESSION['nom_utilisateur'] = 'erzer';
            //$_SESSION['iscommercial'] = $user[0]['iscommercial'];
        //	$this->request->redirect('admin11');
            // On redirige vers le fichier admin.php
            //exit();
			}

	       }else
			{
				//$errors = $_POST->errors('login');
					$this->request->redirect('admin/login');
			}
		}
 }
 public function action_logout() 
 {
		
				 // D�marrage ou restauration de la session
  session_start();
  // R�initialisation du tableau de session
  // On le vide int�gralement
  $_SESSION = array();
  // Destruction de la session
  session_destroy();
  // Destruction du tableau de session
  unset($_SESSION);
 $this->request->redirect('admin/login');
		
 }
 

}
//end page