예제 #1
0
 /**
  * 验证属性是否存在
  * @param string $name 属性名
  * @return 返回属性名
  */
 protected function validate_attribute($name)
 {
     if (empty(self::$keys)) {
         self::$keys = array_keys(get_class_vars(get_class($this)));
     }
     if (in_array($name, self::$keys)) {
         return $name;
     }
 }
예제 #2
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;
 }
예제 #3
0
 /**
  * 根据群联系人对象创建群联系人
  * @param Group_Contact $group_contact 群联系人对象
  * @throws Exception
  */
 public function insert($group_contact)
 {
     $query = $this->db->query("INSERT INTO `gcp_contacts` (`gid`,`uid`, `formatted_name`," . " `phonetic`, `given_name`, `middle_name`, `family_name`," . " `prefix`, `suffix`, `organization`, `department`, `note`," . " `birthday`, `title`, `nickname`, `sort`," . "`created`, `modified`, `fid`) VALUES (? , ?, " . " ? , ?, ? , ? , ? , ? , ? , ? , ?, ? , ? , ? , ? , ? , ? , ? , ? )", array($group_contact->get_group_id(), $group_contact->get_user_id(), $group_contact->get_formatted_name(), $group_contact->get_phonetic(), $group_contact->get_given_name(), $group_contact->get_middle_name(), $group_contact->get_family_name(), $group_contact->get_prefix(), $group_contact->get_suffix(), $group_contact->get_organization(), $group_contact->get_department(), $group_contact->get_note(), $group_contact->get_birthday(), $group_contact->get_title(), $group_contact->get_nickname(), $group_contact->get_sort(), $group_contact->get_modified_at(), $group_contact->get_modified_at(), $group_contact->get_momo_user_id()));
     $id = $query->insert_id();
     if ($id) {
         $sqls = array_merge($this->_edit_avatar($group_contact->get_group_id(), $id, $group_contact->get_avatar()));
         foreach (array('emails', 'tels', 'addresses', 'ims', 'urls', 'events', 'relations') as $type) {
             $sqls = array_merge($sqls, $this->_add_info($group_contact->get_group_id(), $id, $type, call_user_func(array($group_contact, 'get_' . $type))));
         }
         foreach ($sqls as $sql) {
             $this->db->query($sql);
         }
         $inserted = $this->find_by_id($group_contact->get_group_id(), $id);
         // clean up database related fields in parameter instance
         if (method_exists($inserted, 'set_id')) {
             $group_contact->set_id($inserted->get_id());
             $group_contact->set_created_at($inserted->get_created_at());
             $group_contact->set_modified_at($inserted->get_modified_at());
         }
     } else {
         throw new Exception('DB Error: Add Group_Contact Fail');
     }
 }