Blame | 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');$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));//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;}//Don't update referrer// if(strlen(trim($this->request->data['referrer']))>0) {// $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']);// } else{$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'])) {$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);} 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$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'],'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;}$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']);$this->set(array('result' => $result,'callback' => $callback,'_serialize' => array('result')));$this->log(print_r($result,1),'registration');$this->render('/Elements/json');}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'));}}