Page 1 of 1

More batch actions with global variables

Posted: 2018-07-28 00:12
by fbrano
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?

Re: More batch actions with global variables

Posted: 2018-07-28 08:27
by pbottcher

Re: More batch actions with global variables

Posted: 2018-07-28 16:36
by fbrano
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.

Re: More batch actions with global variables

Posted: 2018-07-30 08:21
by pbottcher
Hi,

maybe you can post a sample on what you try to achieve.

Re: More batch actions with global variables

Posted: 2018-08-01 06:07
by fbrano
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
}

Re: More batch actions with global variables

Posted: 2018-08-01 07:49
by fbrano
This works, but it updates all records not only selected: sql("update `Pristupy` set `Revidoval`= '12345678'",$eo);

Re: More batch actions with global variables

Posted: 2018-08-01 12:53
by fbrano
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...

Re: More batch actions with global variables

Posted: 2018-08-01 13:27
by pbottcher
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.

Re: More batch actions with global variables

Posted: 2018-08-02 04:53
by fbrano
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.

Re: More batch actions with global variables

Posted: 2018-08-02 05:21
by fbrano
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
}

Re: More batch actions with global variables

Posted: 2018-08-02 06:59
by pbottcher
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).

Re: More batch actions with global variables

Posted: 2018-08-02 12:51
by fbrano
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?

Re: More batch actions with global variables

Posted: 2018-08-02 17:00
by pbottcher
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).

Re: More batch actions with global variables

Posted: 2018-08-03 05:39
by fbrano
Many thanks for your patience with me ;) it is exactly what I was looking for

Re: More batch actions with global variables

Posted: 2018-08-03 21:48
by fbrano
One note how browsers behaved, chrome updated page automaticaly, but IE requested to click on OK in confirmation window.

Re: More batch actions with global variables

Posted: 2019-02-28 10:37
by fbrano
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...