Turba_Driver::toDriverKeys PHP Method

toDriverKeys() public method

Translates the keys of the first hash from the generalized Turba attributes to the driver-specific fields. The translation is based on the contents of $this->map.
public toDriverKeys ( array $hash ) : array
$hash array Hash using Turba keys.
return array Translated version of $hash.
    public function toDriverKeys(array $hash)
    {
        if (!empty($hash['name']) && !empty($this->listNameField) && !empty($hash['__type']) && is_array($this->map['name']) && $hash['__type'] == 'Group') {
            $hash[$this->listNameField] = $hash['name'];
            unset($hash['name']);
        }
        // Add composite fields to $hash if at least one field part exists
        // and the composite field will be saved to storage.
        // Otherwise composite fields won't be computed during an import.
        foreach ($this->map as $key => $val) {
            if (!is_array($val) || empty($this->map[$key]['attribute']) || array_key_exists($key, $hash)) {
                continue;
            }
            foreach ($this->map[$key]['fields'] as $mapfields) {
                if (isset($hash[$mapfields])) {
                    // Add composite field
                    $hash[$key] = null;
                    break;
                }
            }
        }
        $fields = array();
        foreach ($hash as $key => $val) {
            if (!isset($this->map[$key])) {
                continue;
            }
            if (!is_array($this->map[$key])) {
                $fields[$this->map[$key]] = $val;
            } elseif (!empty($this->map[$key]['attribute'])) {
                $fieldarray = array();
                foreach ($this->map[$key]['fields'] as $mapfields) {
                    $fieldarray[] = isset($hash[$mapfields]) ? $hash[$mapfields] : '';
                }
                $fields[$this->map[$key]['attribute']] = Turba::formatCompositeField($this->map[$key]['format'], $fieldarray);
            } else {
                // If 'parse' is not specified, use 'format' and 'fields'.
                if (!isset($this->map[$key]['parse'])) {
                    $this->map[$key]['parse'] = array(array('format' => $this->map[$key]['format'], 'fields' => $this->map[$key]['fields']));
                }
                foreach ($this->map[$key]['parse'] as $parse) {
                    $splitval = sscanf($val, $parse['format']);
                    $count = 0;
                    $tmp_fields = array();
                    foreach ($parse['fields'] as $mapfield) {
                        if (isset($hash[$mapfield])) {
                            // If the compositing fields are set
                            // individually, then don't set them at all.
                            break 2;
                        }
                        $tmp_fields[$this->map[$mapfield]] = $splitval[$count++];
                    }
                    // Exit if we found the best match.
                    if ($splitval[$count - 1] !== null) {
                        break;
                    }
                }
                $fields = array_merge($fields, $tmp_fields);
            }
        }
        return $fields;
    }

Usage Example

Example #1
0
 /**
  * Translates the keys of the first hash from the generalized Turba
  * attributes to the driver-specific fields. The translation is based on
  * the contents of $this->map.
  *
  * @param array $hash  Hash using Turba keys.
  *
  * @return array  Translated version of $hash.
  */
 public function toDriverKeys(array $hash)
 {
     if (isset($hash['__tags'])) {
         if (!is_array($hash['__tags'])) {
             $hash['__tags'] = $GLOBALS['injector']->getInstance('Turba_Tagger')->split($hash['__tags']);
         }
         usort($hash['__tags'], 'strcoll');
         $hash['__internaltags'] = serialize($hash['__tags']);
     }
     $hash = parent::toDriverKeys($hash);
     if (isset($hash['name'])) {
         $hash['name'] = array('full-name' => $hash['name']);
     }
     /* TODO: use Horde_Kolab_Format_Xml_Type_Composite_* */
     foreach (array('full-name', 'given-name', 'middle-names', 'last-name', 'initials', 'prefix', 'suffix') as $sub) {
         if (isset($hash[$sub])) {
             $hash['name'][$sub] = $hash[$sub];
             unset($hash[$sub]);
         }
     }
     if (isset($hash['__type']) && $hash['__type'] == 'Group' && isset($hash['name']['full-name'])) {
         $hash['display-name'] = $hash['name']['full-name'];
     }
     if (isset($hash['emails'])) {
         $list = new Horde_Mail_Rfc822_List($hash['emails']);
         $hash['email'] = array();
         foreach ($list as $address) {
             $hash['email'][] = array('smtp-address' => $address->bare_address);
         }
         unset($hash['emails']);
     }
     foreach (array('phone-business1', 'phone-business2', 'phone-businessfax', 'phone-car', 'phone-company', 'phone-home1', 'phone-home2', 'phone-homefax', 'phone-mobile', 'phone-pager', 'phone-radio', 'phone-assistant') as $sub) {
         if (isset($hash[$sub])) {
             if (!isset($hash['phone'])) {
                 $hash['phone'] = array();
             }
             $hash['phone'][] = array('type' => substr($sub, 6), 'number' => $hash[$sub]);
             unset($hash[$sub]);
         }
     }
     $address = array();
     foreach (array('addr-business-street', 'addr-business-locality', 'addr-business-region', 'addr-business-postal-code', 'addr-business-country') as $sub) {
         if (isset($hash[$sub])) {
             $address[substr($sub, 14)] = $hash[$sub];
             unset($hash[$sub]);
         }
     }
     if ($address) {
         $hash['address'] = array();
         $address['type'] = 'business';
         $hash['address'][] = $address;
     }
     $address = array();
     foreach (array('addr-home-street', 'addr-home-locality', 'addr-home-region', 'addr-home-postal-code', 'addr-home-country') as $sub) {
         if (isset($hash[$sub])) {
             $address[substr($sub, 10)] = $hash[$sub];
             unset($hash[$sub]);
         }
     }
     if ($address) {
         if (!isset($hash['address'])) {
             $hash['address'] = array();
         }
         $address['type'] = 'home';
         $hash['address'][] = $address;
     }
     if (isset($hash['categories'])) {
         $hash['categories'] = unserialize($hash['categories']);
     }
     if (!empty($hash['birthday'])) {
         $hash['birthday'] = new DateTime($hash['birthday']);
     }
     if (!empty($hash['anniversary'])) {
         $hash['anniversary'] = new DateTime($hash['anniversary']);
     }
     return $hash;
 }
All Usage Examples Of Turba_Driver::toDriverKeys