Wednesday, May 23, 2012

Re: Passing data to controller action based on button clicked

You could also forgo the hidden fields, and just grab the needed ID at the time of redirect:
eg.
 if($this->data['Submit'] == 'Previous){
  $this->redirect(array('controller' => 'drugs', 'action' => 'view', $this->Drug->getPrevious($id));
 }

But with the first approach, you would have the information available as to whether there actually was a next or previous item, and not display the button if there wasn't.

-d

On Wednesday, May 23, 2012 3:07:32 PM UTC-4, Dave Milsom wrote:
Off the top of my head, here is what I would do:

1. In the edit function, grab the IDs for the previous and next items. I would probably have two methods in the model, getNext($id) and getPrevious($id), or something like that, which return the IDs for the next and previous items, respectively. Then you can do $this->set('previous', $this->Drug->getPrevious($id)); and $this->set('next', $this->Drug->getNext($id));
 
2. In your edit view, have two hidden fields:
 echo $this->Form->hidden('Drug.next_id', array('value'=>$next));
 echo $this->Form->hidden('Drug.previous_id', array('value'=>$previous));

3. Your buttons:
 echo $this->Form->submit('Save', array('name' => 'Submit')); 
 echo $this->Form->submit('Previous', array('name' => 'Submit')); 
 echo $this->Form->submit('Next', array('name' => 'Submit')); 

4. In your edit function, save the data, then check for which button was pushed, and redirect to the appropriate place. I wasn't sure if you wanted the previous and next to go to the edit views of those or not:
 if($this->data['Submit'] == 'Previous){
  $this->redirect(array('controller' => 'drugs', 'action' => 'view', $this->data['Drug']['previous_id']));
 } else {
 ..... etc
 
I think this works, and seems a bit cleaner than your approach. Like I said, it's off the top of my head, so there may be some small bits which need massaging.

-d

On Wednesday, May 23, 2012 9:59:38 AM UTC-4, JonQCoder wrote:
In my app I have an edit view; the view has a standard save button. I
would like to  have a save, previous, and next button. I have the
controller actions done to move to the next and previous item, but I
don't know how to pass the data to it from the edit view/controller.
Right now if I click the previous or next button it just does the
standard save. So I tried this:

    echo $this->Form->submit('Save', array('name'=>'save');
    echo $this->Form->submit('Save & Next', array('name'=>'savenext');

//In the controlller edit function
    if (isset($this->params['form']['submit']) && $this->params['form']
['submit'] == "savenext") {
      // Save and next button Clicked
    }

I would like for it save the edit and move to the next item. I also
tried calling the function directly and passing it the data like so:
$this->next($id,$generic), but it doesn't have the rest of the data so
it won't save. If I passed the function $this->data, would it work
correctly? I changed the buttons to links just to be sure it's
working, and it is, so ignore the link and see it as I have it above.
Any advice? Code below.

<div class="frenchTranslations form">
<table>
  <?php echo $this->Form->create('FrenchTranslation');?>
  <tr>
      <td colspan="3">
<?php __($drug['Drug']['generic']); ?>
</td>
      <td align="right">
<?php __($drug['Drug']['lrc']); ?>
</td>
  </tr>
  <tr>
      <td>Foreign TradeName:</td>
      <td colspan="3"><?php echo $this->Form-
>input('french_tradenames', array('style'=>'height: 25px;',
'label'=>false)); ?></td>
  </tr>
  <tr>
      <td>Classification:</td>
      <td><?php __($drug['Drug']['category']); ?></td><td
colspan="2"><?php echo $this->Form->input('french_category',
array('label'=>false, 'size'=>70)); ?></td>
    </tr>
    <tr>
        <td>T<sup>1/2</sup> = <?php __($drug['Drug']['ahl']); ?></td>
        <td>Oral = <?php __($drug['Drug']['oral']); ?></td>
        <td>Mw = <?php __($drug['Drug']['mw']); ?></td>
        <td>RID = <?php __($drug['Drug']['rid']); ?></td>
  </tr>
  <tr>
      <td colspan="4"><p>Clinical Comments:</p></td>
    </tr>
    <tr>
      <td colspan="4"><?php __($drug['Drug']
['clinical_recommendations']); ?></td>
  </tr>
  <tr>
    <td colspan="4"><?php echo $this->Form-
>input('french_clinical_recommendations', array('label'=>'Translate
Clinical Comments:')); ?></td>
    <?php echo $this->Form->input('id',array('type'=>'hidden','value'=>
$drug['Drug']['id'])); ?>
    <?php echo $this->Form->input('drug_id', array('type' =>
'hidden','value' => $drug['Drug']['id'])); ?>
    <?php echo $this->Form->input('user_id',
array('type'=>'hidden','value'=>$user)); ?>

    <?php echo $this->Form-
>input('User.id',array('type'=>'hidden','value'=>$user));//The user ID
last edit will be stored in ?>
    <?php echo $this->Form-
>input('User.last_edit',array('type'=>'hidden','value'=>$drug['Drug']
['generic']));//The drug edited last by the specified user ?>
  </tr>
</table>

<?php echo $this->Form->submit('Save',
    array('before'=>$this->Html->link('Previous
Drug',array('action'=>'previous',$drug['Drug']['id'],$drug['Drug']
['generic'])) . "   ",'after'=>"   ". $this->Html->link('Next
Drug',array('action'=>'next',$drug['Drug']['id'],$drug['Drug']
['generic'])))); ?>
<?php echo $this->Form->submit('Next', array('name'=>'savenext')); ?>
<?php echo $this->Form->end(); ?>
</div>

----------------------------------------------------
function edit($id = null) {
    $drug = $this->FrenchTranslation->Drug->read(
      array(
 
'Drug.id','Drug.generic','Drug.ahl','Drug.aap','Drug.rid','Drug.oral','Drug.mw','Drug.clinical_recommendations',
          'Drug.category','Drug.lrc'
      ),
      $id
    );

    $this->set('drug',$drug);

                if (!$id && empty($this->data)) {
                        $this->Session->setFlash(__('Invalid french
translation', true));
                        $this->redirect(array('action' => 'index'));
                }
                if (!empty($this->data)) {
                        if ($this->FrenchTranslation->saveAll($this-
>data)) {
                                $this->Session->setFlash(__('The
french translation has been saved', true));
                                //$this-
>redirect(array('controller'=>'drugs','action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The
french translation could not be saved. Please, try again.', true));
                        }
                }
                if (empty($this->data)) {
                        $this->data = $this->FrenchTranslation-
>read(null, $id);
                }
                $drugs = $this->FrenchTranslation->Drug->find('list');
                $this->set(compact('drugs'));
        }


---------------------------------------------------
  /**
   * Get the next drug in alphabetical order.
   * @param string $generic
   *   The generic name of the drug we are currently on, so we know
which is the next drug.
   */
  function next($id = null,$generic = null) {
    $drugs = $this->FrenchTranslation->Drug->find('list', array(
      'fields' => array('Drug.id', 'Drug.generic'),
      'order' => 'Drug.generic',
      'recursive' => -1)
    );

    $this->FrenchTranslation->id = $id;
    if($this->FrenchTranslation->read()){
      $this->Session->setFlash(__('The drug existed, moved next.',
true));
      //$this->redirect(array('action'=>'edit',$id));
    } else {
    $this->Session->setFlash(__('The drug did not exist, created it,
moved next.', true));
      //$this->redirect(array('action'=>'add', $id));
    }

    $next = false; // set to true when we matched the current drug and
want to get the key of the next one
    foreach($drugs as $key => $drug):
      debug($drug);
      // this is the drug we want to load
      if ($next):
        $this-
>redirect(array('controller'=>'french_translations','action' =>
'view', $key));
      endif;
      // this is the current drug, so flag it so we know next drug we
want to load
      if ($drug == $generic):
        $next = true;
      endif;
    endforeach;
  }

----------------------------------------------------------------
  /**
   * Get the previous drug in alphabetical order.
   * @param string $generic
   *   The generic name of the drug we are currently on, so we know
which is the previous drug.
   */
  function previous($id = null,$generic = null) {
    $drugs = $this->FrenchTranslation->Drug->find('list', array(
      'fields' => array('Drug.id', 'Drug.generic'),
      'order' => 'Drug.generic DESC',
      'recursive' => -1)
    );
    $next = false; // set to true when we matched the current drug and
want to get the key of the previous one
    foreach($drugs as $key => $drug):
      //debug($drug);
      // this is the drug we want to load
      if ($next):
        $this-
>redirect(array('controller'=>'french_translations','action' =>
'view', $key));
      endif;
      // this is the current drug, so flag it so we know next drug we
want to load
      if ($drug == $generic):
        $next = true;
      endif;
    endforeach;
  }
}

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: