Using Bootstrap Modal for ZfcUser in ZF2? - php

as i am a complete newbie to Zend Framework this might be a total beginner question:
I did some tutorials and now i have a Zend-Framework skeleton-application with ZfcUser (and ZfcBase) up and running.
Everything works fine so far, but what i want to accomplish is that the Login and Registration opens up in a Bootstrap-Modal-Dialog.
So now i see that in ./vendor/ZfcUser/config/module.config.php you can define the routes, but i have no idea what to do, when i want the whole dialogs being "served" with the index of my main-application (i guess i will need this to let the login dialog open up from the main menu from anywhere in the application).
So can someone help me with getting this to work? I really have no idea how to start at all and any help is highly appreciated :)
best regards

If you want to use Bootstrap Model for ZfcUser login, You need to change approach of login a bit.
You should use ZfcUser API Calls to validate login instead of post data on /user/login page.
You need to override the ZfcUser login.html to change form action button from submit to simple button/link binded with ajax request.
Call ZfcUserLoginWidget into Bootstrap Model's body
Form/ajax action set to your custom auth page
Where ZfcUser API Calls validate & return json response with success/failure.
ZfcUser validation no more work automatically, You need to apply through jQuery, Javascript from Json Response.
So First copy vendor/ZfcUser/view/user/login.html to module/[YourModule]/view/zfc-user/user/login.phtml
Now replace submit button with normal button like that :
<input type="button" value="Sign in" onClick="javascript:verifyLogin(this.form);" />
--
--
<script type="text/javascript">
function verifyLogin(frm)
{
var data = $(frm).serialize();
$.ajax({
type: "POST",
url: "<?php echo $this->url('authurl') ?>",
data: data,
success: function(resp){
alert(resp.status);
},
error: function(resp){
},
dataType: 'json'
});
}
</script>
You should add a route for authurl for YourController/authAction
Add your html for Bootstrap model on parent template view :
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Login Box
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<?php echo $this->zfcUserLoginWidget(); ?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
Now YourController/authAction code should work like that :
$request = $this->getRequest();
$data = $request->getPost();
$this->getRequest()->getPost()->set('identity', $data['identity']);
$this->getRequest()->getPost()->set('credential', $data['credential']);
$this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters();
$this->zfcUserAuthentication()->getAuthService()->clearIdentity();
$adapter = $this->zfcUserAuthentication()->getAuthAdapter();
$adapter->prepareForAuthentication($this->getRequest());
$auth = $this->zfcUserAuthentication()->getAuthService()->authenticate($adapter);
if (!$auth->isValid()) {
//$this->flashMessenger()->setNamespace('zfcuser-login-form')->addMessage($this->failedLoginMessage);
$adapter->resetAdapters();
$response_data = array(
'status' => 'Failure'
) ;
}
else
{
$response_data = array(
'status' => 'OK'
) ;
}
$response = $this->getResponse();
$response->setStatusCode(200);
$response->setContent(json_encode($response_data));
return $response;

The Login Widget viewhelper is probably what you are looking for. Within your desired .phtml template just add the viewhelper to render the login form.
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">× </button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<?php //Add the Widget to the Modal Body here!?>
<?php echo $this->zfcUserLoginWidget(); ?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
If you want to alter the template of the widget you'll need to edit/add the zfcuser.global.php in your ../config/autoload/ folder and change/uncomment this the following to your desired login-widget template.
'user_login_widget_view_template' => 'your_module/whatever/login.phtml',

Related

Codeigniter - Show Information in Modal Window

I have a Modal Window. It is working good with javascript. I tried something myself but I can't show any information of Customer in Modal Window. When clicking on the info button, I want to show the Customer's information. How can I show any customer information in Modal Window?
Controller:
public function index()
{
$viewData['countries'] = $this->db->get("country")->row();
$viewData['customers'] = $this->customer_model->get_all();
$this->lang->load('content', $this->session->userdata('people_lang'));
$this->load->view('customers', $viewData);
}
public function getInfo(){
$cusId = $this->input->post('cusId');
$viewData['customers'] = $this->db->where("cusId", $cusId)->get("customer")->row();
$this->load->view('customers/getInfo', $viewData);
}
This Ajax Code:
<script type="text/javascript">
function showCustomers(str){
$.ajax({
type: "POST",
data: { cusId: str },
url: "<?=base_url('customers/getInfo');?>",
success: function(data) {
$('#divInfo').html(data);
}
});
}
</script>
This Modal:
<!-- Modal -->
<div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Modal Tittle</h4>
</div>
<div class="modal-body">
<div id="divInfo"></div>
</div>
<div class="modal-footer">
<button data-dismiss="modal" class="btn btn-default" type="button">Close</button>
<button class="btn btn-warning" type="button"> Confirm</button>
</div>
</div>
</div>
</div>
<!-- modal -->
This View:
<a data-toggle="modal" href="#myModal3"><button class="btn btn-primary btn-xs" onclick="showCustomers(this.id);" id="<?php echo $customers->cusId; ?>"><i class="fa fa-eye"></i></button>
But This is not working. Where is I do mistake?
I think you can do this by adding a couple of lines.
Note* Code is not tested, i can practically painting the idea to solve this.
In the index function of your controller
public function index(){
$viewData['countries'] = $this->db->get("country")->row();
$viewData['customers'] = $this->customer_model->get_all();
//**new line
// Assuming you already have method in your Model to get customer by id
// Check if there's a GET request of 'customerID' then assign to the $viewDataByID array the data from the model, else nothing.
$viewDataByID['customersById'] = (isset($_GET['cusomterID'])) ? $this->customer_model->get_by_id($id) : '';
//Then you'll have to update the variable sent to the view by storing both in one array
$allData['viewData']= $viewData;
$allData['viewDataByID'] = $viewDataByID;
$this->lang->load('content', $this->session->userdata('people_lang'));
$this->load->view('customers', $allData);// $allData get sent to the view
}
In your modal you can sent the request
Please update the variables in the view. You can do a print_r($allData) or var_dump($allData) to clearly see the data tree and how you can make use of them.
Alternatively if you do not want to use your index for this, you can create another function within your controller to handle the customer by id, json_encode the result and then use jQuery to get the data.
Hope this helps.
Here's one way you could do it:
<!--modify button to include an onclick element, pass a value to it-->
<button class="btn btn-primary btn-xs" id="info_modal" onclick="show_modal('<?=$customer_name;?>')"><i class="fa fa-eye"></i></button>
You could modify your modal to have some id's in anticipation of your data:
<div class="modal fade" id="info_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
....
<div class="modal-body">
View File List According to Customer ID.
<div id="customer_name"></div>
</div>
Then in your script (if using jquery):
//add passed string to the modal and then display it
function show_modal(customer_name){
$("#customer_name").html(customer_name);
$("#info_modal").modal('show');
}
I answered myself this question.
<td data-title="Edit" align="center"><button class="btn btn-primary btn-xs"><i class="fa fa-pencil"></i></button> <a data-toggle="modal" href="#<?php echo $customer->cusId; ?>"><button class="btn btn-primary btn-xs"><i class="fa fa-eye"></i></button></a></td>
I edit a href like that href="#<?php echo $customer->cusId; ?>
Afterthat I just write this code on modal top:
<?php foreach ($customers as $get) { ?>
<div class="modal fade" id="<?php echo $get->cusId; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<?php } ?>
I edit id like that id="#<?php echo $get->cusId; ?>
It works better now!

How to show bootstrap modal dialog in vue js?

I have been trying to show modal dialog using bootstrap and vue js. I want to show dialog once user successfully registers.
<div>
....
<div class="modal fade" v-if="isEnabled">
<div class="modal-dialog" role="dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Modal body text goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
....
</div>
But not able to display the dialog. Can anybody help me to do this?
The modal component is managed by jQuery, you have to use jquery to open your modal.
I need you Vue code too help you much, if it's an ajax request to register user, wait the success callback to make a
$('#modal').modal('show')
If it's a synchronous process, call you modal in the mounted method of you Vue instance.
for eg:
if you want to show modal box on click button then use
<button type="button" data-toggle="modal" data-target="#myModal">Open Modal</button>
and if you want to open model box after any process is finished then use
$('#modal').modal('show');
"#myModal" is ID of the modal box you want to show.
more info https://www.w3schools.com/bootstrap/bootstrap_modal.asp

How to load an external PHP file inside modal pop-up?

Scenario - I am planning to load an external PHP file which contains an Update form inside a modal pop-up on a jquery button click event.
I am able to load pop-up on button click but could not find a clear and brief solution to load an external file inside the modal pop-up. For the sake of example lets assume the edit form has just email text field.
NOTE- I am trying to load file locally
JS Fiddle - jsfiddle.net/1aeur58f/1
I want to display the file content inside the body of the pop-up as shown in the JsFiddle
So, I do not think that you have to load an external source at all. If you would like to include the content in the page, I think that you should do something like I did below.
<?php
$user = $client->get('account');
?>
<!-- Make the model show up -->
<!-- Pretend this is the form -->
<button type="button" id="mymodal" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel"><?= 'Some kind of title' ?></h4>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>

Should I (and how) run a php script with modal bootstrap?

I have created a phpscript - calculator it's here
How to optimize this short script in php - salary calc
and it is working.
Now I would like to do sth like this. A person on my web page clicks the button, the calculator opens in sth like little window, he uses calculator and when he finishes, closes the window with calculator and is on the webpage again. As I understand I should do it with bootstrap modal like this: http://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_modal_sm&stacked=h
Do I understand it right?
How should I link my phpscript with this modal?
thank You for your hints?
what you need is an Ajax method with modal event listener,
Modal HTML
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Small Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<div id="showcal"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
B.S Modal event listener with Ajax Method
$(document).ready(function(){
$('#myModal').on('shown.bs.modal', function () {
$.ajax({
type: 'GET',
url: "calculator.php", //this file has the calculator function code
success:function(data){
$('#showcal').html(data);
}
});
});
});
and in last calculator.php
<?php
//Here comes the calculator code
?>
Lemme know if it worked and if not will make it work.

how can i send parameters with <a> for set query MySQL in smarty & Php

i want create grid with bootstrap in smarty / php
in grid i have name, family, ... & edit button.
when i click in btn edit, modal popup open, but i don't know how sent record ID to function for set query & show in modal:
sample code:
www.bootply.com/webdeveloper/iQrK1Yxlkk
btn:
<a href="" class="btn btn-success" data-toggle="modal" data-target="#myModal" data-backdrop="static">
Edit User
</a>
Modal:
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
username: ...
<br>
Password: ...
<br>
avatar: ...
<br>
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
The simpliest solution:
remove attributes data-toggle="modal" and data-target="#myModal"
Add data-id attribute for tag a
Add bind jquery to click for tag a
when user click to tag a:
Insert record ID into hidden text input in modal form
Run bootstrap modal manually $('#myModal').modal()
Hope it will help
EDIT:
Also in documentation i see special event when modal run (probably it is more bootstrap.js way...):
$('#myModal').on('show.bs.modal', function (e) {
// get record ID here
})
so you can use e.target to get attributes of tag a

Categories