| Conditions | 27 |
| Paths | 16425 |
| Total Lines | 288 |
| Code Lines | 113 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 208 | public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0) |
||
| 209 | { |
||
| 210 | // phpcs:enable |
||
| 211 | global $user, $langs, $conf, $mysoc, $hookmanager; |
||
| 212 | |||
| 213 | if (empty($srctemplatepath)) { |
||
| 214 | dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING); |
||
| 215 | return -1; |
||
| 216 | } |
||
| 217 | |||
| 218 | // Add odtgeneration hook |
||
| 219 | if (!is_object($hookmanager)) { |
||
| 220 | include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'; |
||
| 221 | $hookmanager = new HookManager($this->db); |
||
| 222 | } |
||
| 223 | $hookmanager->initHooks(array('odtgeneration')); |
||
| 224 | global $action; |
||
| 225 | |||
| 226 | if (!is_object($outputlangs)) { |
||
| 227 | $outputlangs = $langs; |
||
| 228 | } |
||
| 229 | $sav_charset_output = $outputlangs->charset_output; |
||
| 230 | $outputlangs->charset_output = 'UTF-8'; |
||
| 231 | |||
| 232 | $outputlangs->loadLangs(array("main", "dict", "companies", "bills")); |
||
| 233 | |||
| 234 | if ($conf->mrp->dir_output) { |
||
| 235 | // If $object is id instead of object |
||
| 236 | if (!is_object($object)) { |
||
| 237 | $id = $object; |
||
| 238 | $object = new MO($this->db); |
||
| 239 | $result = $object->fetch($id); |
||
| 240 | if ($result < 0) { |
||
| 241 | dol_print_error($this->db, $object->error); |
||
| 242 | return -1; |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | $object->fetch_thirdparty(); |
||
| 247 | |||
| 248 | $dir = $conf->mrp->multidir_output[isset($object->entity) ? $object->entity : 1]; |
||
| 249 | $objectref = dol_sanitizeFileName($object->ref); |
||
| 250 | if (!preg_match('/specimen/i', $objectref)) { |
||
| 251 | $dir .= "/".$objectref; |
||
| 252 | } |
||
| 253 | $file = $dir."/".$objectref.".odt"; |
||
| 254 | |||
| 255 | if (!file_exists($dir)) { |
||
| 256 | if (dol_mkdir($dir) < 0) { |
||
| 257 | $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir); |
||
| 258 | return -1; |
||
| 259 | } |
||
| 260 | } |
||
| 261 | |||
| 262 | if (file_exists($dir)) { |
||
| 263 | //print "srctemplatepath=".$srctemplatepath; // Src filename |
||
| 264 | $newfile = basename($srctemplatepath); |
||
| 265 | $newfiletmp = preg_replace('/\.od(t|s)/i', '', $newfile); |
||
| 266 | $newfiletmp = preg_replace('/template_/i', '', $newfiletmp); |
||
| 267 | $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp); |
||
| 268 | $newfiletmp = $objectref.'_'.$newfiletmp; |
||
| 269 | //$file=$dir.'/'.$newfiletmp.'.'.dol_print_date(dol_now(),'%Y%m%d%H%M%S').'.odt'; |
||
| 270 | // Get extension (ods or odt) |
||
| 271 | $newfileformat = substr($newfile, strrpos($newfile, '.') + 1); |
||
| 272 | if (!empty($conf->global->MAIN_DOC_USE_TIMING)) { |
||
| 273 | $format = $conf->global->MAIN_DOC_USE_TIMING; |
||
| 274 | if ($format == '1') { |
||
| 275 | $format = '%Y%m%d%H%M%S'; |
||
| 276 | } |
||
| 277 | $filename = $newfiletmp.'-'.dol_print_date(dol_now(), $format).'.'.$newfileformat; |
||
| 278 | } else { |
||
| 279 | $filename = $newfiletmp.'.'.$newfileformat; |
||
| 280 | } |
||
| 281 | $file = $dir.'/'.$filename; |
||
| 282 | //print "newdir=".$dir; |
||
| 283 | //print "newfile=".$newfile; |
||
| 284 | //print "file=".$file; |
||
| 285 | //print "conf->societe->dir_temp=".$conf->societe->dir_temp; |
||
| 286 | |||
| 287 | dol_mkdir($conf->mrp->dir_temp); |
||
| 288 | if (!is_writable($conf->mrp->dir_temp)) { |
||
| 289 | $this->error = "Failed to write in temp directory ".$conf->mrp->dir_temp; |
||
| 290 | dol_syslog('Error in write_file: '.$this->error, LOG_ERR); |
||
| 291 | return -1; |
||
| 292 | } |
||
| 293 | |||
| 294 | // If CUSTOMER contact defined on order, we use it |
||
| 295 | $usecontact = false; |
||
| 296 | $arrayidcontact = $object->getIdContact('external', 'CUSTOMER'); |
||
| 297 | if (count($arrayidcontact) > 0) { |
||
| 298 | $usecontact = true; |
||
| 299 | $result = $object->fetch_contact($arrayidcontact[0]); |
||
| 300 | } |
||
| 301 | |||
| 302 | // Recipient name |
||
| 303 | $contactobject = null; |
||
| 304 | if (!empty($usecontact)) { |
||
| 305 | if ($usecontact && ($object->contact->fk_soc != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) || !empty($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT)))) { |
||
| 306 | $socobject = $object->contact; |
||
| 307 | } else { |
||
| 308 | $socobject = $object->thirdparty; |
||
| 309 | // if we have a CUSTOMER contact and we dont use it as recipient we store the contact object for later use |
||
| 310 | $contactobject = $object->contact; |
||
| 311 | } |
||
| 312 | } else { |
||
| 313 | $socobject = $object->thirdparty; |
||
| 314 | } |
||
| 315 | |||
| 316 | // Make substitution |
||
| 317 | $substitutionarray = array( |
||
| 318 | '__QTY_TO_PRODUCE__' => $object->qty, |
||
| 319 | ); |
||
| 320 | complete_substitutions_array($substitutionarray, $langs, $object); |
||
| 321 | // Call the ODTSubstitution hook |
||
| 322 | $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$substitutionarray); |
||
| 323 | $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 324 | |||
| 325 | // Line of free text |
||
| 326 | $newfreetext = ''; |
||
| 327 | $paramfreetext = 'MRP_MO_FREE_TEXT'; |
||
| 328 | if (!empty($conf->global->$paramfreetext)) { |
||
| 329 | $newfreetext = make_substitutions($conf->global->$paramfreetext, $substitutionarray); |
||
| 330 | } |
||
| 331 | |||
| 332 | // Open and load template |
||
| 333 | require_once ODTPHP_PATH.'odf.php'; |
||
| 334 | try { |
||
| 335 | $odfHandler = new odf( |
||
| 336 | $srctemplatepath, |
||
| 337 | array( |
||
| 338 | 'PATH_TO_TMP' => $conf->mrp->dir_temp, |
||
| 339 | 'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy. |
||
| 340 | 'DELIMITER_LEFT' => '{', |
||
| 341 | 'DELIMITER_RIGHT' => '}' |
||
| 342 | ) |
||
| 343 | ); |
||
| 344 | } catch (Exception $e) { |
||
| 345 | $this->error = $e->getMessage(); |
||
| 346 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 347 | return -1; |
||
| 348 | } |
||
| 349 | |||
| 350 | // After construction $odfHandler->contentXml contains content and |
||
| 351 | // [!-- BEGIN row.lines --]*[!-- END row.lines --] has been replaced by |
||
| 352 | // [!-- BEGIN lines --]*[!-- END lines --] |
||
| 353 | //print html_entity_decode($odfHandler->__toString()); |
||
| 354 | //print exit; |
||
| 355 | /* |
||
| 356 | |||
| 357 | |||
| 358 | // Make substitutions into odt of freetext |
||
| 359 | try { |
||
| 360 | $odfHandler->setVars('free_text', $newfreetext, true, 'UTF-8'); |
||
| 361 | } catch (OdfException $e) { |
||
| 362 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 363 | } |
||
| 364 | |||
| 365 | // Define substitution array |
||
| 366 | $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object); |
||
| 367 | $array_object_from_properties = $this->get_substitutionarray_each_var_object($object, $outputlangs); |
||
| 368 | $array_objet = $this->get_substitutionarray_object($object, $outputlangs); |
||
| 369 | $array_user = $this->get_substitutionarray_user($user, $outputlangs); |
||
| 370 | $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs); |
||
| 371 | $array_thirdparty = $this->get_substitutionarray_thirdparty($socobject, $outputlangs); |
||
| 372 | $array_other = $this->get_substitutionarray_other($outputlangs); |
||
| 373 | // retrieve contact information for use in object as contact_xxx tags |
||
| 374 | $array_thirdparty_contact = array(); |
||
| 375 | if ($usecontact && is_object($contactobject)) { |
||
| 376 | $array_thirdparty_contact = $this->get_substitutionarray_contact($contactobject, $outputlangs, 'contact'); |
||
| 377 | } |
||
| 378 | |||
| 379 | $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact); |
||
| 380 | complete_substitutions_array($tmparray, $outputlangs, $object); |
||
| 381 | |||
| 382 | // Call the ODTSubstitution hook |
||
| 383 | $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray); |
||
| 384 | $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 385 | |||
| 386 | foreach ($tmparray as $key => $value) { |
||
| 387 | try { |
||
| 388 | if (preg_match('/logo$/', $key)) { |
||
| 389 | // Image |
||
| 390 | if (file_exists($value)) { |
||
| 391 | $odfHandler->setImage($key, $value); |
||
| 392 | } else { |
||
| 393 | $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8'); |
||
| 394 | } |
||
| 395 | } else { |
||
| 396 | // Text |
||
| 397 | $odfHandler->setVars($key, $value, true, 'UTF-8'); |
||
| 398 | } |
||
| 399 | } catch (OdfException $e) { |
||
| 400 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 401 | } |
||
| 402 | } |
||
| 403 | |||
| 404 | // Replace tags of lines |
||
| 405 | try { |
||
| 406 | $foundtagforlines = 1; |
||
| 407 | try { |
||
| 408 | $listlines = $odfHandler->setSegment('lines'); |
||
| 409 | } catch (OdfException $e) { |
||
| 410 | // We may arrive here if tags for lines not present into template |
||
| 411 | $foundtagforlines = 0; |
||
| 412 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 413 | } |
||
| 414 | |||
| 415 | if ($foundtagforlines) { |
||
| 416 | $linenumber = 0; |
||
| 417 | foreach ($object->lines as $line) { |
||
| 418 | $linenumber++; |
||
| 419 | $tmparray = $this->get_substitutionarray_lines($line, $outputlangs, $linenumber); |
||
| 420 | complete_substitutions_array($tmparray, $outputlangs, $object, $line, "completesubstitutionarray_lines"); |
||
| 421 | // Call the ODTSubstitutionLine hook |
||
| 422 | $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray, 'line'=>$line); |
||
| 423 | $reshook = $hookmanager->executeHooks('ODTSubstitutionLine', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 424 | foreach ($tmparray as $key => $val) { |
||
| 425 | try { |
||
| 426 | $listlines->setVars($key, $val, true, 'UTF-8'); |
||
| 427 | } catch (OdfException $e) { |
||
| 428 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 429 | } catch (SegmentException $e) { |
||
| 430 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 431 | } |
||
| 432 | } |
||
| 433 | $listlines->merge(); |
||
| 434 | } |
||
| 435 | $odfHandler->mergeSegment($listlines); |
||
| 436 | } |
||
| 437 | } catch (OdfException $e) { |
||
| 438 | $this->error = $e->getMessage(); |
||
| 439 | dol_syslog($this->error, LOG_WARNING); |
||
| 440 | return -1; |
||
| 441 | } |
||
| 442 | |||
| 443 | // Replace labels translated |
||
| 444 | $tmparray = $outputlangs->get_translations_for_substitutions(); |
||
| 445 | foreach ($tmparray as $key => $value) { |
||
| 446 | try { |
||
| 447 | $odfHandler->setVars($key, $value, true, 'UTF-8'); |
||
| 448 | } catch (OdfException $e) { |
||
| 449 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 450 | } |
||
| 451 | } |
||
| 452 | |||
| 453 | // Call the beforeODTSave hook |
||
| 454 | |||
| 455 | $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray); |
||
| 456 | $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 457 | */ |
||
| 458 | // Write new file |
||
| 459 | if (!empty($conf->global->MAIN_ODT_AS_PDF)) { |
||
| 460 | try { |
||
| 461 | $odfHandler->exportAsAttachedPDF($file); |
||
| 462 | } catch (Exception $e) { |
||
| 463 | $this->error = $e->getMessage(); |
||
| 464 | dol_syslog('Error in exportAsAttachedPDF: '.$e->getMessage(), LOG_INFO); |
||
| 465 | return -1; |
||
| 466 | } |
||
| 467 | } else { |
||
| 468 | try { |
||
| 469 | $odfHandler->saveToDisk($file); |
||
| 470 | } catch (Exception $e) { |
||
| 471 | $this->error = $e->getMessage(); |
||
| 472 | dol_syslog('Error in saveToDisk: '.$e->getMessage(), LOG_INFO); |
||
| 473 | return -1; |
||
| 474 | } |
||
| 475 | } |
||
| 476 | |||
| 477 | $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray); |
||
|
|
|||
| 478 | $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 479 | |||
| 480 | if (!empty($conf->global->MAIN_UMASK)) { |
||
| 481 | @chmod($file, octdec($conf->global->MAIN_UMASK)); |
||
| 482 | } |
||
| 483 | |||
| 484 | $odfHandler = null; // Destroy object |
||
| 485 | |||
| 486 | $this->result = array('fullpath'=>$file); |
||
| 487 | |||
| 488 | return 1; // Success |
||
| 489 | } else { |
||
| 490 | $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir); |
||
| 491 | return -1; |
||
| 492 | } |
||
| 493 | } |
||
| 494 | |||
| 495 | return -1; |
||
| 496 | } |
||
| 498 |