Adding action buttons to detail view

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
balfons
Veteran Member
Posts: 91
Joined: 2018-10-22 15:27

Adding action buttons to detail view

Post by balfons » 2021-10-19 13:53

Hi!

I'm working in a project that is in need of having custom action buttons in detail view so we can perform some custom actions. I watched the course that made Ahmed at Udemy and copied the example. My question is that, even I succeed in displaying buttons and creating a pair of custom actions (just printing an alert), I still have a problem with more elaborated actions.

Here is my code in hooks\rh_people.php (rh_people is the table what I am working with). I'll explain what it was supposed to do (but it doesn't) after:

Code: Select all

function rh_people_dv($selectedID, $memberInfo, &$html, &$args) {
		
		ob_start(); ?>
		
		<script>
			$j(function(){
				<?php if($selectedID){ ?> //per a que els botons no es mostren per un registre nou
					$j('#appginihelper-dv-actionbuttons').append(
						'<p></p>' +
						'<div class="btn-group-vertical btn-group-lg" style="width: 100%;">' +
							'<button type="button" name="activar" class="btn btn-success" onclick="activar_persona()">' +
								'<i class="glyphicon glyphicon-plus"></i> <span> Activar</span></button>' +		
							'<button type="button" name="desactivar" class="btn btn-warning" onclick="desactivar_persona()">' +
								'<i class="glyphicon glyphicon-minus"></i> <span> Desactivar</span></button>' +
						'</div>'
					);
				<?php } ?>
			
			});
		
			function activar_persona(){
				
				<?php
					$id = makeSafe($selectedID);
					$memberID = getLoggedMemberID();

					$var_aux = "SELECT rh_people.status from rh_people where rh_people.id = '{$id}'";
					$status = sqlValue($var_aux);
					
					if ($status == "No"){

						?> alert("Aquesta persona està INACTIVA, voleu TORNAR A ACTIVAR-LA?"); <?php
//						sql ("insert into rh_people_activity_periods (name, family_name, entry_date, log_date, log_creator) values ( {$id}, {$id}, curdate(), curdate(), '{$memberID}' )", $eo);
					}
					
					elseif ($status == "Si"){
			
						?> alert("Aquesta persona ja està ACTIVA"); <?php

					}
				?>
			}
			
			function desactivar_persona(){
				
					alert("AQUEST ÉS EL BOTÓ DESACTIVAR");
				<?php
				
//				sql("update rh_people_activity_periods set rh_people_activity_periods.leaving_date = curdate() where rh_people_activity_periods.name = '{$id}' and rh_people_activity_periods.leaving_date is null", $eo);
				?>
				
			}
			
		</script>
		
		<?php
		$form_code = ob_get_contents();
		ob_end_clean();
		
		$html .= $form_code;
	}
In the detail view of my table rh_people I have 2 buttons: Activar and Desactivar. Every record of this table has a field named "status". What I wanted to do is:

- If status is 'Si' and I click on 'Activar' - just display a message in a popup
- If status is 'No' and I click on 'Activar' - display a message in a popup and run an "sql insert into"
- If status is 'Si' and I click on 'Desactivar' - display a message in a popup and run an "sql update"
- If status is 'No' and I click on 'Activar' - just display a message in a popup

Which is my problem? sql is executed when I enter the record, even if I don't click on the buttons.

Anyone having this problem? Or anyone using this code to display/configure action buttons?

Thanks in advance!

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

Re: Adding action buttons to detail view

Post by pbottcher » 2021-10-21 15:14

Hi,

not sure if I get you code correct, but I believe you are mixing the PHP and Javascript usage.

In your case you need to react on the click of the button.

If you have the information of the status on your page, you could just catch that information and check which button has been clicked. Then decide if you just display a message, or call an php script (possibly via ajax) to update your database.
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.

balfons
Veteran Member
Posts: 91
Joined: 2018-10-22 15:27

Re: Adding action buttons to detail view

Post by balfons » 2021-10-22 09:57

Hi, thanks for answering!
We will check the code again following your tips

Post Reply