Changed license type to GPLv3 in top of files
[fa-stable.git] / includes / references.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 include_once($path_to_root . "/includes/db/references_db.inc");
13
14 class references 
15 {
16         
17         function save($type, $id, $reference) 
18         {
19                 add_reference($type, $id, $reference);
20                 
21                 references::save_last($reference, $type);
22         }
23         
24         function get($type, $id) 
25         {
26                 return get_reference($type, $id);
27         }       
28         
29         function delete($type, $id) 
30         {
31                 delete_reference($type, $id);
32         }       
33         
34         function exists($type, $reference) 
35         {
36                 return (find_reference($type, $reference) != null);
37         }
38         
39         function save_last($reference, $type) 
40         {
41                 $next = references::increment($reference);
42                 save_next_reference($type, $next);
43         }
44         
45         function get_next($type) 
46         {
47                 return get_next_reference($type);
48         }
49         
50         //------------------------------------
51
52         function is_valid($reference) 
53         {
54                 return strlen(trim($reference)) > 0;
55         }
56         
57         function increment($reference) 
58         {
59         if (is_numeric($reference))
60                 return $reference + 1;
61         else 
62                 return $reference;
63         }
64         
65         //------------------------------------
66 }
67
68 //----------------------------------------------------------------------------
69
70 function is_new_reference($ref, $type)
71 {
72         $db_info = get_systype_db_info($type);
73         $db_name = $db_info[0];
74         $db_type = $db_info[1];
75         $db_ref = $db_info[3];
76         
77         if ($db_ref != null) 
78         {
79                 $sql = "SELECT $db_ref FROM $db_name WHERE $db_ref='$ref'";
80                 if ($db_type != null)
81                         $sql .= " AND $db_type=$type";
82                          
83                 $result = db_query($sql, "could not test for unique reference");
84                 
85                 return (db_num_rows($result) == 0);
86         }
87         
88         // it's a type that doesn't use references - shouldn't be calling here, but say yes anyways
89         return true;
90 }
91
92 ?>