]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Project/Project.php
Release 6.4.0
[Github/sugarcrm.git] / modules / Project / Project.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 class Project extends SugarBean {
40         // database table columns
41         var $id;
42         var $date_entered;
43         var $date_modified;
44         var $assigned_user_id;
45         var $modified_user_id;
46         var $created_by;
47         var $name;
48         var $description;
49         var $deleted;
50
51
52         // related information
53         var $assigned_user_name;
54         var $modified_by_name;
55         var $created_by_name;
56
57         var $account_id;
58         var $contact_id;
59         var $opportunity_id;
60         var $email_id;
61     var $estimated_start_date;
62
63         // calculated information
64         var $total_estimated_effort;
65         var $total_actual_effort;
66
67         var $object_name = 'Project';
68         var $module_dir = 'Project';
69         var $new_schema = true;
70         var $table_name = 'project';
71
72         // This is used to retrieve related fields from form posts.
73         var $additional_column_fields = array(
74                 'account_id',
75                 'contact_id',
76                 'opportunity_id',
77         );
78
79         var $relationship_fields = array(
80                 'account_id' => 'accounts',
81                 'contact_id'=>'contacts',
82                 'opportunity_id'=>'opportunities',
83                 'email_id' => 'emails',
84         );
85
86         //////////////////////////////////////////////////////////////////
87         // METHODS
88         //////////////////////////////////////////////////////////////////
89
90         /**
91          *
92          */
93         function Project()
94         {
95                 parent::SugarBean();
96         }
97
98         /**
99          * overriding the base class function to do a join with users table
100          */
101
102         /**
103          *
104          */
105         function fill_in_additional_detail_fields()
106         {
107             parent::fill_in_additional_detail_fields();
108
109                 $this->assigned_user_name = get_assigned_user_name($this->assigned_user_id);
110                 //$this->total_estimated_effort = $this->_get_total_estimated_effort($this->id);
111                 //$this->total_actual_effort = $this->_get_total_actual_effort($this->id);
112         }
113
114         /**
115          *
116          */
117         function fill_in_additional_list_fields()
118         {
119             parent::fill_in_additional_list_fields();
120                 $this->assigned_user_name = get_assigned_user_name($this->assigned_user_id);
121                 //$this->total_estimated_effort = $this->_get_total_estimated_effort($this->id);
122                 //$this->total_actual_effort = $this->_get_total_actual_effort($this->id);
123         }
124
125     /**
126     * Save changes that have been made to a relationship.
127     *
128     * @param $is_update true if this save is an update.
129     */
130     function save_relationship_changes($is_update, $exclude=array())
131     {
132         parent::save_relationship_changes($is_update, $exclude);
133         $new_rel_id = false;
134         $new_rel_link = false;
135         //this allows us to dynamically relate modules without adding it to the relationship_fields array
136         if(!empty($_REQUEST['relate_id']) && !in_array($_REQUEST['relate_to'], $exclude) && $_REQUEST['relate_id'] != $this->id){
137             $new_rel_id = $_REQUEST['relate_id'];
138             $new_rel_relname = $_REQUEST['relate_to'];
139             if(!empty($this->in_workflow) && !empty($this->not_use_rel_in_req)) {
140                 $new_rel_id = $this->new_rel_id;
141                 $new_rel_relname = $this->new_rel_relname;
142             }
143             $new_rel_link = $new_rel_relname;
144             //Try to find the link in this bean based on the relationship
145             foreach ( $this->field_defs as $key => $def ) {
146                 if (isset($def['type']) && $def['type'] == 'link'
147                 && isset($def['relationship']) && $def['relationship'] == $new_rel_relname) {
148                     $new_rel_link = $key;
149                 }
150             }
151             if ($new_rel_link == 'contacts') {
152                 $accountId = $this->db->getOne('SELECT account_id FROM accounts_contacts WHERE contact_id=' . $this->db->quoted($new_rel_id));
153                 if ($accountId !== false) {
154                     if($this->load_relationship('accounts')){
155                         $this->accounts->add($accountId);
156                     }
157                 }
158             }
159         }
160     }
161         /**
162          *
163          */
164         function _get_total_estimated_effort($project_id)
165         {
166                 $return_value = '';
167
168                 $query = 'SELECT SUM('.$this->db->convert('estimated_effort', "IFNULL", 0).') total_estimated_effort';
169                 $query.= ' FROM project_task';
170                 $query.= " WHERE parent_id='{$project_id}' AND deleted=0";
171
172                 $result = $this->db->query($query,true," Error filling in additional detail fields: ");
173                 $row = $this->db->fetchByAssoc($result);
174                 if($row != null)
175                 {
176                         $return_value = $row['total_estimated_effort'];
177                 }
178
179                 return $return_value;
180         }
181
182         /**
183          *
184          */
185         function _get_total_actual_effort($project_id)
186         {
187                 $return_value = '';
188
189                 $query = 'SELECT SUM('.$this->db->convert('actual_effort', "IFNULL", 0).') total_actual_effort';
190                 $query.=  ' FROM project_task';
191                 $query.=  " WHERE parent_id='{$project_id}' AND deleted=0";
192
193                 $result = $this->db->query($query,true," Error filling in additional detail fields: ");
194                 $row = $this->db->fetchByAssoc($result);
195                 if($row != null)
196                 {
197                         $return_value = $row['total_actual_effort'];
198                 }
199
200                 return $return_value;
201         }
202
203         /**
204          *
205          */
206         function get_summary_text()
207         {
208                 return $this->name;
209         }
210
211         /**
212          *
213          */
214         function build_generic_where_clause ($the_query_string)
215         {
216                 $where_clauses = array();
217                 $the_query_string = $GLOBALS['db']->quote($the_query_string);
218                 array_push($where_clauses, "project.name LIKE '%$the_query_string%'");
219
220                 $the_where = '';
221                 foreach($where_clauses as $clause)
222                 {
223                         if($the_where != '') $the_where .= " OR ";
224                         $the_where .= $clause;
225                 }
226
227                 return $the_where;
228         }
229
230         function get_list_view_data()
231         {
232                 $field_list = $this->get_list_view_array();
233                 $field_list['USER_NAME'] = empty($this->user_name) ? '' : $this->user_name;
234                 $field_list['ASSIGNED_USER_NAME'] = $this->assigned_user_name;
235                 return $field_list;
236         }
237           function bean_implements($interface){
238                 switch($interface){
239                         case 'ACL':return true;
240                 }
241                 return false;
242         }
243
244     function create_export_query(&$order_by, &$where, $relate_link_join='')
245     {
246         $custom_join = $this->custom_fields->getJOIN(true, true,$where);
247                 if($custom_join)
248                                 $custom_join['join'] .= $relate_link_join;
249                 $query = "SELECT
250                                 project.*,
251                 users.user_name as assigned_user_name ";
252         if($custom_join){
253                         $query .=  $custom_join['select'];
254                 }
255         $query .= " FROM project ";
256
257                 if($custom_join){
258                         $query .=  $custom_join['join'];
259                 }
260         $query .= " LEFT JOIN users
261                         ON project.assigned_user_id=users.id ";
262
263         $where_auto = " project.deleted=0 ";
264
265         if($where != "")
266                 $query .= "where ($where) AND ".$where_auto;
267         else
268             $query .= "where ".$where_auto;
269
270         if(!empty($order_by)){
271                 //check to see if order by variable already has table name by looking for dot "."
272                 $table_defined_already = strpos($order_by, ".");
273
274                 if($table_defined_already === false){
275                         //table not defined yet, define accounts to avoid "ambigous column" SQL error
276                         $query .= " ORDER BY $order_by";
277                 }else{
278                         //table already defined, just add it to end of query
279                     $query .= " ORDER BY $order_by";
280                 }
281         }
282         return $query;
283     }
284         function getAllProjectTasks(){
285                 $projectTasks = array();
286
287                 $query = "SELECT * FROM project_task WHERE project_id = '" . $this->id. "' AND deleted = 0 ORDER BY project_task_id";
288                 $result = $this->db->query($query,true,"Error retrieving project tasks");
289                 $row = $this->db->fetchByAssoc($result);
290
291                 while ($row != null){
292                         $projectTaskBean = new ProjectTask();
293                         $projectTaskBean->id = $row['id'];
294                         $projectTaskBean->retrieve();
295                         array_push($projectTasks, $projectTaskBean);
296
297                         $row = $this->db->fetchByAssoc($result);
298                 }
299
300                 return $projectTasks;
301         }
302 }
303 ?>