Yii Framework Forum: Tbfileupload Widget - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Tbfileupload Widget Pass a value to controller via the widget?

#1 User is offline   bodyfarmer 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 22-May 12

Posted 28 December 2013 - 09:17 PM

I have a property model view. In this view, there's TbFileUpload widget to upload photos of the property. I'm trying to pass property_id from property view to photo/upload controller but got undefined property_id error.


Here's my property view:
<h1>View Property #<?php echo $model->id; ?></h1>

<!--content begins-->

<!--content ends-->

<div id="prop_description">
		<h5 id="prop_hd">Description</h5>
		<?php echo $model->description; ?>
</div>

<?php 		
	echo $property_id= $model->id;
	$model_photo=new Photo;
	$model_photo->property_id = $property_id;
	?>
<?php $this->widget('bootstrap.widgets.TbFileUpload', array(
    'url' => $this->createUrl("photo/upload"),
    'model' => $model_photo,	
    'attribute' => 'picture', // see the attribute?
    'multiple' => true,
    'options' => array(
    'maxFileSize' => 2000000,
    'acceptFileTypes' => 'js:/(\.|\/)(gif|jpe?g|png)$/i',
))); ?>




the photo/upload controller


<?php

class PhotoController extends CController {
// ... more code here
 
/**
* Handles resource upload
* @throws CHttpException
*/


public function actionUpload()
{
	//Here we define the paths where the files will be stored temporarily
	$path = realpath( Yii::app( )->getBasePath( )."/../images/uploads/tmp/" )."/";
    $publicPath = Yii::app( )->getBaseUrl( )."/images/uploads/tmp/";
	
    header('Vary: Accept');
    if (isset($_SERVER['HTTP_ACCEPT']) && 
        (strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false))
    {
        header('Content-type: application/json');
    } else {
        header('Content-type: text/plain');
    }
    $data = array();
 
    $model_photo = new Photo('upload');
    $model_photo->picture = CUploadedFile::getInstance($model_photo, 'picture');
    if ($model_photo->picture !== null  && $model_photo->validate(array('picture')))
    {
		// data from file          			
			$model_photo->type = $model_photo->picture->getType( );
			$model_photo->size = $model_photo->picture->getSize( );		
		//(optional) Generate a random name for our file
            $filename = md5( Yii::app( )->user->id.microtime( ).$model_photo->name);
            $filename .= ".".$model_photo->picture->getExtensionName( );
			
        $model_photo->picture->saveAs($path.$filename);
        $model_photo->name = $model_photo->picture->name;
///////////////////// i got the error here	////////////////////////////////	
//$model_photo->property_id = $property_id;
		$model_photo->url = $publicPath.$filename;
        
		
		// save picture name
        if( $model_photo->save())
        {
            // return data to the fileuploader
            $data[] = array(
				
				//'property_id'=>$model_photo->picture->property_id,
                'name' => $model_photo->picture->name,
                'type' => $model_photo->picture->type,
                'size' => $model_photo->picture->size,
                // we need to return the place where our image has been saved
                'url' => $publicPath.$filename, // Should we add a helper method?
                // we need to provide a thumbnail url to display on the list
                // after upload. Again, the helper method now getting thumbnail.
                'thumbnail_url' =>  $publicPath."thumbs/$filename",
                // we need to include the action that is going to delete the picture
                // if we want to after loading 
                'delete_url' => $this->createUrl('photo/delete', 
                    array('id' => $model_photo->id, 'method' => 'uploader')),
                'delete_type' => 'POST');
        } else {
            $data[] = array('error' => 'Unable to save model after saving picture');
        }
    } else {
        if ($model_photo->hasErrors('picture'))
        {
            $data[] = array('error', $model_photo->getErrors('picture'));
        } else {
            throw new CHttpException(500, "Could not upload file ".     CHtml::errorSummary($model_photo));
        }
    }
    // JQuery File Upload expects JSON data
    echo json_encode($data);
}
 
// .... more code here 
 
} // end of controller


upload without property_id
Attached File  property_id.png (32.04K)
Number of downloads: 5

Any help would be awesome!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users