Editor Events help needed

Editor Events help needed

borconiborconi Posts: 56Questions: 19Answers: 1

I'm trying to understand what's the correct order way.

I have some queries which are executed on the preEdit event in PHP, but the JSON returned to the HTML doesn't reflect this changes.
My current order is:

$editor->leftJoin(....);
$editor->where(...);
$editor->distinct(true); //custom function added as discussed in another question.
$editor->on('preEdit',function(...){...});
$editor->process( $_POST );
$editor->json();

All the queries are executed correctly and data is acutally updated as expected on the tables, but I need to refresh the page to see the changes, since the json returned after an edit doesn't reflect the changes just made in the preEdit part.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,726Questions: 1Answers: 10,110 Site admin

    Can you show me what is in your preEdit function please? I'm not entirely clear if you are modifying the data being submitted, writing to the database yourself or something else.

    Thanks,
    Allan

  • borconiborconi Posts: 56Questions: 19Answers: 1
    edited October 2016

    Hi Allan.

    Please see below (Don't judge :smile: it's not in production yet, code cleanup will happen)

    $editor=Editor::inst( $db, 'jobs' )
        ->fields(
            Field::inst( 'company.Name' )->set(false),
            Field::inst( 'stores.short_add' )->set(false),
            Field::inst( 'jobs.description' )->options( 'job_descriptions', 'id', 'long_description', function ($q) {
                                        global $owner;
                                        $q->where('owner',$owner);})
                                        ->validator( 'Validate::dbValues' ),
            Field::inst( 'job_descriptions.short_description' )->set(false),
            Field::inst( 'jobs.startdate' )->set(false),
            Field::inst( 'clients.Company_Name' )->set(false),
            Field::inst( 'operatives.name' )->set(false),
            Field::inst( 'jobs.freq' )->set(false),
            Field::inst( 'jobs.job_end'),
            Field::inst( 'jobs.freq_type' )->options( function () {
            return array(
                array( 'value' => '0', 'label' => 'Interval' ),
                array( 'value' => '1', 'label' => 'Set Date' ),
                array( 'value' => '2', 'label' => 'One Off/Add-Hoc' )
            );
        } )->set(false),
            
            Field::inst( 'subcontracted.to_c' )->options( function () use ( $db ) {
                                                $out[] = array('value' => '', 'label' => 'Select from list');
                                                $attrList=$db->selectDistinct ('clients',['id','company_Name']);
                                                while ( $row = $attrList->fetch() ) 
                                                    {
                                                    $out[] = array(
                                                            "value" => $row['id'],
                                                            "label" => $row['company_Name']
                                                    );
                                                    }     
                                                return $out;})
                                              ->validator( function ( $val, $data, $opts ) use ( $db ) {
                                                 
                                                                    $attrList=$db->selectDistinct ('clients',['id']);
                                                                    $res=$attrList->fetchAll();
                                                                    $res2=array_column($res, 'id');
                                                                    
                                                                    if (($val=="") || (array_search($val,$res2)!==false))
                                                                            return true;
                                                                    else {
                                                                            return "Invalid subcontractor selected";
                                                                        }
                                                })->set(false),
            Field::inst( 'jobs_assigned.operativeid' )->options( function () use ( $db ) {
                                                         $out[] = array('value' => '', 'label' => 'Select from list');
                                                         global $owner; 
                                                        $attrList=$db->selectDistinct ('operatives',['id','name'],array( 'owner'=>$owner));
                                                        while ( $row = $attrList->fetch() ) 
                                                            {
                                                            $out[] = array(
                                                                    "value" => $row['id'],
                                                                    "label" => $row['name']
                                                            );}     
                                                        return $out;})
                                             ->validator( function ( $val, $data, $opts ) use ( $db ) {
                                                                    global $owner;
                                                                    $attrList=$db->selectDistinct ('operatives',['id'],array( 'owner'=>$owner));
                                                                    $res=$attrList->fetchAll();
                                                                    $res2=array_column($res, 'id');
                                                                    if (($val=="") || (array_search($val,$res2)!==false))
                                                                            return true;
                                                                    else {
                                                                            return "Invalid operative selected";
                                                                         }
                                                            })->set(false),
            Field::inst( 'jobs.owner' )->set(false)
        )
        ->leftJoin( 'stores', 'stores.id', '=', 'jobs.store_id' )
        ->leftJoin( 'company', 'stores.company_id','=','company.id')
        ->leftJoin( 'subcontracted', 'subcontracted.jobid','=','jobs.id')
        ->leftJoin( 'clients','clients.id','=','subcontracted.to_c')
        ->leftJoin( 'jobs_assigned','jobs_assigned.jobid','=','jobs.id')
        ->leftJoin( 'operatives','jobs_assigned.operativeid','=','operatives.id')
        ->leftJoin( 'job_descriptions','job_descriptions.id','=','jobs.description');
        $editor->where(function ($q) 
        {
            
        $q->where('jobs.job_end',null)
        ->where(function($r) {
            global $owner;
            $r->where('jobs.owner',$owner)
           ->or_where( 'jobs.id',"(select `jobid` from `subcontracted` where `from_c`='$owner')","IN", false );
        });
         } );
         
         $editor->distinct(true);
          $editor->on( 'preEdit', function ( $editor, $id, $values )  {
                  global $db2;
                  global $owner;
              if (isset($values['jobs']['job_end']))
              {
                    $stmt=$db2->prepare("delete from `schedule` where `jobid`=? and `scheduled_date`>?");
                    $stmt->execute(array($id,$values['jobs']['job_end']));
              }
             
              if (array_key_exists("subcontracted",$values) || array_key_exists("jobs_assigned",$values))
              {
    
                  $stmt=$db2->prepare("delete from `jobs_assigned` where `jobid`=?");
                  $stmt->execute(array($id));
                  
                  if (@$values['jobs_assigned']['operativeid'] == '')
                  {
                      $stmt=$db2->prepare("select * from `subcontracted` where `from_c`=? and `jobid`=?");
                      $stmt->execute(array($owner,$id));
                      if ($stmt->rowCount()==0) {
                          $stmt=$db2->prepare("insert into `subcontracted` values (null,?,?,?)");
                          $stmt->execute(array($id,$owner,$values['subcontracted']['to_c']));
                          $stmt->execute(array($id,$values['subcontracted']['to_c'],$values['subcontracted']['to_c']));
                          return true;
                                                }
                      else {
                            $do_more=true;
                            while ($do_more) {
                                $result=$stmt->fetch();
                                if ($result['from_c']==$result['to_c'])
                                {
                                $db2->query("delete from `subcontracted` where id=".$result['id']);
                                $do_more=false;
                                }
                                else
                                {
                                    $db2->query("delete from `subcontracted` where id=".$result['id']);
                                    $stmt->execute(array($result['to_c'],$id));
                                }
                            }
                          $stmt=$db2->prepare("insert into `subcontracted` values (null,?,?,?)");
                          $stmt->execute(array($id,$owner,$values['subcontracted']['to_c']));
                          $stmt->execute(array($id,$values['subcontracted']['to_c'],$values['subcontracted']['to_c']));
                          return true;
                          
                           }
                  }
                  else 
                  {
                      $stmt=$db2->prepare("select * from `subcontracted` where `from_c`=? and `jobid`=?");
                      $stmt->execute(array($owner,$id));
                       if ($stmt->rowCount()>0)
                       {
                           $do_more=true;
                            while ($do_more) {
                                $result=$stmt->fetch();
                                if ($result['from_c']==$result['to_c'])
                                {
                                $db2->query("delete from `subcontracted` where id=".$result['id']);
                                $do_more=false;
                                }
                                else
                                {
                                    $db2->query("delete from `subcontracted` where id=".$result['id']);
                                    $stmt->execute(array($result['to_c'],$id));
                                }
                            }
                          $stmt=$db2->prepare("insert into `subcontracted` values (null,?,?,?)");
                          $stmt->execute(array($id,$owner,$owner)); 
                       }
                      
                     $stmt=$db2->prepare("insert into `jobs_assigned` values (null,?,?)");
                     $stmt->execute(array($id,$values['jobs_assigned']['operativeid']));
                  }
                  
              } 
              
           } );
            
        
        
        
        $editor->process( $_POST );
        $editor->json();
    
  • allanallan Posts: 61,726Questions: 1Answers: 10,110 Site admin
    Answer ✓

    Could you explain a little bit about what it should do, what it isn't doing and what it is doing please? I can see the queries, but I'm not sure what the intended goal is.

    Thanks,
    Allan

  • borconiborconi Posts: 56Questions: 19Answers: 1

    Hi Alan.
    In a nutshell, I have a view where the end user see the Company name, company location, job description, job start date, job frequency, subcontractor, operative and he/she can edit the following 3 fields:

    1) Job description (select from predefined values)
    2) Subcontractor
    3) Operative.

    Subcontractor and Operative cannot coexist at the same time so a job is either subcontracted either assigned to an operative (we call the employee operative)

    The ugly part is, that a subcontracted job, can be further subcontracted out, like X more levels down the chain but you as the individual in the chain you do not know (and you shouldn't know) if that is the case, you only know you have either give that job to your own employe, either subcontracted out. If you change a job which was subcontracted out, to somebody, the script needs to clear all the elements down the chain, but only down, because there might be elements up the chain in the subcontracted hierarchy. All this is done in the preEdit event.

    Now when I changed something I was/am expecting to see the values updated in the view on the client side. When I opened this question the view was not update, all the data was changed correctly inside the database, but the HTML table wasn't reflecting this changes.

    I haven't change anything since then, but now it is working as expected so I have NO CLUE what caused the behaviour.

    I'm loading all the JS and CSS from the CDN, expect the editor itself which is a local copy, so my only logical explanation to this is that something loaded wrongly from the CDN, because I cannot think of any other logical explanation.

    You can consider the question answered, thank you.

This discussion has been closed.