3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
35 Class CRM_Campaign_BAO_Petition
extends CRM_Campaign_BAO_Survey
{
36 function __construct() {
37 parent
::__construct();
38 // expire cookie in one day
39 $this->cookieExpire
= (1 * 60 * 60 * 24);
43 * Function to get Petition Details for dashboard.
47 static function getPetitionSummary($params = array(
48 ), $onlyCount = FALSE) {
49 //build the limit and order clause.
50 $limitClause = $orderByClause = $lookupTableJoins = NULL;
53 'sort' => 'created_date',
56 'sortOrder' => 'desc',
58 foreach ($sortParams as $name => $default) {
59 if (CRM_Utils_Array
::value($name, $params)) {
60 $sortParams[$name] = $params[$name];
64 //need to lookup tables.
65 $orderOnPetitionTable = TRUE;
66 if ($sortParams['sort'] == 'campaign') {
67 $orderOnPetitionTable = FALSE;
69 LEFT JOIN civicrm_campaign campaign ON ( campaign.id = petition.campaign_id )';
70 $orderByClause = "ORDER BY campaign.title {$sortParams['sortOrder']}";
72 elseif ($sortParams['sort'] == 'activity_type') {
73 $orderOnPetitionTable = FALSE;
75 LEFT JOIN civicrm_option_value activity_type ON ( activity_type.value = petition.activity_type_id
76 OR petition.activity_type_id IS NULL )
77 INNER JOIN civicrm_option_group grp ON ( activity_type.option_group_id = grp.id AND grp.name = 'activity_type' )";
78 $orderByClause = "ORDER BY activity_type.label {$sortParams['sortOrder']}";
80 elseif ($sortParams['sort'] == 'isActive') {
81 $sortParams['sort'] = 'is_active';
83 if ($orderOnPetitionTable) {
84 $orderByClause = "ORDER BY petition.{$sortParams['sort']} {$sortParams['sortOrder']}";
86 $limitClause = "LIMIT {$sortParams['offset']}, {$sortParams['rowCount']}";
89 //build the where clause.
90 $queryParams = $where = array();
92 //we only have activity type as a
93 //difference between survey and petition.
94 $petitionTypeID = CRM_Core_OptionGroup
::getValue('activity_type', 'petition', 'name');
95 if ($petitionTypeID) {
96 $where[] = "( petition.activity_type_id = %1 )";
97 $queryParams[1] = array($petitionTypeID, 'Positive');
99 if (CRM_Utils_Array
::value('title', $params)) {
100 $where[] = "( petition.title LIKE %2 )";
101 $queryParams[2] = array('%' . trim($params['title']) . '%', 'String');
103 if (CRM_Utils_Array
::value('campaign_id', $params)) {
104 $where[] = '( petition.campaign_id = %3 )';
105 $queryParams[3] = array($params['campaign_id'], 'Positive');
108 if (!empty($where)) {
109 $whereClause = ' WHERE ' . implode(" \nAND ", $where);
113 SELECT petition.id as id,
114 petition.title as title,
115 petition.is_active as is_active,
116 petition.result_id as result_id,
117 petition.is_default as is_default,
118 petition.campaign_id as campaign_id,
119 petition.activity_type_id as activity_type_id';
122 $selectClause = 'SELECT COUNT(*)';
124 $fromClause = 'FROM civicrm_survey petition';
126 $query = "{$selectClause} {$fromClause} {$whereClause} {$orderByClause} {$limitClause}";
129 return (int)CRM_Core_DAO
::singleValueQuery($query, $queryParams);
132 $petitions = array();
143 $petition = CRM_Core_DAO
::executeQuery($query, $queryParams);
144 while ($petition->fetch()) {
145 foreach ($properties as $property) {
146 $petitions[$petition->id
][$property] = $petition->$property;
154 * Get the petition count.
158 static function getPetitionCount() {
159 $whereClause = 'WHERE ( 1 )';
160 $queryParams = array();
161 $petitionTypeID = CRM_Core_OptionGroup
::getValue('activity_type', 'petition', 'name');
162 if ($petitionTypeID) {
163 $whereClause = "WHERE ( petition.activity_type_id = %1 )";
164 $queryParams[1] = array($petitionTypeID, 'Positive');
166 $query = "SELECT COUNT(*) FROM civicrm_survey petition {$whereClause}";
168 return (int)CRM_Core_DAO
::singleValueQuery($query, $queryParams);
172 * takes an associative array and creates a petition signature activity
174 * @param array $params (reference ) an assoc array of name/value pairs
176 * @return object CRM_Campaign_BAO_Petition
180 function createSignature(&$params) {
181 if (empty($params)) {
185 if (!isset($params['sid'])) {
186 $statusMsg = ts('No survey sid parameter. Cannot process signature.');
187 CRM_Core_Session
::setStatus($statusMsg, ts('Sorry'), 'error');
191 if (isset($params['contactId'])) {
193 // add signature as activity with survey id as source id
194 // get the activity type id associated with this survey
195 $surveyInfo = CRM_Campaign_BAO_Petition
::getSurveyInfo($params['sid']);
198 // activity status id (from /civicrm/admin/optionValue?reset=1&action=browse&gid=25)
199 // 1-Schedule, 2-Completed
201 $activityParams = array(
202 'source_contact_id' => $params['contactId'],
203 'target_contact_id' => $params['contactId'],
204 'source_record_id' => $params['sid'],
205 'subject' => $surveyInfo['title'],
206 'activity_type_id' => $surveyInfo['activity_type_id'],
207 'activity_date_time' => date("YmdHis"),
208 'status_id' => $params['statusId'],
209 'activity_campaign_id' => $params['activity_campaign_id'],
213 // *** check for activity using source id - if already signed
214 $activity = CRM_Activity_BAO_Activity
::create($activityParams);
216 // save activity custom data
217 if (CRM_Utils_Array
::value('custom', $params) &&
218 is_array($params['custom'])
220 CRM_Core_BAO_CustomValueTable
::store($params['custom'], 'civicrm_activity', $activity->id
);
223 // set permanent cookie to indicate this petition already signed on the computer
224 setcookie('signed_' . $params['sid'], $activity->id
, time() +
$this->cookieExpire
, '/');
230 function confirmSignature($activity_id, $contact_id, $petition_id) {
231 // change activity status to completed (status_id = 2)
232 // I wonder why do we need contact_id when we have activity_id anyway? [chastell]
233 $sql = 'UPDATE civicrm_activity SET status_id = 2 WHERE id = %1 AND source_contact_id = %2';
234 $params = array(1 => array($activity_id, 'Integer'), 2 => array($contact_id, 'Integer'));
235 CRM_Core_DAO
::executeQuery($sql, $params);
237 // remove 'Unconfirmed' tag for this contact
238 $tag_name = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
245 DELETE FROM civicrm_entity_tag
246 WHERE entity_table = 'civicrm_contact'
248 AND tag_id = ( SELECT id FROM civicrm_tag WHERE name = %2 )";
249 $params = array(1 => array($contact_id, 'Integer'),
250 2 => array($tag_name, 'String'),
252 CRM_Core_DAO
::executeQuery($sql, $params);
254 // set permanent cookie to indicate this users email address now confirmed
255 setcookie("confirmed_{$petition_id}",
257 time() +
$this->cookieExpire
,
265 * Function to get Petition Signature Total
267 * @param boolean $all
271 static function getPetitionSignatureTotalbyCountry($surveyId) {
272 $countries = array();
274 SELECT count(civicrm_address.country_id) as total,
275 IFNULL(country_id,'') as country_id,IFNULL(iso_code,'') as country_iso, IFNULL(civicrm_country.name,'') as country
276 FROM civicrm_activity a, civicrm_survey, civicrm_contact
277 LEFT JOIN civicrm_address ON civicrm_address.contact_id = civicrm_contact.id AND civicrm_address.is_primary = 1
278 LEFT JOIN civicrm_country ON civicrm_address.country_id = civicrm_country.id
280 a.source_contact_id = civicrm_contact.id AND
281 a.activity_type_id = civicrm_survey.activity_type_id AND
282 civicrm_survey.id = %1 AND
283 a.source_record_id = %1 ";
285 $params = array(1 => array($surveyId, 'Integer'));
286 $sql .= " GROUP BY civicrm_address.country_id";
287 $fields = array('total', 'country_id', 'country_iso', 'country');
289 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
290 while ($dao->fetch()) {
292 foreach ($fields as $field) {
293 $row[$field] = $dao->$field;
301 * Function to get Petition Signature Total
303 * @param boolean $all
307 static function getPetitionSignatureTotal($surveyId) {
308 $surveyInfo = CRM_Campaign_BAO_Petition
::getSurveyInfo((int) $surveyId);
309 //$activityTypeID = $surveyInfo['activity_type_id'];
310 $signature = array();
314 status_id,count(id) as total
315 FROM civicrm_activity
317 source_record_id = " . (int) $surveyId . " AND activity_type_id = " . (int) $surveyInfo['activity_type_id'] . " GROUP BY status_id";
319 $statusTotal = array();
321 $dao = CRM_Core_DAO
::executeQuery($sql);
322 while ($dao->fetch()) {
323 $total +
= $dao->total
;
324 $statusTotal['status'][$dao->status_id
] = $dao->total
;
326 $statusTotal['count'] = $total;
331 public function getSurveyInfo($surveyId = NULL) {
332 $surveyInfo = array();
335 SELECT activity_type_id,
338 ov.label AS activity_type
339 FROM civicrm_survey s, civicrm_option_value ov, civicrm_option_group og
340 WHERE s.id = " . (int) $surveyId ."
341 AND s.activity_type_id = ov.value
342 AND ov.option_group_id = og.id
343 AND og.name = 'activity_type'";
345 $dao = CRM_Core_DAO
::executeQuery($sql);
346 while ($dao->fetch()) {
347 //$survey['campaign_id'] = $dao->campaign_id;
348 //$survey['campaign_name'] = $dao->campaign_name;
349 $surveyInfo['activity_type'] = $dao->activity_type
;
350 $surveyInfo['activity_type_id'] = $dao->activity_type_id
;
351 $surveyInfo['title'] = $dao->title
;
358 * Function to get Petition Signature Details
360 * @param boolean $all
364 static function getPetitionSignature($surveyId, $status_id = NULL) {
366 // sql injection protection
367 $surveyId = (int)$surveyId;
368 $signature = array();
372 a.source_record_id as survey_id,
373 a.activity_date_time,
375 civicrm_contact.id as contact_id,
376 civicrm_contact.contact_type,civicrm_contact.contact_sub_type,image_URL,
377 first_name,last_name,sort_name,
378 employer_id,organization_name,
380 IFNULL(gender_id,'') AS gender_id,
381 IFNULL(state_province_id,'') AS state_province_id,
382 IFNULL(country_id,'') as country_id,IFNULL(iso_code,'') as country_iso, IFNULL(civicrm_country.name,'') as country
383 FROM civicrm_activity a, civicrm_survey, civicrm_contact
384 LEFT JOIN civicrm_address ON civicrm_address.contact_id = civicrm_contact.id AND civicrm_address.is_primary = 1
385 LEFT JOIN civicrm_country ON civicrm_address.country_id = civicrm_country.id
387 a.source_contact_id = civicrm_contact.id AND
388 a.activity_type_id = civicrm_survey.activity_type_id AND
389 civicrm_survey.id = %1 AND
390 a.source_record_id = %1 ";
392 $params = array(1 => array($surveyId, 'Integer'));
395 $sql .= " AND status_id = %2";
396 $params[2] = array($status_id, 'Integer');
398 $sql .= " ORDER BY a.activity_date_time";
401 'id', 'survey_id', 'contact_id',
402 'activity_date_time', 'activity_type_id',
403 'status_id', 'first_name', 'last_name',
404 'sort_name', 'gender_id', 'country_id',
405 'state_province_id', 'country_iso', 'country',
409 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
410 while ($dao->fetch()) {
412 foreach ($fields as $field) {
413 $row[$field] = $dao->$field;
421 * This function returns all entities assigned to a specific tag
423 * @param object $tag an object of a tag.
425 * @return array $contactIds array of contact ids
428 function getEntitiesByTag($tag) {
429 $contactIds = array();
430 $entityTagDAO = new CRM_Core_DAO_EntityTag();
431 $entityTagDAO->tag_id
= $tag['id'];
432 $entityTagDAO->find();
434 while ($entityTagDAO->fetch()) {
435 $contactIds[] = $entityTagDAO->entity_id
;
441 * Function to check if contact has signed this petition
443 * @param int $surveyId
444 * @param int $contactId
447 static function checkSignature($surveyId, $contactId) {
449 $surveyInfo = CRM_Campaign_BAO_Petition
::getSurveyInfo($surveyId);
450 $signature = array();
454 a.source_record_id AS source_record_id,
455 a.source_contact_id AS source_contact_id,
456 a.activity_date_time AS activity_date_time,
457 a.activity_type_id AS activity_type_id,
458 a.status_id AS status_id,
460 FROM civicrm_activity a
461 WHERE a.source_record_id = %2
462 AND a.activity_type_id = %3
463 AND a.source_contact_id = %4
465 $params = array(1 => array($surveyInfo['title'], 'String'),
466 2 => array($surveyId, 'Integer'),
467 3 => array($surveyInfo['activity_type_id'], 'Integer'),
468 4 => array($contactId, 'Integer'),
471 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
472 while ($dao->fetch()) {
473 $signature[$dao->id
]['id'] = $dao->id
;
474 $signature[$dao->id
]['source_record_id'] = $dao->source_record_id
;
475 $signature[$dao->id
]['source_contact_id'] = CRM_Contact_BAO_Contact
::displayName($dao->source_contact_id
);
476 $signature[$dao->id
]['activity_date_time'] = $dao->activity_date_time
;
477 $signature[$dao->id
]['activity_type_id'] = $dao->activity_type_id
;
478 $signature[$dao->id
]['status_id'] = $dao->status_id
;
479 $signature[$dao->id
]['survey_title'] = $dao->survey_title
;
480 $signature[$dao->id
]['contactId'] = $dao->source_contact_id
;
487 * takes an associative array and sends a thank you or email verification email
489 * @param array $params (reference ) an assoc array of name/value pairs
495 function sendEmail($params, $sendEmailMode) {
498 * CRM_Campaign_Form_Petition_Signature::EMAIL_THANK
499 * connected user via login/pwd - thank you
500 * or dedupe contact matched who doesn't have a tag CIVICRM_TAG_UNCONFIRMED - thank you
501 * or login using fb connect - thank you + click to add msg to fb wall
503 * CRM_Campaign_Form_Petition_Signature::EMAIL_CONFIRM
504 * send a confirmation request email
507 // check if the group defined by CIVICRM_PETITION_CONTACTS exists, else create it
508 $petitionGroupName = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
514 $dao = new CRM_Contact_DAO_Group();
515 $dao->title
= $petitionGroupName;
516 if (!$dao->find(TRUE)) {
518 $dao->visibility
= 'Public Pages';
521 $group_id = $dao->id
;
524 $petitionParams['id'] = $params['sid'];
525 $petitionInfo = array();
526 CRM_Campaign_BAO_Survey
::retrieve($petitionParams, $petitionInfo);
527 if (empty($petitionInfo)) {
528 CRM_Core_Error
::fatal('Petition doesn\'t exist.');
531 //get the default domain email address.
532 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain
::getNameAndEmail();
534 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
536 $toName = CRM_Contact_BAO_Contact
::displayName($params['contactId']);
538 $replyTo = "do-not-reply@$emailDomain";
540 // set additional general message template params (custom tokens to use in email msg templates)
541 // tokens then available in msg template as {$petition.title}, etc
542 $petitionTokens['title'] = $petitionInfo['title'];
543 $petitionTokens['petitionId'] = $params['sid'];
544 $tplParams['petition'] = $petitionTokens;
546 switch ($sendEmailMode) {
547 case CRM_Campaign_Form_Petition_Signature
::EMAIL_THANK
:
549 // add this contact to the CIVICRM_PETITION_CONTACTS group
550 // Cannot pass parameter 1 by reference
551 $p = array($params['contactId']);
552 CRM_Contact_BAO_GroupContact
::addContactsToGroup($p, $group_id, 'API');
554 if ($params['email-Primary']) {
555 CRM_Core_BAO_MessageTemplates
::sendTemplate(
557 'groupName' => 'msg_tpl_workflow_petition',
558 'valueName' => 'petition_sign',
559 'contactId' => $params['contactId'],
560 'tplParams' => $tplParams,
561 'from' => "\"{$domainEmailName}\" <{$domainEmailAddress}>",
563 'toEmail' => $params['email-Primary'],
564 'replyTo' => $replyTo,
565 'petitionId' => $params['sid'],
566 'petitionTitle' => $petitionInfo['title'],
572 case CRM_Campaign_Form_Petition_Signature
::EMAIL_CONFIRM
:
573 // create mailing event subscription record for this contact
574 // this will allow using a hash key to confirm email address by sending a url link
575 $se = CRM_Mailing_Event_BAO_Subscribe
::subscribe($group_id,
576 $params['email-Primary'],
580 // require_once 'CRM/Core/BAO/Domain.php';
581 // $domain = CRM_Core_BAO_Domain::getDomain();
582 $config = CRM_Core_Config
::singleton();
583 $localpart = CRM_Core_BAO_MailSettings
::defaultLocalpart();
585 $replyTo = implode($config->verpSeparator
,
595 $confirmUrl = CRM_Utils_System
::url('civicrm/petition/confirm',
596 "reset=1&cid={$se->contact_id}&sid={$se->id}&h={$se->hash}&a={$params['activityId']}&p={$params['sid']}",
599 $confirmUrlPlainText = CRM_Utils_System
::url('civicrm/petition/confirm',
600 "reset=1&cid={$se->contact_id}&sid={$se->id}&h={$se->hash}&a={$params['activityId']}&p={$params['sid']}",
606 // set email specific message template params and assign to tplParams
607 $petitionTokens['confirmUrl'] = $confirmUrl;
608 $petitionTokens['confirmUrlPlainText'] = $confirmUrlPlainText;
609 $tplParams['petition'] = $petitionTokens;
611 if ($params['email-Primary']) {
612 CRM_Core_BAO_MessageTemplates
::sendTemplate(
614 'groupName' => 'msg_tpl_workflow_petition',
615 'valueName' => 'petition_confirmation_needed',
616 'contactId' => $params['contactId'],
617 'tplParams' => $tplParams,
618 'from' => "\"{$domainEmailName}\" <{$domainEmailAddress}>",
620 'toEmail' => $params['email-Primary'],
621 'replyTo' => $replyTo,
622 'petitionId' => $params['sid'],
623 'petitionTitle' => $petitionInfo['title'],
624 'confirmUrl' => $confirmUrl,