PageRenderTime 56ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/projet/class/project.class.php

https://github.com/asterix14/dolibarr
PHP | 928 lines | 674 code | 94 blank | 160 comment | 148 complexity | 498acfa02be94919d36529c149c013e0 MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2002-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2005-2008 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2010 Regis Houssin <regis@dolibarr.fr>
  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 2 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. */
  19. /**
  20. * \file htdocs/projet/class/project.class.php
  21. * \ingroup projet
  22. * \brief Fichier de la classe de gestion des projets
  23. */
  24. require_once(DOL_DOCUMENT_ROOT . "/core/class/commonobject.class.php");
  25. /**
  26. * \class Project
  27. * \brief Class to manage projects
  28. */
  29. class Project extends CommonObject
  30. {
  31. public $element = 'project'; //!< Id that identify managed objects
  32. public $table_element = 'projet'; //!< Name of table without prefix where object is stored
  33. var $id;
  34. var $ref;
  35. var $description;
  36. var $statut;
  37. var $title;
  38. var $date_c;
  39. var $date_m;
  40. var $date_start;
  41. var $date_end;
  42. var $socid;
  43. var $user_author_id; //!< Id of project creator. Not defined if shared project.
  44. var $public; //!< Tell if this is a public or private project
  45. var $note_private;
  46. var $note_public;
  47. var $statuts_short;
  48. var $statuts;
  49. /**
  50. * Constructor
  51. *
  52. * @param DoliDB $DB Database handler
  53. */
  54. function Project($DB)
  55. {
  56. $this->db = $DB;
  57. $this->societe = new Societe($DB);
  58. $this->statuts_short = array(0 => 'Draft', 1 => 'Validated', 2 => 'Closed');
  59. $this->statuts = array(0 => 'Draft', 1 => 'Validated', 2 => 'Closed');
  60. }
  61. /**
  62. * Create a project into database
  63. *
  64. * @param User $user User making creation
  65. * @param int $notrigger Disable triggers
  66. * @return int <0 if KO, id of created project if OK
  67. */
  68. function create($user, $notrigger=0)
  69. {
  70. global $conf;
  71. $error = 0;
  72. $ret = 0;
  73. // Check parameters
  74. if (!trim($this->ref))
  75. {
  76. $this->error = 'ErrorFieldsRequired';
  77. dol_syslog("Project::Create error -1 ref null", LOG_ERR);
  78. return -1;
  79. }
  80. $this->db->begin();
  81. $sql = "INSERT INTO " . MAIN_DB_PREFIX . "projet (";
  82. $sql.= "ref";
  83. $sql.= ", title";
  84. $sql.= ", description";
  85. $sql.= ", fk_soc";
  86. $sql.= ", fk_user_creat";
  87. $sql.= ", fk_statut";
  88. $sql.= ", public";
  89. $sql.= ", datec";
  90. $sql.= ", dateo";
  91. $sql.= ", datee";
  92. $sql.= ") VALUES (";
  93. $sql.= "'" . $this->db->escape($this->ref) . "'";
  94. $sql.= ", '" . $this->db->escape($this->title) . "'";
  95. $sql.= ", '" . $this->db->escape($this->description) . "'";
  96. $sql.= ", " . ($this->socid > 0 ? $this->socid : "null");
  97. $sql.= ", " . $user->id;
  98. $sql.= ", 0";
  99. $sql.= ", " . ($this->public ? 1 : 0);
  100. $sql.= ", " . ($this->datec != '' ? $this->db->idate($this->datec) : 'null');
  101. $sql.= ", " . ($this->dateo != '' ? $this->db->idate($this->dateo) : 'null');
  102. $sql.= ", " . ($this->datee != '' ? $this->db->idate($this->datee) : 'null');
  103. $sql.= ")";
  104. dol_syslog("Project::create sql=" . $sql, LOG_DEBUG);
  105. $resql = $this->db->query($sql);
  106. if ($resql)
  107. {
  108. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "projet");
  109. $ret = $this->id;
  110. if (!$notrigger)
  111. {
  112. // Call triggers
  113. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  114. $interface = new Interfaces($this->db);
  115. $result = $interface->run_triggers('PROJECT_CREATE', $this, $user, $langs, $conf);
  116. if ($result < 0)
  117. {
  118. $error++;
  119. $this->errors = $interface->errors;
  120. }
  121. // End call triggers
  122. }
  123. }
  124. else
  125. {
  126. $this->error = $this->db->lasterror();
  127. $this->errno = $this->db->lasterrno();
  128. dol_syslog("Project::Create error -2 " . $this->error, LOG_ERR);
  129. $error++;
  130. }
  131. if (!$error && !empty($conf->global->MAIN_DISABLEDRAFTSTATUS))
  132. {
  133. $res = $this->setValid($user);
  134. if ($res < 0) $error++;
  135. }
  136. if (!$error)
  137. {
  138. $this->db->commit();
  139. return $ret;
  140. }
  141. else
  142. {
  143. $this->db->rollback();
  144. return -1;
  145. }
  146. }
  147. /**
  148. * Update a project
  149. *
  150. * @param User $user User object of making update
  151. * @param int $notrigger 1=Disable all triggers
  152. * @return int
  153. */
  154. function update($user, $notrigger=0)
  155. {
  156. global $langs, $conf;
  157. // Clean parameters
  158. $this->title = trim($this->title);
  159. $this->description = trim($this->description);
  160. if (dol_strlen(trim($this->ref)) > 0)
  161. {
  162. $sql = "UPDATE " . MAIN_DB_PREFIX . "projet SET";
  163. $sql.= " ref='" . $this->ref . "'";
  164. $sql.= ", title = '" . $this->db->escape($this->title) . "'";
  165. $sql.= ", description = '" . $this->db->escape($this->description) . "'";
  166. $sql.= ", fk_soc = " . ($this->socid > 0 ? $this->socid : "null");
  167. $sql.= ", fk_statut = " . $this->statut;
  168. $sql.= ", public = " . ($this->public ? 1 : 0);
  169. $sql.= ", datec=" . ($this->date_c != '' ? $this->db->idate($this->date_c) : 'null');
  170. $sql.= ", dateo=" . ($this->date_start != '' ? $this->db->idate($this->date_start) : 'null');
  171. $sql.= ", datee=" . ($this->date_end != '' ? $this->db->idate($this->date_end) : 'null');
  172. $sql.= " WHERE rowid = " . $this->id;
  173. dol_syslog("Project::Update sql=" . $sql, LOG_DEBUG);
  174. if ($this->db->query($sql))
  175. {
  176. if (!$notrigger)
  177. {
  178. // Call triggers
  179. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  180. $interface = new Interfaces($this->db);
  181. $result = $interface->run_triggers('PROJECT_MODIFY', $this, $user, $langs, $conf);
  182. if ($result < 0)
  183. {
  184. $error++;
  185. $this->errors = $interface->errors;
  186. }
  187. // End call triggers
  188. }
  189. $result = 1;
  190. }
  191. else
  192. {
  193. $this->error = $this->db->lasterror();
  194. dol_syslog("Project::Update error -2 " . $this->error, LOG_ERR);
  195. $result = -2;
  196. }
  197. }
  198. else
  199. {
  200. dol_syslog("Project::Update ref null");
  201. $result = -1;
  202. }
  203. return $result;
  204. }
  205. /**
  206. * Get object and lines from database
  207. *
  208. * @param int $id Id of object to load
  209. * @param string $ref Ref of project
  210. * @return int >0 if OK, 0 if not found, <0 if KO
  211. */
  212. function fetch($id, $ref='')
  213. {
  214. if (empty($id) && empty($ref)) return -1;
  215. $sql = "SELECT rowid, ref, title, description, public, datec";
  216. $sql.= ", tms, dateo, datee, fk_soc, fk_user_creat, fk_statut, note_private, note_public";
  217. $sql.= " FROM " . MAIN_DB_PREFIX . "projet";
  218. if ($ref) $sql.= " WHERE ref='" . $ref . "'";
  219. else $sql.= " WHERE rowid=" . $id;
  220. dol_syslog("Project::fetch sql=" . $sql, LOG_DEBUG);
  221. $resql = $this->db->query($sql);
  222. if ($resql)
  223. {
  224. if ($this->db->num_rows($resql))
  225. {
  226. $obj = $this->db->fetch_object($resql);
  227. $this->id = $obj->rowid;
  228. $this->ref = $obj->ref;
  229. $this->title = $obj->title;
  230. $this->titre = $obj->title; // TODO deprecated
  231. $this->description = $obj->description;
  232. $this->date_c = $this->db->jdate($obj->datec);
  233. $this->datec = $this->db->jdate($obj->datec); // TODO deprecated
  234. $this->date_m = $this->db->jdate($obj->tms);
  235. $this->datem = $this->db->jdate($obj->tms); // TODO deprecated
  236. $this->date_start = $this->db->jdate($obj->dateo);
  237. $this->date_end = $this->db->jdate($obj->datee);
  238. $this->note_private = $obj->note_private;
  239. $this->note_public = $obj->note_public;
  240. $this->socid = $obj->fk_soc;
  241. $this->societe->id = $obj->fk_soc; // TODO For backward compatibility
  242. $this->user_author_id = $obj->fk_user_creat;
  243. $this->public = $obj->public;
  244. $this->statut = $obj->fk_statut;
  245. $this->db->free($resql);
  246. return 1;
  247. }
  248. else
  249. {
  250. return 0;
  251. }
  252. }
  253. else
  254. {
  255. $this->error = $this->db->lasterror();
  256. dol_syslog("Project::fetch " . $this->error, LOG_ERR);
  257. return -1;
  258. }
  259. }
  260. /**
  261. * Return list of projects
  262. *
  263. * @param int $socid To filter on a particular third party
  264. * @return array List of projects
  265. */
  266. function liste_array($socid='')
  267. {
  268. global $conf;
  269. $projects = array();
  270. $sql = "SELECT rowid, title";
  271. $sql.= " FROM " . MAIN_DB_PREFIX . "projet";
  272. $sql.= " WHERE entity = " . $conf->entity;
  273. if (! empty($socid)) $sql.= " AND fk_soc = " . $socid;
  274. $resql = $this->db->query($sql);
  275. if ($resql)
  276. {
  277. $nump = $this->db->num_rows($resql);
  278. if ($nump)
  279. {
  280. $i = 0;
  281. while ($i < $nump)
  282. {
  283. $obj = $this->db->fetch_object($resql);
  284. $projects[$obj->rowid] = $obj->title;
  285. $i++;
  286. }
  287. }
  288. return $projects;
  289. }
  290. else
  291. {
  292. print $this->db->lasterror();
  293. }
  294. }
  295. /**
  296. * Return list of elements for type linked to project
  297. *
  298. * @param string $type 'propal','order','invoice','order_supplier','invoice_supplier'
  299. * @return array List of orders linked to project, <0 if error
  300. */
  301. function get_element_list($type)
  302. {
  303. $elements = array();
  304. $sql = '';
  305. if ($type == 'propal')
  306. $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "propal WHERE fk_projet=" . $this->id;
  307. if ($type == 'order')
  308. $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "commande WHERE fk_projet=" . $this->id;
  309. if ($type == 'invoice')
  310. $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "facture WHERE fk_projet=" . $this->id;
  311. if ($type == 'invoice_predefined')
  312. $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "facture_rec WHERE fk_projet=" . $this->id;
  313. if ($type == 'order_supplier')
  314. $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "commande_fournisseur WHERE fk_projet=" . $this->id;
  315. if ($type == 'invoice_supplier')
  316. $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "facture_fourn WHERE fk_projet=" . $this->id;
  317. if ($type == 'contract')
  318. $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "contrat WHERE fk_projet=" . $this->id;
  319. if ($type == 'intervention')
  320. $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "fichinter WHERE fk_projet=" . $this->id;
  321. if ($type == 'trip')
  322. $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "deplacement WHERE fk_projet=" . $this->id;
  323. if ($type == 'agenda')
  324. $sql = "SELECT id as rowid FROM " . MAIN_DB_PREFIX . "actioncomm WHERE fk_project=" . $this->id;
  325. if (! $sql) return -1;
  326. //print $sql;
  327. dol_syslog("Project::get_element_list sql=" . $sql);
  328. $result = $this->db->query($sql);
  329. if ($result)
  330. {
  331. $nump = $this->db->num_rows($result);
  332. if ($nump)
  333. {
  334. $i = 0;
  335. while ($i < $nump)
  336. {
  337. $obj = $this->db->fetch_object($result);
  338. $elements[$i] = $obj->rowid;
  339. $i++;
  340. }
  341. $this->db->free($result);
  342. /* Return array */
  343. return $elements;
  344. }
  345. }
  346. else
  347. {
  348. dol_print_error($this->db);
  349. }
  350. }
  351. /**
  352. * Delete a project from database
  353. *
  354. * @param User $user User
  355. * @param int $notrigger Disable triggers
  356. * @return int <0 if KO, 0 if not possible, >0 if OK
  357. */
  358. function delete($user, $notrigger=0)
  359. {
  360. global $langs, $conf;
  361. require_once(DOL_DOCUMENT_ROOT . "/core/lib/files.lib.php");
  362. $error = 0;
  363. $this->db->begin();
  364. if (!$error)
  365. {
  366. // Delete linked contacts
  367. $res = $this->delete_linked_contact();
  368. if ($res < 0)
  369. {
  370. $this->error = 'ErrorFailToDeleteLinkedContact';
  371. //$error++;
  372. $this->db->rollback();
  373. return 0;
  374. }
  375. }
  376. $sql = "DELETE FROM " . MAIN_DB_PREFIX . "projet_task";
  377. $sql.= " WHERE fk_projet=" . $this->id;
  378. dol_syslog(get_class($this) . "::delete sql=" . $sql, LOG_DEBUG);
  379. $resql = $this->db->query($sql);
  380. $sql = "DELETE FROM " . MAIN_DB_PREFIX . "projet";
  381. $sql.= " WHERE rowid=" . $this->id;
  382. dol_syslog(get_class($this) . "::delete sql=" . $sql, LOG_DEBUG);
  383. $resql = $this->db->query($sql);
  384. if ($resql)
  385. {
  386. // We remove directory
  387. $projectref = dol_sanitizeFileName($this->ref);
  388. if ($conf->projet->dir_output)
  389. {
  390. $dir = $conf->projet->dir_output . "/" . $projectref;
  391. $file = $conf->projet->dir_output . "/" . $projectref . "/" . $projectref . ".pdf";
  392. if (file_exists($file))
  393. {
  394. dol_delete_preview($this);
  395. if (!dol_delete_file($file))
  396. {
  397. $this->error = 'ErrorFailToDeleteFile';
  398. $this->db->rollback();
  399. return 0;
  400. }
  401. }
  402. if (file_exists($dir))
  403. {
  404. $res = @dol_delete_dir($dir);
  405. if (!$res)
  406. {
  407. $this->error = 'ErrorFailToDeleteDir';
  408. $this->db->rollback();
  409. return 0;
  410. }
  411. }
  412. }
  413. if (!$notrigger)
  414. {
  415. // Call triggers
  416. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  417. $interface = new Interfaces($this->db);
  418. $result = $interface->run_triggers('PROJECT_DELETE', $this, $user, $langs, $conf);
  419. if ($result < 0)
  420. {
  421. $error++;
  422. $this->errors = $interface->errors;
  423. }
  424. // End call triggers
  425. }
  426. dol_syslog("Project::delete sql=" . $sql, LOG_DEBUG);
  427. $this->db->commit();
  428. return 1;
  429. }
  430. else
  431. {
  432. $this->error = $this->db->lasterror();
  433. dol_syslog("Project::delete " . $this->error, LOG_ERR);
  434. $this->db->rollback();
  435. return -1;
  436. }
  437. }
  438. /**
  439. * Validate a project
  440. *
  441. * @param User $user User that validate
  442. * @return int <0 if KO, >0 if OK
  443. */
  444. function setValid($user)
  445. {
  446. global $langs, $conf;
  447. if ($this->statut != 1)
  448. {
  449. $this->db->begin();
  450. $sql = "UPDATE " . MAIN_DB_PREFIX . "projet";
  451. $sql.= " SET fk_statut = 1";
  452. $sql.= " WHERE rowid = " . $this->id;
  453. $sql.= " AND entity = " . $conf->entity;
  454. dol_syslog("Project::setValid sql=" . $sql);
  455. $resql = $this->db->query($sql);
  456. if ($resql)
  457. {
  458. // Appel des triggers
  459. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  460. $interface = new Interfaces($this->db);
  461. $result = $interface->run_triggers('PROJECT_VALIDATE', $this, $user, $langs, $conf);
  462. if ($result < 0)
  463. {
  464. $error++;
  465. $this->errors = $interface->errors;
  466. }
  467. // Fin appel triggers
  468. if (!$error)
  469. {
  470. $this->db->commit();
  471. return 1;
  472. }
  473. else
  474. {
  475. $this->db->rollback();
  476. $this->error = join(',', $this->errors);
  477. dol_syslog("Project::setValid " . $this->error, LOG_ERR);
  478. return -1;
  479. }
  480. }
  481. else
  482. {
  483. $this->db->rollback();
  484. $this->error = $this->db->lasterror();
  485. dol_syslog("Project::setValid " . $this->error, LOG_ERR);
  486. return -1;
  487. }
  488. }
  489. }
  490. /**
  491. * Close a project
  492. *
  493. * @param User $user User that validate
  494. * @return int <0 if KO, >0 if OK
  495. */
  496. function setClose($user)
  497. {
  498. global $langs, $conf;
  499. if ($this->statut != 2)
  500. {
  501. $this->db->begin();
  502. $sql = "UPDATE " . MAIN_DB_PREFIX . "projet";
  503. $sql.= " SET fk_statut = 2";
  504. $sql.= " WHERE rowid = " . $this->id;
  505. $sql.= " AND entity = " . $conf->entity;
  506. $sql.= " AND fk_statut = 1";
  507. dol_syslog("Project::setClose sql=" . $sql);
  508. $resql = $this->db->query($sql);
  509. if ($resql)
  510. {
  511. // Appel des triggers
  512. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  513. $interface = new Interfaces($this->db);
  514. $result = $interface->run_triggers('PROJECT_CLOSE', $this, $user, $langs, $conf);
  515. if ($result < 0)
  516. {
  517. $error++;
  518. $this->errors = $interface->errors;
  519. }
  520. // Fin appel triggers
  521. if (!$error)
  522. {
  523. $this->db->commit();
  524. return 1;
  525. }
  526. else
  527. {
  528. $this->db->rollback();
  529. $this->error = join(',', $this->errors);
  530. dol_syslog("Project::setClose " . $this->error, LOG_ERR);
  531. return -1;
  532. }
  533. }
  534. else
  535. {
  536. $this->db->rollback();
  537. $this->error = $this->db->lasterror();
  538. dol_syslog("Project::setClose " . $this->error, LOG_ERR);
  539. return -1;
  540. }
  541. }
  542. }
  543. /**
  544. * Return status label of object
  545. *
  546. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  547. * @return string Label
  548. */
  549. function getLibStatut($mode=0)
  550. {
  551. return $this->LibStatut($this->statut, $mode);
  552. }
  553. /**
  554. * Renvoi status label for a status
  555. *
  556. * @param int $statut id statut
  557. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  558. * @return string Label
  559. */
  560. function LibStatut($statut, $mode=0)
  561. {
  562. global $langs;
  563. if ($mode == 0)
  564. {
  565. return $langs->trans($this->statuts[$statut]);
  566. }
  567. if ($mode == 1)
  568. {
  569. return $langs->trans($this->statuts_short[$statut]);
  570. }
  571. if ($mode == 2)
  572. {
  573. if ($statut == 0)
  574. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut0') . ' ' . $langs->trans($this->statuts_short[$statut]);
  575. if ($statut == 1)
  576. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut4') . ' ' . $langs->trans($this->statuts_short[$statut]);
  577. if ($statut == 2)
  578. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut6') . ' ' . $langs->trans($this->statuts_short[$statut]);
  579. }
  580. if ($mode == 3)
  581. {
  582. if ($statut == 0)
  583. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut0');
  584. if ($statut == 1)
  585. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut4');
  586. if ($statut == 2)
  587. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut6');
  588. }
  589. if ($mode == 4)
  590. {
  591. if ($statut == 0)
  592. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut0') . ' ' . $langs->trans($this->statuts_short[$statut]);
  593. if ($statut == 1)
  594. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut4') . ' ' . $langs->trans($this->statuts_short[$statut]);
  595. if ($statut == 2)
  596. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut6') . ' ' . $langs->trans($this->statuts_short[$statut]);
  597. }
  598. if ($mode == 5)
  599. {
  600. if ($statut == 0)
  601. return $langs->trans($this->statuts_short[$statut]) . ' ' . img_picto($langs->trans($this->statuts_short[$statut]), 'statut0');
  602. if ($statut == 1)
  603. return $langs->trans($this->statuts_short[$statut]) . ' ' . img_picto($langs->trans($this->statuts_short[$statut]), 'statut1');
  604. if ($statut == 2)
  605. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut6') . ' ' . $langs->trans($this->statuts_short[$statut]);
  606. }
  607. }
  608. /**
  609. * Renvoie nom clicable (avec eventuellement le picto)
  610. *
  611. * @param int $withpicto 0=Pas de picto, 1=Inclut le picto dans le lien, 2=Picto seul
  612. * @param string $option Variant ('', 'nolink')
  613. * @param int $addlabel 0=Default, 1=Add label into string
  614. * @return string Chaine avec URL
  615. */
  616. function getNomUrl($withpicto=0, $option='', $addlabel=0)
  617. {
  618. global $langs;
  619. $result = '';
  620. $lien = '';
  621. $lienfin = '';
  622. if ($option != 'nolink')
  623. {
  624. $lien = '<a href="' . DOL_URL_ROOT . '/projet/fiche.php?id=' . $this->id . '">';
  625. $lienfin = '</a>';
  626. }
  627. $picto = 'projectpub';
  628. if (!$this->public) $picto = 'project';
  629. $label = $langs->trans("ShowProject") . ': ' . $this->ref . ($this->title ? ' - ' . $this->title : '');
  630. if ($withpicto) $result.=($lien . img_object($label, $picto) . $lienfin);
  631. if ($withpicto && $withpicto != 2) $result.=' ';
  632. if ($withpicto != 2) $result.=$lien . $this->ref . $lienfin . (($addlabel && $this->title) ? ' - ' . $this->title : '');
  633. return $result;
  634. }
  635. /**
  636. * Initialise an instance with random values.
  637. * Used to build previews or test instances.
  638. * id must be 0 if object instance is a specimen.
  639. *
  640. * @return void
  641. */
  642. function initAsSpecimen()
  643. {
  644. global $user, $langs, $conf;
  645. $now = mktime();
  646. // Charge tableau des id de societe socids
  647. $socids = array();
  648. $sql = "SELECT rowid";
  649. $sql.= " FROM " . MAIN_DB_PREFIX . "societe";
  650. $sql.= " WHERE client IN (1, 3)";
  651. $sql.= " AND entity = " . $conf->entity;
  652. $sql.= " LIMIT 10";
  653. $resql = $this->db->query($sql);
  654. if ($resql)
  655. {
  656. $num_socs = $this->db->num_rows($resql);
  657. $i = 0;
  658. while ($i < $num_socs)
  659. {
  660. $i++;
  661. $row = $this->db->fetch_row($resql);
  662. $socids[$i] = $row[0];
  663. }
  664. }
  665. // Charge tableau des produits prodids
  666. $prodids = array();
  667. $sql = "SELECT rowid";
  668. $sql.= " FROM " . MAIN_DB_PREFIX . "product";
  669. $sql.= " WHERE tosell = 1";
  670. $sql.= " AND entity = " . $conf->entity;
  671. $resql = $this->db->query($sql);
  672. if ($resql)
  673. {
  674. $num_prods = $this->db->num_rows($resql);
  675. $i = 0;
  676. while ($i < $num_prods)
  677. {
  678. $i++;
  679. $row = $this->db->fetch_row($resql);
  680. $prodids[$i] = $row[0];
  681. }
  682. }
  683. // Initialise parametres
  684. $this->id = 0;
  685. $this->ref = 'SPECIMEN';
  686. $this->specimen = 1;
  687. $socid = rand(1, $num_socs);
  688. $this->socid = $socids[$socid];
  689. $this->date_c = $now;
  690. $this->date_m = $now;
  691. $this->date_start = $now;
  692. $this->note_public = 'SPECIMEN';
  693. $nbp = rand(1, 9);
  694. $xnbp = 0;
  695. while ($xnbp < $nbp)
  696. {
  697. $line = new Task($this->db);
  698. $line->desc = $langs->trans("Description") . " " . $xnbp;
  699. $line->qty = 1;
  700. $prodid = rand(1, $num_prods);
  701. $line->fk_product = $prodids[$prodid];
  702. $xnbp++;
  703. }
  704. }
  705. /**
  706. * Check if user has read permission on project
  707. *
  708. * @param User $user Object user to evaluate
  709. * @param int $noprint 0=Print forbidden message if no permission, 1=Return -1 if no permission
  710. * @return void
  711. */
  712. function restrictedProjectArea($user, $noprint=0)
  713. {
  714. // To verify role of users
  715. $userAccess = 0;
  716. if ($user->rights->projet->all->lire)
  717. {
  718. $userAccess = 1;
  719. }
  720. else if ($this->public && $user->rights->projet->lire)
  721. {
  722. $userAccess = 1;
  723. }
  724. else
  725. {
  726. foreach (array('internal', 'external') as $source)
  727. {
  728. $userRole = $this->liste_contact(4, $source);
  729. $num = count($userRole);
  730. $nblinks = 0;
  731. while ($nblinks < $num)
  732. {
  733. if (preg_match('/PROJECT/', $userRole[$nblinks]['code']) && $user->id == $userRole[$nblinks]['id'])
  734. {
  735. $userAccess++;
  736. }
  737. $nblinks++;
  738. }
  739. }
  740. //if (empty($nblinks)) // If nobody has permission, we grant creator
  741. //{
  742. // if ((!empty($this->user_author_id) && $this->user_author_id == $user->id))
  743. // {
  744. // $userAccess = 1;
  745. // }
  746. //}
  747. }
  748. if (!$userAccess)
  749. {
  750. if (!$noprint)
  751. {
  752. accessforbidden('', 0);
  753. }
  754. else
  755. {
  756. return -1;
  757. }
  758. }
  759. return $userAccess;
  760. }
  761. /**
  762. * Return array of projects a user has permission on, is affected to, or all projects
  763. *
  764. * @param User $user User object
  765. * @param int $mode 0=All project I have permission on, 1=Projects affected to me only, 2=Will return list of all projects with no test on contacts
  766. * @param int $list 0=Return array,1=Return string list
  767. * @param int $socid 0=No filter on third party, id of third party
  768. * @return array Array of projects
  769. */
  770. function getProjectsAuthorizedForUser($user, $mode=0, $list=0, $socid=0)
  771. {
  772. global $conf;
  773. $projects = array();
  774. $temp = array();
  775. $sql = "SELECT DISTINCT p.rowid, p.ref";
  776. $sql.= " FROM " . MAIN_DB_PREFIX . "projet as p";
  777. if ($mode == 0 || $mode == 1)
  778. {
  779. $sql.= ", " . MAIN_DB_PREFIX . "element_contact as ec";
  780. $sql.= ", " . MAIN_DB_PREFIX . "c_type_contact as ctc";
  781. }
  782. $sql.= " WHERE p.entity = " . $conf->entity;
  783. // Internal users must see project he is contact to even if project linked to a third party he can't see.
  784. //if ($socid || ! $user->rights->societe->client->voir) $sql.= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".$socid.")";
  785. if ($socid)
  786. $sql.= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = " . $socid . ")";
  787. if ($mode == 0)
  788. {
  789. $sql.= " AND ec.element_id = p.rowid AND ( p.public = 1";
  790. //$sql.= " OR p.fk_user_creat = ".$user->id;
  791. $sql.= " OR ( ctc.rowid = ec.fk_c_type_contact";
  792. $sql.= " AND ctc.element = '" . $this->element . "'";
  793. $sql.= " AND ec.fk_socpeople = " . $user->id . " ) )";
  794. }
  795. if ($mode == 1)
  796. {
  797. $sql.= " AND ec.element_id = p.rowid";
  798. $sql.= " AND ctc.rowid = ec.fk_c_type_contact";
  799. $sql.= " AND ctc.element = '" . $this->element . "'";
  800. $sql.= " AND ec.fk_socpeople = " . $user->id;
  801. }
  802. if ($mode == 2)
  803. {
  804. // No filter. Use this if user has permission to see all project
  805. }
  806. //print $sql;
  807. $resql = $this->db->query($sql);
  808. if ($resql)
  809. {
  810. $num = $this->db->num_rows($resql);
  811. $i = 0;
  812. while ($i < $num)
  813. {
  814. $row = $this->db->fetch_row($resql);
  815. $projects[$row[0]] = $row[1];
  816. $temp[] = $row[0];
  817. $i++;
  818. }
  819. $this->db->free($resql);
  820. if ($list)
  821. {
  822. if (empty($temp)) return 0;
  823. $result = implode(',', $temp);
  824. return $result;
  825. }
  826. }
  827. else
  828. {
  829. dol_print_error($this->db);
  830. }
  831. return $projects;
  832. }
  833. }
  834. ?>