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