<?php
// Storing membership details, multiple rows with styleid and num classes for that style
// connected to a membership

private function _addMembershipDetails($data, $membership, $member)
{
    $errors = false;

    if (!($membership instanceof KDatabaseRowInterface)) {
        $membership_id = null;
    } else {
        $membership_id = $membership->id;
    }

    $controller = KService::get('com://site/studio.controller.membership_detail');

    // At least one class must be selected, so array_sum needs to be > 0
    if (array_sum($data)) {

        foreach ($data as $style_id => $num_classes) {

            // Reset the model before each iteration
            $controller->getModel()->reset();

            if ($num_classes > 0) {
                $data = array(
                        'membership_id'    => $membership_id,
                        'style_id'         => $style_id,
                        'num_classes'      => $num_classes);

                $membership_detail = $controller->add($data);
                
                if (!($membership_detail instanceof KDatabaseRowInterface)) {
                    $errors  = true;
                } 
            }
        }

    } else {
        $errors = true;
    }

    return $errors;
}