List Plugin Listcsv Issue

teoyh

Member
I love the concept of the Listcsv which allow me to run a php file before and after the row record in the csv is imported.

I had however encounter a strange issue in the Listcsv and just wonder if the rest is having the same issue as me.

When I added a php file to run in the File to run before row is imported, i noticed that out of 10 rows in my csv file only the last row is imported.

The above will not happen if I don't use the File to run before row is imported. I can still use the File to run after row is imported.

Can someone help , I really love the File to run before row is imported features.
 
I just tested here, and it works fine for me using both, and I can't see anything in the code which would cause this.

Can you paste the code from both of you files? Remember to wrap it in code tags (or use the "Insert" button above the editor).

-- hugh
 
Code:
<?php
// File to run before row is imported
$db = FabrikWorker::getDbo(false, 3);
$user =& JFactory::getUser();
$regby=$user->get('username');
$db->setQuery("delete from plan_ot_submit where regby='$regby'");
$db->query();
?>

Code:
<?php
// File to run after row is imported
$db = FabrikWorker::getDbo(false, 3);
$user =& JFactory::getUser();
$application = JFactory::getApplication();
$regby=$user->get('username');
$reqname=$user->get('name');
$Datereg = date("Y-m-d");
$current_day = date("N");//This return a numeric digit Friday=5
$days_to_friday = 5 - $current_day;
$Fridate =  date("Y-m-d", strtotime("+ {$days_to_friday} Days"));

//Delete Illegal Upload
$db->setQuery("DELETE FROM plan_ot_submit where (length(sdate)=0 or length(empno)=0 or length(planot)=0) and regby='$regby' and sys_date=curdate()");
$db->query();

//Update sdate to proper date value
$db->setQuery("update plan_ot_submit a set kostl=(select mid(kostl,4,6) from stm_employee b where a.empno=mid(b.sapno,3,8) group by kostl), errcode=2,sdate=date_format(str_to_date(sdate, '%Y%m%d'),'%Y-%m-%d') where length(sdate)=8 and regby='$regby' and sys_date=curdate()");
$db->query();

//Delete Duplicate Record
$db->setQuery("Delete a FROM plan_ot_submit a JOIN (SELECT MAX(t.id) AS max_a1, t.sdate, t.empno FROM plan_ot_submit t GROUP BY t.sdate, t.empno HAVING COUNT(*) > 1) b
ON b.sdate = a.sdate and a.empno=b.empno AND b.max_a1 != a.id and a.regby='$regby'");
$db->query();

//Check if employee valid comparing Cost Centre
$db->setQuery("update plan_ot_submit a set errcode=1,remark='..Invalid employee, this employee not from your dept.' where (select mid(kostl,4,6) from stm_employee b where a.empno=mid(b.sapno,3,8)) not in (SELECT kostl FROM planot_admin where userid='$regby' and (role='ADMIN' or role='ADMIN_BAK')) and regby='$regby' and sys_date=curdate()");
$db->query();
$db->setQuery("update plan_ot_submit set errcode=1,remark='..invalid employee no.' where (length(empno)>10 or mid(empno,1,2)='oa') and regby='$regby' and sys_date=curdate()") ;
$db->query();
$db->setQuery("update plan_ot_submit set errcode=1,remark='..this employee no longer exist.' where empno not in (select mid(sapno,3,10) from stm_employee) and regby='$regby' and sys_date=curdate()") ;
$db->query();

//Error Checking For Submitted Record
// Plan OT Value Range Check
$db->setQuery("update plan_ot_submit set errcode=1,remark='..Plan OT value cannot exceed 22.5 or < than 0' where (planot>22.5 or planot<0) and sys_date=curdate() and regby='$regby' and sys_date=curdate() ");
$db->query();
// Date Validation
$db->setQuery("update plan_ot_submit set errcode=1,remark='..Invalid date format' where (sdate is null or length(sdate)<>10 or instr(sdate,'-')=0) and regby='$regby' and sys_date=curdate()");
$db->query();
//Checking of submission of past date
$db->setQuery("update plan_ot_submit set errcode=1,remark='..no submission of past date' where sdate < '$Datereg' and regby='$regby' and sys_date=curdate()") ;
$db->query();
//Checking of advance submission of public holiday not allowed
//dayname(sdate) not in ('Saturday','Sunday','Friday')
$db->setQuery("update plan_ot_submit set errcode=1,remark='..too early to submit public holiday' where sdate <> '$Datereg' and instr((Select ph from ph where active='1' and remark='PH'),sdate)>0 and datediff(sdate,current_date)>4 and regby='$regby' and sys_date=curdate()");
$db->query();
//Checking of advance submission of non public holiday week
$db->setQuery("update plan_ot_submit set errcode=1,remark='..no advanced submission' where sdate > '$Datereg' and instr((Select ph from ph where active='1' and remark='PH'),sdate)=0 and dayname(sdate) not in ('Saturday','Sunday') and regby='$regby' and sys_date=curdate()");
$db->query();
//Prevent advance submission of Sat and Sunday before Friday
$db->setQuery("update plan_ot_submit set errcode=1,remark='..weekend submission only allowed on Friday or Thursday if Friday is a holiday.' where dayname(sdate) in ('Saturday','Sunday') and (dayname(curdate()) not in ('Friday','Saturday','Sunday') and instr((select ph from ph where active='1' and remark='PH'),'$Fridate')=0 and regby='$regby' and sys_date=curdate())");
$db->query();

//Alert User error message
$query = "SELECT count(id) FROM plan_ot_submit where errcode=1 and regby='$regby' and sys_date=curdate()";
$db->setQuery($query);
$errcount= $db->loadResult();
//$URL="http://stmportal/index.php?option=com_fabrik&view=table&tableid=340&calculations=0&resetfilters=1&Itemid=1184"; 
//header ("Location: $URL"); 
if($errcount>0){
    $application->enqueueMessage(JText::_('Please check the respective record there are some error detected.'), 'error');
} else {
    $application->enqueueMessage(JText::_('Verification completed, there are no error detected, you may proceed to print and submit these records.'), 'info');
}

?>
 
The user is importing the csv records to plan_ot_submit.

As there are many other doing the same thing I cannot effort to empty the table by default. I therefore had to delete all record base on current login user before allowing him to upload the new record.

Once uploaded, the 2nd part of the code will do the check and clean up as well as update the date to proper format.

I did another test, instead of delete I just do other query. With that all the record get uploaded. So its something got to do with the delete.

Between the table plan_ot_submit is resided in another server if you notice this $db = FabrikWorker::getDbo(false, 3);
 
Are you sure you should be doing that delete 'on before row import'?

Remember that code runs before EVERY row is imported. So I think what heppens is, you run that, run the 'after' code which inserts a row with $regby, then on the next row you promptly delete it again with the 'before row' code.

Lucily for you I just updated the plugin, discussed on this thread:

http://fabrikar.com/forums/index.ph...-tables-via-listcsv-plugin.43234/#post-220727

... so there is now a "Before Import Starts" section of code, which is where you can do your initial deletion.

You can also wrap you big chunk of code up in a class, and just call it from the 'on after row code', as per the examples I gave in that thread.

-- hugh
 
Wow so cool, you the man , super like. Now with the Before Import Start I can simply throw my php file of the following code into the Before Import Start and that take care of everything.

Then when its uploaded I can clean up with the another php files under the Import Complete.

But I am still trying to understand what you write about the class thing, I will try to figure it out. Thanks a million.
 
I got it for the benefit of others see the setup I attached.

The in the class file it look something like this ; which will delete all record before the new one are being uploaded

Code:
<?php
/**
* Created by PhpStorm.
* User: Hugh
* Date: 2/11/2016
* Time: 11:56 PM
*/

class importStates
{
     function importStateRow($importModel)
     {
        $db = FabrikWorker::getDbo(false, 3);
        $user =& JFactory::getUser();
        $regby=$user->get('username');
        $db->setQuery("delete from plan_ot_submit where regby='$regby'");
        $db->query();
        return true;
     }

     function afterImportStateRow($importModel)
     {

     }

     function importStateComplete($importModel)
     {
         JFactory::getApplication()->enqueueMessage('YAY!');
     }
}
 

Attachments

  • Before Import.jpg
    Before Import.jpg
    19.5 KB · Views: 88
Yeah, pretty much.

First, you might want to change the names, I just used 'states' as the table I'm working on is of US States (Alabama, New York, etc).

Also, one of the main points of using a class is so you can put big chunks of code in there, rather than inline. It's much easier to deal with PHP in a file you can edit with a decent text editor. So you can copy that big chunks of code from your main 'after import row' inline code, and put it in the afterImportStateRow() function in the class (or whatever you want to call it).

And, you should mae the function names meaningful, so
So ... lets' call it importPlan ... here's the class file ...

Code:
<?php
/**
 * Created by PhpStorm.
 * User: Hugh
 * Date: 2/11/2016
 * Time: 11:56 PM
 */

class importPlan
{
    function startImportPlan()
    {
        $db = FabrikWorker::getDbo(false, 3);
        $user =& JFactory::getUser();
        $regby=$user->get('username');
        $db->setQuery("delete from plan_ot_submit where regby='$regby'");
        $db->query();
        return true;
    }

    function afterImportPlanRow()
    {
        // File to run after row is imported
        $db = FabrikWorker::getDbo(false, 3);
        $user =& JFactory::getUser();
        $application = JFactory::getApplication();
        $regby=$user->get('username');
        $reqname=$user->get('name');
        $Datereg = date("Y-m-d");
        $current_day = date("N");//This return a numeric digit Friday=5
        $days_to_friday = 5 - $current_day;
        $Fridate =  date("Y-m-d", strtotime("+ {$days_to_friday} Days"));

        //Delete Illegal Upload
        $db->setQuery("DELETE FROM plan_ot_submit where (length(sdate)=0 or length(empno)=0 or length(planot)=0) and regby='$regby' and sys_date=curdate()");
        $db->query();

        //Update sdate to proper date value
        $db->setQuery("update plan_ot_submit a set kostl=(select mid(kostl,4,6) from stm_employee b where a.empno=mid(b.sapno,3,8) group by kostl), errcode=2,sdate=date_format(str_to_date(sdate, '%Y%m%d'),'%Y-%m-%d') where length(sdate)=8 and regby='$regby' and sys_date=curdate()");
        $db->query();

        //Delete Duplicate Record
        $db->setQuery("Delete a FROM plan_ot_submit a JOIN (SELECT MAX(t.id) AS max_a1, t.sdate, t.empno FROM plan_ot_submit t GROUP BY t.sdate, t.empno HAVING COUNT(*) > 1) b
ON b.sdate = a.sdate and a.empno=b.empno AND b.max_a1 != a.id and a.regby='$regby'");
        $db->query();

        //Check if employee valid comparing Cost Centre
        $db->setQuery("update plan_ot_submit a set errcode=1,remark='..Invalid employee, this employee not from your dept.' where (select mid(kostl,4,6) from stm_employee b where a.empno=mid(b.sapno,3,8)) not in (SELECT kostl FROM planot_admin where userid='$regby' and (role='ADMIN' or role='ADMIN_BAK')) and regby='$regby' and sys_date=curdate()");
        $db->query();
        $db->setQuery("update plan_ot_submit set errcode=1,remark='..invalid employee no.' where (length(empno)>10 or mid(empno,1,2)='oa') and regby='$regby' and sys_date=curdate()") ;
        $db->query();
        $db->setQuery("update plan_ot_submit set errcode=1,remark='..this employee no longer exist.' where empno not in (select mid(sapno,3,10) from stm_employee) and regby='$regby' and sys_date=curdate()") ;
        $db->query();

        //Error Checking For Submitted Record
        // Plan OT Value Range Check
        $db->setQuery("update plan_ot_submit set errcode=1,remark='..Plan OT value cannot exceed 22.5 or < than 0' where (planot>22.5 or planot<0) and sys_date=curdate() and regby='$regby' and sys_date=curdate() ");
        $db->query();
        // Date Validation
        $db->setQuery("update plan_ot_submit set errcode=1,remark='..Invalid date format' where (sdate is null or length(sdate)<>10 or instr(sdate,'-')=0) and regby='$regby' and sys_date=curdate()");
        $db->query();
        //Checking of submission of past date
        $db->setQuery("update plan_ot_submit set errcode=1,remark='..no submission of past date' where sdate < '$Datereg' and regby='$regby' and sys_date=curdate()") ;
        $db->query();
        //Checking of advance submission of public holiday not allowed
        //dayname(sdate) not in ('Saturday','Sunday','Friday')
        $db->setQuery("update plan_ot_submit set errcode=1,remark='..too early to submit public holiday' where sdate <> '$Datereg' and instr((Select ph from ph where active='1' and remark='PH'),sdate)>0 and datediff(sdate,current_date)>4 and regby='$regby' and sys_date=curdate()");
        $db->query();
        //Checking of advance submission of non public holiday week
        $db->setQuery("update plan_ot_submit set errcode=1,remark='..no advanced submission' where sdate > '$Datereg' and instr((Select ph from ph where active='1' and remark='PH'),sdate)=0 and dayname(sdate) not in ('Saturday','Sunday') and regby='$regby' and sys_date=curdate()");
        $db->query();
        //Prevent advance submission of Sat and Sunday before Friday
        $db->setQuery("update plan_ot_submit set errcode=1,remark='..weekend submission only allowed on Friday or Thursday if Friday is a holiday.' where dayname(sdate) in ('Saturday','Sunday') and (dayname(curdate()) not in ('Friday','Saturday','Sunday') and instr((select ph from ph where active='1' and remark='PH'),'$Fridate')=0 and regby='$regby' and sys_date=curdate())");
        $db->query();

        //Alert User error message
        $query = "SELECT count(id) FROM plan_ot_submit where errcode=1 and regby='$regby' and sys_date=curdate()";
        $db->setQuery($query);
        $errcount= $db->loadResult();
        //$URL="http://stmportal/index.php?option=com_fabrik&view=table&tableid=340&calculations=0&resetfilters=1&Itemid=1184";
        //header ("Location: $URL");
        if($errcount>0){
            $application->enqueueMessage(JText::_('Please check the respective record there are some error detected.'), 'error');
        } else {
            $application->enqueueMessage(JText::_('Verification completed, there are no error detected, you may proceed to print and submit these records.'), 'info');
        }
    }
}

Select that for the "Before import file".

The "Before import code" is then ...

Code:
$this->userClass = new importPlan();
$this->userClass->startImportPlan();

And the "After import row code" is ...

Code:
$this->userClass->afterImportPlanRow();

Much cleaner.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top