]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Employees/Employee.php
Release 6.5.3
[Github/sugarcrm.git] / modules / Employees / Employee.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('include/SugarObjects/templates/person/Person.php');
48
49 // Employee is used to store customer information.
50 class Employee extends Person {
51         // Stored fields
52         var $name = '';
53         var $id;
54         var $is_admin;
55         var $first_name;
56         var $last_name;
57         var $full_name;
58         var $user_name;
59         var $title;
60         var $description;
61         var $department;
62         var $reports_to_id;
63         var $reports_to_name;
64         var $phone_home;
65         var $phone_mobile;
66         var $phone_work;
67         var $phone_other;
68         var $phone_fax;
69         var $email1;
70         var $email2;
71         var $address_street;
72         var $address_city;
73         var $address_state;
74         var $address_postalcode;
75         var $address_country;
76         var $date_entered;
77         var $date_modified;
78         var $modified_user_id;
79         var $created_by;
80         var $created_by_name;
81         var $modified_by_name;
82         var $status;
83         var $messenger_id;
84         var $messenger_type;
85         var $employee_status;
86         var $error_string;
87
88         var $module_dir = "Employees";
89
90
91         var $table_name = "users";
92
93         var $object_name = "Employee";
94         var $user_preferences;
95
96         var $encodeFields = Array("first_name", "last_name", "description");
97
98         // This is used to retrieve related fields from form posts.
99         var $additional_column_fields = Array('reports_to_name');
100
101
102
103         var $new_schema = true;
104
105         function Employee() {
106                 parent::Person();
107                 $this->setupCustomFields('Users');
108                 $this->emailAddress = new SugarEmailAddress();
109         }
110
111
112         function get_summary_text() {
113         $this->_create_proper_name_field();
114         return $this->name;
115     }
116
117
118         function fill_in_additional_list_fields() {
119                 $this->fill_in_additional_detail_fields();
120         }
121
122         function fill_in_additional_detail_fields()
123         {
124                 global $locale;
125                 $query = "SELECT u1.first_name, u1.last_name from users u1, users u2 where u1.id = u2.reports_to_id AND u2.id = '$this->id' and u1.deleted=0";
126                 $result =$this->db->query($query, true, "Error filling in additional detail fields") ;
127
128                 $row = $this->db->fetchByAssoc($result);
129
130                 if($row != null)
131                 {
132                         $this->reports_to_name = stripslashes($locale->getLocaleFormattedName($row['first_name'], $row['last_name']));
133                 }
134                 else
135                 {
136                         $this->reports_to_name = '';
137                 }
138         }
139
140         function retrieve_employee_id($employee_name)
141         {
142                 $query = "SELECT id from users where user_name='$user_name' AND deleted=0";
143                 $result  = $this->db->query($query, false,"Error retrieving employee ID: ");
144                 $row = $this->db->fetchByAssoc($result);
145                 return $row['id'];
146         }
147
148         /**
149          * @return -- returns a list of all employees in the system.
150          * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
151          * All Rights Reserved..
152          * Contributor(s): ______________________________________..
153          */
154         function verify_data()
155         {
156                 //none of the checks from the users module are valid here since the user_name and
157                 //is_admin_on fields are not editable.
158                 return TRUE;
159         }
160
161         function get_list_view_data(){
162
163         global $current_user;
164                 $this->_create_proper_name_field(); // create proper NAME (by combining first + last)
165                 $user_fields = $this->get_list_view_array();
166                 // Copy over the reports_to_name
167                 if ( isset($GLOBALS['app_list_strings']['messenger_type_dom'][$this->messenger_type]) )
168             $user_fields['MESSENGER_TYPE'] = $GLOBALS['app_list_strings']['messenger_type_dom'][$this->messenger_type];
169                 if ( isset($GLOBALS['app_list_strings']['employee_status_dom'][$this->employee_status]) )
170             $user_fields['EMPLOYEE_STATUS'] = $GLOBALS['app_list_strings']['employee_status_dom'][$this->employee_status];
171                 $user_fields['REPORTS_TO_NAME'] = $this->reports_to_name;
172                 $user_fields['NAME'] = empty($this->name) ? '' : $this->name;
173                 $user_fields['EMAIL1'] = $this->emailAddress->getPrimaryAddress($this,$this->id,'Users');
174                 $this->email1 = $user_fields['EMAIL1'];
175         $user_fields['EMAIL1_LINK'] = $current_user->getEmailLink('email1', $this, '', '', 'ListView');
176                 return $user_fields;
177         }
178
179         function list_view_parse_additional_sections(&$list_form, $xTemplateSection){
180                 return $list_form;
181         }
182
183
184         function create_export_query($order_by, $where) {
185                 include('modules/Employees/field_arrays.php');
186
187                 $cols = '';
188                 foreach($fields_array['Employee']['export_fields'] as $field) {
189                         $cols .= (empty($cols)) ? '' : ', ';
190                         $cols .= $field;
191                 }
192
193                 $query = "SELECT {$cols} FROM users ";
194
195                 $where_auto = " users.deleted = 0";
196
197                 if($where != "")
198                         $query .= " WHERE $where AND " . $where_auto;
199                 else
200                         $query .= " WHERE " . $where_auto;
201
202                 if($order_by != "")
203                         $query .= " ORDER BY $order_by";
204                 else
205                         $query .= " ORDER BY users.user_name";
206
207                 return $query;
208         }
209
210         //use parent class
211         /**
212          * Generate the name field from the first_name and last_name fields.
213          */
214         /*
215         function _create_proper_name_field() {
216         global $locale;
217         $full_name = $locale->getLocaleFormattedName($this->first_name, $this->last_name);
218         $this->name = $full_name;
219         $this->full_name = $full_name;
220         }
221         */
222
223         function preprocess_fields_on_save(){
224                 parent::preprocess_fields_on_save();
225
226         }
227
228
229     /**
230      * create_new_list_query
231      *
232      * Return the list query used by the list views and export button. Next generation of create_new_list_query function.
233      *
234      * We overrode this function in the Employees module to add the additional filter check so that we do not retrieve portal users for the Employees list view queries
235      *
236      * @param string $order_by custom order by clause
237      * @param string $where custom where clause
238      * @param array $filter Optioanal
239      * @param array $params Optional     *
240      * @param int $show_deleted Optional, default 0, show deleted records is set to 1.
241      * @param string $join_type
242      * @param boolean $return_array Optional, default false, response as array
243      * @param object $parentbean creating a subquery for this bean.
244      * @param boolean $singleSelect Optional, default false.
245      * @return String select query string, optionally an array value will be returned if $return_array= true.
246      */
247     function create_new_list_query($order_by, $where, $filter=array(), $params=array(), $show_deleted=0, $join_type='', $return_array=false, $parentbean=null, $singleSelect=false)
248     {
249         //create the filter for portal only users, as they should not be showing up in query results
250         if(empty($where)){
251             $where = ' users.portal_only = 0 ';
252         }else{
253             $where .= ' and users.portal_only = 0 ';
254         }
255
256         //return parent method, specifying for array to be returned
257         return parent::create_new_list_query($order_by, $where, $filter,$params, $show_deleted, $join_type, $return_array, $parentbean, $singleSelect);
258     }
259
260     /*
261      * Overwrite Sugar bean which returns the current objects custom fields.  Lets return User custom fields instead
262      */
263     function hasCustomFields()
264     {
265
266         //Check to see if there are custom user fields that we should report on, first check the custom_fields array
267         $userCustomfields = !empty($GLOBALS['dictionary']['Employee']['custom_fields']);
268         if(!$userCustomfields){
269             //custom Fields not set, so traverse employee fields to see if any custom fields exist
270             foreach ($GLOBALS['dictionary']['Employee']['fields'] as $k=>$v){
271                 if(!empty($v['source']) && $v['source'] == 'custom_fields'){
272                     //custom field has been found, set flag to true and break
273                     $userCustomfields = true;
274                     break;
275                 }
276
277             }
278         }
279
280         //return result of search for custom fields
281         return $userCustomfields;
282     }
283 }
284
285 ?>