19 use Luracast\Restler\RestException;
 
   21 require_once DOL_DOCUMENT_ROOT.
'/projet/class/project.class.php';
 
   22 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
 
   35  public static $FIELDS = array(
 
   58    $this->project = 
new Project($this->db);
 
   59    $this->task = 
new Task($this->db);
 
 
   72  public function get($id)
 
   74    if (!DolibarrApiAccess::$user->rights->projet->lire) {
 
   75      throw new RestException(401);
 
   78    $result = $this->project->fetch($id);
 
   80      throw new RestException(404, 
'Project not found');
 
   84      throw new RestException(401, 
'Access not allowed for login '.DolibarrApiAccess::$user->login);
 
   87    $this->project->fetchObjectLinked();
 
 
  108  public function index($sortfield = 
"t.rowid", $sortorder = 
'ASC', $limit = 100, $page = 0, $thirdparty_ids = 
'', $category = 0, $sqlfilters = 
'', $properties = 
'')
 
  110    if (!DolibarrApiAccess::$user->rights->projet->lire) {
 
  111      throw new RestException(401);
 
  117    $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
 
  121    if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
 
  122      $search_sale = DolibarrApiAccess::$user->id;
 
  125    $sql = 
"SELECT t.rowid";
 
  126    if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
 
  127      $sql .= 
", sc.fk_soc, sc.fk_user"; 
 
  129    $sql .= 
" FROM ".MAIN_DB_PREFIX.
"projet as t";
 
  130    $sql .= 
" LEFT JOIN ".MAIN_DB_PREFIX.
"projet_extrafields AS ef ON ef.fk_object = t.rowid";  
 
  132      $sql .= 
", ".MAIN_DB_PREFIX.
"categorie_project as c";
 
  134    if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
 
  135      $sql .= 
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc"; 
 
  138    $sql .= 
' WHERE t.entity IN ('.getEntity(
'project').
')';
 
  139    if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
 
  140      $sql .= 
" AND t.fk_soc = sc.fk_soc";
 
  143      $sql .= 
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
 
  145    if ($search_sale > 0) {
 
  146      $sql .= 
" AND t.rowid = sc.fk_soc"; 
 
  149    if ($search_sale > 0) {
 
  150      $sql .= 
" AND sc.fk_user = ".((int) $search_sale);
 
  154      $sql .= 
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_project = t.rowid ";
 
  161        throw new RestException(400, 
'Error when validating parameter sqlfilters -> '.$errormessage);
 
  165    $sql .= $this->db->order($sortfield, $sortorder);
 
  170      $offset = $limit * $page;
 
  172      $sql .= $this->db->plimit($limit + 1, $offset);
 
  176    $result = $this->db->query($sql);
 
  179      $num = $this->db->num_rows($result);
 
  180      $min = min($num, ($limit <= 0 ? $num : $limit));
 
  183        $obj = $this->db->fetch_object($result);
 
  184        $project_static = 
new Project($this->db);
 
  185        if ($project_static->fetch($obj->rowid)) {
 
  191      throw new RestException(503, 
'Error when retrieve project list : '.$this->db->lasterror());
 
 
  203  public function post($request_data = 
null)
 
  205    if (!DolibarrApiAccess::$user->rights->projet->creer) {
 
  206      throw new RestException(401, 
"Insuffisant rights");
 
  209    $result = $this->
_validate($request_data);
 
  211    foreach ($request_data as $field => $value) {
 
  212      if ($field === 
'caller') {
 
  214        $this->project->context[
'caller'] = $request_data[
'caller'];
 
  218      $this->project->$field = $value;
 
  227    if ($this->project->create(DolibarrApiAccess::$user) < 0) {
 
  228      throw new RestException(500, 
"Error creating project", array_merge(array($this->project->error), $this->project->errors));
 
  231    return $this->project->id;
 
 
  244  public function getLines($id, $includetimespent = 0)
 
  246    if (!DolibarrApiAccess::$user->rights->projet->lire) {
 
  247      throw new RestException(401);
 
  250    $result = $this->project->fetch($id);
 
  252      throw new RestException(404, 
'Project not found');
 
  256      throw new RestException(401, 
'Access not allowed for login '.DolibarrApiAccess::$user->login);
 
  258    $this->project->getLinesArray(DolibarrApiAccess::$user);
 
  260    foreach ($this->project->lines as $line) {      
 
  261      if ($includetimespent == 1) {
 
  262        $timespent = $line->getSummaryOfTimeSpent(0);
 
  264      if ($includetimespent == 2) {
 
  265        $timespent = $line->fetchTimeSpentOnTask();
 
 
  286    if (!DolibarrApiAccess::$user->rights->projet->lire) {
 
  287      throw new RestException(401);
 
  290    $result = $this->project->fetch($id);
 
  292      throw new RestException(404, 
'Project not found');
 
  296      throw new RestException(401, 
'Access not allowed for login '.DolibarrApiAccess::$user->login);
 
  299    require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
 
  300    $taskstatic = 
new Task($this->db);
 
  301    $userp = DolibarrApiAccess::$user;
 
  303      $userp = 
new User($this->db);
 
  304      $userp->fetch($userid);
 
  306    $this->project->roles = $taskstatic->getUserRolesForProjectsOrTasks($userp, 
null, $id, 0);
 
  308    foreach ($this->project->roles as $line) {
 
 
  455  public function put($id, $request_data = 
null)
 
  457    if (!DolibarrApiAccess::$user->rights->projet->creer) {
 
  458      throw new RestException(401);
 
  461    $result = $this->project->fetch($id);
 
  463      throw new RestException(404, 
'Project not found');
 
  467      throw new RestException(401, 
'Access not allowed for login '.DolibarrApiAccess::$user->login);
 
  469    foreach ($request_data as $field => $value) {
 
  470      if ($field == 
'id') {
 
  473      if ($field === 
'caller') {
 
  475        $this->project->context[
'caller'] = $request_data[
'caller'];
 
  479      if ($field == 
'array_options' && is_array($value)) {
 
  480        foreach ($value as $index => $val) {
 
  481          $this->project->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->project);
 
  485      $this->project->$field = $this->
_checkValForAPI($field, $value, $this->project);
 
  488    if ($this->project->update(DolibarrApiAccess::$user) >= 0) {
 
  489      return $this->
get($id);
 
  491      throw new RestException(500, $this->project->error);
 
 
  502  public function delete($id)
 
  504    if (!DolibarrApiAccess::$user->rights->projet->supprimer) {
 
  505      throw new RestException(401);
 
  507    $result = $this->project->fetch($id);
 
  509      throw new RestException(404, 
'Project not found');
 
  513      throw new RestException(401, 
'Access not allowed for login '.DolibarrApiAccess::$user->login);
 
  516    if (!$this->project->delete(DolibarrApiAccess::$user)) {
 
  517      throw new RestException(500, 
'Error when delete project : '.$this->project->error);
 
  523        'message' => 
'Project deleted' 
 
  548    if (!DolibarrApiAccess::$user->rights->projet->creer) {
 
  549      throw new RestException(401);
 
  551    $result = $this->project->fetch($id);
 
  553      throw new RestException(404, 
'Project not found');
 
  557      throw new RestException(401, 
'Access not allowed for login '.DolibarrApiAccess::$user->login);
 
  560    $result = $this->project->setValid(DolibarrApiAccess::$user, $notrigger);
 
  562      throw new RestException(304, 
'Error nothing done. May be object is already validated');
 
  565      throw new RestException(500, 
'Error when validating Project: '.$this->project->error);
 
  571        'message' => 
'Project validated' 
 
  587    $object = parent::_cleanObjectDatas($object);
 
  589    unset($object->datec);
 
  590    unset($object->datem);
 
  591    unset($object->barcode_type);
 
  592    unset($object->barcode_type_code);
 
  593    unset($object->barcode_type_label);
 
  594    unset($object->barcode_type_coder);
 
  595    unset($object->cond_reglement_id);
 
  596    unset($object->cond_reglement);
 
  597    unset($object->fk_delivery_address);
 
  598    unset($object->shipping_method_id);
 
  599    unset($object->fk_account);
 
  600    unset($object->note);
 
  601    unset($object->fk_incoterms);
 
  602    unset($object->label_incoterms);
 
  603    unset($object->location_incoterms);
 
  604    unset($object->name);
 
  605    unset($object->lastname);
 
  606    unset($object->firstname);
 
  607    unset($object->civility_id);
 
  608    unset($object->mode_reglement_id);
 
  609    unset($object->country);
 
  610    unset($object->country_id);
 
  611    unset($object->country_code);
 
  613    unset($object->weekWorkLoad);
 
  614    unset($object->weekWorkLoad);
 
  618    unset($object->total_ht);
 
  619    unset($object->total_tva);
 
  620    unset($object->total_localtax1);
 
  621    unset($object->total_localtax2);
 
  622    unset($object->total_ttc);
 
  624    unset($object->comments);
 
 
  639    foreach (self::$FIELDS as $field) {
 
  640      if (!isset($data[$field])) {
 
  641        throw new RestException(400, 
"$field field missing");
 
  643      $object[$field] = $data[$field];
 
 
 
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
 
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
 
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
 
Class to manage projects.
 
post($request_data=null)
Create project object.
 
getLines($id, $includetimespent=0)
Get tasks of a project.
 
validate($id, $notrigger=0)
Validate a project.
 
_validate($data)
Validate fields before create or update object.
 
_cleanObjectDatas($object)
Clean sensible object datas.
 
__construct()
Constructor.
 
put($id, $request_data=null)
Add a task to given project.
 
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $category=0, $sqlfilters='', $properties='')
List projects.
 
getRoles($id, $userid=0)
Get roles a user is assigned to a project with.
 
Class to manage Dolibarr users.
 
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
 
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.