dolibarr /htdocs/core/class/interfaces.class.php

Language PHP Lines 296
MD5 Hash 235b7a726bd4282f3db5b1806d6d2d0f
Repository https://github.com/asterix14/dolibarr.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
/* Copyright (C) 2005-2009 Laurent Destailleur  <eldy@users.sourceforge.net>
 * Copyright (C) 2006      Rodolphe Quiedeville <rodolphe@quiedeville.org>
 * Copyright (C) 2010      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/class/interfaces.class.php
 *   \ingroup		core
 *   \brief			Fichier de la classe de gestion des triggers
 */


/**
 *    \class      Interfaces
 *    \brief      Classe de la gestion des triggers
 */

class Interfaces
{
    var $dir;				// Directory with all core and external triggers files
    var $errors	= array();	// Array for errors

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

    /**
     *   \brief      Fonction appelee lors du declenchement d'un evenement Dolibarr.
     *               Cette fonction declenche tous les triggers trouves actifs.
     *   \param      action      Trigger event code
     *   \param      object      Objet concern
     *   \param      user        Objet user
     *   \param      lang        Objet lang
     *   \param      conf        Objet conf
     *   \return     int         Nb of triggers ran if no error, -Nb of triggers with errors otherwise.
     */
    function run_triggers($action,$object,$user,$langs,$conf)
    {
        // Check parameters
        if (! is_object($object) || ! is_object($conf))	// Error
        {
            dol_syslog('interface::run_triggers was called with wrong parameters action='.$action.' object='.is_object($object).' user='.is_object($user).' langs='.is_object($langs).' conf='.is_object($conf), LOG_ERR);
            return -1;
        }
        if (! is_object($user) || ! is_object($langs))	// Warning
        {
            dol_syslog('interface::run_triggers was called with wrong parameters action='.$action.' object='.is_object($object).' user='.is_object($user).' langs='.is_object($langs).' conf='.is_object($conf), LOG_WARNING);
        }

        foreach($conf->triggers_modules as $reldir)
        {
            $dir=dol_buildpath($reldir,0);
            //print "xx".$dir;exit;

            // Check if directory exists
            if (!is_dir($dir)) continue;

            $handle=opendir($dir);
            $modules = array();
            $nbfile = $nbtotal = $nbok = $nbko = 0;
            if (is_resource($handle))
            {
                while (($file = readdir($handle))!==false)
                {
                    if (is_readable($dir."/".$file) && preg_match('/^interface_([^_]+)_(.+)\.class\.php$/i',$file,$reg))
                    {
                        $nbfile++;

                        $modName = "Interface".ucfirst($reg[2]);
                        //print "file=$file"; print "modName=$modName"; exit;
                        if (in_array($modName,$modules))
                        {
                            $langs->load("errors");
                            dol_syslog("Interface::run_triggers action=".$action." ".$langs->trans("ErrorDuplicateTrigger",$modName,"/htdocs/core/triggers/"),LOG_ERR);
                            continue;
                        }

                        // Check if trigger file is disabled by name
                        if (preg_match('/NORUN$/i',$file))
                        {
                            continue;
                        }
                        // Check if trigger file is for a particular module
                        $qualified=true;
                        if (strtolower($reg[1]) != 'all')
                        {
                            $module=preg_replace('/^mod/i','',$reg[1]);
                            $constparam='MAIN_MODULE_'.strtoupper($module);
                            if (empty($conf->global->$constparam)) $qualified=false;
                        }

                        if (! $qualified)
                        {
                            dol_syslog("Interfaces::run_triggers action=".$action." Triggers for file '".$file."' need module to be enabled",LOG_INFO);
                            continue;
                        }

                        include_once($dir."/".$file);
                        $objMod = new $modName($this->db);
                        $i=0;
                        if ($objMod)
                        {
                            // Bypass if workflow module is enabled and if the trigger asked to be disable in such case
                            if (! empty($conf->workflow->enabled) && ! empty($objMod->disabled_if_workflow))
                            {
                                dol_syslog("Interfaces::run_triggers action=".$action." Bypass triggers for file '".$file."'",LOG_INFO);
                                continue;
                            }

                            dol_syslog("Interfaces::run_triggers action=".$action." Launch triggers for file '".$file."'",LOG_INFO);

                            $modules[$i] = $modName;
                            //dol_syslog("Interfaces::run_triggers Launch triggers for file '".$file."'",LOG_INFO);
                            $result=$objMod->run_trigger($action,$object,$user,$langs,$conf);
                            if ($result > 0)
                            {
                                // Action OK
                                $nbtotal++;
                                $nbok++;
                            }
                            if ($result == 0)
                            {
                                // Aucune action faite
                                $nbtotal++;
                            }
                            if ($result < 0)
                            {
                                // Action KO
                                $nbtotal++;
                                $nbko++;
                                $this->errors[]=$objMod->error;
                            }
                            $i++;
                        }
                        else
                        {
                            dol_syslog("Interfaces::run_triggers action=".$action." Failed to instantiate trigger for file '".$file."'",LOG_ERR);
                        }
                    }
                }
                closedir($handle);
            }
        }

        if ($nbko)
        {
            dol_syslog("Interfaces::run_triggers action=".$action." Files found: ".$nbfile.", Files launched: ".$nbtotal.", Done: ".$nbok.", Failed: ".$nbko, LOG_ERR);
            return -$nbko;
        }
        else
        {
            //dol_syslog("Interfaces::run_triggers Files found: ".$nbfile.", Files launched: ".$nbtotal.", Done: ".$nbok.", Failed: ".$nbko, LOG_DEBUG);
            return $nbok;
        }
    }

    /**
     *   Return list of triggers. Function used by admin page htdoc/admin/triggers
     * 	@param		workflow		0=Return all triggers, 1=Return only triggers not disabled if workflow module activated
     * 	@return		array			Array list of triggers
     */
    function getTriggersList($workflow=0)
    {
        global $conf, $langs;

        $form = new Form($this->db);

        $files = array();
        $modules = array();
        $orders = array();
        $i = 0;

        foreach($conf->triggers_modules as $reldir)
        {
            $dir=dol_buildpath($reldir,0);
            //print "xx".$dir;exit;

            // Check if directory exists
            if (!is_dir($dir)) continue;

            $handle=opendir($dir);
            if (is_resource($handle))
            {
                while (($file = readdir($handle))!==false)
                {
                    if (is_readable($dir.'/'.$file) && preg_match('/^interface_([^_]+)_(.+)\.class\.php/',$file,$reg))
                    {
                        $modName = 'Interface'.ucfirst($reg[2]);
                        //print "file=$file"; print "modName=$modName"; exit;
                        if (in_array($modName,$modules))
                        {
                            $langs->load("errors");
                            print '<div class="error">'.$langs->trans("Error").' : '.$langs->trans("ErrorDuplicateTrigger",$modName,"/htdocs/core/triggers/").'</div>';
                            $objMod = new $modName($this->db);

                            $modules[$i] = $modName;
                            $files[$i] = $file;
                            $orders[$i] = $objMod->family;   // Tri par famille
                            $i++;
                        }
                        else
                        {
                            include_once($dir.'/'.$file);
                            $objMod = new $modName($this->db);

                            $modules[$i] = $modName;
                            $files[$i] = $file;
                            $orders[$i] = $objMod->family;   // Tri par famille
                            $i++;
                        }
                    }
                }
                closedir($handle);
            }
        }

        asort($orders);

        $triggers = array();
        $j = 0;

        // Loop on each trigger
        foreach ($orders as $key => $value)
        {
            $modName = $modules[$key];
            if ($modName)
            {
                $objMod = new $modName($this->db);
                // Bypass if workflow module is enabled and if the trigger is compatible
                if ($workflow && ! empty($objMod->disabled_if_workflow)) continue;
            }

            // Define disabledbyname and disabledbymodule
            $disabledbyname=0;
            $disabledbymodule=1;
            $module='';
            if (preg_match('/NORUN$/i',$files[$key])) $disabledbyname=1;
            if (preg_match('/^interface_([^_]+)_(.+)\.class\.php/i',$files[$key],$reg))
            {
                // Check if trigger file is for a particular module
                $module=preg_replace('/^mod/i','',$reg[1]);
                $constparam='MAIN_MODULE_'.strtoupper($module);
                if (strtolower($reg[1]) == 'all') $disabledbymodule=0;
                else if (empty($conf->global->$constparam)) $disabledbymodule=2;
            }

            $triggers[$j]['picto'] = $objMod->picto?img_object('',$objMod->picto):img_object('','generic');
            $triggers[$j]['file'] = $files[$key];
            $triggers[$j]['version'] = $objMod->getVersion();
            $triggers[$j]['status'] = img_picto($langs->trans("Active"),'tick');
            if ($disabledbyname > 0 || $disabledbymodule > 1) $triggers[$j]['status'] = "&nbsp;";

            $text ='<b>'.$langs->trans("Description").':</b><br>';
            $text.=$objMod->getDesc().'<br>';
            $text.='<br><b>'.$langs->trans("Status").':</b><br>';
            if ($disabledbyname == 1)
            {
                $text.=$langs->trans("TriggerDisabledByName").'<br>';
                if ($disabledbymodule == 2) $text.=$langs->trans("TriggerDisabledAsModuleDisabled",$module).'<br>';
            }
            else
            {
                if ($disabledbymodule == 0) $text.=$langs->trans("TriggerAlwaysActive").'<br>';
                if ($disabledbymodule == 1) $text.=$langs->trans("TriggerActiveAsModuleActive",$module).'<br>';
                if ($disabledbymodule == 2) $text.=$langs->trans("TriggerDisabledAsModuleDisabled",$module).'<br>';
            }

            $triggers[$j]['info'] = $form->textwithpicto('',$text);
            $j++;
        }
        return $triggers;
    }

}
?>
Back to Top