Merge pull request #8588 from fappels/develop-ecmfiles

NEW Add triggers on ECM object and add fill src_object_type/id fields
This commit is contained in:
Laurent Destailleur 2018-04-13 23:09:16 +02:00 committed by GitHub
commit 53dd3a3e94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 52 deletions

View File

@ -5792,7 +5792,7 @@ function dol_concatdesc($text1,$text2,$forxml=false)
*/
function getCommonSubstitutionArray($outputlangs, $onlykey=0, $exclude=null, $object=null)
{
global $db, $conf, $mysoc, $user;
global $db, $conf, $mysoc, $user, $extrafields;
$substitutionarray=array();
@ -5964,14 +5964,17 @@ function getCommonSubstitutionArray($outputlangs, $onlykey=0, $exclude=null, $ob
// Create dynamic tags for __EXTRAFIELD_FIELD__
if ($object->table_element && $object->id > 0)
{
if (! is_object($extrafields)) $extrafieldstmp = new ExtraFields($db);
$extralabels = $extrafieldstmp->fetch_name_optionals_label($object->table_element, true);
$object->fetch_optionals();
if (is_array($extrafieldstmp->attributes[$object->table_element]['label']) && count($extrafieldstmp->attributes[$object->table_element]['label']) > 0)
{
foreach ($extrafieldstmp->attributes[$object->table_element]['label'] as $key => $label) {
$substitutionarray['__EXTRAFIELD_' . strtoupper($key) . '__'] = $object->array_options['options_' . $key];
}
if (! is_object($extrafields)) $extrafields = new ExtraFields($db);
$extrafields->fetch_name_optionals_label($object->table_element, true);
if ($object->fetch_optionals() > 0)
{
if (is_array($extrafields->attributes[$object->table_element]['label']) && count($extrafields->attributes[$object->table_element]['label']) > 0)
{
foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $label) {
$substitutionarray['__EXTRAFIELD_' . strtoupper($key) . '__'] = $object->array_options['options_' . $key];
}
}
}
}

View File

@ -3,6 +3,7 @@
* Copyright (C) 2014-2016 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2015 Florian Henry <florian.henry@open-concept.pro>
* Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
* Copyright (C) 2018 Francis Appels <francis.appels@yahoo.com>
* Copyright (C) ---Put here your own copyright and developer email---
*
* This program is free software; you can redistribute it and/or modify
@ -33,7 +34,7 @@ require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
/**
* Class to manage ECM files
*/
class EcmFiles //extends CommonObject
class EcmFiles extends CommonObject
{
/**
* @var string Id to identify managed objects
@ -65,6 +66,8 @@ class EcmFiles //extends CommonObject
public $fk_user_c;
public $fk_user_m;
public $acl;
public $src_object_type;
public $src_object_id;
/**
*/
@ -142,6 +145,9 @@ class EcmFiles //extends CommonObject
if (isset($this->acl)) {
$this->acl = trim($this->acl);
}
if (isset($this->src_object_type)) {
$this->src_object_type = trim($this->src_object_type);
}
if (empty($this->date_c)) $this->date_c = dol_now();
if (empty($this->date_m)) $this->date_m = dol_now();
@ -161,15 +167,27 @@ class EcmFiles //extends CommonObject
$obj = $this->db->fetch_object($resql);
$maxposition = (int) $obj->maxposition;
}
else dol_print_error($this->db);
else
{
$this->errors[] = 'Error ' . $this->db->lasterror();
return --$error;
}
$maxposition=$maxposition+1;
}
else
{
$maxposition=$this->position;
}
$maxposition=$maxposition+1;
// Check parameters
if (empty($this->filename) || empty($this->filepath))
{
$this->errors[] = 'Bad property filename or filepath';
return -1;
return --$error;
}
if (! isset($this->entity))
{
$this->entity = $conf->entity;
}
// Put here code to add control on parameters values
@ -192,12 +210,14 @@ class EcmFiles //extends CommonObject
$sql.= 'date_m,';
$sql.= 'fk_user_c,';
$sql.= 'fk_user_m,';
$sql.= 'acl';
$sql.= 'acl,';
$sql.= 'src_object_type,';
$sql.= 'src_object_id';
$sql .= ') VALUES (';
$sql .= " '".$ref."', ";
$sql .= ' '.(! isset($this->label)?'NULL':"'".$this->db->escape($this->label)."'").',';
$sql .= ' '.(! isset($this->share)?'NULL':"'".$this->db->escape($this->share)."'").',';
$sql .= ' '.(! isset($this->entity)?$conf->entity:$this->entity).',';
$sql .= ' '.$this->entity.',';
$sql .= ' '.(! isset($this->filename)?'NULL':"'".$this->db->escape($this->filename)."'").',';
$sql .= ' '.(! isset($this->filepath)?'NULL':"'".$this->db->escape($this->filepath)."'").',';
$sql .= ' '.(! isset($this->fullpath_orig)?'NULL':"'".$this->db->escape($this->fullpath_orig)."'").',';
@ -211,7 +231,9 @@ class EcmFiles //extends CommonObject
$sql .= ' '.(! isset($this->date_m) || dol_strlen($this->date_m)==0?'NULL':"'".$this->db->idate($this->date_m)."'").',';
$sql .= ' '.(! isset($this->fk_user_c)?$user->id:$this->fk_user_c).',';
$sql .= ' '.(! isset($this->fk_user_m)?'NULL':$this->fk_user_m).',';
$sql .= ' '.(! isset($this->acl)?'NULL':"'".$this->db->escape($this->acl)."'");
$sql .= ' '.(! isset($this->acl)?'NULL':"'".$this->db->escape($this->acl)."'").',';
$sql .= ' '.(! isset($this->src_object_type)?'NULL':"'".$this->db->escape($this->src_object_type)."'").',';
$sql .= ' '.(! isset($this->src_object_id)?'NULL':$this->src_object_id);
$sql .= ')';
$this->db->begin();
@ -227,14 +249,13 @@ class EcmFiles //extends CommonObject
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
$this->position = $maxposition;
if (!$notrigger) {
// Uncomment this and change MYOBJECT to your own tag if you
// want this action to call a trigger.
//// Call triggers
//$result=$this->call_trigger('MYOBJECT_CREATE',$user);
//if ($result < 0) $error++;
//// End call triggers
// Triggers
if (! $notrigger)
{
// Call triggers
$result=$this->call_trigger(strtoupper(get_class($this)).'_CREATE',$user);
if ($result < 0) { $error++; }
// End call triggers
}
}
@ -283,7 +304,9 @@ class EcmFiles //extends CommonObject
$sql .= " t.date_m,";
$sql .= " t.fk_user_c,";
$sql .= " t.fk_user_m,";
$sql .= " t.acl";
$sql .= " t.acl,";
$sql .= " t.src_object_type,";
$sql .= " t.src_object_id";
$sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
$sql.= ' WHERE 1 = 1';
/* Fetching this table depends on filepath+filename, it must not depends on entity
@ -333,12 +356,14 @@ class EcmFiles //extends CommonObject
$this->fk_user_c = $obj->fk_user_c;
$this->fk_user_m = $obj->fk_user_m;
$this->acl = $obj->acl;
$this->src_object_type = $obj->src_object_type;
$this->src_object_id = $obj->src_object_id;
}
// Retrieve all extrafields for invoice
// fetch optionals attributes and labels
// $this->fetch_optionals();
// $this->fetch_lines();
$this->db->free($resql);
@ -390,7 +415,9 @@ class EcmFiles //extends CommonObject
$sql .= " t.date_m,";
$sql .= " t.fk_user_c,";
$sql .= " t.fk_user_m,";
$sql .= " t.acl";
$sql .= " t.acl,";
$sql .= " t.src_object_type,";
$sql .= " t.src_object_id";
$sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element. ' as t';
// Manage filter
@ -443,6 +470,9 @@ class EcmFiles //extends CommonObject
$line->fk_user_c = $obj->fk_user_c;
$line->fk_user_m = $obj->fk_user_m;
$line->acl = $obj->acl;
$line->src_object_type = $obj->src_object_type;
$line->src_object_id = $obj->src_object_id;
$this->lines[] = $line;
}
$this->db->free($resql);
@ -465,6 +495,8 @@ class EcmFiles //extends CommonObject
*/
public function update(User $user, $notrigger = false)
{
global $conf;
$error = 0;
dol_syslog(__METHOD__, LOG_DEBUG);
@ -507,16 +539,15 @@ class EcmFiles //extends CommonObject
if (isset($this->extraparams)) {
$this->extraparams = trim($this->extraparams);
}
if (isset($this->fk_user_c)) {
$this->fk_user_c = trim($this->fk_user_c);
}
if (isset($this->fk_user_m)) {
$this->fk_user_m = trim($this->fk_user_m);
}
if (isset($this->acl)) {
$this->acl = trim($this->acl);
}
if (isset($this->src_object_type)) {
$this->src_object_type = trim($this->src_object_type);
}
// Check parameters
// Put here code to add a control on parameters values
@ -538,9 +569,10 @@ class EcmFiles //extends CommonObject
$sql .= ' extraparams = '.(isset($this->extraparams)?"'".$this->db->escape($this->extraparams)."'":"null").',';
$sql .= ' date_c = '.(! isset($this->date_c) || dol_strlen($this->date_c) != 0 ? "'".$this->db->idate($this->date_c)."'" : 'null').',';
//$sql .= ' date_m = '.(! isset($this->date_m) || dol_strlen($this->date_m) != 0 ? "'".$this->db->idate($this->date_m)."'" : 'null').','; // Field automatically updated
$sql .= ' fk_user_c = '.(isset($this->fk_user_c)?$this->fk_user_c:"null").',';
$sql .= ' fk_user_m = '.($this->fk_user_m > 0?$this->fk_user_m:$user->id).',';
$sql .= ' acl = '.(isset($this->acl)?"'".$this->db->escape($this->acl)."'":"null");
$sql .= ' acl = '.(isset($this->acl)?"'".$this->db->escape($this->acl)."'":"null").',';
$sql .= ' src_object_id = '.($this->src_object_id > 0?$this->src_object_id:"null").',';
$sql .= ' src_object_type = '.(isset($this->src_object_type)?"'".$this->db->escape($this->src_object_type)."'":"null");
$sql .= ' WHERE rowid=' . $this->id;
$this->db->begin();
@ -552,14 +584,13 @@ class EcmFiles //extends CommonObject
dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
}
if (!$error && !$notrigger) {
// Uncomment this and change MYOBJECT to your own tag if you
// want this action calls a trigger.
//// Call triggers
//$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
//if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
//// End call triggers
// Triggers
if (! $error && ! $notrigger)
{
// Call triggers
$result=$this->call_trigger(strtoupper(get_class($this)).'_MODIFY',$user);
if ($result < 0) { $error++; } //Do also here what you must do to rollback action if trigger fail
// End call triggers
}
// Commit or rollback
@ -590,16 +621,13 @@ class EcmFiles //extends CommonObject
$this->db->begin();
if (!$error) {
if (!$notrigger) {
// Uncomment this and change MYOBJECT to your own tag if you
// want this action calls a trigger.
//// Call triggers
//$result=$this->call_trigger('MYOBJECT_DELETE',$user);
//if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
//// End call triggers
}
// Triggers
if (! $notrigger)
{
// Call triggers
$result=$this->call_trigger(strtoupper(get_class($this)).'_DELETE',$user);
if ($result < 0) { $error++; } //Do also here what you must do to rollback action if trigger fail
// End call triggers
}
// If you need to delete child tables to, you can insert them here
@ -781,8 +809,9 @@ class EcmFiles //extends CommonObject
$this->fk_user_c = $user->id;
$this->fk_user_m = '';
$this->acl = '';
$this->src_object_type = 'product';
$this->src_object_id = 1;
}
}
@ -804,4 +833,6 @@ class EcmfilesLine
public $fk_user_c;
public $fk_user_m;
public $acl;
public $src_object_type;
public $src_object_id;
}