Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.53 KB | None | 0 0
  1. <?php
  2.  
  3. class MobileController extends Controller
  4. {
  5.     /**
  6.      * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
  7.      * using two-column layout. See 'protected/views/layouts/column2.php'.
  8.      */
  9.     public $layout='//layouts/column2';
  10.  
  11.     /**
  12.      * @var CActiveRecord the currently loaded data model instance.
  13.      */
  14.     private $_model;
  15.  
  16.     /**
  17.      * @return array action filters
  18.      */
  19.     public function filters()
  20.     {
  21.         return array(
  22.             'accessControl', // perform access control for CRUD operations
  23.         );
  24.     }
  25.  
  26.     /**
  27.      * Specifies the access control rules.
  28.      * This method is used by the 'accessControl' filter.
  29.      * @return array access control rules
  30.      */
  31.     public function accessRules()
  32.     {
  33.         return array(
  34.             array('allow',  // allow all users to perform 'index' and 'view' actions
  35.                 'actions'=>array( 'registerUser', 'addFacebookFriends', 'getFriends', 'getEvents',
  36.                                     'notifyPosition' ),
  37.                 'users'=>array('*'),
  38.             ),
  39.             array('allow', // allow authenticated user to perform 'create' and 'update' actions
  40.                 'actions'=>array(),
  41.                 'users'=>array('@'),
  42.             ),
  43.             array('allow', // allow admin user to perform 'admin' and 'delete' actions
  44.                 'actions'=>array(),
  45.                 'users'=>array('admin'),
  46.             ),
  47.             array('deny',  // deny all users
  48.                 'users'=>array('*'),
  49.             ),
  50.         );
  51.     }
  52.    
  53.     private function enableDebug() {
  54.         set_time_limit(0);
  55.         error_reporting(E_ALL);
  56.         ini_set('display_errors', '1');
  57.     }
  58.    
  59.    
  60.    
  61.     public function actionRegisterUser() {
  62.        
  63.         $this->enableDebug();
  64.        
  65.         $model = new User;
  66.         $friends = array();
  67.        
  68.         if ( isset($_GET['username']) ) $model->username = $_GET['username'];
  69.         if ( isset($_GET['password']) ) $model->password = $_GET['password'];
  70.         if ( isset($_GET['fb_id']) ) $model->fb_id = $_GET['fb_id'];
  71.         if ( isset($_GET['fb_id_friends']) ) $friends = explode(',', $_GET['fb_id_friends']);
  72.        
  73.         $model->last_update = 0;
  74.         $model->lat = 0;
  75.         $model->lng = 0;
  76.        
  77.         $result = new Result();
  78.         try {
  79.             if ( $model->save() ) {
  80.                 $this->addFaebookFriends($model, $friends);
  81.                 $result->setMessage('User ' . $model->username . ' has been created.');
  82.             } else {
  83.                 $result->setStatus( false );
  84.                 $result->setMessage('Error during creaton of the user ' . $model->username);
  85.                 $result->setErrors( $model->errors );
  86.             }
  87.         } catch ( CDbException $ex ) {
  88.             $result->setStatus( false );
  89.             $result->setMessage('The username ' . $model->username . ' has already been created.');
  90.         }
  91.        
  92.         echo CJSON::encode( $result );
  93.         exit();
  94.     }
  95.    
  96.     private function addFaebookFriends( $user, $fList ) {
  97.         foreach ( $fList as $fID ) {
  98.             $userFriend = User::model()->find('fb_id=?', array( $fID ) );
  99.             if ( $userFriend !== null ) { // L'amico e' gia' stato inserito
  100.                 $f = new Friend();
  101.                 $f->user_id = $user->id;
  102.                 $f->friend_id = $userFriend->id;
  103.                 try { $f->save(); } catch ( CDbException $ex ) {}
  104.             }  
  105.         }
  106.         return;
  107.     }
  108.    
  109.     public function actionGetFriends() {
  110.         $this->enableDebug();
  111.        
  112.         $model = null;
  113.         $friends = array();
  114.        
  115.         if ( isset($_GET['id']) ) $model = User::model()->findByPk($_GET['id']);
  116.        
  117.         if ( $model !== null ) {
  118.             foreach ( $model->friends as $f )
  119.                 if ( $f->isOnline() )
  120.                     $friends[] = $f;
  121.         }
  122.        
  123.         echo CJSON::encode( $friends );
  124.         exit();
  125.     }
  126.    
  127.     public function actionAddEvent() {
  128.         $this->enableDebug();
  129.         $model = new Event();
  130.        
  131.         if ( isset($_GET['user_id']) ) $model->user_id = $_GET['user_id'];
  132.         if ( isset($_GET['organizer']) ) $model->organizer = $_GET['organizer'];
  133.         if ( isset($_GET['title']) ) $model->title = $_GET['title'];
  134.         if ( isset($_GET['date_start']) ) $model->date_start = $_GET['date_start'];
  135.         if ( isset($_GET['date_end']) ) $model->date_end = $_GET['date_end'];
  136.         if ( isset($_GET['lat']) ) $model->lat = $_GET['lat'];
  137.         if ( isset($_GET['lng']) ) $model->lng = $_GET['lng'];
  138.        
  139.         $result = new Result();
  140.        
  141.         if ( $model->save() ) {
  142.             $result->setMessage('Event ' . $model->title . ' has been created.');
  143.         } else {
  144.             $result->setStatus( false );
  145.             $result->setMessage('Error during creaton of the event ' . $model->title);
  146.             $result->setErrors( $model->errors );
  147.         }
  148.        
  149.         echo CJSON::encode( $result );
  150.         exit();
  151.        
  152.        
  153.     }
  154.    
  155.     public function actionGetEvents() {
  156.         $this->enableDebug();
  157.        
  158.         $filterTitle = '';
  159.         $filterDateStart = '';
  160.         $filterDateEnd = '';
  161.        
  162.         if ( isset($_GET['filter_title']) ) $filterTitle = $_GET['filter_title'];
  163.         if ( isset($_GET['filter_date_start']) ) $filterDateStart = $_GET['filter_date_start'];
  164.         if ( isset($_GET['filter_date_end']) ) $filterDateEnd = $_GET['filter_date_end'];
  165.        
  166.         $criteria = new CDbCriteria;
  167.         $criteria->order = 'date_start ASC, title ASC';
  168.         if ( $filterDateStart != '' ) $criteria->compare('date_start','>'.$filterDateStart);
  169.         if ( $filterDateEnd != '' ) $criteria->compare('date_end','<'.$filterDateEnd);
  170.         if ( $filterTitle != '' ) $criteria->compare('title',$this->title,true);
  171.        
  172.         echo CJSON::encode( Event::model()->findAll($criteria) );
  173.         exit();
  174.        
  175.     }
  176.    
  177.     public function actionAddFacebookFriends() {
  178.         $this->enableDebug();
  179.        
  180.         $model = null;
  181.         $friends = array();
  182.         $result = new Result();
  183.        
  184.         if ( isset($_GET['id']) ) $model = User::model()->findByPk($_GET['id']);
  185.        
  186.        
  187.         if ( $model === null ) {
  188.             $result->setStatus( false );
  189.             $result->setMessage( 'Cannot find a username with that userID: ' . $_GET['id'] );      
  190.         } else {
  191.        
  192.             if ( isset($_GET['fb_id_friends']) ) $friends = explode(',', $_GET['fb_id_friends']);
  193.             $this->addFaebookFriends($model, $friends);
  194.             $result->setMessage('All facebook\'s friends has added to this account.');
  195.        
  196.         }
  197.        
  198.         echo CJSON::encode( $result );
  199.         exit();
  200.     }
  201.    
  202.     public function actionNotifyPosition() {
  203.        
  204.         $this->enableDebug();
  205.         $model = null;
  206.         $result = new Result();
  207.        
  208.         if ( isset($_GET['id']) ) $model = User::model()->findByPk($_GET['id']);
  209.        
  210.         if ( $model === null ) {
  211.             $result->setStatus( false );
  212.             $result->setMessage( 'Cannot find a username with that userID: ' . $_GET['id'] );      
  213.         } else {
  214.        
  215.             if ( isset($_GET['lat']) ) $model->lat = $_GET['lat'];
  216.             if ( isset($_GET['lng']) ) $model->lng = $_GET['lng'];
  217.             $model->last_update = time();
  218.            
  219.             if ( $model->save() ) {
  220.                 $result->setMessage('New position for user ' . $model->username . ' has been updated.');
  221.             } else {
  222.                 $result->setStatus( false );
  223.                 $result->setMessage('Error during the notification of the new position');
  224.                 $result->setErrors( $model->errors );
  225.             }
  226.        
  227.         }
  228.        
  229.         echo CJSON::encode( $result );
  230.         exit();
  231.        
  232.     }
  233.    
  234.    
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement