How do I get when it finishes inserting the data, perform direct application automatically generates reports based on the data that has just inputted ?
My controller (Save) :
public function actionCreate()
{
$model=new PurchaseOrder;
if(isset($_POST['PurchaseOrder']))
{
$model->attributes=$_POST['PurchaseOrder'];
if($model->save())
$this->redirect('index');
}
$this->render('create',array(
'model'=>$model,
));
}
My controller (Generate Report) :
public function actionCetak()
{
if(isset($_POST['PrintPO'])){
$data = $_POST['no_po'];
if($data==''){
return false;
}else{
$HTML2PDF = Yii::app()->ePdf->HTML2PDF();
$HTML2PDF->WriteHTML($this->renderPartial('data_print', array(
'data' => $this->loadCetak($data)
), true));
$HTML2PDF->Output();
}
}else{
$this->render('form_cetak_po');
}
}
both functions run smoothly, but when I combining into :
public function actionCreate()
{
$model=new PurchaseOrder;
if(isset($_POST['PurchaseOrder']))
{
$model->attributes=$_POST['PurchaseOrder'];
if($model->save())
$HTML2PDF = Yii::app()->ePdf->HTML2PDF();
$HTML2PDF->WriteHTML($this->renderPartial('data_print', array(
'data' => $this->loadCetak($model->id)
), true));
$HTML2PDF->Output();
$this->redirect('index');
}
$this->render('create',array(
'model'=>$model,
));
}
Application only do the insert function. How do I order when finished doing the insert, directly akang application and the data it generates can be downloaded.
Thanks
You misses brackets after if(save())
Look at this :
if(isset($_POST['PurchaseOrder']))
{
$model->attributes=$_POST['PurchaseOrder'];
if($model->save())
{ // <-- add this
$HTML2PDF = Yii::app()->ePdf->HTML2PDF();
$HTML2PDF->WriteHTML($this->renderPartial('data_print', array(
'data' => $this->loadCetak($data)
), true));
$HTML2PDF->Output();
} //<-- add this
else
{
$this->redirect('index');
}
}
Redirecting after download
I don't think this can be done.
The common thing in popular download sites is the reverse: first you go to the "after" page and then the download starts.
You can do this :
after save,redirect to something like this
index.php?r=controller/index&file_info=information
and in index method, check if ($_GET['file_info']), then create your pdf file and make the download.
Look at the top answer here
Related
why did I get this bad request (#400) error after submitting my update on my 'news' section :
Missing required parameters: id
This is my update function
php
public function actionUpdate($id)
{
$model = $this->findModel($id);
$oldFile = $model->getImageFile();
$currentImage=$model->image;
if ($model->load(Yii::$app->request->post())) {
// process uploaded image file instance
$image = $model->uploadImage();
// revert back if no valid file instance uploaded
if ($image === false) {
$model->image = $currentImage;
}
if ($model->save()) {
// upload only if valid uploaded file instance found
if ($image !== false && unlink($oldFile)) { // delete old and overwrite
$path = $model->getImageFile();
$image->saveAs($path);
}
return $this->redirect(['view', ['id'=>$model->id,'image'=>$model->image],
]);
}
} else {
return $this->render('update', [
'model'=>$model,
]);
}
}
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
Please note that the updating and uploading image process were actually a success (that's why I didn't show my model's method code that my actionUpdate called). If I go directly to view the recently updated page and view the content, it clearly displays the updated contents along with the image without problem. The error only occurs just right after the update is submitted. I thought I had pass the parameter after the update by this line :
return $this->redirect(['view', ['id'=>$model->id,'image'=>$model->image],
Right before that line I even echo $model->id to test and it echoed out the id.
I've looked for similar problem in StackOverflow and find someone suggested this :
php
if($model->save())
{
$lastInsertID = $model->getPrimaryKey();
return $this->redirect(['view', 'id' => $lastInsertID]);
}
But it didn't work. Any idea?
You should pass URL as flat array:
return $this->redirect(['view', 'id' => $model->id,'image' => $model->image]);
I am using Zend 1.12
I have a form which has two fields: email, and file
My index view shows the form. And after validation is successful, I want to parse the csv file and show the content on result view:
My Code:
IndexController:
class IndexController extends Zend_Controller_Action {
public function init() {
/* Initialize action controller here */
}
public function indexAction() {
// action body
$form = new Application_Form_FileUpload();
$form->submit->setLabel('Upload');
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
$file = $form->getValue('file');
$email = $form->getValue('email');
//$this->_helper->viewRenderer('result', null, true);
//$this->_helper->redirector('result', 'index','', array('email' => $email, 'file' => $file));
$this->_helper->redirector->gotoRouteAndExit (array(
'controller' => 'index',
'action' =>'result',
'name' => $file));
} else {
$form->populate($formData);
}
}
}
public function resultAction() {
$email = $this->_getParam('name');
$this->view->email = $email;
}
}
My index view:
<?php
$this->form->setAction($this->url(array('action' => 'index')));
echo $this->form;
?>
My result view:
<?php
echo $this->email;
//echo the content of the csv file;
?>
My result view is empty always.
What is the correct/right way to get the form data, and if validation is successful, show the content in result view.
Edit: I have succeeded to get parameters passed to resultaction.
I just was looking into different file. That is why it was not showing up.
But still, is it the right way to do it for my case? For it seems not correct.
i have read this article http://yiiframework.com/wiki/2
and i have done everything step by step .
but it's not working !
this is the model which i just copy and then paste :
class Item extends CActiveRecord
{
public $image;
// ... other attributes
public function rules()
{
return array(
array('image', 'file', 'types'=>'jpg, gif, png'),
);
}
}
my view is exactly the same as the article :
$form = $this->beginWidget(
'CActiveForm',
array(
'id' => 'upload-form',
'enableAjaxValidation' => false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)
);
// ...
echo $form->labelEx($model, 'image');
echo $form->fileField($model, 'image');
echo $form->error($model, 'image');
// ...
echo CHtml::submitButton('Submit');
$this->endWidget();
and my controller is :
class ItemController extends CController
{
public function actionCreate()
{
$model=new Item;
if(isset($_POST['Item']))
{
$model->attributes=$_POST['Item'];
$model->image=CUploadedFile::getInstance($model,'image');
if($model->save())
{
$model->image->saveAs(dirname(__FILE__).'/a.txt');
// redirect to success page
}
}
$this->render('create', array('model'=>$model));
}
}
when i choose an image(jpg , png , or something else) the controller doesn't see my file , i mean isset($_FILES['Item']) is false ... i know it's false because i check it with var_dump hundreds of times :
public function actionCreate()
{
$model=new Item;
var_dump(isset($_POST['Item']));
...
i also test var_dump(isset($_FILES['Item'])) which was false either .
for every kinds of file(except plain txt file) $_POST['Item']) remains empty.
i checked my request using firebug network panel(both Firefox and chrome) and the request had the file .
i have already check this question , seems it's the same problem but the answers wasn't useful because CUploadedFile::getInstance and CUploadedFile::getInstanceByName are also return null for my case
what do you think ?
The Problem was not related to Yii Framework or my php code because i just test the same code on two different machine and that works just fine .
i am using wamp on my own computer and i guess the problem was something related to Apache or php configurations.
In controller change and use like this:
class ItemController extends CController
{
public function actionCreate()
{
$model=new Item;
if(isset($_POST['Item']))
{
$model->attributes=$_POST['Item'];
$uploadedFile = CUploadedFile::getInstance($model,'image');
if($model->save())
{
if(!empty($uploadedFile)) // check if uploaded file is set or not
{
if($model->image == null || empty($model->image)){
$rnd = rand(0,9999);// generate random number between 0-9999
$fileName = "{$rnd}-{$uploadedFile}";
$model->image = $fileName;
}
$uploadedFile->saveAs(dirname(__FILE__)..'/images/'. $model->image);
// redirect to success page
}
}
}
$this->render('create', array('model'=>$model));
}
}
everyone. Just like my post, how to create a file only once?
Here's FileController.php
public function actionCreate()
{
$model=new File;
if(isset($_POST['File']))
{
$simpan=$model->nama_file=CUploadedFile::getInstance($model,'nama_file');
if(empty($simpan)){
$model->attributes=$_POST['File'];
$model->save();
}
else{
$model->attributes=$_POST['File'];
$model->pengunggah=Yii::app()->user->id;
$model->nama_file = CUploadedFile::getInstance($model, 'nama_file');
if($model->save()){
$simpan->saveAs(Yii::app()->basePath .
'/../files/' . $model->nama_file.'');
//Yii::app()->user->setFlash('success', "Data berhasil disimpan!");
$this->redirect(array('view','id'=>$model->id_file));
}
}
}
$this->render('create',array(
'model'=>$model,
));
}
After a user create a file, he/she cannot create another, it only allows user to create only once. How to implement it? Thanks a lot
So here is what i am doing, i have created a dependent drop down in a form and i am validating it on my controller :
public function actionCreate()
{
$model=new Management;
if(isset($_POST['Management']))
{
if($_POST['Management']['company_id']==''){
//error please select a company
Yii::app()->user->setFlash('company_error',"Please select a company.");
$this->redirect(array('create'));
}else{
if($_POST['Management']['site_id']==''){
//error please select a site
Yii::app()->user->setFlash('site_error',"Please select a site.");
Yii::app()->user->setFlash('cid',$_POST['Management']['company_id']);
$this->redirect(array('create'));
}else{
//save
$model->attributes=$_POST['Management'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
}
}
$this->render('create',array(
'model'=>$model,'update'=>''
));
}
and i am passing the company id to my form view, and i want to keep the previous drop down selected if the validation fails. this is what my dropdown looks like :
<div class="row">
<?php echo $form->labelEx($model,'company_id'); ?>
<?php echo $form->dropDownList($model, 'company_id', $company, /*array('options'=>array($cid=>array('selected'=>true))),*/
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('Management/dropDownSite'),
'update'=>'#SectorManagement_site_id',
'data'=>array('CompanyId'=>'js:this.value'),
)));
?>
<?php echo $form->error($model,'company_id'); ?>
</div>
Now when i uncomment the code :
array('options'=>array($cid=>array('selected'=>true))),
from the above drop down it shows the pre-selected value but then the dependent drop down does not work..any idea how should i solve this. Thanks in advance. :)
I think you are looking at this the wrong way. If your validation fails, you redirect, while you are already on the page you are redirecting too.
I am not a 100% sure but what I think you are looking for in your action is
public function actionCreate()
{
$model = new Management;
if (isset($_POST['Management'])) {
$model->attributes = $_POST['Management'];
if ($_POST['Management']['company_id'] == '') {
//error please select a company
Yii::app()->user->setFlash('company_error', "Please select a company.");
} elseif ($_POST['Management']['site_id'] == '') {
//error please select a site
Yii::app()->user->setFlash('site_error', "Please select a site.");
Yii::app()->user->setFlash('cid', $_POST['Management']['company_id']);
} elseif ($model->save()) {
$this->redirect(array('view', 'id' => $model->id));
}
}
$this->render(
'create',
array(
'model' => $model,
'update' => ''
)
);
}
Your view can probably stay how it is.
To improve this even further you should handle validation in your model as in :
public function rules()
{
return array(
array('company_id, site_id', 'required'),
);
}
and then all you would need to do in your action is :
public function actionCreate()
{
$model = new Management;
if (isset($_POST['Management'])) {
$model->attributes = $_POST['Management'];
if ($model->validate() && $model->save()) {
$this->redirect(array('view', 'id' => $model->id));
}
}
$this->render(
'create',
array(
'model' => $model,
'update' => ''
)
);
}
I figured out finally, this is how it worked for me:
public function actionCreate()
{
$model=new Management;
if(isset($_POST['Management']))
{
if($_POST['Management']['company_id']==''){
//error please select a company
Yii::app()->user->setFlash('company_error',"Please select a company.");
$this->redirect(array('create'));
}else{
if($_POST['Management']['site_id']==''){
//error please select a site
Yii::app()->user->setFlash('site_error',"Please select a site.");
$model->company_id = $_POST['Management']['company_id'];
$this->render('create',array(
'model'=>$model,
'update'=>'',
));
exit;
}else{
//save
$model->attributes=$_POST['Management'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
}
}
$this->render('create',array(
'model'=>$model,'update'=>''
));
}