Skip to content

Commit

Permalink
New: Trigger now have a priority to define sort execution order.
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Jan 31, 2012
1 parent 0011b1d commit bf8c022
Show file tree
Hide file tree
Showing 7 changed files with 2,450 additions and 0 deletions.
256 changes: 256 additions & 0 deletions htdocs/core/triggers/interface_20_all_Logevents.class.php
@@ -0,0 +1,256 @@
<?php
/* Copyright (C) 2005-2009 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2009 Regis Houssin <regis@dolibarr.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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* \file htdocs/core/triggers/interface_20_all_Logevents.class.php
* \ingroup core
* \brief Trigger file for
*/


/**
* \class InterfaceLogevents
* \brief Class of triggers for security events
*/
class InterfaceLogevents
{
var $db;
var $error;

var $date;
var $duree;
var $texte;
var $desc;

/**
* Constructor
*
* @param DoliDB $db Database handler
*/
function InterfaceLogevents($db)
{
$this->db = $db;

$this->name = preg_replace('/^Interface/i','',get_class($this));
$this->family = "core";
$this->description = "Triggers of this module allows to add security event records inside Dolibarr.";
$this->version = 'dolibarr'; // 'experimental' or 'dolibarr' or version
$this->picto = 'technic';
}

/**
* Return name of trigger file
* @return string Name of trigger file
*/
function getName()
{
return $this->name;
}

/**
* Return description of trigger file
* @return string Description of trigger file
*/
function getDesc()
{
return $this->description;
}

/**
* Return version of trigger file
* @return string Version of trigger file
*/
function getVersion()
{
global $langs;
$langs->load("admin");

if ($this->version == 'experimental') return $langs->trans("Experimental");
elseif ($this->version == 'dolibarr') return DOL_VERSION;
elseif ($this->version) return $this->version;
else return $langs->trans("Unknown");
}

/**
* Function called when a Dolibarrr business event is done.
* All functions "run_trigger" are triggered if file is inside directory htdocs/core/triggers
* @param action Code de l'evenement
* @param object Objet concerne
* @param user Objet user
* @param langs Objet langs
* @param conf Objet conf
* @param entity Value for instance of data (Always 1 except if module MultiCompany is installed)
* @return int <0 if KO, 0 if no triggered ran, >0 if OK
*/
function run_trigger($action,$object,$user,$langs,$conf,$entity=1)
{
if (! empty($conf->global->MAIN_LOGEVENTS_DISABLE_ALL)) return 0; // Log events is disabled (hidden features)

$key='MAIN_LOGEVENTS_'.$action;
//dol_syslog("xxxxxxxxxxx".$key);
if (empty($conf->global->$key)) return 0; // Log events not enabled for this action

if (empty($conf->entity)) $conf->entity = $entity; // forcing of the entity if it's not defined (ex: in login form)

$this->date=gmmktime();
$this->duree=0;

// Actions
if ($action == 'USER_LOGIN')
{
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);

// Initialisation donnees (date,duree,texte,desc)
$this->texte="(UserLogged,".$object->login.")";
$this->desc="(UserLogged,".$object->login.")";
}
if ($action == 'USER_LOGIN_FAILED')
{
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);

// Initialisation donnees (date,duree,texte,desc)
$this->texte=$object->trigger_mesg; // Message direct
$this->desc=$object->trigger_mesg; // Message direct
}
if ($action == 'USER_LOGOUT')
{
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);

// Initialisation donnees (date,duree,texte,desc)
$this->texte="(UserLogoff,".$object->login.")";
$this->desc="(UserLogoff,".$object->login.")";
}
if ($action == 'USER_CREATE')
{
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
$langs->load("users");

// Initialisation donnees (date,duree,texte,desc)
$this->texte=$langs->transnoentities("NewUserCreated",$object->login);
$this->desc=$langs->transnoentities("NewUserCreated",$object->login);
}
elseif ($action == 'USER_MODIFY')
{
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
$langs->load("users");

// Initialisation donnees (date,duree,texte,desc)
$this->texte=$langs->transnoentities("EventUserModified",$object->login);
$this->desc=$langs->transnoentities("EventUserModified",$object->login);
}
elseif ($action == 'USER_NEW_PASSWORD')
{
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
$langs->load("users");

// Initialisation donnees (date,duree,texte,desc)
$this->texte=$langs->transnoentities("NewUserPassword",$object->login);
$this->desc=$langs->transnoentities("NewUserPassword",$object->login);
}
elseif ($action == 'USER_ENABLEDISABLE')
{
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
$langs->load("users");
// Initialisation donnees (date,duree,texte,desc)
if ($object->statut == 0)
{
$this->texte=$langs->transnoentities("UserEnabled",$object->login);
$this->desc=$langs->transnoentities("UserEnabled",$object->login);
}
if ($object->statut == 1)
{
$this->texte=$langs->transnoentities("UserDisabled",$object->login);
$this->desc=$langs->transnoentities("UserDisabled",$object->login);
}
}
elseif ($action == 'USER_DELETE')
{
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
$langs->load("users");
// Initialisation donnees (date,duree,texte,desc)
$this->texte=$langs->transnoentities("UserDeleted",$object->login);
$this->desc=$langs->transnoentities("UserDeleted",$object->login);
}

// Groupes
elseif ($action == 'GROUP_CREATE')
{
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
$langs->load("users");
// Initialisation donnees (date,duree,texte,desc)
$this->texte=$langs->transnoentities("NewGroupCreated",$object->nom);
$this->desc=$langs->transnoentities("NewGroupCreated",$object->nom);
}
elseif ($action == 'GROUP_MODIFY')
{
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
$langs->load("users");
// Initialisation donnees (date,duree,texte,desc)
$this->texte=$langs->transnoentities("GroupModified",$object->nom);
$this->desc=$langs->transnoentities("GroupModified",$object->nom);
}
elseif ($action == 'GROUP_DELETE')
{
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
$langs->load("users");
// Initialisation donnees (date,duree,texte,desc)
$this->texte=$langs->transnoentities("GroupDeleted",$object->nom);
$this->desc=$langs->transnoentities("GroupDeleted",$object->nom);
}

// If not found
/*
else
{
dol_syslog("Trigger '".$this->name."' for action '$action' was ran by ".__FILE__." but no handler found for this action.");
return 0;
}
*/

// Add entry in event table
if ($this->date)
{
include_once(DOL_DOCUMENT_ROOT.'/core/class/events.class.php');

$event=new Events($this->db);
$event->type=$action;
$event->dateevent=$this->date;
$event->label=$this->texte;
$event->description=$this->desc;
$event->user_agent=$_SERVER["HTTP_USER_AGENT"];

$result=$event->create($user);
if ($result > 0)
{
return 1;
}
else
{
$error ="Failed to insert security event: ".$event->error;
$this->error=$error;

dol_syslog(get_class($this).": ".$this->error, LOG_ERR);
return -1;
}
}

return 0;
}

}
?>
139 changes: 139 additions & 0 deletions htdocs/core/triggers/interface_20_modPaypal_PaypalWorkflow.class.php
@@ -0,0 +1,139 @@
<?php
/* Copyright (C) 2011 Regis Houssin <regis@dolibarr.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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* \file /htdocs/core/triggers/interface_20_modPaypal_PaypalWorkflow.class.php
* \ingroup paypal
* \brief Trigger file for paypal workflow
*/


/**
* \class InterfacePaypalWorkflow
* \brief Class of triggers for paypal module
*/
class InterfacePaypalWorkflow
{
var $db;

/**
* Constructor
*
* @param DoliDB $db Database handler
*/
function InterfacePaypalWorkflow($db)
{
$this->db = $db;

$this->name = preg_replace('/^Interface/i','',get_class($this));
$this->family = "paypal";
$this->description = "Triggers of this module allows to manage paypal workflow";
$this->version = 'dolibarr'; // 'development', 'experimental', 'dolibarr' or version
$this->picto = 'paypal@paypal';
}


/**
* \brief Renvoi nom du lot de triggers
* \return string Nom du lot de triggers
*/
function getName()
{
return $this->name;
}

/**
* \brief Renvoi descriptif du lot de triggers
* \return string Descriptif du lot de triggers
*/
function getDesc()
{
return $this->description;
}

/**
* \brief Renvoi version du lot de triggers
* \return string Version du lot de triggers
*/
function getVersion()
{
global $langs;
$langs->load("admin");

if ($this->version == 'development') return $langs->trans("Development");
elseif ($this->version == 'experimental') return $langs->trans("Experimental");
elseif ($this->version == 'dolibarr') return DOL_VERSION;
elseif ($this->version) return $this->version;
else return $langs->trans("Unknown");
}

/**
* Fonction appelee lors du declenchement d'un evenement Dolibarr.
* D'autres fonctions run_trigger peuvent etre presentes dans core/triggers
*
* @param string $action Code de l'evenement
* @param CommonObject $object Objet concerne
* @param User $user Objet user
* @param Translate $lang Objet lang
* @param Conf $conf Objet conf
* @return int <0 if fatal error, 0 si nothing done, >0 if ok
*/
function run_trigger($action,$object,$user,$langs,$conf)
{
// Mettre ici le code a executer en reaction de l'action
// Les donnees de l'action sont stockees dans $object

if ($action == 'PAYPAL_PAYMENT_OK')
{
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". source=".$object->source." ref=".$object->ref);

require_once(DOL_DOCUMENT_ROOT."/societe/class/societe.class.php");

$soc = new Societe($this->db);

// Parse element/subelement (ex: project_task)
$element = $path = $filename = $object->source;
if (preg_match('/^([^_]+)_([^_]+)/i',$object->source,$regs))
{
$element = $path = $regs[1];
$filename = $regs[2];
}
// For compatibility
if ($element == 'order') { $path = $filename = 'commande'; }
if ($element == 'invoice') { $path = 'compta/facture'; $filename = 'facture'; }

dol_include_once('/'.$path.'/class/'.$filename.'.class.php');

$classname = ucfirst($filename);
$obj = new $classname($this->db);

$ret = $obj->fetch('',$object->ref);
if ($ret < 0) return -1;

// Add payer id
$soc->setValueFrom('ref_int', $object->payerID, 'societe', $obj->socid);

// Add transaction id
$obj->setValueFrom('ref_int',$object->resArray["TRANSACTIONID"]);

}

return 0;
}

}
?>

0 comments on commit bf8c022

Please sign in to comment.