3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class provides the functionality to add contact(s) to Organization
39 class CRM_Contact_Form_Task_AddToOrganization
extends CRM_Contact_Form_Task
{
48 function preProcess() {
49 // initialize the task and row fields
54 * Function to build the form
60 function buildQuickForm() {
61 CRM_Utils_System
::setTitle(ts('Add Contacts to Organization'));
62 $this->addElement('text', 'name', ts('Find Target Organization'));
65 'relationship_type_id',
66 ts('Relationship Type'),
68 '' => ts('- select -')) +
69 CRM_Contact_BAO_Relationship
::getRelationType("Organization"), TRUE
72 $searchRows = $this->get('searchRows');
73 $searchCount = $this->get('searchCount');
75 $checkBoxes = array();
77 foreach ($searchRows as $id => $row) {
82 $checkBoxes[$id] = $this->createElement('radio', NULL, NULL, NULL, $id);
85 $this->addGroup($checkBoxes, 'contact_check');
87 $checkBoxes[$chekFlag]->setChecked(TRUE);
89 $this->assign('searchRows', $searchRows);
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'));
100 $this->addButtons(array(
103 'name' => ts('Add to Organization'),
108 'name' => ts('Cancel'),
115 * process the form after the input has been submitted and validated
121 public function postProcess() {
122 // store the submitted values in an array
123 $params = $this->controller
->exportValues($this->_name
);
125 $this->set('searchDone', 0);
126 if (!empty($_POST['_qf_AddToOrganization_refresh'])) {
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);
135 $data['relationship_type_id'] = $params['relationship_type_id'];
136 $data['is_active'] = 1;
140 if (is_array($this->_contactIds
)) {
141 foreach ($this->_contactIds
as $value) {
143 $ids['contact'] = $value;
144 $errors = CRM_Contact_BAO_Relationship
::checkValidRelationship($params, $ids, $params['contact_check']);
150 if (CRM_Contact_BAO_Relationship
::checkDuplicateRelationship($params,
151 CRM_Utils_Array
::value('contact', $ids),
153 $params['contact_check']
158 CRM_Contact_BAO_Relationship
::add($data, $ids, $params['contact_check']);
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");
165 $status = array(ts('%count %2 %3 relationship created', array('count' => $valid, 'plural' => '%count %2 %3 relationships created', 2 => $relationship, 3 => $org)));
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));
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'));
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));