
Untitled
By: a guest on
May 1st, 2012 | syntax:
None | size: 1.65 KB | hits: 11 | expires: Never
public function findOptions(array $existing_selection=NULL){
$values = array();
$limit = $this->get('limit');
if (!$this->get('related_field_id')) return $values;
// find the sections of the related fields
$sections = $this->Database->fetch("SELECT DISTINCT (s.id), s.name, f.id as `field_id`
FROM `tbl_sections` AS `s`
LEFT JOIN `tbl_fields` AS `f` ON `s`.id = `f`.parent_section
WHERE `f`.id IN ('" . implode("','", $this->get('related_field_id')) . "')
ORDER BY s.sortorder ASC");
foreach($sections as $section){
$group = array('name' => $section['name'], 'section' => $section['id'], 'values' => array());
// build a list of entry IDs with the correct sort order
$entryManager = new EntryManager($this->_Parent->_Parent);
$entries = $entryManager->fetch(NULL, $section['id'], $limit, 0);
$results = array();
foreach($entries as $entry) $results[] = $entry->get('id');
// if a value is already selected, ensure it is added to the list (if it isn't in the available options)
if(!is_null($existing_selection) && !empty($existing_selection)){
foreach($existing_selection as $key => $entry_id){
$x = $this->findFieldIDFromRelationID($entry_id);
if($x == $section['field_id']) $results[] = $entry_id;
}
}
if(is_array($results) && !empty($results)){
foreach($results as $entry_id){
$value = $this->__findPrimaryFieldValueFromRelationID($entry_id);
$group['values'][$entry_id] = $value['value'];
}
}
$values[] = $group;
}
return $values;
}