]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Cases/Case.php
Release 6.4.0
[Github/sugarcrm.git] / modules / Cases / Case.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
48
49
50
51
52
53
54
55
56
57
58 // Case is used to store customer information.
59 class aCase extends Basic {
60         var $field_name_map = array();
61         // Stored fields
62         var $id;
63         var $date_entered;
64         var $date_modified;
65         var $modified_user_id;
66         var $assigned_user_id;
67         var $case_number;
68         var $resolution;
69         var $description;
70         var $name;
71         var $status;
72         var $priority;
73
74
75         var $created_by;
76         var $created_by_name;
77         var $modified_by_name;
78
79         // These are related
80         var $bug_id;
81         var $account_name;
82         var $account_id;
83         var $contact_id;
84         var $task_id;
85         var $note_id;
86         var $meeting_id;
87         var $call_id;
88         var $email_id;
89         var $assigned_user_name;
90         var $account_name1;
91
92         var $table_name = "cases";
93         var $rel_account_table = "accounts_cases";
94         var $rel_contact_table = "contacts_cases";
95         var $module_dir = 'Cases';
96         var $object_name = "Case";
97         var $importable = true;
98         /** "%1" is the case_number, for emails
99          * leave the %1 in if you customize this
100          * YOU MUST LEAVE THE BRACKETS AS WELL*/
101         var $emailSubjectMacro = '[CASE:%1]';
102
103         // This is used to retrieve related fields from form posts.
104         var $additional_column_fields = Array('bug_id', 'assigned_user_name', 'assigned_user_id', 'contact_id', 'task_id', 'note_id', 'meeting_id', 'call_id', 'email_id');
105
106         var $relationship_fields = Array('account_id'=>'accounts', 'bug_id' => 'bugs',
107                                                                         'task_id'=>'tasks', 'note_id'=>'notes',
108                                                                         'meeting_id'=>'meetings', 'call_id'=>'calls', 'email_id'=>'emails',
109                                                                         );
110
111         function aCase() {
112                 parent::SugarBean();
113                 global $sugar_config;
114                 if(!$sugar_config['require_accounts']){
115                         unset($this->required_fields['account_name']);
116                 }
117
118                 $this->setupCustomFields('Cases');
119                 foreach ($this->field_defs as $field) {
120                  $this->field_name_map[$field['name']] = $field;
121         }
122         }
123
124         var $new_schema = true;
125
126
127
128
129
130         function get_summary_text()
131         {
132                 return "$this->name";
133         }
134
135         function listviewACLHelper(){
136                 $array_assign = parent::listviewACLHelper();
137                 $is_owner = false;
138                 if(!empty($this->account_id)){
139
140                         if(!empty($this->account_id_owner)){
141                                 global $current_user;
142                                 $is_owner = $current_user->id == $this->account_id_owner;
143                         }
144                 }
145                         if(!ACLController::moduleSupportsACL('Accounts') || ACLController::checkAccess('Accounts', 'view', $is_owner)){
146                                 $array_assign['ACCOUNT'] = 'a';
147                         }else{
148                                 $array_assign['ACCOUNT'] = 'span';
149                         }
150
151                 return $array_assign;
152         }
153
154         function save_relationship_changes($is_update)
155         {
156                 parent::save_relationship_changes($is_update);
157
158                 if (!empty($this->contact_id)) {
159                         $this->set_case_contact_relationship($this->contact_id);
160                 }
161         }
162
163         function set_case_contact_relationship($contact_id)
164         {
165                 global $app_list_strings;
166                 $default = $app_list_strings['case_relationship_type_default_key'];
167                 $this->load_relationship('contacts');
168                 $this->contacts->add($contact_id,array('contact_role'=>$default));
169         }
170
171         function fill_in_additional_list_fields()
172         {
173                 parent::fill_in_additional_list_fields();
174                 /*// Fill in the assigned_user_name
175                 //$this->assigned_user_name = get_assigned_user_name($this->assigned_user_id);
176
177                 $account_info = $this->getAccount($this->id);
178                 $this->account_name = $account_info['account_name'];
179                 $this->account_id = $account_info['account_id'];*/
180         }
181
182         function fill_in_additional_detail_fields()
183         {
184                 parent::fill_in_additional_detail_fields();
185                 // Fill in the assigned_user_name
186                 $this->assigned_user_name = get_assigned_user_name($this->assigned_user_id);
187
188                 $this->created_by_name = get_assigned_user_name($this->created_by);
189                 $this->modified_by_name = get_assigned_user_name($this->modified_user_id);
190
191         if(!empty($this->id)) {
192                     $account_info = $this->getAccount($this->id);
193             if(!empty($account_info)) {
194                 $this->account_name = $account_info['account_name'];
195                 $this->account_id = $account_info['account_id'];
196             }
197         }
198         }
199
200
201         /** Returns a list of the associated contacts
202          * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
203          * All Rights Reserved..
204          * Contributor(s): ______________________________________..
205         */
206         function get_contacts()
207         {
208                 $this->load_relationship('contacts');
209                 $query_array=$this->contacts->getQuery(true);
210
211                 //update the select clause in the retruned query.
212                 $query_array['select']="SELECT contacts.id, contacts.first_name, contacts.last_name, contacts.title, contacts.email1, contacts.phone_work, contacts_cases.contact_role as case_role, contacts_cases.id as case_rel_id ";
213
214                 $query='';
215                 foreach ($query_array as $qstring) {
216                         $query.=' '.$qstring;
217                 }
218             $temp = Array('id', 'first_name', 'last_name', 'title', 'email1', 'phone_work', 'case_role', 'case_rel_id');
219                 return $this->build_related_list2($query, new Contact(), $temp);
220         }
221
222         function get_list_view_data(){
223                 global $current_language;
224                 $app_list_strings = return_app_list_strings_language($current_language);
225
226                 $temp_array = $this->get_list_view_array();
227                 $temp_array['NAME'] = (($this->name == "") ? "<em>blank</em>" : $this->name);
228                 $temp_array['PRIORITY'] = empty($this->priority)? "" : $app_list_strings['case_priority_dom'][$this->priority];
229                 $temp_array['STATUS'] = empty($this->status)? "" : $app_list_strings['case_status_dom'][$this->status];
230                 $temp_array['ENCODED_NAME'] = $this->name;
231                 $temp_array['CASE_NUMBER'] = $this->case_number;
232                 $temp_array['SET_COMPLETE'] =  "<a href='index.php?return_module=Home&return_action=index&action=EditView&module=Cases&record=$this->id&status=Closed'>".SugarThemeRegistry::current()->getImage("close_inline","title=".translate('LBL_LIST_CLOSE','Cases')." border='0'",null,null,'.gif',translate('LBL_LIST_CLOSE','Cases'))."</a>";
233                 //$temp_array['ACCOUNT_NAME'] = $this->account_name; //overwrites the account_name value returned from the cases table.
234                 return $temp_array;
235         }
236
237         /**
238                 builds a generic search based on the query string using or
239                 do not include any $this-> because this is called on without having the class instantiated
240         */
241         function build_generic_where_clause ($the_query_string) {
242         $where_clauses = Array();
243         $the_query_string = $this->db->quote($the_query_string);
244         array_push($where_clauses, "cases.name like '$the_query_string%'");
245         array_push($where_clauses, "accounts.name like '$the_query_string%'");
246
247         if (is_numeric($the_query_string)) array_push($where_clauses, "cases.case_number like '$the_query_string%'");
248
249         $the_where = "";
250
251         foreach($where_clauses as $clause)
252         {
253                 if($the_where != "") $the_where .= " or ";
254                 $the_where .= $clause;
255         }
256
257         if($the_where != ""){
258                 $the_where = "(".$the_where.")";
259         }
260
261         return $the_where;
262         }
263
264         function set_notification_body($xtpl, $case)
265         {
266                 global $app_list_strings;
267
268                 $xtpl->assign("CASE_SUBJECT", $case->name);
269                 $xtpl->assign("CASE_PRIORITY", (isset($case->priority) ? $app_list_strings['case_priority_dom'][$case->priority]:""));
270                 $xtpl->assign("CASE_STATUS", (isset($case->status) ? $app_list_strings['case_status_dom'][$case->status]:""));
271                 $xtpl->assign("CASE_DESCRIPTION", $case->description);
272
273                 return $xtpl;
274         }
275
276                 function bean_implements($interface){
277                 switch($interface){
278                         case 'ACL':return true;
279                 }
280                 return false;
281         }
282
283         function save($check_notify = FALSE){
284                 return parent::save($check_notify);
285         }
286
287         /**
288          * retrieves the Subject line macro for InboundEmail parsing
289          * @return string
290          */
291         function getEmailSubjectMacro() {
292                 global $sugar_config;
293                 return (isset($sugar_config['inbound_email_case_subject_macro']) && !empty($sugar_config['inbound_email_case_subject_macro'])) ?
294                         $sugar_config['inbound_email_case_subject_macro'] : $this->emailSubjectMacro;
295         }
296
297         function getAccount($case_id){
298                 if(empty($case_id)) return array();
299             $ret_array = array();
300                 $query = "SELECT acc.id, acc.name from accounts  acc, cases  where acc.id = cases.account_id and cases.id = '" . $case_id . "' and cases.deleted=0 and acc.deleted=0";
301                 $result = $this->db->query($query,true," Error filling in additional detail fields: ");
302
303                 // Get the id and the name.
304                 $row = $this->db->fetchByAssoc($result);
305
306                 if($row != null){
307                         $ret_array['account_name'] = stripslashes($row['name']);
308                         $ret_array['account_id']        = $row['id'];
309                 }
310                 else{
311                         $ret_array['account_name'] = '';
312                         $ret_array['account_id']        = '';
313                 }
314                 return $ret_array;
315         }
316 }
317 ?>