Rev 21079 | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?phpApp::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','identifyUser');$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() {$mobileRequired = true;$referrerRequired = true;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));$docsSubmitted=false;//If Social profile doesn't existif(empty($socialProfile)) {//Check if user with same email is registered and if so just add his profileif(!empty($this->request->data['email'])) {$conditions = array('email'=>$this->request->data['email']);$user = $this->SocialProfile->User->find('first',array('conditions'=>$conditions));/*if(!empty($this->request->data['referrer']) || !empty($this->request->data['utm_campaign'])){$referrerRequired = false;}*/if(!empty($user)) {//Existing userif(!empty($user['User']['mobile_number'])){$mobileRequired = false;}if(!empty($user['User']['activated']) && $user['User']['activated']==1) {$referrerRequired = false;}//We don't need to update the 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'],'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']);//Check for utm parametersif(!empty($this->request->data['utm_source'])){$userData['utm_source'] = $this->request->data['utm_source'];}if(!empty($this->request->data['utm_medium'])){$userData['utm_medium'] = $this->request->data['utm_medium'];}if(!empty($this->request->data['utm_term'])){$userData['utm_term'] = $this->request->data['utm_term'];}if(!empty($this->request->data['utm_content'])){$userData['utm_content'] = $this->request->data['utm_content'];}if(!empty($this->request->data['utm_campaign'])){$userData['utm_campaign'] = $this->request->data['utm_campaign'];}if(!empty($this->request->data['utm_campaign']) || !empty($this->request->data['referrer'])) {if(!empty($this->request->data['utm_campaign'])) {$referrer = $this->request->data['utm_campaign'];}elseif (!empty($this->request->data['referrer'])) {$referrer = $this->request->data['referrer'];}$this->log(print_r($referrer,1),'activations');$this->loadModel('ActivationCode');//$exists = $this->ActivationCode->findByCode(strtoupper($referrer));$opt['conditions'] = array('code' => strtoupper($referrer) , 'status'=> 0 );$exists = $this->ActivationCode->find('first',$opt);$this->log(print_r($exists,1),'activations');if(empty($exists)){$referrerRequired = true;} else {$this->log(print_r('In else where exists in not empty',1),'activations');$userData['activated'] = 1;$referrerRequired = false;}}if($this->SocialProfile->User->save($userData)) {$data['user_id'] = $this->SocialProfile->User->getLastInsertId();} else{$result = array('success' => false, 'message' => $this->SocialProfile->User->validationErrors,'mobileRequired'=>$mobileRequired,'referrerRequired'=>$referrerRequired);break;}}$this->SocialProfile->create();if ($this->SocialProfile->save($data)) {$result = array('success' => true, 'message' => 'Social Profile Created','id' => $data['user_id'],'mobileRequired'=>$mobileRequired,'referrerRequired'=>$referrerRequired);if(!$referrerRequired) {$ignorereferrer = Configure::read('referrercode');if(in_array(strtoupper($referrer) , $ignorereferrer)){}else{$upstatus = "update activation_codes set status = 1 where code = '".strtoupper($referrer)."'";$this->SocialProfile->query($upstatus);}$this->markUserActivated($data['user_id']);}} else {$result = array('success' => false, 'message' => 'Social Profile Could Not Be Created','id' => -1,'mobileRequired'=>$mobileRequired,'referrerRequired'=>$referrerRequired);}} else {$result = array('success' => false, 'message' => "Email is missing",'mobileRequired'=>$mobileRequired,'referrerRequired'=>$referrerRequired);break;}} else {//If Social Profile exists, do not update referrer and email$userData = array('id'=>$socialProfile['SocialProfile']['user_id'],'first_name'=>$this->request->data['name'],'gender'=>$this->request->data['gender'],'profile_pic'=> $this->request->data['profile_pic']);//Check for utm parametersif(!empty($this->request->data['utm_source'])){$userData['utm_source'] = $this->request->data['utm_source'];}if(!empty($this->request->data['utm_medium'])){$userData['utm_medium'] = $this->request->data['utm_medium'];}if(!empty($this->request->data['utm_term'])){$userData['utm_term'] = $this->request->data['utm_term'];}if(!empty($this->request->data['utm_content'])){$userData['utm_content'] = $this->request->data['utm_content'];}if(!empty($this->request->data['utm_campaign'])){$userData['utm_campaign'] = $this->request->data['utm_campaign'];}$this->SocialProfile->User->save($userData);//Update token ra$socialProfile['SocialProfile']['access_token'] = $data['access_token'];$this->SocialProfile->save($socialProfile);//Check for mobile number$mobilenumber = $this->SocialProfile->User->find('first',array('conditions'=>array('id'=>$socialProfile['SocialProfile']['user_id']),'recursive'=>-1));if(!empty($mobilenumber['User']['mobile_number'])){$mobileRequired = false;}//Check if activated feild is set or not.if(!empty($mobilenumber['User']['activated'])) {$referrerRequired = false;} else {$uid=$socialProfile['SocialProfile']['user_id'];$res = $this->SocialProfile->query("select * from user_docs where user_id=$uid");$this->log("res".print_r($res,1),'registration');$docsSubmitted = !empty($res);}$data['user_id'] = $socialProfile['SocialProfile']['user_id'];$result = array('success' => true, 'message' => 'Existing Social Profile','id' => $socialProfile['SocialProfile']['user_id'],'mobileRequired'=>$mobileRequired,'referrerRequired'=>$referrerRequired);}}$this->updateSaholicUser($data['user_id'],$this->request->data['email']);$result['docsSubmitted'] = $docsSubmitted;$this->set(array('result' => $result,'callback' => $callback,'_serialize' => array('result')));$this->log(print_r($result,1),'registration');//$this->identifyUser($data['user_id']);$this->render('/Elements/json');}private function identifyUser($id) {$options = array('conditions'=>array('id'=>$id),'recursive'=>-1);$user = $this->User->find('first',$options);$this->log("[Identify] ".print_r($user,1),'registration');$pmaurl = Configure::read('pmaurl');if(!empty($user)) {$data = array('id'=>$user['User']['id'],'email'=>$user['User']['email'],'mobilenumber'=>$user['User']['mobilenumber'],'name'=>$user['User']['first_name'],'referral_code'=>$user['User']['referrer']);$this->post_request($pmaurl.'/identify',$data);}}private function updateSaholicUser($userId,$email=null) {if(!$email){//Handle it properlyreturn;}$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&from=profitmandi";$response = $this->make_request($url,null);$this->log('response '.print_r($response,1),'registration');if(!empty($response)){if($response['userId']<1)return;$data = array('account_type'=>'saholic','user_id'=>$userId,'account_key'=>$response['userId']);$this->UserAccount->create();$this->UserAccount->save($data);$data = array('account_type'=>'cartId','user_id'=>$userId,'account_key'=>$response['cartId']);$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'));}}