Subversion Repositories SmartDukaan

Rev

Rev 13532 | Rev 13567 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
App::uses('AppController', 'Controller');
/**
 * Categories Controller
 *
 * @property Category $Category
 * @property PaginatorComponent $Paginator
 */
class CategoriesController extends AppController {

/**
 * Components
 *
 * @var array
 */
        public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
        public function index() {
                // $this->Category->recursive = 0;
                // $this->set('categories', $this->Paginator->paginate());
                $userId = $this->request->query('user_id');
                $this->loadModel('UserCategory');
                $options = array('conditions' => array('user_id'=>$userId),'recursive'=>-1);
                $userCategories = $this->UserCategory->find('all',$options);            
                $this->response->type('json');
                $this->layout = 'ajax';
                $conditions = array(array('Category.parent_id !='=>0));
                if(!empty($userCategories)){
                        foreach ($userCategories as $key => $value) {
                                $categoryIds[] = $value['UserCategory']['category_id'];
                        }
                        array_push($conditions,array('Category.id'=>$categoryIds));
                }
                $this->Category->recursive = -1;                
                $categories = $this->Paginator->paginate(null,$conditions);
                $callback = $this->request->query('callback');
                $result = array('categories' => $categories);
                $this->set(array(
                    'result' => $result,
                    'callback' => $callback,
                    '_serialize' => array('result')
                ));
                $this->render('/Elements/jsonp');
        }

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function view($id = null) {
                if (!$this->Category->exists($id)) {
                        throw new NotFoundException(__('Invalid category'));
                }
                $options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
                $this->set('category', $this->Category->find('first', $options));
        }

/**
 * add method
 *
 * @return void
 */
        public function add() {
                if ($this->request->is('post')) {
                        $this->Category->create();
                        if ($this->Category->save($this->request->data)) {
                                $this->Session->setFlash(__('The category has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The category could not be saved. Please, try again.'));
                        }
                }
        }

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function edit($id = null) {
                if (!$this->Category->exists($id)) {
                        throw new NotFoundException(__('Invalid category'));
                }
                if ($this->request->is(array('post', 'put'))) {
                        if ($this->Category->save($this->request->data)) {
                                $this->Session->setFlash(__('The category has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The category could not be saved. Please, try again.'));
                        }
                } else {
                        $options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
                        $this->request->data = $this->Category->find('first', $options);
                }
        }

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function delete($id = null) {
                $this->Category->id = $id;
                if (!$this->Category->exists()) {
                        throw new NotFoundException(__('Invalid category'));
                }
                $this->request->onlyAllow('post', 'delete');
                if ($this->Category->delete()) {
                        $this->Session->setFlash(__('The category has been deleted.'));
                } else {
                        $this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
                }
                return $this->redirect(array('action' => 'index'));
        }

/**
 * admin_index method
 *
 * @return void
 */
        public function admin_index() {
                $this->Category->recursive = 0;
                $this->set('categories', $this->Paginator->paginate());
        }

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function admin_view($id = null) {
                if (!$this->Category->exists($id)) {
                        throw new NotFoundException(__('Invalid category'));
                }
                $options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
                $this->set('category', $this->Category->find('first', $options));
        }

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

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function admin_edit($id = null) {
                if (!$this->Category->exists($id)) {
                        throw new NotFoundException(__('Invalid category'));
                }
                if ($this->request->is(array('post', 'put'))) {
                        if ($this->Category->save($this->request->data)) {
                                $this->Session->setFlash(__('The category has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The category could not be saved. Please, try again.'));
                        }
                } else {
                        $options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
                        $this->request->data = $this->Category->find('first', $options);
                }
        }

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function admin_delete($id = null) {
                $this->Category->id = $id;
                if (!$this->Category->exists()) {
                        throw new NotFoundException(__('Invalid category'));
                }
                $this->request->onlyAllow('post', 'delete');
                if ($this->Category->delete()) {
                        $this->Session->setFlash(__('The category has been deleted.'));
                } else {
                        $this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
                }
                return $this->redirect(array('action' => 'index'));
        }}