Subversion Repositories SmartDukaan

Rev

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

<?php
App::uses('AppController', 'Controller');
/**
 * SocialProfiles Controller
 *
 * @property SocialProfile $SocialProfile
 * @property PaginatorComponent $Paginator
 */
class SocialProfilesController extends AppController {

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

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

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

/**
 * add method
 *
 * @return void
 */
        public function add() {
                if ($this->request->is('post')) {
                        $this->log(print_r($this->request->data,1),'registration');
                        $data = $this->request->data;
                        $data['social_id'] = $this->request->data['id'];
                        $data['access_token'] = $this->request->data['token'];
                        unset($data['id']);
                        unset($data['token']);
                        unset($data['gender']);
                        $this->response->type('json');
                        $this->layout = 'ajax';
                        $conditions = array('social_id'=>$this->request->data['id'],'type'=>$this->request->data['type']);
                        $socialProfile = $this->SocialProfile->find('first',array('conditions'=>$conditions));
                        if(empty($socialProfile)){
                                //Check if user with same email is registered and if so just add his profile
                                $conditions = array('email'=>$this->request->data['email']);
                                $user = $this->SocialProfile->User->find('first',array('conditions'=>$conditions));
                                if(!empty($user)) {
                                        $data['user_id'] = $user['User']['id']; 
                                }else{
                                        //Create a new user and then insert user_id in social_profiles table
                                        $userData = array('email'=>$this->request->data['email'],'username'=>$this->request->data['email'],'first_name'=>$this->request->data['name'],'gender'=>$this->request->data['gender'],'mobile_number'=>$this->request->data['mobile_number'],'referrer'=>$this->request->data['referrer']);
                                        if($this->SocialProfile->User->save($userData)) {
                                                $data['user_id'] = $this->SocialProfile->User->getLastInsertId();
                                        }else{
                                                $result = array('success' => false, 'message' => $this->SocialProfile->User->validationErrors);
                                                break;
                                        }
                                }
                                $this->SocialProfile->create();
                                if ($this->SocialProfile->save($data)) {
                                        $result = array('success' => true, 'message' => 'Social Profile Created','id' => $data['user_id']);
                                } else {
                                        $result = array('success' => false, 'message' => 'Social Profile Could Not Be Created','id' => -1);
                                }
                        } else {
                                $result = array('success' => true, 'message' => 'Existing Social Profile','id' => $socialProfile['SocialProfile']['user_id']);
                        }
                }
                $this->set(array(
                    'result' => $result,
                    'callback' => $callback,
                    '_serialize' => array('result')
                ));
                //$this->render('/Elements/jsonp');
                $this->render('/Elements/json');
        }

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

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

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

/**
 * admin_add method
 *
 * @return void
 */
        public function admin_add() {
                if ($this->request->is('post')) {
                        $this->SocialProfile->create();
                        if ($this->SocialProfile->save($this->request->data)) {
                                $this->Session->setFlash(__('The social profile has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
                        }
                }
                $users = $this->SocialProfile->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->SocialProfile->exists($id)) {
                        throw new NotFoundException(__('Invalid social profile'));
                }
                if ($this->request->is(array('post', 'put'))) {
                        if ($this->SocialProfile->save($this->request->data)) {
                                $this->Session->setFlash(__('The social profile has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
                        }
                } else {
                        $options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
                        $this->request->data = $this->SocialProfile->find('first', $options);
                }
                $users = $this->SocialProfile->User->find('list');
                $this->set(compact('users'));
        }

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