3 * FusionForge project manager
5 * Copyright 1999-2000, Tim Perdue/Sourceforge
6 * Copyright 2002, Tim Perdue/GForge, LLC
8 * This file is part of FusionForge. FusionForge is free software;
9 * you can redistribute it and/or modify it under the terms of the
10 * GNU General Public License as published by the Free Software
11 * Foundation; either version 2 of the Licence, or (at your option)
14 * FusionForge is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 require_once $gfcommon.'pm/ProjectTaskFactory.class.php';
26 function printr($var, $name='$var', $echo=true) {
29 //$str = highlight_string("<?php\n$name = ".var_export($var, 1).";\n? >\n", 1);
30 // $str=var_export($var, 1);
33 $var =var_export($var, 1);
37 $fp=fopen('/tmp/msp.log','a');
38 fwrite($fp,"\n-------".date('Y-m-d H:i:s')."-----".$name."-----\n".$var);
44 function printrcomplete() {
45 // exec("/bin/cat /tmp/msp.log | mail -s\"printr\" tim@gforgegroup.com");
46 // exec("/bin/rm -f /tmp/msp.log");
49 function &pm_import_tasks($group_project_id,&$tasks) {
53 printr($tasks,'MSPCheckin::in-array');
54 printr(getenv('TZ'),'MSPCheckin::entry TZ');
56 $pg =& projectgroup_get_object($group_project_id);
57 if (!$pg || !is_object($pg)) {
58 $array['success']=false;
59 $array['errormessage']='Could Not Get ProjectGroup';
60 } elseif ($pg->isError()) {
61 $array['success']=false;
62 $array['errormessage']='Could Not Get ProjectGroup: '.$pg->getErrorMessage();
65 //printr($count,'count - count of tasks');
67 // Build hash list of technicians so we can get their ID for assigning tasks
69 $engine = RBACEngine::getInstance () ;
70 $techs = $engine->getUsersByAllowedAction ('pm', $pg->getID(), 'tech') ;
71 $tcount=count($techs);
72 for ($i=0; $i<$tcount; $i++) {
73 $tarr[strtolower($techs[$i]->getUnixName())]=$techs[$i]->getID();
74 $tarr[strtolower($techs[$i]->getRealName())]=$techs[$i]->getID();
76 $invalid_names=array();
78 // Create a linked list based on the msproj_id
80 for ($i=0; $i<$count; $i++) {
81 $msprojid[$tasks[$i]['msproj_id']] =& $tasks[$i];
82 $resrc = $tasks[$i]['resources'];
83 for ($j=0; $j<count($resrc); $j++) {
84 //validate user - see if they really exist as techs in this subproject
85 if (!util_ifsetor($tarr[strtolower($resrc[$j]['user_name'])])) {
86 //create list of bogus names to send back
87 if (array_search(strtolower($resrc[$j]['user_name']),$invalid_names) === false) {
88 $invalid_names[]=$resrc[$j]['user_name'];
95 // invalid assignees - send it back for remapping
97 if (count($invalid_names)) {
98 $array['success']=false;
99 $array['errormessage']='Invalid Resource Name:';
100 foreach ($invalid_names as $i) {
101 $array['errormessage'] .= ' ' . $i;
103 $array['resourcename']=$invalid_names;
104 for ($i=0; $i<$tcount; $i++) {
105 $array['usernames'][$techs[$i]->getID()]=$techs[$i]->getUnixName();
110 // Begin inserting/updating the tasks
112 for ($i=0; $i<$count; $i++) {
116 //no task_id so it must be new - create it
117 if (!$tasks[$i]['id']) {
118 if (!$tasks[$i]['notes']) {
119 $tasks[$i]['notes']='None Provided';
122 $pt = new ProjectTask($pg);
123 if (!$pt || !is_object($pt)) {
124 $array['success']=false;
126 $array['errormessage']='Could Not Get ProjectTask';
127 } elseif ($pt->isError()) {
128 $array['success']=false;
130 $array['errormessage']='Could Not Get ProjectTask: '.$pt->getErrorMessage();
132 //remap priority names=>numbers
133 $priority=$tasks[$i]['priority'];
134 if (!$priority || $priority < 1 || $priority > 5) {
135 // printr($priority,'Invalid Priority On New Task');
140 $resrc = $tasks[$i]['resources'];
141 for ($ucount=0; $ucount< count($resrc); $ucount++) {
142 //get their user_id from the $tarr we created earlier
143 $assignees[]=$tarr[strtolower($resrc[$ucount]['user_name'])];
145 //don't do anything with dependencies yet - we may only have
146 //the MSprojid from dependent items
147 $hours = $tasks[$i]['work'];
151 $percent_complete= intval($tasks[$i]['percent_complete']);
152 if (!$percent_complete) {
154 } elseif ($percent_complete > 100) {
155 $percent_complete=100;
158 // Convert category name to category_id if given.
159 if (isset($tasks[$i]['category'])) {
160 if ($tasks[$i]['category'] == 'None') {
163 $res = db_query_params('SELECT category_id FROM project_category WHERE group_project_id=$1 AND category_name=$2',
164 array($group_project_id, $tasks[$i]['category']));
165 $category_id = db_result($res, 0, 'category_id');
168 $array['errormessage']='Error No category named : '.$tasks[$i]['category'];
173 $category_id = $pt->getCategoryID();
177 addslashes($tasks[$i]['name']),
178 addslashes($tasks[$i]['notes']),
181 strtotime($tasks[$i]['start_date']),
182 strtotime($tasks[$i]['end_date']),
187 $tasks[$i]['duration'],
188 $tasks[$i]['parent_id'])) {
189 $array['success']=false;
191 $array['errormessage']='Error Creating ProjectTask: '.$pt->getErrorMessage();
196 $tasks[$i]['id'] = $pt->getID();
197 $tasks[$i]['obj'] = $pt;
198 $pt->setExternalID($tasks[$i]['msproj_id']);
204 //update existing task
206 $pt = projecttask_get_object($tasks[$i]['id']);
207 if (!$pt || !is_object($pt)) {
208 printr($tasks[$i]['id'],'Could not get task');
209 // $array['success']=false;
211 // $array['errormessage']='Could Not Get ProjectTask';
212 } elseif ($pt->isError()) {
213 printr($tasks[$i]['id'],'Could not get task - error in task');
214 $array['success']=false;
216 $array['errormessage']='Could Not Get ProjectTask: '.$pt->getErrorMessage();
218 //remap priority names=>numbers
219 $priority=$tasks[$i]['priority'];
220 if (!$priority || $priority < 1 || $priority > 5) {
221 printr($priority,'Invalid Priority On Existing Task');
226 $resrc = $tasks[$i]['resources'];
227 for ($ucount=0; $ucount<count($resrc); $ucount++) {
228 //get their user_id from the $tarr we created earlier
229 $assignees[]=$tarr[strtolower($resrc[$ucount]['user_name'])];
231 //don't do anything with dependencies yet - we may only have the
232 //MSprojid from dependent items
233 $hours = $tasks[$i]['work'];
237 $percent_complete= intval($tasks[$i]['percent_complete']);
238 if (!$percent_complete) {
240 } elseif ($percent_complete > 100) {
241 $percent_complete=100;
244 // Convert category name to category_id if given.
245 if (isset($tasks[$i]['category'])) {
246 if ($tasks[$i]['category'] == 'None') {
249 $res = db_query_params('SELECT category_id FROM project_category WHERE group_project_id=$1 AND category_name=$2',
250 array($group_project_id, $tasks[$i]['category']));
251 $category_id = db_result($res, 0, 'category_id');
254 $array['errormessage']='Error No category named : '.$tasks[$i]['category'];
259 $category_id = $pt->getCategoryID();
263 addslashes($tasks[$i]['name']),
264 addslashes($tasks[$i]['notes']),
267 strtotime($tasks[$i]['start_date']),
268 strtotime($tasks[$i]['end_date']),
273 $pt->getDependentOn(),
275 $tasks[$i]['duration'],
276 $tasks[$i]['parent_id'])) {
277 $array['success']=false;
279 $array['errormessage']='Error Updating ProjectTask: '.$pt->getErrorMessage();
285 $tasks[$i]['id'] = $pt->getID();
286 $tasks[$i]['obj'] = $pt;
287 $pt->setExternalID($tasks[$i]['msproj_id']);
291 } //if task->iserror()
293 //accumulate list of completed tasks
294 //any task not in this list will be deleted
295 $completed[$tasks[$i]['id']]=true;
300 // Do task dependencies
303 printr($was_error,'Right before deps');
306 for ($i=0; $i<$count; $i++) {
307 $darr=$tasks[$i]['dependenton'];
310 //iterate each dependency in a task
311 for ($dcount=0; $dcount<count($darr); $dcount++) {
312 //get the id of the task we're dependent on -
313 // may have to get it from msprojid linked list
314 $id=$darr[$dcount]['task_id'];
316 $id=$msprojid[$darr[$dcount]['msproj_id']]['id'];
318 //prevent task from being dependent on itself
319 if ($id == $tasks[$i]['id']) {
322 $deps[$id]=$darr[$dcount]['link_type'];
324 printr($deps,'Deps for task id: '.$tasks[$i]['id']);
325 if (isset($tasks[$i]['obj']) && is_object($tasks[$i]['obj'])) {
326 printr($deps,'11 Done Setting deps for task id: '.$tasks[$i]['id']);
327 if (!$tasks[$i]['obj']->setDependentOn($deps)) {
329 $array['success']=false;
330 printr($tasks[$i]['obj'],'FAILED TO SET DEPENDENCIES: '.$tasks[$i]['obj']->getErrorMessage());
332 printr($deps,'22 Done Setting deps for task id: '.$tasks[$i]['id']);
335 // $array['success']=false;
336 printr($foo,'PROJECT TASK OBJECT DOES NOT EXIST IN OBJ ARRAY');
338 printr($deps,'Done Setting deps for task id: '.$tasks[$i]['id']);
340 } //iterates tasks to do dependencies
345 // Delete unreferenced tasks
347 printr($was_error,'Right before deleting unreferenced tasks');
349 $ptf = new ProjectTaskFactory($pg);
350 $pt_arr=& $ptf->getTasks();
351 for ($i=0; $i<count($pt_arr); $i++) {
352 if (is_object($pt_arr[$i])) {
353 if (!util_ifsetor($completed[$pt_arr[$i]->getID()])) {
354 printr($pt_arr[$i]->getID(),'Deleting task');
355 if (!$pt_arr[$i]->delete(true)) {
356 echo $pt_arr[$i]->getErrorMessage();
358 printr($foo,'Deleting Unreferenced Tasks');
368 $array['success']=true;
371 // printr($array,'MSPCheckin::return-array');
372 printr(getenv('TZ'),'MSPCheckin::exit TZ');
378 // c-file-style: "bsd"