Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign profileID to template in UFNotify, add example #29441

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 2 additions & 38 deletions CRM/Core/BAO/UFGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2642,41 +2642,21 @@ public static function commonSendMail($contactID, &$values) {
return;

}
$template = CRM_Core_Smarty::singleton();

$displayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
$contactID,
'display_name'
);

self::profileDisplay($values['id'], $values['values'], $template);
$emailList = explode(',', $values['email']);

$contactLink = CRM_Utils_System::url('civicrm/contact/view',
"reset=1&cid=$contactID",
TRUE, NULL, FALSE, FALSE, TRUE
);

//get the default domain email address.
[$domainEmailName, $domainEmailAddress] = CRM_Core_BAO_Domain::getNameAndEmail();

if (!$domainEmailAddress || $domainEmailAddress == 'info@EXAMPLE.ORG') {
if (!$domainEmailAddress || $domainEmailAddress === 'info@EXAMPLE.ORG') {
$fixUrl = CRM_Utils_System::url('civicrm/admin/domain', 'action=update&reset=1');
CRM_Core_Error::statusBounce(ts('The site administrator needs to enter a valid \'FROM Email Address\' in <a href="%1">Administer CiviCRM &raquo; Communications &raquo; FROM Email Addresses</a>. The email address used may need to be a valid mail account with your email service provider.', [1 => $fixUrl]));
}

foreach ($emailList as $emailTo) {
// FIXME: take the below out of the foreach loop
CRM_Core_BAO_MessageTemplate::sendTemplate(
[
'groupName' => 'msg_tpl_workflow_uf',
'workflow' => 'uf_notify',
'contactId' => $contactID,
'tplParams' => [
'displayName' => $displayName,
'currentDate' => date('r'),
'contactLink' => $contactLink,
],
'modelProps' => ['contactID' => $contactID, 'profileID' => $values['id'], 'profileFields' => $values['values']],
'from' => "$domainEmailName <$domainEmailAddress>",
'toEmail' => $emailTo,
]
Expand Down Expand Up @@ -2722,22 +2702,6 @@ public static function checkFieldsEmptyValues($gid, $cid, $params, $skipCheck =
return NULL;
}

/**
* Assign uf fields to template.
*
* @param int $gid
* Group id.
* @param array $values
* @param CRM_Core_Smarty $template
*/
public static function profileDisplay($gid, $values, $template) {
$groupTitle = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $gid, 'title');
$template->assign('grouptitle', $groupTitle);
if (count($values)) {
$template->assign('values', $values);
}
}

/**
* Format fields for dupe Contact Matching.
*
Expand Down
74 changes: 74 additions & 0 deletions CRM/Core/WorkflowMessage/Profile/Profile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

use Civi\Api4\UFField;
use Civi\Api4\UFGroup;
use Civi\Api4\WorkflowMessage;
use Civi\Test;
use Civi\WorkflowMessage\WorkflowMessageExample;

/**
* Basic profile example.
*
* @noinspection PhpUnused
* @noinspection UnknownInspectionInspection
*/
class CRM_Core_WorkflowMessage_Profile_Profile extends WorkflowMessageExample {

/**
* Get the examples this class is able to deliver.
*/
public function getExamples(): iterable {
$workflows = ['uf_notify'];
foreach ($workflows as $workflow) {
yield [
'name' => 'workflow/' . $workflow . '/general',
'title' => ts('Profile Notification'),
'tags' => ['preview'],
'workflow' => $workflow,
];
}
}

/**
* Build an example to use when rendering the workflow.
*
* @param array $example
*
* @throws \CRM_Core_Exception
*/
public function build(array &$example): void {
$workFlow = WorkflowMessage::get(TRUE)->addWhere('name', '=', $example['workflow'])->execute()->first();
$this->setWorkflowName($workFlow['name']);
$messageTemplate = new $workFlow['class']();
$this->addExampleData($messageTemplate, $example);
$example['data'] = $this->toArray($messageTemplate);
}

/**
* Add relevant example data.
*
* @param \CRM_Core_WorkflowMessage_UFNotify $messageTemplate
* @param array $example
*
* @throws \CRM_Core_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
private function addExampleData(\CRM_Core_WorkflowMessage_UFNotify $messageTemplate, $example): void {
$contact = Test::example('entity/Contact/Barb');
$messageTemplate->setContact($contact);
$profile = UFGroup::get(FALSE)->setLimit(1)->execute()->first();
$fields = UFField::get(FALSE)->addWhere('id', '=', $profile['id'])->execute();
$values = [];
foreach ($fields as $field) {
if (isset($contact[$field['field_name']])) {
$values[$field['label']] = $contact[$field['field_name']];
}
else {
$values[$field['label']] = ts('User entered field');
}
}
$messageTemplate->setProfileID($profile['id']);
$messageTemplate->setProfileFields($values);
}

}
86 changes: 86 additions & 0 deletions CRM/Core/WorkflowMessage/UFNotify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

use Civi\WorkflowMessage\GenericWorkflowMessage;

/**
* Receipt sent when someone receives a copy of profile that has been filled out.
*
* @method int getProfileID()
* @method $this setProfileID(int $profileID)
* @method array getProfileFields()
* @method $this setProfileFields(array $profileFields)
*
* @support template-only
*
* @see CRM_Core_BAO_UFGroup::commonSendMail
*/
class CRM_Core_WorkflowMessage_UFNotify extends GenericWorkflowMessage {
public const WORKFLOW = 'uf_notify';

/**
* @var int
*
* @scope tplParams
*/
protected $profileID;

/**
* @var array
*
* @scope tplParams as values
*/
protected $profileFields;

/**
* @var string
*
* @scope tplParams
*/
protected $contactLink;

public function getContactLink(): string {
return CRM_Utils_System::url('civicrm/contact/view',
"reset=1&cid=" . $this->getContactID(),
TRUE, NULL, FALSE, FALSE, TRUE
);
}

/**
* @var string
*
* @scope tplParams as grouptitle
*/
protected $groupTitle;

public function getGroupTitle(): string {
return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->getProfileID(), 'frontend_title');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got error because my profile had missing frontend_title

TypeError: CRM_Core_WorkflowMessage_UFNotify::getGroupTitle(): Return value must be of type string, null returned in CRM_Core_WorkflowMessage_UFNotify->getGroupTitle() (line 65 of /Users/pradeep/Sites/drupal7/sites/all/modules/civicrm/CRM/Core/WorkflowMessage/UFNotify.php).


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using
CRM_Core_BAO_UFGroup::getFrontEndTitle($this->getProfileID());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pradpnayak did you test this by backporting it - in 5.72 frontend_title should be a required field

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @eileenmcnaughton

My bad, tested by upgrading to 5.71 beta. The frontend title is not updated with the title field and the field is required through UI.

}

/**
* @var string
*
* @scope tplParams
*/
protected $userDisplayName;

public function getUserDisplayName(): string {
$loggedInUser = CRM_Core_Session::getLoggedInContactID();
if (!$loggedInUser) {
return '';
}
return CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
$loggedInUser,
'display_name'
);
}

}
10 changes: 10 additions & 0 deletions CRM/Upgrade/Incremental/php/FiveSeventyTwo.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ class CRM_Upgrade_Incremental_php_FiveSeventyTwo extends CRM_Upgrade_Incremental
public function upgrade_5_72_alpha1($rev): void {
$this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
$this->addTask('Remove localized suffixes from civicrm_mailing_group.entity_table', 'fixMailingGroupEntityTable');

$this->addTask('Replace displayName smarty token in UFNotify subject',
'updateMessageToken', 'uf_notify', 'ts 1=$displayName', 'ts 1=$userDisplayName', $rev
);
$this->addTask('Replace displayName smarty token in UFNotify',
'updateMessageToken', 'uf_notify', '$displayName', 'contact.display_name', $rev
);
$this->addTask('Replace currentDate smarty token in UFNotify',
'updateMessageToken', 'uf_notify', '$currentDate', 'domain.now|crmDate:"Full"', $rev
);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions xml/templates/message_templates/uf_notify_html.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
{ts}Submitted For{/ts}
</td>
<td {$valueStyle}>
{$displayName}
{contact.display_name}
</td>
</tr>
<tr>
<td {$labelStyle}>
{ts}Date{/ts}
</td>
<td {$valueStyle}>
{$currentDate}
{domain.now|crmDate:"Full"}
</td>
</tr>
<tr>
Expand Down
2 changes: 1 addition & 1 deletion xml/templates/message_templates/uf_notify_subject.tpl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{$grouptitle} {ts 1=$displayName}Submitted by %1{/ts} - {contact.display_name}
{$grouptitle} {ts 1=$userDisplayName}Submitted by %1{/ts} - {contact.display_name}