Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class provides the functionality to add contact(s) to Organization | |
38 | */ | |
39 | class CRM_Contact_Form_Task_AddToOrganization extends CRM_Contact_Form_Task { | |
40 | ||
41 | /** | |
c490a46a | 42 | * Build the form object |
6a488035 | 43 | * |
6a488035 TO |
44 | * |
45 | * @return void | |
46 | */ | |
00be9182 | 47 | public function preProcess() { |
6a488035 TO |
48 | // initialize the task and row fields |
49 | parent::preProcess(); | |
50 | } | |
51 | ||
52 | /** | |
c490a46a | 53 | * Build the form object |
6a488035 | 54 | * |
6a488035 | 55 | * |
355ba699 | 56 | * @return void |
6a488035 | 57 | */ |
00be9182 | 58 | public function buildQuickForm() { |
6a488035 TO |
59 | CRM_Utils_System::setTitle(ts('Add Contacts to Organization')); |
60 | $this->addElement('text', 'name', ts('Find Target Organization')); | |
61 | ||
62 | $this->add('select', | |
63 | 'relationship_type_id', | |
64 | ts('Relationship Type'), | |
65 | array( | |
3bdca100 | 66 | '' => ts('- select -'), |
353ffa53 | 67 | ) + |
6a488035 TO |
68 | CRM_Contact_BAO_Relationship::getRelationType("Organization"), TRUE |
69 | ); | |
70 | ||
71 | $searchRows = $this->get('searchRows'); | |
72 | $searchCount = $this->get('searchCount'); | |
73 | if ($searchRows) { | |
74 | $checkBoxes = array(); | |
75 | $chekFlag = 0; | |
76 | foreach ($searchRows as $id => $row) { | |
77 | if (!$chekFlag) { | |
78 | $chekFlag = $id; | |
79 | } | |
80 | ||
81 | $checkBoxes[$id] = $this->createElement('radio', NULL, NULL, NULL, $id); | |
82 | } | |
83 | ||
84 | $this->addGroup($checkBoxes, 'contact_check'); | |
85 | if ($chekFlag) { | |
86 | $checkBoxes[$chekFlag]->setChecked(TRUE); | |
87 | } | |
88 | $this->assign('searchRows', $searchRows); | |
89 | } | |
90 | ||
6a488035 TO |
91 | $this->assign('searchCount', $searchCount); |
92 | $this->assign('searchDone', $this->get('searchDone')); | |
93 | $this->assign('contact_type_display', ts('Organization')); | |
97e557d7 CW |
94 | $this->addElement('submit', $this->getButtonName('refresh'), ts('Search'), array('class' => 'crm-form-submit')); |
95 | $this->addElement('submit', $this->getButtonName('cancel'), ts('Cancel'), array('class' => 'crm-form-submit')); | |
6a488035 | 96 | |
6a488035 TO |
97 | $this->addButtons(array( |
98 | array( | |
99 | 'type' => 'next', | |
100 | 'name' => ts('Add to Organization'), | |
101 | 'isDefault' => TRUE, | |
102 | ), | |
103 | array( | |
104 | 'type' => 'cancel', | |
105 | 'name' => ts('Cancel'), | |
106 | ), | |
107 | ) | |
108 | ); | |
109 | } | |
110 | ||
111 | /** | |
100fef9d | 112 | * Process the form after the input has been submitted and validated |
6a488035 | 113 | * |
6a488035 | 114 | * |
355ba699 | 115 | * @return void |
6a488035 TO |
116 | */ |
117 | public function postProcess() { | |
118 | // store the submitted values in an array | |
119 | $params = $this->controller->exportValues($this->_name); | |
120 | ||
121 | $this->set('searchDone', 0); | |
a7488080 | 122 | if (!empty($_POST['_qf_AddToOrganization_refresh'])) { |
6a488035 TO |
123 | $searchParams['contact_type'] = array('Organization' => 'Organization'); |
124 | $searchParams['rel_contact'] = $params['name']; | |
125 | CRM_Contact_Form_Task_AddToHousehold::search($this, $searchParams); | |
126 | $this->set('searchDone', 1); | |
127 | return; | |
128 | } | |
129 | ||
130 | $data = array(); | |
131 | $data['relationship_type_id'] = $params['relationship_type_id']; | |
132 | $data['is_active'] = 1; | |
133 | $invalid = 0; | |
134 | $valid = 0; | |
135 | $duplicate = 0; | |
136 | if (is_array($this->_contactIds)) { | |
137 | foreach ($this->_contactIds as $value) { | |
138 | $ids = array(); | |
139 | $ids['contact'] = $value; | |
140 | $errors = CRM_Contact_BAO_Relationship::checkValidRelationship($params, $ids, $params['contact_check']); | |
141 | if ($errors) { | |
142 | $invalid++; | |
143 | continue; | |
144 | } | |
145 | ||
146 | if (CRM_Contact_BAO_Relationship::checkDuplicateRelationship($params, | |
353ffa53 TO |
147 | CRM_Utils_Array::value('contact', $ids), |
148 | // step 2 | |
149 | $params['contact_check'] | |
150 | ) | |
151 | ) { | |
6a488035 TO |
152 | $duplicate++; |
153 | continue; | |
154 | } | |
155 | CRM_Contact_BAO_Relationship::add($data, $ids, $params['contact_check']); | |
156 | $valid++; | |
157 | } | |
158 | $org = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['contact_check'], 'display_name'); | |
159 | list($rtype, $a_b) = explode('_', $data['relationship_type_id'], 2); | |
160 | $relationship = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $rtype, "label_$a_b"); | |
161 | ||
353ffa53 TO |
162 | $status = array( |
163 | ts('%count %2 %3 relationship created', array( | |
164 | 'count' => $valid, | |
165 | 'plural' => '%count %2 %3 relationships created', | |
166 | 2 => $relationship, | |
3bdca100 | 167 | 3 => $org, |
168 | )), | |
353ffa53 | 169 | ); |
6a488035 | 170 | if ($duplicate) { |
353ffa53 TO |
171 | $status[] = ts('%count was skipped because the contact is already %2 %3', array( |
172 | 'count' => $duplicate, | |
173 | 'plural' => '%count were skipped because the contacts are already %2 %3', | |
174 | 2 => $relationship, | |
3bdca100 | 175 | 3 => $org, |
353ffa53 | 176 | )); |
6a488035 TO |
177 | } |
178 | if ($invalid) { | |
353ffa53 TO |
179 | $status[] = ts('%count relationship was not created because the contact is not of the right type for this relationship', array( |
180 | 'count' => $invalid, | |
3bdca100 | 181 | 'plural' => '%count relationships were not created because the contact is not of the right type for this relationship', |
353ffa53 | 182 | )); |
6a488035 TO |
183 | } |
184 | $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>'; | |
353ffa53 TO |
185 | CRM_Core_Session::setStatus($status, ts('Relationship created.', array( |
186 | 'count' => $valid, | |
3bdca100 | 187 | 'plural' => 'Relationships created.', |
353ffa53 | 188 | )), 'success', array('expires' => 0)); |
6a488035 TO |
189 | } |
190 | } | |
191 | ||
192 | } |