Hi all…I really need some help, as nothing on the net refers to my specific situation.
I am using the wizard-behavior extension. It is built using CFormModel rather than CActiveRecord Model. I am pretty much stuck using things the way they are setup…so…
I have a 4 step registration form, first 3 steps are fine, however, the 4th page is to upload a photo. I can not for the life of me get it to pass the file to the controller.
Here is my Model:
<?php
class UploadPhoto extends CFormModel {
public $userPhoto;
public function rules() {
return array(
array('userPhoto','file','types'=>'jpg,jpeg,gif','allowEmpty'=>false),
);
}
public function attributeLabels() {
return array(
'userPhoto'=>'Upload A Photo',
);
}
public function getForm() {
return new CForm(array(
'showErrorSummary'=>false,
'enctype'=>'multipart/form-data',
'elements'=>array(
'userPhoto'=>array(
'type'=>'file',
'hint'=>'Please Choose One',
),
),
'buttons'=>array(
'reset'=>array(
'type'=>'submit',
'label'=>'Reset Form'
),
'submit'=>array(
'type'=>'submit',
'label'=>'Next',
)
)
), $this);
}
/**
* Recovers a partially completed registration.
* @param string User registration UUID; data will be recovered from the registration file.
* @return mixed array: user registration data; boolean: false if the registration data could not be recovered
*/
public function recoverRegistration($uuid) {
$registrationFile = Yii::getPathOfAlias('application.runtime.registration').DIRECTORY_SEPARATOR.$uuid.'_draft.txt';
if (file_exists($registrationFile)) {
$data = unserialize(@file_get_contents($registrationFile));
unlink($registrationFile);
return $data;
}
else
return false;
}
/**
* Saves a draft of registration data.
*/
public function saveRegistration($data) {
$uuid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
$registrationDir = Yii::getPathOfAlias('application.runtime.registration');
$registrationDirReady = true;
if (!file_exists($registrationDir)) {
if (!mkdir($registrationDir) || !chmod($registrationDir, 0775))
$registrationDirReady = false;
}
if ($registrationDirReady && file_put_contents(
$registrationDir.DIRECTORY_SEPARATOR.$uuid.'_draft.txt', serialize($data)
))
return $uuid;
return false;
}
}
And My Form:
<?php
echo $event->sender->menu->run();
echo '<div>Step '.$event->sender->currentStep.' of '.$event->sender->stepCount;
echo '<h3>'.$event->sender->getStepLabel($event->step).'</h3>';
echo CHtml::tag('div',array('class'=>'form'),$form);
And my Controller:
public function wizardFinished($event) {
$command = Yii::app()->db->createCommand();
if(isset($event->data['UserEmployeeProfile'])){
$db_array = merge_array($event->data['User'],$event->data['ContactDetails'],$event->data['UserEmployeeProfile'],$event->data['UploadPhoto']);
}else{
$db_array = array_merge($event->data['User'],$event->data['ContactDetails'],$event->data['UserEmployerProfile'],$event->data['UploadPhoto']);
}
$this->UploadPhoto($db_array['userPhoto']);
$db_array['userPhoto']=$_SESSION['newName'];
if ($event->step===true){
$command->insert('users',$db_array);
// ...redirect to another page
$this->render('completed', compact('event'));
}else{
$this->render('finished', compact('event'));
}
$event->sender->reset();
Yii::app()->end();
}
public function UploadPhoto($inPhoto){
Yii::import('application.extensions.upload.Upload');
// receive file from post
//$Upload = new Upload( (isset($inPhoto) ? $inPhoto : null) );
$fullpath = 'C:/Users/Pirificio/Pictures/'.$inPhoto; //Used to test locally
$Upload = new Upload($fullpath);
$Upload->jpeg_quality = 100;
$Upload->no_script = false;
$Upload->image_resize = true;
$Upload->image_x = 700;
$Upload->image_y = 500;
$Upload->image_ratio = true;
// some vars
$newName = md5($inPhoto);
$destPath = Yii::app()->getBasePath().'/../images/members/';
$destName = 'big';
// verify if was uploaded
if ($Upload->uploaded) {
$Upload->file_new_name_body = $newName;
$Upload->process($destPath);
// if was processed
if ($Upload->processed) {
$destName = $Upload->file_dst_name;
// create the thumb
unset($Upload);
$destName = 'small';
$Upload = new Upload($destPath.$destName);
$Upload->file_new_name_body = $newName;
$Upload->no_script = false;
$Upload->image_resize = true;
$Upload->image_x = 120;
$Upload->image_y = 80;
$Upload->image_ratio = true;
$Upload->process($destPath);
} else {
echo($Upload->error);
};
};
$_SESSION['newName']=$newName;
return;
}
As you can see, I am kinda cheating a bit, and basically processing all info collected from the wizard in $event->data and mashing it into an array and writing it to the db. Before I write to the db though, I need to get the file, and process the photo. Basically the file isn’t passed to $event->data like everything else is.
HELP Please!!!
Pirificio