cleanup function and remove unused param
[civicrm-core.git] / tools / drupal / modules / civicrm_regsite / civicrm_regsite.module
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.1 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2011 |
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-2011
32 * $Id$
33 *
34 */
35
36 // ** REGSITE settings **
37 define('REGSITE_PROFILE_ID', 15);
38 define('EMPOYER_RELATIONSHIP_TYPE_ID', 4);
39 define('REGSITE_INDIVIDUAL_GROUP', 16);
40
41 define('CIVICRM_REGSITE_FROM_EMAIL', "CiviCRM Site Registration <info@civicrm.org>");
42 define('CIVICRM_SURVEY_FROM_EMAIL', "CiviCRM Training <info@civicrm.org>");
43 define('CIVICRM_ARCHIVE_EMAIL', "CiviCRM Email Archival <archive@civicrm.org>");
44
45 // ** SURVEY settings **
46 // =======================
47 define('CIVICRM_EVENT_SURVEY_CG_TITLE', 'Event_Survey');
48 // Note: Don't forget to change name of the event type in xml data file
49 // when you change the event type id below
50 define('CIVICRM_EVENT_SURVEY_EVENT_TYPE_ID', 3);
51 // Note: Don't forget to change name of the template directory
52 // when you change the profile id below
53 define('CIVICRM_EVENT_SURVEY_PROFILE_ID', 9);
54 function civicrm_regsite_civicrm_config(&$config) {
55 $template = &CRM_Core_Smarty::singleton();
56
57 $regsiteRoot = dirname(__FILE__) . DIRECTORY_SEPARATOR;
58
59 $regsiteDir = $regsiteRoot . 'templates';
60
61 if (is_array($template->template_dir)) {
62 array_unshift($template->template_dir, $regsiteDir);
63 }
64 else {
65 $template->template_dir = array($regsiteDir, $template->template_dir);
66 }
67
68 // also fix php include path
69 $include_path = $regsiteRoot . PATH_SEPARATOR . get_include_path();
70 set_include_path($include_path);
71
72 // set the timezone
73 date_default_timezone_set('America/Los_Angeles');
74 }
75
76 function civicrm_regsite_civicrm_buildForm($formName, &$form) {
77 if ($formName == 'CRM_Profile_Form_Edit') {
78 if ($form->getVar('_gid') == REGSITE_PROFILE_ID) {
79 _civicrm_regsite_civicrm_buildForm_Profile_RegSite($form);
80 }
81 elseif ($form->getVar('_gid') == CIVICRM_EVENT_SURVEY_PROFILE_ID) {
82 _event_survey_civicrm_buildForm_Profile($formName, $form);
83 }
84 }
85 }
86
87 function &_civicrm_regsite_get_permissioned_contacts($orgID) {
88 $sql = "
89 SELECT c.first_name, c.last_name, c.display_name, e.email
90 FROM civicrm_contact c
91 INNER JOIN civicrm_email e on e.contact_id = c.id
92 INNER JOIN civicrm_relationship r on r.contact_id_a = c.id
93 WHERE r.contact_id_b = %1
94 AND r.relationship_type_id = %2
95 AND r.is_active = 1
96 AND r.is_permission_a_b = 1
97 AND e.is_primary = 1
98 ";
99 $params = array(1 => array($orgID, 'Integer'),
100 2 => array(EMPOYER_RELATIONSHIP_TYPE_ID, 'Integer'),
101 );
102 return CRM_Core_DAO::executeQuery($sql, $params);
103 }
104
105 function _civicrm_regsite_civicrm_buildForm_Profile_RegSite(&$form) {
106 // add first name, last name and email
107 $attributes = CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact');
108
109 // first_name
110 $form->add('text', 'regsite_first_name', ts('First Name'), $attributes['first_name'], TRUE);
111
112 // last_name
113 $form->add('text', 'regsite_last_name', ts('Last Name'), $attributes['last_name'], TRUE);
114
115 // email
116 $form->add('text', 'regsite_email', ts('Email'), $attributes['first_name'], TRUE);
117
118 $defaults = array();
119
120 // also set the default appropriately
121 // if logged in user, use logged in users name and email
122 $session = &CRM_Core_Session::singleton();
123 $contactID = $session->get('userID');
124 if ($contactID) {
125 $params = array('contact_id' => $contactID,
126 'return.first_name' => 1,
127 'return.last_name' => 1,
128 'return.email' => 1,
129 'version' => 3,
130 );
131
132 require_once 'api/api.php';
133 $contact = civicrm_api('contact', 'get', $params);
134 if (!civicrm_error($contact)) {
135 $defaults['regsite_first_name'] = CRM_Utils_Array::value('first_name', $contact['values'][$contactID]);
136 $defaults['regsite_last_name'] = CRM_Utils_Array::value('last_name', $contact['values'][$contactID]);
137 $defaults['regsite_email'] = CRM_Utils_Array::value('email', $contact['values'][$contactID]);
138 }
139 }
140
141 $orgID = $form->getVar('_id');
142 if (empty($defaults) &&
143 $orgID
144 ) {
145 $dao = &_civicrm_regsite_get_permissioned_contacts($orgID);
146
147 // and then check for all permissioned relatioships in that org
148 if ($dao->fetch()) {
149 $defaults['regsite_first_name'] = $dao->first_name;
150 $defaults['regsite_last_name'] = $dao->last_name;
151 $defaults['regsite_email'] = $dao->email;
152 }
153 }
154
155 $form->setDefaults($defaults);
156 }
157
158 function civicrm_regsite_civicrm_postProcess($class, &$form) {
159 if (is_a($form, 'CRM_Profile_Form_Edit')) {
160 $gid = $form->getVar('_gid');
161 if ($form->getVar('_gid') == REGSITE_PROFILE_ID) {
162 _civicrm_regsite_civicrm_postProcess_Profile_RegSite($form);
163 }
164 elseif ($form->getVar('_gid') == CIVICRM_EVENT_SURVEY_PROFILE_ID) {
165 _event_survey_civicrm_postProcess_Profile($class, $form);
166 }
167 }
168 }
169
170 function _civicrm_regsite_civicrm_postProcess_Profile_RegSite(&$form) {
171 $params = $form->controller->exportValues($form->getName());
172
173 // first create the contact from the name and email
174 $orgContactParams = array('first_name' => CRM_Utils_Array::value('regsite_first_name', $params),
175 'last_name' => CRM_Utils_Array::value('regsite_last_name', $params),
176 'email' => CRM_Utils_Array::value('regsite_email', $params),
177 );
178
179 $dedupeParams = CRM_Dedupe_Finder::formatParams($orgContactParams, 'Individual');
180 $dedupeParams['check_permission'] = FALSE;
181 $dupeIDs = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual', 'Unsupervised');
182
183 $contactID = NULL;
184 if (is_array($dupeIDs) && !empty($dupeIDs)) {
185 $contactID = array_pop($dupeIDs);
186 }
187
188 $orgContactParams['email'] = array();
189 $orgContactParams['email'][1] = array();
190 $orgContactParams['email'][1]['email'] = CRM_Utils_Array::value('regsite_email', $params);
191 $orgContactParams['email'][1]['is_primary'] = 1;
192 $orgContactParams['email'][1]['location_type_id'] = 3;
193 $contactID = CRM_Contact_BAO_Contact::createProfileContact($orgContactParams,
194 CRM_Core_DAO::$_nullArray,
195 $contactID
196 );
197
198 // now lets add the contact to the group as specified in the constants
199 $groupParams = array('contact_id' => $contactID,
200 'group_id' => REGSITE_INDIVIDUAL_GROUP,
201 'version' => 3,
202 );
203 require_once 'api/api.php';
204 civicrm_api('GroupContact', 'create', $groupParams);
205
206 // check that there is a employee / employer relationship between the two
207 // and if so permission that relationship, if no create it
208 $relationship = new CRM_Contact_DAO_Relationship();
209 $relationship->contact_id_a = $contactID;
210 $relationship->contact_id_b = $form->getVar('_id');
211 $relationship->relationship_type_id = EMPOYER_RELATIONSHIP_TYPE_ID;
212
213 $relationship->selectAdd();
214 $relationship->selectAdd('id, is_active, is_permission_a_b');
215 $relationship->find(TRUE);
216
217 $relationship->is_active = 1;
218 $relationship->is_permission_a_b = 1;
219 $relationship->save();
220
221 $smarty = &CRM_Core_Smarty::singleton();
222
223 // lets get the profile values
224 require_once 'CRM/Core/BAO/UFGroup.php';
225 $smartyParams = array();
226
227 $smartyParams['organizationName'] = CRM_Utils_Array::value('organization_name', $params);
228 $smartyParams['contactName'] = "{$params['regsite_first_name']} {$params['regsite_last_name']} ({$params['regsite_email']})";
229
230 $profileValues = array();
231 CRM_Core_BAO_UFGroup::getValues($form->getVar('_id'),
232 $form->getVar('_fields'),
233 $profileValues
234 );
235
236 // create a hashLink
237 $orgID = $form->getVar('_id');
238 $smartyParams['hashLink'] = CRM_Utils_System::url('civicrm/profile/edit',
239 "reset=1&id=$orgID&gid=" . REGSITE_PROFILE_ID .
240 "&cs=" .
241 CRM_Contact_BAO_Contact_Utils::generateChecksum($orgID),
242 TRUE, NULL, FALSE
243 );
244
245 $smarty->assign_by_ref('displayValues', $smartyParams);
246 $smarty->assign_by_ref('profileValues', $profileValues);
247
248 $subject = $smarty->fetch('Mail/RegSite/Subject.tpl');
249 $body = $smarty->fetch('Mail/RegSite/Message.tpl');
250
251 // now send email to both user and org
252 $params = array('from' => CIVICRM_REGSITE_FROM_EMAIL,
253 'toName' => "{CRM_Utils_Array::value( 'regsite_first_name', $params )} {CRM_Utils_Array::value( 'regsite_last_name', $params )}",
254 'toEmail' => CRM_Utils_Array::value('regsite_email', $params),
255 'cc' => CRM_Utils_Array::value('email-Primary', $params),
256 'bcc' => CIVICRM_ARCHIVE_EMAIL,
257 'subject' => $subject,
258 'text' => $body,
259 );
260
261 require_once 'CRM/Utils/Mail.php';
262 CRM_Utils_Mail::send($params);
263 }
264
265 function civicrm_regsite_civicrm_pageRun(&$page) {
266 $name = $page->getVar('_name');
267 if ($name == 'CRM_Profile_Page_Dynamic') {
268 if ($page->getVar('_gid') == REGSITE_PROFILE_ID) {
269 return _civicrm_regsite_civicrm_pageRun_Profile_RegSite($page);
270 }
271 elseif ($page->getVar('_gid') == CIVICRM_EVENT_SURVEY_PROFILE_ID) {
272 _event_survey_civicrm_pageRun_Profile($page);
273 }
274 }
275 }
276
277 function _civicrm_regsite_civicrm_pageRun_Profile_RegSite(&$page) {
278 // get the id of the org
279 $orgID = $page->getVar('_id');
280
281 $dao = &_civicrm_regsite_get_permissioned_contacts($orgID);
282
283 $names = array();
284 while ($dao->fetch()) {
285 $names[] = "{$dao->display_name} ({$dao->email})";
286 }
287
288 if (!empty($names)) {
289 $contactPersonString = implode(', ', $names);
290 $page->assign('contactPersonString', $contactPersonString);
291 }
292 }
293
294 function civicrm_regsite_civicrm_links($op, $objectName, $objectId, &$links) {
295 if ($op != 'view.contact.userDashBoard') {
296 return;
297 }
298
299 // take the update link and move it to the profile
300 $links[CRM_Core_Action::UPDATE]['url'] = 'civicrm/profile/edit';
301 $links[CRM_Core_Action::UPDATE]['qs'] = "reset=1&gid=" . REGSITE_PROFILE_ID . "&id=%%cbid%%";
302
303 return $links;
304 }
305
306 function _event_survey_civicrm_buildForm_Profile($formName, &$form) {
307 $cgID = _event_survey_civicrm_getCustomGroupID(CIVICRM_EVENT_SURVEY_CG_TITLE);
308 if (empty($cgID)) {
309 return;
310 }
311 require_once 'CRM/Core/BAO/CustomGroup.php';
312 $groupTree = &CRM_Core_BAO_CustomGroup::getTree('Participant',
313 $form,
314 NULL,
315 $cgID,
316 CIVICRM_EVENT_SURVEY_EVENT_TYPE_ID
317 );
318 // simplified formatted groupTree
319 $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $form);
320 CRM_Core_BAO_CustomGroup::buildQuickForm($form, $groupTree, FALSE, 'event_survey_');
321
322 if (isset($groupTree) && is_array($groupTree)) {
323 $participantId = CRM_Utils_Request::retrieve('pid', 'Positive', $form, TRUE, 0, 'REQUEST');
324 $contactId = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Participant", $participantId, 'contact_id');
325 $form->setVar('_id', $contactId);
326 }
327 }
328
329 function _event_survey_civicrm_postProcess_Profile($class, &$form) {
330 $cgID = _event_survey_civicrm_getCustomGroupID(CIVICRM_EVENT_SURVEY_CG_TITLE);
331 if (empty($cgID)) {
332 return;
333 }
334 require_once 'CRM/Core/BAO/CustomGroup.php';
335 $groupTree = &CRM_Core_BAO_CustomGroup::getTree('Participant',
336 $form,
337 NULL,
338 $cgID,
339 CIVICRM_EVENT_SURVEY_EVENT_TYPE_ID
340 );
341 // simplified formatted groupTree
342 $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $form);
343
344 if (isset($groupTree) && is_array($groupTree)) {
345 $participantId = CRM_Utils_Request::retrieve('pid', 'Positive', $form, TRUE, 0, 'REQUEST');
346
347 $params = $form->controller->exportValues($form->getVar('_name'));
348 require_once 'CRM/Core/BAO/CustomValueTable.php';
349 CRM_Core_BAO_CustomValueTable::postProcess($params,
350 $groupTree[$cgID]['fields'],
351 'civicrm_participant',
352 $participantId,
353 'Participant'
354 );
355 // mailing part
356 $smarty = &CRM_Core_Smarty::singleton();
357 $smarty->assign_by_ref('profileValues', $params);
358
359 $subject = $smarty->fetch('Mail/Survey/Subject.tpl');
360 $body = $smarty->fetch('Mail/Survey/Message.tpl');
361
362 // now send email to both user and org
363 $params = array('from' => CIVICRM_REGSITE_FROM_EMAIL,
364 'toName' => "{$params['first_name']} {$params['last_name']}",
365 'toEmail' => $params['email-Primary'],
366 'bcc' => CIVICRM_ARCHIVE_EMAIL,
367 'subject' => $subject,
368 'text' => $body,
369 );
370 require_once 'CRM/Utils/Mail.php';
371 CRM_Utils_Mail::send($params);
372
373 return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/profile/view',
374 "reset=1&gid=" . $form->getVar('_gid') .
375 "&id=" . $form->getVar('_id') .
376 "&pid=" . $participantId
377 ));
378 }
379 }
380
381 function _event_survey_civicrm_pageRun_Profile(&$page) {
382 $cgID = _survey_civicrm_getCustomGroupID(CIVICRM_EVENT_SURVEY_CG_TITLE);
383 if (empty($cgID)) {
384 return;
385 }
386 $participantId = CRM_Utils_Request::retrieve('pid', 'Positive', $page, TRUE, 0, 'REQUEST');
387 $contactID = $page->getVar('_id');
388
389 require_once 'CRM/Core/BAO/CustomGroup.php';
390 $groupTree = &CRM_Core_BAO_CustomGroup::getTree('Participant',
391 $form,
392 $participantId,
393 $cgID,
394 CIVICRM_EVENT_SURVEY_EVENT_TYPE_ID
395 );
396 CRM_Core_BAO_CustomGroup::buildCustomDataView($page,
397 $groupTree,
398 FALSE, NULL,
399 'event_survey_'
400 );
401 }
402
403 function _event_survey_civicrm_getCustomGroupID($customGroupName) {
404 require_once 'CRM/Utils/Type.php';
405 $customGroupName = CRM_Utils_Type::escape($customGroupName, 'String');
406 return CRM_Core_DAO::getFieldValue("CRM_Core_DAO_CustomGroup", $customGroupName, 'id', 'name');
407 }
408