Questions and discussion about developing processes and programming in PHP, JavaScript, web services & REST API.
Forum rules: Please search to see if a question has already asked before creating a new topic. Please don't post the same question in multiple forums.
#812666
Greetings!

I am making a Leave workflow. I am soliciting help with regards to the following

1. Database Access -- I have a report table named LEAVE. When a user logs in, I have a grid consisting of the user's leave details to include Start Date, End Date and Number of Days. I request javascript to execute the same.

2. Number of leave days - To calculate the number of leave days, I used a PHP code snippet provided in Processmaker wiki. I applied the trigger after the form gets submitted. However, the variable numLeaveDays does not take that value. Help required in this aspect.

Regards
#812674
ic57369k wrote:1. Database Access -- I have a report table named LEAVE. When a user logs in, I have a grid consisting of the user's leave details to include Start Date, End Date and Number of Days. I request javascript to execute the same.
JavaScript is only designed to access the data in the current browser window or URLs as a security feature. If you don't need data from your form, I recommend creating a trigger that fires before the form which uses executeQuery() to populate the grid. For example:
Code: Select all
$username = @@USR_USERNAME;
$sql = "SELECT START_DATE AS startDate, END_DATE AS endDate, NUM_DAYS AS numDays 
    FROM PMF_LEAVE_DETAILS WHERE USERNAME = '$username'";
@=leaveDetailsGrid = executeQuery($sql);
 
Where the grid is associated with the variable "leaveDetailsGrid" and the IDs of the fields inside the grid are "startDate", "endDate" and "numDays".

If you need the user to be selectable in the form, then you can use a dropdown box whose variable is "selectUser":
Code: Select all
if (isset(@@action) and @@action == 'QUERY') {
   @@action = ''; //reset action
   $username = @@selectUser;
   $sql = "SELECT START_DATE AS startDate, END_DATE AS endDate, NUM_DAYS AS numDays 
      FROM PMF_LEAVE_DETAILS WHERE USERNAME = '$username'";
   @=leaveDetailsGrid = executeQuery($sql);
   $formId = '4216425755a4c39fdf13ca5096948423'; //set to ID of DynaForm with grid
   PMFRedirectToStep(@@APPLICATION, @%INDEX, 'DYNAFORM', $formId);
}
 
Where a dropdown box in the DynaForm is associated with the variable "selectUser" and a hidden field is associated with the variable "action". The DynaForm's ID is '4216425755a4c39fdf13ca5096948423'.

The DynaForm should have the following JavaScript:
Code: Select all
$("selectUser").setOnchange(function(newVal, oldVal) {
  $("#action").setValue('QUERY');
  var formId = $('form').prop('id');
  $('#' + formId).submitForm();
});
Here is a sample process to show the idea:
(27.76 KiB) Downloaded 465 times
#812675
ic57369k wrote:2. Number of leave days - To calculate the number of leave days, I used a PHP code snippet provided in Processmaker wiki. I applied the trigger after the form gets submitted. However, the variable numLeaveDays does not take that value. Help required in this aspect.
What PHP code you are using? Explain what kind of DynaForm fields or database fields you are using? Are you trying to set the value in a normal text field or a text field inside a grid?
#812718
Greetings!

Thanks for the reply. I will explain the process as follows.

1. I have created a process "Leave" for leave management. I have attached the process and the report table LEAVE also. This process works perfectly well. I have made mail notifications as well. There is no issue in this process.

2. The next process is Populate_Grid_with_dependency_field-3.pmx where, I am accessing the LEAVE report table and querying database based on User ID input. I have changed the Populate_Grid_with_dependency_field-3.pmx process to rename the variables and queries based on LEAVE process variables and also changed reference to the report table in the trigger to PMT_LEAVE table.

3. Now, I want the user to input the username in the dynaform. Once, the username is entered, it should populate the LeaveDetailsGrid grid of the user name inputted. Also, I want to create options for deleting, updating and adding new data in the LeaveDetailsGrid which will update the PMT_LEAVE table. But, the code is not working.

Help solicited.Thanks and Regards in advance
Attachments
(28.72 KiB) Downloaded 425 times
(21.19 KiB) Downloaded 346 times
(234.09 KiB) Downloaded 313 times
#812733
Greetings
1. I want to create a process where, the user inputs a user ID and then its leave record is displayed immediately in a dynaform grid when the user has inputted the user id in the field. That means, I require to have a javascript and sql code to inspect the user id field for change in state. As soon as the user inputs the data in the user id field, the sql query works on the user id and queries the database and populates the dynaform grid.

2. Secondly, I want to add/modify/delete rows in dynaform grid and correspondingly the database (PM table) also should be written according to changes in dynaform grid. So I am looking for Modify, and Delete Record Buttons next to each dynaform grid record to manipulate that row in the database (PM table) too.

Help solicited please. Regards and Happy New Year!

amosbatto wrote:
ic57369k wrote:1. Database Access -- I have a report table named LEAVE. When a user logs in, I have a grid consisting of the user's leave details to include Start Date, End Date and Number of Days. I request javascript to execute the same.
JavaScript is only designed to access the data in the current browser window or URLs as a security feature. If you don't need data from your form, I recommend creating a trigger that fires before the form which uses executeQuery() to populate the grid. For example:
Code: Select all
$username = @@USR_USERNAME;
$sql = "SELECT START_DATE AS startDate, END_DATE AS endDate, NUM_DAYS AS numDays 
    FROM PMF_LEAVE_DETAILS WHERE USERNAME = '$username'";
@=leaveDetailsGrid = executeQuery($sql);
Where the grid is associated with the variable "leaveDetailsGrid" and the IDs of the fields inside the grid are "startDate", "endDate" and "numDays".

If you need the user to be selectable in the form, then you can use a dropdown box whose variable is "selectUser":
Code: Select all
if (isset(@@action) and @@action == 'QUERY') {
   @@action = ''; //reset action
   $username = @@selectUser;
   $sql = "SELECT START_DATE AS startDate, END_DATE AS endDate, NUM_DAYS AS numDays 
      FROM PMF_LEAVE_DETAILS WHERE USERNAME = '$username'";
   @=leaveDetailsGrid = executeQuery($sql);
   $formId = '4216425755a4c39fdf13ca5096948423'; //set to ID of DynaForm with grid
   PMFRedirectToStep(@@APPLICATION, @%INDEX, 'DYNAFORM', $formId);
}
Where a dropdown box in the DynaForm is associated with the variable "selectUser" and a hidden field is associated with the variable "action". The DynaForm's ID is '4216425755a4c39fdf13ca5096948423'.

The DynaForm should have the following JavaScript:
Code: Select all
$("selectUser").setOnchange(function(newVal, oldVal) {
  $("#action").setValue('QUERY');
  var formId = $('form').prop('id');
  $('#' + formId).submitForm();
});
Here is a sample process to show the idea:
Populate_Grid_with_dependency_field-3.pmx
#812741
This trigger code doesn't work because you didn't create an ENDDATE field in your PMT_LEAVE table:
Code: Select all
if (isset(@@action) and @@action == 'QUERY') {
   @@action = ''; //reset action
   $username = @@empUser;
   $sql = "SELECT FROMDATE, ENDDATE, NUMLVEDAYS 
      FROM PMT_LEAVE WHERE EMPUSER = '$username'";
   @@leaveDetailsGrid = executeQuery($sql);
   $formId = '4216425755a4c39fdf13ca5096948423'; //set to DynaForm's ID
   PMFRedirectToStep(@@APPLICATION, @%INDEX, 'DYNAFORM', $formId);
} 
You need to add a trigger to your Leave process to calculate the end date. For example:
Code: Select all
if (!empty(@@startDate) and !empty(@@numberDays)) {
   $start = strtotime(@@startDate);
   $end = strtotime('+ '. @@numberDays .'  days', $start);
   @@endDate = date('Y-m-d', $end);
} 
Where startDate is a datetime field and numberDays is a text field limited to integers with a validation rule.

You can also add week days like this:
Code: Select all
   $end = strtotime('+ '. @@numberDays .'  weekdays', $start); 
Then add endDate to your Report Table.

As for editing the contents in your table, you are going to need a separate PM Table, because you should NOT write directly to your PMT_LEAVE table because its records are going to be overwritten when cases are updated in your Leave processes.

Basically, you are going to need a separate PM Table to hold data that can be updated by two different processes and you are going to need an ID field to identify each record, so you can keep track of what record you are changing with each process. I recommend that you create an auto-increment primary key field in your PM Table so the ID is automatically created for you when a new record is inserted in the table. The grid can store this ID in a hidden field. When the grid is submitted, then you look up the specific record and update it with executeQuery() in a trigger. If the hidden field is empty then you know to add a new record to the table.

For example, lets say that you create a PM Table named "PMT_LEAVE_DATA" which contains the following fields:
ID_LEAVE (auto-increment primary field, integer)
EMPUSER (var_char),
FROMDATE (date),
ENDDATE (date),
NUMLVEDAYS (integer)

Then, your trigger code would be something like this:
Code: Select all
//if looking up the records:
if (isset(@@action) and @@action == 'QUERY') {
   @@action = ''; //reset action
   $username = @@empUser;
   $sql = "SELECT ID_LEAVE, FROMDATE, ENDDATE, NUMLVEDAYS 
      FROM PMT_LEAVE_DATA WHERE EMPUSER = '$username'";
   @=leaveDetailsGrid = executeQuery($sql);
   $formId = '4216425755a4c39fdf13ca5096948423'; //set to DynaForm's ID
   PMFRedirectToStep(@@APPLICATION, @%INDEX, 'DYNAFORM', $formId);
}
//if submitting data
elseif (isset(@@action) and @@action == '' and !empty(@@empUser)) {
   $empUser = @@empUser;
   $aLeaveIDs = array();
   
   foreach (@=leaveDetailsGrid as $aRow) {
       //if adding a new row:
       if (empty($aRow['ID_LEAVE'])) {
           //don't include the ID_LEAVE field because it will automatically be inserted by MySQL:
           $sql = "INSERT INTO PMT_LEAVE_DATA ( EMPUSER, FROMDATE, ENDDATE, NUMLVEDAYS ) 
               VALUES ( '$empuser', '{$aRow['FROMDATE']}', '{$aRow['ENDDATE']}', {$aRow['NUMLVEDAYS']} )";
           @@insertResult = executeQuery($sql);
       }
       else { //if an existing row, then update it in the database:
           $aLeaveIDs[] = $aRow['ID_LEAVE'];
           $sql = "UPDATE PMT_LEAVE_DATA SET FROMDATE='{$aRow['FROMDATE']}',
              ENDDATE='{$aRow['ENDDATE']}', NUMLVEDAYS={$aRow['NUMLVEDAYS']} 
              WHERE ID_LEAVE={$aRow['ID_LEAVE']} ";
           @@updateResult = executeQuery($sql);
       }
   }
   
   //delete any records for the user which don't appear in the grid:
   //(these are the records that were deleted in the grid)
   $validIDs = implode(', ', $aRow['ID_LEAVE']);
   $sql = "DELETE FROM PMT_LEAVE_DATA WHERE EMPUSER='$empUser' AND 
       ID_LEAVE NOT IN ($validIDs)";
   @@deleteResult = executeQuery($sql);
}
 
You probably will have to debug this code, but it gives you an idea how to proceed.
Want to create your own meme coin?

In the world of cryptocurrencies, a unique and exc[…]

The market for cryptocurrencies is demonstrating a[…]

What's SAP FICO?

Embarking on a dissertation can be one of the most[…]

Hello. For rental housing, there are software solu[…]