PageRenderTime 233ms CodeModel.GetById 82ms app.highlight 58ms RepoModel.GetById 82ms app.codeStats 1ms

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