1: <?php
2: namespace Ctct\Components\Activities;
3:
4: use Ctct\Components\Component;
5: use Ctct\Util\Config;
6: use Ctct\Exceptions\IllegalArgumentException;
7:
8: 9: 10: 11: 12: 13: 14:
15: class AddContacts extends Component
16: {
17: public $import_data = array();
18: public $lists = array();
19: public $column_names = array();
20:
21: public function __construct(Array $contacts, Array $lists, Array $columnNames = array())
22: {
23: if (!empty($contacts)) {
24: if ($contacts[0] instanceof AddContactsImportData) {
25: $this->import_data = $contacts;
26: } else {
27: $msg = sprintf(Config::get('errors.id_or_object'), "AddContactsImportData");
28: throw new IllegalArgumentException($msg);
29: }
30: }
31:
32: $this->lists = $lists;
33:
34: if (empty($columnNames)) {
35: $usedColumns[] = Config::get('activities_columns.email');
36: $contact = $contacts[0];
37:
38: if (isset($contact->first_name)) {
39: $usedColumns[] = Config::get('activities_columns.first_name');
40: }
41: if (isset($contact->last_name)) {
42: $usedColumns[] = Config::get('activities_columns.last_name');
43: }
44: if (isset($contact->birthday_day)) {
45: $usedColumns[] = Config::get('activities_columns.birthday_day');
46: }
47: if (isset($contact->birthday_month)) {
48: $usedColumns[] = Config::get('activities_columns.birthday_month');
49: }
50: if (isset($contact->anniversary)) {
51: $usedColumns[] = Config::get('activities_columns.anniversary');
52: }
53: if (isset($contact->job_title)) {
54: $usedColumns[] = Config::get('activities_columns.job_title');
55: }
56: if (isset($contact->company_name)) {
57: $usedColumns[] = Config::get('activities_columns.company_name');
58: }
59: if (isset($contact->work_phone)) {
60: $usedColumns[] = Config::get('activities_columns.work_phone');
61: }
62: if (isset($contact->home_phone)) {
63: $usedColumns[] = Config::get('activities_columns.home_phone');
64: }
65:
66: if (isset($contact->birthday_day)) {
67: $usedColumns[] = Config::get('activities_columns.birthday_day');
68: }
69: if (isset($contact->birthday_month)) {
70: $usedColumns[] = Config::get('activities_columns.birthday_month');
71: }
72: if (isset($contact->anniversary)) {
73: $usedColumns[] = Config::get('activities_columns.anniversary');
74: }
75:
76:
77: if (!empty($contact->addresses)) {
78: $address = $contact->addresses[0];
79: if (isset($address->line1)) {
80: $usedColumns[] = Config::get('activities_columns.address1');
81: }
82: if (isset($address->line2)) {
83: $usedColumns[] = Config::get('activities_columns.address2');
84: }
85: if (isset($address->line3)) {
86: $usedColumns[] = Config::get('activities_columns.address3');
87: }
88: if (isset($address->city)) {
89: $usedColumns[] = Config::get('activities_columns.city');
90: }
91: if (isset($address->state_code)) {
92: $usedColumns[] = Config::get('activities_columns.state');
93: }
94: if (isset($address->state_province)) {
95: $usedColumns[] = Config::get('activities_columns.state_province');
96: }
97: if (isset($address->country)) {
98: $usedColumns[] = Config::get('activities_columns.country');
99: }
100: if (isset($address->postal_code)) {
101: $usedColumns[] = Config::get('activities_columns.postal_code');
102: }
103: if (isset($address->sub_postal_code)) {
104: $usedColumns[] = Config::get('activities_columns.sub_postal_code');
105: }
106: }
107:
108:
109: if (!empty($contact->custom_fields)) {
110: foreach ($contact->custom_fields as $customField) {
111: if (strpos($customField->name, 'custom_field_') !== false) {
112: $customFieldNumber = substr($customField->name, 13);
113: $usedColumns[] = Config::get('activities_columns.custom_field_' . $customFieldNumber);
114: }
115: }
116: }
117: $this->column_names = $usedColumns;
118: } else {
119: $this->column_names = $columnNames;
120: }
121: }
122:
123: 124: 125: 126:
127: public function toJson()
128: {
129: foreach ($this->import_data as $contact) {
130: foreach ($contact as $key => $value) {
131: if ($value == null) {
132: unset($contact->$key);
133: }
134: }
135: }
136: return json_encode($this);
137: }
138: }
139: