<?php
/*
* Generated by CRUDigniter v3.2
* www.crudigniter.com
*/
class Mission extends FrontApplication{
function __construct()
{
parent::__construct();
$this->load->model('Mission_model');
}
function pdf($id){
$this->data["mission"]=$this->Mission_model->get_mission_to_pdf($id);
$this->load->library("html2pdf/HTML2PDF");
$this->load->view("mission/pdf",$this->data);
}
function externe($id){
$this->data["mission"]=$this->Mission_model->get_mission_to_externe($id);
$this->load->library("html2pdf/HTML2PDF");
$this->load->view("mission/externe",$this->data);
}
function exportlist($datedebut, $datefin,$typemission,$etat,$iduser){
if($typemission>0){
$this->db->where("typemission_id",$typemission);
}
if($iduser>0){
$this->db->where("users_id",$iduser);
}
if($etat>=0){
$this->db->where("etat",$etat);
}
$this->data["mission"]=$this->Mission_model->get_all_mission_by_date($datedebut, $datefin);
$this->load->library("html2pdf/HTML2PDF");
$this->load->view("mission/export",$this->data);
}
/*
* Listing of mission
*/
function index($msg=null)
{
$params['limit'] = RECORDS_PER_PAGE;
$params['offset'] = ($this->input->get('per_page')) ? $this->input->get('per_page') : 0;
$params['readException']=in_array($this->data["readException"], $this->data["access"]["Mission"]->actions);
$config = $this->config->item('pagination');
$config['base_url'] = site_url('mission/index?');
$config['total_rows'] = $this->Mission_model->get_all_mission_count();
$this->pagination->initialize($config);
$user = $this->session->userdata("auth");
//-----------------------------------------------------------------------
$this->load->model('Typemission_model');
$this->data['all_typemission'] = $this->Typemission_model->get_all_typemission();
//--------------------------------------------------------------------------
$this->load->model('User_model');
$this->data['all_users'] = $this->User_model->get_all_users();
//-----------------------------------------------------------------------------
$this->data['mission'] = $this->Mission_model->get_all_mission($params,$user["type"]);
if($msg != null && $msg =="confirm"){
$this->data['msg']= "Vous avez d'autres demandes confirmées dans la méme periode";
}
$this->data['_view'] = 'mission/index';
$this->load->view('layouts/main',$this->data);
}
/*
* Adding a new mission
*/
function add()
{
$this->load->model('User_model');
$this->load->library('form_validation');
$this->form_validation->set_rules('datedebut','Datedebut','required');
$this->form_validation->set_rules('datefin','Datefin','required');
$user = $this->session->userdata("auth");
if( !$this->User_model->isUserOut($user["iduser"],$this->input->post('datedebut'),$this->input->post('datefin')) && $this->form_validation->run())
{
$params = array(
'transport_id' => $this->input->post('transport_id'),
'objetmission_id' => $this->input->post('objetmission_id'),
'typemission_id' => $this->input->post('typemission_id'),
'interim_id' => $this->input->post('interim_id'),
'description' => $this->input->post('description'),
'datedebut' =>$this->input->post('datedebut'),
'datefin' => $this->input->post('datefin'),
'destination' => $this->input->post('destination'),
'users_id' =>$user["iduser"],
'depart_id' =>$user["iddepart"],
'etat' => 0,
'creationdate' => date("Y-m-d H:i")
);
$mission_id = $this->Mission_model->add_mission($params);
$this->data["mission"]=$this->Mission_model->get_mission_to_pdf($mission_id);
$this->data["user"]= $user;
$notified = $this->User_model->get_user($user["responsable_id"]);
$this->data["isnotifed"]= true;
$this->data["crypted"] = md5($notified["email"]);
$this->data["counter"]= $this->User_model->HasDemande($user["iduser"],$this->input->post('datedebut'),$this->input->post('datefin'));
$message = $this->load->view("mail/mission",$this->data,true);
$this->email->from($this->sendmail, 'Notification:'."Mission");
$this->email->to($notified["email"]);
$this->email->subject("Notification de demande de mission pour ".$user["name"]." ".$user["prenom"]);
$this->email->message($message);
echo $this->email->send();
if($this->User_model->isResponsableOut($user["responsable_id"])){
$interim = $this->User_model->get_user_interim($user["responsable_id"]);
$this->data["crypted"] = md5($interim["email"]);
$message = $this->load->view("mail/mission",$this->data,true);
$this->email->from($this->sendmail, 'Notification:'."Mission");
$this->email->to($interim["email"]);
$this->email->subject("Notification de demande de mission pour ".$user["name"]." ".$user["prenom"]);
$this->email->message($message);
$this->email->send();
}
redirect('mission/index');
}
else
{
if($this->input->post('datedebut')!= false && $this->User_model->isUserOut($user["iduser"],$this->input->post('datedebut'),$this->input->post('datefin')) ){
$this->data["message"]="Vous avez une demande déja en cours dans cette date";
}
$this->load->model('Transport_model');
$this->data['all_transport'] = $this->Transport_model->get_all_transport();
$this->load->model('Objetmission_model');
$this->data['all_objetmission'] = $this->Objetmission_model->get_all_objetmission();
$this->load->model('Typemission_model');
$this->data['all_typemission'] = $this->Typemission_model->get_all_typemission();
$this->load->model('User_model');
$user = $this->session->userdata("auth");
$this->data['all_users'] = $this->User_model->get_all_users_same_service($user["service_id"]);
$this->data['_view'] = 'mission/add';
$this->load->view('layouts/main',$this->data);
}
}
/*
* Editing a mission
*/
function edit($id)
{
// check if the mission exists before trying to edit it
$this->data['mission'] = $this->Mission_model->get_mission($id);
if(isset($this->data['mission']['id']))
{
$this->load->library('form_validation');
$this->form_validation->set_rules('datedebut','Datedebut','required');
$this->form_validation->set_rules('datefin','Datefin','required');
$this->form_validation->set_rules('destination','Destination','required');
if($this->form_validation->run())
{
$params = array(
'transport_id' => $this->input->post('transport_id'),
'objetmission_id' => $this->input->post('objetmission_id'),
'typemission_id' => $this->input->post('typemission_id'),
'interim_id' => $this->input->post('interim_id'),
'datedebut' => date("Y-m-d H:i",strtotime($this->input->post('datedebut'))),
'datefin' => date("Y-m-d H:i",strtotime($this->input->post('datefin'))),
'destination' => $this->input->post('destination'),
'description' => $this->input->post('description')
);
$this->Mission_model->update_mission($id,$params);
redirect('mission/index');
}
else
{
$this->load->model('Transport_model');
$this->data['all_transport'] = $this->Transport_model->get_all_transport();
$this->load->model('Objetmission_model');
$this->data['all_objetmission'] = $this->Objetmission_model->get_all_objetmission();
$this->load->model('Typemission_model');
$this->data['all_typemission'] = $this->Typemission_model->get_all_typemission();
$this->load->model('Typeconge_model');
$this->data['all_typeconge'] = $this->Typeconge_model->get_all_typeconge();
$this->load->model('Objetautorisation_model');
$this->data['all_objetautorisation'] = $this->Objetautorisation_model->get_all_objetautorisation();
$this->load->model('User_model');
$this->data['all_users'] = $this->User_model->get_all_users();
$this->data['_view'] = 'mission/edit';
$this->load->view('layouts/main',$this->data);
}
}
else
show_error('The mission you are trying to edit does not exist.');
}
/*
* Deleting mission
*/
function remove($id)
{
$mission = $this->Mission_model->get_mission($id);
// check if the mission exists before trying to delete it
if(isset($mission['id']))
{
$this->Mission_model->delete_mission($id);
redirect('mission/index');
}
else
show_error('The mission you are trying to delete does not exist.');
}
function annuler($id){
if($this->session->userdata("auth")!= false){
$updated = $this->Mission_model->update_etat($id,3);
redirect('conge/index/');
}
}
function confirm($id,$email=null){
if($email!= null){
$this->load->model("User_model");
$connected = $this->User_model->connectMail($email);
if(isset($connected[0])){
$this->session->set_userdata("auth",$connected[0]);
}
}
if($this->session->userdata("auth")!= false){
$mission = $this->Mission_model->get_mission($id);
$this->load->model('User_model');
if($this->User_model->isUserOut($mission["users_id"],$mission["datedebut"],$mission["datefin"])){
redirect("Mission/index/confirm");
}else{
$updated = $this->Mission_model->update_etat($id,1);
if($updated != false){
$this->load->model('Notifier_model');
$user = $this->User_model->get_user($mission["users_id"]);
$usermail = $this->User_model->connect($user["login"],$user["password"]);
$notifieds = $this->Notifier_model->get_notified_by_user_id($mission["users_id"]);
$this->data["mission"]=$this->Mission_model->get_mission_to_pdf($id);
$this->data["user"]= $usermail;
$this->data["isnotifed"]=false;
$message = $this->load->view("mail/mission",$this->data,true);
foreach ($notifieds as $notified) {
if($user["responsable_id"]!=$notified["notifyto"]){
$this->email->from($this->sendmail, 'Notification:'."Mission");
$this->email->to($notified["email"]);
$this->email->subject("Notification de demande de mission");
$this->email->message($message);
echo $this->email->send();
}
}
//Send mail to user with reject or confirm
$this->data["isnotifed"]=false;
$this->email->from($this->sendmail, 'Notification:'."Mission");
$this->email->to($user["email"]);
$this->email->subject("Notification de demande de mission est confirmee");
$this->email->message($message);
echo $this->email->send();
//----------------------------------------------------------
/* $this->load->model("Typemission_model");
$typemission= $this->Typemission_model->get_typemission($mission["typemission_id"]);
redirect('mission/'.$typemission["file"].'/'.$id);*/
redirect('mission/index');
}
}
}else{
redirect('Login/index/');
}
}
function reject($id,$email=null){
if($email!= null){
$this->load->model("User_model");
$connected = $this->User_model->connectMail($email);
if(isset($connected[0])){
$this->session->set_userdata("auth",$connected[0]);
}
}
if($this->session->userdata("auth")!= false){
$mission = $this->Mission_model->get_mission($id);
$this->data["isnotifed"]=false;
$message = $this->load->view("mail/mission",$this->data,true);
$this->email->from($this->sendmail, 'Notification:'."Mission");
$this->email->to($user["email"]);
$this->email->subject("Notification de demande de mission est rejetee");
$this->email->message($message);
echo $this->email->send();
$this->Mission_model->update_etat($id,2);
redirect('mission/index');
}else{
redirect('Login/index/');
}
}
function ChartEtat($datedebut=null,$datefin=null)
{
if($datedebut==null && $datefin==null){
echo json_encode($this->Mission_model->ChartEtat());
}else{
echo json_encode($this->Mission_model->ChartEtat($datedebut,$datefin));
}
}
}