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.
10 * FusionForge is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License,
13 * or (at your option) any later version.
15 * FusionForge is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with FusionForge; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 require_once $gfcommon.'pm/ProjectTaskFactory.class.php';
28 function printr($var, $name='$var', $echo=true) {
31 //$str = highlight_string("<?php\n$name = ".var_export($var, 1).";\n? >\n", 1);
32 // $str=var_export($var, 1);
35 $var =var_export($var, 1);
39 $fp=fopen('/tmp/msp.log','a');
40 fwrite($fp,"\n-------".date('Y-m-d H:i:s')."-----".$name."-----\n".$var);
46 function printrcomplete() {
47 // exec("/bin/cat /tmp/msp.log | mail -s\"printr\" tim@gforgegroup.com");
48 // exec("/bin/rm -f /tmp/msp.log");
51 function &pm_import_tasks($group_project_id,&$tasks) {
52 printr($tasks,'MSPCheckin::in-array');
53 printr(getenv('TZ'),'MSPCheckin::entry TZ');
55 $pg =& projectgroup_get_object($group_project_id);
56 if (!$pg || !is_object($pg)) {
57 $array['success']=false;
58 $array['errormessage']='Could Not Get ProjectGroup';
59 } elseif ($pg->isError()) {
60 $array['success']=false;
61 $array['errormessage']='Could Not Get ProjectGroup: '.$pg->getErrorMessage();
64 //printr($count,'count - count of tasks');
66 // Build hash list of technicians so we can get their ID for assigning tasks
68 $engine = RBACEngine::getInstance () ;
69 $techs = $engine->getUsersByAllowedAction ('pm', $pg->getID(), 'tech') ;
70 $tcount=count($techs);
71 for ($i=0; $i<$tcount; $i++) {
72 $tarr[strtolower($techs[$i]->getUnixName())]=$techs[$i]->getID();
73 $tarr[strtolower($techs[$i]->getRealName())]=$techs[$i]->getID();
75 $invalid_names=array();
77 // Create a linked list based on the msproj_id
79 for ($i=0; $i<$count; $i++) {
80 $msprojid[$tasks[$i]['msproj_id']] =& $tasks[$i];
81 $resrc = $tasks[$i]['resources'];
82 for ($j=0; $j<count($resrc); $j++) {
83 //validate user - see if they really exist as techs in this subproject
84 if (!$tarr[strtolower($resrc[$j]['user_name'])]) {
85 //create list of bogus names to send back
86 if (array_search(strtolower($resrc[$j]['user_name']),$invalid_names) === false) {
87 $invalid_names[]=$resrc[$j]['user_name'];
94 // invalid assignees - send it back for remapping
96 if (count($invalid_names)) {
97 $array['success']=false;
98 $array['errormessage']='Invalid Resource Name';
99 $array['resourcename']=$invalid_names;
100 for ($i=0; $i<$tcount; $i++) {
101 $array['usernames'][$techs[$i]->getID()]=$techs[$i]->getUnixName();
106 // Begin inserting/updating the tasks
108 for ($i=0; $i<$count; $i++) {
112 //no task_id so it must be new - create it
113 if (!$tasks[$i]['id']) {
114 if (!$tasks[$i]['notes']) {
115 $tasks[$i]['notes']='None Provided';
118 $pt = new ProjectTask($pg);
119 if (!$pt || !is_object($pt)) {
120 $array['success']=false;
122 $array['errormessage']='Could Not Get ProjectTask';
123 } elseif ($pt->isError()) {
124 $array['success']=false;
126 $array['errormessage']='Could Not Get ProjectTask: '.$pt->getErrorMessage();
128 //remap priority names=>numbers
129 $priority=$tasks[$i]['priority'];
130 if (!$priority || $priority < 1 || $priority > 5) {
131 // printr($priority,'Invalid Priority On New Task');
136 $resrc = $tasks[$i]['resources'];
137 for ($ucount=0; $ucount< count($resrc); $ucount++) {
138 //get their user_id from the $tarr we created earlier
139 $assignees[]=$tarr[strtolower($resrc[$ucount]['user_name'])];
141 //don't do anything with dependencies yet - we may only have
142 //the MSprojid from dependent items
143 $hours = $tasks[$i]['work'];
147 $percent_complete= intval($tasks[$i]['percent_complete']);
148 if (!$percent_complete) {
150 } elseif ($percent_complete > 100) {
151 $percent_complete=100;
154 // Convert category name to category_id if given.
155 if (isset($tasks[$i]['category'])) {
156 if ($tasks[$i]['category'] == 'None') {
159 $res = db_query_params('SELECT category_id FROM project_category WHERE group_project_id=$1 AND category_name=$2',
160 array($group_project_id, $tasks[$i]['category']));
161 $category_id = db_result($res, 0, 'category_id');
164 $array['errormessage']='Error No category named : '.$tasks[$i]['category'];
169 $category_id = $pt->getCategoryID();
173 addslashes($tasks[$i]['name']),
174 addslashes($tasks[$i]['notes']),
177 strtotime($tasks[$i]['start_date']),
178 strtotime($tasks[$i]['end_date']),
183 $tasks[$i]['duration'],
184 $tasks[$i]['parent_id'])) {
185 $array['success']=false;
187 $array['errormessage']='Error Creating ProjectTask: '.$pt->getErrorMessage();
192 $tasks[$i]['id'] = $pt->getID();
193 $tasks[$i]['obj'] = $pt;
194 $pt->setExternalID($tasks[$i]['msproj_id']);
200 //update existing task
202 $pt = &projecttask_get_object($tasks[$i]['id']);
203 if (!$pt || !is_object($pt)) {
204 printr($tasks[$i]['id'],'Could not get task');
205 // $array['success']=false;
207 // $array['errormessage']='Could Not Get ProjectTask';
208 } elseif ($pt->isError()) {
209 printr($tasks[$i]['id'],'Could not get task - error in task');
210 $array['success']=false;
212 $array['errormessage']='Could Not Get ProjectTask: '.$pt->getErrorMessage();
214 //remap priority names=>numbers
215 $priority=$tasks[$i]['priority'];
216 if (!$priority || $priority < 1 || $priority > 5) {
217 printr($priority,'Invalid Priority On Existing Task');
222 $resrc = $tasks[$i]['resources'];
223 for ($ucount=0; $ucount<count($resrc); $ucount++) {
224 //get their user_id from the $tarr we created earlier
225 $assignees[]=$tarr[strtolower($resrc[$ucount]['user_name'])];
227 //don't do anything with dependencies yet - we may only have the
228 //MSprojid from dependent items
229 $hours = $tasks[$i]['work'];
233 $percent_complete= intval($tasks[$i]['percent_complete']);
234 if (!$percent_complete) {
236 } elseif ($percent_complete > 100) {
237 $percent_complete=100;
240 // Convert category name to category_id if given.
241 if (isset($tasks[$i]['category'])) {
242 if ($tasks[$i]['category'] == 'None') {
245 $res = db_query_params('SELECT category_id FROM project_category WHERE group_project_id=$1 AND category_name=$2',
246 array($group_project_id, $tasks[$i]['category']));
247 $category_id = db_result($res, 0, 'category_id');
250 $array['errormessage']='Error No category named : '.$tasks[$i]['category'];
255 $category_id = $pt->getCategoryID();
259 addslashes($tasks[$i]['name']),
260 addslashes($tasks[$i]['notes']),
263 strtotime($tasks[$i]['start_date']),
264 strtotime($tasks[$i]['end_date']),
269 $pt->getDependentOn(),
271 $tasks[$i]['duration'],
272 $tasks[$i]['parent_id'])) {
273 $array['success']=false;
275 $array['errormessage']='Error Updating ProjectTask: '.$pt->getErrorMessage();
281 $tasks[$i]['id'] = $pt->getID();
282 $tasks[$i]['obj'] = $pt;
283 $pt->setExternalID($tasks[$i]['msproj_id']);
287 } //if task->iserror()
289 //accumulate list of completed tasks
290 //any task not in this list will be deleted
291 $completed[$tasks[$i]['id']]=true;
296 // Do task dependencies
299 printr($was_error,'Right before deps');
302 for ($i=0; $i<$count; $i++) {
303 $darr=$tasks[$i]['dependenton'];
306 //iterate each dependency in a task
307 for ($dcount=0; $dcount<count($darr); $dcount++) {
308 //get the id of the task we're dependent on -
309 // may have to get it from msprojid linked list
310 $id=$darr[$dcount]['task_id'];
312 $id=$msprojid[$darr[$dcount]['msproj_id']]['id'];
314 //prevent task from being dependent on itself
315 if ($id == $tasks[$i]['id']) {
318 $deps[$id]=$darr[$dcount]['link_type'];
320 printr($deps,'Deps for task id: '.$tasks[$i]['id']);
321 if (is_object($tasks[$i]['obj'])) {
322 printr($deps,'11 Done Setting deps for task id: '.$tasks[$i]['id']);
323 if (!$tasks[$i]['obj']->setDependentOn($deps)) {
325 $array['success']=false;
326 printr($tasks[$i]['obj'],'FAILED TO SET DEPENDENCIES: '.$tasks[$i]['obj']->getErrorMessage());
328 printr($deps,'22 Done Setting deps for task id: '.$tasks[$i]['id']);
331 // $array['success']=false;
332 printr($foo,'PROJECT TASK OBJECT DOES NOT EXIST IN OBJ ARRAY');
334 printr($deps,'Done Setting deps for task id: '.$tasks[$i]['id']);
336 } //iterates tasks to do dependencies
341 // Delete unreferenced tasks
343 printr($was_error,'Right before deleting unreferenced tasks');
345 $ptf =& new ProjectTaskFactory($pg);
346 $pt_arr=& $ptf->getTasks();
347 for ($i=0; $i<count($pt_arr); $i++) {
348 if (is_object($pt_arr[$i])) {
349 if (!$completed[$pt_arr[$i]->getID()]) {
350 printr($pt_arr[$i]->getID(),'Deleting task');
351 if (!$pt_arr[$i]->delete(true)) {
352 echo $pt_arr[$i]->getErrorMessage();
354 printr($foo,'Deleting Unreferenced Tasks');
364 $array['success']=true;
367 // printr($array,'MSPCheckin::return-array');
368 printr(getenv('TZ'),'MSPCheckin::exit TZ');
374 // c-file-style: "bsd"