]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Audit/Audit.php
Release 6.2.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-2011 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'] == 'field_name')
152                                                                  {
153                                                                         global $mod_strings;
154                                                                         if(isset($focus->field_defs[$row['field_name']]['vname'])) {
155                                                                                 $label = $focus->field_defs[$row['field_name']]['vname'];
156                                                                                 $temp_list[$field['name']] = translate($label, $focus->module_dir);
157                                                                         }
158                                                                 }
159                         }
160                     }
161
162                     $temp_list['created_by'] = $row['user_name'];
163                     $audit_list[] = $temp_list;
164                 }
165         }
166         return $audit_list;
167     }
168
169     function getAssociatedFieldName($fieldName, $fieldValue){
170     global $focus,  $genericAssocFieldsArray, $moduleAssocFieldsArray;
171
172         if(!empty($moduleAssocFieldsArray[$focus->object_name]) && array_key_exists($fieldName, $moduleAssocFieldsArray[$focus->object_name])){
173         $assocFieldsArray =  $moduleAssocFieldsArray[$focus->object_name];
174
175         }
176         else if(array_key_exists($fieldName, $genericAssocFieldsArray)){
177             $assocFieldsArray =  $genericAssocFieldsArray;
178         }
179         else{
180             return $fieldValue;
181         }
182         $query = "";
183         $field_arr = $assocFieldsArray[$fieldName];
184         $query = "SELECT ";
185         if(is_array($field_arr['select_field_name'])){
186                 $count = count($field_arr['select_field_name']);
187             $index = 1;
188             foreach($field_arr['select_field_name'] as $col){
189                 $query .= $col;
190                 if($index < $count){
191                         $query .= ", ";
192                 }
193                 $index++;
194             }
195          }
196          else{
197                 $query .= $field_arr['select_field_name'];
198          }
199
200          $query .= " FROM ".$field_arr['table_name']." WHERE ".$field_arr['select_field_join']." = '".$fieldValue."'";
201
202          $result = $focus->db->query($query);
203          if(!empty($result)){
204                 if($row = $focus->db->fetchByAssoc($result)){
205                 if(is_array($field_arr['select_field_name'])){
206                         $returnVal = "";
207                         foreach($field_arr['select_field_name'] as $col){
208                                 $returnVal .= $row[$col]." ";
209                         }
210                         return $returnVal;
211                 }
212                 else{
213                         return $row[$field_arr['select_field_name']];
214                 }
215             }
216         }
217     }
218 }
219 ?>