Subversion Repositories SmartDukaan

Rev

Rev 13769 | Rev 14139 | 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');
                //Configure::load('dev');
                $this->apihost = Configure::read('saholicapihost');
        }
/**
 * 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
                                if(!empty($this->request->data['email'])) {
                                        $conditions = array('email'=>$this->request->data['email']);
                                        $user = $this->SocialProfile->User->find('first',array('conditions'=>$conditions));

                                        if(!empty($user)) {
                                                $userData = array('id'=>$user['User']['id'],'email'=>$this->request->data['email'],'username'=>$this->request->data['email'],'first_name'=>$this->request->data['name'],'gender'=>$this->request->data['gender'],'referrer'=>$this->request->data['referrer'],'profile_pic'=> $this->request->data['profile_pic']);
                                                $this->SocialProfile->User->save($userData);
                                                $data['user_id'] = $user['User']['id']; 
                                        }else{
                                                //Create a new user and then insert user_id in social_profiles table
                                                $userData = array('profile_pic'=> $this->request->data['profile_pic'], 'email'=>$this->request->data['email'],'username'=>$this->request->data['email'],'first_name'=>$this->request->data['name'],'gender'=>$this->request->data['gender'],'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' => false, 'message' => "Email is missing");
                                        break;
                                }                               
                        } else {
                                $userData = array('id'=>$socialProfile['SocialProfile']['user_id'],'email'=>$this->request->data['email'],'username'=>$this->request->data['email'],'first_name'=>$this->request->data['name'],'gender'=>$this->request->data['gender'],'referrer'=>$this->request->data['referrer'],'profile_pic'=> $this->request->data['profile_pic']);
                                $this->SocialProfile->User->save($userData);
                                $data['user_id'] = $socialProfile['SocialProfile']['user_id'];
                                $result = array('success' => true, 'message' => 'Existing Social Profile','id' => $socialProfile['SocialProfile']['user_id']);
                        }
                }
                $this->updateSaholicUser($data['user_id'],$this->request->data['email']);
                $this->set(array(
                    'result' => $result,
                    'callback' => $callback,
                    '_serialize' => array('result')
                ));
                //$this->render('/Elements/jsonp');
                $this->render('/Elements/json');
        }

        private function updateSaholicUser($userId,$email=null) {
                if(!$email){
                        //Handle it properly
                        return;
                }
                $this->log('userId '.$userId,'registration');
                $this->log('email '.$email ,'registration');
                $this->loadModel('UserAccount');
                $options = array('conditions'=>array('user_id' => $userId,'account_type' => 'saholic'),'recursive'=>-1);
                $exists = $this->UserAccount->find('count',$options);
                if(!$exists){
                        $url = $this->apihost."register?email=$email";
                        $response = $this->make_request($url,null);
                        $this->log('response '.print_r($response,1),'registration');
                        if(!empty($response)){
                                $data = array('account_type'=>'saholic','user_id'=>$userId,'account_key'=>$response['userId']);
                                $this->UserAccount->create();
                                $this->UserAccount->save($data);
                        }
                }
        }
/**
 * 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'));
        }}