// Adding jQuery to the view page.
Yii::app()->getClientScript()->registerCoreScript('jquery');
$event = new Event;
// Adding an empty PhotoEvent to the form
$photosEvent = array( new PhotoEvent, );
// If everything is setted I will check and process the inputs
if( isset( $_POST['submitDatas'] ) && isset( $_POST['PhotoEvent'], $_POST['Event'] ) )
{
$event->attributes= = $_POST['Event'];
// DB date layout
$event->expireDate = date_format(date_create($event->expireDate), 'Y-m-d');
$valid = $event->validate();
// This is the crappy part of the script that I need to improve and make it more elegance
// I've to add to the photoEvent array a new PhotoEvent for every istance in $_POST['PhotoEvent']
foreach ( $_POST['PhotoEvent'] as $i => $photo ) {
$photosEvent[$i] = new PhotoEvent;
if ( isset( $_POST['PhotoEvent'][$i] ) )
$photosEvent[$i]->photoUrl = CUploadedFile::getInstance($photosEvent[$i], "photoUrl[$i]" );
// Saving each period ( i've to change the date format couse in Italy we use dd/mm/yyyy
foreach ( $photosEvent as $i => $photo ) {
$photo->eventId = $event->id;
// Taking and saving the image in the filesystem in the IMG_DIR_PATH/ directory path.
// In this case IMG_DIR_PATH is in the root of the
// site and you need to give it rights to write the image :)
$photo->photoUrl->saveAs('IMG_DIR_PATH/'.$photo->photoUrl->getName());
/* If you need to resize the image use this path of code
$image = new Image('IMG_DIR_PATH/'.$photo->photoUrl->getName());
$image->resize(200, 200);
$image->save();
*/
$photo->save(false);
}
$this->redirect(array('show','id'=>$lastminuteevent->id));
}
}
$this->render('create', array(
'event' => $event,
'photosEvent' => $photosEvent,
'photosNumber' => isset($_POST['PhotoEvent']) ? count($_POST['PhotoEvent'])-1 : 0, //How many PhotoEvent the user added
'update' => false,
));
}
?>
```
I wish you will enjoy my guide :) Please gimme some feedback to improve it :)