Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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 | /** | |
42 | * Build the form | |
43 | * | |
44 | * @access public | |
45 | * | |
46 | * @return void | |
47 | */ | |
48 | function preProcess() { | |
49 | // initialize the task and row fields | |
50 | parent::preProcess(); | |
51 | } | |
52 | ||
53 | /** | |
54 | * Function to build the form | |
55 | * | |
56 | * @access public | |
57 | * | |
355ba699 | 58 | * @return void |
6a488035 TO |
59 | */ |
60 | function buildQuickForm() { | |
61 | CRM_Utils_System::setTitle(ts('Add Contacts to Organization')); | |
62 | $this->addElement('text', 'name', ts('Find Target Organization')); | |
63 | ||
64 | $this->add('select', | |
65 | 'relationship_type_id', | |
66 | ts('Relationship Type'), | |
67 | array( | |
68 | '' => ts('- select -')) + | |
69 | CRM_Contact_BAO_Relationship::getRelationType("Organization"), TRUE | |
70 | ); | |
71 | ||
72 | $searchRows = $this->get('searchRows'); | |
73 | $searchCount = $this->get('searchCount'); | |
74 | if ($searchRows) { | |
75 | $checkBoxes = array(); | |
76 | $chekFlag = 0; | |
77 | foreach ($searchRows as $id => $row) { | |
78 | if (!$chekFlag) { | |
79 | $chekFlag = $id; | |
80 | } | |
81 | ||
82 | $checkBoxes[$id] = $this->createElement('radio', NULL, NULL, NULL, $id); | |
83 | } | |
84 | ||
85 | $this->addGroup($checkBoxes, 'contact_check'); | |
86 | if ($chekFlag) { | |
87 | $checkBoxes[$chekFlag]->setChecked(TRUE); | |
88 | } | |
89 | $this->assign('searchRows', $searchRows); | |
90 | } | |
91 | ||
92 | ||
93 | $this->assign('searchCount', $searchCount); | |
94 | $this->assign('searchDone', $this->get('searchDone')); | |
95 | $this->assign('contact_type_display', ts('Organization')); | |
96 | $this->addElement('submit', $this->getButtonName('refresh'), ts('Search'), array('class' => 'form-submit')); | |
97 | $this->addElement('submit', $this->getButtonName('cancel'), ts('Cancel'), array('class' => 'form-submit')); | |
98 | ||
99 | ||
100 | $this->addButtons(array( | |
101 | array( | |
102 | 'type' => 'next', | |
103 | 'name' => ts('Add to Organization'), | |
104 | 'isDefault' => TRUE, | |
105 | ), | |
106 | array( | |
107 | 'type' => 'cancel', | |
108 | 'name' => ts('Cancel'), | |
109 | ), | |
110 | ) | |
111 | ); | |
112 | } | |
113 | ||
114 | /** | |
115 | * process the form after the input has been submitted and validated | |
116 | * | |
117 | * @access public | |
118 | * | |
355ba699 | 119 | * @return void |
6a488035 TO |
120 | */ |
121 | public function postProcess() { | |
122 | // store the submitted values in an array | |
123 | $params = $this->controller->exportValues($this->_name); | |
124 | ||
125 | $this->set('searchDone', 0); | |
a7488080 | 126 | if (!empty($_POST['_qf_AddToOrganization_refresh'])) { |
6a488035 TO |
127 | $searchParams['contact_type'] = array('Organization' => 'Organization'); |
128 | $searchParams['rel_contact'] = $params['name']; | |
129 | CRM_Contact_Form_Task_AddToHousehold::search($this, $searchParams); | |
130 | $this->set('searchDone', 1); | |
131 | return; | |
132 | } | |
133 | ||
134 | $data = array(); | |
135 | $data['relationship_type_id'] = $params['relationship_type_id']; | |
136 | $data['is_active'] = 1; | |
137 | $invalid = 0; | |
138 | $valid = 0; | |
139 | $duplicate = 0; | |
140 | if (is_array($this->_contactIds)) { | |
141 | foreach ($this->_contactIds as $value) { | |
142 | $ids = array(); | |
143 | $ids['contact'] = $value; | |
144 | $errors = CRM_Contact_BAO_Relationship::checkValidRelationship($params, $ids, $params['contact_check']); | |
145 | if ($errors) { | |
146 | $invalid++; | |
147 | continue; | |
148 | } | |
149 | ||
150 | if (CRM_Contact_BAO_Relationship::checkDuplicateRelationship($params, | |
151 | CRM_Utils_Array::value('contact', $ids), | |
152 | // step 2 | |
153 | $params['contact_check'] | |
154 | )) { | |
155 | $duplicate++; | |
156 | continue; | |
157 | } | |
158 | CRM_Contact_BAO_Relationship::add($data, $ids, $params['contact_check']); | |
159 | $valid++; | |
160 | } | |
161 | $org = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['contact_check'], 'display_name'); | |
162 | list($rtype, $a_b) = explode('_', $data['relationship_type_id'], 2); | |
163 | $relationship = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $rtype, "label_$a_b"); | |
164 | ||
165 | $status = array(ts('%count %2 %3 relationship created', array('count' => $valid, 'plural' => '%count %2 %3 relationships created', 2 => $relationship, 3 => $org))); | |
166 | if ($duplicate) { | |
167 | $status[] = ts('%count was skipped because the contact is already %2 %3', array('count' => $duplicate, 'plural' => '%count were skipped because the contacts are already %2 %3', 2 => $relationship, 3 => $org)); | |
168 | } | |
169 | if ($invalid) { | |
170 | $status[] = ts('%count relationship was not created because the contact is not of the right type for this relationship', array('count' => $invalid, 'plural' => '%count relationships were not created because the contact is not of the right type for this relationship')); | |
171 | } | |
172 | $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>'; | |
173 | CRM_Core_Session::setStatus($status, ts('Relationship Created', array('count' => $valid, 'plural' => 'Relationships Created')), 'success', array('expires' => 0)); | |
174 | } | |
175 | } | |
176 | ||
177 | } |