Merged changes from stable branch up to 2.3.12
[fa-stable.git] / includes / hooks.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 //
13 // FrontAccounting extension modules integration.
14 // This file is included in session.inc even before session is started,
15 // and includes hooks.php connector files from all installed extensions.
16 // To make hooks active install_hooks() have to be called after interface
17 // language is set.
18 //
19 // To find how various hooks are processed look into respective hook_* functions below.
20 //
21 class hooks {
22         var $module_name; // extension module name.
23
24         // 
25         // Helper for updating databases with extension scheme
26         //
27         // $comp can be company number, -1 for all, 
28         // $updates - table of filename => array(table, field, property)
29         // $check_only - don't update database, check table/field/property existence only
30         //
31         function update_databases($comp, $updates, $check_only=false)
32         {
33                 global $db_connections, $path_to_root;
34         
35                 if ($comp == -1) 
36                         $conn = $db_connections;
37                 else
38                         $conn = array( $comp => $db_connections[$comp]);
39                 $result = true;
40
41                 foreach($conn as $comp => $con) {
42                         set_global_connection($comp);
43                         foreach($updates as $file => $update) {
44                                 $table = @$update[0];
45                                 $field = @$update[1];
46                                 $properties = @$update[2];
47
48                                 $ok = check_table($con['tbpref'], $table, $field, $properties) == 0;
49
50                                 if (!$check_only && !$ok) {
51                                         $ok = db_import($path_to_root.'/modules/'.$this->module_name.'/sql/'.$file,
52                                                 $con);
53                                 }
54                                 $result &= $ok;
55                                 if (!$result)
56                                         break;
57                         }
58                         db_close();
59                         if (!$result)
60                                 break;
61                 }
62                 set_global_connection(0); // return to siteadmin account
63
64                 return $result;
65         }
66         //
67         //      Install additional tabs provided by extension
68         //
69         function install_tabs($app)
70         {
71 //              $app->add_application(new example_class); // add menu tab defined by example_class
72         }
73         //
74         //      Install additonal menu options provided by extension
75         //
76         function install_options($app)
77         {
78 //              global $path_to_root;
79 //
80 //              switch($app->id) {
81 //                      case 'orders':
82 //                              $app->add_rapp_function( 0, _("&Example option"), 
83 //                                      $path_to_root.'/modules/example/example.php?', 'SA_OPEN');
84 //              }
85         }
86         
87         function install_access()
88         {
89 //              $security_areas['SA_EXAMPLE'] = array(SS_EXAMPLE|100, _("Example security area."));
90 //
91 //              $security_sections = array(SS_EXAMPLE => _("Example module implementation"));
92 //
93 //              return array($security_areas, $security_sections);
94         }
95
96         //
97         // Price in words. $doc_type is set to document type and can be used to suppress 
98         // price in words printing for selected document types.
99         // Used instead of built in simple english price_in_words() function.
100         //
101         //      Returns: amount in words as string.
102
103         function price_in_words($amount, $doc_type)
104         {
105         }
106
107         //
108         // Exchange rate currency $curr as on date $date.
109         // Keep in mind FA has internally implemented 3 exrate providers
110         // If any of them supports your currency, you can simply use function below
111         // with apprioprate provider set, otherwise implement your own.
112         // Returns: $curr value in home currency units as a real number.
113
114         function retrieve_exrate($curr, $date)
115         {
116 //              $provider = 'ECB'; // 'ECB', 'YAHOO' or 'GOOGLE'
117 //              return get_extern_rate($curr, $provider, $date);
118                 return null;
119         }
120
121         // External authentication
122         // If used should return true after successfull athentication, false otherwise.
123         function authenticate($login, $password)
124         {
125                 return null;
126         }
127         // Generic function called at the end of Tax Report (report 709)
128         // Can be used e.g. for special database updates on every report printing
129         // or to print special tax report footer 
130         //
131         // Returns: nothing
132         function tax_report_done()
133         {
134         }
135         // Following database transaction hooks akcepts array of parameters:
136         // 'cart' => transaction data
137         // 'trans_type' => transaction type
138
139         function db_prewrite(&$cart, $trans_type)
140         {
141                 return true;
142         }
143
144         function db_postwrite(&$cart, $trans_type)
145         {
146                 return true;
147         }
148
149         function db_prevoid($trans_type, $trans_no)
150         {
151                 return true;
152         }
153         /*
154                 This method is called after module install.
155         */
156         function install_extension($check_only=true)
157         {
158                 return true;
159         }
160         /*
161                 This method is called after module uninstall.
162         */
163         function uninstall_extension($check_only=true)
164         {
165                 return true;
166         }
167         /*
168                 This method is called on extension activation for company.
169         */
170         function activate_extension($company, $check_only=true)
171         {
172                 return true;
173         }
174         /*
175                 This method is called when extension is deactivated for company.
176         */
177         function deactivate_extension($company, $check_only=true)
178         {
179                 return true;
180         }
181 }
182
183 /*
184         Installs hooks provided by extension modules
185 */
186 function install_hooks()
187 {
188         global $path_to_root, $Hooks, $installed_extensions;
189
190         $Hooks = array();
191         
192         // include current language related $Hooks object if locale file exists
193         if (file_exists($path_to_root . "/lang/".$_SESSION['language']->code."/locale.inc"))
194         {
195                 include_once($path_to_root . "/lang/".$_SESSION['language']->code."/locale.inc");
196                 $code = $_SESSION['language']->code;
197                 $hook_class = 'hooks_'.$code;
198                 $Hooks[$code] = new $hook_class;
199                 unset($code, $hook_class);
200         }
201         // install hooks provided by active extensions
202         foreach($installed_extensions as $ext) {
203                         $hook_class = 'hooks_'.$ext['package'];
204                         if ($ext['active'] && class_exists($hook_class)) {
205                                 $Hooks[$ext['package']] = new $hook_class;
206                         }
207         }
208 }
209 /*
210         Non active hooks are not included in $Hooks array, so we can use special function to 
211         activate.
212 */
213 function activate_hooks($ext, $comp)
214 {
215         global $Hooks;
216
217         $hooks = @$Hooks[$ext];
218         if (!$hooks) {
219                 $hookclass = 'hooks_'.$ext;
220                 if (class_exists($hookclass))
221                         $hooks = new $hookclass;
222                 else
223                         return true; // extension does not have hooks file
224         }
225         if (!$hooks)
226                 return false;
227         else
228                 return $hooks->activate_extension($comp, false);
229 }
230 /*
231         Calls hook $method defined in extension $ext (if any)
232 */
233 function hook_invoke($ext, $method, &$data, $opts=null)
234 {
235
236         global $Hooks;
237
238         $ret = null;
239         if (isset($Hooks[$ext]) && method_exists($Hooks[$ext], $method)) {
240                 set_ext_domain('modules/'.$ext);
241                 $ret = $Hooks[$ext]->$method($data, $opts);
242                 set_ext_domain();
243         } 
244         return $ret;
245 }
246
247 /*
248         Calls hook $methods defined in all extensions (if any)
249 */
250 function hook_invoke_all($method, &$data, $opts=null)
251 {
252
253         global $Hooks;
254         
255         $return = array();
256         foreach($Hooks as $ext => $hook)
257                 if (method_exists($hook, $method)) {
258                         set_ext_domain('modules/'.$ext);
259                         $result = $hook->$method($data, $opts);
260                         if (isset($result) && is_array($result)) {
261                                 $return = array_merge_recursive($return, $result);
262                         } else if (isset($result)) {
263                                 $return[] = $result;
264                                 }
265                         set_ext_domain();
266                 }
267         return $return;
268 }
269 /*
270         Returns first non-null result returned from hook.
271 */
272 function hook_invoke_first($method, &$data, $opts=null)
273 {
274
275         global $Hooks;
276
277         $result = null;
278         foreach($Hooks as $ext => $hook) {
279                 if (method_exists($hook, $method)) {
280                         set_ext_domain('modules/'.$ext);
281                         $result = $hook->$method($data, $opts);
282                         set_ext_domain();
283                         if (isset($result))
284                                 break;
285                 }
286         }
287         return $result;
288 }
289 /*
290         Returns last non-null result returned from modules method. Helps implement hooks overriding by 
291         extensions installed later.
292 */
293 function hook_invoke_last($method, &$data, $opts=null)
294 {
295
296         global $Hooks;
297
298         $result = null;
299         $Reverse = array_reverse($Hooks);
300         foreach($Reverse as $ext => $hook) {
301                 if (method_exists($hook, $method)) {
302                         set_ext_domain('modules/'.$ext);
303                         $result = $hook->$method($data, $opts);
304                         set_ext_domain();
305                         if (isset($result))
306                                 break;
307                 }
308         }
309         return $result;
310 }
311 //------------------------------------------------------------------------------------------
312 //      Database transaction hooks.
313 //      $type - type of transaction (simplifies cart processing)
314 //      $cart - transaction cart
315 //      $args is optional array of parameters
316 //
317 // For FA 2.3 prewrite, postwrite and prevoid hooks are implemented for following transaction types:
318 //
319 // ST_BANKPAYMENT, ST_BANKDEPOSIT, ST_BANKTRANSFER,
320 // ST_SALESORDER, ST_SALESQUOTE, ST_SALESINVOICE, ST_CUSTCREDIT, ST_CUSTPAYMENT, ST_CUSTDELIVERY,
321 // ST_LOCTRANSFER, ST_INVADJUST, 
322 // ST_PURCHORDER, ST_SUPPINVOICE, ST_SUPPCREDIT, ST_SUPPAYMENT, ST_SUPPRECEIVE,
323 // ST_WORKORDER, ST_MANUISSUE, ST_MANURECEIVE, 
324
325 /*
326         Invoked after transaction has been read from database to cart.
327         Not implemented yet.
328 */
329 //function hook_db_postread(&$cart, $type)
330 //{
331 //      hook_invoke_all('db_postread', $cart, $type);
332 //}
333
334 /*
335         Invoked before transaction is written to database.
336 */
337 function hook_db_prewrite(&$cart, $type)
338 {
339         return hook_invoke_all('db_prewrite', $cart, $type);
340 }
341
342 /*
343         Invoked after transaction has been written to database.
344 */
345 function hook_db_postwrite(&$cart, $type)
346 {
347         return hook_invoke_all('db_postwrite', $cart, $type);
348 }
349 /*
350         Invoked before transaction is voided
351 */
352 function hook_db_prevoid($type, $type_no)
353 {
354         return hook_invoke_all('db_prevoid', $type, $type_no);
355 }
356
357 //-------------------------------------------------------------------------------------------
358 //
359 //      Various hooks
360 //
361 //      Alternative exchange rates feeds.
362 //
363 function hook_retrieve_exrate($currency, $date)
364 {
365         return hook_invoke_last('retrieve_exrate', $currency, $date);
366 }
367 //
368 // Generic function called at the end of Tax Report (report 709)
369 //
370 function hook_tax_report_done()
371 {
372         return hook_invoke_all('tax_report_done', $dummy);
373 }
374 //
375 //      Amount in words displayed on various documents (especially sales invoice)
376 //
377 function hook_price_in_words($amount, $document)
378 {
379         return hook_invoke_last('price_in_words', $amount, $document);
380 }
381 //
382 //      Session handling hook. This is special case of hook class which have to be run before session is started.
383 //      If fa_session_manager class is defined in any installed extension, this class provides session handling
384 //      for application, otherwise standard php session handling is used.
385 //
386 function hook_session_start($company)
387 {
388         if (class_exists('fa_session_manager')) {
389                 global $SessionManager;
390                 $SessionManager = new fa_session_manager($company);
391                 return $SessionManager->installed;
392         }
393         return false;
394 }
395 //
396 //      Third party authentication modules.
397 //      Returns true after successfull authentication, false otherwise, null if no login hook is defined.
398 //
399 function hook_authenticate($login, $password)
400 {
401         return hook_invoke_last('authenticate', $login, $password);
402 }