Merge pull request #24077 from frederic34/cleanworkstation

clean code
This commit is contained in:
Laurent Destailleur 2023-03-02 14:08:56 +01:00 committed by GitHub
commit 9349b676f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
<?php
/* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2020 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
/* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2020 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
* Copyright (C) 2023 Frédéric France <frederic.france@netlogic.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -24,8 +25,6 @@
// Put here all includes required by your class file
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
//require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
//require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
/**
* Class for Workstation
@ -118,19 +117,30 @@ class Workstation extends CommonObject
public $rowid;
public $ref;
public $label;
public $note_public;
public $note_private;
public $date_creation;
public $tms;
public $fk_user_creat;
public $fk_user_modif;
public $import_key;
/**
* @var int status enabled or disabled
*/
public $status;
public $nb_operators_required;
public $thm_operator_estimated;
public $thm_machine_estimated;
// END MODULEBUILDER PROPERTIES
/**
* @var int[] array of ID
*/
public $resources;
/**
* @var int[] array of ID
*/
public $usergroups;
/**
* Constructor
@ -155,12 +165,6 @@ class Workstation extends CommonObject
$this->fields['entity']['enabled'] = 0;
}
// Example to show how to set values of fields definition dynamically
/*if ($user->rights->workstation->workstation->read) {
$this->fields['myfield']['visible'] = 1;
$this->fields['myfield']['noteditable'] = 0;
}*/
// Unset fields that are disabled
foreach ($this->fields as $key => $val) {
if (isset($val['enabled']) && empty($val['enabled'])) {
@ -511,132 +515,6 @@ class Workstation extends CommonObject
return $this->deleteLineCommon($user, $idline, $notrigger);
}
/**
* Validate object
*
* @param User $user User making status change
* @param int $notrigger 1=Does not execute triggers, 0= execute triggers
* @return int <=0 if OK, 0=Nothing done, >0 if KO
*/
public function validate($user, $notrigger = 0)
{
global $conf, $langs;
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
$error = 0;
// Protection
if ($this->status == self::STATUS_VALIDATED) {
dol_syslog(get_class($this)."::validate action abandonned: already validated", LOG_WARNING);
return 0;
}
/*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->workstation->workstation->write))
|| (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->workstation->workstation->workstation_advance->validate))))
{
$this->error='NotEnoughPermissions';
dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
return -1;
}*/
$now = dol_now();
$this->db->begin();
// Define new ref
if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life
$num = $this->getNextNumRef();
} else {
$num = $this->ref;
}
$this->newref = $num;
if (!empty($num)) {
// Validate
$sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
$sql .= " SET ref = '".$this->db->escape($num)."',";
$sql .= " status = ".self::STATUS_VALIDATED;
if (!empty($this->fields['date_validation'])) {
$sql .= ", date_validation = '".$this->db->idate($now)."'";
}
if (!empty($this->fields['fk_user_valid'])) {
$sql .= ", fk_user_valid = ".$user->id;
}
$sql .= " WHERE rowid = ".((int) $this->id);
dol_syslog(get_class($this)."::validate()", LOG_DEBUG);
$resql = $this->db->query($sql);
if (!$resql) {
dol_print_error($this->db);
$this->error = $this->db->lasterror();
$error++;
}
if (!$error && !$notrigger) {
// Call trigger
$result = $this->call_trigger('WORKSTATION_VALIDATE', $user);
if ($result < 0) {
$error++;
}
// End call triggers
}
}
if (!$error) {
$this->oldref = $this->ref;
// Rename directory if dir was a temporary ref
if (preg_match('/^[\(]?PROV/i', $this->ref)) {
// Now we rename also files into index
$sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref) + 1).")), filepath = 'workstation/".$this->db->escape($this->newref)."'";
$sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'workstation/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
$resql = $this->db->query($sql);
if (!$resql) {
$error++; $this->error = $this->db->lasterror();
}
// We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
$oldref = dol_sanitizeFileName($this->ref);
$newref = dol_sanitizeFileName($num);
$dirsource = $conf->workstation->dir_output.'/workstation/'.$oldref;
$dirdest = $conf->workstation->dir_output.'/workstation/'.$newref;
if (!$error && file_exists($dirsource)) {
dol_syslog(get_class($this)."::validate() rename dir ".$dirsource." into ".$dirdest);
if (@rename($dirsource, $dirdest)) {
dol_syslog("Rename ok");
// Rename docs starting with $oldref with $newref
$listoffiles = dol_dir_list($conf->workstation->dir_output.'/workstation/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
foreach ($listoffiles as $fileentry) {
$dirsource = $fileentry['name'];
$dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
$dirsource = $fileentry['path'].'/'.$dirsource;
$dirdest = $fileentry['path'].'/'.$dirdest;
@rename($dirsource, $dirdest);
}
}
}
}
}
// Set new ref and current status
if (!$error) {
$this->ref = $num;
$this->status = self::STATUS_VALIDATED;
}
if (!$error) {
$this->db->commit();
return 1;
} else {
$this->db->rollback();
return -1;
}
}
/**
* Set draft status
*
@ -877,7 +755,7 @@ class Workstation extends CommonObject
$this->user_creation_id = $obj->fk_user_creat;
$this->user_modification_id = $obj->fk_user_modif;
$this->date_creation = $this->db->jdate($obj->datec);
$this->date_creation = $this->db->jdate($obj->datec);
$this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
}
@ -898,31 +776,6 @@ class Workstation extends CommonObject
$this->initAsSpecimenCommon();
}
/**
* Create an array of lines
*
* @return array|int array of lines if OK, <0 if KO
*/
public function getLinesArray()
{
$this->lines = array();
/*
$objectline = new WorkstationLine($this->db);
$result = $objectline->fetchAll('ASC', 'position', 0, 0, array('customsql'=>'fk_workstation = '.((int) $this->id)));
if (is_numeric($result)) {
$this->error = $objectline->error;
$this->errors = $objectline->errors;
return $result;
}
$this->lines = $result;
*/
return $this->lines;
}
/**
* Returns the reference to the following non used object depending on the active numbering module.
*
@ -1016,34 +869,4 @@ class Workstation extends CommonObject
return $result;
}
/**
* Action executed by scheduler
* CAN BE A CRON TASK. In such a case, parameters come from the schedule job setup field 'Parameters'
* Use public function doScheduledJob($param1, $param2, ...) to get parameters
*
* @return int 0 if OK, <>0 if KO (this function is used also by cron so only 0 is OK)
*/
public function doScheduledJob()
{
global $conf, $langs;
//$conf->global->SYSLOG_FILE = 'DOL_DATA_ROOT/dolibarr_mydedicatedlofile.log';
$error = 0;
$this->output = '';
$this->error = '';
dol_syslog(__METHOD__, LOG_DEBUG);
$now = dol_now();
$this->db->begin();
// ...
$this->db->commit();
return $error;
}
}