<?php
// src/Controller/ArticlesController.php

namespace App\Controller;

use App\Controller\AppController;
use Cake\Event\Event;
use Cake\ORM\TableRegistry;
class SettingsController extends AppController
{
    public $helpers = array('Html', 'Form');
    public $components = array('RequestHandler', 'Paginator', 'Flash');


    public function beforeFilter(Event $event)
    {
        parent::beforeFilter($event);
         //$this->Security->config('unlockedActions', ['sectionAjax', 'section_ajax']);
         //$this->Security->config('allowedActions', ['sectionAjax', 'section_ajax']);

        if(isset($this->Security) && ($this->request->is('ajax') || $this->request['action'] == 'index')) {
			$this->Security->config('unlockedActions', [$this->request['action']]);
		}
         
    }


    public function division()
    {
        $this->loadModel('Division');
        $divisions = $this->Division->find('all');
        $this->set(compact('divisions'));
        if ($this->request->is('post')) {
            $division_n = $this->request->getData('division');
                $this->set('division_n',$division_n);
                $this->loadModel('Division');
                $division = TableRegistry::get("Division");
                $query = $division->query();
                            $result =$query->insert(['division','address'])
                            ->values([
                                'division' => $division_n ,
                                'address' => $this->request->getData('address') ,
                            ])
                            ->execute();
                $this->Flash->success(__('Division saved successfully'));
                return $this->redirect(['action' => 'division']);
        }
    }
    public function divedit($id)
    {
        $this->loadModel('Division');
        
         $division = $this->Division->findById($id)->firstOrFail();
    if ($this->request->is(['post', 'put'])) {
            $this->loadModel('Division');
            $division_n = $this->request->getData('division');
            $this->set('division_n',$division_n);
        
                $div = TableRegistry::get("Division");
                $query = $div->query();
                $result = $query->update()
                        ->set(['division' => $division_n,'address' => $this->request->getData('address')])
                        ->where(['id' => $this->request->getData('id')])
                        ->execute();
                $this->Flash->success(__('Division has been updated.'));
                return $this->redirect(['action' => 'division']);
        }
        $this->set('division', $division);
    }
    

    public function divdelete($id)
    {
        $this->loadModel('Division');
        $article = $this->Division->findById($id)->firstOrFail();
        if ($this->Division->delete($article)) {
            $this->Flash->success(__('The {0}  has been deleted.', $article->division));
            return $this->redirect(['action' => 'division']);
        }
    }
    
     public function caste()
    {
        $this->loadModel('Caste');
        $castes = $this->Caste->find('all');
        $this->set(compact('castes'));
        if ($this->request->is('post')) {
            $caste = $this->request->getData('caste');
                $this->set('caste',$caste);
                $this->loadModel('Caste');
                $cast = TableRegistry::get("Caste");
                $query = $cast->query();
                            $result =$query->insert(['caste'])
                            ->values([
                                'caste' => $caste ,
                            ])
                            ->execute();
                $this->Flash->success(__('Caste saved successfully'));
                return $this->redirect(['action' => 'caste']);
        }
    }
    public function castedit($id)
    {
        $this->loadModel('Caste');
        
         $caste = $this->Caste->findById($id)->firstOrFail();
    if ($this->request->is(['post', 'put'])) {
            $this->loadModel('Caste');
            $caste = $this->request->getData('caste');
            $this->set('caste',$caste);
        
                $div = TableRegistry::get("Caste");
                $query = $div->query();
                $result = $query->update()
                        ->set(['caste' => $caste])
                        ->where(['id' => $this->request->getData('id')])
                        ->execute();
                $this->Flash->success(__('Caste has been updated.'));
                return $this->redirect(['action' => 'caste']);
        }
        $this->set('caste', $caste);
    }
    

    public function castdelete($id)
    {
        $this->loadModel('Caste');
        $article = $this->Caste->findById($id)->firstOrFail();
        if ($this->Caste->delete($article)) {
            $this->Flash->success(__('The {0}  has been deleted.', $article->caste));
            return $this->redirect(['action' => 'caste']);
        }
    }

  public function relationship()
    {
        $this->loadModel('Relationship');
        $relationships = $this->Relationship->find('all');
        $this->set(compact('relationships'));
        if ($this->request->is('post')) {
            $relationship = $this->request->getData('relationship');
                $this->set('relationship',$relationship);
                $this->loadModel('Relationship');
                $cast = TableRegistry::get("Relationship");
                $query = $cast->query();
                            $result =$query->insert(['relationship'])
                            ->values([
                                'relationship' => $relationship ,
                            ])
                            ->execute();
                $this->Flash->success(__('Relationship saved successfully'));
                return $this->redirect(['action' => 'relationship']);
        }
    }
    public function relationedit($id)
    {
        $this->loadModel('Relationship');
        
         $relationship = $this->Relationship->findById($id)->firstOrFail();
    if ($this->request->is(['post', 'put'])) {
            $this->loadModel('Relationship');
            $relationship = $this->request->getData('relationship');
            $this->set('caste',$relationship);
        
                $div = TableRegistry::get("Relationship");
                $query = $div->query();
                $result = $query->update()
                        ->set(['relationship' => $relationship])
                        ->where(['id' => $this->request->getData('id')])
                        ->execute();
                $this->Flash->success(__('Relationship has been updated.'));
                return $this->redirect(['action' => 'relationship']);
        }
        $this->set('relationship', $relationship);
    }
    

    public function relationdelete($id)
    {
        $this->loadModel('Relationship');
        $article = $this->Relationship->findById($id)->firstOrFail();
        if ($this->Relationship->delete($article)) {
            $this->Flash->success(__('The {0}  has been deleted.', $article->relationship));
            return $this->redirect(['action' => 'relationship']);
        }
    }


    public function section()
    {
        $this->loadModel('Section');
        $sections = $this->Section->find('all');
        $this->set(compact('sections'));
        $this->loadModel('Division');
        $division = $this->Division->find('list', array( 'keyField' => 'division', 'valueField' => 'division' ));
        $this->set('division',$division);
        if ($this->request->is(['post', 'put'])) {
            $this->loadModel('Division');
            $division = $this->Division->find('all', ['conditions' => ['Division.division' => $this->request->getData('division') ]  ]);
            $row = $division->first();
            $division_id = $row['id'];
            $this->loadModel('Section');
            $section = TableRegistry::get("Section");
            $query = $section->query();
                        $result =$query->insert(['section'])
                        ->values([
                            'section' => $this->request->getData('section') ,
                        ])
                        ->execute();

                        $this->loadModel('Section');
                        $sec = $this->Section->find('all', ['conditions' => ['Section.section' => $this->request->getData('section') ]  ]);
                        $row = $sec->first();
                        $section_id = $row['id'];

                $this->loadModel('MapDivisionSection');
                $div = TableRegistry::get("MapDivisionSection");
                $query = $div->query();
                $result =$query->insert(['division_id','section_id'])
                ->values([
                    'division_id' => $division_id ,
                    'section_id' => $section_id ,
                ])
                ->execute();
                $this->Flash->success(__('Section saved successfully'));
                return $this->redirect(['action' => 'section']);
        }
       
    }
    
    public function secedit($id)
    {
        $this->loadModel('Section');
         $section = $this->Section->findById($id)->firstOrFail();
    if ($this->request->is(['post', 'put'])) {
            $this->loadModel('Section');
            $section = $this->request->getData('section');
            $this->set('section',$section);
        
                $sec = TableRegistry::get("Section");
                $query = $sec->query();
                $result = $query->update()
                        ->set(['section' => $section])
                        ->where(['id' => $this->request->getData('id')])
                        ->execute();
                $this->Flash->success(__('Section has been updated.'));
                return $this->redirect(['action' => 'section']);
        }
        $this->set('section', $section);
    }

    public function secdelete($id)
    {
        $this->loadModel('MapDivisionSection');
        $s_id = $this->MapDivisionSection->findBySection_id($id)->firstOrFail();
        $this->MapDivisionSection->delete($s_id);
        $this->loadModel('Section');
        $article = $this->Section->findById($id)->firstOrFail();
        if ($this->Section->delete($article)) {
            $this->Flash->success(__('The {0}  has been deleted.', $article->section));
            return $this->redirect(['action' => 'section']);
        }
    }
    public function designation()
    {
        $this->loadModel('Designation');
        $designations = $this->Designation->find('all');
        $this->set(compact('designations'));
        if ($this->request->is('post')) {
            $desig = $this->request->getData('designation');
                $this->set('desig',$desig);
                $this->loadModel('Designation');
                $designation = TableRegistry::get("Designation");
                $query = $designation->query();
                            $result =$query->insert(['designation'])
                            ->values([
                                'designation' => $desig ,
                            ])
                            ->execute();
                $this->Flash->success(__('Designation saved successfully'));
                return $this->redirect(['action' => 'designation']);
        }
    }
    public function desigedit($id)
    {
        $this->loadModel('Designation');
        
         $designation = $this->Designation->findById($id)->firstOrFail();
    if ($this->request->is(['post', 'put'])) {
            $this->loadModel('Designation');
            $desig = $this->request->getData('designation');
            $this->set('desig',$desig);
        
                $designation = TableRegistry::get("Designation");
                $query = $designation->query();
                $result = $query->update()
                        ->set(['designation' => $desig])
                        ->where(['id' => $this->request->getData('id')])
                        ->execute();
                $this->Flash->success(__('Designation has been updated.'));
                return $this->redirect(['action' => 'designation']);
        }
        $this->set('designation', $designation);
    }

    public function desigdelete($id)
    {
        $this->loadModel('Designation');
        $article = $this->Designation->findById($id)->firstOrFail();
        if ($this->Designation->delete($article)) {
            $this->Flash->success(__('The {0}  has been deleted.', $article->designation));
            return $this->redirect(['action' => 'designation']);
        }
    }

   
}
?>