Yii update from modal window - php

I need to update a modal window and show it when I click in the edit button.
CONTROLLER:
public function actionUpdateAjax($id)
{
$contratos = ZfContratos::model()->findByPk($id);
$this->renderPartial('//ZfContratos/_form_update', array('model'=>$contratos), false, true);
}
INDEX:
<?php $this->beginWidget(
'bootstrap.widgets.TbModal',
array('id' => 'actualizar_contrato')
); ?>
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h4>Actualizar contrato</h4>
</div>
<div class="modal-body">
<?php $this->renderPartial('//ZfContratos/_form_update', array('model'=>$contrato));?>
</div>
<div class="modal-footer">
<?php $this->widget(
'bootstrap.widgets.TbButton',
array(
'label' => 'CANCELAR',
'url' => '#',
'htmlOptions' => array('data-dismiss' => 'modal'),
)
); ?>
</div>
<?php $this->endWidget(); ?>
AND VIEW _contratos
At this moment I have this:
<?php echo CHtml::link('EDITAR', array('//ZfInmuebles/UpdateAjax', 'id'=>$data->zf_contrato_id), array('class'=>'btn', 'id'=>'vermas')); ?>
But I need that to be an ajaxbutton or ajaxlink, that refresh the div "actualizar_contrato" and show it.

This is just a reference implementation to give you an idea of how to do this, do modify as per your requirements
// your view
// Button to trigger modal
<?php echo CHtml::ajaxLink('EDITAR',array('//ZfInmuebles/UpdateAjax', 'id'=>$data-> zf_contrato_id), array(
'type'=>'POST',
"success"=>'js:function(html){
$("#actualizar_contrato >.modal-body").html(html);
$("#actualizar_contrato").modal("show");
}
'));?
.....
// modal
<?php $this->beginWidget(
'bootstrap.widgets.TbModal',
array('id' => 'actualizar_contrato')
); ?>
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h4>Actualizar contrato</h4>
</div>
<div class="modal-body">
<?php
$contrato = new ZfContratos;
$this->renderPartial('//ZfContratos/_form_update', array('model'=>$contrato));
?>
</div>
<div class="modal-footer">
<?php echo CHtml::ajaxLink('Save', // Link Text
array('//ZfContratos/update', 'id'=>$data->zf_contrato_id), //url
array(// ajaxOptions
'data'=> 'js:$("#id_of_the_form").serialize()' ,
'type'=>'POST',
'success'=>'js:function(html){$("#actualizar_contrato >.modal-body").html(html) }'
)
)
);?>
<?php $this->widget(
'bootstrap.widgets.TbButton',
array(
'label' => 'CANCELAR',
'url' => '#',
'htmlOptions' => array('data-dismiss' => 'modal'),
)
); ?>
</div>
<?php $this->endWidget(); ?>
.....
And now in your controller modify as like below
//ZfContratos Controller
public function actionUpdate(){
// Your regular logic for saving
if(Yii::app()->request->isAjaxRequest){
if($model->save()){
$this->renderPartial("_mvsavedView",array('model'=>$model)) //OR echo "Saved Successfully";
} else {
print_r($model->getErrors());
}
Yii::app()->end();
}
}

Related

How to make TbModal work properly in PhP (Yii)

I am looking to make a simple Pop-Up window using TbModal (as requested by my superiors). However, I have no clue how to do this. I have never written a modal before and I am not certain if I am doing it completely wrong.
All I need to do, is to create a Pop-up window once a button is clicked. In this window I need to simply add a name, a description and something that is called carousel. However, whenever I try to make it work I get the following error:
Alias "bootstrap.widgets.TbModal" is invalid. Make sure it points to an existing directory or file.
Below is what the button in my view looks like (using a TbButtonColumn):
'buttons'=>array(
'update'=>array(
),
'delete'=>array(
),
'clone'=>array(
'id'=>'clone-dashboard-button',
'label'=>'Clone',
'title'=>'Clone',
'url'=>'Yii::app()->createUrl("path/to/actionClone", array("id"=>$data->id))',
'option'=>array(
'rel'=>'tooltip',
'data-toggle'=>'tooltip',
'title'=>'Clone'
),
),
),
As well as the modal:
<?php $this->beginWidget('bootstrap.widgets.TbModal', array(
'id'=>'clone-form',
// 'focus'=>'#Dashboard_clone_title',
'htmlOptions'=>array(
'tabindex'=>'-1',
))); ?>
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h4>Test</h4>
</div>
<div class="model-body">
<div class="row">
<div class ="col-md-2">
<?php echo $form->labelEx($model,'title'); ?>
<?php echo $form->textArea($model,'title', array('rows'=>1, 'cols'=>20,'class'=>'resize-non form-control')); // Title input?>
</div>
<div class ="col-md-2">
<?php echo $form->labelEx($model,'description'); ?>
<?php echo $form->textArea($model,'description', array('rows'=>1, 'cols'=>30,'class'=>'resize-non form-control')); //Description ?>
</div>
<div class ="col-md-2">
<?php echo $form->labelEx($model,'carousel'); ?>
<?php echo $form->checkbox($model,'carousel'); ?>
</div>
</div>
</div>
<div class="modal-footer">
<!--Buttons-->
<div class="pull-right">
<?php $this->widget('booster.widgets.TbButton', array(
'buttonType'=>'Clone',
'url'=>'Yii::app()->createUrl("path/to/actionClone", array("id"=>$data->id))',
'htmlOptions'=>array(
'onClick'=>"
$(this).attr(\"disabled\", \"disabled\");"
),
)); ?>
<?php $this->widget('booster.widgets.TbButton', array(
'buttonType'=>'button',
'label'=>'Cancel',
'htmlOptions'=>array(
'data-dismiss'=>'modal',
),
)); ?>
</div>
</div>
<?php $this->endWidget(); ?>

How to put Glyphicons Icons in input?

How can I add a Glyphicons Icons in the input below?
A part of the code:
class GrupoUsuarioForm{
$this->setAttributes(array(
'method' => 'POST',
'class' => 'formGrupoUsuario',
'name' => 'formGrupoUsuario'
));
$this->setInputFilter(new GrupoUsuarioInputFilter());
$dataCadastro = new Text('data_grp');
$dataCadastro->setValue(date('d/m/Y'))
->setAttributes(array(
//'style' => 'width: 20%',
'id', 'dataGrp',
'readOnly' => 'true'
));
$dataCadastro->setLabel('Data do Cadastro');
$this->add($dataCadastro);
Would like this return HTML
<label>Textbox with calendar icon</label>
<div class="input-append"><input type="text" id="" name="">
<span class="add-on"><i class="icon-calendar"></i></span></div>
Where excatly do you want put this icon?
You can render form's elements as you want.
In controller:
public function myAction()
{
return new ViewModel([
'form' => new GrupoUsuarioForm()
]);
}
In your view:
// HTML code here
...
<?php $form = $this->form; ?>
<?php $form->prepare(); ?>
<?php echo $this->form()->openTag($form); ?>
...
<?php echo $this->formLabel($form->get('data_grp')); ?>
<div class="input-group">
<div class="input-append">
<?php echo $this->formInput($form->get('data_grp')); ?>
<span class="add-on"><i class="icon-calendar"></i></span>
</div>
</div>
...
<?php echo $this->form()->closeTag(); ?>

If I do not update any information in my update form and click "Save" button,the valu of my database auto increase

I am in the learning stage in php.Please help me with my update form.Advanced thanks. I have a form to update any user information with a button "Save" and "Reset" button.I update any information the version number increase.But the problem is when i do not update any information and click on "Save" button the version number increases.It should not increase.I am giving my Controller and form code....
public function actionCreate()
{
$model=new CvUpload;
$model2=new CvUpload;
$UserType=new UserType;
$CvHistory=new CvHistory;
$this->performAjaxValidation($model);
if(!empty($_POST))
{
$id = $_POST['CvUpload']['user_id'];
$cv_type = $_POST['CvUpload']['cv_type'];
$criteria = new CDbCriteria();
$criteria->condition = "user_id = $id AND is_current='yes' AND cv_type='$cv_type'";
$CvUploadmodel = CvUpload::model()->find($criteria);
if(!empty($CvUploadmodel))
{
$CvUploadmodel->is_current='no';
$CvUploadmodel->status='inactive';
if($CvUploadmodel->save(false)){
$model->attributes=$_POST['CvUpload'];
$model->upload_date = new CDbExpression("NOW()");
$model->update_date = new CDbExpression("NOW()");
$model->version_id = $CvUploadmodel->version_id+1;
if($model->save(false))
{
$CvHistory->cv_id = $model->cv_id;
$CvHistory->user_id = $model->user_id;
$CvHistory->job_title_id = $model->job_title_id;
$CvHistory->cv_type = $model->cv_type;
$CvHistory->version_id = $model->version_id;
$CvHistory->file_name = $model->file_name;
$CvHistory->upload_date = $model->upload_date;
$CvHistory->update_date = $model->update_date;
$CvHistory->is_current = $model->is_current;
$CvHistory->status = $model->status;
if($CvHistory->save(false))
{
$this->redirect(array('view','id'=>$model->cv_id));
}
}
}
}
else
{
$model->attributes=$_POST['CvUpload'];
$model->upload_date = new CDbExpression("NOW()");
$model->update_date = new CDbExpression("NOW()");
$model->version_id=1;
if($model->save())
{
$CvHistory->cv_id = $model->cv_id;
$CvHistory->user_id = $model->user_id;
$CvHistory->job_title_id = $model->job_title_id;
$CvHistory->cv_type = $model->cv_type;
$CvHistory->version_id = $model->version_id;
$CvHistory->file_name = $model->file_name;
$CvHistory->upload_date = $model->upload_date;
$CvHistory->update_date = $model->update_date;
$CvHistory->is_current = $model->is_current;
$CvHistory->status = $model->status;
if($CvHistory->save(false))
{
$this->redirect(array('view','id'=>$model->cv_id));
}
}
}
}
$this->render('create',array(
'model'=>$model,
'UserType'=>$UserType,
'CvHistory'=>$CvHistory,
));
}
public function actionUpdate($id)
{
$model=$this->loadModel($id);
$CvHistory=new CvHistory;
if(isset($_POST['CvUpload']))
{
$model->attributes=$_POST['CvUpload'];
if($model->save())
{
$criteria = new CDbCriteria();
$criteria->condition = "cv_id = $model->cv_id AND is_current = 'yes'";
$CvHistory = CvHistory::model()->find($criteria);
$CvHistory->is_current = 'no';
$CvHistory->status = 'inactive';
if($CvHistory->save(false))
{
$new_CvHistory=new CvHistory;
$new_CvHistory->cv_id = $model->cv_id;
$new_CvHistory->user_id = $model->user_id;
$new_CvHistory->job_title_id = $model->job_title_id;
$new_CvHistory->cv_type = $model->cv_type;
$new_CvHistory->version_id = $CvHistory->version_id+1;
$model->version_id = $new_CvHistory->version_id ;
$new_CvHistory->file_name = $model->file_name;
$new_CvHistory->upload_date = $model->upload_date;
$new_CvHistory->update_date = new CDbExpression("NOW()");
$model->update_date = $new_CvHistory->update_date ;
$new_CvHistory->status = $model->status;
$new_CvHistory->is_current = "yes";
$new_CvHistory->save(false);
$model->save(false);
if($new_CvHistory->save(false))$this->redirect(array('view','id'=>$model->cv_id));
}
}
}
$this->render('update',array(
'model'=>$model,
'CvHistory'=>$CvHistory,
));
}
MY form button code:
<style>
#type {
background-color:#f5f5f5;
font-family: 'Arial';
font-size: 20px;
text-decoration: none;
color:#b2b2b2;
border:none;
float: right;
}
div.form label {
display: block;
font-size: 1em;
font-weight: bold;
}
.types-add{
float: right;
}
#type:hover {
border: none;
}
</style>
<?php if (Yii::app()->user->hasFlash('created')): ?>
<div class="flash-success">
<?php echo Yii::app()->user->getFlash('created'); ?>
</div>
<?php endif; ?>
<div class="form">
<?php $form=$this->beginWidget(
'booster.widgets.TbActiveForm',
array(
'id'=>'CvUpload-form',
'type' => 'horizontal',
'htmlOptions' => array(
'class' => 'well',
'enctype' => 'multipart/form-data',
)
));
?>
<fieldset>
<h1 align="center">CV Upload</h1>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php
$criteria=new CDbCriteria();
echo $form->dropDownListGroup(
$model,
'user_id',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => CHtml::listData(User::model()->findAll($criteria), 'id', 'user_id'),
'dataProvider'=>$model->searchByUserId(Yii::app()->user->getId()),
'htmlOptions' => array('prompt'=>'Select'),
)
)
); ?>
</div>
<div class="row" id="jobTitle">
<?php
$criteria = new CDbCriteria;
$criteria->condition = "status= 'active'";
echo $form->dropDownListGroup(
$model,
'job_title_id',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => CHtml::listData(JobTitle::model()->findAll($criteria), 'id', 'name'),
'htmlOptions' => array('prompt'=>'Select job title'),
)
)
); ?>
</div>
<?php echo CHtml::form($this->createUrl('uploadreport'), 'post', array('enctype'=>'multipart/form-data'));?>
<label class="col-sm-3 control-label required" for="CvUpload_file_name">
Upload File
</label>
<div class="row" id="file_upload" style="margin-left:20%; margin-bottom:2%;">
<?php
$this->widget('CMultiFileUpload',array(
'name' => 'files',
'accept' => 'doc|docx|pdf',
'max' => 1,
'htmlOptions' => array('size' => 25),
));
echo CHtml::htmlButton('Upload',array(
'onclick'=>'javascript: send();', // on submit call JS send() function
'id'=> 'post-submit-btn', // button id 'newcreate'
'class'=>'btn btn-primary',
));
?>
</div>
<?php Yii::app()->clientScript->registerCoreScript("jquery"); ?>
<script src="http://code.jquery.com/jquery-migrate-1.2.0.js"></script>
<script>
function send(){
var formData = new FormData($("#CvUpload-form")[0]);
$.ajax({
url: '<?php echo Yii::app()->createUrl("CvUpload/upload"); ?>',
type: 'POST',
data: formData,
datatype:'json',
beforeSend: function() {
},
success: function (data) {
$("#CvUpload_file_name").val(data);
},
complete: function() {
},
error: function (data) {
alert("There may a error on uploading. Try again later");
},
cache: false,
contentType: false,
processData: false
});
return false;
}
</script>
<div class="row" id="file_name">
<?php echo $form->textFieldGroup(
$model,'file_name',
array(
'wrapperHtmlOptions' => array(
'class'=> 'col-sm-5',
),
)
);
?>
</div>
<div class="row" id="statustype">
<?php
$status = array('active'=>'Active', 'inactive'=>'Inactive');
echo $form->dropDownListGroup(
$model,
'status',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => $status,
'htmlOptions' => array('prompt'=>'Select a status'),
)
)
); ?>
</div>
<div class="row" id="statustype">
<?php
$is_current = array('yes'=>'Yes', 'no'=>'No');
echo $form->dropDownListGroup(
$model,
'is_current',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => $is_current,
'htmlOptions' => array('prompt'=>'Select a status'),
)
)
); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save',array(
'class'=>'btn btn-primary',
'style'=>"position:relative; margin-left:25%",
'id'=> 'yt1', // button id
)); ?>
</div>
</fieldset>
<?php $this->endWidget(); ?>
</div><!-- form -->
<!--/*/*
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'cv-upload-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'user_id'); ?>
<?php echo $form->textField($model,'user_id',array('size'=>20,'maxlength'=>20)); ?>
<?php echo $form->error($model,'user_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'upload_date');enter code here ?>
<?php echo $form->textField($model,'upload_date'); ?>
<?php echo $form->error($model,'upload_date'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'file_name'); ?>
<?php echo $form->textField($model,'file_name',array('size'=>60,'maxlength'=>150)); ?>
<?php echo $form->error($model,'file_name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'file_location'); ?>
<?php echo $form->textField($model,'file_location',array('size'=>60,'maxlength'=>150)); ?>
<?php echo $form->error($model,'file_location'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
If you don't change form values, it doesn't mean that this data wouldn't be sended to server.
Here at least button save will sended, so this case will always true:
if(!empty($_POST))
You didn't show whole form, but I suggest that fields user_id and cv_type has values (no mater values where changed or not, if form has fields they will be submited)
$id = $_POST['CvUpload']['user_id'];
$cv_type = $_POST['CvUpload']['cv_type'];
That's why model $CvUploadmodel = CvUpload::model()->find($criteria); loads and increament version_id

how to get value from previous view in Yii

I'm not sure how to describe my question. First I added a button at my CCGridview:
array(
'class'=>'CButtonColumn',
'template' => '{view}{update}{delete}{upload_image}',
'buttons' => array(
'upload_image' => array(
'label' => 'upload foto',
'url' => 'Yii::app()->createUrl("/image/create",
array("product_id" => $data->product_id))',
),
),
),
when I clicked it will bring me to the /image/create view which has a product_id value. For example on that gridview I clicked record number 7, so the url would be:
(webApp)/index.php/image/create?product_id=7
Since it rendering a partial _form so the form has the attribute according to the image table which has this attributes: id, title, filename, product_id.
So the view will be something like:
<div class="row">
<?php echo $form->labelEx($model,'title'); ?>
<?php echo $form->textField($model,'title',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($model,'title'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'filename'); ?>
<?php echo $form->fileField($model,'filename',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($model,'filename'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'product_id'); ?>
<?php echo $form->textField($model,'product_id'); ?>
<?php echo $form->error($model,'product_id'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
My question is, how do we use the value from the url which I mentioned before is 7 (../create?product_id=7) into the product_id attribute without have to type it at provided textField?
In other word I will remove this from the view:
<div class="row">
<?php echo $form->labelEx($model,'product_id'); ?>
<?php echo $form->textField($model,'product_id'); ?>
<?php echo $form->error($model,'product_id'); ?>
</div>
But when i submitted, the form the value (7) should have been passed/saved at product_id field.
Added:
My controller actionCreate is
//...
public function actionCreate()
{
$dir = Yii::app()->basePath . '/../productimages/';
$uploaded = false;
$model=new Image();
if(isset($_POST['Image']))
{
$model->attributes=$_POST['Image'];
$tempSave=CUploadedFile::getInstance($model,'filename');
if($model->validate())
{
$uploaded = $tempSave->saveAs($dir.'/'.$tempSave->getName());
$this->redirect(array('/products/index'));
}
}
$this->render('index', array(
'model' => $model,
'uploaded' => $uploaded,
'dir' => $dir,
));
}
That's it. Many thanks..
modify your controller this way:
if(isset($_POST['Image']))
{
$model->attributes=$_POST['Image'];
$tempSave=CUploadedFile::getInstance($model,'filename');
if($model->validate())
{
$uploaded = $tempSave->saveAs($dir.'/'.$tempSave->getName());
$this->redirect(array('/products/index'));
}
} else {
//initialize defaults
$model->product_id=intval($_GET['product_id']);
}

Yii Ajax Update depending dropdown selection

I have dropdown list where there are five or six items (here items are services). I used here ajax request which updates the id “package_id” from the URL 'url'=>$this->createUrl('servicePackage'), once I select any service item. Till now, I have done successfully. But I want that ajax should use different URL and update different id i.e. “registrationid” if I select 4th number item, otherwise it should update “package_id”.
Is it possible?
Is it possible?
The form is:
<div class="form-group">
<?php echo $form->labelEx($model,'service_id', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php echo $form->dropDownList($model,'service_id',CHtml::listData(Service::model()->findAll(),'id', 'name'),
array(
'class' => 'form-control',
'prompt'=>'Select a Service',
'ajax' => array('type'=>'POST',
'url'=>$this->createUrl('servicePackage'),
'update'=>'#'.CHtml::activeId($model, 'package_id'), // ajax updates package_id, but I want ajax update registration_id if I select item no 4
'data'=>array('service_id'=>'js:this.value'),
)));
?>
</div>
<?php echo $form->error($model,'service_id'); ?>
</div>
<div class="form-group" id="packageid">
<?php echo $form->labelEx($model,'package_id', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php
echo $form->dropDownList($model,'package_id',$model->getServicePackage($model->service_id),array(
'class' => 'form-control','prompt'=>'Select a Package',
'ajax' =>
array('type'=>'POST',
'url'=>$this->createUrl('packagePrice'), //url to call.
'update'=>'#price', //selector to update
'data'=>array('package_id'=>'js:this.value'),
)));
?>
</div>
<?php echo $form->error($model,'package_id'); ?>
</div>
<div class="form-group" id="registrationid">
<?php echo $form->labelEx($model,'registration_id', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php
$registrants = Registration::model()->findAll();
$data = CHtml::listData($registrants,'id', function($registrants) {
return CHtml::encode($registrants->name.' ('.$registrants->id.')');
});
echo $form->dropDownList($model,'registration_id',$data,
array(
'class' => 'required form-control chzn-select',
'prompt'=>'Select a Registrant',
'ajax' => array('type'=>'POST',
'url'=>$this->createUrl('Order'), //url to call.
'update'=>'#'.CHtml::activeId($model, 'order_id'), //selector to update
'data'=>array('registration_id'=>'js:this.value'),
)
));
?>
</div>
<?php echo $form->error($model,'registration_id'); ?>
</div>

Categories