/** * Returns default template ID. * If default template doesn't exist, it will install it * * @return int template ID * @throws Exception if there's something wrong with the default template */ public static function getDefaultTemplateID() { $default_template_title = sprintf("%s - %s", ts('Donation Receipts', array('domain' => 'de.systopia.donrec')), ts('Default template', array('domain' => 'de.systopia.donrec'))); $result = civicrm_api3('MessageTemplate', 'get', array('msg_title' => $default_template_title, 'return' => 'id')); if (!empty($result['id'])) { // we found it! return $result['id']; } if ($result['count'] > 1) { // oops, there's more of them... CRM_Core_Error::debug_log_message("de.systopia.donrec: getDefaultTemplate '{$default_template_title}' is ambiguous."); $first_result = reset($result['values']); return $first_result['id']; } // default template is not installed yet, so do it $default_template_file = dirname(__DIR__) . '/../../templates/Export/default_template.tpl'; $default_template_html = file_get_contents($default_template_file); if ($default_template_html === FALSE) { throw new Exception("Cannot load default template from '{$default_template_file}'."); } // TODO: what is this...? $workflowId = CRM_Donrec_DataStructure::getFirstUsedOptionValueId(); $params = array('msg_title' => $default_template_title, 'msg_html' => $default_template_html, 'is_active' => 1, 'workflow_id' => $workflowId, 'is_default' => 0, 'is_reserved' => 0); $result = CRM_Core_BAO_MessageTemplate::add($params); if ($result) { return $result->id; } else { throw new Exception("Cannot create default template."); } }