improve default profile behavior
This commit is contained in:
parent
518f8809c7
commit
ab27dccbe7
4 changed files with 44 additions and 17 deletions
|
@ -194,25 +194,28 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
|
|||
// Assign template variables.
|
||||
$this->assign('op', $this->_op);
|
||||
$this->assign('profile_name', $profile_name);
|
||||
$this->assign('is_default', $this->profile->is_default());
|
||||
|
||||
// Add form elements.
|
||||
$is_default = $profile_name == 'default';
|
||||
$this->add(
|
||||
($is_default ? 'static' : 'text'),
|
||||
'name',
|
||||
E::ts('Profile name'),
|
||||
array(),
|
||||
!$is_default
|
||||
);
|
||||
|
||||
$this->add(
|
||||
'text', // field type
|
||||
'selector', // field name
|
||||
E::ts('Project IDs'), // field label
|
||||
['class' => 'huge'],
|
||||
TRUE // is required
|
||||
'name', // field name
|
||||
E::ts('Profile name'),
|
||||
['class' => 'huge'] + ($this->profile->is_default() && $this->_op == 'edit' ? ['readonly'] : []),
|
||||
!$this->profile->is_default()
|
||||
);
|
||||
|
||||
// Do only display selector if this is not the default profile
|
||||
if (!$this->profile->is_default()) {
|
||||
$this->add(
|
||||
'text', // field type
|
||||
'selector', // field name
|
||||
E::ts('Project IDs'), // field label
|
||||
['class' => 'huge'],
|
||||
TRUE // is required
|
||||
);
|
||||
}
|
||||
|
||||
$this->add(
|
||||
'select',
|
||||
'xcm_profile',
|
||||
|
|
|
@ -20,8 +20,10 @@ class CRM_Twingle_Page_Profiles extends CRM_Core_Page {
|
|||
public function run() {
|
||||
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();
|
||||
$profiles[$profile_id]['is_default'] = $profile->is_default();
|
||||
foreach (CRM_Twingle_Profile::allowedAttributes() as $attribute) {
|
||||
$profiles[$profile_name][$attribute] = $profile->getAttribute($attribute);
|
||||
}
|
||||
|
|
|
@ -124,6 +124,15 @@ class CRM_Twingle_Profile {
|
|||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this the default profile?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_default() {
|
||||
return $this->name == 'default';
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves an attribute of the profile.
|
||||
*
|
||||
|
@ -359,18 +368,29 @@ class CRM_Twingle_Profile {
|
|||
* @param $project_id
|
||||
*
|
||||
* @return CRM_Twingle_Profile
|
||||
* @throws \CRM_Twingle_Exceptions_ProfileException
|
||||
* @throws \Civi\Core\Exception\DBQueryException
|
||||
*/
|
||||
public static function getProfileForProject($project_id) {
|
||||
$profiles = self::getProfiles();
|
||||
$default_profile = NULL;
|
||||
|
||||
foreach ($profiles as $profile) {
|
||||
if ($profile->matches($project_id)) {
|
||||
return $profile;
|
||||
}
|
||||
if ($profile->is_default()) {
|
||||
$default_profile = $profile;
|
||||
}
|
||||
}
|
||||
|
||||
// If none matches, use the default profile.
|
||||
return $profiles['default'];
|
||||
if (!empty($default_profile)) {
|
||||
return $default_profile;
|
||||
}
|
||||
else {
|
||||
throw new ProfileException('Could not find default profile', ProfileException::ERROR_CODE_DEFAULT_PROFILE_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue