Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-01-12-16-09-32
[civicrm-core.git] / CRM / Contact / Form / Task / AddToOrganization.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
31 * @copyright CiviCRM LLC (c) 2004-2014
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 /**
42 * Build the form object
43 *
44 *
45 * @return void
46 */
47 public function preProcess() {
48 // initialize the task and row fields
49 parent::preProcess();
50 }
51
52 /**
53 * Build the form object
54 *
55 *
56 * @return void
57 */
58 public function buildQuickForm() {
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(
66 '' => ts('- select -')
67 ) +
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
91
92 $this->assign('searchCount', $searchCount);
93 $this->assign('searchDone', $this->get('searchDone'));
94 $this->assign('contact_type_display', ts('Organization'));
95 $this->addElement('submit', $this->getButtonName('refresh'), ts('Search'), array('class' => 'crm-form-submit'));
96 $this->addElement('submit', $this->getButtonName('cancel'), ts('Cancel'), array('class' => 'crm-form-submit'));
97
98
99 $this->addButtons(array(
100 array(
101 'type' => 'next',
102 'name' => ts('Add to Organization'),
103 'isDefault' => TRUE,
104 ),
105 array(
106 'type' => 'cancel',
107 'name' => ts('Cancel'),
108 ),
109 )
110 );
111 }
112
113 /**
114 * Process the form after the input has been submitted and validated
115 *
116 *
117 * @return void
118 */
119 public function postProcess() {
120 // store the submitted values in an array
121 $params = $this->controller->exportValues($this->_name);
122
123 $this->set('searchDone', 0);
124 if (!empty($_POST['_qf_AddToOrganization_refresh'])) {
125 $searchParams['contact_type'] = array('Organization' => 'Organization');
126 $searchParams['rel_contact'] = $params['name'];
127 CRM_Contact_Form_Task_AddToHousehold::search($this, $searchParams);
128 $this->set('searchDone', 1);
129 return;
130 }
131
132 $data = array();
133 $data['relationship_type_id'] = $params['relationship_type_id'];
134 $data['is_active'] = 1;
135 $invalid = 0;
136 $valid = 0;
137 $duplicate = 0;
138 if (is_array($this->_contactIds)) {
139 foreach ($this->_contactIds as $value) {
140 $ids = array();
141 $ids['contact'] = $value;
142 $errors = CRM_Contact_BAO_Relationship::checkValidRelationship($params, $ids, $params['contact_check']);
143 if ($errors) {
144 $invalid++;
145 continue;
146 }
147
148 if (CRM_Contact_BAO_Relationship::checkDuplicateRelationship($params,
149 CRM_Utils_Array::value('contact', $ids),
150 // step 2
151 $params['contact_check']
152 )
153 ) {
154 $duplicate++;
155 continue;
156 }
157 CRM_Contact_BAO_Relationship::add($data, $ids, $params['contact_check']);
158 $valid++;
159 }
160 $org = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['contact_check'], 'display_name');
161 list($rtype, $a_b) = explode('_', $data['relationship_type_id'], 2);
162 $relationship = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $rtype, "label_$a_b");
163
164 $status = array(
165 ts('%count %2 %3 relationship created', array(
166 'count' => $valid,
167 'plural' => '%count %2 %3 relationships created',
168 2 => $relationship,
169 3 => $org
170 ))
171 );
172 if ($duplicate) {
173 $status[] = ts('%count was skipped because the contact is already %2 %3', array(
174 'count' => $duplicate,
175 'plural' => '%count were skipped because the contacts are already %2 %3',
176 2 => $relationship,
177 3 => $org
178 ));
179 }
180 if ($invalid) {
181 $status[] = ts('%count relationship was not created because the contact is not of the right type for this relationship', array(
182 'count' => $invalid,
183 'plural' => '%count relationships were not created because the contact is not of the right type for this relationship'
184 ));
185 }
186 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
187 CRM_Core_Session::setStatus($status, ts('Relationship created.', array(
188 'count' => $valid,
189 'plural' => 'Relationships created.'
190 )), 'success', array('expires' => 0));
191 }
192 }
193
194 }