Refresh On Redirect

When my users log in I call a function that copies their avatar pic and overwrites the generic avatar pic (avatar.jpg) with theirs

this is done with the "copy" function. works well. when a particular user logs on, the file does indeed change in the file system but after the log on redirect it does not refresh on screen… I need to manually refresh and then the appropriate pic appears.

basically…when log on is successful the following code is called




if (Yii::app()->user->returnUrl=='/index.php')

$this->redirect(Yii::app()->controller->module->returnUrl);

else

$this->redirect(Yii::app()->user->returnUrl);

reason I overwrite the avatar.jpg is because I’m using CSS to draw the pic on screen. So the CSS simply displays the avatar.jpg

is there a way to force a refresh after the logon or clear the cache for that image?

thanks

It should be two different images so there is no need to clear the cache for this. Give an example of your code to show the image in your view. Also what are you using for your user model (are you using a module or defuat etc).

Also, do you show your images globally or just in one view this will make a difference because one is just a simple if else and one is a widget

here’s where I use the CSS to display the image (in my main.php layout)





 $this->widget('bootstrap.widgets.TbNavbar', array(

	'htmlOptions' => array('class' => 'navbar-fixed-top'),

        'type'=>'inverse', // null or 'inverse'

        'brand'=>'',

        'brandOptions'=>array(),

        'collapse'=>true, // requires bootstrap-responsive.css

        'items'=>array(

                array(

                'class'=>'bootstrap.widgets.TbMenu',

                'htmlOptions'=>array('class'=>'container_tweak pull-right'  ),

                'items'=>array(

				array('label' => Yii::t('general',"hello ").Yii::app()->user->name, 'url'=>Yii::app()->getModule('user')->profileUrl),

				array('label'=>"", 'url'=>null,'visible'=>true,'itemOptions'=>array('class'=>'user_icon')),

                )),


            ),


        ),


    ),

)



the user_icon class being used on the last line calls this CSS code




.user_icon {


    width: 50px;

    height: 50px;

    background-image: url('../images/users/gen/avatar.jpg');

    background-repeat: no-repeat;

    float:left;

}



now… my login controller calls the following




public function actionLogin()

	{

	

	

		if (Yii::app()->user->isGuest) {

			$model=new UserLogin;

			// collect user input data

			if(isset($_POST['UserLogin']))

			{

				$model->attributes=$_POST['UserLogin'];

				// validate user input and redirect to previous page if valid

				if($model->validate()) {

					$this->lastViset();


					/*  set avatar pic to current user just logged in  */

                                        $this->setUserAvatar()

					if (Yii::app()->user->returnUrl=='/index.php')

						$this->redirect(Yii::app()->controller->module->returnUrl);

					else

						$this->redirect(Yii::app()->user->returnUrl);

						

				}

			}

			// display the login form

			$this->render('/user/login',array('model'=>$model));

		} //else

			$this->redirect(Yii::app()->controller->module->returnUrl);

			

	}



and here’s the setUserAvatar() function




private function setUserAvatar(){

	$users=User::model()->findByPk($id);

	

	$basefile = Yii::getPathOfAlias('webroot.images.users').'/'.$img.'';

	$targetfle =Yii::getPathOfAlias('webroot.images.users').'/gen/avatar.jpg'; 


        copy($basefile,$targetfle)


}



so that’s the code being used. so for some reason after a successful login the image changes in the file system then the redirect happens and still uses whatever pic the avatar.jpg was previously set to. hit refresh once and the new pic comes up

You are trying to repace a file with the copy() however, this wont work becuase your image is being set in the css. You would need to override the css to reflect your new image.

ie


[code]

private function setUserAvatar(){

	$users=User::model()->findByPk($id);

   

	$basefile = Yii::getPathOfAlias('webroot.images.users').'/'.$img;

	

	$userImage = '<style type="text/css">.user_icon {background-image: url('.$basefile.') !important;}</style>


   	return $userImage;


}



however id use a widget and remove some things




remove the 

private function setUserAvatar(){}


.user_icon {


	width: 50px;

	height: 50px;

	background-image: url('../images/users/gen/avatar.jpg');

	background-repeat: no-repeat;

	float:left;

}


and change


public function actionLogin()

	{

    	if (Yii::app()->user->isGuest) {

        	$model=new UserLogin;

        	// collect user input data

        	if(isset($_POST['UserLogin']))

        	{

            	$model->attributes=$_POST['UserLogin'];

            	// validate user input and redirect to previous page if valid

            	if($model->validate()) {

                	$this->lastViset();

                	if (Yii::app()->user->returnUrl=='/index.php')

                    	$this->redirect(Yii::app()->controller->module->returnUrl);

                	else

                    	$this->redirect(Yii::app()->user->returnUrl);

            	}

        	}

        	// display the login form

        	$this->render('/user/login',array('model'=>$model));

    	} //else

        	$this->redirect(Yii::app()->controller->module->returnUrl); 

	}




i would use a widget like the one i just attached to this and save it where ever you want mine is uner extensions -> yiiuserimg -> YiiUserImg.php) . You will have to replace "image" with whatever your db field name is. You can use it like


$this->widget('ext.yiiuserimg.YiiUserImg', array(

   'htmlOptions'=>array(

    	'title'=>'User image',

    	'alt'=>'User Image Directory Can't Be Found',

    	'style' => 'width: 50px; height: 50px;',

   )

 ));

You could put that in any view ie the navbar, profile page, on comments etc.

yeah… a widget might be an idea, thanks for the code!

but how could you put that into the navbar code?

ive never used the bootstrap extenstion but maybe something like this might work

id put the if else into a function


$imgbaseurl = Yii::app()->baseUrl.'/';

	$defaultimgbaseurl = Yii::app()->theme->baseUrl.'/images/users/gen/avatar.jpg'; //Path to your default image location and image name!

	if(Yii::app()->user->isGuest)

	{

	$userImage = $defaultimgbaseurl;

	}

	else {	

    	$userObject = Yii::app()->getModule('user')->user(); //Renders the user model

    	$userimg = Yii::app()->baseUrl.'/'.$userObject->image;  //Renders the current user's image path.  Filename is what I call my file path in my Users Model.  It can be called anything just make sure you chage it everywhere.

    	$imagelink = $imgbaseurl.$userObject->image; //user image

    	if (empty($userObject->image)) //if image column in your database is empty 

    	{

    	$userImage = $defaultimgbaseurl; //render the default image 

    	}

    	else //if it's not empty 

    	{

   	$userImage = $imagelink; //render the user image

    	}

	}

  


	$this->widget('bootstrap.widgets.TbNavbar',array(

	'encodeLabel'=>false,// Added this'htmlOptions' => array('class' => 'navbar-fixed-top'),

    	'type'=>'inverse', // null or 'inverse'

    	'brand'=>'',

    	'brandOptions'=>array(),

    	'collapse'=>true, // requires bootstrap-responsive.css

    	'items'=>array(

        	array(

            	'class'=>'bootstrap.widgets.TbMenu',

            	'htmlOptions'=>array('class'=>'pull-right'),

            	'items'=>array(

                	array('label'=>$userImage,

                    	'items'=>array(

                        	//only show if user is logged in

                        	array('label' => Yii::t('general',"hello ").Yii::app()->user->name, 'url'=>Yii::app()->getModule('user')->profileUrl), 'visible'=>!Yii::app()->user->isGuest,

                    	),

                	),

            	),

        	),

    	),

	)); 

i didn’t test it and it was from just reading the docs pretty quickly so it will most likely need to be adjusted. As far as putting a image there using a widget you would probably have to extend the navbar to accept it and call your modified version

thanks for all the help… ended up going a different route since I had a hard time plugging the widget in the navbar

but you pointed me in the right direction and I was able to solve my issue

I’ll post my solution a little later in case some are curious

thanks again!