More batch actions with global variables

The recommended method of customizing your AppGini-generated application is through hooks. But sometimes you might need to add functionality not accessible through hooks. You can discuss this here.
Post Reply
User avatar
fbrano
Veteran Member
Posts: 70
Joined: 2018-03-19 10:39
Location: Slovakia
Contact:

More batch actions with global variables

Post by fbrano » 2018-07-28 00:12

How to write a code in table.php part batch_actions(&$args) second and next menu item?

Is it possible to write the second and next functions in the same table-TV.js file?

How to store a $memberInfo array as a global variable for functions used in batch actions?

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

Re: More batch actions with global variables

Post by pbottcher » 2018-07-28 08:27

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.

User avatar
fbrano
Veteran Member
Posts: 70
Joined: 2018-03-19 10:39
Location: Slovakia
Contact:

Re: More batch actions with global variables

Post by fbrano » 2018-07-28 16:36

Sure, I did label print example, but I have to figure out how to update certain field in the range of selected records with login of actual user.
ver 23.15 1484

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

Re: More batch actions with global variables

Post by pbottcher » 2018-07-30 08:21

Hi,

maybe you can post a sample on what you try to achieve.
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.

User avatar
fbrano
Veteran Member
Posts: 70
Joined: 2018-03-19 10:39
Location: Slovakia
Contact:

Re: More batch actions with global variables

Post by fbrano » 2018-08-01 06:07

I used example with print-labels, but add sql update for certain field, which I need to be filled up with logged user ID stroed in custom[1] field in $memberInfo database, here is code:



<?php
/*
Including the following files allows us to use many shortcut
functions provided by AppGini. Here, we'll be using the
following functions:
makeSafe()
protect against malicious SQL injection attacks
sql()
connect to the database and execute a SQL query
db_fetch_assoc()
same as PHP built-in mysqli_fetch_assoc() function
*/
$curr_dir = dirname(__FILE__);
include("{$curr_dir}/defaultLang.php");
include("{$curr_dir}/language.php");
include("{$curr_dir}/lib.php");

/* receive calling parameters */
$table = $_REQUEST['table'];
$ids = $_REQUEST['ids']; /* this is an array of IDs */

/* a comma-separated list of IDs to use in the query */
$cs_ids = '';
foreach($ids as $id){
$cs_ids .= "'" . makeSafe($id) . "',";
}
$cs_ids = substr($cs_ids, 0, -1); /* remove last comma */

/* retrieve the records and display mail labels */
$res = sql( "select * from Pristupy where ID in ({$cs_ids})", $eo);
while($row = db_fetch_assoc($res)){
/* update field with logged user ID stored in custom[1] */
sql("update `Pristupy` set `Revidoval`= '{$memberInfo['custom'][1]}'",$eo);
?>
<b>Pouzivatel: <?php echo $row['Pouzivatel']; ?> <?php echo sqlValue("SELECT Pouzivatel FROM Pouzivatelia where Osc='{$row['Pouzivatel']}'"); ?></b><br>
<i>System: <?php echo sqlValue("SELECT System FROM Systemy where ID='{$row['System']}'"); ?>
Rola: <?php echo sqlValue("SELECT Rola FROM Role where ID='{$row['Rola']}'"); ?></i><br>
Datum platnosti: <?php echo $row['Datum_platnosti']; ?>
VPN: <?php echo $row['VPN']; ?><br>
Stav: <?php echo $row['Stav_pristupu']; ?>
Revidoval: <?php echo $row['Revidoval']; ?>
<hr>
<?php
}
ver 23.15 1484

User avatar
fbrano
Veteran Member
Posts: 70
Joined: 2018-03-19 10:39
Location: Slovakia
Contact:

Re: More batch actions with global variables

Post by fbrano » 2018-08-01 07:49

This works, but it updates all records not only selected: sql("update `Pristupy` set `Revidoval`= '12345678'",$eo);
ver 23.15 1484

User avatar
fbrano
Veteran Member
Posts: 70
Joined: 2018-03-19 10:39
Location: Slovakia
Contact:

Re: More batch actions with global variables

Post by fbrano » 2018-08-01 12:53

My last experience put the update before while command works for selected records it can update date of revision but can not update information about logged user from memberinfo array, any idea?
/* retrieve the records and display mail labels */
$res = sql( "select * from Pristupy where ID in ({$cs_ids})", $eo);
sql("update `Pristupy` set `Revidoval`= '{$memberInfo['custom'][0]}' where ID in ({$cs_ids})",$eo);
sql("update `Pristupy` set `Datum_revizie`= (CurDate()) where ID in ({$cs_ids})",$eo);
while...
ver 23.15 1484

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

Re: More batch actions with global variables

Post by pbottcher » 2018-08-01 13:27

Hi,

I think you should add the following to your code in order to get the data

Code: Select all

$memberinfo = getMemberInfo();

and change

Code: Select all

sql("update `Pristupy` set `Revidoval`= '{$memberinfo['custom'][1]}'",$eo);

to

Code: Select all

sqlvalue("update `Pristupy` set `Revidoval`= '{$memberinfo['custom1']}' where ID={$row['ID']}");

or if you do not have it in the while loop

Code: Select all

sqlvalue("update `Pristupy` set `Revidoval`= '{$memberinfo['custom1']}' where ID in ({$cs_ids})"}:

I assume that you have filled the custom1 field in the membership_users table according to your needs.
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.

User avatar
fbrano
Veteran Member
Posts: 70
Joined: 2018-03-19 10:39
Location: Slovakia
Contact:

Re: More batch actions with global variables

Post by fbrano » 2018-08-02 04:53

Adding $memberinfo = getMemberInfo(); in mail_labels.php file do not solved task. Is there way how to do it directly in function file table-tv.js without calling mail_labels.php? I do not need output a separate page, only refresh table view after update fields 'Revised' and 'Date_of_revision' btw updating date of revision works fine.
ver 23.15 1484

User avatar
fbrano
Veteran Member
Posts: 70
Joined: 2018-03-19 10:39
Location: Slovakia
Contact:

Re: More batch actions with global variables

Post by fbrano » 2018-08-02 05:21

Many thanks it works with this syntax:

/* retrieve the records and display mail labels */
$res = sql( "select * from Pristupy where ID in ({$cs_ids})", $eo);
$memberinfo = getMemberInfo();
sql("update `Pristupy` set `Revidoval`= '{$memberinfo['custom'][1]}' where ID in ({$cs_ids})",$eo);
sql("update `Pristupy` set `Datum_revizie`= (CurDate()) where ID in ({$cs_ids})",$eo);
while($row = db_fetch_assoc($res)){
?>
<b>Pouzivatel: <?php echo $row['Pouzivatel']; ?> <?php echo sqlValue("SELECT Pouzivatel FROM Pouzivatelia where Osc='{$row['Pouzivatel']}'"); ?></b><br>
<i>System: <?php echo sqlValue("SELECT System FROM Systemy where ID='{$row['System']}'"); ?>,
Rola: <?php echo sqlValue("SELECT Rola FROM Role where ID='{$row['Rola']}'"); ?></i><br>
Datum platnosti: <?php echo $row['Datum_platnosti']; ?>,
VPN: <?php echo $row['VPN']; ?>,
Stav: <?php echo $row['Stav_pristupu']; ?>,<br>
Revidoval: <?php echo $memberinfo['custom'][0]; ?>, <?php echo $memberinfo[email]; ?>,
Datum revizie: <?php echo $row['Datum_revizie']; ?>
<hr>
<?php
}
ver 23.15 1484

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

Re: More batch actions with global variables

Post by pbottcher » 2018-08-02 06:59

Hi,

you have an incorrect call.

Code: Select all

sql("update `Pristupy` set `Revidoval`= '{$memberinfo['custom'][1]}' where ID in ({$cs_ids})",$eo);

needs to be

Code: Select all

sql("update `Pristupy` set `Revidoval`= '{$memberinfo['custom1']}' where ID in ({$cs_ids})",$eo);

also the (sample) mail_labels.php does not necessarily need to ouput a new page. You can do whatever you need in there (as you do in your posted file).
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.

User avatar
fbrano
Veteran Member
Posts: 70
Joined: 2018-03-19 10:39
Location: Slovakia
Contact:

Re: More batch actions with global variables

Post by fbrano » 2018-08-02 12:51

With systax '{$memberinfo['custom'][1]}' I got expected value, but in this form '{$memberinfo['custom1']}' there is no output.

What to add to code and where to add it as I want to refresh page after finishing batch action to be able to see updated fields automaticaly not only manualy via F5 in browser?
ver 23.15 1484

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

Re: More batch actions with global variables

Post by pbottcher » 2018-08-02 17:00

Hi,

yes you are right, i is $memberinfo['custom'][1].

What you can do in order to just execute the batch action and reload your page. Put in the <tablename>-tv.js the following:

Code: Select all

function <BATCHFUNCTION_DEFINED_IN_THE_BATCH_ACTION>(table_name, ids){

	var url = '<YOURPHPFUNCTION>?table=' + table_name;
	for(var i = 0; i < ids.length; i++){
		url = url + '&' 
			+ encodeURI('ids[]') + '=' 
			+ encodeURIComponent(ids[i]);
	}
	$j.ajax({
    url: url,
    success: function(data){
    }
  })
  location.reload();
}
In the <YOURPHPFUNCTION>.php you execute the batch action (without output).
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.

User avatar
fbrano
Veteran Member
Posts: 70
Joined: 2018-03-19 10:39
Location: Slovakia
Contact:

Re: More batch actions with global variables

Post by fbrano » 2018-08-03 05:39

Many thanks for your patience with me ;) it is exactly what I was looking for
ver 23.15 1484

User avatar
fbrano
Veteran Member
Posts: 70
Joined: 2018-03-19 10:39
Location: Slovakia
Contact:

Re: More batch actions with global variables

Post by fbrano » 2018-08-03 21:48

One note how browsers behaved, chrome updated page automaticaly, but IE requested to click on OK in confirmation window.
ver 23.15 1484

User avatar
fbrano
Veteran Member
Posts: 70
Joined: 2018-03-19 10:39
Location: Slovakia
Contact:

Re: More batch actions with global variables

Post by fbrano » 2019-02-28 10:37

This works fine:
$memberinfo = getMemberInfo();
sql("update `Systemy` set `Vlastnik`= '{$memberinfo['custom'][1]}' where ID in ({$cs_ids})",$eo);

but I'd like to replace value in field Vlastnik on all selected rows in db Systemy with superodinate of Vlastnik found in db Pouzivatelia in field Manazer:
// $osc_vlastnika = sqlValue("select Vlastnik from Systemy where id='{$ids[1]}'");
// $osc_manazera = sqlValue("select Manazer from Pouzivatelia where Osc='{$osc_vlastnika}'");
// sql("update `Systemy` set `Vlastnik`= '{$osc_manazera}' where ID in ({$cs_ids})",$eo);


Is it possible after selecting rows in table view, and starting a batch action to ask a user to choose from list of lookup values of field Pouzivatel and Osc from db Pouzivatelia who to replace with? Notification of who did the change will be in field Revidoval filled with $memberinfo['custom'][1].

Many thanks for any ideas...
ver 23.15 1484

Post Reply