3 * FusionForge project manager
5 * Copyright 1999-2000, Tim Perdue/Sourceforge
6 * Copyright 2002, Tim Perdue/GForge, LLC
7 * Copyright 2016, Franck Villaume - TrivialDev
9 * This file is part of FusionForge. FusionForge is free software;
10 * you can redistribute it and/or modify it under the terms of the
11 * GNU General Public License as published by the Free Software
12 * Foundation; either version 2 of the Licence, or (at your option)
15 * FusionForge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 require_once $gfcommon.'pm/ProjectTaskFactory.class.php';
27 function &pm_import_tasks($group_project_id, &$tasks, $replace=true) {
30 $pg = projectgroup_get_object($group_project_id);
31 if (!$pg || !is_object($pg)) {
32 $array['success']=false;
33 $array['errormessage']=_('Could Not Get ProjectGroup');
34 } elseif ($pg->isError()) {
35 $array['success']=false;
36 $array['errormessage']=_('Could Not Get ProjectGroup')._(': ').$pg->getErrorMessage();
40 // Build hash list of technicians so we can get their ID for assigning tasks
42 $engine = RBACEngine::getInstance () ;
43 $techs = $engine->getUsersByAllowedAction ('pm', $pg->getID(), 'tech') ;
44 $tcount=count($techs);
45 for ($i=0; $i<$tcount; $i++) {
46 $tarr[strtolower($techs[$i]->getUnixName())]=$techs[$i]->getID();
47 $tarr[strtolower($techs[$i]->getRealName())]=$techs[$i]->getID();
49 $invalid_names=array();
51 // Create a linked list based on the msproj_id
53 for ($i=0; $i<$count; $i++) {
54 if (isset($tasks[$i]['msproj_id'])) {
55 $msprojid[$tasks[$i]['msproj_id']] =& $tasks[$i];
57 if (isset($tasks[$i]['resources'])) {
58 $resrc = $tasks[$i]['resources'];
59 for ($j=0; $j<count($resrc); $j++) {
60 //validate user - see if they really exist as techs in this subproject
61 if (!util_ifsetor($tarr[strtolower($resrc[$j]['user_name'])])) {
62 //create list of bogus names to send back
63 if (array_search(strtolower($resrc[$j]['user_name']),$invalid_names) === false) {
64 $invalid_names[]=$resrc[$j]['user_name'];
72 // invalid assignees - send it back for remapping
74 if (count($invalid_names)) {
75 $array['success']=false;
76 $array['errormessage']=_('Invalid Resource Name')._(':');
77 foreach ($invalid_names as $i) {
78 $array['errormessage'] .= ' ' . $i;
80 $array['resourcename']=$invalid_names;
81 for ($i=0; $i<$tcount; $i++) {
82 $array['usernames'][$techs[$i]->getID()]=$techs[$i]->getUnixName();
87 // Begin inserting/updating the tasks
89 for ($i=0; $i<$count; $i++) {
93 //no task_id so it must be new - create it
94 if (!$tasks[$i]['id']) {
95 if (!$tasks[$i]['notes']) {
96 $tasks[$i]['notes']=_('None Provided');
99 $pt = new ProjectTask($pg);
100 if (!$pt || !is_object($pt)) {
101 $array['success']=false;
103 $array['errormessage']=_('Could Not Get ProjectTask');
104 } elseif ($pt->isError()) {
105 $array['success']=false;
107 $array['errormessage']=_('Could Not Get ProjectTask')._(': ').$pt->getErrorMessage();
109 //remap priority names=>numbers
110 $priority=$tasks[$i]['priority'];
111 if (!$priority || $priority < 1 || $priority > 5) {
116 if (isset($tasks[$i]['resources'])) {
117 $resrc = $tasks[$i]['resources'];
118 for ($ucount=0; $ucount< count($resrc); $ucount++) {
119 //get their user_id from the $tarr we created earlier
120 $assignees[]=$tarr[strtolower($resrc[$ucount]['user_name'])];
123 //don't do anything with dependencies yet - we may only have
124 //the MSprojid from dependent items
125 $hours = $tasks[$i]['work'];
129 $percent_complete= intval($tasks[$i]['percent_complete']);
130 if (!$percent_complete) {
132 } elseif ($percent_complete > 100) {
133 $percent_complete=100;
136 // Convert category name to category_id if given.
137 if (isset($tasks[$i]['category'])) {
138 if ($tasks[$i]['category'] == 'None') {
141 $res = db_query_params('SELECT category_id FROM project_category WHERE group_project_id=$1 AND category_name=$2',
142 array($group_project_id, $tasks[$i]['category']));
143 $category_id = db_result($res, 0, 'category_id');
146 $array['errormessage']=_('Error No category named')._(': ').$tasks[$i]['category'];
151 $category_id = $pt->getCategoryID();
159 strtotime($tasks[$i]['start_date']),
160 strtotime($tasks[$i]['end_date']),
165 $tasks[$i]['duration'],
166 $tasks[$i]['parent_id'])) {
167 $array['success']=false;
169 $array['errormessage']=_('Error Creating ProjectTask')._(': ').$pt->getErrorMessage();
174 $tasks[$i]['id'] = $pt->getID();
175 $tasks[$i]['obj'] = $pt;
176 if (isset($tasks[$i]['msproj_id'])) {
177 $pt->setExternalID($tasks[$i]['msproj_id']);
184 //update existing task
186 $pt = projecttask_get_object($tasks[$i]['id']);
187 if (!$pt || !is_object($pt)) {
188 } elseif ($pt->isError()) {
189 $array['success']=false;
191 $array['errormessage']=_('Could Not Get ProjectTask')._(': ').$pt->getErrorMessage();
193 //remap priority names=>numbers
194 $priority=$tasks[$i]['priority'];
195 if (!$priority || $priority < 1 || $priority > 5) {
201 if (isset($tasks[$i]['resources'])) {
202 $resrc = $tasks[$i]['resources'];
203 for ($ucount=0; $ucount<count($resrc); $ucount++) {
204 //get their user_id from the $tarr we created earlier
205 $assignees[]=$tarr[strtolower($resrc[$ucount]['user_name'])];
209 //don't do anything with dependencies yet - we may only have the
210 //MSprojid from dependent items
211 $hours = $tasks[$i]['work'];
215 $percent_complete= intval($tasks[$i]['percent_complete']);
216 if (!$percent_complete) {
218 } elseif ($percent_complete > 100) {
219 $percent_complete=100;
222 // Convert category name to category_id if given.
223 if (isset($tasks[$i]['category'])) {
224 if ($tasks[$i]['category'] == 'None') {
227 $res = db_query_params('SELECT category_id FROM project_category WHERE group_project_id=$1 AND category_name=$2',
228 array($group_project_id, $tasks[$i]['category']));
229 $category_id = db_result($res, 0, 'category_id');
232 $array['errormessage']=_('Error No category named')._(': ').$tasks[$i]['category'];
237 $category_id = $pt->getCategoryID();
240 $depends_on = $pt->getDependentOn();
247 strtotime($tasks[$i]['start_date']),
248 strtotime($tasks[$i]['end_date']),
255 $tasks[$i]['duration'],
256 $tasks[$i]['parent_id'])) {
257 $array['success']=false;
259 $array['errormessage']=_('Error Updating ProjectTask')._(': ').$pt->getErrorMessage();
265 $tasks[$i]['id'] = $pt->getID();
266 $tasks[$i]['obj'] = $pt;
267 if (isset($tasks[$i]['msproj_id'])) {
268 $pt->setExternalID($tasks[$i]['msproj_id']);
273 } //if task->iserror()
275 //accumulate list of completed tasks
276 //any task not in this list will be deleted
277 $completed[$tasks[$i]['id']]=true;
281 // Do task dependencies
286 for ($i=0; $i<$count; $i++) {
287 if (isset($tasks[$i]['dependenton'])) {
288 $darr=$tasks[$i]['dependenton'];
291 //iterate each dependency in a task
292 for ($dcount=0; $dcount<count($darr); $dcount++) {
293 //get the id of the task we're dependent on -
294 // may have to get it from msprojid linked list
295 $id=$darr[$dcount]['task_id'];
297 $id=$msprojid[$darr[$dcount]['msproj_id']]['id'];
299 //prevent task from being dependent on itself
300 if ($id == $tasks[$i]['id']) {
303 $deps[$id]=$darr[$dcount]['link_type'];
305 if (isset($tasks[$i]['obj']) && is_object($tasks[$i]['obj'])) {
306 if (!$tasks[$i]['obj']->setDependentOn($deps)) {
308 $array['success']=false;
313 } //iterates tasks to do dependencies
318 // Delete unreferenced tasks
320 if ($replace && !$was_error) {
321 $ptf = new ProjectTaskFactory($pg);
322 $pt_arr=& $ptf->getTasks();
323 for ($i=0; $i<count($pt_arr); $i++) {
324 if (is_object($pt_arr[$i])) {
325 if (!util_ifsetor($completed[$pt_arr[$i]->getID()])) {
326 if (!$pt_arr[$i]->delete(true)) {
327 echo $pt_arr[$i]->getErrorMessage();
337 $array['success']=true;
345 // c-file-style: "bsd"