I followed the XUpload Workflow, yiiframework.com/wiki/348/xupload-workflow/
I get the following error:
Undefined variable: model
C:\xampp\htdocs\UOFApp\protected\views\childphoto\_form.php(15)
03
04 <?php $form = $this->beginWidget(‘GxActiveForm’, array(
05 ‘id’ => ‘childphoto-form’,
06 ‘enableAjaxValidation’ => false,
07 ‘htmlOptions’ => array(‘enctype’ => ‘multipart/form-data’),
08 ));
09 ?>
10
11 <p class="note">
12 <?php echo Yii::t(‘app’, ‘Fields with’); ?> <span class=“required”>*</span> <?php echo Yii::t(‘app’, ‘are required’); ?>.
13 </p>
14
15 <?php echo $form->errorSummary($model); ?>
16
17
18 <div class="row">
I have an existing model named Childphoto , here is what the relevant code from ChildphotoController.php looks like:
<?php
class ChildphotoController extends GxController {
public function filters() {
return array(
'accessControl',
);
}
public function accessRules() {
return array(
array('allow',
'actions'=>array('index','view'),
'users'=>array('*'),
),
array('allow',
'actions'=>array('minicreate', 'create','update','download'),
'users'=>array('@'),
),
array('allow',
'actions'=>array('admin','delete','upload','form'),
'users'=>array('@'),
'expression'=>'isset(Yii::app()->user->type) && (Yii::app()->user->type == "admin" || Yii::app()->user->type == "staff")',
),
array('deny',
'users'=>array('*'),
),
);
}
public function actionView($id) {
$this->render('view', array(
'model' => $this->loadModel($id, 'Childphoto'),
));
}
public function actionForm( ) {
$model = new Childphoto;
Yii::import( "xupload.models.XUploadForm" );
$photos = new XUploadForm;
//Check if the form has been submitted
if( isset( $_POST['Childphoto'] ) ) {
//Assign our safe attributes
$model->attributes = $_POST['Childphoto'];
//Start a transaction in case something goes wrong
$transaction = Yii::app( )->db->beginTransaction( );
try {
//Save the model to the database
if($model->save()){
$transaction->commit();
}
} catch(Exception $e) {
$transaction->rollback( );
Yii::app( )->handleException( $e );
}
}
$this->render( 'form', array(
'model' => $model,
'photos' => $photos,
) );
}
public function actionUpload( ) {
Yii::import( "xupload.models.XUploadForm" );
//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/";
//This is for IE which doens't handle 'Content-type: application/json' correctly
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' );
}
//Here we check if we are deleting and uploaded file
if( isset( $_GET["_method"] ) ) {
if( $_GET["_method"] == "delete" ) {
if( $_GET["file"][0] !== '.' ) {
$file = $path.$_GET["file"];
if( is_file( $file ) ) {
unlink( $file );
}
}
echo json_encode( true );
}
} else {
$model = new XUploadForm;
$model->file = CUploadedFile::getInstance( $model, 'file' );
//We check that the file was successfully uploaded
if( $model->file !== null ) {
//Grab some data
$model->mime_type = $model->file->getType( );
$model->size = $model->file->getSize( );
$model->name = $model->file->getName( );
//(optional) Generate a random name for our file
$filename = md5( Yii::app( )->user->id.microtime( ).$model->name);
$filename .= ".".$model->file->getExtensionName( );
if( $model->validate( ) ) {
//Move our file to our temporary dir
$model->file->saveAs( $path.$filename );
//chmod( $path.$filename, 0777 ); //uncomment for linux server
//here you can also generate the image versions you need
//using something like PHPThumb
//Now we need to save this path to the user's session
if( Yii::app( )->user->hasState( 'images' ) ) {
$userImages = Yii::app( )->user->getState( 'images' );
} else {
$userImages = array();
}
$userImages[] = array(
"path" => $path.$filename,
//the same file or a thumb version that you generated
"thumb" => $path.$filename,
"filename" => $filename,
'size' => $model->size,
'mime' => $model->mime_type,
'name' => $model->name,
);
Yii::app( )->user->setState( 'images', $userImages );
//Now we need to tell our widget that the upload was succesful
//We do so, using the json structure defined in
// github.com/blueimp/jQuery-File-Upload/wiki/Setup
echo json_encode( array( array(
"name" => $model->name,
"type" => $model->mime_type,
"size" => $model->size,
"url" => $publicPath.$filename,
"thumbnail_url" => $publicPath."thumbs/$filename",
"delete_url" => $this->createUrl( "upload", array(
"_method" => "delete",
"file" => $filename
) ),
"delete_type" => "POST"
) ) );
} else {
//If the upload failed for some reason we log some data and let the widget know
echo json_encode( array(
array( "error" => $model->getErrors( 'file' ),
) ) );
Yii::log( "XUploadAction: ".CVarDumper::dumpAsString( $model->getErrors( ) ),
CLogger::LEVEL_ERROR, "xupload.actions.XUploadAction"
);
}
} else {
throw new CHttpException( 500, "Could not upload file" );
}
}
}
Here is my /protected/views/childphoto/form.php
<fieldset>
<?php
$form = $this->beginWidget('GxActiveForm', array(
'id' => 'childphoto-form',
'enableAjaxValidation' => false,
//This is very important when uploading files
'htmlOptions' => array('enctype' => 'multipart/form-data'),
));
?>
<div class="row">
<?php //echo $form->labelEx($model,'field1'); ?>
<?php //echo $form->textField($model,'field1'); ?>
<?php //echo $form->error($model,'field1'); ?>
</div>
<!-- Other Fields... -->
<div class="row">
<?php //echo $form->labelEx($model,'photos'); ?>
<?php
$this->widget( 'xupload.XUpload', array(
'url' => Yii::app( )->createUrl( "/childphoto/upload"),
//our XUploadForm
'model' => $photos,
//We set this for the widget to be able to target our own form
'htmlOptions' => array('id'=>'childphoto-form'),
'attribute' => 'file',
'multiple' => false,
//Note that we are using a custom view for our widget
//Thats becase the default widget includes the 'form'
//which we don't want here
'formView' => 'application.views.childphoto._form',
)
);
?>
</div>
<button type="submit">Submit</button>
<?php $this->endWidget(); ?>
</fieldset>
Here is my /protected/views/childphoto/_form.php
<div class="wide form">
<?php $form = $this->beginWidget('GxActiveForm', array(
'id' => 'childphoto-form',
'enableAjaxValidation' => false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
));
?>
<p class="note">
<?php echo Yii::t('app', 'Fields with'); ?> <span class="required">*</span> <?php echo Yii::t('app', 'are required'); ?>.
</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'Date of Photo'); ?>
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model' => $model,
'attribute' => 'date_version',
'options' => array(
'showOn' => 'both',
'dateFormat' => 'yy-mm-dd',
'showOtherMonths' => true,
'selectOtherMonths' => true,
'changeYear' => true,
'changeMonth' => true,
'showButtonPanel' => true,
),
'htmlOptions' => array(
'size' => '30',
'maxlength' => '30',
),
));?>
<?php echo $form->error($model,'date_version'); ?>
</div><!-- row -->
<div class="row">
<?php echo $form->labelEx($model,'notes'); ?>
<?php echo $form->textArea($model, 'notes'); ?>
<?php echo $form->error($model,'notes'); ?>
</div><!-- row -->
<div class="row">
<?php //echo $form->labelEx($model,'user_id'); ?>
<?php echo $form->hiddenField($model, 'user_id', array(Yii::app()->user->id)); ?>
<?php echo $form->error($model,'user_id'); ?>
</div><!-- row -->
<div class="row">
<?php $form->labelEx($model,'date_entered'); ?>
<?php $form->textField($model, 'date_entered', array('class'=>'clear-input txt')); ?>
<?php echo $form->error($model,'date_entered'); ?>
</div><!-- row -->
<div class="row">
<?php $form->labelEx($model,'date_updated'); ?>
<?php $form->textField($model, 'date_updated'); ?>
<?php echo $form->error($model,'date_updated'); ?>
</div><!-- row -->
<?php
echo GxHtml::submitButton(Yii::t('app', 'Save'));
$this->endWidget();
?>
</div><!-- form -->
I’m not understanding the example workflow’s use of form.php and _form.php , so that is probably where I’ve made mistakes. In /protected/views/childphoto/form.php , in the XUpload widget section, I’m not sure what to use for the assignment for model (the example assigned $photos), and don’t know what the file _form.php should be containing. Should form.php and _form.php have the same form id (childphoto-form)? How does the /protected/controllers/ChildphotoController.php 's actionCreate function interact now (auto-generated by gii crud)? Is the /protected/views/childphoto/create.php viewfile still used?
$this->widget( 'xupload.XUpload', array(
'url' => Yii::app( )->createUrl( "/childphoto/upload"),
//our XUploadForm
'model' => $photos, // Is this supposed to be $childphoto instead??? or $model???
//We set this for the widget to be able to target our own form
'htmlOptions' => array('id'=>'childphoto-form'),
'attribute' => 'file',
'multiple' => false,
//Note that we are using a custom view for our widget
//Thats becase the default widget includes the 'form'
//which we don't want here
'formView' => 'application.views.childphoto._form',
)
);
Do the attributes in my /protected/models/Childphoto.php need to correlate with the attributes in the /extensions/xupload/models/XUploadForm.php file? In other words, do I need to assign file, mime_type, size, name, filename attributes in my Childphoto model?
from /protected/extensions/xupload/models/XUploadForm.php:
<?php
class XUploadForm extends CFormModel
{
public $file;
public $mime_type;
public $size;
public $name;
public $filename;