/** * Returns an array of options and wether or not they are selected for this user. * Used for rendering sets of checkboxes. */ public static function choicesList($intersection_table, $owner_col, $owner_id, $option_table, $option_cols = false, $join_cols = false, $remove_unknown = true) { //do something like this: /* SELECT tlo.id, tlo.language_phrase, ttlo.trainer_language_option_id FROM trainer_language_option as tlo LEFT JOIN trainer_to_trainer_language_option as ttlo ON ttlo.trainer_language_option_id = tlo.id AND ttlo.trainer_id = xx WHERE tlo.is_deleted = 0 */ $cols[] = 'id'; if (is_array($option_cols)) { $cols = array_merge($cols, $option_cols); } else { if ($option_cols) { $cols[] = $option_cols; } } if (is_array($join_cols)) { foreach ($join_cols as $ecol) { $cols[] = 'c.' . $ecol; } } else { if ($join_cols) { $cols[] = 'c.' . $join_cols; } } $optionsTable = new MultiOptionList(array('name' => $option_table)); $select = $optionsTable->select()->from($option_table, $cols)->setIntegrityCheck(false); if ($owner_id || $join_cols) { if (!$owner_id) { $owner_id = 0; } $select->joinLeft(array('c' => $intersection_table), $option_table . '.id = c.' . $option_table . '_id AND c.' . $owner_col . ' = ' . $owner_id, 'c.' . $owner_col); } //might want to add to base class eventually $info = $optionsTable->info(); if (array_search('is_deleted', $info['cols']) !== false) { $select->where('is_deleted = 0'); } if (isset($cols[1])) { $select->order($cols[1] . ' ASC'); } $rows = $optionsTable->fetchAll($select); $rowArray = $rows->toArray(); // unset 'unknown' if ($option_cols and $remove_unknown) { foreach ($rowArray as $key => $row) { if (is_array($option_cols)) { foreach ($option_cols as $option_col) { if ($row[$option_col] == 'unknown') { unset($rowArray[$key]); } } } else { if ($option_cols == 'unknown') { unset($rowArray[$key]); } } } } return $rowArray; }