예제 #1
0
 /**
  * 过滤输入、创建群联系人对象
  * @param array $data 联系人信息
  * @return Group_Contact $contact
  */
 public function array_to_Group_contact($data)
 {
     $contact = new Group_Contact();
     $location_model = Location_Model::instance();
     $bjx_arr = Kohana::config_load('bjx');
     foreach ($data as $type => $value) {
         switch ($type) {
             case 'tels':
                 if (!empty($value)) {
                     $values = $tmp = array();
                     foreach ($value as $val) {
                         if (!in_array(trim($val['value']), $tmp)) {
                             $tmp[] = trim($val['value']);
                             $values[] = array('value' => trim($val['value']), 'type' => $val['type'], 'city' => $location_model->get_tel_location(trim($val['value'])), 'pref' => !empty($val['pref']) ? (int) $val['pref'] : 0);
                         }
                     }
                     call_user_func(array($contact, 'set_' . $type), $values);
                 }
                 break;
             case 'ims':
                 if (!empty($value)) {
                     $values = $tmp = $protocols = array();
                     foreach ($value as $val) {
                         $val['protocol'] = strtolower($val['protocol']);
                         $keys = array_keys($tmp, trim($val['value']));
                         $key = isset($keys[0]) ? $keys[0] : -1;
                         if ($key < 0 or $protocols[$key] != $val['protocol']) {
                             $tmp[] = trim($val['value']);
                             $protocols[] = $val['protocol'];
                             $values[] = array('value' => trim($val['value']), 'protocol' => $val['protocol'], 'type' => $val['type']);
                         }
                     }
                     call_user_func(array($contact, 'set_' . $type), $values);
                 }
                 break;
             case 'addresses':
                 if (!empty($value)) {
                     $values = $tmp = array();
                     $t = '';
                     foreach ($value as $val) {
                         $t = trim($val['country']) . '|' . trim($val['region']) . '|' . trim($val['city']) . '|' . trim($val['street']) . '|' . trim($val['postal']);
                         if (!in_array($t, $tmp)) {
                             $values[] = array('country' => trim($val['country']), 'region' => trim($val['region']), 'city' => trim($val['city']), 'street' => trim($val['street']), 'postal' => trim($val['postal']), 'type' => $val['type']);
                             $tmp[] = $t;
                         }
                     }
                     call_user_func(array($contact, 'set_' . $type), $values);
                 }
                 break;
             case 'emails':
             case 'urls':
             case 'events':
             case 'relations':
                 if (!empty($value)) {
                     $values = $tmp = array();
                     foreach ($value as $val) {
                         if (!in_array(trim($val['value']), $tmp)) {
                             $tmp[] = trim($val['value']);
                             $values[] = array('value' => trim($val['value']), 'type' => $val['type']);
                         }
                     }
                     call_user_func(array($contact, 'set_' . $type), $values);
                 }
                 break;
             case 'birthday':
                 $contactModel = Contact_Model::instance();
                 call_user_func(array($contact, 'set_' . $type), !empty($value) ? $contactModel->_filter_birthday($value) : '');
                 break;
             case 'id':
                 break;
             default:
                 call_user_func(array($contact, 'set_' . $type), !empty($value) ? $value : '');
                 break;
         }
     }
     $formatted_name = $this->name_to_formatted_name($data['family_name'], $data['given_name']);
     //拼接后的全名为空,并且输入的全名不是空的,把全名拆分设置
     if (empty($formatted_name) and !empty($data['formatted_name'])) {
         $name = $this->formatted_name_to_name($data['formatted_name']);
         $contact->set_given_name($name['given_name']);
         $contact->set_family_name($name['family_name']);
     } else {
         $fn = $formatted_name;
     }
     if (!empty($fn)) {
         require_once Kohana::find_file('vendor', 'pinyin/c2p');
         $phonetic = getPinYin($fn, false, ' ');
         $tmp = explode(' ', $phonetic);
         $sort = '';
         if (is_array($tmp)) {
             foreach ($tmp as $t) {
                 $sort .= isset($t[0]) ? $t[0] : '';
             }
         }
         $t = ord($sort[0]);
         if (empty($sort) or $t < 97 or $t > 122) {
             $sort = '#';
         }
         $sort = substr($sort, 0, 20);
         $contact->set_formatted_name($fn);
         $contact->set_phonetic(implode('', $tmp));
         $contact->set_sort($sort);
     } else {
         $contact->set_formatted_name('');
         $contact->set_phonetic('');
         $contact->set_sort('#');
     }
     return $contact;
 }