| Total Complexity | 87 | 
| Total Lines | 577 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like ProductStockEntrepot often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProductStockEntrepot, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 45 | class ProductStockEntrepot extends CommonObject  | 
            ||
| 46 | { | 
            ||
| 47 | /**  | 
            ||
| 48 | * @var string Id to identify managed objects  | 
            ||
| 49 | */  | 
            ||
| 50 | public $element = 'ProductStockEntrepot';  | 
            ||
| 51 | |||
| 52 | /**  | 
            ||
| 53 | * @var string Name of table without prefix where object is stored  | 
            ||
| 54 | */  | 
            ||
| 55 | public $table_element = 'product_warehouse_properties';  | 
            ||
| 56 | |||
| 57 | /**  | 
            ||
| 58 | * @var int ID  | 
            ||
| 59 | */  | 
            ||
| 60 | public $fk_product;  | 
            ||
| 61 | |||
| 62 | /**  | 
            ||
| 63 | * @var int ID  | 
            ||
| 64 | */  | 
            ||
| 65 | public $fk_entrepot;  | 
            ||
| 66 | |||
| 67 | public $seuil_stock_alerte;  | 
            ||
| 68 | public $desiredstock;  | 
            ||
| 69 | public $import_key;  | 
            ||
| 70 | |||
| 71 | |||
| 72 | /**  | 
            ||
| 73 | * Constructor  | 
            ||
| 74 | *  | 
            ||
| 75 | * @param DoliDB $db Database handler  | 
            ||
| 76 | */  | 
            ||
| 77 | public function __construct(DoliDB $db)  | 
            ||
| 80 | }  | 
            ||
| 81 | |||
| 82 | /**  | 
            ||
| 83 | * Create object into database  | 
            ||
| 84 | *  | 
            ||
| 85 | * @param User $user User that creates  | 
            ||
| 86 | * @param int $notrigger 0=launch triggers after, 1=disable triggers  | 
            ||
| 87 | * @return int Return integer <0 if KO, Id of created object if OK  | 
            ||
| 88 | */  | 
            ||
| 89 | public function create(User $user, $notrigger = 0)  | 
            ||
| 90 |     { | 
            ||
| 91 | dol_syslog(__METHOD__, LOG_DEBUG);  | 
            ||
| 92 | |||
| 93 | $error = 0;  | 
            ||
| 94 | |||
| 95 | // Clean parameters  | 
            ||
| 96 | |||
| 97 |         if (isset($this->fk_product)) { | 
            ||
| 98 | $this->fk_product = (int) $this->fk_product;  | 
            ||
| 99 | }  | 
            ||
| 100 |         if (isset($this->fk_entrepot)) { | 
            ||
| 101 | $this->fk_entrepot = (int) $this->fk_entrepot;  | 
            ||
| 102 | }  | 
            ||
| 103 |         if (isset($this->seuil_stock_alerte)) { | 
            ||
| 104 | $this->seuil_stock_alerte = trim($this->seuil_stock_alerte);  | 
            ||
| 105 | }  | 
            ||
| 106 |         if (isset($this->desiredstock)) { | 
            ||
| 107 | $this->desiredstock = trim($this->desiredstock);  | 
            ||
| 108 | }  | 
            ||
| 109 |         if (isset($this->import_key)) { | 
            ||
| 110 | $this->import_key = trim($this->import_key);  | 
            ||
| 111 | }  | 
            ||
| 112 | |||
| 113 | // Check parameters  | 
            ||
| 114 | // Put here code to add control on parameters values  | 
            ||
| 115 | |||
| 116 | // Insert request  | 
            ||
| 117 |         $sql = 'INSERT INTO ' . $this->db->prefix() . $this->table_element . '('; | 
            ||
| 118 | $sql .= 'fk_product,';  | 
            ||
| 119 | $sql .= 'fk_entrepot,';  | 
            ||
| 120 | $sql .= 'seuil_stock_alerte,';  | 
            ||
| 121 | $sql .= 'desiredstock,';  | 
            ||
| 122 | $sql .= 'import_key';  | 
            ||
| 123 |         $sql .= ') VALUES ('; | 
            ||
| 124 | $sql .= ' ' . (!isset($this->fk_product) ? 'NULL' : $this->fk_product) . ',';  | 
            ||
| 125 | $sql .= ' ' . (!isset($this->fk_entrepot) ? 'NULL' : $this->fk_entrepot) . ',';  | 
            ||
| 126 | $sql .= ' ' . (!isset($this->seuil_stock_alerte) ? '0' : $this->seuil_stock_alerte) . ',';  | 
            ||
| 127 | $sql .= ' ' . (!isset($this->desiredstock) ? '0' : $this->desiredstock) . ',';  | 
            ||
| 128 | $sql .= ' ' . (!isset($this->import_key) ? 'NULL' : "'" . $this->db->escape($this->import_key) . "'");  | 
            ||
| 129 | $sql .= ')';  | 
            ||
| 130 | |||
| 131 | $this->db->begin();  | 
            ||
| 132 | |||
| 133 | $resql = $this->db->query($sql);  | 
            ||
| 134 |         if (!$resql) { | 
            ||
| 135 | $error++;  | 
            ||
| 136 | $this->errors[] = 'Error ' . $this->db->lasterror();  | 
            ||
| 137 |             dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR); | 
            ||
| 138 | }  | 
            ||
| 139 | |||
| 140 |         if (!$error) { | 
            ||
| 141 | $this->id = $this->db->last_insert_id($this->db->prefix() . $this->table_element);  | 
            ||
| 142 | |||
| 143 |             //if (!$notrigger) { | 
            ||
| 144 | // Uncomment this and change MYOBJECT to your own tag if you  | 
            ||
| 145 | // want this action to call a trigger.  | 
            ||
| 146 | |||
| 147 | //// Call triggers  | 
            ||
| 148 |             //$result=$this->call_trigger('MYOBJECT_CREATE',$user); | 
            ||
| 149 | //if ($result < 0) $error++;  | 
            ||
| 150 | //// End call triggers  | 
            ||
| 151 | //}  | 
            ||
| 152 | }  | 
            ||
| 153 | |||
| 154 | // Commit or rollback  | 
            ||
| 155 |         if ($error) { | 
            ||
| 156 | $this->db->rollback();  | 
            ||
| 157 | |||
| 158 | return -1 * $error;  | 
            ||
| 159 |         } else { | 
            ||
| 160 | $this->db->commit();  | 
            ||
| 161 | |||
| 162 | return $this->id;  | 
            ||
| 163 | }  | 
            ||
| 164 | }  | 
            ||
| 165 | |||
| 166 | /**  | 
            ||
| 167 | * Load object in memory from the database  | 
            ||
| 168 | *  | 
            ||
| 169 | * @param int $id Id object  | 
            ||
| 170 | * @param int $fk_product Id product  | 
            ||
| 171 | * @param int $fk_entrepot Id warehouse  | 
            ||
| 172 | * @return int Return integer <0 if KO, 0 if not found, >0 if OK  | 
            ||
| 173 | */  | 
            ||
| 174 | public function fetch($id, $fk_product = 0, $fk_entrepot = 0)  | 
            ||
| 175 |     { | 
            ||
| 176 |         if (empty($id) && (empty($fk_product) || empty($fk_entrepot))) { | 
            ||
| 177 | return -1;  | 
            ||
| 178 | }  | 
            ||
| 179 | |||
| 180 | dol_syslog(__METHOD__, LOG_DEBUG);  | 
            ||
| 181 | |||
| 182 | $sql = "SELECT";  | 
            ||
| 183 | $sql .= " t.rowid,";  | 
            ||
| 184 | $sql .= " t.tms,";  | 
            ||
| 185 | $sql .= " t.fk_product,";  | 
            ||
| 186 | $sql .= " t.fk_entrepot,";  | 
            ||
| 187 | $sql .= " t.seuil_stock_alerte,";  | 
            ||
| 188 | $sql .= " t.desiredstock,";  | 
            ||
| 189 | $sql .= " t.import_key";  | 
            ||
| 190 | $sql .= " FROM " . $this->db->prefix() . $this->table_element . " as t";  | 
            ||
| 191 |         if (!empty($id)) { | 
            ||
| 192 | $sql .= " WHERE t.rowid = " . ((int) $id);  | 
            ||
| 193 |         } else { | 
            ||
| 194 | $sql .= " WHERE t.fk_product = " . ((int) $fk_product) . " AND t.fk_entrepot = " . ((int) $fk_entrepot);  | 
            ||
| 195 | }  | 
            ||
| 196 | |||
| 197 | $resql = $this->db->query($sql);  | 
            ||
| 198 |         if ($resql) { | 
            ||
| 199 | $numrows = $this->db->num_rows($resql);  | 
            ||
| 200 |             if ($numrows) { | 
            ||
| 201 | $obj = $this->db->fetch_object($resql);  | 
            ||
| 202 | |||
| 203 | $this->id = $obj->rowid;  | 
            ||
| 204 | |||
| 205 | $this->tms = $this->db->jdate($obj->tms);  | 
            ||
| 206 | $this->fk_product = $obj->fk_product;  | 
            ||
| 207 | $this->fk_entrepot = $obj->fk_entrepot;  | 
            ||
| 208 | $this->seuil_stock_alerte = $obj->seuil_stock_alerte;  | 
            ||
| 209 | $this->desiredstock = $obj->desiredstock;  | 
            ||
| 210 | $this->import_key = $obj->import_key;  | 
            ||
| 211 | }  | 
            ||
| 212 | |||
| 213 | // Retrieve all extrafield  | 
            ||
| 214 | // fetch optionals attributes and labels  | 
            ||
| 215 | $this->fetch_optionals();  | 
            ||
| 216 | |||
| 217 | // $this->fetch_lines();  | 
            ||
| 218 | |||
| 219 | $this->db->free($resql);  | 
            ||
| 220 | |||
| 221 |             if ($numrows) { | 
            ||
| 222 | return 1;  | 
            ||
| 223 |             } else { | 
            ||
| 224 | return 0;  | 
            ||
| 225 | }  | 
            ||
| 226 |         } else { | 
            ||
| 227 | $this->errors[] = 'Error ' . $this->db->lasterror();  | 
            ||
| 228 |             dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR); | 
            ||
| 229 | |||
| 230 | return -1;  | 
            ||
| 231 | }  | 
            ||
| 232 | }  | 
            ||
| 233 | |||
| 234 | /**  | 
            ||
| 235 | * Load object in memory from the database  | 
            ||
| 236 | *  | 
            ||
| 237 | * @param int $fk_product Product from which we want to get limit and desired stock by warehouse  | 
            ||
| 238 | * @param int $fk_entrepot Warehouse in which we want to get products limit and desired stock  | 
            ||
| 239 | * @param string $sortorder Sort Order  | 
            ||
| 240 | * @param string $sortfield Sort field  | 
            ||
| 241 | * @param int $limit Limit  | 
            ||
| 242 | * @param int $offset Offset limit  | 
            ||
| 243 | * @param string|array $filter Filter USF.  | 
            ||
| 244 | * @param string $filtermode Filter mode (AND or OR)  | 
            ||
| 245 | * @return int|array Return integer <0 if KO, array if OK  | 
            ||
| 246 | */  | 
            ||
| 247 | public function fetchAll($fk_product = 0, $fk_entrepot = 0, $sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')  | 
            ||
| 248 |     { | 
            ||
| 249 | dol_syslog(__METHOD__, LOG_DEBUG);  | 
            ||
| 250 | |||
| 251 | $sql = "SELECT";  | 
            ||
| 252 | $sql .= " t.rowid,";  | 
            ||
| 253 | $sql .= " t.tms,";  | 
            ||
| 254 | $sql .= " t.fk_product,";  | 
            ||
| 255 | $sql .= " t.fk_entrepot,";  | 
            ||
| 256 | $sql .= " t.seuil_stock_alerte,";  | 
            ||
| 257 | $sql .= " t.desiredstock,";  | 
            ||
| 258 | $sql .= " t.import_key";  | 
            ||
| 259 | $sql .= " FROM " . $this->db->prefix() . $this->table_element . " as t";  | 
            ||
| 260 | $sql .= " WHERE 1=1";  | 
            ||
| 261 | |||
| 262 | // Manage filter  | 
            ||
| 263 |         if (is_array($filter)) { | 
            ||
| 264 | $sqlwhere = array();  | 
            ||
| 265 |             if (count($filter) > 0) { | 
            ||
| 266 |                 foreach ($filter as $key => $value) { | 
            ||
| 267 | $sqlwhere[] = $this->db->sanitize($key) . " LIKE '%" . $this->db->escape($this->db->escapeforlike($value)) . "%'";  | 
            ||
| 268 | }  | 
            ||
| 269 | }  | 
            ||
| 270 |             if (count($sqlwhere) > 0) { | 
            ||
| 271 |                 $sql .= " AND " . implode(' ' . $this->db->escape($filtermode) . ' ', $sqlwhere); | 
            ||
| 272 | }  | 
            ||
| 273 | |||
| 274 | $filter = '';  | 
            ||
| 275 | }  | 
            ||
| 276 | |||
| 277 | // Manage filter  | 
            ||
| 278 | $errormessage = '';  | 
            ||
| 279 | $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);  | 
            ||
| 280 |         if ($errormessage) { | 
            ||
| 281 | $this->errors[] = $errormessage;  | 
            ||
| 282 |             dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR); | 
            ||
| 283 | return -1;  | 
            ||
| 284 | }  | 
            ||
| 285 | |||
| 286 |         if (!empty($fk_product) && $fk_product > 0) { | 
            ||
| 287 | $sql .= " AND fk_product = " . ((int) $fk_product);  | 
            ||
| 288 |         } elseif (!empty($fk_entrepot) && $fk_entrepot > 0) { | 
            ||
| 289 | $sql .= " AND fk_entrepot = " . ((int) $fk_entrepot);  | 
            ||
| 290 | }  | 
            ||
| 291 | // "elseif" used instead of "if" because getting list with specified fk_product and specified fk_entrepot would be the same as doing a fetch  | 
            ||
| 292 | |||
| 293 |         if (!empty($sortfield)) { | 
            ||
| 294 | $sql .= $this->db->order($sortfield, $sortorder);  | 
            ||
| 295 | }  | 
            ||
| 296 |         if (!empty($limit)) { | 
            ||
| 297 | $sql .= $this->db->plimit($limit, $offset);  | 
            ||
| 298 | }  | 
            ||
| 299 | |||
| 300 | $lines = array();  | 
            ||
| 301 | |||
| 302 | $resql = $this->db->query($sql);  | 
            ||
| 303 |         if ($resql) { | 
            ||
| 304 |             while ($obj = $this->db->fetch_object($resql)) { | 
            ||
| 305 | $lines[$obj->rowid] = array(  | 
            ||
| 306 | 'id' => $obj->rowid  | 
            ||
| 307 | ,'fk_product' => $obj->fk_product  | 
            ||
| 308 | ,'fk_entrepot' => $obj->fk_entrepot  | 
            ||
| 309 | ,'seuil_stock_alerte' => $obj->seuil_stock_alerte  | 
            ||
| 310 | ,'desiredstock' => $obj->desiredstock  | 
            ||
| 311 | );  | 
            ||
| 312 | }  | 
            ||
| 313 | $this->db->free($resql);  | 
            ||
| 314 | |||
| 315 | return $lines;  | 
            ||
| 316 |         } else { | 
            ||
| 317 | $this->errors[] = 'Error ' . $this->db->lasterror();  | 
            ||
| 318 |             dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR); | 
            ||
| 319 | |||
| 320 | return -1;  | 
            ||
| 321 | }  | 
            ||
| 322 | }  | 
            ||
| 323 | |||
| 324 | /**  | 
            ||
| 325 | * Update object into database  | 
            ||
| 326 | *  | 
            ||
| 327 | * @param User $user User that modifies  | 
            ||
| 328 | * @param int $notrigger 0=launch triggers after, 1=disable triggers  | 
            ||
| 329 | * @return int Return integer <0 if KO, >0 if OK  | 
            ||
| 330 | */  | 
            ||
| 331 | public function update(User $user, $notrigger = 0)  | 
            ||
| 332 |     { | 
            ||
| 333 | $error = 0;  | 
            ||
| 334 | |||
| 335 | dol_syslog(__METHOD__, LOG_DEBUG);  | 
            ||
| 336 | |||
| 337 | // Clean parameters  | 
            ||
| 338 | |||
| 339 |         if (isset($this->fk_product)) { | 
            ||
| 340 | $this->fk_product = (int) $this->fk_product;  | 
            ||
| 341 | }  | 
            ||
| 342 |         if (isset($this->fk_entrepot)) { | 
            ||
| 343 | $this->fk_entrepot = (int) $this->fk_entrepot;  | 
            ||
| 344 | }  | 
            ||
| 345 |         if (isset($this->seuil_stock_alerte)) { | 
            ||
| 346 | $this->seuil_stock_alerte = trim($this->seuil_stock_alerte);  | 
            ||
| 347 | }  | 
            ||
| 348 |         if (isset($this->desiredstock)) { | 
            ||
| 349 | $this->desiredstock = trim($this->desiredstock);  | 
            ||
| 350 | }  | 
            ||
| 351 |         if (isset($this->import_key)) { | 
            ||
| 352 | $this->import_key = trim($this->import_key);  | 
            ||
| 353 | }  | 
            ||
| 354 | |||
| 355 | |||
| 356 | // Check parameters  | 
            ||
| 357 | // Put here code to add a control on parameters values  | 
            ||
| 358 | |||
| 359 | // Update request  | 
            ||
| 360 | $sql = 'UPDATE ' . $this->db->prefix() . $this->table_element . ' SET';  | 
            ||
| 361 | |||
| 362 | $sql .= ' tms = ' . (dol_strlen($this->tms) != 0 ? "'" . $this->db->idate($this->tms) . "'" : "'" . $this->db->idate(dol_now()) . "'") . ',';  | 
            ||
| 363 | $sql .= ' fk_product = ' . (isset($this->fk_product) ? $this->fk_product : "null") . ',';  | 
            ||
| 364 | $sql .= ' fk_entrepot = ' . (isset($this->fk_entrepot) ? $this->fk_entrepot : "null") . ',';  | 
            ||
| 365 | $sql .= ' seuil_stock_alerte = ' . (isset($this->seuil_stock_alerte) ? $this->seuil_stock_alerte : "null") . ',';  | 
            ||
| 366 | $sql .= ' desiredstock = ' . (isset($this->desiredstock) ? $this->desiredstock : "null") . ',';  | 
            ||
| 367 | $sql .= ' import_key = ' . (isset($this->import_key) ? "'" . $this->db->escape($this->import_key) . "'" : "null");  | 
            ||
| 368 | |||
| 369 | |||
| 370 | $sql .= ' WHERE rowid=' . ((int) $this->id);  | 
            ||
| 371 | |||
| 372 | $this->db->begin();  | 
            ||
| 373 | |||
| 374 | $resql = $this->db->query($sql);  | 
            ||
| 375 |         if (!$resql) { | 
            ||
| 376 | $error++;  | 
            ||
| 377 | $this->errors[] = 'Error ' . $this->db->lasterror();  | 
            ||
| 378 |             dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR); | 
            ||
| 379 | }  | 
            ||
| 380 | |||
| 381 |         //if (!$error && !$notrigger) { | 
            ||
| 382 | // Uncomment this and change MYOBJECT to your own tag if you  | 
            ||
| 383 | // want this action calls a trigger.  | 
            ||
| 384 | |||
| 385 | //// Call triggers  | 
            ||
| 386 |         //$result=$this->call_trigger('MYOBJECT_MODIFY',$user); | 
            ||
| 387 |         //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} | 
            ||
| 388 | //// End call triggers  | 
            ||
| 389 | //}  | 
            ||
| 390 | |||
| 391 | // Commit or rollback  | 
            ||
| 392 |         if ($error) { | 
            ||
| 393 | $this->db->rollback();  | 
            ||
| 394 | |||
| 395 | return -1 * $error;  | 
            ||
| 396 |         } else { | 
            ||
| 397 | $this->db->commit();  | 
            ||
| 398 | |||
| 399 | return 1;  | 
            ||
| 400 | }  | 
            ||
| 401 | }  | 
            ||
| 402 | |||
| 403 | /**  | 
            ||
| 404 | * Delete object in database  | 
            ||
| 405 | *  | 
            ||
| 406 | * @param User $user User that deletes  | 
            ||
| 407 | * @param int $notrigger 0=launch triggers after, 1=disable triggers  | 
            ||
| 408 | * @return int Return integer <0 if KO, >0 if OK  | 
            ||
| 409 | */  | 
            ||
| 410 | public function delete(User $user, $notrigger = 0)  | 
            ||
| 411 |     { | 
            ||
| 412 | dol_syslog(__METHOD__, LOG_DEBUG);  | 
            ||
| 413 | |||
| 414 | $error = 0;  | 
            ||
| 415 | |||
| 416 | $this->db->begin();  | 
            ||
| 417 | |||
| 418 |         //if (!$error && !$notrigger) { | 
            ||
| 419 | // Uncomment this and change MYOBJECT to your own tag if you  | 
            ||
| 420 | // want this action calls a trigger.  | 
            ||
| 421 | |||
| 422 | //// Call triggers  | 
            ||
| 423 |         //$result=$this->call_trigger('MYOBJECT_DELETE',$user); | 
            ||
| 424 |         //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} | 
            ||
| 425 | //// End call triggers  | 
            ||
| 426 | //}  | 
            ||
| 427 | |||
| 428 |         if (!$error) { | 
            ||
| 429 | $sql = 'DELETE FROM ' . $this->db->prefix() . $this->table_element;  | 
            ||
| 430 | $sql .= ' WHERE rowid=' . ((int) $this->id);  | 
            ||
| 431 | |||
| 432 | $resql = $this->db->query($sql);  | 
            ||
| 433 |             if (!$resql) { | 
            ||
| 434 | $error++;  | 
            ||
| 435 | $this->errors[] = 'Error ' . $this->db->lasterror();  | 
            ||
| 436 |                 dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR); | 
            ||
| 437 | }  | 
            ||
| 438 | }  | 
            ||
| 439 | |||
| 440 | // Commit or rollback  | 
            ||
| 441 |         if ($error) { | 
            ||
| 442 | $this->db->rollback();  | 
            ||
| 443 | |||
| 444 | return -1 * $error;  | 
            ||
| 445 |         } else { | 
            ||
| 446 | $this->db->commit();  | 
            ||
| 447 | |||
| 448 | return 1;  | 
            ||
| 449 | }  | 
            ||
| 450 | }  | 
            ||
| 451 | |||
| 452 | /**  | 
            ||
| 453 | * Load an object from its id and create a new one in database  | 
            ||
| 454 | *  | 
            ||
| 455 | * @param User $user User making the clone  | 
            ||
| 456 | * @param int $fromid Id of object to clone  | 
            ||
| 457 | * @return int New id of clone  | 
            ||
| 458 | */  | 
            ||
| 459 | public function createFromClone(User $user, $fromid)  | 
            ||
| 460 |     { | 
            ||
| 461 | dol_syslog(__METHOD__, LOG_DEBUG);  | 
            ||
| 462 | |||
| 463 | $error = 0;  | 
            ||
| 464 | $object = new ProductStockEntrepot($this->db);  | 
            ||
| 465 | |||
| 466 | $this->db->begin();  | 
            ||
| 467 | |||
| 468 | // Load source object  | 
            ||
| 469 | $object->fetch($fromid);  | 
            ||
| 470 | // Reset object  | 
            ||
| 471 | $object->id = 0;  | 
            ||
| 472 | |||
| 473 | // Clear fields  | 
            ||
| 474 | // ...  | 
            ||
| 475 | |||
| 476 | // Create clone  | 
            ||
| 477 | $object->context['createfromclone'] = 'createfromclone';  | 
            ||
| 478 | $result = $object->create($user);  | 
            ||
| 479 | |||
| 480 | // Other options  | 
            ||
| 481 |         if ($result < 0) { | 
            ||
| 482 | $error++;  | 
            ||
| 483 | $this->errors = $object->errors;  | 
            ||
| 484 |             dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR); | 
            ||
| 485 | }  | 
            ||
| 486 | |||
| 487 | unset($object->context['createfromclone']);  | 
            ||
| 488 | |||
| 489 | // End  | 
            ||
| 490 |         if (!$error) { | 
            ||
| 491 | $this->db->commit();  | 
            ||
| 492 | |||
| 493 | return $object->id;  | 
            ||
| 494 |         } else { | 
            ||
| 495 | $this->db->rollback();  | 
            ||
| 496 | |||
| 497 | return -1;  | 
            ||
| 498 | }  | 
            ||
| 499 | }  | 
            ||
| 500 | |||
| 501 | /**  | 
            ||
| 502 | * Return a link to the user card (with optionally the picto)  | 
            ||
| 503 | * Use this->id,this->lastname, this->firstname  | 
            ||
| 504 | *  | 
            ||
| 505 | * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)  | 
            ||
| 506 | * @param string $option On what the link point to  | 
            ||
| 507 | * @param integer $notooltip 1=Disable tooltip  | 
            ||
| 508 | * @param int $maxlen Max length of visible user name  | 
            ||
| 509 | * @param string $morecss Add more css on link  | 
            ||
| 510 | * @return string String with URL  | 
            ||
| 511 | */  | 
            ||
| 512 | public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')  | 
            ||
| 513 |     { | 
            ||
| 514 | global $langs;  | 
            ||
| 515 | |||
| 516 | $result = '';  | 
            ||
| 517 | |||
| 518 |         $label = '<u>' . $langs->trans("MyModule") . '</u>'; | 
            ||
| 519 | $label .= '<div width="100%">';  | 
            ||
| 520 |         $label .= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref; | 
            ||
| 521 | |||
| 522 |         $link = '<a href="' . constant('BASE_URL') . '/ProductEntrepot/card.php?id=' . $this->id . '"'; | 
            ||
| 523 | $link .= ($notooltip ? '' : ' title="' . dol_escape_htmltag($label, 1) . '" class="classfortooltip' . ($morecss ? ' ' . $morecss : '') . '"');  | 
            ||
| 524 | $link .= '>';  | 
            ||
| 525 | $linkend = '</a>';  | 
            ||
| 526 | |||
| 527 |         if ($withpicto) { | 
            ||
| 528 | $result .= ($link . img_object(($notooltip ? '' : $label), 'label', ($notooltip ? '' : 'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1) . $linkend);  | 
            ||
| 529 |             if ($withpicto != 2) { | 
            ||
| 530 | $result .= ' ';  | 
            ||
| 531 | }  | 
            ||
| 532 | }  | 
            ||
| 533 | $result .= $link . $this->ref . $linkend;  | 
            ||
| 534 | |||
| 535 | return $result;  | 
            ||
| 536 | }  | 
            ||
| 537 | |||
| 538 | /**  | 
            ||
| 539 | * Return the label of the status  | 
            ||
| 540 | *  | 
            ||
| 541 | * @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  | 
            ||
| 542 | * @return string Label of status  | 
            ||
| 543 | */  | 
            ||
| 544 | public function getLibStatut($mode = 0)  | 
            ||
| 545 |     { | 
            ||
| 546 | return $this->LibStatut($this->status, $mode);  | 
            ||
| 547 | }  | 
            ||
| 548 | |||
| 549 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps  | 
            ||
| 550 | /**  | 
            ||
| 551 | * Renvoi le libelle d'un status donne  | 
            ||
| 552 | *  | 
            ||
| 553 | * @param int $status Id status  | 
            ||
| 554 | * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto  | 
            ||
| 555 | * @return string Label of status  | 
            ||
| 556 | */  | 
            ||
| 557 | public function LibStatut($status, $mode = 0)  | 
            ||
| 558 |     { | 
            ||
| 559 | // phpcs:enable  | 
            ||
| 560 | global $langs;  | 
            ||
| 561 | |||
| 562 |         if ($mode == 0) { | 
            ||
| 563 |             if ($status == 1) { | 
            ||
| 564 |                 return $langs->trans('Enabled'); | 
            ||
| 565 |             } elseif ($status == 0) { | 
            ||
| 566 |                 return $langs->trans('Disabled'); | 
            ||
| 567 | }  | 
            ||
| 568 |         } elseif ($mode == 1) { | 
            ||
| 569 |             if ($status == 1) { | 
            ||
| 570 |                 return $langs->trans('Enabled'); | 
            ||
| 571 |             } elseif ($status == 0) { | 
            ||
| 572 |                 return $langs->trans('Disabled'); | 
            ||
| 573 | }  | 
            ||
| 574 |         } elseif ($mode == 2) { | 
            ||
| 575 |             if ($status == 1) { | 
            ||
| 576 |                 return img_picto($langs->trans('Enabled'), 'statut4') . ' ' . $langs->trans('Enabled'); | 
            ||
| 577 |             } elseif ($status == 0) { | 
            ||
| 578 |                 return img_picto($langs->trans('Disabled'), 'statut5') . ' ' . $langs->trans('Disabled'); | 
            ||
| 579 | }  | 
            ||
| 580 |         } elseif ($mode == 3) { | 
            ||
| 581 |             if ($status == 1) { | 
            ||
| 582 |                 return img_picto($langs->trans('Enabled'), 'statut4'); | 
            ||
| 583 |             } elseif ($status == 0) { | 
            ||
| 584 |                 return img_picto($langs->trans('Disabled'), 'statut5'); | 
            ||
| 585 | }  | 
            ||
| 586 |         } elseif ($mode == 4) { | 
            ||
| 587 |             if ($status == 1) { | 
            ||
| 588 |                 return img_picto($langs->trans('Enabled'), 'statut4') . ' ' . $langs->trans('Enabled'); | 
            ||
| 589 |             } elseif ($status == 0) { | 
            ||
| 590 |                 return img_picto($langs->trans('Disabled'), 'statut5') . ' ' . $langs->trans('Disabled'); | 
            ||
| 591 | }  | 
            ||
| 592 |         } elseif ($mode == 5) { | 
            ||
| 593 |             if ($status == 1) { | 
            ||
| 594 |                 return $langs->trans('Enabled') . ' ' . img_picto($langs->trans('Enabled'), 'statut4'); | 
            ||
| 595 |             } elseif ($status == 0) { | 
            ||
| 596 |                 return $langs->trans('Disabled') . ' ' . img_picto($langs->trans('Disabled'), 'statut5'); | 
            ||
| 597 | }  | 
            ||
| 598 | }  | 
            ||
| 599 | |||
| 600 | return '';  | 
            ||
| 601 | }  | 
            ||
| 602 | |||
| 603 | |||
| 604 | /**  | 
            ||
| 605 | * Initialise object with example values  | 
            ||
| 606 | * Id must be 0 if object instance is a specimen  | 
            ||
| 607 | *  | 
            ||
| 608 | * @return int  | 
            ||
| 609 | */  | 
            ||
| 610 | public function initAsSpecimen()  | 
            ||
| 622 | }  | 
            ||
| 623 | }  | 
            ||
| 624 |