Passed
Push — EXTRACT_CLASSES ( c25e41...9f3ede )
by Rafael
55:18
created

EmailCollectorAction::initAsSpecimen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* Copyright (C) 2017       Laurent Destailleur         <[email protected]>
4
 * Copyright (C) 2024       Frédéric France             <[email protected]>
5
 * Copyright (C) 2024		MDW							<[email protected]>
6
 * Copyright (C) 2024       Rafael San José             <[email protected]>
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace Dolibarr\Code\EmailCollector\Classes;
23
24
use Dolibarr\Core\Base\CommonObject;
25
26
/**
27
 * \file        class/emailcollectoraction.class.php
28
 * \ingroup     emailcollector
29
 * \brief       This file is a CRUD class file for EmailCollectorAction (Create/Read/Update/Delete)
30
 */
31
32
/**
33
 * Class for EmailCollectorAction
34
 */
35
class EmailCollectorAction extends CommonObject
36
{
37
    /**
38
     * @var string ID to identify managed object
39
     */
40
    public $element = 'emailcollectoraction';
41
42
    /**
43
     * @var string Name of table without prefix where object is stored
44
     */
45
    public $table_element = 'emailcollector_emailcollectoraction';
46
47
    /**
48
     * @var string String with name of icon for emailcollectoraction. Must be the part after the 'object_' into object_emailcollectoraction.png
49
     */
50
    public $picto = 'emailcollectoraction@emailcollector';
51
52
53
    /**
54
     *  'type' if the field format.
55
     *  'label' the translation key.
56
     *  'enabled' is a condition when the field must be managed.
57
     *  'visible' says if field is visible in list (Examples: 0=Not visible, 1=Visible on list and create/update/view forms, 2=Visible on list only. Using a negative value means field is not shown by default on list but can be selected for viewing)
58
     *  'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
59
     *  'default' is a default value for creation (can still be replaced by the global setup of default values)
60
     *  'index' if we want an index in database.
61
     *  'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommended to name the field fk_...).
62
     *  'position' is the sort order of field.
63
     *  'searchall' is 1 if we want to search in this field when making a search from the quick search button.
64
     *  'isameasure' must be set to 1 if you want to have a total on list for this field. Field type must be summable like integer or double(24,8).
65
     *  'css' is the CSS style to use on field. For example: 'maxwidth200'
66
     *  'help' is a string visible as a tooltip on field
67
     *  'comment' is not used. You can store here any text of your choice. It is not used by application.
68
     *  'showoncombobox' if value of the field must be visible into the label of the combobox that list record
69
     *  'arrayofkeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")
70
     */
71
72
    // BEGIN MODULEBUILDER PROPERTIES
73
    /**
74
     * @var array<string,array{type:string,label:string,enabled:int<0,2>|string,position:int,notnull?:int,visible:int,noteditable?:int,default?:string,index?:int,foreignkey?:string,searchall?:int,isameasure?:int,css?:string,csslist?:string,help?:string,showoncombobox?:int,disabled?:int,arrayofkeyval?:array<int,string>,comment?:string}>  Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string,array{type:...ring>,comment?:string}> at position 16 could not be parsed: Expected '}' at position 16, but found 'int'.
Loading history...
75
     */
76
    public $fields = array(
77
        'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'position' => 1, 'notnull' => 1, 'index' => 1, 'comment' => "Id",),
78
        'fk_emailcollector' => array('type' => 'integer', 'label' => 'Id of emailcollector', 'foreignkey' => 'emailcollector.rowid'),
79
        'type' => array('type' => 'varchar(128)', 'label' => 'Type', 'enabled' => 1, 'visible' => 1, 'position' => 10, 'notnull' => 1, 'index' => 1),
80
        'actionparam' => array('type' => 'text', 'label' => 'ParamForAction', 'enabled' => 1, 'visible' => 1, 'position' => 40, 'notnull' => -1),
81
        'date_creation' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -2, 'position' => 500, 'notnull' => 1,),
82
        'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -2, 'position' => 501, 'notnull' => 1,),
83
        'fk_user_creat' => array('type' => 'integer', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => -2, 'position' => 510, 'notnull' => 1, 'foreignkey' => 'llx_user.rowid',),
84
        'fk_user_modif' => array('type' => 'integer', 'label' => 'UserModif', 'enabled' => 1, 'visible' => -2, 'position' => 511, 'notnull' => -1,),
85
        'position' => array('type' => 'integer', 'label' => 'Position', 'enabled' => 1, 'visible' => 1, 'position' => 600, 'default' => '0',),
86
        'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -2, 'position' => 1000, 'notnull' => -1,),
87
        'status' => array('type' => 'integer', 'label' => 'Status', 'enabled' => 1, 'visible' => 1, 'position' => 1000, 'notnull' => 1, 'default' => 1, 'arrayofkeyval' => array('0' => 'Disabled', '1' => 'Enabled')),
88
    );
89
    public $rowid;
90
    public $fk_emailcollector;
91
    public $type;
92
    public $actionparam;
93
94
    /**
95
     * @var integer|string date_creation
96
     */
97
    public $date_creation;
98
99
    public $fk_user_creat;
100
    public $fk_user_modif;
101
    public $position;
102
    public $import_key;
103
    public $status;
104
    // END MODULEBUILDER PROPERTIES
105
106
    /**
107
     * Constructor
108
     *
109
     * @param DoliDB $db Database handler
110
     */
111
    public function __construct(DoliDB $db)
0 ignored issues
show
Bug introduced by
The type Dolibarr\Code\EmailCollector\Classes\DoliDB was not found. Did you mean DoliDB? If so, make sure to prefix the type with \.
Loading history...
112
    {
113
        global $conf, $langs;
114
115
        $this->db = $db;
116
117
        $this->ismultientitymanaged = 0;
118
        $this->isextrafieldmanaged = 0;
119
120
        if (!getDolGlobalString('MAIN_SHOW_TECHNICAL_ID') && isset($this->fields['rowid'])) {
121
            $this->fields['rowid']['visible'] = 0;
122
        }
123
        if (!isModEnabled('multicompany') && isset($this->fields['entity'])) {
124
            $this->fields['entity']['enabled'] = 0;
125
        }
126
127
        // Unset fields that are disabled
128
        foreach ($this->fields as $key => $val) {
129
            if (isset($val['enabled']) && empty($val['enabled'])) {
130
                unset($this->fields[$key]);
131
            }
132
        }
133
134
        // Translate some data of arrayofkeyval
135
        foreach ($this->fields as $key => $val) {
136
            if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
137
                foreach ($val['arrayofkeyval'] as $key2 => $val2) {
138
                    $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
139
                }
140
            }
141
        }
142
    }
143
144
    /**
145
     * Create object into database
146
     *
147
     * @param  User $user      User that creates
148
     * @param  int  $notrigger 0=launch triggers after, 1=disable triggers
149
     * @return int             Return integer <0 if KO, Id of created object if OK
150
     */
151
    public function create(User $user, $notrigger = 0)
0 ignored issues
show
Bug introduced by
The type Dolibarr\Code\EmailCollector\Classes\User was not found. Did you mean User? If so, make sure to prefix the type with \.
Loading history...
152
    {
153
        global $langs;
154
        if (empty($this->type)) {
155
            $langs->load("errors");
156
            $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type"));
157
            return -1;
158
        }
159
160
        return $this->createCommon($user, $notrigger);
161
    }
162
163
    /**
164
     * Clone and object into another one
165
     *
166
     * @param   User    $user       User that creates
167
     * @param   int     $fromid     Id of object to clone
168
     * @return  mixed               New object created, <0 if KO
169
     */
170
    public function createFromClone(User $user, $fromid)
171
    {
172
        global $langs, $hookmanager, $extrafields;
173
        $error = 0;
174
175
        dol_syslog(__METHOD__, LOG_DEBUG);
176
177
        $object = new self($this->db);
178
179
        $this->db->begin();
180
181
        // Load source object
182
        $object->fetchCommon($fromid);
183
        // Reset some properties
184
        unset($object->id);
185
        unset($object->fk_user_creat);
186
        unset($object->import_key);
187
188
        // Clear fields
189
        $object->ref = "copy_of_" . $object->ref;
190
        // $object->title = $langs->trans("CopyOf")." ".$object->title;
191
192
        // Clear extrafields that are unique
193
        if (is_array($object->array_options) && count($object->array_options) > 0) {
194
            $extrafields->fetch_name_optionals_label($this->table_element);
195
            foreach ($object->array_options as $key => $option) {
196
                $shortkey = preg_replace('/options_/', '', $key);
197
                if (!empty($extrafields->attributes[$this->element]['unique'][$shortkey])) {
198
                    unset($object->array_options[$key]);
199
                }
200
            }
201
        }
202
203
        // Create clone
204
        $object->context['createfromclone'] = 'createfromclone';
205
        $result = $object->createCommon($user);
206
        if ($result < 0) {
207
            $error++;
208
            $this->error = $object->error;
209
            $this->errors = $object->errors;
210
        }
211
212
        unset($object->context['createfromclone']);
213
214
        // End
215
        if (!$error) {
216
            $this->db->commit();
217
            return $object;
218
        } else {
219
            $this->db->rollback();
220
            return -1;
221
        }
222
    }
223
224
    /**
225
     * Load object in memory from the database
226
     *
227
     * @param int    $id   Id object
228
     * @param string $ref  Ref
229
     * @return int         Return integer <0 if KO, 0 if not found, >0 if OK
230
     */
231
    public function fetch($id, $ref = null)
232
    {
233
        $result = $this->fetchCommon($id, $ref);
234
235
        return $result;
236
    }
237
238
    /**
239
     * Update object into database
240
     *
241
     * @param  User $user      User that modifies
242
     * @param  int  $notrigger 0=launch triggers after, 1=disable triggers
243
     * @return int             Return integer <0 if KO, >0 if OK
244
     */
245
    public function update(User $user, $notrigger = 0)
246
    {
247
        return $this->updateCommon($user, $notrigger);
248
    }
249
250
    /**
251
     * Delete object in database
252
     *
253
     * @param User  $user       User that deletes
254
     * @param int   $notrigger  0=launch triggers after, 1=disable triggers
255
     * @return int              Return integer <0 if KO, >0 if OK
256
     */
257
    public function delete(User $user, $notrigger = 0)
258
    {
259
        return $this->deleteCommon($user, $notrigger);
260
    }
261
262
    /**
263
     *  Return a link to the object card (with optionally the picto)
264
     *
265
     *  @param  int     $withpicto                  Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
266
     *  @param  string  $option                     On what the link point to ('nolink', ...)
267
     *  @param  int     $notooltip                  1=Disable tooltip
268
     *  @param  string  $morecss                    Add more css on link
269
     *  @param  int     $save_lastsearch_value      -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
270
     *  @return string                              String with URL
271
     */
272
    public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
273
    {
274
        global $db, $conf, $langs, $hookmanager;
275
        global $dolibarr_main_authentication, $dolibarr_main_demo;
276
        global $menumanager;
277
278
        if (!empty($conf->dol_no_mouse_hover)) {
279
            $notooltip = 1; // Force disable tooltips
280
        }
281
282
        $result = '';
283
284
        $label = '<u>' . $langs->trans("EmailcollectorAction") . '</u>';
285
        $label .= '<br>';
286
        $label .= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
287
288
        $url = dol_buildpath('/emailcollector/emailcollectoraction_card.php', 1) . '?id=' . $this->id;
289
290
        if ($option != 'nolink') {
291
            // Add param to save lastsearch_values or not
292
            $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
293
            if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
294
                $add_save_lastsearch_values = 1;
295
            }
296
            if ($add_save_lastsearch_values) {
297
                $url .= '&save_lastsearch_values=1';
298
            }
299
        }
300
301
        $linkclose = '';
302
        if (empty($notooltip)) {
303
            if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
304
                $label = $langs->trans("ShowEmailcollectorAction");
305
                $linkclose .= ' alt="' . dol_escape_htmltag($label, 1) . '"';
306
            }
307
            $linkclose .= ' title="' . dol_escape_htmltag($label, 1) . '"';
308
            $linkclose .= ' class="classfortooltip' . ($morecss ? ' ' . $morecss : '') . '"';
309
310
            /*
311
            $hookmanager->initHooks(array('emailcollectoractiondao'));
312
            $parameters=array('id'=>$this->id);
313
            $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action);    // Note that $action and $object may have been modified by some hooks
314
            if ($reshook > 0) $linkclose = $hookmanager->resPrint;
315
            */
316
        } else {
317
            $linkclose = ($morecss ? ' class="' . $morecss . '"' : '');
318
        }
319
320
        $linkstart = '<a href="' . $url . '"';
321
        $linkstart .= $linkclose . '>';
322
        $linkend = '</a>';
323
324
        $result .= $linkstart;
325
        if ($withpicto) {
326
            $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="' . (($withpicto != 2) ? 'paddingright ' : '') . 'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
327
        }
328
        if ($withpicto != 2) {
329
            $result .= $this->ref;
330
        }
331
        $result .= $linkend;
332
        //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
333
334
        global $action, $hookmanager;
335
        $hookmanager->initHooks(array('emailcollectoractiondao'));
336
        $parameters = array('id' => $this->id, 'getnomurl' => &$result);
337
        $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
338
        if ($reshook > 0) {
339
            $result = $hookmanager->resPrint;
340
        } else {
341
            $result .= $hookmanager->resPrint;
342
        }
343
344
        return $result;
345
    }
346
347
    /**
348
     *  Return label of the status
349
     *
350
     *  @param  int     $mode          0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
351
     *  @return string                 Label of status
352
     */
353
    public function getLibStatut($mode = 0)
354
    {
355
        return $this->LibStatut($this->status, $mode);
356
    }
357
358
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
359
    /**
360
     *  Return the status
361
     *
362
     *  @param  int     $status        Id status
363
     *  @param  int     $mode          0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
364
     *  @return string                 Label of status
365
     */
366
    public function LibStatut($status, $mode = 0)
367
    {
368
		// phpcs:enable
369
        if (empty($this->labelStatus)) {
370
            global $langs;
371
            //$langs->load("emailcollector");
372
            $this->labelStatus[1] = $langs->trans('Enabled');
373
            $this->labelStatus[0] = $langs->trans('Disabled');
374
        }
375
376
        if ($mode == 0) {
377
            return $this->labelStatus[$status];
378
        } elseif ($mode == 1) {
379
            return $this->labelStatus[$status];
380
        } elseif ($mode == 2) {
381
            if ($status == 1) {
382
                return img_picto($this->labelStatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle') . ' ' . $this->labelStatus[$status];
383
            } elseif ($status == 0) {
384
                return img_picto($this->labelStatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle') . ' ' . $this->labelStatus[$status];
385
            }
386
        } elseif ($mode == 3) {
387
            if ($status == 1) {
388
                return img_picto($this->labelStatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle');
389
            } elseif ($status == 0) {
390
                return img_picto($this->labelStatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle');
391
            }
392
        } elseif ($mode == 4) {
393
            if ($status == 1) {
394
                return img_picto($this->labelStatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle') . ' ' . $this->labelStatus[$status];
395
            } elseif ($status == 0) {
396
                return img_picto($this->labelStatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle') . ' ' . $this->labelStatus[$status];
397
            }
398
        } elseif ($mode == 5) {
399
            if ($status == 1) {
400
                return $this->labelStatus[$status] . ' ' . img_picto($this->labelStatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle');
401
            } elseif ($status == 0) {
402
                return $this->labelStatus[$status] . ' ' . img_picto($this->labelStatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle');
403
            }
404
        } elseif ($mode == 6) {
405
            if ($status == 1) {
406
                return $this->labelStatus[$status] . ' ' . img_picto($this->labelStatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle');
407
            } elseif ($status == 0) {
408
                return $this->labelStatus[$status] . ' ' . img_picto($this->labelStatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle');
409
            }
410
        }
411
        return "";
412
    }
413
414
    /**
415
     *  Charge les information d'ordre info dans l'objet commande
416
     *
417
     *  @param  int     $id       Id of order
418
     *  @return void
419
     */
420
    public function info($id)
421
    {
422
        $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
423
        $sql .= ' fk_user_creat, fk_user_modif';
424
        $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
425
        $sql .= ' WHERE t.rowid = ' . ((int) $id);
426
        $result = $this->db->query($sql);
427
        if ($result) {
428
            if ($this->db->num_rows($result)) {
429
                $obj = $this->db->fetch_object($result);
430
431
                $this->id = $obj->rowid;
432
433
                $this->user_creation_id = $obj->fk_user_creat;
434
                $this->user_modification_id = $obj->fk_user_modif;
435
                $this->date_creation = $this->db->jdate($obj->datec);
436
                $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
437
            }
438
439
            $this->db->free($result);
440
        } else {
441
            dol_print_error($this->db);
442
        }
443
    }
444
445
    /**
446
     * Initialise object with example values
447
     * Id must be 0 if object instance is a specimen
448
     *
449
     * @return int
450
     */
451
    public function initAsSpecimen()
452
    {
453
        return $this->initAsSpecimenCommon();
454
    }
455
}
456