Subversion Repositories SmartDukaan

Rev

Rev 17113 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
App::uses('AppController', 'Controller');
/**
 * UserUrls Controller
 *
 * @property UserUrl $UserUrl
 * @property PaginatorComponent $Paginator
 */
class UserUrlsController extends AppController {

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

        public function beforeFilter() {
                parent::beforeFilter();
                $this->Auth->allow('add');
        }
/**
 * index method
 *
 * @return void
 */
        public function index() {
                throw new NotFoundException(__('Access Denied'));
                $this->UserUrl->recursive = 0;
                $this->set('userUrls', $this->Paginator->paginate());
        }

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

/**
 * add method
 *
 * @return void
 */
        public function add() {
                if ($this->request->is('post')) {
                        if(isset($this->request->data['pushdata']) && !empty($this->request->data['pushdata'])) {
                                $pushdata = json_decode($this->request->data['pushdata'],1);    
                                $pushdata['pushdata'][0]['ip'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
                                /*
                                $this->UserUrl->create();
                                if ($this->UserUrl->saveAll($pushdata['pushdata'])) {
                                        $result = array('success' => true,'message'=>__('The url has been saved.'));
                                } else {
                                        $result = array('success' => false,'message'=>__('The url could not be saved. Please, try again.'));
                                }
                                */
                                if(isset($pushdata['pushdata'][0]['httpstatus']) && !empty($pushdata['pushdata'][0]['httpstatus'])) {
                                        $this->loadModel('SaholicLog');
                                        $this->SaholicLog->create();
                                        $this->SaholicLog->saveAll($pushdata['pushdata']);
                                }                               
                                //Nodejs api call
                                $url = Configure::read('nodeurl') . '/addBrowsingHistory/?pushed_by=php';
                                $this->make_request($url,json_encode($pushdata));
                                $result = array('success' => true,'message'=>__('The url has been saved.'));
                                $this->response->type('json');
                                $this->layout = 'ajax';
                                $this->set(array(
                                    'result' => $result,
                                    // 'callback' => $callback,
                                    '_serialize' => array('result')
                                ));
                                $this->render('/Elements/json');
                        }                       
                }               
        }

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

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

/**
 * admin_index method
 *
 * @return void
 */
        public function admin_index() {         
                $q = $this->request->query('q');
                if(isset($q) && !empty($q)){
                        // $this->Paginator->settings = array('conditions' => array('UserUrl.url LIKE'=>'%'.$q.'%'),'order' => array('id'=>'desc'));
                        $url = Configure::read('nodeurl') . "/searchBrowsingHistory/?search_term=$q&posted_by=php";
                        $this->set(compact('q'));
                } else {
                        // $this->Paginator->settings = array('order' => array('id'=>'desc'));
                        $url = Configure::read('nodeurl') . '/getBrowsingHistory/?posted_by=php';
                }
                // $this->set('userUrls', $this->Paginator->paginate());
                $page = $this->request->query('page');
                if(!isset($page)){
                        $page = 1;
                }
                $url = $this->UserUrl->getNodeUrl($url,$page);
                $userUrls = $this->make_request($url,null);
                $this->set(compact('userUrls', 'page'));
        }

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

        public function admin_by($userId = null) {
                /*$options = array('conditions' => array('UserUrl.user_id' => $userId),'order'=>array('id'=>'desc'),'limit' => 100);
                $this->Paginator->settings = $options;
                $userUrls = $this->Paginator->paginate();               
                $this->set(compact('userUrls'));*/
                $page = $this->request->query('page');
                if(!isset($page)){
                        $page = 1;
                }       
                $url = Configure::read('nodeurl') . '/getBrowsingHistoryByUserId/?posted_by=php&user_id='.$userId;
                $url = $this->UserUrl->getNodeUrl($url,$page);
                $userUrls = $this->make_request($url,null);
                $userInfoApiUrl = Configure::read('nodeurl') . '/getUserById/?user_id='.$userId;
                $user = $this->make_request($userInfoApiUrl,null);
                $this->set(compact('userUrls', 'page','user','userId'));
        }
/**
 * admin_add method
 *
 * @return void
 */
        public function admin_add() {
                if ($this->request->is('post')) {
                        $this->UserUrl->create();
                        if ($this->UserUrl->save($this->request->data)) {
                                $this->Session->setFlash(__('The user url has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The user url could not be saved. Please, try again.'));
                        }
                }
                $users = $this->UserUrl->User->find('list');
                $this->set(compact('users'));
        }

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

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