Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

<?php
App::uses('AppController', 'Controller');
/**
 * Products Controller
 *
 * @property Product $Product
 * @property PaginatorComponent $Paginator
 */
class ProductsController extends AppController {

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

        public function beforeFilter() {
                parent::beforeFilter();
                $this->Auth->allow('viaweb');
                $callback = $this->request->query('callback');
        }

/**
 * index method
 *
 * @return void
 */
        public function index() {
                $this->response->type('json');
                $this->layout = 'ajax';
                // $this->Product->recursive = -1;
                $this->Product->recursive = 1;
                $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');
        }

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

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

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

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

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

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

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

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

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function admin_delete($id = null) {
                $this->Product->id = $id;
                if (!$this->Product->exists()) {
                        throw new NotFoundException(__('Invalid product'));
                }
                $this->request->onlyAllow('post', 'delete');
                if ($this->Product->delete()) {
                        $this->Session->setFlash(__('The product has been deleted.'));
                } else {
                        $this->Session->setFlash(__('The product could not be deleted. Please, try again.'));
                }
                return $this->redirect(array('action' => 'index'));
        }
        
        public function viaweb() {
                // debug($this->params->url);die;
                if(!$this->isAuthorized()){
                        $hosturl = 'http://' . $_SERVER['SERVER_NAME'] . Router::url('/');
                        // $this->redirect($hosturl."users/login/?next=".$hosturl.$this->params->url);
                        $next = $hosturl.$this->params->url;
                        $this->Session->write('next', $next);
                        $this->redirect($hosturl."users/login");
                }
                $this->loadModel('User');
                $this->User->unbindModel(
                        array('belongsTo' => array('Group'))
                );
                $user = $this->User->read(null,$this->Auth->user('id'));
                $img = base64_decode($this->params['named']['img']);
                $url = base64_decode($this->params['named']['url']);
                $des = base64_decode($this->params['named']['des']);
                $name= base64_decode($this->params['named']['name']);
                $store = base64_decode($this->params['named']['store']);
                $price = $this->params['named']['price'];
                //$this->layout = 'homepage';
                $conditions = array('parent_id !='>0);
                $categories = $this->Product->Category->find('list',array('conditions'=>$conditions));
                $this->set(compact('img','url','des','categories','user','name','store','price'));
        }

        public function addviabookmarklet() {
                if ($this->request->is('post')) {
                        $this->Product->create();
                        if ($this->Product->save($this->request->data)) {
                                $this->loadModel('Store');
                                $conditions = array('name'=>$this->request->data['store']);
                                $store = $this->Store->find('first',array('conditions'=>$conditions));
                                $data = $this->request->data;
                                $data['store_id'] = $store['Store']['id'];
                                $data['product_id'] = $this->Product->getLastInsertId();
                                $data['price'] = str_replace(',','',$data['price']);
                                unset($data['store']);
                                unset($data['name']);
                                unset($data['category_id']);
                                unset($data['name']);
                                if($this->Product->StoreProduct->save($data)) {
                                        $this->Session->setFlash(__('The product has been saved.'));
                                }else{
                                        print_r($this->Product->StoreProduct->validationErrors);
                                        $this->Product->id = $this->Product->getLastInsertId();
                                        $this->Product->delete();
                                }       
                        } else {
                                $this->Session->setFlash(__('The product could not be saved. Please, try again.'));
                        }
                }
        }
}