PageRenderTime 67ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/Cases/Case.php

https://github.com/jacknicole/sugarcrm_dev
PHP | 317 lines | 187 code | 59 blank | 71 comment | 21 complexity | dcf507ff89d22df6b643485b9950111d MD5 | raw file
  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. * Description: TODO: To be written.
  39. * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
  40. * All Rights Reserved.
  41. * Contributor(s): ______________________________________..
  42. ********************************************************************************/
  43. // Case is used to store customer information.
  44. class aCase extends Basic {
  45. var $field_name_map = array();
  46. // Stored fields
  47. var $id;
  48. var $date_entered;
  49. var $date_modified;
  50. var $modified_user_id;
  51. var $assigned_user_id;
  52. var $case_number;
  53. var $resolution;
  54. var $description;
  55. var $name;
  56. var $status;
  57. var $priority;
  58. var $created_by;
  59. var $created_by_name;
  60. var $modified_by_name;
  61. // These are related
  62. var $bug_id;
  63. var $account_name;
  64. var $account_id;
  65. var $contact_id;
  66. var $task_id;
  67. var $note_id;
  68. var $meeting_id;
  69. var $call_id;
  70. var $email_id;
  71. var $assigned_user_name;
  72. var $account_name1;
  73. var $table_name = "cases";
  74. var $rel_account_table = "accounts_cases";
  75. var $rel_contact_table = "contacts_cases";
  76. var $module_dir = 'Cases';
  77. var $object_name = "Case";
  78. var $importable = true;
  79. /** "%1" is the case_number, for emails
  80. * leave the %1 in if you customize this
  81. * YOU MUST LEAVE THE BRACKETS AS WELL*/
  82. var $emailSubjectMacro = '[CASE:%1]';
  83. // This is used to retrieve related fields from form posts.
  84. 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');
  85. var $relationship_fields = Array('account_id'=>'accounts', 'bug_id' => 'bugs',
  86. 'task_id'=>'tasks', 'note_id'=>'notes',
  87. 'meeting_id'=>'meetings', 'call_id'=>'calls', 'email_id'=>'emails',
  88. );
  89. function aCase() {
  90. parent::SugarBean();
  91. global $sugar_config;
  92. if(!$sugar_config['require_accounts']){
  93. unset($this->required_fields['account_name']);
  94. }
  95. $this->setupCustomFields('Cases');
  96. foreach ($this->field_defs as $field) {
  97. $this->field_name_map[$field['name']] = $field;
  98. }
  99. }
  100. var $new_schema = true;
  101. function get_summary_text()
  102. {
  103. return "$this->name";
  104. }
  105. function listviewACLHelper(){
  106. $array_assign = parent::listviewACLHelper();
  107. $is_owner = false;
  108. if(!empty($this->account_id)){
  109. if(!empty($this->account_id_owner)){
  110. global $current_user;
  111. $is_owner = $current_user->id == $this->account_id_owner;
  112. }
  113. }
  114. if(!ACLController::moduleSupportsACL('Accounts') || ACLController::checkAccess('Accounts', 'view', $is_owner)){
  115. $array_assign['ACCOUNT'] = 'a';
  116. }else{
  117. $array_assign['ACCOUNT'] = 'span';
  118. }
  119. return $array_assign;
  120. }
  121. function save_relationship_changes($is_update)
  122. {
  123. parent::save_relationship_changes($is_update);
  124. if (!empty($this->contact_id)) {
  125. $this->set_case_contact_relationship($this->contact_id);
  126. }
  127. }
  128. function set_case_contact_relationship($contact_id)
  129. {
  130. global $app_list_strings;
  131. $default = $app_list_strings['case_relationship_type_default_key'];
  132. $this->load_relationship('contacts');
  133. $this->contacts->add($contact_id,array('contact_role'=>$default));
  134. }
  135. function fill_in_additional_list_fields()
  136. {
  137. parent::fill_in_additional_list_fields();
  138. /*// Fill in the assigned_user_name
  139. //$this->assigned_user_name = get_assigned_user_name($this->assigned_user_id);
  140. $account_info = $this->getAccount($this->id);
  141. $this->account_name = $account_info['account_name'];
  142. $this->account_id = $account_info['account_id'];*/
  143. }
  144. function fill_in_additional_detail_fields()
  145. {
  146. parent::fill_in_additional_detail_fields();
  147. // Fill in the assigned_user_name
  148. $this->assigned_user_name = get_assigned_user_name($this->assigned_user_id);
  149. $this->created_by_name = get_assigned_user_name($this->created_by);
  150. $this->modified_by_name = get_assigned_user_name($this->modified_user_id);
  151. if(!empty($this->id)) {
  152. $account_info = $this->getAccount($this->id);
  153. if(!empty($account_info)) {
  154. $this->account_name = $account_info['account_name'];
  155. $this->account_id = $account_info['account_id'];
  156. }
  157. }
  158. }
  159. /** Returns a list of the associated contacts
  160. * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
  161. * All Rights Reserved..
  162. * Contributor(s): ______________________________________..
  163. */
  164. function get_contacts()
  165. {
  166. $this->load_relationship('contacts');
  167. $query_array=$this->contacts->getQuery(true);
  168. //update the select clause in the retruned query.
  169. $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 ";
  170. $query='';
  171. foreach ($query_array as $qstring) {
  172. $query.=' '.$qstring;
  173. }
  174. $temp = Array('id', 'first_name', 'last_name', 'title', 'email1', 'phone_work', 'case_role', 'case_rel_id');
  175. return $this->build_related_list2($query, new Contact(), $temp);
  176. }
  177. function get_list_view_data(){
  178. global $current_language;
  179. $app_list_strings = return_app_list_strings_language($current_language);
  180. $temp_array = $this->get_list_view_array();
  181. $temp_array['NAME'] = (($this->name == "") ? "<em>blank</em>" : $this->name);
  182. $temp_array['PRIORITY'] = empty($this->priority)? "" : $app_list_strings['case_priority_dom'][$this->priority];
  183. $temp_array['STATUS'] = empty($this->status)? "" : $app_list_strings['case_status_dom'][$this->status];
  184. $temp_array['ENCODED_NAME'] = $this->name;
  185. $temp_array['CASE_NUMBER'] = $this->case_number;
  186. $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'")."</a>";
  187. //$temp_array['ACCOUNT_NAME'] = $this->account_name; //overwrites the account_name value returned from the cases table.
  188. return $temp_array;
  189. }
  190. /**
  191. builds a generic search based on the query string using or
  192. do not include any $this-> because this is called on without having the class instantiated
  193. */
  194. function build_generic_where_clause ($the_query_string) {
  195. $where_clauses = Array();
  196. $the_query_string = $this->db->quote($the_query_string);
  197. array_push($where_clauses, "cases.name like '$the_query_string%'");
  198. array_push($where_clauses, "accounts.name like '$the_query_string%'");
  199. if (is_numeric($the_query_string)) array_push($where_clauses, "cases.case_number like '$the_query_string%'");
  200. $the_where = "";
  201. foreach($where_clauses as $clause)
  202. {
  203. if($the_where != "") $the_where .= " or ";
  204. $the_where .= $clause;
  205. }
  206. if($the_where != ""){
  207. $the_where = "(".$the_where.")";
  208. }
  209. return $the_where;
  210. }
  211. function set_notification_body($xtpl, $case)
  212. {
  213. global $app_list_strings;
  214. $xtpl->assign("CASE_SUBJECT", $case->name);
  215. $xtpl->assign("CASE_PRIORITY", (isset($case->priority) ? $app_list_strings['case_priority_dom'][$case->priority]:""));
  216. $xtpl->assign("CASE_STATUS", (isset($case->status) ? $app_list_strings['case_status_dom'][$case->status]:""));
  217. $xtpl->assign("CASE_DESCRIPTION", $case->description);
  218. return $xtpl;
  219. }
  220. function bean_implements($interface){
  221. switch($interface){
  222. case 'ACL':return true;
  223. }
  224. return false;
  225. }
  226. function save($check_notify = FALSE){
  227. return parent::save($check_notify);
  228. }
  229. /**
  230. * retrieves the Subject line macro for InboundEmail parsing
  231. * @return string
  232. */
  233. function getEmailSubjectMacro() {
  234. global $sugar_config;
  235. return (isset($sugar_config['inbound_email_case_subject_macro']) && !empty($sugar_config['inbound_email_case_subject_macro'])) ?
  236. $sugar_config['inbound_email_case_subject_macro'] : $this->emailSubjectMacro;
  237. }
  238. function getAccount($case_id){
  239. if(empty($case_id)) return array();
  240. $ret_array = array();
  241. $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";
  242. $result = $this->db->query($query,true," Error filling in additional detail fields: ");
  243. // Get the id and the name.
  244. $row = $this->db->fetchByAssoc($result);
  245. if($row != null){
  246. $ret_array['account_name'] = stripslashes($row['name']);
  247. $ret_array['account_id'] = $row['id'];
  248. }
  249. else{
  250. $ret_array['account_name'] = '';
  251. $ret_array['account_id'] = '';
  252. }
  253. return $ret_array;
  254. }
  255. }
  256. ?>