handle existing projects

This commit is contained in:
Marc Michalsky forumZFD 2020-09-29 08:54:33 +02:00
parent 5d597de5de
commit e923ba91dc
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9

View file

@ -60,23 +60,6 @@ class TwingleProject {
self::$bInitialized = TRUE; self::$bInitialized = TRUE;
} }
$json_file = file_get_contents(E::path() .
'/CRM/TwingleCampaign/Upgrader/resources/campaigns.json');
$campaign_info = json_decode($json_file, TRUE);
if (!$campaign_info) {
\Civi::log()->error("Could not read json file");
throw new \Exception('Could not read json file');
}
foreach ($campaign_info['custom_fields'] as $custom_field) {
$result = CustomField::fetch($custom_field['name']);
array_push(self::$customFields, $result);
}
self::$bInitialized = TRUE;
}
/** /**
* Create the project as a campaign in CiviCRM if it does not exist * Create the project as a campaign in CiviCRM if it does not exist
* *
@ -84,12 +67,70 @@ class TwingleProject {
* @throws \CiviCRM_API3_Exception * @throws \CiviCRM_API3_Exception
*/ */
public function create() { public function create() {
$values = $this->values;
try { // Translate $value keys to custom field names
return civicrm_api3('Campaign', 'create', $values); $translatedValues = $this->translateValues();
} catch (\CiviCRM_API3_Exception $e) {
return null; // Create project if it does not exist yet and give back the result
if (!$this->exists()) {
$result = civicrm_api3('Campaign', 'create', $translatedValues);
$this->id = $result['id'];
$this->timestamp = $result['last_update'];
return [
'id' => $this->id,
'project_id' => $this->values['id'],
'state' => 'created',
];
} }
// Give information back if project already exists
return [
'id' => is_array($this->id) ? implode(', ', $this->id) : $this->id,
'project_id' => $this->values['id'],
'state' => 'exists',
];
}
/**
* Update an existing project
*
* @return array
* @throws \CiviCRM_API3_Exception
*/
public function update() {
// Translate $value keys to custom field names
$translatedValues = $this->translateValues();
$result = civicrm_api3('Campaign', 'create', $translatedValues);
return [
'title' => $this->values['title'],
'id' => $this->id,
'project_id' => $this->values['id'],
'state' => 'updated',
];
}
/**
* Translate $value keys to custom field names
*
* @return array
*/
private function translateValues() {
$values = [];
foreach (TwingleProject::$customFieldMapping as $field => $custom) {
if (array_key_exists(
str_replace('twingle_project_', '', $field),
$this->values)
) {
$values[$custom] = $this->values[str_replace(
'twingle_project_',
'',
$field)];
}
}
// Add necessary attributes
$values['title'] = $this->values['name'];
$values['campaign_type_id'] = 'twingle_project';
return $values;
} }
/** /**