3
\$\begingroup\$

School HABTM AthleticDirector

I am building a tool for assigning athletic directors (ADs) to schools. The tool has a school menu/filter which, when selected, shows a list of ADs already assigned to the school, and those yet to be assigned.

The tool can be used in two ways:

  1. in a stand-alone fashion, where you arrive at the tool with no school id, then select a school, OR
  2. where you arrive FROM the school list via an "assign ADs" link, in which case the school id is known.

I have this working, but it seems like there might be a better way. Mainly, I'm wondering if there's some way to not need to support retrieving the school id in two different manners. Though maybe that's inevitable?

Here's my code:

When viewing the school list, the Assign AD link is as so, passing the school id as a param:

// e.g. /assign_school_ads/5
echo $this->Html->link(__('Assign ADs'), array('action' => 'assign_school_ads', $school['School']['id']));

The controller is then written to handle the school $id either as a param (from the assign link) or as data (from a school menu selection)

//School Controller
public function assign_school_ads($id = null) {

   $this->School->id = ($id) ? $id : $this->data['School']['school_id'];

    if ($id AND !$this->School->exists()) {

        throw new NotFoundException(__('Invalid school.'));
    }

    $assignedSchoolADs = $this->School->getSchoolADs();

    // school menu
    $schools = $this->School->find('list', array(
        'conditions' => array(
            'School.status' => true,
            ),
        ));

    $this->set(compact('assignedSchoolADs', 'schools'));
}

//Model
    public function getSchoolADs() {

    $params = array(
        'contain' => array(
            'AthleticDirector' => array(
                'fields' => array ('AthleticDirector.name'),
                'order' => array ('AthleticDirector.name' => 'ASC'),
                'conditions' => array ('AthleticDirector.status' => true),
                ),

            ),
        'conditions' => array(
            'School.id' => $this->id
            ),
        'fields' => array(
            'School.id AS schoolID'
            ),
    );

    return $this->find('first', $params);
}

//View
<?php

$data = $this->Js->get('#SchoolFilter')->serializeForm(array('isForm' => true, 'inline' => true));

$this->Js->get('#SchoolSchoolId')->event(
        'change', $this->Js->request(
                array('action' => 'assign_school_ads'), array(
                    'update' => '#content',
                    'data' => $data,
                    'async' => true,
                    'dataExpression' => true,
                    'method' => 'POST'
                )
        )
);

// School Filter
$schoolFilter = $this->Form->create('School', array_merge(array('id' => 'SchoolFilter'), $this->params['pass']));
$schoolFilter .= $this->Form->input('school_id', array('label'=>'Schools', 'empty'=>'- select -', 'default' => $this->params['pass']));
$schoolFilter .= $this->Form->end();

?>

<div class="schools index">

<h2><?php echo __('Assign School Athletic Directors'); ?></h2>

<div id ="searchFilters">
<?php
    echo $schoolFilter;
?>
</div>

<!-- EXISTING AD LIST -->

    <table>
       <?php

            if (!empty($assignedSchoolADs)) {

                if (!empty($assignedSchoolADs['AthleticDirector'])) {

                    foreach($assignedSchoolADs['AthleticDirector'] as $ad){

                    echo '
                        <tr>
                            <td>' . $ad['name'] . '</td>
                            <td>' . $ad['AthleticDirectorsSchool']['id'] . '</td>
                        </tr>';
                    }

                } else {

                    echo '
                        <tr>
                            <td>There are no Athletic Directors assigned to this school.</td>
                        </tr>';
                }

            } else {

                echo '
                    <tr>
                        <td>Please select a school.</td>
                    </tr>';
            }

        ?>

    </table>
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.