Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class MobileController extends Controller
- {
- /**
- * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
- * using two-column layout. See 'protected/views/layouts/column2.php'.
- */
- public $layout='//layouts/column2';
- /**
- * @var CActiveRecord the currently loaded data model instance.
- */
- private $_model;
- /**
- * @return array action filters
- */
- public function filters()
- {
- return array(
- 'accessControl', // perform access control for CRUD operations
- );
- }
- /**
- * Specifies the access control rules.
- * This method is used by the 'accessControl' filter.
- * @return array access control rules
- */
- public function accessRules()
- {
- return array(
- array('allow', // allow all users to perform 'index' and 'view' actions
- 'actions'=>array( 'registerUser', 'addFacebookFriends', 'getFriends', 'getEvents',
- 'notifyPosition' ),
- 'users'=>array('*'),
- ),
- array('allow', // allow authenticated user to perform 'create' and 'update' actions
- 'actions'=>array(),
- 'users'=>array('@'),
- ),
- array('allow', // allow admin user to perform 'admin' and 'delete' actions
- 'actions'=>array(),
- 'users'=>array('admin'),
- ),
- array('deny', // deny all users
- 'users'=>array('*'),
- ),
- );
- }
- private function enableDebug() {
- set_time_limit(0);
- error_reporting(E_ALL);
- ini_set('display_errors', '1');
- }
- public function actionRegisterUser() {
- $this->enableDebug();
- $model = new User;
- $friends = array();
- if ( isset($_GET['username']) ) $model->username = $_GET['username'];
- if ( isset($_GET['password']) ) $model->password = $_GET['password'];
- if ( isset($_GET['fb_id']) ) $model->fb_id = $_GET['fb_id'];
- if ( isset($_GET['fb_id_friends']) ) $friends = explode(',', $_GET['fb_id_friends']);
- $model->last_update = 0;
- $model->lat = 0;
- $model->lng = 0;
- $result = new Result();
- try {
- if ( $model->save() ) {
- $this->addFaebookFriends($model, $friends);
- $result->setMessage('User ' . $model->username . ' has been created.');
- } else {
- $result->setStatus( false );
- $result->setMessage('Error during creaton of the user ' . $model->username);
- $result->setErrors( $model->errors );
- }
- } catch ( CDbException $ex ) {
- $result->setStatus( false );
- $result->setMessage('The username ' . $model->username . ' has already been created.');
- }
- echo CJSON::encode( $result );
- exit();
- }
- private function addFaebookFriends( $user, $fList ) {
- foreach ( $fList as $fID ) {
- $userFriend = User::model()->find('fb_id=?', array( $fID ) );
- if ( $userFriend !== null ) { // L'amico e' gia' stato inserito
- $f = new Friend();
- $f->user_id = $user->id;
- $f->friend_id = $userFriend->id;
- try { $f->save(); } catch ( CDbException $ex ) {}
- }
- }
- return;
- }
- public function actionGetFriends() {
- $this->enableDebug();
- $model = null;
- $friends = array();
- if ( isset($_GET['id']) ) $model = User::model()->findByPk($_GET['id']);
- if ( $model !== null ) {
- foreach ( $model->friends as $f )
- if ( $f->isOnline() )
- $friends[] = $f;
- }
- echo CJSON::encode( $friends );
- exit();
- }
- public function actionAddEvent() {
- $this->enableDebug();
- $model = new Event();
- if ( isset($_GET['user_id']) ) $model->user_id = $_GET['user_id'];
- if ( isset($_GET['organizer']) ) $model->organizer = $_GET['organizer'];
- if ( isset($_GET['title']) ) $model->title = $_GET['title'];
- if ( isset($_GET['date_start']) ) $model->date_start = $_GET['date_start'];
- if ( isset($_GET['date_end']) ) $model->date_end = $_GET['date_end'];
- if ( isset($_GET['lat']) ) $model->lat = $_GET['lat'];
- if ( isset($_GET['lng']) ) $model->lng = $_GET['lng'];
- $result = new Result();
- if ( $model->save() ) {
- $result->setMessage('Event ' . $model->title . ' has been created.');
- } else {
- $result->setStatus( false );
- $result->setMessage('Error during creaton of the event ' . $model->title);
- $result->setErrors( $model->errors );
- }
- echo CJSON::encode( $result );
- exit();
- }
- public function actionGetEvents() {
- $this->enableDebug();
- $filterTitle = '';
- $filterDateStart = '';
- $filterDateEnd = '';
- if ( isset($_GET['filter_title']) ) $filterTitle = $_GET['filter_title'];
- if ( isset($_GET['filter_date_start']) ) $filterDateStart = $_GET['filter_date_start'];
- if ( isset($_GET['filter_date_end']) ) $filterDateEnd = $_GET['filter_date_end'];
- $criteria = new CDbCriteria;
- $criteria->order = 'date_start ASC, title ASC';
- if ( $filterDateStart != '' ) $criteria->compare('date_start','>'.$filterDateStart);
- if ( $filterDateEnd != '' ) $criteria->compare('date_end','<'.$filterDateEnd);
- if ( $filterTitle != '' ) $criteria->compare('title',$this->title,true);
- echo CJSON::encode( Event::model()->findAll($criteria) );
- exit();
- }
- public function actionAddFacebookFriends() {
- $this->enableDebug();
- $model = null;
- $friends = array();
- $result = new Result();
- if ( isset($_GET['id']) ) $model = User::model()->findByPk($_GET['id']);
- if ( $model === null ) {
- $result->setStatus( false );
- $result->setMessage( 'Cannot find a username with that userID: ' . $_GET['id'] );
- } else {
- if ( isset($_GET['fb_id_friends']) ) $friends = explode(',', $_GET['fb_id_friends']);
- $this->addFaebookFriends($model, $friends);
- $result->setMessage('All facebook\'s friends has added to this account.');
- }
- echo CJSON::encode( $result );
- exit();
- }
- public function actionNotifyPosition() {
- $this->enableDebug();
- $model = null;
- $result = new Result();
- if ( isset($_GET['id']) ) $model = User::model()->findByPk($_GET['id']);
- if ( $model === null ) {
- $result->setStatus( false );
- $result->setMessage( 'Cannot find a username with that userID: ' . $_GET['id'] );
- } else {
- if ( isset($_GET['lat']) ) $model->lat = $_GET['lat'];
- if ( isset($_GET['lng']) ) $model->lng = $_GET['lng'];
- $model->last_update = time();
- if ( $model->save() ) {
- $result->setMessage('New position for user ' . $model->username . ' has been updated.');
- } else {
- $result->setStatus( false );
- $result->setMessage('Error during the notification of the new position');
- $result->setErrors( $model->errors );
- }
- }
- echo CJSON::encode( $result );
- exit();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement