PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/htdocs/core/lib/project.lib.php

https://bitbucket.org/speedealing/speedealing
PHP | 753 lines | 488 code | 100 blank | 165 comment | 112 complexity | 4b68b88a879005182331e91df8f10e81 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2006-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2010 Regis Houssin <regis.houssin@capnetworks.com>
  4. * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. * or see http://www.gnu.org/
  19. */
  20. /**
  21. * \file htdocs/core/lib/project.lib.php
  22. * \brief Functions used by project module
  23. * \ingroup project
  24. */
  25. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  26. /**
  27. * Prepare array with list of tabs
  28. *
  29. * @param Object $object Object related to tabs
  30. * @return array Array of tabs to shoc
  31. */
  32. function project_prepare_head($object)
  33. {
  34. global $langs, $conf, $user;
  35. $h = 0;
  36. $head = array();
  37. $head[$h][0] = DOL_URL_ROOT.'/projet/fiche.php?id='.$object->id;
  38. $head[$h][1] = $langs->trans("Project");
  39. $head[$h][2] = 'project';
  40. $h++;
  41. $head[$h][0] = DOL_URL_ROOT.'/projet/contact.php?id='.$object->id;
  42. $head[$h][1] = $langs->trans("ProjectContact");
  43. $head[$h][2] = 'contact';
  44. $h++;
  45. if (! empty($conf->fournisseur->enabled) || ! empty($conf->propal->enabled) || ! empty($conf->commande->enabled)
  46. || ! empty($conf->facture->enabled) || ! empty($conf->contrat->enabled)
  47. || ! empty($conf->ficheinter->enabled) || ! empty($conf->agenda->enabled) || ! empty($conf->deplacement->enabled))
  48. {
  49. $head[$h][0] = DOL_URL_ROOT.'/projet/element.php?id='.$object->id;
  50. $head[$h][1] = $langs->trans("Referers");
  51. $head[$h][2] = 'element';
  52. $h++;
  53. }
  54. // Show more tabs from modules
  55. // Entries must be declared in modules descriptor with line
  56. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  57. // $this->tabs = array('entity:-tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to remove a tab
  58. complete_head_from_modules($conf,$langs,$object,$head,$h,'project');
  59. $head[$h][0] = DOL_URL_ROOT.'/projet/document.php?id='.$object->id;
  60. /*$filesdir = $conf->projet->dir_output . "/" . dol_sanitizeFileName($object->ref);
  61. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  62. $listoffiles=dol_dir_list($filesdir,'files',1);
  63. $head[$h][1] = (count($listoffiles)?$langs->trans('DocumentsNb',count($listoffiles)):$langs->trans('Documents'));*/
  64. $head[$h][1] = $langs->trans('Documents');
  65. $head[$h][2] = 'document';
  66. $h++;
  67. $head[$h][0] = DOL_URL_ROOT.'/projet/note.php?id='.$object->id;
  68. $head[$h][1] = $langs->trans('Notes');
  69. $head[$h][2] = 'notes';
  70. $h++;
  71. // Then tab for sub level of projet, i mean tasks
  72. $head[$h][0] = DOL_URL_ROOT.'/projet/tasks.php?id='.$object->id;
  73. $head[$h][1] = $langs->trans("Tasks");
  74. $head[$h][2] = 'tasks';
  75. $h++;
  76. /* Now this is a filter in the Task tab.
  77. $head[$h][0] = DOL_URL_ROOT.'/projet/tasks.php?id='.$object->id.'&mode=mine';
  78. $head[$h][1] = $langs->trans("MyTasks");
  79. $head[$h][2] = 'mytasks';
  80. $h++;
  81. */
  82. $head[$h][0] = DOL_URL_ROOT.'/projet/ganttview.php?id='.$object->id;
  83. $head[$h][1] = $langs->trans("Gantt");
  84. $head[$h][2] = 'gantt';
  85. $h++;
  86. return $head;
  87. }
  88. /**
  89. * Prepare array with list of tabs
  90. *
  91. * @param Object $object Object related to tabs
  92. * @return array Array of tabs to shoc
  93. */
  94. function task_prepare_head($object)
  95. {
  96. global $langs, $conf, $user;
  97. $h = 0;
  98. $head = array();
  99. $head[$h][0] = DOL_URL_ROOT.'/projet/tasks/task.php?id='.$object->id.(GETPOST('withproject')?'&withproject=1':'');;
  100. $head[$h][1] = $langs->trans("Card");
  101. $head[$h][2] = 'task_task';
  102. $h++;
  103. $head[$h][0] = DOL_URL_ROOT.'/projet/tasks/contact.php?id='.$object->id.(GETPOST('withproject')?'&withproject=1':'');;
  104. $head[$h][1] = $langs->trans("TaskRessourceLinks");
  105. $head[$h][2] = 'task_contact';
  106. $h++;
  107. $head[$h][0] = DOL_URL_ROOT.'/projet/tasks/time.php?id='.$object->id.(GETPOST('withproject')?'&withproject=1':'');;
  108. $head[$h][1] = $langs->trans("TimeSpent");
  109. $head[$h][2] = 'task_time';
  110. $h++;
  111. // Show more tabs from modules
  112. // Entries must be declared in modules descriptor with line
  113. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  114. // $this->tabs = array('entity:-tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to remove a tab
  115. complete_head_from_modules($conf,$langs,$object,$head,$h,'task');
  116. $head[$h][0] = DOL_URL_ROOT.'/projet/tasks/document.php?id='.$object->id.(GETPOST('withproject')?'&withproject=1':'');;
  117. /*$filesdir = $conf->projet->dir_output . "/" . dol_sanitizeFileName($object->ref);
  118. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  119. $listoffiles=dol_dir_list($filesdir,'files',1);
  120. $head[$h][1] = (count($listoffiles)?$langs->trans('DocumentsNb',count($listoffiles)):$langs->trans('Documents'));*/
  121. $head[$h][1] = $langs->trans('Documents');
  122. $head[$h][2] = 'task_document';
  123. $h++;
  124. $head[$h][0] = DOL_URL_ROOT.'/projet/tasks/note.php?id='.$object->id.(GETPOST('withproject')?'&withproject=1':'');;
  125. $head[$h][1] = $langs->trans('Notes');
  126. $head[$h][2] = 'task_notes';
  127. $h++;
  128. return $head;
  129. }
  130. /**
  131. * Show a combo list with projects qualified for a third party
  132. *
  133. * @param int $socid Id third party (-1=all, 0=only projects not linked to a third party, id=projects not linked or linked to third party id)
  134. * @param int $selected Id project preselected
  135. * @param string $htmlname Nom de la zone html
  136. * @param int $maxlength Maximum length of label
  137. * @return int Nbre of project if OK, <0 if KO
  138. */
  139. function select_projects($socid=-1, $selected='', $htmlname='projectid', $maxlength=16)
  140. {
  141. global $db,$user,$conf,$langs;
  142. $hideunselectables = false;
  143. if (! empty($conf->global->PROJECT_HIDE_UNSELECTABLES)) $hideunselectables = true;
  144. $projectsListId = false;
  145. if (empty($user->rights->projet->all->lire))
  146. {
  147. $projectstatic=new Project($db);
  148. $projectsListId = $projectstatic->getProjectsAuthorizedForUser($user,0,1);
  149. }
  150. // Search all projects
  151. $sql = 'SELECT p.rowid, p.ref, p.title, p.fk_soc, p.fk_statut, p.public';
  152. $sql.= ' FROM '.MAIN_DB_PREFIX .'projet as p';
  153. $sql.= " WHERE p.entity = ".$conf->entity;
  154. if ($projectsListId) $sql.= " AND p.rowid IN (".$projectsListId.")";
  155. if ($socid == 0) $sql.= " AND (p.fk_soc=0 OR p.fk_soc IS NULL)";
  156. $sql.= " ORDER BY p.title ASC";
  157. dol_syslog("project.lib::select_projects sql=".$sql);
  158. $resql=$db->query($sql);
  159. if ($resql)
  160. {
  161. print '<select class="flat" name="'.$htmlname.'">';
  162. print '<option value="0">&nbsp;</option>';
  163. $num = $db->num_rows($resql);
  164. $i = 0;
  165. if ($num)
  166. {
  167. while ($i < $num)
  168. {
  169. $obj = $db->fetch_object($resql);
  170. // If we ask to filter on a company and user has no permission to see all companies and project is linked to another company, we hide project.
  171. if ($socid > 0 && (empty($obj->fk_soc) || $obj->fk_soc == $socid) && ! $user->rights->societe->lire)
  172. {
  173. // Do nothing
  174. }
  175. else
  176. {
  177. $labeltoshow=dol_trunc($obj->ref,18);
  178. //if ($obj->public) $labeltoshow.=' ('.$langs->trans("SharedProject").')';
  179. //else $labeltoshow.=' ('.$langs->trans("Private").')';
  180. if (!empty($selected) && $selected == $obj->rowid && $obj->fk_statut > 0)
  181. {
  182. print '<option value="'.$obj->rowid.'" selected="selected">'.$labeltoshow.'</option>';
  183. }
  184. else
  185. {
  186. $disabled=0;
  187. if (! $obj->fk_statut > 0)
  188. {
  189. $disabled=1;
  190. $labeltoshow.=' - '.$langs->trans("Draft");
  191. }
  192. if ($socid > 0 && (! empty($obj->fk_soc) && $obj->fk_soc != $socid))
  193. {
  194. $disabled=1;
  195. $labeltoshow.=' - '.$langs->trans("LinkedToAnotherCompany");
  196. }
  197. if ($hideunselectables && $disabled)
  198. {
  199. $resultat='';
  200. }
  201. else
  202. {
  203. $resultat='<option value="'.$obj->rowid.'"';
  204. if ($disabled) $resultat.=' disabled="disabled"';
  205. //if ($obj->public) $labeltoshow.=' ('.$langs->trans("Public").')';
  206. //else $labeltoshow.=' ('.$langs->trans("Private").')';
  207. $resultat.='>'.$labeltoshow;
  208. if (! $disabled) $resultat.=' - '.dol_trunc($obj->title,$maxlength);
  209. $resultat.='</option>';
  210. }
  211. print $resultat;
  212. }
  213. }
  214. $i++;
  215. }
  216. }
  217. print '</select>';
  218. $db->free($resql);
  219. return $num;
  220. }
  221. else
  222. {
  223. dol_print_error($db);
  224. return -1;
  225. }
  226. }
  227. /**
  228. * Show task lines with a particular parent
  229. *
  230. * @param string &$inc Counter that count number of lines legitimate to show (for return)
  231. * @param int $parent Id of parent task to start
  232. * @param array &$lines Array of all tasks
  233. * @param int &$level Level of task
  234. * @param string $var Color
  235. * @param int $showproject Show project columns
  236. * @param int &$taskrole Array of roles of user for each tasks
  237. * @param int $projectsListId List of id of project allowed to user (separated with comma)
  238. * @param int $addordertick Add a tick to move task
  239. * @return void
  240. */
  241. function projectLinesa(&$inc, $parent, &$lines, &$level, $var, $showproject, &$taskrole, $projectsListId='', $addordertick=0)
  242. {
  243. global $user, $bc, $langs;
  244. global $projectstatic, $taskstatic;
  245. $lastprojectid=0;
  246. $projectsArrayId=explode(',',$projectsListId);
  247. $numlines=count($lines);
  248. $total=0;
  249. for ($i = 0 ; $i < $numlines ; $i++)
  250. {
  251. if ($parent == 0) $level = 0;
  252. // Process line
  253. // print "i:".$i."-".$lines[$i]->fk_project.'<br>';
  254. if ($lines[$i]->fk_parent == $parent)
  255. {
  256. // Show task line.
  257. $showline=1;
  258. $showlineingray=0;
  259. // If there is filters to use
  260. if (is_array($taskrole))
  261. {
  262. // If task not legitimate to show, search if a legitimate task exists later in tree
  263. if (! isset($taskrole[$lines[$i]->id]) && $lines[$i]->id != $lines[$i]->fk_parent)
  264. {
  265. // So search if task has a subtask legitimate to show
  266. $foundtaskforuserdeeper=0;
  267. searchTaskInChild($foundtaskforuserdeeper,$lines[$i]->id,$lines,$taskrole);
  268. //print '$foundtaskforuserpeeper='.$foundtaskforuserdeeper.'<br>';
  269. if ($foundtaskforuserdeeper > 0)
  270. {
  271. $showlineingray=1; // We will show line but in gray
  272. }
  273. else
  274. {
  275. $showline=0; // No reason to show line
  276. }
  277. }
  278. }
  279. if ($showline)
  280. {
  281. // Break on a new project
  282. if ($parent == 0 && $lines[$i]->fk_project != $lastprojectid)
  283. {
  284. $var = !$var;
  285. $lastprojectid=$lines[$i]->fk_project;
  286. }
  287. print '<tr '.$bc[$var].' id="row-'.$lines[$i]->id.'">'."\n";
  288. // Project
  289. if ($showproject)
  290. {
  291. print "<td>";
  292. //var_dump($taskrole);
  293. if ($showlineingray) print '<i>';
  294. $projectstatic->id=$lines[$i]->fk_project;
  295. $projectstatic->ref=$lines[$i]->projectref;
  296. $projectstatic->public=$lines[$i]->public;
  297. if ($lines[$i]->public || in_array($lines[$i]->fk_project,$projectsArrayId)) print $projectstatic->getNomUrl(1);
  298. else print $projectstatic->getNomUrl(1,'nolink');
  299. if ($showlineingray) print '</i>';
  300. print "</td>";
  301. }
  302. // Ref of task
  303. print '<td>';
  304. if ($showlineingray)
  305. {
  306. print '<i>'.img_object('','projecttask').' '.$lines[$i]->id.'</i>';
  307. }
  308. else
  309. {
  310. $taskstatic->id=$lines[$i]->id;
  311. $taskstatic->ref=$lines[$i]->id;
  312. $taskstatic->label=($taskrole[$lines[$i]->id]?$langs->trans("YourRole").': '.$taskrole[$lines[$i]->id]:'');
  313. print $taskstatic->getNomUrl(1,($showproject?'':'withproject'));
  314. }
  315. print '</td>';
  316. // Title of task
  317. print "<td>";
  318. if ($showlineingray) print '<i>';
  319. else print '<a href="'.DOL_URL_ROOT.'/projet/tasks/task.php?id='.$lines[$i]->id.($showproject?'':'&withproject=1').'">';
  320. for ($k = 0 ; $k < $level ; $k++)
  321. {
  322. print "&nbsp; &nbsp; &nbsp;";
  323. }
  324. print $lines[$i]->label;
  325. if ($showlineingray) print '</i>';
  326. else print '</a>';
  327. print "</td>\n";
  328. // Date start
  329. print '<td align="center">';
  330. print dol_print_date($lines[$i]->date_start,'day');
  331. print '</td>';
  332. // Date end
  333. print '<td align="center">';
  334. print dol_print_date($lines[$i]->date_end,'day');
  335. print '</td>';
  336. // Progress
  337. print '<td align="right">';
  338. print $lines[$i]->progress.' %';
  339. print '</td>';
  340. // Time spent
  341. print '<td align="right">';
  342. if ($showlineingray) print '<i>';
  343. else print '<a href="'.DOL_URL_ROOT.'/projet/tasks/time.php?id='.$lines[$i]->id.($showproject?'':'&withproject=1').'">';
  344. if ($lines[$i]->duration) print convertSecondToTime($lines[$i]->duration,'all');
  345. else print '--:--';
  346. if ($showlineingray) print '</i>';
  347. else print '</a>';
  348. print '</td>';
  349. // Tick to drag and drop
  350. if ($addordertick)
  351. {
  352. print '<td align="center" class="tdlineupdown">&nbsp;</td>';
  353. }
  354. print "</tr>\n";
  355. if (! $showlineingray) $inc++;
  356. $level++;
  357. if ($lines[$i]->id) projectLinesa($inc, $lines[$i]->id, $lines, $level, $var, $showproject, $taskrole, $projectsListId);
  358. $level--;
  359. $total += $lines[$i]->duration;
  360. }
  361. }
  362. else
  363. {
  364. //$level--;
  365. }
  366. }
  367. if ($total>0)
  368. {
  369. print '<tr class="liste_total"><td class="liste_total">'.$langs->trans("Total").'</td>';
  370. if ($showproject) print '<td></td>';
  371. print '<td></td>';
  372. print '<td></td>';
  373. print '<td></td>';
  374. print '<td></td>';
  375. print '<td align="right" nowrap="nowrap" class="liste_total">'.convertSecondToTime($total).'</td></tr>';
  376. }
  377. return $inc;
  378. }
  379. /**
  380. * Output a task line
  381. *
  382. * @param string &$inc ?
  383. * @param string $parent ?
  384. * @param Object $lines ?
  385. * @param int &$level ?
  386. * @param string &$projectsrole ?
  387. * @param string &$tasksrole ?
  388. * @param int $mytask 0 or 1 to enable only if task is a task i am affected to
  389. * @return $inc
  390. */
  391. function projectLinesb(&$inc, $parent, $lines, &$level, &$projectsrole, &$tasksrole, $mytask=0)
  392. {
  393. global $user, $bc, $langs;
  394. global $form, $projectstatic, $taskstatic;
  395. $lastprojectid=0;
  396. $var=true;
  397. $numlines=count($lines);
  398. for ($i = 0 ; $i < $numlines ; $i++)
  399. {
  400. if ($parent == 0) $level = 0;
  401. if ($lines[$i]->fk_parent == $parent)
  402. {
  403. // Break on a new project
  404. if ($parent == 0 && $lines[$i]->fk_project != $lastprojectid)
  405. {
  406. $var = !$var;
  407. $lastprojectid=$lines[$i]->fk_project;
  408. }
  409. print "<tr ".$bc[$var].">\n";
  410. // Project
  411. print "<td>";
  412. $projectstatic->id=$lines[$i]->fk_project;
  413. $projectstatic->ref=$lines[$i]->projectref;
  414. $projectstatic->public=$lines[$i]->public;
  415. $projectstatic->label=$langs->transnoentitiesnoconv("YourRole").': '.$projectsrole[$lines[$i]->fk_project];
  416. print $projectstatic->getNomUrl(1);
  417. print "</td>";
  418. // Ref
  419. print '<td>';
  420. $taskstatic->id=$lines[$i]->id;
  421. $taskstatic->ref=$lines[$i]->id;
  422. print $taskstatic->getNomUrl(1);
  423. print '</td>';
  424. // Label task
  425. print "<td>";
  426. for ($k = 0 ; $k < $level ; $k++)
  427. {
  428. print "&nbsp;&nbsp;&nbsp;";
  429. }
  430. print $lines[$i]->label;
  431. print "</td>\n";
  432. // Date start
  433. print '<td align="center">';
  434. print dol_print_date($lines[$i]->date_start,'day');
  435. print '</td>';
  436. // Date end
  437. print '<td align="center">';
  438. print dol_print_date($lines[$i]->date_end,'day');
  439. print '</td>';
  440. // Progress
  441. print '<td align="right">';
  442. print $lines[$i]->progress.' %';
  443. print '</td>';
  444. // Time spent
  445. print '<td align="right">';
  446. if ($lines[$i]->duration) print convertSecondToTime($lines[$i]->duration,'all');
  447. else print '--:--';
  448. print "</td>\n";
  449. $disabledproject=1;$disabledtask=1;
  450. //print "x".$lines[$i]->fk_project;
  451. //var_dump($lines[$i]);
  452. //var_dump($projectsrole[$lines[$i]->fk_project]);
  453. // If at least one role for project
  454. if ($lines[$i]->public || ! empty($projectsrole[$lines[$i]->fk_project]) || $user->rights->projet->all->creer)
  455. {
  456. $disabledproject=0;
  457. $disabledtask=0;
  458. }
  459. // If mytask and no role on task
  460. if ($mytask && empty($tasksrole[$lines[$i]->id]))
  461. {
  462. $disabledtask=1;
  463. }
  464. print '<td nowrap="nowrap">';
  465. $s =$form->select_date('',$lines[$i]->id,'','','',"addtime",1,0,1,$disabledtask);
  466. $s.='&nbsp;&nbsp;&nbsp;';
  467. $s.=$form->select_duration($lines[$i]->id,'',$disabledtask);
  468. $s.='&nbsp;<input type="submit" class="button"'.($disabledtask?' disabled="disabled"':'').' value="'.$langs->trans("Add").'">';
  469. print $s;
  470. print '</td>';
  471. print '<td align="right">';
  472. if ((! $lines[$i]->public) && $disabledproject) print $form->textwithpicto('',$langs->trans("YouAreNotContactOfProject"));
  473. else if ($disabledtask) print $form->textwithpicto('',$langs->trans("TaskIsNotAffectedToYou"));
  474. print '</td>';
  475. print "</tr>\n";
  476. $inc++;
  477. $level++;
  478. if ($lines[$i]->id) projectLinesb($inc, $lines[$i]->id, $lines, $level, $projectsrole, $tasksrole, $mytask);
  479. $level--;
  480. }
  481. else
  482. {
  483. //$level--;
  484. }
  485. }
  486. return $inc;
  487. }
  488. /**
  489. * Search in task lines with a particular parent if there is a task for a particular user (in taskrole)
  490. *
  491. * @param string &$inc Counter that count number of lines legitimate to show (for return)
  492. * @param int $parent Id of parent task to start
  493. * @param array &$lines Array of all tasks
  494. * @param string &$taskrole Array of task filtered on a particular user
  495. * @return int 1 if there is
  496. */
  497. function searchTaskInChild(&$inc, $parent, &$lines, &$taskrole)
  498. {
  499. //print 'Search in line with parent id = '.$parent.'<br>';
  500. $numlines=count($lines);
  501. for ($i = 0 ; $i < $numlines ; $i++)
  502. {
  503. // Process line $lines[$i]
  504. if ($lines[$i]->fk_parent == $parent && $lines[$i]->id != $lines[$i]->fk_parent)
  505. {
  506. // If task is legitimate to show, no more need to search deeper
  507. if (isset($taskrole[$lines[$i]->id]))
  508. {
  509. //print 'Found a legitimate task id='.$lines[$i]->id.'<br>';
  510. $inc++;
  511. return $inc;
  512. }
  513. searchTaskInChild($inc, $lines[$i]->id, $lines, $taskrole);
  514. //print 'Found inc='.$inc.'<br>';
  515. if ($inc > 0) return $inc;
  516. }
  517. }
  518. return $inc;
  519. }
  520. /**
  521. * Clean task not linked to a parent
  522. *
  523. * @param DoliDB $db Database handler
  524. * @return int Nb of records deleted
  525. */
  526. function clean_orphelins($db)
  527. {
  528. $nb=0;
  529. // There is orphelins. We clean that
  530. $listofid=array();
  531. // Get list of id in array listofid
  532. $sql='SELECT rowid FROM '.MAIN_DB_PREFIX.'projet_task';
  533. $resql = $db->query($sql);
  534. if ($resql)
  535. {
  536. $num = $db->num_rows($resql);
  537. $i = 0;
  538. while ($i < $num && $i < 100)
  539. {
  540. $obj = $db->fetch_object($resql);
  541. $listofid[]=$obj->rowid;
  542. $i++;
  543. }
  544. }
  545. else
  546. {
  547. dol_print_error($db);
  548. }
  549. if (count($listofid))
  550. {
  551. // Removed orphelins records
  552. print 'Some orphelins were found and restored to be parents so records are visible again: ';
  553. print join(',',$listofid);
  554. $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task";
  555. $sql.= " SET fk_task_parent = 0";
  556. $sql.= " WHERE fk_task_parent NOT IN (".join(',',$listofid).")";
  557. $resql = $db->query($sql);
  558. if ($resql)
  559. {
  560. $nb=$db->affected_rows($sql);
  561. return $nb;
  562. }
  563. else
  564. {
  565. return -1;
  566. }
  567. }
  568. }
  569. /**
  570. * Return HTML table with list of projects and number of opened tasks
  571. *
  572. * @param DoliDB $db Database handler
  573. * @param int $socid Id thirdparty
  574. * @param int $projectsListId Id of project i have permission on
  575. * @param int $mytasks Limited to task i am contact to
  576. * @return void
  577. */
  578. function print_projecttasks_array($db, $socid, $projectsListId, $mytasks=0)
  579. {
  580. global $langs,$conf,$user,$bc;
  581. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  582. $projectstatic=new Project($db);
  583. $sortfield='';
  584. $sortorder='';
  585. print '<table class="noborder" width="100%">';
  586. print '<tr class="liste_titre">';
  587. print_liste_field_titre($langs->trans("Project"),"index.php","","","","",$sortfield,$sortorder);
  588. print_liste_field_titre($langs->trans("NbOpenTasks"),"","","","",'align="right"',$sortfield,$sortorder);
  589. print_liste_field_titre($langs->trans("Status"),"","","","",'align="right"',$sortfield,$sortorder);
  590. print "</tr>\n";
  591. $sql = "SELECT p.rowid as projectid, p.ref, p.title, p.fk_user_creat, p.public, p.fk_statut, COUNT(t.rowid) as nb";
  592. $sql.= " FROM ".MAIN_DB_PREFIX."projet as p";
  593. if ($mytasks)
  594. {
  595. $sql.= ", ".MAIN_DB_PREFIX."projet_task as t";
  596. $sql.= ", ".MAIN_DB_PREFIX."element_contact as ec";
  597. $sql.= ", ".MAIN_DB_PREFIX."c_type_contact as ctc";
  598. }
  599. else
  600. {
  601. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."projet_task as t ON p.rowid = t.fk_projet";
  602. }
  603. $sql.= " WHERE p.entity = ".$conf->entity;
  604. $sql.= " AND p.rowid IN (".$projectsListId.")";
  605. if ($socid) $sql.= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".$socid.")";
  606. if ($mytasks)
  607. {
  608. $sql.= " AND p.rowid = t.fk_projet";
  609. $sql.= " AND ec.element_id = t.rowid";
  610. $sql.= " AND ctc.rowid = ec.fk_c_type_contact";
  611. $sql.= " AND ctc.element = 'project_task'";
  612. $sql.= " AND ec.fk_socpeople = ".$user->id;
  613. }
  614. $sql.= " GROUP BY p.rowid, p.ref, p.title, p.fk_user_creat, p.public, p.fk_statut";
  615. $sql.= " ORDER BY p.title, p.ref";
  616. $var=true;
  617. $resql = $db->query($sql);
  618. if ( $resql )
  619. {
  620. $num = $db->num_rows($resql);
  621. $i = 0;
  622. while ($i < $num)
  623. {
  624. $objp = $db->fetch_object($resql);
  625. $projectstatic->id = $objp->projectid;
  626. $projectstatic->user_author_id = $objp->fk_user_creat;
  627. $projectstatic->public = $objp->public;
  628. // Check is user has read permission on project
  629. $userAccess = $projectstatic->restrictedProjectArea($user);
  630. if ($userAccess >= 0)
  631. {
  632. $var=!$var;
  633. print "<tr ".$bc[$var].">";
  634. print '<td nowrap="nowrap">';
  635. $projectstatic->ref=$objp->ref;
  636. print $projectstatic->getNomUrl(1);
  637. print ' - '.$objp->title.'</td>';
  638. print '<td align="right">'.$objp->nb.'</td>';
  639. $projectstatic->statut = $objp->fk_statut;
  640. print '<td align="right">'.$projectstatic->getLibStatut(3).'</td>';
  641. print "</tr>\n";
  642. }
  643. $i++;
  644. }
  645. $db->free($resql);
  646. }
  647. else
  648. {
  649. dol_print_error($db);
  650. }
  651. print "</table>";
  652. }
  653. ?>