Skip to content

Commit

Permalink
Merge branch 'pick_profile_by_id'
Browse files Browse the repository at this point in the history
[#73] Identify profiles by ID
  • Loading branch information
jensschuppe committed Apr 5, 2024
2 parents 8bcdff4 + fc40af8 commit aa2c938
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 89 deletions.
93 changes: 56 additions & 37 deletions CRM/Twingle/Form/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -139,10 +145,10 @@ public function preProcess(): void {
$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.
Expand All @@ -162,60 +168,70 @@ public function buildQuickForm(): void {

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,
],
]);
}
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';
}
$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]));
$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;
}
}
$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';
Expand Down Expand Up @@ -632,9 +648,12 @@ public function validate() {
*/
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;
}
Expand Down
3 changes: 2 additions & 1 deletion CRM/Twingle/Form/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ public function validate() {
// 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',
Expand Down
7 changes: 4 additions & 3 deletions CRM/Twingle/Page/Profiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ class CRM_Twingle_Page_Profiles extends CRM_Core_Page {
public function run():void {
CRM_Utils_System::setTitle(E::ts('Twingle API Profiles'));
$profiles = [];
foreach (CRM_Twingle_Profile::getProfiles() as $profile_name => $profile) {
$profiles[$profile_name]['name'] = $profile_name;
foreach (CRM_Twingle_Profile::getProfiles() as $profile_id => $profile) {
$profiles[$profile_id]['id'] = $profile_id;
$profiles[$profile_id]['name'] = $profile->getName();
foreach (CRM_Twingle_Profile::allowedAttributes() as $attribute) {
$profiles[$profile_name][$attribute] = $profile->getAttribute($attribute);
$profiles[$profile_id][$attribute] = $profile->getAttribute($attribute);
}
}
$this->assign('profiles', $profiles);
Expand Down
Loading

0 comments on commit aa2c938

Please sign in to comment.