/** * generate a new, unique ID with the pattern passed in the constructor * * The generator needs to be locked before this can happen. * * @param $chunk the set of contributions used for this receipt as used in CRM_Donrec_Logic_Engine * @return unique ID string */ public function generateID($snapshot_lines) { // prepare tokens // FIXME: check for occurance $contact_id = $snapshot_lines[0]['contact_id']; $snapshot_line = isset($snapshot_lines['id']) ? $snapshot_lines : $snapshot_lines[0]; $this->tokens['contact_id'] = $snapshot_line['contact_id']; $this->tokens['issue_year'] = date("Y"); // get database-infos $table = CRM_Donrec_DataStructure::getTableName('zwb_donation_receipt'); $fields = CRM_Donrec_DataStructure::getCustomFields('zwb_donation_receipt'); $field = $fields['receipt_id']; // prepare pattern and regexp $serial_regexp = '/' . $this->serial_regexp . '/'; $pattern = $this->pattern; foreach ($this->tokens as $token => $value) { $pattern = str_replace("{" . $token . "}", $value, $pattern); } if ($this->is_test) { return preg_replace($serial_regexp, "TEST", $pattern); } // get the length and position of the serial-token preg_match($serial_regexp, $pattern, $match, PREG_OFFSET_CAPTURE); $serial_token_length = strlen($match[0][0]); $serial_token_position = $match[0][1]; // get everything behind the serial-token $serial_token_suffix = substr($pattern, $serial_token_position + $serial_token_length); // mysql counts from 1 $serial_token_position++; // build the LOCATE-part of the query if ($serial_token_suffix) { $length_query = "FOR LOCATE('{$serial_token_suffix}', `{$field}`) - {$serial_token_position}"; } // replace the token to get the mysql-regexp-string $mysql_regexp = '^' . preg_replace($serial_regexp, "[0-9]+", $pattern) . '$'; // build and run query $query = "\n SELECT MAX(CAST(SUBSTRING(`{$field}` FROM {$serial_token_position} {$length_query}) AS UNSIGNED))\n FROM `{$table}`\n WHERE `{$field}` REGEXP '{$mysql_regexp}'\n "; $last_serial = CRM_Core_DAO::singleValueQuery($query); // prepare receipt_id if ($last_serial) { $receipt_id = preg_replace($serial_regexp, $last_serial + 1, $pattern); } else { $receipt_id = preg_replace($serial_regexp, 1, $pattern); } // check length of receipt-id if (strlen($receipt_id) > 64) { $msg = "Receipt-ID is too long (Maximum length is 64 chars): '{$receipt_id}'"; CRM_Core_Error::debug_log_message("de.systopia.donrec: {$msg}"); throw new Exception($msg); } return $receipt_id; }
/** * Make sure all the data structures are there when the module is enabled */ public function enable() { // create snapshot database tables $this->executeSqlFile('sql/donrec.sql', true); // create/update custom groups CRM_Donrec_DataStructure::update(); // rename the custom fields according to l10. // FIXME: this is a workaround: if you do this before, the table name change, // BUT we should not be working with static table names CRM_Donrec_DataStructure::translateCustomGroups(); // make sure the template is there CRM_Donrec_Logic_Template::getDefaultTemplateID(); }
/** * get the profile object that was used to create this receipt */ public function getProfile() { CRM_Donrec_Logic_ReceiptItem::getCustomFields(); $receipt_table_name = CRM_Donrec_DataStructure::getTableName('zwb_donation_receipt'); $profile_column_name = CRM_Donrec_DataStructure::getCustomFields('zwb_donation_receipt')['profile']; $profile = CRM_Core_DAO::singleValueQuery("SELECT `{$profile_column_name}` FROM `{$receipt_table_name}` WHERE `id` = %1", array(1 => array($this->Id, 'Integer'))); return CRM_Donrec_Logic_Profile::getProfile($profile, TRUE); }
/** * 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."); } }