function region($fieldName, $options = array()) { $defOpt = array('countrySelect' => null, 'options' => true, 'other' => array('label' => __('Other', true), 'optLabel' => true, 'div' => array('class' => 'input otherInput text')), 'div' => array('class' => array('input', 'extendedSelect'))); if (!empty($options['countrySelect'])) { $defOpt = array_merge($defOpt, array('empty' => array('div' => array('class' => array('input', 'extendedSelectCase', 'extendedSelectEmpty', 'select')), 'other' => true), 'cases' => array('div' => array('class' => array('input', 'extendedSelectCase', 'select'), 'style' => 'display:none'), 'disabled' => 'disabled'), 'more' => array('div' => array('class' => 'input extendedSelectCase extendedSelectMore text', 'style' => 'display:none'), 'disabled' => 'disabled'))); } else { $defOpt = array_merge($defOpt, array('div' => array('class' => 'input extendedSelect select'))); } $opt = $this->mergeOpt($defOpt, $options); $loadScript = false; $out = null; App::import('Lib', 'SparkForm.Geography'); $regions = array(); $needMore = false; if ($opt['options'] === true) { $selected = Geography::getCountries(array_keys($regions)); } else { $selected = Set::normalize($opt['options']); } $opt['options'] = array(); foreach ($selected as $key => $country) { if (!empty($country) && is_string($country)) { $label = $country; } elseif (is_array($country) && !empty($country['label'])) { $label = $country['label']; } else { $label = Geography::getCountry($key); } $cregions = Geography::getRegions($key); if (is_array($country) && !empty($country['regions']) && !is_array($country['regions'])) { $selected = Set::normalize($country['regions']); $cregions = array_intersect_key($cregions, $selected); } if (!empty($cregions)) { $regions[] = array('label' => array('text' => Geography::getLocalTerm('region', $key)), 'rel' => $key, 'options' => $cregions); $opt['options'][$label] = $cregions; } else { $needMore = true; } } if (!$needMore) { $opt['more'] = false; } $allOpt = $opt['options']; if ($opt['other']) { $loadScript = true; if ($opt['other'] === true) { $opt['other'] = array(); } elseif (!is_array($opt['other'])) { $opt['other'] = array('label' => $opt['other']); } $opt['other'] = array_merge($defOpt['other'], $opt['other']); if ($opt['other']['optLabel'] === true) { $opt['other']['optLabel'] = $opt['other']['label']; } $allOpt['other'] = $opt['other']['optLabel']; unset($opt['other']['optLabel']); } $localOpt = array('countrySelect', 'options', 'other', 'empty', 'cases', 'more', 'type'); $fowardOpt = array_diff_key($options, array_flip($localOpt)); if (empty($opt['countrySelect'])) { $opt['options'] = $allOpt; $opt['type'] = null; if ($opt['other']) { $opt['after'] = $this->input($fieldName . '_other', $opt['other']); } $out = $this->input($fieldName, $opt); } else { if (preg_match('/[_a-z0-9]/i', $opt['countrySelect'])) { $opt['countrySelect'] = '#' . $this->domId($opt['countrySelect']); $this->setEntity($fieldName); } $loadScript = true; $out = ''; if ($opt['empty']) { $opt['empty'] = $this->mergeOpt($defOpt['empty'], (array) $opt['empty'], $fowardOpt); $opt['empty']['options'] = $allOpt; if ($opt['empty']['other']) { $opt['empty']['after'] = $this->input($fieldName . '_other', $opt['other']); } $out .= $this->input($fieldName, $opt['empty']); } foreach ($regions as $region) { $caseOpt = $this->mergeOpt($opt['cases'], $region, $fowardOpt); $caseOpt['div']['rel'] = $region['rel']; unset($caseOpt['rel']); $out .= $this->input($fieldName, $caseOpt); } if ($opt['more']) { $opt['more'] = array_merge($defOpt['more'], (array) $opt['more']); $out .= $this->input($fieldName, $opt['more']); } if ($opt['div']) { $opt['div']['linked'] = $opt['countrySelect']; $out = '<div' . $this->_parseAttributes($opt['div']) . '>' . $out . '</div>'; } } if ($loadScript) { $this->Html->script('/spark_form/js/region_select', array('inline' => false)); } return $out; }