FichemetrologiquesController.php 11.1 KB
<?php
namespace App\Controller;

use App\Controller\AppController;
use Cake\ORM\TableRegistry;

// use App\Plugins\GoogleCharts\GoogleCharts\vendor\GoogleCharts;
/**
 * Fichemetrologiques Controller
 *
 * @property \App\Model\Table\FichemetrologiquesTable $Fichemetrologiques
 */
class FichemetrologiquesController extends AppController
{

    // public $helpers = array('GoogleCharts.GoogleCharts');
    
    /**
     * Give authorization for fichemetrologiques
     *
     * @param
     *            $user
     * @return boolean
     */
    public function isAuthorized($user)
    {
        /*
         * $configuration = $this->confLabinvent;
         * $role = TableRegistry::get('Users')->find()->where(['username' => $user[$configuration->authentificationType_ldap][0]])->first()['role'];
         *
         * $action = $this->request->getAttribute('params')['action'];
         *
         * if($this->userHasRoleAtLeast('Administration')) return true;
         * if($this->userHasRoleAtLeast('Responsable') && $action == 'fichemetro') return true;
         * //Pour un "utilisateur"
         * if (in_array($action, ['edit', 'delete'])) {
         * $id = (int)$this->request->getAttribute('params')['pass'][0];
         * if($this->isOwnedBy($id, $user['sn'][0].' '.$user['givenname'][0])) return true;
         * if($role == 'Responsable' && $this->isRespGroup($id, $user[$configuration->authentificationType_ldap][0])) return true;
         * }
         *
         * //return parent::isAuthorized($user);
         */
        
        // Tout profil peut tout faire !!! (A affiner, non ?)
        return true;
    }

    /**
     * Index method
     *
     * @return \Cake\Network\Response|null
     */
    public function index()
    {
        $this->paginate = [
            'contain' => [
                'Suivis'
            ]
        ];
        $fichemetrologiques = $this->paginate($this->Fichemetrologiques->find('all'));
        $nbFichemetrologique = $this->Fichemetrologiques->find('all')->count();
        
        $this->set(compact('fichemetrologiques', 'nbFichemetrologique'));
        $this->set('_serialize', [
            'fichemetrologique'
        ]);
    }

    /**
     * View method
     *
     * @param string|null $id
     *            Fichemetrologique id.
     * @return \Cake\Network\Response|null
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null)
    {
        $fichemetrologique = $this->Fichemetrologiques->get($id, [
            'contain' => [
                'Suivis'
            ]
        ]);
        $unite = TableRegistry::get('Unites')->find()
            ->select('nom')
            ->where([
            'id =' => $fichemetrologique->suivi->unite_id
        ])
            ->first()['nom'];
        $this->set('unite', $unite);
        $this->set('fichemetrologique', $fichemetrologique);
        $this->set('_serialize', [
            'fichemetrologique'
        ]);
    }

    /**
     * Add method
     *
     * @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $fichemetrologique = $this->Fichemetrologiques->newEntity();
        
        $numunite = TableRegistry::get('Suivis')->find()
            ->select('unite_id')
            ->where([
            'id =' => $this->request->getAttribute('params')['pass'][0]
        ])
            ->first()['unite_id'];
        $unite = TableRegistry::get('Unites')->find()
            ->select('nom')
            ->where([
            'id =' => $numunite
        ])
            ->first()['nom'];
        $symbole = TableRegistry::get('Unites')->find()
            ->select('symbole')
            ->where([
            'id =' => $numunite
        ])
            ->first()['symbole'];
        $resol = TableRegistry::get('Suivis')->find()
            ->select('resolution')
            ->where([
            'id =' => $this->request->getAttribute('params')['pass'][0]
        ])
            ->first()['resolution'];
        $suivi = TableRegistry::get('Suivis')->find()
            ->where([
            'id =' => $this->request->getAttribute('params')['pass'][0]
        ])
            ->first();
        
        if ($this->request->is('post')) {
            $fichemetrologique = $this->Fichemetrologiques->patchEntity($fichemetrologique, $this->request->getData, [
                'associated' => 'Mesures'
            ]);
            
            if ($fichemetrologique->resolution == 0)
                $fichemetrologique->resolution = 'numerique';
            else
                $fichemetrologique->resolution = 'cadran';
            
            if ($fichemetrologique->etatmateriel == 0)
                $fichemetrologique->etatmateriel = 'Neuf';
            else if ($fichemetrologique->etatmateriel == 1)
                $fichemetrologique->etatmateriel = 'RAS';
            else
                $fichemetrologique->etatmateriel = 'dégradé';
            $suivis = TableRegistry::get('Suivis')->get($this->request->getAttribute('params')['pass'][0], [
                'contain' => []
            ]);
            if ($suivi->date_prochain_controle !== null) {
                if ($suivis->date_prochain_controle < $fichemetrologique->datefiche) {
                    $fichemetrologique->retard = 'RAS';
                } else {
                    $fichemetrologique->retard = 'OUI';
                }
            } else {
                $fichemetrologique->retard = 'RAS';
            }
            if ($this->Fichemetrologiques->save($fichemetrologique)) {
                
                $suivis->conforme = $fichemetrologique->conformeEMTstandart;
                $suivis->date_controle = $fichemetrologique->datefiche;
                $datecontrole = $fichemetrologique->datefiche;
                $valfrequence = $suivis->frequence;
                if ($suivis->type_frequence == '/ Jours')
                    $type_freque = ' day';
                else if ($suivis->type_frequence == '/ Mois')
                    $type_freque = ' month';
                else if ($suivis->type_frequence == '/ Ans')
                    $type_freque = ' year';
                else
                    $type_freque = ' week';
                
                $suivis->date_prochain_controle = date('Y-m-d', strtotime('+' . $valfrequence . ' ' . $type_freque, strtotime(date('Y-m-d'))));
                TableRegistry::get('Suivis')->save($suivis);
                $this->Flash->success(__('La fiche metrologique a bien été ajouté.'));
                return $this->redirect([
                    'controller' => 'Suivis',
                    'action' => 'view',
                    $fichemetrologique->suivi_id
                ]);
            } else {
                $this->Flash->error(__('La fiche metrologique n\'a pas pu être ajouté.'));
            }
        }
        
        $this->set(compact('suivi', 'resol', 'unite', 'symbole', 'fichemetrologique'));
        $this->set('_serialize', [
            'fichemetrologique'
        ]);
    }

    /**
     * Edit method
     *
     * @param string|null $id
     *            Fichemetrologique id.
     * @return \Cake\Network\Response|void Redirects on successful edit, renders view otherwise.
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    public function edit($id = null)
    {
        $fichemetrologique = $this->Fichemetrologiques->get($id, [
            'contain' => []
        ]);
        
        if ($this->request->is([
            'patch',
            'post',
            'put'
        ])) {
            $fichemetrologique = $this->Fichemetrologiques->patchEntity($fichemetrologique, $this->request->data);
            if ($fichemetrologique->typemesure == 0)
                $fichemetrologique->typemesure = 'direct';
            else
                $fichemetrologique->typemesure = 'indirect';
            if ($fichemetrologique->resolution == 0)
                $fichemetrologique->resolution = 'numerique';
            else
                $fichemetrologique->resolution = 'cadran';
            
            if ($this->Fichemetrologiques->save($fichemetrologique)) {
                
                $this->Flash->success(__('La fichemetrologique a bien été édité.'));
                return $this->redirect([
                    'action' => 'view',
                    $id
                ]);
            } else {
                $this->Flash->error(__('La fichemetrologique n\'a pas pu être édité.'));
            }
        }
        /*
         * $this->getView()->loadHelper('GoogleCharts.GoogleCharts');
         *
         * $chart = new GoogleCharts();
         *
         * $chart->type("LineChart");
         * //Options array holds all options for Chart API
         * $chart->options(['title' => 'Recent Scores']);
         * $chart->columns([
         * //Each column key should correspond to a field in your data array
         * 'event_date' => [
         * //Tells the chart what type of data this is
         * 'type' => 'string',
         * //The chart label for this column
         * 'label' => 'Date'
         * ],
         * 'score' => [
         * 'type' => 'number',
         * 'label' => 'Score',
         * //Optional NumberFormat pattern
         * 'format' => '#,###'
         * ]
         * ]);
         * $mesures = $this->Mesure->find('list', ['conditions' => [ 'fichemetrologique_id =' => $fichemetrologique->id]] );
         * //Loop through our data and creates data rows
         * //Data will be added to rows based on the column keys above (event_date, score).
         * //If there are missing fields in your data or the keys do not match, then this will not work.
         * foreach($mesures as $mesure) {
         * $chart->addRow($round);
         * }
         *
         * //You can also use this way to loop through data and creates data rows:
         * foreach($rounds as $round) {
         * $chart->addRow([
         * 'event_date' => $round['event_date'],
         * 'score' => $round['score']
         * ]);
         * }
         *
         * //You can also manually add rows:
         * $chart->addRow([
         * 'event_date' => '1/1/2012',
         * 'score' => 55
         * ]);
         */
        $this->set(compact('fichemetrologique', 'chart'));
        $this->set('_serialize', [
            'fichemetrologique'
        ]);
    }

    /*
     * Delete method
     *
     * @param string|null $id Fichemetrologique id.
     * @return \Cake\Network\Response|null Redirects to index.
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function delete($id = null)
    {
        $this->request->allowMethod([
            'post',
            'delete'
        ]);
        $fichemetrologique = $this->Fichemetrologiques->get($id);
        if ($this->Fichemetrologiques->delete($fichemetrologique)) {
            $this->Flash->success(__('La fiche metrologique a bien été supprimé.'));
        } else {
            $this->Flash->error(__('La fiche metrologique n\'a pas pu être supprimé.'));
        }
        return $this->redirect([
            'controller' => 'Suivis',
            'action' => 'view',
            $fichemetrologique->suivi_id
        ]);
    }
}