Merge branch 'pick_profile_by_id'

[#73] Identify profiles by ID
This commit is contained in:
Jens Schuppe 2024-04-05 12:27:06 +02:00
commit aa2c938abe
5 changed files with 200 additions and 89 deletions

View file

@ -33,6 +33,12 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
*/
protected ?CRM_Twingle_Profile $profile = NULL;
/**
* The ID of this profile.
* @var int|NULL
*/
protected $profile_id = NULL;
/**
* @var string
*
@ -139,10 +145,10 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
$op = CRM_Utils_Request::retrieve('op', 'String', $this);
$this->_op = is_string($op) ? $op : 'create';
// Verify that a profile with the given name exists.
$profile_name = CRM_Utils_Request::retrieve('name', 'String', $this);
if (is_string($profile_name)) {
$this->profile = CRM_Twingle_Profile::getProfile($profile_name);
// Verify that a profile with the given id exists.
if ($this->_op != 'copy' && $this->_op != 'create') {
$this->profile_id = CRM_Utils_Request::retrieve('id', 'Int', $this);
$this->profile = CRM_Twingle_Profile::getProfile($this->profile_id);
}
// Set redirect destination.
@ -162,12 +168,12 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
switch ($this->_op) {
case 'delete':
if (isset($profile_name)) {
CRM_Utils_System::setTitle(E::ts('Delete Twingle API profile <em>%1</em>', [1 => $profile_name]));
if ($this->profile) {
CRM_Utils_System::setTitle(E::ts('Delete Twingle API profile <em>%1</em>', [1 => $this->profile->getName()]));
$this->addButtons([
[
'type' => 'submit',
'name' => ($profile_name == 'default' ? E::ts('Reset') : E::ts('Delete')),
'name' => ($this->profile->getName() == 'default' ? E::ts('Reset') : E::ts('Delete')),
'isDefault' => TRUE,
],
]);
@ -175,47 +181,57 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
parent::buildQuickForm();
return;
case 'edit':
// When editing without a valid profile name, edit the default profile.
if (!isset($profile_name)) {
$profile_name = 'default';
$this->profile = CRM_Twingle_Profile::getProfile($profile_name);
}
if (!isset($this->profile)) {
throw new BaseException(E::ts('Could not retrieve profile with name "%1"', [1 => $profile_name]));
}
CRM_Utils_System::setTitle(E::ts('Edit Twingle API profile <em>%1</em>', [1 => $this->profile->getName()]));
break;
case 'copy':
// Retrieve the source profile name.
$profile_name = CRM_Utils_Request::retrieve('source_name', 'String', $this);
// When copying without a valid profile name, copy the default profile.
if (!is_string($profile_name)) {
$profile_name = 'default';
$source_id = CRM_Utils_Request::retrieve('source_id', 'Int', $this);
// When copying without a valid profile id, copy the default profile.
if (!$source_id) {
$this->profile = CRM_Twingle_Profile::createDefaultProfile();
} else {
try {
$source_profile = CRM_Twingle_Profile::getProfile($source_id);
$this->profile = $source_profile->copy();
$this->profile->validate();
} catch (ProfileValidationError $e) {
if ($e->getErrorCode() == ProfileValidationError::ERROR_CODE_PROFILE_VALIDATION_FAILED) {
Civi::log()->error($e->getLogMessage());
CRM_Core_Session::setStatus(E::ts('The profile is invalid and cannot be copied.'), E::ts('Error'));
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/settings/twingle/profiles', 'reset=1'));
return;
}
} catch (ProfileException $e) {
if ($e->getErrorCode() == ProfileException::ERROR_CODE_PROFILE_NOT_FOUND) {
Civi::log()->error($e->getLogMessage());
CRM_Core_Session::setStatus(E::ts('The profile to be copied could not be found.'), E::ts('Error'));
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/settings/twingle/profiles', 'reset=1'));
return;
}
}
catch (Civi\Core\Exception\DBQueryException $e) {
Civi::log()->error($e->getMessage());
CRM_Core_Session::setStatus(E::ts('A database error has occurred. See the log for details.'), E::ts('Error'));
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/settings/twingle/profiles', 'reset=1'));
return;
}
}
$originalProfile = CRM_Twingle_Profile::getProfile($profile_name);
if (!isset($originalProfile)) {
throw new BaseException(E::ts('Could not retrieve profile with name "%1"', [1 => $profile_name]));
}
$this->profile = clone $originalProfile;
// Propose a new name for this profile.
$profile_name = $profile_name . '_copy';
$this->profile->setName($profile_name);
CRM_Utils_System::setTitle(E::ts('New Twingle API profile'));
break;
case 'edit':
CRM_Utils_System::setTitle(E::ts('Edit Twingle API profile <em>%1</em>', [1 => $this->profile->getName()]));
break;
case 'create':
// Load factory default profile values.
$this->profile = CRM_Twingle_Profile::createDefaultProfile($profile_name ?? 'default');
$this->profile = CRM_Twingle_Profile::createDefaultProfile(E::ts('New Profile'));
CRM_Utils_System::setTitle(E::ts('New Twingle API profile'));
break;
}
// Assign template variables.
$this->assign('op', $this->_op);
$this->assign('profile_name', $profile_name);
$this->assign('profile_name', $this->profile->getName());
$this->assign('is_default', $this->profile->is_default());
// Add form elements.
$is_default = $profile_name == 'default';
@ -632,9 +648,12 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
*/
public function setDefaultValues() {
$defaults = parent::setDefaultValues();
if (in_array($this->_op, ['create', 'edit', 'copy'], TRUE)) {
$defaults['name'] = isset($this->profile) ? $this->profile->getName() : NULL;
$profile_data = isset($this->profile) ? $this->profile->getData() : [];
if (in_array($this->_op, ['create', 'edit', 'copy'])) {
if (!$this->profile) {
$this->profile = CRM_Twingle_Profile::createDefaultProfile()->copy();
}
$defaults['name'] = $this->profile->getName();
$profile_data = $this->profile->getData();
foreach ($profile_data as $element_name => $value) {
$defaults[$element_name] = $value;
}

View file

@ -134,7 +134,8 @@ class CRM_Twingle_Form_Settings extends CRM_Core_Form {
// if activity creation is active, make sure the fields are set
$protection_mode = $this->_submitValues['twingle_protect_recurring'] ?? NULL;
if ($protection_mode == CRM_Twingle_Config::RCUR_PROTECTION_ACTIVITY) {
foreach (['twingle_protect_recurring_activity_type',
foreach ([
'twingle_protect_recurring_activity_type',
'twingle_protect_recurring_activity_subject',
'twingle_protect_recurring_activity_status',
'twingle_protect_recurring_activity_assignee',