]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/Sugarpdf/SugarpdfHelper.php
Release 6.5.0
[Github/sugarcrm.git] / include / Sugarpdf / SugarpdfHelper.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38
39     function wrapTD($html, $options){
40         return wrapTag("td",$html, $options);
41     }
42
43     function wrapTR($html, $options){
44         return wrapTag("tr",$html, $options);
45     }
46
47     function wrapTable($html, $options){
48         return wrapTag("table",$html, $options);
49     }
50
51     function wrapB($html){
52         return "<b>".$html."</b>";
53     }
54
55     function wrapI($html){
56         return "<i>".$html."</i>";
57     }
58     function wrapTag($tag, $html, $options){
59         // Wrap the tags defined in the options array (like b, i, font... tags)
60         if(!empty($options)){
61             foreach($options as $k=>$v){
62                 if(is_array($v)){
63                     $html = wrapTag($k, "$html", $v);
64                 }
65             }
66         }
67         // wrap the HTML content with the passed tag
68         $return = "<$tag ";
69         if(!empty($options)){
70             foreach($options as $k=>$v){
71                 if(!is_array($v)){
72                     $return .= " $k=".'"'.$v.'"';
73                 }
74             }
75         }
76         return $return.">".$html."</$tag>";
77     }
78
79     /**
80      * This function prepare a string to be ready for the PDF printing.
81      * @param $string
82      * @return string
83      */
84     function prepare_string($string){
85         global $locale;
86         $string = html_entity_decode($string, ENT_QUOTES);
87         // return $locale->translateCharset($string, 'UTF-8', $locale->getExportCharset());
88         return $string;
89     }
90      /**
91      * Copy of format_number() from currency with fix for sugarpdf.
92      * @return String formatted currency value
93      * @see modules/Currencies/Currency.php
94      */
95     function format_number_sugarpdf($amount, $round = null, $decimals = null, $params = array()) {
96         global $app_strings, $current_user, $sugar_config, $locale;
97         static $current_users_currency = null;
98         static $last_override_currency = null;
99         static $override_currency_id = null;
100         static $currency;
101
102         $seps = get_number_seperators();
103         $num_grp_sep = $seps[0];
104         $dec_sep = $seps[1];
105
106         // cn: bug 8522 - sig digits not honored in pdfs
107         if(is_null($decimals)) {
108             $decimals = $locale->getPrecision();
109         }
110         if(is_null($round)) {
111             $round = $locale->getPrecision();
112         }
113
114         // only create a currency object if we need it
115         if((!empty($params['currency_symbol']) && $params['currency_symbol']) ||
116           (!empty($params['convert']) && $params['convert']) ||
117           (!empty($params['currency_id']))) {
118                 // if we have an override currency_id
119                 if(!empty($params['currency_id'])) {
120                     if($override_currency_id != $params['currency_id']) {
121                         $override_currency_id = $params['currency_id'];
122                         $currency = new Currency();
123                         $currency->retrieve($override_currency_id);
124                         $last_override_currency = $currency;
125                     } else {
126                         $currency = $last_override_currency;
127                     }
128
129                 } elseif(!isset($current_users_currency)) { // else use current user's
130                     $current_users_currency = new Currency();
131                     if($current_user->getPreference('currency')) $current_users_currency->retrieve($current_user->getPreference('currency'));
132                     else $current_users_currency->retrieve('-99'); // use default if none set
133                     $currency = $current_users_currency;
134                 }
135         }
136         if(!empty($params['convert']) && $params['convert']) {
137             $amount = $currency->convertFromDollar($amount, 6);
138         }
139
140         if(!empty($params['currency_symbol']) && $params['currency_symbol']) {
141             if(!empty($params['symbol_override'])) {
142                 $symbol = $params['symbol_override'];
143             }
144
145             // BEGIN SUGARPDF
146             /*elseif(!empty($params['type']) && $params['type'] == 'pdf') {
147                 $symbol = $currency->getPdfCurrencySymbol();
148                 $symbol_space = false;
149             }*/
150             elseif(!empty($params['type']) && $params['type'] == 'sugarpdf') {
151                 $symbol = $currency->symbol;
152                 $symbol_space = false;
153             }
154             // END SUGARPDF
155
156             else {
157                 if(empty($currency->symbol))
158                     $symbol = $currency->getDefaultCurrencySymbol();
159                 else
160                     $symbol = $currency->symbol;
161                 $symbol_space = true;
162             }
163         } else {
164             $symbol = '';
165         }
166
167         if(isset($params['charset_convert'])) {
168             $symbol = $locale->translateCharset($symbol, 'UTF-8', $locale->getExportCharset());
169         }
170
171         if(empty($params['human'])) {
172            $amount = number_format(round($amount, $round), $decimals, $dec_sep, $num_grp_sep);
173            $amount = format_place_symbol($amount, $symbol,(empty($params['symbol_space']) ? false : true));
174         } else {
175             // If amount is more greater than a thousand(positive or negative)
176             if(strpos($amount, '.') > 0) {
177                $checkAmount = strlen(substr($amount, 0, strpos($amount, '.')));
178             }
179
180             if($checkAmount >= 1000 || $checkAmount <= -1000) {
181                 $amount = round(($amount / 1000), 0);
182                 $amount = $amount . 'k';
183                 $amount = format_place_symbol($amount, $symbol,(empty($params['symbol_space']) ? false : true));
184             } else {
185                 $amount = format_place_symbol($amount, $symbol,(empty($params['symbol_space']) ? false : true));
186             }
187         }
188
189         if(!empty($params['percentage']) && $params['percentage']) $amount .= $app_strings['LBL_PERCENTAGE_SYMBOL'];
190         return $amount;
191
192     } //end function format_number
193 ?>