Completed
Branch develop (b526f7)
by
unknown
29:42
created

MyObject::getLinesArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) 2017  Laurent Destailleur <[email protected]>
3
 * Copyright (C) ---Put here your own copyright and developer email---
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
/**
20
 * \file        htdocs/modulebuilder/template/class/myobject.class.php
21
 * \ingroup     mymodule
22
 * \brief       This file is a CRUD class file for MyObject (Create/Read/Update/Delete)
23
 */
24
25
// Put here all includes required by your class file
26
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
27
//require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
28
//require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
29
30
/**
31
 * Class for MyObject
32
 */
33
class MyObject extends CommonObject
34
{
35
	/**
36
	 * @var string ID to identify managed object
37
	 */
38
	public $element = 'myobject';
39
40
	/**
41
	 * @var string Name of table without prefix where object is stored
42
	 */
43
	public $table_element = 'mymodule_myobject';
44
45
	/**
46
	 * @var string Name of subtable if this object has sub lines
47
	 */
48
	//public $table_element_line = 'mymodule_myobjectline';
49
50
	/**
51
	 * @var int  Does myobject support multicompany module ? 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
52
	 */
53
	public $ismultientitymanaged = 0;
54
55
	/**
56
	 * @var int  Does myobject support extrafields ? 0=No, 1=Yes
57
	 */
58
	public $isextrafieldmanaged = 1;
59
60
	/**
61
	 * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
62
	 */
63
	public $picto = 'myobject@mymodule';
64
65
66
	const STATUS_DRAFT = 0;
67
	const STATUS_VALIDATED = 1;
68
	const STATUS_DISABLED = 9;
69
70
71
	/**
72
	 *  'type' if the field format ('integer', 'integer:Class:pathtoclass', 'varchar(x)', 'double(24,8)', 'text', 'html', 'datetime', 'timestamp', 'float')
73
	 *  'label' the translation key.
74
	 *  'enabled' is a condition when the field must be managed.
75
	 *  '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, 3=Visible on create/update/view form only (not list), 4=Visible on list and update/view form only (not create). Using a negative value means field is not shown by default on list but can be selected for viewing)
76
	 *  'noteditable' says if field is not editable (1 or 0)
77
	 *  'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
78
	 *  'default' is a default value for creation (can still be replaced by the global setup of default values)
79
	 *  'index' if we want an index in database.
80
	 *  'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
81
	 *  'position' is the sort order of field.
82
	 *  'searchall' is 1 if we want to search in this field when making a search from the quick search button.
83
	 *  '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).
84
	 *  'css' is the CSS style to use on field. For example: 'maxwidth200'
85
	 *  'help' is a string visible as a tooltip on field
86
	 *  'comment' is not used. You can store here any text of your choice. It is not used by application.
87
	 *  'showoncombobox' if value of the field must be visible into the label of the combobox that list record
88
	 *  'arraykeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")
89
	 */
90
91
	// BEGIN MODULEBUILDER PROPERTIES
92
	/**
93
     * @var array  Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
94
     */
95
	public $fields=array(
96
	    'rowid'         =>array('type'=>'integer',      'label'=>'TechnicalID',      'enabled'=>1, 'visible'=>-2, 'noteditable'=>1, 'notnull'=> 1, 'index'=>1, 'position'=>1, 'comment'=>'Id'),
97
		'ref'           =>array('type'=>'varchar(128)', 'label'=>'Ref',              'enabled'=>1, 'visible'=>1,  'noteditable'=>0, 'notnull'=> 1, 'default'=>'(PROV)', 'showoncombobox'=>1, 'index'=>1, 'position'=>10, 'searchall'=>1, 'comment'=>'Reference of object'),
98
	    'entity'        =>array('type'=>'integer',      'label'=>'Entity',           'enabled'=>1, 'visible'=>0,  'notnull'=> 1, 'default'=>1, 'index'=>1, 'position'=>20),
99
	    'label'         =>array('type'=>'varchar(255)', 'label'=>'Label',            'enabled'=>1, 'visible'=>1,  'position'=>30, 'searchall'=>1, 'css'=>'minwidth200', 'help'=>'Help text', 'showoncombobox'=>1),
100
	    'amount'        =>array('type'=>'double(24,8)', 'label'=>'Amount',           'enabled'=>1, 'visible'=>1,  'default'=>'null', 'position'=>40,  'searchall'=>0, 'isameasure'=>1, 'help'=>'Help text for amount'),
101
	    'qty'           =>array('type'=>'real',         'label'=>'Qty',              'enabled'=>1, 'visible'=>1,  'default'=>'0', 'position'=>45,  'searchall'=>0, 'isameasure'=>1, 'help'=>'Help text for quantity', 'css'=>'maxwidth75imp'),
102
	    'fk_soc' 		=>array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'visible'=> 1, 'enabled'=>1, 'position'=>50, 'notnull'=>-1, 'index'=>1, 'help'=>'LinkToThirparty'),
103
		'description'   =>array('type'=>'text',			'label'=>'Description',		 'enabled'=>1, 'visible'=>0,  'position'=>60),
104
		'note_public'   =>array('type'=>'html',			'label'=>'NotePublic',		 'enabled'=>1, 'visible'=>0,  'position'=>61),
105
		'note_private'  =>array('type'=>'html',			'label'=>'NotePrivate',		 'enabled'=>1, 'visible'=>0,  'position'=>62),
106
		'date_creation' =>array('type'=>'datetime',     'label'=>'DateCreation',     'enabled'=>1, 'visible'=>-2, 'notnull'=> 1, 'position'=>500),
107
	    'tms'           =>array('type'=>'timestamp',    'label'=>'DateModification', 'enabled'=>1, 'visible'=>-2, 'notnull'=> 0, 'position'=>501),
108
		//'date_validation'    =>array('type'=>'datetime',     'label'=>'DateCreation',     'enabled'=>1, 'visible'=>-2, 'position'=>502),
109
		'fk_user_creat' =>array('type'=>'integer',      'label'=>'UserAuthor',       'enabled'=>1, 'visible'=>-2, 'notnull'=> 1, 'position'=>510, 'foreignkey'=>'user.rowid'),
110
		'fk_user_modif' =>array('type'=>'integer',      'label'=>'UserModif',        'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>511),
111
		//'fk_user_valid' =>array('type'=>'integer',      'label'=>'UserValidation',        'enabled'=>1, 'visible'=>-1, 'position'=>512),
112
		'import_key'    =>array('type'=>'varchar(14)',  'label'=>'ImportId',         'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'index'=>0,  'position'=>1000),
113
	    'status'        =>array('type'=>'integer',      'label'=>'Status',           'enabled'=>1, 'visible'=>1,  'notnull'=> 1, 'default'=>0, 'index'=>1,  'position'=>1000, 'arrayofkeyval'=>array(0=>'Draft', 1=>'Active', 9=>'Canceled')),
114
	);
115
116
	/**
117
	 * @var int ID
118
	 */
119
	public $rowid;
120
121
	/**
122
	 * @var string Ref
123
	 */
124
	public $ref;
125
126
	/**
127
	 * @var int Entity
128
	 */
129
	public $entity;
130
131
	/**
132
     * @var string label
133
     */
134
    public $label;
135
136
    /**
137
     * @var string amount
138
     */
139
	public $amount;
140
141
	/**
142
	 * @var int Status
143
	 */
144
	public $status;
145
146
	/**
147
     * @var integer|string date_creation
148
     */
149
	public $date_creation;
150
151
	/**
152
     * @var integer tms
153
     */
154
	public $tms;
155
156
	/**
157
     * @var int ID
158
     */
159
	public $fk_user_creat;
160
161
	/**
162
     * @var int ID
163
     */
164
	public $fk_user_modif;
165
166
	/**
167
     * @var string import_key
168
     */
169
	public $import_key;
170
	// END MODULEBUILDER PROPERTIES
171
172
173
174
	// If this object has a subtable with lines
175
176
	/**
177
	 * @var int    Name of subtable line
178
	 */
179
	//public $table_element_line = 'myobjectdet';
180
181
	/**
182
	 * @var int    Field with ID of parent key if this field has a parent
183
	 */
184
	//public $fk_element = 'fk_myobject';
185
186
	/**
187
	 * @var int    Name of subtable class that manage subtable lines
188
	 */
189
	//public $class_element_line = 'MyObjectline';
190
191
	/**
192
	 * @var array  Array of child tables (child tables to delete before deleting a record)
193
	 */
194
	//protected $childtables=array('myobjectdet');
195
196
	/**
197
	 * @var MyObjectLine[]     Array of subtable lines
198
	 */
199
	//public $lines = array();
200
201
202
203
	/**
204
	 * Constructor
205
	 *
206
	 * @param DoliDb $db Database handler
207
	 */
208
	public function __construct(DoliDB $db)
209
	{
210
		global $conf, $langs;
211
212
		$this->db = $db;
213
214
		if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields['rowid'])) $this->fields['rowid']['visible']=0;
215
		if (empty($conf->multicompany->enabled) && isset($this->fields['entity'])) $this->fields['entity']['enabled']=0;
216
217
		// Unset fields that are disabled
218
		foreach($this->fields as $key => $val)
219
		{
220
			if (isset($val['enabled']) && empty($val['enabled']))
221
			{
222
				unset($this->fields[$key]);
223
			}
224
		}
225
226
		// Translate some data of arrayofkeyval
227
		foreach($this->fields as $key => $val)
228
		{
229
			if (is_array($this->fields['status']['arrayofkeyval']))
230
			{
231
				foreach($this->fields['status']['arrayofkeyval'] as $key2 => $val2)
232
				{
233
					$this->fields['status']['arrayofkeyval'][$key2]=$langs->trans($val2);
234
				}
235
			}
236
		}
237
	}
238
239
	/**
240
	 * Create object into database
241
	 *
242
	 * @param  User $user      User that creates
243
	 * @param  bool $notrigger false=launch triggers after, true=disable triggers
244
	 * @return int             <0 if KO, Id of created object if OK
245
	 */
246
	public function create(User $user, $notrigger = false)
247
	{
248
		return $this->createCommon($user, $notrigger);
249
	}
250
251
	/**
252
	 * Clone an object into another one
253
	 *
254
	 * @param  	User 	$user      	User that creates
255
	 * @param  	int 	$fromid     Id of object to clone
256
	 * @return 	mixed 				New object created, <0 if KO
257
	 */
258
	public function createFromClone(User $user, $fromid)
259
	{
260
		global $langs, $extrafields;
261
	    $error = 0;
262
263
	    dol_syslog(__METHOD__, LOG_DEBUG);
264
265
	    $object = new self($this->db);
266
267
	    $this->db->begin();
268
269
	    // Load source object
270
	    $object->fetchCommon($fromid);
271
	    // Reset some properties
272
	    unset($object->id);
273
	    unset($object->fk_user_creat);
274
	    unset($object->import_key);
275
276
	    // Clear fields
277
	    $object->ref = "copy_of_".$object->ref;
278
	    $object->title = $langs->trans("CopyOf")." ".$object->title;
279
	    // ...
280
	    // Clear extrafields that are unique
281
	    if (is_array($object->array_options) && count($object->array_options) > 0)
282
	    {
283
	    	$extrafields->fetch_name_optionals_label($this->element);
284
	    	foreach($object->array_options as $key => $option)
285
	    	{
286
	    		$shortkey = preg_replace('/options_/', '', $key);
287
	    		if (! empty($extrafields->attributes[$this->element]['unique'][$shortkey]))
288
	    		{
289
	    			//var_dump($key); var_dump($clonedObj->array_options[$key]); exit;
290
	    			unset($object->array_options[$key]);
291
	    		}
292
	    	}
293
	    }
294
295
	    // Create clone
296
		$object->context['createfromclone'] = 'createfromclone';
297
	    $result = $object->createCommon($user);
298
	    if ($result < 0) {
299
	        $error++;
300
	        $this->error = $object->error;
301
	        $this->errors = $object->errors;
302
	    }
303
304
	    unset($object->context['createfromclone']);
305
306
	    // End
307
	    if (!$error) {
308
	        $this->db->commit();
309
	        return $object;
310
	    } else {
311
	        $this->db->rollback();
312
	        return -1;
313
	    }
314
	}
315
316
	/**
317
	 * Load object in memory from the database
318
	 *
319
	 * @param int    $id   Id object
320
	 * @param string $ref  Ref
321
	 * @return int         <0 if KO, 0 if not found, >0 if OK
322
	 */
323
	public function fetch($id, $ref = null)
324
	{
325
		$result = $this->fetchCommon($id, $ref);
326
		if ($result > 0 && ! empty($this->table_element_line)) $this->fetchLines();
327
		return $result;
328
	}
329
330
	/**
331
	 * Load object lines in memory from the database
332
	 *
333
	 * @return int         <0 if KO, 0 if not found, >0 if OK
334
	 */
335
	public function fetchLines()
336
	{
337
		$this->lines=array();
338
339
		$result = $this->fetchLinesCommon();
340
		return $result;
341
	}
342
343
344
	/**
345
	 * Load list of objects in memory from the database.
346
	 *
347
	 * @param  string      $sortorder    Sort Order
348
	 * @param  string      $sortfield    Sort field
349
	 * @param  int         $limit        limit
350
	 * @param  int         $offset       Offset
351
	 * @param  array       $filter       Filter array. Example array('field'=>'valueforlike', 'customurl'=>...)
352
	 * @param  string      $filtermode   Filter mode (AND or OR)
353
	 * @return array|int                 int <0 if KO, array of pages if OK
354
	 */
355
	public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
356
	{
357
		global $conf;
358
359
		dol_syslog(__METHOD__, LOG_DEBUG);
360
361
		$records=array();
362
363
		$sql = 'SELECT ';
364
		$sql .= $this->getFieldList();
365
		$sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element. ' as t';
366
		if ($this->ismultientitymanaged) $sql .= ' WHERE t.entity IN ('.getEntity($this->table_element).')';
367
		else $sql .= ' WHERE 1 = 1';
368
		// Manage filter
369
		$sqlwhere = array();
370
		if (count($filter) > 0) {
371
			foreach ($filter as $key => $value) {
372
				if ($key=='t.rowid') {
373
					$sqlwhere[] = $key . '='. $value;
374
				}
375
				elseif (strpos($key, 'date') !== false) {
376
					$sqlwhere[] = $key.' = \''.$this->db->idate($value).'\'';
377
				}
378
				elseif ($key=='customsql') {
379
					$sqlwhere[] = $value;
380
				}
381
				else {
382
					$sqlwhere[] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\'';
383
				}
384
			}
385
		}
386
		if (count($sqlwhere) > 0) {
387
			$sql .= ' AND (' . implode(' '.$filtermode.' ', $sqlwhere).')';
388
		}
389
390
		if (!empty($sortfield)) {
391
			$sql .= $this->db->order($sortfield, $sortorder);
392
		}
393
		if (!empty($limit)) {
394
			$sql .=  ' ' . $this->db->plimit($limit, $offset);
395
		}
396
397
		$resql = $this->db->query($sql);
398
		if ($resql) {
399
			$num = $this->db->num_rows($resql);
400
            $i = 0;
401
			while ($i < min($limit, $num))
402
			{
403
			    $obj = $this->db->fetch_object($resql);
404
405
				$record = new self($this->db);
406
				$record->setVarsFromFetchObj($obj);
407
408
				$records[$record->id] = $record;
409
410
				$i++;
411
			}
412
			$this->db->free($resql);
413
414
			return $records;
415
		} else {
416
			$this->errors[] = 'Error ' . $this->db->lasterror();
417
			dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
418
419
			return -1;
420
		}
421
	}
422
423
	/**
424
	 * Update object into database
425
	 *
426
	 * @param  User $user      User that modifies
427
	 * @param  bool $notrigger false=launch triggers after, true=disable triggers
428
	 * @return int             <0 if KO, >0 if OK
429
	 */
430
	public function update(User $user, $notrigger = false)
431
	{
432
		return $this->updateCommon($user, $notrigger);
433
	}
434
435
	/**
436
	 * Delete object in database
437
	 *
438
	 * @param User $user       User that deletes
439
	 * @param bool $notrigger  false=launch triggers after, true=disable triggers
440
	 * @return int             <0 if KO, >0 if OK
441
	 */
442
	public function delete(User $user, $notrigger = false)
443
	{
444
		return $this->deleteCommon($user, $notrigger);
445
		//return $this->deleteCommon($user, $notrigger, 1);
446
	}
447
448
	/**
449
	 *  Delete a line of object in database
450
	 *
451
	 *	@param  User	$user       User that delete
452
	 *  @param	int		$idline		Id of line to delete
453
	 *  @param 	bool 	$notrigger  false=launch triggers after, true=disable triggers
454
	 *  @return int         		>0 if OK, <0 if KO
455
	 */
456
	public function deleteLine(User $user, $idline, $notrigger = false)
457
	{
458
		if ($this->status < 0)
459
		{
460
			$this->error = 'ErrorDeleteLineNotAllowedByObjectStatus';
461
			return -2;
462
		}
463
464
		return $this->deleteLineCommon($user, $idline, $notrigger);
465
	}
466
467
    /**
468
     *  Return a link to the object card (with optionaly the picto)
469
     *
470
     *  @param  int     $withpicto                  Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
471
     *  @param  string  $option                     On what the link point to ('nolink', ...)
472
     *  @param  int     $notooltip                  1=Disable tooltip
473
     *  @param  string  $morecss                    Add more css on link
474
     *  @param  int     $save_lastsearch_value      -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
475
     *  @return	string                              String with URL
476
     */
477
    public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
478
    {
479
        global $conf, $langs, $hookmanager;
480
481
        if (! empty($conf->dol_no_mouse_hover)) $notooltip=1;   // Force disable tooltips
482
483
        $result = '';
484
485
        $label = '<u>' . $langs->trans("MyObject") . '</u>';
486
        $label.= '<br>';
487
        $label.= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
488
489
        $url = dol_buildpath('/mymodule/myobject_card.php', 1).'?id='.$this->id;
490
491
        if ($option != 'nolink')
492
        {
493
            // Add param to save lastsearch_values or not
494
            $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
495
            if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1;
496
            if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1';
497
        }
498
499
        $linkclose='';
500
        if (empty($notooltip))
501
        {
502
            if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
503
            {
504
                $label=$langs->trans("ShowMyObject");
505
                $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
506
            }
507
            $linkclose.=' title="'.dol_escape_htmltag($label, 1).'"';
508
            $linkclose.=' class="classfortooltip'.($morecss?' '.$morecss:'').'"';
509
510
            /*
511
             $hookmanager->initHooks(array('myobjectdao'));
512
             $parameters=array('id'=>$this->id);
513
             $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action);    // Note that $action and $object may have been modified by some hooks
514
             if ($reshook > 0) $linkclose = $hookmanager->resPrint;
515
             */
516
        }
517
        else $linkclose = ($morecss?' class="'.$morecss.'"':'');
518
519
		$linkstart = '<a href="'.$url.'"';
520
		$linkstart.=$linkclose.'>';
521
		$linkend='</a>';
522
523
		$result .= $linkstart;
524
		if ($withpicto) $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);
525
		if ($withpicto != 2) $result.= $this->ref;
526
		$result .= $linkend;
527
		//if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
528
529
		global $action,$hookmanager;
530
		$hookmanager->initHooks(array('myobjectdao'));
531
		$parameters=array('id'=>$this->id, 'getnomurl'=>$result);
532
		$reshook=$hookmanager->executeHooks('getNomUrl', $parameters, $this, $action);    // Note that $action and $object may have been modified by some hooks
533
		if ($reshook > 0) $result = $hookmanager->resPrint;
534
		else $result .= $hookmanager->resPrint;
535
536
		return $result;
537
    }
538
539
	/**
540
	 *  Return label of the status
541
	 *
542
	 *  @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
543
	 *  @return	string 			       Label of status
544
	 */
545
	public function getLibStatut($mode = 0)
546
	{
547
		return $this->LibStatut($this->status, $mode);
548
	}
549
550
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
551
	/**
552
	 *  Return the status
553
	 *
554
	 *  @param	int		$status        Id status
555
	 *  @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
556
	 *  @return string 			       Label of status
557
	 */
558
	public function LibStatut($status, $mode = 0)
559
	{
560
		// phpcs:enable
561
		if (empty($this->labelstatus))
562
		{
563
			global $langs;
564
			//$langs->load("mymodule");
565
			$this->labelstatus[self::STATUS_DRAFT] = $langs->trans('Draft');
566
			$this->labelstatus[self::STATUS_VALIDATED] = $langs->trans('Enabled');
567
			$this->labelstatus[self::STATUS_CANCELED] = $langs->trans('Disabled');
568
		}
569
570
		if ($mode == 0)
571
		{
572
			return $this->labelstatus[$status];
573
		}
574
		elseif ($mode == 1)
575
		{
576
			return $this->labelstatus[$status];
577
		}
578
		elseif ($mode == 2)
579
		{
580
			return img_picto($this->labelstatus[$status], 'statut'.$status, '', false, 0, 0, '', 'valignmiddle').' '.$this->labelstatus[$status];
581
		}
582
		elseif ($mode == 3)
583
		{
584
			return img_picto($this->labelstatus[$status], 'statut'.$status, '', false, 0, 0, '', 'valignmiddle');
585
		}
586
		elseif ($mode == 4)
587
		{
588
			return img_picto($this->labelstatus[$status], 'statut'.$status, '', false, 0, 0, '', 'valignmiddle').' '.$this->labelstatus[$status];
589
		}
590
		elseif ($mode == 5)
591
		{
592
			return $this->labelstatus[$status].' '.img_picto($this->labelstatus[$status], 'statut'.$status, '', false, 0, 0, '', 'valignmiddle');
593
		}
594
		elseif ($mode == 6)
595
		{
596
			return $this->labelstatus[$status].' '.img_picto($this->labelstatus[$status], 'statut'.$status, '', false, 0, 0, '', 'valignmiddle');
597
		}
598
	}
599
600
	/**
601
	 *	Load the info information in the object
602
	 *
603
	 *	@param  int		$id       Id of object
604
	 *	@return	void
605
	 */
606
	public function info($id)
607
	{
608
		$sql = 'SELECT rowid, date_creation as datec, tms as datem,';
609
		$sql.= ' fk_user_creat, fk_user_modif';
610
		$sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
611
		$sql.= ' WHERE t.rowid = '.$id;
612
		$result=$this->db->query($sql);
613
		if ($result)
614
		{
615
			if ($this->db->num_rows($result))
616
			{
617
				$obj = $this->db->fetch_object($result);
618
				$this->id = $obj->rowid;
619
				if ($obj->fk_user_author)
620
				{
621
					$cuser = new User($this->db);
622
					$cuser->fetch($obj->fk_user_author);
623
					$this->user_creation   = $cuser;
624
				}
625
626
				if ($obj->fk_user_valid)
627
				{
628
					$vuser = new User($this->db);
629
					$vuser->fetch($obj->fk_user_valid);
630
					$this->user_validation = $vuser;
631
				}
632
633
				if ($obj->fk_user_cloture)
634
				{
635
					$cluser = new User($this->db);
636
					$cluser->fetch($obj->fk_user_cloture);
637
					$this->user_cloture   = $cluser;
638
				}
639
640
				$this->date_creation     = $this->db->jdate($obj->datec);
641
				$this->date_modification = $this->db->jdate($obj->datem);
642
				$this->date_validation   = $this->db->jdate($obj->datev);
643
			}
644
645
			$this->db->free($result);
646
		}
647
		else
648
		{
649
			dol_print_error($this->db);
650
		}
651
	}
652
653
	/**
654
	 * Initialise object with example values
655
	 * Id must be 0 if object instance is a specimen
656
	 *
657
	 * @return void
658
	 */
659
	public function initAsSpecimen()
660
	{
661
		$this->initAsSpecimenCommon();
662
	}
663
664
	/**
665
	 * 	Create an array of lines
666
	 *
667
	 * 	@return array|int		array of lines if OK, <0 if KO
668
	 */
669
	public function getLinesArray()
670
	{
671
	    $this->lines=array();
672
673
	    $objectline = new MyObjectLine($this->db);
674
	    $result = $objectline->fetchAll('', '', 0, 0, array('customsql'=>'fk_myobject = '.$this->id));
1 ignored issue
show
Bug introduced by
The method fetchAll() does not seem to exist on object<MyObjectLine>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
675
676
	    if (is_numeric($result))
677
	    {
678
	        $this->error = $this->error;
679
	        $this->errors = $this->errors;
680
	        return $result;
681
	    }
682
	    else
683
	    {
684
	        $this->lines = $result;
685
	        return $this->lines;
686
	    }
687
	}
688
689
	/**
690
	 *  Create a document onto disk according to template module.
691
	 *
692
	 *  @param	    string		$modele			Force template to use ('' to not force)
693
	 *  @param		Translate	$outputlangs	objet lang a utiliser pour traduction
694
	 *  @param      int			$hidedetails    Hide details of lines
695
	 *  @param      int			$hidedesc       Hide description
696
	 *  @param      int			$hideref        Hide ref
697
	 *  @param      null|array  $moreparams     Array to provide more information
698
	 *  @return     int         				0 if KO, 1 if OK
699
	 */
700
	public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
701
	{
702
		global $conf,$langs;
703
704
		$langs->load("mymodule@mymodule");
705
706
		if (! dol_strlen($modele)) {
707
708
			$modele = 'standard';
709
710
			if ($this->modelpdf) {
711
				$modele = $this->modelpdf;
712
			} elseif (! empty($conf->global->MYOBJECT_ADDON_PDF)) {
713
				$modele = $conf->global->MYOBJECT_ADDON_PDF;
714
			}
715
		}
716
717
		$modelpath = "core/modules/mymodule/doc/";
718
719
		return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
720
	}
721
722
	/**
723
	 * Action executed by scheduler
724
	 * CAN BE A CRON TASK. In such a case, parameters come from the schedule job setup field 'Parameters'
725
	 *
726
	 * @return	int			0 if OK, <>0 if KO (this function is used also by cron so only 0 is OK)
727
	 */
728
	//public function doScheduledJob($param1, $param2, ...)
729
	public function doScheduledJob()
730
	{
731
		global $conf, $langs;
732
733
		//$conf->global->SYSLOG_FILE = 'DOL_DATA_ROOT/dolibarr_mydedicatedlofile.log';
734
735
		$error = 0;
736
		$this->output = '';
737
		$this->error='';
738
739
		dol_syslog(__METHOD__, LOG_DEBUG);
740
741
		$now = dol_now();
742
743
		$this->db->begin();
744
745
		// ...
746
747
		$this->db->commit();
748
749
		return $error;
750
	}
751
}
752
753
/**
754
 * Class MyObjectLine. You can also remove this and generate a CRUD class for lines objects.
755
 */
756
class MyObjectLine
757
{
758
	// To complete with content of an object MyObjectLine
759
}
760