0

I'm using ATK4 4.1.1 and have added a button to a page. I want this button to trigger an update to a database table when it is pressed and so have created code like this in a page.

    $f=$p->add('Form',null,null,array('form_empty'))->setFormClass('horizontal bottom-padded');

    $b1=$f->add('Button')->set('Finished');
    $b1->js("click")->univ()->ajaxec($this->api->url(null, array("s"=>"D")));

    $b1->js('click', $b1->js()->load(
      $this->api->url(null,array('s'=>'D'))
    ) );

    ..  ..   ..  ..  

   if (isset($_GET['s'])) {            
      $ss=$this->add('Model_SprintStatus')
               ->loadBy(array('member_id'=>$p->api->auth->get('id')));
       $new_status=$_GET['s'];
       $ss->set('status',$new_status);
       $ss->update();
    }

When i access this page, it displays ok but when the button is clicked, i get an error saying method

BaseException

 Method is not defined for this object

 Additional information:

 method: url
 arguments: Array ( [0] => [1] => Array ( [s] => D ) 

I used an example from the following agiletoolkit.org section called Anatomy of a reload. As i got this error, i took the example and created a new page using the same code as the example and i get a similar error from that page as well.

  BaseException

  Method is not defined for this object

  Additional information:

  method: url
  arguments: Array ( [0] => [1] => Array ( [side] => 1 [myajax] => 1 ) ) 

Besides trying the above ajaxec line, i also tried the following

 $b1->js('click', $b1->js()->load( $this->api->url(null,array('s'=>'D'))));

and

 $b1->js('click', $b1->js()->atk4_load( $this->api->url(null,array('s'=>'D'))));

but they also come back with the same error.

Maybe i've missed something or it's possible it's a change between ATK4 4.1.1 and 4.2 but i'm not in a position to upgrade at this time as trying to meet a deadline so what method do i have to execute this update from the button click in ATK 4.1.1

Thanks

Jasonw
  • 5,054
  • 7
  • 43
  • 48
Trevor North
  • 2,286
  • 1
  • 16
  • 19

2 Answers2

1

There is an easier way:

if($this->add('Button')->setLabel('Clickme')->isClicked()){

      // ....

      $this->js()->univ()->successMessage('Updated')
           ->page($this->api->getDestinationURL())->execute();

}

This was only briefly documented in http://agiletoolkit.org/whatsnew/may2011

Trevor North
  • 2,286
  • 1
  • 16
  • 19
romaninsh
  • 10,606
  • 4
  • 50
  • 70
  • Can you confirm the syntax on addButton ? On 4.1, i'm getting an error saying Method is not defined for this object addButton. – Trevor North Aug 04 '12 at 18:11
  • OK - worked out the correct syntax for add Button and have amended the above but now getting an ajax error when the button is pressed with no info as to why - any suggestions ? The POST URL is http://team.pscrum/?page=tktdetail&paperless_tktdetail_button=clicked – Trevor North Aug 04 '12 at 18:23
0

And the answer is ...

    // create a form to put the button in with minimal styling
    $f=$p->add('Form',null,null,array('form_empty'))->setFormClass('horizontal bottom-padded');

    // Add the button with a confirm request and do an ajax call
    // passing a new GET parameter called state
    $f->add('Button')->set('Update')
        ->js('click')->univ()->confirm('Confirm ?')
        ->ajaxec($this->api->getDestinationURL(null,array('state'=>'D')));

    // If the parameter is set, this is executed by the ajax callback
    if (isset($_GET['state'])) {             
         //add the model (change name to yours)
         // You can either use load($id) if you already have the primary key
         // or an array of fields and use loadBy to get the records to be updated 
         $ss=$this->add('Model_SprintStatus')->loadBy(array(
                'member_id'=>$p->api->auth->get('id')
                )
              );

          // set some variables to hold the old status from the model 
          // and the new one which was in the GET parameter we passed
          $old_status=$ss->get('status');
          $new_status=$_GET['state'];

          // Set the status column to the new value and execute the update
          $ss->set('status',$new_status);
          $ss->update();

          // and provide some feedback to the user that it worked !
          $this->js()->univ()->successMessage('Updated')
               ->page($this->api->getDestinationURL())->execute();
    }

If you dont want any feedback to the user, you can omit the ->confirm() on the button definition and the whole line containing successMessage and presumably you could also change the state of the button by changing it's class inside the if (isset($_GET...

Trevor North
  • 2,286
  • 1
  • 16
  • 19