Getting Jquery to call Function In Hooks file

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
mrunne
Posts: 5
Joined: 2018-11-13 16:39

Getting Jquery to call Function In Hooks file

Post by mrunne » 2018-11-13 22:26

I have the following sql update working in my after_update function in my hooks php file.
/* This will move all equipment to users new desk and add all equipment at desk */
$id = $data['selectedID'];
$newSeat = $data['CubeNum'] ;
if ($id == '557'){
}
else{
sql("UPDATE `pnb_equip` SET `Location` = $newSeat WHERE `Assigned`= $id ", $eo);
sql("UPDATE `pnb_equip` SET `Assigned` = $id WHERE `Location`= $newSeat ", $eo);
}

This code works perfectly, except for the fact that I don't want it to run every time I update a Detail View record. With that being said, I created a Jquery script below that places a button on the detail view and it is working as expected. I know that by the working text alerts.
<script>
$j(function(){
<?php if($selectedID){ ?>
$j('#pnb_user_dv_action_buttons .btn-toolbar').append(
'<div class="btn-group-vertical btn-group-lg" style="width: 100%;">' +
'<button type="button" class="btn btn-warning btn-lg" onclick="moveEquipment()">' +
'<i class="glyphicon glyphicon-ok"></i> Move Equipment </button>' +
'</div>'
);
<?php } ?>
});
function moveequipment(){
var txt;
var r = confirm("You are moving all equipment to users current Seat");
if (r == true) {
alert('Completing Move');
}
else {
alert("Canceled")
}
}
</script>
The issue is how do I make the sql changes work by using the confirm popups?
Mr. Runne
Dev Engineer

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Getting Jquery to call Function In Hooks file

Post by pbottcher » 2018-11-14 07:37

Hi,

if you want to use jquery you would need to add an ajax call to a php file which would contain your SQL statement to be executed. You cannot directly call a function (not without additional work).

Just another idea:
Why dont you add a checkbox field that would indicate that you want to update your database with your mentioned SQL queries. In the after_update function you could react on the value of the checkbox and decide to run your SQL or not.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

mrunne
Posts: 5
Joined: 2018-11-13 16:39

Re: Getting Jquery to call Function In Hooks file

Post by mrunne » 2019-04-30 21:55

Changed the button to a checkbox, but still unable to do the Move equipment sql statements

<?php if($selectedID){ ?>
$j('#pnb_user_dv_action_buttons .btn-toolbar').append(
'<div class="form-check">' +
'<input type="checkbox" class="form-check" id="exampleCheck1" >' +
'<label class="form-check-label" for="exampleCheck1">Move All Equipment too?</label>'
);

<?php } ?>
});


is added to the dv function

And in the after_update

if (!empty($_GET['exampleCheck1'])) {
/* This will move all equipment to users new desk and add all equipment at desk */
$id = $data['selectedID'];
$newSeat = $data['CubeNum'] ;
if ($id == '557'){
}
else{
sql("UPDATE `pnb_equip` SET `Location` = $newSeat WHERE `Assigned`= $id ", $eo);
sql("UPDATE `pnb_equip` SET `Assigned` = $id WHERE `Location`= $newSeat ", $eo);
}


any help would be great
Mr. Runne
Dev Engineer

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Getting Jquery to call Function In Hooks file

Post by pbottcher » 2019-05-01 18:05

Hi,

you added a checkbox to the DV, but this checkbox is on the page only. It has no reference to the data you are submitting once you click on the save button.
The idea with the checkbox was, that you add it in AppGini to you database and use it once you press save and get that value.

i.e. you add the field move_equiment to your table and make it hidden on the TV and the DV .

Then you can use the code above and add

Code: Select all

$j('#exampleCheck1').on('change', function () { $j('#move_equiment').val($j('#exampleCheck1')[0].checked); });

this will write true or false into your field move_equiment.

in the after_update function you can check for this value and act accordingly.
Like:

Code: Select all

if ($data['move_equiment'] == 'true') {

$id = $data['selectedID'];
$newSeat = $data['CubeNum'] ;
if ($id == '557'){
}
else{
sql("UPDATE `pnb_equip` SET `Location` = $newSeat WHERE `Assigned`= $id ", $eo);
sql("UPDATE `pnb_equip` SET `Assigned` = $id WHERE `Location`= $newSeat ", $eo);

// set the fieldvalue back to false for the next update !!
sqlvalue("UPDATE pnb_user set move_equiment='false' where id =".$id");
}
}
Watch that in this case true and false are strings!

Hope that helps
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

mrunne
Posts: 5
Joined: 2018-11-13 16:39

Re: Getting Jquery to call Function In Hooks file

Post by mrunne » 2019-05-01 19:27

Is that the only way by updating the table in the DB? I really just want an easier way of referencing the if/then statement in the after_update code.
Mr. Runne
Dev Engineer

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Getting Jquery to call Function In Hooks file

Post by pbottcher » 2019-05-01 20:54

Well the other option would be to use ajax. But this would always happen bevor the save has really executed, so this might not be what you want.

You could initiate an ajax call once the user checks the checkbox and run your SQL, but what happens if the user does not store the data and aborts the editing?
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

mrunne
Posts: 5
Joined: 2018-11-13 16:39

Re: Getting Jquery to call Function In Hooks file

Post by mrunne » 2019-05-02 15:16

Nothing should happen at that point. The update should only happen if the checkbox is checked and the submit button is clicked. Could an ajax call work under those conditions?
Mr. Runne
Dev Engineer

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Getting Jquery to call Function In Hooks file

Post by pbottcher » 2019-05-02 17:52

Nope, you would have to create some logic to handle the ajax call in combination with the after_update hook and all possible option that a user could do.
I would go for the other option.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

mrunne
Posts: 5
Joined: 2018-11-13 16:39

Re: Getting Jquery to call Function In Hooks file

Post by mrunne » 2019-08-19 18:58

So I added the hidden checkbox and SQL column or field. My issue is the code is not working as expected. Currently when I dump the data array my SQL updates is not showing. Any ideas?
===
code
====
function pnb_user_dv($selectedID, $memberInfo, &$html, $data){

/* if this is the print preview, don't modify the detail view */
if(isset($_REQUEST['dvprint_x'])) return;

ob_start(); ?>

<script>
$j(function(){
<?php if($selectedID){ ?>
$j('#pnb_user_dv_action_buttons .btn-toolbar').append(
'<div class="form-check">' +
'<input type="checkbox" class="form-check" id="exampleCheck1" >' +
'<label class="form-check-label" for="exampleCheck1">Move All Equipment too?</label>'
);

<?php } ?>
});
</script>
<script>
$j('#exampleCheck1').on('change', function () { $j('#move_equiment').val($j('#exampleCheck1')[0].checked); });
</script>

<?php
$form_code = ob_get_contents();
ob_end_clean();

$html .= $form_code;
}
===
Array dump
===
Array
(
[UsrId] => 557
[FirstName] => Floor
[MiddleInitial] =>
[LastName] => Stock
[EmailAddress] => [email protected]
[AlternatePhone] =>
[MobilePhone] => --- --- _---
[Status] => Employee
[CubeNum] => 12
[VZWEB] =>
[Manager] =>
[Exec] =>
[UsrManager] => 246
[UsrDirector] => 184
[Move_Equipment] =>
[selectedID] => 557
)
Mr. Runne
Dev Engineer

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Getting Jquery to call Function In Hooks file

Post by pbottcher » 2019-08-21 17:30

Hi,

you need to show your field move_equiment in AppGini in the detail view, but hide it in you actual DV via jquery.

You can try this.

Code: Select all

function pnb_user_dv($selectedID, $memberInfo, &$html, $data){

/* if this is the print preview, don't modify the detail view */
if(isset($_REQUEST['dvprint_x'])) return;

ob_start(); ?>

<script>
$j(function(){
<?php if($selectedID){ ?>
$j('#move_equiment').parents(".form-group").hide();   // this will hide the extra field (plus label in the detail view
$j('#pnb_user_dv_action_buttons .btn-toolbar').append(
'<div class="form-check">' +
'<input type="checkbox" class="form-check" id="exampleCheck1" >' +
'<label class="form-check-label" for="exampleCheck1">Move All Equipment too?</label>'
);
$j('#exampleCheck1').on('change', function () { $j('#move_equiment').val($j('#exampleCheck1')[0].checked); });
<?php } ?>
});
</script>	

<?php
$form_code = ob_get_contents();
ob_end_clean();

$html .= $form_code;

	}
Now you should get either false or empty in the move_equiment if the checkbox was never checked, or unchecked
or you will get true if the checkbox is checked.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

Post Reply