]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Audit/Audit.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Audit / Audit.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
40  * Description:  TODO: To be written.
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42  * All Rights Reserved.
43  * Contributor(s): ______________________________________..
44  ********************************************************************************/
45
46
47 require_once('modules/Audit/field_assoc.php');
48         
49 class Audit extends SugarBean {
50         var $module_dir = "Audit";
51         var $object_name = "Audit";
52
53
54         // This is used to retrieve related fields from form posts.
55         var $additional_column_fields = Array();
56
57         function Audit() {
58                 parent::SugarBean();
59         }
60
61         var $new_schema = true;
62
63         function get_summary_text()
64         {
65                 return $this->name;
66         }
67
68     function create_export_query(&$order_by, &$where)
69     {
70     }
71
72         function fill_in_additional_list_fields()
73         {
74         }
75
76         function fill_in_additional_detail_fields()
77         {
78         }
79
80         function fill_in_additional_parent_fields()
81         {
82         }
83
84         function get_list_view_data()
85     {
86         }
87
88     function get_audit_link()
89     {
90
91     }
92
93    function get_audit_list()
94     {
95
96         global $focus, $genericAssocFieldsArray, $moduleAssocFieldsArray, $current_user, $timedate, $app_strings;   
97         $audit_list = array();
98         if(!empty($_REQUEST['record'])) {
99                         $result = $focus->retrieve($_REQUEST['record']);
100
101         if($result == null || !$focus->ACLAccess('', $focus->isOwner($current_user->id)))
102                 {
103                         sugar_die($app_strings['ERROR_NO_RECORD']);
104                 }
105                 }
106
107         if($focus->is_AuditEnabled()){
108             $order= ' order by '.$focus->get_audit_table_name().'.date_created desc' ;//order by contacts_audit.date_created desc
109             $query = "SELECT ".$focus->get_audit_table_name().".*, users.user_name FROM ".$focus->get_audit_table_name().", users WHERE ".$focus->get_audit_table_name().".created_by = users.id AND ".$focus->get_audit_table_name().".parent_id = '$focus->id'".$order;
110
111                     $result = $focus->db->query($query);
112                 // We have some data.
113                 require('metadata/audit_templateMetaData.php');
114                 $fieldDefs = $dictionary['audit']['fields'];
115                             while (($row = $focus->db->fetchByAssoc($result))!= null) {
116                     $temp_list = array();
117
118                     foreach($fieldDefs as $field){
119                                                 if(isset($row[$field['name']])) {
120                                 if(($field['name'] == 'before_value_string' || $field['name'] == 'after_value_string') &&
121                                         (array_key_exists($row['field_name'], $genericAssocFieldsArray) || (!empty($moduleAssocFieldsArray[$focus->object_name]) && array_key_exists($row['field_name'], $moduleAssocFieldsArray[$focus->object_name])) )
122                                    ) {
123
124                                    $temp_list[$field['name']] = Audit::getAssociatedFieldName($row['field_name'], $row[$field['name']]);
125                                 } else{
126                                    $temp_list[$field['name']] = $row[$field['name']];
127                                 }
128                                 
129                                 if ($field['name'] == 'date_created') {
130                                         $temp_list[$field['name']]=$timedate->to_display_date_time($temp_list[$field['name']]);
131                                 }
132                                                                  if(($field['name'] == 'before_value_string' || $field['name'] == 'after_value_string') && ($row['data_type'] == "enum" || $row['data_type'] == "multienum"))
133                                                                  {
134                                                                         global $app_list_strings;
135                                     $enum_keys = unencodeMultienum($temp_list[$field['name']]);
136                                     $enum_values = array();
137                                     foreach($enum_keys as $enum_key) {
138                                                                         if(isset($focus->field_defs[$row['field_name']]['options'])) {
139                                                                                 $domain = $focus->field_defs[$row['field_name']]['options'];
140                                             if(isset($app_list_strings[$domain][$enum_key]))
141                                                 $enum_values[] = $app_list_strings[$domain][$enum_key];
142                                                                         }
143                                     }
144                                     if(!empty($enum_values)){
145                                         $temp_list[$field['name']] = implode(', ', $enum_values);
146                                     }
147                                                                         if($temp_list['data_type']==='date'){
148                                                                                 $temp_list[$field['name']]=$timedate->to_display_date($temp_list[$field['name']], false);
149                                                                         }
150                                                                  }
151                                                                  elseif(($field['name'] == 'before_value_string' || $field['name'] == 'after_value_string') && ($row['data_type'] == "datetimecombo")) {
152                                                                         if (!empty($temp_list[$field['name']]) && $temp_list[$field['name']] != 'NULL') {
153                                                                             $temp_list[$field['name']]=$timedate->to_display_date_time($temp_list[$field['name']]);
154                                                                         } else {
155                                                                                 $temp_list[$field['name']] = '';
156                                                                         }
157                                                                  }
158                                                                  elseif($field['name'] == 'field_name')
159                                                                  {
160                                                                         global $mod_strings;
161                                                                         if(isset($focus->field_defs[$row['field_name']]['vname'])) {
162                                                                                 $label = $focus->field_defs[$row['field_name']]['vname'];
163                                                                                 $temp_list[$field['name']] = translate($label, $focus->module_dir);
164                                                                         }
165                                                                 }
166                         }
167                     }
168
169                     $temp_list['created_by'] = $row['user_name'];
170                     $audit_list[] = $temp_list;
171                 }
172         }
173         return $audit_list;
174     }
175
176     function getAssociatedFieldName($fieldName, $fieldValue){
177     global $focus,  $genericAssocFieldsArray, $moduleAssocFieldsArray;
178
179         if(!empty($moduleAssocFieldsArray[$focus->object_name]) && array_key_exists($fieldName, $moduleAssocFieldsArray[$focus->object_name])){
180         $assocFieldsArray =  $moduleAssocFieldsArray[$focus->object_name];
181
182         }
183         else if(array_key_exists($fieldName, $genericAssocFieldsArray)){
184             $assocFieldsArray =  $genericAssocFieldsArray;
185         }
186         else{
187             return $fieldValue;
188         }
189         $query = "";
190         $field_arr = $assocFieldsArray[$fieldName];
191         $query = "SELECT ";
192         if(is_array($field_arr['select_field_name'])){
193                 $count = count($field_arr['select_field_name']);
194             $index = 1;
195             foreach($field_arr['select_field_name'] as $col){
196                 $query .= $col;
197                 if($index < $count){
198                         $query .= ", ";
199                 }
200                 $index++;
201             }
202          }
203          else{
204                 $query .= $field_arr['select_field_name'];
205          }
206
207          $query .= " FROM ".$field_arr['table_name']." WHERE ".$field_arr['select_field_join']." = '".$fieldValue."'";
208
209          $result = $focus->db->query($query);
210          if(!empty($result)){
211                 if($row = $focus->db->fetchByAssoc($result)){
212                 if(is_array($field_arr['select_field_name'])){
213                         $returnVal = "";
214                         foreach($field_arr['select_field_name'] as $col){
215                                 $returnVal .= $row[$col]." ";
216                         }
217                         return $returnVal;
218                 }
219                 else{
220                         return $row[$field_arr['select_field_name']];
221                 }
222             }
223         }
224     }
225 }
226 ?>