Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 1.65 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public function findOptions(array $existing_selection=NULL){
  2.  
  3.                         $values = array();
  4.                         $limit = $this->get('limit');
  5.                        
  6.                         if (!$this->get('related_field_id')) return $values;
  7.                        
  8.                         // find the sections of the related fields
  9.                         $sections = $this->Database->fetch("SELECT DISTINCT (s.id), s.name, f.id as `field_id`
  10.                                                                                                 FROM `tbl_sections` AS `s`
  11.                                                                                                 LEFT JOIN `tbl_fields` AS `f` ON `s`.id = `f`.parent_section
  12.                                                                                                 WHERE `f`.id IN ('" . implode("','", $this->get('related_field_id')) . "')
  13.                                                                                                 ORDER BY s.sortorder ASC");
  14.                        
  15.                         foreach($sections as $section){
  16.  
  17.                                 $group = array('name' => $section['name'], 'section' => $section['id'], 'values' => array());
  18.                                
  19.                                 // build a list of entry IDs with the correct sort order
  20.                                 $entryManager = new EntryManager($this->_Parent->_Parent);
  21.                                 $entries = $entryManager->fetch(NULL, $section['id'], $limit, 0);
  22.                                
  23.                                 $results = array();
  24.                                 foreach($entries as $entry) $results[] = $entry->get('id');
  25.                                
  26.                                 // if a value is already selected, ensure it is added to the list (if it isn't in the available options)
  27.                                 if(!is_null($existing_selection) && !empty($existing_selection)){
  28.                                         foreach($existing_selection as $key => $entry_id){
  29.                                                 $x = $this->findFieldIDFromRelationID($entry_id);
  30.                                                 if($x == $section['field_id']) $results[] = $entry_id;
  31.                                         }
  32.                                 }
  33.                                
  34.                                 if(is_array($results) && !empty($results)){
  35.                                         foreach($results as $entry_id){
  36.                                                 $value = $this->__findPrimaryFieldValueFromRelationID($entry_id);
  37.                                                 $group['values'][$entry_id] = $value['value'];
  38.                                         }
  39.                                 }
  40.  
  41.                                 $values[] = $group;
  42.                         }
  43.  
  44.                         return $values;
  45.  
  46.                 }