dolibarr 21.0.0-alpha
html_generic.modules.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2005-2006 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2012 Marcos GarcĂ­a <marcosgdf@gmail.com>
6 * Copyright (C) 2014-2020 Alexandre Spangaro <aspangaro@open-dsi.fr>
7 * Copyright (C) 2015 Benoit Bruchard <benoitb21@gmail.com>
8 * Copyright (C) 2015 Benjamin Neumann <btdn@sigsoft.org>
9 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
30require_once DOL_DOCUMENT_ROOT.'/core/modules/dons/modules_don.php';
31require_once DOL_DOCUMENT_ROOT.'/don/class/don.class.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
33
34
39{
45 public function __construct($db)
46 {
47 global $conf, $langs;
48
49 $this->db = $db;
50 $this->name = "generic";
51 $this->description = $langs->trans('DonationsReceiptModel').'';
52 $this->option_multilang = 1;
53
54 $this->type = 'html';
55 }
56
62 public function isEnabled()
63 {
64 return true;
65 }
66
73 private function loadTranslationFiles($outputlangs)
74 {
75 if (!is_object($outputlangs)) {
76 global $langs;
77 $outputlangs = $langs;
78 }
79
80 $outputlangs->loadLangs(array("main", "dict", "companies", "bills", "products", "donations"));
81
82 return $outputlangs;
83 }
84
91 private function getDonationPaymentType($don)
92 {
93 $formclass = new Form($this->db);
94
95 // This is not the proper way to do it but $formclass->form_modes_reglement
96 // prints the translation instead of returning it
97 $formclass->load_cache_types_paiements();
98
99 if ($don->mode_reglement_id) {
100 $paymentmode = $formclass->cache_types_paiements[$don->mode_reglement_id]['label'];
101 } else {
102 $paymentmode = '';
103 }
104
105 return $paymentmode;
106 }
107
116 private function getContents($don, $outputlangs, $currency)
117 {
118 global $user, $conf, $langs, $mysoc;
119
120 $now = dol_now();
121
122 $currency = !empty($currency) ? $currency : $conf->currency;
123
124 $donmodel = DOL_DOCUMENT_ROOT."/core/modules/dons/html_generic.html";
125 $form = implode('', file($donmodel));
126 $form = str_replace('__NOW__', dol_print_date($now, 'day', false, $outputlangs), $form);
127 $form = str_replace('__REF__', (string) $don->id, $form);
128 $form = str_replace('__DATE__', dol_print_date($don->date, 'day', false, $outputlangs), $form);
129
130 $form = str_replace('__BENEFICIARY_NAME__', $mysoc->name, $form);
131 $form = str_replace('__BENEFICIARY_FULL_ADDRESS__', $mysoc->getFullAddress(1, "<br>", 1), $form);
132
133 $form = str_replace('__PAYMENTMODE_LABEL__', $this->getDonationPaymentType($don), $form);
134 $form = str_replace('__AMOUNT__', price($don->amount), $form);
135 $form = str_replace('__CURRENCY_CODE__', $conf->currency, $form);
136 if (isModEnabled("societe") && getDolGlobalString('DONATION_USE_THIRDPARTIES') && $don->socid > 0 && $don->thirdparty) {
137 $form = str_replace('__DONOR_FULL_NAME__', $don->thirdparty->name, $form);
138 $form = str_replace('__DONOR_FULL_ADDRESS__', $don->thirdparty->getFullAddress(1, ", ", 1), $form);
139 } else {
140 $form = str_replace('__DONOR_FULL_NAME__', $don->getFullName($langs), $form);
141 $form = str_replace('__DONOR_FULL_ADDRESS__', $don->getFullAddress(1, " ", 1), $form);
142 }
143
144 $form = str_replace('__DonationTitle__', $outputlangs->trans("DonationTitle"), $form);
145 $form = str_replace('__DonationRef__', $outputlangs->trans("DonationRef"), $form);
146 $form = str_replace('__Date__', $outputlangs->trans("Date"), $form);
147 $form = str_replace('__DonationDatePayment__', $outputlangs->trans("DonationDatePayment"), $form);
148 $form = str_replace('__Donor__', $outputlangs->trans("Donor"), $form);
149 $form = str_replace('__Amount__', $outputlangs->trans("Amount"), $form);
150 $form = str_replace('__PaymentMode__', $outputlangs->trans("PaymentMode"), $form);
151
152 $notePublic = '';
153 if (getDolGlobalInt('DONATION_NOTE_PUBLIC') >= 1 && !empty($don->note_public)) {
154 $notePublic = '<div id="note-public"><p>'.$don->note_public.'</p></div>';
155 }
156 $form = str_replace('__NOTE_PUBLIC__', $notePublic, $form);
157
158 $donationMessage = '';
159 if (getDolGlobalString('DONATION_MESSAGE')) {
160 $donationMessage = '<div id="donation-message"><p>' . getDolGlobalString('DONATION_MESSAGE').'</p></div>';
161 }
162 $form = str_replace('__DONATION_MESAGE__', $donationMessage, $form);
163
164 return $form;
165 }
166
174 private function saveFile($path, $contents)
175 {
176 dol_syslog("html_generic::saveFile $path");
177 $handle = fopen($path, "w");
178 fwrite($handle, $contents);
179 fclose($handle);
180 dolChmod($path);
181
182 return 1;
183 }
184
185 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
194 public function write_file($don, $outputlangs, $currency = '')
195 {
196 // phpcs:enable
197 global $user, $conf, $langs, $mysoc;
198
199 $id = (!is_object($don) ? $don : '');
200
201 $outputlangs = $this->loadTranslationFiles($outputlangs);
202
203 if (!empty($conf->don->dir_output)) {
204 // Definition of the object don (for upward compatibility)
205 if (!is_object($don)) {
206 $don = new Don($this->db);
207 $ret = $don->fetch($id);
208 $id = $don->id;
209 }
210
211 // Definition of $dir and $file
212 if (!empty($don->specimen)) {
213 $dir = $conf->don->dir_output;
214 $file = $dir."/SPECIMEN.html";
215 } else {
216 $donref = dol_sanitizeFileName($don->ref);
217 $dir = $conf->don->dir_output."/".$donref;
218 $file = $dir."/".$donref.".html";
219 }
220
221 if (!file_exists($dir)) {
222 if (dol_mkdir($dir) < 0) {
223 $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
224 return -1;
225 }
226 }
227
228 if (file_exists($dir)) {
229 $this->saveFile($file, $this->getContents($don, $outputlangs, $currency));
230
231 $this->result = array('fullpath' => $file);
232
233 return 1;
234 } else {
235 $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
236 return 0;
237 }
238 } else {
239 $this->error = $langs->trans("ErrorConstantNotDefined", "DON_OUTPUTDIR");
240 return 0;
241 }
242 }
243}
$id
Definition account.php:39
Class to manage donations.
Definition don.class.php:41
Class to manage generation of HTML components Only common components must be here.
Parent class of subscription templates.
Class to generate document for a generic donations receipt.
saveFile($path, $contents)
Write the object to document file to disk.
getDonationPaymentType($don)
Write the object to document file to disk.
__construct($db)
Constructor.
getContents($don, $outputlangs, $currency)
Get the contents of the file.
write_file($don, $outputlangs, $currency='')
Write the object to document file to disk.
isEnabled()
Return if a module can be used or not.
loadTranslationFiles($outputlangs)
Load translation files.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dolChmod($filepath, $newmask='')
Change mod of a file.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:137
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:140