dolibarr 19.0.3
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 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
29require_once DOL_DOCUMENT_ROOT.'/core/modules/dons/modules_don.php';
30require_once DOL_DOCUMENT_ROOT.'/don/class/don.class.php';
31require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
32
33
38{
44 public function __construct($db)
45 {
46 global $conf, $langs;
47
48 $this->db = $db;
49 $this->name = "generic";
50 $this->description = $langs->trans('DonationsReceiptModel').'';
51 $this->option_multilang = 1;
52
53 $this->type = 'html';
54 }
55
61 public function isEnabled()
62 {
63 return true;
64 }
65
72 private function loadTranslationFiles($outputlangs)
73 {
74 if (!is_object($outputlangs)) {
75 global $langs;
76 $outputlangs = $langs;
77 }
78
79 $outputlangs->loadLangs(array("main", "dict", "companies", "bills", "products", "donations"));
80
81 return $outputlangs;
82 }
83
90 private function getDonationPaymentType($don)
91 {
92 $formclass = new Form($this->db);
93
94 // This is not the proper way to do it but $formclass->form_modes_reglement
95 // prints the translation instead of returning it
96 $formclass->load_cache_types_paiements();
97
98 if ($don->mode_reglement_id) {
99 $paymentmode = $formclass->cache_types_paiements[$don->mode_reglement_id]['label'];
100 } else {
101 $paymentmode = '';
102 }
103
104 return $paymentmode;
105 }
106
115 private function getContents($don, $outputlangs, $currency)
116 {
117 global $user, $conf, $langs, $mysoc;
118
119 $now = dol_now();
120
121 $currency = !empty($currency) ? $currency : $conf->currency;
122
123 $donmodel = DOL_DOCUMENT_ROOT."/core/modules/dons/html_generic.html";
124 $form = implode('', file($donmodel));
125 $form = str_replace('__NOW__', dol_print_date($now, 'day', false, $outputlangs), $form);
126 $form = str_replace('__REF__', $don->id, $form);
127 $form = str_replace('__DATE__', dol_print_date($don->date, 'day', false, $outputlangs), $form);
128
129 $form = str_replace('__BENEFICIARY_NAME__', $mysoc->name, $form);
130 $form = str_replace('__BENEFICIARY_FULL_ADDRESS__', $mysoc->getFullAddress(1, "<br>", 1), $form);
131
132 $form = str_replace('__PAYMENTMODE_LABEL__', $this->getDonationPaymentType($don), $form);
133 $form = str_replace('__AMOUNT__', price($don->amount), $form);
134 $form = str_replace('__CURRENCY_CODE__', $conf->currency, $form);
135 if (isModEnabled("societe") && getDolGlobalString('DONATION_USE_THIRDPARTIES') && $don->socid > 0 && $don->thirdparty) {
136 $form = str_replace('__DONOR_FULL_NAME__', $don->thirdparty->name, $form);
137 $form = str_replace('__DONOR_FULL_ADDRESS__', $don->thirdparty->getFullAddress(1, ", ", 1), $form);
138 } else {
139 $form = str_replace('__DONOR_FULL_NAME__', $don->getFullName($langs), $form);
140 $form = str_replace('__DONOR_FULL_ADDRESS__', $don->getFullAddress(1, " ", 1), $form);
141 }
142
143 $form = str_replace('__DonationTitle__', $outputlangs->trans("DonationTitle"), $form);
144 $form = str_replace('__DonationRef__', $outputlangs->trans("DonationRef"), $form);
145 $form = str_replace('__Date__', $outputlangs->trans("Date"), $form);
146 $form = str_replace('__DonationDatePayment__', $outputlangs->trans("DonationDatePayment"), $form);
147 $form = str_replace('__Donor__', $outputlangs->trans("Donor"), $form);
148 $form = str_replace('__Amount__', $outputlangs->trans("Amount"), $form);
149 $form = str_replace('__PaymentMode__', $outputlangs->trans("PaymentMode"), $form);
150
151 $notePublic = '';
152 if (getDolGlobalInt('DONATION_NOTE_PUBLIC') >= 1 && !empty($don->note_public)) {
153 $notePublic = '<div id="note-public"><p>'.$don->note_public.'</p></div>';
154 }
155 $form = str_replace('__NOTE_PUBLIC__', $notePublic, $form);
156
157 $donationMessage = '';
158 if (getDolGlobalString('DONATION_MESSAGE')) {
159 $donationMessage = '<div id="donation-message"><p>' . getDolGlobalString('DONATION_MESSAGE').'</p></div>';
160 }
161 $form = str_replace('__DONATION_MESAGE__', $donationMessage, $form);
162
163 return $form;
164 }
165
173 private function saveFile($path, $contents)
174 {
175 dol_syslog("html_generic::saveFile $path");
176 $handle = fopen($path, "w");
177 fwrite($handle, $contents);
178 fclose($handle);
179 dolChmod($path);
180 }
181
182 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
191 public function write_file($don, $outputlangs, $currency = '')
192 {
193 // phpcs:enable
194 global $user, $conf, $langs, $mysoc;
195
196 $id = (!is_object($don) ? $don : '');
197
198 $outputlangs = $this->loadTranslationFiles($outputlangs);
199
200 if (!empty($conf->don->dir_output)) {
201 // Definition of the object don (for upward compatibility)
202 if (!is_object($don)) {
203 $don = new Don($this->db);
204 $ret = $don->fetch($id);
205 $id = $don->id;
206 }
207
208 // Definition of $dir and $file
209 if (!empty($don->specimen)) {
210 $dir = $conf->don->dir_output;
211 $file = $dir."/SPECIMEN.html";
212 } else {
213 $donref = dol_sanitizeFileName($don->ref);
214 $dir = $conf->don->dir_output."/".$donref;
215 $file = $dir."/".$donref.".html";
216 }
217
218 if (!file_exists($dir)) {
219 if (dol_mkdir($dir) < 0) {
220 $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
221 return -1;
222 }
223 }
224
225 if (file_exists($dir)) {
226 $this->saveFile($file, $this->getContents($don, $outputlangs, $currency));
227
228 $this->result = array('fullpath'=>$file);
229
230 return 1;
231 } else {
232 $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
233 return 0;
234 }
235 } else {
236 $this->error = $langs->trans("ErrorConstantNotDefined", "DON_OUTPUTDIR");
237 return 0;
238 }
239 }
240}
Class to manage donations.
Definition don.class.php:40
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.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
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_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
getDolGlobalString($key, $default='')
Return 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:121
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:124