Subversion Repositories SmartDukaan

Rev

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

<?php
App::uses('AppController', 'Controller');
/**
 * StoreProducts Controller
 *
 * @property StoreProduct $StoreProduct
 * @property PaginatorComponent $Paginator
 */
class StoreProductsController extends AppController {

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

        public function beforeFilter() {
                parent::beforeFilter();
                $this->Auth->allow('bycategory');
                $callback = $this->request->query('callback');
        }
/**
 * index method
 *
 * @return void
 */
        public function index() {
                $user_id = $this->request->query('user_id');
                $this->response->type('json');
                $this->layout = 'ajax';
                // $this->Product->recursive = -1;
                $this->StoreProduct->recursive = 1;
                $this->Paginator->settings = array('limit'=>300);
                $result = array('products' => $this->Paginator->paginate());
                $callback = $this->request->query('callback');
                $this->set(array(
                    'result' => $result,
                    'callback' => $callback,
                    '_serialize' => array('result')
                ));
                $this->render('/Elements/jsonp');
        }

        public function bycategory() {
                $userId = $this->request->query('user_id');
                // $userId = 1;//delete me 
                $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 = null;
                if(!empty($userCategories)){
                        foreach ($userCategories as $key => $value) {
                                $categoryIds[] = $value['UserCategory']['category_id'];
                        }
                        $conditions = array('Product.category_id'=>$categoryIds);
                }
                $this->StoreProduct->recursive = -1;
                $this->StoreProduct->Behaviors->attach('Containable');
                $this->Paginator->settings = array('contain'=>array('Product'),'limit'=>50,'fields'=>array('Product.category_id','StoreProduct.id','StoreProduct.title','StoreProduct.thumbnail','StoreProduct.price','StoreProduct.cashback','StoreProduct.available_price'),'conditions'=>$conditions);
                $result = array('products' => $this->Paginator->paginate(),'categories'=>$categoryIds);
                $callback = $this->request->query('callback');
                $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->StoreProduct->exists($id)) {
                        throw new NotFoundException(__('Invalid store product'));
                }
                $options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
                $this->set('storeProduct', $this->StoreProduct->find('first', $options));
        }

/**
 * add method
 *
 * @return void
 */
        public function add() {
                if ($this->request->is('post')) {
                        $this->StoreProduct->create();
                        if ($this->StoreProduct->save($this->request->data)) {
                                $this->Session->setFlash(__('The store product has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
                        }
                }
                $stores = $this->StoreProduct->Store->find('list');
                $products = $this->StoreProduct->Product->find('list');
                $this->set(compact('stores', 'products'));
        }

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

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

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

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

/**
 * admin_add method
 *
 * @return void
 */
        public function admin_add() {
                if ($this->request->is('post')) {
                        $this->StoreProduct->create();
                        if ($this->StoreProduct->save($this->request->data)) {
                                $this->Session->setFlash(__('The store product has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
                        }
                }
                $stores = $this->StoreProduct->Store->find('list');
                $products = $this->StoreProduct->Product->find('list');
                $this->set(compact('stores', 'products'));
        }

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

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