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

dev/core#4213 Make frontend_title, name required for Group entity #26546

Merged
merged 4 commits into from Jul 14, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 17 additions & 34 deletions CRM/Campaign/BAO/Petition.php
Expand Up @@ -9,6 +9,8 @@
+--------------------------------------------------------------------+
*/

use Civi\Api4\Group;

/**
*
* @package CRM
Expand Down Expand Up @@ -514,32 +516,21 @@ public static function checkSignature($surveyId, $contactId) {
* (reference ) an assoc array of name/value pairs.
*
* @param int $sendEmailMode
* CRM_Campaign_Form_Petition_Signature::EMAIL_THANK or CRM_Campaign_Form_Petition_Signature::EMAIL_CONFIRM
*
* @throws Exception
* @throws CRM_Core_Exception
*/
public static function sendEmail($params, $sendEmailMode) {

/* sendEmailMode
* CRM_Campaign_Form_Petition_Signature::EMAIL_THANK
* connected user via login/pwd - thank you
* or dedupe contact matched who doesn't have a tag CIVICRM_TAG_UNCONFIRMED - thank you
* or login using fb connect - thank you + click to add msg to fb wall
*
* CRM_Campaign_Form_Petition_Signature::EMAIL_CONFIRM
* send a confirmation request email
*/

// check if the group defined by CIVICRM_PETITION_CONTACTS exists, else create it
$petitionGroupName = Civi::settings()->get('petition_contacts');

$dao = new CRM_Contact_DAO_Group();
$dao->title = $petitionGroupName;
if (!$dao->find(TRUE)) {
$dao->is_active = 1;
$dao->visibility = 'User and User Admin Only';
$dao->save();
public static function sendEmail(array $params, int $sendEmailMode): void {
$surveyID = $params['sid'];
$contactID = $params['contactId'];
$activityID = $params['activityId'] ?? NULL;
$group_id = Group::get(FALSE)->addWhere('title', '=', Civi::settings()->get('petition_contacts'))->addSelect('id')->execute()->first()['id'] ?? NULL;
if (!$group_id) {
$group_id = Group::create(FALSE)->setValues([
'title' => Civi::settings()->get('petition_contacts'),
'visibility' => 'User and User Admin Only',
])->execute()->first()['id'];
}
$group_id = $dao->id;

// get petition info
$petitionParams['id'] = $params['sid'];
Expand All @@ -550,7 +541,7 @@ public static function sendEmail($params, $sendEmailMode) {
}

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

$emailDomain = CRM_Core_BAO_MailSettings::defaultDomain();

Expand All @@ -568,25 +559,17 @@ public static function sendEmail($params, $sendEmailMode) {

switch ($sendEmailMode) {
case CRM_Campaign_Form_Petition_Signature::EMAIL_THANK:

// add this contact to the CIVICRM_PETITION_CONTACTS group
// Cannot pass parameter 1 by reference
$p = [$params['contactId']];
CRM_Contact_BAO_GroupContact::addContactsToGroup($p, $group_id, 'API');
CRM_Contact_BAO_GroupContact::addContactsToGroup([$contactID], $group_id, 'API');

if ($params['email-Primary']) {
CRM_Core_BAO_MessageTemplate::sendTemplate(
[
'groupName' => 'msg_tpl_workflow_petition',
'workflow' => 'petition_sign',
'contactId' => $params['contactId'],
'tplParams' => $tplParams,
'modelProps' => ['surveyID' => $surveyID, 'contactID' => $contactID],
'from' => "\"{$domainEmailName}\" <{$domainEmailAddress}>",
'toName' => $toName,
'toEmail' => $params['email-Primary'],
'replyTo' => $replyTo,
'petitionId' => $params['sid'],
'petitionTitle' => $petitionInfo['title'],
]
);
}
Expand Down
13 changes: 7 additions & 6 deletions CRM/Contact/BAO/Group.php
Expand Up @@ -342,6 +342,11 @@ public static function create(&$params) {
'parents' => NULL,
];

if (empty($params['id']) && empty($params['frontend_title'])) {
// If we were calling writeRecord it would handle this, but we need
// to migrate the other bits of magic.
$params['frontend_title'] = $params['title'];
}
$hook = empty($params['id']) ? 'create' : 'edit';
CRM_Utils_Hook::pre($hook, 'Group', $params['id'] ?? NULL, $params);

Expand Down Expand Up @@ -1091,12 +1096,8 @@ public static function getGroupsHierarchy(
$title = $dao->title;
$description = $dao->description;
if ($public) {
if (!empty($dao->frontend_title)) {
$title = $dao->frontend_title;
}
if (!empty($dao->frontend_description)) {
$description = $dao->frontend_description;
}
$title = $dao->frontend_title;
$description = $dao->frontend_description;
}
if ($dao->parents) {
$parentArray = explode(',', $dao->parents);
Expand Down
18 changes: 13 additions & 5 deletions CRM/Contact/DAO/Group.php
Expand Up @@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Contact/Group.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:e98767d560ee98268e800a2aec983cea)
* (GenCodeChecksum:5cb6d58b6f91122093003d8c0db4fab1)
*/

/**
Expand Down Expand Up @@ -68,7 +68,7 @@ class CRM_Contact_DAO_Group extends CRM_Core_DAO {
/**
* Internal name of Group.
*
* @var string|null
* @var string
* (SQL type: varchar(64))
* Note that values will be retrieved from the database as a string.
*/
Expand All @@ -77,7 +77,7 @@ class CRM_Contact_DAO_Group extends CRM_Core_DAO {
/**
* Name of Group.
*
* @var string|null
* @var string
* (SQL type: varchar(255))
* Note that values will be retrieved from the database as a string.
*/
Expand Down Expand Up @@ -134,6 +134,7 @@ class CRM_Contact_DAO_Group extends CRM_Core_DAO {
* @var string|null
* (SQL type: text)
* Note that values will be retrieved from the database as a string.
* @deprecated
*/
public $where_clause;

Expand All @@ -143,6 +144,7 @@ class CRM_Contact_DAO_Group extends CRM_Core_DAO {
* @var string|null
* (SQL type: text)
* Note that values will be retrieved from the database as a string.
* @deprecated
*/
public $select_tables;

Expand All @@ -152,6 +154,7 @@ class CRM_Contact_DAO_Group extends CRM_Core_DAO {
* @var string|null
* (SQL type: text)
* Note that values will be retrieved from the database as a string.
* @deprecated
*/
public $where_tables;

Expand Down Expand Up @@ -238,7 +241,7 @@ class CRM_Contact_DAO_Group extends CRM_Core_DAO {
/**
* Alternative public title for this Group.
*
* @var string|null
* @var string
* (SQL type: varchar(255))
* Note that values will be retrieved from the database as a string.
*/
Expand Down Expand Up @@ -324,6 +327,7 @@ public static function &fields() {
'type' => CRM_Utils_Type::T_STRING,
'title' => ts('Group Name'),
'description' => ts('Internal name of Group.'),
'required' => TRUE,
'maxlength' => 64,
'size' => CRM_Utils_Type::BIG,
'usage' => [
Expand All @@ -344,6 +348,7 @@ public static function &fields() {
'type' => CRM_Utils_Type::T_STRING,
'title' => ts('Group Title'),
'description' => ts('Name of Group.'),
'required' => TRUE,
'maxlength' => 255,
'size' => CRM_Utils_Type::HUGE,
'usage' => [
Expand Down Expand Up @@ -494,6 +499,7 @@ public static function &fields() {
'entity' => 'Group',
'bao' => 'CRM_Contact_BAO_Group',
'localizable' => 0,
'deprecated' => TRUE,
'readonly' => TRUE,
'add' => '1.6',
],
Expand All @@ -514,6 +520,7 @@ public static function &fields() {
'bao' => 'CRM_Contact_BAO_Group',
'localizable' => 0,
'serialize' => self::SERIALIZE_PHP,
'deprecated' => TRUE,
'readonly' => TRUE,
'add' => '1.6',
],
Expand All @@ -534,6 +541,7 @@ public static function &fields() {
'bao' => 'CRM_Contact_BAO_Group',
'localizable' => 0,
'serialize' => self::SERIALIZE_PHP,
'deprecated' => TRUE,
'readonly' => TRUE,
'add' => '1.6',
],
Expand Down Expand Up @@ -756,6 +764,7 @@ public static function &fields() {
'type' => CRM_Utils_Type::T_STRING,
'title' => ts('Public Group Title'),
'description' => ts('Alternative public title for this Group.'),
'required' => TRUE,
'maxlength' => 255,
'size' => CRM_Utils_Type::HUGE,
'usage' => [
Expand All @@ -765,7 +774,6 @@ public static function &fields() {
'token' => FALSE,
],
'where' => 'civicrm_group.frontend_title',
'default' => NULL,
'table_name' => 'civicrm_group',
'entity' => 'Group',
'bao' => 'CRM_Contact_BAO_Group',
Expand Down
6 changes: 4 additions & 2 deletions CRM/Core/I18n/SchemaStructure.php
Expand Up @@ -92,8 +92,8 @@ public static function &columns() {
'description' => "text COMMENT 'Optional description.'",
],
'civicrm_group' => [
'title' => "varchar(255) COMMENT 'Name of Group.'",
'frontend_title' => "varchar(255) DEFAULT NULL COMMENT 'Alternative public title for this Group.'",
'title' => "varchar(255) NOT NULL COMMENT 'Name of Group.'",
'frontend_title' => "varchar(255) NOT NULL COMMENT 'Alternative public title for this Group.'",
'frontend_description' => "text DEFAULT NULL COMMENT 'Alternative public description of the group.'",
],
'civicrm_contribution_page' => [
Expand Down Expand Up @@ -416,9 +416,11 @@ public static function &widgets() {
'civicrm_group' => [
'title' => [
'type' => "Text",
'required' => "true",
],
'frontend_title' => [
'type' => "Text",
'required' => "TRUE",
],
'frontend_description' => [
'type' => "TextArea",
Expand Down
4 changes: 2 additions & 2 deletions CRM/Group/Form/Edit.php
Expand Up @@ -62,13 +62,13 @@ class CRM_Group_Form_Edit extends CRM_Core_Form {
*/
protected function setEntityFields() {
$this->entityFields = [
'frontend_title' => ['name' => 'frontend_title', 'required' => TRUE],
'frontend_description' => ['name' => 'frontend_description'],
'title' => [
'name' => 'title',
'required' => TRUE,
],
'description' => ['name' => 'description'],
'frontend_title' => ['name' => 'frontend_title'],
'frontend_description' => ['name' => 'frontend_description'],
];
}

Expand Down
5 changes: 3 additions & 2 deletions CRM/Mailing/Event/BAO/MailingEventConfirm.php
Expand Up @@ -113,8 +113,9 @@ public static function confirm(int $contact_id, int $subscribe_id, string $hash)
$bao->body_html = $html;
$templates = $bao->getTemplates();

$html = CRM_Utils_Token::replaceWelcomeTokens($templates['html'], $group->title, TRUE);
$text = CRM_Utils_Token::replaceWelcomeTokens($templates['text'], $group->title, FALSE);
// We can stop doing this here once it has been done in an upgrade script.
$html = str_replace('{welcome.group}', '{group.frontend_title}', $templates['html']);
$text = str_replace('{welcome.group}', '{group.frontend_title}', $templates['text']);

$tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [
'controller' => __CLASS__,
Expand Down
8 changes: 4 additions & 4 deletions CRM/Mailing/Event/BAO/MailingEventUnsubscribe.php
Expand Up @@ -278,18 +278,18 @@ public static function unsub_from_mailing($job_id, $queue_id, $hash, $return = F
}
while ($doAdded->fetch()) {
$returnGroups[$doAdded->group_id] = [
'title' => !empty($doAdded->frontend_title) ? $doAdded->frontend_title : $doAdded->title,
'description' => !empty($doAdded->frontend_description) ? $doAdded->frontend_description : $doAdded->description,
'title' => $doAdded->frontend_title,
'description' => $doAdded->frontend_description,
];
}
return $returnGroups;
}
else {
while ($doCached->fetch()) {
$groups[$doCached->group_id] = !empty($doCached->frontend_title) ? $doCached->frontend_title : $doCached->title;
$groups[$doCached->group_id] = $doCached->frontend_title;
}
while ($doAdded->fetch()) {
$groups[$doAdded->group_id] = !empty($doAdded->frontend_title) ? $doAdded->frontend_title : $doAdded->title;
$groups[$doAdded->group_id] = $doAdded->frontend_title;
}
}
$transaction = new CRM_Core_Transaction();
Expand Down
4 changes: 2 additions & 2 deletions CRM/Mailing/Form/Subscribe.php
Expand Up @@ -89,8 +89,8 @@ public function buildQuickForm() {
while ($dao->fetch()) {
$row = [];
$row['id'] = $dao->id;
$row['title'] = $dao->frontend_title ?? $dao->title;
$row['description'] = $dao->frontend_description ?? $dao->description;
$row['title'] = $dao->frontend_title;
$row['description'] = $dao->frontend_description;
$row['checkbox'] = CRM_Core_Form::CB_PREFIX . $row['id'];
$this->addElement('checkbox',
$row['checkbox'],
Expand Down
9 changes: 9 additions & 0 deletions CRM/Upgrade/Incremental/MessageTemplates.php
Expand Up @@ -350,6 +350,15 @@ protected function getTemplateUpdates() {
['name' => 'contribution_offline_receipt', 'type' => 'html'],
],
],
[
'version' => '5.65.alpha1',
'upgrade_descriptor' => ts('Update to use tokens'),
'templates' => [
['name' => 'petition_sign', 'type' => 'text'],
['name' => 'petition_sign', 'type' => 'html'],
['name' => 'petition_sign', 'type' => 'subject'],
],
],
];
}

Expand Down
5 changes: 5 additions & 0 deletions CRM/Upgrade/Incremental/php/FiveSixtyFive.php
Expand Up @@ -29,6 +29,11 @@ class CRM_Upgrade_Incremental_php_FiveSixtyFive extends CRM_Upgrade_Incremental_
*/
public function upgrade_5_65_alpha1($rev): void {
$this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
// These 3 should run after the sql file.
$this->addTask('Make Group.name required', 'alterColumn', 'civicrm_group', 'name', "varchar(255) NOT NULL COMMENT 'Internal name of Group.'");
$this->addTask('Make Group.title required', 'alterColumn', 'civicrm_group', 'title', "varchar(255) NOT NULL COMMENT 'Alternative public title for this Group.'", TRUE);
$this->addTask('Make Group.frontend_title required', 'alterColumn', 'civicrm_group', 'frontend_title', "varchar(255) NOT NULL COMMENT 'Alternative public description of the group.'", TRUE);

}

}
30 changes: 30 additions & 0 deletions CRM/Upgrade/Incremental/sql/5.65.alpha1.mysql.tpl
@@ -1 +1,31 @@
{* file to handle db changes in 5.65.alpha1 during upgrade *}

-- Ensure new name field is not null/unique. Setting to ID is a bit lazy - but it works.
UPDATE civicrm_group SET `name` = `id` WHERE name IS NULL;

-- Add name field, make frontend_title required (in conjunction with php function)
{if $multilingual}
{foreach from=$locales item=locale}
UPDATE `civicrm_group`
SET `frontend_title_{$locale}` = `title_{$locale}`
WHERE `frontend_title_{$locale}` IS NULL OR `frontend_title_{$locale}` = '';

UPDATE `civicrm_group`
SET `frontend_description_{$locale}` = `description`
WHERE `frontend_description_{$locale}` IS NULL OR `frontend_description_{$locale}` = '' AND `description` <> '';
Copy link
Contributor

Choose a reason for hiding this comment

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

My eye just caught this now too. I think it's intended to be (frontend_description_{$locale} IS NULL OR frontend_description_{$locale} = '') AND description <> '' but it gets evaluated as frontend_description_{$locale} IS NULL OR (frontend_description_{$locale} = '' AND description <> ''). In mysql, unlike everything else, AND has a higher precedence than OR: https://dev.mysql.com/doc/refman/8.0/en/operator-precedence.html

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like you fixed this too - thanks!

{/foreach}
{else}
UPDATE `civicrm_group`
SET `frontend_title` = `title`
WHERE `frontend_title` IS NULL OR `frontend_title` = '';

UPDATE `civicrm_group`
SET `frontend_description` = `description`
WHERE `frontend_description` IS NULL OR `frontend_description` = '' AND description <> '';
{/if}

UPDATE civicrm_mailing_component
SET body_html = REPLACE(body_html, '{welcome.group}', '{group.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.

This doesn't work because it tries to interpret these tokens as smarty.

Copy link
Contributor

Choose a reason for hiding this comment

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

PR: #26871

Copy link
Contributor Author

Choose a reason for hiding this comment

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

body_text = REPLACE(body_text, '{welcome.group}', '{group.frontend_title}'),
subject = REPLACE(subject, '{welcome.group}', '{group.frontend_title}')
WHERE component_type = 'Welcome';
3 changes: 3 additions & 0 deletions CRM/Utils/Token.php
Expand Up @@ -926,6 +926,8 @@ public static function &replaceSubscribeInviteTokens($str) {
/**
* Replace welcome/confirmation tokens
*
* @deprecated since 5.65 will be removed around 5.71
*
* @param string $str
* The string with tokens to be replaced.
* @param string $group
Expand All @@ -937,6 +939,7 @@ public static function &replaceSubscribeInviteTokens($str) {
* The processed string
*/
public static function &replaceWelcomeTokens($str, $group, $html) {
CRM_Core_Error::deprecatedFunctionWarning('use the token processor');
if (self::token_match('welcome', 'group', $str)) {
self::token_replace('welcome', 'group', $group, $str);
}
Expand Down