/** * 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; }
/** * 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); }