Friday, 10 January 2014

Cakephp

CitiesController
----------------------------
<?php
App::uses('AppController', 'Controller');
/**
 * Cities Controller
 *
 * @property City $City
 */
class CitiesController extends AppController {


/**
 * index method
 *
 * @return void
 */
    public function index() {
        $this->City->recursive = 0;
        $this->set('cities', $this->paginate());
                $array = $this->actionPermission($this->paginate(),$this->modelClass);
                $this->set('array',$array);
    }

/**
 * view method
 *
 * @param string $id
 * @return void
 */
    public function view($id = null) {
        $this->City->id = $id;
        if (!$this->City->exists()) {
            throw new NotFoundException(__('Invalid city'));
        }
        $this->set('city', $this->City->read(null, $id));
    }

/**
 * add method
 *
 * @return void
 */
    public function add() {
        if ($this->request->is('post')) {
            $this->City->create();
            if ($this->City->save($this->request->data)) {
                                $this->Session->write('redirect_controller','cities');
                                $this->Session->write('redirect_method','index');
                $this->Session->setFlash(__('The city has been saved'));
                $this->redirect(array('controller'=>'users','action' => 'welcome'));
            } else {
                $this->Session->setFlash(__('The city could not be saved. Please, try again.'));
            }
        }
        $countries = $this->City->Country->find('list');
        $states = $this->City->State->find('list');
        $this->set(compact('countries', 'states'));
    }

/**
 * edit method
 *
 * @param string $id
 * @return void
 */
    public function edit($id = null) {
        $this->City->id = $id;
        if (!$this->City->exists()) {
            throw new NotFoundException(__('Invalid city'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->City->save($this->request->data)) {
                $this->Session->setFlash(__('The city has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The city could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->City->read(null, $id);
        }
        $countries = $this->City->Country->find('list');
        $states = $this->City->State->find('list');
        $this->set(compact('countries', 'states'));
    }

/**
 * delete method
 *
 * @param string $id
 * @return void
 */
    public function delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->City->id = $id;
        if (!$this->City->exists()) {
            throw new NotFoundException(__('Invalid city'));
        }
        if ($this->City->delete()) {
            $this->Session->setFlash(__('City deleted'));
            $this->Session->write('redirect_controller','cities');
                        $this->Session->write('redirect_method','index');
                        $this->redirect(array('controller'=>'users','action' => 'welcome'));

        }
        $this->Session->setFlash(__('City was not deleted'));
        $this->redirect(array('action' => 'index'));
    }
       
        public function get_city_list(){
            if(!$this->request->data['form_name']){
            $this->set("form_name","");
            }else{
                $this->set("form_name",$this->request->data['form_name']);
            }
             if($this->request->data["state_id"]){
                $city  =   $this->City->find('list',array('conditions'=>array('City.state_id'=>$this->request->data["state_id"])));
                $this->set('cities',$city);
                $this->layout = 'ajax';
            }
        }
        public function admin_get_city_list(){
            if(!$this->request->data['form_name']){
            $this->set("form_name","");
            }else{
                $this->set("form_name",$this->request->data['form_name']);
            }
             if($this->request->data["state_id"]){
                $city  =   $this->City->find('list',array('conditions'=>array('City.state_id'=>$this->request->data["state_id"])));
                $this->set('cities',$city);
                $this->layout = 'ajax';
            }
        }
/**
 * admin_index method
 *
 * @return void
 */
    public function admin_index() {
        $this->City->recursive = 0;
        $this->set('cities', $this->paginate());
    }

/**
 * admin_view method
 *
 * @param string $id
 * @return void
 */
    public function admin_view($id = null) {
        $this->City->id = $id;
        if (!$this->City->exists()) {
            throw new NotFoundException(__('Invalid city'));
        }
        $this->set('city', $this->City->read(null, $id));
    }

/**
 * admin_add method
 *
 * @return void
 */
    public function admin_add() {
        if ($this->request->is('post')) {
            $this->City->create();
            if ($this->City->save($this->request->data)) {
                $this->Session->setFlash(__('The city has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The city could not be saved. Please, try again.'));
            }
        }
        $countries = $this->City->Country->find('list');
        $states = $this->City->State->find('list');
        $this->set(compact('countries', 'states'));
    }

/**
 * admin_edit method
 *
 * @param string $id
 * @return void
 */
    public function admin_edit($id = null) {
        $this->City->id = $id;
        if (!$this->City->exists()) {
            throw new NotFoundException(__('Invalid city'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->City->save($this->request->data)) {
                $this->Session->setFlash(__('The city has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The city could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->City->read(null, $id);
        }
        $countries = $this->City->Country->find('list');
        $states = $this->City->State->find('list');
        $this->set(compact('countries', 'states'));
    }

/**
 * admin_delete method
 *
 * @param string $id
 * @return void
 */
    public function admin_delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->City->id = $id;
        if (!$this->City->exists()) {
            throw new NotFoundException(__('Invalid city'));
        }
        if ($this->City->delete()) {
            $this->Session->setFlash(__('City deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('City was not deleted'));
        $this->redirect(array('action' => 'index'));
    }
        Public function get_cities_list_form($value=null){
             if($value){
                $cities  =   $this->City->find('list',array('conditions'=>array('City.state_id'=>$value)));
                return $cities;
            }
        }
        function get_city(){
              $cities = $this->City->find('list',array('order' => array('City.name' => 'asc')));
              return $cities;
          }
}

No comments:

Post a Comment